blob: deae1ce346588d65d4cb95c157e627f1439b420a [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
glepnir3af0a8d2025-02-20 22:06:16 +01001371 if (!shown_match_ok && compl == compl_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001372 {
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;
glepnir07f0dbe2025-02-18 20:27:30 +01001892 compl_selected_item = -1;
glepnir6a38aff2024-12-16 21:56:16 +01001893 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001894 VIM_CLEAR_STRING(compl_pattern);
1895 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001896 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001897 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001898 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001899#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001900 // clear v:completed_item
1901 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001902#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001903}
1904
1905/*
1906 * Return TRUE when Insert completion is active.
1907 */
1908 int
1909ins_compl_active(void)
1910{
1911 return compl_started;
1912}
1913
1914/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001915 * Return True when wp is the actual completion window
1916 */
1917 int
1918ins_compl_win_active(win_T *wp UNUSED)
1919{
1920 return ins_compl_active()
1921#if defined(FEAT_QUICKFIX)
1922 && (!wp->w_p_pvw
1923# ifdef FEAT_PROP_POPUP
1924 && !(wp->w_popup_flags & POPF_INFO)
1925# endif
1926 )
1927#endif
1928 ;
1929}
1930
1931/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001932 * Selected one of the matches. When FALSE the match was edited or using the
1933 * longest common string.
1934 */
1935 int
1936ins_compl_used_match(void)
1937{
1938 return compl_used_match;
1939}
1940
1941/*
1942 * Initialize get longest common string.
1943 */
1944 void
1945ins_compl_init_get_longest(void)
1946{
1947 compl_get_longest = FALSE;
1948}
1949
1950/*
1951 * Returns TRUE when insert completion is interrupted.
1952 */
1953 int
1954ins_compl_interrupted(void)
1955{
1956 return compl_interrupted;
1957}
1958
1959/*
1960 * Returns TRUE if the <Enter> key selects a match in the completion popup
1961 * menu.
1962 */
1963 int
1964ins_compl_enter_selects(void)
1965{
1966 return compl_enter_selects;
1967}
1968
1969/*
1970 * Return the column where the text starts that is being completed
1971 */
1972 colnr_T
1973ins_compl_col(void)
1974{
1975 return compl_col;
1976}
1977
1978/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001979 * Return the length in bytes of the text being completed
1980 */
1981 int
1982ins_compl_len(void)
1983{
1984 return compl_length;
1985}
1986
1987/*
glepniredd4ac32025-01-29 18:53:51 +01001988 * Return TRUE when preinsert is set otherwise FALSE.
1989 */
1990 static int
1991ins_compl_has_preinsert(void)
1992{
1993 return (get_cot_flags() & (COT_PREINSERT | COT_FUZZY)) == COT_PREINSERT;
1994}
1995
1996/*
1997 * Returns TRUE if the pre-insert effect is valid and the cursor is within
1998 * the `compl_ins_end_col` range.
1999 */
2000 int
2001ins_compl_preinsert_effect(void)
2002{
2003 if (!ins_compl_has_preinsert())
2004 return FALSE;
2005
2006 return curwin->w_cursor.col < compl_ins_end_col;
2007}
2008
2009/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002010 * Delete one character before the cursor and show the subset of the matches
2011 * that match the word that is now before the cursor.
2012 * Returns the character to be used, NUL if the work is done and another char
2013 * to be got from the user.
2014 */
2015 int
2016ins_compl_bs(void)
2017{
2018 char_u *line;
2019 char_u *p;
2020
glepniredd4ac32025-01-29 18:53:51 +01002021 if (ins_compl_preinsert_effect())
2022 ins_compl_delete();
2023
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002024 line = ml_get_curline();
2025 p = line + curwin->w_cursor.col;
2026 MB_PTR_BACK(line, p);
2027
2028 // Stop completion when the whole word was deleted. For Omni completion
2029 // allow the word to be deleted, we won't match everything.
2030 // Respect the 'backspace' option.
2031 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002032 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2033 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002034 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2035 - compl_length < 0))
2036 return K_BS;
2037
2038 // Deleted more than what was used to find matches or didn't finish
2039 // finding all matches: need to look for matches all over again.
2040 if (curwin->w_cursor.col <= compl_col + compl_length
2041 || ins_compl_need_restart())
2042 ins_compl_restart();
2043
John Marriott5e6ea922024-11-23 14:01:57 +01002044 VIM_CLEAR_STRING(compl_leader);
2045 compl_leader.length = (size_t)((p - line) - compl_col);
2046 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2047 if (compl_leader.string == NULL)
2048 {
2049 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002050 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002051 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002052
2053 ins_compl_new_leader();
2054 if (compl_shown_match != NULL)
2055 // Make sure current match is not a hidden item.
2056 compl_curr_match = compl_shown_match;
2057 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002058}
2059
2060/*
2061 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2062 * be called.
2063 */
2064 static int
2065ins_compl_need_restart(void)
2066{
2067 // Return TRUE if we didn't complete finding matches or when the
2068 // 'completefunc' returned "always" in the "refresh" dictionary item.
2069 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002070 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002071 && compl_opt_refresh_always);
2072}
2073
2074/*
2075 * Called after changing "compl_leader".
2076 * Show the popup menu with a different set of matches.
2077 * May also search for matches again if the previous search was interrupted.
2078 */
2079 static void
2080ins_compl_new_leader(void)
2081{
2082 ins_compl_del_pum();
2083 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002084 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002085 compl_used_match = FALSE;
2086
2087 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002088 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002089 else
2090 {
2091#ifdef FEAT_SPELL
2092 spell_bad_len = 0; // need to redetect bad word
2093#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002094 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002095 // the popup menu display the changed text before the cursor. Set
2096 // "compl_restarting" to avoid that the first match is inserted.
2097 pum_call_update_screen();
2098#ifdef FEAT_GUI
2099 if (gui.in_use)
2100 {
2101 // Show the cursor after the match, not after the redrawn text.
2102 setcursor();
2103 out_flush_cursor(FALSE, FALSE);
2104 }
2105#endif
2106 compl_restarting = TRUE;
2107 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2108 compl_cont_status = 0;
2109 compl_restarting = FALSE;
2110 }
2111
glepnir44180412025-02-20 22:09:48 +01002112 compl_enter_selects = !compl_used_match && compl_selected_item != -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002113
2114 // Show the popup menu with a different set of matches.
2115 ins_compl_show_pum();
2116
2117 // Don't let Enter select the original text when there is no popup menu.
2118 if (compl_match_array == NULL)
2119 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002120 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2121 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002122}
2123
2124/*
2125 * Return the length of the completion, from the completion start column to
2126 * the cursor column. Making sure it never goes below zero.
2127 */
2128 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002129get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002130{
2131 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002132 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002133}
2134
2135/*
2136 * Append one character to the match leader. May reduce the number of
2137 * matches.
2138 */
2139 void
2140ins_compl_addleader(int c)
2141{
2142 int cc;
2143
glepniredd4ac32025-01-29 18:53:51 +01002144 if (ins_compl_preinsert_effect())
2145 ins_compl_delete();
2146
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002147 if (stop_arrow() == FAIL)
2148 return;
2149 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2150 {
2151 char_u buf[MB_MAXBYTES + 1];
2152
2153 (*mb_char2bytes)(c, buf);
2154 buf[cc] = NUL;
2155 ins_char_bytes(buf, cc);
2156 if (compl_opt_refresh_always)
2157 AppendToRedobuff(buf);
2158 }
2159 else
2160 {
2161 ins_char(c);
2162 if (compl_opt_refresh_always)
2163 AppendCharToRedobuff(c);
2164 }
2165
2166 // If we didn't complete finding matches we must search again.
2167 if (ins_compl_need_restart())
2168 ins_compl_restart();
2169
2170 // When 'always' is set, don't reset compl_leader. While completing,
2171 // cursor doesn't point original position, changing compl_leader would
2172 // break redo.
2173 if (!compl_opt_refresh_always)
2174 {
John Marriott5e6ea922024-11-23 14:01:57 +01002175 VIM_CLEAR_STRING(compl_leader);
2176 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2177 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2178 compl_leader.length);
2179 if (compl_leader.string == NULL)
2180 {
2181 compl_leader.length = 0;
2182 return;
2183 }
2184
2185 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002186 }
2187}
2188
2189/*
2190 * Setup for finding completions again without leaving CTRL-X mode. Used when
2191 * BS or a key was typed while still searching for matches.
2192 */
2193 static void
2194ins_compl_restart(void)
2195{
2196 ins_compl_free();
2197 compl_started = FALSE;
2198 compl_matches = 0;
2199 compl_cont_status = 0;
2200 compl_cont_mode = 0;
2201}
2202
2203/*
2204 * Set the first match, the original text.
2205 */
2206 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002207ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002208{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002209 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002210 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2211 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002212 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002213 {
John Marriott5e6ea922024-11-23 14:01:57 +01002214 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002215 if (p != NULL)
2216 {
John Marriott5e6ea922024-11-23 14:01:57 +01002217 VIM_CLEAR_STRING(compl_first_match->cp_str);
2218 compl_first_match->cp_str.string = p;
2219 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002220 }
2221 }
2222 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002223 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002224 {
John Marriott5e6ea922024-11-23 14:01:57 +01002225 char_u *p = vim_strnsave(str, len);
2226 if (p != NULL)
2227 {
2228 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2229 compl_first_match->cp_prev->cp_str.string = p;
2230 compl_first_match->cp_prev->cp_str.length = len;
2231 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002232 }
2233}
2234
2235/*
2236 * Append one character to the match leader. May reduce the number of
2237 * matches.
2238 */
2239 void
2240ins_compl_addfrommatch(void)
2241{
2242 char_u *p;
2243 int len = (int)curwin->w_cursor.col - (int)compl_col;
2244 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002245
John Marriott5e6ea922024-11-23 14:01:57 +01002246 p = compl_shown_match->cp_str.string;
2247 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002248 {
John Marriott5e6ea922024-11-23 14:01:57 +01002249 size_t plen;
2250 compl_T *cp;
2251
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002252 // When still at the original match use the first entry that matches
2253 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002254 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002255 return;
2256
2257 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002258 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002259 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002260 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002261 {
John Marriott5e6ea922024-11-23 14:01:57 +01002262 if (compl_leader.string == NULL
2263 || ins_compl_equal(cp, compl_leader.string,
2264 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002265 {
John Marriott5e6ea922024-11-23 14:01:57 +01002266 p = cp->cp_str.string;
2267 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002268 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002269 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002270 }
John Marriott5e6ea922024-11-23 14:01:57 +01002271 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002272 return;
2273 }
2274 p += len;
2275 c = PTR2CHAR(p);
2276 ins_compl_addleader(c);
2277}
2278
2279/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002280 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002281 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2282 * compl_cont_mode and compl_cont_status.
2283 * Returns TRUE when the character is not to be inserted.
2284 */
2285 static int
2286set_ctrl_x_mode(int c)
2287{
2288 int retval = FALSE;
2289
2290 switch (c)
2291 {
2292 case Ctrl_E:
2293 case Ctrl_Y:
2294 // scroll the window one line up or down
2295 ctrl_x_mode = CTRL_X_SCROLL;
2296 if (!(State & REPLACE_FLAG))
2297 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2298 else
2299 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2300 edit_submode_pre = NULL;
2301 showmode();
2302 break;
2303 case Ctrl_L:
2304 // complete whole line
2305 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2306 break;
2307 case Ctrl_F:
2308 // complete filenames
2309 ctrl_x_mode = CTRL_X_FILES;
2310 break;
2311 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002312 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002313 ctrl_x_mode = CTRL_X_DICTIONARY;
2314 break;
2315 case Ctrl_R:
2316 // Register insertion without exiting CTRL-X mode
2317 // Simply allow ^R to happen without affecting ^X mode
2318 break;
2319 case Ctrl_T:
2320 // complete words from a thesaurus
2321 ctrl_x_mode = CTRL_X_THESAURUS;
2322 break;
2323#ifdef FEAT_COMPL_FUNC
2324 case Ctrl_U:
2325 // user defined completion
2326 ctrl_x_mode = CTRL_X_FUNCTION;
2327 break;
2328 case Ctrl_O:
2329 // omni completion
2330 ctrl_x_mode = CTRL_X_OMNI;
2331 break;
2332#endif
2333 case 's':
2334 case Ctrl_S:
2335 // complete spelling suggestions
2336 ctrl_x_mode = CTRL_X_SPELL;
2337#ifdef FEAT_SPELL
2338 ++emsg_off; // Avoid getting the E756 error twice.
2339 spell_back_to_badword();
2340 --emsg_off;
2341#endif
2342 break;
2343 case Ctrl_RSB:
2344 // complete tag names
2345 ctrl_x_mode = CTRL_X_TAGS;
2346 break;
2347#ifdef FEAT_FIND_ID
2348 case Ctrl_I:
2349 case K_S_TAB:
2350 // complete keywords from included files
2351 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2352 break;
2353 case Ctrl_D:
2354 // complete definitions from included files
2355 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2356 break;
2357#endif
2358 case Ctrl_V:
2359 case Ctrl_Q:
2360 // complete vim commands
2361 ctrl_x_mode = CTRL_X_CMDLINE;
2362 break;
2363 case Ctrl_Z:
2364 // stop completion
2365 ctrl_x_mode = CTRL_X_NORMAL;
2366 edit_submode = NULL;
2367 showmode();
2368 retval = TRUE;
2369 break;
2370 case Ctrl_P:
2371 case Ctrl_N:
2372 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2373 // just started ^X mode, or there were enough ^X's to cancel
2374 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2375 // do normal expansion when interrupting a different mode (say
2376 // ^X^F^X^P or ^P^X^X^P, see below)
2377 // nothing changes if interrupting mode 0, (eg, the flag
2378 // doesn't change when going to ADDING mode -- Acevedo
2379 if (!(compl_cont_status & CONT_INTRPT))
2380 compl_cont_status |= CONT_LOCAL;
2381 else if (compl_cont_mode != 0)
2382 compl_cont_status &= ~CONT_LOCAL;
2383 // FALLTHROUGH
2384 default:
2385 // If we have typed at least 2 ^X's... for modes != 0, we set
2386 // compl_cont_status = 0 (eg, as if we had just started ^X
2387 // mode).
2388 // For mode 0, we set "compl_cont_mode" to an impossible
2389 // value, in both cases ^X^X can be used to restart the same
2390 // mode (avoiding ADDING mode).
2391 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2392 // 'complete' and local ^P expansions respectively.
2393 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2394 // mode -- Acevedo
2395 if (c == Ctrl_X)
2396 {
2397 if (compl_cont_mode != 0)
2398 compl_cont_status = 0;
2399 else
2400 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2401 }
2402 ctrl_x_mode = CTRL_X_NORMAL;
2403 edit_submode = NULL;
2404 showmode();
2405 break;
2406 }
2407
2408 return retval;
2409}
2410
2411/*
glepnir1c5a1202024-12-04 20:27:34 +01002412 * Trigger CompleteDone event and adds relevant information to v:event
2413 */
2414 static void
2415trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2416{
2417#if defined(FEAT_EVAL)
2418 save_v_event_T save_v_event;
2419 dict_T *v_event = get_v_event(&save_v_event);
2420 char_u *mode_str = NULL;
2421
2422 mode = mode & ~CTRL_X_WANT_IDENT;
2423 if (ctrl_x_mode_names[mode])
2424 mode_str = (char_u *)ctrl_x_mode_names[mode];
2425
2426 (void)dict_add_string(v_event, "complete_word",
2427 word == NULL ? (char_u *)"" : word);
2428 (void)dict_add_string(v_event, "complete_type",
2429 mode_str != NULL ? mode_str : (char_u *)"");
2430
2431 dict_set_items_ro(v_event);
2432#endif
2433 ins_apply_autocmds(EVENT_COMPLETEDONE);
2434
2435#if defined(FEAT_EVAL)
2436 restore_v_event(v_event, &save_v_event);
2437#endif
2438}
2439
2440/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002441 * Stop insert completion mode
2442 */
2443 static int
2444ins_compl_stop(int c, int prev_mode, int retval)
2445{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002446 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002447 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002448
2449 // Get here when we have finished typing a sequence of ^N and
2450 // ^P or other completion characters in CTRL-X mode. Free up
2451 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002452 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002453 {
glepnir40891ba2025-02-10 22:18:00 +01002454 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002455
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002456 // If any of the original typed text has been changed, eg when
2457 // ignorecase is set, we must add back-spaces to the redo
2458 // buffer. We add as few as necessary to delete just the part
2459 // of the original text that has changed.
2460 // When using the longest match, edited the match or used
2461 // CTRL-E then don't use the current match.
2462 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002463 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002464 ins_compl_fixRedoBufForLeader(ptr);
2465 }
2466
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002467 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002468
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002469 // When completing whole lines: fix indent for 'cindent'.
2470 // Otherwise, break line if it's too long.
2471 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2472 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002473 // re-indent the current line
2474 if (want_cindent)
2475 {
2476 do_c_expr_indent();
2477 want_cindent = FALSE; // don't do it again
2478 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002479 }
2480 else
2481 {
2482 int prev_col = curwin->w_cursor.col;
2483
2484 // put the cursor on the last char, for 'tw' formatting
2485 if (prev_col > 0)
2486 dec_cursor();
2487 // only format when something was inserted
2488 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2489 insertchar(NUL, 0, -1);
2490 if (prev_col > 0
2491 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2492 inc_cursor();
2493 }
2494
2495 // If the popup menu is displayed pressing CTRL-Y means accepting
2496 // the selection without inserting anything. When
2497 // compl_enter_selects is set the Enter key does the same.
2498 if ((c == Ctrl_Y || (compl_enter_selects
2499 && (c == CAR || c == K_KENTER || c == NL)))
2500 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002501 {
2502 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002503 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002504 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002505
2506 // CTRL-E means completion is Ended, go back to the typed text.
2507 // but only do this, if the Popup is still visible
2508 if (c == Ctrl_E)
2509 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002510 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002511 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002512
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002513 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002514 if (compl_leader.string != NULL)
2515 {
2516 p = compl_leader.string;
2517 plen = compl_leader.length;
2518 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002519 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002520 {
2521 p = compl_orig_text.string;
2522 plen = compl_orig_text.length;
2523 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002524 if (p != NULL)
2525 {
2526 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002527
John Marriott5e6ea922024-11-23 14:01:57 +01002528 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002529 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002530 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002531 retval = TRUE;
2532 }
2533
glepnir001c26c2025-02-02 09:36:22 +01002534 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2535 ins_compl_delete();
2536
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002537 auto_format(FALSE, TRUE);
2538
2539 // Trigger the CompleteDonePre event to give scripts a chance to
2540 // act upon the completion before clearing the info, and restore
2541 // ctrl_x_mode, so that complete_info() can be used.
2542 ctrl_x_mode = prev_mode;
2543 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2544
2545 ins_compl_free();
2546 compl_started = FALSE;
2547 compl_matches = 0;
2548 if (!shortmess(SHM_COMPLETIONMENU))
2549 msg_clr_cmdline(); // necessary for "noshowmode"
2550 ctrl_x_mode = CTRL_X_NORMAL;
2551 compl_enter_selects = FALSE;
2552 if (edit_submode != NULL)
2553 {
2554 edit_submode = NULL;
2555 showmode();
2556 }
2557
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002558 if (c == Ctrl_C && cmdwin_type != 0)
2559 // Avoid the popup menu remains displayed when leaving the
2560 // command line window.
2561 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002562 // Indent now if a key was typed that is in 'cinkeys'.
2563 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2564 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002565 // Trigger the CompleteDone event to give scripts a chance to act
2566 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002567 trigger_complete_done_event(prev_mode, word);
2568 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002569
2570 return retval;
2571}
2572
2573/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002574 * Prepare for Insert mode completion, or stop it.
2575 * Called just after typing a character in Insert mode.
2576 * Returns TRUE when the character is not to be inserted;
2577 */
2578 int
2579ins_compl_prep(int c)
2580{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002581 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002582 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002583
2584 // Forget any previous 'special' messages if this is actually
2585 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2586 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2587 edit_submode_extra = NULL;
2588
zeertzjq440d4cb2023-03-02 17:51:32 +00002589 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002590 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002591 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002592 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002593 return retval;
2594
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002595#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002596 // Ignore mouse events in a popup window
2597 if (is_mouse_key(c))
2598 {
2599 // Ignore drag and release events, the position does not need to be in
2600 // the popup and it may have just closed.
2601 if (c == K_LEFTRELEASE
2602 || c == K_LEFTRELEASE_NM
2603 || c == K_MIDDLERELEASE
2604 || c == K_RIGHTRELEASE
2605 || c == K_X1RELEASE
2606 || c == K_X2RELEASE
2607 || c == K_LEFTDRAG
2608 || c == K_MIDDLEDRAG
2609 || c == K_RIGHTDRAG
2610 || c == K_X1DRAG
2611 || c == K_X2DRAG)
2612 return retval;
2613 if (popup_visible)
2614 {
2615 int row = mouse_row;
2616 int col = mouse_col;
2617 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2618
2619 if (wp != NULL && WIN_IS_POPUP(wp))
2620 return retval;
2621 }
2622 }
2623#endif
2624
zeertzjqdca29d92021-08-31 19:12:51 +02002625 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2626 {
2627 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2628 || !vim_is_ctrl_x_key(c))
2629 {
2630 // Not starting another completion mode.
2631 ctrl_x_mode = CTRL_X_CMDLINE;
2632
2633 // CTRL-X CTRL-Z should stop completion without inserting anything
2634 if (c == Ctrl_Z)
2635 retval = TRUE;
2636 }
2637 else
2638 {
2639 ctrl_x_mode = CTRL_X_CMDLINE;
2640
2641 // Other CTRL-X keys first stop completion, then start another
2642 // completion mode.
2643 ins_compl_prep(' ');
2644 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2645 }
2646 }
2647
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002648 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002649 if (ctrl_x_mode_not_defined_yet()
2650 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002651 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002652 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002654 }
2655
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002656 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002657 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2658 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002659 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002660 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002661 {
2662 // We're already in CTRL-X mode, do we stay in it?
2663 if (!vim_is_ctrl_x_key(c))
2664 {
glepnir40891ba2025-02-10 22:18:00 +01002665 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002666 edit_submode = NULL;
2667 }
2668 showmode();
2669 }
2670
2671 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2672 {
2673 // Show error message from attempted keyword completion (probably
2674 // 'Pattern not found') until another key is hit, then go back to
2675 // showing what mode we are in.
2676 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002677 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002678 && c != Ctrl_R && !ins_compl_pum_key(c))
2679 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002680 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002681 }
2682 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2683 // Trigger the CompleteDone event to give scripts a chance to act
2684 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002685 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002686
LemonBoy2bf52dd2022-04-09 18:17:34 +01002687 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002688
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002689 // reset continue_* if we left expansion-mode, if we stay they'll be
2690 // (re)set properly in ins_complete()
2691 if (!vim_is_ctrl_x_key(c))
2692 {
2693 compl_cont_status = 0;
2694 compl_cont_mode = 0;
2695 }
2696
2697 return retval;
2698}
2699
2700/*
2701 * Fix the redo buffer for the completion leader replacing some of the typed
2702 * text. This inserts backspaces and appends the changed text.
2703 * "ptr" is the known leader text or NUL.
2704 */
2705 static void
2706ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2707{
John Marriott5e6ea922024-11-23 14:01:57 +01002708 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002709 char_u *p;
2710 char_u *ptr = ptr_arg;
2711
2712 if (ptr == NULL)
2713 {
John Marriott5e6ea922024-11-23 14:01:57 +01002714 if (compl_leader.string != NULL)
2715 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002716 else
2717 return; // nothing to do
2718 }
John Marriott5e6ea922024-11-23 14:01:57 +01002719 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720 {
John Marriott5e6ea922024-11-23 14:01:57 +01002721 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002722 // Find length of common prefix between original text and new completion
2723 while (p[len] != NUL && p[len] == ptr[len])
2724 len++;
2725 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002726 if (len > 0)
2727 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002728 // Add backspace characters for each remaining character in
2729 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002730 for (p += len; *p != NUL; MB_PTR_ADV(p))
2731 AppendCharToRedobuff(K_BS);
2732 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002733 if (ptr != NULL)
2734 AppendToRedobuffLit(ptr + len, -1);
2735}
2736
2737/*
2738 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2739 * (depending on flag) starting from buf and looking for a non-scanned
2740 * buffer (other than curbuf). curbuf is special, if it is called with
2741 * buf=curbuf then it has to be the first call for a given flag/expansion.
2742 *
2743 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2744 */
2745 static buf_T *
2746ins_compl_next_buf(buf_T *buf, int flag)
2747{
2748 static win_T *wp = NULL;
glepnir40891ba2025-02-10 22:18:00 +01002749 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002750
2751 if (flag == 'w') // just windows
2752 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002753 if (buf == curbuf || !win_valid(wp))
2754 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01002756
2757 while (TRUE)
2758 {
2759 // Move to next window (wrap to first window if at the end)
2760 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2761 // Break if we're back at start or found an unscanned buffer
2762 if (wp == curwin || !wp->w_buffer->b_scanned)
2763 break;
2764 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002765 buf = wp->w_buffer;
2766 }
2767 else
glepnir40891ba2025-02-10 22:18:00 +01002768 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002769 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2770 // (unlisted buffers)
2771 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01002772 while (TRUE)
2773 {
2774 // Move to next buffer (wrap to first buffer if at the end)
2775 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2776 // Break if we're back at start buffer
2777 if (buf == curbuf)
2778 break;
2779
2780 // Check buffer conditions based on flag
2781 if (flag == 'U')
2782 skip_buffer = buf->b_p_bl;
2783 else
2784 skip_buffer = !buf->b_p_bl ||
2785 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2786
2787 // Break if we found a buffer that matches our criteria
2788 if (!skip_buffer && !buf->b_scanned)
2789 break;
2790 }
2791 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002792 return buf;
2793}
2794
2795#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002796
2797# ifdef FEAT_EVAL
2798static callback_T cfu_cb; // 'completefunc' callback function
2799static callback_T ofu_cb; // 'omnifunc' callback function
2800static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2801# endif
2802
2803/*
2804 * Copy a global callback function to a buffer local callback.
2805 */
2806 static void
2807copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2808{
2809 free_callback(bufcb);
2810 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2811 copy_callback(bufcb, globcb);
2812}
2813
2814/*
2815 * Parse the 'completefunc' option value and set the callback function.
2816 * Invoked when the 'completefunc' option is set. The option value can be a
2817 * name of a function (string), or function(<name>) or funcref(<name>) or a
2818 * lambda expression.
2819 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002820 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002821did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002822{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002823 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2824 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002825
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002826 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002827
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002828 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002829}
2830
2831/*
2832 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002833 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002834 */
2835 void
2836set_buflocal_cfu_callback(buf_T *buf UNUSED)
2837{
2838# ifdef FEAT_EVAL
2839 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2840# endif
2841}
2842
2843/*
2844 * Parse the 'omnifunc' option value and set the callback function.
2845 * Invoked when the 'omnifunc' option is set. The option value can be a
2846 * name of a function (string), or function(<name>) or funcref(<name>) or a
2847 * lambda expression.
2848 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002849 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002850did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002851{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002852 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2853 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002854
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002855 set_buflocal_ofu_callback(curbuf);
2856 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002857}
2858
2859/*
2860 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002861 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002862 */
2863 void
2864set_buflocal_ofu_callback(buf_T *buf UNUSED)
2865{
2866# ifdef FEAT_EVAL
2867 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2868# endif
2869}
2870
2871/*
2872 * Parse the 'thesaurusfunc' option value and set the callback function.
2873 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2874 * name of a function (string), or function(<name>) or funcref(<name>) or a
2875 * lambda expression.
2876 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002877 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002878did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002879{
2880 int retval;
2881
zeertzjq6eda2692024-11-03 09:23:33 +01002882 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002883 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002884 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2885 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002886 else
2887 {
2888 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002889 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002890 // when using :set, free the local callback
2891 if (!(args->os_flags & OPT_GLOBAL))
2892 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002893 }
2894
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002895 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002896}
2897
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002898/*
2899 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002900 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002901 */
2902 int
2903set_ref_in_insexpand_funcs(int copyID)
2904{
2905 int abort = FALSE;
2906
2907 abort = set_ref_in_callback(&cfu_cb, copyID);
2908 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2909 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2910
2911 return abort;
2912}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002913
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002914/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002915 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002916 */
2917 static char_u *
2918get_complete_funcname(int type)
2919{
2920 switch (type)
2921 {
2922 case CTRL_X_FUNCTION:
2923 return curbuf->b_p_cfu;
2924 case CTRL_X_OMNI:
2925 return curbuf->b_p_ofu;
2926 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002927 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002928 default:
2929 return (char_u *)"";
2930 }
2931}
2932
2933/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002934 * Get the callback to use for insert mode completion.
2935 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002936 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002937get_insert_callback(int type)
2938{
2939 if (type == CTRL_X_FUNCTION)
2940 return &curbuf->b_cfu_cb;
2941 if (type == CTRL_X_OMNI)
2942 return &curbuf->b_ofu_cb;
2943 // CTRL_X_THESAURUS
2944 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2945}
2946
2947/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002948 * Execute user defined complete function 'completefunc', 'omnifunc' or
2949 * 'thesaurusfunc', and get matches in "matches".
2950 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002951 */
2952 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002953expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002954{
2955 list_T *matchlist = NULL;
2956 dict_T *matchdict = NULL;
2957 typval_T args[3];
2958 char_u *funcname;
2959 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002960 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002961 typval_T rettv;
2962 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002963 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002964
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002965 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002966 if (*funcname == NUL)
2967 return;
2968
2969 // Call 'completefunc' to obtain the list of matches.
2970 args[0].v_type = VAR_NUMBER;
2971 args[0].vval.v_number = 0;
2972 args[1].v_type = VAR_STRING;
2973 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2974 args[2].v_type = VAR_UNKNOWN;
2975
2976 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002977 // Lock the text to avoid weird things from happening. Also disallow
2978 // switching to another window, it should not be needed and may end up in
2979 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002980 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002981
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002982 cb = get_insert_callback(type);
2983 retval = call_callback(cb, 0, &rettv, 2, args);
2984
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002985 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002986 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002987 {
2988 switch (rettv.v_type)
2989 {
2990 case VAR_LIST:
2991 matchlist = rettv.vval.v_list;
2992 break;
2993 case VAR_DICT:
2994 matchdict = rettv.vval.v_dict;
2995 break;
2996 case VAR_SPECIAL:
2997 if (rettv.vval.v_number == VVAL_NONE)
2998 compl_opt_suppress_empty = TRUE;
2999 // FALLTHROUGH
3000 default:
3001 // TODO: Give error message?
3002 clear_tv(&rettv);
3003 break;
3004 }
3005 }
zeertzjqcfe45652022-05-27 17:26:55 +01003006 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003007
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003008 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003009 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003010 validate_cursor();
3011 if (!EQUAL_POS(curwin->w_cursor, pos))
3012 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003013 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003014 goto theend;
3015 }
3016
3017 if (matchlist != NULL)
3018 ins_compl_add_list(matchlist);
3019 else if (matchdict != NULL)
3020 ins_compl_add_dict(matchdict);
3021
3022theend:
3023 // Restore State, it might have been changed.
3024 State = save_State;
3025
3026 if (matchdict != NULL)
3027 dict_unref(matchdict);
3028 if (matchlist != NULL)
3029 list_unref(matchlist);
3030}
3031#endif // FEAT_COMPL_FUNC
3032
3033#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003034
3035 static inline int
3036get_user_highlight_attr(char_u *hlname)
3037{
3038 if (hlname != NULL && *hlname != NUL)
3039 return syn_name2attr(hlname);
3040 return -1;
3041}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003042/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003043 * Add a match to the list of matches from a typeval_T.
3044 * If the given string is already in the list of completions, then return
3045 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3046 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003047 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003048 */
3049 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003050ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003051{
3052 char_u *word;
3053 int dup = FALSE;
3054 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003055 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003056 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003057 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003058 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003059 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003060 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003061 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003062
Bram Moolenaar08928322020-01-04 14:32:48 +01003063 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003064 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3065 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003066 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3067 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3068 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3069 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3070 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003071
glepnir0fe17f82024-10-08 22:26:44 +02003072 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003073 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003074
3075 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003076 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003077
Bram Moolenaard61efa52022-07-23 09:52:04 +01003078 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3079 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3080 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003081 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003082 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3083 dup = dict_get_number(tv->vval.v_dict, "dup");
3084 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3085 empty = dict_get_number(tv->vval.v_dict, "empty");
3086 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3087 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003088 flags |= CP_EQUAL;
3089 }
3090 else
3091 {
3092 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003093 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003094 }
3095 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003096 {
3097 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003098 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003099 }
glepnir508e7852024-07-25 21:39:08 +02003100 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003101 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003102 if (status != OK)
3103 clear_tv(&user_data);
3104 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003105}
3106
3107/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108 * Add completions from a list.
3109 */
3110 static void
3111ins_compl_add_list(list_T *list)
3112{
3113 listitem_T *li;
3114 int dir = compl_direction;
3115
3116 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003117 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003118 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003119 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003120 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003121 // if dir was BACKWARD then honor it just once
3122 dir = FORWARD;
3123 else if (did_emsg)
3124 break;
3125 }
3126}
3127
3128/*
3129 * Add completions from a dict.
3130 */
3131 static void
3132ins_compl_add_dict(dict_T *dict)
3133{
3134 dictitem_T *di_refresh;
3135 dictitem_T *di_words;
3136
3137 // Check for optional "refresh" item.
3138 compl_opt_refresh_always = FALSE;
3139 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3140 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3141 {
3142 char_u *v = di_refresh->di_tv.vval.v_string;
3143
3144 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3145 compl_opt_refresh_always = TRUE;
3146 }
3147
3148 // Add completions from a "words" list.
3149 di_words = dict_find(dict, (char_u *)"words", 5);
3150 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3151 ins_compl_add_list(di_words->di_tv.vval.v_list);
3152}
3153
3154/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003155 * Start completion for the complete() function.
3156 * "startcol" is where the matched text starts (1 is first column).
3157 * "list" is the list of matches.
3158 */
3159 static void
3160set_completion(colnr_T startcol, list_T *list)
3161{
3162 int save_w_wrow = curwin->w_wrow;
3163 int save_w_leftcol = curwin->w_leftcol;
3164 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003165 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003166 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3167 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3168 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003169
3170 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003171 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003172 ins_compl_prep(' ');
3173 ins_compl_clear();
3174 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003175 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003176
3177 compl_direction = FORWARD;
3178 if (startcol > curwin->w_cursor.col)
3179 startcol = curwin->w_cursor.col;
3180 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003181 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003182 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3183 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003184 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3185 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003186 if (p_ic)
3187 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003188 if (compl_orig_text.string == NULL)
3189 {
3190 compl_orig_text.length = 0;
3191 return;
3192 }
3193 compl_orig_text.length = (size_t)compl_length;
3194 if (ins_compl_add(compl_orig_text.string,
3195 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3196 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003197 return;
3198
3199 ctrl_x_mode = CTRL_X_EVAL;
3200
3201 ins_compl_add_list(list);
3202 compl_matches = ins_compl_make_cyclic();
3203 compl_started = TRUE;
3204 compl_used_match = TRUE;
3205 compl_cont_status = 0;
3206
3207 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003208 int no_select = compl_no_select || compl_longest;
3209 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003210 {
3211 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003212 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003213 // Down/Up has no real effect.
3214 ins_complete(K_UP, FALSE);
3215 }
3216 else
3217 ins_complete(Ctrl_N, FALSE);
3218 compl_enter_selects = compl_no_insert;
3219
3220 // Lazily show the popup menu, unless we got interrupted.
3221 if (!compl_interrupted)
3222 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003223 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003224 out_flush();
3225}
3226
3227/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003228 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003229 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003230 void
3231f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003232{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003233 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003234
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003235 if (in_vim9script()
3236 && (check_for_number_arg(argvars, 0) == FAIL
3237 || check_for_list_arg(argvars, 1) == FAIL))
3238 return;
3239
Bram Moolenaar24959102022-05-07 20:01:16 +01003240 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003241 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003242 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003243 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003244 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003245
3246 // Check for undo allowed here, because if something was already inserted
3247 // the line was already saved for undo and this check isn't done.
3248 if (!undo_allowed())
3249 return;
3250
Bram Moolenaard83392a2022-09-01 12:22:46 +01003251 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003252 {
3253 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3254 if (startcol > 0)
3255 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003256 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003257}
3258
3259/*
3260 * "complete_add()" function
3261 */
3262 void
3263f_complete_add(typval_T *argvars, typval_T *rettv)
3264{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003265 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003266 return;
3267
Bram Moolenaar440cf092021-04-03 20:13:30 +02003268 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003269}
3270
3271/*
3272 * "complete_check()" function
3273 */
3274 void
3275f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3276{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003277 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003278 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003279
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003280 ins_compl_check_keys(0, TRUE);
3281 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003282
3283 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003284}
3285
3286/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003287 * Return Insert completion mode name string
3288 */
3289 static char_u *
3290ins_compl_mode(void)
3291{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003292 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003293 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3294
3295 return (char_u *)"";
3296}
3297
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003298/*
3299 * Assign the sequence number to all the completion matches which don't have
3300 * one assigned yet.
3301 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003302 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003303ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003304{
3305 int number = 0;
3306 compl_T *match;
3307
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003308 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003309 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003310 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003311 // This should normally succeed already at the first loop
3312 // cycle, so it's fast!
3313 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003314 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003315 if (match->cp_number != -1)
3316 {
3317 number = match->cp_number;
3318 break;
3319 }
3320 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003321 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003322 for (match = match->cp_next;
3323 match != NULL && match->cp_number == -1;
3324 match = match->cp_next)
3325 match->cp_number = ++number;
3326 }
3327 else // BACKWARD
3328 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003329 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003330 // number. This should normally succeed already at the
3331 // first loop cycle, so it's fast!
3332 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003333 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003334 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003335 if (match->cp_number != -1)
3336 {
3337 number = match->cp_number;
3338 break;
3339 }
glepnir40891ba2025-02-10 22:18:00 +01003340 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003341 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003342 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003343 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003344 for (match = match->cp_prev; match
3345 && match->cp_number == -1;
3346 match = match->cp_prev)
3347 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003348 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003349 }
3350}
3351
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003352/*
glepnir037b0282025-01-16 14:37:44 +01003353 * Fill the dict of complete_info
3354 */
3355 static void
3356fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3357{
3358 dict_add_string(di, "word", match->cp_str.string);
3359 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3360 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3361 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3362 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3363 if (add_match)
3364 dict_add_bool(di, "match", match->cp_in_match_array);
3365 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3366 // Add an empty string for backwards compatibility
3367 dict_add_string(di, "user_data", (char_u *)"");
3368 else
3369 dict_add_tv(di, "user_data", &match->cp_user_data);
3370}
3371
3372/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003373 * Get complete information
3374 */
3375 static void
3376get_complete_info(list_T *what_list, dict_T *retdict)
3377{
3378 int ret = OK;
3379 listitem_T *item;
3380#define CI_WHAT_MODE 0x01
3381#define CI_WHAT_PUM_VISIBLE 0x02
3382#define CI_WHAT_ITEMS 0x04
3383#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003384#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003385#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003386#define CI_WHAT_ALL 0xff
3387 int what_flag;
3388
3389 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003390 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003391 else
3392 {
3393 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003394 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003395 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003396 {
3397 char_u *what = tv_get_string(&item->li_tv);
3398
3399 if (STRCMP(what, "mode") == 0)
3400 what_flag |= CI_WHAT_MODE;
3401 else if (STRCMP(what, "pum_visible") == 0)
3402 what_flag |= CI_WHAT_PUM_VISIBLE;
3403 else if (STRCMP(what, "items") == 0)
3404 what_flag |= CI_WHAT_ITEMS;
3405 else if (STRCMP(what, "selected") == 0)
3406 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003407 else if (STRCMP(what, "completed") == 0)
3408 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003409 else if (STRCMP(what, "matches") == 0)
3410 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003411 }
3412 }
3413
3414 if (ret == OK && (what_flag & CI_WHAT_MODE))
3415 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3416
3417 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3418 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3419
glepnir037b0282025-01-16 14:37:44 +01003420 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3421 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003422 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003423 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003424 dict_T *di;
3425 compl_T *match;
3426 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003427 int has_items = what_flag & CI_WHAT_ITEMS;
3428 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003429 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003430
glepnird4088ed2024-12-31 10:55:22 +01003431 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003432 {
3433 li = list_alloc();
3434 if (li == NULL)
3435 return;
glepnird4088ed2024-12-31 10:55:22 +01003436 ret = dict_add_list(retdict, (has_matches && !has_items)
3437 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003438 }
3439 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3440 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3441 ins_compl_update_sequence_numbers();
3442 if (ret == OK && compl_first_match != NULL)
3443 {
3444 int list_idx = 0;
3445 match = compl_first_match;
3446 do
3447 {
3448 if (!match_at_original_text(match))
3449 {
glepnird4088ed2024-12-31 10:55:22 +01003450 if (has_items
3451 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003452 {
3453 di = dict_alloc();
3454 if (di == NULL)
3455 return;
3456 ret = list_append_dict(li, di);
3457 if (ret != OK)
3458 return;
glepnir037b0282025-01-16 14:37:44 +01003459 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003460 }
glepnird4088ed2024-12-31 10:55:22 +01003461 if (compl_curr_match != NULL
3462 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003463 selected_idx = list_idx;
3464 list_idx += 1;
3465 }
3466 match = match->cp_next;
3467 }
3468 while (match != NULL && !is_first_match(match));
3469 }
3470 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3471 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003472
glepnir037b0282025-01-16 14:37:44 +01003473 if (ret == OK && selected_idx != -1 && has_completed)
3474 {
3475 di = dict_alloc();
3476 if (di == NULL)
3477 return;
3478 fill_complete_info_dict(di, compl_curr_match, FALSE);
3479 ret = dict_add_dict(retdict, "completed", di);
3480 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003481 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003482}
3483
3484/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003485 * "complete_info()" function
3486 */
3487 void
3488f_complete_info(typval_T *argvars, typval_T *rettv)
3489{
3490 list_T *what_list = NULL;
3491
Bram Moolenaar93a10962022-06-16 11:42:09 +01003492 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003493 return;
3494
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003495 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3496 return;
3497
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003498 if (argvars[0].v_type != VAR_UNKNOWN)
3499 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003500 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003501 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003502 what_list = argvars[0].vval.v_list;
3503 }
3504 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003505}
3506#endif
3507
3508/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003509 * Returns TRUE when using a user-defined function for thesaurus completion.
3510 */
3511 static int
3512thesaurus_func_complete(int type UNUSED)
3513{
3514#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003515 return type == CTRL_X_THESAURUS
3516 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003517#else
3518 return FALSE;
3519#endif
3520}
3521
3522/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003523 * Return value of process_next_cpt_value()
3524 */
3525enum
3526{
3527 INS_COMPL_CPT_OK = 1,
3528 INS_COMPL_CPT_CONT,
3529 INS_COMPL_CPT_END
3530};
3531
3532/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003533 * state information used for getting the next set of insert completion
3534 * matches.
3535 */
3536typedef struct
3537{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003538 char_u *e_cpt_copy; // copy of 'complete'
3539 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003540 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003541 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003542 pos_T prev_match_pos; // previous match position
3543 int set_match_pos; // save first_match_pos/last_match_pos
3544 pos_T first_match_pos; // first match position
3545 pos_T last_match_pos; // last match position
3546 int found_all; // found all matches of a certain type.
3547 char_u *dict; // dictionary file to search
3548 int dict_f; // "dict" is an exact file name or not
3549} ins_compl_next_state_T;
3550
3551/*
3552 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003553 *
3554 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003555 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003556 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003557 * st->found_all - all matches of this type are found
3558 * st->ins_buf - search for completions in this buffer
3559 * st->first_match_pos - position of the first completion match
3560 * st->last_match_pos - position of the last completion match
3561 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003562 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003563 * st->dict - name of the dictionary or thesaurus file to search
3564 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003565 *
3566 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003567 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3568 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003569 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003570 */
3571 static int
3572process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003573 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003574 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003575 pos_T *start_match_pos,
3576 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003577{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003578 int compl_type = -1;
3579 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003580
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003581 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003582
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003583 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3584 st->e_cpt++;
3585
3586 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003587 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003588 st->ins_buf = curbuf;
3589 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003590 // Move the cursor back one character so that ^N can match the
3591 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003592 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 {
3594 // Move the cursor to after the last character in the
3595 // buffer, so that word at start of buffer is found
3596 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003597 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003598 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003599 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003600 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003601 compl_type = 0;
3602
3603 // Remember the first match so that the loop stops when we
3604 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003607 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003608 && (st->ins_buf = ins_compl_next_buf(
3609 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 {
3611 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003612 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003613 {
3614 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003615 st->first_match_pos.col = st->last_match_pos.col = 0;
3616 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3617 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618 compl_type = 0;
3619 }
3620 else // unloaded buffer, scan like dictionary
3621 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003622 st->found_all = TRUE;
3623 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 {
3625 status = INS_COMPL_CPT_CONT;
3626 goto done;
3627 }
3628 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003629 st->dict = st->ins_buf->b_fname;
3630 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003631 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003632 if (!shortmess(SHM_COMPLETIONSCAN))
3633 {
3634 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3635 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3636 st->ins_buf->b_fname == NULL
3637 ? buf_spname(st->ins_buf)
3638 : st->ins_buf->b_sfname == NULL
3639 ? st->ins_buf->b_fname
3640 : st->ins_buf->b_sfname);
3641 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3642 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003643 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003644 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003645 status = INS_COMPL_CPT_END;
3646 else
3647 {
3648 if (ctrl_x_mode_line_or_eval())
3649 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003650 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003651 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003652 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 compl_type = CTRL_X_DICTIONARY;
3654 else
3655 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003656 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003657 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003658 st->dict = st->e_cpt;
3659 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 }
3661 }
3662#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003663 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003664 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003665 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666 compl_type = CTRL_X_PATH_DEFINES;
3667#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003668 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003670 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003671 if (!shortmess(SHM_COMPLETIONSCAN))
3672 {
3673 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3674 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3675 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3676 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003677 }
3678 else
3679 compl_type = -1;
3680
3681 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003682 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003683
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003684 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003685 if (compl_type == -1)
3686 status = INS_COMPL_CPT_CONT;
3687 }
3688
3689done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691 return status;
3692}
3693
3694#ifdef FEAT_FIND_ID
3695/*
3696 * Get the next set of identifiers or defines matching "compl_pattern" in
3697 * included files.
3698 */
3699 static void
3700get_next_include_file_completion(int compl_type)
3701{
John Marriott5e6ea922024-11-23 14:01:57 +01003702 find_pattern_in_path(compl_pattern.string, compl_direction,
3703 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003704 (compl_type == CTRL_X_PATH_DEFINES
3705 && !(compl_cont_status & CONT_SOL))
3706 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003707 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708}
3709#endif
3710
3711/*
3712 * Get the next set of words matching "compl_pattern" in dictionary or
3713 * thesaurus files.
3714 */
3715 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003716get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003717{
3718#ifdef FEAT_COMPL_FUNC
3719 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003720 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003721 else
3722#endif
3723 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003724 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003725 : (compl_type == CTRL_X_THESAURUS
3726 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3727 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003728 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003729 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003730 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003731}
3732
3733/*
3734 * Get the next set of tag names matching "compl_pattern".
3735 */
3736 static void
3737get_next_tag_completion(void)
3738{
3739 int save_p_ic;
3740 char_u **matches;
3741 int num_matches;
3742
3743 // set p_ic according to p_ic, p_scs and pat for find_tags().
3744 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003745 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003746
3747 // Find up to TAG_MANY matches. Avoids that an enormous number
3748 // of matches is found when compl_pattern is empty
3749 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003750 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003751 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003752 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003753 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3754 ins_compl_add_matches(num_matches, matches, p_ic);
3755 g_tag_at_cursor = FALSE;
3756 p_ic = save_p_ic;
3757}
3758
3759/*
glepnir8159fb12024-07-17 20:32:54 +02003760 * Compare function for qsort
3761 */
3762static int compare_scores(const void *a, const void *b)
3763{
3764 int idx_a = *(const int *)a;
3765 int idx_b = *(const int *)b;
3766 int score_a = compl_fuzzy_scores[idx_a];
3767 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003768 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3769 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003770}
3771
3772/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003773 * Get the next set of filename matching "compl_pattern".
3774 */
3775 static void
3776get_next_filename_completion(void)
3777{
3778 char_u **matches;
3779 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003780 char_u *ptr;
3781 garray_T fuzzy_indices;
3782 int i;
3783 int score;
3784 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003785 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003786 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3787 char_u **sorted_matches;
3788 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003789 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003790
glepnirb9de1a02024-08-02 19:14:38 +02003791#ifdef BACKSLASH_IN_FILENAME
3792 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3793 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3794#else
3795 char pathsep = PATHSEP;
3796#endif
3797
glepnir8159fb12024-07-17 20:32:54 +02003798 if (in_fuzzy)
3799 {
glepnirb9de1a02024-08-02 19:14:38 +02003800#ifdef BACKSLASH_IN_FILENAME
3801 if (curbuf->b_p_csl[0] == 's')
3802 {
John Marriott5e6ea922024-11-23 14:01:57 +01003803 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003804 {
3805 if (leader[i] == '\\')
3806 leader[i] = '/';
3807 }
3808 }
3809 else if (curbuf->b_p_csl[0] == 'b')
3810 {
John Marriott5e6ea922024-11-23 14:01:57 +01003811 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003812 {
3813 if (leader[i] == '/')
3814 leader[i] = '\\';
3815 }
3816 }
3817#endif
3818 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003819 if (last_sep == NULL)
3820 {
3821 // No path separator or separator is the last character,
3822 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003823 VIM_CLEAR_STRING(compl_pattern);
3824 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3825 if (compl_pattern.string == NULL)
3826 return;
3827 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003828 }
3829 else if (*(last_sep + 1) == '\0')
3830 in_fuzzy = FALSE;
3831 else
3832 {
3833 // Split leader into path and file parts
3834 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003835 char_u *path_with_wildcard;
3836
3837 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003838 if (path_with_wildcard != NULL)
3839 {
John Marriott5e6ea922024-11-23 14:01:57 +01003840 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3841 VIM_CLEAR_STRING(compl_pattern);
3842 compl_pattern.string = path_with_wildcard;
3843 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003844
3845 // Move leader to the file part
3846 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003847 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003848 }
3849 }
glepnir8159fb12024-07-17 20:32:54 +02003850 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003851
John Marriott5e6ea922024-11-23 14:01:57 +01003852 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003853 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3854 return;
3855
3856 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003857 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003858#ifdef BACKSLASH_IN_FILENAME
3859 if (curbuf->b_p_csl[0] != NUL)
3860 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003861 for (i = 0; i < num_matches; ++i)
3862 {
glepnir8159fb12024-07-17 20:32:54 +02003863 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003864 while (*ptr != NUL)
3865 {
3866 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3867 *ptr = '/';
3868 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3869 *ptr = '\\';
3870 ptr += (*mb_ptr2len)(ptr);
3871 }
3872 }
3873 }
3874#endif
glepnir8159fb12024-07-17 20:32:54 +02003875
3876 if (in_fuzzy)
3877 {
3878 ga_init2(&fuzzy_indices, sizeof(int), 10);
3879 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3880
3881 for (i = 0; i < num_matches; i++)
3882 {
3883 ptr = matches[i];
3884 score = fuzzy_match_str(ptr, leader);
3885 if (score > 0)
3886 {
3887 if (ga_grow(&fuzzy_indices, 1) == OK)
3888 {
3889 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3890 compl_fuzzy_scores[i] = score;
3891 fuzzy_indices.ga_len++;
3892 }
3893 }
3894 }
3895
Christian Brabandt220474d2024-07-20 13:26:44 +02003896 // prevent qsort from deref NULL pointer
3897 if (fuzzy_indices.ga_len > 0)
3898 {
3899 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3900 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003901
Christian Brabandt220474d2024-07-20 13:26:44 +02003902 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3903 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3904 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003905
Christian Brabandt220474d2024-07-20 13:26:44 +02003906 FreeWild(num_matches, matches);
3907 matches = sorted_matches;
3908 num_matches = fuzzy_indices.ga_len;
3909 }
glepnir6b6280c2024-07-27 16:25:45 +02003910 else if (leader_len > 0)
3911 {
3912 FreeWild(num_matches, matches);
3913 num_matches = 0;
3914 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003915
glepnir8159fb12024-07-17 20:32:54 +02003916 vim_free(compl_fuzzy_scores);
3917 ga_clear(&fuzzy_indices);
3918 }
3919
glepnir6b6280c2024-07-27 16:25:45 +02003920 if (num_matches > 0)
3921 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003922}
3923
3924/*
3925 * Get the next set of command-line completions matching "compl_pattern".
3926 */
3927 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003928get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003929{
3930 char_u **matches;
3931 int num_matches;
3932
John Marriott5e6ea922024-11-23 14:01:57 +01003933 if (expand_cmdline(&compl_xp, compl_pattern.string,
3934 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003935 ins_compl_add_matches(num_matches, matches, FALSE);
3936}
3937
3938/*
3939 * Get the next set of spell suggestions matching "compl_pattern".
3940 */
3941 static void
3942get_next_spell_completion(linenr_T lnum UNUSED)
3943{
3944#ifdef FEAT_SPELL
3945 char_u **matches;
3946 int num_matches;
3947
John Marriott5e6ea922024-11-23 14:01:57 +01003948 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003949 if (num_matches > 0)
3950 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003951 else
3952 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003953#endif
3954}
3955
3956/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003957 * Return the next word or line from buffer "ins_buf" at position
3958 * "cur_match_pos" for completion. The length of the match is set in "len".
3959 */
3960 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003961ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003962 buf_T *ins_buf, // buffer being scanned
3963 pos_T *cur_match_pos, // current match position
3964 int *match_len,
3965 int *cont_s_ipos) // next ^X<> will set initial_pos
3966{
3967 char_u *ptr;
3968 int len;
3969
3970 *match_len = 0;
3971 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3972 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003973 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003974 if (ctrl_x_mode_line_or_eval())
3975 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003976 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003977 {
3978 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3979 return NULL;
3980 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003981 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003982 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003983 {
3984 char_u *tmp_ptr = ptr;
3985
3986 ptr = skipwhite(tmp_ptr);
3987 len -= (int)(ptr - tmp_ptr);
3988 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003989 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003990 }
3991 else
3992 {
3993 char_u *tmp_ptr = ptr;
3994
John Marriott5e6ea922024-11-23 14:01:57 +01003995 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003996 {
3997 tmp_ptr += compl_length;
3998 // Skip if already inside a word.
3999 if (vim_iswordp(tmp_ptr))
4000 return NULL;
4001 // Find start of next word.
4002 tmp_ptr = find_word_start(tmp_ptr);
4003 }
4004 // Find end of this word.
4005 tmp_ptr = find_word_end(tmp_ptr);
4006 len = (int)(tmp_ptr - ptr);
4007
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004008 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004009 {
4010 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4011 {
4012 // Try next line, if any. the new word will be
4013 // "join" as if the normal command "J" was used.
4014 // IOSIZE is always greater than
4015 // compl_length, so the next STRNCPY always
4016 // works -- Acevedo
4017 STRNCPY(IObuff, ptr, len);
4018 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4019 tmp_ptr = ptr = skipwhite(ptr);
4020 // Find start of next word.
4021 tmp_ptr = find_word_start(tmp_ptr);
4022 // Find end of next word.
4023 tmp_ptr = find_word_end(tmp_ptr);
4024 if (tmp_ptr > ptr)
4025 {
4026 if (*ptr != ')' && IObuff[len - 1] != TAB)
4027 {
4028 if (IObuff[len - 1] != ' ')
4029 IObuff[len++] = ' ';
4030 // IObuf =~ "\k.* ", thus len >= 2
4031 if (p_js
4032 && (IObuff[len - 2] == '.'
4033 || (vim_strchr(p_cpo, CPO_JOINSP)
4034 == NULL
4035 && (IObuff[len - 2] == '?'
4036 || IObuff[len - 2] == '!'))))
4037 IObuff[len++] = ' ';
4038 }
4039 // copy as much as possible of the new word
4040 if (tmp_ptr - ptr >= IOSIZE - len)
4041 tmp_ptr = ptr + IOSIZE - len - 1;
4042 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4043 len += (int)(tmp_ptr - ptr);
4044 *cont_s_ipos = TRUE;
4045 }
4046 IObuff[len] = NUL;
4047 ptr = IObuff;
4048 }
4049 if (len == compl_length)
4050 return NULL;
4051 }
4052 }
4053
4054 *match_len = len;
4055 return ptr;
4056}
4057
4058/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004059 * Get the next set of words matching "compl_pattern" for default completion(s)
4060 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004061 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4062 * position "st->start_pos" in the "compl_direction" direction. If
4063 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4064 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004065 * Returns OK if a new next match is found, otherwise returns FAIL.
4066 */
4067 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004068get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004069{
4070 int found_new_match = FAIL;
4071 int save_p_scs;
4072 int save_p_ws;
4073 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004074 char_u *ptr = NULL;
4075 int len = 0;
4076 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
4077 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004078
4079 // If 'infercase' is set, don't use 'smartcase' here
4080 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004081 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004082 p_scs = FALSE;
4083
4084 // Buffers other than curbuf are scanned from the beginning or the
4085 // end but never from the middle, thus setting nowrapscan in this
4086 // buffer is a good idea, on the other hand, we always set
4087 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4088 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004089 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004090 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004091 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004092 p_ws = TRUE;
4093 looped_around = FALSE;
4094 for (;;)
4095 {
4096 int cont_s_ipos = FALSE;
4097
4098 ++msg_silent; // Don't want messages for wrapscan.
4099
4100 // ctrl_x_mode_line_or_eval() || word-wise search that
4101 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02004102 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004103 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01004104 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02004105 else if (in_fuzzy)
4106 found_new_match = search_for_fuzzy_match(st->ins_buf,
4107 st->cur_match_pos, leader, compl_direction,
4108 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004109 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004110 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004111 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004112 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004113 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004114 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004115 {
4116 // set "compl_started" even on fail
4117 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004118 st->first_match_pos = *st->cur_match_pos;
4119 st->last_match_pos = *st->cur_match_pos;
4120 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004121 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004122 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4123 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004124 {
4125 found_new_match = FAIL;
4126 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004127 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004128 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4129 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4130 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004131 {
4132 if (looped_around)
4133 found_new_match = FAIL;
4134 else
4135 looped_around = TRUE;
4136 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004137 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004138 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4139 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4140 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004141 {
4142 if (looped_around)
4143 found_new_match = FAIL;
4144 else
4145 looped_around = TRUE;
4146 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004147 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004148 if (found_new_match == FAIL)
4149 break;
4150
4151 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004152 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004153 && start_pos->lnum == st->cur_match_pos->lnum
4154 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004155 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004156
glepnir8159fb12024-07-17 20:32:54 +02004157 if (!in_fuzzy)
4158 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4159 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004160 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004161 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004162
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004163 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004164 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004165 0, cont_s_ipos) != NOTDONE)
4166 {
4167 found_new_match = OK;
4168 break;
4169 }
4170 }
4171 p_scs = save_p_scs;
4172 p_ws = save_p_ws;
4173
4174 return found_new_match;
4175}
4176
4177/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004178 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004179 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004180 */
4181 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004182get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004183{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004184 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004185
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004186 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004187 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004188 case -1:
4189 break;
4190#ifdef FEAT_FIND_ID
4191 case CTRL_X_PATH_PATTERNS:
4192 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004193 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004194 break;
4195#endif
4196
4197 case CTRL_X_DICTIONARY:
4198 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004199 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4200 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004201 break;
4202
4203 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004204 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004205 break;
4206
4207 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004208 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209 break;
4210
4211 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004212 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004213 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004214 break;
4215
4216#ifdef FEAT_COMPL_FUNC
4217 case CTRL_X_FUNCTION:
4218 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004219 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004220 break;
4221#endif
4222
4223 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004224 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004225 break;
4226
4227 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004228 found_new_match = get_next_default_completion(st, ini);
4229 if (found_new_match == FAIL && st->ins_buf == curbuf)
4230 st->found_all = TRUE;
4231 }
4232
4233 // check if compl_curr_match has changed, (e.g. other type of
4234 // expansion added something)
4235 if (type != 0 && compl_curr_match != compl_old_match)
4236 found_new_match = OK;
4237
4238 return found_new_match;
4239}
4240
4241/*
4242 * Get the next expansion(s), using "compl_pattern".
4243 * The search starts at position "ini" in curbuf and in the direction
4244 * compl_direction.
4245 * When "compl_started" is FALSE start at that position, otherwise continue
4246 * where we stopped searching before.
4247 * This may return before finding all the matches.
4248 * Return the total number of matches or -1 if still unknown -- Acevedo
4249 */
4250 static int
4251ins_compl_get_exp(pos_T *ini)
4252{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004253 static ins_compl_next_state_T st;
4254 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004255 int i;
4256 int found_new_match;
4257 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004258 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004259
4260 if (!compl_started)
4261 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004262 buf_T *buf;
4263
4264 FOR_ALL_BUFFERS(buf)
4265 buf->b_scanned = 0;
4266 if (!st_cleared)
4267 {
4268 CLEAR_FIELD(st);
4269 st_cleared = TRUE;
4270 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004271 st.found_all = FALSE;
4272 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004273 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004274 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004275 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4276 ? (char_u *)"." : curbuf->b_p_cpt);
4277 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004278 st.last_match_pos = st.first_match_pos = *ini;
4279 }
4280 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4281 st.ins_buf = curbuf; // In case the buffer was wiped out.
4282
4283 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004284 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004285 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004286
4287 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4288 for (;;)
4289 {
4290 found_new_match = FAIL;
4291 st.set_match_pos = FALSE;
4292
4293 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4294 // or if found_all says this entry is done. For ^X^L only use the
4295 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004296 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004297 && (!compl_started || st.found_all))
4298 {
glepnir8159fb12024-07-17 20:32:54 +02004299 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004300
4301 if (status == INS_COMPL_CPT_END)
4302 break;
4303 if (status == INS_COMPL_CPT_CONT)
4304 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004305 }
4306
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004307 // If complete() was called then compl_pattern has been reset. The
4308 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004309 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004310 break;
4311
4312 // get the next set of completion matches
4313 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004314
4315 // break the loop for specialized modes (use 'complete' just for the
4316 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4317 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004318 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4319 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004320 {
4321 if (got_int)
4322 break;
4323 // Fill the popup menu as soon as possible.
4324 if (type != -1)
4325 ins_compl_check_keys(0, FALSE);
4326
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004327 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004328 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4329 break;
4330 compl_started = TRUE;
4331 }
4332 else
4333 {
4334 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004335 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004336 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004337
4338 compl_started = FALSE;
4339 }
4340 }
4341 compl_started = TRUE;
4342
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004343 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004344 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004345 found_new_match = FAIL;
4346
4347 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004348 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004349 && !ctrl_x_mode_line_or_eval()))
4350 i = ins_compl_make_cyclic();
4351
4352 if (compl_old_match != NULL)
4353 {
4354 // If several matches were added (FORWARD) or the search failed and has
4355 // just been made cyclic then we have to move compl_curr_match to the
4356 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004357 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004358 : compl_old_match->cp_prev;
4359 if (compl_curr_match == NULL)
4360 compl_curr_match = compl_old_match;
4361 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004362 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004363
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004364 return i;
4365}
4366
4367/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004368 * Update "compl_shown_match" to the actually shown match, it may differ when
4369 * "compl_leader" is used to omit some of the matches.
4370 */
4371 static void
4372ins_compl_update_shown_match(void)
4373{
4374 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004375 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004376 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004377 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004378 compl_shown_match = compl_shown_match->cp_next;
4379
4380 // If we didn't find it searching forward, and compl_shows_dir is
4381 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004382 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004383 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004384 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004385 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004386 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004387 {
4388 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004389 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004390 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004391 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004392 compl_shown_match = compl_shown_match->cp_prev;
4393 }
4394}
4395
4396/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004397 * Delete the old text being completed.
4398 */
4399 void
4400ins_compl_delete(void)
4401{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004402 // In insert mode: Delete the typed part.
4403 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004404 int col = compl_col + (compl_status_adding() ? compl_length : 0);
John Marriottf4b36412025-02-23 09:09:59 +01004405 string_T remaining = {NULL, 0};
glepnir76bdb822025-02-08 19:04:51 +01004406 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01004407 int has_preinsert = ins_compl_preinsert_effect();
4408 if (has_preinsert)
4409 {
glepnirbfb4eea2025-01-31 15:28:29 +01004410 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004411 curwin->w_cursor.col = compl_ins_end_col;
4412 }
4413
glepnir76bdb822025-02-08 19:04:51 +01004414 if (curwin->w_cursor.lnum > compl_lnum)
4415 {
4416 if (curwin->w_cursor.col < ml_get_curline_len())
4417 {
John Marriottf4b36412025-02-23 09:09:59 +01004418 char_u *line = ml_get_cursor();
4419 remaining.length = ml_get_cursor_len();
4420 remaining.string = vim_strnsave(line, remaining.length);
4421 if (remaining.string == NULL)
glepnir76bdb822025-02-08 19:04:51 +01004422 return;
4423 }
4424 while (curwin->w_cursor.lnum > compl_lnum)
4425 {
4426 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
4427 {
John Marriottf4b36412025-02-23 09:09:59 +01004428 if (remaining.string)
4429 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004430 return;
4431 }
zeertzjq060e6552025-02-21 20:06:26 +01004432 deleted_lines_mark(curwin->w_cursor.lnum, 1L);
glepnir76bdb822025-02-08 19:04:51 +01004433 curwin->w_cursor.lnum--;
4434 }
4435 // move cursor to end of line
4436 curwin->w_cursor.col = ml_get_curline_len();
4437 }
4438
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004439 if ((int)curwin->w_cursor.col > col)
4440 {
4441 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01004442 {
John Marriottf4b36412025-02-23 09:09:59 +01004443 if (remaining.string)
4444 vim_free(remaining.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004445 return;
glepnire3647c82025-02-10 21:16:32 +01004446 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004447 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004448 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004449 }
4450
John Marriottf4b36412025-02-23 09:09:59 +01004451 if (remaining.string != NULL)
glepnir76bdb822025-02-08 19:04:51 +01004452 {
4453 orig_col = curwin->w_cursor.col;
John Marriottf4b36412025-02-23 09:09:59 +01004454 ins_str(remaining.string, remaining.length);
glepnir76bdb822025-02-08 19:04:51 +01004455 curwin->w_cursor.col = orig_col;
John Marriottf4b36412025-02-23 09:09:59 +01004456 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004457 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004458 // TODO: is this sufficient for redrawing? Redrawing everything causes
4459 // flicker, thus we can't do that.
4460 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004461#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004462 // clear v:completed_item
4463 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004464#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004465}
4466
4467/*
glepnir76bdb822025-02-08 19:04:51 +01004468 * Insert a completion string that contains newlines.
4469 * The string is split and inserted line by line.
4470 */
4471 static void
4472ins_compl_expand_multiple(char_u *str)
4473{
4474 char_u *start = str;
4475 char_u *curr = str;
4476
4477 while (*curr != NUL)
4478 {
4479 if (*curr == '\n')
4480 {
4481 // Insert the text chunk before newline
4482 if (curr > start)
4483 ins_char_bytes(start, (int)(curr - start));
4484
4485 // Handle newline
4486 open_line(FORWARD, OPENLINE_KEEPTRAIL, FALSE, NULL);
4487 start = curr + 1;
4488 }
4489 curr++;
4490 }
4491
4492 // Handle remaining text after last newline (if any)
4493 if (curr > start)
4494 ins_char_bytes(start, (int)(curr - start));
4495
4496 compl_ins_end_col = curwin->w_cursor.col;
4497}
4498
4499/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004500 * Insert the new text being completed.
4501 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004502 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4503 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004504 */
4505 void
glepniredd4ac32025-01-29 18:53:51 +01004506ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004507{
glepnir76bdb822025-02-08 19:04:51 +01004508 int compl_len = get_compl_len();
4509 int preinsert = ins_compl_has_preinsert();
4510 char_u *cp_str = compl_shown_match->cp_str.string;
4511 size_t cp_str_len = compl_shown_match->cp_str.length;
4512 size_t leader_len = ins_compl_leader_len();
4513 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004514
4515 // Make sure we don't go over the end of the string, this can happen with
4516 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004517 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004518 {
glepnir76bdb822025-02-08 19:04:51 +01004519 if (has_multiple)
4520 ins_compl_expand_multiple(cp_str + compl_len);
4521 else
4522 {
4523 ins_compl_insert_bytes(cp_str + compl_len, -1);
4524 if (preinsert && move_cursor)
4525 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
4526 }
glepniredd4ac32025-01-29 18:53:51 +01004527 }
4528 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004529 compl_used_match = FALSE;
4530 else
4531 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004532#ifdef FEAT_EVAL
4533 {
4534 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4535
4536 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4537 }
4538#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004539 if (!in_compl_func)
4540 compl_curr_match = compl_shown_match;
4541}
4542
4543/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004544 * show the file name for the completion match (if any). Truncate the file
4545 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004546 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004547 static void
4548ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004549{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004550 char *lead = _("match in file");
4551 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4552 char_u *s;
4553 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004554
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004555 if (space <= 0)
4556 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004557
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004558 // We need the tail that fits. With double-byte encoding going
4559 // back from the end is very slow, thus go from the start and keep
4560 // the text that fits in "space" between "s" and "e".
4561 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004562 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004563 space -= ptr2cells(e);
4564 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004565 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004566 space += ptr2cells(s);
4567 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004568 }
4569 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004570 msg_hist_off = TRUE;
4571 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4572 s > compl_shown_match->cp_fname ? "<" : "", s);
4573 msg((char *)IObuff);
4574 msg_hist_off = FALSE;
4575 redraw_cmdline = FALSE; // don't overwrite!
4576}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004577
glepnirdca57fb2024-06-04 22:01:21 +02004578/*
zeertzjq551d8c32024-06-05 19:53:32 +02004579 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004580 */
glepnira218cc62024-06-03 19:32:39 +02004581 static compl_T *
4582find_comp_when_fuzzy(void)
4583{
4584 int score;
4585 char_u* str;
4586 int target_idx = -1;
4587 int is_forward = compl_shows_dir_forward();
4588 int is_backward = compl_shows_dir_backward();
4589 compl_T *comp = NULL;
4590
glepnir43eef882024-06-19 20:20:48 +02004591 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004592 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004593 return compl_first_match != compl_shown_match ? compl_first_match :
4594 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004595
4596 if (is_forward)
4597 target_idx = compl_selected_item + 1;
4598 else if (is_backward)
4599 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004600 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004601
4602 score = compl_match_array[target_idx].pum_score;
4603 str = compl_match_array[target_idx].pum_text;
4604
4605 comp = compl_first_match;
4606 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004607 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004608 return comp;
4609 comp = comp->cp_next;
4610 } while (comp != NULL && !is_first_match(comp));
4611
4612 return NULL;
4613}
4614
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004615/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004616 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004617 * times. The number of matches found is returned in 'num_matches'.
4618 *
4619 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4620 * get more completions. If it is FALSE, then do nothing when there are no more
4621 * completions in the given direction.
4622 *
4623 * If "advance" is TRUE, then completion will move to the first match.
4624 * Otherwise, the original text will be shown.
4625 *
4626 * Returns OK on success and -1 if the number of matches are unknown.
4627 */
4628 static int
4629find_next_completion_match(
4630 int allow_get_expansion,
4631 int todo, // repeat completion this many times
4632 int advance,
4633 int *num_matches)
4634{
4635 int found_end = FALSE;
4636 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004637 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004638 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4639 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004640
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004641 while (--todo >= 0)
4642 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004643 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004644 {
glepniraced8c22024-06-15 15:13:05 +02004645 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4646 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004647 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004648 && (is_first_match(compl_shown_match->cp_next)
4649 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004650 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004651 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004652 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004653 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004654 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004655 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4656 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004657 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004658 }
4659 else
4660 {
4661 if (!allow_get_expansion)
4662 {
4663 if (advance)
4664 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004665 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004666 compl_pending -= todo + 1;
4667 else
4668 compl_pending += todo + 1;
4669 }
4670 return -1;
4671 }
4672
4673 if (!compl_no_select && advance)
4674 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004675 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004676 --compl_pending;
4677 else
4678 ++compl_pending;
4679 }
4680
4681 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004682 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004683
4684 // handle any pending completions
4685 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004686 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004687 {
4688 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4689 {
4690 compl_shown_match = compl_shown_match->cp_next;
4691 --compl_pending;
4692 }
4693 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4694 {
4695 compl_shown_match = compl_shown_match->cp_prev;
4696 ++compl_pending;
4697 }
4698 else
4699 break;
4700 }
4701 found_end = FALSE;
4702 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004703 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004704 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004705 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004706 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004707 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004708 ++todo;
4709 else
4710 // Remember a matching item.
4711 found_compl = compl_shown_match;
4712
4713 // Stop at the end of the list when we found a usable match.
4714 if (found_end)
4715 {
4716 if (found_compl != NULL)
4717 {
4718 compl_shown_match = found_compl;
4719 break;
4720 }
4721 todo = 1; // use first usable match after wrapping around
4722 }
4723 }
4724
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004725 return OK;
4726}
4727
4728/*
4729 * Fill in the next completion in the current direction.
4730 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4731 * get more completions. If it is FALSE, then we just do nothing when there
4732 * are no more completions in a given direction. The latter case is used when
4733 * we are still in the middle of finding completions, to allow browsing
4734 * through the ones found so far.
4735 * Return the total number of matches, or -1 if still unknown -- webb.
4736 *
4737 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4738 * compl_shown_match here.
4739 *
4740 * Note that this function may be called recursively once only. First with
4741 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4742 * calls this function with "allow_get_expansion" FALSE.
4743 */
4744 static int
4745ins_compl_next(
4746 int allow_get_expansion,
4747 int count, // repeat completion this many times; should
4748 // be at least 1
4749 int insert_match, // Insert the newly selected match
4750 int in_compl_func) // called from complete_check()
4751{
4752 int num_matches = -1;
4753 int todo = count;
4754 int advance;
4755 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004756 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004757 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004758 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4759 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004760 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004761
4762 // When user complete function return -1 for findstart which is next
4763 // time of 'always', compl_shown_match become NULL.
4764 if (compl_shown_match == NULL)
4765 return -1;
4766
John Marriott5e6ea922024-11-23 14:01:57 +01004767 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004768 && !match_at_original_text(compl_shown_match)
4769 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004770 // Update "compl_shown_match" to the actually shown match
4771 ins_compl_update_shown_match();
4772
4773 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004774 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004775 // Delete old text to be replaced
4776 ins_compl_delete();
4777
4778 // When finding the longest common text we stick at the original text,
4779 // don't let CTRL-N or CTRL-P move to the first match.
4780 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4781
4782 // When restarting the search don't insert the first match either.
4783 if (compl_restarting)
4784 {
4785 advance = FALSE;
4786 compl_restarting = FALSE;
4787 }
4788
4789 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4790 // around.
4791 if (find_next_completion_match(allow_get_expansion, todo, advance,
4792 &num_matches) == -1)
4793 return -1;
4794
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004795 if (curbuf != orig_curbuf)
4796 {
4797 // In case some completion function switched buffer, don't want to
4798 // insert the completion elsewhere.
4799 return -1;
4800 }
4801
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004802 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01004803 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004804 {
glepnir6a38aff2024-12-16 21:56:16 +01004805 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004806 compl_used_match = FALSE;
4807 }
4808 else if (insert_match)
4809 {
4810 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01004811 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004812 else
glepnir6a38aff2024-12-16 21:56:16 +01004813 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004814 }
4815 else
4816 compl_used_match = FALSE;
4817
4818 if (!allow_get_expansion)
4819 {
4820 // may undisplay the popup menu first
4821 ins_compl_upd_pum();
4822
4823 if (pum_enough_matches())
4824 // Will display the popup menu, don't redraw yet to avoid flicker.
4825 pum_call_update_screen();
4826 else
4827 // Not showing the popup menu yet, redraw to show the user what was
4828 // inserted.
4829 update_screen(0);
4830
4831 // display the updated popup menu
4832 ins_compl_show_pum();
4833#ifdef FEAT_GUI
4834 if (gui.in_use)
4835 {
4836 // Show the cursor after the match, not after the redrawn text.
4837 setcursor();
4838 out_flush_cursor(FALSE, FALSE);
4839 }
4840#endif
4841
4842 // Delete old text to be replaced, since we're still searching and
4843 // don't want to match ourselves!
4844 ins_compl_delete();
4845 }
4846
4847 // Enter will select a match when the match wasn't inserted and the popup
4848 // menu is visible.
4849 if (compl_no_insert && !started)
4850 compl_enter_selects = TRUE;
4851 else
4852 compl_enter_selects = !insert_match && compl_match_array != NULL;
4853
4854 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004855 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004856 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004857
4858 return num_matches;
4859}
4860
4861/*
4862 * Call this while finding completions, to check whether the user has hit a key
4863 * that should change the currently displayed completion, or exit completion
4864 * mode. Also, when compl_pending is not zero, show a completion as soon as
4865 * possible. -- webb
4866 * "frequency" specifies out of how many calls we actually check.
4867 * "in_compl_func" is TRUE when called from complete_check(), don't set
4868 * compl_curr_match.
4869 */
4870 void
4871ins_compl_check_keys(int frequency, int in_compl_func)
4872{
4873 static int count = 0;
4874 int c;
4875
4876 // Don't check when reading keys from a script, :normal or feedkeys().
4877 // That would break the test scripts. But do check for keys when called
4878 // from complete_check().
4879 if (!in_compl_func && (using_script() || ex_normal_busy))
4880 return;
4881
4882 // Only do this at regular intervals
4883 if (++count < frequency)
4884 return;
4885 count = 0;
4886
4887 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4888 // can't do its work correctly.
4889 c = vpeekc_any();
4890 if (c != NUL)
4891 {
4892 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4893 {
4894 c = safe_vgetc(); // Eat the character
4895 compl_shows_dir = ins_compl_key2dir(c);
4896 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4897 c != K_UP && c != K_DOWN, in_compl_func);
4898 }
4899 else
4900 {
4901 // Need to get the character to have KeyTyped set. We'll put it
4902 // back with vungetc() below. But skip K_IGNORE.
4903 c = safe_vgetc();
4904 if (c != K_IGNORE)
4905 {
4906 // Don't interrupt completion when the character wasn't typed,
4907 // e.g., when doing @q to replay keys.
4908 if (c != Ctrl_R && KeyTyped)
4909 compl_interrupted = TRUE;
4910
4911 vungetc(c);
4912 }
4913 }
4914 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004915 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004916 {
4917 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4918
4919 compl_pending = 0;
4920 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4921 }
4922}
4923
4924/*
4925 * Decide the direction of Insert mode complete from the key typed.
4926 * Returns BACKWARD or FORWARD.
4927 */
4928 static int
4929ins_compl_key2dir(int c)
4930{
4931 if (c == Ctrl_P || c == Ctrl_L
4932 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4933 return BACKWARD;
4934 return FORWARD;
4935}
4936
4937/*
4938 * Return TRUE for keys that are used for completion only when the popup menu
4939 * is visible.
4940 */
4941 static int
4942ins_compl_pum_key(int c)
4943{
4944 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4945 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4946 || c == K_UP || c == K_DOWN);
4947}
4948
4949/*
4950 * Decide the number of completions to move forward.
4951 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4952 */
4953 static int
4954ins_compl_key2count(int c)
4955{
4956 int h;
4957
4958 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4959 {
4960 h = pum_get_height();
4961 if (h > 3)
4962 h -= 2; // keep some context
4963 return h;
4964 }
4965 return 1;
4966}
4967
4968/*
4969 * Return TRUE if completion with "c" should insert the match, FALSE if only
4970 * to change the currently selected completion.
4971 */
4972 static int
4973ins_compl_use_match(int c)
4974{
4975 switch (c)
4976 {
4977 case K_UP:
4978 case K_DOWN:
4979 case K_PAGEDOWN:
4980 case K_KPAGEDOWN:
4981 case K_S_DOWN:
4982 case K_PAGEUP:
4983 case K_KPAGEUP:
4984 case K_S_UP:
4985 return FALSE;
4986 }
4987 return TRUE;
4988}
4989
4990/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004991 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4992 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004993 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004994 * Uses the global variables: compl_cont_status and ctrl_x_mode
4995 */
4996 static int
4997get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4998{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004999 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005000 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005001 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005002 {
5003 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5004 ;
5005 compl_col += ++startcol;
5006 compl_length = curs_col - startcol;
5007 }
5008 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005009 {
John Marriott5e6ea922024-11-23 14:01:57 +01005010 compl_pattern.string = str_foldcase(line + compl_col,
5011 compl_length, NULL, 0);
5012 if (compl_pattern.string == NULL)
5013 {
5014 compl_pattern.length = 0;
5015 return FAIL;
5016 }
5017 compl_pattern.length = STRLEN(compl_pattern.string);
5018 }
5019 else
5020 {
5021 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5022 if (compl_pattern.string == NULL)
5023 {
5024 compl_pattern.length = 0;
5025 return FAIL;
5026 }
5027 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005028 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005029 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005030 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005031 {
5032 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005033 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005034 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005035
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005036 if (!vim_iswordp(line + compl_col)
5037 || (compl_col > 0
5038 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005039 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005040 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005041 prefixlen = 0;
5042 }
John Marriott5e6ea922024-11-23 14:01:57 +01005043
5044 // we need up to 2 extra chars for the prefix
5045 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5046 compl_pattern.string = alloc(n);
5047 if (compl_pattern.string == NULL)
5048 {
5049 compl_pattern.length = 0;
5050 return FAIL;
5051 }
5052 STRCPY((char *)compl_pattern.string, prefix);
5053 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005054 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005055 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005056 }
5057 else if (--startcol < 0
5058 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5059 {
John Marriott5e6ea922024-11-23 14:01:57 +01005060 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5061
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005062 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005063 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5064 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005065 {
John Marriott5e6ea922024-11-23 14:01:57 +01005066 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005067 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005068 }
John Marriott5e6ea922024-11-23 14:01:57 +01005069 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005070 compl_col += curs_col;
5071 compl_length = 0;
5072 }
5073 else
5074 {
5075 // Search the point of change class of multibyte character
5076 // or not a word single byte character backward.
5077 if (has_mbyte)
5078 {
5079 int base_class;
5080 int head_off;
5081
5082 startcol -= (*mb_head_off)(line, line + startcol);
5083 base_class = mb_get_class(line + startcol);
5084 while (--startcol >= 0)
5085 {
5086 head_off = (*mb_head_off)(line, line + startcol);
5087 if (base_class != mb_get_class(line + startcol
5088 - head_off))
5089 break;
5090 startcol -= head_off;
5091 }
5092 }
5093 else
5094 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5095 ;
5096 compl_col += ++startcol;
5097 compl_length = (int)curs_col - startcol;
5098 if (compl_length == 1)
5099 {
5100 // Only match word with at least two chars -- webb
5101 // there's no need to call quote_meta,
5102 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005103 compl_pattern.string = alloc(7);
5104 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005105 {
John Marriott5e6ea922024-11-23 14:01:57 +01005106 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005107 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005108 }
John Marriott5e6ea922024-11-23 14:01:57 +01005109 STRCPY((char *)compl_pattern.string, "\\<");
5110 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5111 STRCAT((char *)compl_pattern.string, "\\k");
5112 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005113 }
5114 else
5115 {
John Marriott5e6ea922024-11-23 14:01:57 +01005116 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5117
5118 compl_pattern.string = alloc(n);
5119 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005120 {
John Marriott5e6ea922024-11-23 14:01:57 +01005121 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005122 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005123 }
John Marriott5e6ea922024-11-23 14:01:57 +01005124 STRCPY((char *)compl_pattern.string, "\\<");
5125 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005126 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005127 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005128 }
5129 }
5130
5131 return OK;
5132}
5133
5134/*
5135 * Get the pattern, column and length for whole line completion or for the
5136 * complete() function.
5137 * Sets the global variables: compl_col, compl_length and compl_pattern.
5138 */
5139 static int
5140get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5141{
5142 compl_col = (colnr_T)getwhitecols(line);
5143 compl_length = (int)curs_col - (int)compl_col;
5144 if (compl_length < 0) // cursor in indent: empty pattern
5145 compl_length = 0;
5146 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005147 {
John Marriott5e6ea922024-11-23 14:01:57 +01005148 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5149 NULL, 0);
5150 if (compl_pattern.string == NULL)
5151 {
5152 compl_pattern.length = 0;
5153 return FAIL;
5154 }
5155 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005156 }
John Marriott5e6ea922024-11-23 14:01:57 +01005157 else
5158 {
5159 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5160 if (compl_pattern.string == NULL)
5161 {
5162 compl_pattern.length = 0;
5163 return FAIL;
5164 }
5165 compl_pattern.length = (size_t)compl_length;
5166 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005167
5168 return OK;
5169}
5170
5171/*
5172 * Get the pattern, column and length for filename completion.
5173 * Sets the global variables: compl_col, compl_length and compl_pattern.
5174 */
5175 static int
5176get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5177{
5178 // Go back to just before the first filename character.
5179 if (startcol > 0)
5180 {
5181 char_u *p = line + startcol;
5182
5183 MB_PTR_BACK(line, p);
5184 while (p > line && vim_isfilec(PTR2CHAR(p)))
5185 MB_PTR_BACK(line, p);
5186 if (p == line && vim_isfilec(PTR2CHAR(p)))
5187 startcol = 0;
5188 else
5189 startcol = (int)(p - line) + 1;
5190 }
5191
5192 compl_col += startcol;
5193 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005194 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5195 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005196 {
John Marriott5e6ea922024-11-23 14:01:57 +01005197 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005198 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005199 }
5200
John Marriott5e6ea922024-11-23 14:01:57 +01005201 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005202
5203 return OK;
5204}
5205
5206/*
5207 * Get the pattern, column and length for command-line completion.
5208 * Sets the global variables: compl_col, compl_length and compl_pattern.
5209 */
5210 static int
5211get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5212{
John Marriott5e6ea922024-11-23 14:01:57 +01005213 compl_pattern.string = vim_strnsave(line, curs_col);
5214 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005215 {
John Marriott5e6ea922024-11-23 14:01:57 +01005216 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005217 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005218 }
John Marriott5e6ea922024-11-23 14:01:57 +01005219 compl_pattern.length = curs_col;
5220 set_cmd_context(&compl_xp, compl_pattern.string,
5221 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005222 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5223 || compl_xp.xp_context == EXPAND_NOTHING)
5224 // No completion possible, use an empty pattern to get a
5225 // "pattern not found" message.
5226 compl_col = curs_col;
5227 else
John Marriott5e6ea922024-11-23 14:01:57 +01005228 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005229 compl_length = curs_col - compl_col;
5230
5231 return OK;
5232}
5233
5234/*
5235 * Get the pattern, column and length for user defined completion ('omnifunc',
5236 * 'completefunc' and 'thesaurusfunc')
5237 * Sets the global variables: compl_col, compl_length and compl_pattern.
5238 * Uses the global variable: spell_bad_len
5239 */
5240 static int
5241get_userdefined_compl_info(colnr_T curs_col UNUSED)
5242{
5243 int ret = FAIL;
5244
5245#ifdef FEAT_COMPL_FUNC
5246 // Call user defined function 'completefunc' with "a:findstart"
5247 // set to 1 to obtain the length of text to use for completion.
5248 char_u *line;
5249 typval_T args[3];
5250 int col;
5251 char_u *funcname;
5252 pos_T pos;
5253 int save_State = State;
5254 callback_T *cb;
5255
5256 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5257 // length as a string
5258 funcname = get_complete_funcname(ctrl_x_mode);
5259 if (*funcname == NUL)
5260 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005261 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005262 ? "completefunc" : "omnifunc");
5263 return FAIL;
5264 }
5265
5266 args[0].v_type = VAR_NUMBER;
5267 args[0].vval.v_number = 1;
5268 args[1].v_type = VAR_STRING;
5269 args[1].vval.v_string = (char_u *)"";
5270 args[2].v_type = VAR_UNKNOWN;
5271 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005272 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005273 cb = get_insert_callback(ctrl_x_mode);
5274 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005275 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005276
5277 State = save_State;
5278 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005279 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005280 validate_cursor();
5281 if (!EQUAL_POS(curwin->w_cursor, pos))
5282 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005283 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005284 return FAIL;
5285 }
5286
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005287 // Return value -2 means the user complete function wants to cancel the
5288 // complete without an error, do the same if the function did not execute
5289 // successfully.
5290 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005291 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005292 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005293 if (col == -3)
5294 {
5295 ctrl_x_mode = CTRL_X_NORMAL;
5296 edit_submode = NULL;
5297 if (!shortmess(SHM_COMPLETIONMENU))
5298 msg_clr_cmdline();
5299 return FAIL;
5300 }
5301
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005302 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005303 // completion.
5304 compl_opt_refresh_always = FALSE;
5305 compl_opt_suppress_empty = FALSE;
5306
5307 if (col < 0)
5308 col = curs_col;
5309 compl_col = col;
5310 if (compl_col > curs_col)
5311 compl_col = curs_col;
5312
5313 // Setup variables for completion. Need to obtain "line" again,
5314 // it may have become invalid.
5315 line = ml_get(curwin->w_cursor.lnum);
5316 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005317 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5318 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005319 {
John Marriott5e6ea922024-11-23 14:01:57 +01005320 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005321 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005322 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005323
John Marriott5e6ea922024-11-23 14:01:57 +01005324 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005325 ret = OK;
5326#endif
5327
5328 return ret;
5329}
5330
5331/*
5332 * Get the pattern, column and length for spell completion.
5333 * Sets the global variables: compl_col, compl_length and compl_pattern.
5334 * Uses the global variable: spell_bad_len
5335 */
5336 static int
5337get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5338{
5339 int ret = FAIL;
5340#ifdef FEAT_SPELL
5341 char_u *line;
5342
5343 if (spell_bad_len > 0)
5344 compl_col = curs_col - spell_bad_len;
5345 else
5346 compl_col = spell_word_start(startcol);
5347 if (compl_col >= (colnr_T)startcol)
5348 {
5349 compl_length = 0;
5350 compl_col = curs_col;
5351 }
5352 else
5353 {
5354 spell_expand_check_cap(compl_col);
5355 compl_length = (int)curs_col - compl_col;
5356 }
5357 // Need to obtain "line" again, it may have become invalid.
5358 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005359 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5360 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005361 {
John Marriott5e6ea922024-11-23 14:01:57 +01005362 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005363 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005364 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005365
John Marriott5e6ea922024-11-23 14:01:57 +01005366 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005367 ret = OK;
5368#endif
5369
5370 return ret;
5371}
5372
5373/*
5374 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005375 * "startcol" - start column number of the completion pattern/text
5376 * "cur_col" - current cursor column
5377 * On return, "line_invalid" is set to TRUE, if the current line may have
5378 * become invalid and needs to be fetched again.
5379 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005380 */
5381 static int
5382compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5383{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005384 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005385 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5386 && !thesaurus_func_complete(ctrl_x_mode)))
5387 {
5388 return get_normal_compl_info(line, startcol, curs_col);
5389 }
5390 else if (ctrl_x_mode_line_or_eval())
5391 {
5392 return get_wholeline_compl_info(line, curs_col);
5393 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005394 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005395 {
5396 return get_filename_compl_info(line, startcol, curs_col);
5397 }
5398 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5399 {
5400 return get_cmdline_compl_info(line, curs_col);
5401 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005402 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005403 || thesaurus_func_complete(ctrl_x_mode))
5404 {
5405 if (get_userdefined_compl_info(curs_col) == FAIL)
5406 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005407 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005408 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005409 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005410 {
5411 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5412 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005413 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005414 }
5415 else
5416 {
5417 internal_error("ins_complete()");
5418 return FAIL;
5419 }
5420
5421 return OK;
5422}
5423
5424/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005425 * Continue an interrupted completion mode search in "line".
5426 *
5427 * If this same ctrl_x_mode has been interrupted use the text from
5428 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5429 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5430 * the same line as the cursor then fix it (the line has been split because it
5431 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5432 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005433 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005434 static void
5435ins_compl_continue_search(char_u *line)
5436{
5437 // it is a continued search
5438 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005439 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5440 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005441 {
5442 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5443 {
5444 // line (probably) wrapped, set compl_startpos to the
5445 // first non_blank in the line, if it is not a wordchar
5446 // include it to get a better pattern, but then we don't
5447 // want the "\\<" prefix, check it below
5448 compl_col = (colnr_T)getwhitecols(line);
5449 compl_startpos.col = compl_col;
5450 compl_startpos.lnum = curwin->w_cursor.lnum;
5451 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5452 }
5453 else
5454 {
5455 // S_IPOS was set when we inserted a word that was at the
5456 // beginning of the line, which means that we'll go to SOL
5457 // mode but first we need to redefine compl_startpos
5458 if (compl_cont_status & CONT_S_IPOS)
5459 {
5460 compl_cont_status |= CONT_SOL;
5461 compl_startpos.col = (colnr_T)(skipwhite(
5462 line + compl_length
5463 + compl_startpos.col) - line);
5464 }
5465 compl_col = compl_startpos.col;
5466 }
5467 compl_length = curwin->w_cursor.col - (int)compl_col;
5468 // IObuff is used to add a "word from the next line" would we
5469 // have enough space? just being paranoid
5470#define MIN_SPACE 75
5471 if (compl_length > (IOSIZE - MIN_SPACE))
5472 {
5473 compl_cont_status &= ~CONT_SOL;
5474 compl_length = (IOSIZE - MIN_SPACE);
5475 compl_col = curwin->w_cursor.col - compl_length;
5476 }
5477 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5478 if (compl_length < 1)
5479 compl_cont_status &= CONT_LOCAL;
5480 }
5481 else if (ctrl_x_mode_line_or_eval())
5482 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5483 else
5484 compl_cont_status = 0;
5485}
5486
5487/*
5488 * start insert mode completion
5489 */
5490 static int
5491ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005492{
5493 char_u *line;
5494 int startcol = 0; // column where searched text starts
5495 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005496 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005497 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005498 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005499
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005500 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005501
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005502 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005503 did_si = FALSE;
5504 can_si = FALSE;
5505 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005506 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005507 return FAIL;
5508
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005509 line = ml_get(curwin->w_cursor.lnum);
5510 curs_col = curwin->w_cursor.col;
5511 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01005512 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005513
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005514 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5515 && compl_cont_mode == ctrl_x_mode)
5516 // this same ctrl-x_mode was interrupted previously. Continue the
5517 // completion.
5518 ins_compl_continue_search(line);
5519 else
5520 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005521
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005522 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005523 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005524 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005525 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005526 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5527 compl_cont_status = 0;
5528 compl_cont_status |= CONT_N_ADDS;
5529 compl_startpos = curwin->w_cursor;
5530 startcol = (int)curs_col;
5531 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005532 }
5533
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005534 // Work out completion pattern and original text -- webb
5535 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5536 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005537 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5538 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005539 // restore did_ai, so that adding comment leader works
5540 did_ai = save_did_ai;
5541 return FAIL;
5542 }
5543 // If "line" was changed while getting completion info get it again.
5544 if (line_invalid)
5545 line = ml_get(curwin->w_cursor.lnum);
5546
glepnir7cfe6932024-09-15 20:06:28 +02005547 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005548 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005549 {
5550 edit_submode_pre = (char_u *)_(" Adding");
5551 if (ctrl_x_mode_line_or_eval())
5552 {
5553 // Insert a new line, keep indentation but ignore 'comments'.
5554 char_u *old = curbuf->b_p_com;
5555
5556 curbuf->b_p_com = (char_u *)"";
5557 compl_startpos.lnum = curwin->w_cursor.lnum;
5558 compl_startpos.col = compl_col;
5559 ins_eol('\r');
5560 curbuf->b_p_com = old;
5561 compl_length = 0;
5562 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01005563 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005564 }
glepnir7cfe6932024-09-15 20:06:28 +02005565 else if (ctrl_x_mode_normal() && in_fuzzy)
5566 {
5567 compl_startpos = curwin->w_cursor;
5568 compl_cont_status &= CONT_S_IPOS;
5569 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005570 }
5571 else
5572 {
5573 edit_submode_pre = NULL;
5574 compl_startpos.col = compl_col;
5575 }
5576
5577 if (compl_cont_status & CONT_LOCAL)
5578 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5579 else
5580 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5581
5582 // If any of the original typed text has been changed we need to fix
5583 // the redo buffer.
5584 ins_compl_fixRedoBufForLeader(NULL);
5585
5586 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005587 VIM_CLEAR_STRING(compl_orig_text);
5588 compl_orig_text.length = (size_t)compl_length;
5589 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005590 if (p_ic)
5591 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005592 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5593 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005594 {
John Marriott5e6ea922024-11-23 14:01:57 +01005595 VIM_CLEAR_STRING(compl_pattern);
5596 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005597 return FAIL;
5598 }
5599
5600 // showmode might reset the internal line pointers, so it must
5601 // be called before line = ml_get(), or when this address is no
5602 // longer needed. -- Acevedo.
5603 edit_submode_extra = (char_u *)_("-- Searching...");
5604 edit_submode_highl = HLF_COUNT;
5605 showmode();
5606 edit_submode_extra = NULL;
5607 out_flush();
5608
5609 return OK;
5610}
5611
5612/*
5613 * display the completion status message
5614 */
5615 static void
5616ins_compl_show_statusmsg(void)
5617{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005618 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005619 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005620 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005621 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005622 ? (char_u *)_("Hit end of paragraph")
5623 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005624 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005625 }
5626
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005627 if (edit_submode_extra == NULL)
5628 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005629 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005630 {
5631 edit_submode_extra = (char_u *)_("Back at original");
5632 edit_submode_highl = HLF_W;
5633 }
5634 else if (compl_cont_status & CONT_S_IPOS)
5635 {
5636 edit_submode_extra = (char_u *)_("Word from other line");
5637 edit_submode_highl = HLF_COUNT;
5638 }
5639 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5640 {
5641 edit_submode_extra = (char_u *)_("The only match");
5642 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005643 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005644 }
5645 else
5646 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005647#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005648 // Update completion sequence number when needed.
5649 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005650 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005651#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005652 // The match should always have a sequence number now, this is
5653 // just a safety check.
5654 if (compl_curr_match->cp_number != -1)
5655 {
5656 // Space for 10 text chars. + 2x10-digit no.s = 31.
5657 // Translations may need more than twice that.
5658 static char_u match_ref[81];
5659
5660 if (compl_matches > 0)
5661 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005662 _("match %d of %d"),
5663 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005664 else
5665 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005666 _("match %d"),
5667 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005668 edit_submode_extra = match_ref;
5669 edit_submode_highl = HLF_R;
5670 if (dollar_vcol >= 0)
5671 curs_columns(FALSE);
5672 }
5673 }
5674 }
5675
5676 // Show a message about what (completion) mode we're in.
5677 if (!compl_opt_suppress_empty)
5678 {
5679 showmode();
5680 if (!shortmess(SHM_COMPLETIONMENU))
5681 {
5682 if (edit_submode_extra != NULL)
5683 {
5684 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005685 {
5686 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005687 msg_attr((char *)edit_submode_extra,
5688 edit_submode_highl < HLF_COUNT
5689 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005690 msg_hist_off = FALSE;
5691 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005692 }
5693 else
5694 msg_clr_cmdline(); // necessary for "noshowmode"
5695 }
5696 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005697}
5698
5699/*
5700 * Do Insert mode completion.
5701 * Called when character "c" was typed, which has a meaning for completion.
5702 * Returns OK if completion was done, FAIL if something failed (out of mem).
5703 */
5704 int
5705ins_complete(int c, int enable_pum)
5706{
5707 int n;
5708 int save_w_wrow;
5709 int save_w_leftcol;
5710 int insert_match;
5711
5712 compl_direction = ins_compl_key2dir(c);
5713 insert_match = ins_compl_use_match(c);
5714
5715 if (!compl_started)
5716 {
5717 if (ins_compl_start() == FAIL)
5718 return FAIL;
5719 }
5720 else if (insert_match && stop_arrow() == FAIL)
5721 return FAIL;
5722
5723 compl_shown_match = compl_curr_match;
5724 compl_shows_dir = compl_direction;
5725
5726 // Find next match (and following matches).
5727 save_w_wrow = curwin->w_wrow;
5728 save_w_leftcol = curwin->w_leftcol;
5729 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5730
5731 // may undisplay the popup menu
5732 ins_compl_upd_pum();
5733
5734 if (n > 1) // all matches have been found
5735 compl_matches = n;
5736 compl_curr_match = compl_shown_match;
5737 compl_direction = compl_shows_dir;
5738
5739 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5740 // mode.
5741 if (got_int && !global_busy)
5742 {
5743 (void)vgetc();
5744 got_int = FALSE;
5745 }
5746
5747 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005748 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005749 {
5750 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5751 // because we couldn't expand anything at first place, but if we used
5752 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5753 // (such as M in M'exico) if not tried already. -- Acevedo
5754 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005755 || compl_status_adding()
5756 || (ctrl_x_mode_not_default()
5757 && !ctrl_x_mode_path_patterns()
5758 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005759 compl_cont_status &= ~CONT_N_ADDS;
5760 }
5761
5762 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5763 compl_cont_status |= CONT_S_IPOS;
5764 else
5765 compl_cont_status &= ~CONT_S_IPOS;
5766
5767 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005768
5769 // Show the popup menu, unless we got interrupted.
5770 if (enable_pum && !compl_interrupted)
5771 show_pum(save_w_wrow, save_w_leftcol);
5772
5773 compl_was_interrupted = compl_interrupted;
5774 compl_interrupted = FALSE;
5775
5776 return OK;
5777}
5778
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005779/*
5780 * Remove (if needed) and show the popup menu
5781 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005782 static void
5783show_pum(int prev_w_wrow, int prev_w_leftcol)
5784{
5785 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005786 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005787 RedrawingDisabled = 0;
5788
5789 // If the cursor moved or the display scrolled we need to remove the pum
5790 // first.
5791 setcursor();
5792 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5793 ins_compl_del_pum();
5794
5795 ins_compl_show_pum();
5796 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005797
5798 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005799}
5800
5801/*
5802 * Looks in the first "len" chars. of "src" for search-metachars.
5803 * If dest is not NULL the chars. are copied there quoting (with
5804 * a backslash) the metachars, and dest would be NUL terminated.
5805 * Returns the length (needed) of dest
5806 */
5807 static unsigned
5808quote_meta(char_u *dest, char_u *src, int len)
5809{
5810 unsigned m = (unsigned)len + 1; // one extra for the NUL
5811
5812 for ( ; --len >= 0; src++)
5813 {
5814 switch (*src)
5815 {
5816 case '.':
5817 case '*':
5818 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005819 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005820 break;
5821 // FALLTHROUGH
5822 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005823 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005824 break;
5825 // FALLTHROUGH
5826 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005827 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005828 break;
5829 // FALLTHROUGH
5830 case '^': // currently it's not needed.
5831 case '$':
5832 m++;
5833 if (dest != NULL)
5834 *dest++ = '\\';
5835 break;
5836 }
5837 if (dest != NULL)
5838 *dest++ = *src;
5839 // Copy remaining bytes of a multibyte character.
5840 if (has_mbyte)
5841 {
5842 int i, mb_len;
5843
5844 mb_len = (*mb_ptr2len)(src) - 1;
5845 if (mb_len > 0 && len >= mb_len)
5846 for (i = 0; i < mb_len; ++i)
5847 {
5848 --len;
5849 ++src;
5850 if (dest != NULL)
5851 *dest++ = *src;
5852 }
5853 }
5854 }
5855 if (dest != NULL)
5856 *dest = NUL;
5857
5858 return m;
5859}
5860
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005861#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005862 void
5863free_insexpand_stuff(void)
5864{
John Marriott5e6ea922024-11-23 14:01:57 +01005865 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005866# ifdef FEAT_EVAL
5867 free_callback(&cfu_cb);
5868 free_callback(&ofu_cb);
5869 free_callback(&tsrfu_cb);
5870# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005871}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005872#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005873
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005874#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005875/*
5876 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5877 * spelled word, if there is one.
5878 */
5879 static void
5880spell_back_to_badword(void)
5881{
5882 pos_T tpos = curwin->w_cursor;
5883
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005884 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005885 if (curwin->w_cursor.col != tpos.col)
5886 start_arrow(&tpos);
5887}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005888#endif