blob: 89e0ebd81888d13b748a91adbf065ec677dadce4 [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");
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127
128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000134 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100135 */
136static compl_T *compl_first_match = NULL;
137static compl_T *compl_curr_match = NULL;
138static compl_T *compl_shown_match = NULL;
139static compl_T *compl_old_match = NULL;
140
141// After using a cursor key <Enter> selects a match in the popup menu,
142// otherwise it inserts a line break.
143static int compl_enter_selects = FALSE;
144
145// When "compl_leader" is not NULL only matches that start with this string
146// are used.
147static char_u *compl_leader = NULL;
148
149static int compl_get_longest = FALSE; // put longest common string
150 // in compl_leader
151
152static int compl_no_insert = FALSE; // FALSE: select & insert
153 // TRUE: noinsert
154static int compl_no_select = FALSE; // FALSE: select & insert
155 // TRUE: noselect
156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static 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
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static 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 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
266int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
270int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
271int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
272int ctrl_x_mode_path_patterns(void) {
273 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
274int ctrl_x_mode_path_defines(void) {
275 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
276int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
277int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200278int ctrl_x_mode_cmdline(void) {
279 return ctrl_x_mode == CTRL_X_CMDLINE
280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100281int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
282int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
283int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000284static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100285int ctrl_x_mode_line_or_eval(void) {
286 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
287
288/*
289 * Whether other than default completion has been selected.
290 */
291 int
292ctrl_x_mode_not_default(void)
293{
294 return ctrl_x_mode != CTRL_X_NORMAL;
295}
296
297/*
zeertzjqdca29d92021-08-31 19:12:51 +0200298 * Whether CTRL-X was typed without a following character,
299 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300 */
301 int
302ctrl_x_mode_not_defined_yet(void)
303{
304 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
305}
306
307/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000308 * Return TRUE if currently in "normal" or "adding" insert completion matches
309 * state
310 */
311 int
312compl_status_adding(void)
313{
314 return compl_cont_status & CONT_ADDING;
315}
316
317/*
318 * Return TRUE if the completion pattern includes start of line, just for
319 * word-wise expansion.
320 */
321 int
322compl_status_sol(void)
323{
324 return compl_cont_status & CONT_SOL;
325}
326
327/*
328 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
329 */
330 int
331compl_status_local(void)
332{
333 return compl_cont_status & CONT_LOCAL;
334}
335
336/*
337 * Clear the completion status flags
338 */
339 void
340compl_status_clear(void)
341{
342 compl_cont_status = 0;
343}
344
345/*
346 * Return TRUE if completion is using the forward direction matches
347 */
348 static int
349compl_dir_forward(void)
350{
351 return compl_direction == FORWARD;
352}
353
354/*
355 * Return TRUE if currently showing forward completion matches
356 */
357 static int
358compl_shows_dir_forward(void)
359{
360 return compl_shows_dir == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing backward completion matches
365 */
366 static int
367compl_shows_dir_backward(void)
368{
369 return compl_shows_dir == BACKWARD;
370}
371
372/*
373 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100374 */
375 int
376has_compl_option(int dict_opt)
377{
378 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200379#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100380 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200381#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100382 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100383 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
384#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100385 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386#endif
387 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 {
389 ctrl_x_mode = CTRL_X_NORMAL;
390 edit_submode = NULL;
391 msg_attr(dict_opt ? _("'dictionary' option is empty")
392 : _("'thesaurus' option is empty"),
393 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100394 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 {
396 vim_beep(BO_COMPL);
397 setcursor();
398 out_flush();
399#ifdef FEAT_EVAL
400 if (!get_vim_var_nr(VV_TESTING))
401#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100402 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 }
404 return FALSE;
405 }
406 return TRUE;
407}
408
409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000410 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100411 * This depends on the current mode.
412 */
413 int
414vim_is_ctrl_x_key(int c)
415{
416 // Always allow ^R - let its results then be checked
417 if (c == Ctrl_R)
418 return TRUE;
419
420 // Accept <PageUp> and <PageDown> if the popup menu is visible.
421 if (ins_compl_pum_key(c))
422 return TRUE;
423
424 switch (ctrl_x_mode)
425 {
426 case 0: // Not in any CTRL-X mode
427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
428 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200429 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100430 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
431 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
432 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
433 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
434 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200435 || c == Ctrl_S || c == Ctrl_K || c == 's'
436 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437 case CTRL_X_SCROLL:
438 return (c == Ctrl_Y || c == Ctrl_E);
439 case CTRL_X_WHOLE_LINE:
440 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
441 case CTRL_X_FILES:
442 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
443 case CTRL_X_DICTIONARY:
444 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_THESAURUS:
446 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_TAGS:
448 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
449#ifdef FEAT_FIND_ID
450 case CTRL_X_PATH_PATTERNS:
451 return (c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_PATH_DEFINES:
453 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
454#endif
455 case CTRL_X_CMDLINE:
456 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
457 || c == Ctrl_X);
458#ifdef FEAT_COMPL_FUNC
459 case CTRL_X_FUNCTION:
460 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_OMNI:
462 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_SPELL:
465 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_EVAL:
467 return (c == Ctrl_P || c == Ctrl_N);
468 }
469 internal_error("vim_is_ctrl_x_key()");
470 return FALSE;
471}
472
473/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000474 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000475 */
476 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478{
479 return match->cp_flags & CP_ORIGINAL_TEXT;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Returns TRUE if "match" is the first match in the completion list.
484 */
485 static int
486is_first_match(compl_T *match)
487{
488 return match == compl_first_match;
489}
490
491/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100492 * Return TRUE when character "c" is part of the item currently being
493 * completed. Used to decide whether to abandon complete mode when the menu
494 * is visible.
495 */
496 int
497ins_compl_accept_char(int c)
498{
499 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
500 // When expanding an identifier only accept identifier chars.
501 return vim_isIDc(c);
502
503 switch (ctrl_x_mode)
504 {
505 case CTRL_X_FILES:
506 // When expanding file name only accept file name chars. But not
507 // path separators, so that "proto/<Tab>" expands files in
508 // "proto", not "proto/" as a whole
509 return vim_isfilec(c) && !vim_ispathsep(c);
510
511 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200512 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 case CTRL_X_OMNI:
514 // Command line and Omni completion can work with just about any
515 // printable character, but do stop at white space.
516 return vim_isprintc(c) && !VIM_ISWHITE(c);
517
518 case CTRL_X_WHOLE_LINE:
519 // For while line completion a space can be part of the line.
520 return vim_isprintc(c);
521 }
522 return vim_iswordc(c);
523}
524
525/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000526 * Get the completed text by inferring the case of the originally typed text.
527 */
528 static char_u *
529ins_compl_infercase_gettext(
530 char_u *str,
531 int actual_len,
532 int actual_compl_length,
533 int min_len)
534{
535 int *wca; // Wide character array.
536 char_u *p;
537 int i, c;
538 int has_lower = FALSE;
539 int was_letter = FALSE;
540
541 IObuff[0] = NUL;
542
543 // Allocate wide character array for the completion and fill it.
544 wca = ALLOC_MULT(int, actual_len);
545 if (wca == NULL)
546 return IObuff;
547
548 p = str;
549 for (i = 0; i < actual_len; ++i)
550 if (has_mbyte)
551 wca[i] = mb_ptr2char_adv(&p);
552 else
553 wca[i] = *(p++);
554
555 // Rule 1: Were any chars converted to lower?
556 p = compl_orig_text;
557 for (i = 0; i < min_len; ++i)
558 {
559 if (has_mbyte)
560 c = mb_ptr2char_adv(&p);
561 else
562 c = *(p++);
563 if (MB_ISLOWER(c))
564 {
565 has_lower = TRUE;
566 if (MB_ISUPPER(wca[i]))
567 {
568 // Rule 1 is satisfied.
569 for (i = actual_compl_length; i < actual_len; ++i)
570 wca[i] = MB_TOLOWER(wca[i]);
571 break;
572 }
573 }
574 }
575
576 // Rule 2: No lower case, 2nd consecutive letter converted to
577 // upper case.
578 if (!has_lower)
579 {
580 p = compl_orig_text;
581 for (i = 0; i < min_len; ++i)
582 {
583 if (has_mbyte)
584 c = mb_ptr2char_adv(&p);
585 else
586 c = *(p++);
587 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
588 {
589 // Rule 2 is satisfied.
590 for (i = actual_compl_length; i < actual_len; ++i)
591 wca[i] = MB_TOUPPER(wca[i]);
592 break;
593 }
594 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
595 }
596 }
597
598 // Copy the original case of the part we typed.
599 p = compl_orig_text;
600 for (i = 0; i < min_len; ++i)
601 {
602 if (has_mbyte)
603 c = mb_ptr2char_adv(&p);
604 else
605 c = *(p++);
606 if (MB_ISLOWER(c))
607 wca[i] = MB_TOLOWER(wca[i]);
608 else if (MB_ISUPPER(c))
609 wca[i] = MB_TOUPPER(wca[i]);
610 }
611
612 // Generate encoding specific output from wide character array.
613 // Multi-byte characters can occupy up to five bytes more than
614 // ASCII characters, and we also need one byte for NUL, so stay
615 // six bytes away from the edge of IObuff.
616 p = IObuff;
617 i = 0;
618 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
619 if (has_mbyte)
620 p += (*mb_char2bytes)(wca[i++], p);
621 else
622 *(p++) = wca[i++];
623 *p = NUL;
624
625 vim_free(wca);
626
627 return IObuff;
628}
629
630/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
632 * case of the originally typed text is used, and the case of the completed
633 * text is inferred, ie this tries to work out what case you probably wanted
634 * the rest of the word to be in -- webb
635 */
636 int
637ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200638 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100639 int len,
640 int icase,
641 char_u *fname,
642 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200643 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100644{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200645 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100646 char_u *p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100647 int actual_len; // Take multi-byte characters
648 int actual_compl_length; // into account.
649 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200650 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100651
652 if (p_ic && curbuf->b_p_inf && len > 0)
653 {
654 // Infer case of completed part.
655
656 // Find actual length of completion.
657 if (has_mbyte)
658 {
659 p = str;
660 actual_len = 0;
661 while (*p != NUL)
662 {
663 MB_PTR_ADV(p);
664 ++actual_len;
665 }
666 }
667 else
668 actual_len = len;
669
670 // Find actual length of original text.
671 if (has_mbyte)
672 {
673 p = compl_orig_text;
674 actual_compl_length = 0;
675 while (*p != NUL)
676 {
677 MB_PTR_ADV(p);
678 ++actual_compl_length;
679 }
680 }
681 else
682 actual_compl_length = compl_length;
683
684 // "actual_len" may be smaller than "actual_compl_length" when using
685 // thesaurus, only use the minimum when comparing.
686 min_len = actual_len < actual_compl_length
687 ? actual_len : actual_compl_length;
688
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000689 str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
690 min_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200692 if (cont_s_ipos)
693 flags |= CP_CONT_S_IPOS;
694 if (icase)
695 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696
Bram Moolenaar08928322020-01-04 14:32:48 +0100697 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698}
699
700/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000701 * Add a match to the list of matches. The arguments are:
702 * str - text of the match to add
703 * len - length of "str". If -1, then the length of "str" is
704 * computed.
705 * fname - file name to associate with this match.
706 * cptext - list of strings to use with this match (for abbr, menu, info
707 * and kind)
708 * user_data - user supplied data (any vim type) for this match
709 * cdir - match direction. If 0, use "compl_direction".
710 * flags_arg - match flags (cp_flags)
711 * adup - accept this match even if it is already present.
712 * If "cdir" is FORWARD, then the match is added after the current match.
713 * Otherwise, it is added before the current match.
714 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 * If the given string is already in the list of completions, then return
716 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
717 * maybe because alloc() returns NULL, then FAIL is returned.
718 */
719 static int
720ins_compl_add(
721 char_u *str,
722 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 char_u **cptext, // extra text for popup menu or NULL
725 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100726 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200727 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100728 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729{
730 compl_T *match;
731 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200732 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733
Bram Moolenaarceb06192021-04-04 15:05:22 +0200734 if (flags & CP_FAST)
735 fast_breakcheck();
736 else
737 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 if (got_int)
739 return FAIL;
740 if (len < 0)
741 len = (int)STRLEN(str);
742
743 // If the same match is already present, don't add it.
744 if (compl_first_match != NULL && !adup)
745 {
746 match = compl_first_match;
747 do
748 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000749 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100750 && STRNCMP(match->cp_str, str, len) == 0
751 && match->cp_str[len] == NUL)
752 return NOTDONE;
753 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000754 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100755 }
756
757 // Remove any popup menu before changing the list of matches.
758 ins_compl_del_pum();
759
760 // Allocate a new match structure.
761 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200762 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100763 if (match == NULL)
764 return FAIL;
765 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200766 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100767 match->cp_number = 0;
768 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
769 {
770 vim_free(match);
771 return FAIL;
772 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100773
774 // match-fname is:
775 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200776 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 // - NULL otherwise. --Acevedo
778 if (fname != NULL
779 && compl_curr_match != NULL
780 && compl_curr_match->cp_fname != NULL
781 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
782 match->cp_fname = compl_curr_match->cp_fname;
783 else if (fname != NULL)
784 {
785 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200786 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100787 }
788 else
789 match->cp_fname = NULL;
790 match->cp_flags = flags;
791
792 if (cptext != NULL)
793 {
794 int i;
795
796 for (i = 0; i < CPT_COUNT; ++i)
797 if (cptext[i] != NULL && *cptext[i] != NUL)
798 match->cp_text[i] = vim_strsave(cptext[i]);
799 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100800#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100801 if (user_data != NULL)
802 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100803#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000805 // Link the new match structure after (FORWARD) or before (BACKWARD) the
806 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 if (compl_first_match == NULL)
808 match->cp_next = match->cp_prev = NULL;
809 else if (dir == FORWARD)
810 {
811 match->cp_next = compl_curr_match->cp_next;
812 match->cp_prev = compl_curr_match;
813 }
814 else // BACKWARD
815 {
816 match->cp_next = compl_curr_match;
817 match->cp_prev = compl_curr_match->cp_prev;
818 }
819 if (match->cp_next)
820 match->cp_next->cp_prev = match;
821 if (match->cp_prev)
822 match->cp_prev->cp_next = match;
823 else // if there's nothing before, it is the first match
824 compl_first_match = match;
825 compl_curr_match = match;
826
827 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200828 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 ins_compl_longest_match(match);
830
831 return OK;
832}
833
834/*
835 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200836 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100837 */
838 static int
839ins_compl_equal(compl_T *match, char_u *str, int len)
840{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200841 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200842 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200843 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100844 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
845 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
846}
847
848/*
849 * Reduce the longest common string for match "match".
850 */
851 static void
852ins_compl_longest_match(compl_T *match)
853{
854 char_u *p, *s;
855 int c1, c2;
856 int had_match;
857
858 if (compl_leader == NULL)
859 {
860 // First match, use it as a whole.
861 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000862 if (compl_leader == NULL)
863 return;
864
865 had_match = (curwin->w_cursor.col > compl_col);
866 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000867 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000868 ins_redraw(FALSE);
869
870 // When the match isn't there (to avoid matching itself) remove it
871 // again after redrawing.
872 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100873 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100874 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000875
876 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100877 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000878
879 // Reduce the text if this match differs from compl_leader.
880 p = compl_leader;
881 s = match->cp_str;
882 while (*p != NUL)
883 {
884 if (has_mbyte)
885 {
886 c1 = mb_ptr2char(p);
887 c2 = mb_ptr2char(s);
888 }
889 else
890 {
891 c1 = *p;
892 c2 = *s;
893 }
894 if ((match->cp_flags & CP_ICASE)
895 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
896 break;
897 if (has_mbyte)
898 {
899 MB_PTR_ADV(p);
900 MB_PTR_ADV(s);
901 }
902 else
903 {
904 ++p;
905 ++s;
906 }
907 }
908
909 if (*p != NUL)
910 {
911 // Leader was shortened, need to change the inserted text.
912 *p = NUL;
913 had_match = (curwin->w_cursor.col > compl_col);
914 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000915 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000916 ins_redraw(FALSE);
917
918 // When the match isn't there (to avoid matching itself) remove it
919 // again after redrawing.
920 if (!had_match)
921 ins_compl_delete();
922 }
923
924 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925}
926
927/*
928 * Add an array of matches to the list of matches.
929 * Frees matches[].
930 */
931 static void
932ins_compl_add_matches(
933 int num_matches,
934 char_u **matches,
935 int icase)
936{
937 int i;
938 int add_r = OK;
939 int dir = compl_direction;
940
941 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100942 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200943 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100944 // if dir was BACKWARD then honor it just once
945 dir = FORWARD;
946 FreeWild(num_matches, matches);
947}
948
949/*
950 * Make the completion list cyclic.
951 * Return the number of matches (excluding the original).
952 */
953 static int
954ins_compl_make_cyclic(void)
955{
956 compl_T *match;
957 int count = 0;
958
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000959 if (compl_first_match == NULL)
960 return 0;
961
962 // Find the end of the list.
963 match = compl_first_match;
964 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000965 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100966 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967 match = match->cp_next;
968 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000970 match->cp_next = compl_first_match;
971 compl_first_match->cp_prev = match;
972
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100973 return count;
974}
975
976/*
977 * Return whether there currently is a shown match.
978 */
979 int
980ins_compl_has_shown_match(void)
981{
982 return compl_shown_match == NULL
983 || compl_shown_match != compl_shown_match->cp_next;
984}
985
986/*
987 * Return whether the shown match is long enough.
988 */
989 int
990ins_compl_long_shown_match(void)
991{
992 return (int)STRLEN(compl_shown_match->cp_str)
993 > curwin->w_cursor.col - compl_col;
994}
995
996/*
997 * Set variables that store noselect and noinsert behavior from the
998 * 'completeopt' value.
999 */
1000 void
1001completeopt_was_set(void)
1002{
1003 compl_no_insert = FALSE;
1004 compl_no_select = FALSE;
1005 if (strstr((char *)p_cot, "noselect") != NULL)
1006 compl_no_select = TRUE;
1007 if (strstr((char *)p_cot, "noinsert") != NULL)
1008 compl_no_insert = TRUE;
1009}
1010
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001011
1012// "compl_match_array" points the currently displayed list of entries in the
1013// popup menu. It is NULL when there is no popup menu.
1014static pumitem_T *compl_match_array = NULL;
1015static int compl_match_arraysize;
1016
1017/*
1018 * Update the screen and when there is any scrolling remove the popup menu.
1019 */
1020 static void
1021ins_compl_upd_pum(void)
1022{
1023 int h;
1024
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001025 if (compl_match_array == NULL)
1026 return;
1027
1028 h = curwin->w_cline_height;
1029 // Update the screen later, before drawing the popup menu over it.
1030 pum_call_update_screen();
1031 if (h != curwin->w_cline_height)
1032 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033}
1034
1035/*
1036 * Remove any popup menu.
1037 */
1038 static void
1039ins_compl_del_pum(void)
1040{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001041 if (compl_match_array == NULL)
1042 return;
1043
1044 pum_undisplay();
1045 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001046}
1047
1048/*
1049 * Return TRUE if the popup menu should be displayed.
1050 */
1051 int
1052pum_wanted(void)
1053{
1054 // 'completeopt' must contain "menu" or "menuone"
1055 if (vim_strchr(p_cot, 'm') == NULL)
1056 return FALSE;
1057
1058 // The display looks bad on a B&W display.
1059 if (t_colors < 8
1060#ifdef FEAT_GUI
1061 && !gui.in_use
1062#endif
1063 )
1064 return FALSE;
1065 return TRUE;
1066}
1067
1068/*
1069 * Return TRUE if there are two or more matches to be shown in the popup menu.
1070 * One if 'completopt' contains "menuone".
1071 */
1072 static int
1073pum_enough_matches(void)
1074{
1075 compl_T *compl;
1076 int i;
1077
1078 // Don't display the popup menu if there are no matches or there is only
1079 // one (ignoring the original text).
1080 compl = compl_first_match;
1081 i = 0;
1082 do
1083 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001084 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001085 break;
1086 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001087 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088
1089 if (strstr((char *)p_cot, "menuone") != NULL)
1090 return (i >= 1);
1091 return (i >= 2);
1092}
1093
Bram Moolenaar3075a452021-11-17 15:51:52 +00001094#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001095/*
1096 * Allocate Dict for the completed item.
1097 * { word, abbr, menu, kind, info }
1098 */
1099 static dict_T *
1100ins_compl_dict_alloc(compl_T *match)
1101{
1102 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1103
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001104 if (dict == NULL)
1105 return NULL;
1106
1107 dict_add_string(dict, "word", match->cp_str);
1108 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1109 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1110 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1111 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1112 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1113 dict_add_string(dict, "user_data", (char_u *)"");
1114 else
1115 dict_add_tv(dict, "user_data", &match->cp_user_data);
1116
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001117 return dict;
1118}
1119
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001120/*
1121 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1122 * completion menu is changed.
1123 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001124 static void
1125trigger_complete_changed_event(int cur)
1126{
1127 dict_T *v_event;
1128 dict_T *item;
1129 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001130 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001131
1132 if (recursive)
1133 return;
1134
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001135 if (cur < 0)
1136 item = dict_alloc();
1137 else
1138 item = ins_compl_dict_alloc(compl_curr_match);
1139 if (item == NULL)
1140 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001141 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001142 dict_add_dict(v_event, "completed_item", item);
1143 pum_set_event_info(v_event);
1144 dict_set_items_ro(v_event);
1145
1146 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001147 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001148 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001149 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001150 recursive = FALSE;
1151
Bram Moolenaar3075a452021-11-17 15:51:52 +00001152 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001153}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001154#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001155
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001157 * Build a popup menu to show the completion matches.
1158 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1159 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001160 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001161 static int
1162ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163{
1164 compl_T *compl;
1165 compl_T *shown_compl = NULL;
1166 int did_find_shown_match = FALSE;
1167 int shown_match_ok = FALSE;
1168 int i;
1169 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001170 int lead_len = 0;
1171
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001172 // Need to build the popup menu list.
1173 compl_match_arraysize = 0;
1174 compl = compl_first_match;
1175 if (compl_leader != NULL)
1176 lead_len = (int)STRLEN(compl_leader);
1177
1178 do
1179 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001180 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001181 && (compl_leader == NULL
1182 || ins_compl_equal(compl, compl_leader, lead_len)))
1183 ++compl_match_arraysize;
1184 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001185 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001186
1187 if (compl_match_arraysize == 0)
1188 return -1;
1189
1190 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1191 if (compl_match_array == NULL)
1192 return -1;
1193
1194 // If the current match is the original text don't find the first
1195 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001196 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001197 shown_match_ok = TRUE;
1198
1199 i = 0;
1200 compl = compl_first_match;
1201 do
1202 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001203 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001204 && (compl_leader == NULL
1205 || ins_compl_equal(compl, compl_leader, lead_len)))
1206 {
1207 if (!shown_match_ok)
1208 {
1209 if (compl == compl_shown_match || did_find_shown_match)
1210 {
1211 // This item is the shown match or this is the
1212 // first displayed item after the shown match.
1213 compl_shown_match = compl;
1214 did_find_shown_match = TRUE;
1215 shown_match_ok = TRUE;
1216 }
1217 else
1218 // Remember this displayed match for when the
1219 // shown match is just below it.
1220 shown_compl = compl;
1221 cur = i;
1222 }
1223
1224 if (compl->cp_text[CPT_ABBR] != NULL)
1225 compl_match_array[i].pum_text =
1226 compl->cp_text[CPT_ABBR];
1227 else
1228 compl_match_array[i].pum_text = compl->cp_str;
1229 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1230 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1231 if (compl->cp_text[CPT_MENU] != NULL)
1232 compl_match_array[i++].pum_extra =
1233 compl->cp_text[CPT_MENU];
1234 else
1235 compl_match_array[i++].pum_extra = compl->cp_fname;
1236 }
1237
1238 if (compl == compl_shown_match)
1239 {
1240 did_find_shown_match = TRUE;
1241
1242 // When the original text is the shown match don't set
1243 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001244 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001245 shown_match_ok = TRUE;
1246
1247 if (!shown_match_ok && shown_compl != NULL)
1248 {
1249 // The shown match isn't displayed, set it to the
1250 // previously displayed match.
1251 compl_shown_match = shown_compl;
1252 shown_match_ok = TRUE;
1253 }
1254 }
1255 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001256 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001257
1258 if (!shown_match_ok) // no displayed match at all
1259 cur = -1;
1260
1261 return cur;
1262}
1263
1264/*
1265 * Show the popup menu for the list of matches.
1266 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1267 */
1268 void
1269ins_compl_show_pum(void)
1270{
1271 int i;
1272 int cur = -1;
1273 colnr_T col;
1274
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001275 if (!pum_wanted() || !pum_enough_matches())
1276 return;
1277
1278#if defined(FEAT_EVAL)
1279 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001280 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001281#endif
1282
1283 // Update the screen later, before drawing the popup menu over it.
1284 pum_call_update_screen();
1285
1286 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001287 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001289 else
1290 {
1291 // popup menu already exists, only need to find the current item.
1292 for (i = 0; i < compl_match_arraysize; ++i)
1293 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1294 || compl_match_array[i].pum_text
1295 == compl_shown_match->cp_text[CPT_ABBR])
1296 {
1297 cur = i;
1298 break;
1299 }
1300 }
1301
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001302 if (compl_match_array == NULL)
1303 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001304
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001305 // In Replace mode when a $ is displayed at the end of the line only
1306 // part of the screen would be updated. We do need to redraw here.
1307 dollar_vcol = -1;
1308
1309 // Compute the screen column of the start of the completed text.
1310 // Use the cursor to get all wrapping and other settings right.
1311 col = curwin->w_cursor.col;
1312 curwin->w_cursor.col = compl_col;
1313 pum_display(compl_match_array, compl_match_arraysize, cur);
1314 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001315
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001316#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001317 if (has_completechanged())
1318 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001319#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001320}
1321
1322#define DICT_FIRST (1) // use just first element in "dict"
1323#define DICT_EXACT (2) // "dict" is the exact name of a file
1324
1325/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001326 * Add any identifiers that match the given pattern "pat" in the list of
1327 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001328 */
1329 static void
1330ins_compl_dictionaries(
1331 char_u *dict_start,
1332 char_u *pat,
1333 int flags, // DICT_FIRST and/or DICT_EXACT
1334 int thesaurus) // Thesaurus completion
1335{
1336 char_u *dict = dict_start;
1337 char_u *ptr;
1338 char_u *buf;
1339 regmatch_T regmatch;
1340 char_u **files;
1341 int count;
1342 int save_p_scs;
1343 int dir = compl_direction;
1344
1345 if (*dict == NUL)
1346 {
1347#ifdef FEAT_SPELL
1348 // When 'dictionary' is empty and spell checking is enabled use
1349 // "spell".
1350 if (!thesaurus && curwin->w_p_spell)
1351 dict = (char_u *)"spell";
1352 else
1353#endif
1354 return;
1355 }
1356
1357 buf = alloc(LSIZE);
1358 if (buf == NULL)
1359 return;
1360 regmatch.regprog = NULL; // so that we can goto theend
1361
1362 // If 'infercase' is set, don't use 'smartcase' here
1363 save_p_scs = p_scs;
1364 if (curbuf->b_p_inf)
1365 p_scs = FALSE;
1366
1367 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1368 // to only match at the start of a line. Otherwise just match the
1369 // pattern. Also need to double backslashes.
1370 if (ctrl_x_mode_line_or_eval())
1371 {
1372 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1373 size_t len;
1374
1375 if (pat_esc == NULL)
1376 goto theend;
1377 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001378 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001379 if (ptr == NULL)
1380 {
1381 vim_free(pat_esc);
1382 goto theend;
1383 }
1384 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1385 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1386 vim_free(pat_esc);
1387 vim_free(ptr);
1388 }
1389 else
1390 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001391 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 if (regmatch.regprog == NULL)
1393 goto theend;
1394 }
1395
1396 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1397 regmatch.rm_ic = ignorecase(pat);
1398 while (*dict != NUL && !got_int && !compl_interrupted)
1399 {
1400 // copy one dictionary file name into buf
1401 if (flags == DICT_EXACT)
1402 {
1403 count = 1;
1404 files = &dict;
1405 }
1406 else
1407 {
1408 // Expand wildcards in the dictionary name, but do not allow
1409 // backticks (for security, the 'dict' option may have been set in
1410 // a modeline).
1411 copy_option_part(&dict, buf, LSIZE, ",");
1412# ifdef FEAT_SPELL
1413 if (!thesaurus && STRCMP(buf, "spell") == 0)
1414 count = -1;
1415 else
1416# endif
1417 if (vim_strchr(buf, '`') != NULL
1418 || expand_wildcards(1, &buf, &count, &files,
1419 EW_FILE|EW_SILENT) != OK)
1420 count = 0;
1421 }
1422
1423# ifdef FEAT_SPELL
1424 if (count == -1)
1425 {
1426 // Complete from active spelling. Skip "\<" in the pattern, we
1427 // don't use it as a RE.
1428 if (pat[0] == '\\' && pat[1] == '<')
1429 ptr = pat + 2;
1430 else
1431 ptr = pat;
1432 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1433 }
1434 else
1435# endif
1436 if (count > 0) // avoid warning for using "files" uninit
1437 {
1438 ins_compl_files(count, files, thesaurus, flags,
1439 &regmatch, buf, &dir);
1440 if (flags != DICT_EXACT)
1441 FreeWild(count, files);
1442 }
1443 if (flags != 0)
1444 break;
1445 }
1446
1447theend:
1448 p_scs = save_p_scs;
1449 vim_regfree(regmatch.regprog);
1450 vim_free(buf);
1451}
1452
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001453/*
1454 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1455 * skipping the word at 'skip_word'. Returns OK on success.
1456 */
1457 static int
1458thesarurs_add_words_in_line(
1459 char_u *fname,
1460 char_u **buf_arg,
1461 int dir,
1462 char_u *skip_word)
1463{
1464 int status = OK;
1465 char_u *ptr;
1466 char_u *wstart;
1467
1468 // Add the other matches on the line
1469 ptr = *buf_arg;
1470 while (!got_int)
1471 {
1472 // Find start of the next word. Skip white
1473 // space and punctuation.
1474 ptr = find_word_start(ptr);
1475 if (*ptr == NUL || *ptr == NL)
1476 break;
1477 wstart = ptr;
1478
1479 // Find end of the word.
1480 if (has_mbyte)
1481 // Japanese words may have characters in
1482 // different classes, only separate words
1483 // with single-byte non-word characters.
1484 while (*ptr != NUL)
1485 {
1486 int l = (*mb_ptr2len)(ptr);
1487
1488 if (l < 2 && !vim_iswordc(*ptr))
1489 break;
1490 ptr += l;
1491 }
1492 else
1493 ptr = find_word_end(ptr);
1494
1495 // Add the word. Skip the regexp match.
1496 if (wstart != skip_word)
1497 {
1498 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1499 fname, dir, FALSE);
1500 if (status == FAIL)
1501 break;
1502 }
1503 }
1504
1505 *buf_arg = ptr;
1506 return status;
1507}
1508
1509/*
1510 * Process "count" dictionary/thesaurus "files" and add the text matching
1511 * "regmatch".
1512 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001513 static void
1514ins_compl_files(
1515 int count,
1516 char_u **files,
1517 int thesaurus,
1518 int flags,
1519 regmatch_T *regmatch,
1520 char_u *buf,
1521 int *dir)
1522{
1523 char_u *ptr;
1524 int i;
1525 FILE *fp;
1526 int add_r;
1527
1528 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1529 {
1530 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1531 if (flags != DICT_EXACT)
1532 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001533 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001534 vim_snprintf((char *)IObuff, IOSIZE,
1535 _("Scanning dictionary: %s"), (char *)files[i]);
1536 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1537 }
1538
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001539 if (fp == NULL)
1540 continue;
1541
1542 // Read dictionary file line by line.
1543 // Check each line for a match.
1544 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001545 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001546 ptr = buf;
1547 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001548 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001549 ptr = regmatch->startp[0];
1550 if (ctrl_x_mode_line_or_eval())
1551 ptr = find_line_end(ptr);
1552 else
1553 ptr = find_word_end(ptr);
1554 add_r = ins_compl_add_infercase(regmatch->startp[0],
1555 (int)(ptr - regmatch->startp[0]),
1556 p_ic, files[i], *dir, FALSE);
1557 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001558 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001559 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001560 ptr = buf;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001561 add_r = thesarurs_add_words_in_line(files[i], &ptr, *dir,
1562 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001563 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001564 if (add_r == OK)
1565 // if dir was BACKWARD then honor it just once
1566 *dir = FORWARD;
1567 else if (add_r == FAIL)
1568 break;
1569 // avoid expensive call to vim_regexec() when at end
1570 // of line
1571 if (*ptr == '\n' || got_int)
1572 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001573 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001574 line_breakcheck();
1575 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001576 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001577 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001578 }
1579}
1580
1581/*
1582 * Find the start of the next word.
1583 * Returns a pointer to the first char of the word. Also stops at a NUL.
1584 */
1585 char_u *
1586find_word_start(char_u *ptr)
1587{
1588 if (has_mbyte)
1589 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1590 ptr += (*mb_ptr2len)(ptr);
1591 else
1592 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1593 ++ptr;
1594 return ptr;
1595}
1596
1597/*
1598 * Find the end of the word. Assumes it starts inside a word.
1599 * Returns a pointer to just after the word.
1600 */
1601 char_u *
1602find_word_end(char_u *ptr)
1603{
1604 int start_class;
1605
1606 if (has_mbyte)
1607 {
1608 start_class = mb_get_class(ptr);
1609 if (start_class > 1)
1610 while (*ptr != NUL)
1611 {
1612 ptr += (*mb_ptr2len)(ptr);
1613 if (mb_get_class(ptr) != start_class)
1614 break;
1615 }
1616 }
1617 else
1618 while (vim_iswordc(*ptr))
1619 ++ptr;
1620 return ptr;
1621}
1622
1623/*
1624 * Find the end of the line, omitting CR and NL at the end.
1625 * Returns a pointer to just after the line.
1626 */
1627 static char_u *
1628find_line_end(char_u *ptr)
1629{
1630 char_u *s;
1631
1632 s = ptr + STRLEN(ptr);
1633 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1634 --s;
1635 return s;
1636}
1637
1638/*
1639 * Free the list of completions
1640 */
1641 static void
1642ins_compl_free(void)
1643{
1644 compl_T *match;
1645 int i;
1646
1647 VIM_CLEAR(compl_pattern);
1648 VIM_CLEAR(compl_leader);
1649
1650 if (compl_first_match == NULL)
1651 return;
1652
1653 ins_compl_del_pum();
1654 pum_clear();
1655
1656 compl_curr_match = compl_first_match;
1657 do
1658 {
1659 match = compl_curr_match;
1660 compl_curr_match = compl_curr_match->cp_next;
1661 vim_free(match->cp_str);
1662 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001663 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001664 vim_free(match->cp_fname);
1665 for (i = 0; i < CPT_COUNT; ++i)
1666 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001667#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001668 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001669#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001670 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001671 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001672 compl_first_match = compl_curr_match = NULL;
1673 compl_shown_match = NULL;
1674 compl_old_match = NULL;
1675}
1676
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001677/*
1678 * Reset/clear the completion state.
1679 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001680 void
1681ins_compl_clear(void)
1682{
1683 compl_cont_status = 0;
1684 compl_started = FALSE;
1685 compl_matches = 0;
1686 VIM_CLEAR(compl_pattern);
1687 VIM_CLEAR(compl_leader);
1688 edit_submode_extra = NULL;
1689 VIM_CLEAR(compl_orig_text);
1690 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001691#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001692 // clear v:completed_item
1693 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001694#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001695}
1696
1697/*
1698 * Return TRUE when Insert completion is active.
1699 */
1700 int
1701ins_compl_active(void)
1702{
1703 return compl_started;
1704}
1705
1706/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 * Selected one of the matches. When FALSE the match was edited or using the
1708 * longest common string.
1709 */
1710 int
1711ins_compl_used_match(void)
1712{
1713 return compl_used_match;
1714}
1715
1716/*
1717 * Initialize get longest common string.
1718 */
1719 void
1720ins_compl_init_get_longest(void)
1721{
1722 compl_get_longest = FALSE;
1723}
1724
1725/*
1726 * Returns TRUE when insert completion is interrupted.
1727 */
1728 int
1729ins_compl_interrupted(void)
1730{
1731 return compl_interrupted;
1732}
1733
1734/*
1735 * Returns TRUE if the <Enter> key selects a match in the completion popup
1736 * menu.
1737 */
1738 int
1739ins_compl_enter_selects(void)
1740{
1741 return compl_enter_selects;
1742}
1743
1744/*
1745 * Return the column where the text starts that is being completed
1746 */
1747 colnr_T
1748ins_compl_col(void)
1749{
1750 return compl_col;
1751}
1752
1753/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001754 * Return the length in bytes of the text being completed
1755 */
1756 int
1757ins_compl_len(void)
1758{
1759 return compl_length;
1760}
1761
1762/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001763 * Delete one character before the cursor and show the subset of the matches
1764 * that match the word that is now before the cursor.
1765 * Returns the character to be used, NUL if the work is done and another char
1766 * to be got from the user.
1767 */
1768 int
1769ins_compl_bs(void)
1770{
1771 char_u *line;
1772 char_u *p;
1773
1774 line = ml_get_curline();
1775 p = line + curwin->w_cursor.col;
1776 MB_PTR_BACK(line, p);
1777
1778 // Stop completion when the whole word was deleted. For Omni completion
1779 // allow the word to be deleted, we won't match everything.
1780 // Respect the 'backspace' option.
1781 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001782 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1783 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001784 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1785 - compl_length < 0))
1786 return K_BS;
1787
1788 // Deleted more than what was used to find matches or didn't finish
1789 // finding all matches: need to look for matches all over again.
1790 if (curwin->w_cursor.col <= compl_col + compl_length
1791 || ins_compl_need_restart())
1792 ins_compl_restart();
1793
1794 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001795 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001796 if (compl_leader == NULL)
1797 return K_BS;
1798
1799 ins_compl_new_leader();
1800 if (compl_shown_match != NULL)
1801 // Make sure current match is not a hidden item.
1802 compl_curr_match = compl_shown_match;
1803 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001804}
1805
1806/*
1807 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1808 * be called.
1809 */
1810 static int
1811ins_compl_need_restart(void)
1812{
1813 // Return TRUE if we didn't complete finding matches or when the
1814 // 'completefunc' returned "always" in the "refresh" dictionary item.
1815 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001816 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001817 && compl_opt_refresh_always);
1818}
1819
1820/*
1821 * Called after changing "compl_leader".
1822 * Show the popup menu with a different set of matches.
1823 * May also search for matches again if the previous search was interrupted.
1824 */
1825 static void
1826ins_compl_new_leader(void)
1827{
1828 ins_compl_del_pum();
1829 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001830 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 compl_used_match = FALSE;
1832
1833 if (compl_started)
1834 ins_compl_set_original_text(compl_leader);
1835 else
1836 {
1837#ifdef FEAT_SPELL
1838 spell_bad_len = 0; // need to redetect bad word
1839#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001840 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001841 // the popup menu display the changed text before the cursor. Set
1842 // "compl_restarting" to avoid that the first match is inserted.
1843 pum_call_update_screen();
1844#ifdef FEAT_GUI
1845 if (gui.in_use)
1846 {
1847 // Show the cursor after the match, not after the redrawn text.
1848 setcursor();
1849 out_flush_cursor(FALSE, FALSE);
1850 }
1851#endif
1852 compl_restarting = TRUE;
1853 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1854 compl_cont_status = 0;
1855 compl_restarting = FALSE;
1856 }
1857
1858 compl_enter_selects = !compl_used_match;
1859
1860 // Show the popup menu with a different set of matches.
1861 ins_compl_show_pum();
1862
1863 // Don't let Enter select the original text when there is no popup menu.
1864 if (compl_match_array == NULL)
1865 compl_enter_selects = FALSE;
1866}
1867
1868/*
1869 * Return the length of the completion, from the completion start column to
1870 * the cursor column. Making sure it never goes below zero.
1871 */
1872 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001873get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001874{
1875 int off = (int)curwin->w_cursor.col - (int)compl_col;
1876
1877 if (off < 0)
1878 return 0;
1879 return off;
1880}
1881
1882/*
1883 * Append one character to the match leader. May reduce the number of
1884 * matches.
1885 */
1886 void
1887ins_compl_addleader(int c)
1888{
1889 int cc;
1890
1891 if (stop_arrow() == FAIL)
1892 return;
1893 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1894 {
1895 char_u buf[MB_MAXBYTES + 1];
1896
1897 (*mb_char2bytes)(c, buf);
1898 buf[cc] = NUL;
1899 ins_char_bytes(buf, cc);
1900 if (compl_opt_refresh_always)
1901 AppendToRedobuff(buf);
1902 }
1903 else
1904 {
1905 ins_char(c);
1906 if (compl_opt_refresh_always)
1907 AppendCharToRedobuff(c);
1908 }
1909
1910 // If we didn't complete finding matches we must search again.
1911 if (ins_compl_need_restart())
1912 ins_compl_restart();
1913
1914 // When 'always' is set, don't reset compl_leader. While completing,
1915 // cursor doesn't point original position, changing compl_leader would
1916 // break redo.
1917 if (!compl_opt_refresh_always)
1918 {
1919 vim_free(compl_leader);
1920 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001921 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001922 if (compl_leader != NULL)
1923 ins_compl_new_leader();
1924 }
1925}
1926
1927/*
1928 * Setup for finding completions again without leaving CTRL-X mode. Used when
1929 * BS or a key was typed while still searching for matches.
1930 */
1931 static void
1932ins_compl_restart(void)
1933{
1934 ins_compl_free();
1935 compl_started = FALSE;
1936 compl_matches = 0;
1937 compl_cont_status = 0;
1938 compl_cont_mode = 0;
1939}
1940
1941/*
1942 * Set the first match, the original text.
1943 */
1944 static void
1945ins_compl_set_original_text(char_u *str)
1946{
1947 char_u *p;
1948
1949 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001950 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1951 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001952 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001953 {
1954 p = vim_strsave(str);
1955 if (p != NULL)
1956 {
1957 vim_free(compl_first_match->cp_str);
1958 compl_first_match->cp_str = p;
1959 }
1960 }
1961 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001962 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001963 {
1964 p = vim_strsave(str);
1965 if (p != NULL)
1966 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001967 vim_free(compl_first_match->cp_prev->cp_str);
1968 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969 }
1970 }
1971}
1972
1973/*
1974 * Append one character to the match leader. May reduce the number of
1975 * matches.
1976 */
1977 void
1978ins_compl_addfrommatch(void)
1979{
1980 char_u *p;
1981 int len = (int)curwin->w_cursor.col - (int)compl_col;
1982 int c;
1983 compl_T *cp;
1984
1985 p = compl_shown_match->cp_str;
1986 if ((int)STRLEN(p) <= len) // the match is too short
1987 {
1988 // When still at the original match use the first entry that matches
1989 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001990 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001991 return;
1992
1993 p = NULL;
1994 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001995 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001997 if (compl_leader == NULL
1998 || ins_compl_equal(cp, compl_leader,
1999 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002000 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002001 p = cp->cp_str;
2002 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002003 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002004 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002005 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002006 return;
2007 }
2008 p += len;
2009 c = PTR2CHAR(p);
2010 ins_compl_addleader(c);
2011}
2012
2013/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002014 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002015 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2016 * compl_cont_mode and compl_cont_status.
2017 * Returns TRUE when the character is not to be inserted.
2018 */
2019 static int
2020set_ctrl_x_mode(int c)
2021{
2022 int retval = FALSE;
2023
2024 switch (c)
2025 {
2026 case Ctrl_E:
2027 case Ctrl_Y:
2028 // scroll the window one line up or down
2029 ctrl_x_mode = CTRL_X_SCROLL;
2030 if (!(State & REPLACE_FLAG))
2031 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2032 else
2033 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2034 edit_submode_pre = NULL;
2035 showmode();
2036 break;
2037 case Ctrl_L:
2038 // complete whole line
2039 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2040 break;
2041 case Ctrl_F:
2042 // complete filenames
2043 ctrl_x_mode = CTRL_X_FILES;
2044 break;
2045 case Ctrl_K:
2046 // complete words from a dictinoary
2047 ctrl_x_mode = CTRL_X_DICTIONARY;
2048 break;
2049 case Ctrl_R:
2050 // Register insertion without exiting CTRL-X mode
2051 // Simply allow ^R to happen without affecting ^X mode
2052 break;
2053 case Ctrl_T:
2054 // complete words from a thesaurus
2055 ctrl_x_mode = CTRL_X_THESAURUS;
2056 break;
2057#ifdef FEAT_COMPL_FUNC
2058 case Ctrl_U:
2059 // user defined completion
2060 ctrl_x_mode = CTRL_X_FUNCTION;
2061 break;
2062 case Ctrl_O:
2063 // omni completion
2064 ctrl_x_mode = CTRL_X_OMNI;
2065 break;
2066#endif
2067 case 's':
2068 case Ctrl_S:
2069 // complete spelling suggestions
2070 ctrl_x_mode = CTRL_X_SPELL;
2071#ifdef FEAT_SPELL
2072 ++emsg_off; // Avoid getting the E756 error twice.
2073 spell_back_to_badword();
2074 --emsg_off;
2075#endif
2076 break;
2077 case Ctrl_RSB:
2078 // complete tag names
2079 ctrl_x_mode = CTRL_X_TAGS;
2080 break;
2081#ifdef FEAT_FIND_ID
2082 case Ctrl_I:
2083 case K_S_TAB:
2084 // complete keywords from included files
2085 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2086 break;
2087 case Ctrl_D:
2088 // complete definitions from included files
2089 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2090 break;
2091#endif
2092 case Ctrl_V:
2093 case Ctrl_Q:
2094 // complete vim commands
2095 ctrl_x_mode = CTRL_X_CMDLINE;
2096 break;
2097 case Ctrl_Z:
2098 // stop completion
2099 ctrl_x_mode = CTRL_X_NORMAL;
2100 edit_submode = NULL;
2101 showmode();
2102 retval = TRUE;
2103 break;
2104 case Ctrl_P:
2105 case Ctrl_N:
2106 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2107 // just started ^X mode, or there were enough ^X's to cancel
2108 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2109 // do normal expansion when interrupting a different mode (say
2110 // ^X^F^X^P or ^P^X^X^P, see below)
2111 // nothing changes if interrupting mode 0, (eg, the flag
2112 // doesn't change when going to ADDING mode -- Acevedo
2113 if (!(compl_cont_status & CONT_INTRPT))
2114 compl_cont_status |= CONT_LOCAL;
2115 else if (compl_cont_mode != 0)
2116 compl_cont_status &= ~CONT_LOCAL;
2117 // FALLTHROUGH
2118 default:
2119 // If we have typed at least 2 ^X's... for modes != 0, we set
2120 // compl_cont_status = 0 (eg, as if we had just started ^X
2121 // mode).
2122 // For mode 0, we set "compl_cont_mode" to an impossible
2123 // value, in both cases ^X^X can be used to restart the same
2124 // mode (avoiding ADDING mode).
2125 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2126 // 'complete' and local ^P expansions respectively.
2127 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2128 // mode -- Acevedo
2129 if (c == Ctrl_X)
2130 {
2131 if (compl_cont_mode != 0)
2132 compl_cont_status = 0;
2133 else
2134 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2135 }
2136 ctrl_x_mode = CTRL_X_NORMAL;
2137 edit_submode = NULL;
2138 showmode();
2139 break;
2140 }
2141
2142 return retval;
2143}
2144
2145/*
2146 * Stop insert completion mode
2147 */
2148 static int
2149ins_compl_stop(int c, int prev_mode, int retval)
2150{
2151 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002152 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002153
2154 // Get here when we have finished typing a sequence of ^N and
2155 // ^P or other completion characters in CTRL-X mode. Free up
2156 // memory that was used, and make sure we can redo the insert.
2157 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2158 {
2159 // If any of the original typed text has been changed, eg when
2160 // ignorecase is set, we must add back-spaces to the redo
2161 // buffer. We add as few as necessary to delete just the part
2162 // of the original text that has changed.
2163 // When using the longest match, edited the match or used
2164 // CTRL-E then don't use the current match.
2165 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2166 ptr = compl_curr_match->cp_str;
2167 else
2168 ptr = NULL;
2169 ins_compl_fixRedoBufForLeader(ptr);
2170 }
2171
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002172 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002173
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002174 // When completing whole lines: fix indent for 'cindent'.
2175 // Otherwise, break line if it's too long.
2176 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2177 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002178 // re-indent the current line
2179 if (want_cindent)
2180 {
2181 do_c_expr_indent();
2182 want_cindent = FALSE; // don't do it again
2183 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002184 }
2185 else
2186 {
2187 int prev_col = curwin->w_cursor.col;
2188
2189 // put the cursor on the last char, for 'tw' formatting
2190 if (prev_col > 0)
2191 dec_cursor();
2192 // only format when something was inserted
2193 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2194 insertchar(NUL, 0, -1);
2195 if (prev_col > 0
2196 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2197 inc_cursor();
2198 }
2199
2200 // If the popup menu is displayed pressing CTRL-Y means accepting
2201 // the selection without inserting anything. When
2202 // compl_enter_selects is set the Enter key does the same.
2203 if ((c == Ctrl_Y || (compl_enter_selects
2204 && (c == CAR || c == K_KENTER || c == NL)))
2205 && pum_visible())
2206 retval = TRUE;
2207
2208 // CTRL-E means completion is Ended, go back to the typed text.
2209 // but only do this, if the Popup is still visible
2210 if (c == Ctrl_E)
2211 {
2212 ins_compl_delete();
2213 if (compl_leader != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002214 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002215 else if (compl_first_match != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002216 ins_bytes(compl_orig_text + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002217 retval = TRUE;
2218 }
2219
2220 auto_format(FALSE, TRUE);
2221
2222 // Trigger the CompleteDonePre event to give scripts a chance to
2223 // act upon the completion before clearing the info, and restore
2224 // ctrl_x_mode, so that complete_info() can be used.
2225 ctrl_x_mode = prev_mode;
2226 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2227
2228 ins_compl_free();
2229 compl_started = FALSE;
2230 compl_matches = 0;
2231 if (!shortmess(SHM_COMPLETIONMENU))
2232 msg_clr_cmdline(); // necessary for "noshowmode"
2233 ctrl_x_mode = CTRL_X_NORMAL;
2234 compl_enter_selects = FALSE;
2235 if (edit_submode != NULL)
2236 {
2237 edit_submode = NULL;
2238 showmode();
2239 }
2240
2241#ifdef FEAT_CMDWIN
2242 if (c == Ctrl_C && cmdwin_type != 0)
2243 // Avoid the popup menu remains displayed when leaving the
2244 // command line window.
2245 update_screen(0);
2246#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002247 // Indent now if a key was typed that is in 'cinkeys'.
2248 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2249 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002250 // Trigger the CompleteDone event to give scripts a chance to act
2251 // upon the end of completion.
2252 ins_apply_autocmds(EVENT_COMPLETEDONE);
2253
2254 return retval;
2255}
2256
2257/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002258 * Prepare for Insert mode completion, or stop it.
2259 * Called just after typing a character in Insert mode.
2260 * Returns TRUE when the character is not to be inserted;
2261 */
2262 int
2263ins_compl_prep(int c)
2264{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002265 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002266 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002267
2268 // Forget any previous 'special' messages if this is actually
2269 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2270 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2271 edit_submode_extra = NULL;
2272
2273 // Ignore end of Select mode mapping and mouse scroll buttons.
2274 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002275 || c == K_MOUSELEFT || c == K_MOUSERIGHT
2276 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002277 return retval;
2278
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002279#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002280 // Ignore mouse events in a popup window
2281 if (is_mouse_key(c))
2282 {
2283 // Ignore drag and release events, the position does not need to be in
2284 // the popup and it may have just closed.
2285 if (c == K_LEFTRELEASE
2286 || c == K_LEFTRELEASE_NM
2287 || c == K_MIDDLERELEASE
2288 || c == K_RIGHTRELEASE
2289 || c == K_X1RELEASE
2290 || c == K_X2RELEASE
2291 || c == K_LEFTDRAG
2292 || c == K_MIDDLEDRAG
2293 || c == K_RIGHTDRAG
2294 || c == K_X1DRAG
2295 || c == K_X2DRAG)
2296 return retval;
2297 if (popup_visible)
2298 {
2299 int row = mouse_row;
2300 int col = mouse_col;
2301 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2302
2303 if (wp != NULL && WIN_IS_POPUP(wp))
2304 return retval;
2305 }
2306 }
2307#endif
2308
zeertzjqdca29d92021-08-31 19:12:51 +02002309 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2310 {
2311 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2312 || !vim_is_ctrl_x_key(c))
2313 {
2314 // Not starting another completion mode.
2315 ctrl_x_mode = CTRL_X_CMDLINE;
2316
2317 // CTRL-X CTRL-Z should stop completion without inserting anything
2318 if (c == Ctrl_Z)
2319 retval = TRUE;
2320 }
2321 else
2322 {
2323 ctrl_x_mode = CTRL_X_CMDLINE;
2324
2325 // Other CTRL-X keys first stop completion, then start another
2326 // completion mode.
2327 ins_compl_prep(' ');
2328 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2329 }
2330 }
2331
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002332 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002333 if (ctrl_x_mode_not_defined_yet()
2334 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002335 {
2336 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2337 compl_used_match = TRUE;
2338
2339 }
2340
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002341 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002342 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2343 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002344 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002345 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002346 {
2347 // We're already in CTRL-X mode, do we stay in it?
2348 if (!vim_is_ctrl_x_key(c))
2349 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002350 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002351 ctrl_x_mode = CTRL_X_NORMAL;
2352 else
2353 ctrl_x_mode = CTRL_X_FINISHED;
2354 edit_submode = NULL;
2355 }
2356 showmode();
2357 }
2358
2359 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2360 {
2361 // Show error message from attempted keyword completion (probably
2362 // 'Pattern not found') until another key is hit, then go back to
2363 // showing what mode we are in.
2364 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002365 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002366 && c != Ctrl_R && !ins_compl_pum_key(c))
2367 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002368 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002369 }
2370 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2371 // Trigger the CompleteDone event to give scripts a chance to act
2372 // upon the (possibly failed) completion.
2373 ins_apply_autocmds(EVENT_COMPLETEDONE);
2374
LemonBoy2bf52dd2022-04-09 18:17:34 +01002375 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002376
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002377 // reset continue_* if we left expansion-mode, if we stay they'll be
2378 // (re)set properly in ins_complete()
2379 if (!vim_is_ctrl_x_key(c))
2380 {
2381 compl_cont_status = 0;
2382 compl_cont_mode = 0;
2383 }
2384
2385 return retval;
2386}
2387
2388/*
2389 * Fix the redo buffer for the completion leader replacing some of the typed
2390 * text. This inserts backspaces and appends the changed text.
2391 * "ptr" is the known leader text or NUL.
2392 */
2393 static void
2394ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2395{
2396 int len;
2397 char_u *p;
2398 char_u *ptr = ptr_arg;
2399
2400 if (ptr == NULL)
2401 {
2402 if (compl_leader != NULL)
2403 ptr = compl_leader;
2404 else
2405 return; // nothing to do
2406 }
2407 if (compl_orig_text != NULL)
2408 {
2409 p = compl_orig_text;
2410 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2411 ;
2412 if (len > 0)
2413 len -= (*mb_head_off)(p, p + len);
2414 for (p += len; *p != NUL; MB_PTR_ADV(p))
2415 AppendCharToRedobuff(K_BS);
2416 }
2417 else
2418 len = 0;
2419 if (ptr != NULL)
2420 AppendToRedobuffLit(ptr + len, -1);
2421}
2422
2423/*
2424 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2425 * (depending on flag) starting from buf and looking for a non-scanned
2426 * buffer (other than curbuf). curbuf is special, if it is called with
2427 * buf=curbuf then it has to be the first call for a given flag/expansion.
2428 *
2429 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2430 */
2431 static buf_T *
2432ins_compl_next_buf(buf_T *buf, int flag)
2433{
2434 static win_T *wp = NULL;
2435
2436 if (flag == 'w') // just windows
2437 {
2438 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2439 wp = curwin;
2440 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2441 && wp->w_buffer->b_scanned)
2442 ;
2443 buf = wp->w_buffer;
2444 }
2445 else
2446 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2447 // (unlisted buffers)
2448 // When completing whole lines skip unloaded buffers.
2449 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2450 && ((flag == 'U'
2451 ? buf->b_p_bl
2452 : (!buf->b_p_bl
2453 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2454 || buf->b_scanned))
2455 ;
2456 return buf;
2457}
2458
2459#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002460
2461# ifdef FEAT_EVAL
2462static callback_T cfu_cb; // 'completefunc' callback function
2463static callback_T ofu_cb; // 'omnifunc' callback function
2464static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2465# endif
2466
2467/*
2468 * Copy a global callback function to a buffer local callback.
2469 */
2470 static void
2471copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2472{
2473 free_callback(bufcb);
2474 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2475 copy_callback(bufcb, globcb);
2476}
2477
2478/*
2479 * Parse the 'completefunc' option value and set the callback function.
2480 * Invoked when the 'completefunc' option is set. The option value can be a
2481 * name of a function (string), or function(<name>) or funcref(<name>) or a
2482 * lambda expression.
2483 */
2484 int
2485set_completefunc_option(void)
2486{
2487 int retval;
2488
2489 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2490 if (retval == OK)
2491 set_buflocal_cfu_callback(curbuf);
2492
2493 return retval;
2494}
2495
2496/*
2497 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002498 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002499 */
2500 void
2501set_buflocal_cfu_callback(buf_T *buf UNUSED)
2502{
2503# ifdef FEAT_EVAL
2504 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2505# endif
2506}
2507
2508/*
2509 * Parse the 'omnifunc' option value and set the callback function.
2510 * Invoked when the 'omnifunc' option is set. The option value can be a
2511 * name of a function (string), or function(<name>) or funcref(<name>) or a
2512 * lambda expression.
2513 */
2514 int
2515set_omnifunc_option(void)
2516{
2517 int retval;
2518
2519 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2520 if (retval == OK)
2521 set_buflocal_ofu_callback(curbuf);
2522
2523 return retval;
2524}
2525
2526/*
2527 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002528 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002529 */
2530 void
2531set_buflocal_ofu_callback(buf_T *buf UNUSED)
2532{
2533# ifdef FEAT_EVAL
2534 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2535# endif
2536}
2537
2538/*
2539 * Parse the 'thesaurusfunc' option value and set the callback function.
2540 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2541 * name of a function (string), or function(<name>) or funcref(<name>) or a
2542 * lambda expression.
2543 */
2544 int
2545set_thesaurusfunc_option(void)
2546{
2547 int retval;
2548
2549 if (*curbuf->b_p_tsrfu != NUL)
2550 {
2551 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002552 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2553 &curbuf->b_tsrfu_cb);
2554 }
2555 else
2556 {
2557 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002558 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2559 }
2560
2561 return retval;
2562}
2563
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002564/*
2565 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002566 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002567 */
2568 int
2569set_ref_in_insexpand_funcs(int copyID)
2570{
2571 int abort = FALSE;
2572
2573 abort = set_ref_in_callback(&cfu_cb, copyID);
2574 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2575 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2576
2577 return abort;
2578}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002579
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002580/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002581 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002582 */
2583 static char_u *
2584get_complete_funcname(int type)
2585{
2586 switch (type)
2587 {
2588 case CTRL_X_FUNCTION:
2589 return curbuf->b_p_cfu;
2590 case CTRL_X_OMNI:
2591 return curbuf->b_p_ofu;
2592 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002593 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002594 default:
2595 return (char_u *)"";
2596 }
2597}
2598
2599/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002600 * Get the callback to use for insert mode completion.
2601 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002602 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002603get_insert_callback(int type)
2604{
2605 if (type == CTRL_X_FUNCTION)
2606 return &curbuf->b_cfu_cb;
2607 if (type == CTRL_X_OMNI)
2608 return &curbuf->b_ofu_cb;
2609 // CTRL_X_THESAURUS
2610 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2611}
2612
2613/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002614 * Execute user defined complete function 'completefunc', 'omnifunc' or
2615 * 'thesaurusfunc', and get matches in "matches".
2616 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002617 */
2618 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002619expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620{
2621 list_T *matchlist = NULL;
2622 dict_T *matchdict = NULL;
2623 typval_T args[3];
2624 char_u *funcname;
2625 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002626 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002627 typval_T rettv;
2628 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002629 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002630
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002631 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002632 if (*funcname == NUL)
2633 return;
2634
2635 // Call 'completefunc' to obtain the list of matches.
2636 args[0].v_type = VAR_NUMBER;
2637 args[0].vval.v_number = 0;
2638 args[1].v_type = VAR_STRING;
2639 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2640 args[2].v_type = VAR_UNKNOWN;
2641
2642 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002643 // Lock the text to avoid weird things from happening. Also disallow
2644 // switching to another window, it should not be needed and may end up in
2645 // Insert mode in another buffer.
2646 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002647
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002648 cb = get_insert_callback(type);
2649 retval = call_callback(cb, 0, &rettv, 2, args);
2650
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002651 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 {
2654 switch (rettv.v_type)
2655 {
2656 case VAR_LIST:
2657 matchlist = rettv.vval.v_list;
2658 break;
2659 case VAR_DICT:
2660 matchdict = rettv.vval.v_dict;
2661 break;
2662 case VAR_SPECIAL:
2663 if (rettv.vval.v_number == VVAL_NONE)
2664 compl_opt_suppress_empty = TRUE;
2665 // FALLTHROUGH
2666 default:
2667 // TODO: Give error message?
2668 clear_tv(&rettv);
2669 break;
2670 }
2671 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002672 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002673
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002674 curwin->w_cursor = pos; // restore the cursor position
2675 validate_cursor();
2676 if (!EQUAL_POS(curwin->w_cursor, pos))
2677 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002678 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679 goto theend;
2680 }
2681
2682 if (matchlist != NULL)
2683 ins_compl_add_list(matchlist);
2684 else if (matchdict != NULL)
2685 ins_compl_add_dict(matchdict);
2686
2687theend:
2688 // Restore State, it might have been changed.
2689 State = save_State;
2690
2691 if (matchdict != NULL)
2692 dict_unref(matchdict);
2693 if (matchlist != NULL)
2694 list_unref(matchlist);
2695}
2696#endif // FEAT_COMPL_FUNC
2697
2698#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2699/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002700 * Add a match to the list of matches from a typeval_T.
2701 * If the given string is already in the list of completions, then return
2702 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2703 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002704 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002705 */
2706 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002707ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002708{
2709 char_u *word;
2710 int dup = FALSE;
2711 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002712 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002713 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002714 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002715 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002716
Bram Moolenaar08928322020-01-04 14:32:48 +01002717 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002718 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2719 {
2720 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2721 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2722 (char_u *)"abbr", FALSE);
2723 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2724 (char_u *)"menu", FALSE);
2725 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2726 (char_u *)"kind", FALSE);
2727 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2728 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002729 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002730 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2731 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2732 flags |= CP_ICASE;
2733 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2734 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2735 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2736 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2737 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2738 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2739 flags |= CP_EQUAL;
2740 }
2741 else
2742 {
2743 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002744 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002745 }
2746 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002747 {
2748 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002749 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002750 }
2751 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2752 if (status != OK)
2753 clear_tv(&user_data);
2754 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002755}
2756
2757/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758 * Add completions from a list.
2759 */
2760 static void
2761ins_compl_add_list(list_T *list)
2762{
2763 listitem_T *li;
2764 int dir = compl_direction;
2765
2766 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002767 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002768 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002769 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002770 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002771 // if dir was BACKWARD then honor it just once
2772 dir = FORWARD;
2773 else if (did_emsg)
2774 break;
2775 }
2776}
2777
2778/*
2779 * Add completions from a dict.
2780 */
2781 static void
2782ins_compl_add_dict(dict_T *dict)
2783{
2784 dictitem_T *di_refresh;
2785 dictitem_T *di_words;
2786
2787 // Check for optional "refresh" item.
2788 compl_opt_refresh_always = FALSE;
2789 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2790 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2791 {
2792 char_u *v = di_refresh->di_tv.vval.v_string;
2793
2794 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2795 compl_opt_refresh_always = TRUE;
2796 }
2797
2798 // Add completions from a "words" list.
2799 di_words = dict_find(dict, (char_u *)"words", 5);
2800 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2801 ins_compl_add_list(di_words->di_tv.vval.v_list);
2802}
2803
2804/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002805 * Start completion for the complete() function.
2806 * "startcol" is where the matched text starts (1 is first column).
2807 * "list" is the list of matches.
2808 */
2809 static void
2810set_completion(colnr_T startcol, list_T *list)
2811{
2812 int save_w_wrow = curwin->w_wrow;
2813 int save_w_leftcol = curwin->w_leftcol;
2814 int flags = CP_ORIGINAL_TEXT;
2815
2816 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002817 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002818 ins_compl_prep(' ');
2819 ins_compl_clear();
2820 ins_compl_free();
2821
2822 compl_direction = FORWARD;
2823 if (startcol > curwin->w_cursor.col)
2824 startcol = curwin->w_cursor.col;
2825 compl_col = startcol;
2826 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2827 // compl_pattern doesn't need to be set
2828 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2829 if (p_ic)
2830 flags |= CP_ICASE;
2831 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002832 -1, NULL, NULL, NULL, 0,
2833 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002834 return;
2835
2836 ctrl_x_mode = CTRL_X_EVAL;
2837
2838 ins_compl_add_list(list);
2839 compl_matches = ins_compl_make_cyclic();
2840 compl_started = TRUE;
2841 compl_used_match = TRUE;
2842 compl_cont_status = 0;
2843
2844 compl_curr_match = compl_first_match;
2845 if (compl_no_insert || compl_no_select)
2846 {
2847 ins_complete(K_DOWN, FALSE);
2848 if (compl_no_select)
2849 // Down/Up has no real effect.
2850 ins_complete(K_UP, FALSE);
2851 }
2852 else
2853 ins_complete(Ctrl_N, FALSE);
2854 compl_enter_selects = compl_no_insert;
2855
2856 // Lazily show the popup menu, unless we got interrupted.
2857 if (!compl_interrupted)
2858 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002859 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002860 out_flush();
2861}
2862
2863/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002864 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002865 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002866 void
2867f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002868{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002869 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002870 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002871
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002872 if (in_vim9script()
2873 && (check_for_number_arg(argvars, 0) == FAIL
2874 || check_for_list_arg(argvars, 1) == FAIL))
2875 return;
2876
Bram Moolenaar24959102022-05-07 20:01:16 +01002877 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002878 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002879 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002880 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002881 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002882
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002883 // "textlock" is set when evaluating 'completefunc' but we can change
2884 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002885 textlock = 0;
2886
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002887 // Check for undo allowed here, because if something was already inserted
2888 // the line was already saved for undo and this check isn't done.
2889 if (!undo_allowed())
2890 return;
2891
2892 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002893 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002894 else
2895 {
2896 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2897 if (startcol > 0)
2898 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002899 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002900 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002901}
2902
2903/*
2904 * "complete_add()" function
2905 */
2906 void
2907f_complete_add(typval_T *argvars, typval_T *rettv)
2908{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002909 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002910 return;
2911
Bram Moolenaar440cf092021-04-03 20:13:30 +02002912 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002913}
2914
2915/*
2916 * "complete_check()" function
2917 */
2918 void
2919f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2920{
2921 int saved = RedrawingDisabled;
2922
2923 RedrawingDisabled = 0;
2924 ins_compl_check_keys(0, TRUE);
2925 rettv->vval.v_number = ins_compl_interrupted();
2926 RedrawingDisabled = saved;
2927}
2928
2929/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002930 * Return Insert completion mode name string
2931 */
2932 static char_u *
2933ins_compl_mode(void)
2934{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002935 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002936 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2937
2938 return (char_u *)"";
2939}
2940
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002941/*
2942 * Assign the sequence number to all the completion matches which don't have
2943 * one assigned yet.
2944 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002945 static void
2946ins_compl_update_sequence_numbers()
2947{
2948 int number = 0;
2949 compl_T *match;
2950
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002951 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002952 {
2953 // search backwards for the first valid (!= -1) number.
2954 // This should normally succeed already at the first loop
2955 // cycle, so it's fast!
2956 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002957 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002958 if (match->cp_number != -1)
2959 {
2960 number = match->cp_number;
2961 break;
2962 }
2963 if (match != NULL)
2964 // go up and assign all numbers which are not assigned
2965 // yet
2966 for (match = match->cp_next;
2967 match != NULL && match->cp_number == -1;
2968 match = match->cp_next)
2969 match->cp_number = ++number;
2970 }
2971 else // BACKWARD
2972 {
2973 // search forwards (upwards) for the first valid (!= -1)
2974 // number. This should normally succeed already at the
2975 // first loop cycle, so it's fast!
2976 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002977 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002978 if (match->cp_number != -1)
2979 {
2980 number = match->cp_number;
2981 break;
2982 }
2983 if (match != NULL)
2984 // go down and assign all numbers which are not
2985 // assigned yet
2986 for (match = match->cp_prev; match
2987 && match->cp_number == -1;
2988 match = match->cp_prev)
2989 match->cp_number = ++number;
2990 }
2991}
2992
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002993/*
2994 * Get complete information
2995 */
2996 static void
2997get_complete_info(list_T *what_list, dict_T *retdict)
2998{
2999 int ret = OK;
3000 listitem_T *item;
3001#define CI_WHAT_MODE 0x01
3002#define CI_WHAT_PUM_VISIBLE 0x02
3003#define CI_WHAT_ITEMS 0x04
3004#define CI_WHAT_SELECTED 0x08
3005#define CI_WHAT_INSERTED 0x10
3006#define CI_WHAT_ALL 0xff
3007 int what_flag;
3008
3009 if (what_list == NULL)
3010 what_flag = CI_WHAT_ALL;
3011 else
3012 {
3013 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003014 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003015 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003016 {
3017 char_u *what = tv_get_string(&item->li_tv);
3018
3019 if (STRCMP(what, "mode") == 0)
3020 what_flag |= CI_WHAT_MODE;
3021 else if (STRCMP(what, "pum_visible") == 0)
3022 what_flag |= CI_WHAT_PUM_VISIBLE;
3023 else if (STRCMP(what, "items") == 0)
3024 what_flag |= CI_WHAT_ITEMS;
3025 else if (STRCMP(what, "selected") == 0)
3026 what_flag |= CI_WHAT_SELECTED;
3027 else if (STRCMP(what, "inserted") == 0)
3028 what_flag |= CI_WHAT_INSERTED;
3029 }
3030 }
3031
3032 if (ret == OK && (what_flag & CI_WHAT_MODE))
3033 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3034
3035 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3036 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3037
3038 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3039 {
3040 list_T *li;
3041 dict_T *di;
3042 compl_T *match;
3043
3044 li = list_alloc();
3045 if (li == NULL)
3046 return;
3047 ret = dict_add_list(retdict, "items", li);
3048 if (ret == OK && compl_first_match != NULL)
3049 {
3050 match = compl_first_match;
3051 do
3052 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003053 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003054 {
3055 di = dict_alloc();
3056 if (di == NULL)
3057 return;
3058 ret = list_append_dict(li, di);
3059 if (ret != OK)
3060 return;
3061 dict_add_string(di, "word", match->cp_str);
3062 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3063 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3064 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3065 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003066 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3067 // Add an empty string for backwards compatibility
3068 dict_add_string(di, "user_data", (char_u *)"");
3069 else
3070 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003071 }
3072 match = match->cp_next;
3073 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003074 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003075 }
3076 }
3077
3078 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003079 {
3080 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3081 ins_compl_update_sequence_numbers();
3082 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3083 ? compl_curr_match->cp_number - 1 : -1);
3084 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003085
3086 // TODO
3087 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3088}
3089
3090/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003091 * "complete_info()" function
3092 */
3093 void
3094f_complete_info(typval_T *argvars, typval_T *rettv)
3095{
3096 list_T *what_list = NULL;
3097
3098 if (rettv_dict_alloc(rettv) != OK)
3099 return;
3100
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003101 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3102 return;
3103
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003104 if (argvars[0].v_type != VAR_UNKNOWN)
3105 {
3106 if (argvars[0].v_type != VAR_LIST)
3107 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003108 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003109 return;
3110 }
3111 what_list = argvars[0].vval.v_list;
3112 }
3113 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003114}
3115#endif
3116
3117/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003118 * Returns TRUE when using a user-defined function for thesaurus completion.
3119 */
3120 static int
3121thesaurus_func_complete(int type UNUSED)
3122{
3123#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003124 return type == CTRL_X_THESAURUS
3125 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003126#else
3127 return FALSE;
3128#endif
3129}
3130
3131/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003132 * Return value of process_next_cpt_value()
3133 */
3134enum
3135{
3136 INS_COMPL_CPT_OK = 1,
3137 INS_COMPL_CPT_CONT,
3138 INS_COMPL_CPT_END
3139};
3140
3141/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003142 * state information used for getting the next set of insert completion
3143 * matches.
3144 */
3145typedef struct
3146{
3147 char_u *e_cpt; // current entry in 'complete'
3148 buf_T *ins_buf; // buffer being scanned
3149 pos_T *cur_match_pos; // current match position
3150 pos_T prev_match_pos; // previous match position
3151 int set_match_pos; // save first_match_pos/last_match_pos
3152 pos_T first_match_pos; // first match position
3153 pos_T last_match_pos; // last match position
3154 int found_all; // found all matches of a certain type.
3155 char_u *dict; // dictionary file to search
3156 int dict_f; // "dict" is an exact file name or not
3157} ins_compl_next_state_T;
3158
3159/*
3160 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003161 *
3162 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003163 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003164 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003165 * st->found_all - all matches of this type are found
3166 * st->ins_buf - search for completions in this buffer
3167 * st->first_match_pos - position of the first completion match
3168 * st->last_match_pos - position of the last completion match
3169 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003170 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003171 * st->dict - name of the dictionary or thesaurus file to search
3172 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003173 *
3174 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003175 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3176 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003177 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003178 */
3179 static int
3180process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003181 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003182 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003183 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003184{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003185 int compl_type = -1;
3186 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003187
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003188 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003189
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003190 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3191 st->e_cpt++;
3192
3193 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003194 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003195 st->ins_buf = curbuf;
3196 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003197 // Move the cursor back one character so that ^N can match the
3198 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003199 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003200 {
3201 // Move the cursor to after the last character in the
3202 // buffer, so that word at start of buffer is found
3203 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003204 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3205 st->first_match_pos.col =
3206 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003207 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003208 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003209 compl_type = 0;
3210
3211 // Remember the first match so that the loop stops when we
3212 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003213 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003214 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003215 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3216 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003217 {
3218 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003219 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003220 {
3221 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003222 st->first_match_pos.col = st->last_match_pos.col = 0;
3223 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3224 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003225 compl_type = 0;
3226 }
3227 else // unloaded buffer, scan like dictionary
3228 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003229 st->found_all = TRUE;
3230 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003231 {
3232 status = INS_COMPL_CPT_CONT;
3233 goto done;
3234 }
3235 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003236 st->dict = st->ins_buf->b_fname;
3237 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003238 }
3239 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3240 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003241 st->ins_buf->b_fname == NULL
3242 ? buf_spname(st->ins_buf)
3243 : st->ins_buf->b_sfname == NULL
3244 ? st->ins_buf->b_fname
3245 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003246 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3247 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003248 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 status = INS_COMPL_CPT_END;
3250 else
3251 {
3252 if (ctrl_x_mode_line_or_eval())
3253 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003256 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003257 compl_type = CTRL_X_DICTIONARY;
3258 else
3259 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003260 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003261 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003262 st->dict = st->e_cpt;
3263 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003264 }
3265 }
3266#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003267 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003268 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003269 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003270 compl_type = CTRL_X_PATH_DEFINES;
3271#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003272 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003273 {
3274 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3275 compl_type = CTRL_X_TAGS;
3276 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3277 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3278 }
3279 else
3280 compl_type = -1;
3281
3282 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003283 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003284
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003285 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003286 if (compl_type == -1)
3287 status = INS_COMPL_CPT_CONT;
3288 }
3289
3290done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003291 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003292 return status;
3293}
3294
3295#ifdef FEAT_FIND_ID
3296/*
3297 * Get the next set of identifiers or defines matching "compl_pattern" in
3298 * included files.
3299 */
3300 static void
3301get_next_include_file_completion(int compl_type)
3302{
3303 find_pattern_in_path(compl_pattern, compl_direction,
3304 (int)STRLEN(compl_pattern), FALSE, FALSE,
3305 (compl_type == CTRL_X_PATH_DEFINES
3306 && !(compl_cont_status & CONT_SOL))
3307 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3308 (linenr_T)1, (linenr_T)MAXLNUM);
3309}
3310#endif
3311
3312/*
3313 * Get the next set of words matching "compl_pattern" in dictionary or
3314 * thesaurus files.
3315 */
3316 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003317get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003318{
3319#ifdef FEAT_COMPL_FUNC
3320 if (thesaurus_func_complete(compl_type))
3321 expand_by_function(compl_type, compl_pattern);
3322 else
3323#endif
3324 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003325 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 : (compl_type == CTRL_X_THESAURUS
3327 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3328 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3329 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003330 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003331 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003332}
3333
3334/*
3335 * Get the next set of tag names matching "compl_pattern".
3336 */
3337 static void
3338get_next_tag_completion(void)
3339{
3340 int save_p_ic;
3341 char_u **matches;
3342 int num_matches;
3343
3344 // set p_ic according to p_ic, p_scs and pat for find_tags().
3345 save_p_ic = p_ic;
3346 p_ic = ignorecase(compl_pattern);
3347
3348 // Find up to TAG_MANY matches. Avoids that an enormous number
3349 // of matches is found when compl_pattern is empty
3350 g_tag_at_cursor = TRUE;
3351 if (find_tags(compl_pattern, &num_matches, &matches,
3352 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003353 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003354 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3355 ins_compl_add_matches(num_matches, matches, p_ic);
3356 g_tag_at_cursor = FALSE;
3357 p_ic = save_p_ic;
3358}
3359
3360/*
3361 * Get the next set of filename matching "compl_pattern".
3362 */
3363 static void
3364get_next_filename_completion(void)
3365{
3366 char_u **matches;
3367 int num_matches;
3368
3369 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3370 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3371 return;
3372
3373 // May change home directory back to "~".
3374 tilde_replace(compl_pattern, num_matches, matches);
3375#ifdef BACKSLASH_IN_FILENAME
3376 if (curbuf->b_p_csl[0] != NUL)
3377 {
3378 int i;
3379
3380 for (i = 0; i < num_matches; ++i)
3381 {
3382 char_u *ptr = matches[i];
3383
3384 while (*ptr != NUL)
3385 {
3386 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3387 *ptr = '/';
3388 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3389 *ptr = '\\';
3390 ptr += (*mb_ptr2len)(ptr);
3391 }
3392 }
3393 }
3394#endif
3395 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3396}
3397
3398/*
3399 * Get the next set of command-line completions matching "compl_pattern".
3400 */
3401 static void
3402get_next_cmdline_completion()
3403{
3404 char_u **matches;
3405 int num_matches;
3406
3407 if (expand_cmdline(&compl_xp, compl_pattern,
3408 (int)STRLEN(compl_pattern),
3409 &num_matches, &matches) == EXPAND_OK)
3410 ins_compl_add_matches(num_matches, matches, FALSE);
3411}
3412
3413/*
3414 * Get the next set of spell suggestions matching "compl_pattern".
3415 */
3416 static void
3417get_next_spell_completion(linenr_T lnum UNUSED)
3418{
3419#ifdef FEAT_SPELL
3420 char_u **matches;
3421 int num_matches;
3422
3423 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3424 if (num_matches > 0)
3425 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003426 else
3427 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003428#endif
3429}
3430
3431/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003432 * Return the next word or line from buffer "ins_buf" at position
3433 * "cur_match_pos" for completion. The length of the match is set in "len".
3434 */
3435 static char_u *
3436ins_comp_get_next_word_or_line(
3437 buf_T *ins_buf, // buffer being scanned
3438 pos_T *cur_match_pos, // current match position
3439 int *match_len,
3440 int *cont_s_ipos) // next ^X<> will set initial_pos
3441{
3442 char_u *ptr;
3443 int len;
3444
3445 *match_len = 0;
3446 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3447 cur_match_pos->col;
3448 if (ctrl_x_mode_line_or_eval())
3449 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003450 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003451 {
3452 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3453 return NULL;
3454 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3455 if (!p_paste)
3456 ptr = skipwhite(ptr);
3457 }
3458 len = (int)STRLEN(ptr);
3459 }
3460 else
3461 {
3462 char_u *tmp_ptr = ptr;
3463
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003464 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003465 {
3466 tmp_ptr += compl_length;
3467 // Skip if already inside a word.
3468 if (vim_iswordp(tmp_ptr))
3469 return NULL;
3470 // Find start of next word.
3471 tmp_ptr = find_word_start(tmp_ptr);
3472 }
3473 // Find end of this word.
3474 tmp_ptr = find_word_end(tmp_ptr);
3475 len = (int)(tmp_ptr - ptr);
3476
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003477 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003478 {
3479 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3480 {
3481 // Try next line, if any. the new word will be
3482 // "join" as if the normal command "J" was used.
3483 // IOSIZE is always greater than
3484 // compl_length, so the next STRNCPY always
3485 // works -- Acevedo
3486 STRNCPY(IObuff, ptr, len);
3487 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3488 tmp_ptr = ptr = skipwhite(ptr);
3489 // Find start of next word.
3490 tmp_ptr = find_word_start(tmp_ptr);
3491 // Find end of next word.
3492 tmp_ptr = find_word_end(tmp_ptr);
3493 if (tmp_ptr > ptr)
3494 {
3495 if (*ptr != ')' && IObuff[len - 1] != TAB)
3496 {
3497 if (IObuff[len - 1] != ' ')
3498 IObuff[len++] = ' ';
3499 // IObuf =~ "\k.* ", thus len >= 2
3500 if (p_js
3501 && (IObuff[len - 2] == '.'
3502 || (vim_strchr(p_cpo, CPO_JOINSP)
3503 == NULL
3504 && (IObuff[len - 2] == '?'
3505 || IObuff[len - 2] == '!'))))
3506 IObuff[len++] = ' ';
3507 }
3508 // copy as much as possible of the new word
3509 if (tmp_ptr - ptr >= IOSIZE - len)
3510 tmp_ptr = ptr + IOSIZE - len - 1;
3511 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3512 len += (int)(tmp_ptr - ptr);
3513 *cont_s_ipos = TRUE;
3514 }
3515 IObuff[len] = NUL;
3516 ptr = IObuff;
3517 }
3518 if (len == compl_length)
3519 return NULL;
3520 }
3521 }
3522
3523 *match_len = len;
3524 return ptr;
3525}
3526
3527/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003528 * Get the next set of words matching "compl_pattern" for default completion(s)
3529 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003530 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3531 * position "st->start_pos" in the "compl_direction" direction. If
3532 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3533 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003534 * Returns OK if a new next match is found, otherwise returns FAIL.
3535 */
3536 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003537get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003538{
3539 int found_new_match = FAIL;
3540 int save_p_scs;
3541 int save_p_ws;
3542 int looped_around = FALSE;
3543 char_u *ptr;
3544 int len;
3545
3546 // If 'infercase' is set, don't use 'smartcase' here
3547 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003548 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003549 p_scs = FALSE;
3550
3551 // Buffers other than curbuf are scanned from the beginning or the
3552 // end but never from the middle, thus setting nowrapscan in this
3553 // buffer is a good idea, on the other hand, we always set
3554 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3555 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003556 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003557 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003558 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559 p_ws = TRUE;
3560 looped_around = FALSE;
3561 for (;;)
3562 {
3563 int cont_s_ipos = FALSE;
3564
3565 ++msg_silent; // Don't want messages for wrapscan.
3566
3567 // ctrl_x_mode_line_or_eval() || word-wise search that
3568 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003569 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003570 found_new_match = search_for_exact_line(st->ins_buf,
3571 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003573 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3574 NULL, compl_direction, compl_pattern, 1L,
3575 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003577 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003578 {
3579 // set "compl_started" even on fail
3580 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003581 st->first_match_pos = *st->cur_match_pos;
3582 st->last_match_pos = *st->cur_match_pos;
3583 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003584 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003585 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3586 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003587 {
3588 found_new_match = FAIL;
3589 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003590 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003591 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3592 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3593 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003594 {
3595 if (looped_around)
3596 found_new_match = FAIL;
3597 else
3598 looped_around = TRUE;
3599 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003600 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003601 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3602 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3603 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003604 {
3605 if (looped_around)
3606 found_new_match = FAIL;
3607 else
3608 looped_around = TRUE;
3609 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003610 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003611 if (found_new_match == FAIL)
3612 break;
3613
3614 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003615 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003616 && start_pos->lnum == st->cur_match_pos->lnum
3617 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003619
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003620 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3621 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003622 if (ptr == NULL)
3623 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003625 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003626 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003627 0, cont_s_ipos) != NOTDONE)
3628 {
3629 found_new_match = OK;
3630 break;
3631 }
3632 }
3633 p_scs = save_p_scs;
3634 p_ws = save_p_ws;
3635
3636 return found_new_match;
3637}
3638
3639/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003640 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003641 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003642 */
3643 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003644get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003645{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003646 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003647
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003648 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003649 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003650 case -1:
3651 break;
3652#ifdef FEAT_FIND_ID
3653 case CTRL_X_PATH_PATTERNS:
3654 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003655 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003656 break;
3657#endif
3658
3659 case CTRL_X_DICTIONARY:
3660 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003661 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3662 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003663 break;
3664
3665 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003667 break;
3668
3669 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003670 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003671 break;
3672
3673 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003674 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003676 break;
3677
3678#ifdef FEAT_COMPL_FUNC
3679 case CTRL_X_FUNCTION:
3680 case CTRL_X_OMNI:
3681 expand_by_function(type, compl_pattern);
3682 break;
3683#endif
3684
3685 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003686 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003687 break;
3688
3689 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003690 found_new_match = get_next_default_completion(st, ini);
3691 if (found_new_match == FAIL && st->ins_buf == curbuf)
3692 st->found_all = TRUE;
3693 }
3694
3695 // check if compl_curr_match has changed, (e.g. other type of
3696 // expansion added something)
3697 if (type != 0 && compl_curr_match != compl_old_match)
3698 found_new_match = OK;
3699
3700 return found_new_match;
3701}
3702
3703/*
3704 * Get the next expansion(s), using "compl_pattern".
3705 * The search starts at position "ini" in curbuf and in the direction
3706 * compl_direction.
3707 * When "compl_started" is FALSE start at that position, otherwise continue
3708 * where we stopped searching before.
3709 * This may return before finding all the matches.
3710 * Return the total number of matches or -1 if still unknown -- Acevedo
3711 */
3712 static int
3713ins_compl_get_exp(pos_T *ini)
3714{
3715 static ins_compl_next_state_T st;
3716 int i;
3717 int found_new_match;
3718 int type = ctrl_x_mode;
3719
3720 if (!compl_started)
3721 {
3722 FOR_ALL_BUFFERS(st.ins_buf)
3723 st.ins_buf->b_scanned = 0;
3724 st.found_all = FALSE;
3725 st.ins_buf = curbuf;
3726 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3727 ? (char_u *)"." : curbuf->b_p_cpt;
3728 st.last_match_pos = st.first_match_pos = *ini;
3729 }
3730 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3731 st.ins_buf = curbuf; // In case the buffer was wiped out.
3732
3733 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003734 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003735 ? &st.last_match_pos : &st.first_match_pos;
3736
3737 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3738 for (;;)
3739 {
3740 found_new_match = FAIL;
3741 st.set_match_pos = FALSE;
3742
3743 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3744 // or if found_all says this entry is done. For ^X^L only use the
3745 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003746 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003747 && (!compl_started || st.found_all))
3748 {
3749 int status = process_next_cpt_value(&st, &type, ini);
3750
3751 if (status == INS_COMPL_CPT_END)
3752 break;
3753 if (status == INS_COMPL_CPT_CONT)
3754 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003755 }
3756
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003757 // If complete() was called then compl_pattern has been reset. The
3758 // following won't work then, bail out.
3759 if (compl_pattern == NULL)
3760 break;
3761
3762 // get the next set of completion matches
3763 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003764
3765 // break the loop for specialized modes (use 'complete' just for the
3766 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3767 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003768 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3769 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003770 {
3771 if (got_int)
3772 break;
3773 // Fill the popup menu as soon as possible.
3774 if (type != -1)
3775 ins_compl_check_keys(0, FALSE);
3776
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003777 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003778 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3779 break;
3780 compl_started = TRUE;
3781 }
3782 else
3783 {
3784 // Mark a buffer scanned when it has been scanned completely
3785 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003786 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003787
3788 compl_started = FALSE;
3789 }
3790 }
3791 compl_started = TRUE;
3792
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003793 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003794 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003795 found_new_match = FAIL;
3796
3797 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003798 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003799 && !ctrl_x_mode_line_or_eval()))
3800 i = ins_compl_make_cyclic();
3801
3802 if (compl_old_match != NULL)
3803 {
3804 // If several matches were added (FORWARD) or the search failed and has
3805 // just been made cyclic then we have to move compl_curr_match to the
3806 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003807 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003808 : compl_old_match->cp_prev;
3809 if (compl_curr_match == NULL)
3810 compl_curr_match = compl_old_match;
3811 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003812 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003813
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003814 return i;
3815}
3816
3817/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003818 * Update "compl_shown_match" to the actually shown match, it may differ when
3819 * "compl_leader" is used to omit some of the matches.
3820 */
3821 static void
3822ins_compl_update_shown_match(void)
3823{
3824 while (!ins_compl_equal(compl_shown_match,
3825 compl_leader, (int)STRLEN(compl_leader))
3826 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003827 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003828 compl_shown_match = compl_shown_match->cp_next;
3829
3830 // If we didn't find it searching forward, and compl_shows_dir is
3831 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003832 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003833 && !ins_compl_equal(compl_shown_match,
3834 compl_leader, (int)STRLEN(compl_leader))
3835 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003836 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003837 {
3838 while (!ins_compl_equal(compl_shown_match,
3839 compl_leader, (int)STRLEN(compl_leader))
3840 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003841 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003842 compl_shown_match = compl_shown_match->cp_prev;
3843 }
3844}
3845
3846/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003847 * Delete the old text being completed.
3848 */
3849 void
3850ins_compl_delete(void)
3851{
3852 int col;
3853
3854 // In insert mode: Delete the typed part.
3855 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003856 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003857 if ((int)curwin->w_cursor.col > col)
3858 {
3859 if (stop_arrow() == FAIL)
3860 return;
3861 backspace_until_column(col);
3862 }
3863
3864 // TODO: is this sufficient for redrawing? Redrawing everything causes
3865 // flicker, thus we can't do that.
3866 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003867#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003868 // clear v:completed_item
3869 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003870#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003871}
3872
3873/*
3874 * Insert the new text being completed.
3875 * "in_compl_func" is TRUE when called from complete_check().
3876 */
3877 void
3878ins_compl_insert(int in_compl_func)
3879{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003880 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003881
3882 // Make sure we don't go over the end of the string, this can happen with
3883 // illegal bytes.
3884 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3885 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003886 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003887 compl_used_match = FALSE;
3888 else
3889 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003890#ifdef FEAT_EVAL
3891 {
3892 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3893
3894 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3895 }
3896#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003897 if (!in_compl_func)
3898 compl_curr_match = compl_shown_match;
3899}
3900
3901/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003902 * show the file name for the completion match (if any). Truncate the file
3903 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003904 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003905 static void
3906ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003907{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003908 char *lead = _("match in file");
3909 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3910 char_u *s;
3911 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003912
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003913 if (space <= 0)
3914 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003915
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003916 // We need the tail that fits. With double-byte encoding going
3917 // back from the end is very slow, thus go from the start and keep
3918 // the text that fits in "space" between "s" and "e".
3919 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003920 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003921 space -= ptr2cells(e);
3922 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003923 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003924 space += ptr2cells(s);
3925 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003926 }
3927 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003928 msg_hist_off = TRUE;
3929 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3930 s > compl_shown_match->cp_fname ? "<" : "", s);
3931 msg((char *)IObuff);
3932 msg_hist_off = FALSE;
3933 redraw_cmdline = FALSE; // don't overwrite!
3934}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003935
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003936/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003937 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003938 * times. The number of matches found is returned in 'num_matches'.
3939 *
3940 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3941 * get more completions. If it is FALSE, then do nothing when there are no more
3942 * completions in the given direction.
3943 *
3944 * If "advance" is TRUE, then completion will move to the first match.
3945 * Otherwise, the original text will be shown.
3946 *
3947 * Returns OK on success and -1 if the number of matches are unknown.
3948 */
3949 static int
3950find_next_completion_match(
3951 int allow_get_expansion,
3952 int todo, // repeat completion this many times
3953 int advance,
3954 int *num_matches)
3955{
3956 int found_end = FALSE;
3957 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003958
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003959 while (--todo >= 0)
3960 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003961 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003962 {
3963 compl_shown_match = compl_shown_match->cp_next;
3964 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003965 && (is_first_match(compl_shown_match->cp_next)
3966 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003967 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003968 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003969 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003970 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003971 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003972 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003973 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003974 }
3975 else
3976 {
3977 if (!allow_get_expansion)
3978 {
3979 if (advance)
3980 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003981 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003982 compl_pending -= todo + 1;
3983 else
3984 compl_pending += todo + 1;
3985 }
3986 return -1;
3987 }
3988
3989 if (!compl_no_select && advance)
3990 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003991 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003992 --compl_pending;
3993 else
3994 ++compl_pending;
3995 }
3996
3997 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003998 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003999
4000 // handle any pending completions
4001 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004002 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004003 {
4004 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4005 {
4006 compl_shown_match = compl_shown_match->cp_next;
4007 --compl_pending;
4008 }
4009 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4010 {
4011 compl_shown_match = compl_shown_match->cp_prev;
4012 ++compl_pending;
4013 }
4014 else
4015 break;
4016 }
4017 found_end = FALSE;
4018 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004019 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004020 && compl_leader != NULL
4021 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004022 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004023 ++todo;
4024 else
4025 // Remember a matching item.
4026 found_compl = compl_shown_match;
4027
4028 // Stop at the end of the list when we found a usable match.
4029 if (found_end)
4030 {
4031 if (found_compl != NULL)
4032 {
4033 compl_shown_match = found_compl;
4034 break;
4035 }
4036 todo = 1; // use first usable match after wrapping around
4037 }
4038 }
4039
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004040 return OK;
4041}
4042
4043/*
4044 * Fill in the next completion in the current direction.
4045 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4046 * get more completions. If it is FALSE, then we just do nothing when there
4047 * are no more completions in a given direction. The latter case is used when
4048 * we are still in the middle of finding completions, to allow browsing
4049 * through the ones found so far.
4050 * Return the total number of matches, or -1 if still unknown -- webb.
4051 *
4052 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4053 * compl_shown_match here.
4054 *
4055 * Note that this function may be called recursively once only. First with
4056 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4057 * calls this function with "allow_get_expansion" FALSE.
4058 */
4059 static int
4060ins_compl_next(
4061 int allow_get_expansion,
4062 int count, // repeat completion this many times; should
4063 // be at least 1
4064 int insert_match, // Insert the newly selected match
4065 int in_compl_func) // called from complete_check()
4066{
4067 int num_matches = -1;
4068 int todo = count;
4069 int advance;
4070 int started = compl_started;
4071
4072 // When user complete function return -1 for findstart which is next
4073 // time of 'always', compl_shown_match become NULL.
4074 if (compl_shown_match == NULL)
4075 return -1;
4076
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004077 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004078 // Update "compl_shown_match" to the actually shown match
4079 ins_compl_update_shown_match();
4080
4081 if (allow_get_expansion && insert_match
4082 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4083 // Delete old text to be replaced
4084 ins_compl_delete();
4085
4086 // When finding the longest common text we stick at the original text,
4087 // don't let CTRL-N or CTRL-P move to the first match.
4088 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4089
4090 // When restarting the search don't insert the first match either.
4091 if (compl_restarting)
4092 {
4093 advance = FALSE;
4094 compl_restarting = FALSE;
4095 }
4096
4097 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4098 // around.
4099 if (find_next_completion_match(allow_get_expansion, todo, advance,
4100 &num_matches) == -1)
4101 return -1;
4102
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004103 // Insert the text of the new completion, or the compl_leader.
4104 if (compl_no_insert && !started)
4105 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004106 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004107 compl_used_match = FALSE;
4108 }
4109 else if (insert_match)
4110 {
4111 if (!compl_get_longest || compl_used_match)
4112 ins_compl_insert(in_compl_func);
4113 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004114 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004115 }
4116 else
4117 compl_used_match = FALSE;
4118
4119 if (!allow_get_expansion)
4120 {
4121 // may undisplay the popup menu first
4122 ins_compl_upd_pum();
4123
4124 if (pum_enough_matches())
4125 // Will display the popup menu, don't redraw yet to avoid flicker.
4126 pum_call_update_screen();
4127 else
4128 // Not showing the popup menu yet, redraw to show the user what was
4129 // inserted.
4130 update_screen(0);
4131
4132 // display the updated popup menu
4133 ins_compl_show_pum();
4134#ifdef FEAT_GUI
4135 if (gui.in_use)
4136 {
4137 // Show the cursor after the match, not after the redrawn text.
4138 setcursor();
4139 out_flush_cursor(FALSE, FALSE);
4140 }
4141#endif
4142
4143 // Delete old text to be replaced, since we're still searching and
4144 // don't want to match ourselves!
4145 ins_compl_delete();
4146 }
4147
4148 // Enter will select a match when the match wasn't inserted and the popup
4149 // menu is visible.
4150 if (compl_no_insert && !started)
4151 compl_enter_selects = TRUE;
4152 else
4153 compl_enter_selects = !insert_match && compl_match_array != NULL;
4154
4155 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004156 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004157 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004158
4159 return num_matches;
4160}
4161
4162/*
4163 * Call this while finding completions, to check whether the user has hit a key
4164 * that should change the currently displayed completion, or exit completion
4165 * mode. Also, when compl_pending is not zero, show a completion as soon as
4166 * possible. -- webb
4167 * "frequency" specifies out of how many calls we actually check.
4168 * "in_compl_func" is TRUE when called from complete_check(), don't set
4169 * compl_curr_match.
4170 */
4171 void
4172ins_compl_check_keys(int frequency, int in_compl_func)
4173{
4174 static int count = 0;
4175 int c;
4176
4177 // Don't check when reading keys from a script, :normal or feedkeys().
4178 // That would break the test scripts. But do check for keys when called
4179 // from complete_check().
4180 if (!in_compl_func && (using_script() || ex_normal_busy))
4181 return;
4182
4183 // Only do this at regular intervals
4184 if (++count < frequency)
4185 return;
4186 count = 0;
4187
4188 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4189 // can't do its work correctly.
4190 c = vpeekc_any();
4191 if (c != NUL)
4192 {
4193 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4194 {
4195 c = safe_vgetc(); // Eat the character
4196 compl_shows_dir = ins_compl_key2dir(c);
4197 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4198 c != K_UP && c != K_DOWN, in_compl_func);
4199 }
4200 else
4201 {
4202 // Need to get the character to have KeyTyped set. We'll put it
4203 // back with vungetc() below. But skip K_IGNORE.
4204 c = safe_vgetc();
4205 if (c != K_IGNORE)
4206 {
4207 // Don't interrupt completion when the character wasn't typed,
4208 // e.g., when doing @q to replay keys.
4209 if (c != Ctrl_R && KeyTyped)
4210 compl_interrupted = TRUE;
4211
4212 vungetc(c);
4213 }
4214 }
4215 }
4216 if (compl_pending != 0 && !got_int && !compl_no_insert)
4217 {
4218 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4219
4220 compl_pending = 0;
4221 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4222 }
4223}
4224
4225/*
4226 * Decide the direction of Insert mode complete from the key typed.
4227 * Returns BACKWARD or FORWARD.
4228 */
4229 static int
4230ins_compl_key2dir(int c)
4231{
4232 if (c == Ctrl_P || c == Ctrl_L
4233 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4234 return BACKWARD;
4235 return FORWARD;
4236}
4237
4238/*
4239 * Return TRUE for keys that are used for completion only when the popup menu
4240 * is visible.
4241 */
4242 static int
4243ins_compl_pum_key(int c)
4244{
4245 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4246 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4247 || c == K_UP || c == K_DOWN);
4248}
4249
4250/*
4251 * Decide the number of completions to move forward.
4252 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4253 */
4254 static int
4255ins_compl_key2count(int c)
4256{
4257 int h;
4258
4259 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4260 {
4261 h = pum_get_height();
4262 if (h > 3)
4263 h -= 2; // keep some context
4264 return h;
4265 }
4266 return 1;
4267}
4268
4269/*
4270 * Return TRUE if completion with "c" should insert the match, FALSE if only
4271 * to change the currently selected completion.
4272 */
4273 static int
4274ins_compl_use_match(int c)
4275{
4276 switch (c)
4277 {
4278 case K_UP:
4279 case K_DOWN:
4280 case K_PAGEDOWN:
4281 case K_KPAGEDOWN:
4282 case K_S_DOWN:
4283 case K_PAGEUP:
4284 case K_KPAGEUP:
4285 case K_S_UP:
4286 return FALSE;
4287 }
4288 return TRUE;
4289}
4290
4291/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004292 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4293 * completion)
4294 * Sets the global variables: compl_col, compl_length and compl_pattern.
4295 * Uses the global variables: compl_cont_status and ctrl_x_mode
4296 */
4297 static int
4298get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4299{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004300 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004301 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004302 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004303 {
4304 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4305 ;
4306 compl_col += ++startcol;
4307 compl_length = curs_col - startcol;
4308 }
4309 if (p_ic)
4310 compl_pattern = str_foldcase(line + compl_col,
4311 compl_length, NULL, 0);
4312 else
4313 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4314 if (compl_pattern == NULL)
4315 return FAIL;
4316 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004317 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004318 {
4319 char_u *prefix = (char_u *)"\\<";
4320
4321 // we need up to 2 extra chars for the prefix
4322 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4323 compl_length) + 2);
4324 if (compl_pattern == NULL)
4325 return FAIL;
4326 if (!vim_iswordp(line + compl_col)
4327 || (compl_col > 0
4328 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4329 prefix = (char_u *)"";
4330 STRCPY((char *)compl_pattern, prefix);
4331 (void)quote_meta(compl_pattern + STRLEN(prefix),
4332 line + compl_col, compl_length);
4333 }
4334 else if (--startcol < 0
4335 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4336 {
4337 // Match any word of at least two chars
4338 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4339 if (compl_pattern == NULL)
4340 return FAIL;
4341 compl_col += curs_col;
4342 compl_length = 0;
4343 }
4344 else
4345 {
4346 // Search the point of change class of multibyte character
4347 // or not a word single byte character backward.
4348 if (has_mbyte)
4349 {
4350 int base_class;
4351 int head_off;
4352
4353 startcol -= (*mb_head_off)(line, line + startcol);
4354 base_class = mb_get_class(line + startcol);
4355 while (--startcol >= 0)
4356 {
4357 head_off = (*mb_head_off)(line, line + startcol);
4358 if (base_class != mb_get_class(line + startcol
4359 - head_off))
4360 break;
4361 startcol -= head_off;
4362 }
4363 }
4364 else
4365 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4366 ;
4367 compl_col += ++startcol;
4368 compl_length = (int)curs_col - startcol;
4369 if (compl_length == 1)
4370 {
4371 // Only match word with at least two chars -- webb
4372 // there's no need to call quote_meta,
4373 // alloc(7) is enough -- Acevedo
4374 compl_pattern = alloc(7);
4375 if (compl_pattern == NULL)
4376 return FAIL;
4377 STRCPY((char *)compl_pattern, "\\<");
4378 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4379 STRCAT((char *)compl_pattern, "\\k");
4380 }
4381 else
4382 {
4383 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4384 compl_length) + 2);
4385 if (compl_pattern == NULL)
4386 return FAIL;
4387 STRCPY((char *)compl_pattern, "\\<");
4388 (void)quote_meta(compl_pattern + 2, line + compl_col,
4389 compl_length);
4390 }
4391 }
4392
4393 return OK;
4394}
4395
4396/*
4397 * Get the pattern, column and length for whole line completion or for the
4398 * complete() function.
4399 * Sets the global variables: compl_col, compl_length and compl_pattern.
4400 */
4401 static int
4402get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4403{
4404 compl_col = (colnr_T)getwhitecols(line);
4405 compl_length = (int)curs_col - (int)compl_col;
4406 if (compl_length < 0) // cursor in indent: empty pattern
4407 compl_length = 0;
4408 if (p_ic)
4409 compl_pattern = str_foldcase(line + compl_col, compl_length,
4410 NULL, 0);
4411 else
4412 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4413 if (compl_pattern == NULL)
4414 return FAIL;
4415
4416 return OK;
4417}
4418
4419/*
4420 * Get the pattern, column and length for filename completion.
4421 * Sets the global variables: compl_col, compl_length and compl_pattern.
4422 */
4423 static int
4424get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4425{
4426 // Go back to just before the first filename character.
4427 if (startcol > 0)
4428 {
4429 char_u *p = line + startcol;
4430
4431 MB_PTR_BACK(line, p);
4432 while (p > line && vim_isfilec(PTR2CHAR(p)))
4433 MB_PTR_BACK(line, p);
4434 if (p == line && vim_isfilec(PTR2CHAR(p)))
4435 startcol = 0;
4436 else
4437 startcol = (int)(p - line) + 1;
4438 }
4439
4440 compl_col += startcol;
4441 compl_length = (int)curs_col - startcol;
4442 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4443 if (compl_pattern == NULL)
4444 return FAIL;
4445
4446 return OK;
4447}
4448
4449/*
4450 * Get the pattern, column and length for command-line completion.
4451 * Sets the global variables: compl_col, compl_length and compl_pattern.
4452 */
4453 static int
4454get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4455{
4456 compl_pattern = vim_strnsave(line, curs_col);
4457 if (compl_pattern == NULL)
4458 return FAIL;
4459 set_cmd_context(&compl_xp, compl_pattern,
4460 (int)STRLEN(compl_pattern), curs_col, FALSE);
4461 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4462 || compl_xp.xp_context == EXPAND_NOTHING)
4463 // No completion possible, use an empty pattern to get a
4464 // "pattern not found" message.
4465 compl_col = curs_col;
4466 else
4467 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4468 compl_length = curs_col - compl_col;
4469
4470 return OK;
4471}
4472
4473/*
4474 * Get the pattern, column and length for user defined completion ('omnifunc',
4475 * 'completefunc' and 'thesaurusfunc')
4476 * Sets the global variables: compl_col, compl_length and compl_pattern.
4477 * Uses the global variable: spell_bad_len
4478 */
4479 static int
4480get_userdefined_compl_info(colnr_T curs_col UNUSED)
4481{
4482 int ret = FAIL;
4483
4484#ifdef FEAT_COMPL_FUNC
4485 // Call user defined function 'completefunc' with "a:findstart"
4486 // set to 1 to obtain the length of text to use for completion.
4487 char_u *line;
4488 typval_T args[3];
4489 int col;
4490 char_u *funcname;
4491 pos_T pos;
4492 int save_State = State;
4493 callback_T *cb;
4494
4495 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4496 // length as a string
4497 funcname = get_complete_funcname(ctrl_x_mode);
4498 if (*funcname == NUL)
4499 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004500 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004501 ? "completefunc" : "omnifunc");
4502 return FAIL;
4503 }
4504
4505 args[0].v_type = VAR_NUMBER;
4506 args[0].vval.v_number = 1;
4507 args[1].v_type = VAR_STRING;
4508 args[1].vval.v_string = (char_u *)"";
4509 args[2].v_type = VAR_UNKNOWN;
4510 pos = curwin->w_cursor;
4511 ++textwinlock;
4512 cb = get_insert_callback(ctrl_x_mode);
4513 col = call_callback_retnr(cb, 2, args);
4514 --textwinlock;
4515
4516 State = save_State;
4517 curwin->w_cursor = pos; // restore the cursor position
4518 validate_cursor();
4519 if (!EQUAL_POS(curwin->w_cursor, pos))
4520 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004521 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004522 return FAIL;
4523 }
4524
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004525 // Return value -2 means the user complete function wants to cancel the
4526 // complete without an error, do the same if the function did not execute
4527 // successfully.
4528 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004529 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004530 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004531 if (col == -3)
4532 {
4533 ctrl_x_mode = CTRL_X_NORMAL;
4534 edit_submode = NULL;
4535 if (!shortmess(SHM_COMPLETIONMENU))
4536 msg_clr_cmdline();
4537 return FAIL;
4538 }
4539
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004540 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004541 // completion.
4542 compl_opt_refresh_always = FALSE;
4543 compl_opt_suppress_empty = FALSE;
4544
4545 if (col < 0)
4546 col = curs_col;
4547 compl_col = col;
4548 if (compl_col > curs_col)
4549 compl_col = curs_col;
4550
4551 // Setup variables for completion. Need to obtain "line" again,
4552 // it may have become invalid.
4553 line = ml_get(curwin->w_cursor.lnum);
4554 compl_length = curs_col - compl_col;
4555 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4556 if (compl_pattern == NULL)
4557 return FAIL;
4558
4559 ret = OK;
4560#endif
4561
4562 return ret;
4563}
4564
4565/*
4566 * Get the pattern, column and length for spell completion.
4567 * Sets the global variables: compl_col, compl_length and compl_pattern.
4568 * Uses the global variable: spell_bad_len
4569 */
4570 static int
4571get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4572{
4573 int ret = FAIL;
4574#ifdef FEAT_SPELL
4575 char_u *line;
4576
4577 if (spell_bad_len > 0)
4578 compl_col = curs_col - spell_bad_len;
4579 else
4580 compl_col = spell_word_start(startcol);
4581 if (compl_col >= (colnr_T)startcol)
4582 {
4583 compl_length = 0;
4584 compl_col = curs_col;
4585 }
4586 else
4587 {
4588 spell_expand_check_cap(compl_col);
4589 compl_length = (int)curs_col - compl_col;
4590 }
4591 // Need to obtain "line" again, it may have become invalid.
4592 line = ml_get(curwin->w_cursor.lnum);
4593 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4594 if (compl_pattern == NULL)
4595 return FAIL;
4596
4597 ret = OK;
4598#endif
4599
4600 return ret;
4601}
4602
4603/*
4604 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004605 * "startcol" - start column number of the completion pattern/text
4606 * "cur_col" - current cursor column
4607 * On return, "line_invalid" is set to TRUE, if the current line may have
4608 * become invalid and needs to be fetched again.
4609 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004610 */
4611 static int
4612compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4613{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004614 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004615 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4616 && !thesaurus_func_complete(ctrl_x_mode)))
4617 {
4618 return get_normal_compl_info(line, startcol, curs_col);
4619 }
4620 else if (ctrl_x_mode_line_or_eval())
4621 {
4622 return get_wholeline_compl_info(line, curs_col);
4623 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004624 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004625 {
4626 return get_filename_compl_info(line, startcol, curs_col);
4627 }
4628 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4629 {
4630 return get_cmdline_compl_info(line, curs_col);
4631 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004632 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004633 || thesaurus_func_complete(ctrl_x_mode))
4634 {
4635 if (get_userdefined_compl_info(curs_col) == FAIL)
4636 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004637 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004638 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004639 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004640 {
4641 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4642 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004643 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004644 }
4645 else
4646 {
4647 internal_error("ins_complete()");
4648 return FAIL;
4649 }
4650
4651 return OK;
4652}
4653
4654/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004655 * Continue an interrupted completion mode search in "line".
4656 *
4657 * If this same ctrl_x_mode has been interrupted use the text from
4658 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4659 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4660 * the same line as the cursor then fix it (the line has been split because it
4661 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4662 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004663 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004664 static void
4665ins_compl_continue_search(char_u *line)
4666{
4667 // it is a continued search
4668 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004669 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4670 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004671 {
4672 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4673 {
4674 // line (probably) wrapped, set compl_startpos to the
4675 // first non_blank in the line, if it is not a wordchar
4676 // include it to get a better pattern, but then we don't
4677 // want the "\\<" prefix, check it below
4678 compl_col = (colnr_T)getwhitecols(line);
4679 compl_startpos.col = compl_col;
4680 compl_startpos.lnum = curwin->w_cursor.lnum;
4681 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4682 }
4683 else
4684 {
4685 // S_IPOS was set when we inserted a word that was at the
4686 // beginning of the line, which means that we'll go to SOL
4687 // mode but first we need to redefine compl_startpos
4688 if (compl_cont_status & CONT_S_IPOS)
4689 {
4690 compl_cont_status |= CONT_SOL;
4691 compl_startpos.col = (colnr_T)(skipwhite(
4692 line + compl_length
4693 + compl_startpos.col) - line);
4694 }
4695 compl_col = compl_startpos.col;
4696 }
4697 compl_length = curwin->w_cursor.col - (int)compl_col;
4698 // IObuff is used to add a "word from the next line" would we
4699 // have enough space? just being paranoid
4700#define MIN_SPACE 75
4701 if (compl_length > (IOSIZE - MIN_SPACE))
4702 {
4703 compl_cont_status &= ~CONT_SOL;
4704 compl_length = (IOSIZE - MIN_SPACE);
4705 compl_col = curwin->w_cursor.col - compl_length;
4706 }
4707 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4708 if (compl_length < 1)
4709 compl_cont_status &= CONT_LOCAL;
4710 }
4711 else if (ctrl_x_mode_line_or_eval())
4712 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4713 else
4714 compl_cont_status = 0;
4715}
4716
4717/*
4718 * start insert mode completion
4719 */
4720 static int
4721ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004722{
4723 char_u *line;
4724 int startcol = 0; // column where searched text starts
4725 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004726 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004727 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004728 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004729
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004730 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004731
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004732 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004733 did_si = FALSE;
4734 can_si = FALSE;
4735 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004736 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004737 return FAIL;
4738
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004739 line = ml_get(curwin->w_cursor.lnum);
4740 curs_col = curwin->w_cursor.col;
4741 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004742
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004743 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4744 && compl_cont_mode == ctrl_x_mode)
4745 // this same ctrl-x_mode was interrupted previously. Continue the
4746 // completion.
4747 ins_compl_continue_search(line);
4748 else
4749 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004750
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004751 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004752 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004753 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004754 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004755 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4756 compl_cont_status = 0;
4757 compl_cont_status |= CONT_N_ADDS;
4758 compl_startpos = curwin->w_cursor;
4759 startcol = (int)curs_col;
4760 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004761 }
4762
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004763 // Work out completion pattern and original text -- webb
4764 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4765 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004766 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4767 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004768 // restore did_ai, so that adding comment leader works
4769 did_ai = save_did_ai;
4770 return FAIL;
4771 }
4772 // If "line" was changed while getting completion info get it again.
4773 if (line_invalid)
4774 line = ml_get(curwin->w_cursor.lnum);
4775
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004776 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004777 {
4778 edit_submode_pre = (char_u *)_(" Adding");
4779 if (ctrl_x_mode_line_or_eval())
4780 {
4781 // Insert a new line, keep indentation but ignore 'comments'.
4782 char_u *old = curbuf->b_p_com;
4783
4784 curbuf->b_p_com = (char_u *)"";
4785 compl_startpos.lnum = curwin->w_cursor.lnum;
4786 compl_startpos.col = compl_col;
4787 ins_eol('\r');
4788 curbuf->b_p_com = old;
4789 compl_length = 0;
4790 compl_col = curwin->w_cursor.col;
4791 }
4792 }
4793 else
4794 {
4795 edit_submode_pre = NULL;
4796 compl_startpos.col = compl_col;
4797 }
4798
4799 if (compl_cont_status & CONT_LOCAL)
4800 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4801 else
4802 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4803
4804 // If any of the original typed text has been changed we need to fix
4805 // the redo buffer.
4806 ins_compl_fixRedoBufForLeader(NULL);
4807
4808 // Always add completion for the original text.
4809 vim_free(compl_orig_text);
4810 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4811 if (p_ic)
4812 flags |= CP_ICASE;
4813 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4814 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4815 {
4816 VIM_CLEAR(compl_pattern);
4817 VIM_CLEAR(compl_orig_text);
4818 return FAIL;
4819 }
4820
4821 // showmode might reset the internal line pointers, so it must
4822 // be called before line = ml_get(), or when this address is no
4823 // longer needed. -- Acevedo.
4824 edit_submode_extra = (char_u *)_("-- Searching...");
4825 edit_submode_highl = HLF_COUNT;
4826 showmode();
4827 edit_submode_extra = NULL;
4828 out_flush();
4829
4830 return OK;
4831}
4832
4833/*
4834 * display the completion status message
4835 */
4836 static void
4837ins_compl_show_statusmsg(void)
4838{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004839 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004840 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004841 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004842 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004843 ? (char_u *)_(e_hitend)
4844 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004845 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004846 }
4847
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004848 if (edit_submode_extra == NULL)
4849 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004850 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004851 {
4852 edit_submode_extra = (char_u *)_("Back at original");
4853 edit_submode_highl = HLF_W;
4854 }
4855 else if (compl_cont_status & CONT_S_IPOS)
4856 {
4857 edit_submode_extra = (char_u *)_("Word from other line");
4858 edit_submode_highl = HLF_COUNT;
4859 }
4860 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4861 {
4862 edit_submode_extra = (char_u *)_("The only match");
4863 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004864 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004865 }
4866 else
4867 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004868#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004869 // Update completion sequence number when needed.
4870 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004871 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004872#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004873 // The match should always have a sequence number now, this is
4874 // just a safety check.
4875 if (compl_curr_match->cp_number != -1)
4876 {
4877 // Space for 10 text chars. + 2x10-digit no.s = 31.
4878 // Translations may need more than twice that.
4879 static char_u match_ref[81];
4880
4881 if (compl_matches > 0)
4882 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004883 _("match %d of %d"),
4884 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004885 else
4886 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004887 _("match %d"),
4888 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004889 edit_submode_extra = match_ref;
4890 edit_submode_highl = HLF_R;
4891 if (dollar_vcol >= 0)
4892 curs_columns(FALSE);
4893 }
4894 }
4895 }
4896
4897 // Show a message about what (completion) mode we're in.
4898 if (!compl_opt_suppress_empty)
4899 {
4900 showmode();
4901 if (!shortmess(SHM_COMPLETIONMENU))
4902 {
4903 if (edit_submode_extra != NULL)
4904 {
4905 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004906 {
4907 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004908 msg_attr((char *)edit_submode_extra,
4909 edit_submode_highl < HLF_COUNT
4910 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004911 msg_hist_off = FALSE;
4912 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004913 }
4914 else
4915 msg_clr_cmdline(); // necessary for "noshowmode"
4916 }
4917 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004918}
4919
4920/*
4921 * Do Insert mode completion.
4922 * Called when character "c" was typed, which has a meaning for completion.
4923 * Returns OK if completion was done, FAIL if something failed (out of mem).
4924 */
4925 int
4926ins_complete(int c, int enable_pum)
4927{
4928 int n;
4929 int save_w_wrow;
4930 int save_w_leftcol;
4931 int insert_match;
4932
4933 compl_direction = ins_compl_key2dir(c);
4934 insert_match = ins_compl_use_match(c);
4935
4936 if (!compl_started)
4937 {
4938 if (ins_compl_start() == FAIL)
4939 return FAIL;
4940 }
4941 else if (insert_match && stop_arrow() == FAIL)
4942 return FAIL;
4943
4944 compl_shown_match = compl_curr_match;
4945 compl_shows_dir = compl_direction;
4946
4947 // Find next match (and following matches).
4948 save_w_wrow = curwin->w_wrow;
4949 save_w_leftcol = curwin->w_leftcol;
4950 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4951
4952 // may undisplay the popup menu
4953 ins_compl_upd_pum();
4954
4955 if (n > 1) // all matches have been found
4956 compl_matches = n;
4957 compl_curr_match = compl_shown_match;
4958 compl_direction = compl_shows_dir;
4959
4960 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4961 // mode.
4962 if (got_int && !global_busy)
4963 {
4964 (void)vgetc();
4965 got_int = FALSE;
4966 }
4967
4968 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004969 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004970 {
4971 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4972 // because we couldn't expand anything at first place, but if we used
4973 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4974 // (such as M in M'exico) if not tried already. -- Acevedo
4975 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004976 || compl_status_adding()
4977 || (ctrl_x_mode_not_default()
4978 && !ctrl_x_mode_path_patterns()
4979 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004980 compl_cont_status &= ~CONT_N_ADDS;
4981 }
4982
4983 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4984 compl_cont_status |= CONT_S_IPOS;
4985 else
4986 compl_cont_status &= ~CONT_S_IPOS;
4987
4988 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004989
4990 // Show the popup menu, unless we got interrupted.
4991 if (enable_pum && !compl_interrupted)
4992 show_pum(save_w_wrow, save_w_leftcol);
4993
4994 compl_was_interrupted = compl_interrupted;
4995 compl_interrupted = FALSE;
4996
4997 return OK;
4998}
4999
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005000/*
5001 * Remove (if needed) and show the popup menu
5002 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005003 static void
5004show_pum(int prev_w_wrow, int prev_w_leftcol)
5005{
5006 // RedrawingDisabled may be set when invoked through complete().
5007 int n = RedrawingDisabled;
5008
5009 RedrawingDisabled = 0;
5010
5011 // If the cursor moved or the display scrolled we need to remove the pum
5012 // first.
5013 setcursor();
5014 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5015 ins_compl_del_pum();
5016
5017 ins_compl_show_pum();
5018 setcursor();
5019 RedrawingDisabled = n;
5020}
5021
5022/*
5023 * Looks in the first "len" chars. of "src" for search-metachars.
5024 * If dest is not NULL the chars. are copied there quoting (with
5025 * a backslash) the metachars, and dest would be NUL terminated.
5026 * Returns the length (needed) of dest
5027 */
5028 static unsigned
5029quote_meta(char_u *dest, char_u *src, int len)
5030{
5031 unsigned m = (unsigned)len + 1; // one extra for the NUL
5032
5033 for ( ; --len >= 0; src++)
5034 {
5035 switch (*src)
5036 {
5037 case '.':
5038 case '*':
5039 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005040 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005041 break;
5042 // FALLTHROUGH
5043 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005044 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005045 break;
5046 // FALLTHROUGH
5047 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005048 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005049 break;
5050 // FALLTHROUGH
5051 case '^': // currently it's not needed.
5052 case '$':
5053 m++;
5054 if (dest != NULL)
5055 *dest++ = '\\';
5056 break;
5057 }
5058 if (dest != NULL)
5059 *dest++ = *src;
5060 // Copy remaining bytes of a multibyte character.
5061 if (has_mbyte)
5062 {
5063 int i, mb_len;
5064
5065 mb_len = (*mb_ptr2len)(src) - 1;
5066 if (mb_len > 0 && len >= mb_len)
5067 for (i = 0; i < mb_len; ++i)
5068 {
5069 --len;
5070 ++src;
5071 if (dest != NULL)
5072 *dest++ = *src;
5073 }
5074 }
5075 }
5076 if (dest != NULL)
5077 *dest = NUL;
5078
5079 return m;
5080}
5081
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005082#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005083 void
5084free_insexpand_stuff(void)
5085{
5086 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005087# ifdef FEAT_EVAL
5088 free_callback(&cfu_cb);
5089 free_callback(&ofu_cb);
5090 free_callback(&tsrfu_cb);
5091# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005092}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005093#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005094
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005095#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005096/*
5097 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5098 * spelled word, if there is one.
5099 */
5100 static void
5101spell_back_to_badword(void)
5102{
5103 pos_T tpos = curwin->w_cursor;
5104
5105 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5106 if (curwin->w_cursor.col != tpos.col)
5107 start_arrow(&tpos);
5108}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005109#endif