blob: 3cefb39748fdd9f87375f835798726a9c77498a6 [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()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
127# ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128static char e_compldel[] = N_("E840: Completion function deleted text");
129# endif
130
131/*
132 * All the current matches are stored in a list.
133 * "compl_first_match" points to the start of the list.
134 * "compl_curr_match" points to the currently selected entry.
135 * "compl_shown_match" is different from compl_curr_match during
136 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000137 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100138 */
139static compl_T *compl_first_match = NULL;
140static compl_T *compl_curr_match = NULL;
141static compl_T *compl_shown_match = NULL;
142static compl_T *compl_old_match = NULL;
143
144// After using a cursor key <Enter> selects a match in the popup menu,
145// otherwise it inserts a line break.
146static int compl_enter_selects = FALSE;
147
148// When "compl_leader" is not NULL only matches that start with this string
149// are used.
150static char_u *compl_leader = NULL;
151
152static int compl_get_longest = FALSE; // put longest common string
153 // in compl_leader
154
155static int compl_no_insert = FALSE; // FALSE: select & insert
156 // TRUE: noinsert
157static int compl_no_select = FALSE; // FALSE: select & insert
158 // TRUE: noselect
159
160// Selected one of the matches. When FALSE the match was edited or using the
161// longest common string.
162static int compl_used_match;
163
164// didn't finish finding completions.
165static int compl_was_interrupted = FALSE;
166
167// Set when character typed while looking for matches and it means we should
168// stop looking for matches.
169static int compl_interrupted = FALSE;
170
171static int compl_restarting = FALSE; // don't insert match
172
173// When the first completion is done "compl_started" is set. When it's
174// FALSE the word to be completed must be located.
175static int compl_started = FALSE;
176
177// Which Ctrl-X mode are we in?
178static int ctrl_x_mode = CTRL_X_NORMAL;
179
180static int compl_matches = 0;
181static char_u *compl_pattern = NULL;
182static int compl_direction = FORWARD;
183static int compl_shows_dir = FORWARD;
184static int compl_pending = 0; // > 1 for postponed CTRL-N
185static pos_T compl_startpos;
186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
193static int compl_opt_refresh_always = FALSE;
194static int compl_opt_suppress_empty = FALSE;
195
Bram Moolenaar08928322020-01-04 14:32:48 +0100196static 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 +0100197static void ins_compl_longest_match(compl_T *match);
198static void ins_compl_del_pum(void);
199static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
200static char_u *find_line_end(char_u *ptr);
201static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100202static int ins_compl_need_restart(void);
203static void ins_compl_new_leader(void);
204static int ins_compl_len(void);
205static void ins_compl_restart(void);
206static void ins_compl_set_original_text(char_u *str);
207static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
208# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
209static void ins_compl_add_list(list_T *list);
210static void ins_compl_add_dict(dict_T *dict);
211# endif
212static int ins_compl_key2dir(int c);
213static int ins_compl_pum_key(int c);
214static int ins_compl_key2count(int c);
215static void show_pum(int prev_w_wrow, int prev_w_leftcol);
216static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100217
218#ifdef FEAT_SPELL
219static void spell_back_to_badword(void);
220static int spell_bad_len = 0; // length of located bad word
221#endif
222
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100223/*
224 * CTRL-X pressed in Insert mode.
225 */
226 void
227ins_ctrl_x(void)
228{
zeertzjqdca29d92021-08-31 19:12:51 +0200229 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000231 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100232 if (compl_cont_status & CONT_N_ADDS)
233 compl_cont_status |= CONT_INTRPT;
234 else
235 compl_cont_status = 0;
236 // We're not sure which CTRL-X mode it will be yet
237 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
238 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
239 edit_submode_pre = NULL;
240 showmode();
241 }
zeertzjqdca29d92021-08-31 19:12:51 +0200242 else
243 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
244 // CTRL-V look like CTRL-N
245 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100246
247 trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100248}
249
250/*
251 * Functions to check the current CTRL-X mode.
252 */
253int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
254int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
255int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
256int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
257int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
258int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
259int ctrl_x_mode_path_patterns(void) {
260 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
261int ctrl_x_mode_path_defines(void) {
262 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
263int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
264int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200265int ctrl_x_mode_cmdline(void) {
266 return ctrl_x_mode == CTRL_X_CMDLINE
267 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100268int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
269int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
270int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
271int ctrl_x_mode_line_or_eval(void) {
272 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
273
274/*
275 * Whether other than default completion has been selected.
276 */
277 int
278ctrl_x_mode_not_default(void)
279{
280 return ctrl_x_mode != CTRL_X_NORMAL;
281}
282
283/*
zeertzjqdca29d92021-08-31 19:12:51 +0200284 * Whether CTRL-X was typed without a following character,
285 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100286 */
287 int
288ctrl_x_mode_not_defined_yet(void)
289{
290 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
291}
292
293/*
294 * Return TRUE if the 'dict' or 'tsr' option can be used.
295 */
296 int
297has_compl_option(int dict_opt)
298{
299 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200300#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100301 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200302#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100303 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100304 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
305#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100306 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100307#endif
308 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100309 {
310 ctrl_x_mode = CTRL_X_NORMAL;
311 edit_submode = NULL;
312 msg_attr(dict_opt ? _("'dictionary' option is empty")
313 : _("'thesaurus' option is empty"),
314 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100315 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100316 {
317 vim_beep(BO_COMPL);
318 setcursor();
319 out_flush();
320#ifdef FEAT_EVAL
321 if (!get_vim_var_nr(VV_TESTING))
322#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100323 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100324 }
325 return FALSE;
326 }
327 return TRUE;
328}
329
330/*
331 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
332 * This depends on the current mode.
333 */
334 int
335vim_is_ctrl_x_key(int c)
336{
337 // Always allow ^R - let its results then be checked
338 if (c == Ctrl_R)
339 return TRUE;
340
341 // Accept <PageUp> and <PageDown> if the popup menu is visible.
342 if (ins_compl_pum_key(c))
343 return TRUE;
344
345 switch (ctrl_x_mode)
346 {
347 case 0: // Not in any CTRL-X mode
348 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
349 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200350 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100351 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
352 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
353 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
354 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
355 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200356 || c == Ctrl_S || c == Ctrl_K || c == 's'
357 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100358 case CTRL_X_SCROLL:
359 return (c == Ctrl_Y || c == Ctrl_E);
360 case CTRL_X_WHOLE_LINE:
361 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
362 case CTRL_X_FILES:
363 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
364 case CTRL_X_DICTIONARY:
365 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
366 case CTRL_X_THESAURUS:
367 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
368 case CTRL_X_TAGS:
369 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
370#ifdef FEAT_FIND_ID
371 case CTRL_X_PATH_PATTERNS:
372 return (c == Ctrl_P || c == Ctrl_N);
373 case CTRL_X_PATH_DEFINES:
374 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
375#endif
376 case CTRL_X_CMDLINE:
377 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
378 || c == Ctrl_X);
379#ifdef FEAT_COMPL_FUNC
380 case CTRL_X_FUNCTION:
381 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
382 case CTRL_X_OMNI:
383 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
384#endif
385 case CTRL_X_SPELL:
386 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
387 case CTRL_X_EVAL:
388 return (c == Ctrl_P || c == Ctrl_N);
389 }
390 internal_error("vim_is_ctrl_x_key()");
391 return FALSE;
392}
393
394/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000395 * Returns TRUE if the currently shown text is the original text when the
396 * completion began.
397 */
398 static int
399ins_compl_at_original_text(compl_T *match)
400{
401 return match->cp_flags & CP_ORIGINAL_TEXT;
402}
403
404/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100405 * Return TRUE when character "c" is part of the item currently being
406 * completed. Used to decide whether to abandon complete mode when the menu
407 * is visible.
408 */
409 int
410ins_compl_accept_char(int c)
411{
412 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
413 // When expanding an identifier only accept identifier chars.
414 return vim_isIDc(c);
415
416 switch (ctrl_x_mode)
417 {
418 case CTRL_X_FILES:
419 // When expanding file name only accept file name chars. But not
420 // path separators, so that "proto/<Tab>" expands files in
421 // "proto", not "proto/" as a whole
422 return vim_isfilec(c) && !vim_ispathsep(c);
423
424 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200425 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100426 case CTRL_X_OMNI:
427 // Command line and Omni completion can work with just about any
428 // printable character, but do stop at white space.
429 return vim_isprintc(c) && !VIM_ISWHITE(c);
430
431 case CTRL_X_WHOLE_LINE:
432 // For while line completion a space can be part of the line.
433 return vim_isprintc(c);
434 }
435 return vim_iswordc(c);
436}
437
438/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000439 * Get the completed text by inferring the case of the originally typed text.
440 */
441 static char_u *
442ins_compl_infercase_gettext(
443 char_u *str,
444 int actual_len,
445 int actual_compl_length,
446 int min_len)
447{
448 int *wca; // Wide character array.
449 char_u *p;
450 int i, c;
451 int has_lower = FALSE;
452 int was_letter = FALSE;
453
454 IObuff[0] = NUL;
455
456 // Allocate wide character array for the completion and fill it.
457 wca = ALLOC_MULT(int, actual_len);
458 if (wca == NULL)
459 return IObuff;
460
461 p = str;
462 for (i = 0; i < actual_len; ++i)
463 if (has_mbyte)
464 wca[i] = mb_ptr2char_adv(&p);
465 else
466 wca[i] = *(p++);
467
468 // Rule 1: Were any chars converted to lower?
469 p = compl_orig_text;
470 for (i = 0; i < min_len; ++i)
471 {
472 if (has_mbyte)
473 c = mb_ptr2char_adv(&p);
474 else
475 c = *(p++);
476 if (MB_ISLOWER(c))
477 {
478 has_lower = TRUE;
479 if (MB_ISUPPER(wca[i]))
480 {
481 // Rule 1 is satisfied.
482 for (i = actual_compl_length; i < actual_len; ++i)
483 wca[i] = MB_TOLOWER(wca[i]);
484 break;
485 }
486 }
487 }
488
489 // Rule 2: No lower case, 2nd consecutive letter converted to
490 // upper case.
491 if (!has_lower)
492 {
493 p = compl_orig_text;
494 for (i = 0; i < min_len; ++i)
495 {
496 if (has_mbyte)
497 c = mb_ptr2char_adv(&p);
498 else
499 c = *(p++);
500 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
501 {
502 // Rule 2 is satisfied.
503 for (i = actual_compl_length; i < actual_len; ++i)
504 wca[i] = MB_TOUPPER(wca[i]);
505 break;
506 }
507 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
508 }
509 }
510
511 // Copy the original case of the part we typed.
512 p = compl_orig_text;
513 for (i = 0; i < min_len; ++i)
514 {
515 if (has_mbyte)
516 c = mb_ptr2char_adv(&p);
517 else
518 c = *(p++);
519 if (MB_ISLOWER(c))
520 wca[i] = MB_TOLOWER(wca[i]);
521 else if (MB_ISUPPER(c))
522 wca[i] = MB_TOUPPER(wca[i]);
523 }
524
525 // Generate encoding specific output from wide character array.
526 // Multi-byte characters can occupy up to five bytes more than
527 // ASCII characters, and we also need one byte for NUL, so stay
528 // six bytes away from the edge of IObuff.
529 p = IObuff;
530 i = 0;
531 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
532 if (has_mbyte)
533 p += (*mb_char2bytes)(wca[i++], p);
534 else
535 *(p++) = wca[i++];
536 *p = NUL;
537
538 vim_free(wca);
539
540 return IObuff;
541}
542
543/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100544 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
545 * case of the originally typed text is used, and the case of the completed
546 * text is inferred, ie this tries to work out what case you probably wanted
547 * the rest of the word to be in -- webb
548 */
549 int
550ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200551 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100552 int len,
553 int icase,
554 char_u *fname,
555 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200556 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100557{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200558 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100559 char_u *p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100560 int actual_len; // Take multi-byte characters
561 int actual_compl_length; // into account.
562 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200563 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100564
565 if (p_ic && curbuf->b_p_inf && len > 0)
566 {
567 // Infer case of completed part.
568
569 // Find actual length of completion.
570 if (has_mbyte)
571 {
572 p = str;
573 actual_len = 0;
574 while (*p != NUL)
575 {
576 MB_PTR_ADV(p);
577 ++actual_len;
578 }
579 }
580 else
581 actual_len = len;
582
583 // Find actual length of original text.
584 if (has_mbyte)
585 {
586 p = compl_orig_text;
587 actual_compl_length = 0;
588 while (*p != NUL)
589 {
590 MB_PTR_ADV(p);
591 ++actual_compl_length;
592 }
593 }
594 else
595 actual_compl_length = compl_length;
596
597 // "actual_len" may be smaller than "actual_compl_length" when using
598 // thesaurus, only use the minimum when comparing.
599 min_len = actual_len < actual_compl_length
600 ? actual_len : actual_compl_length;
601
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000602 str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
603 min_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100604 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200605 if (cont_s_ipos)
606 flags |= CP_CONT_S_IPOS;
607 if (icase)
608 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200609
Bram Moolenaar08928322020-01-04 14:32:48 +0100610 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100611}
612
613/*
614 * Add a match to the list of matches.
615 * If the given string is already in the list of completions, then return
616 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
617 * maybe because alloc() returns NULL, then FAIL is returned.
618 */
619 static int
620ins_compl_add(
621 char_u *str,
622 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100623 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 char_u **cptext, // extra text for popup menu or NULL
625 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100626 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200627 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100628 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100629{
630 compl_T *match;
631 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200632 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100633
Bram Moolenaarceb06192021-04-04 15:05:22 +0200634 if (flags & CP_FAST)
635 fast_breakcheck();
636 else
637 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100638 if (got_int)
639 return FAIL;
640 if (len < 0)
641 len = (int)STRLEN(str);
642
643 // If the same match is already present, don't add it.
644 if (compl_first_match != NULL && !adup)
645 {
646 match = compl_first_match;
647 do
648 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000649 if (!ins_compl_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100650 && STRNCMP(match->cp_str, str, len) == 0
651 && match->cp_str[len] == NUL)
652 return NOTDONE;
653 match = match->cp_next;
654 } while (match != NULL && match != compl_first_match);
655 }
656
657 // Remove any popup menu before changing the list of matches.
658 ins_compl_del_pum();
659
660 // Allocate a new match structure.
661 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200662 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100663 if (match == NULL)
664 return FAIL;
665 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200666 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100667 match->cp_number = 0;
668 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
669 {
670 vim_free(match);
671 return FAIL;
672 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673
674 // match-fname is:
675 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200676 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100677 // - NULL otherwise. --Acevedo
678 if (fname != NULL
679 && compl_curr_match != NULL
680 && compl_curr_match->cp_fname != NULL
681 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
682 match->cp_fname = compl_curr_match->cp_fname;
683 else if (fname != NULL)
684 {
685 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200686 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687 }
688 else
689 match->cp_fname = NULL;
690 match->cp_flags = flags;
691
692 if (cptext != NULL)
693 {
694 int i;
695
696 for (i = 0; i < CPT_COUNT; ++i)
697 if (cptext[i] != NULL && *cptext[i] != NUL)
698 match->cp_text[i] = vim_strsave(cptext[i]);
699 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100700#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100701 if (user_data != NULL)
702 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100703#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100704
705 // Link the new match structure in the list of matches.
706 if (compl_first_match == NULL)
707 match->cp_next = match->cp_prev = NULL;
708 else if (dir == FORWARD)
709 {
710 match->cp_next = compl_curr_match->cp_next;
711 match->cp_prev = compl_curr_match;
712 }
713 else // BACKWARD
714 {
715 match->cp_next = compl_curr_match;
716 match->cp_prev = compl_curr_match->cp_prev;
717 }
718 if (match->cp_next)
719 match->cp_next->cp_prev = match;
720 if (match->cp_prev)
721 match->cp_prev->cp_next = match;
722 else // if there's nothing before, it is the first match
723 compl_first_match = match;
724 compl_curr_match = match;
725
726 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200727 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728 ins_compl_longest_match(match);
729
730 return OK;
731}
732
733/*
734 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200735 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736 */
737 static int
738ins_compl_equal(compl_T *match, char_u *str, int len)
739{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200740 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200741 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200742 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100743 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
744 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
745}
746
747/*
748 * Reduce the longest common string for match "match".
749 */
750 static void
751ins_compl_longest_match(compl_T *match)
752{
753 char_u *p, *s;
754 int c1, c2;
755 int had_match;
756
757 if (compl_leader == NULL)
758 {
759 // First match, use it as a whole.
760 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000761 if (compl_leader == NULL)
762 return;
763
764 had_match = (curwin->w_cursor.col > compl_col);
765 ins_compl_delete();
766 ins_bytes(compl_leader + ins_compl_len());
767 ins_redraw(FALSE);
768
769 // When the match isn't there (to avoid matching itself) remove it
770 // again after redrawing.
771 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100772 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100773 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000774
775 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100776 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000777
778 // Reduce the text if this match differs from compl_leader.
779 p = compl_leader;
780 s = match->cp_str;
781 while (*p != NUL)
782 {
783 if (has_mbyte)
784 {
785 c1 = mb_ptr2char(p);
786 c2 = mb_ptr2char(s);
787 }
788 else
789 {
790 c1 = *p;
791 c2 = *s;
792 }
793 if ((match->cp_flags & CP_ICASE)
794 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
795 break;
796 if (has_mbyte)
797 {
798 MB_PTR_ADV(p);
799 MB_PTR_ADV(s);
800 }
801 else
802 {
803 ++p;
804 ++s;
805 }
806 }
807
808 if (*p != NUL)
809 {
810 // Leader was shortened, need to change the inserted text.
811 *p = NUL;
812 had_match = (curwin->w_cursor.col > compl_col);
813 ins_compl_delete();
814 ins_bytes(compl_leader + ins_compl_len());
815 ins_redraw(FALSE);
816
817 // When the match isn't there (to avoid matching itself) remove it
818 // again after redrawing.
819 if (!had_match)
820 ins_compl_delete();
821 }
822
823 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100824}
825
826/*
827 * Add an array of matches to the list of matches.
828 * Frees matches[].
829 */
830 static void
831ins_compl_add_matches(
832 int num_matches,
833 char_u **matches,
834 int icase)
835{
836 int i;
837 int add_r = OK;
838 int dir = compl_direction;
839
840 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100841 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200842 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 // if dir was BACKWARD then honor it just once
844 dir = FORWARD;
845 FreeWild(num_matches, matches);
846}
847
848/*
849 * Make the completion list cyclic.
850 * Return the number of matches (excluding the original).
851 */
852 static int
853ins_compl_make_cyclic(void)
854{
855 compl_T *match;
856 int count = 0;
857
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000858 if (compl_first_match == NULL)
859 return 0;
860
861 // Find the end of the list.
862 match = compl_first_match;
863 // there's always an entry for the compl_orig_text, it doesn't count.
864 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100865 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000866 match = match->cp_next;
867 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100868 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000869 match->cp_next = compl_first_match;
870 compl_first_match->cp_prev = match;
871
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100872 return count;
873}
874
875/*
876 * Return whether there currently is a shown match.
877 */
878 int
879ins_compl_has_shown_match(void)
880{
881 return compl_shown_match == NULL
882 || compl_shown_match != compl_shown_match->cp_next;
883}
884
885/*
886 * Return whether the shown match is long enough.
887 */
888 int
889ins_compl_long_shown_match(void)
890{
891 return (int)STRLEN(compl_shown_match->cp_str)
892 > curwin->w_cursor.col - compl_col;
893}
894
895/*
896 * Set variables that store noselect and noinsert behavior from the
897 * 'completeopt' value.
898 */
899 void
900completeopt_was_set(void)
901{
902 compl_no_insert = FALSE;
903 compl_no_select = FALSE;
904 if (strstr((char *)p_cot, "noselect") != NULL)
905 compl_no_select = TRUE;
906 if (strstr((char *)p_cot, "noinsert") != NULL)
907 compl_no_insert = TRUE;
908}
909
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100910
911// "compl_match_array" points the currently displayed list of entries in the
912// popup menu. It is NULL when there is no popup menu.
913static pumitem_T *compl_match_array = NULL;
914static int compl_match_arraysize;
915
916/*
917 * Update the screen and when there is any scrolling remove the popup menu.
918 */
919 static void
920ins_compl_upd_pum(void)
921{
922 int h;
923
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000924 if (compl_match_array == NULL)
925 return;
926
927 h = curwin->w_cline_height;
928 // Update the screen later, before drawing the popup menu over it.
929 pum_call_update_screen();
930 if (h != curwin->w_cline_height)
931 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100932}
933
934/*
935 * Remove any popup menu.
936 */
937 static void
938ins_compl_del_pum(void)
939{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000940 if (compl_match_array == NULL)
941 return;
942
943 pum_undisplay();
944 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100945}
946
947/*
948 * Return TRUE if the popup menu should be displayed.
949 */
950 int
951pum_wanted(void)
952{
953 // 'completeopt' must contain "menu" or "menuone"
954 if (vim_strchr(p_cot, 'm') == NULL)
955 return FALSE;
956
957 // The display looks bad on a B&W display.
958 if (t_colors < 8
959#ifdef FEAT_GUI
960 && !gui.in_use
961#endif
962 )
963 return FALSE;
964 return TRUE;
965}
966
967/*
968 * Return TRUE if there are two or more matches to be shown in the popup menu.
969 * One if 'completopt' contains "menuone".
970 */
971 static int
972pum_enough_matches(void)
973{
974 compl_T *compl;
975 int i;
976
977 // Don't display the popup menu if there are no matches or there is only
978 // one (ignoring the original text).
979 compl = compl_first_match;
980 i = 0;
981 do
982 {
983 if (compl == NULL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000984 || (!ins_compl_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100985 break;
986 compl = compl->cp_next;
987 } while (compl != compl_first_match);
988
989 if (strstr((char *)p_cot, "menuone") != NULL)
990 return (i >= 1);
991 return (i >= 2);
992}
993
Bram Moolenaar3075a452021-11-17 15:51:52 +0000994#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200995/*
996 * Allocate Dict for the completed item.
997 * { word, abbr, menu, kind, info }
998 */
999 static dict_T *
1000ins_compl_dict_alloc(compl_T *match)
1001{
1002 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1003
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001004 if (dict == NULL)
1005 return NULL;
1006
1007 dict_add_string(dict, "word", match->cp_str);
1008 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1009 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1010 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1011 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1012 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1013 dict_add_string(dict, "user_data", (char_u *)"");
1014 else
1015 dict_add_tv(dict, "user_data", &match->cp_user_data);
1016
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001017 return dict;
1018}
1019
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001020 static void
1021trigger_complete_changed_event(int cur)
1022{
1023 dict_T *v_event;
1024 dict_T *item;
1025 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001026 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001027
1028 if (recursive)
1029 return;
1030
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001031 if (cur < 0)
1032 item = dict_alloc();
1033 else
1034 item = ins_compl_dict_alloc(compl_curr_match);
1035 if (item == NULL)
1036 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001037 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001038 dict_add_dict(v_event, "completed_item", item);
1039 pum_set_event_info(v_event);
1040 dict_set_items_ro(v_event);
1041
1042 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001043 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001044 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001045 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001046 recursive = FALSE;
1047
Bram Moolenaar3075a452021-11-17 15:51:52 +00001048 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001049}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001050#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001051
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001052/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001053 * Build a popup menu to show the completion matches.
1054 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1055 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001056 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001057 static int
1058ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001059{
1060 compl_T *compl;
1061 compl_T *shown_compl = NULL;
1062 int did_find_shown_match = FALSE;
1063 int shown_match_ok = FALSE;
1064 int i;
1065 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001066 int lead_len = 0;
1067
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001068 // Need to build the popup menu list.
1069 compl_match_arraysize = 0;
1070 compl = compl_first_match;
1071 if (compl_leader != NULL)
1072 lead_len = (int)STRLEN(compl_leader);
1073
1074 do
1075 {
1076 if (!ins_compl_at_original_text(compl)
1077 && (compl_leader == NULL
1078 || ins_compl_equal(compl, compl_leader, lead_len)))
1079 ++compl_match_arraysize;
1080 compl = compl->cp_next;
1081 } while (compl != NULL && compl != compl_first_match);
1082
1083 if (compl_match_arraysize == 0)
1084 return -1;
1085
1086 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1087 if (compl_match_array == NULL)
1088 return -1;
1089
1090 // If the current match is the original text don't find the first
1091 // match after it, don't highlight anything.
1092 if (ins_compl_at_original_text(compl_shown_match))
1093 shown_match_ok = TRUE;
1094
1095 i = 0;
1096 compl = compl_first_match;
1097 do
1098 {
1099 if (!ins_compl_at_original_text(compl)
1100 && (compl_leader == NULL
1101 || ins_compl_equal(compl, compl_leader, lead_len)))
1102 {
1103 if (!shown_match_ok)
1104 {
1105 if (compl == compl_shown_match || did_find_shown_match)
1106 {
1107 // This item is the shown match or this is the
1108 // first displayed item after the shown match.
1109 compl_shown_match = compl;
1110 did_find_shown_match = TRUE;
1111 shown_match_ok = TRUE;
1112 }
1113 else
1114 // Remember this displayed match for when the
1115 // shown match is just below it.
1116 shown_compl = compl;
1117 cur = i;
1118 }
1119
1120 if (compl->cp_text[CPT_ABBR] != NULL)
1121 compl_match_array[i].pum_text =
1122 compl->cp_text[CPT_ABBR];
1123 else
1124 compl_match_array[i].pum_text = compl->cp_str;
1125 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1126 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1127 if (compl->cp_text[CPT_MENU] != NULL)
1128 compl_match_array[i++].pum_extra =
1129 compl->cp_text[CPT_MENU];
1130 else
1131 compl_match_array[i++].pum_extra = compl->cp_fname;
1132 }
1133
1134 if (compl == compl_shown_match)
1135 {
1136 did_find_shown_match = TRUE;
1137
1138 // When the original text is the shown match don't set
1139 // compl_shown_match.
1140 if (ins_compl_at_original_text(compl))
1141 shown_match_ok = TRUE;
1142
1143 if (!shown_match_ok && shown_compl != NULL)
1144 {
1145 // The shown match isn't displayed, set it to the
1146 // previously displayed match.
1147 compl_shown_match = shown_compl;
1148 shown_match_ok = TRUE;
1149 }
1150 }
1151 compl = compl->cp_next;
1152 } while (compl != NULL && compl != compl_first_match);
1153
1154 if (!shown_match_ok) // no displayed match at all
1155 cur = -1;
1156
1157 return cur;
1158}
1159
1160/*
1161 * Show the popup menu for the list of matches.
1162 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1163 */
1164 void
1165ins_compl_show_pum(void)
1166{
1167 int i;
1168 int cur = -1;
1169 colnr_T col;
1170
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001171 if (!pum_wanted() || !pum_enough_matches())
1172 return;
1173
1174#if defined(FEAT_EVAL)
1175 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001176 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001177#endif
1178
1179 // Update the screen later, before drawing the popup menu over it.
1180 pum_call_update_screen();
1181
1182 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001183 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001184 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001185 else
1186 {
1187 // popup menu already exists, only need to find the current item.
1188 for (i = 0; i < compl_match_arraysize; ++i)
1189 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1190 || compl_match_array[i].pum_text
1191 == compl_shown_match->cp_text[CPT_ABBR])
1192 {
1193 cur = i;
1194 break;
1195 }
1196 }
1197
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001198 if (compl_match_array == NULL)
1199 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001200
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001201 // In Replace mode when a $ is displayed at the end of the line only
1202 // part of the screen would be updated. We do need to redraw here.
1203 dollar_vcol = -1;
1204
1205 // Compute the screen column of the start of the completed text.
1206 // Use the cursor to get all wrapping and other settings right.
1207 col = curwin->w_cursor.col;
1208 curwin->w_cursor.col = compl_col;
1209 pum_display(compl_match_array, compl_match_arraysize, cur);
1210 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001211
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001212#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001213 if (has_completechanged())
1214 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001215#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001216}
1217
1218#define DICT_FIRST (1) // use just first element in "dict"
1219#define DICT_EXACT (2) // "dict" is the exact name of a file
1220
1221/*
1222 * Add any identifiers that match the given pattern in the list of dictionary
1223 * files "dict_start" to the list of completions.
1224 */
1225 static void
1226ins_compl_dictionaries(
1227 char_u *dict_start,
1228 char_u *pat,
1229 int flags, // DICT_FIRST and/or DICT_EXACT
1230 int thesaurus) // Thesaurus completion
1231{
1232 char_u *dict = dict_start;
1233 char_u *ptr;
1234 char_u *buf;
1235 regmatch_T regmatch;
1236 char_u **files;
1237 int count;
1238 int save_p_scs;
1239 int dir = compl_direction;
1240
1241 if (*dict == NUL)
1242 {
1243#ifdef FEAT_SPELL
1244 // When 'dictionary' is empty and spell checking is enabled use
1245 // "spell".
1246 if (!thesaurus && curwin->w_p_spell)
1247 dict = (char_u *)"spell";
1248 else
1249#endif
1250 return;
1251 }
1252
1253 buf = alloc(LSIZE);
1254 if (buf == NULL)
1255 return;
1256 regmatch.regprog = NULL; // so that we can goto theend
1257
1258 // If 'infercase' is set, don't use 'smartcase' here
1259 save_p_scs = p_scs;
1260 if (curbuf->b_p_inf)
1261 p_scs = FALSE;
1262
1263 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1264 // to only match at the start of a line. Otherwise just match the
1265 // pattern. Also need to double backslashes.
1266 if (ctrl_x_mode_line_or_eval())
1267 {
1268 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1269 size_t len;
1270
1271 if (pat_esc == NULL)
1272 goto theend;
1273 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001274 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001275 if (ptr == NULL)
1276 {
1277 vim_free(pat_esc);
1278 goto theend;
1279 }
1280 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1281 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1282 vim_free(pat_esc);
1283 vim_free(ptr);
1284 }
1285 else
1286 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001287 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001288 if (regmatch.regprog == NULL)
1289 goto theend;
1290 }
1291
1292 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1293 regmatch.rm_ic = ignorecase(pat);
1294 while (*dict != NUL && !got_int && !compl_interrupted)
1295 {
1296 // copy one dictionary file name into buf
1297 if (flags == DICT_EXACT)
1298 {
1299 count = 1;
1300 files = &dict;
1301 }
1302 else
1303 {
1304 // Expand wildcards in the dictionary name, but do not allow
1305 // backticks (for security, the 'dict' option may have been set in
1306 // a modeline).
1307 copy_option_part(&dict, buf, LSIZE, ",");
1308# ifdef FEAT_SPELL
1309 if (!thesaurus && STRCMP(buf, "spell") == 0)
1310 count = -1;
1311 else
1312# endif
1313 if (vim_strchr(buf, '`') != NULL
1314 || expand_wildcards(1, &buf, &count, &files,
1315 EW_FILE|EW_SILENT) != OK)
1316 count = 0;
1317 }
1318
1319# ifdef FEAT_SPELL
1320 if (count == -1)
1321 {
1322 // Complete from active spelling. Skip "\<" in the pattern, we
1323 // don't use it as a RE.
1324 if (pat[0] == '\\' && pat[1] == '<')
1325 ptr = pat + 2;
1326 else
1327 ptr = pat;
1328 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1329 }
1330 else
1331# endif
1332 if (count > 0) // avoid warning for using "files" uninit
1333 {
1334 ins_compl_files(count, files, thesaurus, flags,
1335 &regmatch, buf, &dir);
1336 if (flags != DICT_EXACT)
1337 FreeWild(count, files);
1338 }
1339 if (flags != 0)
1340 break;
1341 }
1342
1343theend:
1344 p_scs = save_p_scs;
1345 vim_regfree(regmatch.regprog);
1346 vim_free(buf);
1347}
1348
1349 static void
1350ins_compl_files(
1351 int count,
1352 char_u **files,
1353 int thesaurus,
1354 int flags,
1355 regmatch_T *regmatch,
1356 char_u *buf,
1357 int *dir)
1358{
1359 char_u *ptr;
1360 int i;
1361 FILE *fp;
1362 int add_r;
1363
1364 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1365 {
1366 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1367 if (flags != DICT_EXACT)
1368 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001369 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001370 vim_snprintf((char *)IObuff, IOSIZE,
1371 _("Scanning dictionary: %s"), (char *)files[i]);
1372 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1373 }
1374
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001375 if (fp == NULL)
1376 continue;
1377
1378 // Read dictionary file line by line.
1379 // Check each line for a match.
1380 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001381 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001382 ptr = buf;
1383 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001384 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001385 ptr = regmatch->startp[0];
1386 if (ctrl_x_mode_line_or_eval())
1387 ptr = find_line_end(ptr);
1388 else
1389 ptr = find_word_end(ptr);
1390 add_r = ins_compl_add_infercase(regmatch->startp[0],
1391 (int)(ptr - regmatch->startp[0]),
1392 p_ic, files[i], *dir, FALSE);
1393 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001394 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001395 char_u *wstart;
1396
1397 // Add the other matches on the line
1398 ptr = buf;
1399 while (!got_int)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001400 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001401 // Find start of the next word. Skip white
1402 // space and punctuation.
1403 ptr = find_word_start(ptr);
1404 if (*ptr == NUL || *ptr == NL)
1405 break;
1406 wstart = ptr;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001407
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001408 // Find end of the word.
1409 if (has_mbyte)
1410 // Japanese words may have characters in
1411 // different classes, only separate words
1412 // with single-byte non-word characters.
1413 while (*ptr != NUL)
1414 {
1415 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001416
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001417 if (l < 2 && !vim_iswordc(*ptr))
1418 break;
1419 ptr += l;
1420 }
1421 else
1422 ptr = find_word_end(ptr);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001423
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001424 // Add the word. Skip the regexp match.
1425 if (wstart != regmatch->startp[0])
1426 add_r = ins_compl_add_infercase(wstart,
1427 (int)(ptr - wstart),
1428 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001429 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001430 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001431 if (add_r == OK)
1432 // if dir was BACKWARD then honor it just once
1433 *dir = FORWARD;
1434 else if (add_r == FAIL)
1435 break;
1436 // avoid expensive call to vim_regexec() when at end
1437 // of line
1438 if (*ptr == '\n' || got_int)
1439 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001440 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001441 line_breakcheck();
1442 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001443 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001445 }
1446}
1447
1448/*
1449 * Find the start of the next word.
1450 * Returns a pointer to the first char of the word. Also stops at a NUL.
1451 */
1452 char_u *
1453find_word_start(char_u *ptr)
1454{
1455 if (has_mbyte)
1456 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1457 ptr += (*mb_ptr2len)(ptr);
1458 else
1459 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1460 ++ptr;
1461 return ptr;
1462}
1463
1464/*
1465 * Find the end of the word. Assumes it starts inside a word.
1466 * Returns a pointer to just after the word.
1467 */
1468 char_u *
1469find_word_end(char_u *ptr)
1470{
1471 int start_class;
1472
1473 if (has_mbyte)
1474 {
1475 start_class = mb_get_class(ptr);
1476 if (start_class > 1)
1477 while (*ptr != NUL)
1478 {
1479 ptr += (*mb_ptr2len)(ptr);
1480 if (mb_get_class(ptr) != start_class)
1481 break;
1482 }
1483 }
1484 else
1485 while (vim_iswordc(*ptr))
1486 ++ptr;
1487 return ptr;
1488}
1489
1490/*
1491 * Find the end of the line, omitting CR and NL at the end.
1492 * Returns a pointer to just after the line.
1493 */
1494 static char_u *
1495find_line_end(char_u *ptr)
1496{
1497 char_u *s;
1498
1499 s = ptr + STRLEN(ptr);
1500 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1501 --s;
1502 return s;
1503}
1504
1505/*
1506 * Free the list of completions
1507 */
1508 static void
1509ins_compl_free(void)
1510{
1511 compl_T *match;
1512 int i;
1513
1514 VIM_CLEAR(compl_pattern);
1515 VIM_CLEAR(compl_leader);
1516
1517 if (compl_first_match == NULL)
1518 return;
1519
1520 ins_compl_del_pum();
1521 pum_clear();
1522
1523 compl_curr_match = compl_first_match;
1524 do
1525 {
1526 match = compl_curr_match;
1527 compl_curr_match = compl_curr_match->cp_next;
1528 vim_free(match->cp_str);
1529 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001530 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001531 vim_free(match->cp_fname);
1532 for (i = 0; i < CPT_COUNT; ++i)
1533 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001534#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001535 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001536#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001537 vim_free(match);
1538 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
1539 compl_first_match = compl_curr_match = NULL;
1540 compl_shown_match = NULL;
1541 compl_old_match = NULL;
1542}
1543
1544 void
1545ins_compl_clear(void)
1546{
1547 compl_cont_status = 0;
1548 compl_started = FALSE;
1549 compl_matches = 0;
1550 VIM_CLEAR(compl_pattern);
1551 VIM_CLEAR(compl_leader);
1552 edit_submode_extra = NULL;
1553 VIM_CLEAR(compl_orig_text);
1554 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001555#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001556 // clear v:completed_item
1557 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001558#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001559}
1560
1561/*
1562 * Return TRUE when Insert completion is active.
1563 */
1564 int
1565ins_compl_active(void)
1566{
1567 return compl_started;
1568}
1569
1570/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001571 * Selected one of the matches. When FALSE the match was edited or using the
1572 * longest common string.
1573 */
1574 int
1575ins_compl_used_match(void)
1576{
1577 return compl_used_match;
1578}
1579
1580/*
1581 * Initialize get longest common string.
1582 */
1583 void
1584ins_compl_init_get_longest(void)
1585{
1586 compl_get_longest = FALSE;
1587}
1588
1589/*
1590 * Returns TRUE when insert completion is interrupted.
1591 */
1592 int
1593ins_compl_interrupted(void)
1594{
1595 return compl_interrupted;
1596}
1597
1598/*
1599 * Returns TRUE if the <Enter> key selects a match in the completion popup
1600 * menu.
1601 */
1602 int
1603ins_compl_enter_selects(void)
1604{
1605 return compl_enter_selects;
1606}
1607
1608/*
1609 * Return the column where the text starts that is being completed
1610 */
1611 colnr_T
1612ins_compl_col(void)
1613{
1614 return compl_col;
1615}
1616
1617/*
1618 * Delete one character before the cursor and show the subset of the matches
1619 * that match the word that is now before the cursor.
1620 * Returns the character to be used, NUL if the work is done and another char
1621 * to be got from the user.
1622 */
1623 int
1624ins_compl_bs(void)
1625{
1626 char_u *line;
1627 char_u *p;
1628
1629 line = ml_get_curline();
1630 p = line + curwin->w_cursor.col;
1631 MB_PTR_BACK(line, p);
1632
1633 // Stop completion when the whole word was deleted. For Omni completion
1634 // allow the word to be deleted, we won't match everything.
1635 // Respect the 'backspace' option.
1636 if ((int)(p - line) - (int)compl_col < 0
1637 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar440cf092021-04-03 20:13:30 +02001638 && ctrl_x_mode != CTRL_X_OMNI)
1639 || ctrl_x_mode == CTRL_X_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001640 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1641 - compl_length < 0))
1642 return K_BS;
1643
1644 // Deleted more than what was used to find matches or didn't finish
1645 // finding all matches: need to look for matches all over again.
1646 if (curwin->w_cursor.col <= compl_col + compl_length
1647 || ins_compl_need_restart())
1648 ins_compl_restart();
1649
1650 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001651 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001652 if (compl_leader != NULL)
1653 {
1654 ins_compl_new_leader();
1655 if (compl_shown_match != NULL)
1656 // Make sure current match is not a hidden item.
1657 compl_curr_match = compl_shown_match;
1658 return NUL;
1659 }
1660 return K_BS;
1661}
1662
1663/*
1664 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1665 * be called.
1666 */
1667 static int
1668ins_compl_need_restart(void)
1669{
1670 // Return TRUE if we didn't complete finding matches or when the
1671 // 'completefunc' returned "always" in the "refresh" dictionary item.
1672 return compl_was_interrupted
1673 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
1674 && compl_opt_refresh_always);
1675}
1676
1677/*
1678 * Called after changing "compl_leader".
1679 * Show the popup menu with a different set of matches.
1680 * May also search for matches again if the previous search was interrupted.
1681 */
1682 static void
1683ins_compl_new_leader(void)
1684{
1685 ins_compl_del_pum();
1686 ins_compl_delete();
1687 ins_bytes(compl_leader + ins_compl_len());
1688 compl_used_match = FALSE;
1689
1690 if (compl_started)
1691 ins_compl_set_original_text(compl_leader);
1692 else
1693 {
1694#ifdef FEAT_SPELL
1695 spell_bad_len = 0; // need to redetect bad word
1696#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001697 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001698 // the popup menu display the changed text before the cursor. Set
1699 // "compl_restarting" to avoid that the first match is inserted.
1700 pum_call_update_screen();
1701#ifdef FEAT_GUI
1702 if (gui.in_use)
1703 {
1704 // Show the cursor after the match, not after the redrawn text.
1705 setcursor();
1706 out_flush_cursor(FALSE, FALSE);
1707 }
1708#endif
1709 compl_restarting = TRUE;
1710 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1711 compl_cont_status = 0;
1712 compl_restarting = FALSE;
1713 }
1714
1715 compl_enter_selects = !compl_used_match;
1716
1717 // Show the popup menu with a different set of matches.
1718 ins_compl_show_pum();
1719
1720 // Don't let Enter select the original text when there is no popup menu.
1721 if (compl_match_array == NULL)
1722 compl_enter_selects = FALSE;
1723}
1724
1725/*
1726 * Return the length of the completion, from the completion start column to
1727 * the cursor column. Making sure it never goes below zero.
1728 */
1729 static int
1730ins_compl_len(void)
1731{
1732 int off = (int)curwin->w_cursor.col - (int)compl_col;
1733
1734 if (off < 0)
1735 return 0;
1736 return off;
1737}
1738
1739/*
1740 * Append one character to the match leader. May reduce the number of
1741 * matches.
1742 */
1743 void
1744ins_compl_addleader(int c)
1745{
1746 int cc;
1747
1748 if (stop_arrow() == FAIL)
1749 return;
1750 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1751 {
1752 char_u buf[MB_MAXBYTES + 1];
1753
1754 (*mb_char2bytes)(c, buf);
1755 buf[cc] = NUL;
1756 ins_char_bytes(buf, cc);
1757 if (compl_opt_refresh_always)
1758 AppendToRedobuff(buf);
1759 }
1760 else
1761 {
1762 ins_char(c);
1763 if (compl_opt_refresh_always)
1764 AppendCharToRedobuff(c);
1765 }
1766
1767 // If we didn't complete finding matches we must search again.
1768 if (ins_compl_need_restart())
1769 ins_compl_restart();
1770
1771 // When 'always' is set, don't reset compl_leader. While completing,
1772 // cursor doesn't point original position, changing compl_leader would
1773 // break redo.
1774 if (!compl_opt_refresh_always)
1775 {
1776 vim_free(compl_leader);
1777 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001778 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001779 if (compl_leader != NULL)
1780 ins_compl_new_leader();
1781 }
1782}
1783
1784/*
1785 * Setup for finding completions again without leaving CTRL-X mode. Used when
1786 * BS or a key was typed while still searching for matches.
1787 */
1788 static void
1789ins_compl_restart(void)
1790{
1791 ins_compl_free();
1792 compl_started = FALSE;
1793 compl_matches = 0;
1794 compl_cont_status = 0;
1795 compl_cont_mode = 0;
1796}
1797
1798/*
1799 * Set the first match, the original text.
1800 */
1801 static void
1802ins_compl_set_original_text(char_u *str)
1803{
1804 char_u *p;
1805
1806 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001807 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1808 // be at the last item for backward completion
1809 if (ins_compl_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001810 {
1811 p = vim_strsave(str);
1812 if (p != NULL)
1813 {
1814 vim_free(compl_first_match->cp_str);
1815 compl_first_match->cp_str = p;
1816 }
1817 }
1818 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001819 && ins_compl_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 {
1821 p = vim_strsave(str);
1822 if (p != NULL)
1823 {
1824 vim_free(compl_first_match->cp_prev->cp_str);
1825 compl_first_match->cp_prev->cp_str = p;
1826 }
1827 }
1828}
1829
1830/*
1831 * Append one character to the match leader. May reduce the number of
1832 * matches.
1833 */
1834 void
1835ins_compl_addfrommatch(void)
1836{
1837 char_u *p;
1838 int len = (int)curwin->w_cursor.col - (int)compl_col;
1839 int c;
1840 compl_T *cp;
1841
1842 p = compl_shown_match->cp_str;
1843 if ((int)STRLEN(p) <= len) // the match is too short
1844 {
1845 // When still at the original match use the first entry that matches
1846 // the leader.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001847 if (!ins_compl_at_original_text(compl_shown_match))
1848 return;
1849
1850 p = NULL;
1851 for (cp = compl_shown_match->cp_next; cp != NULL
1852 && cp != compl_first_match; cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001853 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001854 if (compl_leader == NULL
1855 || ins_compl_equal(cp, compl_leader,
1856 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001857 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001858 p = cp->cp_str;
1859 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001860 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001861 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001862 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001863 return;
1864 }
1865 p += len;
1866 c = PTR2CHAR(p);
1867 ins_compl_addleader(c);
1868}
1869
1870/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001871 * Set the CTRL-X completion mode based on the key 'c' typed after a CTRL-X.
1872 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
1873 * compl_cont_mode and compl_cont_status.
1874 * Returns TRUE when the character is not to be inserted.
1875 */
1876 static int
1877set_ctrl_x_mode(int c)
1878{
1879 int retval = FALSE;
1880
1881 switch (c)
1882 {
1883 case Ctrl_E:
1884 case Ctrl_Y:
1885 // scroll the window one line up or down
1886 ctrl_x_mode = CTRL_X_SCROLL;
1887 if (!(State & REPLACE_FLAG))
1888 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1889 else
1890 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1891 edit_submode_pre = NULL;
1892 showmode();
1893 break;
1894 case Ctrl_L:
1895 // complete whole line
1896 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1897 break;
1898 case Ctrl_F:
1899 // complete filenames
1900 ctrl_x_mode = CTRL_X_FILES;
1901 break;
1902 case Ctrl_K:
1903 // complete words from a dictinoary
1904 ctrl_x_mode = CTRL_X_DICTIONARY;
1905 break;
1906 case Ctrl_R:
1907 // Register insertion without exiting CTRL-X mode
1908 // Simply allow ^R to happen without affecting ^X mode
1909 break;
1910 case Ctrl_T:
1911 // complete words from a thesaurus
1912 ctrl_x_mode = CTRL_X_THESAURUS;
1913 break;
1914#ifdef FEAT_COMPL_FUNC
1915 case Ctrl_U:
1916 // user defined completion
1917 ctrl_x_mode = CTRL_X_FUNCTION;
1918 break;
1919 case Ctrl_O:
1920 // omni completion
1921 ctrl_x_mode = CTRL_X_OMNI;
1922 break;
1923#endif
1924 case 's':
1925 case Ctrl_S:
1926 // complete spelling suggestions
1927 ctrl_x_mode = CTRL_X_SPELL;
1928#ifdef FEAT_SPELL
1929 ++emsg_off; // Avoid getting the E756 error twice.
1930 spell_back_to_badword();
1931 --emsg_off;
1932#endif
1933 break;
1934 case Ctrl_RSB:
1935 // complete tag names
1936 ctrl_x_mode = CTRL_X_TAGS;
1937 break;
1938#ifdef FEAT_FIND_ID
1939 case Ctrl_I:
1940 case K_S_TAB:
1941 // complete keywords from included files
1942 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
1943 break;
1944 case Ctrl_D:
1945 // complete definitions from included files
1946 ctrl_x_mode = CTRL_X_PATH_DEFINES;
1947 break;
1948#endif
1949 case Ctrl_V:
1950 case Ctrl_Q:
1951 // complete vim commands
1952 ctrl_x_mode = CTRL_X_CMDLINE;
1953 break;
1954 case Ctrl_Z:
1955 // stop completion
1956 ctrl_x_mode = CTRL_X_NORMAL;
1957 edit_submode = NULL;
1958 showmode();
1959 retval = TRUE;
1960 break;
1961 case Ctrl_P:
1962 case Ctrl_N:
1963 // ^X^P means LOCAL expansion if nothing interrupted (eg we
1964 // just started ^X mode, or there were enough ^X's to cancel
1965 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
1966 // do normal expansion when interrupting a different mode (say
1967 // ^X^F^X^P or ^P^X^X^P, see below)
1968 // nothing changes if interrupting mode 0, (eg, the flag
1969 // doesn't change when going to ADDING mode -- Acevedo
1970 if (!(compl_cont_status & CONT_INTRPT))
1971 compl_cont_status |= CONT_LOCAL;
1972 else if (compl_cont_mode != 0)
1973 compl_cont_status &= ~CONT_LOCAL;
1974 // FALLTHROUGH
1975 default:
1976 // If we have typed at least 2 ^X's... for modes != 0, we set
1977 // compl_cont_status = 0 (eg, as if we had just started ^X
1978 // mode).
1979 // For mode 0, we set "compl_cont_mode" to an impossible
1980 // value, in both cases ^X^X can be used to restart the same
1981 // mode (avoiding ADDING mode).
1982 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
1983 // 'complete' and local ^P expansions respectively.
1984 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
1985 // mode -- Acevedo
1986 if (c == Ctrl_X)
1987 {
1988 if (compl_cont_mode != 0)
1989 compl_cont_status = 0;
1990 else
1991 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
1992 }
1993 ctrl_x_mode = CTRL_X_NORMAL;
1994 edit_submode = NULL;
1995 showmode();
1996 break;
1997 }
1998
1999 return retval;
2000}
2001
2002/*
2003 * Stop insert completion mode
2004 */
2005 static int
2006ins_compl_stop(int c, int prev_mode, int retval)
2007{
2008 char_u *ptr;
2009#ifdef FEAT_CINDENT
2010 int want_cindent;
2011#endif
2012
2013 // Get here when we have finished typing a sequence of ^N and
2014 // ^P or other completion characters in CTRL-X mode. Free up
2015 // memory that was used, and make sure we can redo the insert.
2016 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2017 {
2018 // If any of the original typed text has been changed, eg when
2019 // ignorecase is set, we must add back-spaces to the redo
2020 // buffer. We add as few as necessary to delete just the part
2021 // of the original text that has changed.
2022 // When using the longest match, edited the match or used
2023 // CTRL-E then don't use the current match.
2024 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2025 ptr = compl_curr_match->cp_str;
2026 else
2027 ptr = NULL;
2028 ins_compl_fixRedoBufForLeader(ptr);
2029 }
2030
2031#ifdef FEAT_CINDENT
2032 want_cindent = (get_can_cindent() && cindent_on());
2033#endif
2034 // When completing whole lines: fix indent for 'cindent'.
2035 // Otherwise, break line if it's too long.
2036 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2037 {
2038#ifdef FEAT_CINDENT
2039 // re-indent the current line
2040 if (want_cindent)
2041 {
2042 do_c_expr_indent();
2043 want_cindent = FALSE; // don't do it again
2044 }
2045#endif
2046 }
2047 else
2048 {
2049 int prev_col = curwin->w_cursor.col;
2050
2051 // put the cursor on the last char, for 'tw' formatting
2052 if (prev_col > 0)
2053 dec_cursor();
2054 // only format when something was inserted
2055 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2056 insertchar(NUL, 0, -1);
2057 if (prev_col > 0
2058 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2059 inc_cursor();
2060 }
2061
2062 // If the popup menu is displayed pressing CTRL-Y means accepting
2063 // the selection without inserting anything. When
2064 // compl_enter_selects is set the Enter key does the same.
2065 if ((c == Ctrl_Y || (compl_enter_selects
2066 && (c == CAR || c == K_KENTER || c == NL)))
2067 && pum_visible())
2068 retval = TRUE;
2069
2070 // CTRL-E means completion is Ended, go back to the typed text.
2071 // but only do this, if the Popup is still visible
2072 if (c == Ctrl_E)
2073 {
2074 ins_compl_delete();
2075 if (compl_leader != NULL)
2076 ins_bytes(compl_leader + ins_compl_len());
2077 else if (compl_first_match != NULL)
2078 ins_bytes(compl_orig_text + ins_compl_len());
2079 retval = TRUE;
2080 }
2081
2082 auto_format(FALSE, TRUE);
2083
2084 // Trigger the CompleteDonePre event to give scripts a chance to
2085 // act upon the completion before clearing the info, and restore
2086 // ctrl_x_mode, so that complete_info() can be used.
2087 ctrl_x_mode = prev_mode;
2088 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2089
2090 ins_compl_free();
2091 compl_started = FALSE;
2092 compl_matches = 0;
2093 if (!shortmess(SHM_COMPLETIONMENU))
2094 msg_clr_cmdline(); // necessary for "noshowmode"
2095 ctrl_x_mode = CTRL_X_NORMAL;
2096 compl_enter_selects = FALSE;
2097 if (edit_submode != NULL)
2098 {
2099 edit_submode = NULL;
2100 showmode();
2101 }
2102
2103#ifdef FEAT_CMDWIN
2104 if (c == Ctrl_C && cmdwin_type != 0)
2105 // Avoid the popup menu remains displayed when leaving the
2106 // command line window.
2107 update_screen(0);
2108#endif
2109#ifdef FEAT_CINDENT
2110 // Indent now if a key was typed that is in 'cinkeys'.
2111 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2112 do_c_expr_indent();
2113#endif
2114 // Trigger the CompleteDone event to give scripts a chance to act
2115 // upon the end of completion.
2116 ins_apply_autocmds(EVENT_COMPLETEDONE);
2117
2118 return retval;
2119}
2120
2121/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002122 * Prepare for Insert mode completion, or stop it.
2123 * Called just after typing a character in Insert mode.
2124 * Returns TRUE when the character is not to be inserted;
2125 */
2126 int
2127ins_compl_prep(int c)
2128{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002129 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002130 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002131
2132 // Forget any previous 'special' messages if this is actually
2133 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2134 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2135 edit_submode_extra = NULL;
2136
2137 // Ignore end of Select mode mapping and mouse scroll buttons.
2138 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01002139 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002140 return retval;
2141
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002142#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002143 // Ignore mouse events in a popup window
2144 if (is_mouse_key(c))
2145 {
2146 // Ignore drag and release events, the position does not need to be in
2147 // the popup and it may have just closed.
2148 if (c == K_LEFTRELEASE
2149 || c == K_LEFTRELEASE_NM
2150 || c == K_MIDDLERELEASE
2151 || c == K_RIGHTRELEASE
2152 || c == K_X1RELEASE
2153 || c == K_X2RELEASE
2154 || c == K_LEFTDRAG
2155 || c == K_MIDDLEDRAG
2156 || c == K_RIGHTDRAG
2157 || c == K_X1DRAG
2158 || c == K_X2DRAG)
2159 return retval;
2160 if (popup_visible)
2161 {
2162 int row = mouse_row;
2163 int col = mouse_col;
2164 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2165
2166 if (wp != NULL && WIN_IS_POPUP(wp))
2167 return retval;
2168 }
2169 }
2170#endif
2171
zeertzjqdca29d92021-08-31 19:12:51 +02002172 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2173 {
2174 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2175 || !vim_is_ctrl_x_key(c))
2176 {
2177 // Not starting another completion mode.
2178 ctrl_x_mode = CTRL_X_CMDLINE;
2179
2180 // CTRL-X CTRL-Z should stop completion without inserting anything
2181 if (c == Ctrl_Z)
2182 retval = TRUE;
2183 }
2184 else
2185 {
2186 ctrl_x_mode = CTRL_X_CMDLINE;
2187
2188 // Other CTRL-X keys first stop completion, then start another
2189 // completion mode.
2190 ins_compl_prep(' ');
2191 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2192 }
2193 }
2194
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002195 // Set "compl_get_longest" when finding the first matches.
2196 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
2197 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
2198 {
2199 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2200 compl_used_match = TRUE;
2201
2202 }
2203
2204 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002205 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2206 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002207 retval = set_ctrl_x_mode(c);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002208 else if (ctrl_x_mode != CTRL_X_NORMAL)
2209 {
2210 // We're already in CTRL-X mode, do we stay in it?
2211 if (!vim_is_ctrl_x_key(c))
2212 {
2213 if (ctrl_x_mode == CTRL_X_SCROLL)
2214 ctrl_x_mode = CTRL_X_NORMAL;
2215 else
2216 ctrl_x_mode = CTRL_X_FINISHED;
2217 edit_submode = NULL;
2218 }
2219 showmode();
2220 }
2221
2222 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2223 {
2224 // Show error message from attempted keyword completion (probably
2225 // 'Pattern not found') until another key is hit, then go back to
2226 // showing what mode we are in.
2227 showmode();
2228 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
2229 && c != Ctrl_R && !ins_compl_pum_key(c))
2230 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002231 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002232 }
2233 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2234 // Trigger the CompleteDone event to give scripts a chance to act
2235 // upon the (possibly failed) completion.
2236 ins_apply_autocmds(EVENT_COMPLETEDONE);
2237
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002238 trigger_modechanged();
2239
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002240 // reset continue_* if we left expansion-mode, if we stay they'll be
2241 // (re)set properly in ins_complete()
2242 if (!vim_is_ctrl_x_key(c))
2243 {
2244 compl_cont_status = 0;
2245 compl_cont_mode = 0;
2246 }
2247
2248 return retval;
2249}
2250
2251/*
2252 * Fix the redo buffer for the completion leader replacing some of the typed
2253 * text. This inserts backspaces and appends the changed text.
2254 * "ptr" is the known leader text or NUL.
2255 */
2256 static void
2257ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2258{
2259 int len;
2260 char_u *p;
2261 char_u *ptr = ptr_arg;
2262
2263 if (ptr == NULL)
2264 {
2265 if (compl_leader != NULL)
2266 ptr = compl_leader;
2267 else
2268 return; // nothing to do
2269 }
2270 if (compl_orig_text != NULL)
2271 {
2272 p = compl_orig_text;
2273 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2274 ;
2275 if (len > 0)
2276 len -= (*mb_head_off)(p, p + len);
2277 for (p += len; *p != NUL; MB_PTR_ADV(p))
2278 AppendCharToRedobuff(K_BS);
2279 }
2280 else
2281 len = 0;
2282 if (ptr != NULL)
2283 AppendToRedobuffLit(ptr + len, -1);
2284}
2285
2286/*
2287 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2288 * (depending on flag) starting from buf and looking for a non-scanned
2289 * buffer (other than curbuf). curbuf is special, if it is called with
2290 * buf=curbuf then it has to be the first call for a given flag/expansion.
2291 *
2292 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2293 */
2294 static buf_T *
2295ins_compl_next_buf(buf_T *buf, int flag)
2296{
2297 static win_T *wp = NULL;
2298
2299 if (flag == 'w') // just windows
2300 {
2301 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2302 wp = curwin;
2303 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2304 && wp->w_buffer->b_scanned)
2305 ;
2306 buf = wp->w_buffer;
2307 }
2308 else
2309 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2310 // (unlisted buffers)
2311 // When completing whole lines skip unloaded buffers.
2312 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2313 && ((flag == 'U'
2314 ? buf->b_p_bl
2315 : (!buf->b_p_bl
2316 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2317 || buf->b_scanned))
2318 ;
2319 return buf;
2320}
2321
2322#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002323
2324# ifdef FEAT_EVAL
2325static callback_T cfu_cb; // 'completefunc' callback function
2326static callback_T ofu_cb; // 'omnifunc' callback function
2327static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2328# endif
2329
2330/*
2331 * Copy a global callback function to a buffer local callback.
2332 */
2333 static void
2334copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2335{
2336 free_callback(bufcb);
2337 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2338 copy_callback(bufcb, globcb);
2339}
2340
2341/*
2342 * Parse the 'completefunc' option value and set the callback function.
2343 * Invoked when the 'completefunc' option is set. The option value can be a
2344 * name of a function (string), or function(<name>) or funcref(<name>) or a
2345 * lambda expression.
2346 */
2347 int
2348set_completefunc_option(void)
2349{
2350 int retval;
2351
2352 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2353 if (retval == OK)
2354 set_buflocal_cfu_callback(curbuf);
2355
2356 return retval;
2357}
2358
2359/*
2360 * Copy the global 'completefunc' callback function to the buffer-local
2361 * 'completefunc' callback for 'buf'.
2362 */
2363 void
2364set_buflocal_cfu_callback(buf_T *buf UNUSED)
2365{
2366# ifdef FEAT_EVAL
2367 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2368# endif
2369}
2370
2371/*
2372 * Parse the 'omnifunc' option value and set the callback function.
2373 * Invoked when the 'omnifunc' option is set. The option value can be a
2374 * name of a function (string), or function(<name>) or funcref(<name>) or a
2375 * lambda expression.
2376 */
2377 int
2378set_omnifunc_option(void)
2379{
2380 int retval;
2381
2382 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2383 if (retval == OK)
2384 set_buflocal_ofu_callback(curbuf);
2385
2386 return retval;
2387}
2388
2389/*
2390 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
2391 * callback for 'buf'.
2392 */
2393 void
2394set_buflocal_ofu_callback(buf_T *buf UNUSED)
2395{
2396# ifdef FEAT_EVAL
2397 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2398# endif
2399}
2400
2401/*
2402 * Parse the 'thesaurusfunc' option value and set the callback function.
2403 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2404 * name of a function (string), or function(<name>) or funcref(<name>) or a
2405 * lambda expression.
2406 */
2407 int
2408set_thesaurusfunc_option(void)
2409{
2410 int retval;
2411
2412 if (*curbuf->b_p_tsrfu != NUL)
2413 {
2414 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002415 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2416 &curbuf->b_tsrfu_cb);
2417 }
2418 else
2419 {
2420 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002421 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2422 }
2423
2424 return retval;
2425}
2426
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002427/*
2428 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
2429 * 'copyID' so that they are not garbage collected.
2430 */
2431 int
2432set_ref_in_insexpand_funcs(int copyID)
2433{
2434 int abort = FALSE;
2435
2436 abort = set_ref_in_callback(&cfu_cb, copyID);
2437 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2438 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2439
2440 return abort;
2441}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002442
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002443/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002444 * Get the user-defined completion function name for completion 'type'
2445 */
2446 static char_u *
2447get_complete_funcname(int type)
2448{
2449 switch (type)
2450 {
2451 case CTRL_X_FUNCTION:
2452 return curbuf->b_p_cfu;
2453 case CTRL_X_OMNI:
2454 return curbuf->b_p_ofu;
2455 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002456 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002457 default:
2458 return (char_u *)"";
2459 }
2460}
2461
2462/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002463 * Get the callback to use for insert mode completion.
2464 */
2465 callback_T *
2466get_insert_callback(int type)
2467{
2468 if (type == CTRL_X_FUNCTION)
2469 return &curbuf->b_cfu_cb;
2470 if (type == CTRL_X_OMNI)
2471 return &curbuf->b_ofu_cb;
2472 // CTRL_X_THESAURUS
2473 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2474}
2475
2476/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002477 * Execute user defined complete function 'completefunc', 'omnifunc' or
2478 * 'thesaurusfunc', and get matches in "matches".
2479 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002480 */
2481 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002482expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002483{
2484 list_T *matchlist = NULL;
2485 dict_T *matchdict = NULL;
2486 typval_T args[3];
2487 char_u *funcname;
2488 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002489 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002490 typval_T rettv;
2491 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002492 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002493
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002494 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002495 if (*funcname == NUL)
2496 return;
2497
2498 // Call 'completefunc' to obtain the list of matches.
2499 args[0].v_type = VAR_NUMBER;
2500 args[0].vval.v_number = 0;
2501 args[1].v_type = VAR_STRING;
2502 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2503 args[2].v_type = VAR_UNKNOWN;
2504
2505 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002506 // Lock the text to avoid weird things from happening. Also disallow
2507 // switching to another window, it should not be needed and may end up in
2508 // Insert mode in another buffer.
2509 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002510
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002511 cb = get_insert_callback(type);
2512 retval = call_callback(cb, 0, &rettv, 2, args);
2513
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002514 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002515 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002516 {
2517 switch (rettv.v_type)
2518 {
2519 case VAR_LIST:
2520 matchlist = rettv.vval.v_list;
2521 break;
2522 case VAR_DICT:
2523 matchdict = rettv.vval.v_dict;
2524 break;
2525 case VAR_SPECIAL:
2526 if (rettv.vval.v_number == VVAL_NONE)
2527 compl_opt_suppress_empty = TRUE;
2528 // FALLTHROUGH
2529 default:
2530 // TODO: Give error message?
2531 clear_tv(&rettv);
2532 break;
2533 }
2534 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002535 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002536
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002537 curwin->w_cursor = pos; // restore the cursor position
2538 validate_cursor();
2539 if (!EQUAL_POS(curwin->w_cursor, pos))
2540 {
2541 emsg(_(e_compldel));
2542 goto theend;
2543 }
2544
2545 if (matchlist != NULL)
2546 ins_compl_add_list(matchlist);
2547 else if (matchdict != NULL)
2548 ins_compl_add_dict(matchdict);
2549
2550theend:
2551 // Restore State, it might have been changed.
2552 State = save_State;
2553
2554 if (matchdict != NULL)
2555 dict_unref(matchdict);
2556 if (matchlist != NULL)
2557 list_unref(matchlist);
2558}
2559#endif // FEAT_COMPL_FUNC
2560
2561#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2562/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002563 * Add a match to the list of matches from a typeval_T.
2564 * If the given string is already in the list of completions, then return
2565 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2566 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002567 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002568 */
2569 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002570ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002571{
2572 char_u *word;
2573 int dup = FALSE;
2574 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002575 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002576 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002577 typval_T user_data;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002578
Bram Moolenaar08928322020-01-04 14:32:48 +01002579 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002580 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2581 {
2582 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2583 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2584 (char_u *)"abbr", FALSE);
2585 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2586 (char_u *)"menu", FALSE);
2587 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2588 (char_u *)"kind", FALSE);
2589 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2590 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002591 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002592 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2593 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2594 flags |= CP_ICASE;
2595 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2596 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2597 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2598 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2599 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2600 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2601 flags |= CP_EQUAL;
2602 }
2603 else
2604 {
2605 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002606 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002607 }
2608 if (word == NULL || (!empty && *word == NUL))
2609 return FAIL;
Bram Moolenaar08928322020-01-04 14:32:48 +01002610 return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002611}
2612
2613/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002614 * Add completions from a list.
2615 */
2616 static void
2617ins_compl_add_list(list_T *list)
2618{
2619 listitem_T *li;
2620 int dir = compl_direction;
2621
2622 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002623 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002624 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002625 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002626 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002627 // if dir was BACKWARD then honor it just once
2628 dir = FORWARD;
2629 else if (did_emsg)
2630 break;
2631 }
2632}
2633
2634/*
2635 * Add completions from a dict.
2636 */
2637 static void
2638ins_compl_add_dict(dict_T *dict)
2639{
2640 dictitem_T *di_refresh;
2641 dictitem_T *di_words;
2642
2643 // Check for optional "refresh" item.
2644 compl_opt_refresh_always = FALSE;
2645 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2646 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2647 {
2648 char_u *v = di_refresh->di_tv.vval.v_string;
2649
2650 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2651 compl_opt_refresh_always = TRUE;
2652 }
2653
2654 // Add completions from a "words" list.
2655 di_words = dict_find(dict, (char_u *)"words", 5);
2656 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2657 ins_compl_add_list(di_words->di_tv.vval.v_list);
2658}
2659
2660/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002661 * Start completion for the complete() function.
2662 * "startcol" is where the matched text starts (1 is first column).
2663 * "list" is the list of matches.
2664 */
2665 static void
2666set_completion(colnr_T startcol, list_T *list)
2667{
2668 int save_w_wrow = curwin->w_wrow;
2669 int save_w_leftcol = curwin->w_leftcol;
2670 int flags = CP_ORIGINAL_TEXT;
2671
2672 // If already doing completions stop it.
2673 if (ctrl_x_mode != CTRL_X_NORMAL)
2674 ins_compl_prep(' ');
2675 ins_compl_clear();
2676 ins_compl_free();
2677
2678 compl_direction = FORWARD;
2679 if (startcol > curwin->w_cursor.col)
2680 startcol = curwin->w_cursor.col;
2681 compl_col = startcol;
2682 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2683 // compl_pattern doesn't need to be set
2684 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2685 if (p_ic)
2686 flags |= CP_ICASE;
2687 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002688 -1, NULL, NULL, NULL, 0,
2689 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002690 return;
2691
2692 ctrl_x_mode = CTRL_X_EVAL;
2693
2694 ins_compl_add_list(list);
2695 compl_matches = ins_compl_make_cyclic();
2696 compl_started = TRUE;
2697 compl_used_match = TRUE;
2698 compl_cont_status = 0;
2699
2700 compl_curr_match = compl_first_match;
2701 if (compl_no_insert || compl_no_select)
2702 {
2703 ins_complete(K_DOWN, FALSE);
2704 if (compl_no_select)
2705 // Down/Up has no real effect.
2706 ins_complete(K_UP, FALSE);
2707 }
2708 else
2709 ins_complete(Ctrl_N, FALSE);
2710 compl_enter_selects = compl_no_insert;
2711
2712 // Lazily show the popup menu, unless we got interrupted.
2713 if (!compl_interrupted)
2714 show_pum(save_w_wrow, save_w_leftcol);
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002715 trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002716 out_flush();
2717}
2718
2719/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002720 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002721 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002722 void
2723f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002724{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002725 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002726 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002727
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002728 if (in_vim9script()
2729 && (check_for_number_arg(argvars, 0) == FAIL
2730 || check_for_list_arg(argvars, 1) == FAIL))
2731 return;
2732
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002733 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002734 {
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002735 emsg(_("E785: complete() can only be used in Insert mode"));
2736 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002737 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002738
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002739 // "textlock" is set when evaluating 'completefunc' but we can change
2740 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002741 textlock = 0;
2742
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002743 // Check for undo allowed here, because if something was already inserted
2744 // the line was already saved for undo and this check isn't done.
2745 if (!undo_allowed())
2746 return;
2747
2748 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002749 emsg(_(e_invarg));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002750 else
2751 {
2752 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2753 if (startcol > 0)
2754 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002756 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002757}
2758
2759/*
2760 * "complete_add()" function
2761 */
2762 void
2763f_complete_add(typval_T *argvars, typval_T *rettv)
2764{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002765 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002766 return;
2767
Bram Moolenaar440cf092021-04-03 20:13:30 +02002768 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002769}
2770
2771/*
2772 * "complete_check()" function
2773 */
2774 void
2775f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2776{
2777 int saved = RedrawingDisabled;
2778
2779 RedrawingDisabled = 0;
2780 ins_compl_check_keys(0, TRUE);
2781 rettv->vval.v_number = ins_compl_interrupted();
2782 RedrawingDisabled = saved;
2783}
2784
2785/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002786 * Return Insert completion mode name string
2787 */
2788 static char_u *
2789ins_compl_mode(void)
2790{
zeertzjq27fef592021-10-03 12:01:27 +01002791 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || ctrl_x_mode == CTRL_X_SCROLL
2792 || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002793 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2794
2795 return (char_u *)"";
2796}
2797
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002798 static void
2799ins_compl_update_sequence_numbers()
2800{
2801 int number = 0;
2802 compl_T *match;
2803
2804 if (compl_direction == FORWARD)
2805 {
2806 // search backwards for the first valid (!= -1) number.
2807 // This should normally succeed already at the first loop
2808 // cycle, so it's fast!
2809 for (match = compl_curr_match->cp_prev; match != NULL
2810 && match != compl_first_match;
2811 match = match->cp_prev)
2812 if (match->cp_number != -1)
2813 {
2814 number = match->cp_number;
2815 break;
2816 }
2817 if (match != NULL)
2818 // go up and assign all numbers which are not assigned
2819 // yet
2820 for (match = match->cp_next;
2821 match != NULL && match->cp_number == -1;
2822 match = match->cp_next)
2823 match->cp_number = ++number;
2824 }
2825 else // BACKWARD
2826 {
2827 // search forwards (upwards) for the first valid (!= -1)
2828 // number. This should normally succeed already at the
2829 // first loop cycle, so it's fast!
2830 for (match = compl_curr_match->cp_next; match != NULL
2831 && match != compl_first_match;
2832 match = match->cp_next)
2833 if (match->cp_number != -1)
2834 {
2835 number = match->cp_number;
2836 break;
2837 }
2838 if (match != NULL)
2839 // go down and assign all numbers which are not
2840 // assigned yet
2841 for (match = match->cp_prev; match
2842 && match->cp_number == -1;
2843 match = match->cp_prev)
2844 match->cp_number = ++number;
2845 }
2846}
2847
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002848/*
2849 * Get complete information
2850 */
2851 static void
2852get_complete_info(list_T *what_list, dict_T *retdict)
2853{
2854 int ret = OK;
2855 listitem_T *item;
2856#define CI_WHAT_MODE 0x01
2857#define CI_WHAT_PUM_VISIBLE 0x02
2858#define CI_WHAT_ITEMS 0x04
2859#define CI_WHAT_SELECTED 0x08
2860#define CI_WHAT_INSERTED 0x10
2861#define CI_WHAT_ALL 0xff
2862 int what_flag;
2863
2864 if (what_list == NULL)
2865 what_flag = CI_WHAT_ALL;
2866 else
2867 {
2868 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002869 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002870 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002871 {
2872 char_u *what = tv_get_string(&item->li_tv);
2873
2874 if (STRCMP(what, "mode") == 0)
2875 what_flag |= CI_WHAT_MODE;
2876 else if (STRCMP(what, "pum_visible") == 0)
2877 what_flag |= CI_WHAT_PUM_VISIBLE;
2878 else if (STRCMP(what, "items") == 0)
2879 what_flag |= CI_WHAT_ITEMS;
2880 else if (STRCMP(what, "selected") == 0)
2881 what_flag |= CI_WHAT_SELECTED;
2882 else if (STRCMP(what, "inserted") == 0)
2883 what_flag |= CI_WHAT_INSERTED;
2884 }
2885 }
2886
2887 if (ret == OK && (what_flag & CI_WHAT_MODE))
2888 ret = dict_add_string(retdict, "mode", ins_compl_mode());
2889
2890 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
2891 ret = dict_add_number(retdict, "pum_visible", pum_visible());
2892
2893 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
2894 {
2895 list_T *li;
2896 dict_T *di;
2897 compl_T *match;
2898
2899 li = list_alloc();
2900 if (li == NULL)
2901 return;
2902 ret = dict_add_list(retdict, "items", li);
2903 if (ret == OK && compl_first_match != NULL)
2904 {
2905 match = compl_first_match;
2906 do
2907 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002908 if (!ins_compl_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002909 {
2910 di = dict_alloc();
2911 if (di == NULL)
2912 return;
2913 ret = list_append_dict(li, di);
2914 if (ret != OK)
2915 return;
2916 dict_add_string(di, "word", match->cp_str);
2917 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
2918 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
2919 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
2920 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002921 if (match->cp_user_data.v_type == VAR_UNKNOWN)
2922 // Add an empty string for backwards compatibility
2923 dict_add_string(di, "user_data", (char_u *)"");
2924 else
2925 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002926 }
2927 match = match->cp_next;
2928 }
2929 while (match != NULL && match != compl_first_match);
2930 }
2931 }
2932
2933 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002934 {
2935 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
2936 ins_compl_update_sequence_numbers();
2937 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
2938 ? compl_curr_match->cp_number - 1 : -1);
2939 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002940
2941 // TODO
2942 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
2943}
2944
2945/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002946 * "complete_info()" function
2947 */
2948 void
2949f_complete_info(typval_T *argvars, typval_T *rettv)
2950{
2951 list_T *what_list = NULL;
2952
2953 if (rettv_dict_alloc(rettv) != OK)
2954 return;
2955
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002956 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
2957 return;
2958
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002959 if (argvars[0].v_type != VAR_UNKNOWN)
2960 {
2961 if (argvars[0].v_type != VAR_LIST)
2962 {
2963 emsg(_(e_listreq));
2964 return;
2965 }
2966 what_list = argvars[0].vval.v_list;
2967 }
2968 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002969}
2970#endif
2971
2972/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002973 * Returns TRUE when using a user-defined function for thesaurus completion.
2974 */
2975 static int
2976thesaurus_func_complete(int type UNUSED)
2977{
2978#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002979 return type == CTRL_X_THESAURUS
2980 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002981#else
2982 return FALSE;
2983#endif
2984}
2985
2986/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00002987 * Return value of process_next_cpt_value()
2988 */
2989enum
2990{
2991 INS_COMPL_CPT_OK = 1,
2992 INS_COMPL_CPT_CONT,
2993 INS_COMPL_CPT_END
2994};
2995
2996/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002997 * state information used for getting the next set of insert completion
2998 * matches.
2999 */
3000typedef struct
3001{
3002 char_u *e_cpt; // current entry in 'complete'
3003 buf_T *ins_buf; // buffer being scanned
3004 pos_T *cur_match_pos; // current match position
3005 pos_T prev_match_pos; // previous match position
3006 int set_match_pos; // save first_match_pos/last_match_pos
3007 pos_T first_match_pos; // first match position
3008 pos_T last_match_pos; // last match position
3009 int found_all; // found all matches of a certain type.
3010 char_u *dict; // dictionary file to search
3011 int dict_f; // "dict" is an exact file name or not
3012} ins_compl_next_state_T;
3013
3014/*
3015 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003016 *
3017 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003018 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003019 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003020 * st->found_all - all matches of this type are found
3021 * st->ins_buf - search for completions in this buffer
3022 * st->first_match_pos - position of the first completion match
3023 * st->last_match_pos - position of the last completion match
3024 * st->set_match_pos - TRUE if the first match position should be saved to
3025 * avoid loops after the search wraps around.
3026 * st->dict - name of the dictionary or thesaurus file to search
3027 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003028 *
3029 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
3030 * Returns INS_COMPL_CPT_CONT to skip the current value and process the next
3031 * option value.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003032 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003033 */
3034 static int
3035process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003036 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003037 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003038 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003039{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003040 int compl_type = -1;
3041 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003042
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003043 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003044
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003045 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3046 st->e_cpt++;
3047
3048 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003049 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003050 st->ins_buf = curbuf;
3051 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003052 // Move the cursor back one character so that ^N can match the
3053 // word immediately after the cursor.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003054 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003055 {
3056 // Move the cursor to after the last character in the
3057 // buffer, so that word at start of buffer is found
3058 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003059 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3060 st->first_match_pos.col =
3061 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003062 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003063 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003064 compl_type = 0;
3065
3066 // Remember the first match so that the loop stops when we
3067 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003068 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003069 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003070 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3071 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003072 {
3073 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003074 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003075 {
3076 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003077 st->first_match_pos.col = st->last_match_pos.col = 0;
3078 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3079 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003080 compl_type = 0;
3081 }
3082 else // unloaded buffer, scan like dictionary
3083 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003084 st->found_all = TRUE;
3085 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003086 {
3087 status = INS_COMPL_CPT_CONT;
3088 goto done;
3089 }
3090 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003091 st->dict = st->ins_buf->b_fname;
3092 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003093 }
3094 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3095 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003096 st->ins_buf->b_fname == NULL
3097 ? buf_spname(st->ins_buf)
3098 : st->ins_buf->b_sfname == NULL
3099 ? st->ins_buf->b_fname
3100 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003101 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3102 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003103 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003104 status = INS_COMPL_CPT_END;
3105 else
3106 {
3107 if (ctrl_x_mode_line_or_eval())
3108 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003109 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003110 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003111 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003112 compl_type = CTRL_X_DICTIONARY;
3113 else
3114 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003115 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003116 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003117 st->dict = st->e_cpt;
3118 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003119 }
3120 }
3121#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003122 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003123 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003124 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003125 compl_type = CTRL_X_PATH_DEFINES;
3126#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003127 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003128 {
3129 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3130 compl_type = CTRL_X_TAGS;
3131 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3132 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3133 }
3134 else
3135 compl_type = -1;
3136
3137 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003138 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003139
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003140 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003141 if (compl_type == -1)
3142 status = INS_COMPL_CPT_CONT;
3143 }
3144
3145done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003146 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003147 return status;
3148}
3149
3150#ifdef FEAT_FIND_ID
3151/*
3152 * Get the next set of identifiers or defines matching "compl_pattern" in
3153 * included files.
3154 */
3155 static void
3156get_next_include_file_completion(int compl_type)
3157{
3158 find_pattern_in_path(compl_pattern, compl_direction,
3159 (int)STRLEN(compl_pattern), FALSE, FALSE,
3160 (compl_type == CTRL_X_PATH_DEFINES
3161 && !(compl_cont_status & CONT_SOL))
3162 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3163 (linenr_T)1, (linenr_T)MAXLNUM);
3164}
3165#endif
3166
3167/*
3168 * Get the next set of words matching "compl_pattern" in dictionary or
3169 * thesaurus files.
3170 */
3171 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003172get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003173{
3174#ifdef FEAT_COMPL_FUNC
3175 if (thesaurus_func_complete(compl_type))
3176 expand_by_function(compl_type, compl_pattern);
3177 else
3178#endif
3179 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003180 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003181 : (compl_type == CTRL_X_THESAURUS
3182 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3183 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3184 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003185 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003186 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003187}
3188
3189/*
3190 * Get the next set of tag names matching "compl_pattern".
3191 */
3192 static void
3193get_next_tag_completion(void)
3194{
3195 int save_p_ic;
3196 char_u **matches;
3197 int num_matches;
3198
3199 // set p_ic according to p_ic, p_scs and pat for find_tags().
3200 save_p_ic = p_ic;
3201 p_ic = ignorecase(compl_pattern);
3202
3203 // Find up to TAG_MANY matches. Avoids that an enormous number
3204 // of matches is found when compl_pattern is empty
3205 g_tag_at_cursor = TRUE;
3206 if (find_tags(compl_pattern, &num_matches, &matches,
3207 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
3208 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
3209 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3210 ins_compl_add_matches(num_matches, matches, p_ic);
3211 g_tag_at_cursor = FALSE;
3212 p_ic = save_p_ic;
3213}
3214
3215/*
3216 * Get the next set of filename matching "compl_pattern".
3217 */
3218 static void
3219get_next_filename_completion(void)
3220{
3221 char_u **matches;
3222 int num_matches;
3223
3224 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3225 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3226 return;
3227
3228 // May change home directory back to "~".
3229 tilde_replace(compl_pattern, num_matches, matches);
3230#ifdef BACKSLASH_IN_FILENAME
3231 if (curbuf->b_p_csl[0] != NUL)
3232 {
3233 int i;
3234
3235 for (i = 0; i < num_matches; ++i)
3236 {
3237 char_u *ptr = matches[i];
3238
3239 while (*ptr != NUL)
3240 {
3241 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3242 *ptr = '/';
3243 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3244 *ptr = '\\';
3245 ptr += (*mb_ptr2len)(ptr);
3246 }
3247 }
3248 }
3249#endif
3250 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3251}
3252
3253/*
3254 * Get the next set of command-line completions matching "compl_pattern".
3255 */
3256 static void
3257get_next_cmdline_completion()
3258{
3259 char_u **matches;
3260 int num_matches;
3261
3262 if (expand_cmdline(&compl_xp, compl_pattern,
3263 (int)STRLEN(compl_pattern),
3264 &num_matches, &matches) == EXPAND_OK)
3265 ins_compl_add_matches(num_matches, matches, FALSE);
3266}
3267
3268/*
3269 * Get the next set of spell suggestions matching "compl_pattern".
3270 */
3271 static void
3272get_next_spell_completion(linenr_T lnum UNUSED)
3273{
3274#ifdef FEAT_SPELL
3275 char_u **matches;
3276 int num_matches;
3277
3278 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3279 if (num_matches > 0)
3280 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003281 else
3282 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003283#endif
3284}
3285
3286/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003287 * Return the next word or line from buffer "ins_buf" at position
3288 * "cur_match_pos" for completion. The length of the match is set in "len".
3289 */
3290 static char_u *
3291ins_comp_get_next_word_or_line(
3292 buf_T *ins_buf, // buffer being scanned
3293 pos_T *cur_match_pos, // current match position
3294 int *match_len,
3295 int *cont_s_ipos) // next ^X<> will set initial_pos
3296{
3297 char_u *ptr;
3298 int len;
3299
3300 *match_len = 0;
3301 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3302 cur_match_pos->col;
3303 if (ctrl_x_mode_line_or_eval())
3304 {
3305 if (compl_cont_status & CONT_ADDING)
3306 {
3307 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3308 return NULL;
3309 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3310 if (!p_paste)
3311 ptr = skipwhite(ptr);
3312 }
3313 len = (int)STRLEN(ptr);
3314 }
3315 else
3316 {
3317 char_u *tmp_ptr = ptr;
3318
3319 if (compl_cont_status & CONT_ADDING)
3320 {
3321 tmp_ptr += compl_length;
3322 // Skip if already inside a word.
3323 if (vim_iswordp(tmp_ptr))
3324 return NULL;
3325 // Find start of next word.
3326 tmp_ptr = find_word_start(tmp_ptr);
3327 }
3328 // Find end of this word.
3329 tmp_ptr = find_word_end(tmp_ptr);
3330 len = (int)(tmp_ptr - ptr);
3331
3332 if ((compl_cont_status & CONT_ADDING) && len == compl_length)
3333 {
3334 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3335 {
3336 // Try next line, if any. the new word will be
3337 // "join" as if the normal command "J" was used.
3338 // IOSIZE is always greater than
3339 // compl_length, so the next STRNCPY always
3340 // works -- Acevedo
3341 STRNCPY(IObuff, ptr, len);
3342 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3343 tmp_ptr = ptr = skipwhite(ptr);
3344 // Find start of next word.
3345 tmp_ptr = find_word_start(tmp_ptr);
3346 // Find end of next word.
3347 tmp_ptr = find_word_end(tmp_ptr);
3348 if (tmp_ptr > ptr)
3349 {
3350 if (*ptr != ')' && IObuff[len - 1] != TAB)
3351 {
3352 if (IObuff[len - 1] != ' ')
3353 IObuff[len++] = ' ';
3354 // IObuf =~ "\k.* ", thus len >= 2
3355 if (p_js
3356 && (IObuff[len - 2] == '.'
3357 || (vim_strchr(p_cpo, CPO_JOINSP)
3358 == NULL
3359 && (IObuff[len - 2] == '?'
3360 || IObuff[len - 2] == '!'))))
3361 IObuff[len++] = ' ';
3362 }
3363 // copy as much as possible of the new word
3364 if (tmp_ptr - ptr >= IOSIZE - len)
3365 tmp_ptr = ptr + IOSIZE - len - 1;
3366 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3367 len += (int)(tmp_ptr - ptr);
3368 *cont_s_ipos = TRUE;
3369 }
3370 IObuff[len] = NUL;
3371 ptr = IObuff;
3372 }
3373 if (len == compl_length)
3374 return NULL;
3375 }
3376 }
3377
3378 *match_len = len;
3379 return ptr;
3380}
3381
3382/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003383 * Get the next set of words matching "compl_pattern" for default completion(s)
3384 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003385 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3386 * position "st->start_pos" in the "compl_direction" direction. If
3387 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3388 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003389 * Returns OK if a new next match is found, otherwise returns FAIL.
3390 */
3391 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003392get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003393{
3394 int found_new_match = FAIL;
3395 int save_p_scs;
3396 int save_p_ws;
3397 int looped_around = FALSE;
3398 char_u *ptr;
3399 int len;
3400
3401 // If 'infercase' is set, don't use 'smartcase' here
3402 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003403 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003404 p_scs = FALSE;
3405
3406 // Buffers other than curbuf are scanned from the beginning or the
3407 // end but never from the middle, thus setting nowrapscan in this
3408 // buffer is a good idea, on the other hand, we always set
3409 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3410 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003411 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003412 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003413 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003414 p_ws = TRUE;
3415 looped_around = FALSE;
3416 for (;;)
3417 {
3418 int cont_s_ipos = FALSE;
3419
3420 ++msg_silent; // Don't want messages for wrapscan.
3421
3422 // ctrl_x_mode_line_or_eval() || word-wise search that
3423 // has added a word that was at the beginning of the line
3424 if (ctrl_x_mode_line_or_eval()
3425 || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003426 found_new_match = search_for_exact_line(st->ins_buf,
3427 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003428 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003429 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3430 NULL, compl_direction, compl_pattern, 1L,
3431 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003432 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003433 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003434 {
3435 // set "compl_started" even on fail
3436 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003437 st->first_match_pos = *st->cur_match_pos;
3438 st->last_match_pos = *st->cur_match_pos;
3439 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003440 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003441 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3442 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003443 {
3444 found_new_match = FAIL;
3445 }
3446 else if ((compl_direction == FORWARD)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003447 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3448 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3449 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003450 {
3451 if (looped_around)
3452 found_new_match = FAIL;
3453 else
3454 looped_around = TRUE;
3455 }
3456 else if ((compl_direction != FORWARD)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003457 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3458 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3459 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003460 {
3461 if (looped_around)
3462 found_new_match = FAIL;
3463 else
3464 looped_around = TRUE;
3465 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003466 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003467 if (found_new_match == FAIL)
3468 break;
3469
3470 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003471 if ((compl_cont_status & CONT_ADDING) && st->ins_buf == curbuf
3472 && start_pos->lnum == st->cur_match_pos->lnum
3473 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003474 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003475
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003476 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3477 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003478 if (ptr == NULL)
3479 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003480
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003481 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003482 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003483 0, cont_s_ipos) != NOTDONE)
3484 {
3485 found_new_match = OK;
3486 break;
3487 }
3488 }
3489 p_scs = save_p_scs;
3490 p_ws = save_p_ws;
3491
3492 return found_new_match;
3493}
3494
3495/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003496 * get the next set of completion matches for 'type'.
3497 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003498 */
3499 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003500get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003501{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003502 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003503
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003504 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003505 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003506 case -1:
3507 break;
3508#ifdef FEAT_FIND_ID
3509 case CTRL_X_PATH_PATTERNS:
3510 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003511 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003512 break;
3513#endif
3514
3515 case CTRL_X_DICTIONARY:
3516 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003517 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3518 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003519 break;
3520
3521 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003522 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003523 break;
3524
3525 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003526 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003527 break;
3528
3529 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003530 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003531 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003532 break;
3533
3534#ifdef FEAT_COMPL_FUNC
3535 case CTRL_X_FUNCTION:
3536 case CTRL_X_OMNI:
3537 expand_by_function(type, compl_pattern);
3538 break;
3539#endif
3540
3541 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003542 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003543 break;
3544
3545 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003546 found_new_match = get_next_default_completion(st, ini);
3547 if (found_new_match == FAIL && st->ins_buf == curbuf)
3548 st->found_all = TRUE;
3549 }
3550
3551 // check if compl_curr_match has changed, (e.g. other type of
3552 // expansion added something)
3553 if (type != 0 && compl_curr_match != compl_old_match)
3554 found_new_match = OK;
3555
3556 return found_new_match;
3557}
3558
3559/*
3560 * Get the next expansion(s), using "compl_pattern".
3561 * The search starts at position "ini" in curbuf and in the direction
3562 * compl_direction.
3563 * When "compl_started" is FALSE start at that position, otherwise continue
3564 * where we stopped searching before.
3565 * This may return before finding all the matches.
3566 * Return the total number of matches or -1 if still unknown -- Acevedo
3567 */
3568 static int
3569ins_compl_get_exp(pos_T *ini)
3570{
3571 static ins_compl_next_state_T st;
3572 int i;
3573 int found_new_match;
3574 int type = ctrl_x_mode;
3575
3576 if (!compl_started)
3577 {
3578 FOR_ALL_BUFFERS(st.ins_buf)
3579 st.ins_buf->b_scanned = 0;
3580 st.found_all = FALSE;
3581 st.ins_buf = curbuf;
3582 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3583 ? (char_u *)"." : curbuf->b_p_cpt;
3584 st.last_match_pos = st.first_match_pos = *ini;
3585 }
3586 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3587 st.ins_buf = curbuf; // In case the buffer was wiped out.
3588
3589 compl_old_match = compl_curr_match; // remember the last current match
3590 st.cur_match_pos = (compl_direction == FORWARD)
3591 ? &st.last_match_pos : &st.first_match_pos;
3592
3593 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3594 for (;;)
3595 {
3596 found_new_match = FAIL;
3597 st.set_match_pos = FALSE;
3598
3599 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3600 // or if found_all says this entry is done. For ^X^L only use the
3601 // entries from 'complete' that look in loaded buffers.
3602 if ((ctrl_x_mode == CTRL_X_NORMAL
3603 || ctrl_x_mode_line_or_eval())
3604 && (!compl_started || st.found_all))
3605 {
3606 int status = process_next_cpt_value(&st, &type, ini);
3607
3608 if (status == INS_COMPL_CPT_END)
3609 break;
3610 if (status == INS_COMPL_CPT_CONT)
3611 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003612 }
3613
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003614 // If complete() was called then compl_pattern has been reset. The
3615 // following won't work then, bail out.
3616 if (compl_pattern == NULL)
3617 break;
3618
3619 // get the next set of completion matches
3620 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003621
3622 // break the loop for specialized modes (use 'complete' just for the
3623 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3624 // match
3625 if ((ctrl_x_mode != CTRL_X_NORMAL
3626 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3627 {
3628 if (got_int)
3629 break;
3630 // Fill the popup menu as soon as possible.
3631 if (type != -1)
3632 ins_compl_check_keys(0, FALSE);
3633
3634 if ((ctrl_x_mode != CTRL_X_NORMAL
3635 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3636 break;
3637 compl_started = TRUE;
3638 }
3639 else
3640 {
3641 // Mark a buffer scanned when it has been scanned completely
3642 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003643 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003644
3645 compl_started = FALSE;
3646 }
3647 }
3648 compl_started = TRUE;
3649
3650 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003651 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003652 found_new_match = FAIL;
3653
3654 i = -1; // total of matches, unknown
3655 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3656 && !ctrl_x_mode_line_or_eval()))
3657 i = ins_compl_make_cyclic();
3658
3659 if (compl_old_match != NULL)
3660 {
3661 // If several matches were added (FORWARD) or the search failed and has
3662 // just been made cyclic then we have to move compl_curr_match to the
3663 // next or previous entry (if any) -- Acevedo
3664 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3665 : compl_old_match->cp_prev;
3666 if (compl_curr_match == NULL)
3667 compl_curr_match = compl_old_match;
3668 }
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003669 trigger_modechanged();
3670
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003671 return i;
3672}
3673
3674/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003675 * Update "compl_shown_match" to the actually shown match, it may differ when
3676 * "compl_leader" is used to omit some of the matches.
3677 */
3678 static void
3679ins_compl_update_shown_match(void)
3680{
3681 while (!ins_compl_equal(compl_shown_match,
3682 compl_leader, (int)STRLEN(compl_leader))
3683 && compl_shown_match->cp_next != NULL
3684 && compl_shown_match->cp_next != compl_first_match)
3685 compl_shown_match = compl_shown_match->cp_next;
3686
3687 // If we didn't find it searching forward, and compl_shows_dir is
3688 // backward, find the last match.
3689 if (compl_shows_dir == BACKWARD
3690 && !ins_compl_equal(compl_shown_match,
3691 compl_leader, (int)STRLEN(compl_leader))
3692 && (compl_shown_match->cp_next == NULL
3693 || compl_shown_match->cp_next == compl_first_match))
3694 {
3695 while (!ins_compl_equal(compl_shown_match,
3696 compl_leader, (int)STRLEN(compl_leader))
3697 && compl_shown_match->cp_prev != NULL
3698 && compl_shown_match->cp_prev != compl_first_match)
3699 compl_shown_match = compl_shown_match->cp_prev;
3700 }
3701}
3702
3703/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003704 * Delete the old text being completed.
3705 */
3706 void
3707ins_compl_delete(void)
3708{
3709 int col;
3710
3711 // In insert mode: Delete the typed part.
3712 // In replace mode: Put the old characters back, if any.
3713 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3714 if ((int)curwin->w_cursor.col > col)
3715 {
3716 if (stop_arrow() == FAIL)
3717 return;
3718 backspace_until_column(col);
3719 }
3720
3721 // TODO: is this sufficient for redrawing? Redrawing everything causes
3722 // flicker, thus we can't do that.
3723 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003724#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003725 // clear v:completed_item
3726 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003727#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003728}
3729
3730/*
3731 * Insert the new text being completed.
3732 * "in_compl_func" is TRUE when called from complete_check().
3733 */
3734 void
3735ins_compl_insert(int in_compl_func)
3736{
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003737 int compl_len = ins_compl_len();
3738
3739 // Make sure we don't go over the end of the string, this can happen with
3740 // illegal bytes.
3741 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3742 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003743 if (ins_compl_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003744 compl_used_match = FALSE;
3745 else
3746 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003747#ifdef FEAT_EVAL
3748 {
3749 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3750
3751 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3752 }
3753#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003754 if (!in_compl_func)
3755 compl_curr_match = compl_shown_match;
3756}
3757
3758/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003759 * show the file name for the completion match (if any). Truncate the file
3760 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003761 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003762 static void
3763ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003764{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003765 char *lead = _("match in file");
3766 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3767 char_u *s;
3768 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003769
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003770 if (space <= 0)
3771 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003772
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003773 // We need the tail that fits. With double-byte encoding going
3774 // back from the end is very slow, thus go from the start and keep
3775 // the text that fits in "space" between "s" and "e".
3776 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003777 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003778 space -= ptr2cells(e);
3779 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003780 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003781 space += ptr2cells(s);
3782 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003783 }
3784 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003785 msg_hist_off = TRUE;
3786 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3787 s > compl_shown_match->cp_fname ? "<" : "", s);
3788 msg((char *)IObuff);
3789 msg_hist_off = FALSE;
3790 redraw_cmdline = FALSE; // don't overwrite!
3791}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003792
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003793/*
3794 * Find the next set of matches for completion. Repeat the completion 'todo'
3795 * times. The number of matches found is returned in 'num_matches'.
3796 *
3797 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3798 * get more completions. If it is FALSE, then do nothing when there are no more
3799 * completions in the given direction.
3800 *
3801 * If "advance" is TRUE, then completion will move to the first match.
3802 * Otherwise, the original text will be shown.
3803 *
3804 * Returns OK on success and -1 if the number of matches are unknown.
3805 */
3806 static int
3807find_next_completion_match(
3808 int allow_get_expansion,
3809 int todo, // repeat completion this many times
3810 int advance,
3811 int *num_matches)
3812{
3813 int found_end = FALSE;
3814 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003815
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003816 while (--todo >= 0)
3817 {
3818 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3819 {
3820 compl_shown_match = compl_shown_match->cp_next;
3821 found_end = (compl_first_match != NULL
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003822 && (compl_shown_match->cp_next == compl_first_match
3823 || compl_shown_match == compl_first_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003824 }
3825 else if (compl_shows_dir == BACKWARD
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003826 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003827 {
3828 found_end = (compl_shown_match == compl_first_match);
3829 compl_shown_match = compl_shown_match->cp_prev;
3830 found_end |= (compl_shown_match == compl_first_match);
3831 }
3832 else
3833 {
3834 if (!allow_get_expansion)
3835 {
3836 if (advance)
3837 {
3838 if (compl_shows_dir == BACKWARD)
3839 compl_pending -= todo + 1;
3840 else
3841 compl_pending += todo + 1;
3842 }
3843 return -1;
3844 }
3845
3846 if (!compl_no_select && advance)
3847 {
3848 if (compl_shows_dir == BACKWARD)
3849 --compl_pending;
3850 else
3851 ++compl_pending;
3852 }
3853
3854 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003855 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003856
3857 // handle any pending completions
3858 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003859 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003860 {
3861 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3862 {
3863 compl_shown_match = compl_shown_match->cp_next;
3864 --compl_pending;
3865 }
3866 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3867 {
3868 compl_shown_match = compl_shown_match->cp_prev;
3869 ++compl_pending;
3870 }
3871 else
3872 break;
3873 }
3874 found_end = FALSE;
3875 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003876 if (!ins_compl_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003877 && compl_leader != NULL
3878 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003879 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003880 ++todo;
3881 else
3882 // Remember a matching item.
3883 found_compl = compl_shown_match;
3884
3885 // Stop at the end of the list when we found a usable match.
3886 if (found_end)
3887 {
3888 if (found_compl != NULL)
3889 {
3890 compl_shown_match = found_compl;
3891 break;
3892 }
3893 todo = 1; // use first usable match after wrapping around
3894 }
3895 }
3896
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003897 return OK;
3898}
3899
3900/*
3901 * Fill in the next completion in the current direction.
3902 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3903 * get more completions. If it is FALSE, then we just do nothing when there
3904 * are no more completions in a given direction. The latter case is used when
3905 * we are still in the middle of finding completions, to allow browsing
3906 * through the ones found so far.
3907 * Return the total number of matches, or -1 if still unknown -- webb.
3908 *
3909 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3910 * compl_shown_match here.
3911 *
3912 * Note that this function may be called recursively once only. First with
3913 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3914 * calls this function with "allow_get_expansion" FALSE.
3915 */
3916 static int
3917ins_compl_next(
3918 int allow_get_expansion,
3919 int count, // repeat completion this many times; should
3920 // be at least 1
3921 int insert_match, // Insert the newly selected match
3922 int in_compl_func) // called from complete_check()
3923{
3924 int num_matches = -1;
3925 int todo = count;
3926 int advance;
3927 int started = compl_started;
3928
3929 // When user complete function return -1 for findstart which is next
3930 // time of 'always', compl_shown_match become NULL.
3931 if (compl_shown_match == NULL)
3932 return -1;
3933
3934 if (compl_leader != NULL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003935 && !ins_compl_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003936 // Update "compl_shown_match" to the actually shown match
3937 ins_compl_update_shown_match();
3938
3939 if (allow_get_expansion && insert_match
3940 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3941 // Delete old text to be replaced
3942 ins_compl_delete();
3943
3944 // When finding the longest common text we stick at the original text,
3945 // don't let CTRL-N or CTRL-P move to the first match.
3946 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3947
3948 // When restarting the search don't insert the first match either.
3949 if (compl_restarting)
3950 {
3951 advance = FALSE;
3952 compl_restarting = FALSE;
3953 }
3954
3955 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3956 // around.
3957 if (find_next_completion_match(allow_get_expansion, todo, advance,
3958 &num_matches) == -1)
3959 return -1;
3960
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003961 // Insert the text of the new completion, or the compl_leader.
3962 if (compl_no_insert && !started)
3963 {
3964 ins_bytes(compl_orig_text + ins_compl_len());
3965 compl_used_match = FALSE;
3966 }
3967 else if (insert_match)
3968 {
3969 if (!compl_get_longest || compl_used_match)
3970 ins_compl_insert(in_compl_func);
3971 else
3972 ins_bytes(compl_leader + ins_compl_len());
3973 }
3974 else
3975 compl_used_match = FALSE;
3976
3977 if (!allow_get_expansion)
3978 {
3979 // may undisplay the popup menu first
3980 ins_compl_upd_pum();
3981
3982 if (pum_enough_matches())
3983 // Will display the popup menu, don't redraw yet to avoid flicker.
3984 pum_call_update_screen();
3985 else
3986 // Not showing the popup menu yet, redraw to show the user what was
3987 // inserted.
3988 update_screen(0);
3989
3990 // display the updated popup menu
3991 ins_compl_show_pum();
3992#ifdef FEAT_GUI
3993 if (gui.in_use)
3994 {
3995 // Show the cursor after the match, not after the redrawn text.
3996 setcursor();
3997 out_flush_cursor(FALSE, FALSE);
3998 }
3999#endif
4000
4001 // Delete old text to be replaced, since we're still searching and
4002 // don't want to match ourselves!
4003 ins_compl_delete();
4004 }
4005
4006 // Enter will select a match when the match wasn't inserted and the popup
4007 // menu is visible.
4008 if (compl_no_insert && !started)
4009 compl_enter_selects = TRUE;
4010 else
4011 compl_enter_selects = !insert_match && compl_match_array != NULL;
4012
4013 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004014 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004015 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004016
4017 return num_matches;
4018}
4019
4020/*
4021 * Call this while finding completions, to check whether the user has hit a key
4022 * that should change the currently displayed completion, or exit completion
4023 * mode. Also, when compl_pending is not zero, show a completion as soon as
4024 * possible. -- webb
4025 * "frequency" specifies out of how many calls we actually check.
4026 * "in_compl_func" is TRUE when called from complete_check(), don't set
4027 * compl_curr_match.
4028 */
4029 void
4030ins_compl_check_keys(int frequency, int in_compl_func)
4031{
4032 static int count = 0;
4033 int c;
4034
4035 // Don't check when reading keys from a script, :normal or feedkeys().
4036 // That would break the test scripts. But do check for keys when called
4037 // from complete_check().
4038 if (!in_compl_func && (using_script() || ex_normal_busy))
4039 return;
4040
4041 // Only do this at regular intervals
4042 if (++count < frequency)
4043 return;
4044 count = 0;
4045
4046 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4047 // can't do its work correctly.
4048 c = vpeekc_any();
4049 if (c != NUL)
4050 {
4051 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4052 {
4053 c = safe_vgetc(); // Eat the character
4054 compl_shows_dir = ins_compl_key2dir(c);
4055 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4056 c != K_UP && c != K_DOWN, in_compl_func);
4057 }
4058 else
4059 {
4060 // Need to get the character to have KeyTyped set. We'll put it
4061 // back with vungetc() below. But skip K_IGNORE.
4062 c = safe_vgetc();
4063 if (c != K_IGNORE)
4064 {
4065 // Don't interrupt completion when the character wasn't typed,
4066 // e.g., when doing @q to replay keys.
4067 if (c != Ctrl_R && KeyTyped)
4068 compl_interrupted = TRUE;
4069
4070 vungetc(c);
4071 }
4072 }
4073 }
4074 if (compl_pending != 0 && !got_int && !compl_no_insert)
4075 {
4076 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4077
4078 compl_pending = 0;
4079 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4080 }
4081}
4082
4083/*
4084 * Decide the direction of Insert mode complete from the key typed.
4085 * Returns BACKWARD or FORWARD.
4086 */
4087 static int
4088ins_compl_key2dir(int c)
4089{
4090 if (c == Ctrl_P || c == Ctrl_L
4091 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4092 return BACKWARD;
4093 return FORWARD;
4094}
4095
4096/*
4097 * Return TRUE for keys that are used for completion only when the popup menu
4098 * is visible.
4099 */
4100 static int
4101ins_compl_pum_key(int c)
4102{
4103 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4104 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4105 || c == K_UP || c == K_DOWN);
4106}
4107
4108/*
4109 * Decide the number of completions to move forward.
4110 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4111 */
4112 static int
4113ins_compl_key2count(int c)
4114{
4115 int h;
4116
4117 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4118 {
4119 h = pum_get_height();
4120 if (h > 3)
4121 h -= 2; // keep some context
4122 return h;
4123 }
4124 return 1;
4125}
4126
4127/*
4128 * Return TRUE if completion with "c" should insert the match, FALSE if only
4129 * to change the currently selected completion.
4130 */
4131 static int
4132ins_compl_use_match(int c)
4133{
4134 switch (c)
4135 {
4136 case K_UP:
4137 case K_DOWN:
4138 case K_PAGEDOWN:
4139 case K_KPAGEDOWN:
4140 case K_S_DOWN:
4141 case K_PAGEUP:
4142 case K_KPAGEUP:
4143 case K_S_UP:
4144 return FALSE;
4145 }
4146 return TRUE;
4147}
4148
4149/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004150 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4151 * completion)
4152 * Sets the global variables: compl_col, compl_length and compl_pattern.
4153 * Uses the global variables: compl_cont_status and ctrl_x_mode
4154 */
4155 static int
4156get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4157{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004158 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004159 {
4160 if (!(compl_cont_status & CONT_ADDING))
4161 {
4162 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4163 ;
4164 compl_col += ++startcol;
4165 compl_length = curs_col - startcol;
4166 }
4167 if (p_ic)
4168 compl_pattern = str_foldcase(line + compl_col,
4169 compl_length, NULL, 0);
4170 else
4171 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4172 if (compl_pattern == NULL)
4173 return FAIL;
4174 }
4175 else if (compl_cont_status & CONT_ADDING)
4176 {
4177 char_u *prefix = (char_u *)"\\<";
4178
4179 // we need up to 2 extra chars for the prefix
4180 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4181 compl_length) + 2);
4182 if (compl_pattern == NULL)
4183 return FAIL;
4184 if (!vim_iswordp(line + compl_col)
4185 || (compl_col > 0
4186 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4187 prefix = (char_u *)"";
4188 STRCPY((char *)compl_pattern, prefix);
4189 (void)quote_meta(compl_pattern + STRLEN(prefix),
4190 line + compl_col, compl_length);
4191 }
4192 else if (--startcol < 0
4193 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4194 {
4195 // Match any word of at least two chars
4196 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4197 if (compl_pattern == NULL)
4198 return FAIL;
4199 compl_col += curs_col;
4200 compl_length = 0;
4201 }
4202 else
4203 {
4204 // Search the point of change class of multibyte character
4205 // or not a word single byte character backward.
4206 if (has_mbyte)
4207 {
4208 int base_class;
4209 int head_off;
4210
4211 startcol -= (*mb_head_off)(line, line + startcol);
4212 base_class = mb_get_class(line + startcol);
4213 while (--startcol >= 0)
4214 {
4215 head_off = (*mb_head_off)(line, line + startcol);
4216 if (base_class != mb_get_class(line + startcol
4217 - head_off))
4218 break;
4219 startcol -= head_off;
4220 }
4221 }
4222 else
4223 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4224 ;
4225 compl_col += ++startcol;
4226 compl_length = (int)curs_col - startcol;
4227 if (compl_length == 1)
4228 {
4229 // Only match word with at least two chars -- webb
4230 // there's no need to call quote_meta,
4231 // alloc(7) is enough -- Acevedo
4232 compl_pattern = alloc(7);
4233 if (compl_pattern == NULL)
4234 return FAIL;
4235 STRCPY((char *)compl_pattern, "\\<");
4236 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4237 STRCAT((char *)compl_pattern, "\\k");
4238 }
4239 else
4240 {
4241 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4242 compl_length) + 2);
4243 if (compl_pattern == NULL)
4244 return FAIL;
4245 STRCPY((char *)compl_pattern, "\\<");
4246 (void)quote_meta(compl_pattern + 2, line + compl_col,
4247 compl_length);
4248 }
4249 }
4250
4251 return OK;
4252}
4253
4254/*
4255 * Get the pattern, column and length for whole line completion or for the
4256 * complete() function.
4257 * Sets the global variables: compl_col, compl_length and compl_pattern.
4258 */
4259 static int
4260get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4261{
4262 compl_col = (colnr_T)getwhitecols(line);
4263 compl_length = (int)curs_col - (int)compl_col;
4264 if (compl_length < 0) // cursor in indent: empty pattern
4265 compl_length = 0;
4266 if (p_ic)
4267 compl_pattern = str_foldcase(line + compl_col, compl_length,
4268 NULL, 0);
4269 else
4270 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4271 if (compl_pattern == NULL)
4272 return FAIL;
4273
4274 return OK;
4275}
4276
4277/*
4278 * Get the pattern, column and length for filename completion.
4279 * Sets the global variables: compl_col, compl_length and compl_pattern.
4280 */
4281 static int
4282get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4283{
4284 // Go back to just before the first filename character.
4285 if (startcol > 0)
4286 {
4287 char_u *p = line + startcol;
4288
4289 MB_PTR_BACK(line, p);
4290 while (p > line && vim_isfilec(PTR2CHAR(p)))
4291 MB_PTR_BACK(line, p);
4292 if (p == line && vim_isfilec(PTR2CHAR(p)))
4293 startcol = 0;
4294 else
4295 startcol = (int)(p - line) + 1;
4296 }
4297
4298 compl_col += startcol;
4299 compl_length = (int)curs_col - startcol;
4300 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4301 if (compl_pattern == NULL)
4302 return FAIL;
4303
4304 return OK;
4305}
4306
4307/*
4308 * Get the pattern, column and length for command-line completion.
4309 * Sets the global variables: compl_col, compl_length and compl_pattern.
4310 */
4311 static int
4312get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4313{
4314 compl_pattern = vim_strnsave(line, curs_col);
4315 if (compl_pattern == NULL)
4316 return FAIL;
4317 set_cmd_context(&compl_xp, compl_pattern,
4318 (int)STRLEN(compl_pattern), curs_col, FALSE);
4319 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4320 || compl_xp.xp_context == EXPAND_NOTHING)
4321 // No completion possible, use an empty pattern to get a
4322 // "pattern not found" message.
4323 compl_col = curs_col;
4324 else
4325 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4326 compl_length = curs_col - compl_col;
4327
4328 return OK;
4329}
4330
4331/*
4332 * Get the pattern, column and length for user defined completion ('omnifunc',
4333 * 'completefunc' and 'thesaurusfunc')
4334 * Sets the global variables: compl_col, compl_length and compl_pattern.
4335 * Uses the global variable: spell_bad_len
4336 */
4337 static int
4338get_userdefined_compl_info(colnr_T curs_col UNUSED)
4339{
4340 int ret = FAIL;
4341
4342#ifdef FEAT_COMPL_FUNC
4343 // Call user defined function 'completefunc' with "a:findstart"
4344 // set to 1 to obtain the length of text to use for completion.
4345 char_u *line;
4346 typval_T args[3];
4347 int col;
4348 char_u *funcname;
4349 pos_T pos;
4350 int save_State = State;
4351 callback_T *cb;
4352
4353 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4354 // length as a string
4355 funcname = get_complete_funcname(ctrl_x_mode);
4356 if (*funcname == NUL)
4357 {
4358 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4359 ? "completefunc" : "omnifunc");
4360 return FAIL;
4361 }
4362
4363 args[0].v_type = VAR_NUMBER;
4364 args[0].vval.v_number = 1;
4365 args[1].v_type = VAR_STRING;
4366 args[1].vval.v_string = (char_u *)"";
4367 args[2].v_type = VAR_UNKNOWN;
4368 pos = curwin->w_cursor;
4369 ++textwinlock;
4370 cb = get_insert_callback(ctrl_x_mode);
4371 col = call_callback_retnr(cb, 2, args);
4372 --textwinlock;
4373
4374 State = save_State;
4375 curwin->w_cursor = pos; // restore the cursor position
4376 validate_cursor();
4377 if (!EQUAL_POS(curwin->w_cursor, pos))
4378 {
4379 emsg(_(e_compldel));
4380 return FAIL;
4381 }
4382
4383 // Return value -2 means the user complete function wants to
4384 // cancel the complete without an error.
4385 // Return value -3 does the same as -2 and leaves CTRL-X mode.
4386 if (col == -2)
4387 return FAIL;
4388 if (col == -3)
4389 {
4390 ctrl_x_mode = CTRL_X_NORMAL;
4391 edit_submode = NULL;
4392 if (!shortmess(SHM_COMPLETIONMENU))
4393 msg_clr_cmdline();
4394 return FAIL;
4395 }
4396
4397 // Reset extended parameters of completion, when start new
4398 // completion.
4399 compl_opt_refresh_always = FALSE;
4400 compl_opt_suppress_empty = FALSE;
4401
4402 if (col < 0)
4403 col = curs_col;
4404 compl_col = col;
4405 if (compl_col > curs_col)
4406 compl_col = curs_col;
4407
4408 // Setup variables for completion. Need to obtain "line" again,
4409 // it may have become invalid.
4410 line = ml_get(curwin->w_cursor.lnum);
4411 compl_length = curs_col - compl_col;
4412 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4413 if (compl_pattern == NULL)
4414 return FAIL;
4415
4416 ret = OK;
4417#endif
4418
4419 return ret;
4420}
4421
4422/*
4423 * Get the pattern, column and length for spell completion.
4424 * Sets the global variables: compl_col, compl_length and compl_pattern.
4425 * Uses the global variable: spell_bad_len
4426 */
4427 static int
4428get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4429{
4430 int ret = FAIL;
4431#ifdef FEAT_SPELL
4432 char_u *line;
4433
4434 if (spell_bad_len > 0)
4435 compl_col = curs_col - spell_bad_len;
4436 else
4437 compl_col = spell_word_start(startcol);
4438 if (compl_col >= (colnr_T)startcol)
4439 {
4440 compl_length = 0;
4441 compl_col = curs_col;
4442 }
4443 else
4444 {
4445 spell_expand_check_cap(compl_col);
4446 compl_length = (int)curs_col - compl_col;
4447 }
4448 // Need to obtain "line" again, it may have become invalid.
4449 line = ml_get(curwin->w_cursor.lnum);
4450 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4451 if (compl_pattern == NULL)
4452 return FAIL;
4453
4454 ret = OK;
4455#endif
4456
4457 return ret;
4458}
4459
4460/*
4461 * Get the completion pattern, column and length.
4462 */
4463 static int
4464compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4465{
4466 if (ctrl_x_mode == CTRL_X_NORMAL
4467 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4468 && !thesaurus_func_complete(ctrl_x_mode)))
4469 {
4470 return get_normal_compl_info(line, startcol, curs_col);
4471 }
4472 else if (ctrl_x_mode_line_or_eval())
4473 {
4474 return get_wholeline_compl_info(line, curs_col);
4475 }
4476 else if (ctrl_x_mode == CTRL_X_FILES)
4477 {
4478 return get_filename_compl_info(line, startcol, curs_col);
4479 }
4480 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4481 {
4482 return get_cmdline_compl_info(line, curs_col);
4483 }
4484 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
4485 || thesaurus_func_complete(ctrl_x_mode))
4486 {
4487 if (get_userdefined_compl_info(curs_col) == FAIL)
4488 return FAIL;
4489 *line_invalid = TRUE; // 'line' may have become invalid
4490 }
4491 else if (ctrl_x_mode == CTRL_X_SPELL)
4492 {
4493 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4494 return FAIL;
4495 *line_invalid = TRUE; // 'line' may have become invalid
4496 }
4497 else
4498 {
4499 internal_error("ins_complete()");
4500 return FAIL;
4501 }
4502
4503 return OK;
4504}
4505
4506/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004507 * Continue an interrupted completion mode search in "line".
4508 *
4509 * If this same ctrl_x_mode has been interrupted use the text from
4510 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4511 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4512 * the same line as the cursor then fix it (the line has been split because it
4513 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4514 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004515 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004516 static void
4517ins_compl_continue_search(char_u *line)
4518{
4519 // it is a continued search
4520 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
4521 if (ctrl_x_mode == CTRL_X_NORMAL
4522 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4523 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4524 {
4525 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4526 {
4527 // line (probably) wrapped, set compl_startpos to the
4528 // first non_blank in the line, if it is not a wordchar
4529 // include it to get a better pattern, but then we don't
4530 // want the "\\<" prefix, check it below
4531 compl_col = (colnr_T)getwhitecols(line);
4532 compl_startpos.col = compl_col;
4533 compl_startpos.lnum = curwin->w_cursor.lnum;
4534 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4535 }
4536 else
4537 {
4538 // S_IPOS was set when we inserted a word that was at the
4539 // beginning of the line, which means that we'll go to SOL
4540 // mode but first we need to redefine compl_startpos
4541 if (compl_cont_status & CONT_S_IPOS)
4542 {
4543 compl_cont_status |= CONT_SOL;
4544 compl_startpos.col = (colnr_T)(skipwhite(
4545 line + compl_length
4546 + compl_startpos.col) - line);
4547 }
4548 compl_col = compl_startpos.col;
4549 }
4550 compl_length = curwin->w_cursor.col - (int)compl_col;
4551 // IObuff is used to add a "word from the next line" would we
4552 // have enough space? just being paranoid
4553#define MIN_SPACE 75
4554 if (compl_length > (IOSIZE - MIN_SPACE))
4555 {
4556 compl_cont_status &= ~CONT_SOL;
4557 compl_length = (IOSIZE - MIN_SPACE);
4558 compl_col = curwin->w_cursor.col - compl_length;
4559 }
4560 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4561 if (compl_length < 1)
4562 compl_cont_status &= CONT_LOCAL;
4563 }
4564 else if (ctrl_x_mode_line_or_eval())
4565 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4566 else
4567 compl_cont_status = 0;
4568}
4569
4570/*
4571 * start insert mode completion
4572 */
4573 static int
4574ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004575{
4576 char_u *line;
4577 int startcol = 0; // column where searched text starts
4578 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004579 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004580 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004581 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004582
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004583 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004584
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004585 did_ai = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004586#ifdef FEAT_SMARTINDENT
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004587 did_si = FALSE;
4588 can_si = FALSE;
4589 can_si_back = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004590#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004591 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004592 return FAIL;
4593
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004594 line = ml_get(curwin->w_cursor.lnum);
4595 curs_col = curwin->w_cursor.col;
4596 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004597
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004598 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4599 && compl_cont_mode == ctrl_x_mode)
4600 // this same ctrl-x_mode was interrupted previously. Continue the
4601 // completion.
4602 ins_compl_continue_search(line);
4603 else
4604 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004605
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004606 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004607 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004608 compl_cont_mode = ctrl_x_mode;
4609 if (ctrl_x_mode != CTRL_X_NORMAL)
4610 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4611 compl_cont_status = 0;
4612 compl_cont_status |= CONT_N_ADDS;
4613 compl_startpos = curwin->w_cursor;
4614 startcol = (int)curs_col;
4615 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004616 }
4617
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004618 // Work out completion pattern and original text -- webb
4619 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4620 {
4621 if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
4622 || thesaurus_func_complete(ctrl_x_mode))
4623 // restore did_ai, so that adding comment leader works
4624 did_ai = save_did_ai;
4625 return FAIL;
4626 }
4627 // If "line" was changed while getting completion info get it again.
4628 if (line_invalid)
4629 line = ml_get(curwin->w_cursor.lnum);
4630
4631 if (compl_cont_status & CONT_ADDING)
4632 {
4633 edit_submode_pre = (char_u *)_(" Adding");
4634 if (ctrl_x_mode_line_or_eval())
4635 {
4636 // Insert a new line, keep indentation but ignore 'comments'.
4637 char_u *old = curbuf->b_p_com;
4638
4639 curbuf->b_p_com = (char_u *)"";
4640 compl_startpos.lnum = curwin->w_cursor.lnum;
4641 compl_startpos.col = compl_col;
4642 ins_eol('\r');
4643 curbuf->b_p_com = old;
4644 compl_length = 0;
4645 compl_col = curwin->w_cursor.col;
4646 }
4647 }
4648 else
4649 {
4650 edit_submode_pre = NULL;
4651 compl_startpos.col = compl_col;
4652 }
4653
4654 if (compl_cont_status & CONT_LOCAL)
4655 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4656 else
4657 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4658
4659 // If any of the original typed text has been changed we need to fix
4660 // the redo buffer.
4661 ins_compl_fixRedoBufForLeader(NULL);
4662
4663 // Always add completion for the original text.
4664 vim_free(compl_orig_text);
4665 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4666 if (p_ic)
4667 flags |= CP_ICASE;
4668 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4669 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4670 {
4671 VIM_CLEAR(compl_pattern);
4672 VIM_CLEAR(compl_orig_text);
4673 return FAIL;
4674 }
4675
4676 // showmode might reset the internal line pointers, so it must
4677 // be called before line = ml_get(), or when this address is no
4678 // longer needed. -- Acevedo.
4679 edit_submode_extra = (char_u *)_("-- Searching...");
4680 edit_submode_highl = HLF_COUNT;
4681 showmode();
4682 edit_submode_extra = NULL;
4683 out_flush();
4684
4685 return OK;
4686}
4687
4688/*
4689 * display the completion status message
4690 */
4691 static void
4692ins_compl_show_statusmsg(void)
4693{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004694 // we found no match if the list has only the "compl_orig_text"-entry
4695 if (compl_first_match == compl_first_match->cp_next)
4696 {
4697 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4698 && compl_length > 1
4699 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4700 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004701 }
4702
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004703 if (edit_submode_extra == NULL)
4704 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004705 if (ins_compl_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004706 {
4707 edit_submode_extra = (char_u *)_("Back at original");
4708 edit_submode_highl = HLF_W;
4709 }
4710 else if (compl_cont_status & CONT_S_IPOS)
4711 {
4712 edit_submode_extra = (char_u *)_("Word from other line");
4713 edit_submode_highl = HLF_COUNT;
4714 }
4715 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4716 {
4717 edit_submode_extra = (char_u *)_("The only match");
4718 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004719 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004720 }
4721 else
4722 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004723#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004724 // Update completion sequence number when needed.
4725 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004726 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004727#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004728 // The match should always have a sequence number now, this is
4729 // just a safety check.
4730 if (compl_curr_match->cp_number != -1)
4731 {
4732 // Space for 10 text chars. + 2x10-digit no.s = 31.
4733 // Translations may need more than twice that.
4734 static char_u match_ref[81];
4735
4736 if (compl_matches > 0)
4737 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004738 _("match %d of %d"),
4739 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004740 else
4741 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004742 _("match %d"),
4743 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004744 edit_submode_extra = match_ref;
4745 edit_submode_highl = HLF_R;
4746 if (dollar_vcol >= 0)
4747 curs_columns(FALSE);
4748 }
4749 }
4750 }
4751
4752 // Show a message about what (completion) mode we're in.
4753 if (!compl_opt_suppress_empty)
4754 {
4755 showmode();
4756 if (!shortmess(SHM_COMPLETIONMENU))
4757 {
4758 if (edit_submode_extra != NULL)
4759 {
4760 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004761 {
4762 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004763 msg_attr((char *)edit_submode_extra,
4764 edit_submode_highl < HLF_COUNT
4765 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004766 msg_hist_off = FALSE;
4767 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004768 }
4769 else
4770 msg_clr_cmdline(); // necessary for "noshowmode"
4771 }
4772 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004773}
4774
4775/*
4776 * Do Insert mode completion.
4777 * Called when character "c" was typed, which has a meaning for completion.
4778 * Returns OK if completion was done, FAIL if something failed (out of mem).
4779 */
4780 int
4781ins_complete(int c, int enable_pum)
4782{
4783 int n;
4784 int save_w_wrow;
4785 int save_w_leftcol;
4786 int insert_match;
4787
4788 compl_direction = ins_compl_key2dir(c);
4789 insert_match = ins_compl_use_match(c);
4790
4791 if (!compl_started)
4792 {
4793 if (ins_compl_start() == FAIL)
4794 return FAIL;
4795 }
4796 else if (insert_match && stop_arrow() == FAIL)
4797 return FAIL;
4798
4799 compl_shown_match = compl_curr_match;
4800 compl_shows_dir = compl_direction;
4801
4802 // Find next match (and following matches).
4803 save_w_wrow = curwin->w_wrow;
4804 save_w_leftcol = curwin->w_leftcol;
4805 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4806
4807 // may undisplay the popup menu
4808 ins_compl_upd_pum();
4809
4810 if (n > 1) // all matches have been found
4811 compl_matches = n;
4812 compl_curr_match = compl_shown_match;
4813 compl_direction = compl_shows_dir;
4814
4815 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4816 // mode.
4817 if (got_int && !global_busy)
4818 {
4819 (void)vgetc();
4820 got_int = FALSE;
4821 }
4822
4823 // we found no match if the list has only the "compl_orig_text"-entry
4824 if (compl_first_match == compl_first_match->cp_next)
4825 {
4826 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4827 // because we couldn't expand anything at first place, but if we used
4828 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4829 // (such as M in M'exico) if not tried already. -- Acevedo
4830 if (compl_length > 1
4831 || (compl_cont_status & CONT_ADDING)
4832 || (ctrl_x_mode != CTRL_X_NORMAL
4833 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4834 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4835 compl_cont_status &= ~CONT_N_ADDS;
4836 }
4837
4838 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4839 compl_cont_status |= CONT_S_IPOS;
4840 else
4841 compl_cont_status &= ~CONT_S_IPOS;
4842
4843 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004844
4845 // Show the popup menu, unless we got interrupted.
4846 if (enable_pum && !compl_interrupted)
4847 show_pum(save_w_wrow, save_w_leftcol);
4848
4849 compl_was_interrupted = compl_interrupted;
4850 compl_interrupted = FALSE;
4851
4852 return OK;
4853}
4854
4855 static void
4856show_pum(int prev_w_wrow, int prev_w_leftcol)
4857{
4858 // RedrawingDisabled may be set when invoked through complete().
4859 int n = RedrawingDisabled;
4860
4861 RedrawingDisabled = 0;
4862
4863 // If the cursor moved or the display scrolled we need to remove the pum
4864 // first.
4865 setcursor();
4866 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4867 ins_compl_del_pum();
4868
4869 ins_compl_show_pum();
4870 setcursor();
4871 RedrawingDisabled = n;
4872}
4873
4874/*
4875 * Looks in the first "len" chars. of "src" for search-metachars.
4876 * If dest is not NULL the chars. are copied there quoting (with
4877 * a backslash) the metachars, and dest would be NUL terminated.
4878 * Returns the length (needed) of dest
4879 */
4880 static unsigned
4881quote_meta(char_u *dest, char_u *src, int len)
4882{
4883 unsigned m = (unsigned)len + 1; // one extra for the NUL
4884
4885 for ( ; --len >= 0; src++)
4886 {
4887 switch (*src)
4888 {
4889 case '.':
4890 case '*':
4891 case '[':
4892 if (ctrl_x_mode == CTRL_X_DICTIONARY
4893 || ctrl_x_mode == CTRL_X_THESAURUS)
4894 break;
4895 // FALLTHROUGH
4896 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004897 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004898 break;
4899 // FALLTHROUGH
4900 case '\\':
4901 if (ctrl_x_mode == CTRL_X_DICTIONARY
4902 || ctrl_x_mode == CTRL_X_THESAURUS)
4903 break;
4904 // FALLTHROUGH
4905 case '^': // currently it's not needed.
4906 case '$':
4907 m++;
4908 if (dest != NULL)
4909 *dest++ = '\\';
4910 break;
4911 }
4912 if (dest != NULL)
4913 *dest++ = *src;
4914 // Copy remaining bytes of a multibyte character.
4915 if (has_mbyte)
4916 {
4917 int i, mb_len;
4918
4919 mb_len = (*mb_ptr2len)(src) - 1;
4920 if (mb_len > 0 && len >= mb_len)
4921 for (i = 0; i < mb_len; ++i)
4922 {
4923 --len;
4924 ++src;
4925 if (dest != NULL)
4926 *dest++ = *src;
4927 }
4928 }
4929 }
4930 if (dest != NULL)
4931 *dest = NUL;
4932
4933 return m;
4934}
4935
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004936#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004937 void
4938free_insexpand_stuff(void)
4939{
4940 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00004941# ifdef FEAT_EVAL
4942 free_callback(&cfu_cb);
4943 free_callback(&ofu_cb);
4944 free_callback(&tsrfu_cb);
4945# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004946}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004947#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004948
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004949#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004950/*
4951 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4952 * spelled word, if there is one.
4953 */
4954 static void
4955spell_back_to_badword(void)
4956{
4957 pos_T tpos = curwin->w_cursor;
4958
4959 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4960 if (curwin->w_cursor.col != tpos.col)
4961 start_arrow(&tpos);
4962}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004963#endif