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