blob: 9bbf1233cfc7e909ff731fe67cfad0df23d8c0b9 [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
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002439 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002440 {
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002441 emsg(_("E785: complete() can only be used in Insert mode"));
2442 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002443 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002444
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002445 // "textlock" is set when evaluating 'completefunc' but we can change
2446 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002447 textlock = 0;
2448
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002449 // Check for undo allowed here, because if something was already inserted
2450 // the line was already saved for undo and this check isn't done.
2451 if (!undo_allowed())
2452 return;
2453
2454 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002455 emsg(_(e_invarg));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002456 else
2457 {
2458 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2459 if (startcol > 0)
2460 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002461 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002462 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002463}
2464
2465/*
2466 * "complete_add()" function
2467 */
2468 void
2469f_complete_add(typval_T *argvars, typval_T *rettv)
2470{
Bram Moolenaar440cf092021-04-03 20:13:30 +02002471 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002472}
2473
2474/*
2475 * "complete_check()" function
2476 */
2477 void
2478f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2479{
2480 int saved = RedrawingDisabled;
2481
2482 RedrawingDisabled = 0;
2483 ins_compl_check_keys(0, TRUE);
2484 rettv->vval.v_number = ins_compl_interrupted();
2485 RedrawingDisabled = saved;
2486}
2487
2488/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002489 * Return Insert completion mode name string
2490 */
2491 static char_u *
2492ins_compl_mode(void)
2493{
2494 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
2495 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2496
2497 return (char_u *)"";
2498}
2499
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002500 static void
2501ins_compl_update_sequence_numbers()
2502{
2503 int number = 0;
2504 compl_T *match;
2505
2506 if (compl_direction == FORWARD)
2507 {
2508 // search backwards for the first valid (!= -1) number.
2509 // This should normally succeed already at the first loop
2510 // cycle, so it's fast!
2511 for (match = compl_curr_match->cp_prev; match != NULL
2512 && match != compl_first_match;
2513 match = match->cp_prev)
2514 if (match->cp_number != -1)
2515 {
2516 number = match->cp_number;
2517 break;
2518 }
2519 if (match != NULL)
2520 // go up and assign all numbers which are not assigned
2521 // yet
2522 for (match = match->cp_next;
2523 match != NULL && match->cp_number == -1;
2524 match = match->cp_next)
2525 match->cp_number = ++number;
2526 }
2527 else // BACKWARD
2528 {
2529 // search forwards (upwards) for the first valid (!= -1)
2530 // number. This should normally succeed already at the
2531 // first loop cycle, so it's fast!
2532 for (match = compl_curr_match->cp_next; match != NULL
2533 && match != compl_first_match;
2534 match = match->cp_next)
2535 if (match->cp_number != -1)
2536 {
2537 number = match->cp_number;
2538 break;
2539 }
2540 if (match != NULL)
2541 // go down and assign all numbers which are not
2542 // assigned yet
2543 for (match = match->cp_prev; match
2544 && match->cp_number == -1;
2545 match = match->cp_prev)
2546 match->cp_number = ++number;
2547 }
2548}
2549
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002550/*
2551 * Get complete information
2552 */
2553 static void
2554get_complete_info(list_T *what_list, dict_T *retdict)
2555{
2556 int ret = OK;
2557 listitem_T *item;
2558#define CI_WHAT_MODE 0x01
2559#define CI_WHAT_PUM_VISIBLE 0x02
2560#define CI_WHAT_ITEMS 0x04
2561#define CI_WHAT_SELECTED 0x08
2562#define CI_WHAT_INSERTED 0x10
2563#define CI_WHAT_ALL 0xff
2564 int what_flag;
2565
2566 if (what_list == NULL)
2567 what_flag = CI_WHAT_ALL;
2568 else
2569 {
2570 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002571 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002572 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002573 {
2574 char_u *what = tv_get_string(&item->li_tv);
2575
2576 if (STRCMP(what, "mode") == 0)
2577 what_flag |= CI_WHAT_MODE;
2578 else if (STRCMP(what, "pum_visible") == 0)
2579 what_flag |= CI_WHAT_PUM_VISIBLE;
2580 else if (STRCMP(what, "items") == 0)
2581 what_flag |= CI_WHAT_ITEMS;
2582 else if (STRCMP(what, "selected") == 0)
2583 what_flag |= CI_WHAT_SELECTED;
2584 else if (STRCMP(what, "inserted") == 0)
2585 what_flag |= CI_WHAT_INSERTED;
2586 }
2587 }
2588
2589 if (ret == OK && (what_flag & CI_WHAT_MODE))
2590 ret = dict_add_string(retdict, "mode", ins_compl_mode());
2591
2592 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
2593 ret = dict_add_number(retdict, "pum_visible", pum_visible());
2594
2595 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
2596 {
2597 list_T *li;
2598 dict_T *di;
2599 compl_T *match;
2600
2601 li = list_alloc();
2602 if (li == NULL)
2603 return;
2604 ret = dict_add_list(retdict, "items", li);
2605 if (ret == OK && compl_first_match != NULL)
2606 {
2607 match = compl_first_match;
2608 do
2609 {
2610 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
2611 {
2612 di = dict_alloc();
2613 if (di == NULL)
2614 return;
2615 ret = list_append_dict(li, di);
2616 if (ret != OK)
2617 return;
2618 dict_add_string(di, "word", match->cp_str);
2619 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
2620 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
2621 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
2622 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002623 if (match->cp_user_data.v_type == VAR_UNKNOWN)
2624 // Add an empty string for backwards compatibility
2625 dict_add_string(di, "user_data", (char_u *)"");
2626 else
2627 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002628 }
2629 match = match->cp_next;
2630 }
2631 while (match != NULL && match != compl_first_match);
2632 }
2633 }
2634
2635 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002636 {
2637 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
2638 ins_compl_update_sequence_numbers();
2639 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
2640 ? compl_curr_match->cp_number - 1 : -1);
2641 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002642
2643 // TODO
2644 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
2645}
2646
2647/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002648 * "complete_info()" function
2649 */
2650 void
2651f_complete_info(typval_T *argvars, typval_T *rettv)
2652{
2653 list_T *what_list = NULL;
2654
2655 if (rettv_dict_alloc(rettv) != OK)
2656 return;
2657
2658 if (argvars[0].v_type != VAR_UNKNOWN)
2659 {
2660 if (argvars[0].v_type != VAR_LIST)
2661 {
2662 emsg(_(e_listreq));
2663 return;
2664 }
2665 what_list = argvars[0].vval.v_list;
2666 }
2667 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002668}
2669#endif
2670
2671/*
2672 * Get the next expansion(s), using "compl_pattern".
2673 * The search starts at position "ini" in curbuf and in the direction
2674 * compl_direction.
2675 * When "compl_started" is FALSE start at that position, otherwise continue
2676 * where we stopped searching before.
2677 * This may return before finding all the matches.
2678 * Return the total number of matches or -1 if still unknown -- Acevedo
2679 */
2680 static int
2681ins_compl_get_exp(pos_T *ini)
2682{
2683 static pos_T first_match_pos;
2684 static pos_T last_match_pos;
2685 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2686 static int found_all = FALSE; // Found all matches of a
2687 // certain type.
2688 static buf_T *ins_buf = NULL; // buffer being scanned
2689
2690 pos_T *pos;
2691 char_u **matches;
2692 int save_p_scs;
2693 int save_p_ws;
2694 int save_p_ic;
2695 int i;
2696 int num_matches;
2697 int len;
2698 int found_new_match;
2699 int type = ctrl_x_mode;
2700 char_u *ptr;
2701 char_u *dict = NULL;
2702 int dict_f = 0;
2703 int set_match_pos;
2704
2705 if (!compl_started)
2706 {
2707 FOR_ALL_BUFFERS(ins_buf)
2708 ins_buf->b_scanned = 0;
2709 found_all = FALSE;
2710 ins_buf = curbuf;
2711 e_cpt = (compl_cont_status & CONT_LOCAL)
2712 ? (char_u *)"." : curbuf->b_p_cpt;
2713 last_match_pos = first_match_pos = *ini;
2714 }
2715 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2716 ins_buf = curbuf; // In case the buffer was wiped out.
2717
2718 compl_old_match = compl_curr_match; // remember the last current match
2719 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2720
2721 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2722 for (;;)
2723 {
2724 found_new_match = FAIL;
2725 set_match_pos = FALSE;
2726
2727 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2728 // or if found_all says this entry is done. For ^X^L only use the
2729 // entries from 'complete' that look in loaded buffers.
2730 if ((ctrl_x_mode == CTRL_X_NORMAL
2731 || ctrl_x_mode_line_or_eval())
2732 && (!compl_started || found_all))
2733 {
2734 found_all = FALSE;
2735 while (*e_cpt == ',' || *e_cpt == ' ')
2736 e_cpt++;
2737 if (*e_cpt == '.' && !curbuf->b_scanned)
2738 {
2739 ins_buf = curbuf;
2740 first_match_pos = *ini;
2741 // Move the cursor back one character so that ^N can match the
2742 // word immediately after the cursor.
2743 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2744 {
2745 // Move the cursor to after the last character in the
2746 // buffer, so that word at start of buffer is found
2747 // correctly.
2748 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2749 first_match_pos.col =
2750 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2751 }
2752 last_match_pos = first_match_pos;
2753 type = 0;
2754
2755 // Remember the first match so that the loop stops when we
2756 // wrap and come back there a second time.
2757 set_match_pos = TRUE;
2758 }
2759 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2760 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2761 {
2762 // Scan a buffer, but not the current one.
2763 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2764 {
2765 compl_started = TRUE;
2766 first_match_pos.col = last_match_pos.col = 0;
2767 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2768 last_match_pos.lnum = 0;
2769 type = 0;
2770 }
2771 else // unloaded buffer, scan like dictionary
2772 {
2773 found_all = TRUE;
2774 if (ins_buf->b_fname == NULL)
2775 continue;
2776 type = CTRL_X_DICTIONARY;
2777 dict = ins_buf->b_fname;
2778 dict_f = DICT_EXACT;
2779 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01002780 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002781 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2782 ins_buf->b_fname == NULL
2783 ? buf_spname(ins_buf)
2784 : ins_buf->b_sfname == NULL
2785 ? ins_buf->b_fname
2786 : ins_buf->b_sfname);
2787 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2788 }
2789 else if (*e_cpt == NUL)
2790 break;
2791 else
2792 {
2793 if (ctrl_x_mode_line_or_eval())
2794 type = -1;
2795 else if (*e_cpt == 'k' || *e_cpt == 's')
2796 {
2797 if (*e_cpt == 'k')
2798 type = CTRL_X_DICTIONARY;
2799 else
2800 type = CTRL_X_THESAURUS;
2801 if (*++e_cpt != ',' && *e_cpt != NUL)
2802 {
2803 dict = e_cpt;
2804 dict_f = DICT_FIRST;
2805 }
2806 }
2807#ifdef FEAT_FIND_ID
2808 else if (*e_cpt == 'i')
2809 type = CTRL_X_PATH_PATTERNS;
2810 else if (*e_cpt == 'd')
2811 type = CTRL_X_PATH_DEFINES;
2812#endif
2813 else if (*e_cpt == ']' || *e_cpt == 't')
2814 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01002815 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002816 type = CTRL_X_TAGS;
2817 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2818 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2819 }
2820 else
2821 type = -1;
2822
2823 // in any case e_cpt is advanced to the next entry
2824 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2825
2826 found_all = TRUE;
2827 if (type == -1)
2828 continue;
2829 }
2830 }
2831
2832 // If complete() was called then compl_pattern has been reset. The
2833 // following won't work then, bail out.
2834 if (compl_pattern == NULL)
2835 break;
2836
2837 switch (type)
2838 {
2839 case -1:
2840 break;
2841#ifdef FEAT_FIND_ID
2842 case CTRL_X_PATH_PATTERNS:
2843 case CTRL_X_PATH_DEFINES:
2844 find_pattern_in_path(compl_pattern, compl_direction,
2845 (int)STRLEN(compl_pattern), FALSE, FALSE,
2846 (type == CTRL_X_PATH_DEFINES
2847 && !(compl_cont_status & CONT_SOL))
2848 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2849 (linenr_T)1, (linenr_T)MAXLNUM);
2850 break;
2851#endif
2852
2853 case CTRL_X_DICTIONARY:
2854 case CTRL_X_THESAURUS:
2855 ins_compl_dictionaries(
2856 dict != NULL ? dict
2857 : (type == CTRL_X_THESAURUS
2858 ? (*curbuf->b_p_tsr == NUL
2859 ? p_tsr
2860 : curbuf->b_p_tsr)
2861 : (*curbuf->b_p_dict == NUL
2862 ? p_dict
2863 : curbuf->b_p_dict)),
2864 compl_pattern,
2865 dict != NULL ? dict_f
2866 : 0, type == CTRL_X_THESAURUS);
2867 dict = NULL;
2868 break;
2869
2870 case CTRL_X_TAGS:
2871 // set p_ic according to p_ic, p_scs and pat for find_tags().
2872 save_p_ic = p_ic;
2873 p_ic = ignorecase(compl_pattern);
2874
2875 // Find up to TAG_MANY matches. Avoids that an enormous number
2876 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002877 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002878 if (find_tags(compl_pattern, &num_matches, &matches,
2879 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2880 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2881 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002882 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002883 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002884 p_ic = save_p_ic;
2885 break;
2886
2887 case CTRL_X_FILES:
2888 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2889 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2890 {
2891
2892 // May change home directory back to "~".
2893 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaarac3150d2019-07-28 16:36:39 +02002894#ifdef BACKSLASH_IN_FILENAME
2895 if (curbuf->b_p_csl[0] != NUL)
2896 {
2897 int i;
2898
2899 for (i = 0; i < num_matches; ++i)
2900 {
2901 char_u *ptr = matches[i];
2902
2903 while (*ptr != NUL)
2904 {
2905 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
2906 *ptr = '/';
2907 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
2908 *ptr = '\\';
2909 ptr += (*mb_ptr2len)(ptr);
2910 }
2911 }
2912 }
2913#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002914 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2915 }
2916 break;
2917
2918 case CTRL_X_CMDLINE:
2919 if (expand_cmdline(&compl_xp, compl_pattern,
2920 (int)STRLEN(compl_pattern),
2921 &num_matches, &matches) == EXPAND_OK)
2922 ins_compl_add_matches(num_matches, matches, FALSE);
2923 break;
2924
2925#ifdef FEAT_COMPL_FUNC
2926 case CTRL_X_FUNCTION:
2927 case CTRL_X_OMNI:
2928 expand_by_function(type, compl_pattern);
2929 break;
2930#endif
2931
2932 case CTRL_X_SPELL:
2933#ifdef FEAT_SPELL
2934 num_matches = expand_spelling(first_match_pos.lnum,
2935 compl_pattern, &matches);
2936 if (num_matches > 0)
2937 ins_compl_add_matches(num_matches, matches, p_ic);
2938#endif
2939 break;
2940
2941 default: // normal ^P/^N and ^X^L
2942 // If 'infercase' is set, don't use 'smartcase' here
2943 save_p_scs = p_scs;
2944 if (ins_buf->b_p_inf)
2945 p_scs = FALSE;
2946
2947 // Buffers other than curbuf are scanned from the beginning or the
2948 // end but never from the middle, thus setting nowrapscan in this
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002949 // buffer is a good idea, on the other hand, we always set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002950 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
2951 save_p_ws = p_ws;
2952 if (ins_buf != curbuf)
2953 p_ws = FALSE;
2954 else if (*e_cpt == '.')
2955 p_ws = TRUE;
2956 for (;;)
2957 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02002958 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002959
2960 ++msg_silent; // Don't want messages for wrapscan.
2961
2962 // ctrl_x_mode_line_or_eval() || word-wise search that
2963 // has added a word that was at the beginning of the line
2964 if (ctrl_x_mode_line_or_eval()
2965 || (compl_cont_status & CONT_SOL))
2966 found_new_match = search_for_exact_line(ins_buf, pos,
2967 compl_direction, compl_pattern);
2968 else
2969 found_new_match = searchit(NULL, ins_buf, pos, NULL,
2970 compl_direction,
2971 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002972 RE_LAST, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002973 --msg_silent;
2974 if (!compl_started || set_match_pos)
2975 {
2976 // set "compl_started" even on fail
2977 compl_started = TRUE;
2978 first_match_pos = *pos;
2979 last_match_pos = *pos;
2980 set_match_pos = FALSE;
2981 }
2982 else if (first_match_pos.lnum == last_match_pos.lnum
2983 && first_match_pos.col == last_match_pos.col)
2984 found_new_match = FAIL;
2985 if (found_new_match == FAIL)
2986 {
2987 if (ins_buf == curbuf)
2988 found_all = TRUE;
2989 break;
2990 }
2991
2992 // when ADDING, the text before the cursor matches, skip it
2993 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
2994 && ini->lnum == pos->lnum
2995 && ini->col == pos->col)
2996 continue;
2997 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
2998 if (ctrl_x_mode_line_or_eval())
2999 {
3000 if (compl_cont_status & CONT_ADDING)
3001 {
3002 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3003 continue;
3004 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3005 if (!p_paste)
3006 ptr = skipwhite(ptr);
3007 }
3008 len = (int)STRLEN(ptr);
3009 }
3010 else
3011 {
3012 char_u *tmp_ptr = ptr;
3013
3014 if (compl_cont_status & CONT_ADDING)
3015 {
3016 tmp_ptr += compl_length;
3017 // Skip if already inside a word.
3018 if (vim_iswordp(tmp_ptr))
3019 continue;
3020 // Find start of next word.
3021 tmp_ptr = find_word_start(tmp_ptr);
3022 }
3023 // Find end of this word.
3024 tmp_ptr = find_word_end(tmp_ptr);
3025 len = (int)(tmp_ptr - ptr);
3026
3027 if ((compl_cont_status & CONT_ADDING)
3028 && len == compl_length)
3029 {
3030 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3031 {
3032 // Try next line, if any. the new word will be
3033 // "join" as if the normal command "J" was used.
3034 // IOSIZE is always greater than
3035 // compl_length, so the next STRNCPY always
3036 // works -- Acevedo
3037 STRNCPY(IObuff, ptr, len);
3038 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3039 tmp_ptr = ptr = skipwhite(ptr);
3040 // Find start of next word.
3041 tmp_ptr = find_word_start(tmp_ptr);
3042 // Find end of next word.
3043 tmp_ptr = find_word_end(tmp_ptr);
3044 if (tmp_ptr > ptr)
3045 {
3046 if (*ptr != ')' && IObuff[len - 1] != TAB)
3047 {
3048 if (IObuff[len - 1] != ' ')
3049 IObuff[len++] = ' ';
3050 // IObuf =~ "\k.* ", thus len >= 2
3051 if (p_js
3052 && (IObuff[len - 2] == '.'
3053 || (vim_strchr(p_cpo, CPO_JOINSP)
3054 == NULL
3055 && (IObuff[len - 2] == '?'
3056 || IObuff[len - 2] == '!'))))
3057 IObuff[len++] = ' ';
3058 }
3059 // copy as much as possible of the new word
3060 if (tmp_ptr - ptr >= IOSIZE - len)
3061 tmp_ptr = ptr + IOSIZE - len - 1;
3062 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3063 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003064 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003065 }
3066 IObuff[len] = NUL;
3067 ptr = IObuff;
3068 }
3069 if (len == compl_length)
3070 continue;
3071 }
3072 }
3073 if (ins_compl_add_infercase(ptr, len, p_ic,
3074 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003075 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003076 {
3077 found_new_match = OK;
3078 break;
3079 }
3080 }
3081 p_scs = save_p_scs;
3082 p_ws = save_p_ws;
3083 }
3084
3085 // check if compl_curr_match has changed, (e.g. other type of
3086 // expansion added something)
3087 if (type != 0 && compl_curr_match != compl_old_match)
3088 found_new_match = OK;
3089
3090 // break the loop for specialized modes (use 'complete' just for the
3091 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3092 // match
3093 if ((ctrl_x_mode != CTRL_X_NORMAL
3094 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3095 {
3096 if (got_int)
3097 break;
3098 // Fill the popup menu as soon as possible.
3099 if (type != -1)
3100 ins_compl_check_keys(0, FALSE);
3101
3102 if ((ctrl_x_mode != CTRL_X_NORMAL
3103 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3104 break;
3105 compl_started = TRUE;
3106 }
3107 else
3108 {
3109 // Mark a buffer scanned when it has been scanned completely
3110 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3111 ins_buf->b_scanned = TRUE;
3112
3113 compl_started = FALSE;
3114 }
3115 }
3116 compl_started = TRUE;
3117
3118 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
3119 && *e_cpt == NUL) // Got to end of 'complete'
3120 found_new_match = FAIL;
3121
3122 i = -1; // total of matches, unknown
3123 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3124 && !ctrl_x_mode_line_or_eval()))
3125 i = ins_compl_make_cyclic();
3126
3127 if (compl_old_match != NULL)
3128 {
3129 // If several matches were added (FORWARD) or the search failed and has
3130 // just been made cyclic then we have to move compl_curr_match to the
3131 // next or previous entry (if any) -- Acevedo
3132 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3133 : compl_old_match->cp_prev;
3134 if (compl_curr_match == NULL)
3135 compl_curr_match = compl_old_match;
3136 }
3137 return i;
3138}
3139
3140/*
3141 * Delete the old text being completed.
3142 */
3143 void
3144ins_compl_delete(void)
3145{
3146 int col;
3147
3148 // In insert mode: Delete the typed part.
3149 // In replace mode: Put the old characters back, if any.
3150 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3151 if ((int)curwin->w_cursor.col > col)
3152 {
3153 if (stop_arrow() == FAIL)
3154 return;
3155 backspace_until_column(col);
3156 }
3157
3158 // TODO: is this sufficient for redrawing? Redrawing everything causes
3159 // flicker, thus we can't do that.
3160 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003161#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003162 // clear v:completed_item
3163 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003164#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003165}
3166
3167/*
3168 * Insert the new text being completed.
3169 * "in_compl_func" is TRUE when called from complete_check().
3170 */
3171 void
3172ins_compl_insert(int in_compl_func)
3173{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003174 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003175 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003176 compl_used_match = FALSE;
3177 else
3178 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003179#ifdef FEAT_EVAL
3180 {
3181 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3182
3183 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3184 }
3185#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003186 if (!in_compl_func)
3187 compl_curr_match = compl_shown_match;
3188}
3189
3190/*
3191 * Fill in the next completion in the current direction.
3192 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3193 * get more completions. If it is FALSE, then we just do nothing when there
3194 * are no more completions in a given direction. The latter case is used when
3195 * we are still in the middle of finding completions, to allow browsing
3196 * through the ones found so far.
3197 * Return the total number of matches, or -1 if still unknown -- webb.
3198 *
3199 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3200 * compl_shown_match here.
3201 *
3202 * Note that this function may be called recursively once only. First with
3203 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3204 * calls this function with "allow_get_expansion" FALSE.
3205 */
3206 static int
3207ins_compl_next(
3208 int allow_get_expansion,
3209 int count, // repeat completion this many times; should
3210 // be at least 1
3211 int insert_match, // Insert the newly selected match
3212 int in_compl_func) // called from complete_check()
3213{
3214 int num_matches = -1;
3215 int todo = count;
3216 compl_T *found_compl = NULL;
3217 int found_end = FALSE;
3218 int advance;
3219 int started = compl_started;
3220
3221 // When user complete function return -1 for findstart which is next
3222 // time of 'always', compl_shown_match become NULL.
3223 if (compl_shown_match == NULL)
3224 return -1;
3225
3226 if (compl_leader != NULL
Bram Moolenaar28976e22021-01-29 21:07:07 +01003227 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003228 {
3229 // Set "compl_shown_match" to the actually shown match, it may differ
3230 // when "compl_leader" is used to omit some of the matches.
3231 while (!ins_compl_equal(compl_shown_match,
3232 compl_leader, (int)STRLEN(compl_leader))
3233 && compl_shown_match->cp_next != NULL
3234 && compl_shown_match->cp_next != compl_first_match)
3235 compl_shown_match = compl_shown_match->cp_next;
3236
3237 // If we didn't find it searching forward, and compl_shows_dir is
3238 // backward, find the last match.
3239 if (compl_shows_dir == BACKWARD
3240 && !ins_compl_equal(compl_shown_match,
3241 compl_leader, (int)STRLEN(compl_leader))
3242 && (compl_shown_match->cp_next == NULL
3243 || compl_shown_match->cp_next == compl_first_match))
3244 {
3245 while (!ins_compl_equal(compl_shown_match,
3246 compl_leader, (int)STRLEN(compl_leader))
3247 && compl_shown_match->cp_prev != NULL
3248 && compl_shown_match->cp_prev != compl_first_match)
3249 compl_shown_match = compl_shown_match->cp_prev;
3250 }
3251 }
3252
3253 if (allow_get_expansion && insert_match
3254 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3255 // Delete old text to be replaced
3256 ins_compl_delete();
3257
3258 // When finding the longest common text we stick at the original text,
3259 // don't let CTRL-N or CTRL-P move to the first match.
3260 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3261
3262 // When restarting the search don't insert the first match either.
3263 if (compl_restarting)
3264 {
3265 advance = FALSE;
3266 compl_restarting = FALSE;
3267 }
3268
3269 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3270 // around.
3271 while (--todo >= 0)
3272 {
3273 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3274 {
3275 compl_shown_match = compl_shown_match->cp_next;
3276 found_end = (compl_first_match != NULL
3277 && (compl_shown_match->cp_next == compl_first_match
3278 || compl_shown_match == compl_first_match));
3279 }
3280 else if (compl_shows_dir == BACKWARD
3281 && compl_shown_match->cp_prev != NULL)
3282 {
3283 found_end = (compl_shown_match == compl_first_match);
3284 compl_shown_match = compl_shown_match->cp_prev;
3285 found_end |= (compl_shown_match == compl_first_match);
3286 }
3287 else
3288 {
3289 if (!allow_get_expansion)
3290 {
3291 if (advance)
3292 {
3293 if (compl_shows_dir == BACKWARD)
3294 compl_pending -= todo + 1;
3295 else
3296 compl_pending += todo + 1;
3297 }
3298 return -1;
3299 }
3300
3301 if (!compl_no_select && advance)
3302 {
3303 if (compl_shows_dir == BACKWARD)
3304 --compl_pending;
3305 else
3306 ++compl_pending;
3307 }
3308
3309 // Find matches.
3310 num_matches = ins_compl_get_exp(&compl_startpos);
3311
3312 // handle any pending completions
3313 while (compl_pending != 0 && compl_direction == compl_shows_dir
3314 && advance)
3315 {
3316 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3317 {
3318 compl_shown_match = compl_shown_match->cp_next;
3319 --compl_pending;
3320 }
3321 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3322 {
3323 compl_shown_match = compl_shown_match->cp_prev;
3324 ++compl_pending;
3325 }
3326 else
3327 break;
3328 }
3329 found_end = FALSE;
3330 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003331 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003332 && compl_leader != NULL
3333 && !ins_compl_equal(compl_shown_match,
3334 compl_leader, (int)STRLEN(compl_leader)))
3335 ++todo;
3336 else
3337 // Remember a matching item.
3338 found_compl = compl_shown_match;
3339
3340 // Stop at the end of the list when we found a usable match.
3341 if (found_end)
3342 {
3343 if (found_compl != NULL)
3344 {
3345 compl_shown_match = found_compl;
3346 break;
3347 }
3348 todo = 1; // use first usable match after wrapping around
3349 }
3350 }
3351
3352 // Insert the text of the new completion, or the compl_leader.
3353 if (compl_no_insert && !started)
3354 {
3355 ins_bytes(compl_orig_text + ins_compl_len());
3356 compl_used_match = FALSE;
3357 }
3358 else if (insert_match)
3359 {
3360 if (!compl_get_longest || compl_used_match)
3361 ins_compl_insert(in_compl_func);
3362 else
3363 ins_bytes(compl_leader + ins_compl_len());
3364 }
3365 else
3366 compl_used_match = FALSE;
3367
3368 if (!allow_get_expansion)
3369 {
3370 // may undisplay the popup menu first
3371 ins_compl_upd_pum();
3372
3373 if (pum_enough_matches())
3374 // Will display the popup menu, don't redraw yet to avoid flicker.
3375 pum_call_update_screen();
3376 else
3377 // Not showing the popup menu yet, redraw to show the user what was
3378 // inserted.
3379 update_screen(0);
3380
3381 // display the updated popup menu
3382 ins_compl_show_pum();
3383#ifdef FEAT_GUI
3384 if (gui.in_use)
3385 {
3386 // Show the cursor after the match, not after the redrawn text.
3387 setcursor();
3388 out_flush_cursor(FALSE, FALSE);
3389 }
3390#endif
3391
3392 // Delete old text to be replaced, since we're still searching and
3393 // don't want to match ourselves!
3394 ins_compl_delete();
3395 }
3396
3397 // Enter will select a match when the match wasn't inserted and the popup
3398 // menu is visible.
3399 if (compl_no_insert && !started)
3400 compl_enter_selects = TRUE;
3401 else
3402 compl_enter_selects = !insert_match && compl_match_array != NULL;
3403
3404 // Show the file name for the match (if any)
3405 // Truncate the file name to avoid a wait for return.
3406 if (compl_shown_match->cp_fname != NULL)
3407 {
3408 char *lead = _("match in file");
3409 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3410 char_u *s;
3411 char_u *e;
3412
3413 if (space > 0)
3414 {
3415 // We need the tail that fits. With double-byte encoding going
3416 // back from the end is very slow, thus go from the start and keep
3417 // the text that fits in "space" between "s" and "e".
3418 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3419 {
3420 space -= ptr2cells(e);
3421 while (space < 0)
3422 {
3423 space += ptr2cells(s);
3424 MB_PTR_ADV(s);
3425 }
3426 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01003427 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003428 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3429 s > compl_shown_match->cp_fname ? "<" : "", s);
3430 msg((char *)IObuff);
Bram Moolenaarcc233582020-12-12 13:32:07 +01003431 msg_hist_off = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003432 redraw_cmdline = FALSE; // don't overwrite!
3433 }
3434 }
3435
3436 return num_matches;
3437}
3438
3439/*
3440 * Call this while finding completions, to check whether the user has hit a key
3441 * that should change the currently displayed completion, or exit completion
3442 * mode. Also, when compl_pending is not zero, show a completion as soon as
3443 * possible. -- webb
3444 * "frequency" specifies out of how many calls we actually check.
3445 * "in_compl_func" is TRUE when called from complete_check(), don't set
3446 * compl_curr_match.
3447 */
3448 void
3449ins_compl_check_keys(int frequency, int in_compl_func)
3450{
3451 static int count = 0;
3452 int c;
3453
3454 // Don't check when reading keys from a script, :normal or feedkeys().
3455 // That would break the test scripts. But do check for keys when called
3456 // from complete_check().
3457 if (!in_compl_func && (using_script() || ex_normal_busy))
3458 return;
3459
3460 // Only do this at regular intervals
3461 if (++count < frequency)
3462 return;
3463 count = 0;
3464
3465 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3466 // can't do its work correctly.
3467 c = vpeekc_any();
3468 if (c != NUL)
3469 {
3470 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3471 {
3472 c = safe_vgetc(); // Eat the character
3473 compl_shows_dir = ins_compl_key2dir(c);
3474 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3475 c != K_UP && c != K_DOWN, in_compl_func);
3476 }
3477 else
3478 {
3479 // Need to get the character to have KeyTyped set. We'll put it
3480 // back with vungetc() below. But skip K_IGNORE.
3481 c = safe_vgetc();
3482 if (c != K_IGNORE)
3483 {
3484 // Don't interrupt completion when the character wasn't typed,
3485 // e.g., when doing @q to replay keys.
3486 if (c != Ctrl_R && KeyTyped)
3487 compl_interrupted = TRUE;
3488
3489 vungetc(c);
3490 }
3491 }
3492 }
3493 if (compl_pending != 0 && !got_int && !compl_no_insert)
3494 {
3495 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3496
3497 compl_pending = 0;
3498 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3499 }
3500}
3501
3502/*
3503 * Decide the direction of Insert mode complete from the key typed.
3504 * Returns BACKWARD or FORWARD.
3505 */
3506 static int
3507ins_compl_key2dir(int c)
3508{
3509 if (c == Ctrl_P || c == Ctrl_L
3510 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3511 return BACKWARD;
3512 return FORWARD;
3513}
3514
3515/*
3516 * Return TRUE for keys that are used for completion only when the popup menu
3517 * is visible.
3518 */
3519 static int
3520ins_compl_pum_key(int c)
3521{
3522 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3523 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3524 || c == K_UP || c == K_DOWN);
3525}
3526
3527/*
3528 * Decide the number of completions to move forward.
3529 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3530 */
3531 static int
3532ins_compl_key2count(int c)
3533{
3534 int h;
3535
3536 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3537 {
3538 h = pum_get_height();
3539 if (h > 3)
3540 h -= 2; // keep some context
3541 return h;
3542 }
3543 return 1;
3544}
3545
3546/*
3547 * Return TRUE if completion with "c" should insert the match, FALSE if only
3548 * to change the currently selected completion.
3549 */
3550 static int
3551ins_compl_use_match(int c)
3552{
3553 switch (c)
3554 {
3555 case K_UP:
3556 case K_DOWN:
3557 case K_PAGEDOWN:
3558 case K_KPAGEDOWN:
3559 case K_S_DOWN:
3560 case K_PAGEUP:
3561 case K_KPAGEUP:
3562 case K_S_UP:
3563 return FALSE;
3564 }
3565 return TRUE;
3566}
3567
3568/*
3569 * Do Insert mode completion.
3570 * Called when character "c" was typed, which has a meaning for completion.
3571 * Returns OK if completion was done, FAIL if something failed (out of mem).
3572 */
3573 int
3574ins_complete(int c, int enable_pum)
3575{
3576 char_u *line;
3577 int startcol = 0; // column where searched text starts
3578 colnr_T curs_col; // cursor column
3579 int n;
3580 int save_w_wrow;
3581 int save_w_leftcol;
3582 int insert_match;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003583#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003584 int save_did_ai = did_ai;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003585#endif
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003586 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003587
3588 compl_direction = ins_compl_key2dir(c);
3589 insert_match = ins_compl_use_match(c);
3590
3591 if (!compl_started)
3592 {
3593 // First time we hit ^N or ^P (in a row, I mean)
3594
3595 did_ai = FALSE;
3596#ifdef FEAT_SMARTINDENT
3597 did_si = FALSE;
3598 can_si = FALSE;
3599 can_si_back = FALSE;
3600#endif
3601 if (stop_arrow() == FAIL)
3602 return FAIL;
3603
3604 line = ml_get(curwin->w_cursor.lnum);
3605 curs_col = curwin->w_cursor.col;
3606 compl_pending = 0;
3607
3608 // If this same ctrl_x_mode has been interrupted use the text from
3609 // "compl_startpos" to the cursor as a pattern to add a new word
3610 // instead of expand the one before the cursor, in word-wise if
3611 // "compl_startpos" is not in the same line as the cursor then fix it
3612 // (the line has been split because it was longer than 'tw'). if SOL
3613 // is set then skip the previous pattern, a word at the beginning of
3614 // the line has been inserted, we'll look for that -- Acevedo.
3615 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3616 && compl_cont_mode == ctrl_x_mode)
3617 {
3618 // it is a continued search
3619 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3620 if (ctrl_x_mode == CTRL_X_NORMAL
3621 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3622 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3623 {
3624 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3625 {
3626 // line (probably) wrapped, set compl_startpos to the
3627 // first non_blank in the line, if it is not a wordchar
3628 // include it to get a better pattern, but then we don't
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003629 // want the "\\<" prefix, check it below
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003630 compl_col = (colnr_T)getwhitecols(line);
3631 compl_startpos.col = compl_col;
3632 compl_startpos.lnum = curwin->w_cursor.lnum;
3633 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3634 }
3635 else
3636 {
3637 // S_IPOS was set when we inserted a word that was at the
3638 // beginning of the line, which means that we'll go to SOL
3639 // mode but first we need to redefine compl_startpos
3640 if (compl_cont_status & CONT_S_IPOS)
3641 {
3642 compl_cont_status |= CONT_SOL;
3643 compl_startpos.col = (colnr_T)(skipwhite(
3644 line + compl_length
3645 + compl_startpos.col) - line);
3646 }
3647 compl_col = compl_startpos.col;
3648 }
3649 compl_length = curwin->w_cursor.col - (int)compl_col;
3650 // IObuff is used to add a "word from the next line" would we
3651 // have enough space? just being paranoid
3652#define MIN_SPACE 75
3653 if (compl_length > (IOSIZE - MIN_SPACE))
3654 {
3655 compl_cont_status &= ~CONT_SOL;
3656 compl_length = (IOSIZE - MIN_SPACE);
3657 compl_col = curwin->w_cursor.col - compl_length;
3658 }
3659 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3660 if (compl_length < 1)
3661 compl_cont_status &= CONT_LOCAL;
3662 }
3663 else if (ctrl_x_mode_line_or_eval())
3664 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3665 else
3666 compl_cont_status = 0;
3667 }
3668 else
3669 compl_cont_status &= CONT_LOCAL;
3670
3671 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3672 {
3673 compl_cont_mode = ctrl_x_mode;
3674 if (ctrl_x_mode != CTRL_X_NORMAL)
3675 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3676 compl_cont_status = 0;
3677 compl_cont_status |= CONT_N_ADDS;
3678 compl_startpos = curwin->w_cursor;
3679 startcol = (int)curs_col;
3680 compl_col = 0;
3681 }
3682
3683 // Work out completion pattern and original text -- webb
3684 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3685 {
3686 if ((compl_cont_status & CONT_SOL)
3687 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3688 {
3689 if (!(compl_cont_status & CONT_ADDING))
3690 {
3691 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3692 ;
3693 compl_col += ++startcol;
3694 compl_length = curs_col - startcol;
3695 }
3696 if (p_ic)
3697 compl_pattern = str_foldcase(line + compl_col,
3698 compl_length, NULL, 0);
3699 else
3700 compl_pattern = vim_strnsave(line + compl_col,
3701 compl_length);
3702 if (compl_pattern == NULL)
3703 return FAIL;
3704 }
3705 else if (compl_cont_status & CONT_ADDING)
3706 {
3707 char_u *prefix = (char_u *)"\\<";
3708
3709 // we need up to 2 extra chars for the prefix
3710 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3711 compl_length) + 2);
3712 if (compl_pattern == NULL)
3713 return FAIL;
3714 if (!vim_iswordp(line + compl_col)
3715 || (compl_col > 0
3716 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3717 prefix = (char_u *)"";
3718 STRCPY((char *)compl_pattern, prefix);
3719 (void)quote_meta(compl_pattern + STRLEN(prefix),
3720 line + compl_col, compl_length);
3721 }
3722 else if (--startcol < 0
3723 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3724 {
3725 // Match any word of at least two chars
3726 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3727 if (compl_pattern == NULL)
3728 return FAIL;
3729 compl_col += curs_col;
3730 compl_length = 0;
3731 }
3732 else
3733 {
3734 // Search the point of change class of multibyte character
3735 // or not a word single byte character backward.
3736 if (has_mbyte)
3737 {
3738 int base_class;
3739 int head_off;
3740
3741 startcol -= (*mb_head_off)(line, line + startcol);
3742 base_class = mb_get_class(line + startcol);
3743 while (--startcol >= 0)
3744 {
3745 head_off = (*mb_head_off)(line, line + startcol);
3746 if (base_class != mb_get_class(line + startcol
3747 - head_off))
3748 break;
3749 startcol -= head_off;
3750 }
3751 }
3752 else
3753 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3754 ;
3755 compl_col += ++startcol;
3756 compl_length = (int)curs_col - startcol;
3757 if (compl_length == 1)
3758 {
3759 // Only match word with at least two chars -- webb
3760 // there's no need to call quote_meta,
3761 // alloc(7) is enough -- Acevedo
3762 compl_pattern = alloc(7);
3763 if (compl_pattern == NULL)
3764 return FAIL;
3765 STRCPY((char *)compl_pattern, "\\<");
3766 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3767 STRCAT((char *)compl_pattern, "\\k");
3768 }
3769 else
3770 {
3771 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3772 compl_length) + 2);
3773 if (compl_pattern == NULL)
3774 return FAIL;
3775 STRCPY((char *)compl_pattern, "\\<");
3776 (void)quote_meta(compl_pattern + 2, line + compl_col,
3777 compl_length);
3778 }
3779 }
3780 }
3781 else if (ctrl_x_mode_line_or_eval())
3782 {
3783 compl_col = (colnr_T)getwhitecols(line);
3784 compl_length = (int)curs_col - (int)compl_col;
3785 if (compl_length < 0) // cursor in indent: empty pattern
3786 compl_length = 0;
3787 if (p_ic)
3788 compl_pattern = str_foldcase(line + compl_col, compl_length,
3789 NULL, 0);
3790 else
3791 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3792 if (compl_pattern == NULL)
3793 return FAIL;
3794 }
3795 else if (ctrl_x_mode == CTRL_X_FILES)
3796 {
3797 // Go back to just before the first filename character.
3798 if (startcol > 0)
3799 {
3800 char_u *p = line + startcol;
3801
3802 MB_PTR_BACK(line, p);
3803 while (p > line && vim_isfilec(PTR2CHAR(p)))
3804 MB_PTR_BACK(line, p);
3805 if (p == line && vim_isfilec(PTR2CHAR(p)))
3806 startcol = 0;
3807 else
3808 startcol = (int)(p - line) + 1;
3809 }
3810
3811 compl_col += startcol;
3812 compl_length = (int)curs_col - startcol;
3813 compl_pattern = addstar(line + compl_col, compl_length,
3814 EXPAND_FILES);
3815 if (compl_pattern == NULL)
3816 return FAIL;
3817 }
3818 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3819 {
3820 compl_pattern = vim_strnsave(line, curs_col);
3821 if (compl_pattern == NULL)
3822 return FAIL;
3823 set_cmd_context(&compl_xp, compl_pattern,
3824 (int)STRLEN(compl_pattern), curs_col, FALSE);
3825 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3826 || compl_xp.xp_context == EXPAND_NOTHING)
3827 // No completion possible, use an empty pattern to get a
3828 // "pattern not found" message.
3829 compl_col = curs_col;
3830 else
3831 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3832 compl_length = curs_col - compl_col;
3833 }
3834 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3835 {
3836#ifdef FEAT_COMPL_FUNC
3837 // Call user defined function 'completefunc' with "a:findstart"
3838 // set to 1 to obtain the length of text to use for completion.
3839 typval_T args[3];
3840 int col;
3841 char_u *funcname;
3842 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003843 int save_State = State;
3844
3845 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3846 // string
3847 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3848 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3849 if (*funcname == NUL)
3850 {
3851 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3852 ? "completefunc" : "omnifunc");
3853 // restore did_ai, so that adding comment leader works
3854 did_ai = save_did_ai;
3855 return FAIL;
3856 }
3857
3858 args[0].v_type = VAR_NUMBER;
3859 args[0].vval.v_number = 1;
3860 args[1].v_type = VAR_STRING;
3861 args[1].vval.v_string = (char_u *)"";
3862 args[2].v_type = VAR_UNKNOWN;
3863 pos = curwin->w_cursor;
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003864 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003865 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003866 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003867
3868 State = save_State;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003869 curwin->w_cursor = pos; // restore the cursor position
3870 validate_cursor();
3871 if (!EQUAL_POS(curwin->w_cursor, pos))
3872 {
3873 emsg(_(e_compldel));
3874 return FAIL;
3875 }
3876
3877 // Return value -2 means the user complete function wants to
3878 // cancel the complete without an error.
3879 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3880 if (col == -2)
3881 return FAIL;
3882 if (col == -3)
3883 {
3884 ctrl_x_mode = CTRL_X_NORMAL;
3885 edit_submode = NULL;
3886 if (!shortmess(SHM_COMPLETIONMENU))
3887 msg_clr_cmdline();
3888 return FAIL;
3889 }
3890
3891 // Reset extended parameters of completion, when start new
3892 // completion.
3893 compl_opt_refresh_always = FALSE;
3894 compl_opt_suppress_empty = FALSE;
3895
3896 if (col < 0)
3897 col = curs_col;
3898 compl_col = col;
3899 if (compl_col > curs_col)
3900 compl_col = curs_col;
3901
3902 // Setup variables for completion. Need to obtain "line" again,
3903 // it may have become invalid.
3904 line = ml_get(curwin->w_cursor.lnum);
3905 compl_length = curs_col - compl_col;
3906 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3907 if (compl_pattern == NULL)
3908#endif
3909 return FAIL;
3910 }
3911 else if (ctrl_x_mode == CTRL_X_SPELL)
3912 {
3913#ifdef FEAT_SPELL
3914 if (spell_bad_len > 0)
3915 compl_col = curs_col - spell_bad_len;
3916 else
3917 compl_col = spell_word_start(startcol);
3918 if (compl_col >= (colnr_T)startcol)
3919 {
3920 compl_length = 0;
3921 compl_col = curs_col;
3922 }
3923 else
3924 {
3925 spell_expand_check_cap(compl_col);
3926 compl_length = (int)curs_col - compl_col;
3927 }
3928 // Need to obtain "line" again, it may have become invalid.
3929 line = ml_get(curwin->w_cursor.lnum);
3930 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3931 if (compl_pattern == NULL)
3932#endif
3933 return FAIL;
3934 }
3935 else
3936 {
3937 internal_error("ins_complete()");
3938 return FAIL;
3939 }
3940
3941 if (compl_cont_status & CONT_ADDING)
3942 {
3943 edit_submode_pre = (char_u *)_(" Adding");
3944 if (ctrl_x_mode_line_or_eval())
3945 {
3946 // Insert a new line, keep indentation but ignore 'comments'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 char_u *old = curbuf->b_p_com;
3948
3949 curbuf->b_p_com = (char_u *)"";
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003950 compl_startpos.lnum = curwin->w_cursor.lnum;
3951 compl_startpos.col = compl_col;
3952 ins_eol('\r');
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003953 curbuf->b_p_com = old;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003954 compl_length = 0;
3955 compl_col = curwin->w_cursor.col;
3956 }
3957 }
3958 else
3959 {
3960 edit_submode_pre = NULL;
3961 compl_startpos.col = compl_col;
3962 }
3963
3964 if (compl_cont_status & CONT_LOCAL)
3965 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
3966 else
3967 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3968
3969 // If any of the original typed text has been changed we need to fix
3970 // the redo buffer.
3971 ins_compl_fixRedoBufForLeader(NULL);
3972
3973 // Always add completion for the original text.
3974 vim_free(compl_orig_text);
3975 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003976 if (p_ic)
3977 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003978 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar08928322020-01-04 14:32:48 +01003979 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003980 {
3981 VIM_CLEAR(compl_pattern);
3982 VIM_CLEAR(compl_orig_text);
3983 return FAIL;
3984 }
3985
3986 // showmode might reset the internal line pointers, so it must
3987 // be called before line = ml_get(), or when this address is no
3988 // longer needed. -- Acevedo.
3989 edit_submode_extra = (char_u *)_("-- Searching...");
3990 edit_submode_highl = HLF_COUNT;
3991 showmode();
3992 edit_submode_extra = NULL;
3993 out_flush();
3994 }
3995 else if (insert_match && stop_arrow() == FAIL)
3996 return FAIL;
3997
3998 compl_shown_match = compl_curr_match;
3999 compl_shows_dir = compl_direction;
4000
4001 // Find next match (and following matches).
4002 save_w_wrow = curwin->w_wrow;
4003 save_w_leftcol = curwin->w_leftcol;
4004 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4005
4006 // may undisplay the popup menu
4007 ins_compl_upd_pum();
4008
4009 if (n > 1) // all matches have been found
4010 compl_matches = n;
4011 compl_curr_match = compl_shown_match;
4012 compl_direction = compl_shows_dir;
4013
4014 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4015 // mode.
4016 if (got_int && !global_busy)
4017 {
4018 (void)vgetc();
4019 got_int = FALSE;
4020 }
4021
4022 // we found no match if the list has only the "compl_orig_text"-entry
4023 if (compl_first_match == compl_first_match->cp_next)
4024 {
4025 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4026 && compl_length > 1
4027 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4028 edit_submode_highl = HLF_E;
4029 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4030 // because we couldn't expand anything at first place, but if we used
4031 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4032 // (such as M in M'exico) if not tried already. -- Acevedo
4033 if ( compl_length > 1
4034 || (compl_cont_status & CONT_ADDING)
4035 || (ctrl_x_mode != CTRL_X_NORMAL
4036 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4037 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4038 compl_cont_status &= ~CONT_N_ADDS;
4039 }
4040
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004041 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 compl_cont_status |= CONT_S_IPOS;
4043 else
4044 compl_cont_status &= ~CONT_S_IPOS;
4045
4046 if (edit_submode_extra == NULL)
4047 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004048 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004049 {
4050 edit_submode_extra = (char_u *)_("Back at original");
4051 edit_submode_highl = HLF_W;
4052 }
4053 else if (compl_cont_status & CONT_S_IPOS)
4054 {
4055 edit_submode_extra = (char_u *)_("Word from other line");
4056 edit_submode_highl = HLF_COUNT;
4057 }
4058 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4059 {
4060 edit_submode_extra = (char_u *)_("The only match");
4061 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004062 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004063 }
4064 else
4065 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004066#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004067 // Update completion sequence number when needed.
4068 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004069 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004070#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004071 // The match should always have a sequence number now, this is
4072 // just a safety check.
4073 if (compl_curr_match->cp_number != -1)
4074 {
4075 // Space for 10 text chars. + 2x10-digit no.s = 31.
4076 // Translations may need more than twice that.
4077 static char_u match_ref[81];
4078
4079 if (compl_matches > 0)
4080 vim_snprintf((char *)match_ref, sizeof(match_ref),
4081 _("match %d of %d"),
4082 compl_curr_match->cp_number, compl_matches);
4083 else
4084 vim_snprintf((char *)match_ref, sizeof(match_ref),
4085 _("match %d"),
4086 compl_curr_match->cp_number);
4087 edit_submode_extra = match_ref;
4088 edit_submode_highl = HLF_R;
4089 if (dollar_vcol >= 0)
4090 curs_columns(FALSE);
4091 }
4092 }
4093 }
4094
4095 // Show a message about what (completion) mode we're in.
4096 if (!compl_opt_suppress_empty)
4097 {
4098 showmode();
4099 if (!shortmess(SHM_COMPLETIONMENU))
4100 {
4101 if (edit_submode_extra != NULL)
4102 {
4103 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004104 {
4105 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106 msg_attr((char *)edit_submode_extra,
4107 edit_submode_highl < HLF_COUNT
4108 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004109 msg_hist_off = FALSE;
4110 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004111 }
4112 else
4113 msg_clr_cmdline(); // necessary for "noshowmode"
4114 }
4115 }
4116
4117 // Show the popup menu, unless we got interrupted.
4118 if (enable_pum && !compl_interrupted)
4119 show_pum(save_w_wrow, save_w_leftcol);
4120
4121 compl_was_interrupted = compl_interrupted;
4122 compl_interrupted = FALSE;
4123
4124 return OK;
4125}
4126
4127 static void
4128show_pum(int prev_w_wrow, int prev_w_leftcol)
4129{
4130 // RedrawingDisabled may be set when invoked through complete().
4131 int n = RedrawingDisabled;
4132
4133 RedrawingDisabled = 0;
4134
4135 // If the cursor moved or the display scrolled we need to remove the pum
4136 // first.
4137 setcursor();
4138 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4139 ins_compl_del_pum();
4140
4141 ins_compl_show_pum();
4142 setcursor();
4143 RedrawingDisabled = n;
4144}
4145
4146/*
4147 * Looks in the first "len" chars. of "src" for search-metachars.
4148 * If dest is not NULL the chars. are copied there quoting (with
4149 * a backslash) the metachars, and dest would be NUL terminated.
4150 * Returns the length (needed) of dest
4151 */
4152 static unsigned
4153quote_meta(char_u *dest, char_u *src, int len)
4154{
4155 unsigned m = (unsigned)len + 1; // one extra for the NUL
4156
4157 for ( ; --len >= 0; src++)
4158 {
4159 switch (*src)
4160 {
4161 case '.':
4162 case '*':
4163 case '[':
4164 if (ctrl_x_mode == CTRL_X_DICTIONARY
4165 || ctrl_x_mode == CTRL_X_THESAURUS)
4166 break;
4167 // FALLTHROUGH
4168 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004169 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004170 break;
4171 // FALLTHROUGH
4172 case '\\':
4173 if (ctrl_x_mode == CTRL_X_DICTIONARY
4174 || ctrl_x_mode == CTRL_X_THESAURUS)
4175 break;
4176 // FALLTHROUGH
4177 case '^': // currently it's not needed.
4178 case '$':
4179 m++;
4180 if (dest != NULL)
4181 *dest++ = '\\';
4182 break;
4183 }
4184 if (dest != NULL)
4185 *dest++ = *src;
4186 // Copy remaining bytes of a multibyte character.
4187 if (has_mbyte)
4188 {
4189 int i, mb_len;
4190
4191 mb_len = (*mb_ptr2len)(src) - 1;
4192 if (mb_len > 0 && len >= mb_len)
4193 for (i = 0; i < mb_len; ++i)
4194 {
4195 --len;
4196 ++src;
4197 if (dest != NULL)
4198 *dest++ = *src;
4199 }
4200 }
4201 }
4202 if (dest != NULL)
4203 *dest = NUL;
4204
4205 return m;
4206}
4207
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004208#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209 void
4210free_insexpand_stuff(void)
4211{
4212 VIM_CLEAR(compl_orig_text);
4213}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004214#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004215
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004216#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004217/*
4218 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4219 * spelled word, if there is one.
4220 */
4221 static void
4222spell_back_to_badword(void)
4223{
4224 pos_T tpos = curwin->w_cursor;
4225
4226 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4227 if (curwin->w_cursor.col != tpos.col)
4228 start_arrow(&tpos);
4229}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004230#endif