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