blob: 1eebfd281b011f024163cdfdf058cd363dbca4aa [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
Bram Moolenaar7591bb32019-03-30 13:53:47 +010040
41# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
42
43// Message for CTRL-X mode, index is ctrl_x_mode.
44static char *ctrl_x_msgs[] =
45{
46 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
47 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
48 NULL, // CTRL_X_SCROLL: depends on state
49 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
54 NULL, // CTRL_X_FINISHED
55 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
57 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
59 N_(" Omni completion (^O^N^P)"),
60 N_(" Spelling suggestion (s^N^P)"),
61 N_(" Keyword Local completion (^N^P)"),
62 NULL, // CTRL_X_EVAL doesn't use msg.
63};
64
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020065#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010066static 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};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020085#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010086
87/*
88 * Array indexes used for cp_text[].
89 */
90#define CPT_ABBR 0 // "abbr"
91#define CPT_MENU 1 // "menu"
92#define CPT_KIND 2 // "kind"
93#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010094#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010095
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
Bram Moolenaarab782c52020-01-04 19:00:11 +0100106#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100107 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100108#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200109 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200110 // cp_flags has CP_FREE_FNAME
111 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100113};
114
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200115// values for cp_flags
116# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
117# define CP_FREE_FNAME 2 // cp_fname is allocated
118# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
119# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
120# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200121# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100122
123static char e_hitend[] = N_("Hit end of paragraph");
124# ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125static char e_compldel[] = N_("E840: Completion function deleted text");
126# endif
127
128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
134 */
135static compl_T *compl_first_match = NULL;
136static compl_T *compl_curr_match = NULL;
137static compl_T *compl_shown_match = NULL;
138static compl_T *compl_old_match = NULL;
139
140// After using a cursor key <Enter> selects a match in the popup menu,
141// otherwise it inserts a line break.
142static int compl_enter_selects = FALSE;
143
144// When "compl_leader" is not NULL only matches that start with this string
145// are used.
146static char_u *compl_leader = NULL;
147
148static int compl_get_longest = FALSE; // put longest common string
149 // in compl_leader
150
151static int compl_no_insert = FALSE; // FALSE: select & insert
152 // TRUE: noinsert
153static int compl_no_select = FALSE; // FALSE: select & insert
154 // TRUE: noselect
155
156// Selected one of the matches. When FALSE the match was edited or using the
157// longest common string.
158static int compl_used_match;
159
160// didn't finish finding completions.
161static int compl_was_interrupted = FALSE;
162
163// Set when character typed while looking for matches and it means we should
164// stop looking for matches.
165static int compl_interrupted = FALSE;
166
167static int compl_restarting = FALSE; // don't insert match
168
169// When the first completion is done "compl_started" is set. When it's
170// FALSE the word to be completed must be located.
171static int compl_started = FALSE;
172
173// Which Ctrl-X mode are we in?
174static int ctrl_x_mode = CTRL_X_NORMAL;
175
176static int compl_matches = 0;
177static char_u *compl_pattern = NULL;
178static int compl_direction = FORWARD;
179static int compl_shows_dir = FORWARD;
180static int compl_pending = 0; // > 1 for postponed CTRL-N
181static pos_T compl_startpos;
182static colnr_T compl_col = 0; // column where the text starts
183 // that is being completed
184static char_u *compl_orig_text = NULL; // text as it was before
185 // completion started
186static int compl_cont_mode = 0;
187static expand_T compl_xp;
188
189static int compl_opt_refresh_always = FALSE;
190static int compl_opt_suppress_empty = FALSE;
191
Bram Moolenaar08928322020-01-04 14:32:48 +0100192static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100193static void ins_compl_longest_match(compl_T *match);
194static void ins_compl_del_pum(void);
195static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
196static char_u *find_line_end(char_u *ptr);
197static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100198static int ins_compl_need_restart(void);
199static void ins_compl_new_leader(void);
200static int ins_compl_len(void);
201static void ins_compl_restart(void);
202static void ins_compl_set_original_text(char_u *str);
203static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
204# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
205static void ins_compl_add_list(list_T *list);
206static void ins_compl_add_dict(dict_T *dict);
207# endif
208static int ins_compl_key2dir(int c);
209static int ins_compl_pum_key(int c);
210static int ins_compl_key2count(int c);
211static void show_pum(int prev_w_wrow, int prev_w_leftcol);
212static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100213
214#ifdef FEAT_SPELL
215static void spell_back_to_badword(void);
216static int spell_bad_len = 0; // length of located bad word
217#endif
218
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100219/*
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
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200290#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100291 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200292#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100293 )
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));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100301 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100302 {
303 vim_beep(BO_COMPL);
304 setcursor();
305 out_flush();
306#ifdef FEAT_EVAL
307 if (!get_vim_var_nr(VV_TESTING))
308#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100309 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100310 }
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.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200475 wca = ALLOC_MULT(int, actual_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100476 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 Moolenaar08928322020-01-04 14:32:48 +0100565 return ins_compl_add(str, len, fname, NULL, 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,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 char_u **cptext, // extra text for popup menu or NULL
580 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100581 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200582 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100583 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100584{
585 compl_T *match;
586 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200587 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100588
Bram Moolenaarceb06192021-04-04 15:05:22 +0200589 if (flags & CP_FAST)
590 fast_breakcheck();
591 else
592 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100593 if (got_int)
594 return FAIL;
595 if (len < 0)
596 len = (int)STRLEN(str);
597
598 // If the same match is already present, don't add it.
599 if (compl_first_match != NULL && !adup)
600 {
601 match = compl_first_match;
602 do
603 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200604 if ( !(match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100605 && STRNCMP(match->cp_str, str, len) == 0
606 && match->cp_str[len] == NUL)
607 return NOTDONE;
608 match = match->cp_next;
609 } while (match != NULL && match != compl_first_match);
610 }
611
612 // Remove any popup menu before changing the list of matches.
613 ins_compl_del_pum();
614
615 // Allocate a new match structure.
616 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200617 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100618 if (match == NULL)
619 return FAIL;
620 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200621 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100622 match->cp_number = 0;
623 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
624 {
625 vim_free(match);
626 return FAIL;
627 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100628
629 // match-fname is:
630 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200631 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100632 // - NULL otherwise. --Acevedo
633 if (fname != NULL
634 && compl_curr_match != NULL
635 && compl_curr_match->cp_fname != NULL
636 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
637 match->cp_fname = compl_curr_match->cp_fname;
638 else if (fname != NULL)
639 {
640 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200641 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100642 }
643 else
644 match->cp_fname = NULL;
645 match->cp_flags = flags;
646
647 if (cptext != NULL)
648 {
649 int i;
650
651 for (i = 0; i < CPT_COUNT; ++i)
652 if (cptext[i] != NULL && *cptext[i] != NUL)
653 match->cp_text[i] = vim_strsave(cptext[i]);
654 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100655#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100656 if (user_data != NULL)
657 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100658#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100659
660 // Link the new match structure in the list of matches.
661 if (compl_first_match == NULL)
662 match->cp_next = match->cp_prev = NULL;
663 else if (dir == FORWARD)
664 {
665 match->cp_next = compl_curr_match->cp_next;
666 match->cp_prev = compl_curr_match;
667 }
668 else // BACKWARD
669 {
670 match->cp_next = compl_curr_match;
671 match->cp_prev = compl_curr_match->cp_prev;
672 }
673 if (match->cp_next)
674 match->cp_next->cp_prev = match;
675 if (match->cp_prev)
676 match->cp_prev->cp_next = match;
677 else // if there's nothing before, it is the first match
678 compl_first_match = match;
679 compl_curr_match = match;
680
681 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200682 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100683 ins_compl_longest_match(match);
684
685 return OK;
686}
687
688/*
689 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200690 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 */
692 static int
693ins_compl_equal(compl_T *match, char_u *str, int len)
694{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200695 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200697 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
699 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
700}
701
702/*
703 * Reduce the longest common string for match "match".
704 */
705 static void
706ins_compl_longest_match(compl_T *match)
707{
708 char_u *p, *s;
709 int c1, c2;
710 int had_match;
711
712 if (compl_leader == NULL)
713 {
714 // First match, use it as a whole.
715 compl_leader = vim_strsave(match->cp_str);
716 if (compl_leader != NULL)
717 {
718 had_match = (curwin->w_cursor.col > compl_col);
719 ins_compl_delete();
720 ins_bytes(compl_leader + ins_compl_len());
721 ins_redraw(FALSE);
722
723 // When the match isn't there (to avoid matching itself) remove it
724 // again after redrawing.
725 if (!had_match)
726 ins_compl_delete();
727 compl_used_match = FALSE;
728 }
729 }
730 else
731 {
732 // Reduce the text if this match differs from compl_leader.
733 p = compl_leader;
734 s = match->cp_str;
735 while (*p != NUL)
736 {
737 if (has_mbyte)
738 {
739 c1 = mb_ptr2char(p);
740 c2 = mb_ptr2char(s);
741 }
742 else
743 {
744 c1 = *p;
745 c2 = *s;
746 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200747 if ((match->cp_flags & CP_ICASE)
748 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100749 break;
750 if (has_mbyte)
751 {
752 MB_PTR_ADV(p);
753 MB_PTR_ADV(s);
754 }
755 else
756 {
757 ++p;
758 ++s;
759 }
760 }
761
762 if (*p != NUL)
763 {
764 // Leader was shortened, need to change the inserted text.
765 *p = NUL;
766 had_match = (curwin->w_cursor.col > compl_col);
767 ins_compl_delete();
768 ins_bytes(compl_leader + ins_compl_len());
769 ins_redraw(FALSE);
770
771 // When the match isn't there (to avoid matching itself) remove it
772 // again after redrawing.
773 if (!had_match)
774 ins_compl_delete();
775 }
776
777 compl_used_match = FALSE;
778 }
779}
780
781/*
782 * Add an array of matches to the list of matches.
783 * Frees matches[].
784 */
785 static void
786ins_compl_add_matches(
787 int num_matches,
788 char_u **matches,
789 int icase)
790{
791 int i;
792 int add_r = OK;
793 int dir = compl_direction;
794
795 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100796 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200797 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100798 // if dir was BACKWARD then honor it just once
799 dir = FORWARD;
800 FreeWild(num_matches, matches);
801}
802
803/*
804 * Make the completion list cyclic.
805 * Return the number of matches (excluding the original).
806 */
807 static int
808ins_compl_make_cyclic(void)
809{
810 compl_T *match;
811 int count = 0;
812
813 if (compl_first_match != NULL)
814 {
815 // Find the end of the list.
816 match = compl_first_match;
817 // there's always an entry for the compl_orig_text, it doesn't count.
818 while (match->cp_next != NULL && match->cp_next != compl_first_match)
819 {
820 match = match->cp_next;
821 ++count;
822 }
823 match->cp_next = compl_first_match;
824 compl_first_match->cp_prev = match;
825 }
826 return count;
827}
828
829/*
830 * Return whether there currently is a shown match.
831 */
832 int
833ins_compl_has_shown_match(void)
834{
835 return compl_shown_match == NULL
836 || compl_shown_match != compl_shown_match->cp_next;
837}
838
839/*
840 * Return whether the shown match is long enough.
841 */
842 int
843ins_compl_long_shown_match(void)
844{
845 return (int)STRLEN(compl_shown_match->cp_str)
846 > curwin->w_cursor.col - compl_col;
847}
848
849/*
850 * Set variables that store noselect and noinsert behavior from the
851 * 'completeopt' value.
852 */
853 void
854completeopt_was_set(void)
855{
856 compl_no_insert = FALSE;
857 compl_no_select = FALSE;
858 if (strstr((char *)p_cot, "noselect") != NULL)
859 compl_no_select = TRUE;
860 if (strstr((char *)p_cot, "noinsert") != NULL)
861 compl_no_insert = TRUE;
862}
863
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100864
865// "compl_match_array" points the currently displayed list of entries in the
866// popup menu. It is NULL when there is no popup menu.
867static pumitem_T *compl_match_array = NULL;
868static int compl_match_arraysize;
869
870/*
871 * Update the screen and when there is any scrolling remove the popup menu.
872 */
873 static void
874ins_compl_upd_pum(void)
875{
876 int h;
877
878 if (compl_match_array != NULL)
879 {
880 h = curwin->w_cline_height;
881 // Update the screen later, before drawing the popup menu over it.
882 pum_call_update_screen();
883 if (h != curwin->w_cline_height)
884 ins_compl_del_pum();
885 }
886}
887
888/*
889 * Remove any popup menu.
890 */
891 static void
892ins_compl_del_pum(void)
893{
894 if (compl_match_array != NULL)
895 {
896 pum_undisplay();
897 VIM_CLEAR(compl_match_array);
898 }
899}
900
901/*
902 * Return TRUE if the popup menu should be displayed.
903 */
904 int
905pum_wanted(void)
906{
907 // 'completeopt' must contain "menu" or "menuone"
908 if (vim_strchr(p_cot, 'm') == NULL)
909 return FALSE;
910
911 // The display looks bad on a B&W display.
912 if (t_colors < 8
913#ifdef FEAT_GUI
914 && !gui.in_use
915#endif
916 )
917 return FALSE;
918 return TRUE;
919}
920
921/*
922 * Return TRUE if there are two or more matches to be shown in the popup menu.
923 * One if 'completopt' contains "menuone".
924 */
925 static int
926pum_enough_matches(void)
927{
928 compl_T *compl;
929 int i;
930
931 // Don't display the popup menu if there are no matches or there is only
932 // one (ignoring the original text).
933 compl = compl_first_match;
934 i = 0;
935 do
936 {
937 if (compl == NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200938 || ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100939 break;
940 compl = compl->cp_next;
941 } while (compl != compl_first_match);
942
943 if (strstr((char *)p_cot, "menuone") != NULL)
944 return (i >= 1);
945 return (i >= 2);
946}
947
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200948#ifdef FEAT_EVAL
949/*
950 * Allocate Dict for the completed item.
951 * { word, abbr, menu, kind, info }
952 */
953 static dict_T *
954ins_compl_dict_alloc(compl_T *match)
955{
956 dict_T *dict = dict_alloc_lock(VAR_FIXED);
957
958 if (dict != NULL)
959 {
960 dict_add_string(dict, "word", match->cp_str);
961 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
962 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
963 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
964 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +0100965 if (match->cp_user_data.v_type == VAR_UNKNOWN)
966 dict_add_string(dict, "user_data", (char_u *)"");
967 else
968 dict_add_tv(dict, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200969 }
970 return dict;
971}
972
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200973 static void
974trigger_complete_changed_event(int cur)
975{
976 dict_T *v_event;
977 dict_T *item;
978 static int recursive = FALSE;
979
980 if (recursive)
981 return;
982
983 v_event = get_vim_var_dict(VV_EVENT);
984 if (cur < 0)
985 item = dict_alloc();
986 else
987 item = ins_compl_dict_alloc(compl_curr_match);
988 if (item == NULL)
989 return;
990 dict_add_dict(v_event, "completed_item", item);
991 pum_set_event_info(v_event);
992 dict_set_items_ro(v_event);
993
994 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200995 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200996 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200997 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200998 recursive = FALSE;
999
1000 dict_free_contents(v_event);
1001 hash_init(&v_event->dv_hashtab);
1002}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001003#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001004
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001005/*
1006 * Show the popup menu for the list of matches.
1007 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1008 */
1009 void
1010ins_compl_show_pum(void)
1011{
1012 compl_T *compl;
1013 compl_T *shown_compl = NULL;
1014 int did_find_shown_match = FALSE;
1015 int shown_match_ok = FALSE;
1016 int i;
1017 int cur = -1;
1018 colnr_T col;
1019 int lead_len = 0;
1020
1021 if (!pum_wanted() || !pum_enough_matches())
1022 return;
1023
1024#if defined(FEAT_EVAL)
1025 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001026 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001027#endif
1028
1029 // Update the screen later, before drawing the popup menu over it.
1030 pum_call_update_screen();
1031
1032 if (compl_match_array == NULL)
1033 {
1034 // Need to build the popup menu list.
1035 compl_match_arraysize = 0;
1036 compl = compl_first_match;
1037 if (compl_leader != NULL)
1038 lead_len = (int)STRLEN(compl_leader);
1039 do
1040 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001041 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001042 && (compl_leader == NULL
1043 || ins_compl_equal(compl, compl_leader, lead_len)))
1044 ++compl_match_arraysize;
1045 compl = compl->cp_next;
1046 } while (compl != NULL && compl != compl_first_match);
1047 if (compl_match_arraysize == 0)
1048 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001049 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001050 if (compl_match_array != NULL)
1051 {
1052 // If the current match is the original text don't find the first
1053 // match after it, don't highlight anything.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001054 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001055 shown_match_ok = TRUE;
1056
1057 i = 0;
1058 compl = compl_first_match;
1059 do
1060 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001061 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001062 && (compl_leader == NULL
1063 || ins_compl_equal(compl, compl_leader, lead_len)))
1064 {
1065 if (!shown_match_ok)
1066 {
1067 if (compl == compl_shown_match || did_find_shown_match)
1068 {
1069 // This item is the shown match or this is the
1070 // first displayed item after the shown match.
1071 compl_shown_match = compl;
1072 did_find_shown_match = TRUE;
1073 shown_match_ok = TRUE;
1074 }
1075 else
1076 // Remember this displayed match for when the
1077 // shown match is just below it.
1078 shown_compl = compl;
1079 cur = i;
1080 }
1081
1082 if (compl->cp_text[CPT_ABBR] != NULL)
1083 compl_match_array[i].pum_text =
1084 compl->cp_text[CPT_ABBR];
1085 else
1086 compl_match_array[i].pum_text = compl->cp_str;
1087 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1088 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1089 if (compl->cp_text[CPT_MENU] != NULL)
1090 compl_match_array[i++].pum_extra =
1091 compl->cp_text[CPT_MENU];
1092 else
1093 compl_match_array[i++].pum_extra = compl->cp_fname;
1094 }
1095
1096 if (compl == compl_shown_match)
1097 {
1098 did_find_shown_match = TRUE;
1099
1100 // When the original text is the shown match don't set
1101 // compl_shown_match.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001102 if (compl->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001103 shown_match_ok = TRUE;
1104
1105 if (!shown_match_ok && shown_compl != NULL)
1106 {
1107 // The shown match isn't displayed, set it to the
1108 // previously displayed match.
1109 compl_shown_match = shown_compl;
1110 shown_match_ok = TRUE;
1111 }
1112 }
1113 compl = compl->cp_next;
1114 } while (compl != NULL && compl != compl_first_match);
1115
1116 if (!shown_match_ok) // no displayed match at all
1117 cur = -1;
1118 }
1119 }
1120 else
1121 {
1122 // popup menu already exists, only need to find the current item.
1123 for (i = 0; i < compl_match_arraysize; ++i)
1124 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1125 || compl_match_array[i].pum_text
1126 == compl_shown_match->cp_text[CPT_ABBR])
1127 {
1128 cur = i;
1129 break;
1130 }
1131 }
1132
1133 if (compl_match_array != NULL)
1134 {
1135 // In Replace mode when a $ is displayed at the end of the line only
1136 // part of the screen would be updated. We do need to redraw here.
1137 dollar_vcol = -1;
1138
1139 // Compute the screen column of the start of the completed text.
1140 // Use the cursor to get all wrapping and other settings right.
1141 col = curwin->w_cursor.col;
1142 curwin->w_cursor.col = compl_col;
1143 pum_display(compl_match_array, compl_match_arraysize, cur);
1144 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001145
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001146#ifdef FEAT_EVAL
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001147 if (has_completechanged())
1148 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001149#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001150 }
1151}
1152
1153#define DICT_FIRST (1) // use just first element in "dict"
1154#define DICT_EXACT (2) // "dict" is the exact name of a file
1155
1156/*
1157 * Add any identifiers that match the given pattern in the list of dictionary
1158 * files "dict_start" to the list of completions.
1159 */
1160 static void
1161ins_compl_dictionaries(
1162 char_u *dict_start,
1163 char_u *pat,
1164 int flags, // DICT_FIRST and/or DICT_EXACT
1165 int thesaurus) // Thesaurus completion
1166{
1167 char_u *dict = dict_start;
1168 char_u *ptr;
1169 char_u *buf;
1170 regmatch_T regmatch;
1171 char_u **files;
1172 int count;
1173 int save_p_scs;
1174 int dir = compl_direction;
1175
1176 if (*dict == NUL)
1177 {
1178#ifdef FEAT_SPELL
1179 // When 'dictionary' is empty and spell checking is enabled use
1180 // "spell".
1181 if (!thesaurus && curwin->w_p_spell)
1182 dict = (char_u *)"spell";
1183 else
1184#endif
1185 return;
1186 }
1187
1188 buf = alloc(LSIZE);
1189 if (buf == NULL)
1190 return;
1191 regmatch.regprog = NULL; // so that we can goto theend
1192
1193 // If 'infercase' is set, don't use 'smartcase' here
1194 save_p_scs = p_scs;
1195 if (curbuf->b_p_inf)
1196 p_scs = FALSE;
1197
1198 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1199 // to only match at the start of a line. Otherwise just match the
1200 // pattern. Also need to double backslashes.
1201 if (ctrl_x_mode_line_or_eval())
1202 {
1203 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1204 size_t len;
1205
1206 if (pat_esc == NULL)
1207 goto theend;
1208 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001209 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001210 if (ptr == NULL)
1211 {
1212 vim_free(pat_esc);
1213 goto theend;
1214 }
1215 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1216 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1217 vim_free(pat_esc);
1218 vim_free(ptr);
1219 }
1220 else
1221 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001222 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (regmatch.regprog == NULL)
1224 goto theend;
1225 }
1226
1227 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1228 regmatch.rm_ic = ignorecase(pat);
1229 while (*dict != NUL && !got_int && !compl_interrupted)
1230 {
1231 // copy one dictionary file name into buf
1232 if (flags == DICT_EXACT)
1233 {
1234 count = 1;
1235 files = &dict;
1236 }
1237 else
1238 {
1239 // Expand wildcards in the dictionary name, but do not allow
1240 // backticks (for security, the 'dict' option may have been set in
1241 // a modeline).
1242 copy_option_part(&dict, buf, LSIZE, ",");
1243# ifdef FEAT_SPELL
1244 if (!thesaurus && STRCMP(buf, "spell") == 0)
1245 count = -1;
1246 else
1247# endif
1248 if (vim_strchr(buf, '`') != NULL
1249 || expand_wildcards(1, &buf, &count, &files,
1250 EW_FILE|EW_SILENT) != OK)
1251 count = 0;
1252 }
1253
1254# ifdef FEAT_SPELL
1255 if (count == -1)
1256 {
1257 // Complete from active spelling. Skip "\<" in the pattern, we
1258 // don't use it as a RE.
1259 if (pat[0] == '\\' && pat[1] == '<')
1260 ptr = pat + 2;
1261 else
1262 ptr = pat;
1263 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1264 }
1265 else
1266# endif
1267 if (count > 0) // avoid warning for using "files" uninit
1268 {
1269 ins_compl_files(count, files, thesaurus, flags,
1270 &regmatch, buf, &dir);
1271 if (flags != DICT_EXACT)
1272 FreeWild(count, files);
1273 }
1274 if (flags != 0)
1275 break;
1276 }
1277
1278theend:
1279 p_scs = save_p_scs;
1280 vim_regfree(regmatch.regprog);
1281 vim_free(buf);
1282}
1283
1284 static void
1285ins_compl_files(
1286 int count,
1287 char_u **files,
1288 int thesaurus,
1289 int flags,
1290 regmatch_T *regmatch,
1291 char_u *buf,
1292 int *dir)
1293{
1294 char_u *ptr;
1295 int i;
1296 FILE *fp;
1297 int add_r;
1298
1299 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1300 {
1301 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1302 if (flags != DICT_EXACT)
1303 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001304 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001305 vim_snprintf((char *)IObuff, IOSIZE,
1306 _("Scanning dictionary: %s"), (char *)files[i]);
1307 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1308 }
1309
1310 if (fp != NULL)
1311 {
1312 // Read dictionary file line by line.
1313 // Check each line for a match.
1314 while (!got_int && !compl_interrupted
1315 && !vim_fgets(buf, LSIZE, fp))
1316 {
1317 ptr = buf;
1318 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
1319 {
1320 ptr = regmatch->startp[0];
1321 if (ctrl_x_mode_line_or_eval())
1322 ptr = find_line_end(ptr);
1323 else
1324 ptr = find_word_end(ptr);
1325 add_r = ins_compl_add_infercase(regmatch->startp[0],
1326 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001327 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001328 if (thesaurus)
1329 {
1330 char_u *wstart;
1331
1332 // Add the other matches on the line
1333 ptr = buf;
1334 while (!got_int)
1335 {
1336 // Find start of the next word. Skip white
1337 // space and punctuation.
1338 ptr = find_word_start(ptr);
1339 if (*ptr == NUL || *ptr == NL)
1340 break;
1341 wstart = ptr;
1342
1343 // Find end of the word.
1344 if (has_mbyte)
1345 // Japanese words may have characters in
1346 // different classes, only separate words
1347 // with single-byte non-word characters.
1348 while (*ptr != NUL)
1349 {
1350 int l = (*mb_ptr2len)(ptr);
1351
1352 if (l < 2 && !vim_iswordc(*ptr))
1353 break;
1354 ptr += l;
1355 }
1356 else
1357 ptr = find_word_end(ptr);
1358
1359 // Add the word. Skip the regexp match.
1360 if (wstart != regmatch->startp[0])
1361 add_r = ins_compl_add_infercase(wstart,
1362 (int)(ptr - wstart),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001363 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001364 }
1365 }
1366 if (add_r == OK)
1367 // if dir was BACKWARD then honor it just once
1368 *dir = FORWARD;
1369 else if (add_r == FAIL)
1370 break;
1371 // avoid expensive call to vim_regexec() when at end
1372 // of line
1373 if (*ptr == '\n' || got_int)
1374 break;
1375 }
1376 line_breakcheck();
1377 ins_compl_check_keys(50, FALSE);
1378 }
1379 fclose(fp);
1380 }
1381 }
1382}
1383
1384/*
1385 * Find the start of the next word.
1386 * Returns a pointer to the first char of the word. Also stops at a NUL.
1387 */
1388 char_u *
1389find_word_start(char_u *ptr)
1390{
1391 if (has_mbyte)
1392 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1393 ptr += (*mb_ptr2len)(ptr);
1394 else
1395 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1396 ++ptr;
1397 return ptr;
1398}
1399
1400/*
1401 * Find the end of the word. Assumes it starts inside a word.
1402 * Returns a pointer to just after the word.
1403 */
1404 char_u *
1405find_word_end(char_u *ptr)
1406{
1407 int start_class;
1408
1409 if (has_mbyte)
1410 {
1411 start_class = mb_get_class(ptr);
1412 if (start_class > 1)
1413 while (*ptr != NUL)
1414 {
1415 ptr += (*mb_ptr2len)(ptr);
1416 if (mb_get_class(ptr) != start_class)
1417 break;
1418 }
1419 }
1420 else
1421 while (vim_iswordc(*ptr))
1422 ++ptr;
1423 return ptr;
1424}
1425
1426/*
1427 * Find the end of the line, omitting CR and NL at the end.
1428 * Returns a pointer to just after the line.
1429 */
1430 static char_u *
1431find_line_end(char_u *ptr)
1432{
1433 char_u *s;
1434
1435 s = ptr + STRLEN(ptr);
1436 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1437 --s;
1438 return s;
1439}
1440
1441/*
1442 * Free the list of completions
1443 */
1444 static void
1445ins_compl_free(void)
1446{
1447 compl_T *match;
1448 int i;
1449
1450 VIM_CLEAR(compl_pattern);
1451 VIM_CLEAR(compl_leader);
1452
1453 if (compl_first_match == NULL)
1454 return;
1455
1456 ins_compl_del_pum();
1457 pum_clear();
1458
1459 compl_curr_match = compl_first_match;
1460 do
1461 {
1462 match = compl_curr_match;
1463 compl_curr_match = compl_curr_match->cp_next;
1464 vim_free(match->cp_str);
1465 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001466 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001467 vim_free(match->cp_fname);
1468 for (i = 0; i < CPT_COUNT; ++i)
1469 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001470#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001471 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001472#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001473 vim_free(match);
1474 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
1475 compl_first_match = compl_curr_match = NULL;
1476 compl_shown_match = NULL;
1477 compl_old_match = NULL;
1478}
1479
1480 void
1481ins_compl_clear(void)
1482{
1483 compl_cont_status = 0;
1484 compl_started = FALSE;
1485 compl_matches = 0;
1486 VIM_CLEAR(compl_pattern);
1487 VIM_CLEAR(compl_leader);
1488 edit_submode_extra = NULL;
1489 VIM_CLEAR(compl_orig_text);
1490 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001491#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001492 // clear v:completed_item
1493 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001494#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001495}
1496
1497/*
1498 * Return TRUE when Insert completion is active.
1499 */
1500 int
1501ins_compl_active(void)
1502{
1503 return compl_started;
1504}
1505
1506/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001507 * Selected one of the matches. When FALSE the match was edited or using the
1508 * longest common string.
1509 */
1510 int
1511ins_compl_used_match(void)
1512{
1513 return compl_used_match;
1514}
1515
1516/*
1517 * Initialize get longest common string.
1518 */
1519 void
1520ins_compl_init_get_longest(void)
1521{
1522 compl_get_longest = FALSE;
1523}
1524
1525/*
1526 * Returns TRUE when insert completion is interrupted.
1527 */
1528 int
1529ins_compl_interrupted(void)
1530{
1531 return compl_interrupted;
1532}
1533
1534/*
1535 * Returns TRUE if the <Enter> key selects a match in the completion popup
1536 * menu.
1537 */
1538 int
1539ins_compl_enter_selects(void)
1540{
1541 return compl_enter_selects;
1542}
1543
1544/*
1545 * Return the column where the text starts that is being completed
1546 */
1547 colnr_T
1548ins_compl_col(void)
1549{
1550 return compl_col;
1551}
1552
1553/*
1554 * Delete one character before the cursor and show the subset of the matches
1555 * that match the word that is now before the cursor.
1556 * Returns the character to be used, NUL if the work is done and another char
1557 * to be got from the user.
1558 */
1559 int
1560ins_compl_bs(void)
1561{
1562 char_u *line;
1563 char_u *p;
1564
1565 line = ml_get_curline();
1566 p = line + curwin->w_cursor.col;
1567 MB_PTR_BACK(line, p);
1568
1569 // Stop completion when the whole word was deleted. For Omni completion
1570 // allow the word to be deleted, we won't match everything.
1571 // Respect the 'backspace' option.
1572 if ((int)(p - line) - (int)compl_col < 0
1573 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar440cf092021-04-03 20:13:30 +02001574 && ctrl_x_mode != CTRL_X_OMNI)
1575 || ctrl_x_mode == CTRL_X_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001576 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1577 - compl_length < 0))
1578 return K_BS;
1579
1580 // Deleted more than what was used to find matches or didn't finish
1581 // finding all matches: need to look for matches all over again.
1582 if (curwin->w_cursor.col <= compl_col + compl_length
1583 || ins_compl_need_restart())
1584 ins_compl_restart();
1585
1586 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001587 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001588 if (compl_leader != NULL)
1589 {
1590 ins_compl_new_leader();
1591 if (compl_shown_match != NULL)
1592 // Make sure current match is not a hidden item.
1593 compl_curr_match = compl_shown_match;
1594 return NUL;
1595 }
1596 return K_BS;
1597}
1598
1599/*
1600 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1601 * be called.
1602 */
1603 static int
1604ins_compl_need_restart(void)
1605{
1606 // Return TRUE if we didn't complete finding matches or when the
1607 // 'completefunc' returned "always" in the "refresh" dictionary item.
1608 return compl_was_interrupted
1609 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
1610 && compl_opt_refresh_always);
1611}
1612
1613/*
1614 * Called after changing "compl_leader".
1615 * Show the popup menu with a different set of matches.
1616 * May also search for matches again if the previous search was interrupted.
1617 */
1618 static void
1619ins_compl_new_leader(void)
1620{
1621 ins_compl_del_pum();
1622 ins_compl_delete();
1623 ins_bytes(compl_leader + ins_compl_len());
1624 compl_used_match = FALSE;
1625
1626 if (compl_started)
1627 ins_compl_set_original_text(compl_leader);
1628 else
1629 {
1630#ifdef FEAT_SPELL
1631 spell_bad_len = 0; // need to redetect bad word
1632#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001633 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001634 // the popup menu display the changed text before the cursor. Set
1635 // "compl_restarting" to avoid that the first match is inserted.
1636 pum_call_update_screen();
1637#ifdef FEAT_GUI
1638 if (gui.in_use)
1639 {
1640 // Show the cursor after the match, not after the redrawn text.
1641 setcursor();
1642 out_flush_cursor(FALSE, FALSE);
1643 }
1644#endif
1645 compl_restarting = TRUE;
1646 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1647 compl_cont_status = 0;
1648 compl_restarting = FALSE;
1649 }
1650
1651 compl_enter_selects = !compl_used_match;
1652
1653 // Show the popup menu with a different set of matches.
1654 ins_compl_show_pum();
1655
1656 // Don't let Enter select the original text when there is no popup menu.
1657 if (compl_match_array == NULL)
1658 compl_enter_selects = FALSE;
1659}
1660
1661/*
1662 * Return the length of the completion, from the completion start column to
1663 * the cursor column. Making sure it never goes below zero.
1664 */
1665 static int
1666ins_compl_len(void)
1667{
1668 int off = (int)curwin->w_cursor.col - (int)compl_col;
1669
1670 if (off < 0)
1671 return 0;
1672 return off;
1673}
1674
1675/*
1676 * Append one character to the match leader. May reduce the number of
1677 * matches.
1678 */
1679 void
1680ins_compl_addleader(int c)
1681{
1682 int cc;
1683
1684 if (stop_arrow() == FAIL)
1685 return;
1686 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1687 {
1688 char_u buf[MB_MAXBYTES + 1];
1689
1690 (*mb_char2bytes)(c, buf);
1691 buf[cc] = NUL;
1692 ins_char_bytes(buf, cc);
1693 if (compl_opt_refresh_always)
1694 AppendToRedobuff(buf);
1695 }
1696 else
1697 {
1698 ins_char(c);
1699 if (compl_opt_refresh_always)
1700 AppendCharToRedobuff(c);
1701 }
1702
1703 // If we didn't complete finding matches we must search again.
1704 if (ins_compl_need_restart())
1705 ins_compl_restart();
1706
1707 // When 'always' is set, don't reset compl_leader. While completing,
1708 // cursor doesn't point original position, changing compl_leader would
1709 // break redo.
1710 if (!compl_opt_refresh_always)
1711 {
1712 vim_free(compl_leader);
1713 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001714 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001715 if (compl_leader != NULL)
1716 ins_compl_new_leader();
1717 }
1718}
1719
1720/*
1721 * Setup for finding completions again without leaving CTRL-X mode. Used when
1722 * BS or a key was typed while still searching for matches.
1723 */
1724 static void
1725ins_compl_restart(void)
1726{
1727 ins_compl_free();
1728 compl_started = FALSE;
1729 compl_matches = 0;
1730 compl_cont_status = 0;
1731 compl_cont_mode = 0;
1732}
1733
1734/*
1735 * Set the first match, the original text.
1736 */
1737 static void
1738ins_compl_set_original_text(char_u *str)
1739{
1740 char_u *p;
1741
1742 // Replace the original text entry.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001743 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001744 // at the last item for backward completion
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001745 if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001746 {
1747 p = vim_strsave(str);
1748 if (p != NULL)
1749 {
1750 vim_free(compl_first_match->cp_str);
1751 compl_first_match->cp_str = p;
1752 }
1753 }
1754 else if (compl_first_match->cp_prev != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001755 && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001756 {
1757 p = vim_strsave(str);
1758 if (p != NULL)
1759 {
1760 vim_free(compl_first_match->cp_prev->cp_str);
1761 compl_first_match->cp_prev->cp_str = p;
1762 }
1763 }
1764}
1765
1766/*
1767 * Append one character to the match leader. May reduce the number of
1768 * matches.
1769 */
1770 void
1771ins_compl_addfrommatch(void)
1772{
1773 char_u *p;
1774 int len = (int)curwin->w_cursor.col - (int)compl_col;
1775 int c;
1776 compl_T *cp;
1777
1778 p = compl_shown_match->cp_str;
1779 if ((int)STRLEN(p) <= len) // the match is too short
1780 {
1781 // When still at the original match use the first entry that matches
1782 // the leader.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001783 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001784 {
1785 p = NULL;
1786 for (cp = compl_shown_match->cp_next; cp != NULL
1787 && cp != compl_first_match; cp = cp->cp_next)
1788 {
1789 if (compl_leader == NULL
1790 || ins_compl_equal(cp, compl_leader,
1791 (int)STRLEN(compl_leader)))
1792 {
1793 p = cp->cp_str;
1794 break;
1795 }
1796 }
1797 if (p == NULL || (int)STRLEN(p) <= len)
1798 return;
1799 }
1800 else
1801 return;
1802 }
1803 p += len;
1804 c = PTR2CHAR(p);
1805 ins_compl_addleader(c);
1806}
1807
1808/*
1809 * Prepare for Insert mode completion, or stop it.
1810 * Called just after typing a character in Insert mode.
1811 * Returns TRUE when the character is not to be inserted;
1812 */
1813 int
1814ins_compl_prep(int c)
1815{
1816 char_u *ptr;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001817#ifdef FEAT_CINDENT
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001818 int want_cindent;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001819#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01001821 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001822
1823 // Forget any previous 'special' messages if this is actually
1824 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
1825 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
1826 edit_submode_extra = NULL;
1827
1828 // Ignore end of Select mode mapping and mouse scroll buttons.
1829 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01001830 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 return retval;
1832
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001833#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001834 // Ignore mouse events in a popup window
1835 if (is_mouse_key(c))
1836 {
1837 // Ignore drag and release events, the position does not need to be in
1838 // the popup and it may have just closed.
1839 if (c == K_LEFTRELEASE
1840 || c == K_LEFTRELEASE_NM
1841 || c == K_MIDDLERELEASE
1842 || c == K_RIGHTRELEASE
1843 || c == K_X1RELEASE
1844 || c == K_X2RELEASE
1845 || c == K_LEFTDRAG
1846 || c == K_MIDDLEDRAG
1847 || c == K_RIGHTDRAG
1848 || c == K_X1DRAG
1849 || c == K_X2DRAG)
1850 return retval;
1851 if (popup_visible)
1852 {
1853 int row = mouse_row;
1854 int col = mouse_col;
1855 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
1856
1857 if (wp != NULL && WIN_IS_POPUP(wp))
1858 return retval;
1859 }
1860 }
1861#endif
1862
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001863 // Set "compl_get_longest" when finding the first matches.
1864 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
1865 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
1866 {
1867 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
1868 compl_used_match = TRUE;
1869
1870 }
1871
1872 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
1873 {
1874 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
1875 // it will be yet. Now we decide.
1876 switch (c)
1877 {
1878 case Ctrl_E:
1879 case Ctrl_Y:
1880 ctrl_x_mode = CTRL_X_SCROLL;
1881 if (!(State & REPLACE_FLAG))
1882 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1883 else
1884 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1885 edit_submode_pre = NULL;
1886 showmode();
1887 break;
1888 case Ctrl_L:
1889 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1890 break;
1891 case Ctrl_F:
1892 ctrl_x_mode = CTRL_X_FILES;
1893 break;
1894 case Ctrl_K:
1895 ctrl_x_mode = CTRL_X_DICTIONARY;
1896 break;
1897 case Ctrl_R:
1898 // Simply allow ^R to happen without affecting ^X mode
1899 break;
1900 case Ctrl_T:
1901 ctrl_x_mode = CTRL_X_THESAURUS;
1902 break;
1903#ifdef FEAT_COMPL_FUNC
1904 case Ctrl_U:
1905 ctrl_x_mode = CTRL_X_FUNCTION;
1906 break;
1907 case Ctrl_O:
1908 ctrl_x_mode = CTRL_X_OMNI;
1909 break;
1910#endif
1911 case 's':
1912 case Ctrl_S:
1913 ctrl_x_mode = CTRL_X_SPELL;
1914#ifdef FEAT_SPELL
1915 ++emsg_off; // Avoid getting the E756 error twice.
1916 spell_back_to_badword();
1917 --emsg_off;
1918#endif
1919 break;
1920 case Ctrl_RSB:
1921 ctrl_x_mode = CTRL_X_TAGS;
1922 break;
1923#ifdef FEAT_FIND_ID
1924 case Ctrl_I:
1925 case K_S_TAB:
1926 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
1927 break;
1928 case Ctrl_D:
1929 ctrl_x_mode = CTRL_X_PATH_DEFINES;
1930 break;
1931#endif
1932 case Ctrl_V:
1933 case Ctrl_Q:
1934 ctrl_x_mode = CTRL_X_CMDLINE;
1935 break;
1936 case Ctrl_P:
1937 case Ctrl_N:
1938 // ^X^P means LOCAL expansion if nothing interrupted (eg we
1939 // just started ^X mode, or there were enough ^X's to cancel
1940 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
1941 // do normal expansion when interrupting a different mode (say
1942 // ^X^F^X^P or ^P^X^X^P, see below)
1943 // nothing changes if interrupting mode 0, (eg, the flag
1944 // doesn't change when going to ADDING mode -- Acevedo
1945 if (!(compl_cont_status & CONT_INTRPT))
1946 compl_cont_status |= CONT_LOCAL;
1947 else if (compl_cont_mode != 0)
1948 compl_cont_status &= ~CONT_LOCAL;
1949 // FALLTHROUGH
1950 default:
1951 // If we have typed at least 2 ^X's... for modes != 0, we set
1952 // compl_cont_status = 0 (eg, as if we had just started ^X
1953 // mode).
1954 // For mode 0, we set "compl_cont_mode" to an impossible
1955 // value, in both cases ^X^X can be used to restart the same
1956 // mode (avoiding ADDING mode).
1957 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
1958 // 'complete' and local ^P expansions respectively.
1959 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
1960 // mode -- Acevedo
1961 if (c == Ctrl_X)
1962 {
1963 if (compl_cont_mode != 0)
1964 compl_cont_status = 0;
1965 else
1966 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
1967 }
1968 ctrl_x_mode = CTRL_X_NORMAL;
1969 edit_submode = NULL;
1970 showmode();
1971 break;
1972 }
1973 }
1974 else if (ctrl_x_mode != CTRL_X_NORMAL)
1975 {
1976 // We're already in CTRL-X mode, do we stay in it?
1977 if (!vim_is_ctrl_x_key(c))
1978 {
1979 if (ctrl_x_mode == CTRL_X_SCROLL)
1980 ctrl_x_mode = CTRL_X_NORMAL;
1981 else
1982 ctrl_x_mode = CTRL_X_FINISHED;
1983 edit_submode = NULL;
1984 }
1985 showmode();
1986 }
1987
1988 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
1989 {
1990 // Show error message from attempted keyword completion (probably
1991 // 'Pattern not found') until another key is hit, then go back to
1992 // showing what mode we are in.
1993 showmode();
1994 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
1995 && c != Ctrl_R && !ins_compl_pum_key(c))
1996 || ctrl_x_mode == CTRL_X_FINISHED)
1997 {
1998 // Get here when we have finished typing a sequence of ^N and
1999 // ^P or other completion characters in CTRL-X mode. Free up
2000 // memory that was used, and make sure we can redo the insert.
2001 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2002 {
2003 // If any of the original typed text has been changed, eg when
2004 // ignorecase is set, we must add back-spaces to the redo
2005 // buffer. We add as few as necessary to delete just the part
2006 // of the original text that has changed.
2007 // When using the longest match, edited the match or used
2008 // CTRL-E then don't use the current match.
2009 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2010 ptr = compl_curr_match->cp_str;
2011 else
2012 ptr = NULL;
2013 ins_compl_fixRedoBufForLeader(ptr);
2014 }
2015
2016#ifdef FEAT_CINDENT
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002017 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002018#endif
2019 // When completing whole lines: fix indent for 'cindent'.
2020 // Otherwise, break line if it's too long.
2021 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2022 {
2023#ifdef FEAT_CINDENT
2024 // re-indent the current line
2025 if (want_cindent)
2026 {
2027 do_c_expr_indent();
2028 want_cindent = FALSE; // don't do it again
2029 }
2030#endif
2031 }
2032 else
2033 {
2034 int prev_col = curwin->w_cursor.col;
2035
2036 // put the cursor on the last char, for 'tw' formatting
2037 if (prev_col > 0)
2038 dec_cursor();
2039 // only format when something was inserted
2040 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2041 insertchar(NUL, 0, -1);
2042 if (prev_col > 0
2043 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2044 inc_cursor();
2045 }
2046
2047 // If the popup menu is displayed pressing CTRL-Y means accepting
2048 // the selection without inserting anything. When
2049 // compl_enter_selects is set the Enter key does the same.
2050 if ((c == Ctrl_Y || (compl_enter_selects
2051 && (c == CAR || c == K_KENTER || c == NL)))
2052 && pum_visible())
2053 retval = TRUE;
2054
2055 // CTRL-E means completion is Ended, go back to the typed text.
2056 // but only do this, if the Popup is still visible
2057 if (c == Ctrl_E)
2058 {
2059 ins_compl_delete();
2060 if (compl_leader != NULL)
2061 ins_bytes(compl_leader + ins_compl_len());
2062 else if (compl_first_match != NULL)
2063 ins_bytes(compl_orig_text + ins_compl_len());
2064 retval = TRUE;
2065 }
2066
2067 auto_format(FALSE, TRUE);
2068
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002069 // Trigger the CompleteDonePre event to give scripts a chance to
2070 // act upon the completion before clearing the info, and restore
2071 // ctrl_x_mode, so that complete_info() can be used.
Bram Moolenaarda812e22020-01-26 18:35:31 +01002072 ctrl_x_mode = prev_mode;
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002073 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
Bram Moolenaar17e04782020-01-17 18:58:59 +01002074
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002075 ins_compl_free();
2076 compl_started = FALSE;
2077 compl_matches = 0;
2078 if (!shortmess(SHM_COMPLETIONMENU))
2079 msg_clr_cmdline(); // necessary for "noshowmode"
2080 ctrl_x_mode = CTRL_X_NORMAL;
2081 compl_enter_selects = FALSE;
2082 if (edit_submode != NULL)
2083 {
2084 edit_submode = NULL;
2085 showmode();
2086 }
2087
2088#ifdef FEAT_CMDWIN
2089 if (c == Ctrl_C && cmdwin_type != 0)
2090 // Avoid the popup menu remains displayed when leaving the
2091 // command line window.
2092 update_screen(0);
2093#endif
2094#ifdef FEAT_CINDENT
2095 // Indent now if a key was typed that is in 'cinkeys'.
2096 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2097 do_c_expr_indent();
2098#endif
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002099 // Trigger the CompleteDone event to give scripts a chance to act
2100 // upon the end of completion.
2101 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002102 }
2103 }
2104 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2105 // Trigger the CompleteDone event to give scripts a chance to act
2106 // upon the (possibly failed) completion.
2107 ins_apply_autocmds(EVENT_COMPLETEDONE);
2108
2109 // reset continue_* if we left expansion-mode, if we stay they'll be
2110 // (re)set properly in ins_complete()
2111 if (!vim_is_ctrl_x_key(c))
2112 {
2113 compl_cont_status = 0;
2114 compl_cont_mode = 0;
2115 }
2116
2117 return retval;
2118}
2119
2120/*
2121 * Fix the redo buffer for the completion leader replacing some of the typed
2122 * text. This inserts backspaces and appends the changed text.
2123 * "ptr" is the known leader text or NUL.
2124 */
2125 static void
2126ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2127{
2128 int len;
2129 char_u *p;
2130 char_u *ptr = ptr_arg;
2131
2132 if (ptr == NULL)
2133 {
2134 if (compl_leader != NULL)
2135 ptr = compl_leader;
2136 else
2137 return; // nothing to do
2138 }
2139 if (compl_orig_text != NULL)
2140 {
2141 p = compl_orig_text;
2142 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2143 ;
2144 if (len > 0)
2145 len -= (*mb_head_off)(p, p + len);
2146 for (p += len; *p != NUL; MB_PTR_ADV(p))
2147 AppendCharToRedobuff(K_BS);
2148 }
2149 else
2150 len = 0;
2151 if (ptr != NULL)
2152 AppendToRedobuffLit(ptr + len, -1);
2153}
2154
2155/*
2156 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2157 * (depending on flag) starting from buf and looking for a non-scanned
2158 * buffer (other than curbuf). curbuf is special, if it is called with
2159 * buf=curbuf then it has to be the first call for a given flag/expansion.
2160 *
2161 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2162 */
2163 static buf_T *
2164ins_compl_next_buf(buf_T *buf, int flag)
2165{
2166 static win_T *wp = NULL;
2167
2168 if (flag == 'w') // just windows
2169 {
2170 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2171 wp = curwin;
2172 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2173 && wp->w_buffer->b_scanned)
2174 ;
2175 buf = wp->w_buffer;
2176 }
2177 else
2178 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2179 // (unlisted buffers)
2180 // When completing whole lines skip unloaded buffers.
2181 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2182 && ((flag == 'U'
2183 ? buf->b_p_bl
2184 : (!buf->b_p_bl
2185 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2186 || buf->b_scanned))
2187 ;
2188 return buf;
2189}
2190
2191#ifdef FEAT_COMPL_FUNC
2192/*
2193 * Execute user defined complete function 'completefunc' or 'omnifunc', and
2194 * get matches in "matches".
2195 */
2196 static void
2197expand_by_function(
2198 int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
2199 char_u *base)
2200{
2201 list_T *matchlist = NULL;
2202 dict_T *matchdict = NULL;
2203 typval_T args[3];
2204 char_u *funcname;
2205 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002206 typval_T rettv;
2207 int save_State = State;
2208
2209 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2210 if (*funcname == NUL)
2211 return;
2212
2213 // Call 'completefunc' to obtain the list of matches.
2214 args[0].v_type = VAR_NUMBER;
2215 args[0].vval.v_number = 0;
2216 args[1].v_type = VAR_STRING;
2217 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2218 args[2].v_type = VAR_UNKNOWN;
2219
2220 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002221 // Lock the text to avoid weird things from happening. Also disallow
2222 // switching to another window, it should not be needed and may end up in
2223 // Insert mode in another buffer.
2224 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002225
2226 // Call a function, which returns a list or dict.
2227 if (call_vim_function(funcname, 2, args, &rettv) == OK)
2228 {
2229 switch (rettv.v_type)
2230 {
2231 case VAR_LIST:
2232 matchlist = rettv.vval.v_list;
2233 break;
2234 case VAR_DICT:
2235 matchdict = rettv.vval.v_dict;
2236 break;
2237 case VAR_SPECIAL:
2238 if (rettv.vval.v_number == VVAL_NONE)
2239 compl_opt_suppress_empty = TRUE;
2240 // FALLTHROUGH
2241 default:
2242 // TODO: Give error message?
2243 clear_tv(&rettv);
2244 break;
2245 }
2246 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002247 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002248
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002249 curwin->w_cursor = pos; // restore the cursor position
2250 validate_cursor();
2251 if (!EQUAL_POS(curwin->w_cursor, pos))
2252 {
2253 emsg(_(e_compldel));
2254 goto theend;
2255 }
2256
2257 if (matchlist != NULL)
2258 ins_compl_add_list(matchlist);
2259 else if (matchdict != NULL)
2260 ins_compl_add_dict(matchdict);
2261
2262theend:
2263 // Restore State, it might have been changed.
2264 State = save_State;
2265
2266 if (matchdict != NULL)
2267 dict_unref(matchdict);
2268 if (matchlist != NULL)
2269 list_unref(matchlist);
2270}
2271#endif // FEAT_COMPL_FUNC
2272
2273#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2274/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002275 * Add a match to the list of matches from a typeval_T.
2276 * If the given string is already in the list of completions, then return
2277 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2278 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002279 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002280 */
2281 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002282ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002283{
2284 char_u *word;
2285 int dup = FALSE;
2286 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002287 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002288 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002289 typval_T user_data;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002290
Bram Moolenaar08928322020-01-04 14:32:48 +01002291 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002292 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2293 {
2294 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2295 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2296 (char_u *)"abbr", FALSE);
2297 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2298 (char_u *)"menu", FALSE);
2299 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2300 (char_u *)"kind", FALSE);
2301 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2302 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002303 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002304 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2305 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2306 flags |= CP_ICASE;
2307 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2308 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2309 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2310 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2311 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2312 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2313 flags |= CP_EQUAL;
2314 }
2315 else
2316 {
2317 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002318 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002319 }
2320 if (word == NULL || (!empty && *word == NUL))
2321 return FAIL;
Bram Moolenaar08928322020-01-04 14:32:48 +01002322 return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002323}
2324
2325/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002326 * Add completions from a list.
2327 */
2328 static void
2329ins_compl_add_list(list_T *list)
2330{
2331 listitem_T *li;
2332 int dir = compl_direction;
2333
2334 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002335 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002336 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002337 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002338 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002339 // if dir was BACKWARD then honor it just once
2340 dir = FORWARD;
2341 else if (did_emsg)
2342 break;
2343 }
2344}
2345
2346/*
2347 * Add completions from a dict.
2348 */
2349 static void
2350ins_compl_add_dict(dict_T *dict)
2351{
2352 dictitem_T *di_refresh;
2353 dictitem_T *di_words;
2354
2355 // Check for optional "refresh" item.
2356 compl_opt_refresh_always = FALSE;
2357 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2358 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2359 {
2360 char_u *v = di_refresh->di_tv.vval.v_string;
2361
2362 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2363 compl_opt_refresh_always = TRUE;
2364 }
2365
2366 // Add completions from a "words" list.
2367 di_words = dict_find(dict, (char_u *)"words", 5);
2368 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2369 ins_compl_add_list(di_words->di_tv.vval.v_list);
2370}
2371
2372/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002373 * Start completion for the complete() function.
2374 * "startcol" is where the matched text starts (1 is first column).
2375 * "list" is the list of matches.
2376 */
2377 static void
2378set_completion(colnr_T startcol, list_T *list)
2379{
2380 int save_w_wrow = curwin->w_wrow;
2381 int save_w_leftcol = curwin->w_leftcol;
2382 int flags = CP_ORIGINAL_TEXT;
2383
2384 // If already doing completions stop it.
2385 if (ctrl_x_mode != CTRL_X_NORMAL)
2386 ins_compl_prep(' ');
2387 ins_compl_clear();
2388 ins_compl_free();
2389
2390 compl_direction = FORWARD;
2391 if (startcol > curwin->w_cursor.col)
2392 startcol = curwin->w_cursor.col;
2393 compl_col = startcol;
2394 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2395 // compl_pattern doesn't need to be set
2396 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2397 if (p_ic)
2398 flags |= CP_ICASE;
2399 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002400 -1, NULL, NULL, NULL, 0,
2401 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002402 return;
2403
2404 ctrl_x_mode = CTRL_X_EVAL;
2405
2406 ins_compl_add_list(list);
2407 compl_matches = ins_compl_make_cyclic();
2408 compl_started = TRUE;
2409 compl_used_match = TRUE;
2410 compl_cont_status = 0;
2411
2412 compl_curr_match = compl_first_match;
2413 if (compl_no_insert || compl_no_select)
2414 {
2415 ins_complete(K_DOWN, FALSE);
2416 if (compl_no_select)
2417 // Down/Up has no real effect.
2418 ins_complete(K_UP, FALSE);
2419 }
2420 else
2421 ins_complete(Ctrl_N, FALSE);
2422 compl_enter_selects = compl_no_insert;
2423
2424 // Lazily show the popup menu, unless we got interrupted.
2425 if (!compl_interrupted)
2426 show_pum(save_w_wrow, save_w_leftcol);
2427 out_flush();
2428}
2429
2430/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002431 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002432 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002433 void
2434f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002435{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002436 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002437 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002438
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002439 if (in_vim9script()
2440 && (check_for_number_arg(argvars, 0) == FAIL
2441 || check_for_list_arg(argvars, 1) == FAIL))
2442 return;
2443
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002444 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002445 {
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002446 emsg(_("E785: complete() can only be used in Insert mode"));
2447 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002448 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002449
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002450 // "textlock" is set when evaluating 'completefunc' but we can change
2451 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002452 textlock = 0;
2453
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002454 // Check for undo allowed here, because if something was already inserted
2455 // the line was already saved for undo and this check isn't done.
2456 if (!undo_allowed())
2457 return;
2458
2459 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002460 emsg(_(e_invarg));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002461 else
2462 {
2463 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2464 if (startcol > 0)
2465 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002466 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002467 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002468}
2469
2470/*
2471 * "complete_add()" function
2472 */
2473 void
2474f_complete_add(typval_T *argvars, typval_T *rettv)
2475{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002476 if (in_vim9script()
2477 && (argvars[0].v_type != VAR_DICT
2478 && argvars[0].v_type != VAR_STRING
2479 && check_for_string_arg(argvars, 0) == FAIL))
2480 return;
2481
Bram Moolenaar440cf092021-04-03 20:13:30 +02002482 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002483}
2484
2485/*
2486 * "complete_check()" function
2487 */
2488 void
2489f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2490{
2491 int saved = RedrawingDisabled;
2492
2493 RedrawingDisabled = 0;
2494 ins_compl_check_keys(0, TRUE);
2495 rettv->vval.v_number = ins_compl_interrupted();
2496 RedrawingDisabled = saved;
2497}
2498
2499/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002500 * Return Insert completion mode name string
2501 */
2502 static char_u *
2503ins_compl_mode(void)
2504{
2505 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
2506 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2507
2508 return (char_u *)"";
2509}
2510
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002511 static void
2512ins_compl_update_sequence_numbers()
2513{
2514 int number = 0;
2515 compl_T *match;
2516
2517 if (compl_direction == FORWARD)
2518 {
2519 // search backwards for the first valid (!= -1) number.
2520 // This should normally succeed already at the first loop
2521 // cycle, so it's fast!
2522 for (match = compl_curr_match->cp_prev; match != NULL
2523 && match != compl_first_match;
2524 match = match->cp_prev)
2525 if (match->cp_number != -1)
2526 {
2527 number = match->cp_number;
2528 break;
2529 }
2530 if (match != NULL)
2531 // go up and assign all numbers which are not assigned
2532 // yet
2533 for (match = match->cp_next;
2534 match != NULL && match->cp_number == -1;
2535 match = match->cp_next)
2536 match->cp_number = ++number;
2537 }
2538 else // BACKWARD
2539 {
2540 // search forwards (upwards) for the first valid (!= -1)
2541 // number. This should normally succeed already at the
2542 // first loop cycle, so it's fast!
2543 for (match = compl_curr_match->cp_next; match != NULL
2544 && match != compl_first_match;
2545 match = match->cp_next)
2546 if (match->cp_number != -1)
2547 {
2548 number = match->cp_number;
2549 break;
2550 }
2551 if (match != NULL)
2552 // go down and assign all numbers which are not
2553 // assigned yet
2554 for (match = match->cp_prev; match
2555 && match->cp_number == -1;
2556 match = match->cp_prev)
2557 match->cp_number = ++number;
2558 }
2559}
2560
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002561/*
2562 * Get complete information
2563 */
2564 static void
2565get_complete_info(list_T *what_list, dict_T *retdict)
2566{
2567 int ret = OK;
2568 listitem_T *item;
2569#define CI_WHAT_MODE 0x01
2570#define CI_WHAT_PUM_VISIBLE 0x02
2571#define CI_WHAT_ITEMS 0x04
2572#define CI_WHAT_SELECTED 0x08
2573#define CI_WHAT_INSERTED 0x10
2574#define CI_WHAT_ALL 0xff
2575 int what_flag;
2576
2577 if (what_list == NULL)
2578 what_flag = CI_WHAT_ALL;
2579 else
2580 {
2581 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002582 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002583 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002584 {
2585 char_u *what = tv_get_string(&item->li_tv);
2586
2587 if (STRCMP(what, "mode") == 0)
2588 what_flag |= CI_WHAT_MODE;
2589 else if (STRCMP(what, "pum_visible") == 0)
2590 what_flag |= CI_WHAT_PUM_VISIBLE;
2591 else if (STRCMP(what, "items") == 0)
2592 what_flag |= CI_WHAT_ITEMS;
2593 else if (STRCMP(what, "selected") == 0)
2594 what_flag |= CI_WHAT_SELECTED;
2595 else if (STRCMP(what, "inserted") == 0)
2596 what_flag |= CI_WHAT_INSERTED;
2597 }
2598 }
2599
2600 if (ret == OK && (what_flag & CI_WHAT_MODE))
2601 ret = dict_add_string(retdict, "mode", ins_compl_mode());
2602
2603 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
2604 ret = dict_add_number(retdict, "pum_visible", pum_visible());
2605
2606 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
2607 {
2608 list_T *li;
2609 dict_T *di;
2610 compl_T *match;
2611
2612 li = list_alloc();
2613 if (li == NULL)
2614 return;
2615 ret = dict_add_list(retdict, "items", li);
2616 if (ret == OK && compl_first_match != NULL)
2617 {
2618 match = compl_first_match;
2619 do
2620 {
2621 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
2622 {
2623 di = dict_alloc();
2624 if (di == NULL)
2625 return;
2626 ret = list_append_dict(li, di);
2627 if (ret != OK)
2628 return;
2629 dict_add_string(di, "word", match->cp_str);
2630 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
2631 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
2632 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
2633 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002634 if (match->cp_user_data.v_type == VAR_UNKNOWN)
2635 // Add an empty string for backwards compatibility
2636 dict_add_string(di, "user_data", (char_u *)"");
2637 else
2638 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002639 }
2640 match = match->cp_next;
2641 }
2642 while (match != NULL && match != compl_first_match);
2643 }
2644 }
2645
2646 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002647 {
2648 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
2649 ins_compl_update_sequence_numbers();
2650 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
2651 ? compl_curr_match->cp_number - 1 : -1);
2652 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002653
2654 // TODO
2655 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
2656}
2657
2658/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002659 * "complete_info()" function
2660 */
2661 void
2662f_complete_info(typval_T *argvars, typval_T *rettv)
2663{
2664 list_T *what_list = NULL;
2665
2666 if (rettv_dict_alloc(rettv) != OK)
2667 return;
2668
2669 if (argvars[0].v_type != VAR_UNKNOWN)
2670 {
2671 if (argvars[0].v_type != VAR_LIST)
2672 {
2673 emsg(_(e_listreq));
2674 return;
2675 }
2676 what_list = argvars[0].vval.v_list;
2677 }
2678 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679}
2680#endif
2681
2682/*
2683 * Get the next expansion(s), using "compl_pattern".
2684 * The search starts at position "ini" in curbuf and in the direction
2685 * compl_direction.
2686 * When "compl_started" is FALSE start at that position, otherwise continue
2687 * where we stopped searching before.
2688 * This may return before finding all the matches.
2689 * Return the total number of matches or -1 if still unknown -- Acevedo
2690 */
2691 static int
2692ins_compl_get_exp(pos_T *ini)
2693{
2694 static pos_T first_match_pos;
2695 static pos_T last_match_pos;
2696 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2697 static int found_all = FALSE; // Found all matches of a
2698 // certain type.
2699 static buf_T *ins_buf = NULL; // buffer being scanned
2700
2701 pos_T *pos;
2702 char_u **matches;
2703 int save_p_scs;
2704 int save_p_ws;
2705 int save_p_ic;
2706 int i;
2707 int num_matches;
2708 int len;
2709 int found_new_match;
2710 int type = ctrl_x_mode;
2711 char_u *ptr;
2712 char_u *dict = NULL;
2713 int dict_f = 0;
2714 int set_match_pos;
2715
2716 if (!compl_started)
2717 {
2718 FOR_ALL_BUFFERS(ins_buf)
2719 ins_buf->b_scanned = 0;
2720 found_all = FALSE;
2721 ins_buf = curbuf;
2722 e_cpt = (compl_cont_status & CONT_LOCAL)
2723 ? (char_u *)"." : curbuf->b_p_cpt;
2724 last_match_pos = first_match_pos = *ini;
2725 }
2726 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2727 ins_buf = curbuf; // In case the buffer was wiped out.
2728
2729 compl_old_match = compl_curr_match; // remember the last current match
2730 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2731
2732 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2733 for (;;)
2734 {
2735 found_new_match = FAIL;
2736 set_match_pos = FALSE;
2737
2738 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2739 // or if found_all says this entry is done. For ^X^L only use the
2740 // entries from 'complete' that look in loaded buffers.
2741 if ((ctrl_x_mode == CTRL_X_NORMAL
2742 || ctrl_x_mode_line_or_eval())
2743 && (!compl_started || found_all))
2744 {
2745 found_all = FALSE;
2746 while (*e_cpt == ',' || *e_cpt == ' ')
2747 e_cpt++;
2748 if (*e_cpt == '.' && !curbuf->b_scanned)
2749 {
2750 ins_buf = curbuf;
2751 first_match_pos = *ini;
2752 // Move the cursor back one character so that ^N can match the
2753 // word immediately after the cursor.
2754 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2755 {
2756 // Move the cursor to after the last character in the
2757 // buffer, so that word at start of buffer is found
2758 // correctly.
2759 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2760 first_match_pos.col =
2761 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2762 }
2763 last_match_pos = first_match_pos;
2764 type = 0;
2765
2766 // Remember the first match so that the loop stops when we
2767 // wrap and come back there a second time.
2768 set_match_pos = TRUE;
2769 }
2770 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2771 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2772 {
2773 // Scan a buffer, but not the current one.
2774 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2775 {
2776 compl_started = TRUE;
2777 first_match_pos.col = last_match_pos.col = 0;
2778 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2779 last_match_pos.lnum = 0;
2780 type = 0;
2781 }
2782 else // unloaded buffer, scan like dictionary
2783 {
2784 found_all = TRUE;
2785 if (ins_buf->b_fname == NULL)
2786 continue;
2787 type = CTRL_X_DICTIONARY;
2788 dict = ins_buf->b_fname;
2789 dict_f = DICT_EXACT;
2790 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01002791 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002792 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2793 ins_buf->b_fname == NULL
2794 ? buf_spname(ins_buf)
2795 : ins_buf->b_sfname == NULL
2796 ? ins_buf->b_fname
2797 : ins_buf->b_sfname);
2798 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2799 }
2800 else if (*e_cpt == NUL)
2801 break;
2802 else
2803 {
2804 if (ctrl_x_mode_line_or_eval())
2805 type = -1;
2806 else if (*e_cpt == 'k' || *e_cpt == 's')
2807 {
2808 if (*e_cpt == 'k')
2809 type = CTRL_X_DICTIONARY;
2810 else
2811 type = CTRL_X_THESAURUS;
2812 if (*++e_cpt != ',' && *e_cpt != NUL)
2813 {
2814 dict = e_cpt;
2815 dict_f = DICT_FIRST;
2816 }
2817 }
2818#ifdef FEAT_FIND_ID
2819 else if (*e_cpt == 'i')
2820 type = CTRL_X_PATH_PATTERNS;
2821 else if (*e_cpt == 'd')
2822 type = CTRL_X_PATH_DEFINES;
2823#endif
2824 else if (*e_cpt == ']' || *e_cpt == 't')
2825 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01002826 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002827 type = CTRL_X_TAGS;
2828 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2829 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2830 }
2831 else
2832 type = -1;
2833
2834 // in any case e_cpt is advanced to the next entry
2835 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2836
2837 found_all = TRUE;
2838 if (type == -1)
2839 continue;
2840 }
2841 }
2842
2843 // If complete() was called then compl_pattern has been reset. The
2844 // following won't work then, bail out.
2845 if (compl_pattern == NULL)
2846 break;
2847
2848 switch (type)
2849 {
2850 case -1:
2851 break;
2852#ifdef FEAT_FIND_ID
2853 case CTRL_X_PATH_PATTERNS:
2854 case CTRL_X_PATH_DEFINES:
2855 find_pattern_in_path(compl_pattern, compl_direction,
2856 (int)STRLEN(compl_pattern), FALSE, FALSE,
2857 (type == CTRL_X_PATH_DEFINES
2858 && !(compl_cont_status & CONT_SOL))
2859 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2860 (linenr_T)1, (linenr_T)MAXLNUM);
2861 break;
2862#endif
2863
2864 case CTRL_X_DICTIONARY:
2865 case CTRL_X_THESAURUS:
2866 ins_compl_dictionaries(
2867 dict != NULL ? dict
2868 : (type == CTRL_X_THESAURUS
2869 ? (*curbuf->b_p_tsr == NUL
2870 ? p_tsr
2871 : curbuf->b_p_tsr)
2872 : (*curbuf->b_p_dict == NUL
2873 ? p_dict
2874 : curbuf->b_p_dict)),
2875 compl_pattern,
2876 dict != NULL ? dict_f
2877 : 0, type == CTRL_X_THESAURUS);
2878 dict = NULL;
2879 break;
2880
2881 case CTRL_X_TAGS:
2882 // set p_ic according to p_ic, p_scs and pat for find_tags().
2883 save_p_ic = p_ic;
2884 p_ic = ignorecase(compl_pattern);
2885
2886 // Find up to TAG_MANY matches. Avoids that an enormous number
2887 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002888 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002889 if (find_tags(compl_pattern, &num_matches, &matches,
2890 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2891 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2892 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002894 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002895 p_ic = save_p_ic;
2896 break;
2897
2898 case CTRL_X_FILES:
2899 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2900 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2901 {
2902
2903 // May change home directory back to "~".
2904 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaarac3150d2019-07-28 16:36:39 +02002905#ifdef BACKSLASH_IN_FILENAME
2906 if (curbuf->b_p_csl[0] != NUL)
2907 {
2908 int i;
2909
2910 for (i = 0; i < num_matches; ++i)
2911 {
2912 char_u *ptr = matches[i];
2913
2914 while (*ptr != NUL)
2915 {
2916 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
2917 *ptr = '/';
2918 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
2919 *ptr = '\\';
2920 ptr += (*mb_ptr2len)(ptr);
2921 }
2922 }
2923 }
2924#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002925 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2926 }
2927 break;
2928
2929 case CTRL_X_CMDLINE:
2930 if (expand_cmdline(&compl_xp, compl_pattern,
2931 (int)STRLEN(compl_pattern),
2932 &num_matches, &matches) == EXPAND_OK)
2933 ins_compl_add_matches(num_matches, matches, FALSE);
2934 break;
2935
2936#ifdef FEAT_COMPL_FUNC
2937 case CTRL_X_FUNCTION:
2938 case CTRL_X_OMNI:
2939 expand_by_function(type, compl_pattern);
2940 break;
2941#endif
2942
2943 case CTRL_X_SPELL:
2944#ifdef FEAT_SPELL
2945 num_matches = expand_spelling(first_match_pos.lnum,
2946 compl_pattern, &matches);
2947 if (num_matches > 0)
2948 ins_compl_add_matches(num_matches, matches, p_ic);
2949#endif
2950 break;
2951
2952 default: // normal ^P/^N and ^X^L
2953 // If 'infercase' is set, don't use 'smartcase' here
2954 save_p_scs = p_scs;
2955 if (ins_buf->b_p_inf)
2956 p_scs = FALSE;
2957
2958 // Buffers other than curbuf are scanned from the beginning or the
2959 // end but never from the middle, thus setting nowrapscan in this
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002960 // buffer is a good idea, on the other hand, we always set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002961 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
2962 save_p_ws = p_ws;
2963 if (ins_buf != curbuf)
2964 p_ws = FALSE;
2965 else if (*e_cpt == '.')
2966 p_ws = TRUE;
2967 for (;;)
2968 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002969 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002970
2971 ++msg_silent; // Don't want messages for wrapscan.
2972
2973 // ctrl_x_mode_line_or_eval() || word-wise search that
2974 // has added a word that was at the beginning of the line
2975 if (ctrl_x_mode_line_or_eval()
2976 || (compl_cont_status & CONT_SOL))
2977 found_new_match = search_for_exact_line(ins_buf, pos,
2978 compl_direction, compl_pattern);
2979 else
2980 found_new_match = searchit(NULL, ins_buf, pos, NULL,
2981 compl_direction,
2982 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002983 RE_LAST, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002984 --msg_silent;
2985 if (!compl_started || set_match_pos)
2986 {
2987 // set "compl_started" even on fail
2988 compl_started = TRUE;
2989 first_match_pos = *pos;
2990 last_match_pos = *pos;
2991 set_match_pos = FALSE;
2992 }
2993 else if (first_match_pos.lnum == last_match_pos.lnum
2994 && first_match_pos.col == last_match_pos.col)
2995 found_new_match = FAIL;
2996 if (found_new_match == FAIL)
2997 {
2998 if (ins_buf == curbuf)
2999 found_all = TRUE;
3000 break;
3001 }
3002
3003 // when ADDING, the text before the cursor matches, skip it
3004 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
3005 && ini->lnum == pos->lnum
3006 && ini->col == pos->col)
3007 continue;
3008 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3009 if (ctrl_x_mode_line_or_eval())
3010 {
3011 if (compl_cont_status & CONT_ADDING)
3012 {
3013 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3014 continue;
3015 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3016 if (!p_paste)
3017 ptr = skipwhite(ptr);
3018 }
3019 len = (int)STRLEN(ptr);
3020 }
3021 else
3022 {
3023 char_u *tmp_ptr = ptr;
3024
3025 if (compl_cont_status & CONT_ADDING)
3026 {
3027 tmp_ptr += compl_length;
3028 // Skip if already inside a word.
3029 if (vim_iswordp(tmp_ptr))
3030 continue;
3031 // Find start of next word.
3032 tmp_ptr = find_word_start(tmp_ptr);
3033 }
3034 // Find end of this word.
3035 tmp_ptr = find_word_end(tmp_ptr);
3036 len = (int)(tmp_ptr - ptr);
3037
3038 if ((compl_cont_status & CONT_ADDING)
3039 && len == compl_length)
3040 {
3041 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3042 {
3043 // Try next line, if any. the new word will be
3044 // "join" as if the normal command "J" was used.
3045 // IOSIZE is always greater than
3046 // compl_length, so the next STRNCPY always
3047 // works -- Acevedo
3048 STRNCPY(IObuff, ptr, len);
3049 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3050 tmp_ptr = ptr = skipwhite(ptr);
3051 // Find start of next word.
3052 tmp_ptr = find_word_start(tmp_ptr);
3053 // Find end of next word.
3054 tmp_ptr = find_word_end(tmp_ptr);
3055 if (tmp_ptr > ptr)
3056 {
3057 if (*ptr != ')' && IObuff[len - 1] != TAB)
3058 {
3059 if (IObuff[len - 1] != ' ')
3060 IObuff[len++] = ' ';
3061 // IObuf =~ "\k.* ", thus len >= 2
3062 if (p_js
3063 && (IObuff[len - 2] == '.'
3064 || (vim_strchr(p_cpo, CPO_JOINSP)
3065 == NULL
3066 && (IObuff[len - 2] == '?'
3067 || IObuff[len - 2] == '!'))))
3068 IObuff[len++] = ' ';
3069 }
3070 // copy as much as possible of the new word
3071 if (tmp_ptr - ptr >= IOSIZE - len)
3072 tmp_ptr = ptr + IOSIZE - len - 1;
3073 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3074 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003075 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003076 }
3077 IObuff[len] = NUL;
3078 ptr = IObuff;
3079 }
3080 if (len == compl_length)
3081 continue;
3082 }
3083 }
3084 if (ins_compl_add_infercase(ptr, len, p_ic,
3085 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003086 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003087 {
3088 found_new_match = OK;
3089 break;
3090 }
3091 }
3092 p_scs = save_p_scs;
3093 p_ws = save_p_ws;
3094 }
3095
3096 // check if compl_curr_match has changed, (e.g. other type of
3097 // expansion added something)
3098 if (type != 0 && compl_curr_match != compl_old_match)
3099 found_new_match = OK;
3100
3101 // break the loop for specialized modes (use 'complete' just for the
3102 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3103 // match
3104 if ((ctrl_x_mode != CTRL_X_NORMAL
3105 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3106 {
3107 if (got_int)
3108 break;
3109 // Fill the popup menu as soon as possible.
3110 if (type != -1)
3111 ins_compl_check_keys(0, FALSE);
3112
3113 if ((ctrl_x_mode != CTRL_X_NORMAL
3114 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3115 break;
3116 compl_started = TRUE;
3117 }
3118 else
3119 {
3120 // Mark a buffer scanned when it has been scanned completely
3121 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3122 ins_buf->b_scanned = TRUE;
3123
3124 compl_started = FALSE;
3125 }
3126 }
3127 compl_started = TRUE;
3128
3129 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
3130 && *e_cpt == NUL) // Got to end of 'complete'
3131 found_new_match = FAIL;
3132
3133 i = -1; // total of matches, unknown
3134 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3135 && !ctrl_x_mode_line_or_eval()))
3136 i = ins_compl_make_cyclic();
3137
3138 if (compl_old_match != NULL)
3139 {
3140 // If several matches were added (FORWARD) or the search failed and has
3141 // just been made cyclic then we have to move compl_curr_match to the
3142 // next or previous entry (if any) -- Acevedo
3143 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3144 : compl_old_match->cp_prev;
3145 if (compl_curr_match == NULL)
3146 compl_curr_match = compl_old_match;
3147 }
3148 return i;
3149}
3150
3151/*
3152 * Delete the old text being completed.
3153 */
3154 void
3155ins_compl_delete(void)
3156{
3157 int col;
3158
3159 // In insert mode: Delete the typed part.
3160 // In replace mode: Put the old characters back, if any.
3161 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3162 if ((int)curwin->w_cursor.col > col)
3163 {
3164 if (stop_arrow() == FAIL)
3165 return;
3166 backspace_until_column(col);
3167 }
3168
3169 // TODO: is this sufficient for redrawing? Redrawing everything causes
3170 // flicker, thus we can't do that.
3171 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003172#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003173 // clear v:completed_item
3174 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003175#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003176}
3177
3178/*
3179 * Insert the new text being completed.
3180 * "in_compl_func" is TRUE when called from complete_check().
3181 */
3182 void
3183ins_compl_insert(int in_compl_func)
3184{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003185 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003186 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003187 compl_used_match = FALSE;
3188 else
3189 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003190#ifdef FEAT_EVAL
3191 {
3192 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3193
3194 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3195 }
3196#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003197 if (!in_compl_func)
3198 compl_curr_match = compl_shown_match;
3199}
3200
3201/*
3202 * Fill in the next completion in the current direction.
3203 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3204 * get more completions. If it is FALSE, then we just do nothing when there
3205 * are no more completions in a given direction. The latter case is used when
3206 * we are still in the middle of finding completions, to allow browsing
3207 * through the ones found so far.
3208 * Return the total number of matches, or -1 if still unknown -- webb.
3209 *
3210 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3211 * compl_shown_match here.
3212 *
3213 * Note that this function may be called recursively once only. First with
3214 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3215 * calls this function with "allow_get_expansion" FALSE.
3216 */
3217 static int
3218ins_compl_next(
3219 int allow_get_expansion,
3220 int count, // repeat completion this many times; should
3221 // be at least 1
3222 int insert_match, // Insert the newly selected match
3223 int in_compl_func) // called from complete_check()
3224{
3225 int num_matches = -1;
3226 int todo = count;
3227 compl_T *found_compl = NULL;
3228 int found_end = FALSE;
3229 int advance;
3230 int started = compl_started;
3231
3232 // When user complete function return -1 for findstart which is next
3233 // time of 'always', compl_shown_match become NULL.
3234 if (compl_shown_match == NULL)
3235 return -1;
3236
3237 if (compl_leader != NULL
Bram Moolenaar28976e22021-01-29 21:07:07 +01003238 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003239 {
3240 // Set "compl_shown_match" to the actually shown match, it may differ
3241 // when "compl_leader" is used to omit some of the matches.
3242 while (!ins_compl_equal(compl_shown_match,
3243 compl_leader, (int)STRLEN(compl_leader))
3244 && compl_shown_match->cp_next != NULL
3245 && compl_shown_match->cp_next != compl_first_match)
3246 compl_shown_match = compl_shown_match->cp_next;
3247
3248 // If we didn't find it searching forward, and compl_shows_dir is
3249 // backward, find the last match.
3250 if (compl_shows_dir == BACKWARD
3251 && !ins_compl_equal(compl_shown_match,
3252 compl_leader, (int)STRLEN(compl_leader))
3253 && (compl_shown_match->cp_next == NULL
3254 || compl_shown_match->cp_next == compl_first_match))
3255 {
3256 while (!ins_compl_equal(compl_shown_match,
3257 compl_leader, (int)STRLEN(compl_leader))
3258 && compl_shown_match->cp_prev != NULL
3259 && compl_shown_match->cp_prev != compl_first_match)
3260 compl_shown_match = compl_shown_match->cp_prev;
3261 }
3262 }
3263
3264 if (allow_get_expansion && insert_match
3265 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3266 // Delete old text to be replaced
3267 ins_compl_delete();
3268
3269 // When finding the longest common text we stick at the original text,
3270 // don't let CTRL-N or CTRL-P move to the first match.
3271 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3272
3273 // When restarting the search don't insert the first match either.
3274 if (compl_restarting)
3275 {
3276 advance = FALSE;
3277 compl_restarting = FALSE;
3278 }
3279
3280 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3281 // around.
3282 while (--todo >= 0)
3283 {
3284 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3285 {
3286 compl_shown_match = compl_shown_match->cp_next;
3287 found_end = (compl_first_match != NULL
3288 && (compl_shown_match->cp_next == compl_first_match
3289 || compl_shown_match == compl_first_match));
3290 }
3291 else if (compl_shows_dir == BACKWARD
3292 && compl_shown_match->cp_prev != NULL)
3293 {
3294 found_end = (compl_shown_match == compl_first_match);
3295 compl_shown_match = compl_shown_match->cp_prev;
3296 found_end |= (compl_shown_match == compl_first_match);
3297 }
3298 else
3299 {
3300 if (!allow_get_expansion)
3301 {
3302 if (advance)
3303 {
3304 if (compl_shows_dir == BACKWARD)
3305 compl_pending -= todo + 1;
3306 else
3307 compl_pending += todo + 1;
3308 }
3309 return -1;
3310 }
3311
3312 if (!compl_no_select && advance)
3313 {
3314 if (compl_shows_dir == BACKWARD)
3315 --compl_pending;
3316 else
3317 ++compl_pending;
3318 }
3319
3320 // Find matches.
3321 num_matches = ins_compl_get_exp(&compl_startpos);
3322
3323 // handle any pending completions
3324 while (compl_pending != 0 && compl_direction == compl_shows_dir
3325 && advance)
3326 {
3327 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3328 {
3329 compl_shown_match = compl_shown_match->cp_next;
3330 --compl_pending;
3331 }
3332 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3333 {
3334 compl_shown_match = compl_shown_match->cp_prev;
3335 ++compl_pending;
3336 }
3337 else
3338 break;
3339 }
3340 found_end = FALSE;
3341 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003342 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003343 && compl_leader != NULL
3344 && !ins_compl_equal(compl_shown_match,
3345 compl_leader, (int)STRLEN(compl_leader)))
3346 ++todo;
3347 else
3348 // Remember a matching item.
3349 found_compl = compl_shown_match;
3350
3351 // Stop at the end of the list when we found a usable match.
3352 if (found_end)
3353 {
3354 if (found_compl != NULL)
3355 {
3356 compl_shown_match = found_compl;
3357 break;
3358 }
3359 todo = 1; // use first usable match after wrapping around
3360 }
3361 }
3362
3363 // Insert the text of the new completion, or the compl_leader.
3364 if (compl_no_insert && !started)
3365 {
3366 ins_bytes(compl_orig_text + ins_compl_len());
3367 compl_used_match = FALSE;
3368 }
3369 else if (insert_match)
3370 {
3371 if (!compl_get_longest || compl_used_match)
3372 ins_compl_insert(in_compl_func);
3373 else
3374 ins_bytes(compl_leader + ins_compl_len());
3375 }
3376 else
3377 compl_used_match = FALSE;
3378
3379 if (!allow_get_expansion)
3380 {
3381 // may undisplay the popup menu first
3382 ins_compl_upd_pum();
3383
3384 if (pum_enough_matches())
3385 // Will display the popup menu, don't redraw yet to avoid flicker.
3386 pum_call_update_screen();
3387 else
3388 // Not showing the popup menu yet, redraw to show the user what was
3389 // inserted.
3390 update_screen(0);
3391
3392 // display the updated popup menu
3393 ins_compl_show_pum();
3394#ifdef FEAT_GUI
3395 if (gui.in_use)
3396 {
3397 // Show the cursor after the match, not after the redrawn text.
3398 setcursor();
3399 out_flush_cursor(FALSE, FALSE);
3400 }
3401#endif
3402
3403 // Delete old text to be replaced, since we're still searching and
3404 // don't want to match ourselves!
3405 ins_compl_delete();
3406 }
3407
3408 // Enter will select a match when the match wasn't inserted and the popup
3409 // menu is visible.
3410 if (compl_no_insert && !started)
3411 compl_enter_selects = TRUE;
3412 else
3413 compl_enter_selects = !insert_match && compl_match_array != NULL;
3414
3415 // Show the file name for the match (if any)
3416 // Truncate the file name to avoid a wait for return.
3417 if (compl_shown_match->cp_fname != NULL)
3418 {
3419 char *lead = _("match in file");
3420 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3421 char_u *s;
3422 char_u *e;
3423
3424 if (space > 0)
3425 {
3426 // We need the tail that fits. With double-byte encoding going
3427 // back from the end is very slow, thus go from the start and keep
3428 // the text that fits in "space" between "s" and "e".
3429 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3430 {
3431 space -= ptr2cells(e);
3432 while (space < 0)
3433 {
3434 space += ptr2cells(s);
3435 MB_PTR_ADV(s);
3436 }
3437 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01003438 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003439 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3440 s > compl_shown_match->cp_fname ? "<" : "", s);
3441 msg((char *)IObuff);
Bram Moolenaarcc233582020-12-12 13:32:07 +01003442 msg_hist_off = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003443 redraw_cmdline = FALSE; // don't overwrite!
3444 }
3445 }
3446
3447 return num_matches;
3448}
3449
3450/*
3451 * Call this while finding completions, to check whether the user has hit a key
3452 * that should change the currently displayed completion, or exit completion
3453 * mode. Also, when compl_pending is not zero, show a completion as soon as
3454 * possible. -- webb
3455 * "frequency" specifies out of how many calls we actually check.
3456 * "in_compl_func" is TRUE when called from complete_check(), don't set
3457 * compl_curr_match.
3458 */
3459 void
3460ins_compl_check_keys(int frequency, int in_compl_func)
3461{
3462 static int count = 0;
3463 int c;
3464
3465 // Don't check when reading keys from a script, :normal or feedkeys().
3466 // That would break the test scripts. But do check for keys when called
3467 // from complete_check().
3468 if (!in_compl_func && (using_script() || ex_normal_busy))
3469 return;
3470
3471 // Only do this at regular intervals
3472 if (++count < frequency)
3473 return;
3474 count = 0;
3475
3476 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3477 // can't do its work correctly.
3478 c = vpeekc_any();
3479 if (c != NUL)
3480 {
3481 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3482 {
3483 c = safe_vgetc(); // Eat the character
3484 compl_shows_dir = ins_compl_key2dir(c);
3485 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3486 c != K_UP && c != K_DOWN, in_compl_func);
3487 }
3488 else
3489 {
3490 // Need to get the character to have KeyTyped set. We'll put it
3491 // back with vungetc() below. But skip K_IGNORE.
3492 c = safe_vgetc();
3493 if (c != K_IGNORE)
3494 {
3495 // Don't interrupt completion when the character wasn't typed,
3496 // e.g., when doing @q to replay keys.
3497 if (c != Ctrl_R && KeyTyped)
3498 compl_interrupted = TRUE;
3499
3500 vungetc(c);
3501 }
3502 }
3503 }
3504 if (compl_pending != 0 && !got_int && !compl_no_insert)
3505 {
3506 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3507
3508 compl_pending = 0;
3509 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3510 }
3511}
3512
3513/*
3514 * Decide the direction of Insert mode complete from the key typed.
3515 * Returns BACKWARD or FORWARD.
3516 */
3517 static int
3518ins_compl_key2dir(int c)
3519{
3520 if (c == Ctrl_P || c == Ctrl_L
3521 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3522 return BACKWARD;
3523 return FORWARD;
3524}
3525
3526/*
3527 * Return TRUE for keys that are used for completion only when the popup menu
3528 * is visible.
3529 */
3530 static int
3531ins_compl_pum_key(int c)
3532{
3533 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3534 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3535 || c == K_UP || c == K_DOWN);
3536}
3537
3538/*
3539 * Decide the number of completions to move forward.
3540 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3541 */
3542 static int
3543ins_compl_key2count(int c)
3544{
3545 int h;
3546
3547 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3548 {
3549 h = pum_get_height();
3550 if (h > 3)
3551 h -= 2; // keep some context
3552 return h;
3553 }
3554 return 1;
3555}
3556
3557/*
3558 * Return TRUE if completion with "c" should insert the match, FALSE if only
3559 * to change the currently selected completion.
3560 */
3561 static int
3562ins_compl_use_match(int c)
3563{
3564 switch (c)
3565 {
3566 case K_UP:
3567 case K_DOWN:
3568 case K_PAGEDOWN:
3569 case K_KPAGEDOWN:
3570 case K_S_DOWN:
3571 case K_PAGEUP:
3572 case K_KPAGEUP:
3573 case K_S_UP:
3574 return FALSE;
3575 }
3576 return TRUE;
3577}
3578
3579/*
3580 * Do Insert mode completion.
3581 * Called when character "c" was typed, which has a meaning for completion.
3582 * Returns OK if completion was done, FAIL if something failed (out of mem).
3583 */
3584 int
3585ins_complete(int c, int enable_pum)
3586{
3587 char_u *line;
3588 int startcol = 0; // column where searched text starts
3589 colnr_T curs_col; // cursor column
3590 int n;
3591 int save_w_wrow;
3592 int save_w_leftcol;
3593 int insert_match;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003594#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003595 int save_did_ai = did_ai;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003596#endif
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003597 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003598
3599 compl_direction = ins_compl_key2dir(c);
3600 insert_match = ins_compl_use_match(c);
3601
3602 if (!compl_started)
3603 {
3604 // First time we hit ^N or ^P (in a row, I mean)
3605
3606 did_ai = FALSE;
3607#ifdef FEAT_SMARTINDENT
3608 did_si = FALSE;
3609 can_si = FALSE;
3610 can_si_back = FALSE;
3611#endif
3612 if (stop_arrow() == FAIL)
3613 return FAIL;
3614
3615 line = ml_get(curwin->w_cursor.lnum);
3616 curs_col = curwin->w_cursor.col;
3617 compl_pending = 0;
3618
3619 // If this same ctrl_x_mode has been interrupted use the text from
3620 // "compl_startpos" to the cursor as a pattern to add a new word
3621 // instead of expand the one before the cursor, in word-wise if
3622 // "compl_startpos" is not in the same line as the cursor then fix it
3623 // (the line has been split because it was longer than 'tw'). if SOL
3624 // is set then skip the previous pattern, a word at the beginning of
3625 // the line has been inserted, we'll look for that -- Acevedo.
3626 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3627 && compl_cont_mode == ctrl_x_mode)
3628 {
3629 // it is a continued search
3630 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3631 if (ctrl_x_mode == CTRL_X_NORMAL
3632 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3633 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3634 {
3635 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3636 {
3637 // line (probably) wrapped, set compl_startpos to the
3638 // first non_blank in the line, if it is not a wordchar
3639 // include it to get a better pattern, but then we don't
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003640 // want the "\\<" prefix, check it below
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003641 compl_col = (colnr_T)getwhitecols(line);
3642 compl_startpos.col = compl_col;
3643 compl_startpos.lnum = curwin->w_cursor.lnum;
3644 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3645 }
3646 else
3647 {
3648 // S_IPOS was set when we inserted a word that was at the
3649 // beginning of the line, which means that we'll go to SOL
3650 // mode but first we need to redefine compl_startpos
3651 if (compl_cont_status & CONT_S_IPOS)
3652 {
3653 compl_cont_status |= CONT_SOL;
3654 compl_startpos.col = (colnr_T)(skipwhite(
3655 line + compl_length
3656 + compl_startpos.col) - line);
3657 }
3658 compl_col = compl_startpos.col;
3659 }
3660 compl_length = curwin->w_cursor.col - (int)compl_col;
3661 // IObuff is used to add a "word from the next line" would we
3662 // have enough space? just being paranoid
3663#define MIN_SPACE 75
3664 if (compl_length > (IOSIZE - MIN_SPACE))
3665 {
3666 compl_cont_status &= ~CONT_SOL;
3667 compl_length = (IOSIZE - MIN_SPACE);
3668 compl_col = curwin->w_cursor.col - compl_length;
3669 }
3670 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3671 if (compl_length < 1)
3672 compl_cont_status &= CONT_LOCAL;
3673 }
3674 else if (ctrl_x_mode_line_or_eval())
3675 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3676 else
3677 compl_cont_status = 0;
3678 }
3679 else
3680 compl_cont_status &= CONT_LOCAL;
3681
3682 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3683 {
3684 compl_cont_mode = ctrl_x_mode;
3685 if (ctrl_x_mode != CTRL_X_NORMAL)
3686 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3687 compl_cont_status = 0;
3688 compl_cont_status |= CONT_N_ADDS;
3689 compl_startpos = curwin->w_cursor;
3690 startcol = (int)curs_col;
3691 compl_col = 0;
3692 }
3693
3694 // Work out completion pattern and original text -- webb
3695 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3696 {
3697 if ((compl_cont_status & CONT_SOL)
3698 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3699 {
3700 if (!(compl_cont_status & CONT_ADDING))
3701 {
3702 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3703 ;
3704 compl_col += ++startcol;
3705 compl_length = curs_col - startcol;
3706 }
3707 if (p_ic)
3708 compl_pattern = str_foldcase(line + compl_col,
3709 compl_length, NULL, 0);
3710 else
3711 compl_pattern = vim_strnsave(line + compl_col,
3712 compl_length);
3713 if (compl_pattern == NULL)
3714 return FAIL;
3715 }
3716 else if (compl_cont_status & CONT_ADDING)
3717 {
3718 char_u *prefix = (char_u *)"\\<";
3719
3720 // we need up to 2 extra chars for the prefix
3721 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3722 compl_length) + 2);
3723 if (compl_pattern == NULL)
3724 return FAIL;
3725 if (!vim_iswordp(line + compl_col)
3726 || (compl_col > 0
3727 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3728 prefix = (char_u *)"";
3729 STRCPY((char *)compl_pattern, prefix);
3730 (void)quote_meta(compl_pattern + STRLEN(prefix),
3731 line + compl_col, compl_length);
3732 }
3733 else if (--startcol < 0
3734 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3735 {
3736 // Match any word of at least two chars
3737 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3738 if (compl_pattern == NULL)
3739 return FAIL;
3740 compl_col += curs_col;
3741 compl_length = 0;
3742 }
3743 else
3744 {
3745 // Search the point of change class of multibyte character
3746 // or not a word single byte character backward.
3747 if (has_mbyte)
3748 {
3749 int base_class;
3750 int head_off;
3751
3752 startcol -= (*mb_head_off)(line, line + startcol);
3753 base_class = mb_get_class(line + startcol);
3754 while (--startcol >= 0)
3755 {
3756 head_off = (*mb_head_off)(line, line + startcol);
3757 if (base_class != mb_get_class(line + startcol
3758 - head_off))
3759 break;
3760 startcol -= head_off;
3761 }
3762 }
3763 else
3764 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3765 ;
3766 compl_col += ++startcol;
3767 compl_length = (int)curs_col - startcol;
3768 if (compl_length == 1)
3769 {
3770 // Only match word with at least two chars -- webb
3771 // there's no need to call quote_meta,
3772 // alloc(7) is enough -- Acevedo
3773 compl_pattern = alloc(7);
3774 if (compl_pattern == NULL)
3775 return FAIL;
3776 STRCPY((char *)compl_pattern, "\\<");
3777 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3778 STRCAT((char *)compl_pattern, "\\k");
3779 }
3780 else
3781 {
3782 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3783 compl_length) + 2);
3784 if (compl_pattern == NULL)
3785 return FAIL;
3786 STRCPY((char *)compl_pattern, "\\<");
3787 (void)quote_meta(compl_pattern + 2, line + compl_col,
3788 compl_length);
3789 }
3790 }
3791 }
3792 else if (ctrl_x_mode_line_or_eval())
3793 {
3794 compl_col = (colnr_T)getwhitecols(line);
3795 compl_length = (int)curs_col - (int)compl_col;
3796 if (compl_length < 0) // cursor in indent: empty pattern
3797 compl_length = 0;
3798 if (p_ic)
3799 compl_pattern = str_foldcase(line + compl_col, compl_length,
3800 NULL, 0);
3801 else
3802 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3803 if (compl_pattern == NULL)
3804 return FAIL;
3805 }
3806 else if (ctrl_x_mode == CTRL_X_FILES)
3807 {
3808 // Go back to just before the first filename character.
3809 if (startcol > 0)
3810 {
3811 char_u *p = line + startcol;
3812
3813 MB_PTR_BACK(line, p);
3814 while (p > line && vim_isfilec(PTR2CHAR(p)))
3815 MB_PTR_BACK(line, p);
3816 if (p == line && vim_isfilec(PTR2CHAR(p)))
3817 startcol = 0;
3818 else
3819 startcol = (int)(p - line) + 1;
3820 }
3821
3822 compl_col += startcol;
3823 compl_length = (int)curs_col - startcol;
3824 compl_pattern = addstar(line + compl_col, compl_length,
3825 EXPAND_FILES);
3826 if (compl_pattern == NULL)
3827 return FAIL;
3828 }
3829 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3830 {
3831 compl_pattern = vim_strnsave(line, curs_col);
3832 if (compl_pattern == NULL)
3833 return FAIL;
3834 set_cmd_context(&compl_xp, compl_pattern,
3835 (int)STRLEN(compl_pattern), curs_col, FALSE);
3836 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3837 || compl_xp.xp_context == EXPAND_NOTHING)
3838 // No completion possible, use an empty pattern to get a
3839 // "pattern not found" message.
3840 compl_col = curs_col;
3841 else
3842 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3843 compl_length = curs_col - compl_col;
3844 }
3845 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3846 {
3847#ifdef FEAT_COMPL_FUNC
3848 // Call user defined function 'completefunc' with "a:findstart"
3849 // set to 1 to obtain the length of text to use for completion.
3850 typval_T args[3];
3851 int col;
3852 char_u *funcname;
3853 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003854 int save_State = State;
3855
3856 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3857 // string
3858 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3859 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3860 if (*funcname == NUL)
3861 {
3862 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3863 ? "completefunc" : "omnifunc");
3864 // restore did_ai, so that adding comment leader works
3865 did_ai = save_did_ai;
3866 return FAIL;
3867 }
3868
3869 args[0].v_type = VAR_NUMBER;
3870 args[0].vval.v_number = 1;
3871 args[1].v_type = VAR_STRING;
3872 args[1].vval.v_string = (char_u *)"";
3873 args[2].v_type = VAR_UNKNOWN;
3874 pos = curwin->w_cursor;
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003875 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003876 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003877 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003878
3879 State = save_State;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003880 curwin->w_cursor = pos; // restore the cursor position
3881 validate_cursor();
3882 if (!EQUAL_POS(curwin->w_cursor, pos))
3883 {
3884 emsg(_(e_compldel));
3885 return FAIL;
3886 }
3887
3888 // Return value -2 means the user complete function wants to
3889 // cancel the complete without an error.
3890 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3891 if (col == -2)
3892 return FAIL;
3893 if (col == -3)
3894 {
3895 ctrl_x_mode = CTRL_X_NORMAL;
3896 edit_submode = NULL;
3897 if (!shortmess(SHM_COMPLETIONMENU))
3898 msg_clr_cmdline();
3899 return FAIL;
3900 }
3901
3902 // Reset extended parameters of completion, when start new
3903 // completion.
3904 compl_opt_refresh_always = FALSE;
3905 compl_opt_suppress_empty = FALSE;
3906
3907 if (col < 0)
3908 col = curs_col;
3909 compl_col = col;
3910 if (compl_col > curs_col)
3911 compl_col = curs_col;
3912
3913 // Setup variables for completion. Need to obtain "line" again,
3914 // it may have become invalid.
3915 line = ml_get(curwin->w_cursor.lnum);
3916 compl_length = curs_col - compl_col;
3917 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3918 if (compl_pattern == NULL)
3919#endif
3920 return FAIL;
3921 }
3922 else if (ctrl_x_mode == CTRL_X_SPELL)
3923 {
3924#ifdef FEAT_SPELL
3925 if (spell_bad_len > 0)
3926 compl_col = curs_col - spell_bad_len;
3927 else
3928 compl_col = spell_word_start(startcol);
3929 if (compl_col >= (colnr_T)startcol)
3930 {
3931 compl_length = 0;
3932 compl_col = curs_col;
3933 }
3934 else
3935 {
3936 spell_expand_check_cap(compl_col);
3937 compl_length = (int)curs_col - compl_col;
3938 }
3939 // Need to obtain "line" again, it may have become invalid.
3940 line = ml_get(curwin->w_cursor.lnum);
3941 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3942 if (compl_pattern == NULL)
3943#endif
3944 return FAIL;
3945 }
3946 else
3947 {
3948 internal_error("ins_complete()");
3949 return FAIL;
3950 }
3951
3952 if (compl_cont_status & CONT_ADDING)
3953 {
3954 edit_submode_pre = (char_u *)_(" Adding");
3955 if (ctrl_x_mode_line_or_eval())
3956 {
3957 // Insert a new line, keep indentation but ignore 'comments'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003958 char_u *old = curbuf->b_p_com;
3959
3960 curbuf->b_p_com = (char_u *)"";
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003961 compl_startpos.lnum = curwin->w_cursor.lnum;
3962 compl_startpos.col = compl_col;
3963 ins_eol('\r');
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003964 curbuf->b_p_com = old;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003965 compl_length = 0;
3966 compl_col = curwin->w_cursor.col;
3967 }
3968 }
3969 else
3970 {
3971 edit_submode_pre = NULL;
3972 compl_startpos.col = compl_col;
3973 }
3974
3975 if (compl_cont_status & CONT_LOCAL)
3976 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
3977 else
3978 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3979
3980 // If any of the original typed text has been changed we need to fix
3981 // the redo buffer.
3982 ins_compl_fixRedoBufForLeader(NULL);
3983
3984 // Always add completion for the original text.
3985 vim_free(compl_orig_text);
3986 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003987 if (p_ic)
3988 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003989 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar08928322020-01-04 14:32:48 +01003990 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003991 {
3992 VIM_CLEAR(compl_pattern);
3993 VIM_CLEAR(compl_orig_text);
3994 return FAIL;
3995 }
3996
3997 // showmode might reset the internal line pointers, so it must
3998 // be called before line = ml_get(), or when this address is no
3999 // longer needed. -- Acevedo.
4000 edit_submode_extra = (char_u *)_("-- Searching...");
4001 edit_submode_highl = HLF_COUNT;
4002 showmode();
4003 edit_submode_extra = NULL;
4004 out_flush();
4005 }
4006 else if (insert_match && stop_arrow() == FAIL)
4007 return FAIL;
4008
4009 compl_shown_match = compl_curr_match;
4010 compl_shows_dir = compl_direction;
4011
4012 // Find next match (and following matches).
4013 save_w_wrow = curwin->w_wrow;
4014 save_w_leftcol = curwin->w_leftcol;
4015 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4016
4017 // may undisplay the popup menu
4018 ins_compl_upd_pum();
4019
4020 if (n > 1) // all matches have been found
4021 compl_matches = n;
4022 compl_curr_match = compl_shown_match;
4023 compl_direction = compl_shows_dir;
4024
4025 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4026 // mode.
4027 if (got_int && !global_busy)
4028 {
4029 (void)vgetc();
4030 got_int = FALSE;
4031 }
4032
4033 // we found no match if the list has only the "compl_orig_text"-entry
4034 if (compl_first_match == compl_first_match->cp_next)
4035 {
4036 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4037 && compl_length > 1
4038 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4039 edit_submode_highl = HLF_E;
4040 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4041 // because we couldn't expand anything at first place, but if we used
4042 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4043 // (such as M in M'exico) if not tried already. -- Acevedo
4044 if ( compl_length > 1
4045 || (compl_cont_status & CONT_ADDING)
4046 || (ctrl_x_mode != CTRL_X_NORMAL
4047 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4048 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4049 compl_cont_status &= ~CONT_N_ADDS;
4050 }
4051
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004052 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053 compl_cont_status |= CONT_S_IPOS;
4054 else
4055 compl_cont_status &= ~CONT_S_IPOS;
4056
4057 if (edit_submode_extra == NULL)
4058 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004059 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060 {
4061 edit_submode_extra = (char_u *)_("Back at original");
4062 edit_submode_highl = HLF_W;
4063 }
4064 else if (compl_cont_status & CONT_S_IPOS)
4065 {
4066 edit_submode_extra = (char_u *)_("Word from other line");
4067 edit_submode_highl = HLF_COUNT;
4068 }
4069 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4070 {
4071 edit_submode_extra = (char_u *)_("The only match");
4072 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004073 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 }
4075 else
4076 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004077#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004078 // Update completion sequence number when needed.
4079 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004080 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004081#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004082 // The match should always have a sequence number now, this is
4083 // just a safety check.
4084 if (compl_curr_match->cp_number != -1)
4085 {
4086 // Space for 10 text chars. + 2x10-digit no.s = 31.
4087 // Translations may need more than twice that.
4088 static char_u match_ref[81];
4089
4090 if (compl_matches > 0)
4091 vim_snprintf((char *)match_ref, sizeof(match_ref),
4092 _("match %d of %d"),
4093 compl_curr_match->cp_number, compl_matches);
4094 else
4095 vim_snprintf((char *)match_ref, sizeof(match_ref),
4096 _("match %d"),
4097 compl_curr_match->cp_number);
4098 edit_submode_extra = match_ref;
4099 edit_submode_highl = HLF_R;
4100 if (dollar_vcol >= 0)
4101 curs_columns(FALSE);
4102 }
4103 }
4104 }
4105
4106 // Show a message about what (completion) mode we're in.
4107 if (!compl_opt_suppress_empty)
4108 {
4109 showmode();
4110 if (!shortmess(SHM_COMPLETIONMENU))
4111 {
4112 if (edit_submode_extra != NULL)
4113 {
4114 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004115 {
4116 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004117 msg_attr((char *)edit_submode_extra,
4118 edit_submode_highl < HLF_COUNT
4119 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004120 msg_hist_off = FALSE;
4121 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004122 }
4123 else
4124 msg_clr_cmdline(); // necessary for "noshowmode"
4125 }
4126 }
4127
4128 // Show the popup menu, unless we got interrupted.
4129 if (enable_pum && !compl_interrupted)
4130 show_pum(save_w_wrow, save_w_leftcol);
4131
4132 compl_was_interrupted = compl_interrupted;
4133 compl_interrupted = FALSE;
4134
4135 return OK;
4136}
4137
4138 static void
4139show_pum(int prev_w_wrow, int prev_w_leftcol)
4140{
4141 // RedrawingDisabled may be set when invoked through complete().
4142 int n = RedrawingDisabled;
4143
4144 RedrawingDisabled = 0;
4145
4146 // If the cursor moved or the display scrolled we need to remove the pum
4147 // first.
4148 setcursor();
4149 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4150 ins_compl_del_pum();
4151
4152 ins_compl_show_pum();
4153 setcursor();
4154 RedrawingDisabled = n;
4155}
4156
4157/*
4158 * Looks in the first "len" chars. of "src" for search-metachars.
4159 * If dest is not NULL the chars. are copied there quoting (with
4160 * a backslash) the metachars, and dest would be NUL terminated.
4161 * Returns the length (needed) of dest
4162 */
4163 static unsigned
4164quote_meta(char_u *dest, char_u *src, int len)
4165{
4166 unsigned m = (unsigned)len + 1; // one extra for the NUL
4167
4168 for ( ; --len >= 0; src++)
4169 {
4170 switch (*src)
4171 {
4172 case '.':
4173 case '*':
4174 case '[':
4175 if (ctrl_x_mode == CTRL_X_DICTIONARY
4176 || ctrl_x_mode == CTRL_X_THESAURUS)
4177 break;
4178 // FALLTHROUGH
4179 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004180 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004181 break;
4182 // FALLTHROUGH
4183 case '\\':
4184 if (ctrl_x_mode == CTRL_X_DICTIONARY
4185 || ctrl_x_mode == CTRL_X_THESAURUS)
4186 break;
4187 // FALLTHROUGH
4188 case '^': // currently it's not needed.
4189 case '$':
4190 m++;
4191 if (dest != NULL)
4192 *dest++ = '\\';
4193 break;
4194 }
4195 if (dest != NULL)
4196 *dest++ = *src;
4197 // Copy remaining bytes of a multibyte character.
4198 if (has_mbyte)
4199 {
4200 int i, mb_len;
4201
4202 mb_len = (*mb_ptr2len)(src) - 1;
4203 if (mb_len > 0 && len >= mb_len)
4204 for (i = 0; i < mb_len; ++i)
4205 {
4206 --len;
4207 ++src;
4208 if (dest != NULL)
4209 *dest++ = *src;
4210 }
4211 }
4212 }
4213 if (dest != NULL)
4214 *dest = NUL;
4215
4216 return m;
4217}
4218
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004219#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004220 void
4221free_insexpand_stuff(void)
4222{
4223 VIM_CLEAR(compl_orig_text);
4224}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004225#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004226
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004227#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004228/*
4229 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4230 * spelled word, if there is one.
4231 */
4232 static void
4233spell_back_to_badword(void)
4234{
4235 pos_T tpos = curwin->w_cursor;
4236
4237 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4238 if (curwin->w_cursor.col != tpos.col)
4239 start_arrow(&tpos);
4240}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004241#endif