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