blob: eeff64ca2c1c2d7f142ab98fc79da61bbc075957 [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
16#ifdef FEAT_INS_EXPAND
17/*
18 * Definitions used for CTRL-X submode.
19 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
20 * ctrl_x_mode_names[] below.
21 */
22# define CTRL_X_WANT_IDENT 0x100
23
24# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
25# define CTRL_X_NOT_DEFINED_YET 1
26# define CTRL_X_SCROLL 2
27# define CTRL_X_WHOLE_LINE 3
28# define CTRL_X_FILES 4
29# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
31# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
32# define CTRL_X_FINISHED 8
33# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
34# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
35# define CTRL_X_CMDLINE 11
36# define CTRL_X_FUNCTION 12
37# define CTRL_X_OMNI 13
38# define CTRL_X_SPELL 14
39# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
40# define CTRL_X_EVAL 16 /* for builtin function complete() */
41
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.
64};
65
66static char *ctrl_x_mode_names[] = {
67 "keyword",
68 "ctrl_x",
69 "unknown", // CTRL_X_SCROLL
70 "whole_line",
71 "files",
72 "tags",
73 "path_patterns",
74 "path_defines",
75 "unknown", // CTRL_X_FINISHED
76 "dictionary",
77 "thesaurus",
78 "cmdline",
79 "function",
80 "omni",
81 "spell",
82 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
83 "eval"
84};
85
86/*
87 * Array indexes used for cp_text[].
88 */
89#define CPT_ABBR 0 // "abbr"
90#define CPT_MENU 1 // "menu"
91#define CPT_KIND 2 // "kind"
92#define CPT_INFO 3 // "info"
93#define CPT_USER_DATA 4 // "user data"
94#define CPT_COUNT 5 // Number of entries
95
96/*
97 * Structure used to store one match for insert completion.
98 */
99typedef struct compl_S compl_T;
100struct compl_S
101{
102 compl_T *cp_next;
103 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200104 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200105 char_u *(cp_text[CPT_COUNT]); // text for the menu
106 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200107 // cp_flags has CP_FREE_FNAME
108 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200109 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100110};
111
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200112// values for cp_flags
113# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
114# define CP_FREE_FNAME 2 // cp_fname is allocated
115# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
116# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
117# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100118
119static char e_hitend[] = N_("Hit end of paragraph");
120# ifdef FEAT_COMPL_FUNC
121static char e_complwin[] = N_("E839: Completion function changed window");
122static char e_compldel[] = N_("E840: Completion function deleted text");
123# endif
124
125/*
126 * All the current matches are stored in a list.
127 * "compl_first_match" points to the start of the list.
128 * "compl_curr_match" points to the currently selected entry.
129 * "compl_shown_match" is different from compl_curr_match during
130 * ins_compl_get_exp().
131 */
132static compl_T *compl_first_match = NULL;
133static compl_T *compl_curr_match = NULL;
134static compl_T *compl_shown_match = NULL;
135static compl_T *compl_old_match = NULL;
136
137// After using a cursor key <Enter> selects a match in the popup menu,
138// otherwise it inserts a line break.
139static int compl_enter_selects = FALSE;
140
141// When "compl_leader" is not NULL only matches that start with this string
142// are used.
143static char_u *compl_leader = NULL;
144
145static int compl_get_longest = FALSE; // put longest common string
146 // in compl_leader
147
148static int compl_no_insert = FALSE; // FALSE: select & insert
149 // TRUE: noinsert
150static int compl_no_select = FALSE; // FALSE: select & insert
151 // TRUE: noselect
152
153// Selected one of the matches. When FALSE the match was edited or using the
154// longest common string.
155static int compl_used_match;
156
157// didn't finish finding completions.
158static int compl_was_interrupted = FALSE;
159
160// Set when character typed while looking for matches and it means we should
161// stop looking for matches.
162static int compl_interrupted = FALSE;
163
164static int compl_restarting = FALSE; // don't insert match
165
166// When the first completion is done "compl_started" is set. When it's
167// FALSE the word to be completed must be located.
168static int compl_started = FALSE;
169
170// Which Ctrl-X mode are we in?
171static int ctrl_x_mode = CTRL_X_NORMAL;
172
173static int compl_matches = 0;
174static char_u *compl_pattern = NULL;
175static int compl_direction = FORWARD;
176static int compl_shows_dir = FORWARD;
177static int compl_pending = 0; // > 1 for postponed CTRL-N
178static pos_T compl_startpos;
179static colnr_T compl_col = 0; // column where the text starts
180 // that is being completed
181static char_u *compl_orig_text = NULL; // text as it was before
182 // completion started
183static int compl_cont_mode = 0;
184static expand_T compl_xp;
185
186static int compl_opt_refresh_always = FALSE;
187static int compl_opt_suppress_empty = FALSE;
188
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200189static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100190static void ins_compl_longest_match(compl_T *match);
191static void ins_compl_del_pum(void);
192static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
193static char_u *find_line_end(char_u *ptr);
194static void ins_compl_free(void);
195static char_u *ins_compl_mode(void);
196static int ins_compl_need_restart(void);
197static void ins_compl_new_leader(void);
198static int ins_compl_len(void);
199static void ins_compl_restart(void);
200static void ins_compl_set_original_text(char_u *str);
201static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
202# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
203static void ins_compl_add_list(list_T *list);
204static void ins_compl_add_dict(dict_T *dict);
205# endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200206static dict_T *ins_compl_dict_alloc(compl_T *match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100207static int ins_compl_key2dir(int c);
208static int ins_compl_pum_key(int c);
209static int ins_compl_key2count(int c);
210static void show_pum(int prev_w_wrow, int prev_w_leftcol);
211static unsigned quote_meta(char_u *dest, char_u *str, int len);
212#endif // FEAT_INS_EXPAND
213
214#ifdef FEAT_SPELL
215static void spell_back_to_badword(void);
216static int spell_bad_len = 0; // length of located bad word
217#endif
218
219#if defined(FEAT_INS_EXPAND) || defined(PROTO)
220/*
221 * CTRL-X pressed in Insert mode.
222 */
223 void
224ins_ctrl_x(void)
225{
226 // CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
227 // CTRL-V works like CTRL-N
228 if (ctrl_x_mode != CTRL_X_CMDLINE)
229 {
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 }
242}
243
244/*
245 * Functions to check the current CTRL-X mode.
246 */
247int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
248int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
249int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
250int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
251int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
252int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
253int ctrl_x_mode_path_patterns(void) {
254 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
255int ctrl_x_mode_path_defines(void) {
256 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
257int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
258int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
259int ctrl_x_mode_cmdline(void) { return ctrl_x_mode == CTRL_X_CMDLINE; }
260int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
261int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
262int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
263int ctrl_x_mode_line_or_eval(void) {
264 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
265
266/*
267 * Whether other than default completion has been selected.
268 */
269 int
270ctrl_x_mode_not_default(void)
271{
272 return ctrl_x_mode != CTRL_X_NORMAL;
273}
274
275/*
276 * Whether CTRL-X was typed without a following character.
277 */
278 int
279ctrl_x_mode_not_defined_yet(void)
280{
281 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
282}
283
284/*
285 * Return TRUE if the 'dict' or 'tsr' option can be used.
286 */
287 int
288has_compl_option(int dict_opt)
289{
290 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
291# ifdef FEAT_SPELL
292 && !curwin->w_p_spell
293# endif
294 )
295 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
296 {
297 ctrl_x_mode = CTRL_X_NORMAL;
298 edit_submode = NULL;
299 msg_attr(dict_opt ? _("'dictionary' option is empty")
300 : _("'thesaurus' option is empty"),
301 HL_ATTR(HLF_E));
302 if (emsg_silent == 0)
303 {
304 vim_beep(BO_COMPL);
305 setcursor();
306 out_flush();
307#ifdef FEAT_EVAL
308 if (!get_vim_var_nr(VV_TESTING))
309#endif
310 ui_delay(2000L, FALSE);
311 }
312 return FALSE;
313 }
314 return TRUE;
315}
316
317/*
318 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
319 * This depends on the current mode.
320 */
321 int
322vim_is_ctrl_x_key(int c)
323{
324 // Always allow ^R - let its results then be checked
325 if (c == Ctrl_R)
326 return TRUE;
327
328 // Accept <PageUp> and <PageDown> if the popup menu is visible.
329 if (ins_compl_pum_key(c))
330 return TRUE;
331
332 switch (ctrl_x_mode)
333 {
334 case 0: // Not in any CTRL-X mode
335 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
336 case CTRL_X_NOT_DEFINED_YET:
337 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
338 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
339 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
340 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
341 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
342 || c == Ctrl_S || c == Ctrl_K || c == 's');
343 case CTRL_X_SCROLL:
344 return (c == Ctrl_Y || c == Ctrl_E);
345 case CTRL_X_WHOLE_LINE:
346 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
347 case CTRL_X_FILES:
348 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
349 case CTRL_X_DICTIONARY:
350 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
351 case CTRL_X_THESAURUS:
352 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
353 case CTRL_X_TAGS:
354 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
355#ifdef FEAT_FIND_ID
356 case CTRL_X_PATH_PATTERNS:
357 return (c == Ctrl_P || c == Ctrl_N);
358 case CTRL_X_PATH_DEFINES:
359 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
360#endif
361 case CTRL_X_CMDLINE:
362 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
363 || c == Ctrl_X);
364#ifdef FEAT_COMPL_FUNC
365 case CTRL_X_FUNCTION:
366 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
367 case CTRL_X_OMNI:
368 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
369#endif
370 case CTRL_X_SPELL:
371 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
372 case CTRL_X_EVAL:
373 return (c == Ctrl_P || c == Ctrl_N);
374 }
375 internal_error("vim_is_ctrl_x_key()");
376 return FALSE;
377}
378
379/*
380 * Return TRUE when character "c" is part of the item currently being
381 * completed. Used to decide whether to abandon complete mode when the menu
382 * is visible.
383 */
384 int
385ins_compl_accept_char(int c)
386{
387 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
388 // When expanding an identifier only accept identifier chars.
389 return vim_isIDc(c);
390
391 switch (ctrl_x_mode)
392 {
393 case CTRL_X_FILES:
394 // When expanding file name only accept file name chars. But not
395 // path separators, so that "proto/<Tab>" expands files in
396 // "proto", not "proto/" as a whole
397 return vim_isfilec(c) && !vim_ispathsep(c);
398
399 case CTRL_X_CMDLINE:
400 case CTRL_X_OMNI:
401 // Command line and Omni completion can work with just about any
402 // printable character, but do stop at white space.
403 return vim_isprintc(c) && !VIM_ISWHITE(c);
404
405 case CTRL_X_WHOLE_LINE:
406 // For while line completion a space can be part of the line.
407 return vim_isprintc(c);
408 }
409 return vim_iswordc(c);
410}
411
412/*
413 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
414 * case of the originally typed text is used, and the case of the completed
415 * text is inferred, ie this tries to work out what case you probably wanted
416 * the rest of the word to be in -- webb
417 */
418 int
419ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200420 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100421 int len,
422 int icase,
423 char_u *fname,
424 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200425 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100426{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200427 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100428 char_u *p;
429 int i, c;
430 int actual_len; // Take multi-byte characters
431 int actual_compl_length; // into account.
432 int min_len;
433 int *wca; // Wide character array.
434 int has_lower = FALSE;
435 int was_letter = FALSE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200436 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437
438 if (p_ic && curbuf->b_p_inf && len > 0)
439 {
440 // Infer case of completed part.
441
442 // Find actual length of completion.
443 if (has_mbyte)
444 {
445 p = str;
446 actual_len = 0;
447 while (*p != NUL)
448 {
449 MB_PTR_ADV(p);
450 ++actual_len;
451 }
452 }
453 else
454 actual_len = len;
455
456 // Find actual length of original text.
457 if (has_mbyte)
458 {
459 p = compl_orig_text;
460 actual_compl_length = 0;
461 while (*p != NUL)
462 {
463 MB_PTR_ADV(p);
464 ++actual_compl_length;
465 }
466 }
467 else
468 actual_compl_length = compl_length;
469
470 // "actual_len" may be smaller than "actual_compl_length" when using
471 // thesaurus, only use the minimum when comparing.
472 min_len = actual_len < actual_compl_length
473 ? actual_len : actual_compl_length;
474
475 // Allocate wide character array for the completion and fill it.
476 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
477 if (wca != NULL)
478 {
479 p = str;
480 for (i = 0; i < actual_len; ++i)
481 if (has_mbyte)
482 wca[i] = mb_ptr2char_adv(&p);
483 else
484 wca[i] = *(p++);
485
486 // Rule 1: Were any chars converted to lower?
487 p = compl_orig_text;
488 for (i = 0; i < min_len; ++i)
489 {
490 if (has_mbyte)
491 c = mb_ptr2char_adv(&p);
492 else
493 c = *(p++);
494 if (MB_ISLOWER(c))
495 {
496 has_lower = TRUE;
497 if (MB_ISUPPER(wca[i]))
498 {
499 // Rule 1 is satisfied.
500 for (i = actual_compl_length; i < actual_len; ++i)
501 wca[i] = MB_TOLOWER(wca[i]);
502 break;
503 }
504 }
505 }
506
507 // Rule 2: No lower case, 2nd consecutive letter converted to
508 // upper case.
509 if (!has_lower)
510 {
511 p = compl_orig_text;
512 for (i = 0; i < min_len; ++i)
513 {
514 if (has_mbyte)
515 c = mb_ptr2char_adv(&p);
516 else
517 c = *(p++);
518 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
519 {
520 // Rule 2 is satisfied.
521 for (i = actual_compl_length; i < actual_len; ++i)
522 wca[i] = MB_TOUPPER(wca[i]);
523 break;
524 }
525 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
526 }
527 }
528
529 // Copy the original case of the part we typed.
530 p = compl_orig_text;
531 for (i = 0; i < min_len; ++i)
532 {
533 if (has_mbyte)
534 c = mb_ptr2char_adv(&p);
535 else
536 c = *(p++);
537 if (MB_ISLOWER(c))
538 wca[i] = MB_TOLOWER(wca[i]);
539 else if (MB_ISUPPER(c))
540 wca[i] = MB_TOUPPER(wca[i]);
541 }
542
543 // Generate encoding specific output from wide character array.
544 // Multi-byte characters can occupy up to five bytes more than
545 // ASCII characters, and we also need one byte for NUL, so stay
546 // six bytes away from the edge of IObuff.
547 p = IObuff;
548 i = 0;
549 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
550 if (has_mbyte)
551 p += (*mb_char2bytes)(wca[i++], p);
552 else
553 *(p++) = wca[i++];
554 *p = NUL;
555
556 vim_free(wca);
557 }
558
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200559 str = IObuff;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100560 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200561 if (cont_s_ipos)
562 flags |= CP_CONT_S_IPOS;
563 if (icase)
564 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200565
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200566 return ins_compl_add(str, len, fname, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100567}
568
569/*
570 * Add a match to the list of matches.
571 * If the given string is already in the list of completions, then return
572 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
573 * maybe because alloc() returns NULL, then FAIL is returned.
574 */
575 static int
576ins_compl_add(
577 char_u *str,
578 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100579 char_u *fname,
580 char_u **cptext, // extra text for popup menu or NULL
581 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200582 int flags_arg,
583 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100584{
585 compl_T *match;
586 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200587 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100588
589 ui_breakcheck();
590 if (got_int)
591 return FAIL;
592 if (len < 0)
593 len = (int)STRLEN(str);
594
595 // If the same match is already present, don't add it.
596 if (compl_first_match != NULL && !adup)
597 {
598 match = compl_first_match;
599 do
600 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200601 if ( !(match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100602 && STRNCMP(match->cp_str, str, len) == 0
603 && match->cp_str[len] == NUL)
604 return NOTDONE;
605 match = match->cp_next;
606 } while (match != NULL && match != compl_first_match);
607 }
608
609 // Remove any popup menu before changing the list of matches.
610 ins_compl_del_pum();
611
612 // Allocate a new match structure.
613 // Copy the values to the new match structure.
614 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
615 if (match == NULL)
616 return FAIL;
617 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200618 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100619 match->cp_number = 0;
620 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
621 {
622 vim_free(match);
623 return FAIL;
624 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100625
626 // match-fname is:
627 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200628 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100629 // - NULL otherwise. --Acevedo
630 if (fname != NULL
631 && compl_curr_match != NULL
632 && compl_curr_match->cp_fname != NULL
633 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
634 match->cp_fname = compl_curr_match->cp_fname;
635 else if (fname != NULL)
636 {
637 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200638 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100639 }
640 else
641 match->cp_fname = NULL;
642 match->cp_flags = flags;
643
644 if (cptext != NULL)
645 {
646 int i;
647
648 for (i = 0; i < CPT_COUNT; ++i)
649 if (cptext[i] != NULL && *cptext[i] != NUL)
650 match->cp_text[i] = vim_strsave(cptext[i]);
651 }
652
653 // Link the new match structure in the list of matches.
654 if (compl_first_match == NULL)
655 match->cp_next = match->cp_prev = NULL;
656 else if (dir == FORWARD)
657 {
658 match->cp_next = compl_curr_match->cp_next;
659 match->cp_prev = compl_curr_match;
660 }
661 else // BACKWARD
662 {
663 match->cp_next = compl_curr_match;
664 match->cp_prev = compl_curr_match->cp_prev;
665 }
666 if (match->cp_next)
667 match->cp_next->cp_prev = match;
668 if (match->cp_prev)
669 match->cp_prev->cp_next = match;
670 else // if there's nothing before, it is the first match
671 compl_first_match = match;
672 compl_curr_match = match;
673
674 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200675 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100676 ins_compl_longest_match(match);
677
678 return OK;
679}
680
681/*
682 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200683 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100684 */
685 static int
686ins_compl_equal(compl_T *match, char_u *str, int len)
687{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200688 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200689 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200690 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
692 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
693}
694
695/*
696 * Reduce the longest common string for match "match".
697 */
698 static void
699ins_compl_longest_match(compl_T *match)
700{
701 char_u *p, *s;
702 int c1, c2;
703 int had_match;
704
705 if (compl_leader == NULL)
706 {
707 // First match, use it as a whole.
708 compl_leader = vim_strsave(match->cp_str);
709 if (compl_leader != NULL)
710 {
711 had_match = (curwin->w_cursor.col > compl_col);
712 ins_compl_delete();
713 ins_bytes(compl_leader + ins_compl_len());
714 ins_redraw(FALSE);
715
716 // When the match isn't there (to avoid matching itself) remove it
717 // again after redrawing.
718 if (!had_match)
719 ins_compl_delete();
720 compl_used_match = FALSE;
721 }
722 }
723 else
724 {
725 // Reduce the text if this match differs from compl_leader.
726 p = compl_leader;
727 s = match->cp_str;
728 while (*p != NUL)
729 {
730 if (has_mbyte)
731 {
732 c1 = mb_ptr2char(p);
733 c2 = mb_ptr2char(s);
734 }
735 else
736 {
737 c1 = *p;
738 c2 = *s;
739 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200740 if ((match->cp_flags & CP_ICASE)
741 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100742 break;
743 if (has_mbyte)
744 {
745 MB_PTR_ADV(p);
746 MB_PTR_ADV(s);
747 }
748 else
749 {
750 ++p;
751 ++s;
752 }
753 }
754
755 if (*p != NUL)
756 {
757 // Leader was shortened, need to change the inserted text.
758 *p = NUL;
759 had_match = (curwin->w_cursor.col > compl_col);
760 ins_compl_delete();
761 ins_bytes(compl_leader + ins_compl_len());
762 ins_redraw(FALSE);
763
764 // When the match isn't there (to avoid matching itself) remove it
765 // again after redrawing.
766 if (!had_match)
767 ins_compl_delete();
768 }
769
770 compl_used_match = FALSE;
771 }
772}
773
774/*
775 * Add an array of matches to the list of matches.
776 * Frees matches[].
777 */
778 static void
779ins_compl_add_matches(
780 int num_matches,
781 char_u **matches,
782 int icase)
783{
784 int i;
785 int add_r = OK;
786 int dir = compl_direction;
787
788 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200789 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir,
790 icase ? CP_ICASE : 0, FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100791 // if dir was BACKWARD then honor it just once
792 dir = FORWARD;
793 FreeWild(num_matches, matches);
794}
795
796/*
797 * Make the completion list cyclic.
798 * Return the number of matches (excluding the original).
799 */
800 static int
801ins_compl_make_cyclic(void)
802{
803 compl_T *match;
804 int count = 0;
805
806 if (compl_first_match != NULL)
807 {
808 // Find the end of the list.
809 match = compl_first_match;
810 // there's always an entry for the compl_orig_text, it doesn't count.
811 while (match->cp_next != NULL && match->cp_next != compl_first_match)
812 {
813 match = match->cp_next;
814 ++count;
815 }
816 match->cp_next = compl_first_match;
817 compl_first_match->cp_prev = match;
818 }
819 return count;
820}
821
822/*
823 * Return whether there currently is a shown match.
824 */
825 int
826ins_compl_has_shown_match(void)
827{
828 return compl_shown_match == NULL
829 || compl_shown_match != compl_shown_match->cp_next;
830}
831
832/*
833 * Return whether the shown match is long enough.
834 */
835 int
836ins_compl_long_shown_match(void)
837{
838 return (int)STRLEN(compl_shown_match->cp_str)
839 > curwin->w_cursor.col - compl_col;
840}
841
842/*
843 * Set variables that store noselect and noinsert behavior from the
844 * 'completeopt' value.
845 */
846 void
847completeopt_was_set(void)
848{
849 compl_no_insert = FALSE;
850 compl_no_select = FALSE;
851 if (strstr((char *)p_cot, "noselect") != NULL)
852 compl_no_select = TRUE;
853 if (strstr((char *)p_cot, "noinsert") != NULL)
854 compl_no_insert = TRUE;
855}
856
857/*
858 * Start completion for the complete() function.
859 * "startcol" is where the matched text starts (1 is first column).
860 * "list" is the list of matches.
861 */
862 void
863set_completion(colnr_T startcol, list_T *list)
864{
865 int save_w_wrow = curwin->w_wrow;
866 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200867 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100868
869 // If already doing completions stop it.
870 if (ctrl_x_mode != CTRL_X_NORMAL)
871 ins_compl_prep(' ');
872 ins_compl_clear();
873 ins_compl_free();
874
875 compl_direction = FORWARD;
876 if (startcol > curwin->w_cursor.col)
877 startcol = curwin->w_cursor.col;
878 compl_col = startcol;
879 compl_length = (int)curwin->w_cursor.col - (int)startcol;
880 // compl_pattern doesn't need to be set
881 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200882 if (p_ic)
883 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100884 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200885 -1, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100886 return;
887
888 ctrl_x_mode = CTRL_X_EVAL;
889
890 ins_compl_add_list(list);
891 compl_matches = ins_compl_make_cyclic();
892 compl_started = TRUE;
893 compl_used_match = TRUE;
894 compl_cont_status = 0;
895
896 compl_curr_match = compl_first_match;
897 if (compl_no_insert || compl_no_select)
898 {
899 ins_complete(K_DOWN, FALSE);
900 if (compl_no_select)
901 // Down/Up has no real effect.
902 ins_complete(K_UP, FALSE);
903 }
904 else
905 ins_complete(Ctrl_N, FALSE);
906 compl_enter_selects = compl_no_insert;
907
908 // Lazily show the popup menu, unless we got interrupted.
909 if (!compl_interrupted)
910 show_pum(save_w_wrow, save_w_leftcol);
911 out_flush();
912}
913
914
915// "compl_match_array" points the currently displayed list of entries in the
916// popup menu. It is NULL when there is no popup menu.
917static pumitem_T *compl_match_array = NULL;
918static int compl_match_arraysize;
919
920/*
921 * Update the screen and when there is any scrolling remove the popup menu.
922 */
923 static void
924ins_compl_upd_pum(void)
925{
926 int h;
927
928 if (compl_match_array != NULL)
929 {
930 h = curwin->w_cline_height;
931 // Update the screen later, before drawing the popup menu over it.
932 pum_call_update_screen();
933 if (h != curwin->w_cline_height)
934 ins_compl_del_pum();
935 }
936}
937
938/*
939 * Remove any popup menu.
940 */
941 static void
942ins_compl_del_pum(void)
943{
944 if (compl_match_array != NULL)
945 {
946 pum_undisplay();
947 VIM_CLEAR(compl_match_array);
948 }
949}
950
951/*
952 * Return TRUE if the popup menu should be displayed.
953 */
954 int
955pum_wanted(void)
956{
957 // 'completeopt' must contain "menu" or "menuone"
958 if (vim_strchr(p_cot, 'm') == NULL)
959 return FALSE;
960
961 // The display looks bad on a B&W display.
962 if (t_colors < 8
963#ifdef FEAT_GUI
964 && !gui.in_use
965#endif
966 )
967 return FALSE;
968 return TRUE;
969}
970
971/*
972 * Return TRUE if there are two or more matches to be shown in the popup menu.
973 * One if 'completopt' contains "menuone".
974 */
975 static int
976pum_enough_matches(void)
977{
978 compl_T *compl;
979 int i;
980
981 // Don't display the popup menu if there are no matches or there is only
982 // one (ignoring the original text).
983 compl = compl_first_match;
984 i = 0;
985 do
986 {
987 if (compl == NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200988 || ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100989 break;
990 compl = compl->cp_next;
991 } while (compl != compl_first_match);
992
993 if (strstr((char *)p_cot, "menuone") != NULL)
994 return (i >= 1);
995 return (i >= 2);
996}
997
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200998 static void
999trigger_complete_changed_event(int cur)
1000{
1001 dict_T *v_event;
1002 dict_T *item;
1003 static int recursive = FALSE;
1004
1005 if (recursive)
1006 return;
1007
1008 v_event = get_vim_var_dict(VV_EVENT);
1009 if (cur < 0)
1010 item = dict_alloc();
1011 else
1012 item = ins_compl_dict_alloc(compl_curr_match);
1013 if (item == NULL)
1014 return;
1015 dict_add_dict(v_event, "completed_item", item);
1016 pum_set_event_info(v_event);
1017 dict_set_items_ro(v_event);
1018
1019 recursive = TRUE;
1020 textlock++;
1021 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
1022 textlock--;
1023 recursive = FALSE;
1024
1025 dict_free_contents(v_event);
1026 hash_init(&v_event->dv_hashtab);
1027}
1028
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001029/*
1030 * Show the popup menu for the list of matches.
1031 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1032 */
1033 void
1034ins_compl_show_pum(void)
1035{
1036 compl_T *compl;
1037 compl_T *shown_compl = NULL;
1038 int did_find_shown_match = FALSE;
1039 int shown_match_ok = FALSE;
1040 int i;
1041 int cur = -1;
1042 colnr_T col;
1043 int lead_len = 0;
1044
1045 if (!pum_wanted() || !pum_enough_matches())
1046 return;
1047
1048#if defined(FEAT_EVAL)
1049 // Dirty hard-coded hack: remove any matchparen highlighting.
1050 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
1051#endif
1052
1053 // Update the screen later, before drawing the popup menu over it.
1054 pum_call_update_screen();
1055
1056 if (compl_match_array == NULL)
1057 {
1058 // Need to build the popup menu list.
1059 compl_match_arraysize = 0;
1060 compl = compl_first_match;
1061 if (compl_leader != NULL)
1062 lead_len = (int)STRLEN(compl_leader);
1063 do
1064 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001065 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001066 && (compl_leader == NULL
1067 || ins_compl_equal(compl, compl_leader, lead_len)))
1068 ++compl_match_arraysize;
1069 compl = compl->cp_next;
1070 } while (compl != NULL && compl != compl_first_match);
1071 if (compl_match_arraysize == 0)
1072 return;
1073 compl_match_array = (pumitem_T *)alloc_clear(
1074 (unsigned)(sizeof(pumitem_T)
1075 * compl_match_arraysize));
1076 if (compl_match_array != NULL)
1077 {
1078 // If the current match is the original text don't find the first
1079 // match after it, don't highlight anything.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001080 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001081 shown_match_ok = TRUE;
1082
1083 i = 0;
1084 compl = compl_first_match;
1085 do
1086 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001087 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088 && (compl_leader == NULL
1089 || ins_compl_equal(compl, compl_leader, lead_len)))
1090 {
1091 if (!shown_match_ok)
1092 {
1093 if (compl == compl_shown_match || did_find_shown_match)
1094 {
1095 // This item is the shown match or this is the
1096 // first displayed item after the shown match.
1097 compl_shown_match = compl;
1098 did_find_shown_match = TRUE;
1099 shown_match_ok = TRUE;
1100 }
1101 else
1102 // Remember this displayed match for when the
1103 // shown match is just below it.
1104 shown_compl = compl;
1105 cur = i;
1106 }
1107
1108 if (compl->cp_text[CPT_ABBR] != NULL)
1109 compl_match_array[i].pum_text =
1110 compl->cp_text[CPT_ABBR];
1111 else
1112 compl_match_array[i].pum_text = compl->cp_str;
1113 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1114 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1115 if (compl->cp_text[CPT_MENU] != NULL)
1116 compl_match_array[i++].pum_extra =
1117 compl->cp_text[CPT_MENU];
1118 else
1119 compl_match_array[i++].pum_extra = compl->cp_fname;
1120 }
1121
1122 if (compl == compl_shown_match)
1123 {
1124 did_find_shown_match = TRUE;
1125
1126 // When the original text is the shown match don't set
1127 // compl_shown_match.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001128 if (compl->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001129 shown_match_ok = TRUE;
1130
1131 if (!shown_match_ok && shown_compl != NULL)
1132 {
1133 // The shown match isn't displayed, set it to the
1134 // previously displayed match.
1135 compl_shown_match = shown_compl;
1136 shown_match_ok = TRUE;
1137 }
1138 }
1139 compl = compl->cp_next;
1140 } while (compl != NULL && compl != compl_first_match);
1141
1142 if (!shown_match_ok) // no displayed match at all
1143 cur = -1;
1144 }
1145 }
1146 else
1147 {
1148 // popup menu already exists, only need to find the current item.
1149 for (i = 0; i < compl_match_arraysize; ++i)
1150 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1151 || compl_match_array[i].pum_text
1152 == compl_shown_match->cp_text[CPT_ABBR])
1153 {
1154 cur = i;
1155 break;
1156 }
1157 }
1158
1159 if (compl_match_array != NULL)
1160 {
1161 // In Replace mode when a $ is displayed at the end of the line only
1162 // part of the screen would be updated. We do need to redraw here.
1163 dollar_vcol = -1;
1164
1165 // Compute the screen column of the start of the completed text.
1166 // Use the cursor to get all wrapping and other settings right.
1167 col = curwin->w_cursor.col;
1168 curwin->w_cursor.col = compl_col;
1169 pum_display(compl_match_array, compl_match_arraysize, cur);
1170 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001171
1172 if (has_completechanged())
1173 trigger_complete_changed_event(cur);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001174 }
1175}
1176
1177#define DICT_FIRST (1) // use just first element in "dict"
1178#define DICT_EXACT (2) // "dict" is the exact name of a file
1179
1180/*
1181 * Add any identifiers that match the given pattern in the list of dictionary
1182 * files "dict_start" to the list of completions.
1183 */
1184 static void
1185ins_compl_dictionaries(
1186 char_u *dict_start,
1187 char_u *pat,
1188 int flags, // DICT_FIRST and/or DICT_EXACT
1189 int thesaurus) // Thesaurus completion
1190{
1191 char_u *dict = dict_start;
1192 char_u *ptr;
1193 char_u *buf;
1194 regmatch_T regmatch;
1195 char_u **files;
1196 int count;
1197 int save_p_scs;
1198 int dir = compl_direction;
1199
1200 if (*dict == NUL)
1201 {
1202#ifdef FEAT_SPELL
1203 // When 'dictionary' is empty and spell checking is enabled use
1204 // "spell".
1205 if (!thesaurus && curwin->w_p_spell)
1206 dict = (char_u *)"spell";
1207 else
1208#endif
1209 return;
1210 }
1211
1212 buf = alloc(LSIZE);
1213 if (buf == NULL)
1214 return;
1215 regmatch.regprog = NULL; // so that we can goto theend
1216
1217 // If 'infercase' is set, don't use 'smartcase' here
1218 save_p_scs = p_scs;
1219 if (curbuf->b_p_inf)
1220 p_scs = FALSE;
1221
1222 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1223 // to only match at the start of a line. Otherwise just match the
1224 // pattern. Also need to double backslashes.
1225 if (ctrl_x_mode_line_or_eval())
1226 {
1227 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1228 size_t len;
1229
1230 if (pat_esc == NULL)
1231 goto theend;
1232 len = STRLEN(pat_esc) + 10;
1233 ptr = alloc((unsigned)len);
1234 if (ptr == NULL)
1235 {
1236 vim_free(pat_esc);
1237 goto theend;
1238 }
1239 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1240 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1241 vim_free(pat_esc);
1242 vim_free(ptr);
1243 }
1244 else
1245 {
1246 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
1247 if (regmatch.regprog == NULL)
1248 goto theend;
1249 }
1250
1251 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1252 regmatch.rm_ic = ignorecase(pat);
1253 while (*dict != NUL && !got_int && !compl_interrupted)
1254 {
1255 // copy one dictionary file name into buf
1256 if (flags == DICT_EXACT)
1257 {
1258 count = 1;
1259 files = &dict;
1260 }
1261 else
1262 {
1263 // Expand wildcards in the dictionary name, but do not allow
1264 // backticks (for security, the 'dict' option may have been set in
1265 // a modeline).
1266 copy_option_part(&dict, buf, LSIZE, ",");
1267# ifdef FEAT_SPELL
1268 if (!thesaurus && STRCMP(buf, "spell") == 0)
1269 count = -1;
1270 else
1271# endif
1272 if (vim_strchr(buf, '`') != NULL
1273 || expand_wildcards(1, &buf, &count, &files,
1274 EW_FILE|EW_SILENT) != OK)
1275 count = 0;
1276 }
1277
1278# ifdef FEAT_SPELL
1279 if (count == -1)
1280 {
1281 // Complete from active spelling. Skip "\<" in the pattern, we
1282 // don't use it as a RE.
1283 if (pat[0] == '\\' && pat[1] == '<')
1284 ptr = pat + 2;
1285 else
1286 ptr = pat;
1287 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1288 }
1289 else
1290# endif
1291 if (count > 0) // avoid warning for using "files" uninit
1292 {
1293 ins_compl_files(count, files, thesaurus, flags,
1294 &regmatch, buf, &dir);
1295 if (flags != DICT_EXACT)
1296 FreeWild(count, files);
1297 }
1298 if (flags != 0)
1299 break;
1300 }
1301
1302theend:
1303 p_scs = save_p_scs;
1304 vim_regfree(regmatch.regprog);
1305 vim_free(buf);
1306}
1307
1308 static void
1309ins_compl_files(
1310 int count,
1311 char_u **files,
1312 int thesaurus,
1313 int flags,
1314 regmatch_T *regmatch,
1315 char_u *buf,
1316 int *dir)
1317{
1318 char_u *ptr;
1319 int i;
1320 FILE *fp;
1321 int add_r;
1322
1323 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1324 {
1325 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1326 if (flags != DICT_EXACT)
1327 {
1328 vim_snprintf((char *)IObuff, IOSIZE,
1329 _("Scanning dictionary: %s"), (char *)files[i]);
1330 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1331 }
1332
1333 if (fp != NULL)
1334 {
1335 // Read dictionary file line by line.
1336 // Check each line for a match.
1337 while (!got_int && !compl_interrupted
1338 && !vim_fgets(buf, LSIZE, fp))
1339 {
1340 ptr = buf;
1341 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
1342 {
1343 ptr = regmatch->startp[0];
1344 if (ctrl_x_mode_line_or_eval())
1345 ptr = find_line_end(ptr);
1346 else
1347 ptr = find_word_end(ptr);
1348 add_r = ins_compl_add_infercase(regmatch->startp[0],
1349 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001350 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001351 if (thesaurus)
1352 {
1353 char_u *wstart;
1354
1355 // Add the other matches on the line
1356 ptr = buf;
1357 while (!got_int)
1358 {
1359 // Find start of the next word. Skip white
1360 // space and punctuation.
1361 ptr = find_word_start(ptr);
1362 if (*ptr == NUL || *ptr == NL)
1363 break;
1364 wstart = ptr;
1365
1366 // Find end of the word.
1367 if (has_mbyte)
1368 // Japanese words may have characters in
1369 // different classes, only separate words
1370 // with single-byte non-word characters.
1371 while (*ptr != NUL)
1372 {
1373 int l = (*mb_ptr2len)(ptr);
1374
1375 if (l < 2 && !vim_iswordc(*ptr))
1376 break;
1377 ptr += l;
1378 }
1379 else
1380 ptr = find_word_end(ptr);
1381
1382 // Add the word. Skip the regexp match.
1383 if (wstart != regmatch->startp[0])
1384 add_r = ins_compl_add_infercase(wstart,
1385 (int)(ptr - wstart),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001386 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001387 }
1388 }
1389 if (add_r == OK)
1390 // if dir was BACKWARD then honor it just once
1391 *dir = FORWARD;
1392 else if (add_r == FAIL)
1393 break;
1394 // avoid expensive call to vim_regexec() when at end
1395 // of line
1396 if (*ptr == '\n' || got_int)
1397 break;
1398 }
1399 line_breakcheck();
1400 ins_compl_check_keys(50, FALSE);
1401 }
1402 fclose(fp);
1403 }
1404 }
1405}
1406
1407/*
1408 * Find the start of the next word.
1409 * Returns a pointer to the first char of the word. Also stops at a NUL.
1410 */
1411 char_u *
1412find_word_start(char_u *ptr)
1413{
1414 if (has_mbyte)
1415 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1416 ptr += (*mb_ptr2len)(ptr);
1417 else
1418 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1419 ++ptr;
1420 return ptr;
1421}
1422
1423/*
1424 * Find the end of the word. Assumes it starts inside a word.
1425 * Returns a pointer to just after the word.
1426 */
1427 char_u *
1428find_word_end(char_u *ptr)
1429{
1430 int start_class;
1431
1432 if (has_mbyte)
1433 {
1434 start_class = mb_get_class(ptr);
1435 if (start_class > 1)
1436 while (*ptr != NUL)
1437 {
1438 ptr += (*mb_ptr2len)(ptr);
1439 if (mb_get_class(ptr) != start_class)
1440 break;
1441 }
1442 }
1443 else
1444 while (vim_iswordc(*ptr))
1445 ++ptr;
1446 return ptr;
1447}
1448
1449/*
1450 * Find the end of the line, omitting CR and NL at the end.
1451 * Returns a pointer to just after the line.
1452 */
1453 static char_u *
1454find_line_end(char_u *ptr)
1455{
1456 char_u *s;
1457
1458 s = ptr + STRLEN(ptr);
1459 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1460 --s;
1461 return s;
1462}
1463
1464/*
1465 * Free the list of completions
1466 */
1467 static void
1468ins_compl_free(void)
1469{
1470 compl_T *match;
1471 int i;
1472
1473 VIM_CLEAR(compl_pattern);
1474 VIM_CLEAR(compl_leader);
1475
1476 if (compl_first_match == NULL)
1477 return;
1478
1479 ins_compl_del_pum();
1480 pum_clear();
1481
1482 compl_curr_match = compl_first_match;
1483 do
1484 {
1485 match = compl_curr_match;
1486 compl_curr_match = compl_curr_match->cp_next;
1487 vim_free(match->cp_str);
1488 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001489 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001490 vim_free(match->cp_fname);
1491 for (i = 0; i < CPT_COUNT; ++i)
1492 vim_free(match->cp_text[i]);
1493 vim_free(match);
1494 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
1495 compl_first_match = compl_curr_match = NULL;
1496 compl_shown_match = NULL;
1497 compl_old_match = NULL;
1498}
1499
1500 void
1501ins_compl_clear(void)
1502{
1503 compl_cont_status = 0;
1504 compl_started = FALSE;
1505 compl_matches = 0;
1506 VIM_CLEAR(compl_pattern);
1507 VIM_CLEAR(compl_leader);
1508 edit_submode_extra = NULL;
1509 VIM_CLEAR(compl_orig_text);
1510 compl_enter_selects = FALSE;
1511 // clear v:completed_item
1512 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
1513}
1514
1515/*
1516 * Return TRUE when Insert completion is active.
1517 */
1518 int
1519ins_compl_active(void)
1520{
1521 return compl_started;
1522}
1523
1524/*
1525 * Get complete information
1526 */
1527 void
1528get_complete_info(list_T *what_list, dict_T *retdict)
1529{
1530 int ret = OK;
1531 listitem_T *item;
1532#define CI_WHAT_MODE 0x01
1533#define CI_WHAT_PUM_VISIBLE 0x02
1534#define CI_WHAT_ITEMS 0x04
1535#define CI_WHAT_SELECTED 0x08
1536#define CI_WHAT_INSERTED 0x10
1537#define CI_WHAT_ALL 0xff
1538 int what_flag;
1539
1540 if (what_list == NULL)
1541 what_flag = CI_WHAT_ALL;
1542 else
1543 {
1544 what_flag = 0;
1545 for (item = what_list->lv_first; item != NULL; item = item->li_next)
1546 {
1547 char_u *what = tv_get_string(&item->li_tv);
1548
1549 if (STRCMP(what, "mode") == 0)
1550 what_flag |= CI_WHAT_MODE;
1551 else if (STRCMP(what, "pum_visible") == 0)
1552 what_flag |= CI_WHAT_PUM_VISIBLE;
1553 else if (STRCMP(what, "items") == 0)
1554 what_flag |= CI_WHAT_ITEMS;
1555 else if (STRCMP(what, "selected") == 0)
1556 what_flag |= CI_WHAT_SELECTED;
1557 else if (STRCMP(what, "inserted") == 0)
1558 what_flag |= CI_WHAT_INSERTED;
1559 }
1560 }
1561
1562 if (ret == OK && (what_flag & CI_WHAT_MODE))
1563 ret = dict_add_string(retdict, "mode", ins_compl_mode());
1564
1565 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
1566 ret = dict_add_number(retdict, "pum_visible", pum_visible());
1567
1568 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
1569 {
1570 list_T *li;
1571 dict_T *di;
1572 compl_T *match;
1573
1574 li = list_alloc();
1575 if (li == NULL)
1576 return;
1577 ret = dict_add_list(retdict, "items", li);
1578 if (ret == OK && compl_first_match != NULL)
1579 {
1580 match = compl_first_match;
1581 do
1582 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001583 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001584 {
1585 di = dict_alloc();
1586 if (di == NULL)
1587 return;
1588 ret = list_append_dict(li, di);
1589 if (ret != OK)
1590 return;
1591 dict_add_string(di, "word", match->cp_str);
1592 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
1593 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
1594 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
1595 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
1596 dict_add_string(di, "user_data",
1597 match->cp_text[CPT_USER_DATA]);
1598 }
1599 match = match->cp_next;
1600 }
1601 while (match != NULL && match != compl_first_match);
1602 }
1603 }
1604
1605 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
1606 ret = dict_add_number(retdict, "selected", (compl_curr_match != NULL) ?
1607 compl_curr_match->cp_number - 1 : -1);
1608
1609 // TODO
1610 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
1611}
1612
1613/*
1614 * Return Insert completion mode name string
1615 */
1616 static char_u *
1617ins_compl_mode(void)
1618{
1619 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
1620 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
1621
1622 return (char_u *)"";
1623}
1624
1625/*
1626 * Selected one of the matches. When FALSE the match was edited or using the
1627 * longest common string.
1628 */
1629 int
1630ins_compl_used_match(void)
1631{
1632 return compl_used_match;
1633}
1634
1635/*
1636 * Initialize get longest common string.
1637 */
1638 void
1639ins_compl_init_get_longest(void)
1640{
1641 compl_get_longest = FALSE;
1642}
1643
1644/*
1645 * Returns TRUE when insert completion is interrupted.
1646 */
1647 int
1648ins_compl_interrupted(void)
1649{
1650 return compl_interrupted;
1651}
1652
1653/*
1654 * Returns TRUE if the <Enter> key selects a match in the completion popup
1655 * menu.
1656 */
1657 int
1658ins_compl_enter_selects(void)
1659{
1660 return compl_enter_selects;
1661}
1662
1663/*
1664 * Return the column where the text starts that is being completed
1665 */
1666 colnr_T
1667ins_compl_col(void)
1668{
1669 return compl_col;
1670}
1671
1672/*
1673 * Delete one character before the cursor and show the subset of the matches
1674 * that match the word that is now before the cursor.
1675 * Returns the character to be used, NUL if the work is done and another char
1676 * to be got from the user.
1677 */
1678 int
1679ins_compl_bs(void)
1680{
1681 char_u *line;
1682 char_u *p;
1683
1684 line = ml_get_curline();
1685 p = line + curwin->w_cursor.col;
1686 MB_PTR_BACK(line, p);
1687
1688 // Stop completion when the whole word was deleted. For Omni completion
1689 // allow the word to be deleted, we won't match everything.
1690 // Respect the 'backspace' option.
1691 if ((int)(p - line) - (int)compl_col < 0
1692 || ((int)(p - line) - (int)compl_col == 0
1693 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
1694 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1695 - compl_length < 0))
1696 return K_BS;
1697
1698 // Deleted more than what was used to find matches or didn't finish
1699 // finding all matches: need to look for matches all over again.
1700 if (curwin->w_cursor.col <= compl_col + compl_length
1701 || ins_compl_need_restart())
1702 ins_compl_restart();
1703
1704 vim_free(compl_leader);
1705 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
1706 if (compl_leader != NULL)
1707 {
1708 ins_compl_new_leader();
1709 if (compl_shown_match != NULL)
1710 // Make sure current match is not a hidden item.
1711 compl_curr_match = compl_shown_match;
1712 return NUL;
1713 }
1714 return K_BS;
1715}
1716
1717/*
1718 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1719 * be called.
1720 */
1721 static int
1722ins_compl_need_restart(void)
1723{
1724 // Return TRUE if we didn't complete finding matches or when the
1725 // 'completefunc' returned "always" in the "refresh" dictionary item.
1726 return compl_was_interrupted
1727 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
1728 && compl_opt_refresh_always);
1729}
1730
1731/*
1732 * Called after changing "compl_leader".
1733 * Show the popup menu with a different set of matches.
1734 * May also search for matches again if the previous search was interrupted.
1735 */
1736 static void
1737ins_compl_new_leader(void)
1738{
1739 ins_compl_del_pum();
1740 ins_compl_delete();
1741 ins_bytes(compl_leader + ins_compl_len());
1742 compl_used_match = FALSE;
1743
1744 if (compl_started)
1745 ins_compl_set_original_text(compl_leader);
1746 else
1747 {
1748#ifdef FEAT_SPELL
1749 spell_bad_len = 0; // need to redetect bad word
1750#endif
1751 // Matches were cleared, need to search for them now. Befor drawing
1752 // the popup menu display the changed text before the cursor. Set
1753 // "compl_restarting" to avoid that the first match is inserted.
1754 pum_call_update_screen();
1755#ifdef FEAT_GUI
1756 if (gui.in_use)
1757 {
1758 // Show the cursor after the match, not after the redrawn text.
1759 setcursor();
1760 out_flush_cursor(FALSE, FALSE);
1761 }
1762#endif
1763 compl_restarting = TRUE;
1764 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1765 compl_cont_status = 0;
1766 compl_restarting = FALSE;
1767 }
1768
1769 compl_enter_selects = !compl_used_match;
1770
1771 // Show the popup menu with a different set of matches.
1772 ins_compl_show_pum();
1773
1774 // Don't let Enter select the original text when there is no popup menu.
1775 if (compl_match_array == NULL)
1776 compl_enter_selects = FALSE;
1777}
1778
1779/*
1780 * Return the length of the completion, from the completion start column to
1781 * the cursor column. Making sure it never goes below zero.
1782 */
1783 static int
1784ins_compl_len(void)
1785{
1786 int off = (int)curwin->w_cursor.col - (int)compl_col;
1787
1788 if (off < 0)
1789 return 0;
1790 return off;
1791}
1792
1793/*
1794 * Append one character to the match leader. May reduce the number of
1795 * matches.
1796 */
1797 void
1798ins_compl_addleader(int c)
1799{
1800 int cc;
1801
1802 if (stop_arrow() == FAIL)
1803 return;
1804 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1805 {
1806 char_u buf[MB_MAXBYTES + 1];
1807
1808 (*mb_char2bytes)(c, buf);
1809 buf[cc] = NUL;
1810 ins_char_bytes(buf, cc);
1811 if (compl_opt_refresh_always)
1812 AppendToRedobuff(buf);
1813 }
1814 else
1815 {
1816 ins_char(c);
1817 if (compl_opt_refresh_always)
1818 AppendCharToRedobuff(c);
1819 }
1820
1821 // If we didn't complete finding matches we must search again.
1822 if (ins_compl_need_restart())
1823 ins_compl_restart();
1824
1825 // When 'always' is set, don't reset compl_leader. While completing,
1826 // cursor doesn't point original position, changing compl_leader would
1827 // break redo.
1828 if (!compl_opt_refresh_always)
1829 {
1830 vim_free(compl_leader);
1831 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
1832 (int)(curwin->w_cursor.col - compl_col));
1833 if (compl_leader != NULL)
1834 ins_compl_new_leader();
1835 }
1836}
1837
1838/*
1839 * Setup for finding completions again without leaving CTRL-X mode. Used when
1840 * BS or a key was typed while still searching for matches.
1841 */
1842 static void
1843ins_compl_restart(void)
1844{
1845 ins_compl_free();
1846 compl_started = FALSE;
1847 compl_matches = 0;
1848 compl_cont_status = 0;
1849 compl_cont_mode = 0;
1850}
1851
1852/*
1853 * Set the first match, the original text.
1854 */
1855 static void
1856ins_compl_set_original_text(char_u *str)
1857{
1858 char_u *p;
1859
1860 // Replace the original text entry.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001861 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001862 // at the last item for backward completion
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001863 if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001864 {
1865 p = vim_strsave(str);
1866 if (p != NULL)
1867 {
1868 vim_free(compl_first_match->cp_str);
1869 compl_first_match->cp_str = p;
1870 }
1871 }
1872 else if (compl_first_match->cp_prev != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001873 && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001874 {
1875 p = vim_strsave(str);
1876 if (p != NULL)
1877 {
1878 vim_free(compl_first_match->cp_prev->cp_str);
1879 compl_first_match->cp_prev->cp_str = p;
1880 }
1881 }
1882}
1883
1884/*
1885 * Append one character to the match leader. May reduce the number of
1886 * matches.
1887 */
1888 void
1889ins_compl_addfrommatch(void)
1890{
1891 char_u *p;
1892 int len = (int)curwin->w_cursor.col - (int)compl_col;
1893 int c;
1894 compl_T *cp;
1895
1896 p = compl_shown_match->cp_str;
1897 if ((int)STRLEN(p) <= len) // the match is too short
1898 {
1899 // When still at the original match use the first entry that matches
1900 // the leader.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001901 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001902 {
1903 p = NULL;
1904 for (cp = compl_shown_match->cp_next; cp != NULL
1905 && cp != compl_first_match; cp = cp->cp_next)
1906 {
1907 if (compl_leader == NULL
1908 || ins_compl_equal(cp, compl_leader,
1909 (int)STRLEN(compl_leader)))
1910 {
1911 p = cp->cp_str;
1912 break;
1913 }
1914 }
1915 if (p == NULL || (int)STRLEN(p) <= len)
1916 return;
1917 }
1918 else
1919 return;
1920 }
1921 p += len;
1922 c = PTR2CHAR(p);
1923 ins_compl_addleader(c);
1924}
1925
1926/*
1927 * Prepare for Insert mode completion, or stop it.
1928 * Called just after typing a character in Insert mode.
1929 * Returns TRUE when the character is not to be inserted;
1930 */
1931 int
1932ins_compl_prep(int c)
1933{
1934 char_u *ptr;
1935 int want_cindent;
1936 int retval = FALSE;
1937
1938 // Forget any previous 'special' messages if this is actually
1939 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
1940 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
1941 edit_submode_extra = NULL;
1942
1943 // Ignore end of Select mode mapping and mouse scroll buttons.
1944 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
1945 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
1946 return retval;
1947
1948 // Set "compl_get_longest" when finding the first matches.
1949 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
1950 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
1951 {
1952 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
1953 compl_used_match = TRUE;
1954
1955 }
1956
1957 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
1958 {
1959 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
1960 // it will be yet. Now we decide.
1961 switch (c)
1962 {
1963 case Ctrl_E:
1964 case Ctrl_Y:
1965 ctrl_x_mode = CTRL_X_SCROLL;
1966 if (!(State & REPLACE_FLAG))
1967 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1968 else
1969 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1970 edit_submode_pre = NULL;
1971 showmode();
1972 break;
1973 case Ctrl_L:
1974 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1975 break;
1976 case Ctrl_F:
1977 ctrl_x_mode = CTRL_X_FILES;
1978 break;
1979 case Ctrl_K:
1980 ctrl_x_mode = CTRL_X_DICTIONARY;
1981 break;
1982 case Ctrl_R:
1983 // Simply allow ^R to happen without affecting ^X mode
1984 break;
1985 case Ctrl_T:
1986 ctrl_x_mode = CTRL_X_THESAURUS;
1987 break;
1988#ifdef FEAT_COMPL_FUNC
1989 case Ctrl_U:
1990 ctrl_x_mode = CTRL_X_FUNCTION;
1991 break;
1992 case Ctrl_O:
1993 ctrl_x_mode = CTRL_X_OMNI;
1994 break;
1995#endif
1996 case 's':
1997 case Ctrl_S:
1998 ctrl_x_mode = CTRL_X_SPELL;
1999#ifdef FEAT_SPELL
2000 ++emsg_off; // Avoid getting the E756 error twice.
2001 spell_back_to_badword();
2002 --emsg_off;
2003#endif
2004 break;
2005 case Ctrl_RSB:
2006 ctrl_x_mode = CTRL_X_TAGS;
2007 break;
2008#ifdef FEAT_FIND_ID
2009 case Ctrl_I:
2010 case K_S_TAB:
2011 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2012 break;
2013 case Ctrl_D:
2014 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2015 break;
2016#endif
2017 case Ctrl_V:
2018 case Ctrl_Q:
2019 ctrl_x_mode = CTRL_X_CMDLINE;
2020 break;
2021 case Ctrl_P:
2022 case Ctrl_N:
2023 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2024 // just started ^X mode, or there were enough ^X's to cancel
2025 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2026 // do normal expansion when interrupting a different mode (say
2027 // ^X^F^X^P or ^P^X^X^P, see below)
2028 // nothing changes if interrupting mode 0, (eg, the flag
2029 // doesn't change when going to ADDING mode -- Acevedo
2030 if (!(compl_cont_status & CONT_INTRPT))
2031 compl_cont_status |= CONT_LOCAL;
2032 else if (compl_cont_mode != 0)
2033 compl_cont_status &= ~CONT_LOCAL;
2034 // FALLTHROUGH
2035 default:
2036 // If we have typed at least 2 ^X's... for modes != 0, we set
2037 // compl_cont_status = 0 (eg, as if we had just started ^X
2038 // mode).
2039 // For mode 0, we set "compl_cont_mode" to an impossible
2040 // value, in both cases ^X^X can be used to restart the same
2041 // mode (avoiding ADDING mode).
2042 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2043 // 'complete' and local ^P expansions respectively.
2044 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2045 // mode -- Acevedo
2046 if (c == Ctrl_X)
2047 {
2048 if (compl_cont_mode != 0)
2049 compl_cont_status = 0;
2050 else
2051 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2052 }
2053 ctrl_x_mode = CTRL_X_NORMAL;
2054 edit_submode = NULL;
2055 showmode();
2056 break;
2057 }
2058 }
2059 else if (ctrl_x_mode != CTRL_X_NORMAL)
2060 {
2061 // We're already in CTRL-X mode, do we stay in it?
2062 if (!vim_is_ctrl_x_key(c))
2063 {
2064 if (ctrl_x_mode == CTRL_X_SCROLL)
2065 ctrl_x_mode = CTRL_X_NORMAL;
2066 else
2067 ctrl_x_mode = CTRL_X_FINISHED;
2068 edit_submode = NULL;
2069 }
2070 showmode();
2071 }
2072
2073 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2074 {
2075 // Show error message from attempted keyword completion (probably
2076 // 'Pattern not found') until another key is hit, then go back to
2077 // showing what mode we are in.
2078 showmode();
2079 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
2080 && c != Ctrl_R && !ins_compl_pum_key(c))
2081 || ctrl_x_mode == CTRL_X_FINISHED)
2082 {
2083 // Get here when we have finished typing a sequence of ^N and
2084 // ^P or other completion characters in CTRL-X mode. Free up
2085 // memory that was used, and make sure we can redo the insert.
2086 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2087 {
2088 // If any of the original typed text has been changed, eg when
2089 // ignorecase is set, we must add back-spaces to the redo
2090 // buffer. We add as few as necessary to delete just the part
2091 // of the original text that has changed.
2092 // When using the longest match, edited the match or used
2093 // CTRL-E then don't use the current match.
2094 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2095 ptr = compl_curr_match->cp_str;
2096 else
2097 ptr = NULL;
2098 ins_compl_fixRedoBufForLeader(ptr);
2099 }
2100
2101#ifdef FEAT_CINDENT
2102 want_cindent = (can_cindent_get() && cindent_on());
2103#endif
2104 // When completing whole lines: fix indent for 'cindent'.
2105 // Otherwise, break line if it's too long.
2106 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2107 {
2108#ifdef FEAT_CINDENT
2109 // re-indent the current line
2110 if (want_cindent)
2111 {
2112 do_c_expr_indent();
2113 want_cindent = FALSE; // don't do it again
2114 }
2115#endif
2116 }
2117 else
2118 {
2119 int prev_col = curwin->w_cursor.col;
2120
2121 // put the cursor on the last char, for 'tw' formatting
2122 if (prev_col > 0)
2123 dec_cursor();
2124 // only format when something was inserted
2125 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2126 insertchar(NUL, 0, -1);
2127 if (prev_col > 0
2128 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2129 inc_cursor();
2130 }
2131
2132 // If the popup menu is displayed pressing CTRL-Y means accepting
2133 // the selection without inserting anything. When
2134 // compl_enter_selects is set the Enter key does the same.
2135 if ((c == Ctrl_Y || (compl_enter_selects
2136 && (c == CAR || c == K_KENTER || c == NL)))
2137 && pum_visible())
2138 retval = TRUE;
2139
2140 // CTRL-E means completion is Ended, go back to the typed text.
2141 // but only do this, if the Popup is still visible
2142 if (c == Ctrl_E)
2143 {
2144 ins_compl_delete();
2145 if (compl_leader != NULL)
2146 ins_bytes(compl_leader + ins_compl_len());
2147 else if (compl_first_match != NULL)
2148 ins_bytes(compl_orig_text + ins_compl_len());
2149 retval = TRUE;
2150 }
2151
2152 auto_format(FALSE, TRUE);
2153
2154 ins_compl_free();
2155 compl_started = FALSE;
2156 compl_matches = 0;
2157 if (!shortmess(SHM_COMPLETIONMENU))
2158 msg_clr_cmdline(); // necessary for "noshowmode"
2159 ctrl_x_mode = CTRL_X_NORMAL;
2160 compl_enter_selects = FALSE;
2161 if (edit_submode != NULL)
2162 {
2163 edit_submode = NULL;
2164 showmode();
2165 }
2166
2167#ifdef FEAT_CMDWIN
2168 if (c == Ctrl_C && cmdwin_type != 0)
2169 // Avoid the popup menu remains displayed when leaving the
2170 // command line window.
2171 update_screen(0);
2172#endif
2173#ifdef FEAT_CINDENT
2174 // Indent now if a key was typed that is in 'cinkeys'.
2175 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2176 do_c_expr_indent();
2177#endif
2178 // Trigger the CompleteDone event to give scripts a chance to act
2179 // upon the completion.
2180 ins_apply_autocmds(EVENT_COMPLETEDONE);
2181 }
2182 }
2183 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2184 // Trigger the CompleteDone event to give scripts a chance to act
2185 // upon the (possibly failed) completion.
2186 ins_apply_autocmds(EVENT_COMPLETEDONE);
2187
2188 // reset continue_* if we left expansion-mode, if we stay they'll be
2189 // (re)set properly in ins_complete()
2190 if (!vim_is_ctrl_x_key(c))
2191 {
2192 compl_cont_status = 0;
2193 compl_cont_mode = 0;
2194 }
2195
2196 return retval;
2197}
2198
2199/*
2200 * Fix the redo buffer for the completion leader replacing some of the typed
2201 * text. This inserts backspaces and appends the changed text.
2202 * "ptr" is the known leader text or NUL.
2203 */
2204 static void
2205ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2206{
2207 int len;
2208 char_u *p;
2209 char_u *ptr = ptr_arg;
2210
2211 if (ptr == NULL)
2212 {
2213 if (compl_leader != NULL)
2214 ptr = compl_leader;
2215 else
2216 return; // nothing to do
2217 }
2218 if (compl_orig_text != NULL)
2219 {
2220 p = compl_orig_text;
2221 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2222 ;
2223 if (len > 0)
2224 len -= (*mb_head_off)(p, p + len);
2225 for (p += len; *p != NUL; MB_PTR_ADV(p))
2226 AppendCharToRedobuff(K_BS);
2227 }
2228 else
2229 len = 0;
2230 if (ptr != NULL)
2231 AppendToRedobuffLit(ptr + len, -1);
2232}
2233
2234/*
2235 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2236 * (depending on flag) starting from buf and looking for a non-scanned
2237 * buffer (other than curbuf). curbuf is special, if it is called with
2238 * buf=curbuf then it has to be the first call for a given flag/expansion.
2239 *
2240 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2241 */
2242 static buf_T *
2243ins_compl_next_buf(buf_T *buf, int flag)
2244{
2245 static win_T *wp = NULL;
2246
2247 if (flag == 'w') // just windows
2248 {
2249 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2250 wp = curwin;
2251 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2252 && wp->w_buffer->b_scanned)
2253 ;
2254 buf = wp->w_buffer;
2255 }
2256 else
2257 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2258 // (unlisted buffers)
2259 // When completing whole lines skip unloaded buffers.
2260 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2261 && ((flag == 'U'
2262 ? buf->b_p_bl
2263 : (!buf->b_p_bl
2264 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2265 || buf->b_scanned))
2266 ;
2267 return buf;
2268}
2269
2270#ifdef FEAT_COMPL_FUNC
2271/*
2272 * Execute user defined complete function 'completefunc' or 'omnifunc', and
2273 * get matches in "matches".
2274 */
2275 static void
2276expand_by_function(
2277 int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
2278 char_u *base)
2279{
2280 list_T *matchlist = NULL;
2281 dict_T *matchdict = NULL;
2282 typval_T args[3];
2283 char_u *funcname;
2284 pos_T pos;
2285 win_T *curwin_save;
2286 buf_T *curbuf_save;
2287 typval_T rettv;
2288 int save_State = State;
2289
2290 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2291 if (*funcname == NUL)
2292 return;
2293
2294 // Call 'completefunc' to obtain the list of matches.
2295 args[0].v_type = VAR_NUMBER;
2296 args[0].vval.v_number = 0;
2297 args[1].v_type = VAR_STRING;
2298 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2299 args[2].v_type = VAR_UNKNOWN;
2300
2301 pos = curwin->w_cursor;
2302 curwin_save = curwin;
2303 curbuf_save = curbuf;
2304
2305 // Call a function, which returns a list or dict.
2306 if (call_vim_function(funcname, 2, args, &rettv) == OK)
2307 {
2308 switch (rettv.v_type)
2309 {
2310 case VAR_LIST:
2311 matchlist = rettv.vval.v_list;
2312 break;
2313 case VAR_DICT:
2314 matchdict = rettv.vval.v_dict;
2315 break;
2316 case VAR_SPECIAL:
2317 if (rettv.vval.v_number == VVAL_NONE)
2318 compl_opt_suppress_empty = TRUE;
2319 // FALLTHROUGH
2320 default:
2321 // TODO: Give error message?
2322 clear_tv(&rettv);
2323 break;
2324 }
2325 }
2326
2327 if (curwin_save != curwin || curbuf_save != curbuf)
2328 {
2329 emsg(_(e_complwin));
2330 goto theend;
2331 }
2332 curwin->w_cursor = pos; // restore the cursor position
2333 validate_cursor();
2334 if (!EQUAL_POS(curwin->w_cursor, pos))
2335 {
2336 emsg(_(e_compldel));
2337 goto theend;
2338 }
2339
2340 if (matchlist != NULL)
2341 ins_compl_add_list(matchlist);
2342 else if (matchdict != NULL)
2343 ins_compl_add_dict(matchdict);
2344
2345theend:
2346 // Restore State, it might have been changed.
2347 State = save_State;
2348
2349 if (matchdict != NULL)
2350 dict_unref(matchdict);
2351 if (matchlist != NULL)
2352 list_unref(matchlist);
2353}
2354#endif // FEAT_COMPL_FUNC
2355
2356#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2357/*
2358 * Add completions from a list.
2359 */
2360 static void
2361ins_compl_add_list(list_T *list)
2362{
2363 listitem_T *li;
2364 int dir = compl_direction;
2365
2366 // Go through the List with matches and add each of them.
2367 for (li = list->lv_first; li != NULL; li = li->li_next)
2368 {
2369 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
2370 // if dir was BACKWARD then honor it just once
2371 dir = FORWARD;
2372 else if (did_emsg)
2373 break;
2374 }
2375}
2376
2377/*
2378 * Add completions from a dict.
2379 */
2380 static void
2381ins_compl_add_dict(dict_T *dict)
2382{
2383 dictitem_T *di_refresh;
2384 dictitem_T *di_words;
2385
2386 // Check for optional "refresh" item.
2387 compl_opt_refresh_always = FALSE;
2388 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2389 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2390 {
2391 char_u *v = di_refresh->di_tv.vval.v_string;
2392
2393 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2394 compl_opt_refresh_always = TRUE;
2395 }
2396
2397 // Add completions from a "words" list.
2398 di_words = dict_find(dict, (char_u *)"words", 5);
2399 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2400 ins_compl_add_list(di_words->di_tv.vval.v_list);
2401}
2402
2403/*
2404 * Add a match to the list of matches from a typeval_T.
2405 * If the given string is already in the list of completions, then return
2406 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2407 * maybe because alloc() returns NULL, then FAIL is returned.
2408 */
2409 int
2410ins_compl_add_tv(typval_T *tv, int dir)
2411{
2412 char_u *word;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002413 int dup = FALSE;
2414 int empty = FALSE;
2415 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002416 char_u *(cptext[CPT_COUNT]);
2417
2418 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2419 {
2420 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2421 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2422 (char_u *)"abbr", FALSE);
2423 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2424 (char_u *)"menu", FALSE);
2425 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2426 (char_u *)"kind", FALSE);
2427 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2428 (char_u *)"info", FALSE);
2429 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
2430 (char_u *)"user_data", FALSE);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002431 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2432 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2433 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002434 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002435 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002436 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002437 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2438 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2439 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2440 flags |= CP_EQUAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002441 }
2442 else
2443 {
2444 word = tv_get_string_chk(tv);
2445 vim_memset(cptext, 0, sizeof(cptext));
2446 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002447 if (word == NULL || (!empty && *word == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002448 return FAIL;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002449 return ins_compl_add(word, -1, NULL, cptext, dir, flags, dup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002450}
2451#endif
2452
2453/*
2454 * Get the next expansion(s), using "compl_pattern".
2455 * The search starts at position "ini" in curbuf and in the direction
2456 * compl_direction.
2457 * When "compl_started" is FALSE start at that position, otherwise continue
2458 * where we stopped searching before.
2459 * This may return before finding all the matches.
2460 * Return the total number of matches or -1 if still unknown -- Acevedo
2461 */
2462 static int
2463ins_compl_get_exp(pos_T *ini)
2464{
2465 static pos_T first_match_pos;
2466 static pos_T last_match_pos;
2467 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2468 static int found_all = FALSE; // Found all matches of a
2469 // certain type.
2470 static buf_T *ins_buf = NULL; // buffer being scanned
2471
2472 pos_T *pos;
2473 char_u **matches;
2474 int save_p_scs;
2475 int save_p_ws;
2476 int save_p_ic;
2477 int i;
2478 int num_matches;
2479 int len;
2480 int found_new_match;
2481 int type = ctrl_x_mode;
2482 char_u *ptr;
2483 char_u *dict = NULL;
2484 int dict_f = 0;
2485 int set_match_pos;
2486
2487 if (!compl_started)
2488 {
2489 FOR_ALL_BUFFERS(ins_buf)
2490 ins_buf->b_scanned = 0;
2491 found_all = FALSE;
2492 ins_buf = curbuf;
2493 e_cpt = (compl_cont_status & CONT_LOCAL)
2494 ? (char_u *)"." : curbuf->b_p_cpt;
2495 last_match_pos = first_match_pos = *ini;
2496 }
2497 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2498 ins_buf = curbuf; // In case the buffer was wiped out.
2499
2500 compl_old_match = compl_curr_match; // remember the last current match
2501 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2502
2503 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2504 for (;;)
2505 {
2506 found_new_match = FAIL;
2507 set_match_pos = FALSE;
2508
2509 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2510 // or if found_all says this entry is done. For ^X^L only use the
2511 // entries from 'complete' that look in loaded buffers.
2512 if ((ctrl_x_mode == CTRL_X_NORMAL
2513 || ctrl_x_mode_line_or_eval())
2514 && (!compl_started || found_all))
2515 {
2516 found_all = FALSE;
2517 while (*e_cpt == ',' || *e_cpt == ' ')
2518 e_cpt++;
2519 if (*e_cpt == '.' && !curbuf->b_scanned)
2520 {
2521 ins_buf = curbuf;
2522 first_match_pos = *ini;
2523 // Move the cursor back one character so that ^N can match the
2524 // word immediately after the cursor.
2525 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2526 {
2527 // Move the cursor to after the last character in the
2528 // buffer, so that word at start of buffer is found
2529 // correctly.
2530 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2531 first_match_pos.col =
2532 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2533 }
2534 last_match_pos = first_match_pos;
2535 type = 0;
2536
2537 // Remember the first match so that the loop stops when we
2538 // wrap and come back there a second time.
2539 set_match_pos = TRUE;
2540 }
2541 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2542 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2543 {
2544 // Scan a buffer, but not the current one.
2545 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2546 {
2547 compl_started = TRUE;
2548 first_match_pos.col = last_match_pos.col = 0;
2549 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2550 last_match_pos.lnum = 0;
2551 type = 0;
2552 }
2553 else // unloaded buffer, scan like dictionary
2554 {
2555 found_all = TRUE;
2556 if (ins_buf->b_fname == NULL)
2557 continue;
2558 type = CTRL_X_DICTIONARY;
2559 dict = ins_buf->b_fname;
2560 dict_f = DICT_EXACT;
2561 }
2562 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2563 ins_buf->b_fname == NULL
2564 ? buf_spname(ins_buf)
2565 : ins_buf->b_sfname == NULL
2566 ? ins_buf->b_fname
2567 : ins_buf->b_sfname);
2568 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2569 }
2570 else if (*e_cpt == NUL)
2571 break;
2572 else
2573 {
2574 if (ctrl_x_mode_line_or_eval())
2575 type = -1;
2576 else if (*e_cpt == 'k' || *e_cpt == 's')
2577 {
2578 if (*e_cpt == 'k')
2579 type = CTRL_X_DICTIONARY;
2580 else
2581 type = CTRL_X_THESAURUS;
2582 if (*++e_cpt != ',' && *e_cpt != NUL)
2583 {
2584 dict = e_cpt;
2585 dict_f = DICT_FIRST;
2586 }
2587 }
2588#ifdef FEAT_FIND_ID
2589 else if (*e_cpt == 'i')
2590 type = CTRL_X_PATH_PATTERNS;
2591 else if (*e_cpt == 'd')
2592 type = CTRL_X_PATH_DEFINES;
2593#endif
2594 else if (*e_cpt == ']' || *e_cpt == 't')
2595 {
2596 type = CTRL_X_TAGS;
2597 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2598 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2599 }
2600 else
2601 type = -1;
2602
2603 // in any case e_cpt is advanced to the next entry
2604 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2605
2606 found_all = TRUE;
2607 if (type == -1)
2608 continue;
2609 }
2610 }
2611
2612 // If complete() was called then compl_pattern has been reset. The
2613 // following won't work then, bail out.
2614 if (compl_pattern == NULL)
2615 break;
2616
2617 switch (type)
2618 {
2619 case -1:
2620 break;
2621#ifdef FEAT_FIND_ID
2622 case CTRL_X_PATH_PATTERNS:
2623 case CTRL_X_PATH_DEFINES:
2624 find_pattern_in_path(compl_pattern, compl_direction,
2625 (int)STRLEN(compl_pattern), FALSE, FALSE,
2626 (type == CTRL_X_PATH_DEFINES
2627 && !(compl_cont_status & CONT_SOL))
2628 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2629 (linenr_T)1, (linenr_T)MAXLNUM);
2630 break;
2631#endif
2632
2633 case CTRL_X_DICTIONARY:
2634 case CTRL_X_THESAURUS:
2635 ins_compl_dictionaries(
2636 dict != NULL ? dict
2637 : (type == CTRL_X_THESAURUS
2638 ? (*curbuf->b_p_tsr == NUL
2639 ? p_tsr
2640 : curbuf->b_p_tsr)
2641 : (*curbuf->b_p_dict == NUL
2642 ? p_dict
2643 : curbuf->b_p_dict)),
2644 compl_pattern,
2645 dict != NULL ? dict_f
2646 : 0, type == CTRL_X_THESAURUS);
2647 dict = NULL;
2648 break;
2649
2650 case CTRL_X_TAGS:
2651 // set p_ic according to p_ic, p_scs and pat for find_tags().
2652 save_p_ic = p_ic;
2653 p_ic = ignorecase(compl_pattern);
2654
2655 // Find up to TAG_MANY matches. Avoids that an enormous number
2656 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002657 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002658 if (find_tags(compl_pattern, &num_matches, &matches,
2659 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2660 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2661 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002662 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002663 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002664 p_ic = save_p_ic;
2665 break;
2666
2667 case CTRL_X_FILES:
2668 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2669 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2670 {
2671
2672 // May change home directory back to "~".
2673 tilde_replace(compl_pattern, num_matches, matches);
2674 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2675 }
2676 break;
2677
2678 case CTRL_X_CMDLINE:
2679 if (expand_cmdline(&compl_xp, compl_pattern,
2680 (int)STRLEN(compl_pattern),
2681 &num_matches, &matches) == EXPAND_OK)
2682 ins_compl_add_matches(num_matches, matches, FALSE);
2683 break;
2684
2685#ifdef FEAT_COMPL_FUNC
2686 case CTRL_X_FUNCTION:
2687 case CTRL_X_OMNI:
2688 expand_by_function(type, compl_pattern);
2689 break;
2690#endif
2691
2692 case CTRL_X_SPELL:
2693#ifdef FEAT_SPELL
2694 num_matches = expand_spelling(first_match_pos.lnum,
2695 compl_pattern, &matches);
2696 if (num_matches > 0)
2697 ins_compl_add_matches(num_matches, matches, p_ic);
2698#endif
2699 break;
2700
2701 default: // normal ^P/^N and ^X^L
2702 // If 'infercase' is set, don't use 'smartcase' here
2703 save_p_scs = p_scs;
2704 if (ins_buf->b_p_inf)
2705 p_scs = FALSE;
2706
2707 // Buffers other than curbuf are scanned from the beginning or the
2708 // end but never from the middle, thus setting nowrapscan in this
2709 // buffers is a good idea, on the other hand, we always set
2710 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
2711 save_p_ws = p_ws;
2712 if (ins_buf != curbuf)
2713 p_ws = FALSE;
2714 else if (*e_cpt == '.')
2715 p_ws = TRUE;
2716 for (;;)
2717 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002718 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002719
2720 ++msg_silent; // Don't want messages for wrapscan.
2721
2722 // ctrl_x_mode_line_or_eval() || word-wise search that
2723 // has added a word that was at the beginning of the line
2724 if (ctrl_x_mode_line_or_eval()
2725 || (compl_cont_status & CONT_SOL))
2726 found_new_match = search_for_exact_line(ins_buf, pos,
2727 compl_direction, compl_pattern);
2728 else
2729 found_new_match = searchit(NULL, ins_buf, pos, NULL,
2730 compl_direction,
2731 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
2732 RE_LAST, (linenr_T)0, NULL, NULL);
2733 --msg_silent;
2734 if (!compl_started || set_match_pos)
2735 {
2736 // set "compl_started" even on fail
2737 compl_started = TRUE;
2738 first_match_pos = *pos;
2739 last_match_pos = *pos;
2740 set_match_pos = FALSE;
2741 }
2742 else if (first_match_pos.lnum == last_match_pos.lnum
2743 && first_match_pos.col == last_match_pos.col)
2744 found_new_match = FAIL;
2745 if (found_new_match == FAIL)
2746 {
2747 if (ins_buf == curbuf)
2748 found_all = TRUE;
2749 break;
2750 }
2751
2752 // when ADDING, the text before the cursor matches, skip it
2753 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
2754 && ini->lnum == pos->lnum
2755 && ini->col == pos->col)
2756 continue;
2757 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
2758 if (ctrl_x_mode_line_or_eval())
2759 {
2760 if (compl_cont_status & CONT_ADDING)
2761 {
2762 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
2763 continue;
2764 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2765 if (!p_paste)
2766 ptr = skipwhite(ptr);
2767 }
2768 len = (int)STRLEN(ptr);
2769 }
2770 else
2771 {
2772 char_u *tmp_ptr = ptr;
2773
2774 if (compl_cont_status & CONT_ADDING)
2775 {
2776 tmp_ptr += compl_length;
2777 // Skip if already inside a word.
2778 if (vim_iswordp(tmp_ptr))
2779 continue;
2780 // Find start of next word.
2781 tmp_ptr = find_word_start(tmp_ptr);
2782 }
2783 // Find end of this word.
2784 tmp_ptr = find_word_end(tmp_ptr);
2785 len = (int)(tmp_ptr - ptr);
2786
2787 if ((compl_cont_status & CONT_ADDING)
2788 && len == compl_length)
2789 {
2790 if (pos->lnum < ins_buf->b_ml.ml_line_count)
2791 {
2792 // Try next line, if any. the new word will be
2793 // "join" as if the normal command "J" was used.
2794 // IOSIZE is always greater than
2795 // compl_length, so the next STRNCPY always
2796 // works -- Acevedo
2797 STRNCPY(IObuff, ptr, len);
2798 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2799 tmp_ptr = ptr = skipwhite(ptr);
2800 // Find start of next word.
2801 tmp_ptr = find_word_start(tmp_ptr);
2802 // Find end of next word.
2803 tmp_ptr = find_word_end(tmp_ptr);
2804 if (tmp_ptr > ptr)
2805 {
2806 if (*ptr != ')' && IObuff[len - 1] != TAB)
2807 {
2808 if (IObuff[len - 1] != ' ')
2809 IObuff[len++] = ' ';
2810 // IObuf =~ "\k.* ", thus len >= 2
2811 if (p_js
2812 && (IObuff[len - 2] == '.'
2813 || (vim_strchr(p_cpo, CPO_JOINSP)
2814 == NULL
2815 && (IObuff[len - 2] == '?'
2816 || IObuff[len - 2] == '!'))))
2817 IObuff[len++] = ' ';
2818 }
2819 // copy as much as possible of the new word
2820 if (tmp_ptr - ptr >= IOSIZE - len)
2821 tmp_ptr = ptr + IOSIZE - len - 1;
2822 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
2823 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002824 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002825 }
2826 IObuff[len] = NUL;
2827 ptr = IObuff;
2828 }
2829 if (len == compl_length)
2830 continue;
2831 }
2832 }
2833 if (ins_compl_add_infercase(ptr, len, p_ic,
2834 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002835 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002836 {
2837 found_new_match = OK;
2838 break;
2839 }
2840 }
2841 p_scs = save_p_scs;
2842 p_ws = save_p_ws;
2843 }
2844
2845 // check if compl_curr_match has changed, (e.g. other type of
2846 // expansion added something)
2847 if (type != 0 && compl_curr_match != compl_old_match)
2848 found_new_match = OK;
2849
2850 // break the loop for specialized modes (use 'complete' just for the
2851 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
2852 // match
2853 if ((ctrl_x_mode != CTRL_X_NORMAL
2854 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
2855 {
2856 if (got_int)
2857 break;
2858 // Fill the popup menu as soon as possible.
2859 if (type != -1)
2860 ins_compl_check_keys(0, FALSE);
2861
2862 if ((ctrl_x_mode != CTRL_X_NORMAL
2863 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
2864 break;
2865 compl_started = TRUE;
2866 }
2867 else
2868 {
2869 // Mark a buffer scanned when it has been scanned completely
2870 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
2871 ins_buf->b_scanned = TRUE;
2872
2873 compl_started = FALSE;
2874 }
2875 }
2876 compl_started = TRUE;
2877
2878 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
2879 && *e_cpt == NUL) // Got to end of 'complete'
2880 found_new_match = FAIL;
2881
2882 i = -1; // total of matches, unknown
2883 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
2884 && !ctrl_x_mode_line_or_eval()))
2885 i = ins_compl_make_cyclic();
2886
2887 if (compl_old_match != NULL)
2888 {
2889 // If several matches were added (FORWARD) or the search failed and has
2890 // just been made cyclic then we have to move compl_curr_match to the
2891 // next or previous entry (if any) -- Acevedo
2892 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
2893 : compl_old_match->cp_prev;
2894 if (compl_curr_match == NULL)
2895 compl_curr_match = compl_old_match;
2896 }
2897 return i;
2898}
2899
2900/*
2901 * Delete the old text being completed.
2902 */
2903 void
2904ins_compl_delete(void)
2905{
2906 int col;
2907
2908 // In insert mode: Delete the typed part.
2909 // In replace mode: Put the old characters back, if any.
2910 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
2911 if ((int)curwin->w_cursor.col > col)
2912 {
2913 if (stop_arrow() == FAIL)
2914 return;
2915 backspace_until_column(col);
2916 }
2917
2918 // TODO: is this sufficient for redrawing? Redrawing everything causes
2919 // flicker, thus we can't do that.
2920 changed_cline_bef_curs();
2921 // clear v:completed_item
2922 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
2923}
2924
2925/*
2926 * Insert the new text being completed.
2927 * "in_compl_func" is TRUE when called from complete_check().
2928 */
2929 void
2930ins_compl_insert(int in_compl_func)
2931{
2932 dict_T *dict;
2933
2934 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002935 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002936 compl_used_match = FALSE;
2937 else
2938 compl_used_match = TRUE;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002939 dict = ins_compl_dict_alloc(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002940 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
2941 if (!in_compl_func)
2942 compl_curr_match = compl_shown_match;
2943}
2944
2945/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002946 * Allocate Dict for the completed item.
2947 * { word, abbr, menu, kind, info }
2948 */
2949 static dict_T *
2950ins_compl_dict_alloc(compl_T *match)
2951{
2952 dict_T *dict = dict_alloc_lock(VAR_FIXED);
2953
2954 if (dict != NULL)
2955 {
2956 dict_add_string(dict, "word", match->cp_str);
2957 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
2958 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
2959 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
2960 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
2961 dict_add_string(dict, "user_data", match->cp_text[CPT_USER_DATA]);
2962 }
2963 return dict;
2964}
2965
2966/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002967 * Fill in the next completion in the current direction.
2968 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
2969 * get more completions. If it is FALSE, then we just do nothing when there
2970 * are no more completions in a given direction. The latter case is used when
2971 * we are still in the middle of finding completions, to allow browsing
2972 * through the ones found so far.
2973 * Return the total number of matches, or -1 if still unknown -- webb.
2974 *
2975 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
2976 * compl_shown_match here.
2977 *
2978 * Note that this function may be called recursively once only. First with
2979 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
2980 * calls this function with "allow_get_expansion" FALSE.
2981 */
2982 static int
2983ins_compl_next(
2984 int allow_get_expansion,
2985 int count, // repeat completion this many times; should
2986 // be at least 1
2987 int insert_match, // Insert the newly selected match
2988 int in_compl_func) // called from complete_check()
2989{
2990 int num_matches = -1;
2991 int todo = count;
2992 compl_T *found_compl = NULL;
2993 int found_end = FALSE;
2994 int advance;
2995 int started = compl_started;
2996
2997 // When user complete function return -1 for findstart which is next
2998 // time of 'always', compl_shown_match become NULL.
2999 if (compl_shown_match == NULL)
3000 return -1;
3001
3002 if (compl_leader != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003003 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003004 {
3005 // Set "compl_shown_match" to the actually shown match, it may differ
3006 // when "compl_leader" is used to omit some of the matches.
3007 while (!ins_compl_equal(compl_shown_match,
3008 compl_leader, (int)STRLEN(compl_leader))
3009 && compl_shown_match->cp_next != NULL
3010 && compl_shown_match->cp_next != compl_first_match)
3011 compl_shown_match = compl_shown_match->cp_next;
3012
3013 // If we didn't find it searching forward, and compl_shows_dir is
3014 // backward, find the last match.
3015 if (compl_shows_dir == BACKWARD
3016 && !ins_compl_equal(compl_shown_match,
3017 compl_leader, (int)STRLEN(compl_leader))
3018 && (compl_shown_match->cp_next == NULL
3019 || compl_shown_match->cp_next == compl_first_match))
3020 {
3021 while (!ins_compl_equal(compl_shown_match,
3022 compl_leader, (int)STRLEN(compl_leader))
3023 && compl_shown_match->cp_prev != NULL
3024 && compl_shown_match->cp_prev != compl_first_match)
3025 compl_shown_match = compl_shown_match->cp_prev;
3026 }
3027 }
3028
3029 if (allow_get_expansion && insert_match
3030 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3031 // Delete old text to be replaced
3032 ins_compl_delete();
3033
3034 // When finding the longest common text we stick at the original text,
3035 // don't let CTRL-N or CTRL-P move to the first match.
3036 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3037
3038 // When restarting the search don't insert the first match either.
3039 if (compl_restarting)
3040 {
3041 advance = FALSE;
3042 compl_restarting = FALSE;
3043 }
3044
3045 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3046 // around.
3047 while (--todo >= 0)
3048 {
3049 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3050 {
3051 compl_shown_match = compl_shown_match->cp_next;
3052 found_end = (compl_first_match != NULL
3053 && (compl_shown_match->cp_next == compl_first_match
3054 || compl_shown_match == compl_first_match));
3055 }
3056 else if (compl_shows_dir == BACKWARD
3057 && compl_shown_match->cp_prev != NULL)
3058 {
3059 found_end = (compl_shown_match == compl_first_match);
3060 compl_shown_match = compl_shown_match->cp_prev;
3061 found_end |= (compl_shown_match == compl_first_match);
3062 }
3063 else
3064 {
3065 if (!allow_get_expansion)
3066 {
3067 if (advance)
3068 {
3069 if (compl_shows_dir == BACKWARD)
3070 compl_pending -= todo + 1;
3071 else
3072 compl_pending += todo + 1;
3073 }
3074 return -1;
3075 }
3076
3077 if (!compl_no_select && advance)
3078 {
3079 if (compl_shows_dir == BACKWARD)
3080 --compl_pending;
3081 else
3082 ++compl_pending;
3083 }
3084
3085 // Find matches.
3086 num_matches = ins_compl_get_exp(&compl_startpos);
3087
3088 // handle any pending completions
3089 while (compl_pending != 0 && compl_direction == compl_shows_dir
3090 && advance)
3091 {
3092 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3093 {
3094 compl_shown_match = compl_shown_match->cp_next;
3095 --compl_pending;
3096 }
3097 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3098 {
3099 compl_shown_match = compl_shown_match->cp_prev;
3100 ++compl_pending;
3101 }
3102 else
3103 break;
3104 }
3105 found_end = FALSE;
3106 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003107 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108 && compl_leader != NULL
3109 && !ins_compl_equal(compl_shown_match,
3110 compl_leader, (int)STRLEN(compl_leader)))
3111 ++todo;
3112 else
3113 // Remember a matching item.
3114 found_compl = compl_shown_match;
3115
3116 // Stop at the end of the list when we found a usable match.
3117 if (found_end)
3118 {
3119 if (found_compl != NULL)
3120 {
3121 compl_shown_match = found_compl;
3122 break;
3123 }
3124 todo = 1; // use first usable match after wrapping around
3125 }
3126 }
3127
3128 // Insert the text of the new completion, or the compl_leader.
3129 if (compl_no_insert && !started)
3130 {
3131 ins_bytes(compl_orig_text + ins_compl_len());
3132 compl_used_match = FALSE;
3133 }
3134 else if (insert_match)
3135 {
3136 if (!compl_get_longest || compl_used_match)
3137 ins_compl_insert(in_compl_func);
3138 else
3139 ins_bytes(compl_leader + ins_compl_len());
3140 }
3141 else
3142 compl_used_match = FALSE;
3143
3144 if (!allow_get_expansion)
3145 {
3146 // may undisplay the popup menu first
3147 ins_compl_upd_pum();
3148
3149 if (pum_enough_matches())
3150 // Will display the popup menu, don't redraw yet to avoid flicker.
3151 pum_call_update_screen();
3152 else
3153 // Not showing the popup menu yet, redraw to show the user what was
3154 // inserted.
3155 update_screen(0);
3156
3157 // display the updated popup menu
3158 ins_compl_show_pum();
3159#ifdef FEAT_GUI
3160 if (gui.in_use)
3161 {
3162 // Show the cursor after the match, not after the redrawn text.
3163 setcursor();
3164 out_flush_cursor(FALSE, FALSE);
3165 }
3166#endif
3167
3168 // Delete old text to be replaced, since we're still searching and
3169 // don't want to match ourselves!
3170 ins_compl_delete();
3171 }
3172
3173 // Enter will select a match when the match wasn't inserted and the popup
3174 // menu is visible.
3175 if (compl_no_insert && !started)
3176 compl_enter_selects = TRUE;
3177 else
3178 compl_enter_selects = !insert_match && compl_match_array != NULL;
3179
3180 // Show the file name for the match (if any)
3181 // Truncate the file name to avoid a wait for return.
3182 if (compl_shown_match->cp_fname != NULL)
3183 {
3184 char *lead = _("match in file");
3185 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3186 char_u *s;
3187 char_u *e;
3188
3189 if (space > 0)
3190 {
3191 // We need the tail that fits. With double-byte encoding going
3192 // back from the end is very slow, thus go from the start and keep
3193 // the text that fits in "space" between "s" and "e".
3194 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3195 {
3196 space -= ptr2cells(e);
3197 while (space < 0)
3198 {
3199 space += ptr2cells(s);
3200 MB_PTR_ADV(s);
3201 }
3202 }
3203 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3204 s > compl_shown_match->cp_fname ? "<" : "", s);
3205 msg((char *)IObuff);
3206 redraw_cmdline = FALSE; // don't overwrite!
3207 }
3208 }
3209
3210 return num_matches;
3211}
3212
3213/*
3214 * Call this while finding completions, to check whether the user has hit a key
3215 * that should change the currently displayed completion, or exit completion
3216 * mode. Also, when compl_pending is not zero, show a completion as soon as
3217 * possible. -- webb
3218 * "frequency" specifies out of how many calls we actually check.
3219 * "in_compl_func" is TRUE when called from complete_check(), don't set
3220 * compl_curr_match.
3221 */
3222 void
3223ins_compl_check_keys(int frequency, int in_compl_func)
3224{
3225 static int count = 0;
3226 int c;
3227
3228 // Don't check when reading keys from a script, :normal or feedkeys().
3229 // That would break the test scripts. But do check for keys when called
3230 // from complete_check().
3231 if (!in_compl_func && (using_script() || ex_normal_busy))
3232 return;
3233
3234 // Only do this at regular intervals
3235 if (++count < frequency)
3236 return;
3237 count = 0;
3238
3239 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3240 // can't do its work correctly.
3241 c = vpeekc_any();
3242 if (c != NUL)
3243 {
3244 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3245 {
3246 c = safe_vgetc(); // Eat the character
3247 compl_shows_dir = ins_compl_key2dir(c);
3248 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3249 c != K_UP && c != K_DOWN, in_compl_func);
3250 }
3251 else
3252 {
3253 // Need to get the character to have KeyTyped set. We'll put it
3254 // back with vungetc() below. But skip K_IGNORE.
3255 c = safe_vgetc();
3256 if (c != K_IGNORE)
3257 {
3258 // Don't interrupt completion when the character wasn't typed,
3259 // e.g., when doing @q to replay keys.
3260 if (c != Ctrl_R && KeyTyped)
3261 compl_interrupted = TRUE;
3262
3263 vungetc(c);
3264 }
3265 }
3266 }
3267 if (compl_pending != 0 && !got_int && !compl_no_insert)
3268 {
3269 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3270
3271 compl_pending = 0;
3272 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3273 }
3274}
3275
3276/*
3277 * Decide the direction of Insert mode complete from the key typed.
3278 * Returns BACKWARD or FORWARD.
3279 */
3280 static int
3281ins_compl_key2dir(int c)
3282{
3283 if (c == Ctrl_P || c == Ctrl_L
3284 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3285 return BACKWARD;
3286 return FORWARD;
3287}
3288
3289/*
3290 * Return TRUE for keys that are used for completion only when the popup menu
3291 * is visible.
3292 */
3293 static int
3294ins_compl_pum_key(int c)
3295{
3296 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3297 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3298 || c == K_UP || c == K_DOWN);
3299}
3300
3301/*
3302 * Decide the number of completions to move forward.
3303 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3304 */
3305 static int
3306ins_compl_key2count(int c)
3307{
3308 int h;
3309
3310 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3311 {
3312 h = pum_get_height();
3313 if (h > 3)
3314 h -= 2; // keep some context
3315 return h;
3316 }
3317 return 1;
3318}
3319
3320/*
3321 * Return TRUE if completion with "c" should insert the match, FALSE if only
3322 * to change the currently selected completion.
3323 */
3324 static int
3325ins_compl_use_match(int c)
3326{
3327 switch (c)
3328 {
3329 case K_UP:
3330 case K_DOWN:
3331 case K_PAGEDOWN:
3332 case K_KPAGEDOWN:
3333 case K_S_DOWN:
3334 case K_PAGEUP:
3335 case K_KPAGEUP:
3336 case K_S_UP:
3337 return FALSE;
3338 }
3339 return TRUE;
3340}
3341
3342/*
3343 * Do Insert mode completion.
3344 * Called when character "c" was typed, which has a meaning for completion.
3345 * Returns OK if completion was done, FAIL if something failed (out of mem).
3346 */
3347 int
3348ins_complete(int c, int enable_pum)
3349{
3350 char_u *line;
3351 int startcol = 0; // column where searched text starts
3352 colnr_T curs_col; // cursor column
3353 int n;
3354 int save_w_wrow;
3355 int save_w_leftcol;
3356 int insert_match;
3357 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003358 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003359
3360 compl_direction = ins_compl_key2dir(c);
3361 insert_match = ins_compl_use_match(c);
3362
3363 if (!compl_started)
3364 {
3365 // First time we hit ^N or ^P (in a row, I mean)
3366
3367 did_ai = FALSE;
3368#ifdef FEAT_SMARTINDENT
3369 did_si = FALSE;
3370 can_si = FALSE;
3371 can_si_back = FALSE;
3372#endif
3373 if (stop_arrow() == FAIL)
3374 return FAIL;
3375
3376 line = ml_get(curwin->w_cursor.lnum);
3377 curs_col = curwin->w_cursor.col;
3378 compl_pending = 0;
3379
3380 // If this same ctrl_x_mode has been interrupted use the text from
3381 // "compl_startpos" to the cursor as a pattern to add a new word
3382 // instead of expand the one before the cursor, in word-wise if
3383 // "compl_startpos" is not in the same line as the cursor then fix it
3384 // (the line has been split because it was longer than 'tw'). if SOL
3385 // is set then skip the previous pattern, a word at the beginning of
3386 // the line has been inserted, we'll look for that -- Acevedo.
3387 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3388 && compl_cont_mode == ctrl_x_mode)
3389 {
3390 // it is a continued search
3391 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3392 if (ctrl_x_mode == CTRL_X_NORMAL
3393 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3394 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3395 {
3396 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3397 {
3398 // line (probably) wrapped, set compl_startpos to the
3399 // first non_blank in the line, if it is not a wordchar
3400 // include it to get a better pattern, but then we don't
3401 // want the "\\<" prefix, check it bellow
3402 compl_col = (colnr_T)getwhitecols(line);
3403 compl_startpos.col = compl_col;
3404 compl_startpos.lnum = curwin->w_cursor.lnum;
3405 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3406 }
3407 else
3408 {
3409 // S_IPOS was set when we inserted a word that was at the
3410 // beginning of the line, which means that we'll go to SOL
3411 // mode but first we need to redefine compl_startpos
3412 if (compl_cont_status & CONT_S_IPOS)
3413 {
3414 compl_cont_status |= CONT_SOL;
3415 compl_startpos.col = (colnr_T)(skipwhite(
3416 line + compl_length
3417 + compl_startpos.col) - line);
3418 }
3419 compl_col = compl_startpos.col;
3420 }
3421 compl_length = curwin->w_cursor.col - (int)compl_col;
3422 // IObuff is used to add a "word from the next line" would we
3423 // have enough space? just being paranoid
3424#define MIN_SPACE 75
3425 if (compl_length > (IOSIZE - MIN_SPACE))
3426 {
3427 compl_cont_status &= ~CONT_SOL;
3428 compl_length = (IOSIZE - MIN_SPACE);
3429 compl_col = curwin->w_cursor.col - compl_length;
3430 }
3431 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3432 if (compl_length < 1)
3433 compl_cont_status &= CONT_LOCAL;
3434 }
3435 else if (ctrl_x_mode_line_or_eval())
3436 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3437 else
3438 compl_cont_status = 0;
3439 }
3440 else
3441 compl_cont_status &= CONT_LOCAL;
3442
3443 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3444 {
3445 compl_cont_mode = ctrl_x_mode;
3446 if (ctrl_x_mode != CTRL_X_NORMAL)
3447 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3448 compl_cont_status = 0;
3449 compl_cont_status |= CONT_N_ADDS;
3450 compl_startpos = curwin->w_cursor;
3451 startcol = (int)curs_col;
3452 compl_col = 0;
3453 }
3454
3455 // Work out completion pattern and original text -- webb
3456 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3457 {
3458 if ((compl_cont_status & CONT_SOL)
3459 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3460 {
3461 if (!(compl_cont_status & CONT_ADDING))
3462 {
3463 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3464 ;
3465 compl_col += ++startcol;
3466 compl_length = curs_col - startcol;
3467 }
3468 if (p_ic)
3469 compl_pattern = str_foldcase(line + compl_col,
3470 compl_length, NULL, 0);
3471 else
3472 compl_pattern = vim_strnsave(line + compl_col,
3473 compl_length);
3474 if (compl_pattern == NULL)
3475 return FAIL;
3476 }
3477 else if (compl_cont_status & CONT_ADDING)
3478 {
3479 char_u *prefix = (char_u *)"\\<";
3480
3481 // we need up to 2 extra chars for the prefix
3482 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3483 compl_length) + 2);
3484 if (compl_pattern == NULL)
3485 return FAIL;
3486 if (!vim_iswordp(line + compl_col)
3487 || (compl_col > 0
3488 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3489 prefix = (char_u *)"";
3490 STRCPY((char *)compl_pattern, prefix);
3491 (void)quote_meta(compl_pattern + STRLEN(prefix),
3492 line + compl_col, compl_length);
3493 }
3494 else if (--startcol < 0
3495 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3496 {
3497 // Match any word of at least two chars
3498 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3499 if (compl_pattern == NULL)
3500 return FAIL;
3501 compl_col += curs_col;
3502 compl_length = 0;
3503 }
3504 else
3505 {
3506 // Search the point of change class of multibyte character
3507 // or not a word single byte character backward.
3508 if (has_mbyte)
3509 {
3510 int base_class;
3511 int head_off;
3512
3513 startcol -= (*mb_head_off)(line, line + startcol);
3514 base_class = mb_get_class(line + startcol);
3515 while (--startcol >= 0)
3516 {
3517 head_off = (*mb_head_off)(line, line + startcol);
3518 if (base_class != mb_get_class(line + startcol
3519 - head_off))
3520 break;
3521 startcol -= head_off;
3522 }
3523 }
3524 else
3525 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3526 ;
3527 compl_col += ++startcol;
3528 compl_length = (int)curs_col - startcol;
3529 if (compl_length == 1)
3530 {
3531 // Only match word with at least two chars -- webb
3532 // there's no need to call quote_meta,
3533 // alloc(7) is enough -- Acevedo
3534 compl_pattern = alloc(7);
3535 if (compl_pattern == NULL)
3536 return FAIL;
3537 STRCPY((char *)compl_pattern, "\\<");
3538 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3539 STRCAT((char *)compl_pattern, "\\k");
3540 }
3541 else
3542 {
3543 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3544 compl_length) + 2);
3545 if (compl_pattern == NULL)
3546 return FAIL;
3547 STRCPY((char *)compl_pattern, "\\<");
3548 (void)quote_meta(compl_pattern + 2, line + compl_col,
3549 compl_length);
3550 }
3551 }
3552 }
3553 else if (ctrl_x_mode_line_or_eval())
3554 {
3555 compl_col = (colnr_T)getwhitecols(line);
3556 compl_length = (int)curs_col - (int)compl_col;
3557 if (compl_length < 0) // cursor in indent: empty pattern
3558 compl_length = 0;
3559 if (p_ic)
3560 compl_pattern = str_foldcase(line + compl_col, compl_length,
3561 NULL, 0);
3562 else
3563 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3564 if (compl_pattern == NULL)
3565 return FAIL;
3566 }
3567 else if (ctrl_x_mode == CTRL_X_FILES)
3568 {
3569 // Go back to just before the first filename character.
3570 if (startcol > 0)
3571 {
3572 char_u *p = line + startcol;
3573
3574 MB_PTR_BACK(line, p);
3575 while (p > line && vim_isfilec(PTR2CHAR(p)))
3576 MB_PTR_BACK(line, p);
3577 if (p == line && vim_isfilec(PTR2CHAR(p)))
3578 startcol = 0;
3579 else
3580 startcol = (int)(p - line) + 1;
3581 }
3582
3583 compl_col += startcol;
3584 compl_length = (int)curs_col - startcol;
3585 compl_pattern = addstar(line + compl_col, compl_length,
3586 EXPAND_FILES);
3587 if (compl_pattern == NULL)
3588 return FAIL;
3589 }
3590 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3591 {
3592 compl_pattern = vim_strnsave(line, curs_col);
3593 if (compl_pattern == NULL)
3594 return FAIL;
3595 set_cmd_context(&compl_xp, compl_pattern,
3596 (int)STRLEN(compl_pattern), curs_col, FALSE);
3597 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3598 || compl_xp.xp_context == EXPAND_NOTHING)
3599 // No completion possible, use an empty pattern to get a
3600 // "pattern not found" message.
3601 compl_col = curs_col;
3602 else
3603 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3604 compl_length = curs_col - compl_col;
3605 }
3606 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3607 {
3608#ifdef FEAT_COMPL_FUNC
3609 // Call user defined function 'completefunc' with "a:findstart"
3610 // set to 1 to obtain the length of text to use for completion.
3611 typval_T args[3];
3612 int col;
3613 char_u *funcname;
3614 pos_T pos;
3615 win_T *curwin_save;
3616 buf_T *curbuf_save;
3617 int save_State = State;
3618
3619 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3620 // string
3621 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3622 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3623 if (*funcname == NUL)
3624 {
3625 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3626 ? "completefunc" : "omnifunc");
3627 // restore did_ai, so that adding comment leader works
3628 did_ai = save_did_ai;
3629 return FAIL;
3630 }
3631
3632 args[0].v_type = VAR_NUMBER;
3633 args[0].vval.v_number = 1;
3634 args[1].v_type = VAR_STRING;
3635 args[1].vval.v_string = (char_u *)"";
3636 args[2].v_type = VAR_UNKNOWN;
3637 pos = curwin->w_cursor;
3638 curwin_save = curwin;
3639 curbuf_save = curbuf;
3640 col = call_func_retnr(funcname, 2, args);
3641
3642 State = save_State;
3643 if (curwin_save != curwin || curbuf_save != curbuf)
3644 {
3645 emsg(_(e_complwin));
3646 return FAIL;
3647 }
3648 curwin->w_cursor = pos; // restore the cursor position
3649 validate_cursor();
3650 if (!EQUAL_POS(curwin->w_cursor, pos))
3651 {
3652 emsg(_(e_compldel));
3653 return FAIL;
3654 }
3655
3656 // Return value -2 means the user complete function wants to
3657 // cancel the complete without an error.
3658 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3659 if (col == -2)
3660 return FAIL;
3661 if (col == -3)
3662 {
3663 ctrl_x_mode = CTRL_X_NORMAL;
3664 edit_submode = NULL;
3665 if (!shortmess(SHM_COMPLETIONMENU))
3666 msg_clr_cmdline();
3667 return FAIL;
3668 }
3669
3670 // Reset extended parameters of completion, when start new
3671 // completion.
3672 compl_opt_refresh_always = FALSE;
3673 compl_opt_suppress_empty = FALSE;
3674
3675 if (col < 0)
3676 col = curs_col;
3677 compl_col = col;
3678 if (compl_col > curs_col)
3679 compl_col = curs_col;
3680
3681 // Setup variables for completion. Need to obtain "line" again,
3682 // it may have become invalid.
3683 line = ml_get(curwin->w_cursor.lnum);
3684 compl_length = curs_col - compl_col;
3685 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3686 if (compl_pattern == NULL)
3687#endif
3688 return FAIL;
3689 }
3690 else if (ctrl_x_mode == CTRL_X_SPELL)
3691 {
3692#ifdef FEAT_SPELL
3693 if (spell_bad_len > 0)
3694 compl_col = curs_col - spell_bad_len;
3695 else
3696 compl_col = spell_word_start(startcol);
3697 if (compl_col >= (colnr_T)startcol)
3698 {
3699 compl_length = 0;
3700 compl_col = curs_col;
3701 }
3702 else
3703 {
3704 spell_expand_check_cap(compl_col);
3705 compl_length = (int)curs_col - compl_col;
3706 }
3707 // Need to obtain "line" again, it may have become invalid.
3708 line = ml_get(curwin->w_cursor.lnum);
3709 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3710 if (compl_pattern == NULL)
3711#endif
3712 return FAIL;
3713 }
3714 else
3715 {
3716 internal_error("ins_complete()");
3717 return FAIL;
3718 }
3719
3720 if (compl_cont_status & CONT_ADDING)
3721 {
3722 edit_submode_pre = (char_u *)_(" Adding");
3723 if (ctrl_x_mode_line_or_eval())
3724 {
3725 // Insert a new line, keep indentation but ignore 'comments'
3726#ifdef FEAT_COMMENTS
3727 char_u *old = curbuf->b_p_com;
3728
3729 curbuf->b_p_com = (char_u *)"";
3730#endif
3731 compl_startpos.lnum = curwin->w_cursor.lnum;
3732 compl_startpos.col = compl_col;
3733 ins_eol('\r');
3734#ifdef FEAT_COMMENTS
3735 curbuf->b_p_com = old;
3736#endif
3737 compl_length = 0;
3738 compl_col = curwin->w_cursor.col;
3739 }
3740 }
3741 else
3742 {
3743 edit_submode_pre = NULL;
3744 compl_startpos.col = compl_col;
3745 }
3746
3747 if (compl_cont_status & CONT_LOCAL)
3748 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
3749 else
3750 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3751
3752 // If any of the original typed text has been changed we need to fix
3753 // the redo buffer.
3754 ins_compl_fixRedoBufForLeader(NULL);
3755
3756 // Always add completion for the original text.
3757 vim_free(compl_orig_text);
3758 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003759 if (p_ic)
3760 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003761 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003762 -1, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003763 {
3764 VIM_CLEAR(compl_pattern);
3765 VIM_CLEAR(compl_orig_text);
3766 return FAIL;
3767 }
3768
3769 // showmode might reset the internal line pointers, so it must
3770 // be called before line = ml_get(), or when this address is no
3771 // longer needed. -- Acevedo.
3772 edit_submode_extra = (char_u *)_("-- Searching...");
3773 edit_submode_highl = HLF_COUNT;
3774 showmode();
3775 edit_submode_extra = NULL;
3776 out_flush();
3777 }
3778 else if (insert_match && stop_arrow() == FAIL)
3779 return FAIL;
3780
3781 compl_shown_match = compl_curr_match;
3782 compl_shows_dir = compl_direction;
3783
3784 // Find next match (and following matches).
3785 save_w_wrow = curwin->w_wrow;
3786 save_w_leftcol = curwin->w_leftcol;
3787 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
3788
3789 // may undisplay the popup menu
3790 ins_compl_upd_pum();
3791
3792 if (n > 1) // all matches have been found
3793 compl_matches = n;
3794 compl_curr_match = compl_shown_match;
3795 compl_direction = compl_shows_dir;
3796
3797 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
3798 // mode.
3799 if (got_int && !global_busy)
3800 {
3801 (void)vgetc();
3802 got_int = FALSE;
3803 }
3804
3805 // we found no match if the list has only the "compl_orig_text"-entry
3806 if (compl_first_match == compl_first_match->cp_next)
3807 {
3808 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3809 && compl_length > 1
3810 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3811 edit_submode_highl = HLF_E;
3812 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3813 // because we couldn't expand anything at first place, but if we used
3814 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3815 // (such as M in M'exico) if not tried already. -- Acevedo
3816 if ( compl_length > 1
3817 || (compl_cont_status & CONT_ADDING)
3818 || (ctrl_x_mode != CTRL_X_NORMAL
3819 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3820 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
3821 compl_cont_status &= ~CONT_N_ADDS;
3822 }
3823
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003824 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003825 compl_cont_status |= CONT_S_IPOS;
3826 else
3827 compl_cont_status &= ~CONT_S_IPOS;
3828
3829 if (edit_submode_extra == NULL)
3830 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003831 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003832 {
3833 edit_submode_extra = (char_u *)_("Back at original");
3834 edit_submode_highl = HLF_W;
3835 }
3836 else if (compl_cont_status & CONT_S_IPOS)
3837 {
3838 edit_submode_extra = (char_u *)_("Word from other line");
3839 edit_submode_highl = HLF_COUNT;
3840 }
3841 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
3842 {
3843 edit_submode_extra = (char_u *)_("The only match");
3844 edit_submode_highl = HLF_COUNT;
3845 }
3846 else
3847 {
3848 // Update completion sequence number when needed.
3849 if (compl_curr_match->cp_number == -1)
3850 {
3851 int number = 0;
3852 compl_T *match;
3853
3854 if (compl_direction == FORWARD)
3855 {
3856 // search backwards for the first valid (!= -1) number.
3857 // This should normally succeed already at the first loop
3858 // cycle, so it's fast!
3859 for (match = compl_curr_match->cp_prev; match != NULL
3860 && match != compl_first_match;
3861 match = match->cp_prev)
3862 if (match->cp_number != -1)
3863 {
3864 number = match->cp_number;
3865 break;
3866 }
3867 if (match != NULL)
3868 // go up and assign all numbers which are not assigned
3869 // yet
3870 for (match = match->cp_next;
3871 match != NULL && match->cp_number == -1;
3872 match = match->cp_next)
3873 match->cp_number = ++number;
3874 }
3875 else // BACKWARD
3876 {
3877 // search forwards (upwards) for the first valid (!= -1)
3878 // number. This should normally succeed already at the
3879 // first loop cycle, so it's fast!
3880 for (match = compl_curr_match->cp_next; match != NULL
3881 && match != compl_first_match;
3882 match = match->cp_next)
3883 if (match->cp_number != -1)
3884 {
3885 number = match->cp_number;
3886 break;
3887 }
3888 if (match != NULL)
3889 // go down and assign all numbers which are not
3890 // assigned yet
3891 for (match = match->cp_prev; match
3892 && match->cp_number == -1;
3893 match = match->cp_prev)
3894 match->cp_number = ++number;
3895 }
3896 }
3897
3898 // The match should always have a sequence number now, this is
3899 // just a safety check.
3900 if (compl_curr_match->cp_number != -1)
3901 {
3902 // Space for 10 text chars. + 2x10-digit no.s = 31.
3903 // Translations may need more than twice that.
3904 static char_u match_ref[81];
3905
3906 if (compl_matches > 0)
3907 vim_snprintf((char *)match_ref, sizeof(match_ref),
3908 _("match %d of %d"),
3909 compl_curr_match->cp_number, compl_matches);
3910 else
3911 vim_snprintf((char *)match_ref, sizeof(match_ref),
3912 _("match %d"),
3913 compl_curr_match->cp_number);
3914 edit_submode_extra = match_ref;
3915 edit_submode_highl = HLF_R;
3916 if (dollar_vcol >= 0)
3917 curs_columns(FALSE);
3918 }
3919 }
3920 }
3921
3922 // Show a message about what (completion) mode we're in.
3923 if (!compl_opt_suppress_empty)
3924 {
3925 showmode();
3926 if (!shortmess(SHM_COMPLETIONMENU))
3927 {
3928 if (edit_submode_extra != NULL)
3929 {
3930 if (!p_smd)
3931 msg_attr((char *)edit_submode_extra,
3932 edit_submode_highl < HLF_COUNT
3933 ? HL_ATTR(edit_submode_highl) : 0);
3934 }
3935 else
3936 msg_clr_cmdline(); // necessary for "noshowmode"
3937 }
3938 }
3939
3940 // Show the popup menu, unless we got interrupted.
3941 if (enable_pum && !compl_interrupted)
3942 show_pum(save_w_wrow, save_w_leftcol);
3943
3944 compl_was_interrupted = compl_interrupted;
3945 compl_interrupted = FALSE;
3946
3947 return OK;
3948}
3949
3950 static void
3951show_pum(int prev_w_wrow, int prev_w_leftcol)
3952{
3953 // RedrawingDisabled may be set when invoked through complete().
3954 int n = RedrawingDisabled;
3955
3956 RedrawingDisabled = 0;
3957
3958 // If the cursor moved or the display scrolled we need to remove the pum
3959 // first.
3960 setcursor();
3961 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
3962 ins_compl_del_pum();
3963
3964 ins_compl_show_pum();
3965 setcursor();
3966 RedrawingDisabled = n;
3967}
3968
3969/*
3970 * Looks in the first "len" chars. of "src" for search-metachars.
3971 * If dest is not NULL the chars. are copied there quoting (with
3972 * a backslash) the metachars, and dest would be NUL terminated.
3973 * Returns the length (needed) of dest
3974 */
3975 static unsigned
3976quote_meta(char_u *dest, char_u *src, int len)
3977{
3978 unsigned m = (unsigned)len + 1; // one extra for the NUL
3979
3980 for ( ; --len >= 0; src++)
3981 {
3982 switch (*src)
3983 {
3984 case '.':
3985 case '*':
3986 case '[':
3987 if (ctrl_x_mode == CTRL_X_DICTIONARY
3988 || ctrl_x_mode == CTRL_X_THESAURUS)
3989 break;
3990 // FALLTHROUGH
3991 case '~':
3992 if (!p_magic) // quote these only if magic is set
3993 break;
3994 // FALLTHROUGH
3995 case '\\':
3996 if (ctrl_x_mode == CTRL_X_DICTIONARY
3997 || ctrl_x_mode == CTRL_X_THESAURUS)
3998 break;
3999 // FALLTHROUGH
4000 case '^': // currently it's not needed.
4001 case '$':
4002 m++;
4003 if (dest != NULL)
4004 *dest++ = '\\';
4005 break;
4006 }
4007 if (dest != NULL)
4008 *dest++ = *src;
4009 // Copy remaining bytes of a multibyte character.
4010 if (has_mbyte)
4011 {
4012 int i, mb_len;
4013
4014 mb_len = (*mb_ptr2len)(src) - 1;
4015 if (mb_len > 0 && len >= mb_len)
4016 for (i = 0; i < mb_len; ++i)
4017 {
4018 --len;
4019 ++src;
4020 if (dest != NULL)
4021 *dest++ = *src;
4022 }
4023 }
4024 }
4025 if (dest != NULL)
4026 *dest = NUL;
4027
4028 return m;
4029}
4030
4031# if defined(EXITFREE) || defined(PROTO)
4032 void
4033free_insexpand_stuff(void)
4034{
4035 VIM_CLEAR(compl_orig_text);
4036}
4037# endif
4038
4039# ifdef FEAT_SPELL
4040/*
4041 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4042 * spelled word, if there is one.
4043 */
4044 static void
4045spell_back_to_badword(void)
4046{
4047 pos_T tpos = curwin->w_cursor;
4048
4049 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4050 if (curwin->w_cursor.col != tpos.col)
4051 start_arrow(&tpos);
4052}
4053# endif
4054
4055#endif // FEAT_INS_EXPAND