blob: 7d04690c5ed701bf6d7585403f5c1e4a1256166d [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)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 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/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010091 * Structure used to store one match for insert completion.
92 */
93typedef struct compl_S compl_T;
94struct compl_S
95{
96 compl_T *cp_next;
97 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +010098 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +010099 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200100 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100101#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100102 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100103#endif
glepnir0fe17f82024-10-08 22:26:44 +0200104 char_u *cp_fname; // file containing the match, allocated when
105 // cp_flags has CP_FREE_FNAME
106 int cp_flags; // CP_ values
107 int cp_number; // sequence number
108 int cp_score; // fuzzy match score
glepnird4088ed2024-12-31 10:55:22 +0100109 int cp_in_match_array; // collected by compl_match_array
glepnir7baa0142024-10-09 20:19:25 +0200110 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200111 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100112};
113
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200114// values for cp_flags
115# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
116# define CP_FREE_FNAME 2 // cp_fname is allocated
117# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
118# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
119# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200120# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100122/*
123 * All the current matches are stored in a list.
124 * "compl_first_match" points to the start of the list.
125 * "compl_curr_match" points to the currently selected entry.
126 * "compl_shown_match" is different from compl_curr_match during
127 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000128 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100129 */
130static compl_T *compl_first_match = NULL;
131static compl_T *compl_curr_match = NULL;
132static compl_T *compl_shown_match = NULL;
133static compl_T *compl_old_match = NULL;
134
135// After using a cursor key <Enter> selects a match in the popup menu,
136// otherwise it inserts a line break.
137static int compl_enter_selects = FALSE;
138
139// When "compl_leader" is not NULL only matches that start with this string
140// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100141static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100142
143static int compl_get_longest = FALSE; // put longest common string
144 // in compl_leader
145
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100146// Selected one of the matches. When FALSE the match was edited or using the
147// longest common string.
148static int compl_used_match;
149
150// didn't finish finding completions.
151static int compl_was_interrupted = FALSE;
152
153// Set when character typed while looking for matches and it means we should
154// stop looking for matches.
155static int compl_interrupted = FALSE;
156
157static int compl_restarting = FALSE; // don't insert match
158
159// When the first completion is done "compl_started" is set. When it's
160// FALSE the word to be completed must be located.
161static int compl_started = FALSE;
162
163// Which Ctrl-X mode are we in?
164static int ctrl_x_mode = CTRL_X_NORMAL;
165
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000166static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100167static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100168static int compl_direction = FORWARD;
169static int compl_shows_dir = FORWARD;
170static int compl_pending = 0; // > 1 for postponed CTRL-N
171static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000172// Length in bytes of the text being completed (this is deleted to be replaced
173// by the match.)
174static int compl_length = 0;
glepnir76bdb822025-02-08 19:04:51 +0100175static linenr_T compl_lnum = 0; // lnum where the completion start
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100176static colnr_T compl_col = 0; // column where the text starts
177 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100178static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100179static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100180 // completion started
181static int compl_cont_mode = 0;
182static expand_T compl_xp;
183
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000184// List of flags for method of completion.
185static int compl_cont_status = 0;
186# define CONT_ADDING 1 // "normal" or "adding" expansion
187# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
188 // it's set only iff N_ADDS is set
189# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
190# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
191 // if so, word-wise-expansion will set SOL
192# define CONT_SOL 16 // pattern includes start of line, just for
193 // word-wise expansion, not set for ^X^L
194# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
195 // expansion, (eg use complete=.)
196
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100197static int compl_opt_refresh_always = FALSE;
198static int compl_opt_suppress_empty = FALSE;
199
glepnira218cc62024-06-03 19:32:39 +0200200static int compl_selected_item = -1;
201
glepnir8159fb12024-07-17 20:32:54 +0200202static int *compl_fuzzy_scores;
203
glepnir6a38aff2024-12-16 21:56:16 +0100204// "compl_match_array" points the currently displayed list of entries in the
205// popup menu. It is NULL when there is no popup menu.
206static pumitem_T *compl_match_array = NULL;
207static int compl_match_arraysize;
208
glepnir80b66202024-11-27 21:53:53 +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, int *user_hl);
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);
John Marriott5e6ea922024-11-23 14:01:57 +0100219static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100220static 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);
glepnir76bdb822025-02-08 19:04:51 +0100230static int ins_compl_has_multiple(void);
231static void ins_compl_expand_multiple(char_u *str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100232
233#ifdef FEAT_SPELL
234static void spell_back_to_badword(void);
235static int spell_bad_len = 0; // length of located bad word
236#endif
237
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100238/*
239 * CTRL-X pressed in Insert mode.
240 */
241 void
242ins_ctrl_x(void)
243{
zeertzjqdca29d92021-08-31 19:12:51 +0200244 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000246 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100247 if (compl_cont_status & CONT_N_ADDS)
248 compl_cont_status |= CONT_INTRPT;
249 else
250 compl_cont_status = 0;
251 // We're not sure which CTRL-X mode it will be yet
252 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
253 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
254 edit_submode_pre = NULL;
255 showmode();
256 }
zeertzjqdca29d92021-08-31 19:12:51 +0200257 else
258 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
259 // CTRL-V look like CTRL-N
260 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100261
LemonBoy2bf52dd2022-04-09 18:17:34 +0100262 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100263}
264
265/*
266 * Functions to check the current CTRL-X mode.
267 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000268int ctrl_x_mode_none(void)
269 { return ctrl_x_mode == 0; }
270int ctrl_x_mode_normal(void)
271 { return ctrl_x_mode == CTRL_X_NORMAL; }
272int ctrl_x_mode_scroll(void)
273 { return ctrl_x_mode == CTRL_X_SCROLL; }
274int ctrl_x_mode_whole_line(void)
275 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
276int ctrl_x_mode_files(void)
277 { return ctrl_x_mode == CTRL_X_FILES; }
278int ctrl_x_mode_tags(void)
279 { return ctrl_x_mode == CTRL_X_TAGS; }
280int ctrl_x_mode_path_patterns(void)
281 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
282int ctrl_x_mode_path_defines(void)
283 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
284int ctrl_x_mode_dictionary(void)
285 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
286int ctrl_x_mode_thesaurus(void)
287 { return ctrl_x_mode == CTRL_X_THESAURUS; }
288int ctrl_x_mode_cmdline(void)
289 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200290 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000291int ctrl_x_mode_function(void)
292 { return ctrl_x_mode == CTRL_X_FUNCTION; }
293int ctrl_x_mode_omni(void)
294 { return ctrl_x_mode == CTRL_X_OMNI; }
295int ctrl_x_mode_spell(void)
296 { return ctrl_x_mode == CTRL_X_SPELL; }
297static int ctrl_x_mode_eval(void)
298 { return ctrl_x_mode == CTRL_X_EVAL; }
299int ctrl_x_mode_line_or_eval(void)
300 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100301
302/*
303 * Whether other than default completion has been selected.
304 */
305 int
306ctrl_x_mode_not_default(void)
307{
308 return ctrl_x_mode != CTRL_X_NORMAL;
309}
310
311/*
zeertzjqdca29d92021-08-31 19:12:51 +0200312 * Whether CTRL-X was typed without a following character,
313 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100314 */
315 int
316ctrl_x_mode_not_defined_yet(void)
317{
318 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
319}
320
321/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000322 * Return TRUE if currently in "normal" or "adding" insert completion matches
323 * state
324 */
325 int
326compl_status_adding(void)
327{
328 return compl_cont_status & CONT_ADDING;
329}
330
331/*
332 * Return TRUE if the completion pattern includes start of line, just for
333 * word-wise expansion.
334 */
335 int
336compl_status_sol(void)
337{
338 return compl_cont_status & CONT_SOL;
339}
340
341/*
342 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
343 */
344 int
345compl_status_local(void)
346{
347 return compl_cont_status & CONT_LOCAL;
348}
349
350/*
351 * Clear the completion status flags
352 */
353 void
354compl_status_clear(void)
355{
356 compl_cont_status = 0;
357}
358
359/*
360 * Return TRUE if completion is using the forward direction matches
361 */
362 static int
363compl_dir_forward(void)
364{
365 return compl_direction == FORWARD;
366}
367
368/*
369 * Return TRUE if currently showing forward completion matches
370 */
371 static int
372compl_shows_dir_forward(void)
373{
374 return compl_shows_dir == FORWARD;
375}
376
377/*
378 * Return TRUE if currently showing backward completion matches
379 */
380 static int
381compl_shows_dir_backward(void)
382{
383 return compl_shows_dir == BACKWARD;
384}
385
386/*
387 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 */
389 int
390has_compl_option(int dict_opt)
391{
392 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200393#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100394 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200395#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100396 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100397 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
398#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100399 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100400#endif
401 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100402 {
403 ctrl_x_mode = CTRL_X_NORMAL;
404 edit_submode = NULL;
405 msg_attr(dict_opt ? _("'dictionary' option is empty")
406 : _("'thesaurus' option is empty"),
407 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100408 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100409 {
410 vim_beep(BO_COMPL);
411 setcursor();
412 out_flush();
413#ifdef FEAT_EVAL
414 if (!get_vim_var_nr(VV_TESTING))
415#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100416 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100417 }
418 return FALSE;
419 }
420 return TRUE;
421}
422
423/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000424 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100425 * This depends on the current mode.
426 */
427 int
428vim_is_ctrl_x_key(int c)
429{
430 // Always allow ^R - let its results then be checked
431 if (c == Ctrl_R)
432 return TRUE;
433
434 // Accept <PageUp> and <PageDown> if the popup menu is visible.
435 if (ins_compl_pum_key(c))
436 return TRUE;
437
438 switch (ctrl_x_mode)
439 {
440 case 0: // Not in any CTRL-X mode
441 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
442 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200443 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100444 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
445 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
446 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
447 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
448 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200449 || c == Ctrl_S || c == Ctrl_K || c == 's'
450 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100451 case CTRL_X_SCROLL:
452 return (c == Ctrl_Y || c == Ctrl_E);
453 case CTRL_X_WHOLE_LINE:
454 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_FILES:
456 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_DICTIONARY:
458 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
459 case CTRL_X_THESAURUS:
460 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_TAGS:
462 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
463#ifdef FEAT_FIND_ID
464 case CTRL_X_PATH_PATTERNS:
465 return (c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_PATH_DEFINES:
467 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
468#endif
469 case CTRL_X_CMDLINE:
470 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
471 || c == Ctrl_X);
472#ifdef FEAT_COMPL_FUNC
473 case CTRL_X_FUNCTION:
474 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
475 case CTRL_X_OMNI:
476 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
477#endif
478 case CTRL_X_SPELL:
479 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
480 case CTRL_X_EVAL:
481 return (c == Ctrl_P || c == Ctrl_N);
482 }
483 internal_error("vim_is_ctrl_x_key()");
484 return FALSE;
485}
486
487/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000488 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000489 */
490 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000491match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000492{
493 return match->cp_flags & CP_ORIGINAL_TEXT;
494}
495
496/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000497 * Returns TRUE if "match" is the first match in the completion list.
498 */
499 static int
500is_first_match(compl_T *match)
501{
502 return match == compl_first_match;
503}
504
505/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100506 * Return TRUE when character "c" is part of the item currently being
507 * completed. Used to decide whether to abandon complete mode when the menu
508 * is visible.
509 */
510 int
511ins_compl_accept_char(int c)
512{
513 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
514 // When expanding an identifier only accept identifier chars.
515 return vim_isIDc(c);
516
517 switch (ctrl_x_mode)
518 {
519 case CTRL_X_FILES:
520 // When expanding file name only accept file name chars. But not
521 // path separators, so that "proto/<Tab>" expands files in
522 // "proto", not "proto/" as a whole
523 return vim_isfilec(c) && !vim_ispathsep(c);
524
525 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200526 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100527 case CTRL_X_OMNI:
528 // Command line and Omni completion can work with just about any
529 // printable character, but do stop at white space.
530 return vim_isprintc(c) && !VIM_ISWHITE(c);
531
532 case CTRL_X_WHOLE_LINE:
533 // For while line completion a space can be part of the line.
534 return vim_isprintc(c);
535 }
536 return vim_iswordc(c);
537}
538
539/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000540 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100541 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000542 */
543 static char_u *
544ins_compl_infercase_gettext(
545 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100546 int char_len,
547 int compl_char_len,
548 int min_len,
549 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000550{
551 int *wca; // Wide character array.
552 char_u *p;
553 int i, c;
554 int has_lower = FALSE;
555 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100556 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000557
558 IObuff[0] = NUL;
559
560 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100561 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000562 if (wca == NULL)
563 return IObuff;
564
565 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100566 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100567 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000568 if (has_mbyte)
569 wca[i] = mb_ptr2char_adv(&p);
570 else
571 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100572 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000573
574 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100575 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000576 for (i = 0; i < min_len; ++i)
577 {
glepnir6e199932024-12-14 21:13:27 +0100578 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000579 if (MB_ISLOWER(c))
580 {
581 has_lower = TRUE;
582 if (MB_ISUPPER(wca[i]))
583 {
584 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100585 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000586 wca[i] = MB_TOLOWER(wca[i]);
587 break;
588 }
589 }
590 }
591
592 // Rule 2: No lower case, 2nd consecutive letter converted to
593 // upper case.
594 if (!has_lower)
595 {
John Marriott5e6ea922024-11-23 14:01:57 +0100596 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000597 for (i = 0; i < min_len; ++i)
598 {
glepnir6e199932024-12-14 21:13:27 +0100599 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000600 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
601 {
602 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100603 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000604 wca[i] = MB_TOUPPER(wca[i]);
605 break;
606 }
607 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
608 }
609 }
610
611 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100612 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000613 for (i = 0; i < min_len; ++i)
614 {
glepnir6e199932024-12-14 21:13:27 +0100615 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000616 if (MB_ISLOWER(c))
617 wca[i] = MB_TOLOWER(wca[i]);
618 else if (MB_ISUPPER(c))
619 wca[i] = MB_TOUPPER(wca[i]);
620 }
621
622 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000623 p = IObuff;
624 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100625 ga_init2(&gap, 1, 500);
626 while (i < char_len)
627 {
628 if (gap.ga_data != NULL)
629 {
630 if (ga_grow(&gap, 10) == FAIL)
631 {
632 ga_clear(&gap);
633 return (char_u *)"[failed]";
634 }
635 p = (char_u *)gap.ga_data + gap.ga_len;
636 if (has_mbyte)
637 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
638 else
639 {
640 *p = wca[i++];
641 ++gap.ga_len;
642 }
643 }
644 else if ((p - IObuff) + 6 >= IOSIZE)
645 {
646 // Multi-byte characters can occupy up to five bytes more than
647 // ASCII characters, and we also need one byte for NUL, so when
648 // getting to six bytes from the edge of IObuff switch to using a
649 // growarray. Add the character in the next round.
650 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100651 {
652 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100653 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100654 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100655 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100656 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100657 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100658 }
659 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000660 p += (*mb_char2bytes)(wca[i++], p);
661 else
662 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000664 vim_free(wca);
665
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100666 if (gap.ga_data != NULL)
667 {
668 *tofree = gap.ga_data;
669 return gap.ga_data;
670 }
671
672 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000673 return IObuff;
674}
675
676/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100677 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
678 * case of the originally typed text is used, and the case of the completed
679 * text is inferred, ie this tries to work out what case you probably wanted
680 * the rest of the word to be in -- webb
681 */
682 int
683ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200684 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100685 int len,
686 int icase,
687 char_u *fname,
688 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200689 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100690{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200691 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100693 int char_len; // count multi-byte characters
694 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200696 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100697 int res;
698 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100699
700 if (p_ic && curbuf->b_p_inf && len > 0)
701 {
702 // Infer case of completed part.
703
704 // Find actual length of completion.
705 if (has_mbyte)
706 {
707 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100708 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709 while (*p != NUL)
710 {
711 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100712 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100713 }
714 }
715 else
glepnir6e199932024-12-14 21:13:27 +0100716 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100717 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100718 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719
720 // Find actual length of original text.
721 if (has_mbyte)
722 {
John Marriott5e6ea922024-11-23 14:01:57 +0100723 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 while (*p != NUL)
726 {
727 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100728 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729 }
730 }
731 else
glepnir6e199932024-12-14 21:13:27 +0100732 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100734 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100738 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100740 str = ins_compl_infercase_gettext(str, char_len,
741 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100742 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200743 if (cont_s_ipos)
744 flags |= CP_CONT_S_IPOS;
745 if (icase)
746 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200747
glepnir5c66e232024-11-15 19:58:27 +0100748 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100749 vim_free(tofree);
750 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100751}
752
753/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000754 * Add a match to the list of matches. The arguments are:
755 * str - text of the match to add
756 * len - length of "str". If -1, then the length of "str" is
757 * computed.
758 * fname - file name to associate with this match.
759 * cptext - list of strings to use with this match (for abbr, menu, info
760 * and kind)
761 * user_data - user supplied data (any vim type) for this match
762 * cdir - match direction. If 0, use "compl_direction".
763 * flags_arg - match flags (cp_flags)
764 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100765 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000766 * If "cdir" is FORWARD, then the match is added after the current match.
767 * Otherwise, it is added before the current match.
768 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100769 * If the given string is already in the list of completions, then return
770 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
771 * maybe because alloc() returns NULL, then FAIL is returned.
772 */
773 static int
774ins_compl_add(
775 char_u *str,
776 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 char_u **cptext, // extra text for popup menu or NULL
779 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200781 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200782 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100783 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784{
785 compl_T *match;
786 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200787 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788
Bram Moolenaarceb06192021-04-04 15:05:22 +0200789 if (flags & CP_FAST)
790 fast_breakcheck();
791 else
792 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100793 if (got_int)
794 return FAIL;
795 if (len < 0)
796 len = (int)STRLEN(str);
797
798 // If the same match is already present, don't add it.
799 if (compl_first_match != NULL && !adup)
800 {
801 match = compl_first_match;
802 do
803 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000804 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100805 && STRNCMP(match->cp_str.string, str, len) == 0
806 && ((int)match->cp_str.length <= len
807 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 return NOTDONE;
809 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000810 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100811 }
812
813 // Remove any popup menu before changing the list of matches.
814 ins_compl_del_pum();
815
816 // Allocate a new match structure.
817 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200818 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 if (match == NULL)
820 return FAIL;
glepnir40891ba2025-02-10 22:18:00 +0100821 match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
John Marriott5e6ea922024-11-23 14:01:57 +0100822 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 {
824 vim_free(match);
825 return FAIL;
826 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100827
John Marriott5e6ea922024-11-23 14:01:57 +0100828 match->cp_str.length = len;
829
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100830 // match-fname is:
831 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200832 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833 // - NULL otherwise. --Acevedo
834 if (fname != NULL
835 && compl_curr_match != NULL
836 && compl_curr_match->cp_fname != NULL
837 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
838 match->cp_fname = compl_curr_match->cp_fname;
839 else if (fname != NULL)
840 {
841 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200842 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 }
844 else
845 match->cp_fname = NULL;
846 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100847 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
848 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849
850 if (cptext != NULL)
851 {
852 int i;
853
854 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100855 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856 if (cptext[i] != NULL && *cptext[i] != NUL)
857 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100858 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100860#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100861 if (user_data != NULL)
862 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100863#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100864
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000865 // Link the new match structure after (FORWARD) or before (BACKWARD) the
866 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100867 if (compl_first_match == NULL)
868 match->cp_next = match->cp_prev = NULL;
869 else if (dir == FORWARD)
870 {
871 match->cp_next = compl_curr_match->cp_next;
872 match->cp_prev = compl_curr_match;
873 }
874 else // BACKWARD
875 {
876 match->cp_next = compl_curr_match;
877 match->cp_prev = compl_curr_match->cp_prev;
878 }
879 if (match->cp_next)
880 match->cp_next->cp_prev = match;
881 if (match->cp_prev)
882 match->cp_prev->cp_next = match;
883 else // if there's nothing before, it is the first match
884 compl_first_match = match;
885 compl_curr_match = match;
886
887 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200888 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 ins_compl_longest_match(match);
890
891 return OK;
892}
893
894/*
895 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200896 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100897 */
898 static int
899ins_compl_equal(compl_T *match, char_u *str, int len)
900{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200901 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200902 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200903 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100904 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
905 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100906}
907
908/*
glepnir6a38aff2024-12-16 21:56:16 +0100909 * when len is -1 mean use whole length of p otherwise part of p
910 */
911 static void
912ins_compl_insert_bytes(char_u *p, int len)
913{
914 if (len == -1)
915 len = (int)STRLEN(p);
916 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100917 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100918}
919
920/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100921 * Checks if the column is within the currently inserted completion text
922 * column range. If it is, it returns a special highlight attribute.
glepnir76bdb822025-02-08 19:04:51 +0100923 * -1 means normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100924 */
925 int
glepnir76bdb822025-02-08 19:04:51 +0100926ins_compl_col_range_attr(linenr_T lnum, int col)
glepnir6a38aff2024-12-16 21:56:16 +0100927{
glepnir76bdb822025-02-08 19:04:51 +0100928 int start_col;
929 int attr;
930
931 if ((get_cot_flags() & COT_FUZZY)
932 || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0)
glepnire8908872025-01-08 18:30:45 +0100933 return -1;
934
glepnir76bdb822025-02-08 19:04:51 +0100935 start_col = compl_col + (int)ins_compl_leader_len();
936 if (!ins_compl_has_multiple())
937 return (col >= start_col && col < compl_ins_end_col) ? attr : -1;
938
939 // Multiple lines
940 if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) ||
941 (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) ||
942 (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col))
943 return attr;
glepnir6a38aff2024-12-16 21:56:16 +0100944
945 return -1;
946}
947
948/*
glepnir76bdb822025-02-08 19:04:51 +0100949 * Returns TRUE if the current completion string contains newline characters,
950 * indicating it's a multi-line completion.
951 */
952 static int
953ins_compl_has_multiple(void)
954{
955 return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL;
956}
957
958/*
959 * Returns TRUE if the given line number falls within the range of a multi-line
960 * completion, i.e. between the starting line (compl_lnum) and current cursor
961 * line. Always returns FALSE for single-line completions.
962 */
963 int
964ins_compl_lnum_in_range(linenr_T lnum)
965{
966 if (!ins_compl_has_multiple())
967 return FALSE;
968 return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum;
969}
970
971/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100972 * Reduce the longest common string for match "match".
973 */
974 static void
975ins_compl_longest_match(compl_T *match)
976{
977 char_u *p, *s;
978 int c1, c2;
979 int had_match;
980
John Marriott5e6ea922024-11-23 14:01:57 +0100981 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100982 {
983 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100984 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
985 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000986 return;
987
John Marriott5e6ea922024-11-23 14:01:57 +0100988 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000989 had_match = (curwin->w_cursor.col > compl_col);
990 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100991 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000992 ins_redraw(FALSE);
993
994 // When the match isn't there (to avoid matching itself) remove it
995 // again after redrawing.
996 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100997 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100998 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000999
1000 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001001 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001002
1003 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +01001004 p = compl_leader.string;
1005 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001006 while (*p != NUL)
1007 {
1008 if (has_mbyte)
1009 {
1010 c1 = mb_ptr2char(p);
1011 c2 = mb_ptr2char(s);
1012 }
1013 else
1014 {
1015 c1 = *p;
1016 c2 = *s;
1017 }
1018 if ((match->cp_flags & CP_ICASE)
1019 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
1020 break;
1021 if (has_mbyte)
1022 {
1023 MB_PTR_ADV(p);
1024 MB_PTR_ADV(s);
1025 }
1026 else
1027 {
1028 ++p;
1029 ++s;
1030 }
1031 }
1032
1033 if (*p != NUL)
1034 {
1035 // Leader was shortened, need to change the inserted text.
1036 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001037 compl_leader.length = (size_t)(p - compl_leader.string);
1038
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001039 had_match = (curwin->w_cursor.col > compl_col);
1040 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001041 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001042 ins_redraw(FALSE);
1043
1044 // When the match isn't there (to avoid matching itself) remove it
1045 // again after redrawing.
1046 if (!had_match)
1047 ins_compl_delete();
1048 }
1049
1050 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001051}
1052
1053/*
1054 * Add an array of matches to the list of matches.
1055 * Frees matches[].
1056 */
1057 static void
1058ins_compl_add_matches(
1059 int num_matches,
1060 char_u **matches,
1061 int icase)
1062{
1063 int i;
1064 int add_r = OK;
1065 int dir = compl_direction;
1066
1067 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001068 {
1069 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1070 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1071 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001072 // if dir was BACKWARD then honor it just once
1073 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001074 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001075 FreeWild(num_matches, matches);
1076}
1077
1078/*
1079 * Make the completion list cyclic.
1080 * Return the number of matches (excluding the original).
1081 */
1082 static int
1083ins_compl_make_cyclic(void)
1084{
1085 compl_T *match;
1086 int count = 0;
1087
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001088 if (compl_first_match == NULL)
1089 return 0;
1090
1091 // Find the end of the list.
1092 match = compl_first_match;
1093 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001094 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001095 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001096 match = match->cp_next;
1097 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001098 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001099 match->cp_next = compl_first_match;
1100 compl_first_match->cp_prev = match;
1101
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001102 return count;
1103}
1104
1105/*
1106 * Return whether there currently is a shown match.
1107 */
1108 int
1109ins_compl_has_shown_match(void)
1110{
1111 return compl_shown_match == NULL
1112 || compl_shown_match != compl_shown_match->cp_next;
1113}
1114
1115/*
1116 * Return whether the shown match is long enough.
1117 */
1118 int
1119ins_compl_long_shown_match(void)
1120{
John Marriott5e6ea922024-11-23 14:01:57 +01001121 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001122 > curwin->w_cursor.col - compl_col;
1123}
1124
1125/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001126 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001127 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001128 unsigned int
1129get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001130{
zeertzjq529b9ad2024-06-05 20:27:06 +02001131 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001132}
1133
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001134/*
1135 * Update the screen and when there is any scrolling remove the popup menu.
1136 */
1137 static void
1138ins_compl_upd_pum(void)
1139{
1140 int h;
1141
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001142 if (compl_match_array == NULL)
1143 return;
1144
1145 h = curwin->w_cline_height;
1146 // Update the screen later, before drawing the popup menu over it.
1147 pum_call_update_screen();
1148 if (h != curwin->w_cline_height)
1149 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001150}
1151
1152/*
1153 * Remove any popup menu.
1154 */
1155 static void
1156ins_compl_del_pum(void)
1157{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001158 if (compl_match_array == NULL)
1159 return;
1160
1161 pum_undisplay();
1162 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163}
1164
1165/*
1166 * Return TRUE if the popup menu should be displayed.
1167 */
1168 int
1169pum_wanted(void)
1170{
1171 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001172 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001173 return FALSE;
1174
1175 // The display looks bad on a B&W display.
1176 if (t_colors < 8
1177#ifdef FEAT_GUI
1178 && !gui.in_use
1179#endif
1180 )
1181 return FALSE;
1182 return TRUE;
1183}
1184
1185/*
1186 * Return TRUE if there are two or more matches to be shown in the popup menu.
1187 * One if 'completopt' contains "menuone".
1188 */
1189 static int
1190pum_enough_matches(void)
1191{
1192 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001193 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001194
1195 // Don't display the popup menu if there are no matches or there is only
1196 // one (ignoring the original text).
1197 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001198 do
1199 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001200 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001201 break;
1202 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001203 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001204
zeertzjq529b9ad2024-06-05 20:27:06 +02001205 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001206 return (i >= 1);
1207 return (i >= 2);
1208}
1209
Bram Moolenaar3075a452021-11-17 15:51:52 +00001210#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001211/*
1212 * Allocate Dict for the completed item.
1213 * { word, abbr, menu, kind, info }
1214 */
1215 static dict_T *
1216ins_compl_dict_alloc(compl_T *match)
1217{
1218 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1219
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001220 if (dict == NULL)
1221 return NULL;
1222
John Marriott5e6ea922024-11-23 14:01:57 +01001223 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001224 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1225 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1226 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1227 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1228 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1229 dict_add_string(dict, "user_data", (char_u *)"");
1230 else
1231 dict_add_tv(dict, "user_data", &match->cp_user_data);
1232
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001233 return dict;
1234}
1235
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001236/*
1237 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1238 * completion menu is changed.
1239 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001240 static void
1241trigger_complete_changed_event(int cur)
1242{
1243 dict_T *v_event;
1244 dict_T *item;
1245 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001246 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001247
1248 if (recursive)
1249 return;
1250
glepnir40891ba2025-02-10 22:18:00 +01001251 item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001252 if (item == NULL)
1253 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001254 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001255 dict_add_dict(v_event, "completed_item", item);
1256 pum_set_event_info(v_event);
1257 dict_set_items_ro(v_event);
1258
1259 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001260 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001261 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001262 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001263 recursive = FALSE;
1264
Bram Moolenaar3075a452021-11-17 15:51:52 +00001265 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001266}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001267#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001268
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001269/*
glepnira218cc62024-06-03 19:32:39 +02001270 * pumitem qsort compare func
1271 */
1272 static int
zeertzjq8e567472024-06-14 20:04:42 +02001273ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001274{
1275 const int sa = (*(pumitem_T *)a).pum_score;
1276 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001277 const int ia = (*(pumitem_T *)a).pum_idx;
1278 const int ib = (*(pumitem_T *)b).pum_idx;
1279 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001280}
1281
1282/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001283 * Build a popup menu to show the completion matches.
1284 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1285 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001286 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001287 static int
1288ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001289{
1290 compl_T *compl;
1291 compl_T *shown_compl = NULL;
1292 int did_find_shown_match = FALSE;
1293 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001294 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001295 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001296 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001297 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001298 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001299 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1300 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnir80b66202024-11-27 21:53:53 +01001301 compl_T *match_head = NULL;
1302 compl_T *match_tail = NULL;
1303 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001304
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001305 // Need to build the popup menu list.
1306 compl_match_arraysize = 0;
1307 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001308
glepnira49c0772024-11-30 10:56:30 +01001309 // If the current match is the original text don't find the first
1310 // match after it, don't highlight anything.
1311 if (match_at_original_text(compl_shown_match))
1312 shown_match_ok = TRUE;
1313
1314 if (compl_leader.string != NULL
1315 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1316 && shown_match_ok == FALSE)
1317 compl_shown_match = compl_no_select ? compl_first_match
1318 : compl_first_match->cp_next;
1319
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001320 do
1321 {
glepnird4088ed2024-12-31 10:55:22 +01001322 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001323 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1324 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001325 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001326 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001327
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001328 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001329 && (compl_leader.string == NULL
1330 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001331 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001332 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001333 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001334 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001335 if (match_head == NULL)
1336 match_head = compl;
1337 else
glepnira49c0772024-11-30 10:56:30 +01001338 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001339 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001340
glepnirf400a0c2025-01-23 19:55:14 +01001341 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001342 {
1343 if (compl == compl_shown_match || did_find_shown_match)
1344 {
1345 // This item is the shown match or this is the
1346 // first displayed item after the shown match.
1347 compl_shown_match = compl;
1348 did_find_shown_match = TRUE;
1349 shown_match_ok = TRUE;
1350 }
1351 else
1352 // Remember this displayed match for when the
1353 // shown match is just below it.
1354 shown_compl = compl;
1355 cur = i;
1356 }
glepnirf400a0c2025-01-23 19:55:14 +01001357 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001358 {
1359 if (i == 0)
1360 shown_compl = compl;
1361 // Update the maximum fuzzy score and the shown match
1362 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001363 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001364 {
1365 did_find_shown_match = TRUE;
1366 max_fuzzy_score = compl->cp_score;
1367 if (!compl_no_select)
1368 compl_shown_match = compl;
1369 }
1370
1371 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1372 {
1373 cur = i;
1374 shown_match_ok = TRUE;
1375 }
1376 }
1377 i++;
glepnir80b66202024-11-27 21:53:53 +01001378 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001379
glepnirf400a0c2025-01-23 19:55:14 +01001380 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001381 {
1382 did_find_shown_match = TRUE;
1383
1384 // When the original text is the shown match don't set
1385 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001386 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001387 shown_match_ok = TRUE;
1388
1389 if (!shown_match_ok && shown_compl != NULL)
1390 {
1391 // The shown match isn't displayed, set it to the
1392 // previously displayed match.
1393 compl_shown_match = shown_compl;
1394 shown_match_ok = TRUE;
1395 }
1396 }
glepnira49c0772024-11-30 10:56:30 +01001397 compl = compl->cp_next;
1398 } while (compl != NULL && !is_first_match(compl));
1399
1400 if (compl_match_arraysize == 0)
1401 return -1;
1402
glepnirc0b7ca42025-02-13 20:27:44 +01001403 if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok)
1404 {
1405 compl_shown_match = shown_compl;
1406 shown_match_ok = TRUE;
1407 cur = 0;
1408 }
1409
glepnira49c0772024-11-30 10:56:30 +01001410 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1411 if (compl_match_array == NULL)
1412 return -1;
1413
1414 compl = match_head;
1415 i = 0;
1416 while (compl != NULL)
1417 {
glepnir6e199932024-12-14 21:13:27 +01001418 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1419 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001420 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1421 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1422 compl_match_array[i].pum_score = compl->cp_score;
1423 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1424 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001425 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1426 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001427 match_next = compl->cp_match_next;
1428 compl->cp_match_next = NULL;
1429 compl = match_next;
1430 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001431
zeertzjqd65aa1b2025-01-25 15:29:03 +01001432 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001433 {
1434 for (i = 0; i < compl_match_arraysize; i++)
1435 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001436 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001437 qsort(compl_match_array, (size_t)compl_match_arraysize,
1438 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001439 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001440 }
glepnira218cc62024-06-03 19:32:39 +02001441
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001442 if (!shown_match_ok) // no displayed match at all
1443 cur = -1;
1444
1445 return cur;
1446}
1447
1448/*
1449 * Show the popup menu for the list of matches.
1450 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1451 */
1452 void
1453ins_compl_show_pum(void)
1454{
1455 int i;
1456 int cur = -1;
1457 colnr_T col;
1458
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001459 if (!pum_wanted() || !pum_enough_matches())
1460 return;
1461
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001462 // Update the screen later, before drawing the popup menu over it.
1463 pum_call_update_screen();
1464
1465 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001466 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001467 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001468 else
1469 {
1470 // popup menu already exists, only need to find the current item.
1471 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001472 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001473 || compl_match_array[i].pum_text
1474 == compl_shown_match->cp_text[CPT_ABBR])
1475 {
1476 cur = i;
1477 break;
1478 }
1479 }
1480
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001481 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001482 {
1483#ifdef FEAT_EVAL
1484 if (compl_started && has_completechanged())
1485 trigger_complete_changed_event(cur);
1486#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001487 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001488 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001489
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001490 // In Replace mode when a $ is displayed at the end of the line only
1491 // part of the screen would be updated. We do need to redraw here.
1492 dollar_vcol = -1;
1493
1494 // Compute the screen column of the start of the completed text.
1495 // Use the cursor to get all wrapping and other settings right.
1496 col = curwin->w_cursor.col;
1497 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001498 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001499 pum_display(compl_match_array, compl_match_arraysize, cur);
1500 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001501
glepnircbb46b42024-02-03 18:11:13 +01001502 // After adding leader, set the current match to shown match.
1503 if (compl_started && compl_curr_match != compl_shown_match)
1504 compl_curr_match = compl_shown_match;
1505
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001506#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001507 if (has_completechanged())
1508 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001509#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001510}
1511
1512#define DICT_FIRST (1) // use just first element in "dict"
1513#define DICT_EXACT (2) // "dict" is the exact name of a file
1514
1515/*
glepnir40c1c332024-06-11 19:37:04 +02001516 * Get current completion leader
1517 */
1518 char_u *
1519ins_compl_leader(void)
1520{
John Marriott5e6ea922024-11-23 14:01:57 +01001521 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1522}
1523
1524/*
1525 * Get current completion leader length
1526 */
1527 size_t
1528ins_compl_leader_len(void)
1529{
1530 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001531}
1532
1533/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001534 * Add any identifiers that match the given pattern "pat" in the list of
1535 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001536 */
1537 static void
1538ins_compl_dictionaries(
1539 char_u *dict_start,
1540 char_u *pat,
1541 int flags, // DICT_FIRST and/or DICT_EXACT
1542 int thesaurus) // Thesaurus completion
1543{
1544 char_u *dict = dict_start;
1545 char_u *ptr;
1546 char_u *buf;
1547 regmatch_T regmatch;
1548 char_u **files;
1549 int count;
1550 int save_p_scs;
1551 int dir = compl_direction;
1552
1553 if (*dict == NUL)
1554 {
1555#ifdef FEAT_SPELL
1556 // When 'dictionary' is empty and spell checking is enabled use
1557 // "spell".
1558 if (!thesaurus && curwin->w_p_spell)
1559 dict = (char_u *)"spell";
1560 else
1561#endif
1562 return;
1563 }
1564
1565 buf = alloc(LSIZE);
1566 if (buf == NULL)
1567 return;
1568 regmatch.regprog = NULL; // so that we can goto theend
1569
1570 // If 'infercase' is set, don't use 'smartcase' here
1571 save_p_scs = p_scs;
1572 if (curbuf->b_p_inf)
1573 p_scs = FALSE;
1574
1575 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1576 // to only match at the start of a line. Otherwise just match the
1577 // pattern. Also need to double backslashes.
1578 if (ctrl_x_mode_line_or_eval())
1579 {
1580 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1581 size_t len;
1582
1583 if (pat_esc == NULL)
1584 goto theend;
1585 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001586 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001587 if (ptr == NULL)
1588 {
1589 vim_free(pat_esc);
1590 goto theend;
1591 }
1592 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1593 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1594 vim_free(pat_esc);
1595 vim_free(ptr);
1596 }
1597 else
1598 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001599 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001600 if (regmatch.regprog == NULL)
1601 goto theend;
1602 }
1603
1604 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1605 regmatch.rm_ic = ignorecase(pat);
1606 while (*dict != NUL && !got_int && !compl_interrupted)
1607 {
1608 // copy one dictionary file name into buf
1609 if (flags == DICT_EXACT)
1610 {
1611 count = 1;
1612 files = &dict;
1613 }
1614 else
1615 {
1616 // Expand wildcards in the dictionary name, but do not allow
1617 // backticks (for security, the 'dict' option may have been set in
1618 // a modeline).
1619 copy_option_part(&dict, buf, LSIZE, ",");
1620# ifdef FEAT_SPELL
1621 if (!thesaurus && STRCMP(buf, "spell") == 0)
1622 count = -1;
1623 else
1624# endif
1625 if (vim_strchr(buf, '`') != NULL
1626 || expand_wildcards(1, &buf, &count, &files,
1627 EW_FILE|EW_SILENT) != OK)
1628 count = 0;
1629 }
1630
1631# ifdef FEAT_SPELL
1632 if (count == -1)
1633 {
1634 // Complete from active spelling. Skip "\<" in the pattern, we
1635 // don't use it as a RE.
1636 if (pat[0] == '\\' && pat[1] == '<')
1637 ptr = pat + 2;
1638 else
1639 ptr = pat;
1640 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1641 }
1642 else
1643# endif
1644 if (count > 0) // avoid warning for using "files" uninit
1645 {
1646 ins_compl_files(count, files, thesaurus, flags,
1647 &regmatch, buf, &dir);
1648 if (flags != DICT_EXACT)
1649 FreeWild(count, files);
1650 }
1651 if (flags != 0)
1652 break;
1653 }
1654
1655theend:
1656 p_scs = save_p_scs;
1657 vim_regfree(regmatch.regprog);
1658 vim_free(buf);
1659}
1660
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001661/*
1662 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1663 * skipping the word at 'skip_word'. Returns OK on success.
1664 */
1665 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001666thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001667 char_u *fname,
1668 char_u **buf_arg,
1669 int dir,
1670 char_u *skip_word)
1671{
1672 int status = OK;
1673 char_u *ptr;
1674 char_u *wstart;
1675
1676 // Add the other matches on the line
1677 ptr = *buf_arg;
1678 while (!got_int)
1679 {
1680 // Find start of the next word. Skip white
1681 // space and punctuation.
1682 ptr = find_word_start(ptr);
1683 if (*ptr == NUL || *ptr == NL)
1684 break;
1685 wstart = ptr;
1686
1687 // Find end of the word.
1688 if (has_mbyte)
1689 // Japanese words may have characters in
1690 // different classes, only separate words
1691 // with single-byte non-word characters.
1692 while (*ptr != NUL)
1693 {
1694 int l = (*mb_ptr2len)(ptr);
1695
1696 if (l < 2 && !vim_iswordc(*ptr))
1697 break;
1698 ptr += l;
1699 }
1700 else
1701 ptr = find_word_end(ptr);
1702
1703 // Add the word. Skip the regexp match.
1704 if (wstart != skip_word)
1705 {
1706 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1707 fname, dir, FALSE);
1708 if (status == FAIL)
1709 break;
1710 }
1711 }
1712
1713 *buf_arg = ptr;
1714 return status;
1715}
1716
1717/*
1718 * Process "count" dictionary/thesaurus "files" and add the text matching
1719 * "regmatch".
1720 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001721 static void
1722ins_compl_files(
1723 int count,
1724 char_u **files,
1725 int thesaurus,
1726 int flags,
1727 regmatch_T *regmatch,
1728 char_u *buf,
1729 int *dir)
1730{
1731 char_u *ptr;
1732 int i;
1733 FILE *fp;
1734 int add_r;
1735
1736 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1737 {
1738 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001739 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001740 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001741 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001742 vim_snprintf((char *)IObuff, IOSIZE,
1743 _("Scanning dictionary: %s"), (char *)files[i]);
1744 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1745 }
1746
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001747 if (fp == NULL)
1748 continue;
1749
1750 // Read dictionary file line by line.
1751 // Check each line for a match.
1752 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001753 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001754 ptr = buf;
1755 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001756 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001757 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001758 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1759 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001760 add_r = ins_compl_add_infercase(regmatch->startp[0],
1761 (int)(ptr - regmatch->startp[0]),
1762 p_ic, files[i], *dir, FALSE);
1763 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001764 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001765 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001766 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001767 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001768 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001769 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001770 if (add_r == OK)
1771 // if dir was BACKWARD then honor it just once
1772 *dir = FORWARD;
1773 else if (add_r == FAIL)
1774 break;
1775 // avoid expensive call to vim_regexec() when at end
1776 // of line
1777 if (*ptr == '\n' || got_int)
1778 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001779 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001780 line_breakcheck();
1781 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001782 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001783 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001784 }
1785}
1786
1787/*
1788 * Find the start of the next word.
1789 * Returns a pointer to the first char of the word. Also stops at a NUL.
1790 */
1791 char_u *
1792find_word_start(char_u *ptr)
1793{
1794 if (has_mbyte)
1795 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1796 ptr += (*mb_ptr2len)(ptr);
1797 else
1798 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1799 ++ptr;
1800 return ptr;
1801}
1802
1803/*
1804 * Find the end of the word. Assumes it starts inside a word.
1805 * Returns a pointer to just after the word.
1806 */
1807 char_u *
1808find_word_end(char_u *ptr)
1809{
1810 int start_class;
1811
1812 if (has_mbyte)
1813 {
1814 start_class = mb_get_class(ptr);
1815 if (start_class > 1)
1816 while (*ptr != NUL)
1817 {
1818 ptr += (*mb_ptr2len)(ptr);
1819 if (mb_get_class(ptr) != start_class)
1820 break;
1821 }
1822 }
1823 else
1824 while (vim_iswordc(*ptr))
1825 ++ptr;
1826 return ptr;
1827}
1828
1829/*
1830 * Find the end of the line, omitting CR and NL at the end.
1831 * Returns a pointer to just after the line.
1832 */
1833 static char_u *
1834find_line_end(char_u *ptr)
1835{
1836 char_u *s;
1837
1838 s = ptr + STRLEN(ptr);
1839 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1840 --s;
1841 return s;
1842}
1843
1844/*
1845 * Free the list of completions
1846 */
1847 static void
1848ins_compl_free(void)
1849{
1850 compl_T *match;
1851 int i;
1852
John Marriott5e6ea922024-11-23 14:01:57 +01001853 VIM_CLEAR_STRING(compl_pattern);
1854 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001855
1856 if (compl_first_match == NULL)
1857 return;
1858
1859 ins_compl_del_pum();
1860 pum_clear();
1861
1862 compl_curr_match = compl_first_match;
1863 do
1864 {
1865 match = compl_curr_match;
1866 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001867 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001868 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001869 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001870 vim_free(match->cp_fname);
1871 for (i = 0; i < CPT_COUNT; ++i)
1872 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001873#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001874 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001875#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001876 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001877 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001878 compl_first_match = compl_curr_match = NULL;
1879 compl_shown_match = NULL;
1880 compl_old_match = NULL;
1881}
1882
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001883/*
1884 * Reset/clear the completion state.
1885 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001886 void
1887ins_compl_clear(void)
1888{
1889 compl_cont_status = 0;
1890 compl_started = FALSE;
1891 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001892 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001893 VIM_CLEAR_STRING(compl_pattern);
1894 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001895 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001896 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001897 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001898#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001899 // clear v:completed_item
1900 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001901#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001902}
1903
1904/*
1905 * Return TRUE when Insert completion is active.
1906 */
1907 int
1908ins_compl_active(void)
1909{
1910 return compl_started;
1911}
1912
1913/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001914 * Return True when wp is the actual completion window
1915 */
1916 int
1917ins_compl_win_active(win_T *wp UNUSED)
1918{
1919 return ins_compl_active()
1920#if defined(FEAT_QUICKFIX)
1921 && (!wp->w_p_pvw
1922# ifdef FEAT_PROP_POPUP
1923 && !(wp->w_popup_flags & POPF_INFO)
1924# endif
1925 )
1926#endif
1927 ;
1928}
1929
1930/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001931 * Selected one of the matches. When FALSE the match was edited or using the
1932 * longest common string.
1933 */
1934 int
1935ins_compl_used_match(void)
1936{
1937 return compl_used_match;
1938}
1939
1940/*
1941 * Initialize get longest common string.
1942 */
1943 void
1944ins_compl_init_get_longest(void)
1945{
1946 compl_get_longest = FALSE;
1947}
1948
1949/*
1950 * Returns TRUE when insert completion is interrupted.
1951 */
1952 int
1953ins_compl_interrupted(void)
1954{
1955 return compl_interrupted;
1956}
1957
1958/*
1959 * Returns TRUE if the <Enter> key selects a match in the completion popup
1960 * menu.
1961 */
1962 int
1963ins_compl_enter_selects(void)
1964{
1965 return compl_enter_selects;
1966}
1967
1968/*
1969 * Return the column where the text starts that is being completed
1970 */
1971 colnr_T
1972ins_compl_col(void)
1973{
1974 return compl_col;
1975}
1976
1977/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001978 * Return the length in bytes of the text being completed
1979 */
1980 int
1981ins_compl_len(void)
1982{
1983 return compl_length;
1984}
1985
1986/*
glepniredd4ac32025-01-29 18:53:51 +01001987 * Return TRUE when preinsert is set otherwise FALSE.
1988 */
1989 static int
1990ins_compl_has_preinsert(void)
1991{
1992 return (get_cot_flags() & (COT_PREINSERT | COT_FUZZY)) == COT_PREINSERT;
1993}
1994
1995/*
1996 * Returns TRUE if the pre-insert effect is valid and the cursor is within
1997 * the `compl_ins_end_col` range.
1998 */
1999 int
2000ins_compl_preinsert_effect(void)
2001{
2002 if (!ins_compl_has_preinsert())
2003 return FALSE;
2004
2005 return curwin->w_cursor.col < compl_ins_end_col;
2006}
2007
2008/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002009 * Delete one character before the cursor and show the subset of the matches
2010 * that match the word that is now before the cursor.
2011 * Returns the character to be used, NUL if the work is done and another char
2012 * to be got from the user.
2013 */
2014 int
2015ins_compl_bs(void)
2016{
2017 char_u *line;
2018 char_u *p;
2019
glepniredd4ac32025-01-29 18:53:51 +01002020 if (ins_compl_preinsert_effect())
2021 ins_compl_delete();
2022
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 line = ml_get_curline();
2024 p = line + curwin->w_cursor.col;
2025 MB_PTR_BACK(line, p);
2026
2027 // Stop completion when the whole word was deleted. For Omni completion
2028 // allow the word to be deleted, we won't match everything.
2029 // Respect the 'backspace' option.
2030 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002031 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2032 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002033 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2034 - compl_length < 0))
2035 return K_BS;
2036
2037 // Deleted more than what was used to find matches or didn't finish
2038 // finding all matches: need to look for matches all over again.
2039 if (curwin->w_cursor.col <= compl_col + compl_length
2040 || ins_compl_need_restart())
2041 ins_compl_restart();
2042
John Marriott5e6ea922024-11-23 14:01:57 +01002043 VIM_CLEAR_STRING(compl_leader);
2044 compl_leader.length = (size_t)((p - line) - compl_col);
2045 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2046 if (compl_leader.string == NULL)
2047 {
2048 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002049 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002050 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002051
2052 ins_compl_new_leader();
2053 if (compl_shown_match != NULL)
2054 // Make sure current match is not a hidden item.
2055 compl_curr_match = compl_shown_match;
2056 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002057}
2058
2059/*
2060 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2061 * be called.
2062 */
2063 static int
2064ins_compl_need_restart(void)
2065{
2066 // Return TRUE if we didn't complete finding matches or when the
2067 // 'completefunc' returned "always" in the "refresh" dictionary item.
2068 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002069 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002070 && compl_opt_refresh_always);
2071}
2072
2073/*
2074 * Called after changing "compl_leader".
2075 * Show the popup menu with a different set of matches.
2076 * May also search for matches again if the previous search was interrupted.
2077 */
2078 static void
2079ins_compl_new_leader(void)
2080{
2081 ins_compl_del_pum();
2082 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002083 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002084 compl_used_match = FALSE;
2085
2086 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002087 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002088 else
2089 {
2090#ifdef FEAT_SPELL
2091 spell_bad_len = 0; // need to redetect bad word
2092#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002093 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002094 // the popup menu display the changed text before the cursor. Set
2095 // "compl_restarting" to avoid that the first match is inserted.
2096 pum_call_update_screen();
2097#ifdef FEAT_GUI
2098 if (gui.in_use)
2099 {
2100 // Show the cursor after the match, not after the redrawn text.
2101 setcursor();
2102 out_flush_cursor(FALSE, FALSE);
2103 }
2104#endif
2105 compl_restarting = TRUE;
2106 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2107 compl_cont_status = 0;
2108 compl_restarting = FALSE;
2109 }
2110
2111 compl_enter_selects = !compl_used_match;
2112
2113 // Show the popup menu with a different set of matches.
2114 ins_compl_show_pum();
2115
2116 // Don't let Enter select the original text when there is no popup menu.
2117 if (compl_match_array == NULL)
2118 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002119 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2120 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002121}
2122
2123/*
2124 * Return the length of the completion, from the completion start column to
2125 * the cursor column. Making sure it never goes below zero.
2126 */
2127 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002128get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002129{
2130 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002131 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002132}
2133
2134/*
2135 * Append one character to the match leader. May reduce the number of
2136 * matches.
2137 */
2138 void
2139ins_compl_addleader(int c)
2140{
2141 int cc;
2142
glepniredd4ac32025-01-29 18:53:51 +01002143 if (ins_compl_preinsert_effect())
2144 ins_compl_delete();
2145
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002146 if (stop_arrow() == FAIL)
2147 return;
2148 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2149 {
2150 char_u buf[MB_MAXBYTES + 1];
2151
2152 (*mb_char2bytes)(c, buf);
2153 buf[cc] = NUL;
2154 ins_char_bytes(buf, cc);
2155 if (compl_opt_refresh_always)
2156 AppendToRedobuff(buf);
2157 }
2158 else
2159 {
2160 ins_char(c);
2161 if (compl_opt_refresh_always)
2162 AppendCharToRedobuff(c);
2163 }
2164
2165 // If we didn't complete finding matches we must search again.
2166 if (ins_compl_need_restart())
2167 ins_compl_restart();
2168
2169 // When 'always' is set, don't reset compl_leader. While completing,
2170 // cursor doesn't point original position, changing compl_leader would
2171 // break redo.
2172 if (!compl_opt_refresh_always)
2173 {
John Marriott5e6ea922024-11-23 14:01:57 +01002174 VIM_CLEAR_STRING(compl_leader);
2175 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2176 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2177 compl_leader.length);
2178 if (compl_leader.string == NULL)
2179 {
2180 compl_leader.length = 0;
2181 return;
2182 }
2183
2184 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002185 }
2186}
2187
2188/*
2189 * Setup for finding completions again without leaving CTRL-X mode. Used when
2190 * BS or a key was typed while still searching for matches.
2191 */
2192 static void
2193ins_compl_restart(void)
2194{
2195 ins_compl_free();
2196 compl_started = FALSE;
2197 compl_matches = 0;
2198 compl_cont_status = 0;
2199 compl_cont_mode = 0;
2200}
2201
2202/*
2203 * Set the first match, the original text.
2204 */
2205 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002206ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002207{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002208 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002209 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2210 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002211 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002212 {
John Marriott5e6ea922024-11-23 14:01:57 +01002213 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002214 if (p != NULL)
2215 {
John Marriott5e6ea922024-11-23 14:01:57 +01002216 VIM_CLEAR_STRING(compl_first_match->cp_str);
2217 compl_first_match->cp_str.string = p;
2218 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002219 }
2220 }
2221 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002222 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002223 {
John Marriott5e6ea922024-11-23 14:01:57 +01002224 char_u *p = vim_strnsave(str, len);
2225 if (p != NULL)
2226 {
2227 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2228 compl_first_match->cp_prev->cp_str.string = p;
2229 compl_first_match->cp_prev->cp_str.length = len;
2230 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002231 }
2232}
2233
2234/*
2235 * Append one character to the match leader. May reduce the number of
2236 * matches.
2237 */
2238 void
2239ins_compl_addfrommatch(void)
2240{
2241 char_u *p;
2242 int len = (int)curwin->w_cursor.col - (int)compl_col;
2243 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002244
John Marriott5e6ea922024-11-23 14:01:57 +01002245 p = compl_shown_match->cp_str.string;
2246 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002247 {
John Marriott5e6ea922024-11-23 14:01:57 +01002248 size_t plen;
2249 compl_T *cp;
2250
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002251 // When still at the original match use the first entry that matches
2252 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002253 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002254 return;
2255
2256 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002257 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002258 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002259 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002260 {
John Marriott5e6ea922024-11-23 14:01:57 +01002261 if (compl_leader.string == NULL
2262 || ins_compl_equal(cp, compl_leader.string,
2263 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002264 {
John Marriott5e6ea922024-11-23 14:01:57 +01002265 p = cp->cp_str.string;
2266 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002267 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002268 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002269 }
John Marriott5e6ea922024-11-23 14:01:57 +01002270 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002271 return;
2272 }
2273 p += len;
2274 c = PTR2CHAR(p);
2275 ins_compl_addleader(c);
2276}
2277
2278/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002279 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002280 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2281 * compl_cont_mode and compl_cont_status.
2282 * Returns TRUE when the character is not to be inserted.
2283 */
2284 static int
2285set_ctrl_x_mode(int c)
2286{
2287 int retval = FALSE;
2288
2289 switch (c)
2290 {
2291 case Ctrl_E:
2292 case Ctrl_Y:
2293 // scroll the window one line up or down
2294 ctrl_x_mode = CTRL_X_SCROLL;
2295 if (!(State & REPLACE_FLAG))
2296 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2297 else
2298 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2299 edit_submode_pre = NULL;
2300 showmode();
2301 break;
2302 case Ctrl_L:
2303 // complete whole line
2304 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2305 break;
2306 case Ctrl_F:
2307 // complete filenames
2308 ctrl_x_mode = CTRL_X_FILES;
2309 break;
2310 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002311 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002312 ctrl_x_mode = CTRL_X_DICTIONARY;
2313 break;
2314 case Ctrl_R:
2315 // Register insertion without exiting CTRL-X mode
2316 // Simply allow ^R to happen without affecting ^X mode
2317 break;
2318 case Ctrl_T:
2319 // complete words from a thesaurus
2320 ctrl_x_mode = CTRL_X_THESAURUS;
2321 break;
2322#ifdef FEAT_COMPL_FUNC
2323 case Ctrl_U:
2324 // user defined completion
2325 ctrl_x_mode = CTRL_X_FUNCTION;
2326 break;
2327 case Ctrl_O:
2328 // omni completion
2329 ctrl_x_mode = CTRL_X_OMNI;
2330 break;
2331#endif
2332 case 's':
2333 case Ctrl_S:
2334 // complete spelling suggestions
2335 ctrl_x_mode = CTRL_X_SPELL;
2336#ifdef FEAT_SPELL
2337 ++emsg_off; // Avoid getting the E756 error twice.
2338 spell_back_to_badword();
2339 --emsg_off;
2340#endif
2341 break;
2342 case Ctrl_RSB:
2343 // complete tag names
2344 ctrl_x_mode = CTRL_X_TAGS;
2345 break;
2346#ifdef FEAT_FIND_ID
2347 case Ctrl_I:
2348 case K_S_TAB:
2349 // complete keywords from included files
2350 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2351 break;
2352 case Ctrl_D:
2353 // complete definitions from included files
2354 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2355 break;
2356#endif
2357 case Ctrl_V:
2358 case Ctrl_Q:
2359 // complete vim commands
2360 ctrl_x_mode = CTRL_X_CMDLINE;
2361 break;
2362 case Ctrl_Z:
2363 // stop completion
2364 ctrl_x_mode = CTRL_X_NORMAL;
2365 edit_submode = NULL;
2366 showmode();
2367 retval = TRUE;
2368 break;
2369 case Ctrl_P:
2370 case Ctrl_N:
2371 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2372 // just started ^X mode, or there were enough ^X's to cancel
2373 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2374 // do normal expansion when interrupting a different mode (say
2375 // ^X^F^X^P or ^P^X^X^P, see below)
2376 // nothing changes if interrupting mode 0, (eg, the flag
2377 // doesn't change when going to ADDING mode -- Acevedo
2378 if (!(compl_cont_status & CONT_INTRPT))
2379 compl_cont_status |= CONT_LOCAL;
2380 else if (compl_cont_mode != 0)
2381 compl_cont_status &= ~CONT_LOCAL;
2382 // FALLTHROUGH
2383 default:
2384 // If we have typed at least 2 ^X's... for modes != 0, we set
2385 // compl_cont_status = 0 (eg, as if we had just started ^X
2386 // mode).
2387 // For mode 0, we set "compl_cont_mode" to an impossible
2388 // value, in both cases ^X^X can be used to restart the same
2389 // mode (avoiding ADDING mode).
2390 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2391 // 'complete' and local ^P expansions respectively.
2392 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2393 // mode -- Acevedo
2394 if (c == Ctrl_X)
2395 {
2396 if (compl_cont_mode != 0)
2397 compl_cont_status = 0;
2398 else
2399 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2400 }
2401 ctrl_x_mode = CTRL_X_NORMAL;
2402 edit_submode = NULL;
2403 showmode();
2404 break;
2405 }
2406
2407 return retval;
2408}
2409
2410/*
glepnir1c5a1202024-12-04 20:27:34 +01002411 * Trigger CompleteDone event and adds relevant information to v:event
2412 */
2413 static void
2414trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2415{
2416#if defined(FEAT_EVAL)
2417 save_v_event_T save_v_event;
2418 dict_T *v_event = get_v_event(&save_v_event);
2419 char_u *mode_str = NULL;
2420
2421 mode = mode & ~CTRL_X_WANT_IDENT;
2422 if (ctrl_x_mode_names[mode])
2423 mode_str = (char_u *)ctrl_x_mode_names[mode];
2424
2425 (void)dict_add_string(v_event, "complete_word",
2426 word == NULL ? (char_u *)"" : word);
2427 (void)dict_add_string(v_event, "complete_type",
2428 mode_str != NULL ? mode_str : (char_u *)"");
2429
2430 dict_set_items_ro(v_event);
2431#endif
2432 ins_apply_autocmds(EVENT_COMPLETEDONE);
2433
2434#if defined(FEAT_EVAL)
2435 restore_v_event(v_event, &save_v_event);
2436#endif
2437}
2438
2439/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002440 * Stop insert completion mode
2441 */
2442 static int
2443ins_compl_stop(int c, int prev_mode, int retval)
2444{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002445 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002446 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002447
2448 // Get here when we have finished typing a sequence of ^N and
2449 // ^P or other completion characters in CTRL-X mode. Free up
2450 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002451 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002452 {
glepnir40891ba2025-02-10 22:18:00 +01002453 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002454
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002455 // If any of the original typed text has been changed, eg when
2456 // ignorecase is set, we must add back-spaces to the redo
2457 // buffer. We add as few as necessary to delete just the part
2458 // of the original text that has changed.
2459 // When using the longest match, edited the match or used
2460 // CTRL-E then don't use the current match.
2461 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002462 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002463 ins_compl_fixRedoBufForLeader(ptr);
2464 }
2465
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002466 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002467
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002468 // When completing whole lines: fix indent for 'cindent'.
2469 // Otherwise, break line if it's too long.
2470 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2471 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002472 // re-indent the current line
2473 if (want_cindent)
2474 {
2475 do_c_expr_indent();
2476 want_cindent = FALSE; // don't do it again
2477 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002478 }
2479 else
2480 {
2481 int prev_col = curwin->w_cursor.col;
2482
2483 // put the cursor on the last char, for 'tw' formatting
2484 if (prev_col > 0)
2485 dec_cursor();
2486 // only format when something was inserted
2487 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2488 insertchar(NUL, 0, -1);
2489 if (prev_col > 0
2490 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2491 inc_cursor();
2492 }
2493
2494 // If the popup menu is displayed pressing CTRL-Y means accepting
2495 // the selection without inserting anything. When
2496 // compl_enter_selects is set the Enter key does the same.
2497 if ((c == Ctrl_Y || (compl_enter_selects
2498 && (c == CAR || c == K_KENTER || c == NL)))
2499 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002500 {
2501 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002502 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002503 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002504
2505 // CTRL-E means completion is Ended, go back to the typed text.
2506 // but only do this, if the Popup is still visible
2507 if (c == Ctrl_E)
2508 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002509 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002510 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002511
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002512 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002513 if (compl_leader.string != NULL)
2514 {
2515 p = compl_leader.string;
2516 plen = compl_leader.length;
2517 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002518 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002519 {
2520 p = compl_orig_text.string;
2521 plen = compl_orig_text.length;
2522 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002523 if (p != NULL)
2524 {
2525 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002526
John Marriott5e6ea922024-11-23 14:01:57 +01002527 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002528 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002529 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002530 retval = TRUE;
2531 }
2532
glepnir001c26c2025-02-02 09:36:22 +01002533 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2534 ins_compl_delete();
2535
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002536 auto_format(FALSE, TRUE);
2537
2538 // Trigger the CompleteDonePre event to give scripts a chance to
2539 // act upon the completion before clearing the info, and restore
2540 // ctrl_x_mode, so that complete_info() can be used.
2541 ctrl_x_mode = prev_mode;
2542 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2543
2544 ins_compl_free();
2545 compl_started = FALSE;
2546 compl_matches = 0;
2547 if (!shortmess(SHM_COMPLETIONMENU))
2548 msg_clr_cmdline(); // necessary for "noshowmode"
2549 ctrl_x_mode = CTRL_X_NORMAL;
2550 compl_enter_selects = FALSE;
2551 if (edit_submode != NULL)
2552 {
2553 edit_submode = NULL;
2554 showmode();
2555 }
2556
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002557 if (c == Ctrl_C && cmdwin_type != 0)
2558 // Avoid the popup menu remains displayed when leaving the
2559 // command line window.
2560 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002561 // Indent now if a key was typed that is in 'cinkeys'.
2562 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2563 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002564 // Trigger the CompleteDone event to give scripts a chance to act
2565 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002566 trigger_complete_done_event(prev_mode, word);
2567 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002568
2569 return retval;
2570}
2571
2572/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002573 * Prepare for Insert mode completion, or stop it.
2574 * Called just after typing a character in Insert mode.
2575 * Returns TRUE when the character is not to be inserted;
2576 */
2577 int
2578ins_compl_prep(int c)
2579{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002580 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002581 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582
2583 // Forget any previous 'special' messages if this is actually
2584 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2585 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2586 edit_submode_extra = NULL;
2587
zeertzjq440d4cb2023-03-02 17:51:32 +00002588 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002589 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002590 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002591 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002592 return retval;
2593
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002594#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002595 // Ignore mouse events in a popup window
2596 if (is_mouse_key(c))
2597 {
2598 // Ignore drag and release events, the position does not need to be in
2599 // the popup and it may have just closed.
2600 if (c == K_LEFTRELEASE
2601 || c == K_LEFTRELEASE_NM
2602 || c == K_MIDDLERELEASE
2603 || c == K_RIGHTRELEASE
2604 || c == K_X1RELEASE
2605 || c == K_X2RELEASE
2606 || c == K_LEFTDRAG
2607 || c == K_MIDDLEDRAG
2608 || c == K_RIGHTDRAG
2609 || c == K_X1DRAG
2610 || c == K_X2DRAG)
2611 return retval;
2612 if (popup_visible)
2613 {
2614 int row = mouse_row;
2615 int col = mouse_col;
2616 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2617
2618 if (wp != NULL && WIN_IS_POPUP(wp))
2619 return retval;
2620 }
2621 }
2622#endif
2623
zeertzjqdca29d92021-08-31 19:12:51 +02002624 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2625 {
2626 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2627 || !vim_is_ctrl_x_key(c))
2628 {
2629 // Not starting another completion mode.
2630 ctrl_x_mode = CTRL_X_CMDLINE;
2631
2632 // CTRL-X CTRL-Z should stop completion without inserting anything
2633 if (c == Ctrl_Z)
2634 retval = TRUE;
2635 }
2636 else
2637 {
2638 ctrl_x_mode = CTRL_X_CMDLINE;
2639
2640 // Other CTRL-X keys first stop completion, then start another
2641 // completion mode.
2642 ins_compl_prep(' ');
2643 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2644 }
2645 }
2646
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002647 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002648 if (ctrl_x_mode_not_defined_yet()
2649 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002650 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002651 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002652 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 }
2654
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002655 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002656 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2657 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002658 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002659 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002660 {
2661 // We're already in CTRL-X mode, do we stay in it?
2662 if (!vim_is_ctrl_x_key(c))
2663 {
glepnir40891ba2025-02-10 22:18:00 +01002664 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002665 edit_submode = NULL;
2666 }
2667 showmode();
2668 }
2669
2670 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2671 {
2672 // Show error message from attempted keyword completion (probably
2673 // 'Pattern not found') until another key is hit, then go back to
2674 // showing what mode we are in.
2675 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002676 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002677 && c != Ctrl_R && !ins_compl_pum_key(c))
2678 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002679 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002680 }
2681 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2682 // Trigger the CompleteDone event to give scripts a chance to act
2683 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002684 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002685
LemonBoy2bf52dd2022-04-09 18:17:34 +01002686 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002687
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002688 // reset continue_* if we left expansion-mode, if we stay they'll be
2689 // (re)set properly in ins_complete()
2690 if (!vim_is_ctrl_x_key(c))
2691 {
2692 compl_cont_status = 0;
2693 compl_cont_mode = 0;
2694 }
2695
2696 return retval;
2697}
2698
2699/*
2700 * Fix the redo buffer for the completion leader replacing some of the typed
2701 * text. This inserts backspaces and appends the changed text.
2702 * "ptr" is the known leader text or NUL.
2703 */
2704 static void
2705ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2706{
John Marriott5e6ea922024-11-23 14:01:57 +01002707 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002708 char_u *p;
2709 char_u *ptr = ptr_arg;
2710
2711 if (ptr == NULL)
2712 {
John Marriott5e6ea922024-11-23 14:01:57 +01002713 if (compl_leader.string != NULL)
2714 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002715 else
2716 return; // nothing to do
2717 }
John Marriott5e6ea922024-11-23 14:01:57 +01002718 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002719 {
John Marriott5e6ea922024-11-23 14:01:57 +01002720 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002721 // Find length of common prefix between original text and new completion
2722 while (p[len] != NUL && p[len] == ptr[len])
2723 len++;
2724 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002725 if (len > 0)
2726 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002727 // Add backspace characters for each remaining character in
2728 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002729 for (p += len; *p != NUL; MB_PTR_ADV(p))
2730 AppendCharToRedobuff(K_BS);
2731 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002732 if (ptr != NULL)
2733 AppendToRedobuffLit(ptr + len, -1);
2734}
2735
2736/*
2737 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2738 * (depending on flag) starting from buf and looking for a non-scanned
2739 * buffer (other than curbuf). curbuf is special, if it is called with
2740 * buf=curbuf then it has to be the first call for a given flag/expansion.
2741 *
2742 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2743 */
2744 static buf_T *
2745ins_compl_next_buf(buf_T *buf, int flag)
2746{
2747 static win_T *wp = NULL;
glepnir40891ba2025-02-10 22:18:00 +01002748 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002749
2750 if (flag == 'w') // just windows
2751 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002752 if (buf == curbuf || !win_valid(wp))
2753 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002754 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01002755
2756 while (TRUE)
2757 {
2758 // Move to next window (wrap to first window if at the end)
2759 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2760 // Break if we're back at start or found an unscanned buffer
2761 if (wp == curwin || !wp->w_buffer->b_scanned)
2762 break;
2763 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002764 buf = wp->w_buffer;
2765 }
2766 else
glepnir40891ba2025-02-10 22:18:00 +01002767 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002768 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2769 // (unlisted buffers)
2770 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01002771 while (TRUE)
2772 {
2773 // Move to next buffer (wrap to first buffer if at the end)
2774 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2775 // Break if we're back at start buffer
2776 if (buf == curbuf)
2777 break;
2778
2779 // Check buffer conditions based on flag
2780 if (flag == 'U')
2781 skip_buffer = buf->b_p_bl;
2782 else
2783 skip_buffer = !buf->b_p_bl ||
2784 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2785
2786 // Break if we found a buffer that matches our criteria
2787 if (!skip_buffer && !buf->b_scanned)
2788 break;
2789 }
2790 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002791 return buf;
2792}
2793
2794#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795
2796# ifdef FEAT_EVAL
2797static callback_T cfu_cb; // 'completefunc' callback function
2798static callback_T ofu_cb; // 'omnifunc' callback function
2799static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2800# endif
2801
2802/*
2803 * Copy a global callback function to a buffer local callback.
2804 */
2805 static void
2806copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2807{
2808 free_callback(bufcb);
2809 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2810 copy_callback(bufcb, globcb);
2811}
2812
2813/*
2814 * Parse the 'completefunc' option value and set the callback function.
2815 * Invoked when the 'completefunc' option is set. The option value can be a
2816 * name of a function (string), or function(<name>) or funcref(<name>) or a
2817 * lambda expression.
2818 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002819 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002820did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002821{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2823 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002824
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002825 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002826
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002827 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002828}
2829
2830/*
2831 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002832 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002833 */
2834 void
2835set_buflocal_cfu_callback(buf_T *buf UNUSED)
2836{
2837# ifdef FEAT_EVAL
2838 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2839# endif
2840}
2841
2842/*
2843 * Parse the 'omnifunc' option value and set the callback function.
2844 * Invoked when the 'omnifunc' option is set. The option value can be a
2845 * name of a function (string), or function(<name>) or funcref(<name>) or a
2846 * lambda expression.
2847 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002848 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002849did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002850{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002851 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2852 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002853
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002854 set_buflocal_ofu_callback(curbuf);
2855 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002856}
2857
2858/*
2859 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002860 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002861 */
2862 void
2863set_buflocal_ofu_callback(buf_T *buf UNUSED)
2864{
2865# ifdef FEAT_EVAL
2866 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2867# endif
2868}
2869
2870/*
2871 * Parse the 'thesaurusfunc' option value and set the callback function.
2872 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2873 * name of a function (string), or function(<name>) or funcref(<name>) or a
2874 * lambda expression.
2875 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002876 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002877did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002878{
2879 int retval;
2880
zeertzjq6eda2692024-11-03 09:23:33 +01002881 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002882 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002883 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2884 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002885 else
2886 {
2887 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002888 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002889 // when using :set, free the local callback
2890 if (!(args->os_flags & OPT_GLOBAL))
2891 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002892 }
2893
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002894 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002895}
2896
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002897/*
2898 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002899 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002900 */
2901 int
2902set_ref_in_insexpand_funcs(int copyID)
2903{
2904 int abort = FALSE;
2905
2906 abort = set_ref_in_callback(&cfu_cb, copyID);
2907 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2908 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2909
2910 return abort;
2911}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002912
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002913/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002914 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002915 */
2916 static char_u *
2917get_complete_funcname(int type)
2918{
2919 switch (type)
2920 {
2921 case CTRL_X_FUNCTION:
2922 return curbuf->b_p_cfu;
2923 case CTRL_X_OMNI:
2924 return curbuf->b_p_ofu;
2925 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002926 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002927 default:
2928 return (char_u *)"";
2929 }
2930}
2931
2932/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002933 * Get the callback to use for insert mode completion.
2934 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002935 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002936get_insert_callback(int type)
2937{
2938 if (type == CTRL_X_FUNCTION)
2939 return &curbuf->b_cfu_cb;
2940 if (type == CTRL_X_OMNI)
2941 return &curbuf->b_ofu_cb;
2942 // CTRL_X_THESAURUS
2943 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2944}
2945
2946/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002947 * Execute user defined complete function 'completefunc', 'omnifunc' or
2948 * 'thesaurusfunc', and get matches in "matches".
2949 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002950 */
2951 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002952expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002953{
2954 list_T *matchlist = NULL;
2955 dict_T *matchdict = NULL;
2956 typval_T args[3];
2957 char_u *funcname;
2958 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002959 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002960 typval_T rettv;
2961 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002962 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002963
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002964 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002965 if (*funcname == NUL)
2966 return;
2967
2968 // Call 'completefunc' to obtain the list of matches.
2969 args[0].v_type = VAR_NUMBER;
2970 args[0].vval.v_number = 0;
2971 args[1].v_type = VAR_STRING;
2972 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2973 args[2].v_type = VAR_UNKNOWN;
2974
2975 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002976 // Lock the text to avoid weird things from happening. Also disallow
2977 // switching to another window, it should not be needed and may end up in
2978 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002979 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002980
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002981 cb = get_insert_callback(type);
2982 retval = call_callback(cb, 0, &rettv, 2, args);
2983
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002984 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002985 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002986 {
2987 switch (rettv.v_type)
2988 {
2989 case VAR_LIST:
2990 matchlist = rettv.vval.v_list;
2991 break;
2992 case VAR_DICT:
2993 matchdict = rettv.vval.v_dict;
2994 break;
2995 case VAR_SPECIAL:
2996 if (rettv.vval.v_number == VVAL_NONE)
2997 compl_opt_suppress_empty = TRUE;
2998 // FALLTHROUGH
2999 default:
3000 // TODO: Give error message?
3001 clear_tv(&rettv);
3002 break;
3003 }
3004 }
zeertzjqcfe45652022-05-27 17:26:55 +01003005 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003006
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003007 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003008 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003009 validate_cursor();
3010 if (!EQUAL_POS(curwin->w_cursor, pos))
3011 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003012 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003013 goto theend;
3014 }
3015
3016 if (matchlist != NULL)
3017 ins_compl_add_list(matchlist);
3018 else if (matchdict != NULL)
3019 ins_compl_add_dict(matchdict);
3020
3021theend:
3022 // Restore State, it might have been changed.
3023 State = save_State;
3024
3025 if (matchdict != NULL)
3026 dict_unref(matchdict);
3027 if (matchlist != NULL)
3028 list_unref(matchlist);
3029}
3030#endif // FEAT_COMPL_FUNC
3031
3032#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003033
3034 static inline int
3035get_user_highlight_attr(char_u *hlname)
3036{
3037 if (hlname != NULL && *hlname != NUL)
3038 return syn_name2attr(hlname);
3039 return -1;
3040}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003041/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003042 * Add a match to the list of matches from a typeval_T.
3043 * If the given string is already in the list of completions, then return
3044 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3045 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003046 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003047 */
3048 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003049ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003050{
3051 char_u *word;
3052 int dup = FALSE;
3053 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003054 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003055 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003056 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003057 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003058 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003059 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003060 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003061
Bram Moolenaar08928322020-01-04 14:32:48 +01003062 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003063 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3064 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003065 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3066 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3067 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3068 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3069 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003070
glepnir0fe17f82024-10-08 22:26:44 +02003071 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003072 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003073
3074 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003075 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003076
Bram Moolenaard61efa52022-07-23 09:52:04 +01003077 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3078 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3079 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003080 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003081 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3082 dup = dict_get_number(tv->vval.v_dict, "dup");
3083 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3084 empty = dict_get_number(tv->vval.v_dict, "empty");
3085 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3086 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003087 flags |= CP_EQUAL;
3088 }
3089 else
3090 {
3091 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003092 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003093 }
3094 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003095 {
3096 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003097 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003098 }
glepnir508e7852024-07-25 21:39:08 +02003099 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003100 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003101 if (status != OK)
3102 clear_tv(&user_data);
3103 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003104}
3105
3106/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003107 * Add completions from a list.
3108 */
3109 static void
3110ins_compl_add_list(list_T *list)
3111{
3112 listitem_T *li;
3113 int dir = compl_direction;
3114
3115 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003116 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003117 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003118 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003119 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003120 // if dir was BACKWARD then honor it just once
3121 dir = FORWARD;
3122 else if (did_emsg)
3123 break;
3124 }
3125}
3126
3127/*
3128 * Add completions from a dict.
3129 */
3130 static void
3131ins_compl_add_dict(dict_T *dict)
3132{
3133 dictitem_T *di_refresh;
3134 dictitem_T *di_words;
3135
3136 // Check for optional "refresh" item.
3137 compl_opt_refresh_always = FALSE;
3138 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3139 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3140 {
3141 char_u *v = di_refresh->di_tv.vval.v_string;
3142
3143 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3144 compl_opt_refresh_always = TRUE;
3145 }
3146
3147 // Add completions from a "words" list.
3148 di_words = dict_find(dict, (char_u *)"words", 5);
3149 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3150 ins_compl_add_list(di_words->di_tv.vval.v_list);
3151}
3152
3153/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003154 * Start completion for the complete() function.
3155 * "startcol" is where the matched text starts (1 is first column).
3156 * "list" is the list of matches.
3157 */
3158 static void
3159set_completion(colnr_T startcol, list_T *list)
3160{
3161 int save_w_wrow = curwin->w_wrow;
3162 int save_w_leftcol = curwin->w_leftcol;
3163 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003164 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003165 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3166 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3167 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003168
3169 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003170 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003171 ins_compl_prep(' ');
3172 ins_compl_clear();
3173 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003174 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003175
3176 compl_direction = FORWARD;
3177 if (startcol > curwin->w_cursor.col)
3178 startcol = curwin->w_cursor.col;
3179 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003180 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003181 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3182 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003183 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3184 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003185 if (p_ic)
3186 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003187 if (compl_orig_text.string == NULL)
3188 {
3189 compl_orig_text.length = 0;
3190 return;
3191 }
3192 compl_orig_text.length = (size_t)compl_length;
3193 if (ins_compl_add(compl_orig_text.string,
3194 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3195 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003196 return;
3197
3198 ctrl_x_mode = CTRL_X_EVAL;
3199
3200 ins_compl_add_list(list);
3201 compl_matches = ins_compl_make_cyclic();
3202 compl_started = TRUE;
3203 compl_used_match = TRUE;
3204 compl_cont_status = 0;
3205
3206 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003207 int no_select = compl_no_select || compl_longest;
3208 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003209 {
3210 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003211 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003212 // Down/Up has no real effect.
3213 ins_complete(K_UP, FALSE);
3214 }
3215 else
3216 ins_complete(Ctrl_N, FALSE);
3217 compl_enter_selects = compl_no_insert;
3218
3219 // Lazily show the popup menu, unless we got interrupted.
3220 if (!compl_interrupted)
3221 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003222 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003223 out_flush();
3224}
3225
3226/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003227 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003228 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003229 void
3230f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003231{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003232 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003233
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003234 if (in_vim9script()
3235 && (check_for_number_arg(argvars, 0) == FAIL
3236 || check_for_list_arg(argvars, 1) == FAIL))
3237 return;
3238
Bram Moolenaar24959102022-05-07 20:01:16 +01003239 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003240 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003241 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003242 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003243 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003244
3245 // Check for undo allowed here, because if something was already inserted
3246 // the line was already saved for undo and this check isn't done.
3247 if (!undo_allowed())
3248 return;
3249
Bram Moolenaard83392a2022-09-01 12:22:46 +01003250 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003251 {
3252 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3253 if (startcol > 0)
3254 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003255 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003256}
3257
3258/*
3259 * "complete_add()" function
3260 */
3261 void
3262f_complete_add(typval_T *argvars, typval_T *rettv)
3263{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003264 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003265 return;
3266
Bram Moolenaar440cf092021-04-03 20:13:30 +02003267 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003268}
3269
3270/*
3271 * "complete_check()" function
3272 */
3273 void
3274f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3275{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003276 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003277 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003278
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003279 ins_compl_check_keys(0, TRUE);
3280 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003281
3282 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003283}
3284
3285/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003286 * Return Insert completion mode name string
3287 */
3288 static char_u *
3289ins_compl_mode(void)
3290{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003291 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003292 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3293
3294 return (char_u *)"";
3295}
3296
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003297/*
3298 * Assign the sequence number to all the completion matches which don't have
3299 * one assigned yet.
3300 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003301 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003302ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003303{
3304 int number = 0;
3305 compl_T *match;
3306
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003307 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003308 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003309 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003310 // This should normally succeed already at the first loop
3311 // cycle, so it's fast!
3312 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003313 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003314 if (match->cp_number != -1)
3315 {
3316 number = match->cp_number;
3317 break;
3318 }
3319 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003320 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003321 for (match = match->cp_next;
3322 match != NULL && match->cp_number == -1;
3323 match = match->cp_next)
3324 match->cp_number = ++number;
3325 }
3326 else // BACKWARD
3327 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003328 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003329 // number. This should normally succeed already at the
3330 // first loop cycle, so it's fast!
3331 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003332 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003333 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003334 if (match->cp_number != -1)
3335 {
3336 number = match->cp_number;
3337 break;
3338 }
glepnir40891ba2025-02-10 22:18:00 +01003339 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003340 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003341 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003342 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003343 for (match = match->cp_prev; match
3344 && match->cp_number == -1;
3345 match = match->cp_prev)
3346 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003347 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003348 }
3349}
3350
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003351/*
glepnir037b0282025-01-16 14:37:44 +01003352 * Fill the dict of complete_info
3353 */
3354 static void
3355fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3356{
3357 dict_add_string(di, "word", match->cp_str.string);
3358 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3359 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3360 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3361 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3362 if (add_match)
3363 dict_add_bool(di, "match", match->cp_in_match_array);
3364 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3365 // Add an empty string for backwards compatibility
3366 dict_add_string(di, "user_data", (char_u *)"");
3367 else
3368 dict_add_tv(di, "user_data", &match->cp_user_data);
3369}
3370
3371/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003372 * Get complete information
3373 */
3374 static void
3375get_complete_info(list_T *what_list, dict_T *retdict)
3376{
3377 int ret = OK;
3378 listitem_T *item;
3379#define CI_WHAT_MODE 0x01
3380#define CI_WHAT_PUM_VISIBLE 0x02
3381#define CI_WHAT_ITEMS 0x04
3382#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003383#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003384#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003385#define CI_WHAT_ALL 0xff
3386 int what_flag;
3387
3388 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003389 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003390 else
3391 {
3392 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003393 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003394 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003395 {
3396 char_u *what = tv_get_string(&item->li_tv);
3397
3398 if (STRCMP(what, "mode") == 0)
3399 what_flag |= CI_WHAT_MODE;
3400 else if (STRCMP(what, "pum_visible") == 0)
3401 what_flag |= CI_WHAT_PUM_VISIBLE;
3402 else if (STRCMP(what, "items") == 0)
3403 what_flag |= CI_WHAT_ITEMS;
3404 else if (STRCMP(what, "selected") == 0)
3405 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003406 else if (STRCMP(what, "completed") == 0)
3407 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003408 else if (STRCMP(what, "matches") == 0)
3409 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003410 }
3411 }
3412
3413 if (ret == OK && (what_flag & CI_WHAT_MODE))
3414 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3415
3416 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3417 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3418
glepnir037b0282025-01-16 14:37:44 +01003419 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3420 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003421 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003422 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003423 dict_T *di;
3424 compl_T *match;
3425 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003426 int has_items = what_flag & CI_WHAT_ITEMS;
3427 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003428 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003429
glepnird4088ed2024-12-31 10:55:22 +01003430 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003431 {
3432 li = list_alloc();
3433 if (li == NULL)
3434 return;
glepnird4088ed2024-12-31 10:55:22 +01003435 ret = dict_add_list(retdict, (has_matches && !has_items)
3436 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003437 }
3438 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3439 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3440 ins_compl_update_sequence_numbers();
3441 if (ret == OK && compl_first_match != NULL)
3442 {
3443 int list_idx = 0;
3444 match = compl_first_match;
3445 do
3446 {
3447 if (!match_at_original_text(match))
3448 {
glepnird4088ed2024-12-31 10:55:22 +01003449 if (has_items
3450 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003451 {
3452 di = dict_alloc();
3453 if (di == NULL)
3454 return;
3455 ret = list_append_dict(li, di);
3456 if (ret != OK)
3457 return;
glepnir037b0282025-01-16 14:37:44 +01003458 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003459 }
glepnird4088ed2024-12-31 10:55:22 +01003460 if (compl_curr_match != NULL
3461 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003462 selected_idx = list_idx;
3463 list_idx += 1;
3464 }
3465 match = match->cp_next;
3466 }
3467 while (match != NULL && !is_first_match(match));
3468 }
3469 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3470 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003471
glepnir037b0282025-01-16 14:37:44 +01003472 if (ret == OK && selected_idx != -1 && has_completed)
3473 {
3474 di = dict_alloc();
3475 if (di == NULL)
3476 return;
3477 fill_complete_info_dict(di, compl_curr_match, FALSE);
3478 ret = dict_add_dict(retdict, "completed", di);
3479 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003480 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003481}
3482
3483/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003484 * "complete_info()" function
3485 */
3486 void
3487f_complete_info(typval_T *argvars, typval_T *rettv)
3488{
3489 list_T *what_list = NULL;
3490
Bram Moolenaar93a10962022-06-16 11:42:09 +01003491 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003492 return;
3493
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003494 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3495 return;
3496
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003497 if (argvars[0].v_type != VAR_UNKNOWN)
3498 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003499 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003500 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003501 what_list = argvars[0].vval.v_list;
3502 }
3503 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003504}
3505#endif
3506
3507/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003508 * Returns TRUE when using a user-defined function for thesaurus completion.
3509 */
3510 static int
3511thesaurus_func_complete(int type UNUSED)
3512{
3513#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003514 return type == CTRL_X_THESAURUS
3515 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003516#else
3517 return FALSE;
3518#endif
3519}
3520
3521/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003522 * Return value of process_next_cpt_value()
3523 */
3524enum
3525{
3526 INS_COMPL_CPT_OK = 1,
3527 INS_COMPL_CPT_CONT,
3528 INS_COMPL_CPT_END
3529};
3530
3531/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003532 * state information used for getting the next set of insert completion
3533 * matches.
3534 */
3535typedef struct
3536{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003537 char_u *e_cpt_copy; // copy of 'complete'
3538 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003539 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003540 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003541 pos_T prev_match_pos; // previous match position
3542 int set_match_pos; // save first_match_pos/last_match_pos
3543 pos_T first_match_pos; // first match position
3544 pos_T last_match_pos; // last match position
3545 int found_all; // found all matches of a certain type.
3546 char_u *dict; // dictionary file to search
3547 int dict_f; // "dict" is an exact file name or not
3548} ins_compl_next_state_T;
3549
3550/*
3551 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003552 *
3553 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003554 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003555 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003556 * st->found_all - all matches of this type are found
3557 * st->ins_buf - search for completions in this buffer
3558 * st->first_match_pos - position of the first completion match
3559 * st->last_match_pos - position of the last completion match
3560 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003561 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003562 * st->dict - name of the dictionary or thesaurus file to search
3563 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003564 *
3565 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003566 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3567 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003568 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003569 */
3570 static int
3571process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003572 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003573 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003574 pos_T *start_match_pos,
3575 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003577 int compl_type = -1;
3578 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003579
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003580 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003581
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003582 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3583 st->e_cpt++;
3584
3585 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003586 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003587 st->ins_buf = curbuf;
3588 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003589 // Move the cursor back one character so that ^N can match the
3590 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003591 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003592 {
3593 // Move the cursor to after the last character in the
3594 // buffer, so that word at start of buffer is found
3595 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003596 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003597 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003598 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003599 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003600 compl_type = 0;
3601
3602 // Remember the first match so that the loop stops when we
3603 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003604 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003605 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003606 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003607 && (st->ins_buf = ins_compl_next_buf(
3608 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003609 {
3610 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003611 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003612 {
3613 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003614 st->first_match_pos.col = st->last_match_pos.col = 0;
3615 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3616 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003617 compl_type = 0;
3618 }
3619 else // unloaded buffer, scan like dictionary
3620 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003621 st->found_all = TRUE;
3622 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003623 {
3624 status = INS_COMPL_CPT_CONT;
3625 goto done;
3626 }
3627 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003628 st->dict = st->ins_buf->b_fname;
3629 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003631 if (!shortmess(SHM_COMPLETIONSCAN))
3632 {
3633 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3634 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3635 st->ins_buf->b_fname == NULL
3636 ? buf_spname(st->ins_buf)
3637 : st->ins_buf->b_sfname == NULL
3638 ? st->ins_buf->b_fname
3639 : st->ins_buf->b_sfname);
3640 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3641 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003642 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003643 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003644 status = INS_COMPL_CPT_END;
3645 else
3646 {
3647 if (ctrl_x_mode_line_or_eval())
3648 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003649 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003650 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003651 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003652 compl_type = CTRL_X_DICTIONARY;
3653 else
3654 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003655 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003656 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003657 st->dict = st->e_cpt;
3658 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 }
3660 }
3661#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003662 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003663 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003664 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 compl_type = CTRL_X_PATH_DEFINES;
3666#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003667 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003670 if (!shortmess(SHM_COMPLETIONSCAN))
3671 {
3672 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3673 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3674 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3675 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 }
3677 else
3678 compl_type = -1;
3679
3680 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003681 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003682
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003683 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003684 if (compl_type == -1)
3685 status = INS_COMPL_CPT_CONT;
3686 }
3687
3688done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003689 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690 return status;
3691}
3692
3693#ifdef FEAT_FIND_ID
3694/*
3695 * Get the next set of identifiers or defines matching "compl_pattern" in
3696 * included files.
3697 */
3698 static void
3699get_next_include_file_completion(int compl_type)
3700{
John Marriott5e6ea922024-11-23 14:01:57 +01003701 find_pattern_in_path(compl_pattern.string, compl_direction,
3702 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003703 (compl_type == CTRL_X_PATH_DEFINES
3704 && !(compl_cont_status & CONT_SOL))
3705 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003706 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003707}
3708#endif
3709
3710/*
3711 * Get the next set of words matching "compl_pattern" in dictionary or
3712 * thesaurus files.
3713 */
3714 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003715get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003716{
3717#ifdef FEAT_COMPL_FUNC
3718 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003719 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003720 else
3721#endif
3722 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003723 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003724 : (compl_type == CTRL_X_THESAURUS
3725 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3726 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003727 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003728 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003729 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003730}
3731
3732/*
3733 * Get the next set of tag names matching "compl_pattern".
3734 */
3735 static void
3736get_next_tag_completion(void)
3737{
3738 int save_p_ic;
3739 char_u **matches;
3740 int num_matches;
3741
3742 // set p_ic according to p_ic, p_scs and pat for find_tags().
3743 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003744 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003745
3746 // Find up to TAG_MANY matches. Avoids that an enormous number
3747 // of matches is found when compl_pattern is empty
3748 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003749 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003751 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003752 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3753 ins_compl_add_matches(num_matches, matches, p_ic);
3754 g_tag_at_cursor = FALSE;
3755 p_ic = save_p_ic;
3756}
3757
3758/*
glepnir8159fb12024-07-17 20:32:54 +02003759 * Compare function for qsort
3760 */
3761static int compare_scores(const void *a, const void *b)
3762{
3763 int idx_a = *(const int *)a;
3764 int idx_b = *(const int *)b;
3765 int score_a = compl_fuzzy_scores[idx_a];
3766 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003767 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3768 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003769}
3770
3771/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003772 * Get the next set of filename matching "compl_pattern".
3773 */
3774 static void
3775get_next_filename_completion(void)
3776{
3777 char_u **matches;
3778 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003779 char_u *ptr;
3780 garray_T fuzzy_indices;
3781 int i;
3782 int score;
3783 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003784 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003785 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3786 char_u **sorted_matches;
3787 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003788 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003789
glepnirb9de1a02024-08-02 19:14:38 +02003790#ifdef BACKSLASH_IN_FILENAME
3791 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3792 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3793#else
3794 char pathsep = PATHSEP;
3795#endif
3796
glepnir8159fb12024-07-17 20:32:54 +02003797 if (in_fuzzy)
3798 {
glepnirb9de1a02024-08-02 19:14:38 +02003799#ifdef BACKSLASH_IN_FILENAME
3800 if (curbuf->b_p_csl[0] == 's')
3801 {
John Marriott5e6ea922024-11-23 14:01:57 +01003802 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003803 {
3804 if (leader[i] == '\\')
3805 leader[i] = '/';
3806 }
3807 }
3808 else if (curbuf->b_p_csl[0] == 'b')
3809 {
John Marriott5e6ea922024-11-23 14:01:57 +01003810 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003811 {
3812 if (leader[i] == '/')
3813 leader[i] = '\\';
3814 }
3815 }
3816#endif
3817 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003818 if (last_sep == NULL)
3819 {
3820 // No path separator or separator is the last character,
3821 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003822 VIM_CLEAR_STRING(compl_pattern);
3823 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3824 if (compl_pattern.string == NULL)
3825 return;
3826 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003827 }
3828 else if (*(last_sep + 1) == '\0')
3829 in_fuzzy = FALSE;
3830 else
3831 {
3832 // Split leader into path and file parts
3833 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003834 char_u *path_with_wildcard;
3835
3836 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003837 if (path_with_wildcard != NULL)
3838 {
John Marriott5e6ea922024-11-23 14:01:57 +01003839 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3840 VIM_CLEAR_STRING(compl_pattern);
3841 compl_pattern.string = path_with_wildcard;
3842 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003843
3844 // Move leader to the file part
3845 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003846 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003847 }
3848 }
glepnir8159fb12024-07-17 20:32:54 +02003849 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003850
John Marriott5e6ea922024-11-23 14:01:57 +01003851 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003852 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3853 return;
3854
3855 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003856 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003857#ifdef BACKSLASH_IN_FILENAME
3858 if (curbuf->b_p_csl[0] != NUL)
3859 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003860 for (i = 0; i < num_matches; ++i)
3861 {
glepnir8159fb12024-07-17 20:32:54 +02003862 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003863 while (*ptr != NUL)
3864 {
3865 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3866 *ptr = '/';
3867 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3868 *ptr = '\\';
3869 ptr += (*mb_ptr2len)(ptr);
3870 }
3871 }
3872 }
3873#endif
glepnir8159fb12024-07-17 20:32:54 +02003874
3875 if (in_fuzzy)
3876 {
3877 ga_init2(&fuzzy_indices, sizeof(int), 10);
3878 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3879
3880 for (i = 0; i < num_matches; i++)
3881 {
3882 ptr = matches[i];
3883 score = fuzzy_match_str(ptr, leader);
3884 if (score > 0)
3885 {
3886 if (ga_grow(&fuzzy_indices, 1) == OK)
3887 {
3888 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3889 compl_fuzzy_scores[i] = score;
3890 fuzzy_indices.ga_len++;
3891 }
3892 }
3893 }
3894
Christian Brabandt220474d2024-07-20 13:26:44 +02003895 // prevent qsort from deref NULL pointer
3896 if (fuzzy_indices.ga_len > 0)
3897 {
3898 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3899 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003900
Christian Brabandt220474d2024-07-20 13:26:44 +02003901 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3902 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3903 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003904
Christian Brabandt220474d2024-07-20 13:26:44 +02003905 FreeWild(num_matches, matches);
3906 matches = sorted_matches;
3907 num_matches = fuzzy_indices.ga_len;
3908 }
glepnir6b6280c2024-07-27 16:25:45 +02003909 else if (leader_len > 0)
3910 {
3911 FreeWild(num_matches, matches);
3912 num_matches = 0;
3913 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003914
glepnir8159fb12024-07-17 20:32:54 +02003915 vim_free(compl_fuzzy_scores);
3916 ga_clear(&fuzzy_indices);
3917 }
3918
glepnir6b6280c2024-07-27 16:25:45 +02003919 if (num_matches > 0)
3920 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003921}
3922
3923/*
3924 * Get the next set of command-line completions matching "compl_pattern".
3925 */
3926 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003927get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003928{
3929 char_u **matches;
3930 int num_matches;
3931
John Marriott5e6ea922024-11-23 14:01:57 +01003932 if (expand_cmdline(&compl_xp, compl_pattern.string,
3933 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003934 ins_compl_add_matches(num_matches, matches, FALSE);
3935}
3936
3937/*
3938 * Get the next set of spell suggestions matching "compl_pattern".
3939 */
3940 static void
3941get_next_spell_completion(linenr_T lnum UNUSED)
3942{
3943#ifdef FEAT_SPELL
3944 char_u **matches;
3945 int num_matches;
3946
John Marriott5e6ea922024-11-23 14:01:57 +01003947 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003948 if (num_matches > 0)
3949 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003950 else
3951 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003952#endif
3953}
3954
3955/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003956 * Return the next word or line from buffer "ins_buf" at position
3957 * "cur_match_pos" for completion. The length of the match is set in "len".
3958 */
3959 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003960ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003961 buf_T *ins_buf, // buffer being scanned
3962 pos_T *cur_match_pos, // current match position
3963 int *match_len,
3964 int *cont_s_ipos) // next ^X<> will set initial_pos
3965{
3966 char_u *ptr;
3967 int len;
3968
3969 *match_len = 0;
3970 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3971 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003972 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003973 if (ctrl_x_mode_line_or_eval())
3974 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003975 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003976 {
3977 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3978 return NULL;
3979 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003980 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003981 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003982 {
3983 char_u *tmp_ptr = ptr;
3984
3985 ptr = skipwhite(tmp_ptr);
3986 len -= (int)(ptr - tmp_ptr);
3987 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003989 }
3990 else
3991 {
3992 char_u *tmp_ptr = ptr;
3993
John Marriott5e6ea922024-11-23 14:01:57 +01003994 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003995 {
3996 tmp_ptr += compl_length;
3997 // Skip if already inside a word.
3998 if (vim_iswordp(tmp_ptr))
3999 return NULL;
4000 // Find start of next word.
4001 tmp_ptr = find_word_start(tmp_ptr);
4002 }
4003 // Find end of this word.
4004 tmp_ptr = find_word_end(tmp_ptr);
4005 len = (int)(tmp_ptr - ptr);
4006
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004007 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004008 {
4009 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4010 {
4011 // Try next line, if any. the new word will be
4012 // "join" as if the normal command "J" was used.
4013 // IOSIZE is always greater than
4014 // compl_length, so the next STRNCPY always
4015 // works -- Acevedo
4016 STRNCPY(IObuff, ptr, len);
4017 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4018 tmp_ptr = ptr = skipwhite(ptr);
4019 // Find start of next word.
4020 tmp_ptr = find_word_start(tmp_ptr);
4021 // Find end of next word.
4022 tmp_ptr = find_word_end(tmp_ptr);
4023 if (tmp_ptr > ptr)
4024 {
4025 if (*ptr != ')' && IObuff[len - 1] != TAB)
4026 {
4027 if (IObuff[len - 1] != ' ')
4028 IObuff[len++] = ' ';
4029 // IObuf =~ "\k.* ", thus len >= 2
4030 if (p_js
4031 && (IObuff[len - 2] == '.'
4032 || (vim_strchr(p_cpo, CPO_JOINSP)
4033 == NULL
4034 && (IObuff[len - 2] == '?'
4035 || IObuff[len - 2] == '!'))))
4036 IObuff[len++] = ' ';
4037 }
4038 // copy as much as possible of the new word
4039 if (tmp_ptr - ptr >= IOSIZE - len)
4040 tmp_ptr = ptr + IOSIZE - len - 1;
4041 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4042 len += (int)(tmp_ptr - ptr);
4043 *cont_s_ipos = TRUE;
4044 }
4045 IObuff[len] = NUL;
4046 ptr = IObuff;
4047 }
4048 if (len == compl_length)
4049 return NULL;
4050 }
4051 }
4052
4053 *match_len = len;
4054 return ptr;
4055}
4056
4057/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004058 * Get the next set of words matching "compl_pattern" for default completion(s)
4059 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004060 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4061 * position "st->start_pos" in the "compl_direction" direction. If
4062 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4063 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004064 * Returns OK if a new next match is found, otherwise returns FAIL.
4065 */
4066 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004067get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004068{
4069 int found_new_match = FAIL;
4070 int save_p_scs;
4071 int save_p_ws;
4072 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004073 char_u *ptr = NULL;
4074 int len = 0;
4075 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
4076 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004077
4078 // If 'infercase' is set, don't use 'smartcase' here
4079 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004080 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004081 p_scs = FALSE;
4082
4083 // Buffers other than curbuf are scanned from the beginning or the
4084 // end but never from the middle, thus setting nowrapscan in this
4085 // buffer is a good idea, on the other hand, we always set
4086 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4087 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004088 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004089 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004090 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004091 p_ws = TRUE;
4092 looped_around = FALSE;
4093 for (;;)
4094 {
4095 int cont_s_ipos = FALSE;
4096
4097 ++msg_silent; // Don't want messages for wrapscan.
4098
4099 // ctrl_x_mode_line_or_eval() || word-wise search that
4100 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02004101 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004102 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01004103 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02004104 else if (in_fuzzy)
4105 found_new_match = search_for_fuzzy_match(st->ins_buf,
4106 st->cur_match_pos, leader, compl_direction,
4107 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004108 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004109 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004110 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004111 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004112 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004113 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004114 {
4115 // set "compl_started" even on fail
4116 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004117 st->first_match_pos = *st->cur_match_pos;
4118 st->last_match_pos = *st->cur_match_pos;
4119 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004120 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004121 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4122 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004123 {
4124 found_new_match = FAIL;
4125 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004126 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004127 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4128 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4129 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004130 {
4131 if (looped_around)
4132 found_new_match = FAIL;
4133 else
4134 looped_around = TRUE;
4135 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004136 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004137 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4138 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4139 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004140 {
4141 if (looped_around)
4142 found_new_match = FAIL;
4143 else
4144 looped_around = TRUE;
4145 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004146 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004147 if (found_new_match == FAIL)
4148 break;
4149
4150 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004151 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004152 && start_pos->lnum == st->cur_match_pos->lnum
4153 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004154 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004155
glepnir8159fb12024-07-17 20:32:54 +02004156 if (!in_fuzzy)
4157 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4158 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004159 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004160 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004161
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004162 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004163 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004164 0, cont_s_ipos) != NOTDONE)
4165 {
4166 found_new_match = OK;
4167 break;
4168 }
4169 }
4170 p_scs = save_p_scs;
4171 p_ws = save_p_ws;
4172
4173 return found_new_match;
4174}
4175
4176/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004177 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004178 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004179 */
4180 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004181get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004182{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004183 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004184
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004185 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004186 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004187 case -1:
4188 break;
4189#ifdef FEAT_FIND_ID
4190 case CTRL_X_PATH_PATTERNS:
4191 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004192 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004193 break;
4194#endif
4195
4196 case CTRL_X_DICTIONARY:
4197 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004198 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4199 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004200 break;
4201
4202 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004203 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004204 break;
4205
4206 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004207 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004208 break;
4209
4210 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004211 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004212 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004213 break;
4214
4215#ifdef FEAT_COMPL_FUNC
4216 case CTRL_X_FUNCTION:
4217 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004218 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004219 break;
4220#endif
4221
4222 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004223 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004224 break;
4225
4226 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004227 found_new_match = get_next_default_completion(st, ini);
4228 if (found_new_match == FAIL && st->ins_buf == curbuf)
4229 st->found_all = TRUE;
4230 }
4231
4232 // check if compl_curr_match has changed, (e.g. other type of
4233 // expansion added something)
4234 if (type != 0 && compl_curr_match != compl_old_match)
4235 found_new_match = OK;
4236
4237 return found_new_match;
4238}
4239
4240/*
4241 * Get the next expansion(s), using "compl_pattern".
4242 * The search starts at position "ini" in curbuf and in the direction
4243 * compl_direction.
4244 * When "compl_started" is FALSE start at that position, otherwise continue
4245 * where we stopped searching before.
4246 * This may return before finding all the matches.
4247 * Return the total number of matches or -1 if still unknown -- Acevedo
4248 */
4249 static int
4250ins_compl_get_exp(pos_T *ini)
4251{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004252 static ins_compl_next_state_T st;
4253 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004254 int i;
4255 int found_new_match;
4256 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004257 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004258
4259 if (!compl_started)
4260 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004261 buf_T *buf;
4262
4263 FOR_ALL_BUFFERS(buf)
4264 buf->b_scanned = 0;
4265 if (!st_cleared)
4266 {
4267 CLEAR_FIELD(st);
4268 st_cleared = TRUE;
4269 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004270 st.found_all = FALSE;
4271 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004272 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004273 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004274 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4275 ? (char_u *)"." : curbuf->b_p_cpt);
4276 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004277 st.last_match_pos = st.first_match_pos = *ini;
4278 }
4279 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4280 st.ins_buf = curbuf; // In case the buffer was wiped out.
4281
4282 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004283 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004284 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004285
4286 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4287 for (;;)
4288 {
4289 found_new_match = FAIL;
4290 st.set_match_pos = FALSE;
4291
4292 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4293 // or if found_all says this entry is done. For ^X^L only use the
4294 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004295 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004296 && (!compl_started || st.found_all))
4297 {
glepnir8159fb12024-07-17 20:32:54 +02004298 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004299
4300 if (status == INS_COMPL_CPT_END)
4301 break;
4302 if (status == INS_COMPL_CPT_CONT)
4303 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004304 }
4305
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004306 // If complete() was called then compl_pattern has been reset. The
4307 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004308 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004309 break;
4310
4311 // get the next set of completion matches
4312 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004313
4314 // break the loop for specialized modes (use 'complete' just for the
4315 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4316 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004317 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4318 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004319 {
4320 if (got_int)
4321 break;
4322 // Fill the popup menu as soon as possible.
4323 if (type != -1)
4324 ins_compl_check_keys(0, FALSE);
4325
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004326 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004327 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4328 break;
4329 compl_started = TRUE;
4330 }
4331 else
4332 {
4333 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004334 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004335 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004336
4337 compl_started = FALSE;
4338 }
4339 }
4340 compl_started = TRUE;
4341
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004342 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004343 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004344 found_new_match = FAIL;
4345
4346 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004347 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004348 && !ctrl_x_mode_line_or_eval()))
4349 i = ins_compl_make_cyclic();
4350
4351 if (compl_old_match != NULL)
4352 {
4353 // If several matches were added (FORWARD) or the search failed and has
4354 // just been made cyclic then we have to move compl_curr_match to the
4355 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004356 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004357 : compl_old_match->cp_prev;
4358 if (compl_curr_match == NULL)
4359 compl_curr_match = compl_old_match;
4360 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004361 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004362
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004363 return i;
4364}
4365
4366/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004367 * Update "compl_shown_match" to the actually shown match, it may differ when
4368 * "compl_leader" is used to omit some of the matches.
4369 */
4370 static void
4371ins_compl_update_shown_match(void)
4372{
4373 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004374 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004375 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004376 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004377 compl_shown_match = compl_shown_match->cp_next;
4378
4379 // If we didn't find it searching forward, and compl_shows_dir is
4380 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004381 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004382 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004383 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004384 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004385 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004386 {
4387 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004388 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004389 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004390 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004391 compl_shown_match = compl_shown_match->cp_prev;
4392 }
4393}
4394
4395/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004396 * Delete the old text being completed.
4397 */
4398 void
4399ins_compl_delete(void)
4400{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004401 // In insert mode: Delete the typed part.
4402 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004403 int col = compl_col + (compl_status_adding() ? compl_length : 0);
glepnir76bdb822025-02-08 19:04:51 +01004404 char_u *remaining = NULL;
4405 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01004406 int has_preinsert = ins_compl_preinsert_effect();
4407 if (has_preinsert)
4408 {
glepnirbfb4eea2025-01-31 15:28:29 +01004409 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004410 curwin->w_cursor.col = compl_ins_end_col;
4411 }
4412
glepnir76bdb822025-02-08 19:04:51 +01004413 if (curwin->w_cursor.lnum > compl_lnum)
4414 {
4415 if (curwin->w_cursor.col < ml_get_curline_len())
4416 {
4417 char_u *line = ml_get_curline();
4418 remaining = vim_strnsave(line + curwin->w_cursor.col,
4419 (size_t)STRLEN(line + curwin->w_cursor.col));
4420 if (remaining == NULL)
4421 return;
4422 }
4423 while (curwin->w_cursor.lnum > compl_lnum)
4424 {
4425 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
4426 {
4427 if (remaining)
4428 VIM_CLEAR(remaining);
4429 return;
4430 }
4431 curwin->w_cursor.lnum--;
4432 }
4433 // move cursor to end of line
4434 curwin->w_cursor.col = ml_get_curline_len();
4435 }
4436
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004437 if ((int)curwin->w_cursor.col > col)
4438 {
4439 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01004440 {
4441 if (remaining)
4442 VIM_CLEAR(remaining);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004443 return;
glepnire3647c82025-02-10 21:16:32 +01004444 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004445 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004446 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004447 }
4448
glepnir76bdb822025-02-08 19:04:51 +01004449 if (remaining != NULL)
4450 {
4451 orig_col = curwin->w_cursor.col;
4452 ins_str(remaining);
4453 curwin->w_cursor.col = orig_col;
4454 vim_free(remaining);
4455 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004456 // TODO: is this sufficient for redrawing? Redrawing everything causes
4457 // flicker, thus we can't do that.
4458 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004459#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004460 // clear v:completed_item
4461 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004462#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004463}
4464
4465/*
glepnir76bdb822025-02-08 19:04:51 +01004466 * Insert a completion string that contains newlines.
4467 * The string is split and inserted line by line.
4468 */
4469 static void
4470ins_compl_expand_multiple(char_u *str)
4471{
4472 char_u *start = str;
4473 char_u *curr = str;
4474
4475 while (*curr != NUL)
4476 {
4477 if (*curr == '\n')
4478 {
4479 // Insert the text chunk before newline
4480 if (curr > start)
4481 ins_char_bytes(start, (int)(curr - start));
4482
4483 // Handle newline
4484 open_line(FORWARD, OPENLINE_KEEPTRAIL, FALSE, NULL);
4485 start = curr + 1;
4486 }
4487 curr++;
4488 }
4489
4490 // Handle remaining text after last newline (if any)
4491 if (curr > start)
4492 ins_char_bytes(start, (int)(curr - start));
4493
4494 compl_ins_end_col = curwin->w_cursor.col;
4495}
4496
4497/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004498 * Insert the new text being completed.
4499 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004500 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4501 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004502 */
4503 void
glepniredd4ac32025-01-29 18:53:51 +01004504ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004505{
glepnir76bdb822025-02-08 19:04:51 +01004506 int compl_len = get_compl_len();
4507 int preinsert = ins_compl_has_preinsert();
4508 char_u *cp_str = compl_shown_match->cp_str.string;
4509 size_t cp_str_len = compl_shown_match->cp_str.length;
4510 size_t leader_len = ins_compl_leader_len();
4511 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004512
4513 // Make sure we don't go over the end of the string, this can happen with
4514 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004515 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004516 {
glepnir76bdb822025-02-08 19:04:51 +01004517 if (has_multiple)
4518 ins_compl_expand_multiple(cp_str + compl_len);
4519 else
4520 {
4521 ins_compl_insert_bytes(cp_str + compl_len, -1);
4522 if (preinsert && move_cursor)
4523 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
4524 }
glepniredd4ac32025-01-29 18:53:51 +01004525 }
4526 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004527 compl_used_match = FALSE;
4528 else
4529 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004530#ifdef FEAT_EVAL
4531 {
4532 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4533
4534 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4535 }
4536#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004537 if (!in_compl_func)
4538 compl_curr_match = compl_shown_match;
4539}
4540
4541/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004542 * show the file name for the completion match (if any). Truncate the file
4543 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004544 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004545 static void
4546ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004547{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004548 char *lead = _("match in file");
4549 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4550 char_u *s;
4551 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004552
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004553 if (space <= 0)
4554 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004555
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004556 // We need the tail that fits. With double-byte encoding going
4557 // back from the end is very slow, thus go from the start and keep
4558 // the text that fits in "space" between "s" and "e".
4559 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004560 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004561 space -= ptr2cells(e);
4562 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004563 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004564 space += ptr2cells(s);
4565 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004566 }
4567 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004568 msg_hist_off = TRUE;
4569 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4570 s > compl_shown_match->cp_fname ? "<" : "", s);
4571 msg((char *)IObuff);
4572 msg_hist_off = FALSE;
4573 redraw_cmdline = FALSE; // don't overwrite!
4574}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004575
glepnirdca57fb2024-06-04 22:01:21 +02004576/*
zeertzjq551d8c32024-06-05 19:53:32 +02004577 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004578 */
glepnira218cc62024-06-03 19:32:39 +02004579 static compl_T *
4580find_comp_when_fuzzy(void)
4581{
4582 int score;
4583 char_u* str;
4584 int target_idx = -1;
4585 int is_forward = compl_shows_dir_forward();
4586 int is_backward = compl_shows_dir_backward();
4587 compl_T *comp = NULL;
4588
glepnir43eef882024-06-19 20:20:48 +02004589 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004590 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004591 return compl_first_match != compl_shown_match ? compl_first_match :
4592 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004593
4594 if (is_forward)
4595 target_idx = compl_selected_item + 1;
4596 else if (is_backward)
4597 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004598 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004599
4600 score = compl_match_array[target_idx].pum_score;
4601 str = compl_match_array[target_idx].pum_text;
4602
4603 comp = compl_first_match;
4604 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004605 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004606 return comp;
4607 comp = comp->cp_next;
4608 } while (comp != NULL && !is_first_match(comp));
4609
4610 return NULL;
4611}
4612
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004613/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004614 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004615 * times. The number of matches found is returned in 'num_matches'.
4616 *
4617 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4618 * get more completions. If it is FALSE, then do nothing when there are no more
4619 * completions in the given direction.
4620 *
4621 * If "advance" is TRUE, then completion will move to the first match.
4622 * Otherwise, the original text will be shown.
4623 *
4624 * Returns OK on success and -1 if the number of matches are unknown.
4625 */
4626 static int
4627find_next_completion_match(
4628 int allow_get_expansion,
4629 int todo, // repeat completion this many times
4630 int advance,
4631 int *num_matches)
4632{
4633 int found_end = FALSE;
4634 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004635 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004636 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4637 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004638
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004639 while (--todo >= 0)
4640 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004641 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004642 {
glepniraced8c22024-06-15 15:13:05 +02004643 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4644 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004645 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004646 && (is_first_match(compl_shown_match->cp_next)
4647 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004648 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004649 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004650 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004651 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004652 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004653 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4654 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004655 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004656 }
4657 else
4658 {
4659 if (!allow_get_expansion)
4660 {
4661 if (advance)
4662 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004663 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004664 compl_pending -= todo + 1;
4665 else
4666 compl_pending += todo + 1;
4667 }
4668 return -1;
4669 }
4670
4671 if (!compl_no_select && advance)
4672 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004673 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004674 --compl_pending;
4675 else
4676 ++compl_pending;
4677 }
4678
4679 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004680 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004681
4682 // handle any pending completions
4683 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004684 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004685 {
4686 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4687 {
4688 compl_shown_match = compl_shown_match->cp_next;
4689 --compl_pending;
4690 }
4691 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4692 {
4693 compl_shown_match = compl_shown_match->cp_prev;
4694 ++compl_pending;
4695 }
4696 else
4697 break;
4698 }
4699 found_end = FALSE;
4700 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004701 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004702 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004703 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004704 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004705 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004706 ++todo;
4707 else
4708 // Remember a matching item.
4709 found_compl = compl_shown_match;
4710
4711 // Stop at the end of the list when we found a usable match.
4712 if (found_end)
4713 {
4714 if (found_compl != NULL)
4715 {
4716 compl_shown_match = found_compl;
4717 break;
4718 }
4719 todo = 1; // use first usable match after wrapping around
4720 }
4721 }
4722
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004723 return OK;
4724}
4725
4726/*
4727 * Fill in the next completion in the current direction.
4728 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4729 * get more completions. If it is FALSE, then we just do nothing when there
4730 * are no more completions in a given direction. The latter case is used when
4731 * we are still in the middle of finding completions, to allow browsing
4732 * through the ones found so far.
4733 * Return the total number of matches, or -1 if still unknown -- webb.
4734 *
4735 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4736 * compl_shown_match here.
4737 *
4738 * Note that this function may be called recursively once only. First with
4739 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4740 * calls this function with "allow_get_expansion" FALSE.
4741 */
4742 static int
4743ins_compl_next(
4744 int allow_get_expansion,
4745 int count, // repeat completion this many times; should
4746 // be at least 1
4747 int insert_match, // Insert the newly selected match
4748 int in_compl_func) // called from complete_check()
4749{
4750 int num_matches = -1;
4751 int todo = count;
4752 int advance;
4753 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004754 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004755 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004756 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4757 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004758 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004759
4760 // When user complete function return -1 for findstart which is next
4761 // time of 'always', compl_shown_match become NULL.
4762 if (compl_shown_match == NULL)
4763 return -1;
4764
John Marriott5e6ea922024-11-23 14:01:57 +01004765 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004766 && !match_at_original_text(compl_shown_match)
4767 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004768 // Update "compl_shown_match" to the actually shown match
4769 ins_compl_update_shown_match();
4770
4771 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004772 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004773 // Delete old text to be replaced
4774 ins_compl_delete();
4775
4776 // When finding the longest common text we stick at the original text,
4777 // don't let CTRL-N or CTRL-P move to the first match.
4778 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4779
4780 // When restarting the search don't insert the first match either.
4781 if (compl_restarting)
4782 {
4783 advance = FALSE;
4784 compl_restarting = FALSE;
4785 }
4786
4787 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4788 // around.
4789 if (find_next_completion_match(allow_get_expansion, todo, advance,
4790 &num_matches) == -1)
4791 return -1;
4792
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004793 if (curbuf != orig_curbuf)
4794 {
4795 // In case some completion function switched buffer, don't want to
4796 // insert the completion elsewhere.
4797 return -1;
4798 }
4799
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004800 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01004801 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004802 {
glepnir6a38aff2024-12-16 21:56:16 +01004803 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004804 compl_used_match = FALSE;
4805 }
4806 else if (insert_match)
4807 {
4808 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01004809 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004810 else
glepnir6a38aff2024-12-16 21:56:16 +01004811 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004812 }
4813 else
4814 compl_used_match = FALSE;
4815
4816 if (!allow_get_expansion)
4817 {
4818 // may undisplay the popup menu first
4819 ins_compl_upd_pum();
4820
4821 if (pum_enough_matches())
4822 // Will display the popup menu, don't redraw yet to avoid flicker.
4823 pum_call_update_screen();
4824 else
4825 // Not showing the popup menu yet, redraw to show the user what was
4826 // inserted.
4827 update_screen(0);
4828
4829 // display the updated popup menu
4830 ins_compl_show_pum();
4831#ifdef FEAT_GUI
4832 if (gui.in_use)
4833 {
4834 // Show the cursor after the match, not after the redrawn text.
4835 setcursor();
4836 out_flush_cursor(FALSE, FALSE);
4837 }
4838#endif
4839
4840 // Delete old text to be replaced, since we're still searching and
4841 // don't want to match ourselves!
4842 ins_compl_delete();
4843 }
4844
4845 // Enter will select a match when the match wasn't inserted and the popup
4846 // menu is visible.
4847 if (compl_no_insert && !started)
4848 compl_enter_selects = TRUE;
4849 else
4850 compl_enter_selects = !insert_match && compl_match_array != NULL;
4851
4852 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004853 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004854 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004855
4856 return num_matches;
4857}
4858
4859/*
4860 * Call this while finding completions, to check whether the user has hit a key
4861 * that should change the currently displayed completion, or exit completion
4862 * mode. Also, when compl_pending is not zero, show a completion as soon as
4863 * possible. -- webb
4864 * "frequency" specifies out of how many calls we actually check.
4865 * "in_compl_func" is TRUE when called from complete_check(), don't set
4866 * compl_curr_match.
4867 */
4868 void
4869ins_compl_check_keys(int frequency, int in_compl_func)
4870{
4871 static int count = 0;
4872 int c;
4873
4874 // Don't check when reading keys from a script, :normal or feedkeys().
4875 // That would break the test scripts. But do check for keys when called
4876 // from complete_check().
4877 if (!in_compl_func && (using_script() || ex_normal_busy))
4878 return;
4879
4880 // Only do this at regular intervals
4881 if (++count < frequency)
4882 return;
4883 count = 0;
4884
4885 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4886 // can't do its work correctly.
4887 c = vpeekc_any();
4888 if (c != NUL)
4889 {
4890 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4891 {
4892 c = safe_vgetc(); // Eat the character
4893 compl_shows_dir = ins_compl_key2dir(c);
4894 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4895 c != K_UP && c != K_DOWN, in_compl_func);
4896 }
4897 else
4898 {
4899 // Need to get the character to have KeyTyped set. We'll put it
4900 // back with vungetc() below. But skip K_IGNORE.
4901 c = safe_vgetc();
4902 if (c != K_IGNORE)
4903 {
4904 // Don't interrupt completion when the character wasn't typed,
4905 // e.g., when doing @q to replay keys.
4906 if (c != Ctrl_R && KeyTyped)
4907 compl_interrupted = TRUE;
4908
4909 vungetc(c);
4910 }
4911 }
4912 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004913 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004914 {
4915 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4916
4917 compl_pending = 0;
4918 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4919 }
4920}
4921
4922/*
4923 * Decide the direction of Insert mode complete from the key typed.
4924 * Returns BACKWARD or FORWARD.
4925 */
4926 static int
4927ins_compl_key2dir(int c)
4928{
4929 if (c == Ctrl_P || c == Ctrl_L
4930 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4931 return BACKWARD;
4932 return FORWARD;
4933}
4934
4935/*
4936 * Return TRUE for keys that are used for completion only when the popup menu
4937 * is visible.
4938 */
4939 static int
4940ins_compl_pum_key(int c)
4941{
4942 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4943 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4944 || c == K_UP || c == K_DOWN);
4945}
4946
4947/*
4948 * Decide the number of completions to move forward.
4949 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4950 */
4951 static int
4952ins_compl_key2count(int c)
4953{
4954 int h;
4955
4956 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4957 {
4958 h = pum_get_height();
4959 if (h > 3)
4960 h -= 2; // keep some context
4961 return h;
4962 }
4963 return 1;
4964}
4965
4966/*
4967 * Return TRUE if completion with "c" should insert the match, FALSE if only
4968 * to change the currently selected completion.
4969 */
4970 static int
4971ins_compl_use_match(int c)
4972{
4973 switch (c)
4974 {
4975 case K_UP:
4976 case K_DOWN:
4977 case K_PAGEDOWN:
4978 case K_KPAGEDOWN:
4979 case K_S_DOWN:
4980 case K_PAGEUP:
4981 case K_KPAGEUP:
4982 case K_S_UP:
4983 return FALSE;
4984 }
4985 return TRUE;
4986}
4987
4988/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004989 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4990 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004991 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004992 * Uses the global variables: compl_cont_status and ctrl_x_mode
4993 */
4994 static int
4995get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4996{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004997 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004998 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004999 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005000 {
5001 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5002 ;
5003 compl_col += ++startcol;
5004 compl_length = curs_col - startcol;
5005 }
5006 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005007 {
John Marriott5e6ea922024-11-23 14:01:57 +01005008 compl_pattern.string = str_foldcase(line + compl_col,
5009 compl_length, NULL, 0);
5010 if (compl_pattern.string == NULL)
5011 {
5012 compl_pattern.length = 0;
5013 return FAIL;
5014 }
5015 compl_pattern.length = STRLEN(compl_pattern.string);
5016 }
5017 else
5018 {
5019 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5020 if (compl_pattern.string == NULL)
5021 {
5022 compl_pattern.length = 0;
5023 return FAIL;
5024 }
5025 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005026 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005027 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005028 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005029 {
5030 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005031 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005032 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005033
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005034 if (!vim_iswordp(line + compl_col)
5035 || (compl_col > 0
5036 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005037 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005038 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005039 prefixlen = 0;
5040 }
John Marriott5e6ea922024-11-23 14:01:57 +01005041
5042 // we need up to 2 extra chars for the prefix
5043 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5044 compl_pattern.string = alloc(n);
5045 if (compl_pattern.string == NULL)
5046 {
5047 compl_pattern.length = 0;
5048 return FAIL;
5049 }
5050 STRCPY((char *)compl_pattern.string, prefix);
5051 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005052 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005053 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005054 }
5055 else if (--startcol < 0
5056 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5057 {
John Marriott5e6ea922024-11-23 14:01:57 +01005058 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5059
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005060 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005061 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5062 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005063 {
John Marriott5e6ea922024-11-23 14:01:57 +01005064 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005065 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005066 }
John Marriott5e6ea922024-11-23 14:01:57 +01005067 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005068 compl_col += curs_col;
5069 compl_length = 0;
5070 }
5071 else
5072 {
5073 // Search the point of change class of multibyte character
5074 // or not a word single byte character backward.
5075 if (has_mbyte)
5076 {
5077 int base_class;
5078 int head_off;
5079
5080 startcol -= (*mb_head_off)(line, line + startcol);
5081 base_class = mb_get_class(line + startcol);
5082 while (--startcol >= 0)
5083 {
5084 head_off = (*mb_head_off)(line, line + startcol);
5085 if (base_class != mb_get_class(line + startcol
5086 - head_off))
5087 break;
5088 startcol -= head_off;
5089 }
5090 }
5091 else
5092 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5093 ;
5094 compl_col += ++startcol;
5095 compl_length = (int)curs_col - startcol;
5096 if (compl_length == 1)
5097 {
5098 // Only match word with at least two chars -- webb
5099 // there's no need to call quote_meta,
5100 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005101 compl_pattern.string = alloc(7);
5102 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005103 {
John Marriott5e6ea922024-11-23 14:01:57 +01005104 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005105 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005106 }
John Marriott5e6ea922024-11-23 14:01:57 +01005107 STRCPY((char *)compl_pattern.string, "\\<");
5108 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5109 STRCAT((char *)compl_pattern.string, "\\k");
5110 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005111 }
5112 else
5113 {
John Marriott5e6ea922024-11-23 14:01:57 +01005114 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5115
5116 compl_pattern.string = alloc(n);
5117 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005118 {
John Marriott5e6ea922024-11-23 14:01:57 +01005119 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005120 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005121 }
John Marriott5e6ea922024-11-23 14:01:57 +01005122 STRCPY((char *)compl_pattern.string, "\\<");
5123 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005124 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005125 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005126 }
5127 }
5128
5129 return OK;
5130}
5131
5132/*
5133 * Get the pattern, column and length for whole line completion or for the
5134 * complete() function.
5135 * Sets the global variables: compl_col, compl_length and compl_pattern.
5136 */
5137 static int
5138get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5139{
5140 compl_col = (colnr_T)getwhitecols(line);
5141 compl_length = (int)curs_col - (int)compl_col;
5142 if (compl_length < 0) // cursor in indent: empty pattern
5143 compl_length = 0;
5144 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005145 {
John Marriott5e6ea922024-11-23 14:01:57 +01005146 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5147 NULL, 0);
5148 if (compl_pattern.string == NULL)
5149 {
5150 compl_pattern.length = 0;
5151 return FAIL;
5152 }
5153 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005154 }
John Marriott5e6ea922024-11-23 14:01:57 +01005155 else
5156 {
5157 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5158 if (compl_pattern.string == NULL)
5159 {
5160 compl_pattern.length = 0;
5161 return FAIL;
5162 }
5163 compl_pattern.length = (size_t)compl_length;
5164 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005165
5166 return OK;
5167}
5168
5169/*
5170 * Get the pattern, column and length for filename completion.
5171 * Sets the global variables: compl_col, compl_length and compl_pattern.
5172 */
5173 static int
5174get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5175{
5176 // Go back to just before the first filename character.
5177 if (startcol > 0)
5178 {
5179 char_u *p = line + startcol;
5180
5181 MB_PTR_BACK(line, p);
5182 while (p > line && vim_isfilec(PTR2CHAR(p)))
5183 MB_PTR_BACK(line, p);
5184 if (p == line && vim_isfilec(PTR2CHAR(p)))
5185 startcol = 0;
5186 else
5187 startcol = (int)(p - line) + 1;
5188 }
5189
5190 compl_col += startcol;
5191 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005192 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5193 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005194 {
John Marriott5e6ea922024-11-23 14:01:57 +01005195 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005196 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005197 }
5198
John Marriott5e6ea922024-11-23 14:01:57 +01005199 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005200
5201 return OK;
5202}
5203
5204/*
5205 * Get the pattern, column and length for command-line completion.
5206 * Sets the global variables: compl_col, compl_length and compl_pattern.
5207 */
5208 static int
5209get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5210{
John Marriott5e6ea922024-11-23 14:01:57 +01005211 compl_pattern.string = vim_strnsave(line, curs_col);
5212 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005213 {
John Marriott5e6ea922024-11-23 14:01:57 +01005214 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005215 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005216 }
John Marriott5e6ea922024-11-23 14:01:57 +01005217 compl_pattern.length = curs_col;
5218 set_cmd_context(&compl_xp, compl_pattern.string,
5219 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005220 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5221 || compl_xp.xp_context == EXPAND_NOTHING)
5222 // No completion possible, use an empty pattern to get a
5223 // "pattern not found" message.
5224 compl_col = curs_col;
5225 else
John Marriott5e6ea922024-11-23 14:01:57 +01005226 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005227 compl_length = curs_col - compl_col;
5228
5229 return OK;
5230}
5231
5232/*
5233 * Get the pattern, column and length for user defined completion ('omnifunc',
5234 * 'completefunc' and 'thesaurusfunc')
5235 * Sets the global variables: compl_col, compl_length and compl_pattern.
5236 * Uses the global variable: spell_bad_len
5237 */
5238 static int
5239get_userdefined_compl_info(colnr_T curs_col UNUSED)
5240{
5241 int ret = FAIL;
5242
5243#ifdef FEAT_COMPL_FUNC
5244 // Call user defined function 'completefunc' with "a:findstart"
5245 // set to 1 to obtain the length of text to use for completion.
5246 char_u *line;
5247 typval_T args[3];
5248 int col;
5249 char_u *funcname;
5250 pos_T pos;
5251 int save_State = State;
5252 callback_T *cb;
5253
5254 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5255 // length as a string
5256 funcname = get_complete_funcname(ctrl_x_mode);
5257 if (*funcname == NUL)
5258 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005259 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005260 ? "completefunc" : "omnifunc");
5261 return FAIL;
5262 }
5263
5264 args[0].v_type = VAR_NUMBER;
5265 args[0].vval.v_number = 1;
5266 args[1].v_type = VAR_STRING;
5267 args[1].vval.v_string = (char_u *)"";
5268 args[2].v_type = VAR_UNKNOWN;
5269 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005270 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005271 cb = get_insert_callback(ctrl_x_mode);
5272 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005273 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005274
5275 State = save_State;
5276 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005277 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005278 validate_cursor();
5279 if (!EQUAL_POS(curwin->w_cursor, pos))
5280 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005281 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005282 return FAIL;
5283 }
5284
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005285 // Return value -2 means the user complete function wants to cancel the
5286 // complete without an error, do the same if the function did not execute
5287 // successfully.
5288 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005289 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005290 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005291 if (col == -3)
5292 {
5293 ctrl_x_mode = CTRL_X_NORMAL;
5294 edit_submode = NULL;
5295 if (!shortmess(SHM_COMPLETIONMENU))
5296 msg_clr_cmdline();
5297 return FAIL;
5298 }
5299
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005300 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005301 // completion.
5302 compl_opt_refresh_always = FALSE;
5303 compl_opt_suppress_empty = FALSE;
5304
5305 if (col < 0)
5306 col = curs_col;
5307 compl_col = col;
5308 if (compl_col > curs_col)
5309 compl_col = curs_col;
5310
5311 // Setup variables for completion. Need to obtain "line" again,
5312 // it may have become invalid.
5313 line = ml_get(curwin->w_cursor.lnum);
5314 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005315 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5316 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005317 {
John Marriott5e6ea922024-11-23 14:01:57 +01005318 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005319 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005320 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005321
John Marriott5e6ea922024-11-23 14:01:57 +01005322 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005323 ret = OK;
5324#endif
5325
5326 return ret;
5327}
5328
5329/*
5330 * Get the pattern, column and length for spell completion.
5331 * Sets the global variables: compl_col, compl_length and compl_pattern.
5332 * Uses the global variable: spell_bad_len
5333 */
5334 static int
5335get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5336{
5337 int ret = FAIL;
5338#ifdef FEAT_SPELL
5339 char_u *line;
5340
5341 if (spell_bad_len > 0)
5342 compl_col = curs_col - spell_bad_len;
5343 else
5344 compl_col = spell_word_start(startcol);
5345 if (compl_col >= (colnr_T)startcol)
5346 {
5347 compl_length = 0;
5348 compl_col = curs_col;
5349 }
5350 else
5351 {
5352 spell_expand_check_cap(compl_col);
5353 compl_length = (int)curs_col - compl_col;
5354 }
5355 // Need to obtain "line" again, it may have become invalid.
5356 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005357 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5358 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005359 {
John Marriott5e6ea922024-11-23 14:01:57 +01005360 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005361 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005362 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005363
John Marriott5e6ea922024-11-23 14:01:57 +01005364 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005365 ret = OK;
5366#endif
5367
5368 return ret;
5369}
5370
5371/*
5372 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005373 * "startcol" - start column number of the completion pattern/text
5374 * "cur_col" - current cursor column
5375 * On return, "line_invalid" is set to TRUE, if the current line may have
5376 * become invalid and needs to be fetched again.
5377 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005378 */
5379 static int
5380compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5381{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005382 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005383 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5384 && !thesaurus_func_complete(ctrl_x_mode)))
5385 {
5386 return get_normal_compl_info(line, startcol, curs_col);
5387 }
5388 else if (ctrl_x_mode_line_or_eval())
5389 {
5390 return get_wholeline_compl_info(line, curs_col);
5391 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005392 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005393 {
5394 return get_filename_compl_info(line, startcol, curs_col);
5395 }
5396 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5397 {
5398 return get_cmdline_compl_info(line, curs_col);
5399 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005400 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005401 || thesaurus_func_complete(ctrl_x_mode))
5402 {
5403 if (get_userdefined_compl_info(curs_col) == FAIL)
5404 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005405 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005406 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005407 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005408 {
5409 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5410 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005411 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005412 }
5413 else
5414 {
5415 internal_error("ins_complete()");
5416 return FAIL;
5417 }
5418
5419 return OK;
5420}
5421
5422/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005423 * Continue an interrupted completion mode search in "line".
5424 *
5425 * If this same ctrl_x_mode has been interrupted use the text from
5426 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5427 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5428 * the same line as the cursor then fix it (the line has been split because it
5429 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5430 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005431 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005432 static void
5433ins_compl_continue_search(char_u *line)
5434{
5435 // it is a continued search
5436 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005437 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5438 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005439 {
5440 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5441 {
5442 // line (probably) wrapped, set compl_startpos to the
5443 // first non_blank in the line, if it is not a wordchar
5444 // include it to get a better pattern, but then we don't
5445 // want the "\\<" prefix, check it below
5446 compl_col = (colnr_T)getwhitecols(line);
5447 compl_startpos.col = compl_col;
5448 compl_startpos.lnum = curwin->w_cursor.lnum;
5449 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5450 }
5451 else
5452 {
5453 // S_IPOS was set when we inserted a word that was at the
5454 // beginning of the line, which means that we'll go to SOL
5455 // mode but first we need to redefine compl_startpos
5456 if (compl_cont_status & CONT_S_IPOS)
5457 {
5458 compl_cont_status |= CONT_SOL;
5459 compl_startpos.col = (colnr_T)(skipwhite(
5460 line + compl_length
5461 + compl_startpos.col) - line);
5462 }
5463 compl_col = compl_startpos.col;
5464 }
5465 compl_length = curwin->w_cursor.col - (int)compl_col;
5466 // IObuff is used to add a "word from the next line" would we
5467 // have enough space? just being paranoid
5468#define MIN_SPACE 75
5469 if (compl_length > (IOSIZE - MIN_SPACE))
5470 {
5471 compl_cont_status &= ~CONT_SOL;
5472 compl_length = (IOSIZE - MIN_SPACE);
5473 compl_col = curwin->w_cursor.col - compl_length;
5474 }
5475 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5476 if (compl_length < 1)
5477 compl_cont_status &= CONT_LOCAL;
5478 }
5479 else if (ctrl_x_mode_line_or_eval())
5480 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5481 else
5482 compl_cont_status = 0;
5483}
5484
5485/*
5486 * start insert mode completion
5487 */
5488 static int
5489ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005490{
5491 char_u *line;
5492 int startcol = 0; // column where searched text starts
5493 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005494 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005495 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005496 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005497
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005498 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005499
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005500 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005501 did_si = FALSE;
5502 can_si = FALSE;
5503 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005504 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005505 return FAIL;
5506
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005507 line = ml_get(curwin->w_cursor.lnum);
5508 curs_col = curwin->w_cursor.col;
5509 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01005510 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005511
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005512 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5513 && compl_cont_mode == ctrl_x_mode)
5514 // this same ctrl-x_mode was interrupted previously. Continue the
5515 // completion.
5516 ins_compl_continue_search(line);
5517 else
5518 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005519
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005520 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005521 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005522 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005523 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005524 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5525 compl_cont_status = 0;
5526 compl_cont_status |= CONT_N_ADDS;
5527 compl_startpos = curwin->w_cursor;
5528 startcol = (int)curs_col;
5529 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005530 }
5531
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005532 // Work out completion pattern and original text -- webb
5533 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5534 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005535 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5536 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005537 // restore did_ai, so that adding comment leader works
5538 did_ai = save_did_ai;
5539 return FAIL;
5540 }
5541 // If "line" was changed while getting completion info get it again.
5542 if (line_invalid)
5543 line = ml_get(curwin->w_cursor.lnum);
5544
glepnir7cfe6932024-09-15 20:06:28 +02005545 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005546 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005547 {
5548 edit_submode_pre = (char_u *)_(" Adding");
5549 if (ctrl_x_mode_line_or_eval())
5550 {
5551 // Insert a new line, keep indentation but ignore 'comments'.
5552 char_u *old = curbuf->b_p_com;
5553
5554 curbuf->b_p_com = (char_u *)"";
5555 compl_startpos.lnum = curwin->w_cursor.lnum;
5556 compl_startpos.col = compl_col;
5557 ins_eol('\r');
5558 curbuf->b_p_com = old;
5559 compl_length = 0;
5560 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01005561 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005562 }
glepnir7cfe6932024-09-15 20:06:28 +02005563 else if (ctrl_x_mode_normal() && in_fuzzy)
5564 {
5565 compl_startpos = curwin->w_cursor;
5566 compl_cont_status &= CONT_S_IPOS;
5567 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005568 }
5569 else
5570 {
5571 edit_submode_pre = NULL;
5572 compl_startpos.col = compl_col;
5573 }
5574
5575 if (compl_cont_status & CONT_LOCAL)
5576 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5577 else
5578 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5579
5580 // If any of the original typed text has been changed we need to fix
5581 // the redo buffer.
5582 ins_compl_fixRedoBufForLeader(NULL);
5583
5584 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005585 VIM_CLEAR_STRING(compl_orig_text);
5586 compl_orig_text.length = (size_t)compl_length;
5587 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005588 if (p_ic)
5589 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005590 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5591 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005592 {
John Marriott5e6ea922024-11-23 14:01:57 +01005593 VIM_CLEAR_STRING(compl_pattern);
5594 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005595 return FAIL;
5596 }
5597
5598 // showmode might reset the internal line pointers, so it must
5599 // be called before line = ml_get(), or when this address is no
5600 // longer needed. -- Acevedo.
5601 edit_submode_extra = (char_u *)_("-- Searching...");
5602 edit_submode_highl = HLF_COUNT;
5603 showmode();
5604 edit_submode_extra = NULL;
5605 out_flush();
5606
5607 return OK;
5608}
5609
5610/*
5611 * display the completion status message
5612 */
5613 static void
5614ins_compl_show_statusmsg(void)
5615{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005616 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005617 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005618 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005619 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005620 ? (char_u *)_("Hit end of paragraph")
5621 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005622 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005623 }
5624
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005625 if (edit_submode_extra == NULL)
5626 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005627 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005628 {
5629 edit_submode_extra = (char_u *)_("Back at original");
5630 edit_submode_highl = HLF_W;
5631 }
5632 else if (compl_cont_status & CONT_S_IPOS)
5633 {
5634 edit_submode_extra = (char_u *)_("Word from other line");
5635 edit_submode_highl = HLF_COUNT;
5636 }
5637 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5638 {
5639 edit_submode_extra = (char_u *)_("The only match");
5640 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005641 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005642 }
5643 else
5644 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005645#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005646 // Update completion sequence number when needed.
5647 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005648 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005649#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005650 // The match should always have a sequence number now, this is
5651 // just a safety check.
5652 if (compl_curr_match->cp_number != -1)
5653 {
5654 // Space for 10 text chars. + 2x10-digit no.s = 31.
5655 // Translations may need more than twice that.
5656 static char_u match_ref[81];
5657
5658 if (compl_matches > 0)
5659 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005660 _("match %d of %d"),
5661 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005662 else
5663 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005664 _("match %d"),
5665 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005666 edit_submode_extra = match_ref;
5667 edit_submode_highl = HLF_R;
5668 if (dollar_vcol >= 0)
5669 curs_columns(FALSE);
5670 }
5671 }
5672 }
5673
5674 // Show a message about what (completion) mode we're in.
5675 if (!compl_opt_suppress_empty)
5676 {
5677 showmode();
5678 if (!shortmess(SHM_COMPLETIONMENU))
5679 {
5680 if (edit_submode_extra != NULL)
5681 {
5682 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005683 {
5684 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005685 msg_attr((char *)edit_submode_extra,
5686 edit_submode_highl < HLF_COUNT
5687 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005688 msg_hist_off = FALSE;
5689 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005690 }
5691 else
5692 msg_clr_cmdline(); // necessary for "noshowmode"
5693 }
5694 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005695}
5696
5697/*
5698 * Do Insert mode completion.
5699 * Called when character "c" was typed, which has a meaning for completion.
5700 * Returns OK if completion was done, FAIL if something failed (out of mem).
5701 */
5702 int
5703ins_complete(int c, int enable_pum)
5704{
5705 int n;
5706 int save_w_wrow;
5707 int save_w_leftcol;
5708 int insert_match;
5709
5710 compl_direction = ins_compl_key2dir(c);
5711 insert_match = ins_compl_use_match(c);
5712
5713 if (!compl_started)
5714 {
5715 if (ins_compl_start() == FAIL)
5716 return FAIL;
5717 }
5718 else if (insert_match && stop_arrow() == FAIL)
5719 return FAIL;
5720
5721 compl_shown_match = compl_curr_match;
5722 compl_shows_dir = compl_direction;
5723
5724 // Find next match (and following matches).
5725 save_w_wrow = curwin->w_wrow;
5726 save_w_leftcol = curwin->w_leftcol;
5727 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5728
5729 // may undisplay the popup menu
5730 ins_compl_upd_pum();
5731
5732 if (n > 1) // all matches have been found
5733 compl_matches = n;
5734 compl_curr_match = compl_shown_match;
5735 compl_direction = compl_shows_dir;
5736
5737 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5738 // mode.
5739 if (got_int && !global_busy)
5740 {
5741 (void)vgetc();
5742 got_int = FALSE;
5743 }
5744
5745 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005746 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005747 {
5748 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5749 // because we couldn't expand anything at first place, but if we used
5750 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5751 // (such as M in M'exico) if not tried already. -- Acevedo
5752 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005753 || compl_status_adding()
5754 || (ctrl_x_mode_not_default()
5755 && !ctrl_x_mode_path_patterns()
5756 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005757 compl_cont_status &= ~CONT_N_ADDS;
5758 }
5759
5760 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5761 compl_cont_status |= CONT_S_IPOS;
5762 else
5763 compl_cont_status &= ~CONT_S_IPOS;
5764
5765 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005766
5767 // Show the popup menu, unless we got interrupted.
5768 if (enable_pum && !compl_interrupted)
5769 show_pum(save_w_wrow, save_w_leftcol);
5770
5771 compl_was_interrupted = compl_interrupted;
5772 compl_interrupted = FALSE;
5773
5774 return OK;
5775}
5776
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005777/*
5778 * Remove (if needed) and show the popup menu
5779 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005780 static void
5781show_pum(int prev_w_wrow, int prev_w_leftcol)
5782{
5783 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005784 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005785 RedrawingDisabled = 0;
5786
5787 // If the cursor moved or the display scrolled we need to remove the pum
5788 // first.
5789 setcursor();
5790 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5791 ins_compl_del_pum();
5792
5793 ins_compl_show_pum();
5794 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005795
5796 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005797}
5798
5799/*
5800 * Looks in the first "len" chars. of "src" for search-metachars.
5801 * If dest is not NULL the chars. are copied there quoting (with
5802 * a backslash) the metachars, and dest would be NUL terminated.
5803 * Returns the length (needed) of dest
5804 */
5805 static unsigned
5806quote_meta(char_u *dest, char_u *src, int len)
5807{
5808 unsigned m = (unsigned)len + 1; // one extra for the NUL
5809
5810 for ( ; --len >= 0; src++)
5811 {
5812 switch (*src)
5813 {
5814 case '.':
5815 case '*':
5816 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005817 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005818 break;
5819 // FALLTHROUGH
5820 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005821 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005822 break;
5823 // FALLTHROUGH
5824 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005825 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005826 break;
5827 // FALLTHROUGH
5828 case '^': // currently it's not needed.
5829 case '$':
5830 m++;
5831 if (dest != NULL)
5832 *dest++ = '\\';
5833 break;
5834 }
5835 if (dest != NULL)
5836 *dest++ = *src;
5837 // Copy remaining bytes of a multibyte character.
5838 if (has_mbyte)
5839 {
5840 int i, mb_len;
5841
5842 mb_len = (*mb_ptr2len)(src) - 1;
5843 if (mb_len > 0 && len >= mb_len)
5844 for (i = 0; i < mb_len; ++i)
5845 {
5846 --len;
5847 ++src;
5848 if (dest != NULL)
5849 *dest++ = *src;
5850 }
5851 }
5852 }
5853 if (dest != NULL)
5854 *dest = NUL;
5855
5856 return m;
5857}
5858
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005859#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005860 void
5861free_insexpand_stuff(void)
5862{
John Marriott5e6ea922024-11-23 14:01:57 +01005863 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005864# ifdef FEAT_EVAL
5865 free_callback(&cfu_cb);
5866 free_callback(&ofu_cb);
5867 free_callback(&tsrfu_cb);
5868# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005869}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005870#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005871
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005872#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005873/*
5874 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5875 * spelled word, if there is one.
5876 */
5877 static void
5878spell_back_to_badword(void)
5879{
5880 pos_T tpos = curwin->w_cursor;
5881
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005882 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005883 if (curwin->w_cursor.col != tpos.col)
5884 start_arrow(&tpos);
5885}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005886#endif