blob: 0e363f737544b1da22a2164851ca5b6c1145f4c5 [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",
71 "unknown", // CTRL_X_SCROLL
72 "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{
2542 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
2543 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2544
2545 return (char_u *)"";
2546}
2547
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002548 static void
2549ins_compl_update_sequence_numbers()
2550{
2551 int number = 0;
2552 compl_T *match;
2553
2554 if (compl_direction == FORWARD)
2555 {
2556 // search backwards for the first valid (!= -1) number.
2557 // This should normally succeed already at the first loop
2558 // cycle, so it's fast!
2559 for (match = compl_curr_match->cp_prev; match != NULL
2560 && match != compl_first_match;
2561 match = match->cp_prev)
2562 if (match->cp_number != -1)
2563 {
2564 number = match->cp_number;
2565 break;
2566 }
2567 if (match != NULL)
2568 // go up and assign all numbers which are not assigned
2569 // yet
2570 for (match = match->cp_next;
2571 match != NULL && match->cp_number == -1;
2572 match = match->cp_next)
2573 match->cp_number = ++number;
2574 }
2575 else // BACKWARD
2576 {
2577 // search forwards (upwards) for the first valid (!= -1)
2578 // number. This should normally succeed already at the
2579 // first loop cycle, so it's fast!
2580 for (match = compl_curr_match->cp_next; match != NULL
2581 && match != compl_first_match;
2582 match = match->cp_next)
2583 if (match->cp_number != -1)
2584 {
2585 number = match->cp_number;
2586 break;
2587 }
2588 if (match != NULL)
2589 // go down and assign all numbers which are not
2590 // assigned yet
2591 for (match = match->cp_prev; match
2592 && match->cp_number == -1;
2593 match = match->cp_prev)
2594 match->cp_number = ++number;
2595 }
2596}
2597
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002598/*
2599 * Get complete information
2600 */
2601 static void
2602get_complete_info(list_T *what_list, dict_T *retdict)
2603{
2604 int ret = OK;
2605 listitem_T *item;
2606#define CI_WHAT_MODE 0x01
2607#define CI_WHAT_PUM_VISIBLE 0x02
2608#define CI_WHAT_ITEMS 0x04
2609#define CI_WHAT_SELECTED 0x08
2610#define CI_WHAT_INSERTED 0x10
2611#define CI_WHAT_ALL 0xff
2612 int what_flag;
2613
2614 if (what_list == NULL)
2615 what_flag = CI_WHAT_ALL;
2616 else
2617 {
2618 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002619 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002620 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002621 {
2622 char_u *what = tv_get_string(&item->li_tv);
2623
2624 if (STRCMP(what, "mode") == 0)
2625 what_flag |= CI_WHAT_MODE;
2626 else if (STRCMP(what, "pum_visible") == 0)
2627 what_flag |= CI_WHAT_PUM_VISIBLE;
2628 else if (STRCMP(what, "items") == 0)
2629 what_flag |= CI_WHAT_ITEMS;
2630 else if (STRCMP(what, "selected") == 0)
2631 what_flag |= CI_WHAT_SELECTED;
2632 else if (STRCMP(what, "inserted") == 0)
2633 what_flag |= CI_WHAT_INSERTED;
2634 }
2635 }
2636
2637 if (ret == OK && (what_flag & CI_WHAT_MODE))
2638 ret = dict_add_string(retdict, "mode", ins_compl_mode());
2639
2640 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
2641 ret = dict_add_number(retdict, "pum_visible", pum_visible());
2642
2643 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
2644 {
2645 list_T *li;
2646 dict_T *di;
2647 compl_T *match;
2648
2649 li = list_alloc();
2650 if (li == NULL)
2651 return;
2652 ret = dict_add_list(retdict, "items", li);
2653 if (ret == OK && compl_first_match != NULL)
2654 {
2655 match = compl_first_match;
2656 do
2657 {
2658 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
2659 {
2660 di = dict_alloc();
2661 if (di == NULL)
2662 return;
2663 ret = list_append_dict(li, di);
2664 if (ret != OK)
2665 return;
2666 dict_add_string(di, "word", match->cp_str);
2667 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
2668 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
2669 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
2670 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002671 if (match->cp_user_data.v_type == VAR_UNKNOWN)
2672 // Add an empty string for backwards compatibility
2673 dict_add_string(di, "user_data", (char_u *)"");
2674 else
2675 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002676 }
2677 match = match->cp_next;
2678 }
2679 while (match != NULL && match != compl_first_match);
2680 }
2681 }
2682
2683 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002684 {
2685 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
2686 ins_compl_update_sequence_numbers();
2687 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
2688 ? compl_curr_match->cp_number - 1 : -1);
2689 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002690
2691 // TODO
2692 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
2693}
2694
2695/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002696 * "complete_info()" function
2697 */
2698 void
2699f_complete_info(typval_T *argvars, typval_T *rettv)
2700{
2701 list_T *what_list = NULL;
2702
2703 if (rettv_dict_alloc(rettv) != OK)
2704 return;
2705
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002706 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
2707 return;
2708
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002709 if (argvars[0].v_type != VAR_UNKNOWN)
2710 {
2711 if (argvars[0].v_type != VAR_LIST)
2712 {
2713 emsg(_(e_listreq));
2714 return;
2715 }
2716 what_list = argvars[0].vval.v_list;
2717 }
2718 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002719}
2720#endif
2721
2722/*
2723 * Get the next expansion(s), using "compl_pattern".
2724 * The search starts at position "ini" in curbuf and in the direction
2725 * compl_direction.
2726 * When "compl_started" is FALSE start at that position, otherwise continue
2727 * where we stopped searching before.
2728 * This may return before finding all the matches.
2729 * Return the total number of matches or -1 if still unknown -- Acevedo
2730 */
2731 static int
2732ins_compl_get_exp(pos_T *ini)
2733{
2734 static pos_T first_match_pos;
2735 static pos_T last_match_pos;
2736 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2737 static int found_all = FALSE; // Found all matches of a
2738 // certain type.
2739 static buf_T *ins_buf = NULL; // buffer being scanned
2740
2741 pos_T *pos;
2742 char_u **matches;
2743 int save_p_scs;
2744 int save_p_ws;
2745 int save_p_ic;
2746 int i;
2747 int num_matches;
2748 int len;
2749 int found_new_match;
2750 int type = ctrl_x_mode;
2751 char_u *ptr;
2752 char_u *dict = NULL;
2753 int dict_f = 0;
2754 int set_match_pos;
Andy Gozas6a230c62021-08-05 16:23:27 +02002755 pos_T prev_pos = {0, 0, 0};
2756 int looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002757
2758 if (!compl_started)
2759 {
2760 FOR_ALL_BUFFERS(ins_buf)
2761 ins_buf->b_scanned = 0;
2762 found_all = FALSE;
2763 ins_buf = curbuf;
2764 e_cpt = (compl_cont_status & CONT_LOCAL)
2765 ? (char_u *)"." : curbuf->b_p_cpt;
2766 last_match_pos = first_match_pos = *ini;
2767 }
2768 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2769 ins_buf = curbuf; // In case the buffer was wiped out.
2770
2771 compl_old_match = compl_curr_match; // remember the last current match
2772 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2773
2774 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2775 for (;;)
2776 {
2777 found_new_match = FAIL;
2778 set_match_pos = FALSE;
2779
2780 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2781 // or if found_all says this entry is done. For ^X^L only use the
2782 // entries from 'complete' that look in loaded buffers.
2783 if ((ctrl_x_mode == CTRL_X_NORMAL
2784 || ctrl_x_mode_line_or_eval())
2785 && (!compl_started || found_all))
2786 {
2787 found_all = FALSE;
2788 while (*e_cpt == ',' || *e_cpt == ' ')
2789 e_cpt++;
2790 if (*e_cpt == '.' && !curbuf->b_scanned)
2791 {
2792 ins_buf = curbuf;
2793 first_match_pos = *ini;
2794 // Move the cursor back one character so that ^N can match the
2795 // word immediately after the cursor.
2796 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2797 {
2798 // Move the cursor to after the last character in the
2799 // buffer, so that word at start of buffer is found
2800 // correctly.
2801 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2802 first_match_pos.col =
2803 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2804 }
2805 last_match_pos = first_match_pos;
2806 type = 0;
2807
2808 // Remember the first match so that the loop stops when we
2809 // wrap and come back there a second time.
2810 set_match_pos = TRUE;
2811 }
2812 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2813 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2814 {
2815 // Scan a buffer, but not the current one.
2816 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2817 {
2818 compl_started = TRUE;
2819 first_match_pos.col = last_match_pos.col = 0;
2820 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2821 last_match_pos.lnum = 0;
2822 type = 0;
2823 }
2824 else // unloaded buffer, scan like dictionary
2825 {
2826 found_all = TRUE;
2827 if (ins_buf->b_fname == NULL)
2828 continue;
2829 type = CTRL_X_DICTIONARY;
2830 dict = ins_buf->b_fname;
2831 dict_f = DICT_EXACT;
2832 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01002833 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002834 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2835 ins_buf->b_fname == NULL
2836 ? buf_spname(ins_buf)
2837 : ins_buf->b_sfname == NULL
2838 ? ins_buf->b_fname
2839 : ins_buf->b_sfname);
2840 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2841 }
2842 else if (*e_cpt == NUL)
2843 break;
2844 else
2845 {
2846 if (ctrl_x_mode_line_or_eval())
2847 type = -1;
2848 else if (*e_cpt == 'k' || *e_cpt == 's')
2849 {
2850 if (*e_cpt == 'k')
2851 type = CTRL_X_DICTIONARY;
2852 else
2853 type = CTRL_X_THESAURUS;
2854 if (*++e_cpt != ',' && *e_cpt != NUL)
2855 {
2856 dict = e_cpt;
2857 dict_f = DICT_FIRST;
2858 }
2859 }
2860#ifdef FEAT_FIND_ID
2861 else if (*e_cpt == 'i')
2862 type = CTRL_X_PATH_PATTERNS;
2863 else if (*e_cpt == 'd')
2864 type = CTRL_X_PATH_DEFINES;
2865#endif
2866 else if (*e_cpt == ']' || *e_cpt == 't')
2867 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01002868 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002869 type = CTRL_X_TAGS;
2870 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2871 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2872 }
2873 else
2874 type = -1;
2875
2876 // in any case e_cpt is advanced to the next entry
2877 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2878
2879 found_all = TRUE;
2880 if (type == -1)
2881 continue;
2882 }
2883 }
2884
2885 // If complete() was called then compl_pattern has been reset. The
2886 // following won't work then, bail out.
2887 if (compl_pattern == NULL)
2888 break;
2889
2890 switch (type)
2891 {
2892 case -1:
2893 break;
2894#ifdef FEAT_FIND_ID
2895 case CTRL_X_PATH_PATTERNS:
2896 case CTRL_X_PATH_DEFINES:
2897 find_pattern_in_path(compl_pattern, compl_direction,
2898 (int)STRLEN(compl_pattern), FALSE, FALSE,
2899 (type == CTRL_X_PATH_DEFINES
2900 && !(compl_cont_status & CONT_SOL))
2901 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2902 (linenr_T)1, (linenr_T)MAXLNUM);
2903 break;
2904#endif
2905
2906 case CTRL_X_DICTIONARY:
2907 case CTRL_X_THESAURUS:
2908 ins_compl_dictionaries(
2909 dict != NULL ? dict
2910 : (type == CTRL_X_THESAURUS
2911 ? (*curbuf->b_p_tsr == NUL
2912 ? p_tsr
2913 : curbuf->b_p_tsr)
2914 : (*curbuf->b_p_dict == NUL
2915 ? p_dict
2916 : curbuf->b_p_dict)),
2917 compl_pattern,
2918 dict != NULL ? dict_f
2919 : 0, type == CTRL_X_THESAURUS);
2920 dict = NULL;
2921 break;
2922
2923 case CTRL_X_TAGS:
2924 // set p_ic according to p_ic, p_scs and pat for find_tags().
2925 save_p_ic = p_ic;
2926 p_ic = ignorecase(compl_pattern);
2927
2928 // Find up to TAG_MANY matches. Avoids that an enormous number
2929 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002930 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002931 if (find_tags(compl_pattern, &num_matches, &matches,
2932 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2933 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2934 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002935 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002936 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002937 p_ic = save_p_ic;
2938 break;
2939
2940 case CTRL_X_FILES:
2941 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2942 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2943 {
2944
2945 // May change home directory back to "~".
2946 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaarac3150d2019-07-28 16:36:39 +02002947#ifdef BACKSLASH_IN_FILENAME
2948 if (curbuf->b_p_csl[0] != NUL)
2949 {
2950 int i;
2951
2952 for (i = 0; i < num_matches; ++i)
2953 {
2954 char_u *ptr = matches[i];
2955
2956 while (*ptr != NUL)
2957 {
2958 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
2959 *ptr = '/';
2960 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
2961 *ptr = '\\';
2962 ptr += (*mb_ptr2len)(ptr);
2963 }
2964 }
2965 }
2966#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002967 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2968 }
2969 break;
2970
2971 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02002972 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002973 if (expand_cmdline(&compl_xp, compl_pattern,
2974 (int)STRLEN(compl_pattern),
2975 &num_matches, &matches) == EXPAND_OK)
2976 ins_compl_add_matches(num_matches, matches, FALSE);
2977 break;
2978
2979#ifdef FEAT_COMPL_FUNC
2980 case CTRL_X_FUNCTION:
2981 case CTRL_X_OMNI:
2982 expand_by_function(type, compl_pattern);
2983 break;
2984#endif
2985
2986 case CTRL_X_SPELL:
2987#ifdef FEAT_SPELL
2988 num_matches = expand_spelling(first_match_pos.lnum,
2989 compl_pattern, &matches);
2990 if (num_matches > 0)
2991 ins_compl_add_matches(num_matches, matches, p_ic);
2992#endif
2993 break;
2994
2995 default: // normal ^P/^N and ^X^L
2996 // If 'infercase' is set, don't use 'smartcase' here
2997 save_p_scs = p_scs;
2998 if (ins_buf->b_p_inf)
2999 p_scs = FALSE;
3000
3001 // Buffers other than curbuf are scanned from the beginning or the
3002 // end but never from the middle, thus setting nowrapscan in this
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003003 // buffer is a good idea, on the other hand, we always set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003004 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3005 save_p_ws = p_ws;
3006 if (ins_buf != curbuf)
3007 p_ws = FALSE;
3008 else if (*e_cpt == '.')
3009 p_ws = TRUE;
Andy Gozas6a230c62021-08-05 16:23:27 +02003010 looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003011 for (;;)
3012 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003013 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003014
3015 ++msg_silent; // Don't want messages for wrapscan.
3016
3017 // ctrl_x_mode_line_or_eval() || word-wise search that
3018 // has added a word that was at the beginning of the line
3019 if (ctrl_x_mode_line_or_eval()
3020 || (compl_cont_status & CONT_SOL))
3021 found_new_match = search_for_exact_line(ins_buf, pos,
3022 compl_direction, compl_pattern);
3023 else
3024 found_new_match = searchit(NULL, ins_buf, pos, NULL,
3025 compl_direction,
3026 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003027 RE_LAST, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003028 --msg_silent;
3029 if (!compl_started || set_match_pos)
3030 {
3031 // set "compl_started" even on fail
3032 compl_started = TRUE;
3033 first_match_pos = *pos;
3034 last_match_pos = *pos;
3035 set_match_pos = FALSE;
3036 }
3037 else if (first_match_pos.lnum == last_match_pos.lnum
Andy Gozas6a230c62021-08-05 16:23:27 +02003038 && first_match_pos.col == last_match_pos.col)
3039 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003040 found_new_match = FAIL;
Andy Gozas6a230c62021-08-05 16:23:27 +02003041 }
3042 else if ((compl_direction == FORWARD)
3043 && (prev_pos.lnum > pos->lnum
3044 || (prev_pos.lnum == pos->lnum
3045 && prev_pos.col >= pos->col)))
3046 {
3047 if (looped_around)
3048 found_new_match = FAIL;
3049 else
3050 looped_around = TRUE;
3051 }
3052 else if ((compl_direction != FORWARD)
3053 && (prev_pos.lnum < pos->lnum
3054 || (prev_pos.lnum == pos->lnum
3055 && prev_pos.col <= pos->col)))
3056 {
3057 if (looped_around)
3058 found_new_match = FAIL;
3059 else
3060 looped_around = TRUE;
3061 }
3062 prev_pos = *pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003063 if (found_new_match == FAIL)
3064 {
3065 if (ins_buf == curbuf)
3066 found_all = TRUE;
3067 break;
3068 }
3069
3070 // when ADDING, the text before the cursor matches, skip it
3071 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
3072 && ini->lnum == pos->lnum
3073 && ini->col == pos->col)
3074 continue;
3075 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3076 if (ctrl_x_mode_line_or_eval())
3077 {
3078 if (compl_cont_status & CONT_ADDING)
3079 {
3080 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3081 continue;
3082 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3083 if (!p_paste)
3084 ptr = skipwhite(ptr);
3085 }
3086 len = (int)STRLEN(ptr);
3087 }
3088 else
3089 {
3090 char_u *tmp_ptr = ptr;
3091
3092 if (compl_cont_status & CONT_ADDING)
3093 {
3094 tmp_ptr += compl_length;
3095 // Skip if already inside a word.
3096 if (vim_iswordp(tmp_ptr))
3097 continue;
3098 // Find start of next word.
3099 tmp_ptr = find_word_start(tmp_ptr);
3100 }
3101 // Find end of this word.
3102 tmp_ptr = find_word_end(tmp_ptr);
3103 len = (int)(tmp_ptr - ptr);
3104
3105 if ((compl_cont_status & CONT_ADDING)
3106 && len == compl_length)
3107 {
3108 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3109 {
3110 // Try next line, if any. the new word will be
3111 // "join" as if the normal command "J" was used.
3112 // IOSIZE is always greater than
3113 // compl_length, so the next STRNCPY always
3114 // works -- Acevedo
3115 STRNCPY(IObuff, ptr, len);
3116 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3117 tmp_ptr = ptr = skipwhite(ptr);
3118 // Find start of next word.
3119 tmp_ptr = find_word_start(tmp_ptr);
3120 // Find end of next word.
3121 tmp_ptr = find_word_end(tmp_ptr);
3122 if (tmp_ptr > ptr)
3123 {
3124 if (*ptr != ')' && IObuff[len - 1] != TAB)
3125 {
3126 if (IObuff[len - 1] != ' ')
3127 IObuff[len++] = ' ';
3128 // IObuf =~ "\k.* ", thus len >= 2
3129 if (p_js
3130 && (IObuff[len - 2] == '.'
3131 || (vim_strchr(p_cpo, CPO_JOINSP)
3132 == NULL
3133 && (IObuff[len - 2] == '?'
3134 || IObuff[len - 2] == '!'))))
3135 IObuff[len++] = ' ';
3136 }
3137 // copy as much as possible of the new word
3138 if (tmp_ptr - ptr >= IOSIZE - len)
3139 tmp_ptr = ptr + IOSIZE - len - 1;
3140 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3141 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003142 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003143 }
3144 IObuff[len] = NUL;
3145 ptr = IObuff;
3146 }
3147 if (len == compl_length)
3148 continue;
3149 }
3150 }
3151 if (ins_compl_add_infercase(ptr, len, p_ic,
3152 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003153 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003154 {
3155 found_new_match = OK;
3156 break;
3157 }
3158 }
3159 p_scs = save_p_scs;
3160 p_ws = save_p_ws;
3161 }
3162
3163 // check if compl_curr_match has changed, (e.g. other type of
3164 // expansion added something)
3165 if (type != 0 && compl_curr_match != compl_old_match)
3166 found_new_match = OK;
3167
3168 // break the loop for specialized modes (use 'complete' just for the
3169 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3170 // match
3171 if ((ctrl_x_mode != CTRL_X_NORMAL
3172 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3173 {
3174 if (got_int)
3175 break;
3176 // Fill the popup menu as soon as possible.
3177 if (type != -1)
3178 ins_compl_check_keys(0, FALSE);
3179
3180 if ((ctrl_x_mode != CTRL_X_NORMAL
3181 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3182 break;
3183 compl_started = TRUE;
3184 }
3185 else
3186 {
3187 // Mark a buffer scanned when it has been scanned completely
3188 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3189 ins_buf->b_scanned = TRUE;
3190
3191 compl_started = FALSE;
3192 }
3193 }
3194 compl_started = TRUE;
3195
3196 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
3197 && *e_cpt == NUL) // Got to end of 'complete'
3198 found_new_match = FAIL;
3199
3200 i = -1; // total of matches, unknown
3201 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3202 && !ctrl_x_mode_line_or_eval()))
3203 i = ins_compl_make_cyclic();
3204
3205 if (compl_old_match != NULL)
3206 {
3207 // If several matches were added (FORWARD) or the search failed and has
3208 // just been made cyclic then we have to move compl_curr_match to the
3209 // next or previous entry (if any) -- Acevedo
3210 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3211 : compl_old_match->cp_prev;
3212 if (compl_curr_match == NULL)
3213 compl_curr_match = compl_old_match;
3214 }
3215 return i;
3216}
3217
3218/*
3219 * Delete the old text being completed.
3220 */
3221 void
3222ins_compl_delete(void)
3223{
3224 int col;
3225
3226 // In insert mode: Delete the typed part.
3227 // In replace mode: Put the old characters back, if any.
3228 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3229 if ((int)curwin->w_cursor.col > col)
3230 {
3231 if (stop_arrow() == FAIL)
3232 return;
3233 backspace_until_column(col);
3234 }
3235
3236 // TODO: is this sufficient for redrawing? Redrawing everything causes
3237 // flicker, thus we can't do that.
3238 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003239#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003240 // clear v:completed_item
3241 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003242#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003243}
3244
3245/*
3246 * Insert the new text being completed.
3247 * "in_compl_func" is TRUE when called from complete_check().
3248 */
3249 void
3250ins_compl_insert(int in_compl_func)
3251{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003252 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003253 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003254 compl_used_match = FALSE;
3255 else
3256 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003257#ifdef FEAT_EVAL
3258 {
3259 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3260
3261 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3262 }
3263#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003264 if (!in_compl_func)
3265 compl_curr_match = compl_shown_match;
3266}
3267
3268/*
3269 * Fill in the next completion in the current direction.
3270 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3271 * get more completions. If it is FALSE, then we just do nothing when there
3272 * are no more completions in a given direction. The latter case is used when
3273 * we are still in the middle of finding completions, to allow browsing
3274 * through the ones found so far.
3275 * Return the total number of matches, or -1 if still unknown -- webb.
3276 *
3277 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3278 * compl_shown_match here.
3279 *
3280 * Note that this function may be called recursively once only. First with
3281 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3282 * calls this function with "allow_get_expansion" FALSE.
3283 */
3284 static int
3285ins_compl_next(
3286 int allow_get_expansion,
3287 int count, // repeat completion this many times; should
3288 // be at least 1
3289 int insert_match, // Insert the newly selected match
3290 int in_compl_func) // called from complete_check()
3291{
3292 int num_matches = -1;
3293 int todo = count;
3294 compl_T *found_compl = NULL;
3295 int found_end = FALSE;
3296 int advance;
3297 int started = compl_started;
3298
3299 // When user complete function return -1 for findstart which is next
3300 // time of 'always', compl_shown_match become NULL.
3301 if (compl_shown_match == NULL)
3302 return -1;
3303
3304 if (compl_leader != NULL
Bram Moolenaar28976e22021-01-29 21:07:07 +01003305 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003306 {
3307 // Set "compl_shown_match" to the actually shown match, it may differ
3308 // when "compl_leader" is used to omit some of the matches.
3309 while (!ins_compl_equal(compl_shown_match,
3310 compl_leader, (int)STRLEN(compl_leader))
3311 && compl_shown_match->cp_next != NULL
3312 && compl_shown_match->cp_next != compl_first_match)
3313 compl_shown_match = compl_shown_match->cp_next;
3314
3315 // If we didn't find it searching forward, and compl_shows_dir is
3316 // backward, find the last match.
3317 if (compl_shows_dir == BACKWARD
3318 && !ins_compl_equal(compl_shown_match,
3319 compl_leader, (int)STRLEN(compl_leader))
3320 && (compl_shown_match->cp_next == NULL
3321 || compl_shown_match->cp_next == compl_first_match))
3322 {
3323 while (!ins_compl_equal(compl_shown_match,
3324 compl_leader, (int)STRLEN(compl_leader))
3325 && compl_shown_match->cp_prev != NULL
3326 && compl_shown_match->cp_prev != compl_first_match)
3327 compl_shown_match = compl_shown_match->cp_prev;
3328 }
3329 }
3330
3331 if (allow_get_expansion && insert_match
3332 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3333 // Delete old text to be replaced
3334 ins_compl_delete();
3335
3336 // When finding the longest common text we stick at the original text,
3337 // don't let CTRL-N or CTRL-P move to the first match.
3338 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3339
3340 // When restarting the search don't insert the first match either.
3341 if (compl_restarting)
3342 {
3343 advance = FALSE;
3344 compl_restarting = FALSE;
3345 }
3346
3347 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3348 // around.
3349 while (--todo >= 0)
3350 {
3351 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3352 {
3353 compl_shown_match = compl_shown_match->cp_next;
3354 found_end = (compl_first_match != NULL
3355 && (compl_shown_match->cp_next == compl_first_match
3356 || compl_shown_match == compl_first_match));
3357 }
3358 else if (compl_shows_dir == BACKWARD
3359 && compl_shown_match->cp_prev != NULL)
3360 {
3361 found_end = (compl_shown_match == compl_first_match);
3362 compl_shown_match = compl_shown_match->cp_prev;
3363 found_end |= (compl_shown_match == compl_first_match);
3364 }
3365 else
3366 {
3367 if (!allow_get_expansion)
3368 {
3369 if (advance)
3370 {
3371 if (compl_shows_dir == BACKWARD)
3372 compl_pending -= todo + 1;
3373 else
3374 compl_pending += todo + 1;
3375 }
3376 return -1;
3377 }
3378
3379 if (!compl_no_select && advance)
3380 {
3381 if (compl_shows_dir == BACKWARD)
3382 --compl_pending;
3383 else
3384 ++compl_pending;
3385 }
3386
3387 // Find matches.
3388 num_matches = ins_compl_get_exp(&compl_startpos);
3389
3390 // handle any pending completions
3391 while (compl_pending != 0 && compl_direction == compl_shows_dir
3392 && advance)
3393 {
3394 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3395 {
3396 compl_shown_match = compl_shown_match->cp_next;
3397 --compl_pending;
3398 }
3399 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3400 {
3401 compl_shown_match = compl_shown_match->cp_prev;
3402 ++compl_pending;
3403 }
3404 else
3405 break;
3406 }
3407 found_end = FALSE;
3408 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003409 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003410 && compl_leader != NULL
3411 && !ins_compl_equal(compl_shown_match,
3412 compl_leader, (int)STRLEN(compl_leader)))
3413 ++todo;
3414 else
3415 // Remember a matching item.
3416 found_compl = compl_shown_match;
3417
3418 // Stop at the end of the list when we found a usable match.
3419 if (found_end)
3420 {
3421 if (found_compl != NULL)
3422 {
3423 compl_shown_match = found_compl;
3424 break;
3425 }
3426 todo = 1; // use first usable match after wrapping around
3427 }
3428 }
3429
3430 // Insert the text of the new completion, or the compl_leader.
3431 if (compl_no_insert && !started)
3432 {
3433 ins_bytes(compl_orig_text + ins_compl_len());
3434 compl_used_match = FALSE;
3435 }
3436 else if (insert_match)
3437 {
3438 if (!compl_get_longest || compl_used_match)
3439 ins_compl_insert(in_compl_func);
3440 else
3441 ins_bytes(compl_leader + ins_compl_len());
3442 }
3443 else
3444 compl_used_match = FALSE;
3445
3446 if (!allow_get_expansion)
3447 {
3448 // may undisplay the popup menu first
3449 ins_compl_upd_pum();
3450
3451 if (pum_enough_matches())
3452 // Will display the popup menu, don't redraw yet to avoid flicker.
3453 pum_call_update_screen();
3454 else
3455 // Not showing the popup menu yet, redraw to show the user what was
3456 // inserted.
3457 update_screen(0);
3458
3459 // display the updated popup menu
3460 ins_compl_show_pum();
3461#ifdef FEAT_GUI
3462 if (gui.in_use)
3463 {
3464 // Show the cursor after the match, not after the redrawn text.
3465 setcursor();
3466 out_flush_cursor(FALSE, FALSE);
3467 }
3468#endif
3469
3470 // Delete old text to be replaced, since we're still searching and
3471 // don't want to match ourselves!
3472 ins_compl_delete();
3473 }
3474
3475 // Enter will select a match when the match wasn't inserted and the popup
3476 // menu is visible.
3477 if (compl_no_insert && !started)
3478 compl_enter_selects = TRUE;
3479 else
3480 compl_enter_selects = !insert_match && compl_match_array != NULL;
3481
3482 // Show the file name for the match (if any)
3483 // Truncate the file name to avoid a wait for return.
3484 if (compl_shown_match->cp_fname != NULL)
3485 {
3486 char *lead = _("match in file");
3487 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3488 char_u *s;
3489 char_u *e;
3490
3491 if (space > 0)
3492 {
3493 // We need the tail that fits. With double-byte encoding going
3494 // back from the end is very slow, thus go from the start and keep
3495 // the text that fits in "space" between "s" and "e".
3496 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3497 {
3498 space -= ptr2cells(e);
3499 while (space < 0)
3500 {
3501 space += ptr2cells(s);
3502 MB_PTR_ADV(s);
3503 }
3504 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01003505 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003506 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3507 s > compl_shown_match->cp_fname ? "<" : "", s);
3508 msg((char *)IObuff);
Bram Moolenaarcc233582020-12-12 13:32:07 +01003509 msg_hist_off = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003510 redraw_cmdline = FALSE; // don't overwrite!
3511 }
3512 }
3513
3514 return num_matches;
3515}
3516
3517/*
3518 * Call this while finding completions, to check whether the user has hit a key
3519 * that should change the currently displayed completion, or exit completion
3520 * mode. Also, when compl_pending is not zero, show a completion as soon as
3521 * possible. -- webb
3522 * "frequency" specifies out of how many calls we actually check.
3523 * "in_compl_func" is TRUE when called from complete_check(), don't set
3524 * compl_curr_match.
3525 */
3526 void
3527ins_compl_check_keys(int frequency, int in_compl_func)
3528{
3529 static int count = 0;
3530 int c;
3531
3532 // Don't check when reading keys from a script, :normal or feedkeys().
3533 // That would break the test scripts. But do check for keys when called
3534 // from complete_check().
3535 if (!in_compl_func && (using_script() || ex_normal_busy))
3536 return;
3537
3538 // Only do this at regular intervals
3539 if (++count < frequency)
3540 return;
3541 count = 0;
3542
3543 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3544 // can't do its work correctly.
3545 c = vpeekc_any();
3546 if (c != NUL)
3547 {
3548 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3549 {
3550 c = safe_vgetc(); // Eat the character
3551 compl_shows_dir = ins_compl_key2dir(c);
3552 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3553 c != K_UP && c != K_DOWN, in_compl_func);
3554 }
3555 else
3556 {
3557 // Need to get the character to have KeyTyped set. We'll put it
3558 // back with vungetc() below. But skip K_IGNORE.
3559 c = safe_vgetc();
3560 if (c != K_IGNORE)
3561 {
3562 // Don't interrupt completion when the character wasn't typed,
3563 // e.g., when doing @q to replay keys.
3564 if (c != Ctrl_R && KeyTyped)
3565 compl_interrupted = TRUE;
3566
3567 vungetc(c);
3568 }
3569 }
3570 }
3571 if (compl_pending != 0 && !got_int && !compl_no_insert)
3572 {
3573 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3574
3575 compl_pending = 0;
3576 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3577 }
3578}
3579
3580/*
3581 * Decide the direction of Insert mode complete from the key typed.
3582 * Returns BACKWARD or FORWARD.
3583 */
3584 static int
3585ins_compl_key2dir(int c)
3586{
3587 if (c == Ctrl_P || c == Ctrl_L
3588 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3589 return BACKWARD;
3590 return FORWARD;
3591}
3592
3593/*
3594 * Return TRUE for keys that are used for completion only when the popup menu
3595 * is visible.
3596 */
3597 static int
3598ins_compl_pum_key(int c)
3599{
3600 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3601 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3602 || c == K_UP || c == K_DOWN);
3603}
3604
3605/*
3606 * Decide the number of completions to move forward.
3607 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3608 */
3609 static int
3610ins_compl_key2count(int c)
3611{
3612 int h;
3613
3614 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3615 {
3616 h = pum_get_height();
3617 if (h > 3)
3618 h -= 2; // keep some context
3619 return h;
3620 }
3621 return 1;
3622}
3623
3624/*
3625 * Return TRUE if completion with "c" should insert the match, FALSE if only
3626 * to change the currently selected completion.
3627 */
3628 static int
3629ins_compl_use_match(int c)
3630{
3631 switch (c)
3632 {
3633 case K_UP:
3634 case K_DOWN:
3635 case K_PAGEDOWN:
3636 case K_KPAGEDOWN:
3637 case K_S_DOWN:
3638 case K_PAGEUP:
3639 case K_KPAGEUP:
3640 case K_S_UP:
3641 return FALSE;
3642 }
3643 return TRUE;
3644}
3645
3646/*
3647 * Do Insert mode completion.
3648 * Called when character "c" was typed, which has a meaning for completion.
3649 * Returns OK if completion was done, FAIL if something failed (out of mem).
3650 */
3651 int
3652ins_complete(int c, int enable_pum)
3653{
3654 char_u *line;
3655 int startcol = 0; // column where searched text starts
3656 colnr_T curs_col; // cursor column
3657 int n;
3658 int save_w_wrow;
3659 int save_w_leftcol;
3660 int insert_match;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003661#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003662 int save_did_ai = did_ai;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003663#endif
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003664 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003665
3666 compl_direction = ins_compl_key2dir(c);
3667 insert_match = ins_compl_use_match(c);
3668
3669 if (!compl_started)
3670 {
3671 // First time we hit ^N or ^P (in a row, I mean)
3672
3673 did_ai = FALSE;
3674#ifdef FEAT_SMARTINDENT
3675 did_si = FALSE;
3676 can_si = FALSE;
3677 can_si_back = FALSE;
3678#endif
3679 if (stop_arrow() == FAIL)
3680 return FAIL;
3681
3682 line = ml_get(curwin->w_cursor.lnum);
3683 curs_col = curwin->w_cursor.col;
3684 compl_pending = 0;
3685
3686 // If this same ctrl_x_mode has been interrupted use the text from
3687 // "compl_startpos" to the cursor as a pattern to add a new word
3688 // instead of expand the one before the cursor, in word-wise if
3689 // "compl_startpos" is not in the same line as the cursor then fix it
3690 // (the line has been split because it was longer than 'tw'). if SOL
3691 // is set then skip the previous pattern, a word at the beginning of
3692 // the line has been inserted, we'll look for that -- Acevedo.
3693 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3694 && compl_cont_mode == ctrl_x_mode)
3695 {
3696 // it is a continued search
3697 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3698 if (ctrl_x_mode == CTRL_X_NORMAL
3699 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3700 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3701 {
3702 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3703 {
3704 // line (probably) wrapped, set compl_startpos to the
3705 // first non_blank in the line, if it is not a wordchar
3706 // include it to get a better pattern, but then we don't
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003707 // want the "\\<" prefix, check it below
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003708 compl_col = (colnr_T)getwhitecols(line);
3709 compl_startpos.col = compl_col;
3710 compl_startpos.lnum = curwin->w_cursor.lnum;
3711 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3712 }
3713 else
3714 {
3715 // S_IPOS was set when we inserted a word that was at the
3716 // beginning of the line, which means that we'll go to SOL
3717 // mode but first we need to redefine compl_startpos
3718 if (compl_cont_status & CONT_S_IPOS)
3719 {
3720 compl_cont_status |= CONT_SOL;
3721 compl_startpos.col = (colnr_T)(skipwhite(
3722 line + compl_length
3723 + compl_startpos.col) - line);
3724 }
3725 compl_col = compl_startpos.col;
3726 }
3727 compl_length = curwin->w_cursor.col - (int)compl_col;
3728 // IObuff is used to add a "word from the next line" would we
3729 // have enough space? just being paranoid
3730#define MIN_SPACE 75
3731 if (compl_length > (IOSIZE - MIN_SPACE))
3732 {
3733 compl_cont_status &= ~CONT_SOL;
3734 compl_length = (IOSIZE - MIN_SPACE);
3735 compl_col = curwin->w_cursor.col - compl_length;
3736 }
3737 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3738 if (compl_length < 1)
3739 compl_cont_status &= CONT_LOCAL;
3740 }
3741 else if (ctrl_x_mode_line_or_eval())
3742 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3743 else
3744 compl_cont_status = 0;
3745 }
3746 else
3747 compl_cont_status &= CONT_LOCAL;
3748
3749 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3750 {
3751 compl_cont_mode = ctrl_x_mode;
3752 if (ctrl_x_mode != CTRL_X_NORMAL)
3753 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3754 compl_cont_status = 0;
3755 compl_cont_status |= CONT_N_ADDS;
3756 compl_startpos = curwin->w_cursor;
3757 startcol = (int)curs_col;
3758 compl_col = 0;
3759 }
3760
3761 // Work out completion pattern and original text -- webb
3762 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3763 {
3764 if ((compl_cont_status & CONT_SOL)
3765 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3766 {
3767 if (!(compl_cont_status & CONT_ADDING))
3768 {
3769 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3770 ;
3771 compl_col += ++startcol;
3772 compl_length = curs_col - startcol;
3773 }
3774 if (p_ic)
3775 compl_pattern = str_foldcase(line + compl_col,
3776 compl_length, NULL, 0);
3777 else
3778 compl_pattern = vim_strnsave(line + compl_col,
3779 compl_length);
3780 if (compl_pattern == NULL)
3781 return FAIL;
3782 }
3783 else if (compl_cont_status & CONT_ADDING)
3784 {
3785 char_u *prefix = (char_u *)"\\<";
3786
3787 // we need up to 2 extra chars for the prefix
3788 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3789 compl_length) + 2);
3790 if (compl_pattern == NULL)
3791 return FAIL;
3792 if (!vim_iswordp(line + compl_col)
3793 || (compl_col > 0
3794 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3795 prefix = (char_u *)"";
3796 STRCPY((char *)compl_pattern, prefix);
3797 (void)quote_meta(compl_pattern + STRLEN(prefix),
3798 line + compl_col, compl_length);
3799 }
3800 else if (--startcol < 0
3801 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3802 {
3803 // Match any word of at least two chars
3804 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3805 if (compl_pattern == NULL)
3806 return FAIL;
3807 compl_col += curs_col;
3808 compl_length = 0;
3809 }
3810 else
3811 {
3812 // Search the point of change class of multibyte character
3813 // or not a word single byte character backward.
3814 if (has_mbyte)
3815 {
3816 int base_class;
3817 int head_off;
3818
3819 startcol -= (*mb_head_off)(line, line + startcol);
3820 base_class = mb_get_class(line + startcol);
3821 while (--startcol >= 0)
3822 {
3823 head_off = (*mb_head_off)(line, line + startcol);
3824 if (base_class != mb_get_class(line + startcol
3825 - head_off))
3826 break;
3827 startcol -= head_off;
3828 }
3829 }
3830 else
3831 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3832 ;
3833 compl_col += ++startcol;
3834 compl_length = (int)curs_col - startcol;
3835 if (compl_length == 1)
3836 {
3837 // Only match word with at least two chars -- webb
3838 // there's no need to call quote_meta,
3839 // alloc(7) is enough -- Acevedo
3840 compl_pattern = alloc(7);
3841 if (compl_pattern == NULL)
3842 return FAIL;
3843 STRCPY((char *)compl_pattern, "\\<");
3844 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3845 STRCAT((char *)compl_pattern, "\\k");
3846 }
3847 else
3848 {
3849 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3850 compl_length) + 2);
3851 if (compl_pattern == NULL)
3852 return FAIL;
3853 STRCPY((char *)compl_pattern, "\\<");
3854 (void)quote_meta(compl_pattern + 2, line + compl_col,
3855 compl_length);
3856 }
3857 }
3858 }
3859 else if (ctrl_x_mode_line_or_eval())
3860 {
3861 compl_col = (colnr_T)getwhitecols(line);
3862 compl_length = (int)curs_col - (int)compl_col;
3863 if (compl_length < 0) // cursor in indent: empty pattern
3864 compl_length = 0;
3865 if (p_ic)
3866 compl_pattern = str_foldcase(line + compl_col, compl_length,
3867 NULL, 0);
3868 else
3869 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3870 if (compl_pattern == NULL)
3871 return FAIL;
3872 }
3873 else if (ctrl_x_mode == CTRL_X_FILES)
3874 {
3875 // Go back to just before the first filename character.
3876 if (startcol > 0)
3877 {
3878 char_u *p = line + startcol;
3879
3880 MB_PTR_BACK(line, p);
3881 while (p > line && vim_isfilec(PTR2CHAR(p)))
3882 MB_PTR_BACK(line, p);
3883 if (p == line && vim_isfilec(PTR2CHAR(p)))
3884 startcol = 0;
3885 else
3886 startcol = (int)(p - line) + 1;
3887 }
3888
3889 compl_col += startcol;
3890 compl_length = (int)curs_col - startcol;
3891 compl_pattern = addstar(line + compl_col, compl_length,
3892 EXPAND_FILES);
3893 if (compl_pattern == NULL)
3894 return FAIL;
3895 }
3896 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3897 {
3898 compl_pattern = vim_strnsave(line, curs_col);
3899 if (compl_pattern == NULL)
3900 return FAIL;
3901 set_cmd_context(&compl_xp, compl_pattern,
3902 (int)STRLEN(compl_pattern), curs_col, FALSE);
3903 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3904 || compl_xp.xp_context == EXPAND_NOTHING)
3905 // No completion possible, use an empty pattern to get a
3906 // "pattern not found" message.
3907 compl_col = curs_col;
3908 else
3909 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3910 compl_length = curs_col - compl_col;
3911 }
3912 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3913 {
3914#ifdef FEAT_COMPL_FUNC
3915 // Call user defined function 'completefunc' with "a:findstart"
3916 // set to 1 to obtain the length of text to use for completion.
3917 typval_T args[3];
3918 int col;
3919 char_u *funcname;
3920 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003921 int save_State = State;
3922
3923 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3924 // string
3925 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3926 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3927 if (*funcname == NUL)
3928 {
3929 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3930 ? "completefunc" : "omnifunc");
3931 // restore did_ai, so that adding comment leader works
3932 did_ai = save_did_ai;
3933 return FAIL;
3934 }
3935
3936 args[0].v_type = VAR_NUMBER;
3937 args[0].vval.v_number = 1;
3938 args[1].v_type = VAR_STRING;
3939 args[1].vval.v_string = (char_u *)"";
3940 args[2].v_type = VAR_UNKNOWN;
3941 pos = curwin->w_cursor;
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003942 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003943 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003944 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003945
3946 State = save_State;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 curwin->w_cursor = pos; // restore the cursor position
3948 validate_cursor();
3949 if (!EQUAL_POS(curwin->w_cursor, pos))
3950 {
3951 emsg(_(e_compldel));
3952 return FAIL;
3953 }
3954
3955 // Return value -2 means the user complete function wants to
3956 // cancel the complete without an error.
3957 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3958 if (col == -2)
3959 return FAIL;
3960 if (col == -3)
3961 {
3962 ctrl_x_mode = CTRL_X_NORMAL;
3963 edit_submode = NULL;
3964 if (!shortmess(SHM_COMPLETIONMENU))
3965 msg_clr_cmdline();
3966 return FAIL;
3967 }
3968
3969 // Reset extended parameters of completion, when start new
3970 // completion.
3971 compl_opt_refresh_always = FALSE;
3972 compl_opt_suppress_empty = FALSE;
3973
3974 if (col < 0)
3975 col = curs_col;
3976 compl_col = col;
3977 if (compl_col > curs_col)
3978 compl_col = curs_col;
3979
3980 // Setup variables for completion. Need to obtain "line" again,
3981 // it may have become invalid.
3982 line = ml_get(curwin->w_cursor.lnum);
3983 compl_length = curs_col - compl_col;
3984 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3985 if (compl_pattern == NULL)
3986#endif
3987 return FAIL;
3988 }
3989 else if (ctrl_x_mode == CTRL_X_SPELL)
3990 {
3991#ifdef FEAT_SPELL
3992 if (spell_bad_len > 0)
3993 compl_col = curs_col - spell_bad_len;
3994 else
3995 compl_col = spell_word_start(startcol);
3996 if (compl_col >= (colnr_T)startcol)
3997 {
3998 compl_length = 0;
3999 compl_col = curs_col;
4000 }
4001 else
4002 {
4003 spell_expand_check_cap(compl_col);
4004 compl_length = (int)curs_col - compl_col;
4005 }
4006 // Need to obtain "line" again, it may have become invalid.
4007 line = ml_get(curwin->w_cursor.lnum);
4008 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4009 if (compl_pattern == NULL)
4010#endif
4011 return FAIL;
4012 }
4013 else
4014 {
4015 internal_error("ins_complete()");
4016 return FAIL;
4017 }
4018
4019 if (compl_cont_status & CONT_ADDING)
4020 {
4021 edit_submode_pre = (char_u *)_(" Adding");
4022 if (ctrl_x_mode_line_or_eval())
4023 {
4024 // Insert a new line, keep indentation but ignore 'comments'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004025 char_u *old = curbuf->b_p_com;
4026
4027 curbuf->b_p_com = (char_u *)"";
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004028 compl_startpos.lnum = curwin->w_cursor.lnum;
4029 compl_startpos.col = compl_col;
4030 ins_eol('\r');
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004031 curbuf->b_p_com = old;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004032 compl_length = 0;
4033 compl_col = curwin->w_cursor.col;
4034 }
4035 }
4036 else
4037 {
4038 edit_submode_pre = NULL;
4039 compl_startpos.col = compl_col;
4040 }
4041
4042 if (compl_cont_status & CONT_LOCAL)
4043 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4044 else
4045 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4046
4047 // If any of the original typed text has been changed we need to fix
4048 // the redo buffer.
4049 ins_compl_fixRedoBufForLeader(NULL);
4050
4051 // Always add completion for the original text.
4052 vim_free(compl_orig_text);
4053 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004054 if (p_ic)
4055 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004056 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar08928322020-01-04 14:32:48 +01004057 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004058 {
4059 VIM_CLEAR(compl_pattern);
4060 VIM_CLEAR(compl_orig_text);
4061 return FAIL;
4062 }
4063
4064 // showmode might reset the internal line pointers, so it must
4065 // be called before line = ml_get(), or when this address is no
4066 // longer needed. -- Acevedo.
4067 edit_submode_extra = (char_u *)_("-- Searching...");
4068 edit_submode_highl = HLF_COUNT;
4069 showmode();
4070 edit_submode_extra = NULL;
4071 out_flush();
4072 }
4073 else if (insert_match && stop_arrow() == FAIL)
4074 return FAIL;
4075
4076 compl_shown_match = compl_curr_match;
4077 compl_shows_dir = compl_direction;
4078
4079 // Find next match (and following matches).
4080 save_w_wrow = curwin->w_wrow;
4081 save_w_leftcol = curwin->w_leftcol;
4082 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4083
4084 // may undisplay the popup menu
4085 ins_compl_upd_pum();
4086
4087 if (n > 1) // all matches have been found
4088 compl_matches = n;
4089 compl_curr_match = compl_shown_match;
4090 compl_direction = compl_shows_dir;
4091
4092 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4093 // mode.
4094 if (got_int && !global_busy)
4095 {
4096 (void)vgetc();
4097 got_int = FALSE;
4098 }
4099
4100 // we found no match if the list has only the "compl_orig_text"-entry
4101 if (compl_first_match == compl_first_match->cp_next)
4102 {
4103 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4104 && compl_length > 1
4105 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4106 edit_submode_highl = HLF_E;
4107 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4108 // because we couldn't expand anything at first place, but if we used
4109 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4110 // (such as M in M'exico) if not tried already. -- Acevedo
4111 if ( compl_length > 1
4112 || (compl_cont_status & CONT_ADDING)
4113 || (ctrl_x_mode != CTRL_X_NORMAL
4114 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4115 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4116 compl_cont_status &= ~CONT_N_ADDS;
4117 }
4118
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004119 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004120 compl_cont_status |= CONT_S_IPOS;
4121 else
4122 compl_cont_status &= ~CONT_S_IPOS;
4123
4124 if (edit_submode_extra == NULL)
4125 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004126 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004127 {
4128 edit_submode_extra = (char_u *)_("Back at original");
4129 edit_submode_highl = HLF_W;
4130 }
4131 else if (compl_cont_status & CONT_S_IPOS)
4132 {
4133 edit_submode_extra = (char_u *)_("Word from other line");
4134 edit_submode_highl = HLF_COUNT;
4135 }
4136 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4137 {
4138 edit_submode_extra = (char_u *)_("The only match");
4139 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004140 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004141 }
4142 else
4143 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004144#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004145 // Update completion sequence number when needed.
4146 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004147 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004148#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004149 // The match should always have a sequence number now, this is
4150 // just a safety check.
4151 if (compl_curr_match->cp_number != -1)
4152 {
4153 // Space for 10 text chars. + 2x10-digit no.s = 31.
4154 // Translations may need more than twice that.
4155 static char_u match_ref[81];
4156
4157 if (compl_matches > 0)
4158 vim_snprintf((char *)match_ref, sizeof(match_ref),
4159 _("match %d of %d"),
4160 compl_curr_match->cp_number, compl_matches);
4161 else
4162 vim_snprintf((char *)match_ref, sizeof(match_ref),
4163 _("match %d"),
4164 compl_curr_match->cp_number);
4165 edit_submode_extra = match_ref;
4166 edit_submode_highl = HLF_R;
4167 if (dollar_vcol >= 0)
4168 curs_columns(FALSE);
4169 }
4170 }
4171 }
4172
4173 // Show a message about what (completion) mode we're in.
4174 if (!compl_opt_suppress_empty)
4175 {
4176 showmode();
4177 if (!shortmess(SHM_COMPLETIONMENU))
4178 {
4179 if (edit_submode_extra != NULL)
4180 {
4181 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004182 {
4183 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004184 msg_attr((char *)edit_submode_extra,
4185 edit_submode_highl < HLF_COUNT
4186 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004187 msg_hist_off = FALSE;
4188 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004189 }
4190 else
4191 msg_clr_cmdline(); // necessary for "noshowmode"
4192 }
4193 }
4194
4195 // Show the popup menu, unless we got interrupted.
4196 if (enable_pum && !compl_interrupted)
4197 show_pum(save_w_wrow, save_w_leftcol);
4198
4199 compl_was_interrupted = compl_interrupted;
4200 compl_interrupted = FALSE;
4201
4202 return OK;
4203}
4204
4205 static void
4206show_pum(int prev_w_wrow, int prev_w_leftcol)
4207{
4208 // RedrawingDisabled may be set when invoked through complete().
4209 int n = RedrawingDisabled;
4210
4211 RedrawingDisabled = 0;
4212
4213 // If the cursor moved or the display scrolled we need to remove the pum
4214 // first.
4215 setcursor();
4216 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4217 ins_compl_del_pum();
4218
4219 ins_compl_show_pum();
4220 setcursor();
4221 RedrawingDisabled = n;
4222}
4223
4224/*
4225 * Looks in the first "len" chars. of "src" for search-metachars.
4226 * If dest is not NULL the chars. are copied there quoting (with
4227 * a backslash) the metachars, and dest would be NUL terminated.
4228 * Returns the length (needed) of dest
4229 */
4230 static unsigned
4231quote_meta(char_u *dest, char_u *src, int len)
4232{
4233 unsigned m = (unsigned)len + 1; // one extra for the NUL
4234
4235 for ( ; --len >= 0; src++)
4236 {
4237 switch (*src)
4238 {
4239 case '.':
4240 case '*':
4241 case '[':
4242 if (ctrl_x_mode == CTRL_X_DICTIONARY
4243 || ctrl_x_mode == CTRL_X_THESAURUS)
4244 break;
4245 // FALLTHROUGH
4246 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004247 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004248 break;
4249 // FALLTHROUGH
4250 case '\\':
4251 if (ctrl_x_mode == CTRL_X_DICTIONARY
4252 || ctrl_x_mode == CTRL_X_THESAURUS)
4253 break;
4254 // FALLTHROUGH
4255 case '^': // currently it's not needed.
4256 case '$':
4257 m++;
4258 if (dest != NULL)
4259 *dest++ = '\\';
4260 break;
4261 }
4262 if (dest != NULL)
4263 *dest++ = *src;
4264 // Copy remaining bytes of a multibyte character.
4265 if (has_mbyte)
4266 {
4267 int i, mb_len;
4268
4269 mb_len = (*mb_ptr2len)(src) - 1;
4270 if (mb_len > 0 && len >= mb_len)
4271 for (i = 0; i < mb_len; ++i)
4272 {
4273 --len;
4274 ++src;
4275 if (dest != NULL)
4276 *dest++ = *src;
4277 }
4278 }
4279 }
4280 if (dest != NULL)
4281 *dest = NUL;
4282
4283 return m;
4284}
4285
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004286#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004287 void
4288free_insexpand_stuff(void)
4289{
4290 VIM_CLEAR(compl_orig_text);
4291}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004292#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004293
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004294#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004295/*
4296 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4297 * spelled word, if there is one.
4298 */
4299 static void
4300spell_back_to_badword(void)
4301{
4302 pos_T tpos = curwin->w_cursor;
4303
4304 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4305 if (curwin->w_cursor.col != tpos.col)
4306 start_arrow(&tpos);
4307}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004308#endif