blob: bcaa3fdf54ad6c97492030153a64b7fde536576b [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 Moolenaard4c4bfa2021-10-16 21:14:11 +0100304 && *curbuf->b_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 Moolenaard4c4bfa2021-10-16 21:14:11 +01002249 return 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
2753 return (type == CTRL_X_THESAURUS
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002754 && curbuf->b_p_tsrfu != NULL
2755 && *curbuf->b_p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002756#else
2757 return FALSE;
2758#endif
2759}
2760
2761/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002762 * Get the next expansion(s), using "compl_pattern".
2763 * The search starts at position "ini" in curbuf and in the direction
2764 * compl_direction.
2765 * When "compl_started" is FALSE start at that position, otherwise continue
2766 * where we stopped searching before.
2767 * This may return before finding all the matches.
2768 * Return the total number of matches or -1 if still unknown -- Acevedo
2769 */
2770 static int
2771ins_compl_get_exp(pos_T *ini)
2772{
2773 static pos_T first_match_pos;
2774 static pos_T last_match_pos;
2775 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2776 static int found_all = FALSE; // Found all matches of a
2777 // certain type.
2778 static buf_T *ins_buf = NULL; // buffer being scanned
2779
2780 pos_T *pos;
2781 char_u **matches;
2782 int save_p_scs;
2783 int save_p_ws;
2784 int save_p_ic;
2785 int i;
2786 int num_matches;
2787 int len;
2788 int found_new_match;
2789 int type = ctrl_x_mode;
2790 char_u *ptr;
2791 char_u *dict = NULL;
2792 int dict_f = 0;
2793 int set_match_pos;
Andy Gozas6a230c62021-08-05 16:23:27 +02002794 pos_T prev_pos = {0, 0, 0};
2795 int looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002796
2797 if (!compl_started)
2798 {
2799 FOR_ALL_BUFFERS(ins_buf)
2800 ins_buf->b_scanned = 0;
2801 found_all = FALSE;
2802 ins_buf = curbuf;
2803 e_cpt = (compl_cont_status & CONT_LOCAL)
2804 ? (char_u *)"." : curbuf->b_p_cpt;
2805 last_match_pos = first_match_pos = *ini;
2806 }
2807 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2808 ins_buf = curbuf; // In case the buffer was wiped out.
2809
2810 compl_old_match = compl_curr_match; // remember the last current match
2811 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2812
2813 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2814 for (;;)
2815 {
2816 found_new_match = FAIL;
2817 set_match_pos = FALSE;
2818
2819 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2820 // or if found_all says this entry is done. For ^X^L only use the
2821 // entries from 'complete' that look in loaded buffers.
2822 if ((ctrl_x_mode == CTRL_X_NORMAL
2823 || ctrl_x_mode_line_or_eval())
2824 && (!compl_started || found_all))
2825 {
2826 found_all = FALSE;
2827 while (*e_cpt == ',' || *e_cpt == ' ')
2828 e_cpt++;
2829 if (*e_cpt == '.' && !curbuf->b_scanned)
2830 {
2831 ins_buf = curbuf;
2832 first_match_pos = *ini;
2833 // Move the cursor back one character so that ^N can match the
2834 // word immediately after the cursor.
2835 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2836 {
2837 // Move the cursor to after the last character in the
2838 // buffer, so that word at start of buffer is found
2839 // correctly.
2840 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2841 first_match_pos.col =
2842 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2843 }
2844 last_match_pos = first_match_pos;
2845 type = 0;
2846
2847 // Remember the first match so that the loop stops when we
2848 // wrap and come back there a second time.
2849 set_match_pos = TRUE;
2850 }
2851 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2852 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2853 {
2854 // Scan a buffer, but not the current one.
2855 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2856 {
2857 compl_started = TRUE;
2858 first_match_pos.col = last_match_pos.col = 0;
2859 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2860 last_match_pos.lnum = 0;
2861 type = 0;
2862 }
2863 else // unloaded buffer, scan like dictionary
2864 {
2865 found_all = TRUE;
2866 if (ins_buf->b_fname == NULL)
2867 continue;
2868 type = CTRL_X_DICTIONARY;
2869 dict = ins_buf->b_fname;
2870 dict_f = DICT_EXACT;
2871 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01002872 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2874 ins_buf->b_fname == NULL
2875 ? buf_spname(ins_buf)
2876 : ins_buf->b_sfname == NULL
2877 ? ins_buf->b_fname
2878 : ins_buf->b_sfname);
2879 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2880 }
2881 else if (*e_cpt == NUL)
2882 break;
2883 else
2884 {
2885 if (ctrl_x_mode_line_or_eval())
2886 type = -1;
2887 else if (*e_cpt == 'k' || *e_cpt == 's')
2888 {
2889 if (*e_cpt == 'k')
2890 type = CTRL_X_DICTIONARY;
2891 else
2892 type = CTRL_X_THESAURUS;
2893 if (*++e_cpt != ',' && *e_cpt != NUL)
2894 {
2895 dict = e_cpt;
2896 dict_f = DICT_FIRST;
2897 }
2898 }
2899#ifdef FEAT_FIND_ID
2900 else if (*e_cpt == 'i')
2901 type = CTRL_X_PATH_PATTERNS;
2902 else if (*e_cpt == 'd')
2903 type = CTRL_X_PATH_DEFINES;
2904#endif
2905 else if (*e_cpt == ']' || *e_cpt == 't')
2906 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01002907 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002908 type = CTRL_X_TAGS;
2909 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2910 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2911 }
2912 else
2913 type = -1;
2914
2915 // in any case e_cpt is advanced to the next entry
2916 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2917
2918 found_all = TRUE;
2919 if (type == -1)
2920 continue;
2921 }
2922 }
2923
2924 // If complete() was called then compl_pattern has been reset. The
2925 // following won't work then, bail out.
2926 if (compl_pattern == NULL)
2927 break;
2928
2929 switch (type)
2930 {
2931 case -1:
2932 break;
2933#ifdef FEAT_FIND_ID
2934 case CTRL_X_PATH_PATTERNS:
2935 case CTRL_X_PATH_DEFINES:
2936 find_pattern_in_path(compl_pattern, compl_direction,
2937 (int)STRLEN(compl_pattern), FALSE, FALSE,
2938 (type == CTRL_X_PATH_DEFINES
2939 && !(compl_cont_status & CONT_SOL))
2940 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2941 (linenr_T)1, (linenr_T)MAXLNUM);
2942 break;
2943#endif
2944
2945 case CTRL_X_DICTIONARY:
2946 case CTRL_X_THESAURUS:
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002947#ifdef FEAT_COMPL_FUNC
2948 if (thesaurus_func_complete(type))
2949 expand_by_function(type, compl_pattern);
2950 else
2951#endif
2952 ins_compl_dictionaries(
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002953 dict != NULL ? dict
2954 : (type == CTRL_X_THESAURUS
2955 ? (*curbuf->b_p_tsr == NUL
2956 ? p_tsr
2957 : curbuf->b_p_tsr)
2958 : (*curbuf->b_p_dict == NUL
2959 ? p_dict
2960 : curbuf->b_p_dict)),
2961 compl_pattern,
2962 dict != NULL ? dict_f
2963 : 0, type == CTRL_X_THESAURUS);
2964 dict = NULL;
2965 break;
2966
2967 case CTRL_X_TAGS:
2968 // set p_ic according to p_ic, p_scs and pat for find_tags().
2969 save_p_ic = p_ic;
2970 p_ic = ignorecase(compl_pattern);
2971
2972 // Find up to TAG_MANY matches. Avoids that an enormous number
2973 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002974 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002975 if (find_tags(compl_pattern, &num_matches, &matches,
2976 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2977 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2978 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002979 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002980 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002981 p_ic = save_p_ic;
2982 break;
2983
2984 case CTRL_X_FILES:
2985 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2986 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2987 {
2988
2989 // May change home directory back to "~".
2990 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaarac3150d2019-07-28 16:36:39 +02002991#ifdef BACKSLASH_IN_FILENAME
2992 if (curbuf->b_p_csl[0] != NUL)
2993 {
2994 int i;
2995
2996 for (i = 0; i < num_matches; ++i)
2997 {
2998 char_u *ptr = matches[i];
2999
3000 while (*ptr != NUL)
3001 {
3002 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3003 *ptr = '/';
3004 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3005 *ptr = '\\';
3006 ptr += (*mb_ptr2len)(ptr);
3007 }
3008 }
3009 }
3010#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003011 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3012 }
3013 break;
3014
3015 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003016 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003017 if (expand_cmdline(&compl_xp, compl_pattern,
3018 (int)STRLEN(compl_pattern),
3019 &num_matches, &matches) == EXPAND_OK)
3020 ins_compl_add_matches(num_matches, matches, FALSE);
3021 break;
3022
3023#ifdef FEAT_COMPL_FUNC
3024 case CTRL_X_FUNCTION:
3025 case CTRL_X_OMNI:
3026 expand_by_function(type, compl_pattern);
3027 break;
3028#endif
3029
3030 case CTRL_X_SPELL:
3031#ifdef FEAT_SPELL
3032 num_matches = expand_spelling(first_match_pos.lnum,
3033 compl_pattern, &matches);
3034 if (num_matches > 0)
3035 ins_compl_add_matches(num_matches, matches, p_ic);
3036#endif
3037 break;
3038
3039 default: // normal ^P/^N and ^X^L
3040 // If 'infercase' is set, don't use 'smartcase' here
3041 save_p_scs = p_scs;
3042 if (ins_buf->b_p_inf)
3043 p_scs = FALSE;
3044
3045 // Buffers other than curbuf are scanned from the beginning or the
3046 // end but never from the middle, thus setting nowrapscan in this
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003047 // buffer is a good idea, on the other hand, we always set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003048 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3049 save_p_ws = p_ws;
3050 if (ins_buf != curbuf)
3051 p_ws = FALSE;
3052 else if (*e_cpt == '.')
3053 p_ws = TRUE;
Andy Gozas6a230c62021-08-05 16:23:27 +02003054 looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003055 for (;;)
3056 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003057 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003058
3059 ++msg_silent; // Don't want messages for wrapscan.
3060
3061 // ctrl_x_mode_line_or_eval() || word-wise search that
3062 // has added a word that was at the beginning of the line
3063 if (ctrl_x_mode_line_or_eval()
3064 || (compl_cont_status & CONT_SOL))
3065 found_new_match = search_for_exact_line(ins_buf, pos,
3066 compl_direction, compl_pattern);
3067 else
3068 found_new_match = searchit(NULL, ins_buf, pos, NULL,
3069 compl_direction,
3070 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003071 RE_LAST, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003072 --msg_silent;
3073 if (!compl_started || set_match_pos)
3074 {
3075 // set "compl_started" even on fail
3076 compl_started = TRUE;
3077 first_match_pos = *pos;
3078 last_match_pos = *pos;
3079 set_match_pos = FALSE;
3080 }
3081 else if (first_match_pos.lnum == last_match_pos.lnum
Andy Gozas6a230c62021-08-05 16:23:27 +02003082 && first_match_pos.col == last_match_pos.col)
3083 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003084 found_new_match = FAIL;
Andy Gozas6a230c62021-08-05 16:23:27 +02003085 }
3086 else if ((compl_direction == FORWARD)
3087 && (prev_pos.lnum > pos->lnum
3088 || (prev_pos.lnum == pos->lnum
3089 && prev_pos.col >= pos->col)))
3090 {
3091 if (looped_around)
3092 found_new_match = FAIL;
3093 else
3094 looped_around = TRUE;
3095 }
3096 else if ((compl_direction != FORWARD)
3097 && (prev_pos.lnum < pos->lnum
3098 || (prev_pos.lnum == pos->lnum
3099 && prev_pos.col <= pos->col)))
3100 {
3101 if (looped_around)
3102 found_new_match = FAIL;
3103 else
3104 looped_around = TRUE;
3105 }
3106 prev_pos = *pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003107 if (found_new_match == FAIL)
3108 {
3109 if (ins_buf == curbuf)
3110 found_all = TRUE;
3111 break;
3112 }
3113
3114 // when ADDING, the text before the cursor matches, skip it
3115 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
3116 && ini->lnum == pos->lnum
3117 && ini->col == pos->col)
3118 continue;
3119 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3120 if (ctrl_x_mode_line_or_eval())
3121 {
3122 if (compl_cont_status & CONT_ADDING)
3123 {
3124 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3125 continue;
3126 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3127 if (!p_paste)
3128 ptr = skipwhite(ptr);
3129 }
3130 len = (int)STRLEN(ptr);
3131 }
3132 else
3133 {
3134 char_u *tmp_ptr = ptr;
3135
3136 if (compl_cont_status & CONT_ADDING)
3137 {
3138 tmp_ptr += compl_length;
3139 // Skip if already inside a word.
3140 if (vim_iswordp(tmp_ptr))
3141 continue;
3142 // Find start of next word.
3143 tmp_ptr = find_word_start(tmp_ptr);
3144 }
3145 // Find end of this word.
3146 tmp_ptr = find_word_end(tmp_ptr);
3147 len = (int)(tmp_ptr - ptr);
3148
3149 if ((compl_cont_status & CONT_ADDING)
3150 && len == compl_length)
3151 {
3152 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3153 {
3154 // Try next line, if any. the new word will be
3155 // "join" as if the normal command "J" was used.
3156 // IOSIZE is always greater than
3157 // compl_length, so the next STRNCPY always
3158 // works -- Acevedo
3159 STRNCPY(IObuff, ptr, len);
3160 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3161 tmp_ptr = ptr = skipwhite(ptr);
3162 // Find start of next word.
3163 tmp_ptr = find_word_start(tmp_ptr);
3164 // Find end of next word.
3165 tmp_ptr = find_word_end(tmp_ptr);
3166 if (tmp_ptr > ptr)
3167 {
3168 if (*ptr != ')' && IObuff[len - 1] != TAB)
3169 {
3170 if (IObuff[len - 1] != ' ')
3171 IObuff[len++] = ' ';
3172 // IObuf =~ "\k.* ", thus len >= 2
3173 if (p_js
3174 && (IObuff[len - 2] == '.'
3175 || (vim_strchr(p_cpo, CPO_JOINSP)
3176 == NULL
3177 && (IObuff[len - 2] == '?'
3178 || IObuff[len - 2] == '!'))))
3179 IObuff[len++] = ' ';
3180 }
3181 // copy as much as possible of the new word
3182 if (tmp_ptr - ptr >= IOSIZE - len)
3183 tmp_ptr = ptr + IOSIZE - len - 1;
3184 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3185 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003186 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003187 }
3188 IObuff[len] = NUL;
3189 ptr = IObuff;
3190 }
3191 if (len == compl_length)
3192 continue;
3193 }
3194 }
3195 if (ins_compl_add_infercase(ptr, len, p_ic,
3196 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003197 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003198 {
3199 found_new_match = OK;
3200 break;
3201 }
3202 }
3203 p_scs = save_p_scs;
3204 p_ws = save_p_ws;
3205 }
3206
3207 // check if compl_curr_match has changed, (e.g. other type of
3208 // expansion added something)
3209 if (type != 0 && compl_curr_match != compl_old_match)
3210 found_new_match = OK;
3211
3212 // break the loop for specialized modes (use 'complete' just for the
3213 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3214 // match
3215 if ((ctrl_x_mode != CTRL_X_NORMAL
3216 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3217 {
3218 if (got_int)
3219 break;
3220 // Fill the popup menu as soon as possible.
3221 if (type != -1)
3222 ins_compl_check_keys(0, FALSE);
3223
3224 if ((ctrl_x_mode != CTRL_X_NORMAL
3225 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3226 break;
3227 compl_started = TRUE;
3228 }
3229 else
3230 {
3231 // Mark a buffer scanned when it has been scanned completely
3232 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3233 ins_buf->b_scanned = TRUE;
3234
3235 compl_started = FALSE;
3236 }
3237 }
3238 compl_started = TRUE;
3239
3240 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
3241 && *e_cpt == NUL) // Got to end of 'complete'
3242 found_new_match = FAIL;
3243
3244 i = -1; // total of matches, unknown
3245 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3246 && !ctrl_x_mode_line_or_eval()))
3247 i = ins_compl_make_cyclic();
3248
3249 if (compl_old_match != NULL)
3250 {
3251 // If several matches were added (FORWARD) or the search failed and has
3252 // just been made cyclic then we have to move compl_curr_match to the
3253 // next or previous entry (if any) -- Acevedo
3254 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3255 : compl_old_match->cp_prev;
3256 if (compl_curr_match == NULL)
3257 compl_curr_match = compl_old_match;
3258 }
3259 return i;
3260}
3261
3262/*
3263 * Delete the old text being completed.
3264 */
3265 void
3266ins_compl_delete(void)
3267{
3268 int col;
3269
3270 // In insert mode: Delete the typed part.
3271 // In replace mode: Put the old characters back, if any.
3272 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3273 if ((int)curwin->w_cursor.col > col)
3274 {
3275 if (stop_arrow() == FAIL)
3276 return;
3277 backspace_until_column(col);
3278 }
3279
3280 // TODO: is this sufficient for redrawing? Redrawing everything causes
3281 // flicker, thus we can't do that.
3282 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003283#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003284 // clear v:completed_item
3285 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003286#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003287}
3288
3289/*
3290 * Insert the new text being completed.
3291 * "in_compl_func" is TRUE when called from complete_check().
3292 */
3293 void
3294ins_compl_insert(int in_compl_func)
3295{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003296 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003297 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003298 compl_used_match = FALSE;
3299 else
3300 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003301#ifdef FEAT_EVAL
3302 {
3303 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3304
3305 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3306 }
3307#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003308 if (!in_compl_func)
3309 compl_curr_match = compl_shown_match;
3310}
3311
3312/*
3313 * Fill in the next completion in the current direction.
3314 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3315 * get more completions. If it is FALSE, then we just do nothing when there
3316 * are no more completions in a given direction. The latter case is used when
3317 * we are still in the middle of finding completions, to allow browsing
3318 * through the ones found so far.
3319 * Return the total number of matches, or -1 if still unknown -- webb.
3320 *
3321 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3322 * compl_shown_match here.
3323 *
3324 * Note that this function may be called recursively once only. First with
3325 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3326 * calls this function with "allow_get_expansion" FALSE.
3327 */
3328 static int
3329ins_compl_next(
3330 int allow_get_expansion,
3331 int count, // repeat completion this many times; should
3332 // be at least 1
3333 int insert_match, // Insert the newly selected match
3334 int in_compl_func) // called from complete_check()
3335{
3336 int num_matches = -1;
3337 int todo = count;
3338 compl_T *found_compl = NULL;
3339 int found_end = FALSE;
3340 int advance;
3341 int started = compl_started;
3342
3343 // When user complete function return -1 for findstart which is next
3344 // time of 'always', compl_shown_match become NULL.
3345 if (compl_shown_match == NULL)
3346 return -1;
3347
3348 if (compl_leader != NULL
Bram Moolenaar28976e22021-01-29 21:07:07 +01003349 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003350 {
3351 // Set "compl_shown_match" to the actually shown match, it may differ
3352 // when "compl_leader" is used to omit some of the matches.
3353 while (!ins_compl_equal(compl_shown_match,
3354 compl_leader, (int)STRLEN(compl_leader))
3355 && compl_shown_match->cp_next != NULL
3356 && compl_shown_match->cp_next != compl_first_match)
3357 compl_shown_match = compl_shown_match->cp_next;
3358
3359 // If we didn't find it searching forward, and compl_shows_dir is
3360 // backward, find the last match.
3361 if (compl_shows_dir == BACKWARD
3362 && !ins_compl_equal(compl_shown_match,
3363 compl_leader, (int)STRLEN(compl_leader))
3364 && (compl_shown_match->cp_next == NULL
3365 || compl_shown_match->cp_next == compl_first_match))
3366 {
3367 while (!ins_compl_equal(compl_shown_match,
3368 compl_leader, (int)STRLEN(compl_leader))
3369 && compl_shown_match->cp_prev != NULL
3370 && compl_shown_match->cp_prev != compl_first_match)
3371 compl_shown_match = compl_shown_match->cp_prev;
3372 }
3373 }
3374
3375 if (allow_get_expansion && insert_match
3376 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3377 // Delete old text to be replaced
3378 ins_compl_delete();
3379
3380 // When finding the longest common text we stick at the original text,
3381 // don't let CTRL-N or CTRL-P move to the first match.
3382 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3383
3384 // When restarting the search don't insert the first match either.
3385 if (compl_restarting)
3386 {
3387 advance = FALSE;
3388 compl_restarting = FALSE;
3389 }
3390
3391 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3392 // around.
3393 while (--todo >= 0)
3394 {
3395 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3396 {
3397 compl_shown_match = compl_shown_match->cp_next;
3398 found_end = (compl_first_match != NULL
3399 && (compl_shown_match->cp_next == compl_first_match
3400 || compl_shown_match == compl_first_match));
3401 }
3402 else if (compl_shows_dir == BACKWARD
3403 && compl_shown_match->cp_prev != NULL)
3404 {
3405 found_end = (compl_shown_match == compl_first_match);
3406 compl_shown_match = compl_shown_match->cp_prev;
3407 found_end |= (compl_shown_match == compl_first_match);
3408 }
3409 else
3410 {
3411 if (!allow_get_expansion)
3412 {
3413 if (advance)
3414 {
3415 if (compl_shows_dir == BACKWARD)
3416 compl_pending -= todo + 1;
3417 else
3418 compl_pending += todo + 1;
3419 }
3420 return -1;
3421 }
3422
3423 if (!compl_no_select && advance)
3424 {
3425 if (compl_shows_dir == BACKWARD)
3426 --compl_pending;
3427 else
3428 ++compl_pending;
3429 }
3430
3431 // Find matches.
3432 num_matches = ins_compl_get_exp(&compl_startpos);
3433
3434 // handle any pending completions
3435 while (compl_pending != 0 && compl_direction == compl_shows_dir
3436 && advance)
3437 {
3438 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3439 {
3440 compl_shown_match = compl_shown_match->cp_next;
3441 --compl_pending;
3442 }
3443 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3444 {
3445 compl_shown_match = compl_shown_match->cp_prev;
3446 ++compl_pending;
3447 }
3448 else
3449 break;
3450 }
3451 found_end = FALSE;
3452 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003453 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003454 && compl_leader != NULL
3455 && !ins_compl_equal(compl_shown_match,
3456 compl_leader, (int)STRLEN(compl_leader)))
3457 ++todo;
3458 else
3459 // Remember a matching item.
3460 found_compl = compl_shown_match;
3461
3462 // Stop at the end of the list when we found a usable match.
3463 if (found_end)
3464 {
3465 if (found_compl != NULL)
3466 {
3467 compl_shown_match = found_compl;
3468 break;
3469 }
3470 todo = 1; // use first usable match after wrapping around
3471 }
3472 }
3473
3474 // Insert the text of the new completion, or the compl_leader.
3475 if (compl_no_insert && !started)
3476 {
3477 ins_bytes(compl_orig_text + ins_compl_len());
3478 compl_used_match = FALSE;
3479 }
3480 else if (insert_match)
3481 {
3482 if (!compl_get_longest || compl_used_match)
3483 ins_compl_insert(in_compl_func);
3484 else
3485 ins_bytes(compl_leader + ins_compl_len());
3486 }
3487 else
3488 compl_used_match = FALSE;
3489
3490 if (!allow_get_expansion)
3491 {
3492 // may undisplay the popup menu first
3493 ins_compl_upd_pum();
3494
3495 if (pum_enough_matches())
3496 // Will display the popup menu, don't redraw yet to avoid flicker.
3497 pum_call_update_screen();
3498 else
3499 // Not showing the popup menu yet, redraw to show the user what was
3500 // inserted.
3501 update_screen(0);
3502
3503 // display the updated popup menu
3504 ins_compl_show_pum();
3505#ifdef FEAT_GUI
3506 if (gui.in_use)
3507 {
3508 // Show the cursor after the match, not after the redrawn text.
3509 setcursor();
3510 out_flush_cursor(FALSE, FALSE);
3511 }
3512#endif
3513
3514 // Delete old text to be replaced, since we're still searching and
3515 // don't want to match ourselves!
3516 ins_compl_delete();
3517 }
3518
3519 // Enter will select a match when the match wasn't inserted and the popup
3520 // menu is visible.
3521 if (compl_no_insert && !started)
3522 compl_enter_selects = TRUE;
3523 else
3524 compl_enter_selects = !insert_match && compl_match_array != NULL;
3525
3526 // Show the file name for the match (if any)
3527 // Truncate the file name to avoid a wait for return.
3528 if (compl_shown_match->cp_fname != NULL)
3529 {
3530 char *lead = _("match in file");
3531 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3532 char_u *s;
3533 char_u *e;
3534
3535 if (space > 0)
3536 {
3537 // We need the tail that fits. With double-byte encoding going
3538 // back from the end is very slow, thus go from the start and keep
3539 // the text that fits in "space" between "s" and "e".
3540 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3541 {
3542 space -= ptr2cells(e);
3543 while (space < 0)
3544 {
3545 space += ptr2cells(s);
3546 MB_PTR_ADV(s);
3547 }
3548 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01003549 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003550 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3551 s > compl_shown_match->cp_fname ? "<" : "", s);
3552 msg((char *)IObuff);
Bram Moolenaarcc233582020-12-12 13:32:07 +01003553 msg_hist_off = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003554 redraw_cmdline = FALSE; // don't overwrite!
3555 }
3556 }
3557
3558 return num_matches;
3559}
3560
3561/*
3562 * Call this while finding completions, to check whether the user has hit a key
3563 * that should change the currently displayed completion, or exit completion
3564 * mode. Also, when compl_pending is not zero, show a completion as soon as
3565 * possible. -- webb
3566 * "frequency" specifies out of how many calls we actually check.
3567 * "in_compl_func" is TRUE when called from complete_check(), don't set
3568 * compl_curr_match.
3569 */
3570 void
3571ins_compl_check_keys(int frequency, int in_compl_func)
3572{
3573 static int count = 0;
3574 int c;
3575
3576 // Don't check when reading keys from a script, :normal or feedkeys().
3577 // That would break the test scripts. But do check for keys when called
3578 // from complete_check().
3579 if (!in_compl_func && (using_script() || ex_normal_busy))
3580 return;
3581
3582 // Only do this at regular intervals
3583 if (++count < frequency)
3584 return;
3585 count = 0;
3586
3587 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3588 // can't do its work correctly.
3589 c = vpeekc_any();
3590 if (c != NUL)
3591 {
3592 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3593 {
3594 c = safe_vgetc(); // Eat the character
3595 compl_shows_dir = ins_compl_key2dir(c);
3596 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3597 c != K_UP && c != K_DOWN, in_compl_func);
3598 }
3599 else
3600 {
3601 // Need to get the character to have KeyTyped set. We'll put it
3602 // back with vungetc() below. But skip K_IGNORE.
3603 c = safe_vgetc();
3604 if (c != K_IGNORE)
3605 {
3606 // Don't interrupt completion when the character wasn't typed,
3607 // e.g., when doing @q to replay keys.
3608 if (c != Ctrl_R && KeyTyped)
3609 compl_interrupted = TRUE;
3610
3611 vungetc(c);
3612 }
3613 }
3614 }
3615 if (compl_pending != 0 && !got_int && !compl_no_insert)
3616 {
3617 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3618
3619 compl_pending = 0;
3620 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3621 }
3622}
3623
3624/*
3625 * Decide the direction of Insert mode complete from the key typed.
3626 * Returns BACKWARD or FORWARD.
3627 */
3628 static int
3629ins_compl_key2dir(int c)
3630{
3631 if (c == Ctrl_P || c == Ctrl_L
3632 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3633 return BACKWARD;
3634 return FORWARD;
3635}
3636
3637/*
3638 * Return TRUE for keys that are used for completion only when the popup menu
3639 * is visible.
3640 */
3641 static int
3642ins_compl_pum_key(int c)
3643{
3644 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3645 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3646 || c == K_UP || c == K_DOWN);
3647}
3648
3649/*
3650 * Decide the number of completions to move forward.
3651 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3652 */
3653 static int
3654ins_compl_key2count(int c)
3655{
3656 int h;
3657
3658 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3659 {
3660 h = pum_get_height();
3661 if (h > 3)
3662 h -= 2; // keep some context
3663 return h;
3664 }
3665 return 1;
3666}
3667
3668/*
3669 * Return TRUE if completion with "c" should insert the match, FALSE if only
3670 * to change the currently selected completion.
3671 */
3672 static int
3673ins_compl_use_match(int c)
3674{
3675 switch (c)
3676 {
3677 case K_UP:
3678 case K_DOWN:
3679 case K_PAGEDOWN:
3680 case K_KPAGEDOWN:
3681 case K_S_DOWN:
3682 case K_PAGEUP:
3683 case K_KPAGEUP:
3684 case K_S_UP:
3685 return FALSE;
3686 }
3687 return TRUE;
3688}
3689
3690/*
3691 * Do Insert mode completion.
3692 * Called when character "c" was typed, which has a meaning for completion.
3693 * Returns OK if completion was done, FAIL if something failed (out of mem).
3694 */
3695 int
3696ins_complete(int c, int enable_pum)
3697{
3698 char_u *line;
3699 int startcol = 0; // column where searched text starts
3700 colnr_T curs_col; // cursor column
3701 int n;
3702 int save_w_wrow;
3703 int save_w_leftcol;
3704 int insert_match;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003705#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003706 int save_did_ai = did_ai;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003707#endif
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003708 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003709
3710 compl_direction = ins_compl_key2dir(c);
3711 insert_match = ins_compl_use_match(c);
3712
3713 if (!compl_started)
3714 {
3715 // First time we hit ^N or ^P (in a row, I mean)
3716
3717 did_ai = FALSE;
3718#ifdef FEAT_SMARTINDENT
3719 did_si = FALSE;
3720 can_si = FALSE;
3721 can_si_back = FALSE;
3722#endif
3723 if (stop_arrow() == FAIL)
3724 return FAIL;
3725
3726 line = ml_get(curwin->w_cursor.lnum);
3727 curs_col = curwin->w_cursor.col;
3728 compl_pending = 0;
3729
3730 // If this same ctrl_x_mode has been interrupted use the text from
3731 // "compl_startpos" to the cursor as a pattern to add a new word
3732 // instead of expand the one before the cursor, in word-wise if
3733 // "compl_startpos" is not in the same line as the cursor then fix it
3734 // (the line has been split because it was longer than 'tw'). if SOL
3735 // is set then skip the previous pattern, a word at the beginning of
3736 // the line has been inserted, we'll look for that -- Acevedo.
3737 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3738 && compl_cont_mode == ctrl_x_mode)
3739 {
3740 // it is a continued search
3741 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3742 if (ctrl_x_mode == CTRL_X_NORMAL
3743 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3744 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3745 {
3746 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3747 {
3748 // line (probably) wrapped, set compl_startpos to the
3749 // first non_blank in the line, if it is not a wordchar
3750 // include it to get a better pattern, but then we don't
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003751 // want the "\\<" prefix, check it below
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003752 compl_col = (colnr_T)getwhitecols(line);
3753 compl_startpos.col = compl_col;
3754 compl_startpos.lnum = curwin->w_cursor.lnum;
3755 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3756 }
3757 else
3758 {
3759 // S_IPOS was set when we inserted a word that was at the
3760 // beginning of the line, which means that we'll go to SOL
3761 // mode but first we need to redefine compl_startpos
3762 if (compl_cont_status & CONT_S_IPOS)
3763 {
3764 compl_cont_status |= CONT_SOL;
3765 compl_startpos.col = (colnr_T)(skipwhite(
3766 line + compl_length
3767 + compl_startpos.col) - line);
3768 }
3769 compl_col = compl_startpos.col;
3770 }
3771 compl_length = curwin->w_cursor.col - (int)compl_col;
3772 // IObuff is used to add a "word from the next line" would we
3773 // have enough space? just being paranoid
3774#define MIN_SPACE 75
3775 if (compl_length > (IOSIZE - MIN_SPACE))
3776 {
3777 compl_cont_status &= ~CONT_SOL;
3778 compl_length = (IOSIZE - MIN_SPACE);
3779 compl_col = curwin->w_cursor.col - compl_length;
3780 }
3781 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3782 if (compl_length < 1)
3783 compl_cont_status &= CONT_LOCAL;
3784 }
3785 else if (ctrl_x_mode_line_or_eval())
3786 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3787 else
3788 compl_cont_status = 0;
3789 }
3790 else
3791 compl_cont_status &= CONT_LOCAL;
3792
3793 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3794 {
3795 compl_cont_mode = ctrl_x_mode;
3796 if (ctrl_x_mode != CTRL_X_NORMAL)
3797 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3798 compl_cont_status = 0;
3799 compl_cont_status |= CONT_N_ADDS;
3800 compl_startpos = curwin->w_cursor;
3801 startcol = (int)curs_col;
3802 compl_col = 0;
3803 }
3804
3805 // Work out completion pattern and original text -- webb
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003806 if (ctrl_x_mode == CTRL_X_NORMAL
3807 || (ctrl_x_mode & CTRL_X_WANT_IDENT
3808 && !thesaurus_func_complete(ctrl_x_mode)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003809 {
3810 if ((compl_cont_status & CONT_SOL)
3811 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3812 {
3813 if (!(compl_cont_status & CONT_ADDING))
3814 {
3815 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3816 ;
3817 compl_col += ++startcol;
3818 compl_length = curs_col - startcol;
3819 }
3820 if (p_ic)
3821 compl_pattern = str_foldcase(line + compl_col,
3822 compl_length, NULL, 0);
3823 else
3824 compl_pattern = vim_strnsave(line + compl_col,
3825 compl_length);
3826 if (compl_pattern == NULL)
3827 return FAIL;
3828 }
3829 else if (compl_cont_status & CONT_ADDING)
3830 {
3831 char_u *prefix = (char_u *)"\\<";
3832
3833 // we need up to 2 extra chars for the prefix
3834 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3835 compl_length) + 2);
3836 if (compl_pattern == NULL)
3837 return FAIL;
3838 if (!vim_iswordp(line + compl_col)
3839 || (compl_col > 0
3840 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3841 prefix = (char_u *)"";
3842 STRCPY((char *)compl_pattern, prefix);
3843 (void)quote_meta(compl_pattern + STRLEN(prefix),
3844 line + compl_col, compl_length);
3845 }
3846 else if (--startcol < 0
3847 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3848 {
3849 // Match any word of at least two chars
3850 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3851 if (compl_pattern == NULL)
3852 return FAIL;
3853 compl_col += curs_col;
3854 compl_length = 0;
3855 }
3856 else
3857 {
3858 // Search the point of change class of multibyte character
3859 // or not a word single byte character backward.
3860 if (has_mbyte)
3861 {
3862 int base_class;
3863 int head_off;
3864
3865 startcol -= (*mb_head_off)(line, line + startcol);
3866 base_class = mb_get_class(line + startcol);
3867 while (--startcol >= 0)
3868 {
3869 head_off = (*mb_head_off)(line, line + startcol);
3870 if (base_class != mb_get_class(line + startcol
3871 - head_off))
3872 break;
3873 startcol -= head_off;
3874 }
3875 }
3876 else
3877 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3878 ;
3879 compl_col += ++startcol;
3880 compl_length = (int)curs_col - startcol;
3881 if (compl_length == 1)
3882 {
3883 // Only match word with at least two chars -- webb
3884 // there's no need to call quote_meta,
3885 // alloc(7) is enough -- Acevedo
3886 compl_pattern = alloc(7);
3887 if (compl_pattern == NULL)
3888 return FAIL;
3889 STRCPY((char *)compl_pattern, "\\<");
3890 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3891 STRCAT((char *)compl_pattern, "\\k");
3892 }
3893 else
3894 {
3895 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3896 compl_length) + 2);
3897 if (compl_pattern == NULL)
3898 return FAIL;
3899 STRCPY((char *)compl_pattern, "\\<");
3900 (void)quote_meta(compl_pattern + 2, line + compl_col,
3901 compl_length);
3902 }
3903 }
3904 }
3905 else if (ctrl_x_mode_line_or_eval())
3906 {
3907 compl_col = (colnr_T)getwhitecols(line);
3908 compl_length = (int)curs_col - (int)compl_col;
3909 if (compl_length < 0) // cursor in indent: empty pattern
3910 compl_length = 0;
3911 if (p_ic)
3912 compl_pattern = str_foldcase(line + compl_col, compl_length,
3913 NULL, 0);
3914 else
3915 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3916 if (compl_pattern == NULL)
3917 return FAIL;
3918 }
3919 else if (ctrl_x_mode == CTRL_X_FILES)
3920 {
3921 // Go back to just before the first filename character.
3922 if (startcol > 0)
3923 {
3924 char_u *p = line + startcol;
3925
3926 MB_PTR_BACK(line, p);
3927 while (p > line && vim_isfilec(PTR2CHAR(p)))
3928 MB_PTR_BACK(line, p);
3929 if (p == line && vim_isfilec(PTR2CHAR(p)))
3930 startcol = 0;
3931 else
3932 startcol = (int)(p - line) + 1;
3933 }
3934
3935 compl_col += startcol;
3936 compl_length = (int)curs_col - startcol;
3937 compl_pattern = addstar(line + compl_col, compl_length,
3938 EXPAND_FILES);
3939 if (compl_pattern == NULL)
3940 return FAIL;
3941 }
3942 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3943 {
3944 compl_pattern = vim_strnsave(line, curs_col);
3945 if (compl_pattern == NULL)
3946 return FAIL;
3947 set_cmd_context(&compl_xp, compl_pattern,
3948 (int)STRLEN(compl_pattern), curs_col, FALSE);
3949 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3950 || compl_xp.xp_context == EXPAND_NOTHING)
3951 // No completion possible, use an empty pattern to get a
3952 // "pattern not found" message.
3953 compl_col = curs_col;
3954 else
3955 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3956 compl_length = curs_col - compl_col;
3957 }
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003958 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
3959 || thesaurus_func_complete(ctrl_x_mode))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003960 {
3961#ifdef FEAT_COMPL_FUNC
3962 // Call user defined function 'completefunc' with "a:findstart"
3963 // set to 1 to obtain the length of text to use for completion.
3964 typval_T args[3];
3965 int col;
3966 char_u *funcname;
3967 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003968 int save_State = State;
3969
3970 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3971 // string
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003972 funcname = get_complete_funcname(ctrl_x_mode);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003973 if (*funcname == NUL)
3974 {
3975 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3976 ? "completefunc" : "omnifunc");
3977 // restore did_ai, so that adding comment leader works
3978 did_ai = save_did_ai;
3979 return FAIL;
3980 }
3981
3982 args[0].v_type = VAR_NUMBER;
3983 args[0].vval.v_number = 1;
3984 args[1].v_type = VAR_STRING;
3985 args[1].vval.v_string = (char_u *)"";
3986 args[2].v_type = VAR_UNKNOWN;
3987 pos = curwin->w_cursor;
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003988 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003989 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003990 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003991
3992 State = save_State;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003993 curwin->w_cursor = pos; // restore the cursor position
3994 validate_cursor();
3995 if (!EQUAL_POS(curwin->w_cursor, pos))
3996 {
3997 emsg(_(e_compldel));
3998 return FAIL;
3999 }
4000
4001 // Return value -2 means the user complete function wants to
4002 // cancel the complete without an error.
4003 // Return value -3 does the same as -2 and leaves CTRL-X mode.
4004 if (col == -2)
4005 return FAIL;
4006 if (col == -3)
4007 {
4008 ctrl_x_mode = CTRL_X_NORMAL;
4009 edit_submode = NULL;
4010 if (!shortmess(SHM_COMPLETIONMENU))
4011 msg_clr_cmdline();
4012 return FAIL;
4013 }
4014
4015 // Reset extended parameters of completion, when start new
4016 // completion.
4017 compl_opt_refresh_always = FALSE;
4018 compl_opt_suppress_empty = FALSE;
4019
4020 if (col < 0)
4021 col = curs_col;
4022 compl_col = col;
4023 if (compl_col > curs_col)
4024 compl_col = curs_col;
4025
4026 // Setup variables for completion. Need to obtain "line" again,
4027 // it may have become invalid.
4028 line = ml_get(curwin->w_cursor.lnum);
4029 compl_length = curs_col - compl_col;
4030 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4031 if (compl_pattern == NULL)
4032#endif
4033 return FAIL;
4034 }
4035 else if (ctrl_x_mode == CTRL_X_SPELL)
4036 {
4037#ifdef FEAT_SPELL
4038 if (spell_bad_len > 0)
4039 compl_col = curs_col - spell_bad_len;
4040 else
4041 compl_col = spell_word_start(startcol);
4042 if (compl_col >= (colnr_T)startcol)
4043 {
4044 compl_length = 0;
4045 compl_col = curs_col;
4046 }
4047 else
4048 {
4049 spell_expand_check_cap(compl_col);
4050 compl_length = (int)curs_col - compl_col;
4051 }
4052 // Need to obtain "line" again, it may have become invalid.
4053 line = ml_get(curwin->w_cursor.lnum);
4054 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4055 if (compl_pattern == NULL)
4056#endif
4057 return FAIL;
4058 }
4059 else
4060 {
4061 internal_error("ins_complete()");
4062 return FAIL;
4063 }
4064
4065 if (compl_cont_status & CONT_ADDING)
4066 {
4067 edit_submode_pre = (char_u *)_(" Adding");
4068 if (ctrl_x_mode_line_or_eval())
4069 {
4070 // Insert a new line, keep indentation but ignore 'comments'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004071 char_u *old = curbuf->b_p_com;
4072
4073 curbuf->b_p_com = (char_u *)"";
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 compl_startpos.lnum = curwin->w_cursor.lnum;
4075 compl_startpos.col = compl_col;
4076 ins_eol('\r');
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077 curbuf->b_p_com = old;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004078 compl_length = 0;
4079 compl_col = curwin->w_cursor.col;
4080 }
4081 }
4082 else
4083 {
4084 edit_submode_pre = NULL;
4085 compl_startpos.col = compl_col;
4086 }
4087
4088 if (compl_cont_status & CONT_LOCAL)
4089 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4090 else
4091 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4092
4093 // If any of the original typed text has been changed we need to fix
4094 // the redo buffer.
4095 ins_compl_fixRedoBufForLeader(NULL);
4096
4097 // Always add completion for the original text.
4098 vim_free(compl_orig_text);
4099 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004100 if (p_ic)
4101 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004102 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar08928322020-01-04 14:32:48 +01004103 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004104 {
4105 VIM_CLEAR(compl_pattern);
4106 VIM_CLEAR(compl_orig_text);
4107 return FAIL;
4108 }
4109
4110 // showmode might reset the internal line pointers, so it must
4111 // be called before line = ml_get(), or when this address is no
4112 // longer needed. -- Acevedo.
4113 edit_submode_extra = (char_u *)_("-- Searching...");
4114 edit_submode_highl = HLF_COUNT;
4115 showmode();
4116 edit_submode_extra = NULL;
4117 out_flush();
4118 }
4119 else if (insert_match && stop_arrow() == FAIL)
4120 return FAIL;
4121
4122 compl_shown_match = compl_curr_match;
4123 compl_shows_dir = compl_direction;
4124
4125 // Find next match (and following matches).
4126 save_w_wrow = curwin->w_wrow;
4127 save_w_leftcol = curwin->w_leftcol;
4128 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4129
4130 // may undisplay the popup menu
4131 ins_compl_upd_pum();
4132
4133 if (n > 1) // all matches have been found
4134 compl_matches = n;
4135 compl_curr_match = compl_shown_match;
4136 compl_direction = compl_shows_dir;
4137
4138 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4139 // mode.
4140 if (got_int && !global_busy)
4141 {
4142 (void)vgetc();
4143 got_int = FALSE;
4144 }
4145
4146 // we found no match if the list has only the "compl_orig_text"-entry
4147 if (compl_first_match == compl_first_match->cp_next)
4148 {
4149 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4150 && compl_length > 1
4151 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4152 edit_submode_highl = HLF_E;
4153 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4154 // because we couldn't expand anything at first place, but if we used
4155 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4156 // (such as M in M'exico) if not tried already. -- Acevedo
4157 if ( compl_length > 1
4158 || (compl_cont_status & CONT_ADDING)
4159 || (ctrl_x_mode != CTRL_X_NORMAL
4160 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4161 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4162 compl_cont_status &= ~CONT_N_ADDS;
4163 }
4164
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004165 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004166 compl_cont_status |= CONT_S_IPOS;
4167 else
4168 compl_cont_status &= ~CONT_S_IPOS;
4169
4170 if (edit_submode_extra == NULL)
4171 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004172 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004173 {
4174 edit_submode_extra = (char_u *)_("Back at original");
4175 edit_submode_highl = HLF_W;
4176 }
4177 else if (compl_cont_status & CONT_S_IPOS)
4178 {
4179 edit_submode_extra = (char_u *)_("Word from other line");
4180 edit_submode_highl = HLF_COUNT;
4181 }
4182 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4183 {
4184 edit_submode_extra = (char_u *)_("The only match");
4185 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004186 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004187 }
4188 else
4189 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004190#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004191 // Update completion sequence number when needed.
4192 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004193 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004194#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004195 // The match should always have a sequence number now, this is
4196 // just a safety check.
4197 if (compl_curr_match->cp_number != -1)
4198 {
4199 // Space for 10 text chars. + 2x10-digit no.s = 31.
4200 // Translations may need more than twice that.
4201 static char_u match_ref[81];
4202
4203 if (compl_matches > 0)
4204 vim_snprintf((char *)match_ref, sizeof(match_ref),
4205 _("match %d of %d"),
4206 compl_curr_match->cp_number, compl_matches);
4207 else
4208 vim_snprintf((char *)match_ref, sizeof(match_ref),
4209 _("match %d"),
4210 compl_curr_match->cp_number);
4211 edit_submode_extra = match_ref;
4212 edit_submode_highl = HLF_R;
4213 if (dollar_vcol >= 0)
4214 curs_columns(FALSE);
4215 }
4216 }
4217 }
4218
4219 // Show a message about what (completion) mode we're in.
4220 if (!compl_opt_suppress_empty)
4221 {
4222 showmode();
4223 if (!shortmess(SHM_COMPLETIONMENU))
4224 {
4225 if (edit_submode_extra != NULL)
4226 {
4227 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004228 {
4229 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004230 msg_attr((char *)edit_submode_extra,
4231 edit_submode_highl < HLF_COUNT
4232 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004233 msg_hist_off = FALSE;
4234 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004235 }
4236 else
4237 msg_clr_cmdline(); // necessary for "noshowmode"
4238 }
4239 }
4240
4241 // Show the popup menu, unless we got interrupted.
4242 if (enable_pum && !compl_interrupted)
4243 show_pum(save_w_wrow, save_w_leftcol);
4244
4245 compl_was_interrupted = compl_interrupted;
4246 compl_interrupted = FALSE;
4247
4248 return OK;
4249}
4250
4251 static void
4252show_pum(int prev_w_wrow, int prev_w_leftcol)
4253{
4254 // RedrawingDisabled may be set when invoked through complete().
4255 int n = RedrawingDisabled;
4256
4257 RedrawingDisabled = 0;
4258
4259 // If the cursor moved or the display scrolled we need to remove the pum
4260 // first.
4261 setcursor();
4262 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4263 ins_compl_del_pum();
4264
4265 ins_compl_show_pum();
4266 setcursor();
4267 RedrawingDisabled = n;
4268}
4269
4270/*
4271 * Looks in the first "len" chars. of "src" for search-metachars.
4272 * If dest is not NULL the chars. are copied there quoting (with
4273 * a backslash) the metachars, and dest would be NUL terminated.
4274 * Returns the length (needed) of dest
4275 */
4276 static unsigned
4277quote_meta(char_u *dest, char_u *src, int len)
4278{
4279 unsigned m = (unsigned)len + 1; // one extra for the NUL
4280
4281 for ( ; --len >= 0; src++)
4282 {
4283 switch (*src)
4284 {
4285 case '.':
4286 case '*':
4287 case '[':
4288 if (ctrl_x_mode == CTRL_X_DICTIONARY
4289 || ctrl_x_mode == CTRL_X_THESAURUS)
4290 break;
4291 // FALLTHROUGH
4292 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004293 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004294 break;
4295 // FALLTHROUGH
4296 case '\\':
4297 if (ctrl_x_mode == CTRL_X_DICTIONARY
4298 || ctrl_x_mode == CTRL_X_THESAURUS)
4299 break;
4300 // FALLTHROUGH
4301 case '^': // currently it's not needed.
4302 case '$':
4303 m++;
4304 if (dest != NULL)
4305 *dest++ = '\\';
4306 break;
4307 }
4308 if (dest != NULL)
4309 *dest++ = *src;
4310 // Copy remaining bytes of a multibyte character.
4311 if (has_mbyte)
4312 {
4313 int i, mb_len;
4314
4315 mb_len = (*mb_ptr2len)(src) - 1;
4316 if (mb_len > 0 && len >= mb_len)
4317 for (i = 0; i < mb_len; ++i)
4318 {
4319 --len;
4320 ++src;
4321 if (dest != NULL)
4322 *dest++ = *src;
4323 }
4324 }
4325 }
4326 if (dest != NULL)
4327 *dest = NUL;
4328
4329 return m;
4330}
4331
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004332#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004333 void
4334free_insexpand_stuff(void)
4335{
4336 VIM_CLEAR(compl_orig_text);
4337}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004338#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004339
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004340#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004341/*
4342 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4343 * spelled word, if there is one.
4344 */
4345 static void
4346spell_back_to_badword(void)
4347{
4348 pos_T tpos = curwin->w_cursor;
4349
4350 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4351 if (curwin->w_cursor.col != tpos.col)
4352 start_arrow(&tpos);
4353}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004354#endif