blob: 387f0666d88930350eecba213728eed9131b4006 [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.
Bram Moolenaar964b3742019-05-24 18:54:09 +0200476 wca = (int *)alloc(actual_len * sizeof(int));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100477 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.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200614 match = (compl_T *)alloc_clear(sizeof(compl_T));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100615 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(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001074 sizeof(pumitem_T) * compl_match_arraysize);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001075 if (compl_match_array != NULL)
1076 {
1077 // If the current match is the original text don't find the first
1078 // match after it, don't highlight anything.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001079 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001080 shown_match_ok = TRUE;
1081
1082 i = 0;
1083 compl = compl_first_match;
1084 do
1085 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001086 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001087 && (compl_leader == NULL
1088 || ins_compl_equal(compl, compl_leader, lead_len)))
1089 {
1090 if (!shown_match_ok)
1091 {
1092 if (compl == compl_shown_match || did_find_shown_match)
1093 {
1094 // This item is the shown match or this is the
1095 // first displayed item after the shown match.
1096 compl_shown_match = compl;
1097 did_find_shown_match = TRUE;
1098 shown_match_ok = TRUE;
1099 }
1100 else
1101 // Remember this displayed match for when the
1102 // shown match is just below it.
1103 shown_compl = compl;
1104 cur = i;
1105 }
1106
1107 if (compl->cp_text[CPT_ABBR] != NULL)
1108 compl_match_array[i].pum_text =
1109 compl->cp_text[CPT_ABBR];
1110 else
1111 compl_match_array[i].pum_text = compl->cp_str;
1112 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1113 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1114 if (compl->cp_text[CPT_MENU] != NULL)
1115 compl_match_array[i++].pum_extra =
1116 compl->cp_text[CPT_MENU];
1117 else
1118 compl_match_array[i++].pum_extra = compl->cp_fname;
1119 }
1120
1121 if (compl == compl_shown_match)
1122 {
1123 did_find_shown_match = TRUE;
1124
1125 // When the original text is the shown match don't set
1126 // compl_shown_match.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001127 if (compl->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001128 shown_match_ok = TRUE;
1129
1130 if (!shown_match_ok && shown_compl != NULL)
1131 {
1132 // The shown match isn't displayed, set it to the
1133 // previously displayed match.
1134 compl_shown_match = shown_compl;
1135 shown_match_ok = TRUE;
1136 }
1137 }
1138 compl = compl->cp_next;
1139 } while (compl != NULL && compl != compl_first_match);
1140
1141 if (!shown_match_ok) // no displayed match at all
1142 cur = -1;
1143 }
1144 }
1145 else
1146 {
1147 // popup menu already exists, only need to find the current item.
1148 for (i = 0; i < compl_match_arraysize; ++i)
1149 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1150 || compl_match_array[i].pum_text
1151 == compl_shown_match->cp_text[CPT_ABBR])
1152 {
1153 cur = i;
1154 break;
1155 }
1156 }
1157
1158 if (compl_match_array != NULL)
1159 {
1160 // In Replace mode when a $ is displayed at the end of the line only
1161 // part of the screen would be updated. We do need to redraw here.
1162 dollar_vcol = -1;
1163
1164 // Compute the screen column of the start of the completed text.
1165 // Use the cursor to get all wrapping and other settings right.
1166 col = curwin->w_cursor.col;
1167 curwin->w_cursor.col = compl_col;
1168 pum_display(compl_match_array, compl_match_arraysize, cur);
1169 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001170
1171 if (has_completechanged())
1172 trigger_complete_changed_event(cur);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001173 }
1174}
1175
1176#define DICT_FIRST (1) // use just first element in "dict"
1177#define DICT_EXACT (2) // "dict" is the exact name of a file
1178
1179/*
1180 * Add any identifiers that match the given pattern in the list of dictionary
1181 * files "dict_start" to the list of completions.
1182 */
1183 static void
1184ins_compl_dictionaries(
1185 char_u *dict_start,
1186 char_u *pat,
1187 int flags, // DICT_FIRST and/or DICT_EXACT
1188 int thesaurus) // Thesaurus completion
1189{
1190 char_u *dict = dict_start;
1191 char_u *ptr;
1192 char_u *buf;
1193 regmatch_T regmatch;
1194 char_u **files;
1195 int count;
1196 int save_p_scs;
1197 int dir = compl_direction;
1198
1199 if (*dict == NUL)
1200 {
1201#ifdef FEAT_SPELL
1202 // When 'dictionary' is empty and spell checking is enabled use
1203 // "spell".
1204 if (!thesaurus && curwin->w_p_spell)
1205 dict = (char_u *)"spell";
1206 else
1207#endif
1208 return;
1209 }
1210
1211 buf = alloc(LSIZE);
1212 if (buf == NULL)
1213 return;
1214 regmatch.regprog = NULL; // so that we can goto theend
1215
1216 // If 'infercase' is set, don't use 'smartcase' here
1217 save_p_scs = p_scs;
1218 if (curbuf->b_p_inf)
1219 p_scs = FALSE;
1220
1221 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1222 // to only match at the start of a line. Otherwise just match the
1223 // pattern. Also need to double backslashes.
1224 if (ctrl_x_mode_line_or_eval())
1225 {
1226 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1227 size_t len;
1228
1229 if (pat_esc == NULL)
1230 goto theend;
1231 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001232 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001233 if (ptr == NULL)
1234 {
1235 vim_free(pat_esc);
1236 goto theend;
1237 }
1238 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1239 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1240 vim_free(pat_esc);
1241 vim_free(ptr);
1242 }
1243 else
1244 {
1245 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
1246 if (regmatch.regprog == NULL)
1247 goto theend;
1248 }
1249
1250 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1251 regmatch.rm_ic = ignorecase(pat);
1252 while (*dict != NUL && !got_int && !compl_interrupted)
1253 {
1254 // copy one dictionary file name into buf
1255 if (flags == DICT_EXACT)
1256 {
1257 count = 1;
1258 files = &dict;
1259 }
1260 else
1261 {
1262 // Expand wildcards in the dictionary name, but do not allow
1263 // backticks (for security, the 'dict' option may have been set in
1264 // a modeline).
1265 copy_option_part(&dict, buf, LSIZE, ",");
1266# ifdef FEAT_SPELL
1267 if (!thesaurus && STRCMP(buf, "spell") == 0)
1268 count = -1;
1269 else
1270# endif
1271 if (vim_strchr(buf, '`') != NULL
1272 || expand_wildcards(1, &buf, &count, &files,
1273 EW_FILE|EW_SILENT) != OK)
1274 count = 0;
1275 }
1276
1277# ifdef FEAT_SPELL
1278 if (count == -1)
1279 {
1280 // Complete from active spelling. Skip "\<" in the pattern, we
1281 // don't use it as a RE.
1282 if (pat[0] == '\\' && pat[1] == '<')
1283 ptr = pat + 2;
1284 else
1285 ptr = pat;
1286 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1287 }
1288 else
1289# endif
1290 if (count > 0) // avoid warning for using "files" uninit
1291 {
1292 ins_compl_files(count, files, thesaurus, flags,
1293 &regmatch, buf, &dir);
1294 if (flags != DICT_EXACT)
1295 FreeWild(count, files);
1296 }
1297 if (flags != 0)
1298 break;
1299 }
1300
1301theend:
1302 p_scs = save_p_scs;
1303 vim_regfree(regmatch.regprog);
1304 vim_free(buf);
1305}
1306
1307 static void
1308ins_compl_files(
1309 int count,
1310 char_u **files,
1311 int thesaurus,
1312 int flags,
1313 regmatch_T *regmatch,
1314 char_u *buf,
1315 int *dir)
1316{
1317 char_u *ptr;
1318 int i;
1319 FILE *fp;
1320 int add_r;
1321
1322 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1323 {
1324 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1325 if (flags != DICT_EXACT)
1326 {
1327 vim_snprintf((char *)IObuff, IOSIZE,
1328 _("Scanning dictionary: %s"), (char *)files[i]);
1329 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1330 }
1331
1332 if (fp != NULL)
1333 {
1334 // Read dictionary file line by line.
1335 // Check each line for a match.
1336 while (!got_int && !compl_interrupted
1337 && !vim_fgets(buf, LSIZE, fp))
1338 {
1339 ptr = buf;
1340 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
1341 {
1342 ptr = regmatch->startp[0];
1343 if (ctrl_x_mode_line_or_eval())
1344 ptr = find_line_end(ptr);
1345 else
1346 ptr = find_word_end(ptr);
1347 add_r = ins_compl_add_infercase(regmatch->startp[0],
1348 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001349 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001350 if (thesaurus)
1351 {
1352 char_u *wstart;
1353
1354 // Add the other matches on the line
1355 ptr = buf;
1356 while (!got_int)
1357 {
1358 // Find start of the next word. Skip white
1359 // space and punctuation.
1360 ptr = find_word_start(ptr);
1361 if (*ptr == NUL || *ptr == NL)
1362 break;
1363 wstart = ptr;
1364
1365 // Find end of the word.
1366 if (has_mbyte)
1367 // Japanese words may have characters in
1368 // different classes, only separate words
1369 // with single-byte non-word characters.
1370 while (*ptr != NUL)
1371 {
1372 int l = (*mb_ptr2len)(ptr);
1373
1374 if (l < 2 && !vim_iswordc(*ptr))
1375 break;
1376 ptr += l;
1377 }
1378 else
1379 ptr = find_word_end(ptr);
1380
1381 // Add the word. Skip the regexp match.
1382 if (wstart != regmatch->startp[0])
1383 add_r = ins_compl_add_infercase(wstart,
1384 (int)(ptr - wstart),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001385 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001386 }
1387 }
1388 if (add_r == OK)
1389 // if dir was BACKWARD then honor it just once
1390 *dir = FORWARD;
1391 else if (add_r == FAIL)
1392 break;
1393 // avoid expensive call to vim_regexec() when at end
1394 // of line
1395 if (*ptr == '\n' || got_int)
1396 break;
1397 }
1398 line_breakcheck();
1399 ins_compl_check_keys(50, FALSE);
1400 }
1401 fclose(fp);
1402 }
1403 }
1404}
1405
1406/*
1407 * Find the start of the next word.
1408 * Returns a pointer to the first char of the word. Also stops at a NUL.
1409 */
1410 char_u *
1411find_word_start(char_u *ptr)
1412{
1413 if (has_mbyte)
1414 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1415 ptr += (*mb_ptr2len)(ptr);
1416 else
1417 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1418 ++ptr;
1419 return ptr;
1420}
1421
1422/*
1423 * Find the end of the word. Assumes it starts inside a word.
1424 * Returns a pointer to just after the word.
1425 */
1426 char_u *
1427find_word_end(char_u *ptr)
1428{
1429 int start_class;
1430
1431 if (has_mbyte)
1432 {
1433 start_class = mb_get_class(ptr);
1434 if (start_class > 1)
1435 while (*ptr != NUL)
1436 {
1437 ptr += (*mb_ptr2len)(ptr);
1438 if (mb_get_class(ptr) != start_class)
1439 break;
1440 }
1441 }
1442 else
1443 while (vim_iswordc(*ptr))
1444 ++ptr;
1445 return ptr;
1446}
1447
1448/*
1449 * Find the end of the line, omitting CR and NL at the end.
1450 * Returns a pointer to just after the line.
1451 */
1452 static char_u *
1453find_line_end(char_u *ptr)
1454{
1455 char_u *s;
1456
1457 s = ptr + STRLEN(ptr);
1458 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1459 --s;
1460 return s;
1461}
1462
1463/*
1464 * Free the list of completions
1465 */
1466 static void
1467ins_compl_free(void)
1468{
1469 compl_T *match;
1470 int i;
1471
1472 VIM_CLEAR(compl_pattern);
1473 VIM_CLEAR(compl_leader);
1474
1475 if (compl_first_match == NULL)
1476 return;
1477
1478 ins_compl_del_pum();
1479 pum_clear();
1480
1481 compl_curr_match = compl_first_match;
1482 do
1483 {
1484 match = compl_curr_match;
1485 compl_curr_match = compl_curr_match->cp_next;
1486 vim_free(match->cp_str);
1487 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001488 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001489 vim_free(match->cp_fname);
1490 for (i = 0; i < CPT_COUNT; ++i)
1491 vim_free(match->cp_text[i]);
1492 vim_free(match);
1493 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
1494 compl_first_match = compl_curr_match = NULL;
1495 compl_shown_match = NULL;
1496 compl_old_match = NULL;
1497}
1498
1499 void
1500ins_compl_clear(void)
1501{
1502 compl_cont_status = 0;
1503 compl_started = FALSE;
1504 compl_matches = 0;
1505 VIM_CLEAR(compl_pattern);
1506 VIM_CLEAR(compl_leader);
1507 edit_submode_extra = NULL;
1508 VIM_CLEAR(compl_orig_text);
1509 compl_enter_selects = FALSE;
1510 // clear v:completed_item
1511 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
1512}
1513
1514/*
1515 * Return TRUE when Insert completion is active.
1516 */
1517 int
1518ins_compl_active(void)
1519{
1520 return compl_started;
1521}
1522
1523/*
1524 * Get complete information
1525 */
1526 void
1527get_complete_info(list_T *what_list, dict_T *retdict)
1528{
1529 int ret = OK;
1530 listitem_T *item;
1531#define CI_WHAT_MODE 0x01
1532#define CI_WHAT_PUM_VISIBLE 0x02
1533#define CI_WHAT_ITEMS 0x04
1534#define CI_WHAT_SELECTED 0x08
1535#define CI_WHAT_INSERTED 0x10
1536#define CI_WHAT_ALL 0xff
1537 int what_flag;
1538
1539 if (what_list == NULL)
1540 what_flag = CI_WHAT_ALL;
1541 else
1542 {
1543 what_flag = 0;
1544 for (item = what_list->lv_first; item != NULL; item = item->li_next)
1545 {
1546 char_u *what = tv_get_string(&item->li_tv);
1547
1548 if (STRCMP(what, "mode") == 0)
1549 what_flag |= CI_WHAT_MODE;
1550 else if (STRCMP(what, "pum_visible") == 0)
1551 what_flag |= CI_WHAT_PUM_VISIBLE;
1552 else if (STRCMP(what, "items") == 0)
1553 what_flag |= CI_WHAT_ITEMS;
1554 else if (STRCMP(what, "selected") == 0)
1555 what_flag |= CI_WHAT_SELECTED;
1556 else if (STRCMP(what, "inserted") == 0)
1557 what_flag |= CI_WHAT_INSERTED;
1558 }
1559 }
1560
1561 if (ret == OK && (what_flag & CI_WHAT_MODE))
1562 ret = dict_add_string(retdict, "mode", ins_compl_mode());
1563
1564 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
1565 ret = dict_add_number(retdict, "pum_visible", pum_visible());
1566
1567 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
1568 {
1569 list_T *li;
1570 dict_T *di;
1571 compl_T *match;
1572
1573 li = list_alloc();
1574 if (li == NULL)
1575 return;
1576 ret = dict_add_list(retdict, "items", li);
1577 if (ret == OK && compl_first_match != NULL)
1578 {
1579 match = compl_first_match;
1580 do
1581 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001582 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001583 {
1584 di = dict_alloc();
1585 if (di == NULL)
1586 return;
1587 ret = list_append_dict(li, di);
1588 if (ret != OK)
1589 return;
1590 dict_add_string(di, "word", match->cp_str);
1591 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
1592 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
1593 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
1594 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
1595 dict_add_string(di, "user_data",
1596 match->cp_text[CPT_USER_DATA]);
1597 }
1598 match = match->cp_next;
1599 }
1600 while (match != NULL && match != compl_first_match);
1601 }
1602 }
1603
1604 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
1605 ret = dict_add_number(retdict, "selected", (compl_curr_match != NULL) ?
1606 compl_curr_match->cp_number - 1 : -1);
1607
1608 // TODO
1609 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
1610}
1611
1612/*
1613 * Return Insert completion mode name string
1614 */
1615 static char_u *
1616ins_compl_mode(void)
1617{
1618 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
1619 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
1620
1621 return (char_u *)"";
1622}
1623
1624/*
1625 * Selected one of the matches. When FALSE the match was edited or using the
1626 * longest common string.
1627 */
1628 int
1629ins_compl_used_match(void)
1630{
1631 return compl_used_match;
1632}
1633
1634/*
1635 * Initialize get longest common string.
1636 */
1637 void
1638ins_compl_init_get_longest(void)
1639{
1640 compl_get_longest = FALSE;
1641}
1642
1643/*
1644 * Returns TRUE when insert completion is interrupted.
1645 */
1646 int
1647ins_compl_interrupted(void)
1648{
1649 return compl_interrupted;
1650}
1651
1652/*
1653 * Returns TRUE if the <Enter> key selects a match in the completion popup
1654 * menu.
1655 */
1656 int
1657ins_compl_enter_selects(void)
1658{
1659 return compl_enter_selects;
1660}
1661
1662/*
1663 * Return the column where the text starts that is being completed
1664 */
1665 colnr_T
1666ins_compl_col(void)
1667{
1668 return compl_col;
1669}
1670
1671/*
1672 * Delete one character before the cursor and show the subset of the matches
1673 * that match the word that is now before the cursor.
1674 * Returns the character to be used, NUL if the work is done and another char
1675 * to be got from the user.
1676 */
1677 int
1678ins_compl_bs(void)
1679{
1680 char_u *line;
1681 char_u *p;
1682
1683 line = ml_get_curline();
1684 p = line + curwin->w_cursor.col;
1685 MB_PTR_BACK(line, p);
1686
1687 // Stop completion when the whole word was deleted. For Omni completion
1688 // allow the word to be deleted, we won't match everything.
1689 // Respect the 'backspace' option.
1690 if ((int)(p - line) - (int)compl_col < 0
1691 || ((int)(p - line) - (int)compl_col == 0
1692 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
1693 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1694 - compl_length < 0))
1695 return K_BS;
1696
1697 // Deleted more than what was used to find matches or didn't finish
1698 // finding all matches: need to look for matches all over again.
1699 if (curwin->w_cursor.col <= compl_col + compl_length
1700 || ins_compl_need_restart())
1701 ins_compl_restart();
1702
1703 vim_free(compl_leader);
1704 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
1705 if (compl_leader != NULL)
1706 {
1707 ins_compl_new_leader();
1708 if (compl_shown_match != NULL)
1709 // Make sure current match is not a hidden item.
1710 compl_curr_match = compl_shown_match;
1711 return NUL;
1712 }
1713 return K_BS;
1714}
1715
1716/*
1717 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1718 * be called.
1719 */
1720 static int
1721ins_compl_need_restart(void)
1722{
1723 // Return TRUE if we didn't complete finding matches or when the
1724 // 'completefunc' returned "always" in the "refresh" dictionary item.
1725 return compl_was_interrupted
1726 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
1727 && compl_opt_refresh_always);
1728}
1729
1730/*
1731 * Called after changing "compl_leader".
1732 * Show the popup menu with a different set of matches.
1733 * May also search for matches again if the previous search was interrupted.
1734 */
1735 static void
1736ins_compl_new_leader(void)
1737{
1738 ins_compl_del_pum();
1739 ins_compl_delete();
1740 ins_bytes(compl_leader + ins_compl_len());
1741 compl_used_match = FALSE;
1742
1743 if (compl_started)
1744 ins_compl_set_original_text(compl_leader);
1745 else
1746 {
1747#ifdef FEAT_SPELL
1748 spell_bad_len = 0; // need to redetect bad word
1749#endif
1750 // Matches were cleared, need to search for them now. Befor drawing
1751 // the popup menu display the changed text before the cursor. Set
1752 // "compl_restarting" to avoid that the first match is inserted.
1753 pum_call_update_screen();
1754#ifdef FEAT_GUI
1755 if (gui.in_use)
1756 {
1757 // Show the cursor after the match, not after the redrawn text.
1758 setcursor();
1759 out_flush_cursor(FALSE, FALSE);
1760 }
1761#endif
1762 compl_restarting = TRUE;
1763 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1764 compl_cont_status = 0;
1765 compl_restarting = FALSE;
1766 }
1767
1768 compl_enter_selects = !compl_used_match;
1769
1770 // Show the popup menu with a different set of matches.
1771 ins_compl_show_pum();
1772
1773 // Don't let Enter select the original text when there is no popup menu.
1774 if (compl_match_array == NULL)
1775 compl_enter_selects = FALSE;
1776}
1777
1778/*
1779 * Return the length of the completion, from the completion start column to
1780 * the cursor column. Making sure it never goes below zero.
1781 */
1782 static int
1783ins_compl_len(void)
1784{
1785 int off = (int)curwin->w_cursor.col - (int)compl_col;
1786
1787 if (off < 0)
1788 return 0;
1789 return off;
1790}
1791
1792/*
1793 * Append one character to the match leader. May reduce the number of
1794 * matches.
1795 */
1796 void
1797ins_compl_addleader(int c)
1798{
1799 int cc;
1800
1801 if (stop_arrow() == FAIL)
1802 return;
1803 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1804 {
1805 char_u buf[MB_MAXBYTES + 1];
1806
1807 (*mb_char2bytes)(c, buf);
1808 buf[cc] = NUL;
1809 ins_char_bytes(buf, cc);
1810 if (compl_opt_refresh_always)
1811 AppendToRedobuff(buf);
1812 }
1813 else
1814 {
1815 ins_char(c);
1816 if (compl_opt_refresh_always)
1817 AppendCharToRedobuff(c);
1818 }
1819
1820 // If we didn't complete finding matches we must search again.
1821 if (ins_compl_need_restart())
1822 ins_compl_restart();
1823
1824 // When 'always' is set, don't reset compl_leader. While completing,
1825 // cursor doesn't point original position, changing compl_leader would
1826 // break redo.
1827 if (!compl_opt_refresh_always)
1828 {
1829 vim_free(compl_leader);
1830 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
1831 (int)(curwin->w_cursor.col - compl_col));
1832 if (compl_leader != NULL)
1833 ins_compl_new_leader();
1834 }
1835}
1836
1837/*
1838 * Setup for finding completions again without leaving CTRL-X mode. Used when
1839 * BS or a key was typed while still searching for matches.
1840 */
1841 static void
1842ins_compl_restart(void)
1843{
1844 ins_compl_free();
1845 compl_started = FALSE;
1846 compl_matches = 0;
1847 compl_cont_status = 0;
1848 compl_cont_mode = 0;
1849}
1850
1851/*
1852 * Set the first match, the original text.
1853 */
1854 static void
1855ins_compl_set_original_text(char_u *str)
1856{
1857 char_u *p;
1858
1859 // Replace the original text entry.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001860 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001861 // at the last item for backward completion
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001862 if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001863 {
1864 p = vim_strsave(str);
1865 if (p != NULL)
1866 {
1867 vim_free(compl_first_match->cp_str);
1868 compl_first_match->cp_str = p;
1869 }
1870 }
1871 else if (compl_first_match->cp_prev != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001872 && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001873 {
1874 p = vim_strsave(str);
1875 if (p != NULL)
1876 {
1877 vim_free(compl_first_match->cp_prev->cp_str);
1878 compl_first_match->cp_prev->cp_str = p;
1879 }
1880 }
1881}
1882
1883/*
1884 * Append one character to the match leader. May reduce the number of
1885 * matches.
1886 */
1887 void
1888ins_compl_addfrommatch(void)
1889{
1890 char_u *p;
1891 int len = (int)curwin->w_cursor.col - (int)compl_col;
1892 int c;
1893 compl_T *cp;
1894
1895 p = compl_shown_match->cp_str;
1896 if ((int)STRLEN(p) <= len) // the match is too short
1897 {
1898 // When still at the original match use the first entry that matches
1899 // the leader.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001900 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001901 {
1902 p = NULL;
1903 for (cp = compl_shown_match->cp_next; cp != NULL
1904 && cp != compl_first_match; cp = cp->cp_next)
1905 {
1906 if (compl_leader == NULL
1907 || ins_compl_equal(cp, compl_leader,
1908 (int)STRLEN(compl_leader)))
1909 {
1910 p = cp->cp_str;
1911 break;
1912 }
1913 }
1914 if (p == NULL || (int)STRLEN(p) <= len)
1915 return;
1916 }
1917 else
1918 return;
1919 }
1920 p += len;
1921 c = PTR2CHAR(p);
1922 ins_compl_addleader(c);
1923}
1924
1925/*
1926 * Prepare for Insert mode completion, or stop it.
1927 * Called just after typing a character in Insert mode.
1928 * Returns TRUE when the character is not to be inserted;
1929 */
1930 int
1931ins_compl_prep(int c)
1932{
1933 char_u *ptr;
1934 int want_cindent;
1935 int retval = FALSE;
1936
1937 // Forget any previous 'special' messages if this is actually
1938 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
1939 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
1940 edit_submode_extra = NULL;
1941
1942 // Ignore end of Select mode mapping and mouse scroll buttons.
1943 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
1944 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
1945 return retval;
1946
1947 // Set "compl_get_longest" when finding the first matches.
1948 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
1949 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
1950 {
1951 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
1952 compl_used_match = TRUE;
1953
1954 }
1955
1956 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
1957 {
1958 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
1959 // it will be yet. Now we decide.
1960 switch (c)
1961 {
1962 case Ctrl_E:
1963 case Ctrl_Y:
1964 ctrl_x_mode = CTRL_X_SCROLL;
1965 if (!(State & REPLACE_FLAG))
1966 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1967 else
1968 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1969 edit_submode_pre = NULL;
1970 showmode();
1971 break;
1972 case Ctrl_L:
1973 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1974 break;
1975 case Ctrl_F:
1976 ctrl_x_mode = CTRL_X_FILES;
1977 break;
1978 case Ctrl_K:
1979 ctrl_x_mode = CTRL_X_DICTIONARY;
1980 break;
1981 case Ctrl_R:
1982 // Simply allow ^R to happen without affecting ^X mode
1983 break;
1984 case Ctrl_T:
1985 ctrl_x_mode = CTRL_X_THESAURUS;
1986 break;
1987#ifdef FEAT_COMPL_FUNC
1988 case Ctrl_U:
1989 ctrl_x_mode = CTRL_X_FUNCTION;
1990 break;
1991 case Ctrl_O:
1992 ctrl_x_mode = CTRL_X_OMNI;
1993 break;
1994#endif
1995 case 's':
1996 case Ctrl_S:
1997 ctrl_x_mode = CTRL_X_SPELL;
1998#ifdef FEAT_SPELL
1999 ++emsg_off; // Avoid getting the E756 error twice.
2000 spell_back_to_badword();
2001 --emsg_off;
2002#endif
2003 break;
2004 case Ctrl_RSB:
2005 ctrl_x_mode = CTRL_X_TAGS;
2006 break;
2007#ifdef FEAT_FIND_ID
2008 case Ctrl_I:
2009 case K_S_TAB:
2010 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2011 break;
2012 case Ctrl_D:
2013 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2014 break;
2015#endif
2016 case Ctrl_V:
2017 case Ctrl_Q:
2018 ctrl_x_mode = CTRL_X_CMDLINE;
2019 break;
2020 case Ctrl_P:
2021 case Ctrl_N:
2022 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2023 // just started ^X mode, or there were enough ^X's to cancel
2024 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2025 // do normal expansion when interrupting a different mode (say
2026 // ^X^F^X^P or ^P^X^X^P, see below)
2027 // nothing changes if interrupting mode 0, (eg, the flag
2028 // doesn't change when going to ADDING mode -- Acevedo
2029 if (!(compl_cont_status & CONT_INTRPT))
2030 compl_cont_status |= CONT_LOCAL;
2031 else if (compl_cont_mode != 0)
2032 compl_cont_status &= ~CONT_LOCAL;
2033 // FALLTHROUGH
2034 default:
2035 // If we have typed at least 2 ^X's... for modes != 0, we set
2036 // compl_cont_status = 0 (eg, as if we had just started ^X
2037 // mode).
2038 // For mode 0, we set "compl_cont_mode" to an impossible
2039 // value, in both cases ^X^X can be used to restart the same
2040 // mode (avoiding ADDING mode).
2041 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2042 // 'complete' and local ^P expansions respectively.
2043 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2044 // mode -- Acevedo
2045 if (c == Ctrl_X)
2046 {
2047 if (compl_cont_mode != 0)
2048 compl_cont_status = 0;
2049 else
2050 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2051 }
2052 ctrl_x_mode = CTRL_X_NORMAL;
2053 edit_submode = NULL;
2054 showmode();
2055 break;
2056 }
2057 }
2058 else if (ctrl_x_mode != CTRL_X_NORMAL)
2059 {
2060 // We're already in CTRL-X mode, do we stay in it?
2061 if (!vim_is_ctrl_x_key(c))
2062 {
2063 if (ctrl_x_mode == CTRL_X_SCROLL)
2064 ctrl_x_mode = CTRL_X_NORMAL;
2065 else
2066 ctrl_x_mode = CTRL_X_FINISHED;
2067 edit_submode = NULL;
2068 }
2069 showmode();
2070 }
2071
2072 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2073 {
2074 // Show error message from attempted keyword completion (probably
2075 // 'Pattern not found') until another key is hit, then go back to
2076 // showing what mode we are in.
2077 showmode();
2078 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
2079 && c != Ctrl_R && !ins_compl_pum_key(c))
2080 || ctrl_x_mode == CTRL_X_FINISHED)
2081 {
2082 // Get here when we have finished typing a sequence of ^N and
2083 // ^P or other completion characters in CTRL-X mode. Free up
2084 // memory that was used, and make sure we can redo the insert.
2085 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2086 {
2087 // If any of the original typed text has been changed, eg when
2088 // ignorecase is set, we must add back-spaces to the redo
2089 // buffer. We add as few as necessary to delete just the part
2090 // of the original text that has changed.
2091 // When using the longest match, edited the match or used
2092 // CTRL-E then don't use the current match.
2093 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2094 ptr = compl_curr_match->cp_str;
2095 else
2096 ptr = NULL;
2097 ins_compl_fixRedoBufForLeader(ptr);
2098 }
2099
2100#ifdef FEAT_CINDENT
2101 want_cindent = (can_cindent_get() && cindent_on());
2102#endif
2103 // When completing whole lines: fix indent for 'cindent'.
2104 // Otherwise, break line if it's too long.
2105 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2106 {
2107#ifdef FEAT_CINDENT
2108 // re-indent the current line
2109 if (want_cindent)
2110 {
2111 do_c_expr_indent();
2112 want_cindent = FALSE; // don't do it again
2113 }
2114#endif
2115 }
2116 else
2117 {
2118 int prev_col = curwin->w_cursor.col;
2119
2120 // put the cursor on the last char, for 'tw' formatting
2121 if (prev_col > 0)
2122 dec_cursor();
2123 // only format when something was inserted
2124 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2125 insertchar(NUL, 0, -1);
2126 if (prev_col > 0
2127 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2128 inc_cursor();
2129 }
2130
2131 // If the popup menu is displayed pressing CTRL-Y means accepting
2132 // the selection without inserting anything. When
2133 // compl_enter_selects is set the Enter key does the same.
2134 if ((c == Ctrl_Y || (compl_enter_selects
2135 && (c == CAR || c == K_KENTER || c == NL)))
2136 && pum_visible())
2137 retval = TRUE;
2138
2139 // CTRL-E means completion is Ended, go back to the typed text.
2140 // but only do this, if the Popup is still visible
2141 if (c == Ctrl_E)
2142 {
2143 ins_compl_delete();
2144 if (compl_leader != NULL)
2145 ins_bytes(compl_leader + ins_compl_len());
2146 else if (compl_first_match != NULL)
2147 ins_bytes(compl_orig_text + ins_compl_len());
2148 retval = TRUE;
2149 }
2150
2151 auto_format(FALSE, TRUE);
2152
2153 ins_compl_free();
2154 compl_started = FALSE;
2155 compl_matches = 0;
2156 if (!shortmess(SHM_COMPLETIONMENU))
2157 msg_clr_cmdline(); // necessary for "noshowmode"
2158 ctrl_x_mode = CTRL_X_NORMAL;
2159 compl_enter_selects = FALSE;
2160 if (edit_submode != NULL)
2161 {
2162 edit_submode = NULL;
2163 showmode();
2164 }
2165
2166#ifdef FEAT_CMDWIN
2167 if (c == Ctrl_C && cmdwin_type != 0)
2168 // Avoid the popup menu remains displayed when leaving the
2169 // command line window.
2170 update_screen(0);
2171#endif
2172#ifdef FEAT_CINDENT
2173 // Indent now if a key was typed that is in 'cinkeys'.
2174 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2175 do_c_expr_indent();
2176#endif
2177 // Trigger the CompleteDone event to give scripts a chance to act
2178 // upon the completion.
2179 ins_apply_autocmds(EVENT_COMPLETEDONE);
2180 }
2181 }
2182 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2183 // Trigger the CompleteDone event to give scripts a chance to act
2184 // upon the (possibly failed) completion.
2185 ins_apply_autocmds(EVENT_COMPLETEDONE);
2186
2187 // reset continue_* if we left expansion-mode, if we stay they'll be
2188 // (re)set properly in ins_complete()
2189 if (!vim_is_ctrl_x_key(c))
2190 {
2191 compl_cont_status = 0;
2192 compl_cont_mode = 0;
2193 }
2194
2195 return retval;
2196}
2197
2198/*
2199 * Fix the redo buffer for the completion leader replacing some of the typed
2200 * text. This inserts backspaces and appends the changed text.
2201 * "ptr" is the known leader text or NUL.
2202 */
2203 static void
2204ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2205{
2206 int len;
2207 char_u *p;
2208 char_u *ptr = ptr_arg;
2209
2210 if (ptr == NULL)
2211 {
2212 if (compl_leader != NULL)
2213 ptr = compl_leader;
2214 else
2215 return; // nothing to do
2216 }
2217 if (compl_orig_text != NULL)
2218 {
2219 p = compl_orig_text;
2220 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2221 ;
2222 if (len > 0)
2223 len -= (*mb_head_off)(p, p + len);
2224 for (p += len; *p != NUL; MB_PTR_ADV(p))
2225 AppendCharToRedobuff(K_BS);
2226 }
2227 else
2228 len = 0;
2229 if (ptr != NULL)
2230 AppendToRedobuffLit(ptr + len, -1);
2231}
2232
2233/*
2234 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2235 * (depending on flag) starting from buf and looking for a non-scanned
2236 * buffer (other than curbuf). curbuf is special, if it is called with
2237 * buf=curbuf then it has to be the first call for a given flag/expansion.
2238 *
2239 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2240 */
2241 static buf_T *
2242ins_compl_next_buf(buf_T *buf, int flag)
2243{
2244 static win_T *wp = NULL;
2245
2246 if (flag == 'w') // just windows
2247 {
2248 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2249 wp = curwin;
2250 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2251 && wp->w_buffer->b_scanned)
2252 ;
2253 buf = wp->w_buffer;
2254 }
2255 else
2256 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2257 // (unlisted buffers)
2258 // When completing whole lines skip unloaded buffers.
2259 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2260 && ((flag == 'U'
2261 ? buf->b_p_bl
2262 : (!buf->b_p_bl
2263 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2264 || buf->b_scanned))
2265 ;
2266 return buf;
2267}
2268
2269#ifdef FEAT_COMPL_FUNC
2270/*
2271 * Execute user defined complete function 'completefunc' or 'omnifunc', and
2272 * get matches in "matches".
2273 */
2274 static void
2275expand_by_function(
2276 int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
2277 char_u *base)
2278{
2279 list_T *matchlist = NULL;
2280 dict_T *matchdict = NULL;
2281 typval_T args[3];
2282 char_u *funcname;
2283 pos_T pos;
2284 win_T *curwin_save;
2285 buf_T *curbuf_save;
2286 typval_T rettv;
2287 int save_State = State;
2288
2289 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2290 if (*funcname == NUL)
2291 return;
2292
2293 // Call 'completefunc' to obtain the list of matches.
2294 args[0].v_type = VAR_NUMBER;
2295 args[0].vval.v_number = 0;
2296 args[1].v_type = VAR_STRING;
2297 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2298 args[2].v_type = VAR_UNKNOWN;
2299
2300 pos = curwin->w_cursor;
2301 curwin_save = curwin;
2302 curbuf_save = curbuf;
2303
2304 // Call a function, which returns a list or dict.
2305 if (call_vim_function(funcname, 2, args, &rettv) == OK)
2306 {
2307 switch (rettv.v_type)
2308 {
2309 case VAR_LIST:
2310 matchlist = rettv.vval.v_list;
2311 break;
2312 case VAR_DICT:
2313 matchdict = rettv.vval.v_dict;
2314 break;
2315 case VAR_SPECIAL:
2316 if (rettv.vval.v_number == VVAL_NONE)
2317 compl_opt_suppress_empty = TRUE;
2318 // FALLTHROUGH
2319 default:
2320 // TODO: Give error message?
2321 clear_tv(&rettv);
2322 break;
2323 }
2324 }
2325
2326 if (curwin_save != curwin || curbuf_save != curbuf)
2327 {
2328 emsg(_(e_complwin));
2329 goto theend;
2330 }
2331 curwin->w_cursor = pos; // restore the cursor position
2332 validate_cursor();
2333 if (!EQUAL_POS(curwin->w_cursor, pos))
2334 {
2335 emsg(_(e_compldel));
2336 goto theend;
2337 }
2338
2339 if (matchlist != NULL)
2340 ins_compl_add_list(matchlist);
2341 else if (matchdict != NULL)
2342 ins_compl_add_dict(matchdict);
2343
2344theend:
2345 // Restore State, it might have been changed.
2346 State = save_State;
2347
2348 if (matchdict != NULL)
2349 dict_unref(matchdict);
2350 if (matchlist != NULL)
2351 list_unref(matchlist);
2352}
2353#endif // FEAT_COMPL_FUNC
2354
2355#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2356/*
2357 * Add completions from a list.
2358 */
2359 static void
2360ins_compl_add_list(list_T *list)
2361{
2362 listitem_T *li;
2363 int dir = compl_direction;
2364
2365 // Go through the List with matches and add each of them.
2366 for (li = list->lv_first; li != NULL; li = li->li_next)
2367 {
2368 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
2369 // if dir was BACKWARD then honor it just once
2370 dir = FORWARD;
2371 else if (did_emsg)
2372 break;
2373 }
2374}
2375
2376/*
2377 * Add completions from a dict.
2378 */
2379 static void
2380ins_compl_add_dict(dict_T *dict)
2381{
2382 dictitem_T *di_refresh;
2383 dictitem_T *di_words;
2384
2385 // Check for optional "refresh" item.
2386 compl_opt_refresh_always = FALSE;
2387 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2388 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2389 {
2390 char_u *v = di_refresh->di_tv.vval.v_string;
2391
2392 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2393 compl_opt_refresh_always = TRUE;
2394 }
2395
2396 // Add completions from a "words" list.
2397 di_words = dict_find(dict, (char_u *)"words", 5);
2398 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2399 ins_compl_add_list(di_words->di_tv.vval.v_list);
2400}
2401
2402/*
2403 * Add a match to the list of matches from a typeval_T.
2404 * If the given string is already in the list of completions, then return
2405 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2406 * maybe because alloc() returns NULL, then FAIL is returned.
2407 */
2408 int
2409ins_compl_add_tv(typval_T *tv, int dir)
2410{
2411 char_u *word;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002412 int dup = FALSE;
2413 int empty = FALSE;
2414 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002415 char_u *(cptext[CPT_COUNT]);
2416
2417 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2418 {
2419 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2420 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2421 (char_u *)"abbr", FALSE);
2422 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2423 (char_u *)"menu", FALSE);
2424 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2425 (char_u *)"kind", FALSE);
2426 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2427 (char_u *)"info", FALSE);
2428 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
2429 (char_u *)"user_data", FALSE);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002430 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2431 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2432 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002433 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002434 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002435 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002436 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2437 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2438 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2439 flags |= CP_EQUAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002440 }
2441 else
2442 {
2443 word = tv_get_string_chk(tv);
2444 vim_memset(cptext, 0, sizeof(cptext));
2445 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002446 if (word == NULL || (!empty && *word == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002447 return FAIL;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002448 return ins_compl_add(word, -1, NULL, cptext, dir, flags, dup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002449}
2450#endif
2451
2452/*
2453 * Get the next expansion(s), using "compl_pattern".
2454 * The search starts at position "ini" in curbuf and in the direction
2455 * compl_direction.
2456 * When "compl_started" is FALSE start at that position, otherwise continue
2457 * where we stopped searching before.
2458 * This may return before finding all the matches.
2459 * Return the total number of matches or -1 if still unknown -- Acevedo
2460 */
2461 static int
2462ins_compl_get_exp(pos_T *ini)
2463{
2464 static pos_T first_match_pos;
2465 static pos_T last_match_pos;
2466 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2467 static int found_all = FALSE; // Found all matches of a
2468 // certain type.
2469 static buf_T *ins_buf = NULL; // buffer being scanned
2470
2471 pos_T *pos;
2472 char_u **matches;
2473 int save_p_scs;
2474 int save_p_ws;
2475 int save_p_ic;
2476 int i;
2477 int num_matches;
2478 int len;
2479 int found_new_match;
2480 int type = ctrl_x_mode;
2481 char_u *ptr;
2482 char_u *dict = NULL;
2483 int dict_f = 0;
2484 int set_match_pos;
2485
2486 if (!compl_started)
2487 {
2488 FOR_ALL_BUFFERS(ins_buf)
2489 ins_buf->b_scanned = 0;
2490 found_all = FALSE;
2491 ins_buf = curbuf;
2492 e_cpt = (compl_cont_status & CONT_LOCAL)
2493 ? (char_u *)"." : curbuf->b_p_cpt;
2494 last_match_pos = first_match_pos = *ini;
2495 }
2496 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2497 ins_buf = curbuf; // In case the buffer was wiped out.
2498
2499 compl_old_match = compl_curr_match; // remember the last current match
2500 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2501
2502 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2503 for (;;)
2504 {
2505 found_new_match = FAIL;
2506 set_match_pos = FALSE;
2507
2508 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2509 // or if found_all says this entry is done. For ^X^L only use the
2510 // entries from 'complete' that look in loaded buffers.
2511 if ((ctrl_x_mode == CTRL_X_NORMAL
2512 || ctrl_x_mode_line_or_eval())
2513 && (!compl_started || found_all))
2514 {
2515 found_all = FALSE;
2516 while (*e_cpt == ',' || *e_cpt == ' ')
2517 e_cpt++;
2518 if (*e_cpt == '.' && !curbuf->b_scanned)
2519 {
2520 ins_buf = curbuf;
2521 first_match_pos = *ini;
2522 // Move the cursor back one character so that ^N can match the
2523 // word immediately after the cursor.
2524 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2525 {
2526 // Move the cursor to after the last character in the
2527 // buffer, so that word at start of buffer is found
2528 // correctly.
2529 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2530 first_match_pos.col =
2531 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2532 }
2533 last_match_pos = first_match_pos;
2534 type = 0;
2535
2536 // Remember the first match so that the loop stops when we
2537 // wrap and come back there a second time.
2538 set_match_pos = TRUE;
2539 }
2540 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2541 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2542 {
2543 // Scan a buffer, but not the current one.
2544 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2545 {
2546 compl_started = TRUE;
2547 first_match_pos.col = last_match_pos.col = 0;
2548 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2549 last_match_pos.lnum = 0;
2550 type = 0;
2551 }
2552 else // unloaded buffer, scan like dictionary
2553 {
2554 found_all = TRUE;
2555 if (ins_buf->b_fname == NULL)
2556 continue;
2557 type = CTRL_X_DICTIONARY;
2558 dict = ins_buf->b_fname;
2559 dict_f = DICT_EXACT;
2560 }
2561 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2562 ins_buf->b_fname == NULL
2563 ? buf_spname(ins_buf)
2564 : ins_buf->b_sfname == NULL
2565 ? ins_buf->b_fname
2566 : ins_buf->b_sfname);
2567 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2568 }
2569 else if (*e_cpt == NUL)
2570 break;
2571 else
2572 {
2573 if (ctrl_x_mode_line_or_eval())
2574 type = -1;
2575 else if (*e_cpt == 'k' || *e_cpt == 's')
2576 {
2577 if (*e_cpt == 'k')
2578 type = CTRL_X_DICTIONARY;
2579 else
2580 type = CTRL_X_THESAURUS;
2581 if (*++e_cpt != ',' && *e_cpt != NUL)
2582 {
2583 dict = e_cpt;
2584 dict_f = DICT_FIRST;
2585 }
2586 }
2587#ifdef FEAT_FIND_ID
2588 else if (*e_cpt == 'i')
2589 type = CTRL_X_PATH_PATTERNS;
2590 else if (*e_cpt == 'd')
2591 type = CTRL_X_PATH_DEFINES;
2592#endif
2593 else if (*e_cpt == ']' || *e_cpt == 't')
2594 {
2595 type = CTRL_X_TAGS;
2596 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2597 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2598 }
2599 else
2600 type = -1;
2601
2602 // in any case e_cpt is advanced to the next entry
2603 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2604
2605 found_all = TRUE;
2606 if (type == -1)
2607 continue;
2608 }
2609 }
2610
2611 // If complete() was called then compl_pattern has been reset. The
2612 // following won't work then, bail out.
2613 if (compl_pattern == NULL)
2614 break;
2615
2616 switch (type)
2617 {
2618 case -1:
2619 break;
2620#ifdef FEAT_FIND_ID
2621 case CTRL_X_PATH_PATTERNS:
2622 case CTRL_X_PATH_DEFINES:
2623 find_pattern_in_path(compl_pattern, compl_direction,
2624 (int)STRLEN(compl_pattern), FALSE, FALSE,
2625 (type == CTRL_X_PATH_DEFINES
2626 && !(compl_cont_status & CONT_SOL))
2627 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2628 (linenr_T)1, (linenr_T)MAXLNUM);
2629 break;
2630#endif
2631
2632 case CTRL_X_DICTIONARY:
2633 case CTRL_X_THESAURUS:
2634 ins_compl_dictionaries(
2635 dict != NULL ? dict
2636 : (type == CTRL_X_THESAURUS
2637 ? (*curbuf->b_p_tsr == NUL
2638 ? p_tsr
2639 : curbuf->b_p_tsr)
2640 : (*curbuf->b_p_dict == NUL
2641 ? p_dict
2642 : curbuf->b_p_dict)),
2643 compl_pattern,
2644 dict != NULL ? dict_f
2645 : 0, type == CTRL_X_THESAURUS);
2646 dict = NULL;
2647 break;
2648
2649 case CTRL_X_TAGS:
2650 // set p_ic according to p_ic, p_scs and pat for find_tags().
2651 save_p_ic = p_ic;
2652 p_ic = ignorecase(compl_pattern);
2653
2654 // Find up to TAG_MANY matches. Avoids that an enormous number
2655 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002656 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002657 if (find_tags(compl_pattern, &num_matches, &matches,
2658 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2659 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2660 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002661 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002662 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002663 p_ic = save_p_ic;
2664 break;
2665
2666 case CTRL_X_FILES:
2667 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2668 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2669 {
2670
2671 // May change home directory back to "~".
2672 tilde_replace(compl_pattern, num_matches, matches);
2673 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2674 }
2675 break;
2676
2677 case CTRL_X_CMDLINE:
2678 if (expand_cmdline(&compl_xp, compl_pattern,
2679 (int)STRLEN(compl_pattern),
2680 &num_matches, &matches) == EXPAND_OK)
2681 ins_compl_add_matches(num_matches, matches, FALSE);
2682 break;
2683
2684#ifdef FEAT_COMPL_FUNC
2685 case CTRL_X_FUNCTION:
2686 case CTRL_X_OMNI:
2687 expand_by_function(type, compl_pattern);
2688 break;
2689#endif
2690
2691 case CTRL_X_SPELL:
2692#ifdef FEAT_SPELL
2693 num_matches = expand_spelling(first_match_pos.lnum,
2694 compl_pattern, &matches);
2695 if (num_matches > 0)
2696 ins_compl_add_matches(num_matches, matches, p_ic);
2697#endif
2698 break;
2699
2700 default: // normal ^P/^N and ^X^L
2701 // If 'infercase' is set, don't use 'smartcase' here
2702 save_p_scs = p_scs;
2703 if (ins_buf->b_p_inf)
2704 p_scs = FALSE;
2705
2706 // Buffers other than curbuf are scanned from the beginning or the
2707 // end but never from the middle, thus setting nowrapscan in this
2708 // buffers is a good idea, on the other hand, we always set
2709 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
2710 save_p_ws = p_ws;
2711 if (ins_buf != curbuf)
2712 p_ws = FALSE;
2713 else if (*e_cpt == '.')
2714 p_ws = TRUE;
2715 for (;;)
2716 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002717 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002718
2719 ++msg_silent; // Don't want messages for wrapscan.
2720
2721 // ctrl_x_mode_line_or_eval() || word-wise search that
2722 // has added a word that was at the beginning of the line
2723 if (ctrl_x_mode_line_or_eval()
2724 || (compl_cont_status & CONT_SOL))
2725 found_new_match = search_for_exact_line(ins_buf, pos,
2726 compl_direction, compl_pattern);
2727 else
2728 found_new_match = searchit(NULL, ins_buf, pos, NULL,
2729 compl_direction,
2730 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
2731 RE_LAST, (linenr_T)0, NULL, NULL);
2732 --msg_silent;
2733 if (!compl_started || set_match_pos)
2734 {
2735 // set "compl_started" even on fail
2736 compl_started = TRUE;
2737 first_match_pos = *pos;
2738 last_match_pos = *pos;
2739 set_match_pos = FALSE;
2740 }
2741 else if (first_match_pos.lnum == last_match_pos.lnum
2742 && first_match_pos.col == last_match_pos.col)
2743 found_new_match = FAIL;
2744 if (found_new_match == FAIL)
2745 {
2746 if (ins_buf == curbuf)
2747 found_all = TRUE;
2748 break;
2749 }
2750
2751 // when ADDING, the text before the cursor matches, skip it
2752 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
2753 && ini->lnum == pos->lnum
2754 && ini->col == pos->col)
2755 continue;
2756 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
2757 if (ctrl_x_mode_line_or_eval())
2758 {
2759 if (compl_cont_status & CONT_ADDING)
2760 {
2761 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
2762 continue;
2763 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2764 if (!p_paste)
2765 ptr = skipwhite(ptr);
2766 }
2767 len = (int)STRLEN(ptr);
2768 }
2769 else
2770 {
2771 char_u *tmp_ptr = ptr;
2772
2773 if (compl_cont_status & CONT_ADDING)
2774 {
2775 tmp_ptr += compl_length;
2776 // Skip if already inside a word.
2777 if (vim_iswordp(tmp_ptr))
2778 continue;
2779 // Find start of next word.
2780 tmp_ptr = find_word_start(tmp_ptr);
2781 }
2782 // Find end of this word.
2783 tmp_ptr = find_word_end(tmp_ptr);
2784 len = (int)(tmp_ptr - ptr);
2785
2786 if ((compl_cont_status & CONT_ADDING)
2787 && len == compl_length)
2788 {
2789 if (pos->lnum < ins_buf->b_ml.ml_line_count)
2790 {
2791 // Try next line, if any. the new word will be
2792 // "join" as if the normal command "J" was used.
2793 // IOSIZE is always greater than
2794 // compl_length, so the next STRNCPY always
2795 // works -- Acevedo
2796 STRNCPY(IObuff, ptr, len);
2797 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2798 tmp_ptr = ptr = skipwhite(ptr);
2799 // Find start of next word.
2800 tmp_ptr = find_word_start(tmp_ptr);
2801 // Find end of next word.
2802 tmp_ptr = find_word_end(tmp_ptr);
2803 if (tmp_ptr > ptr)
2804 {
2805 if (*ptr != ')' && IObuff[len - 1] != TAB)
2806 {
2807 if (IObuff[len - 1] != ' ')
2808 IObuff[len++] = ' ';
2809 // IObuf =~ "\k.* ", thus len >= 2
2810 if (p_js
2811 && (IObuff[len - 2] == '.'
2812 || (vim_strchr(p_cpo, CPO_JOINSP)
2813 == NULL
2814 && (IObuff[len - 2] == '?'
2815 || IObuff[len - 2] == '!'))))
2816 IObuff[len++] = ' ';
2817 }
2818 // copy as much as possible of the new word
2819 if (tmp_ptr - ptr >= IOSIZE - len)
2820 tmp_ptr = ptr + IOSIZE - len - 1;
2821 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
2822 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002823 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002824 }
2825 IObuff[len] = NUL;
2826 ptr = IObuff;
2827 }
2828 if (len == compl_length)
2829 continue;
2830 }
2831 }
2832 if (ins_compl_add_infercase(ptr, len, p_ic,
2833 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002834 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002835 {
2836 found_new_match = OK;
2837 break;
2838 }
2839 }
2840 p_scs = save_p_scs;
2841 p_ws = save_p_ws;
2842 }
2843
2844 // check if compl_curr_match has changed, (e.g. other type of
2845 // expansion added something)
2846 if (type != 0 && compl_curr_match != compl_old_match)
2847 found_new_match = OK;
2848
2849 // break the loop for specialized modes (use 'complete' just for the
2850 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
2851 // match
2852 if ((ctrl_x_mode != CTRL_X_NORMAL
2853 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
2854 {
2855 if (got_int)
2856 break;
2857 // Fill the popup menu as soon as possible.
2858 if (type != -1)
2859 ins_compl_check_keys(0, FALSE);
2860
2861 if ((ctrl_x_mode != CTRL_X_NORMAL
2862 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
2863 break;
2864 compl_started = TRUE;
2865 }
2866 else
2867 {
2868 // Mark a buffer scanned when it has been scanned completely
2869 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
2870 ins_buf->b_scanned = TRUE;
2871
2872 compl_started = FALSE;
2873 }
2874 }
2875 compl_started = TRUE;
2876
2877 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
2878 && *e_cpt == NUL) // Got to end of 'complete'
2879 found_new_match = FAIL;
2880
2881 i = -1; // total of matches, unknown
2882 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
2883 && !ctrl_x_mode_line_or_eval()))
2884 i = ins_compl_make_cyclic();
2885
2886 if (compl_old_match != NULL)
2887 {
2888 // If several matches were added (FORWARD) or the search failed and has
2889 // just been made cyclic then we have to move compl_curr_match to the
2890 // next or previous entry (if any) -- Acevedo
2891 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
2892 : compl_old_match->cp_prev;
2893 if (compl_curr_match == NULL)
2894 compl_curr_match = compl_old_match;
2895 }
2896 return i;
2897}
2898
2899/*
2900 * Delete the old text being completed.
2901 */
2902 void
2903ins_compl_delete(void)
2904{
2905 int col;
2906
2907 // In insert mode: Delete the typed part.
2908 // In replace mode: Put the old characters back, if any.
2909 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
2910 if ((int)curwin->w_cursor.col > col)
2911 {
2912 if (stop_arrow() == FAIL)
2913 return;
2914 backspace_until_column(col);
2915 }
2916
2917 // TODO: is this sufficient for redrawing? Redrawing everything causes
2918 // flicker, thus we can't do that.
2919 changed_cline_bef_curs();
2920 // clear v:completed_item
2921 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
2922}
2923
2924/*
2925 * Insert the new text being completed.
2926 * "in_compl_func" is TRUE when called from complete_check().
2927 */
2928 void
2929ins_compl_insert(int in_compl_func)
2930{
2931 dict_T *dict;
2932
2933 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002934 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002935 compl_used_match = FALSE;
2936 else
2937 compl_used_match = TRUE;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002938 dict = ins_compl_dict_alloc(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002939 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
2940 if (!in_compl_func)
2941 compl_curr_match = compl_shown_match;
2942}
2943
2944/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002945 * Allocate Dict for the completed item.
2946 * { word, abbr, menu, kind, info }
2947 */
2948 static dict_T *
2949ins_compl_dict_alloc(compl_T *match)
2950{
2951 dict_T *dict = dict_alloc_lock(VAR_FIXED);
2952
2953 if (dict != NULL)
2954 {
2955 dict_add_string(dict, "word", match->cp_str);
2956 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
2957 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
2958 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
2959 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
2960 dict_add_string(dict, "user_data", match->cp_text[CPT_USER_DATA]);
2961 }
2962 return dict;
2963}
2964
2965/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002966 * Fill in the next completion in the current direction.
2967 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
2968 * get more completions. If it is FALSE, then we just do nothing when there
2969 * are no more completions in a given direction. The latter case is used when
2970 * we are still in the middle of finding completions, to allow browsing
2971 * through the ones found so far.
2972 * Return the total number of matches, or -1 if still unknown -- webb.
2973 *
2974 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
2975 * compl_shown_match here.
2976 *
2977 * Note that this function may be called recursively once only. First with
2978 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
2979 * calls this function with "allow_get_expansion" FALSE.
2980 */
2981 static int
2982ins_compl_next(
2983 int allow_get_expansion,
2984 int count, // repeat completion this many times; should
2985 // be at least 1
2986 int insert_match, // Insert the newly selected match
2987 int in_compl_func) // called from complete_check()
2988{
2989 int num_matches = -1;
2990 int todo = count;
2991 compl_T *found_compl = NULL;
2992 int found_end = FALSE;
2993 int advance;
2994 int started = compl_started;
2995
2996 // When user complete function return -1 for findstart which is next
2997 // time of 'always', compl_shown_match become NULL.
2998 if (compl_shown_match == NULL)
2999 return -1;
3000
3001 if (compl_leader != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003002 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003003 {
3004 // Set "compl_shown_match" to the actually shown match, it may differ
3005 // when "compl_leader" is used to omit some of the matches.
3006 while (!ins_compl_equal(compl_shown_match,
3007 compl_leader, (int)STRLEN(compl_leader))
3008 && compl_shown_match->cp_next != NULL
3009 && compl_shown_match->cp_next != compl_first_match)
3010 compl_shown_match = compl_shown_match->cp_next;
3011
3012 // If we didn't find it searching forward, and compl_shows_dir is
3013 // backward, find the last match.
3014 if (compl_shows_dir == BACKWARD
3015 && !ins_compl_equal(compl_shown_match,
3016 compl_leader, (int)STRLEN(compl_leader))
3017 && (compl_shown_match->cp_next == NULL
3018 || compl_shown_match->cp_next == compl_first_match))
3019 {
3020 while (!ins_compl_equal(compl_shown_match,
3021 compl_leader, (int)STRLEN(compl_leader))
3022 && compl_shown_match->cp_prev != NULL
3023 && compl_shown_match->cp_prev != compl_first_match)
3024 compl_shown_match = compl_shown_match->cp_prev;
3025 }
3026 }
3027
3028 if (allow_get_expansion && insert_match
3029 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3030 // Delete old text to be replaced
3031 ins_compl_delete();
3032
3033 // When finding the longest common text we stick at the original text,
3034 // don't let CTRL-N or CTRL-P move to the first match.
3035 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3036
3037 // When restarting the search don't insert the first match either.
3038 if (compl_restarting)
3039 {
3040 advance = FALSE;
3041 compl_restarting = FALSE;
3042 }
3043
3044 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3045 // around.
3046 while (--todo >= 0)
3047 {
3048 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3049 {
3050 compl_shown_match = compl_shown_match->cp_next;
3051 found_end = (compl_first_match != NULL
3052 && (compl_shown_match->cp_next == compl_first_match
3053 || compl_shown_match == compl_first_match));
3054 }
3055 else if (compl_shows_dir == BACKWARD
3056 && compl_shown_match->cp_prev != NULL)
3057 {
3058 found_end = (compl_shown_match == compl_first_match);
3059 compl_shown_match = compl_shown_match->cp_prev;
3060 found_end |= (compl_shown_match == compl_first_match);
3061 }
3062 else
3063 {
3064 if (!allow_get_expansion)
3065 {
3066 if (advance)
3067 {
3068 if (compl_shows_dir == BACKWARD)
3069 compl_pending -= todo + 1;
3070 else
3071 compl_pending += todo + 1;
3072 }
3073 return -1;
3074 }
3075
3076 if (!compl_no_select && advance)
3077 {
3078 if (compl_shows_dir == BACKWARD)
3079 --compl_pending;
3080 else
3081 ++compl_pending;
3082 }
3083
3084 // Find matches.
3085 num_matches = ins_compl_get_exp(&compl_startpos);
3086
3087 // handle any pending completions
3088 while (compl_pending != 0 && compl_direction == compl_shows_dir
3089 && advance)
3090 {
3091 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3092 {
3093 compl_shown_match = compl_shown_match->cp_next;
3094 --compl_pending;
3095 }
3096 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3097 {
3098 compl_shown_match = compl_shown_match->cp_prev;
3099 ++compl_pending;
3100 }
3101 else
3102 break;
3103 }
3104 found_end = FALSE;
3105 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003106 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003107 && compl_leader != NULL
3108 && !ins_compl_equal(compl_shown_match,
3109 compl_leader, (int)STRLEN(compl_leader)))
3110 ++todo;
3111 else
3112 // Remember a matching item.
3113 found_compl = compl_shown_match;
3114
3115 // Stop at the end of the list when we found a usable match.
3116 if (found_end)
3117 {
3118 if (found_compl != NULL)
3119 {
3120 compl_shown_match = found_compl;
3121 break;
3122 }
3123 todo = 1; // use first usable match after wrapping around
3124 }
3125 }
3126
3127 // Insert the text of the new completion, or the compl_leader.
3128 if (compl_no_insert && !started)
3129 {
3130 ins_bytes(compl_orig_text + ins_compl_len());
3131 compl_used_match = FALSE;
3132 }
3133 else if (insert_match)
3134 {
3135 if (!compl_get_longest || compl_used_match)
3136 ins_compl_insert(in_compl_func);
3137 else
3138 ins_bytes(compl_leader + ins_compl_len());
3139 }
3140 else
3141 compl_used_match = FALSE;
3142
3143 if (!allow_get_expansion)
3144 {
3145 // may undisplay the popup menu first
3146 ins_compl_upd_pum();
3147
3148 if (pum_enough_matches())
3149 // Will display the popup menu, don't redraw yet to avoid flicker.
3150 pum_call_update_screen();
3151 else
3152 // Not showing the popup menu yet, redraw to show the user what was
3153 // inserted.
3154 update_screen(0);
3155
3156 // display the updated popup menu
3157 ins_compl_show_pum();
3158#ifdef FEAT_GUI
3159 if (gui.in_use)
3160 {
3161 // Show the cursor after the match, not after the redrawn text.
3162 setcursor();
3163 out_flush_cursor(FALSE, FALSE);
3164 }
3165#endif
3166
3167 // Delete old text to be replaced, since we're still searching and
3168 // don't want to match ourselves!
3169 ins_compl_delete();
3170 }
3171
3172 // Enter will select a match when the match wasn't inserted and the popup
3173 // menu is visible.
3174 if (compl_no_insert && !started)
3175 compl_enter_selects = TRUE;
3176 else
3177 compl_enter_selects = !insert_match && compl_match_array != NULL;
3178
3179 // Show the file name for the match (if any)
3180 // Truncate the file name to avoid a wait for return.
3181 if (compl_shown_match->cp_fname != NULL)
3182 {
3183 char *lead = _("match in file");
3184 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3185 char_u *s;
3186 char_u *e;
3187
3188 if (space > 0)
3189 {
3190 // We need the tail that fits. With double-byte encoding going
3191 // back from the end is very slow, thus go from the start and keep
3192 // the text that fits in "space" between "s" and "e".
3193 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3194 {
3195 space -= ptr2cells(e);
3196 while (space < 0)
3197 {
3198 space += ptr2cells(s);
3199 MB_PTR_ADV(s);
3200 }
3201 }
3202 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3203 s > compl_shown_match->cp_fname ? "<" : "", s);
3204 msg((char *)IObuff);
3205 redraw_cmdline = FALSE; // don't overwrite!
3206 }
3207 }
3208
3209 return num_matches;
3210}
3211
3212/*
3213 * Call this while finding completions, to check whether the user has hit a key
3214 * that should change the currently displayed completion, or exit completion
3215 * mode. Also, when compl_pending is not zero, show a completion as soon as
3216 * possible. -- webb
3217 * "frequency" specifies out of how many calls we actually check.
3218 * "in_compl_func" is TRUE when called from complete_check(), don't set
3219 * compl_curr_match.
3220 */
3221 void
3222ins_compl_check_keys(int frequency, int in_compl_func)
3223{
3224 static int count = 0;
3225 int c;
3226
3227 // Don't check when reading keys from a script, :normal or feedkeys().
3228 // That would break the test scripts. But do check for keys when called
3229 // from complete_check().
3230 if (!in_compl_func && (using_script() || ex_normal_busy))
3231 return;
3232
3233 // Only do this at regular intervals
3234 if (++count < frequency)
3235 return;
3236 count = 0;
3237
3238 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3239 // can't do its work correctly.
3240 c = vpeekc_any();
3241 if (c != NUL)
3242 {
3243 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3244 {
3245 c = safe_vgetc(); // Eat the character
3246 compl_shows_dir = ins_compl_key2dir(c);
3247 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3248 c != K_UP && c != K_DOWN, in_compl_func);
3249 }
3250 else
3251 {
3252 // Need to get the character to have KeyTyped set. We'll put it
3253 // back with vungetc() below. But skip K_IGNORE.
3254 c = safe_vgetc();
3255 if (c != K_IGNORE)
3256 {
3257 // Don't interrupt completion when the character wasn't typed,
3258 // e.g., when doing @q to replay keys.
3259 if (c != Ctrl_R && KeyTyped)
3260 compl_interrupted = TRUE;
3261
3262 vungetc(c);
3263 }
3264 }
3265 }
3266 if (compl_pending != 0 && !got_int && !compl_no_insert)
3267 {
3268 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3269
3270 compl_pending = 0;
3271 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3272 }
3273}
3274
3275/*
3276 * Decide the direction of Insert mode complete from the key typed.
3277 * Returns BACKWARD or FORWARD.
3278 */
3279 static int
3280ins_compl_key2dir(int c)
3281{
3282 if (c == Ctrl_P || c == Ctrl_L
3283 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3284 return BACKWARD;
3285 return FORWARD;
3286}
3287
3288/*
3289 * Return TRUE for keys that are used for completion only when the popup menu
3290 * is visible.
3291 */
3292 static int
3293ins_compl_pum_key(int c)
3294{
3295 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3296 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3297 || c == K_UP || c == K_DOWN);
3298}
3299
3300/*
3301 * Decide the number of completions to move forward.
3302 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3303 */
3304 static int
3305ins_compl_key2count(int c)
3306{
3307 int h;
3308
3309 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3310 {
3311 h = pum_get_height();
3312 if (h > 3)
3313 h -= 2; // keep some context
3314 return h;
3315 }
3316 return 1;
3317}
3318
3319/*
3320 * Return TRUE if completion with "c" should insert the match, FALSE if only
3321 * to change the currently selected completion.
3322 */
3323 static int
3324ins_compl_use_match(int c)
3325{
3326 switch (c)
3327 {
3328 case K_UP:
3329 case K_DOWN:
3330 case K_PAGEDOWN:
3331 case K_KPAGEDOWN:
3332 case K_S_DOWN:
3333 case K_PAGEUP:
3334 case K_KPAGEUP:
3335 case K_S_UP:
3336 return FALSE;
3337 }
3338 return TRUE;
3339}
3340
3341/*
3342 * Do Insert mode completion.
3343 * Called when character "c" was typed, which has a meaning for completion.
3344 * Returns OK if completion was done, FAIL if something failed (out of mem).
3345 */
3346 int
3347ins_complete(int c, int enable_pum)
3348{
3349 char_u *line;
3350 int startcol = 0; // column where searched text starts
3351 colnr_T curs_col; // cursor column
3352 int n;
3353 int save_w_wrow;
3354 int save_w_leftcol;
3355 int insert_match;
3356 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003357 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003358
3359 compl_direction = ins_compl_key2dir(c);
3360 insert_match = ins_compl_use_match(c);
3361
3362 if (!compl_started)
3363 {
3364 // First time we hit ^N or ^P (in a row, I mean)
3365
3366 did_ai = FALSE;
3367#ifdef FEAT_SMARTINDENT
3368 did_si = FALSE;
3369 can_si = FALSE;
3370 can_si_back = FALSE;
3371#endif
3372 if (stop_arrow() == FAIL)
3373 return FAIL;
3374
3375 line = ml_get(curwin->w_cursor.lnum);
3376 curs_col = curwin->w_cursor.col;
3377 compl_pending = 0;
3378
3379 // If this same ctrl_x_mode has been interrupted use the text from
3380 // "compl_startpos" to the cursor as a pattern to add a new word
3381 // instead of expand the one before the cursor, in word-wise if
3382 // "compl_startpos" is not in the same line as the cursor then fix it
3383 // (the line has been split because it was longer than 'tw'). if SOL
3384 // is set then skip the previous pattern, a word at the beginning of
3385 // the line has been inserted, we'll look for that -- Acevedo.
3386 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3387 && compl_cont_mode == ctrl_x_mode)
3388 {
3389 // it is a continued search
3390 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3391 if (ctrl_x_mode == CTRL_X_NORMAL
3392 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3393 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3394 {
3395 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3396 {
3397 // line (probably) wrapped, set compl_startpos to the
3398 // first non_blank in the line, if it is not a wordchar
3399 // include it to get a better pattern, but then we don't
3400 // want the "\\<" prefix, check it bellow
3401 compl_col = (colnr_T)getwhitecols(line);
3402 compl_startpos.col = compl_col;
3403 compl_startpos.lnum = curwin->w_cursor.lnum;
3404 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3405 }
3406 else
3407 {
3408 // S_IPOS was set when we inserted a word that was at the
3409 // beginning of the line, which means that we'll go to SOL
3410 // mode but first we need to redefine compl_startpos
3411 if (compl_cont_status & CONT_S_IPOS)
3412 {
3413 compl_cont_status |= CONT_SOL;
3414 compl_startpos.col = (colnr_T)(skipwhite(
3415 line + compl_length
3416 + compl_startpos.col) - line);
3417 }
3418 compl_col = compl_startpos.col;
3419 }
3420 compl_length = curwin->w_cursor.col - (int)compl_col;
3421 // IObuff is used to add a "word from the next line" would we
3422 // have enough space? just being paranoid
3423#define MIN_SPACE 75
3424 if (compl_length > (IOSIZE - MIN_SPACE))
3425 {
3426 compl_cont_status &= ~CONT_SOL;
3427 compl_length = (IOSIZE - MIN_SPACE);
3428 compl_col = curwin->w_cursor.col - compl_length;
3429 }
3430 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3431 if (compl_length < 1)
3432 compl_cont_status &= CONT_LOCAL;
3433 }
3434 else if (ctrl_x_mode_line_or_eval())
3435 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3436 else
3437 compl_cont_status = 0;
3438 }
3439 else
3440 compl_cont_status &= CONT_LOCAL;
3441
3442 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3443 {
3444 compl_cont_mode = ctrl_x_mode;
3445 if (ctrl_x_mode != CTRL_X_NORMAL)
3446 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3447 compl_cont_status = 0;
3448 compl_cont_status |= CONT_N_ADDS;
3449 compl_startpos = curwin->w_cursor;
3450 startcol = (int)curs_col;
3451 compl_col = 0;
3452 }
3453
3454 // Work out completion pattern and original text -- webb
3455 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3456 {
3457 if ((compl_cont_status & CONT_SOL)
3458 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3459 {
3460 if (!(compl_cont_status & CONT_ADDING))
3461 {
3462 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3463 ;
3464 compl_col += ++startcol;
3465 compl_length = curs_col - startcol;
3466 }
3467 if (p_ic)
3468 compl_pattern = str_foldcase(line + compl_col,
3469 compl_length, NULL, 0);
3470 else
3471 compl_pattern = vim_strnsave(line + compl_col,
3472 compl_length);
3473 if (compl_pattern == NULL)
3474 return FAIL;
3475 }
3476 else if (compl_cont_status & CONT_ADDING)
3477 {
3478 char_u *prefix = (char_u *)"\\<";
3479
3480 // we need up to 2 extra chars for the prefix
3481 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3482 compl_length) + 2);
3483 if (compl_pattern == NULL)
3484 return FAIL;
3485 if (!vim_iswordp(line + compl_col)
3486 || (compl_col > 0
3487 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3488 prefix = (char_u *)"";
3489 STRCPY((char *)compl_pattern, prefix);
3490 (void)quote_meta(compl_pattern + STRLEN(prefix),
3491 line + compl_col, compl_length);
3492 }
3493 else if (--startcol < 0
3494 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3495 {
3496 // Match any word of at least two chars
3497 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3498 if (compl_pattern == NULL)
3499 return FAIL;
3500 compl_col += curs_col;
3501 compl_length = 0;
3502 }
3503 else
3504 {
3505 // Search the point of change class of multibyte character
3506 // or not a word single byte character backward.
3507 if (has_mbyte)
3508 {
3509 int base_class;
3510 int head_off;
3511
3512 startcol -= (*mb_head_off)(line, line + startcol);
3513 base_class = mb_get_class(line + startcol);
3514 while (--startcol >= 0)
3515 {
3516 head_off = (*mb_head_off)(line, line + startcol);
3517 if (base_class != mb_get_class(line + startcol
3518 - head_off))
3519 break;
3520 startcol -= head_off;
3521 }
3522 }
3523 else
3524 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3525 ;
3526 compl_col += ++startcol;
3527 compl_length = (int)curs_col - startcol;
3528 if (compl_length == 1)
3529 {
3530 // Only match word with at least two chars -- webb
3531 // there's no need to call quote_meta,
3532 // alloc(7) is enough -- Acevedo
3533 compl_pattern = alloc(7);
3534 if (compl_pattern == NULL)
3535 return FAIL;
3536 STRCPY((char *)compl_pattern, "\\<");
3537 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3538 STRCAT((char *)compl_pattern, "\\k");
3539 }
3540 else
3541 {
3542 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3543 compl_length) + 2);
3544 if (compl_pattern == NULL)
3545 return FAIL;
3546 STRCPY((char *)compl_pattern, "\\<");
3547 (void)quote_meta(compl_pattern + 2, line + compl_col,
3548 compl_length);
3549 }
3550 }
3551 }
3552 else if (ctrl_x_mode_line_or_eval())
3553 {
3554 compl_col = (colnr_T)getwhitecols(line);
3555 compl_length = (int)curs_col - (int)compl_col;
3556 if (compl_length < 0) // cursor in indent: empty pattern
3557 compl_length = 0;
3558 if (p_ic)
3559 compl_pattern = str_foldcase(line + compl_col, compl_length,
3560 NULL, 0);
3561 else
3562 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3563 if (compl_pattern == NULL)
3564 return FAIL;
3565 }
3566 else if (ctrl_x_mode == CTRL_X_FILES)
3567 {
3568 // Go back to just before the first filename character.
3569 if (startcol > 0)
3570 {
3571 char_u *p = line + startcol;
3572
3573 MB_PTR_BACK(line, p);
3574 while (p > line && vim_isfilec(PTR2CHAR(p)))
3575 MB_PTR_BACK(line, p);
3576 if (p == line && vim_isfilec(PTR2CHAR(p)))
3577 startcol = 0;
3578 else
3579 startcol = (int)(p - line) + 1;
3580 }
3581
3582 compl_col += startcol;
3583 compl_length = (int)curs_col - startcol;
3584 compl_pattern = addstar(line + compl_col, compl_length,
3585 EXPAND_FILES);
3586 if (compl_pattern == NULL)
3587 return FAIL;
3588 }
3589 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3590 {
3591 compl_pattern = vim_strnsave(line, curs_col);
3592 if (compl_pattern == NULL)
3593 return FAIL;
3594 set_cmd_context(&compl_xp, compl_pattern,
3595 (int)STRLEN(compl_pattern), curs_col, FALSE);
3596 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3597 || compl_xp.xp_context == EXPAND_NOTHING)
3598 // No completion possible, use an empty pattern to get a
3599 // "pattern not found" message.
3600 compl_col = curs_col;
3601 else
3602 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3603 compl_length = curs_col - compl_col;
3604 }
3605 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3606 {
3607#ifdef FEAT_COMPL_FUNC
3608 // Call user defined function 'completefunc' with "a:findstart"
3609 // set to 1 to obtain the length of text to use for completion.
3610 typval_T args[3];
3611 int col;
3612 char_u *funcname;
3613 pos_T pos;
3614 win_T *curwin_save;
3615 buf_T *curbuf_save;
3616 int save_State = State;
3617
3618 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3619 // string
3620 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3621 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3622 if (*funcname == NUL)
3623 {
3624 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3625 ? "completefunc" : "omnifunc");
3626 // restore did_ai, so that adding comment leader works
3627 did_ai = save_did_ai;
3628 return FAIL;
3629 }
3630
3631 args[0].v_type = VAR_NUMBER;
3632 args[0].vval.v_number = 1;
3633 args[1].v_type = VAR_STRING;
3634 args[1].vval.v_string = (char_u *)"";
3635 args[2].v_type = VAR_UNKNOWN;
3636 pos = curwin->w_cursor;
3637 curwin_save = curwin;
3638 curbuf_save = curbuf;
3639 col = call_func_retnr(funcname, 2, args);
3640
3641 State = save_State;
3642 if (curwin_save != curwin || curbuf_save != curbuf)
3643 {
3644 emsg(_(e_complwin));
3645 return FAIL;
3646 }
3647 curwin->w_cursor = pos; // restore the cursor position
3648 validate_cursor();
3649 if (!EQUAL_POS(curwin->w_cursor, pos))
3650 {
3651 emsg(_(e_compldel));
3652 return FAIL;
3653 }
3654
3655 // Return value -2 means the user complete function wants to
3656 // cancel the complete without an error.
3657 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3658 if (col == -2)
3659 return FAIL;
3660 if (col == -3)
3661 {
3662 ctrl_x_mode = CTRL_X_NORMAL;
3663 edit_submode = NULL;
3664 if (!shortmess(SHM_COMPLETIONMENU))
3665 msg_clr_cmdline();
3666 return FAIL;
3667 }
3668
3669 // Reset extended parameters of completion, when start new
3670 // completion.
3671 compl_opt_refresh_always = FALSE;
3672 compl_opt_suppress_empty = FALSE;
3673
3674 if (col < 0)
3675 col = curs_col;
3676 compl_col = col;
3677 if (compl_col > curs_col)
3678 compl_col = curs_col;
3679
3680 // Setup variables for completion. Need to obtain "line" again,
3681 // it may have become invalid.
3682 line = ml_get(curwin->w_cursor.lnum);
3683 compl_length = curs_col - compl_col;
3684 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3685 if (compl_pattern == NULL)
3686#endif
3687 return FAIL;
3688 }
3689 else if (ctrl_x_mode == CTRL_X_SPELL)
3690 {
3691#ifdef FEAT_SPELL
3692 if (spell_bad_len > 0)
3693 compl_col = curs_col - spell_bad_len;
3694 else
3695 compl_col = spell_word_start(startcol);
3696 if (compl_col >= (colnr_T)startcol)
3697 {
3698 compl_length = 0;
3699 compl_col = curs_col;
3700 }
3701 else
3702 {
3703 spell_expand_check_cap(compl_col);
3704 compl_length = (int)curs_col - compl_col;
3705 }
3706 // Need to obtain "line" again, it may have become invalid.
3707 line = ml_get(curwin->w_cursor.lnum);
3708 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3709 if (compl_pattern == NULL)
3710#endif
3711 return FAIL;
3712 }
3713 else
3714 {
3715 internal_error("ins_complete()");
3716 return FAIL;
3717 }
3718
3719 if (compl_cont_status & CONT_ADDING)
3720 {
3721 edit_submode_pre = (char_u *)_(" Adding");
3722 if (ctrl_x_mode_line_or_eval())
3723 {
3724 // Insert a new line, keep indentation but ignore 'comments'
3725#ifdef FEAT_COMMENTS
3726 char_u *old = curbuf->b_p_com;
3727
3728 curbuf->b_p_com = (char_u *)"";
3729#endif
3730 compl_startpos.lnum = curwin->w_cursor.lnum;
3731 compl_startpos.col = compl_col;
3732 ins_eol('\r');
3733#ifdef FEAT_COMMENTS
3734 curbuf->b_p_com = old;
3735#endif
3736 compl_length = 0;
3737 compl_col = curwin->w_cursor.col;
3738 }
3739 }
3740 else
3741 {
3742 edit_submode_pre = NULL;
3743 compl_startpos.col = compl_col;
3744 }
3745
3746 if (compl_cont_status & CONT_LOCAL)
3747 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
3748 else
3749 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3750
3751 // If any of the original typed text has been changed we need to fix
3752 // the redo buffer.
3753 ins_compl_fixRedoBufForLeader(NULL);
3754
3755 // Always add completion for the original text.
3756 vim_free(compl_orig_text);
3757 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003758 if (p_ic)
3759 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003760 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003761 -1, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003762 {
3763 VIM_CLEAR(compl_pattern);
3764 VIM_CLEAR(compl_orig_text);
3765 return FAIL;
3766 }
3767
3768 // showmode might reset the internal line pointers, so it must
3769 // be called before line = ml_get(), or when this address is no
3770 // longer needed. -- Acevedo.
3771 edit_submode_extra = (char_u *)_("-- Searching...");
3772 edit_submode_highl = HLF_COUNT;
3773 showmode();
3774 edit_submode_extra = NULL;
3775 out_flush();
3776 }
3777 else if (insert_match && stop_arrow() == FAIL)
3778 return FAIL;
3779
3780 compl_shown_match = compl_curr_match;
3781 compl_shows_dir = compl_direction;
3782
3783 // Find next match (and following matches).
3784 save_w_wrow = curwin->w_wrow;
3785 save_w_leftcol = curwin->w_leftcol;
3786 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
3787
3788 // may undisplay the popup menu
3789 ins_compl_upd_pum();
3790
3791 if (n > 1) // all matches have been found
3792 compl_matches = n;
3793 compl_curr_match = compl_shown_match;
3794 compl_direction = compl_shows_dir;
3795
3796 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
3797 // mode.
3798 if (got_int && !global_busy)
3799 {
3800 (void)vgetc();
3801 got_int = FALSE;
3802 }
3803
3804 // we found no match if the list has only the "compl_orig_text"-entry
3805 if (compl_first_match == compl_first_match->cp_next)
3806 {
3807 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3808 && compl_length > 1
3809 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3810 edit_submode_highl = HLF_E;
3811 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3812 // because we couldn't expand anything at first place, but if we used
3813 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3814 // (such as M in M'exico) if not tried already. -- Acevedo
3815 if ( compl_length > 1
3816 || (compl_cont_status & CONT_ADDING)
3817 || (ctrl_x_mode != CTRL_X_NORMAL
3818 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3819 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
3820 compl_cont_status &= ~CONT_N_ADDS;
3821 }
3822
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003823 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003824 compl_cont_status |= CONT_S_IPOS;
3825 else
3826 compl_cont_status &= ~CONT_S_IPOS;
3827
3828 if (edit_submode_extra == NULL)
3829 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003830 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003831 {
3832 edit_submode_extra = (char_u *)_("Back at original");
3833 edit_submode_highl = HLF_W;
3834 }
3835 else if (compl_cont_status & CONT_S_IPOS)
3836 {
3837 edit_submode_extra = (char_u *)_("Word from other line");
3838 edit_submode_highl = HLF_COUNT;
3839 }
3840 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
3841 {
3842 edit_submode_extra = (char_u *)_("The only match");
3843 edit_submode_highl = HLF_COUNT;
3844 }
3845 else
3846 {
3847 // Update completion sequence number when needed.
3848 if (compl_curr_match->cp_number == -1)
3849 {
3850 int number = 0;
3851 compl_T *match;
3852
3853 if (compl_direction == FORWARD)
3854 {
3855 // search backwards for the first valid (!= -1) number.
3856 // This should normally succeed already at the first loop
3857 // cycle, so it's fast!
3858 for (match = compl_curr_match->cp_prev; match != NULL
3859 && match != compl_first_match;
3860 match = match->cp_prev)
3861 if (match->cp_number != -1)
3862 {
3863 number = match->cp_number;
3864 break;
3865 }
3866 if (match != NULL)
3867 // go up and assign all numbers which are not assigned
3868 // yet
3869 for (match = match->cp_next;
3870 match != NULL && match->cp_number == -1;
3871 match = match->cp_next)
3872 match->cp_number = ++number;
3873 }
3874 else // BACKWARD
3875 {
3876 // search forwards (upwards) for the first valid (!= -1)
3877 // number. This should normally succeed already at the
3878 // first loop cycle, so it's fast!
3879 for (match = compl_curr_match->cp_next; match != NULL
3880 && match != compl_first_match;
3881 match = match->cp_next)
3882 if (match->cp_number != -1)
3883 {
3884 number = match->cp_number;
3885 break;
3886 }
3887 if (match != NULL)
3888 // go down and assign all numbers which are not
3889 // assigned yet
3890 for (match = match->cp_prev; match
3891 && match->cp_number == -1;
3892 match = match->cp_prev)
3893 match->cp_number = ++number;
3894 }
3895 }
3896
3897 // The match should always have a sequence number now, this is
3898 // just a safety check.
3899 if (compl_curr_match->cp_number != -1)
3900 {
3901 // Space for 10 text chars. + 2x10-digit no.s = 31.
3902 // Translations may need more than twice that.
3903 static char_u match_ref[81];
3904
3905 if (compl_matches > 0)
3906 vim_snprintf((char *)match_ref, sizeof(match_ref),
3907 _("match %d of %d"),
3908 compl_curr_match->cp_number, compl_matches);
3909 else
3910 vim_snprintf((char *)match_ref, sizeof(match_ref),
3911 _("match %d"),
3912 compl_curr_match->cp_number);
3913 edit_submode_extra = match_ref;
3914 edit_submode_highl = HLF_R;
3915 if (dollar_vcol >= 0)
3916 curs_columns(FALSE);
3917 }
3918 }
3919 }
3920
3921 // Show a message about what (completion) mode we're in.
3922 if (!compl_opt_suppress_empty)
3923 {
3924 showmode();
3925 if (!shortmess(SHM_COMPLETIONMENU))
3926 {
3927 if (edit_submode_extra != NULL)
3928 {
3929 if (!p_smd)
3930 msg_attr((char *)edit_submode_extra,
3931 edit_submode_highl < HLF_COUNT
3932 ? HL_ATTR(edit_submode_highl) : 0);
3933 }
3934 else
3935 msg_clr_cmdline(); // necessary for "noshowmode"
3936 }
3937 }
3938
3939 // Show the popup menu, unless we got interrupted.
3940 if (enable_pum && !compl_interrupted)
3941 show_pum(save_w_wrow, save_w_leftcol);
3942
3943 compl_was_interrupted = compl_interrupted;
3944 compl_interrupted = FALSE;
3945
3946 return OK;
3947}
3948
3949 static void
3950show_pum(int prev_w_wrow, int prev_w_leftcol)
3951{
3952 // RedrawingDisabled may be set when invoked through complete().
3953 int n = RedrawingDisabled;
3954
3955 RedrawingDisabled = 0;
3956
3957 // If the cursor moved or the display scrolled we need to remove the pum
3958 // first.
3959 setcursor();
3960 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
3961 ins_compl_del_pum();
3962
3963 ins_compl_show_pum();
3964 setcursor();
3965 RedrawingDisabled = n;
3966}
3967
3968/*
3969 * Looks in the first "len" chars. of "src" for search-metachars.
3970 * If dest is not NULL the chars. are copied there quoting (with
3971 * a backslash) the metachars, and dest would be NUL terminated.
3972 * Returns the length (needed) of dest
3973 */
3974 static unsigned
3975quote_meta(char_u *dest, char_u *src, int len)
3976{
3977 unsigned m = (unsigned)len + 1; // one extra for the NUL
3978
3979 for ( ; --len >= 0; src++)
3980 {
3981 switch (*src)
3982 {
3983 case '.':
3984 case '*':
3985 case '[':
3986 if (ctrl_x_mode == CTRL_X_DICTIONARY
3987 || ctrl_x_mode == CTRL_X_THESAURUS)
3988 break;
3989 // FALLTHROUGH
3990 case '~':
3991 if (!p_magic) // quote these only if magic is set
3992 break;
3993 // FALLTHROUGH
3994 case '\\':
3995 if (ctrl_x_mode == CTRL_X_DICTIONARY
3996 || ctrl_x_mode == CTRL_X_THESAURUS)
3997 break;
3998 // FALLTHROUGH
3999 case '^': // currently it's not needed.
4000 case '$':
4001 m++;
4002 if (dest != NULL)
4003 *dest++ = '\\';
4004 break;
4005 }
4006 if (dest != NULL)
4007 *dest++ = *src;
4008 // Copy remaining bytes of a multibyte character.
4009 if (has_mbyte)
4010 {
4011 int i, mb_len;
4012
4013 mb_len = (*mb_ptr2len)(src) - 1;
4014 if (mb_len > 0 && len >= mb_len)
4015 for (i = 0; i < mb_len; ++i)
4016 {
4017 --len;
4018 ++src;
4019 if (dest != NULL)
4020 *dest++ = *src;
4021 }
4022 }
4023 }
4024 if (dest != NULL)
4025 *dest = NUL;
4026
4027 return m;
4028}
4029
4030# if defined(EXITFREE) || defined(PROTO)
4031 void
4032free_insexpand_stuff(void)
4033{
4034 VIM_CLEAR(compl_orig_text);
4035}
4036# endif
4037
4038# ifdef FEAT_SPELL
4039/*
4040 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4041 * spelled word, if there is one.
4042 */
4043 static void
4044spell_back_to_badword(void)
4045{
4046 pos_T tpos = curwin->w_cursor;
4047
4048 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4049 if (curwin->w_cursor.col != tpos.col)
4050 start_arrow(&tpos);
4051}
4052# endif
4053
4054#endif // FEAT_INS_EXPAND