blob: 3592fdcaf2b4ee08b04c4667234dd73f76c0aa9f [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
glepnir05460682025-05-26 18:23:27 +020041# define CTRL_X_REGISTER 18 // complete words from registers
Bram Moolenaar7591bb32019-03-30 13:53:47 +010042
43# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
44
45// Message for CTRL-X mode, index is ctrl_x_mode.
46static char *ctrl_x_msgs[] =
47{
48 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
glepnir05460682025-05-26 18:23:27 +020049 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^Rs^U^V^Y)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010050 NULL, // CTRL_X_SCROLL: depends on state
51 N_(" Whole line completion (^L^N^P)"),
52 N_(" File name completion (^F^N^P)"),
53 N_(" Tag completion (^]^N^P)"),
54 N_(" Path pattern completion (^N^P)"),
55 N_(" Definition completion (^D^N^P)"),
56 NULL, // CTRL_X_FINISHED
57 N_(" Dictionary completion (^K^N^P)"),
58 N_(" Thesaurus completion (^T^N^P)"),
59 N_(" Command-line completion (^V^N^P)"),
60 N_(" User defined completion (^U^N^P)"),
61 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020062 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010063 N_(" Keyword Local completion (^N^P)"),
64 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020065 N_(" Command-line completion (^V^N^P)"),
glepnir05460682025-05-26 18:23:27 +020066 N_(" Register completion (^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010067};
68
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020069#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010070static char *ctrl_x_mode_names[] = {
Hirohito Higashi355db992025-04-28 18:07:02 +020071 "keyword",
72 "ctrl_x",
73 "scroll",
74 "whole_line",
75 "files",
76 "tags",
77 "path_patterns",
78 "path_defines",
79 "unknown", // CTRL_X_FINISHED
80 "dictionary",
81 "thesaurus",
82 "cmdline",
83 "function",
84 "omni",
85 "spell",
86 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
87 "eval",
88 "cmdline",
glepnir05460682025-05-26 18:23:27 +020089 "register",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010090};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020091#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010092
93/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010094 * Structure used to store one match for insert completion.
95 */
96typedef struct compl_S compl_T;
97struct compl_S
98{
99 compl_T *cp_next;
100 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +0100101 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +0100102 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200103 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100104#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100105 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100106#endif
glepnir0fe17f82024-10-08 22:26:44 +0200107 char_u *cp_fname; // file containing the match, allocated when
108 // cp_flags has CP_FREE_FNAME
109 int cp_flags; // CP_ values
110 int cp_number; // sequence number
Girish Palyab1565882025-04-15 20:16:00 +0200111 int cp_score; // fuzzy match score or proximity score
glepnird4088ed2024-12-31 10:55:22 +0100112 int cp_in_match_array; // collected by compl_match_array
glepnir7baa0142024-10-09 20:19:25 +0200113 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200114 int cp_user_kind_hlattr; // highlight attribute for kind
Girish Palya0ac1eb32025-04-16 20:18:33 +0200115 int cp_cpt_source_idx; // index of this match's source in 'cpt' option
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126/*
127 * All the current matches are stored in a list.
128 * "compl_first_match" points to the start of the list.
129 * "compl_curr_match" points to the currently selected entry.
130 * "compl_shown_match" is different from compl_curr_match during
Girish Palyacbe53192025-04-14 22:13:15 +0200131 * ins_compl_get_exp(), when new matches are added to the list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000132 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100133 */
134static compl_T *compl_first_match = NULL;
135static compl_T *compl_curr_match = NULL;
136static compl_T *compl_shown_match = NULL;
137static compl_T *compl_old_match = NULL;
138
glepnirf31cfa22025-03-06 21:59:13 +0100139// list used to store the compl_T which have the max score
140// used for completefuzzycollect
141static compl_T **compl_best_matches = NULL;
142static int compl_num_bests = 0;
143// inserted a longest when completefuzzycollect enabled
144static int compl_cfc_longest_ins = FALSE;
145
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100146// After using a cursor key <Enter> selects a match in the popup menu,
147// otherwise it inserts a line break.
148static int compl_enter_selects = FALSE;
149
150// When "compl_leader" is not NULL only matches that start with this string
151// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100152static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100153
154static int compl_get_longest = FALSE; // put longest common string
155 // in compl_leader
156
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Girish Palyacbe53192025-04-14 22:13:15 +0200178static string_T compl_pattern = {NULL, 0}; // search pattern for matching items
179#ifdef FEAT_COMPL_FUNC
180static string_T cpt_compl_pattern = {NULL, 0}; // pattern returned by func in 'cpt'
181#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100182static int compl_direction = FORWARD;
183static int compl_shows_dir = FORWARD;
184static int compl_pending = 0; // > 1 for postponed CTRL-N
185static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000186// Length in bytes of the text being completed (this is deleted to be replaced
187// by the match.)
188static int compl_length = 0;
glepnir76bdb822025-02-08 19:04:51 +0100189static linenr_T compl_lnum = 0; // lnum where the completion start
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100190static colnr_T compl_col = 0; // column where the text starts
191 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100192static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100193static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100194 // completion started
195static int compl_cont_mode = 0;
196static expand_T compl_xp;
197
glepnircf7f0122025-04-15 19:02:00 +0200198static win_T *compl_curr_win = NULL; // win where completion is active
199static buf_T *compl_curr_buf = NULL; // buf where completion is active
200
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000201// List of flags for method of completion.
202static int compl_cont_status = 0;
203# define CONT_ADDING 1 // "normal" or "adding" expansion
204# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
205 // it's set only iff N_ADDS is set
206# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
207# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
208 // if so, word-wise-expansion will set SOL
209# define CONT_SOL 16 // pattern includes start of line, just for
210 // word-wise expansion, not set for ^X^L
211# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
212 // expansion, (eg use complete=.)
213
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100214static int compl_opt_refresh_always = FALSE;
215static int compl_opt_suppress_empty = FALSE;
216
glepnira218cc62024-06-03 19:32:39 +0200217static int compl_selected_item = -1;
218
glepnir8159fb12024-07-17 20:32:54 +0200219static int *compl_fuzzy_scores;
220
Girish Palya0ac1eb32025-04-16 20:18:33 +0200221// Define the structure for completion source (in 'cpt' option) information
222typedef struct cpt_source_T
223{
224 int refresh_always; // Flag array to indicate which 'cpt' functions have 'refresh:always' set
225 int max_matches; // Maximum number of items to display in the menu from the source
226} cpt_source_T;
227
228static cpt_source_T *cpt_sources_array; // Pointer to the array of completion sources
229static int cpt_sources_count; // Total number of completion sources specified in the 'cpt' option
Girish Palya7c621052025-05-26 19:41:59 +0200230static int cpt_sources_index = -1; // Index of the current completion source being expanded
Girish Palyacbe53192025-04-14 22:13:15 +0200231
glepnir6a38aff2024-12-16 21:56:16 +0100232// "compl_match_array" points the currently displayed list of entries in the
233// popup menu. It is NULL when there is no popup menu.
234static pumitem_T *compl_match_array = NULL;
235static int compl_match_arraysize;
236
glepnirf31cfa22025-03-06 21:59:13 +0100237static 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, int score);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100238static void ins_compl_longest_match(compl_T *match);
239static void ins_compl_del_pum(void);
240static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100241static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100242static int ins_compl_need_restart(void);
243static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000244static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100246static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100247static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
248# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
249static void ins_compl_add_list(list_T *list);
250static void ins_compl_add_dict(dict_T *dict);
Girish Palyacbe53192025-04-14 22:13:15 +0200251static int get_userdefined_compl_info(colnr_T curs_col, callback_T *cb, int *startcol);
Girish Palyacbe53192025-04-14 22:13:15 +0200252static void get_cpt_func_completion_matches(callback_T *cb);
Girish Palya0ac1eb32025-04-16 20:18:33 +0200253static callback_T *get_cpt_func_callback(char_u *funcname);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100254# endif
Girish Palya0ac1eb32025-04-16 20:18:33 +0200255static int cpt_sources_init(void);
Girish Palyacbe53192025-04-14 22:13:15 +0200256static int is_cpt_func_refresh_always(void);
Girish Palya0ac1eb32025-04-16 20:18:33 +0200257static void cpt_sources_clear(void);
Girish Palyacbe53192025-04-14 22:13:15 +0200258static void cpt_compl_refresh(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100259static int ins_compl_key2dir(int c);
260static int ins_compl_pum_key(int c);
261static int ins_compl_key2count(int c);
262static void show_pum(int prev_w_wrow, int prev_w_leftcol);
263static unsigned quote_meta(char_u *dest, char_u *str, int len);
glepnir76bdb822025-02-08 19:04:51 +0100264static int ins_compl_has_multiple(void);
265static void ins_compl_expand_multiple(char_u *str);
glepnirf31cfa22025-03-06 21:59:13 +0100266static void ins_compl_longest_insert(char_u *prefix);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100267
268#ifdef FEAT_SPELL
269static void spell_back_to_badword(void);
270static int spell_bad_len = 0; // length of located bad word
271#endif
272
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100273/*
274 * CTRL-X pressed in Insert mode.
275 */
276 void
277ins_ctrl_x(void)
278{
zeertzjqdca29d92021-08-31 19:12:51 +0200279 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100280 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000281 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100282 if (compl_cont_status & CONT_N_ADDS)
283 compl_cont_status |= CONT_INTRPT;
284 else
285 compl_cont_status = 0;
286 // We're not sure which CTRL-X mode it will be yet
287 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
288 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
289 edit_submode_pre = NULL;
290 showmode();
291 }
zeertzjqdca29d92021-08-31 19:12:51 +0200292 else
293 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
294 // CTRL-V look like CTRL-N
295 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100296
LemonBoy2bf52dd2022-04-09 18:17:34 +0100297 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100298}
299
300/*
301 * Functions to check the current CTRL-X mode.
302 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000303int ctrl_x_mode_none(void)
304 { return ctrl_x_mode == 0; }
305int ctrl_x_mode_normal(void)
306 { return ctrl_x_mode == CTRL_X_NORMAL; }
307int ctrl_x_mode_scroll(void)
308 { return ctrl_x_mode == CTRL_X_SCROLL; }
309int ctrl_x_mode_whole_line(void)
310 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
311int ctrl_x_mode_files(void)
312 { return ctrl_x_mode == CTRL_X_FILES; }
313int ctrl_x_mode_tags(void)
314 { return ctrl_x_mode == CTRL_X_TAGS; }
315int ctrl_x_mode_path_patterns(void)
316 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
317int ctrl_x_mode_path_defines(void)
318 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
319int ctrl_x_mode_dictionary(void)
320 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
321int ctrl_x_mode_thesaurus(void)
322 { return ctrl_x_mode == CTRL_X_THESAURUS; }
323int ctrl_x_mode_cmdline(void)
324 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200325 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000326int ctrl_x_mode_function(void)
327 { return ctrl_x_mode == CTRL_X_FUNCTION; }
328int ctrl_x_mode_omni(void)
329 { return ctrl_x_mode == CTRL_X_OMNI; }
330int ctrl_x_mode_spell(void)
331 { return ctrl_x_mode == CTRL_X_SPELL; }
332static int ctrl_x_mode_eval(void)
333 { return ctrl_x_mode == CTRL_X_EVAL; }
334int ctrl_x_mode_line_or_eval(void)
335 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
glepnir05460682025-05-26 18:23:27 +0200336int ctrl_x_mode_register(void)
337 { return ctrl_x_mode == CTRL_X_REGISTER; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100338
339/*
340 * Whether other than default completion has been selected.
341 */
342 int
343ctrl_x_mode_not_default(void)
344{
345 return ctrl_x_mode != CTRL_X_NORMAL;
346}
347
348/*
zeertzjqdca29d92021-08-31 19:12:51 +0200349 * Whether CTRL-X was typed without a following character,
350 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100351 */
352 int
353ctrl_x_mode_not_defined_yet(void)
354{
355 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
356}
357
358/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000359 * Return TRUE if currently in "normal" or "adding" insert completion matches
360 * state
361 */
362 int
363compl_status_adding(void)
364{
365 return compl_cont_status & CONT_ADDING;
366}
367
368/*
369 * Return TRUE if the completion pattern includes start of line, just for
370 * word-wise expansion.
371 */
372 int
373compl_status_sol(void)
374{
375 return compl_cont_status & CONT_SOL;
376}
377
378/*
379 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
380 */
381 int
382compl_status_local(void)
383{
384 return compl_cont_status & CONT_LOCAL;
385}
386
387/*
388 * Clear the completion status flags
389 */
390 void
391compl_status_clear(void)
392{
393 compl_cont_status = 0;
394}
395
396/*
397 * Return TRUE if completion is using the forward direction matches
398 */
399 static int
400compl_dir_forward(void)
401{
402 return compl_direction == FORWARD;
403}
404
405/*
406 * Return TRUE if currently showing forward completion matches
407 */
408 static int
409compl_shows_dir_forward(void)
410{
411 return compl_shows_dir == FORWARD;
412}
413
414/*
415 * Return TRUE if currently showing backward completion matches
416 */
417 static int
418compl_shows_dir_backward(void)
419{
420 return compl_shows_dir == BACKWARD;
421}
422
423/*
424 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100425 */
426 int
427has_compl_option(int dict_opt)
428{
429 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200430#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100431 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200432#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100433 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100434 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
435#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100436 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100437#endif
438 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100439 {
440 ctrl_x_mode = CTRL_X_NORMAL;
441 edit_submode = NULL;
442 msg_attr(dict_opt ? _("'dictionary' option is empty")
443 : _("'thesaurus' option is empty"),
444 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100445 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100446 {
447 vim_beep(BO_COMPL);
448 setcursor();
449 out_flush();
450#ifdef FEAT_EVAL
451 if (!get_vim_var_nr(VV_TESTING))
452#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100453 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100454 }
455 return FALSE;
456 }
457 return TRUE;
458}
459
460/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000461 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100462 * This depends on the current mode.
463 */
464 int
465vim_is_ctrl_x_key(int c)
466{
467 // Always allow ^R - let its results then be checked
glepnir05460682025-05-26 18:23:27 +0200468 if (c == Ctrl_R && ctrl_x_mode != CTRL_X_REGISTER)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100469 return TRUE;
470
471 // Accept <PageUp> and <PageDown> if the popup menu is visible.
472 if (ins_compl_pum_key(c))
473 return TRUE;
474
475 switch (ctrl_x_mode)
476 {
477 case 0: // Not in any CTRL-X mode
478 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
479 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200480 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100481 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
482 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
483 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
484 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
485 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200486 || c == Ctrl_S || c == Ctrl_K || c == 's'
glepnir05460682025-05-26 18:23:27 +0200487 || c == Ctrl_Z || c == Ctrl_R);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100488 case CTRL_X_SCROLL:
489 return (c == Ctrl_Y || c == Ctrl_E);
490 case CTRL_X_WHOLE_LINE:
491 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
492 case CTRL_X_FILES:
493 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
494 case CTRL_X_DICTIONARY:
495 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
496 case CTRL_X_THESAURUS:
497 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
498 case CTRL_X_TAGS:
499 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
500#ifdef FEAT_FIND_ID
501 case CTRL_X_PATH_PATTERNS:
502 return (c == Ctrl_P || c == Ctrl_N);
503 case CTRL_X_PATH_DEFINES:
504 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
505#endif
506 case CTRL_X_CMDLINE:
507 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
508 || c == Ctrl_X);
509#ifdef FEAT_COMPL_FUNC
510 case CTRL_X_FUNCTION:
511 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
512 case CTRL_X_OMNI:
513 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
514#endif
515 case CTRL_X_SPELL:
516 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
517 case CTRL_X_EVAL:
518 return (c == Ctrl_P || c == Ctrl_N);
glepnir05460682025-05-26 18:23:27 +0200519 case CTRL_X_REGISTER:
520 return (c == Ctrl_R || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100521 }
522 internal_error("vim_is_ctrl_x_key()");
523 return FALSE;
524}
525
526/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000527 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000528 */
529 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000530match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000531{
532 return match->cp_flags & CP_ORIGINAL_TEXT;
533}
534
535/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000536 * Returns TRUE if "match" is the first match in the completion list.
537 */
538 static int
539is_first_match(compl_T *match)
540{
541 return match == compl_first_match;
542}
543
544/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100545 * Return TRUE when character "c" is part of the item currently being
546 * completed. Used to decide whether to abandon complete mode when the menu
547 * is visible.
548 */
549 int
550ins_compl_accept_char(int c)
551{
552 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
553 // When expanding an identifier only accept identifier chars.
554 return vim_isIDc(c);
555
556 switch (ctrl_x_mode)
557 {
558 case CTRL_X_FILES:
559 // When expanding file name only accept file name chars. But not
560 // path separators, so that "proto/<Tab>" expands files in
561 // "proto", not "proto/" as a whole
562 return vim_isfilec(c) && !vim_ispathsep(c);
563
564 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200565 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100566 case CTRL_X_OMNI:
567 // Command line and Omni completion can work with just about any
568 // printable character, but do stop at white space.
569 return vim_isprintc(c) && !VIM_ISWHITE(c);
570
571 case CTRL_X_WHOLE_LINE:
572 // For while line completion a space can be part of the line.
573 return vim_isprintc(c);
574 }
575 return vim_iswordc(c);
576}
577
578/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000579 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100580 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000581 */
582 static char_u *
583ins_compl_infercase_gettext(
glepnir19e1dd62025-05-08 22:50:38 +0200584 char_u *str,
585 int char_len,
586 int compl_char_len,
587 int min_len,
588 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000589{
590 int *wca; // Wide character array.
591 char_u *p;
592 int i, c;
593 int has_lower = FALSE;
594 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100595 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000596
597 IObuff[0] = NUL;
598
599 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100600 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000601 if (wca == NULL)
602 return IObuff;
603
604 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100605 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100606 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000607 if (has_mbyte)
608 wca[i] = mb_ptr2char_adv(&p);
609 else
610 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100611 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000612
613 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100614 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000615 for (i = 0; i < min_len; ++i)
616 {
glepnir6e199932024-12-14 21:13:27 +0100617 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000618 if (MB_ISLOWER(c))
619 {
620 has_lower = TRUE;
621 if (MB_ISUPPER(wca[i]))
622 {
623 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100624 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000625 wca[i] = MB_TOLOWER(wca[i]);
626 break;
627 }
628 }
629 }
630
631 // Rule 2: No lower case, 2nd consecutive letter converted to
632 // upper case.
633 if (!has_lower)
634 {
John Marriott5e6ea922024-11-23 14:01:57 +0100635 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000636 for (i = 0; i < min_len; ++i)
637 {
glepnir6e199932024-12-14 21:13:27 +0100638 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000639 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
640 {
641 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100642 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000643 wca[i] = MB_TOUPPER(wca[i]);
644 break;
645 }
646 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
647 }
648 }
649
650 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100651 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000652 for (i = 0; i < min_len; ++i)
653 {
glepnir6e199932024-12-14 21:13:27 +0100654 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000655 if (MB_ISLOWER(c))
656 wca[i] = MB_TOLOWER(wca[i]);
657 else if (MB_ISUPPER(c))
658 wca[i] = MB_TOUPPER(wca[i]);
659 }
660
661 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000662 p = IObuff;
663 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100664 ga_init2(&gap, 1, 500);
665 while (i < char_len)
666 {
667 if (gap.ga_data != NULL)
668 {
669 if (ga_grow(&gap, 10) == FAIL)
670 {
671 ga_clear(&gap);
672 return (char_u *)"[failed]";
673 }
674 p = (char_u *)gap.ga_data + gap.ga_len;
675 if (has_mbyte)
676 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
677 else
678 {
679 *p = wca[i++];
680 ++gap.ga_len;
681 }
682 }
683 else if ((p - IObuff) + 6 >= IOSIZE)
684 {
685 // Multi-byte characters can occupy up to five bytes more than
686 // ASCII characters, and we also need one byte for NUL, so when
687 // getting to six bytes from the edge of IObuff switch to using a
688 // growarray. Add the character in the next round.
689 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100690 {
691 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100692 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100693 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100694 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100695 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100696 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100697 }
698 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000699 p += (*mb_char2bytes)(wca[i++], p);
700 else
701 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000703 vim_free(wca);
704
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100705 if (gap.ga_data != NULL)
706 {
707 *tofree = gap.ga_data;
708 return gap.ga_data;
709 }
710
711 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000712 return IObuff;
713}
714
715/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
717 * case of the originally typed text is used, and the case of the completed
718 * text is inferred, ie this tries to work out what case you probably wanted
719 * the rest of the word to be in -- webb
720 */
721 int
722ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200723 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100724 int len,
725 int icase,
726 char_u *fname,
727 int dir,
glepnirf31cfa22025-03-06 21:59:13 +0100728 int cont_s_ipos, // next ^X<> will set initial_pos
729 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100730{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200731 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 int char_len; // count multi-byte characters
734 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200736 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 int res;
738 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739
740 if (p_ic && curbuf->b_p_inf && len > 0)
741 {
742 // Infer case of completed part.
743
744 // Find actual length of completion.
745 if (has_mbyte)
746 {
747 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100748 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100749 while (*p != NUL)
750 {
751 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100752 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100753 }
754 }
755 else
glepnir6e199932024-12-14 21:13:27 +0100756 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100757 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100758 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100759
760 // Find actual length of original text.
761 if (has_mbyte)
762 {
John Marriott5e6ea922024-11-23 14:01:57 +0100763 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100764 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100765 while (*p != NUL)
766 {
767 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100768 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100769 }
770 }
771 else
glepnir6e199932024-12-14 21:13:27 +0100772 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100773 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100774 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100775
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100776 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100778 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100779
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100780 str = ins_compl_infercase_gettext(str, char_len,
781 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100782 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200783 if (cont_s_ipos)
784 flags |= CP_CONT_S_IPOS;
785 if (icase)
786 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200787
glepnirf31cfa22025-03-06 21:59:13 +0100788 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL, score);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100789 vim_free(tofree);
790 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100791}
792
793/*
glepnirf31cfa22025-03-06 21:59:13 +0100794 * Check if ctrl_x_mode has been configured in 'completefuzzycollect'
795 */
796 static int
797cfc_has_mode(void)
798{
glepnir58760162025-03-13 21:39:51 +0100799 if (ctrl_x_mode_normal() || ctrl_x_mode_dictionary())
800 return (cfc_flags & CFC_KEYWORD) != 0;
801 else if (ctrl_x_mode_files())
802 return (cfc_flags & CFC_FILES) != 0;
803 else if (ctrl_x_mode_whole_line())
804 return (cfc_flags & CFC_WHOLELINE) != 0;
805 else
806 return FALSE;
glepnirf31cfa22025-03-06 21:59:13 +0100807}
808
809/*
Girish Palyab1565882025-04-15 20:16:00 +0200810 * Returns TRUE if matches should be sorted based on proximity to the cursor.
811 */
812 static int
813is_nearest_active(void)
814{
glepnirc3fbaa02025-05-08 23:05:10 +0200815 return (get_cot_flags() & (COT_NEAREST | COT_FUZZY)) == COT_NEAREST;
Girish Palyab1565882025-04-15 20:16:00 +0200816}
817
818/*
819 * Repositions a match in the completion list based on its proximity score.
820 * If the match is at the head and has a higher score than the next node,
821 * or if it's in the middle/tail and has a lower score than the previous node,
822 * it is moved to the correct position while maintaining ascending order.
823 */
824 static void
825reposition_match(compl_T *match)
826{
827 compl_T *insert_before = NULL;
828 compl_T *insert_after = NULL;
829
830 // Node is at head and score is too big
831 if (!match->cp_prev)
832 {
833 if (match->cp_next && match->cp_next->cp_score > 0 &&
834 match->cp_next->cp_score < match->cp_score)
835 {
836 // <c-p>: compl_first_match is at head and newly inserted node
837 compl_first_match = compl_curr_match = match->cp_next;
838 // Find the correct position in ascending order
839 insert_before = match->cp_next;
840 do
841 {
842 insert_after = insert_before;
843 insert_before = insert_before->cp_next;
844 } while (insert_before && insert_before->cp_score > 0 &&
845 insert_before->cp_score < match->cp_score);
846 }
847 else
848 return;
849 }
850 // Node is at tail or in the middle but score is too small
851 else
852 {
853 if (match->cp_prev->cp_score > 0 && match->cp_prev->cp_score > match->cp_score)
854 {
855 // <c-n>: compl_curr_match (and newly inserted match) is at tail
856 if (!match->cp_next)
857 compl_curr_match = compl_curr_match->cp_prev;
858 // Find the correct position in ascending order
859 insert_after = match->cp_prev;
860 do
861 {
862 insert_before = insert_after;
863 insert_after = insert_after->cp_prev;
864 } while (insert_after && insert_after->cp_score > 0 &&
865 insert_after->cp_score > match->cp_score);
866 }
867 else
868 return;
869 }
870
871 if (insert_after)
872 {
873 // Remove the match from its current position
874 if (match->cp_prev)
875 match->cp_prev->cp_next = match->cp_next;
876 else
877 compl_first_match = match->cp_next;
878 if (match->cp_next)
879 match->cp_next->cp_prev = match->cp_prev;
880
881 // Insert the match at the correct position
882 match->cp_next = insert_before;
883 match->cp_prev = insert_after;
884 insert_after->cp_next = match;
885 insert_before->cp_prev = match;
886 }
887}
888
889/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000890 * Add a match to the list of matches. The arguments are:
891 * str - text of the match to add
892 * len - length of "str". If -1, then the length of "str" is
893 * computed.
894 * fname - file name to associate with this match.
895 * cptext - list of strings to use with this match (for abbr, menu, info
896 * and kind)
897 * user_data - user supplied data (any vim type) for this match
898 * cdir - match direction. If 0, use "compl_direction".
899 * flags_arg - match flags (cp_flags)
900 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100901 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000902 * If "cdir" is FORWARD, then the match is added after the current match.
903 * Otherwise, it is added before the current match.
904 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100905 * If the given string is already in the list of completions, then return
906 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
907 * maybe because alloc() returns NULL, then FAIL is returned.
908 */
909 static int
910ins_compl_add(
911 char_u *str,
912 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100913 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 char_u **cptext, // extra text for popup menu or NULL
915 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100916 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200917 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200918 int adup, // accept duplicate match
glepnirf31cfa22025-03-06 21:59:13 +0100919 int *user_hl, // user abbr/kind hlattr
920 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100921{
glepnirf31cfa22025-03-06 21:59:13 +0100922 compl_T *match, *current, *prev;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100923 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200924 int flags = flags_arg;
glepnirf31cfa22025-03-06 21:59:13 +0100925 int inserted = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100926
Bram Moolenaarceb06192021-04-04 15:05:22 +0200927 if (flags & CP_FAST)
928 fast_breakcheck();
929 else
930 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 if (got_int)
932 return FAIL;
933 if (len < 0)
934 len = (int)STRLEN(str);
935
936 // If the same match is already present, don't add it.
937 if (compl_first_match != NULL && !adup)
938 {
939 match = compl_first_match;
940 do
941 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000942 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100943 && STRNCMP(match->cp_str.string, str, len) == 0
944 && ((int)match->cp_str.length <= len
945 || match->cp_str.string[len] == NUL))
Girish Palyab1565882025-04-15 20:16:00 +0200946 {
947 if (is_nearest_active() && score > 0 && score < match->cp_score)
948 {
949 match->cp_score = score;
950 reposition_match(match);
951 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100952 return NOTDONE;
Girish Palyab1565882025-04-15 20:16:00 +0200953 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100954 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000955 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100956 }
957
958 // Remove any popup menu before changing the list of matches.
959 ins_compl_del_pum();
960
961 // Allocate a new match structure.
962 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200963 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100964 if (match == NULL)
965 return FAIL;
glepnir40891ba2025-02-10 22:18:00 +0100966 match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
John Marriott5e6ea922024-11-23 14:01:57 +0100967 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100968 {
969 vim_free(match);
970 return FAIL;
971 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100972
John Marriott5e6ea922024-11-23 14:01:57 +0100973 match->cp_str.length = len;
974
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100975 // match-fname is:
976 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200977 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100978 // - NULL otherwise. --Acevedo
979 if (fname != NULL
980 && compl_curr_match != NULL
981 && compl_curr_match->cp_fname != NULL
982 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
983 match->cp_fname = compl_curr_match->cp_fname;
984 else if (fname != NULL)
985 {
986 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200987 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100988 }
989 else
990 match->cp_fname = NULL;
991 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100992 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
993 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
glepnirf31cfa22025-03-06 21:59:13 +0100994 match->cp_score = score;
Girish Palya0ac1eb32025-04-16 20:18:33 +0200995 match->cp_cpt_source_idx = cpt_sources_index;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100996
997 if (cptext != NULL)
998 {
glepnir19e1dd62025-05-08 22:50:38 +0200999 for (int i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +01001000 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001001 if (cptext[i] != NULL && *cptext[i] != NUL)
1002 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +01001003 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001004 }
Bram Moolenaarab782c52020-01-04 19:00:11 +01001005#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001006 if (user_data != NULL)
1007 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +01001008#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001009
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001010 // Link the new match structure after (FORWARD) or before (BACKWARD) the
1011 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001012 if (compl_first_match == NULL)
1013 match->cp_next = match->cp_prev = NULL;
glepnirf31cfa22025-03-06 21:59:13 +01001014 else if (cfc_has_mode() && score > 0 && compl_get_longest)
1015 {
1016 current = compl_first_match->cp_next;
1017 prev = compl_first_match;
1018 inserted = FALSE;
1019 // The direction is ignored when using longest and
1020 // completefuzzycollect, because matches are inserted
1021 // and sorted by score.
1022 while (current != NULL && current != compl_first_match)
1023 {
1024 if (current->cp_score < score)
1025 {
Hirohito Higashi355db992025-04-28 18:07:02 +02001026 match->cp_next = current;
1027 match->cp_prev = current->cp_prev;
1028 if (current->cp_prev)
glepnirf31cfa22025-03-06 21:59:13 +01001029 current->cp_prev->cp_next = match;
Hirohito Higashi355db992025-04-28 18:07:02 +02001030 current->cp_prev = match;
1031 inserted = TRUE;
1032 break;
glepnirf31cfa22025-03-06 21:59:13 +01001033 }
1034 prev = current;
1035 current = current->cp_next;
1036 }
1037 if (!inserted)
1038 {
1039 prev->cp_next = match;
1040 match->cp_prev = prev;
1041 match->cp_next = compl_first_match;
1042 compl_first_match->cp_prev = match;
1043 }
1044 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001045 else if (dir == FORWARD)
1046 {
1047 match->cp_next = compl_curr_match->cp_next;
1048 match->cp_prev = compl_curr_match;
1049 }
1050 else // BACKWARD
1051 {
1052 match->cp_next = compl_curr_match;
1053 match->cp_prev = compl_curr_match->cp_prev;
1054 }
1055 if (match->cp_next)
1056 match->cp_next->cp_prev = match;
1057 if (match->cp_prev)
1058 match->cp_prev->cp_next = match;
1059 else // if there's nothing before, it is the first match
1060 compl_first_match = match;
1061 compl_curr_match = match;
1062
Girish Palyab1565882025-04-15 20:16:00 +02001063 if (is_nearest_active() && score > 0)
1064 reposition_match(match);
1065
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001066 // Find the longest common string if still doing that.
glepnirf31cfa22025-03-06 21:59:13 +01001067 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001068 ins_compl_longest_match(match);
1069
1070 return OK;
1071}
1072
1073/*
1074 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001075 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001076 */
1077 static int
1078ins_compl_equal(compl_T *match, char_u *str, int len)
1079{
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001080 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +02001081 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001082 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +01001083 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
1084 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001085}
1086
1087/*
glepnir6a38aff2024-12-16 21:56:16 +01001088 * when len is -1 mean use whole length of p otherwise part of p
1089 */
1090 static void
1091ins_compl_insert_bytes(char_u *p, int len)
1092{
1093 if (len == -1)
1094 len = (int)STRLEN(p);
1095 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +01001096 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +01001097}
1098
1099/*
zeertzjqd32bf0a2024-12-17 20:55:13 +01001100 * Checks if the column is within the currently inserted completion text
1101 * column range. If it is, it returns a special highlight attribute.
glepnir76bdb822025-02-08 19:04:51 +01001102 * -1 means normal item.
glepnir6a38aff2024-12-16 21:56:16 +01001103 */
1104 int
glepnir76bdb822025-02-08 19:04:51 +01001105ins_compl_col_range_attr(linenr_T lnum, int col)
glepnir6a38aff2024-12-16 21:56:16 +01001106{
glepnir76bdb822025-02-08 19:04:51 +01001107 int start_col;
1108 int attr;
1109
1110 if ((get_cot_flags() & COT_FUZZY)
1111 || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0)
glepnire8908872025-01-08 18:30:45 +01001112 return -1;
1113
glepnir76bdb822025-02-08 19:04:51 +01001114 start_col = compl_col + (int)ins_compl_leader_len();
1115 if (!ins_compl_has_multiple())
1116 return (col >= start_col && col < compl_ins_end_col) ? attr : -1;
1117
1118 // Multiple lines
1119 if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) ||
1120 (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) ||
1121 (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col))
1122 return attr;
glepnir6a38aff2024-12-16 21:56:16 +01001123
1124 return -1;
1125}
1126
1127/*
glepnir76bdb822025-02-08 19:04:51 +01001128 * Returns TRUE if the current completion string contains newline characters,
1129 * indicating it's a multi-line completion.
1130 */
1131 static int
1132ins_compl_has_multiple(void)
1133{
1134 return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL;
1135}
1136
1137/*
1138 * Returns TRUE if the given line number falls within the range of a multi-line
1139 * completion, i.e. between the starting line (compl_lnum) and current cursor
1140 * line. Always returns FALSE for single-line completions.
1141 */
1142 int
1143ins_compl_lnum_in_range(linenr_T lnum)
1144{
1145 if (!ins_compl_has_multiple())
1146 return FALSE;
1147 return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum;
1148}
1149
1150/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001151 * Reduce the longest common string for match "match".
1152 */
1153 static void
1154ins_compl_longest_match(compl_T *match)
1155{
1156 char_u *p, *s;
1157 int c1, c2;
1158 int had_match;
1159
John Marriott5e6ea922024-11-23 14:01:57 +01001160 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001161 {
1162 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +01001163 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
1164 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001165 return;
1166
John Marriott5e6ea922024-11-23 14:01:57 +01001167 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001168 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001169 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001170
1171 // When the match isn't there (to avoid matching itself) remove it
1172 // again after redrawing.
1173 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001174 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001175 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001176
1177 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001178 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001179
1180 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +01001181 p = compl_leader.string;
1182 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001183 while (*p != NUL)
1184 {
1185 if (has_mbyte)
1186 {
1187 c1 = mb_ptr2char(p);
1188 c2 = mb_ptr2char(s);
1189 }
1190 else
1191 {
1192 c1 = *p;
1193 c2 = *s;
1194 }
1195 if ((match->cp_flags & CP_ICASE)
1196 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
1197 break;
1198 if (has_mbyte)
1199 {
1200 MB_PTR_ADV(p);
1201 MB_PTR_ADV(s);
1202 }
1203 else
1204 {
1205 ++p;
1206 ++s;
1207 }
1208 }
1209
1210 if (*p != NUL)
1211 {
1212 // Leader was shortened, need to change the inserted text.
1213 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001214 compl_leader.length = (size_t)(p - compl_leader.string);
1215
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001216 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001217 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001218
1219 // When the match isn't there (to avoid matching itself) remove it
1220 // again after redrawing.
1221 if (!had_match)
1222 ins_compl_delete();
1223 }
1224
1225 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001226}
1227
1228/*
1229 * Add an array of matches to the list of matches.
1230 * Frees matches[].
1231 */
1232 static void
1233ins_compl_add_matches(
1234 int num_matches,
1235 char_u **matches,
1236 int icase)
1237{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001238 int add_r = OK;
1239 int dir = compl_direction;
1240
glepnir19e1dd62025-05-08 22:50:38 +02001241 for (int i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001242 {
1243 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
glepnirf31cfa22025-03-06 21:59:13 +01001244 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0);
glepnir6e199932024-12-14 21:13:27 +01001245 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001246 // if dir was BACKWARD then honor it just once
1247 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001248 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001249 FreeWild(num_matches, matches);
1250}
1251
1252/*
1253 * Make the completion list cyclic.
1254 * Return the number of matches (excluding the original).
1255 */
1256 static int
1257ins_compl_make_cyclic(void)
1258{
1259 compl_T *match;
1260 int count = 0;
1261
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001262 if (compl_first_match == NULL)
1263 return 0;
1264
1265 // Find the end of the list.
1266 match = compl_first_match;
1267 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001268 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001269 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001270 match = match->cp_next;
1271 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001272 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001273 match->cp_next = compl_first_match;
1274 compl_first_match->cp_prev = match;
1275
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001276 return count;
1277}
1278
1279/*
1280 * Return whether there currently is a shown match.
1281 */
1282 int
1283ins_compl_has_shown_match(void)
1284{
1285 return compl_shown_match == NULL
1286 || compl_shown_match != compl_shown_match->cp_next;
1287}
1288
1289/*
1290 * Return whether the shown match is long enough.
1291 */
1292 int
1293ins_compl_long_shown_match(void)
1294{
John Marriott5e6ea922024-11-23 14:01:57 +01001295 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001296 > curwin->w_cursor.col - compl_col;
1297}
1298
1299/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001300 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001301 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001302 unsigned int
1303get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001304{
zeertzjq529b9ad2024-06-05 20:27:06 +02001305 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001306}
1307
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001308/*
1309 * Update the screen and when there is any scrolling remove the popup menu.
1310 */
1311 static void
1312ins_compl_upd_pum(void)
1313{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001314 if (compl_match_array == NULL)
1315 return;
1316
glepnir19e1dd62025-05-08 22:50:38 +02001317 int h = curwin->w_cline_height;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001318 // Update the screen later, before drawing the popup menu over it.
1319 pum_call_update_screen();
1320 if (h != curwin->w_cline_height)
1321 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001322}
1323
1324/*
1325 * Remove any popup menu.
1326 */
1327 static void
1328ins_compl_del_pum(void)
1329{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001330 if (compl_match_array == NULL)
1331 return;
1332
1333 pum_undisplay();
1334 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001335}
1336
1337/*
1338 * Return TRUE if the popup menu should be displayed.
1339 */
1340 int
1341pum_wanted(void)
1342{
1343 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001344 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001345 return FALSE;
1346
1347 // The display looks bad on a B&W display.
1348 if (t_colors < 8
1349#ifdef FEAT_GUI
1350 && !gui.in_use
1351#endif
1352 )
1353 return FALSE;
1354 return TRUE;
1355}
1356
1357/*
1358 * Return TRUE if there are two or more matches to be shown in the popup menu.
1359 * One if 'completopt' contains "menuone".
1360 */
1361 static int
1362pum_enough_matches(void)
1363{
1364 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001365 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001366
1367 // Don't display the popup menu if there are no matches or there is only
1368 // one (ignoring the original text).
1369 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001370 do
1371 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001372 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001373 break;
1374 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001375 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001376
zeertzjq529b9ad2024-06-05 20:27:06 +02001377 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001378 return (i >= 1);
1379 return (i >= 2);
1380}
1381
Bram Moolenaar3075a452021-11-17 15:51:52 +00001382#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001383/*
1384 * Allocate Dict for the completed item.
1385 * { word, abbr, menu, kind, info }
1386 */
1387 static dict_T *
1388ins_compl_dict_alloc(compl_T *match)
1389{
1390 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1391
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001392 if (dict == NULL)
1393 return NULL;
1394
John Marriott5e6ea922024-11-23 14:01:57 +01001395 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001396 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1397 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1398 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1399 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1400 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1401 dict_add_string(dict, "user_data", (char_u *)"");
1402 else
1403 dict_add_tv(dict, "user_data", &match->cp_user_data);
1404
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001405 return dict;
1406}
1407
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001408/*
1409 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1410 * completion menu is changed.
1411 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001412 static void
1413trigger_complete_changed_event(int cur)
1414{
1415 dict_T *v_event;
1416 dict_T *item;
1417 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001418 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001419
1420 if (recursive)
1421 return;
1422
glepnir40891ba2025-02-10 22:18:00 +01001423 item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001424 if (item == NULL)
1425 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001426 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001427 dict_add_dict(v_event, "completed_item", item);
1428 pum_set_event_info(v_event);
1429 dict_set_items_ro(v_event);
1430
1431 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001432 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001433 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001434 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001435 recursive = FALSE;
1436
Bram Moolenaar3075a452021-11-17 15:51:52 +00001437 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001438}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001439#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001440
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001441/*
glepnira218cc62024-06-03 19:32:39 +02001442 * pumitem qsort compare func
1443 */
1444 static int
zeertzjq8e567472024-06-14 20:04:42 +02001445ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001446{
1447 const int sa = (*(pumitem_T *)a).pum_score;
1448 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001449 const int ia = (*(pumitem_T *)a).pum_idx;
1450 const int ib = (*(pumitem_T *)b).pum_idx;
1451 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001452}
1453
1454/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001455 * Build a popup menu to show the completion matches.
1456 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1457 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001458 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001459 static int
1460ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001461{
1462 compl_T *compl;
1463 compl_T *shown_compl = NULL;
1464 int did_find_shown_match = FALSE;
1465 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001466 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001467 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001468 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001469 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001470 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001471 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1472 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnir80b66202024-11-27 21:53:53 +01001473 compl_T *match_head = NULL;
1474 compl_T *match_tail = NULL;
1475 compl_T *match_next = NULL;
glepnire4e4d1c2025-04-02 20:18:25 +02001476 int update_shown_match = fuzzy_filter;
Christian Brabandtb53d4fb2025-04-16 21:12:30 +02001477 int match_count = 0;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001478 int cur_source = -1;
1479 int max_matches_found = FALSE;
1480 int is_forward = compl_shows_dir_forward() && !fuzzy_filter;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001481
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001482 // Need to build the popup menu list.
1483 compl_match_arraysize = 0;
1484 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001485
glepnira49c0772024-11-30 10:56:30 +01001486 // If the current match is the original text don't find the first
1487 // match after it, don't highlight anything.
1488 if (match_at_original_text(compl_shown_match))
1489 shown_match_ok = TRUE;
1490
glepnire4e4d1c2025-04-02 20:18:25 +02001491 if (fuzzy_filter && ctrl_x_mode_normal() && compl_leader.string == NULL
1492 && compl_shown_match->cp_score > 0)
1493 update_shown_match = FALSE;
1494
glepnira49c0772024-11-30 10:56:30 +01001495 if (compl_leader.string != NULL
1496 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1497 && shown_match_ok == FALSE)
1498 compl_shown_match = compl_no_select ? compl_first_match
1499 : compl_first_match->cp_next;
1500
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001501 do
1502 {
glepnird4088ed2024-12-31 10:55:22 +01001503 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001504 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1505 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001506 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001507 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001508
Girish Palya0ac1eb32025-04-16 20:18:33 +02001509 if (is_forward && compl->cp_cpt_source_idx != -1)
1510 {
1511 if (cur_source != compl->cp_cpt_source_idx)
1512 {
1513 cur_source = compl->cp_cpt_source_idx;
1514 match_count = 1;
1515 max_matches_found = FALSE;
1516 }
Girish Palyadc314052025-05-08 23:28:52 +02001517 else if (cpt_sources_array != NULL && !max_matches_found)
Girish Palya0ac1eb32025-04-16 20:18:33 +02001518 {
1519 int max_matches = cpt_sources_array[cur_source].max_matches;
1520 if (max_matches > 0 && match_count > max_matches)
1521 max_matches_found = TRUE;
1522 }
1523 }
1524
Girish Palyadc314052025-05-08 23:28:52 +02001525 // Apply 'smartcase' behavior during normal mode
1526 if (ctrl_x_mode_normal() && !p_inf && compl_leader.string
1527 && !ignorecase(compl_leader.string) && !fuzzy_filter)
1528 compl->cp_flags &= ~CP_ICASE;
1529
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001530 if (!match_at_original_text(compl)
Girish Palya0ac1eb32025-04-16 20:18:33 +02001531 && !max_matches_found
John Marriott5e6ea922024-11-23 14:01:57 +01001532 && (compl_leader.string == NULL
Girish Palyadc314052025-05-08 23:28:52 +02001533 || ins_compl_equal(compl, compl_leader.string,
1534 (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001535 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001536 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001537 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001538 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001539 if (match_head == NULL)
1540 match_head = compl;
1541 else
glepnira49c0772024-11-30 10:56:30 +01001542 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001543 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001544
glepnirf400a0c2025-01-23 19:55:14 +01001545 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001546 {
1547 if (compl == compl_shown_match || did_find_shown_match)
1548 {
1549 // This item is the shown match or this is the
1550 // first displayed item after the shown match.
1551 compl_shown_match = compl;
1552 did_find_shown_match = TRUE;
1553 shown_match_ok = TRUE;
1554 }
1555 else
1556 // Remember this displayed match for when the
1557 // shown match is just below it.
1558 shown_compl = compl;
1559 cur = i;
1560 }
glepnirf400a0c2025-01-23 19:55:14 +01001561 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001562 {
1563 if (i == 0)
1564 shown_compl = compl;
1565 // Update the maximum fuzzy score and the shown match
1566 // if the current item's score is higher
glepnire4e4d1c2025-04-02 20:18:25 +02001567 if (fuzzy_sort && compl->cp_score > max_fuzzy_score
1568 && update_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001569 {
1570 did_find_shown_match = TRUE;
1571 max_fuzzy_score = compl->cp_score;
1572 if (!compl_no_select)
1573 compl_shown_match = compl;
1574 }
1575
glepnir3af0a8d2025-02-20 22:06:16 +01001576 if (!shown_match_ok && compl == compl_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001577 {
1578 cur = i;
1579 shown_match_ok = TRUE;
1580 }
1581 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001582 if (is_forward && compl->cp_cpt_source_idx != -1)
1583 match_count++;
glepnira49c0772024-11-30 10:56:30 +01001584 i++;
glepnir80b66202024-11-27 21:53:53 +01001585 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001586
glepnirf400a0c2025-01-23 19:55:14 +01001587 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001588 {
1589 did_find_shown_match = TRUE;
1590
1591 // When the original text is the shown match don't set
1592 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001593 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001594 shown_match_ok = TRUE;
1595
1596 if (!shown_match_ok && shown_compl != NULL)
1597 {
1598 // The shown match isn't displayed, set it to the
1599 // previously displayed match.
1600 compl_shown_match = shown_compl;
1601 shown_match_ok = TRUE;
1602 }
1603 }
glepnira49c0772024-11-30 10:56:30 +01001604 compl = compl->cp_next;
1605 } while (compl != NULL && !is_first_match(compl));
1606
1607 if (compl_match_arraysize == 0)
1608 return -1;
1609
glepnirc0b7ca42025-02-13 20:27:44 +01001610 if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok)
1611 {
1612 compl_shown_match = shown_compl;
1613 shown_match_ok = TRUE;
1614 cur = 0;
1615 }
1616
glepnira49c0772024-11-30 10:56:30 +01001617 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1618 if (compl_match_array == NULL)
1619 return -1;
1620
1621 compl = match_head;
1622 i = 0;
1623 while (compl != NULL)
1624 {
glepnir6e199932024-12-14 21:13:27 +01001625 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1626 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001627 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1628 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1629 compl_match_array[i].pum_score = compl->cp_score;
1630 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1631 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001632 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1633 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001634 match_next = compl->cp_match_next;
1635 compl->cp_match_next = NULL;
1636 compl = match_next;
1637 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001638
zeertzjqd65aa1b2025-01-25 15:29:03 +01001639 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001640 {
1641 for (i = 0; i < compl_match_arraysize; i++)
1642 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001643 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001644 qsort(compl_match_array, (size_t)compl_match_arraysize,
1645 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001646 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001647 }
glepnira218cc62024-06-03 19:32:39 +02001648
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001649 if (!shown_match_ok) // no displayed match at all
1650 cur = -1;
1651
1652 return cur;
1653}
1654
1655/*
1656 * Show the popup menu for the list of matches.
1657 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1658 */
1659 void
1660ins_compl_show_pum(void)
1661{
1662 int i;
1663 int cur = -1;
1664 colnr_T col;
1665
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001666 if (!pum_wanted() || !pum_enough_matches())
1667 return;
1668
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001669 // Update the screen later, before drawing the popup menu over it.
1670 pum_call_update_screen();
1671
1672 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001673 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001674 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001675 else
1676 {
1677 // popup menu already exists, only need to find the current item.
1678 for (i = 0; i < compl_match_arraysize; ++i)
glepnir19e1dd62025-05-08 22:50:38 +02001679 {
John Marriott5e6ea922024-11-23 14:01:57 +01001680 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 || compl_match_array[i].pum_text
1682 == compl_shown_match->cp_text[CPT_ABBR])
1683 {
1684 cur = i;
1685 break;
1686 }
glepnir19e1dd62025-05-08 22:50:38 +02001687 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001688 }
1689
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001690 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001691 {
1692#ifdef FEAT_EVAL
1693 if (compl_started && has_completechanged())
1694 trigger_complete_changed_event(cur);
1695#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001696 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001697 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001698
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001699 // In Replace mode when a $ is displayed at the end of the line only
1700 // part of the screen would be updated. We do need to redraw here.
1701 dollar_vcol = -1;
1702
1703 // Compute the screen column of the start of the completed text.
1704 // Use the cursor to get all wrapping and other settings right.
1705 col = curwin->w_cursor.col;
1706 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001707 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001708 pum_display(compl_match_array, compl_match_arraysize, cur);
1709 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001710
glepnircbb46b42024-02-03 18:11:13 +01001711 // After adding leader, set the current match to shown match.
1712 if (compl_started && compl_curr_match != compl_shown_match)
1713 compl_curr_match = compl_shown_match;
1714
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001715#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001716 if (has_completechanged())
1717 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001718#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001719}
1720
1721#define DICT_FIRST (1) // use just first element in "dict"
1722#define DICT_EXACT (2) // "dict" is the exact name of a file
1723
1724/*
glepnir40c1c332024-06-11 19:37:04 +02001725 * Get current completion leader
1726 */
1727 char_u *
1728ins_compl_leader(void)
1729{
John Marriott5e6ea922024-11-23 14:01:57 +01001730 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1731}
1732
1733/*
1734 * Get current completion leader length
1735 */
1736 size_t
1737ins_compl_leader_len(void)
1738{
1739 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001740}
1741
1742/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001743 * Add any identifiers that match the given pattern "pat" in the list of
1744 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001745 */
1746 static void
1747ins_compl_dictionaries(
1748 char_u *dict_start,
1749 char_u *pat,
1750 int flags, // DICT_FIRST and/or DICT_EXACT
1751 int thesaurus) // Thesaurus completion
1752{
1753 char_u *dict = dict_start;
1754 char_u *ptr;
1755 char_u *buf;
1756 regmatch_T regmatch;
1757 char_u **files;
1758 int count;
1759 int save_p_scs;
1760 int dir = compl_direction;
1761
1762 if (*dict == NUL)
1763 {
1764#ifdef FEAT_SPELL
1765 // When 'dictionary' is empty and spell checking is enabled use
1766 // "spell".
1767 if (!thesaurus && curwin->w_p_spell)
1768 dict = (char_u *)"spell";
1769 else
1770#endif
1771 return;
1772 }
1773
1774 buf = alloc(LSIZE);
1775 if (buf == NULL)
1776 return;
1777 regmatch.regprog = NULL; // so that we can goto theend
1778
1779 // If 'infercase' is set, don't use 'smartcase' here
1780 save_p_scs = p_scs;
1781 if (curbuf->b_p_inf)
1782 p_scs = FALSE;
1783
1784 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1785 // to only match at the start of a line. Otherwise just match the
1786 // pattern. Also need to double backslashes.
1787 if (ctrl_x_mode_line_or_eval())
1788 {
1789 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001790
1791 if (pat_esc == NULL)
1792 goto theend;
glepnir19e1dd62025-05-08 22:50:38 +02001793 size_t len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001794 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001795 if (ptr == NULL)
1796 {
1797 vim_free(pat_esc);
1798 goto theend;
1799 }
1800 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1801 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1802 vim_free(pat_esc);
1803 vim_free(ptr);
1804 }
1805 else
1806 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001807 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001808 if (regmatch.regprog == NULL)
1809 goto theend;
1810 }
1811
1812 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1813 regmatch.rm_ic = ignorecase(pat);
1814 while (*dict != NUL && !got_int && !compl_interrupted)
1815 {
1816 // copy one dictionary file name into buf
1817 if (flags == DICT_EXACT)
1818 {
1819 count = 1;
1820 files = &dict;
1821 }
1822 else
1823 {
1824 // Expand wildcards in the dictionary name, but do not allow
1825 // backticks (for security, the 'dict' option may have been set in
1826 // a modeline).
1827 copy_option_part(&dict, buf, LSIZE, ",");
1828# ifdef FEAT_SPELL
1829 if (!thesaurus && STRCMP(buf, "spell") == 0)
1830 count = -1;
1831 else
1832# endif
1833 if (vim_strchr(buf, '`') != NULL
1834 || expand_wildcards(1, &buf, &count, &files,
1835 EW_FILE|EW_SILENT) != OK)
1836 count = 0;
1837 }
1838
1839# ifdef FEAT_SPELL
1840 if (count == -1)
1841 {
1842 // Complete from active spelling. Skip "\<" in the pattern, we
1843 // don't use it as a RE.
1844 if (pat[0] == '\\' && pat[1] == '<')
1845 ptr = pat + 2;
1846 else
1847 ptr = pat;
1848 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1849 }
1850 else
1851# endif
1852 if (count > 0) // avoid warning for using "files" uninit
1853 {
1854 ins_compl_files(count, files, thesaurus, flags,
glepnirf31cfa22025-03-06 21:59:13 +01001855 (cfc_has_mode() ? NULL : &regmatch), buf, &dir);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856 if (flags != DICT_EXACT)
1857 FreeWild(count, files);
1858 }
1859 if (flags != 0)
1860 break;
1861 }
1862
1863theend:
1864 p_scs = save_p_scs;
1865 vim_regfree(regmatch.regprog);
1866 vim_free(buf);
1867}
1868
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001869/*
1870 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1871 * skipping the word at 'skip_word'. Returns OK on success.
1872 */
1873 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001874thesaurus_add_words_in_line(
glepnir19e1dd62025-05-08 22:50:38 +02001875 char_u *fname,
1876 char_u **buf_arg,
1877 int dir,
1878 char_u *skip_word)
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001879{
1880 int status = OK;
1881 char_u *ptr;
1882 char_u *wstart;
1883
1884 // Add the other matches on the line
1885 ptr = *buf_arg;
1886 while (!got_int)
1887 {
1888 // Find start of the next word. Skip white
1889 // space and punctuation.
1890 ptr = find_word_start(ptr);
1891 if (*ptr == NUL || *ptr == NL)
1892 break;
1893 wstart = ptr;
1894
1895 // Find end of the word.
1896 if (has_mbyte)
1897 // Japanese words may have characters in
1898 // different classes, only separate words
1899 // with single-byte non-word characters.
1900 while (*ptr != NUL)
1901 {
1902 int l = (*mb_ptr2len)(ptr);
1903
1904 if (l < 2 && !vim_iswordc(*ptr))
1905 break;
1906 ptr += l;
1907 }
1908 else
1909 ptr = find_word_end(ptr);
1910
1911 // Add the word. Skip the regexp match.
1912 if (wstart != skip_word)
1913 {
1914 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01001915 fname, dir, FALSE, 0);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001916 if (status == FAIL)
1917 break;
1918 }
1919 }
1920
1921 *buf_arg = ptr;
1922 return status;
1923}
1924
1925/*
1926 * Process "count" dictionary/thesaurus "files" and add the text matching
1927 * "regmatch".
1928 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001929 static void
1930ins_compl_files(
1931 int count,
1932 char_u **files,
1933 int thesaurus,
1934 int flags,
1935 regmatch_T *regmatch,
1936 char_u *buf,
1937 int *dir)
1938{
1939 char_u *ptr;
1940 int i;
1941 FILE *fp;
1942 int add_r;
glepnirf31cfa22025-03-06 21:59:13 +01001943 char_u *leader = NULL;
1944 int leader_len = 0;
glepnir58760162025-03-13 21:39:51 +01001945 int in_fuzzy_collect = cfc_has_mode();
glepnirf31cfa22025-03-06 21:59:13 +01001946 int score = 0;
1947 int len = 0;
1948 char_u *line_end = NULL;
1949
1950 if (in_fuzzy_collect)
1951 {
1952 leader = ins_compl_leader();
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +02001953 leader_len = (int)ins_compl_leader_len();
glepnirf31cfa22025-03-06 21:59:13 +01001954 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001955
1956 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1957 {
1958 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001959 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001960 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001961 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001962 vim_snprintf((char *)IObuff, IOSIZE,
1963 _("Scanning dictionary: %s"), (char *)files[i]);
1964 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1965 }
1966
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001967 if (fp == NULL)
1968 continue;
1969
1970 // Read dictionary file line by line.
1971 // Check each line for a match.
1972 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001973 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001974 ptr = buf;
glepnirf31cfa22025-03-06 21:59:13 +01001975 if (regmatch != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001976 {
glepnirf31cfa22025-03-06 21:59:13 +01001977 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001978 {
glepnirf31cfa22025-03-06 21:59:13 +01001979 ptr = regmatch->startp[0];
1980 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1981 : find_word_end(ptr);
1982 add_r = ins_compl_add_infercase(regmatch->startp[0],
1983 (int)(ptr - regmatch->startp[0]),
1984 p_ic, files[i], *dir, FALSE, 0);
1985 if (thesaurus)
1986 {
1987 // For a thesaurus, add all the words in the line
1988 ptr = buf;
1989 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
1990 regmatch->startp[0]);
1991 }
1992 if (add_r == OK)
1993 // if dir was BACKWARD then honor it just once
1994 *dir = FORWARD;
1995 else if (add_r == FAIL)
1996 break;
1997 // avoid expensive call to vim_regexec() when at end
1998 // of line
1999 if (*ptr == '\n' || got_int)
2000 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002001 }
glepnirf31cfa22025-03-06 21:59:13 +01002002 }
2003 else if (in_fuzzy_collect && leader_len > 0)
2004 {
2005 line_end = find_line_end(ptr);
2006 while (ptr < line_end)
2007 {
2008 if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score))
2009 {
2010 char_u *end_ptr = ctrl_x_mode_line_or_eval()
2011 ? find_line_end(ptr) : find_word_end(ptr);
2012 add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr),
2013 p_ic, files[i], *dir, FALSE, score);
2014 if (add_r == FAIL)
2015 break;
2016 ptr = end_ptr; // start from next word
2017 if (compl_get_longest && ctrl_x_mode_normal()
2018 && compl_first_match->cp_next
2019 && score == compl_first_match->cp_next->cp_score)
2020 compl_num_bests++;
2021 }
glepnirf31cfa22025-03-06 21:59:13 +01002022 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002024 line_breakcheck();
2025 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002026 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002027 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002028 }
2029}
2030
2031/*
2032 * Find the start of the next word.
2033 * Returns a pointer to the first char of the word. Also stops at a NUL.
2034 */
2035 char_u *
2036find_word_start(char_u *ptr)
2037{
2038 if (has_mbyte)
glepnir19e1dd62025-05-08 22:50:38 +02002039 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002040 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
2041 ptr += (*mb_ptr2len)(ptr);
glepnir19e1dd62025-05-08 22:50:38 +02002042 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002043 else
glepnir19e1dd62025-05-08 22:50:38 +02002044 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002045 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2046 ++ptr;
glepnir19e1dd62025-05-08 22:50:38 +02002047 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002048 return ptr;
2049}
2050
2051/*
2052 * Find the end of the word. Assumes it starts inside a word.
2053 * Returns a pointer to just after the word.
2054 */
2055 char_u *
2056find_word_end(char_u *ptr)
2057{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002058 if (has_mbyte)
2059 {
glepnir19e1dd62025-05-08 22:50:38 +02002060 int start_class = mb_get_class(ptr);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002061 if (start_class > 1)
2062 while (*ptr != NUL)
2063 {
2064 ptr += (*mb_ptr2len)(ptr);
2065 if (mb_get_class(ptr) != start_class)
2066 break;
2067 }
2068 }
2069 else
2070 while (vim_iswordc(*ptr))
2071 ++ptr;
2072 return ptr;
2073}
2074
2075/*
2076 * Find the end of the line, omitting CR and NL at the end.
2077 * Returns a pointer to just after the line.
2078 */
glepnirdd42b052025-03-08 16:52:55 +01002079 char_u *
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002080find_line_end(char_u *ptr)
2081{
glepnir19e1dd62025-05-08 22:50:38 +02002082 char_u *s = ptr + STRLEN(ptr);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002083 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2084 --s;
2085 return s;
2086}
2087
2088/*
Girish Palyacbe53192025-04-14 22:13:15 +02002089 * Free a completion item in the list
2090 */
2091 static void
2092ins_compl_item_free(compl_T *match)
2093{
Girish Palyacbe53192025-04-14 22:13:15 +02002094 VIM_CLEAR_STRING(match->cp_str);
2095 // several entries may use the same fname, free it just once.
2096 if (match->cp_flags & CP_FREE_FNAME)
2097 vim_free(match->cp_fname);
glepnir19e1dd62025-05-08 22:50:38 +02002098 for (int i = 0; i < CPT_COUNT; ++i)
Girish Palyacbe53192025-04-14 22:13:15 +02002099 vim_free(match->cp_text[i]);
2100#ifdef FEAT_EVAL
2101 clear_tv(&match->cp_user_data);
2102#endif
2103 vim_free(match);
2104}
2105
2106/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002107 * Free the list of completions
2108 */
2109 static void
2110ins_compl_free(void)
2111{
2112 compl_T *match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002113
John Marriott5e6ea922024-11-23 14:01:57 +01002114 VIM_CLEAR_STRING(compl_pattern);
2115 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002116
2117 if (compl_first_match == NULL)
2118 return;
2119
2120 ins_compl_del_pum();
2121 pum_clear();
2122
2123 compl_curr_match = compl_first_match;
2124 do
2125 {
2126 match = compl_curr_match;
2127 compl_curr_match = compl_curr_match->cp_next;
Girish Palyacbe53192025-04-14 22:13:15 +02002128 ins_compl_item_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002129 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002130 compl_first_match = compl_curr_match = NULL;
2131 compl_shown_match = NULL;
2132 compl_old_match = NULL;
2133}
2134
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002135/*
2136 * Reset/clear the completion state.
2137 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002138 void
2139ins_compl_clear(void)
2140{
2141 compl_cont_status = 0;
2142 compl_started = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01002143 compl_cfc_longest_ins = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002144 compl_matches = 0;
glepnir07f0dbe2025-02-18 20:27:30 +01002145 compl_selected_item = -1;
glepnir6a38aff2024-12-16 21:56:16 +01002146 compl_ins_end_col = 0;
glepnircf7f0122025-04-15 19:02:00 +02002147 compl_curr_win = NULL;
2148 compl_curr_buf = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002149 VIM_CLEAR_STRING(compl_pattern);
2150 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002151 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002152 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002153 compl_enter_selects = FALSE;
Girish Palya0ac1eb32025-04-16 20:18:33 +02002154 cpt_sources_clear();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002155#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002156 // clear v:completed_item
2157 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002158#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002159}
2160
2161/*
2162 * Return TRUE when Insert completion is active.
2163 */
2164 int
2165ins_compl_active(void)
2166{
2167 return compl_started;
2168}
2169
2170/*
glepnir8d0bb6d2024-12-24 09:44:35 +01002171 * Return True when wp is the actual completion window
2172 */
2173 int
glepnircf7f0122025-04-15 19:02:00 +02002174ins_compl_win_active(win_T *wp)
glepnir8d0bb6d2024-12-24 09:44:35 +01002175{
glepnircf7f0122025-04-15 19:02:00 +02002176 return ins_compl_active() && wp == compl_curr_win
2177 && wp->w_buffer == compl_curr_buf;
glepnir8d0bb6d2024-12-24 09:44:35 +01002178}
2179
2180/*
Girish Palyacbe53192025-04-14 22:13:15 +02002181 * Selected one of the matches. When FALSE, the match was either edited or
2182 * using the longest common string.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002183 */
2184 int
2185ins_compl_used_match(void)
2186{
2187 return compl_used_match;
2188}
2189
2190/*
2191 * Initialize get longest common string.
2192 */
2193 void
2194ins_compl_init_get_longest(void)
2195{
2196 compl_get_longest = FALSE;
2197}
2198
2199/*
2200 * Returns TRUE when insert completion is interrupted.
2201 */
2202 int
2203ins_compl_interrupted(void)
2204{
2205 return compl_interrupted;
2206}
2207
2208/*
2209 * Returns TRUE if the <Enter> key selects a match in the completion popup
2210 * menu.
2211 */
2212 int
2213ins_compl_enter_selects(void)
2214{
2215 return compl_enter_selects;
2216}
2217
2218/*
2219 * Return the column where the text starts that is being completed
2220 */
2221 colnr_T
2222ins_compl_col(void)
2223{
2224 return compl_col;
2225}
2226
2227/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002228 * Return the length in bytes of the text being completed
2229 */
2230 int
2231ins_compl_len(void)
2232{
2233 return compl_length;
2234}
2235
2236/*
glepnir94a045e2025-03-01 16:12:23 +01002237 * Return TRUE when the 'completeopt' "preinsert" flag is in effect,
2238 * otherwise return FALSE.
glepniredd4ac32025-01-29 18:53:51 +01002239 */
2240 static int
2241ins_compl_has_preinsert(void)
2242{
glepnira2c55592025-02-28 17:43:42 +01002243 int cur_cot_flags = get_cot_flags();
glepnir94a045e2025-03-01 16:12:23 +01002244 return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE))
2245 == (COT_PREINSERT | COT_MENUONE);
glepniredd4ac32025-01-29 18:53:51 +01002246}
2247
2248/*
2249 * Returns TRUE if the pre-insert effect is valid and the cursor is within
2250 * the `compl_ins_end_col` range.
2251 */
2252 int
2253ins_compl_preinsert_effect(void)
2254{
2255 if (!ins_compl_has_preinsert())
2256 return FALSE;
2257
2258 return curwin->w_cursor.col < compl_ins_end_col;
2259}
2260
2261/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002262 * Delete one character before the cursor and show the subset of the matches
2263 * that match the word that is now before the cursor.
2264 * Returns the character to be used, NUL if the work is done and another char
2265 * to be got from the user.
2266 */
2267 int
2268ins_compl_bs(void)
2269{
2270 char_u *line;
2271 char_u *p;
2272
glepniredd4ac32025-01-29 18:53:51 +01002273 if (ins_compl_preinsert_effect())
2274 ins_compl_delete();
2275
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002276 line = ml_get_curline();
2277 p = line + curwin->w_cursor.col;
2278 MB_PTR_BACK(line, p);
2279
2280 // Stop completion when the whole word was deleted. For Omni completion
2281 // allow the word to be deleted, we won't match everything.
2282 // Respect the 'backspace' option.
2283 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002284 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2285 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002286 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2287 - compl_length < 0))
2288 return K_BS;
2289
2290 // Deleted more than what was used to find matches or didn't finish
2291 // finding all matches: need to look for matches all over again.
2292 if (curwin->w_cursor.col <= compl_col + compl_length
2293 || ins_compl_need_restart())
2294 ins_compl_restart();
2295
John Marriott5e6ea922024-11-23 14:01:57 +01002296 VIM_CLEAR_STRING(compl_leader);
2297 compl_leader.length = (size_t)((p - line) - compl_col);
2298 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2299 if (compl_leader.string == NULL)
2300 {
2301 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002302 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002303 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002304
2305 ins_compl_new_leader();
2306 if (compl_shown_match != NULL)
2307 // Make sure current match is not a hidden item.
2308 compl_curr_match = compl_shown_match;
2309 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002310}
2311
2312/*
2313 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2314 * be called.
2315 */
2316 static int
2317ins_compl_need_restart(void)
2318{
2319 // Return TRUE if we didn't complete finding matches or when the
2320 // 'completefunc' returned "always" in the "refresh" dictionary item.
2321 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002322 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002323 && compl_opt_refresh_always);
2324}
2325
2326/*
2327 * Called after changing "compl_leader".
2328 * Show the popup menu with a different set of matches.
2329 * May also search for matches again if the previous search was interrupted.
2330 */
2331 static void
2332ins_compl_new_leader(void)
2333{
2334 ins_compl_del_pum();
2335 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002336 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002337 compl_used_match = FALSE;
2338
2339 if (compl_started)
Girish Palyacbe53192025-04-14 22:13:15 +02002340 {
John Marriott5e6ea922024-11-23 14:01:57 +01002341 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Girish Palyacbe53192025-04-14 22:13:15 +02002342 if (is_cpt_func_refresh_always())
2343 cpt_compl_refresh();
2344 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002345 else
2346 {
2347#ifdef FEAT_SPELL
2348 spell_bad_len = 0; // need to redetect bad word
2349#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002350 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002351 // the popup menu display the changed text before the cursor. Set
2352 // "compl_restarting" to avoid that the first match is inserted.
2353 pum_call_update_screen();
2354#ifdef FEAT_GUI
2355 if (gui.in_use)
2356 {
2357 // Show the cursor after the match, not after the redrawn text.
2358 setcursor();
2359 out_flush_cursor(FALSE, FALSE);
2360 }
2361#endif
2362 compl_restarting = TRUE;
2363 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2364 compl_cont_status = 0;
2365 compl_restarting = FALSE;
2366 }
2367
glepnir44180412025-02-20 22:09:48 +01002368 compl_enter_selects = !compl_used_match && compl_selected_item != -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002369
2370 // Show the popup menu with a different set of matches.
2371 ins_compl_show_pum();
2372
2373 // Don't let Enter select the original text when there is no popup menu.
2374 if (compl_match_array == NULL)
2375 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002376 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2377 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002378}
2379
2380/*
2381 * Return the length of the completion, from the completion start column to
2382 * the cursor column. Making sure it never goes below zero.
2383 */
2384 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002385get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002386{
2387 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002388 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002389}
2390
2391/*
2392 * Append one character to the match leader. May reduce the number of
2393 * matches.
2394 */
2395 void
2396ins_compl_addleader(int c)
2397{
glepnir19e1dd62025-05-08 22:50:38 +02002398 int cc;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002399
glepniredd4ac32025-01-29 18:53:51 +01002400 if (ins_compl_preinsert_effect())
2401 ins_compl_delete();
2402
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002403 if (stop_arrow() == FAIL)
2404 return;
2405 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2406 {
2407 char_u buf[MB_MAXBYTES + 1];
2408
2409 (*mb_char2bytes)(c, buf);
2410 buf[cc] = NUL;
2411 ins_char_bytes(buf, cc);
2412 if (compl_opt_refresh_always)
2413 AppendToRedobuff(buf);
2414 }
2415 else
2416 {
2417 ins_char(c);
2418 if (compl_opt_refresh_always)
2419 AppendCharToRedobuff(c);
2420 }
2421
2422 // If we didn't complete finding matches we must search again.
2423 if (ins_compl_need_restart())
2424 ins_compl_restart();
2425
2426 // When 'always' is set, don't reset compl_leader. While completing,
2427 // cursor doesn't point original position, changing compl_leader would
2428 // break redo.
2429 if (!compl_opt_refresh_always)
2430 {
John Marriott5e6ea922024-11-23 14:01:57 +01002431 VIM_CLEAR_STRING(compl_leader);
2432 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2433 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2434 compl_leader.length);
2435 if (compl_leader.string == NULL)
2436 {
2437 compl_leader.length = 0;
2438 return;
2439 }
2440
2441 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002442 }
2443}
2444
2445/*
2446 * Setup for finding completions again without leaving CTRL-X mode. Used when
2447 * BS or a key was typed while still searching for matches.
2448 */
2449 static void
2450ins_compl_restart(void)
2451{
2452 ins_compl_free();
2453 compl_started = FALSE;
2454 compl_matches = 0;
2455 compl_cont_status = 0;
2456 compl_cont_mode = 0;
Girish Palya0ac1eb32025-04-16 20:18:33 +02002457 cpt_sources_clear();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002458}
2459
2460/*
2461 * Set the first match, the original text.
2462 */
2463 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002464ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002465{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002466 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002467 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2468 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002469 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002470 {
John Marriott5e6ea922024-11-23 14:01:57 +01002471 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002472 if (p != NULL)
2473 {
John Marriott5e6ea922024-11-23 14:01:57 +01002474 VIM_CLEAR_STRING(compl_first_match->cp_str);
2475 compl_first_match->cp_str.string = p;
2476 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002477 }
2478 }
2479 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002480 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002481 {
John Marriott5e6ea922024-11-23 14:01:57 +01002482 char_u *p = vim_strnsave(str, len);
2483 if (p != NULL)
2484 {
2485 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2486 compl_first_match->cp_prev->cp_str.string = p;
2487 compl_first_match->cp_prev->cp_str.length = len;
2488 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002489 }
2490}
2491
2492/*
2493 * Append one character to the match leader. May reduce the number of
2494 * matches.
2495 */
2496 void
2497ins_compl_addfrommatch(void)
2498{
2499 char_u *p;
2500 int len = (int)curwin->w_cursor.col - (int)compl_col;
2501 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002502
John Marriott5e6ea922024-11-23 14:01:57 +01002503 p = compl_shown_match->cp_str.string;
2504 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002505 {
John Marriott5e6ea922024-11-23 14:01:57 +01002506 size_t plen;
2507 compl_T *cp;
2508
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002509 // When still at the original match use the first entry that matches
2510 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002511 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002512 return;
2513
2514 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002515 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002516 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002517 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002518 {
John Marriott5e6ea922024-11-23 14:01:57 +01002519 if (compl_leader.string == NULL
2520 || ins_compl_equal(cp, compl_leader.string,
2521 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002522 {
John Marriott5e6ea922024-11-23 14:01:57 +01002523 p = cp->cp_str.string;
2524 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002525 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002526 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002527 }
John Marriott5e6ea922024-11-23 14:01:57 +01002528 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002529 return;
2530 }
2531 p += len;
2532 c = PTR2CHAR(p);
2533 ins_compl_addleader(c);
2534}
2535
2536/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002537 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002538 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2539 * compl_cont_mode and compl_cont_status.
2540 * Returns TRUE when the character is not to be inserted.
2541 */
2542 static int
2543set_ctrl_x_mode(int c)
2544{
glepnir05460682025-05-26 18:23:27 +02002545 int retval = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002546
2547 switch (c)
2548 {
2549 case Ctrl_E:
2550 case Ctrl_Y:
2551 // scroll the window one line up or down
2552 ctrl_x_mode = CTRL_X_SCROLL;
2553 if (!(State & REPLACE_FLAG))
2554 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2555 else
2556 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2557 edit_submode_pre = NULL;
2558 showmode();
2559 break;
2560 case Ctrl_L:
2561 // complete whole line
2562 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2563 break;
2564 case Ctrl_F:
2565 // complete filenames
2566 ctrl_x_mode = CTRL_X_FILES;
2567 break;
2568 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002569 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002570 ctrl_x_mode = CTRL_X_DICTIONARY;
2571 break;
2572 case Ctrl_R:
glepnir05460682025-05-26 18:23:27 +02002573 // When CTRL-R is followed by '=', don't trigger register completion
2574 // This allows expressions like <C-R>=func()<CR> to work normally
2575 if (vpeekc() == '=')
2576 break;
2577 ctrl_x_mode = CTRL_X_REGISTER;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002578 break;
2579 case Ctrl_T:
2580 // complete words from a thesaurus
2581 ctrl_x_mode = CTRL_X_THESAURUS;
2582 break;
2583#ifdef FEAT_COMPL_FUNC
2584 case Ctrl_U:
2585 // user defined completion
2586 ctrl_x_mode = CTRL_X_FUNCTION;
2587 break;
2588 case Ctrl_O:
2589 // omni completion
2590 ctrl_x_mode = CTRL_X_OMNI;
2591 break;
2592#endif
2593 case 's':
2594 case Ctrl_S:
2595 // complete spelling suggestions
2596 ctrl_x_mode = CTRL_X_SPELL;
2597#ifdef FEAT_SPELL
2598 ++emsg_off; // Avoid getting the E756 error twice.
2599 spell_back_to_badword();
2600 --emsg_off;
2601#endif
2602 break;
2603 case Ctrl_RSB:
2604 // complete tag names
2605 ctrl_x_mode = CTRL_X_TAGS;
2606 break;
2607#ifdef FEAT_FIND_ID
2608 case Ctrl_I:
2609 case K_S_TAB:
2610 // complete keywords from included files
2611 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2612 break;
2613 case Ctrl_D:
2614 // complete definitions from included files
2615 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2616 break;
2617#endif
2618 case Ctrl_V:
2619 case Ctrl_Q:
2620 // complete vim commands
2621 ctrl_x_mode = CTRL_X_CMDLINE;
2622 break;
2623 case Ctrl_Z:
2624 // stop completion
2625 ctrl_x_mode = CTRL_X_NORMAL;
2626 edit_submode = NULL;
2627 showmode();
2628 retval = TRUE;
2629 break;
2630 case Ctrl_P:
2631 case Ctrl_N:
2632 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2633 // just started ^X mode, or there were enough ^X's to cancel
2634 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2635 // do normal expansion when interrupting a different mode (say
2636 // ^X^F^X^P or ^P^X^X^P, see below)
2637 // nothing changes if interrupting mode 0, (eg, the flag
2638 // doesn't change when going to ADDING mode -- Acevedo
2639 if (!(compl_cont_status & CONT_INTRPT))
2640 compl_cont_status |= CONT_LOCAL;
2641 else if (compl_cont_mode != 0)
2642 compl_cont_status &= ~CONT_LOCAL;
2643 // FALLTHROUGH
2644 default:
2645 // If we have typed at least 2 ^X's... for modes != 0, we set
2646 // compl_cont_status = 0 (eg, as if we had just started ^X
2647 // mode).
2648 // For mode 0, we set "compl_cont_mode" to an impossible
2649 // value, in both cases ^X^X can be used to restart the same
2650 // mode (avoiding ADDING mode).
2651 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2652 // 'complete' and local ^P expansions respectively.
2653 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2654 // mode -- Acevedo
2655 if (c == Ctrl_X)
2656 {
2657 if (compl_cont_mode != 0)
2658 compl_cont_status = 0;
2659 else
2660 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2661 }
2662 ctrl_x_mode = CTRL_X_NORMAL;
2663 edit_submode = NULL;
2664 showmode();
2665 break;
2666 }
2667
2668 return retval;
2669}
2670
2671/*
glepnir1c5a1202024-12-04 20:27:34 +01002672 * Trigger CompleteDone event and adds relevant information to v:event
2673 */
2674 static void
2675trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2676{
2677#if defined(FEAT_EVAL)
2678 save_v_event_T save_v_event;
2679 dict_T *v_event = get_v_event(&save_v_event);
2680 char_u *mode_str = NULL;
2681
2682 mode = mode & ~CTRL_X_WANT_IDENT;
2683 if (ctrl_x_mode_names[mode])
2684 mode_str = (char_u *)ctrl_x_mode_names[mode];
2685
2686 (void)dict_add_string(v_event, "complete_word",
2687 word == NULL ? (char_u *)"" : word);
2688 (void)dict_add_string(v_event, "complete_type",
2689 mode_str != NULL ? mode_str : (char_u *)"");
2690
2691 dict_set_items_ro(v_event);
2692#endif
2693 ins_apply_autocmds(EVENT_COMPLETEDONE);
2694
2695#if defined(FEAT_EVAL)
2696 restore_v_event(v_event, &save_v_event);
2697#endif
2698}
2699
2700/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002701 * Stop insert completion mode
2702 */
2703 static int
2704ins_compl_stop(int c, int prev_mode, int retval)
2705{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002706 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002707 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002708
glepnir84a75032025-03-15 09:59:22 +01002709 // Remove pre-inserted text when present.
zeertzjq13436812025-04-23 20:46:35 +02002710 if (ins_compl_preinsert_effect() && ins_compl_win_active(curwin))
glepnir84a75032025-03-15 09:59:22 +01002711 ins_compl_delete();
2712
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002713 // Get here when we have finished typing a sequence of ^N and
2714 // ^P or other completion characters in CTRL-X mode. Free up
2715 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002716 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002717 {
glepnir40891ba2025-02-10 22:18:00 +01002718 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002719
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002720 // If any of the original typed text has been changed, eg when
2721 // ignorecase is set, we must add back-spaces to the redo
2722 // buffer. We add as few as necessary to delete just the part
2723 // of the original text that has changed.
2724 // When using the longest match, edited the match or used
2725 // CTRL-E then don't use the current match.
2726 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002727 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002728 ins_compl_fixRedoBufForLeader(ptr);
2729 }
2730
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002731 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002732
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002733 // When completing whole lines: fix indent for 'cindent'.
2734 // Otherwise, break line if it's too long.
2735 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2736 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002737 // re-indent the current line
2738 if (want_cindent)
2739 {
2740 do_c_expr_indent();
2741 want_cindent = FALSE; // don't do it again
2742 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002743 }
2744 else
2745 {
2746 int prev_col = curwin->w_cursor.col;
2747
2748 // put the cursor on the last char, for 'tw' formatting
2749 if (prev_col > 0)
2750 dec_cursor();
2751 // only format when something was inserted
2752 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2753 insertchar(NUL, 0, -1);
2754 if (prev_col > 0
2755 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2756 inc_cursor();
2757 }
2758
2759 // If the popup menu is displayed pressing CTRL-Y means accepting
2760 // the selection without inserting anything. When
2761 // compl_enter_selects is set the Enter key does the same.
2762 if ((c == Ctrl_Y || (compl_enter_selects
2763 && (c == CAR || c == K_KENTER || c == NL)))
2764 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002765 {
2766 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002767 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002768 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002769
2770 // CTRL-E means completion is Ended, go back to the typed text.
2771 // but only do this, if the Popup is still visible
2772 if (c == Ctrl_E)
2773 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002774 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002775 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002776
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002777 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002778 if (compl_leader.string != NULL)
2779 {
2780 p = compl_leader.string;
2781 plen = compl_leader.length;
2782 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002783 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002784 {
2785 p = compl_orig_text.string;
2786 plen = compl_orig_text.length;
2787 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002788 if (p != NULL)
2789 {
2790 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002791
John Marriott5e6ea922024-11-23 14:01:57 +01002792 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002793 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002794 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002795 retval = TRUE;
2796 }
2797
2798 auto_format(FALSE, TRUE);
2799
2800 // Trigger the CompleteDonePre event to give scripts a chance to
2801 // act upon the completion before clearing the info, and restore
2802 // ctrl_x_mode, so that complete_info() can be used.
2803 ctrl_x_mode = prev_mode;
2804 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2805
2806 ins_compl_free();
2807 compl_started = FALSE;
2808 compl_matches = 0;
2809 if (!shortmess(SHM_COMPLETIONMENU))
2810 msg_clr_cmdline(); // necessary for "noshowmode"
2811 ctrl_x_mode = CTRL_X_NORMAL;
2812 compl_enter_selects = FALSE;
2813 if (edit_submode != NULL)
2814 {
2815 edit_submode = NULL;
2816 showmode();
2817 }
2818
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002819 if (c == Ctrl_C && cmdwin_type != 0)
2820 // Avoid the popup menu remains displayed when leaving the
2821 // command line window.
2822 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002823 // Indent now if a key was typed that is in 'cinkeys'.
2824 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2825 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002826 // Trigger the CompleteDone event to give scripts a chance to act
2827 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002828 trigger_complete_done_event(prev_mode, word);
2829 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002830
2831 return retval;
2832}
2833
2834/*
glepnircf7f0122025-04-15 19:02:00 +02002835 * Cancel completion.
2836 */
2837 int
2838ins_compl_cancel(void)
2839{
2840 return ins_compl_stop(' ', ctrl_x_mode, TRUE);
2841}
2842
2843/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002844 * Prepare for Insert mode completion, or stop it.
2845 * Called just after typing a character in Insert mode.
2846 * Returns TRUE when the character is not to be inserted;
2847 */
2848 int
2849ins_compl_prep(int c)
2850{
glepnir19e1dd62025-05-08 22:50:38 +02002851 int retval = FALSE;
2852 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002853
2854 // Forget any previous 'special' messages if this is actually
2855 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2856 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2857 edit_submode_extra = NULL;
2858
zeertzjq440d4cb2023-03-02 17:51:32 +00002859 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002860 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002861 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002862 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863 return retval;
2864
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002865#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002866 // Ignore mouse events in a popup window
2867 if (is_mouse_key(c))
2868 {
2869 // Ignore drag and release events, the position does not need to be in
2870 // the popup and it may have just closed.
2871 if (c == K_LEFTRELEASE
2872 || c == K_LEFTRELEASE_NM
2873 || c == K_MIDDLERELEASE
2874 || c == K_RIGHTRELEASE
2875 || c == K_X1RELEASE
2876 || c == K_X2RELEASE
2877 || c == K_LEFTDRAG
2878 || c == K_MIDDLEDRAG
2879 || c == K_RIGHTDRAG
2880 || c == K_X1DRAG
2881 || c == K_X2DRAG)
2882 return retval;
2883 if (popup_visible)
2884 {
2885 int row = mouse_row;
2886 int col = mouse_col;
2887 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2888
2889 if (wp != NULL && WIN_IS_POPUP(wp))
2890 return retval;
2891 }
2892 }
2893#endif
2894
zeertzjqdca29d92021-08-31 19:12:51 +02002895 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2896 {
2897 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2898 || !vim_is_ctrl_x_key(c))
2899 {
2900 // Not starting another completion mode.
2901 ctrl_x_mode = CTRL_X_CMDLINE;
2902
2903 // CTRL-X CTRL-Z should stop completion without inserting anything
2904 if (c == Ctrl_Z)
2905 retval = TRUE;
2906 }
2907 else
2908 {
2909 ctrl_x_mode = CTRL_X_CMDLINE;
2910
2911 // Other CTRL-X keys first stop completion, then start another
2912 // completion mode.
2913 ins_compl_prep(' ');
2914 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2915 }
2916 }
2917
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002918 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002919 if (ctrl_x_mode_not_defined_yet()
2920 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002921 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002922 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002923 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002924 }
2925
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002926 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002927 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2928 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002929 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002930 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002931 {
2932 // We're already in CTRL-X mode, do we stay in it?
2933 if (!vim_is_ctrl_x_key(c))
2934 {
glepnir40891ba2025-02-10 22:18:00 +01002935 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002936 edit_submode = NULL;
2937 }
2938 showmode();
2939 }
2940
2941 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2942 {
2943 // Show error message from attempted keyword completion (probably
2944 // 'Pattern not found') until another key is hit, then go back to
2945 // showing what mode we are in.
2946 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002947 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002948 && c != Ctrl_R && !ins_compl_pum_key(c))
2949 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002950 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002951 }
2952 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2953 // Trigger the CompleteDone event to give scripts a chance to act
2954 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002955 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002956
LemonBoy2bf52dd2022-04-09 18:17:34 +01002957 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002958
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002959 // reset continue_* if we left expansion-mode, if we stay they'll be
2960 // (re)set properly in ins_complete()
2961 if (!vim_is_ctrl_x_key(c))
2962 {
2963 compl_cont_status = 0;
2964 compl_cont_mode = 0;
2965 }
2966
2967 return retval;
2968}
2969
2970/*
2971 * Fix the redo buffer for the completion leader replacing some of the typed
2972 * text. This inserts backspaces and appends the changed text.
2973 * "ptr" is the known leader text or NUL.
2974 */
2975 static void
2976ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2977{
John Marriott5e6ea922024-11-23 14:01:57 +01002978 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002979 char_u *p;
2980 char_u *ptr = ptr_arg;
2981
2982 if (ptr == NULL)
2983 {
John Marriott5e6ea922024-11-23 14:01:57 +01002984 if (compl_leader.string != NULL)
2985 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002986 else
2987 return; // nothing to do
2988 }
John Marriott5e6ea922024-11-23 14:01:57 +01002989 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002990 {
John Marriott5e6ea922024-11-23 14:01:57 +01002991 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002992 // Find length of common prefix between original text and new completion
2993 while (p[len] != NUL && p[len] == ptr[len])
2994 len++;
2995 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002996 if (len > 0)
2997 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002998 // Add backspace characters for each remaining character in
2999 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003000 for (p += len; *p != NUL; MB_PTR_ADV(p))
3001 AppendCharToRedobuff(K_BS);
3002 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003003 if (ptr != NULL)
3004 AppendToRedobuffLit(ptr + len, -1);
3005}
3006
3007/*
3008 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3009 * (depending on flag) starting from buf and looking for a non-scanned
3010 * buffer (other than curbuf). curbuf is special, if it is called with
3011 * buf=curbuf then it has to be the first call for a given flag/expansion.
3012 *
3013 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3014 */
3015 static buf_T *
3016ins_compl_next_buf(buf_T *buf, int flag)
3017{
glepnir19e1dd62025-05-08 22:50:38 +02003018 static win_T *wp = NULL;
3019 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003020
3021 if (flag == 'w') // just windows
3022 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003023 if (buf == curbuf || !win_valid(wp))
3024 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003025 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01003026
3027 while (TRUE)
3028 {
Hirohito Higashi355db992025-04-28 18:07:02 +02003029 // Move to next window (wrap to first window if at the end)
3030 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
3031 // Break if we're back at start or found an unscanned buffer
3032 if (wp == curwin || !wp->w_buffer->b_scanned)
3033 break;
glepnir40891ba2025-02-10 22:18:00 +01003034 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003035 buf = wp->w_buffer;
3036 }
3037 else
glepnir40891ba2025-02-10 22:18:00 +01003038 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003039 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3040 // (unlisted buffers)
3041 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01003042 while (TRUE)
3043 {
3044 // Move to next buffer (wrap to first buffer if at the end)
3045 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
3046 // Break if we're back at start buffer
3047 if (buf == curbuf)
3048 break;
3049
3050 // Check buffer conditions based on flag
3051 if (flag == 'U')
3052 skip_buffer = buf->b_p_bl;
3053 else
3054 skip_buffer = !buf->b_p_bl ||
3055 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
3056
3057 // Break if we found a buffer that matches our criteria
3058 if (!skip_buffer && !buf->b_scanned)
3059 break;
3060 }
3061 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003062 return buf;
3063}
3064
3065#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003066
3067# ifdef FEAT_EVAL
3068static callback_T cfu_cb; // 'completefunc' callback function
3069static callback_T ofu_cb; // 'omnifunc' callback function
3070static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
3071# endif
3072
3073/*
3074 * Copy a global callback function to a buffer local callback.
3075 */
3076 static void
3077copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
3078{
3079 free_callback(bufcb);
3080 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
3081 copy_callback(bufcb, globcb);
3082}
3083
3084/*
3085 * Parse the 'completefunc' option value and set the callback function.
3086 * Invoked when the 'completefunc' option is set. The option value can be a
3087 * name of a function (string), or function(<name>) or funcref(<name>) or a
3088 * lambda expression.
3089 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003090 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003091did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003092{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003093 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
3094 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003095
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003096 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003097
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003098 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003099}
3100
3101/*
3102 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003103 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003104 */
3105 void
3106set_buflocal_cfu_callback(buf_T *buf UNUSED)
3107{
3108# ifdef FEAT_EVAL
3109 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
3110# endif
3111}
3112
3113/*
3114 * Parse the 'omnifunc' option value and set the callback function.
3115 * Invoked when the 'omnifunc' option is set. The option value can be a
3116 * name of a function (string), or function(<name>) or funcref(<name>) or a
3117 * lambda expression.
3118 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003119 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003120did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003121{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003122 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
3123 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003124
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003125 set_buflocal_ofu_callback(curbuf);
3126 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003127}
3128
3129/*
3130 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003131 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003132 */
3133 void
3134set_buflocal_ofu_callback(buf_T *buf UNUSED)
3135{
3136# ifdef FEAT_EVAL
3137 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
3138# endif
3139}
3140
3141/*
3142 * Parse the 'thesaurusfunc' option value and set the callback function.
3143 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
3144 * name of a function (string), or function(<name>) or funcref(<name>) or a
3145 * lambda expression.
3146 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003147 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003148did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003149{
3150 int retval;
3151
zeertzjq6eda2692024-11-03 09:23:33 +01003152 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003153 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003154 retval = option_set_callback_func(curbuf->b_p_tsrfu,
3155 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003156 else
3157 {
3158 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003159 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01003160 // when using :set, free the local callback
3161 if (!(args->os_flags & OPT_GLOBAL))
3162 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003163 }
3164
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003165 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003166}
3167
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003168/*
3169 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003170 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003171 */
3172 int
3173set_ref_in_insexpand_funcs(int copyID)
3174{
glepnir19e1dd62025-05-08 22:50:38 +02003175 int abort = set_ref_in_callback(&cfu_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003176 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
3177 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
3178
3179 return abort;
3180}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003181
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003182/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003183 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003184 */
3185 static char_u *
3186get_complete_funcname(int type)
3187{
3188 switch (type)
3189 {
3190 case CTRL_X_FUNCTION:
3191 return curbuf->b_p_cfu;
3192 case CTRL_X_OMNI:
3193 return curbuf->b_p_ofu;
3194 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003195 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003196 default:
3197 return (char_u *)"";
3198 }
3199}
3200
3201/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003202 * Get the callback to use for insert mode completion.
3203 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003204 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003205get_insert_callback(int type)
3206{
3207 if (type == CTRL_X_FUNCTION)
3208 return &curbuf->b_cfu_cb;
3209 if (type == CTRL_X_OMNI)
3210 return &curbuf->b_ofu_cb;
3211 // CTRL_X_THESAURUS
3212 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
3213}
3214
3215/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003216 * Execute user defined complete function 'completefunc', 'omnifunc' or
3217 * 'thesaurusfunc', and get matches in "matches".
Girish Palyacbe53192025-04-14 22:13:15 +02003218 * "type" can be one of CTRL_X_OMNI, CTRL_X_FUNCTION, or CTRL_X_THESAURUS.
3219 * Callback function "cb" is set if triggered by a function in the 'cpt'
3220 * option; otherwise, it is NULL.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003221 */
3222 static void
Girish Palyacbe53192025-04-14 22:13:15 +02003223expand_by_function(int type, char_u *base, callback_T *cb)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003224{
3225 list_T *matchlist = NULL;
3226 dict_T *matchdict = NULL;
3227 typval_T args[3];
3228 char_u *funcname;
3229 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003230 typval_T rettv;
3231 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003232 int retval;
Girish Palyacbe53192025-04-14 22:13:15 +02003233 int is_cpt_function = (cb != NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003234
Girish Palyacbe53192025-04-14 22:13:15 +02003235 if (!is_cpt_function)
3236 {
3237 funcname = get_complete_funcname(type);
3238 if (*funcname == NUL)
3239 return;
3240 cb = get_insert_callback(type);
3241 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003242
3243 // Call 'completefunc' to obtain the list of matches.
3244 args[0].v_type = VAR_NUMBER;
3245 args[0].vval.v_number = 0;
3246 args[1].v_type = VAR_STRING;
3247 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
3248 args[2].v_type = VAR_UNKNOWN;
3249
3250 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01003251 // Lock the text to avoid weird things from happening. Also disallow
3252 // switching to another window, it should not be needed and may end up in
3253 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01003254 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003255
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003256 retval = call_callback(cb, 0, &rettv, 2, args);
3257
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003258 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003259 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003260 {
3261 switch (rettv.v_type)
3262 {
3263 case VAR_LIST:
3264 matchlist = rettv.vval.v_list;
3265 break;
3266 case VAR_DICT:
3267 matchdict = rettv.vval.v_dict;
3268 break;
3269 case VAR_SPECIAL:
3270 if (rettv.vval.v_number == VVAL_NONE)
3271 compl_opt_suppress_empty = TRUE;
3272 // FALLTHROUGH
3273 default:
3274 // TODO: Give error message?
3275 clear_tv(&rettv);
3276 break;
3277 }
3278 }
zeertzjqcfe45652022-05-27 17:26:55 +01003279 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003280
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003281 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003282 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003283 validate_cursor();
3284 if (!EQUAL_POS(curwin->w_cursor, pos))
3285 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003286 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003287 goto theend;
3288 }
3289
3290 if (matchlist != NULL)
3291 ins_compl_add_list(matchlist);
3292 else if (matchdict != NULL)
3293 ins_compl_add_dict(matchdict);
3294
3295theend:
3296 // Restore State, it might have been changed.
3297 State = save_State;
3298
3299 if (matchdict != NULL)
3300 dict_unref(matchdict);
3301 if (matchlist != NULL)
3302 list_unref(matchlist);
3303}
3304#endif // FEAT_COMPL_FUNC
3305
3306#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003307
3308 static inline int
3309get_user_highlight_attr(char_u *hlname)
3310{
3311 if (hlname != NULL && *hlname != NUL)
Hirohito Higashi355db992025-04-28 18:07:02 +02003312 return syn_name2attr(hlname);
glepnir38f99a12024-08-23 18:31:06 +02003313 return -1;
3314}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003315/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003316 * Add a match to the list of matches from a typeval_T.
3317 * If the given string is already in the list of completions, then return
3318 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3319 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003320 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003321 */
3322 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003323ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003324{
3325 char_u *word;
3326 int dup = FALSE;
3327 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003328 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003329 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003330 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003331 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003332 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003333 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003334 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003335
Bram Moolenaar08928322020-01-04 14:32:48 +01003336 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003337 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3338 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003339 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3340 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3341 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3342 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3343 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003344
glepnir0fe17f82024-10-08 22:26:44 +02003345 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003346 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003347
3348 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003349 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003350
Bram Moolenaard61efa52022-07-23 09:52:04 +01003351 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3352 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3353 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003354 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003355 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3356 dup = dict_get_number(tv->vval.v_dict, "dup");
3357 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3358 empty = dict_get_number(tv->vval.v_dict, "empty");
3359 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3360 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003361 flags |= CP_EQUAL;
3362 }
3363 else
3364 {
3365 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003366 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003367 }
3368 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003369 {
3370 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003371 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003372 }
glepnir508e7852024-07-25 21:39:08 +02003373 status = ins_compl_add(word, -1, NULL, cptext,
glepnirf31cfa22025-03-06 21:59:13 +01003374 &user_data, dir, flags, dup, user_hl, 0);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003375 if (status != OK)
3376 clear_tv(&user_data);
3377 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003378}
3379
3380/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003381 * Add completions from a list.
3382 */
3383 static void
3384ins_compl_add_list(list_T *list)
3385{
3386 listitem_T *li;
3387 int dir = compl_direction;
3388
3389 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003390 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003391 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003392 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003393 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003394 // if dir was BACKWARD then honor it just once
3395 dir = FORWARD;
3396 else if (did_emsg)
3397 break;
3398 }
3399}
3400
3401/*
3402 * Add completions from a dict.
3403 */
3404 static void
3405ins_compl_add_dict(dict_T *dict)
3406{
3407 dictitem_T *di_refresh;
3408 dictitem_T *di_words;
3409
3410 // Check for optional "refresh" item.
3411 compl_opt_refresh_always = FALSE;
3412 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3413 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3414 {
3415 char_u *v = di_refresh->di_tv.vval.v_string;
3416
3417 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3418 compl_opt_refresh_always = TRUE;
3419 }
3420
3421 // Add completions from a "words" list.
3422 di_words = dict_find(dict, (char_u *)"words", 5);
3423 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3424 ins_compl_add_list(di_words->di_tv.vval.v_list);
3425}
3426
3427/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003428 * Start completion for the complete() function.
3429 * "startcol" is where the matched text starts (1 is first column).
3430 * "list" is the list of matches.
3431 */
3432 static void
3433set_completion(colnr_T startcol, list_T *list)
3434{
3435 int save_w_wrow = curwin->w_wrow;
3436 int save_w_leftcol = curwin->w_leftcol;
3437 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003438 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003439 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3440 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3441 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003442
3443 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003444 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003445 ins_compl_prep(' ');
3446 ins_compl_clear();
3447 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003448 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003449
3450 compl_direction = FORWARD;
3451 if (startcol > curwin->w_cursor.col)
3452 startcol = curwin->w_cursor.col;
3453 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003454 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003455 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3456 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003457 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3458 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003459 if (p_ic)
3460 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003461 if (compl_orig_text.string == NULL)
3462 {
3463 compl_orig_text.length = 0;
3464 return;
3465 }
3466 compl_orig_text.length = (size_t)compl_length;
3467 if (ins_compl_add(compl_orig_text.string,
Hirohito Higashi355db992025-04-28 18:07:02 +02003468 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3469 flags | CP_FAST, FALSE, NULL, 0) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003470 return;
3471
3472 ctrl_x_mode = CTRL_X_EVAL;
3473
3474 ins_compl_add_list(list);
3475 compl_matches = ins_compl_make_cyclic();
3476 compl_started = TRUE;
3477 compl_used_match = TRUE;
3478 compl_cont_status = 0;
3479
3480 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003481 int no_select = compl_no_select || compl_longest;
3482 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003483 {
3484 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003485 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003486 // Down/Up has no real effect.
3487 ins_complete(K_UP, FALSE);
3488 }
3489 else
3490 ins_complete(Ctrl_N, FALSE);
3491 compl_enter_selects = compl_no_insert;
3492
3493 // Lazily show the popup menu, unless we got interrupted.
3494 if (!compl_interrupted)
3495 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003496 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003497 out_flush();
3498}
3499
3500/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003501 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003502 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003503 void
3504f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003505{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003506 if (in_vim9script()
3507 && (check_for_number_arg(argvars, 0) == FAIL
3508 || check_for_list_arg(argvars, 1) == FAIL))
3509 return;
3510
Bram Moolenaar24959102022-05-07 20:01:16 +01003511 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003512 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003513 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003514 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003515 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003516
3517 // Check for undo allowed here, because if something was already inserted
3518 // the line was already saved for undo and this check isn't done.
3519 if (!undo_allowed())
3520 return;
3521
Bram Moolenaard83392a2022-09-01 12:22:46 +01003522 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003523 {
glepnir19e1dd62025-05-08 22:50:38 +02003524 int startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarff06f282020-04-21 22:01:14 +02003525 if (startcol > 0)
3526 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003527 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003528}
3529
3530/*
3531 * "complete_add()" function
3532 */
3533 void
3534f_complete_add(typval_T *argvars, typval_T *rettv)
3535{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003536 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003537 return;
3538
Bram Moolenaar440cf092021-04-03 20:13:30 +02003539 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003540}
3541
3542/*
3543 * "complete_check()" function
3544 */
3545 void
3546f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3547{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003548 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003549 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003550
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003551 ins_compl_check_keys(0, TRUE);
3552 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003553
3554 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003555}
3556
3557/*
glepnirbcd59952025-04-24 21:48:35 +02003558 * Add match item to the return list.
3559 * Returns FAIL if out of memory, OK otherwise.
3560 */
3561 static int
3562add_match_to_list(
3563 typval_T *rettv,
3564 char_u *str,
3565 int len,
3566 int pos)
3567{
3568 list_T *match;
3569 int ret;
3570
3571 match = list_alloc();
3572 if (match == NULL)
Hirohito Higashi355db992025-04-28 18:07:02 +02003573 return FAIL;
glepnirbcd59952025-04-24 21:48:35 +02003574
3575 if ((ret = list_append_number(match, pos + 1)) == FAIL
3576 || (ret = list_append_string(match, str, len)) == FAIL
3577 || (ret = list_append_list(rettv->vval.v_list, match)) == FAIL)
3578 {
Hirohito Higashi355db992025-04-28 18:07:02 +02003579 vim_free(match);
3580 return FAIL;
glepnirbcd59952025-04-24 21:48:35 +02003581 }
3582
3583 return OK;
3584}
3585
3586/*
3587 * "complete_match()" function
3588 */
3589 void
3590f_complete_match(typval_T *argvars, typval_T *rettv)
3591{
3592 linenr_T lnum;
3593 colnr_T col;
3594 char_u *line = NULL;
3595 char_u *ise = NULL;
3596 regmatch_T regmatch;
3597 char_u *before_cursor = NULL;
3598 char_u *cur_end = NULL;
glepnirbcd59952025-04-24 21:48:35 +02003599 int bytepos = 0;
3600 char_u part[MAXPATHL];
3601 int ret;
3602
3603 if (rettv_list_alloc(rettv) == FAIL)
3604 return;
3605
3606 ise = curbuf->b_p_ise[0] != NUL ? curbuf->b_p_ise : p_ise;
3607
3608 if (argvars[0].v_type == VAR_UNKNOWN)
3609 {
3610 lnum = curwin->w_cursor.lnum;
3611 col = curwin->w_cursor.col;
3612 }
3613 else if (argvars[1].v_type == VAR_UNKNOWN)
3614 {
3615 emsg(_(e_invalid_argument));
3616 return;
3617 }
3618 else
3619 {
3620 lnum = (linenr_T)tv_get_number(&argvars[0]);
3621 col = (colnr_T)tv_get_number(&argvars[1]);
3622 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
3623 {
3624 semsg(_(e_invalid_line_number_nr), lnum);
3625 return;
3626 }
3627 if (col < 1 || col > ml_get_buf_len(curbuf, lnum))
3628 {
3629 semsg(_(e_invalid_column_number_nr), col + 1);
3630 return;
3631 }
3632 }
3633
3634 line = ml_get_buf(curbuf, lnum, FALSE);
3635 if (line == NULL)
3636 return;
3637
3638 before_cursor = vim_strnsave(line, col);
3639 if (before_cursor == NULL)
3640 return;
3641
3642 if (ise == NULL || *ise == NUL)
3643 {
3644 regmatch.regprog = vim_regcomp((char_u *)"\\k\\+$", RE_MAGIC);
3645 if (regmatch.regprog != NULL)
3646 {
3647 if (vim_regexec_nl(&regmatch, before_cursor, (colnr_T)0))
3648 {
Christian Brabandt3accf042025-04-25 19:01:06 +02003649 char_u *trig = vim_strnsave(regmatch.startp[0],
glepnirbcd59952025-04-24 21:48:35 +02003650 regmatch.endp[0] - regmatch.startp[0]);
3651 if (trig == NULL)
3652 {
3653 vim_free(before_cursor);
Christian Brabandt3accf042025-04-25 19:01:06 +02003654 vim_regfree(regmatch.regprog);
glepnirbcd59952025-04-24 21:48:35 +02003655 return;
3656 }
3657
Christian Brabandt3accf042025-04-25 19:01:06 +02003658 bytepos = (int)(regmatch.startp[0] - before_cursor);
glepnirbcd59952025-04-24 21:48:35 +02003659 ret = add_match_to_list(rettv, trig, -1, bytepos);
3660 vim_free(trig);
3661 if (ret == FAIL)
3662 {
Christian Brabandt3accf042025-04-25 19:01:06 +02003663 vim_free(before_cursor);
glepnirbcd59952025-04-24 21:48:35 +02003664 vim_regfree(regmatch.regprog);
3665 return;
3666 }
3667 }
3668 vim_regfree(regmatch.regprog);
3669 }
3670 }
3671 else
3672 {
glepnir08db2f42025-05-14 20:26:19 +02003673 char_u *p = ise;
3674 char_u *p_space = NULL;
3675
glepnirbcd59952025-04-24 21:48:35 +02003676 cur_end = before_cursor + (int)STRLEN(before_cursor);
3677
3678 while (*p != NUL)
3679 {
glepnir8d0e42b2025-05-12 20:28:28 +02003680 int len = 0;
glepnir08db2f42025-05-14 20:26:19 +02003681 if (p_space)
glepnir8d0e42b2025-05-12 20:28:28 +02003682 {
glepnir08db2f42025-05-14 20:26:19 +02003683 len = p - p_space - 1;
3684 memcpy(part, p_space + 1, len);
3685 p_space = NULL;
glepnir8d0e42b2025-05-12 20:28:28 +02003686 }
3687 else
glepnir08db2f42025-05-14 20:26:19 +02003688 {
3689 char_u *next_comma = vim_strchr((*p == ',') ? p + 1 : p, ',');
3690 if (next_comma && *(next_comma + 1) == ' ')
3691 p_space = next_comma;
3692
glepnir8d0e42b2025-05-12 20:28:28 +02003693 len = copy_option_part(&p, part, MAXPATHL, ",");
glepnir08db2f42025-05-14 20:26:19 +02003694 }
glepnirbcd59952025-04-24 21:48:35 +02003695
3696 if (len > 0 && len <= col)
3697 {
3698 if (STRNCMP(cur_end - len, part, len) == 0)
3699 {
3700 bytepos = col - len;
3701 if (add_match_to_list(rettv, part, len, bytepos) == FAIL)
3702 {
3703 vim_free(before_cursor);
3704 return;
3705 }
3706 }
3707 }
3708 }
3709 }
3710
3711 vim_free(before_cursor);
3712}
3713
3714/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003715 * Return Insert completion mode name string
3716 */
3717 static char_u *
3718ins_compl_mode(void)
3719{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003720 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003721 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3722
3723 return (char_u *)"";
3724}
3725
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003726/*
3727 * Assign the sequence number to all the completion matches which don't have
3728 * one assigned yet.
3729 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003730 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003731ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003732{
3733 int number = 0;
3734 compl_T *match;
3735
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003736 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003737 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003738 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003739 // This should normally succeed already at the first loop
3740 // cycle, so it's fast!
3741 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003742 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003743 if (match->cp_number != -1)
3744 {
3745 number = match->cp_number;
3746 break;
3747 }
3748 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003749 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003750 for (match = match->cp_next;
3751 match != NULL && match->cp_number == -1;
3752 match = match->cp_next)
3753 match->cp_number = ++number;
3754 }
3755 else // BACKWARD
3756 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003757 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003758 // number. This should normally succeed already at the
3759 // first loop cycle, so it's fast!
3760 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003761 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003762 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003763 if (match->cp_number != -1)
3764 {
3765 number = match->cp_number;
3766 break;
3767 }
glepnir40891ba2025-02-10 22:18:00 +01003768 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003769 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003770 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003771 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003772 for (match = match->cp_prev; match
3773 && match->cp_number == -1;
3774 match = match->cp_prev)
3775 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003776 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003777 }
3778}
3779
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003780/*
glepnir037b0282025-01-16 14:37:44 +01003781 * Fill the dict of complete_info
3782 */
3783 static void
3784fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3785{
3786 dict_add_string(di, "word", match->cp_str.string);
3787 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3788 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3789 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3790 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3791 if (add_match)
Hirohito Higashi355db992025-04-28 18:07:02 +02003792 dict_add_bool(di, "match", match->cp_in_match_array);
glepnir037b0282025-01-16 14:37:44 +01003793 if (match->cp_user_data.v_type == VAR_UNKNOWN)
Hirohito Higashi355db992025-04-28 18:07:02 +02003794 // Add an empty string for backwards compatibility
3795 dict_add_string(di, "user_data", (char_u *)"");
glepnir037b0282025-01-16 14:37:44 +01003796 else
Hirohito Higashi355db992025-04-28 18:07:02 +02003797 dict_add_tv(di, "user_data", &match->cp_user_data);
glepnir037b0282025-01-16 14:37:44 +01003798}
3799
3800/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003801 * Get complete information
3802 */
3803 static void
3804get_complete_info(list_T *what_list, dict_T *retdict)
3805{
3806 int ret = OK;
3807 listitem_T *item;
3808#define CI_WHAT_MODE 0x01
3809#define CI_WHAT_PUM_VISIBLE 0x02
3810#define CI_WHAT_ITEMS 0x04
3811#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003812#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003813#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003814#define CI_WHAT_ALL 0xff
3815 int what_flag;
Girish Palya0ac1eb32025-04-16 20:18:33 +02003816 int compl_fuzzy_match = (get_cot_flags() & COT_FUZZY) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003817
3818 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003819 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003820 else
3821 {
3822 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003823 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003824 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003825 {
3826 char_u *what = tv_get_string(&item->li_tv);
3827
3828 if (STRCMP(what, "mode") == 0)
3829 what_flag |= CI_WHAT_MODE;
3830 else if (STRCMP(what, "pum_visible") == 0)
3831 what_flag |= CI_WHAT_PUM_VISIBLE;
3832 else if (STRCMP(what, "items") == 0)
3833 what_flag |= CI_WHAT_ITEMS;
3834 else if (STRCMP(what, "selected") == 0)
3835 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003836 else if (STRCMP(what, "completed") == 0)
3837 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003838 else if (STRCMP(what, "matches") == 0)
3839 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003840 }
3841 }
3842
3843 if (ret == OK && (what_flag & CI_WHAT_MODE))
3844 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3845
3846 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3847 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3848
glepnir037b0282025-01-16 14:37:44 +01003849 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3850 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003851 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003852 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003853 dict_T *di;
3854 compl_T *match;
3855 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003856 int has_items = what_flag & CI_WHAT_ITEMS;
3857 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003858 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003859
glepnird4088ed2024-12-31 10:55:22 +01003860 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003861 {
3862 li = list_alloc();
3863 if (li == NULL)
3864 return;
glepnird4088ed2024-12-31 10:55:22 +01003865 ret = dict_add_list(retdict, (has_matches && !has_items)
3866 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003867 }
3868 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3869 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3870 ins_compl_update_sequence_numbers();
3871 if (ret == OK && compl_first_match != NULL)
3872 {
3873 int list_idx = 0;
3874 match = compl_first_match;
3875 do
3876 {
3877 if (!match_at_original_text(match))
3878 {
glepnird4088ed2024-12-31 10:55:22 +01003879 if (has_items
3880 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003881 {
3882 di = dict_alloc();
3883 if (di == NULL)
3884 return;
3885 ret = list_append_dict(li, di);
3886 if (ret != OK)
3887 return;
glepnir037b0282025-01-16 14:37:44 +01003888 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003889 }
glepnird4088ed2024-12-31 10:55:22 +01003890 if (compl_curr_match != NULL
3891 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003892 selected_idx = list_idx;
Girish Palya0ac1eb32025-04-16 20:18:33 +02003893 if (compl_fuzzy_match || match->cp_in_match_array)
3894 list_idx += 1;
Girish Palya8950bf72024-03-20 20:07:29 +01003895 }
3896 match = match->cp_next;
3897 }
3898 while (match != NULL && !is_first_match(match));
3899 }
3900 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3901 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003902
glepnir037b0282025-01-16 14:37:44 +01003903 if (ret == OK && selected_idx != -1 && has_completed)
3904 {
3905 di = dict_alloc();
3906 if (di == NULL)
3907 return;
3908 fill_complete_info_dict(di, compl_curr_match, FALSE);
3909 ret = dict_add_dict(retdict, "completed", di);
3910 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003911 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003912}
3913
3914/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003915 * "complete_info()" function
3916 */
3917 void
3918f_complete_info(typval_T *argvars, typval_T *rettv)
3919{
3920 list_T *what_list = NULL;
3921
Bram Moolenaar93a10962022-06-16 11:42:09 +01003922 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003923 return;
3924
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003925 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3926 return;
3927
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003928 if (argvars[0].v_type != VAR_UNKNOWN)
3929 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003930 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003931 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003932 what_list = argvars[0].vval.v_list;
3933 }
3934 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003935}
3936#endif
3937
3938/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003939 * Returns TRUE when using a user-defined function for thesaurus completion.
3940 */
3941 static int
3942thesaurus_func_complete(int type UNUSED)
3943{
3944#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003945 return type == CTRL_X_THESAURUS
3946 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003947#else
3948 return FALSE;
3949#endif
3950}
3951
3952/*
Girish Palya7c621052025-05-26 19:41:59 +02003953 * Check if 'cpt' list index can be advanced to the next completion source.
3954 */
3955 static int
3956may_advance_cpt_index(char_u *cpt)
3957{
3958 char_u *p = cpt;
3959
3960 while (*p == ',' || *p == ' ') // Skip delimiters
3961 p++;
3962 return (*p != NUL);
3963}
3964
3965/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003966 * Return value of process_next_cpt_value()
3967 */
3968enum
3969{
3970 INS_COMPL_CPT_OK = 1,
3971 INS_COMPL_CPT_CONT,
3972 INS_COMPL_CPT_END
3973};
3974
3975/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003976 * state information used for getting the next set of insert completion
3977 * matches.
3978 */
3979typedef struct
3980{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003981 char_u *e_cpt_copy; // copy of 'complete'
3982 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003983 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003984 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003985 pos_T prev_match_pos; // previous match position
3986 int set_match_pos; // save first_match_pos/last_match_pos
3987 pos_T first_match_pos; // first match position
3988 pos_T last_match_pos; // last match position
3989 int found_all; // found all matches of a certain type.
3990 char_u *dict; // dictionary file to search
3991 int dict_f; // "dict" is an exact file name or not
Girish Palyacbe53192025-04-14 22:13:15 +02003992 callback_T *func_cb; // callback of function in 'cpt' option
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003993} ins_compl_next_state_T;
3994
3995/*
3996 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003997 *
3998 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003999 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004000 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004001 * st->found_all - all matches of this type are found
4002 * st->ins_buf - search for completions in this buffer
4003 * st->first_match_pos - position of the first completion match
4004 * st->last_match_pos - position of the last completion match
4005 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004006 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004007 * st->dict - name of the dictionary or thesaurus file to search
4008 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004009 *
4010 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004011 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
4012 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004013 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004014 */
4015 static int
4016process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004017 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004018 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02004019 pos_T *start_match_pos,
Girish Palya7c621052025-05-26 19:41:59 +02004020 int fuzzy_collect,
4021 int *advance_cpt_idx)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004022{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004023 int compl_type = -1;
4024 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004025
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004026 st->found_all = FALSE;
Girish Palya7c621052025-05-26 19:41:59 +02004027 *advance_cpt_idx = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004028
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004029 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
4030 st->e_cpt++;
4031
4032 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004033 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004034 st->ins_buf = curbuf;
4035 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004036 // Move the cursor back one character so that ^N can match the
4037 // word immediately after the cursor.
glepnir53b14572025-03-12 21:28:39 +01004038 if (ctrl_x_mode_normal() && (!fuzzy_collect && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004039 {
4040 // Move the cursor to after the last character in the
4041 // buffer, so that word at start of buffer is found
4042 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004043 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01004044 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004045 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004046 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004047 compl_type = 0;
4048
4049 // Remember the first match so that the loop stops when we
4050 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004051 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004052 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004053 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004054 && (st->ins_buf = ins_compl_next_buf(
4055 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004056 {
4057 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004058 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004059 {
4060 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004061 st->first_match_pos.col = st->last_match_pos.col = 0;
4062 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
4063 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004064 compl_type = 0;
4065 }
4066 else // unloaded buffer, scan like dictionary
4067 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004068 st->found_all = TRUE;
4069 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004070 {
4071 status = INS_COMPL_CPT_CONT;
4072 goto done;
4073 }
4074 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004075 st->dict = st->ins_buf->b_fname;
4076 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004077 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01004078 if (!shortmess(SHM_COMPLETIONSCAN))
4079 {
4080 msg_hist_off = TRUE; // reset in msg_trunc_attr()
4081 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
4082 st->ins_buf->b_fname == NULL
4083 ? buf_spname(st->ins_buf)
4084 : st->ins_buf->b_sfname == NULL
4085 ? st->ins_buf->b_fname
4086 : st->ins_buf->b_sfname);
4087 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
4088 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004089 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004090 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004091 status = INS_COMPL_CPT_END;
4092 else
4093 {
4094 if (ctrl_x_mode_line_or_eval())
4095 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004096 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004097 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004098 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004099 compl_type = CTRL_X_DICTIONARY;
4100 else
4101 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004102 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004103 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004104 st->dict = st->e_cpt;
4105 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004106 }
4107 }
Girish Palyacbe53192025-04-14 22:13:15 +02004108#ifdef FEAT_COMPL_FUNC
Girish Palya14f6da52025-05-26 19:04:25 +02004109 else if (*st->e_cpt == 'F' || *st->e_cpt == 'o')
Girish Palyacbe53192025-04-14 22:13:15 +02004110 {
4111 compl_type = CTRL_X_FUNCTION;
4112 if (*st->e_cpt == 'o')
4113 st->func_cb = &curbuf->b_ofu_cb;
4114 else
4115 st->func_cb = (*++st->e_cpt != ',' && *st->e_cpt != NUL)
4116 ? get_cpt_func_callback(st->e_cpt) : &curbuf->b_cfu_cb;
4117 if (!st->func_cb)
4118 compl_type = -1;
4119 }
4120#endif
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004121#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004122 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004123 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004124 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004125 compl_type = CTRL_X_PATH_DEFINES;
4126#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004127 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004128 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004129 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01004130 if (!shortmess(SHM_COMPLETIONSCAN))
4131 {
4132 msg_hist_off = TRUE; // reset in msg_trunc_attr()
4133 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
4134 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
4135 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004136 }
4137 else
4138 compl_type = -1;
4139
4140 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004141 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Girish Palya7c621052025-05-26 19:41:59 +02004142 *advance_cpt_idx = may_advance_cpt_index(st->e_cpt);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004143
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004144 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004145 if (compl_type == -1)
4146 status = INS_COMPL_CPT_CONT;
4147 }
4148
4149done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004150 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004151 return status;
4152}
4153
4154#ifdef FEAT_FIND_ID
4155/*
4156 * Get the next set of identifiers or defines matching "compl_pattern" in
4157 * included files.
4158 */
4159 static void
4160get_next_include_file_completion(int compl_type)
4161{
John Marriott5e6ea922024-11-23 14:01:57 +01004162 find_pattern_in_path(compl_pattern.string, compl_direction,
4163 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004164 (compl_type == CTRL_X_PATH_DEFINES
4165 && !(compl_cont_status & CONT_SOL))
4166 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01004167 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004168}
4169#endif
4170
4171/*
4172 * Get the next set of words matching "compl_pattern" in dictionary or
4173 * thesaurus files.
4174 */
4175 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004176get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004177{
4178#ifdef FEAT_COMPL_FUNC
4179 if (thesaurus_func_complete(compl_type))
Girish Palyacbe53192025-04-14 22:13:15 +02004180 expand_by_function(compl_type, compl_pattern.string, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004181 else
4182#endif
4183 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004184 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004185 : (compl_type == CTRL_X_THESAURUS
4186 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
4187 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01004188 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004189 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004190 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004191}
4192
4193/*
4194 * Get the next set of tag names matching "compl_pattern".
4195 */
4196 static void
4197get_next_tag_completion(void)
4198{
4199 int save_p_ic;
4200 char_u **matches;
4201 int num_matches;
4202
4203 // set p_ic according to p_ic, p_scs and pat for find_tags().
4204 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01004205 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004206
4207 // Find up to TAG_MANY matches. Avoids that an enormous number
4208 // of matches is found when compl_pattern is empty
4209 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01004210 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004211 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004212 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004213 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4214 ins_compl_add_matches(num_matches, matches, p_ic);
4215 g_tag_at_cursor = FALSE;
4216 p_ic = save_p_ic;
4217}
4218
4219/*
glepnir8159fb12024-07-17 20:32:54 +02004220 * Compare function for qsort
4221 */
glepnirf31cfa22025-03-06 21:59:13 +01004222 static int
4223compare_scores(const void *a, const void *b)
glepnir8159fb12024-07-17 20:32:54 +02004224{
4225 int idx_a = *(const int *)a;
4226 int idx_b = *(const int *)b;
4227 int score_a = compl_fuzzy_scores[idx_a];
4228 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02004229 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
4230 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02004231}
4232
4233/*
glepnirf31cfa22025-03-06 21:59:13 +01004234 * insert prefix with redraw
4235 */
4236 static void
4237ins_compl_longest_insert(char_u *prefix)
4238{
4239 ins_compl_delete();
4240 ins_compl_insert_bytes(prefix + get_compl_len(), -1);
4241 ins_redraw(FALSE);
4242}
4243
4244/*
4245 * Calculate the longest common prefix among the best fuzzy matches
4246 * stored in compl_best_matches, and insert it as the longest.
4247 */
4248 static void
4249fuzzy_longest_match(void)
4250{
4251 char_u *prefix = NULL;
4252 int prefix_len = 0;
4253 int i = 0;
4254 int j = 0;
4255 char_u *match_str = NULL;
4256 char_u *prefix_ptr = NULL;
4257 char_u *match_ptr = NULL;
4258 char_u *leader = NULL;
4259 size_t leader_len = 0;
4260 compl_T *compl = NULL;
4261 int more_candidates = FALSE;
4262 compl_T *nn_compl = NULL;
4263
4264 if (compl_num_bests == 0)
Hirohito Higashi355db992025-04-28 18:07:02 +02004265 return;
glepnirf31cfa22025-03-06 21:59:13 +01004266
4267 nn_compl = compl_first_match->cp_next->cp_next;
4268 if (nn_compl && nn_compl != compl_first_match)
4269 more_candidates = TRUE;
4270
4271 compl = ctrl_x_mode_whole_line() ? compl_first_match
4272 : compl_first_match->cp_next;
4273 if (compl_num_bests == 1)
4274 {
4275 // no more candidates insert the match str
4276 if (!more_candidates)
4277 {
4278 ins_compl_longest_insert(compl->cp_str.string);
4279 compl_num_bests = 0;
4280 }
4281 compl_num_bests = 0;
4282 return;
4283 }
4284
4285 compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *));
4286 if (compl_best_matches == NULL)
Hirohito Higashi355db992025-04-28 18:07:02 +02004287 return;
glepnirf31cfa22025-03-06 21:59:13 +01004288 while (compl != NULL && i < compl_num_bests)
4289 {
Hirohito Higashi355db992025-04-28 18:07:02 +02004290 compl_best_matches[i] = compl;
4291 compl = compl->cp_next;
4292 i++;
glepnirf31cfa22025-03-06 21:59:13 +01004293 }
4294
4295 prefix = compl_best_matches[0]->cp_str.string;
zeertzjq4422de62025-03-07 19:06:02 +01004296 prefix_len = (int)compl_best_matches[0]->cp_str.length;
glepnirf31cfa22025-03-06 21:59:13 +01004297
4298 for (i = 1; i < compl_num_bests; i++)
4299 {
4300 match_str = compl_best_matches[i]->cp_str.string;
Hirohito Higashi355db992025-04-28 18:07:02 +02004301 prefix_ptr = prefix;
4302 match_ptr = match_str;
4303 j = 0;
glepnirf31cfa22025-03-06 21:59:13 +01004304
4305 while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL)
4306 {
4307 if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0)
4308 break;
4309
4310 MB_PTR_ADV(prefix_ptr);
4311 MB_PTR_ADV(match_ptr);
4312 j++;
4313 }
4314
4315 if (j > 0)
4316 prefix_len = j;
4317 }
4318
4319 leader = ins_compl_leader();
zeertzjq4422de62025-03-07 19:06:02 +01004320 leader_len = ins_compl_leader_len();
glepnirf31cfa22025-03-06 21:59:13 +01004321
4322 // skip non-consecutive prefixes
zeertzjq4422de62025-03-07 19:06:02 +01004323 if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0)
glepnirf31cfa22025-03-06 21:59:13 +01004324 goto end;
4325
zeertzjq4422de62025-03-07 19:06:02 +01004326 prefix = vim_strnsave(prefix, prefix_len);
glepnirf31cfa22025-03-06 21:59:13 +01004327 if (prefix != NULL)
4328 {
4329 ins_compl_longest_insert(prefix);
4330 compl_cfc_longest_ins = TRUE;
4331 vim_free(prefix);
4332 }
4333
4334end:
4335 vim_free(compl_best_matches);
4336 compl_best_matches = NULL;
4337 compl_num_bests = 0;
4338}
4339
4340/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004341 * Get the next set of filename matching "compl_pattern".
4342 */
4343 static void
4344get_next_filename_completion(void)
4345{
4346 char_u **matches;
4347 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02004348 char_u *ptr;
4349 garray_T fuzzy_indices;
4350 int i;
4351 int score;
4352 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01004353 size_t leader_len = ins_compl_leader_len();;
glepnirf31cfa22025-03-06 21:59:13 +01004354 int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0);
glepnir8159fb12024-07-17 20:32:54 +02004355 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02004356 char_u *last_sep = NULL;
glepnirf31cfa22025-03-06 21:59:13 +01004357 int need_collect_bests = in_fuzzy_collect && compl_get_longest;
4358 int max_score = 0;
4359 int current_score = 0;
4360 int dir = compl_direction;
glepnir8159fb12024-07-17 20:32:54 +02004361
glepnirb9de1a02024-08-02 19:14:38 +02004362#ifdef BACKSLASH_IN_FILENAME
4363 char pathsep = (curbuf->b_p_csl[0] == 's') ?
4364 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
4365#else
4366 char pathsep = PATHSEP;
4367#endif
4368
glepnirf31cfa22025-03-06 21:59:13 +01004369 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004370 {
glepnirb9de1a02024-08-02 19:14:38 +02004371#ifdef BACKSLASH_IN_FILENAME
4372 if (curbuf->b_p_csl[0] == 's')
4373 {
John Marriott5e6ea922024-11-23 14:01:57 +01004374 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004375 {
4376 if (leader[i] == '\\')
4377 leader[i] = '/';
4378 }
4379 }
4380 else if (curbuf->b_p_csl[0] == 'b')
4381 {
John Marriott5e6ea922024-11-23 14:01:57 +01004382 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004383 {
4384 if (leader[i] == '/')
4385 leader[i] = '\\';
4386 }
4387 }
4388#endif
4389 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02004390 if (last_sep == NULL)
4391 {
4392 // No path separator or separator is the last character,
4393 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01004394 VIM_CLEAR_STRING(compl_pattern);
4395 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
4396 if (compl_pattern.string == NULL)
4397 return;
4398 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02004399 }
4400 else if (*(last_sep + 1) == '\0')
glepnirf31cfa22025-03-06 21:59:13 +01004401 in_fuzzy_collect = FALSE;
glepnir0be03e12024-07-19 16:45:05 +02004402 else
4403 {
4404 // Split leader into path and file parts
4405 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004406 char_u *path_with_wildcard;
4407
4408 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02004409 if (path_with_wildcard != NULL)
4410 {
John Marriott5e6ea922024-11-23 14:01:57 +01004411 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
4412 VIM_CLEAR_STRING(compl_pattern);
4413 compl_pattern.string = path_with_wildcard;
4414 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02004415
4416 // Move leader to the file part
4417 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004418 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02004419 }
4420 }
glepnir8159fb12024-07-17 20:32:54 +02004421 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004422
John Marriott5e6ea922024-11-23 14:01:57 +01004423 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004424 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
4425 return;
4426
4427 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01004428 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004429#ifdef BACKSLASH_IN_FILENAME
4430 if (curbuf->b_p_csl[0] != NUL)
4431 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004432 for (i = 0; i < num_matches; ++i)
4433 {
glepnir8159fb12024-07-17 20:32:54 +02004434 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004435 while (*ptr != NUL)
4436 {
4437 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
4438 *ptr = '/';
4439 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
4440 *ptr = '\\';
4441 ptr += (*mb_ptr2len)(ptr);
4442 }
4443 }
4444 }
4445#endif
glepnir8159fb12024-07-17 20:32:54 +02004446
glepnirf31cfa22025-03-06 21:59:13 +01004447 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004448 {
4449 ga_init2(&fuzzy_indices, sizeof(int), 10);
4450 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
4451
4452 for (i = 0; i < num_matches; i++)
4453 {
4454 ptr = matches[i];
4455 score = fuzzy_match_str(ptr, leader);
4456 if (score > 0)
4457 {
4458 if (ga_grow(&fuzzy_indices, 1) == OK)
4459 {
4460 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
4461 compl_fuzzy_scores[i] = score;
4462 fuzzy_indices.ga_len++;
4463 }
4464 }
4465 }
4466
Christian Brabandt220474d2024-07-20 13:26:44 +02004467 // prevent qsort from deref NULL pointer
4468 if (fuzzy_indices.ga_len > 0)
4469 {
glepnirf31cfa22025-03-06 21:59:13 +01004470 char_u *match = NULL;
Christian Brabandt220474d2024-07-20 13:26:44 +02004471 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
4472 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02004473
Christian Brabandt220474d2024-07-20 13:26:44 +02004474 for (i = 0; i < fuzzy_indices.ga_len; ++i)
glepnirf31cfa22025-03-06 21:59:13 +01004475 {
Hirohito Higashi355db992025-04-28 18:07:02 +02004476 match = matches[fuzzy_indices_data[i]];
glepnirf31cfa22025-03-06 21:59:13 +01004477 current_score = compl_fuzzy_scores[fuzzy_indices_data[i]];
4478 if (ins_compl_add(match, -1, NULL, NULL, NULL, dir,
Hirohito Higashi355db992025-04-28 18:07:02 +02004479 CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0),
4480 FALSE, NULL, current_score) == OK)
glepnirf31cfa22025-03-06 21:59:13 +01004481 dir = FORWARD;
4482
4483 if (need_collect_bests)
4484 {
4485 if (i == 0 || current_score == max_score)
4486 {
4487 compl_num_bests++;
4488 max_score = current_score;
4489 }
4490 }
4491 }
glepnir8159fb12024-07-17 20:32:54 +02004492
Christian Brabandt220474d2024-07-20 13:26:44 +02004493 FreeWild(num_matches, matches);
Christian Brabandt220474d2024-07-20 13:26:44 +02004494 }
glepnir6b6280c2024-07-27 16:25:45 +02004495 else if (leader_len > 0)
4496 {
4497 FreeWild(num_matches, matches);
4498 num_matches = 0;
4499 }
Christian Brabandt220474d2024-07-20 13:26:44 +02004500
glepnir8159fb12024-07-17 20:32:54 +02004501 vim_free(compl_fuzzy_scores);
4502 ga_clear(&fuzzy_indices);
glepnirf31cfa22025-03-06 21:59:13 +01004503
4504 if (compl_num_bests > 0 && compl_get_longest)
4505 fuzzy_longest_match();
4506 return;
glepnir8159fb12024-07-17 20:32:54 +02004507 }
4508
glepnir6b6280c2024-07-27 16:25:45 +02004509 if (num_matches > 0)
4510 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004511}
4512
4513/*
4514 * Get the next set of command-line completions matching "compl_pattern".
4515 */
4516 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004517get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004518{
4519 char_u **matches;
4520 int num_matches;
4521
John Marriott5e6ea922024-11-23 14:01:57 +01004522 if (expand_cmdline(&compl_xp, compl_pattern.string,
4523 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004524 ins_compl_add_matches(num_matches, matches, FALSE);
4525}
4526
4527/*
4528 * Get the next set of spell suggestions matching "compl_pattern".
4529 */
4530 static void
4531get_next_spell_completion(linenr_T lnum UNUSED)
4532{
4533#ifdef FEAT_SPELL
4534 char_u **matches;
4535 int num_matches;
4536
John Marriott5e6ea922024-11-23 14:01:57 +01004537 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004538 if (num_matches > 0)
4539 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00004540 else
4541 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004542#endif
4543}
4544
4545/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004546 * Return the next word or line from buffer "ins_buf" at position
4547 * "cur_match_pos" for completion. The length of the match is set in "len".
4548 */
4549 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00004550ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004551 buf_T *ins_buf, // buffer being scanned
4552 pos_T *cur_match_pos, // current match position
4553 int *match_len,
4554 int *cont_s_ipos) // next ^X<> will set initial_pos
4555{
4556 char_u *ptr;
4557 int len;
4558
4559 *match_len = 0;
4560 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
4561 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01004562 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004563 if (ctrl_x_mode_line_or_eval())
4564 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004565 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004566 {
4567 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
4568 return NULL;
4569 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01004570 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004571 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01004572 {
4573 char_u *tmp_ptr = ptr;
4574
4575 ptr = skipwhite(tmp_ptr);
4576 len -= (int)(ptr - tmp_ptr);
4577 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004578 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004579 }
4580 else
4581 {
4582 char_u *tmp_ptr = ptr;
4583
John Marriott5e6ea922024-11-23 14:01:57 +01004584 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004585 {
4586 tmp_ptr += compl_length;
4587 // Skip if already inside a word.
4588 if (vim_iswordp(tmp_ptr))
4589 return NULL;
4590 // Find start of next word.
4591 tmp_ptr = find_word_start(tmp_ptr);
4592 }
4593 // Find end of this word.
4594 tmp_ptr = find_word_end(tmp_ptr);
4595 len = (int)(tmp_ptr - ptr);
4596
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004597 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004598 {
4599 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4600 {
4601 // Try next line, if any. the new word will be
4602 // "join" as if the normal command "J" was used.
4603 // IOSIZE is always greater than
4604 // compl_length, so the next STRNCPY always
4605 // works -- Acevedo
4606 STRNCPY(IObuff, ptr, len);
4607 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4608 tmp_ptr = ptr = skipwhite(ptr);
4609 // Find start of next word.
4610 tmp_ptr = find_word_start(tmp_ptr);
4611 // Find end of next word.
4612 tmp_ptr = find_word_end(tmp_ptr);
4613 if (tmp_ptr > ptr)
4614 {
4615 if (*ptr != ')' && IObuff[len - 1] != TAB)
4616 {
4617 if (IObuff[len - 1] != ' ')
4618 IObuff[len++] = ' ';
4619 // IObuf =~ "\k.* ", thus len >= 2
4620 if (p_js
4621 && (IObuff[len - 2] == '.'
4622 || (vim_strchr(p_cpo, CPO_JOINSP)
4623 == NULL
4624 && (IObuff[len - 2] == '?'
4625 || IObuff[len - 2] == '!'))))
4626 IObuff[len++] = ' ';
4627 }
4628 // copy as much as possible of the new word
4629 if (tmp_ptr - ptr >= IOSIZE - len)
4630 tmp_ptr = ptr + IOSIZE - len - 1;
4631 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4632 len += (int)(tmp_ptr - ptr);
4633 *cont_s_ipos = TRUE;
4634 }
4635 IObuff[len] = NUL;
4636 ptr = IObuff;
4637 }
4638 if (len == compl_length)
4639 return NULL;
4640 }
4641 }
4642
4643 *match_len = len;
4644 return ptr;
4645}
4646
4647/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004648 * Get the next set of words matching "compl_pattern" for default completion(s)
4649 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004650 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4651 * position "st->start_pos" in the "compl_direction" direction. If
4652 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4653 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004654 * Returns OK if a new next match is found, otherwise returns FAIL.
4655 */
4656 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004657get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004658{
4659 int found_new_match = FAIL;
4660 int save_p_scs;
4661 int save_p_ws;
4662 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004663 char_u *ptr = NULL;
4664 int len = 0;
glepnirf31cfa22025-03-06 21:59:13 +01004665 int in_collect = (cfc_has_mode() && compl_length > 0);
glepnir8159fb12024-07-17 20:32:54 +02004666 char_u *leader = ins_compl_leader();
glepnirf31cfa22025-03-06 21:59:13 +01004667 int score = 0;
Girish Palyab1565882025-04-15 20:16:00 +02004668 int in_curbuf = st->ins_buf == curbuf;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004669
4670 // If 'infercase' is set, don't use 'smartcase' here
4671 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004672 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004673 p_scs = FALSE;
4674
4675 // Buffers other than curbuf are scanned from the beginning or the
4676 // end but never from the middle, thus setting nowrapscan in this
4677 // buffer is a good idea, on the other hand, we always set
4678 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4679 save_p_ws = p_ws;
Girish Palyab1565882025-04-15 20:16:00 +02004680 if (!in_curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004681 p_ws = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01004682 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004683 p_ws = TRUE;
4684 looped_around = FALSE;
4685 for (;;)
4686 {
4687 int cont_s_ipos = FALSE;
4688
4689 ++msg_silent; // Don't want messages for wrapscan.
4690
glepnirf31cfa22025-03-06 21:59:13 +01004691 if (in_collect)
4692 {
Hirohito Higashi355db992025-04-28 18:07:02 +02004693 found_new_match = search_for_fuzzy_match(st->ins_buf,
glepnir8159fb12024-07-17 20:32:54 +02004694 st->cur_match_pos, leader, compl_direction,
glepnirf31cfa22025-03-06 21:59:13 +01004695 start_pos, &len, &ptr, &score);
4696 }
4697 // ctrl_x_mode_line_or_eval() || word-wise search that
4698 // has added a word that was at the beginning of the line
4699 else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
4700 found_new_match = search_for_exact_line(st->ins_buf,
4701 st->cur_match_pos, compl_direction, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004702 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004703 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004704 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004705 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004706 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004707 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004708 {
4709 // set "compl_started" even on fail
4710 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004711 st->first_match_pos = *st->cur_match_pos;
4712 st->last_match_pos = *st->cur_match_pos;
4713 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004714 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004715 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4716 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004717 {
4718 found_new_match = FAIL;
4719 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004720 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004721 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4722 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4723 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004724 {
4725 if (looped_around)
4726 found_new_match = FAIL;
4727 else
4728 looped_around = TRUE;
4729 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004730 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004731 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4732 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4733 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004734 {
4735 if (looped_around)
4736 found_new_match = FAIL;
4737 else
4738 looped_around = TRUE;
4739 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004740 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004741 if (found_new_match == FAIL)
4742 break;
4743
4744 // when ADDING, the text before the cursor matches, skip it
Girish Palyab1565882025-04-15 20:16:00 +02004745 if (compl_status_adding() && in_curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004746 && start_pos->lnum == st->cur_match_pos->lnum
4747 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004748 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004749
glepnirf31cfa22025-03-06 21:59:13 +01004750 if (!in_collect)
glepnir8159fb12024-07-17 20:32:54 +02004751 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4752 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004753 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004754 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004755
Girish Palyab1565882025-04-15 20:16:00 +02004756 if (is_nearest_active() && in_curbuf)
4757 {
4758 score = st->cur_match_pos->lnum - curwin->w_cursor.lnum;
4759 if (score < 0)
4760 score = -score;
4761 score++;
4762 }
4763
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004764 if (ins_compl_add_infercase(ptr, len, p_ic,
Girish Palyab1565882025-04-15 20:16:00 +02004765 in_curbuf ? NULL : st->ins_buf->b_sfname,
glepnirf31cfa22025-03-06 21:59:13 +01004766 0, cont_s_ipos, score) != NOTDONE)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004767 {
glepnirf31cfa22025-03-06 21:59:13 +01004768 if (in_collect && score == compl_first_match->cp_next->cp_score)
4769 compl_num_bests++;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004770 found_new_match = OK;
4771 break;
4772 }
4773 }
4774 p_scs = save_p_scs;
4775 p_ws = save_p_ws;
4776
4777 return found_new_match;
4778}
4779
4780/*
Girish Palyacbe53192025-04-14 22:13:15 +02004781 * Return the callback function associated with "funcname".
4782 */
4783#ifdef FEAT_COMPL_FUNC
4784 static callback_T *
4785get_cpt_func_callback(char_u *funcname)
4786{
4787 static callback_T cb;
4788 char_u buf[LSIZE];
4789 int slen;
4790
4791 slen = copy_option_part(&funcname, buf, LSIZE, ",");
4792 if (slen > 0 && option_set_callback_func(buf, &cb))
4793 return &cb;
4794 return NULL;
4795}
4796
4797/*
4798 * Retrieve new completion matches by invoking callback "cb".
4799 */
4800 static void
4801expand_cpt_function(callback_T *cb)
4802{
4803 // Re-insert the text removed by ins_compl_delete().
4804 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
4805 // Get matches
4806 get_cpt_func_completion_matches(cb);
4807 // Undo insertion
4808 ins_compl_delete();
4809}
4810#endif
4811
4812/*
glepnir05460682025-05-26 18:23:27 +02004813 * Get completion matches from register contents.
4814 * Extracts words from all available registers and adds them to the completion list.
4815 */
4816 static void
4817get_register_completion(void)
4818{
4819 int dir = compl_direction;
4820 yankreg_T *reg = NULL;
4821 void *reg_ptr = NULL;
4822
4823 for (int i = 0; i < NUM_REGISTERS; i++)
4824 {
4825 int regname = 0;
4826
4827 if (i == 0)
4828 regname = '"'; // unnamed register
4829 else if (i < 10)
4830 regname = '0' + i;
4831 else if (i == DELETION_REGISTER)
4832 regname = '-';
4833#ifdef FEAT_CLIPBOARD
4834 else if (i == STAR_REGISTER)
4835 regname = '*';
4836 else if (i == PLUS_REGISTER)
4837 regname = '+';
4838#endif
4839 else
4840 regname = 'a' + i - 10;
4841
4842 // Skip invalid or black hole register
4843 if (!valid_yank_reg(regname, FALSE) || regname == '_')
4844 continue;
4845
4846 reg_ptr = get_register(regname, FALSE);
4847 if (reg_ptr == NULL)
4848 continue;
4849
4850 reg = (yankreg_T *)reg_ptr;
4851
4852 for (int j = 0; j < reg->y_size; j++)
4853 {
4854 char_u *str = reg->y_array[j].string;
4855 if (str == NULL)
4856 continue;
4857
4858 char_u *p = str;
4859 while (*p != NUL)
4860 {
4861 p = find_word_start(p);
4862 if (*p == NUL)
4863 break;
4864
4865 char_u *word_end = find_word_end(p);
4866
4867 // Add the word to the completion list
4868 int len = (int)(word_end - p);
4869 if (len > 0 && (!compl_orig_text.string
4870 || (p_ic ? STRNICMP(p, compl_orig_text.string,
4871 compl_orig_text.length) == 0
4872 : STRNCMP(p, compl_orig_text.string,
4873 compl_orig_text.length) == 0)))
4874 {
4875 if (ins_compl_add_infercase(p, len, p_ic, NULL,
4876 dir, FALSE, 0) == OK)
4877 dir = FORWARD;
4878 }
4879
4880 p = word_end;
4881 }
4882 }
4883
4884 // Free the register copy
4885 put_register(regname, reg_ptr);
4886 }
4887}
4888
4889/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004890 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004891 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004892 */
4893 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004894get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004895{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004896 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004897
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004898 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004899 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004900 case -1:
4901 break;
4902#ifdef FEAT_FIND_ID
4903 case CTRL_X_PATH_PATTERNS:
4904 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004905 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004906 break;
4907#endif
4908
4909 case CTRL_X_DICTIONARY:
4910 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004911 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4912 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004913 break;
4914
4915 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004916 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004917 break;
4918
4919 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004920 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004921 break;
4922
4923 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004924 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004925 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004926 break;
4927
4928#ifdef FEAT_COMPL_FUNC
4929 case CTRL_X_FUNCTION:
Girish Palyacbe53192025-04-14 22:13:15 +02004930 if (ctrl_x_mode_normal()) // Invoked by a func in 'cpt' option
4931 expand_cpt_function(st->func_cb);
4932 else
4933 expand_by_function(type, compl_pattern.string, NULL);
4934 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004935 case CTRL_X_OMNI:
Girish Palyacbe53192025-04-14 22:13:15 +02004936 expand_by_function(type, compl_pattern.string, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004937 break;
4938#endif
4939
4940 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004941 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004942 break;
4943
glepnir05460682025-05-26 18:23:27 +02004944 case CTRL_X_REGISTER:
4945 get_register_completion();
4946 break;
4947
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004948 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004949 found_new_match = get_next_default_completion(st, ini);
4950 if (found_new_match == FAIL && st->ins_buf == curbuf)
4951 st->found_all = TRUE;
4952 }
4953
4954 // check if compl_curr_match has changed, (e.g. other type of
4955 // expansion added something)
4956 if (type != 0 && compl_curr_match != compl_old_match)
4957 found_new_match = OK;
4958
4959 return found_new_match;
4960}
4961
4962/*
Girish Palya0ac1eb32025-04-16 20:18:33 +02004963 * Strips carets followed by numbers. This suffix typically represents the
4964 * max_matches setting.
4965 */
4966 static void
4967strip_caret_numbers_in_place(char_u *str)
4968{
4969 char_u *read = str, *write = str, *p;
4970
4971 if (str == NULL)
4972 return;
4973
4974 while (*read)
4975 {
4976 if (*read == '^')
4977 {
4978 p = read + 1;
4979 while (vim_isdigit(*p))
4980 p++;
4981 if ((*p == ',' || *p == '\0') && p != read + 1)
4982 {
4983 read = p;
4984 continue;
4985 }
4986 else
4987 *write++ = *read++;
4988 }
4989 else
4990 *write++ = *read++;
4991 }
4992 *write = '\0';
4993}
4994
4995/*
Girish Palya7c621052025-05-26 19:41:59 +02004996 * Safely advance the cpt_sources_index by one.
4997 */
4998 static int
4999advance_cpt_sources_index_safe(void)
5000{
5001 if (cpt_sources_index < cpt_sources_count - 1)
5002 {
5003 cpt_sources_index++;
5004 return OK;
5005 }
5006#ifdef FEAT_EVAL
5007 semsg(_(e_list_index_out_of_range_nr), cpt_sources_index + 1);
5008#endif
5009 return FAIL;
5010}
5011
5012/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005013 * Get the next expansion(s), using "compl_pattern".
5014 * The search starts at position "ini" in curbuf and in the direction
5015 * compl_direction.
5016 * When "compl_started" is FALSE start at that position, otherwise continue
5017 * where we stopped searching before.
5018 * This may return before finding all the matches.
5019 * Return the total number of matches or -1 if still unknown -- Acevedo
5020 */
5021 static int
5022ins_compl_get_exp(pos_T *ini)
5023{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005024 static ins_compl_next_state_T st;
5025 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005026 int i;
5027 int found_new_match;
5028 int type = ctrl_x_mode;
Girish Palya7c621052025-05-26 19:41:59 +02005029 int may_advance_cpt_idx = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005030
5031 if (!compl_started)
5032 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005033 buf_T *buf;
5034
5035 FOR_ALL_BUFFERS(buf)
5036 buf->b_scanned = 0;
5037 if (!st_cleared)
5038 {
5039 CLEAR_FIELD(st);
5040 st_cleared = TRUE;
5041 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005042 st.found_all = FALSE;
5043 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005044 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02005045 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005046 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
5047 ? (char_u *)"." : curbuf->b_p_cpt);
Girish Palya0ac1eb32025-04-16 20:18:33 +02005048 strip_caret_numbers_in_place(st.e_cpt_copy);
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005049 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005050 st.last_match_pos = st.first_match_pos = *ini;
Girish Palyacbe53192025-04-14 22:13:15 +02005051
5052 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Girish Palya0ac1eb32025-04-16 20:18:33 +02005053 && !cpt_sources_init())
Girish Palyacbe53192025-04-14 22:13:15 +02005054 return FAIL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005055 }
5056 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
5057 st.ins_buf = curbuf; // In case the buffer was wiped out.
5058
5059 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02005060 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02005061 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005062
5063 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
Girish Palya7c621052025-05-26 19:41:59 +02005064 cpt_sources_index = 0;
5065 for (;;)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005066 {
5067 found_new_match = FAIL;
5068 st.set_match_pos = FALSE;
5069
5070 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
5071 // or if found_all says this entry is done. For ^X^L only use the
5072 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005073 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005074 && (!compl_started || st.found_all))
5075 {
Girish Palya7c621052025-05-26 19:41:59 +02005076 int status = process_next_cpt_value(&st, &type, ini,
5077 cfc_has_mode(), &may_advance_cpt_idx);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005078
5079 if (status == INS_COMPL_CPT_END)
5080 break;
5081 if (status == INS_COMPL_CPT_CONT)
Girish Palyacbe53192025-04-14 22:13:15 +02005082 {
Girish Palya7c621052025-05-26 19:41:59 +02005083 if (may_advance_cpt_idx && !advance_cpt_sources_index_safe())
5084 break;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005085 continue;
Girish Palyacbe53192025-04-14 22:13:15 +02005086 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005087 }
5088
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005089 // If complete() was called then compl_pattern has been reset. The
5090 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01005091 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005092 break;
5093
5094 // get the next set of completion matches
5095 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005096
Girish Palya7c621052025-05-26 19:41:59 +02005097 if (may_advance_cpt_idx && !advance_cpt_sources_index_safe())
5098 break;
Girish Palyacbe53192025-04-14 22:13:15 +02005099
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005100 // break the loop for specialized modes (use 'complete' just for the
5101 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
5102 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005103 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
5104 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005105 {
5106 if (got_int)
5107 break;
5108 // Fill the popup menu as soon as possible.
5109 if (type != -1)
5110 ins_compl_check_keys(0, FALSE);
5111
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005112 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005113 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
5114 break;
5115 compl_started = TRUE;
5116 }
5117 else
5118 {
5119 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02005120 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005121 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005122
5123 compl_started = FALSE;
5124 }
5125 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02005126 cpt_sources_index = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005127 compl_started = TRUE;
5128
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005129 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00005130 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005131 found_new_match = FAIL;
5132
5133 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005134 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005135 && !ctrl_x_mode_line_or_eval()))
5136 i = ins_compl_make_cyclic();
5137
glepnirf31cfa22025-03-06 21:59:13 +01005138 if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0)
5139 fuzzy_longest_match();
5140
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005141 if (compl_old_match != NULL)
5142 {
5143 // If several matches were added (FORWARD) or the search failed and has
5144 // just been made cyclic then we have to move compl_curr_match to the
5145 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005146 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005147 : compl_old_match->cp_prev;
5148 if (compl_curr_match == NULL)
5149 compl_curr_match = compl_old_match;
5150 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01005151 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01005152
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005153 return i;
5154}
5155
5156/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005157 * Update "compl_shown_match" to the actually shown match, it may differ when
5158 * "compl_leader" is used to omit some of the matches.
5159 */
5160 static void
5161ins_compl_update_shown_match(void)
5162{
5163 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01005164 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005165 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005166 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005167 compl_shown_match = compl_shown_match->cp_next;
5168
5169 // If we didn't find it searching forward, and compl_shows_dir is
5170 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005171 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005172 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01005173 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005174 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005175 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005176 {
5177 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01005178 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005179 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005180 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005181 compl_shown_match = compl_shown_match->cp_prev;
5182 }
5183}
5184
5185/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005186 * Delete the old text being completed.
5187 */
5188 void
5189ins_compl_delete(void)
5190{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005191 // In insert mode: Delete the typed part.
5192 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01005193 int col = compl_col + (compl_status_adding() ? compl_length : 0);
John Marriottf4b36412025-02-23 09:09:59 +01005194 string_T remaining = {NULL, 0};
glepnir76bdb822025-02-08 19:04:51 +01005195 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01005196 int has_preinsert = ins_compl_preinsert_effect();
5197 if (has_preinsert)
5198 {
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +02005199 col += (int)ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01005200 curwin->w_cursor.col = compl_ins_end_col;
5201 }
5202
glepnir76bdb822025-02-08 19:04:51 +01005203 if (curwin->w_cursor.lnum > compl_lnum)
5204 {
5205 if (curwin->w_cursor.col < ml_get_curline_len())
5206 {
John Marriottf4b36412025-02-23 09:09:59 +01005207 char_u *line = ml_get_cursor();
5208 remaining.length = ml_get_cursor_len();
5209 remaining.string = vim_strnsave(line, remaining.length);
5210 if (remaining.string == NULL)
glepnir76bdb822025-02-08 19:04:51 +01005211 return;
5212 }
5213 while (curwin->w_cursor.lnum > compl_lnum)
5214 {
5215 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
5216 {
John Marriottf4b36412025-02-23 09:09:59 +01005217 if (remaining.string)
5218 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01005219 return;
5220 }
zeertzjq060e6552025-02-21 20:06:26 +01005221 deleted_lines_mark(curwin->w_cursor.lnum, 1L);
glepnir76bdb822025-02-08 19:04:51 +01005222 curwin->w_cursor.lnum--;
5223 }
5224 // move cursor to end of line
5225 curwin->w_cursor.col = ml_get_curline_len();
5226 }
5227
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005228 if ((int)curwin->w_cursor.col > col)
5229 {
5230 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01005231 {
John Marriottf4b36412025-02-23 09:09:59 +01005232 if (remaining.string)
5233 vim_free(remaining.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005234 return;
glepnire3647c82025-02-10 21:16:32 +01005235 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005236 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01005237 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005238 }
5239
John Marriottf4b36412025-02-23 09:09:59 +01005240 if (remaining.string != NULL)
glepnir76bdb822025-02-08 19:04:51 +01005241 {
5242 orig_col = curwin->w_cursor.col;
John Marriottf4b36412025-02-23 09:09:59 +01005243 ins_str(remaining.string, remaining.length);
glepnir76bdb822025-02-08 19:04:51 +01005244 curwin->w_cursor.col = orig_col;
John Marriottf4b36412025-02-23 09:09:59 +01005245 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01005246 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 // TODO: is this sufficient for redrawing? Redrawing everything causes
5248 // flicker, thus we can't do that.
5249 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02005250#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005251 // clear v:completed_item
5252 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02005253#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005254}
5255
5256/*
glepnir76bdb822025-02-08 19:04:51 +01005257 * Insert a completion string that contains newlines.
5258 * The string is split and inserted line by line.
5259 */
5260 static void
5261ins_compl_expand_multiple(char_u *str)
5262{
5263 char_u *start = str;
5264 char_u *curr = str;
glepnir5090a1f2025-02-24 19:10:37 +01005265 int base_indent = get_indent();
glepnir76bdb822025-02-08 19:04:51 +01005266
5267 while (*curr != NUL)
5268 {
5269 if (*curr == '\n')
5270 {
5271 // Insert the text chunk before newline
5272 if (curr > start)
5273 ins_char_bytes(start, (int)(curr - start));
5274
5275 // Handle newline
glepnir5090a1f2025-02-24 19:10:37 +01005276 open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL);
glepnir76bdb822025-02-08 19:04:51 +01005277 start = curr + 1;
5278 }
5279 curr++;
5280 }
5281
5282 // Handle remaining text after last newline (if any)
5283 if (curr > start)
5284 ins_char_bytes(start, (int)(curr - start));
5285
5286 compl_ins_end_col = curwin->w_cursor.col;
5287}
5288
5289/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005290 * Insert the new text being completed.
5291 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01005292 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
5293 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005294 */
5295 void
glepniredd4ac32025-01-29 18:53:51 +01005296ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005297{
glepnir76bdb822025-02-08 19:04:51 +01005298 int compl_len = get_compl_len();
5299 int preinsert = ins_compl_has_preinsert();
5300 char_u *cp_str = compl_shown_match->cp_str.string;
5301 size_t cp_str_len = compl_shown_match->cp_str.length;
5302 size_t leader_len = ins_compl_leader_len();
5303 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00005304
5305 // Make sure we don't go over the end of the string, this can happen with
5306 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01005307 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01005308 {
glepnir76bdb822025-02-08 19:04:51 +01005309 if (has_multiple)
5310 ins_compl_expand_multiple(cp_str + compl_len);
5311 else
5312 {
5313 ins_compl_insert_bytes(cp_str + compl_len, -1);
5314 if (preinsert && move_cursor)
5315 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
5316 }
glepniredd4ac32025-01-29 18:53:51 +01005317 }
5318 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005319 compl_used_match = FALSE;
5320 else
5321 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02005322#ifdef FEAT_EVAL
5323 {
5324 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
5325
5326 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
5327 }
5328#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005329 if (!in_compl_func)
5330 compl_curr_match = compl_shown_match;
5331}
5332
5333/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005334 * show the file name for the completion match (if any). Truncate the file
5335 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005336 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005337 static void
5338ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005339{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005340 char *lead = _("match in file");
5341 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5342 char_u *s;
5343 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005344
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005345 if (space <= 0)
5346 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005347
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005348 // We need the tail that fits. With double-byte encoding going
5349 // back from the end is very slow, thus go from the start and keep
5350 // the text that fits in "space" between "s" and "e".
5351 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005352 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005353 space -= ptr2cells(e);
5354 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005355 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005356 space += ptr2cells(s);
5357 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005358 }
5359 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005360 msg_hist_off = TRUE;
5361 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5362 s > compl_shown_match->cp_fname ? "<" : "", s);
5363 msg((char *)IObuff);
5364 msg_hist_off = FALSE;
5365 redraw_cmdline = FALSE; // don't overwrite!
5366}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005367
glepnirdca57fb2024-06-04 22:01:21 +02005368/*
zeertzjq551d8c32024-06-05 19:53:32 +02005369 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02005370 */
glepnira218cc62024-06-03 19:32:39 +02005371 static compl_T *
5372find_comp_when_fuzzy(void)
5373{
5374 int score;
5375 char_u* str;
5376 int target_idx = -1;
5377 int is_forward = compl_shows_dir_forward();
5378 int is_backward = compl_shows_dir_backward();
5379 compl_T *comp = NULL;
5380
glepnir43eef882024-06-19 20:20:48 +02005381 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02005382 || (is_backward && compl_selected_item == 0))
glepnir5a18ccf2025-05-11 13:48:33 +02005383 return match_at_original_text(compl_first_match)
5384 ? compl_first_match : compl_first_match->cp_prev;
glepnira218cc62024-06-03 19:32:39 +02005385
5386 if (is_forward)
5387 target_idx = compl_selected_item + 1;
5388 else if (is_backward)
Hirohito Higashi355db992025-04-28 18:07:02 +02005389 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02005390 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02005391
5392 score = compl_match_array[target_idx].pum_score;
5393 str = compl_match_array[target_idx].pum_text;
5394
5395 comp = compl_first_match;
Hirohito Higashia4a00a72025-05-08 22:58:31 +02005396 do
5397 {
Hirohito Higashi355db992025-04-28 18:07:02 +02005398 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
5399 return comp;
5400 comp = comp->cp_next;
glepnira218cc62024-06-03 19:32:39 +02005401 } while (comp != NULL && !is_first_match(comp));
5402
5403 return NULL;
5404}
5405
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005406/*
Girish Palya0ac1eb32025-04-16 20:18:33 +02005407 * Find the appropriate completion item when 'complete' ('cpt') includes
5408 * a 'max_matches' postfix. In this case, we search for a match where
5409 * 'cp_in_match_array' is set, indicating that the match is also present
5410 * in 'compl_match_array'.
5411 */
5412 static compl_T *
5413find_comp_when_cpt_sources(void)
5414{
5415 int is_forward = compl_shows_dir_forward();
5416 compl_T *match = compl_shown_match;
5417
5418 do
5419 match = is_forward ? match->cp_next : match->cp_prev;
5420 while (match->cp_next && !match->cp_in_match_array
5421 && !match_at_original_text(match));
5422 return match;
5423}
5424
5425/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005426 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005427 * times. The number of matches found is returned in 'num_matches'.
5428 *
5429 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
5430 * get more completions. If it is FALSE, then do nothing when there are no more
5431 * completions in the given direction.
5432 *
5433 * If "advance" is TRUE, then completion will move to the first match.
5434 * Otherwise, the original text will be shown.
5435 *
5436 * Returns OK on success and -1 if the number of matches are unknown.
5437 */
5438 static int
5439find_next_completion_match(
5440 int allow_get_expansion,
5441 int todo, // repeat completion this many times
5442 int advance,
5443 int *num_matches)
5444{
5445 int found_end = FALSE;
5446 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02005447 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02005448 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
5449 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Girish Palya0ac1eb32025-04-16 20:18:33 +02005450 int cpt_sources_active = compl_match_array && cpt_sources_array;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005451
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005452 while (--todo >= 0)
5453 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005454 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005455 {
Girish Palya0ac1eb32025-04-16 20:18:33 +02005456 if (compl_match_array != NULL && compl_fuzzy_match)
5457 compl_shown_match = find_comp_when_fuzzy();
5458 else if (cpt_sources_active)
5459 compl_shown_match = find_comp_when_cpt_sources();
5460 else
5461 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005462 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005463 && (is_first_match(compl_shown_match->cp_next)
5464 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005465 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005466 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005467 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005468 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005469 found_end = is_first_match(compl_shown_match);
Girish Palya0ac1eb32025-04-16 20:18:33 +02005470 if (compl_match_array != NULL && compl_fuzzy_match)
5471 compl_shown_match = find_comp_when_fuzzy();
5472 else if (cpt_sources_active)
5473 compl_shown_match = find_comp_when_cpt_sources();
5474 else
5475 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005476 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005477 }
5478 else
5479 {
5480 if (!allow_get_expansion)
5481 {
5482 if (advance)
5483 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005484 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005485 compl_pending -= todo + 1;
5486 else
5487 compl_pending += todo + 1;
5488 }
5489 return -1;
5490 }
5491
5492 if (!compl_no_select && advance)
5493 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005494 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005495 --compl_pending;
5496 else
5497 ++compl_pending;
5498 }
5499
5500 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005501 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005502
5503 // handle any pending completions
5504 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005505 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005506 {
5507 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5508 {
5509 compl_shown_match = compl_shown_match->cp_next;
5510 --compl_pending;
5511 }
5512 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5513 {
5514 compl_shown_match = compl_shown_match->cp_prev;
5515 ++compl_pending;
5516 }
5517 else
5518 break;
5519 }
5520 found_end = FALSE;
5521 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005522 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01005523 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005524 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01005525 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02005526 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005527 ++todo;
5528 else
5529 // Remember a matching item.
5530 found_compl = compl_shown_match;
5531
5532 // Stop at the end of the list when we found a usable match.
5533 if (found_end)
5534 {
5535 if (found_compl != NULL)
5536 {
5537 compl_shown_match = found_compl;
5538 break;
5539 }
5540 todo = 1; // use first usable match after wrapping around
5541 }
5542 }
5543
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005544 return OK;
5545}
5546
5547/*
5548 * Fill in the next completion in the current direction.
5549 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
5550 * get more completions. If it is FALSE, then we just do nothing when there
5551 * are no more completions in a given direction. The latter case is used when
5552 * we are still in the middle of finding completions, to allow browsing
5553 * through the ones found so far.
5554 * Return the total number of matches, or -1 if still unknown -- webb.
5555 *
5556 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
5557 * compl_shown_match here.
5558 *
5559 * Note that this function may be called recursively once only. First with
5560 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
5561 * calls this function with "allow_get_expansion" FALSE.
5562 */
5563 static int
5564ins_compl_next(
5565 int allow_get_expansion,
5566 int count, // repeat completion this many times; should
5567 // be at least 1
5568 int insert_match, // Insert the newly selected match
5569 int in_compl_func) // called from complete_check()
5570{
5571 int num_matches = -1;
5572 int todo = count;
5573 int advance;
5574 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005575 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02005576 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02005577 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
5578 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01005579 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005580
5581 // When user complete function return -1 for findstart which is next
5582 // time of 'always', compl_shown_match become NULL.
5583 if (compl_shown_match == NULL)
5584 return -1;
5585
John Marriott5e6ea922024-11-23 14:01:57 +01005586 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02005587 && !match_at_original_text(compl_shown_match)
5588 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005589 // Update "compl_shown_match" to the actually shown match
5590 ins_compl_update_shown_match();
5591
5592 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02005593 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005594 // Delete old text to be replaced
5595 ins_compl_delete();
5596
5597 // When finding the longest common text we stick at the original text,
5598 // don't let CTRL-N or CTRL-P move to the first match.
5599 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
5600
5601 // When restarting the search don't insert the first match either.
5602 if (compl_restarting)
5603 {
5604 advance = FALSE;
5605 compl_restarting = FALSE;
5606 }
5607
5608 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
5609 // around.
5610 if (find_next_completion_match(allow_get_expansion, todo, advance,
5611 &num_matches) == -1)
5612 return -1;
5613
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005614 if (curbuf != orig_curbuf)
5615 {
5616 // In case some completion function switched buffer, don't want to
5617 // insert the completion elsewhere.
5618 return -1;
5619 }
5620
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005621 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01005622 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005623 {
glepnir6a38aff2024-12-16 21:56:16 +01005624 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005625 compl_used_match = FALSE;
5626 }
5627 else if (insert_match)
5628 {
5629 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01005630 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005631 else
glepnir6a38aff2024-12-16 21:56:16 +01005632 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005633 }
5634 else
5635 compl_used_match = FALSE;
5636
5637 if (!allow_get_expansion)
5638 {
5639 // may undisplay the popup menu first
5640 ins_compl_upd_pum();
5641
5642 if (pum_enough_matches())
5643 // Will display the popup menu, don't redraw yet to avoid flicker.
5644 pum_call_update_screen();
5645 else
5646 // Not showing the popup menu yet, redraw to show the user what was
5647 // inserted.
5648 update_screen(0);
5649
5650 // display the updated popup menu
5651 ins_compl_show_pum();
5652#ifdef FEAT_GUI
5653 if (gui.in_use)
5654 {
5655 // Show the cursor after the match, not after the redrawn text.
5656 setcursor();
5657 out_flush_cursor(FALSE, FALSE);
5658 }
5659#endif
5660
5661 // Delete old text to be replaced, since we're still searching and
5662 // don't want to match ourselves!
5663 ins_compl_delete();
5664 }
5665
5666 // Enter will select a match when the match wasn't inserted and the popup
5667 // menu is visible.
5668 if (compl_no_insert && !started)
5669 compl_enter_selects = TRUE;
5670 else
5671 compl_enter_selects = !insert_match && compl_match_array != NULL;
5672
5673 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005674 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005675 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005676
5677 return num_matches;
5678}
5679
5680/*
5681 * Call this while finding completions, to check whether the user has hit a key
5682 * that should change the currently displayed completion, or exit completion
5683 * mode. Also, when compl_pending is not zero, show a completion as soon as
5684 * possible. -- webb
5685 * "frequency" specifies out of how many calls we actually check.
5686 * "in_compl_func" is TRUE when called from complete_check(), don't set
5687 * compl_curr_match.
5688 */
5689 void
5690ins_compl_check_keys(int frequency, int in_compl_func)
5691{
5692 static int count = 0;
5693 int c;
5694
5695 // Don't check when reading keys from a script, :normal or feedkeys().
5696 // That would break the test scripts. But do check for keys when called
5697 // from complete_check().
5698 if (!in_compl_func && (using_script() || ex_normal_busy))
5699 return;
5700
5701 // Only do this at regular intervals
5702 if (++count < frequency)
5703 return;
5704 count = 0;
5705
5706 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5707 // can't do its work correctly.
5708 c = vpeekc_any();
5709 if (c != NUL)
5710 {
5711 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5712 {
5713 c = safe_vgetc(); // Eat the character
5714 compl_shows_dir = ins_compl_key2dir(c);
5715 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
5716 c != K_UP && c != K_DOWN, in_compl_func);
5717 }
5718 else
5719 {
5720 // Need to get the character to have KeyTyped set. We'll put it
5721 // back with vungetc() below. But skip K_IGNORE.
5722 c = safe_vgetc();
5723 if (c != K_IGNORE)
5724 {
5725 // Don't interrupt completion when the character wasn't typed,
5726 // e.g., when doing @q to replay keys.
5727 if (c != Ctrl_R && KeyTyped)
5728 compl_interrupted = TRUE;
5729
5730 vungetc(c);
5731 }
5732 }
5733 }
zeertzjq529b9ad2024-06-05 20:27:06 +02005734 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005735 {
5736 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5737
5738 compl_pending = 0;
5739 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
5740 }
5741}
5742
5743/*
5744 * Decide the direction of Insert mode complete from the key typed.
5745 * Returns BACKWARD or FORWARD.
5746 */
5747 static int
5748ins_compl_key2dir(int c)
5749{
5750 if (c == Ctrl_P || c == Ctrl_L
5751 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
5752 return BACKWARD;
5753 return FORWARD;
5754}
5755
5756/*
5757 * Return TRUE for keys that are used for completion only when the popup menu
5758 * is visible.
5759 */
5760 static int
5761ins_compl_pum_key(int c)
5762{
5763 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
5764 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5765 || c == K_UP || c == K_DOWN);
5766}
5767
5768/*
5769 * Decide the number of completions to move forward.
5770 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5771 */
5772 static int
5773ins_compl_key2count(int c)
5774{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005775 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
5776 {
glepnir19e1dd62025-05-08 22:50:38 +02005777 int h = pum_get_height();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005778 if (h > 3)
5779 h -= 2; // keep some context
5780 return h;
5781 }
5782 return 1;
5783}
5784
5785/*
5786 * Return TRUE if completion with "c" should insert the match, FALSE if only
5787 * to change the currently selected completion.
5788 */
5789 static int
5790ins_compl_use_match(int c)
5791{
5792 switch (c)
5793 {
5794 case K_UP:
5795 case K_DOWN:
5796 case K_PAGEDOWN:
5797 case K_KPAGEDOWN:
5798 case K_S_DOWN:
5799 case K_PAGEUP:
5800 case K_KPAGEUP:
5801 case K_S_UP:
5802 return FALSE;
5803 }
5804 return TRUE;
5805}
5806
5807/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005808 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
5809 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01005810 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005811 * Uses the global variables: compl_cont_status and ctrl_x_mode
5812 */
5813 static int
5814get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
5815{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005816 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005817 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005818 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005819 {
5820 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5821 ;
5822 compl_col += ++startcol;
5823 compl_length = curs_col - startcol;
5824 }
5825 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005826 {
John Marriott5e6ea922024-11-23 14:01:57 +01005827 compl_pattern.string = str_foldcase(line + compl_col,
5828 compl_length, NULL, 0);
5829 if (compl_pattern.string == NULL)
5830 {
5831 compl_pattern.length = 0;
5832 return FAIL;
5833 }
5834 compl_pattern.length = STRLEN(compl_pattern.string);
5835 }
5836 else
5837 {
5838 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5839 if (compl_pattern.string == NULL)
5840 {
5841 compl_pattern.length = 0;
5842 return FAIL;
5843 }
5844 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005845 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005846 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005847 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005848 {
5849 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005850 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005851 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005852
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005853 if (!vim_iswordp(line + compl_col)
5854 || (compl_col > 0
5855 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005856 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005857 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005858 prefixlen = 0;
5859 }
John Marriott5e6ea922024-11-23 14:01:57 +01005860
5861 // we need up to 2 extra chars for the prefix
5862 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5863 compl_pattern.string = alloc(n);
5864 if (compl_pattern.string == NULL)
5865 {
5866 compl_pattern.length = 0;
5867 return FAIL;
5868 }
5869 STRCPY((char *)compl_pattern.string, prefix);
5870 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005871 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005872 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005873 }
5874 else if (--startcol < 0
5875 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5876 {
John Marriott5e6ea922024-11-23 14:01:57 +01005877 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5878
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005879 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005880 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5881 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005882 {
John Marriott5e6ea922024-11-23 14:01:57 +01005883 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005884 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005885 }
John Marriott5e6ea922024-11-23 14:01:57 +01005886 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005887 compl_col += curs_col;
5888 compl_length = 0;
5889 }
5890 else
5891 {
5892 // Search the point of change class of multibyte character
5893 // or not a word single byte character backward.
5894 if (has_mbyte)
5895 {
5896 int base_class;
5897 int head_off;
5898
5899 startcol -= (*mb_head_off)(line, line + startcol);
5900 base_class = mb_get_class(line + startcol);
5901 while (--startcol >= 0)
5902 {
5903 head_off = (*mb_head_off)(line, line + startcol);
5904 if (base_class != mb_get_class(line + startcol
5905 - head_off))
5906 break;
5907 startcol -= head_off;
5908 }
5909 }
5910 else
glepnir19e1dd62025-05-08 22:50:38 +02005911 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005912 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5913 ;
glepnir19e1dd62025-05-08 22:50:38 +02005914 }
5915
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005916 compl_col += ++startcol;
5917 compl_length = (int)curs_col - startcol;
5918 if (compl_length == 1)
5919 {
5920 // Only match word with at least two chars -- webb
5921 // there's no need to call quote_meta,
5922 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005923 compl_pattern.string = alloc(7);
5924 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005925 {
John Marriott5e6ea922024-11-23 14:01:57 +01005926 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005927 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005928 }
John Marriott5e6ea922024-11-23 14:01:57 +01005929 STRCPY((char *)compl_pattern.string, "\\<");
5930 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5931 STRCAT((char *)compl_pattern.string, "\\k");
5932 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005933 }
5934 else
5935 {
John Marriott5e6ea922024-11-23 14:01:57 +01005936 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5937
5938 compl_pattern.string = alloc(n);
5939 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005940 {
John Marriott5e6ea922024-11-23 14:01:57 +01005941 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005942 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005943 }
John Marriott5e6ea922024-11-23 14:01:57 +01005944 STRCPY((char *)compl_pattern.string, "\\<");
5945 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005946 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005947 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005948 }
5949 }
5950
5951 return OK;
5952}
5953
5954/*
5955 * Get the pattern, column and length for whole line completion or for the
5956 * complete() function.
5957 * Sets the global variables: compl_col, compl_length and compl_pattern.
5958 */
5959 static int
5960get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5961{
5962 compl_col = (colnr_T)getwhitecols(line);
5963 compl_length = (int)curs_col - (int)compl_col;
5964 if (compl_length < 0) // cursor in indent: empty pattern
5965 compl_length = 0;
5966 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005967 {
John Marriott5e6ea922024-11-23 14:01:57 +01005968 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5969 NULL, 0);
5970 if (compl_pattern.string == NULL)
5971 {
5972 compl_pattern.length = 0;
5973 return FAIL;
5974 }
5975 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005976 }
John Marriott5e6ea922024-11-23 14:01:57 +01005977 else
5978 {
5979 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5980 if (compl_pattern.string == NULL)
5981 {
5982 compl_pattern.length = 0;
5983 return FAIL;
5984 }
5985 compl_pattern.length = (size_t)compl_length;
5986 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005987
5988 return OK;
5989}
5990
5991/*
5992 * Get the pattern, column and length for filename completion.
5993 * Sets the global variables: compl_col, compl_length and compl_pattern.
5994 */
5995 static int
5996get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5997{
5998 // Go back to just before the first filename character.
5999 if (startcol > 0)
6000 {
6001 char_u *p = line + startcol;
6002
6003 MB_PTR_BACK(line, p);
6004 while (p > line && vim_isfilec(PTR2CHAR(p)))
6005 MB_PTR_BACK(line, p);
6006 if (p == line && vim_isfilec(PTR2CHAR(p)))
6007 startcol = 0;
6008 else
6009 startcol = (int)(p - line) + 1;
6010 }
6011
6012 compl_col += startcol;
6013 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01006014 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
6015 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02006016 {
John Marriott5e6ea922024-11-23 14:01:57 +01006017 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006018 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02006019 }
6020
John Marriott5e6ea922024-11-23 14:01:57 +01006021 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006022
6023 return OK;
6024}
6025
6026/*
6027 * Get the pattern, column and length for command-line completion.
6028 * Sets the global variables: compl_col, compl_length and compl_pattern.
6029 */
6030 static int
6031get_cmdline_compl_info(char_u *line, colnr_T curs_col)
6032{
John Marriott5e6ea922024-11-23 14:01:57 +01006033 compl_pattern.string = vim_strnsave(line, curs_col);
6034 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02006035 {
John Marriott5e6ea922024-11-23 14:01:57 +01006036 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006037 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02006038 }
John Marriott5e6ea922024-11-23 14:01:57 +01006039 compl_pattern.length = curs_col;
6040 set_cmd_context(&compl_xp, compl_pattern.string,
6041 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006042 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
6043 || compl_xp.xp_context == EXPAND_NOTHING)
6044 // No completion possible, use an empty pattern to get a
6045 // "pattern not found" message.
6046 compl_col = curs_col;
6047 else
John Marriott5e6ea922024-11-23 14:01:57 +01006048 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006049 compl_length = curs_col - compl_col;
6050
6051 return OK;
6052}
6053
6054/*
6055 * Get the pattern, column and length for user defined completion ('omnifunc',
6056 * 'completefunc' and 'thesaurusfunc')
6057 * Sets the global variables: compl_col, compl_length and compl_pattern.
6058 * Uses the global variable: spell_bad_len
Girish Palyacbe53192025-04-14 22:13:15 +02006059 * Callback function "cb" is set if triggered by a function in the 'cpt'
6060 * option; otherwise, it is NULL.
6061 * "startcol", when not NULL, contains the column returned by function.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006062 */
6063 static int
glepnir19e1dd62025-05-08 22:50:38 +02006064get_userdefined_compl_info(
6065 colnr_T curs_col UNUSED,
6066 callback_T *cb UNUSED,
6067 int *startcol UNUSED)
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006068{
6069 int ret = FAIL;
6070
6071#ifdef FEAT_COMPL_FUNC
6072 // Call user defined function 'completefunc' with "a:findstart"
6073 // set to 1 to obtain the length of text to use for completion.
glepnir19e1dd62025-05-08 22:50:38 +02006074 char_u *line = NULL;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006075 typval_T args[3];
6076 int col;
glepnir19e1dd62025-05-08 22:50:38 +02006077 char_u *funcname = NULL;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006078 pos_T pos;
6079 int save_State = State;
Girish Palyacbe53192025-04-14 22:13:15 +02006080 int len;
glepnir19e1dd62025-05-08 22:50:38 +02006081 string_T *compl_pat = NULL;
Girish Palyacbe53192025-04-14 22:13:15 +02006082 int is_cpt_function = (cb != NULL);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006083
Girish Palyacbe53192025-04-14 22:13:15 +02006084 if (!is_cpt_function)
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006085 {
Girish Palyacbe53192025-04-14 22:13:15 +02006086 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
6087 // length as a string
6088 funcname = get_complete_funcname(ctrl_x_mode);
6089 if (*funcname == NUL)
6090 {
6091 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
6092 ? "completefunc" : "omnifunc");
6093 return FAIL;
6094 }
6095 cb = get_insert_callback(ctrl_x_mode);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006096 }
6097
6098 args[0].v_type = VAR_NUMBER;
6099 args[0].vval.v_number = 1;
6100 args[1].v_type = VAR_STRING;
6101 args[1].vval.v_string = (char_u *)"";
6102 args[2].v_type = VAR_UNKNOWN;
6103 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01006104 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006105 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01006106 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006107
6108 State = save_State;
6109 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02006110 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006111 validate_cursor();
6112 if (!EQUAL_POS(curwin->w_cursor, pos))
6113 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00006114 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006115 return FAIL;
6116 }
6117
Girish Palyacbe53192025-04-14 22:13:15 +02006118 if (startcol != NULL)
6119 *startcol = col;
6120
LemonBoy9bcb9ca2022-05-26 15:23:26 +01006121 // Return value -2 means the user complete function wants to cancel the
6122 // complete without an error, do the same if the function did not execute
6123 // successfully.
6124 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006125 return FAIL;
glepnir19e1dd62025-05-08 22:50:38 +02006126
LemonBoy9bcb9ca2022-05-26 15:23:26 +01006127 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006128 if (col == -3)
6129 {
Girish Palyacbe53192025-04-14 22:13:15 +02006130 if (is_cpt_function)
6131 return FAIL;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006132 ctrl_x_mode = CTRL_X_NORMAL;
6133 edit_submode = NULL;
6134 if (!shortmess(SHM_COMPLETIONMENU))
6135 msg_clr_cmdline();
6136 return FAIL;
6137 }
6138
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00006139 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006140 // completion.
6141 compl_opt_refresh_always = FALSE;
6142 compl_opt_suppress_empty = FALSE;
6143
Girish Palyacbe53192025-04-14 22:13:15 +02006144 if (col < 0 || col > curs_col)
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006145 col = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006146
6147 // Setup variables for completion. Need to obtain "line" again,
6148 // it may have become invalid.
6149 line = ml_get(curwin->w_cursor.lnum);
Girish Palyacbe53192025-04-14 22:13:15 +02006150 len = curs_col - col;
6151 compl_pat = is_cpt_function ? &cpt_compl_pattern : &compl_pattern;
6152 compl_pat->string = vim_strnsave(line + col, (size_t)len);
6153 if (compl_pat->string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02006154 {
Girish Palyacbe53192025-04-14 22:13:15 +02006155 compl_pat->length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006156 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02006157 }
Girish Palyacbe53192025-04-14 22:13:15 +02006158 compl_pat->length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006159
Girish Palyacbe53192025-04-14 22:13:15 +02006160 if (!is_cpt_function)
6161 {
6162 compl_col = col;
6163 compl_length = len;
6164 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006165 ret = OK;
6166#endif
6167
6168 return ret;
6169}
6170
6171/*
6172 * Get the pattern, column and length for spell completion.
6173 * Sets the global variables: compl_col, compl_length and compl_pattern.
6174 * Uses the global variable: spell_bad_len
6175 */
6176 static int
6177get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
6178{
6179 int ret = FAIL;
6180#ifdef FEAT_SPELL
glepnir19e1dd62025-05-08 22:50:38 +02006181 char_u *line = NULL;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006182
6183 if (spell_bad_len > 0)
6184 compl_col = curs_col - spell_bad_len;
6185 else
6186 compl_col = spell_word_start(startcol);
glepnir19e1dd62025-05-08 22:50:38 +02006187
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006188 if (compl_col >= (colnr_T)startcol)
6189 {
6190 compl_length = 0;
6191 compl_col = curs_col;
6192 }
6193 else
6194 {
6195 spell_expand_check_cap(compl_col);
6196 compl_length = (int)curs_col - compl_col;
6197 }
6198 // Need to obtain "line" again, it may have become invalid.
6199 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01006200 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
6201 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02006202 {
John Marriott5e6ea922024-11-23 14:01:57 +01006203 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006204 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02006205 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006206
John Marriott5e6ea922024-11-23 14:01:57 +01006207 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006208 ret = OK;
6209#endif
6210
6211 return ret;
6212}
6213
6214/*
6215 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006216 * "startcol" - start column number of the completion pattern/text
6217 * "cur_col" - current cursor column
6218 * On return, "line_invalid" is set to TRUE, if the current line may have
6219 * become invalid and needs to be fetched again.
6220 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006221 */
6222 static int
6223compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
6224{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006225 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006226 || (ctrl_x_mode & CTRL_X_WANT_IDENT
6227 && !thesaurus_func_complete(ctrl_x_mode)))
6228 {
6229 return get_normal_compl_info(line, startcol, curs_col);
6230 }
6231 else if (ctrl_x_mode_line_or_eval())
6232 {
6233 return get_wholeline_compl_info(line, curs_col);
6234 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006235 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006236 {
6237 return get_filename_compl_info(line, startcol, curs_col);
6238 }
6239 else if (ctrl_x_mode == CTRL_X_CMDLINE)
6240 {
6241 return get_cmdline_compl_info(line, curs_col);
6242 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006243 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006244 || thesaurus_func_complete(ctrl_x_mode))
6245 {
Girish Palyacbe53192025-04-14 22:13:15 +02006246 if (get_userdefined_compl_info(curs_col, NULL, NULL) != OK)
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006247 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006248 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006249 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006250 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006251 {
6252 if (get_spell_compl_info(startcol, curs_col) == FAIL)
6253 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006254 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006255 }
glepnir05460682025-05-26 18:23:27 +02006256 else if (ctrl_x_mode_register())
6257 {
6258 return get_normal_compl_info(line, startcol, curs_col);
6259 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00006260 else
6261 {
6262 internal_error("ins_complete()");
6263 return FAIL;
6264 }
6265
6266 return OK;
6267}
6268
6269/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006270 * Continue an interrupted completion mode search in "line".
6271 *
6272 * If this same ctrl_x_mode has been interrupted use the text from
6273 * "compl_startpos" to the cursor as a pattern to add a new word instead of
6274 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
6275 * the same line as the cursor then fix it (the line has been split because it
6276 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
6277 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006278 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006279 static void
6280ins_compl_continue_search(char_u *line)
6281{
6282 // it is a continued search
6283 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006284 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
6285 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006286 {
6287 if (compl_startpos.lnum != curwin->w_cursor.lnum)
6288 {
6289 // line (probably) wrapped, set compl_startpos to the
6290 // first non_blank in the line, if it is not a wordchar
6291 // include it to get a better pattern, but then we don't
6292 // want the "\\<" prefix, check it below
6293 compl_col = (colnr_T)getwhitecols(line);
6294 compl_startpos.col = compl_col;
6295 compl_startpos.lnum = curwin->w_cursor.lnum;
6296 compl_cont_status &= ~CONT_SOL; // clear SOL if present
6297 }
6298 else
6299 {
6300 // S_IPOS was set when we inserted a word that was at the
6301 // beginning of the line, which means that we'll go to SOL
6302 // mode but first we need to redefine compl_startpos
6303 if (compl_cont_status & CONT_S_IPOS)
6304 {
6305 compl_cont_status |= CONT_SOL;
6306 compl_startpos.col = (colnr_T)(skipwhite(
6307 line + compl_length
6308 + compl_startpos.col) - line);
6309 }
6310 compl_col = compl_startpos.col;
6311 }
6312 compl_length = curwin->w_cursor.col - (int)compl_col;
6313 // IObuff is used to add a "word from the next line" would we
6314 // have enough space? just being paranoid
6315#define MIN_SPACE 75
6316 if (compl_length > (IOSIZE - MIN_SPACE))
6317 {
6318 compl_cont_status &= ~CONT_SOL;
6319 compl_length = (IOSIZE - MIN_SPACE);
6320 compl_col = curwin->w_cursor.col - compl_length;
6321 }
6322 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
6323 if (compl_length < 1)
6324 compl_cont_status &= CONT_LOCAL;
6325 }
6326 else if (ctrl_x_mode_line_or_eval())
6327 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
6328 else
6329 compl_cont_status = 0;
6330}
6331
6332/*
6333 * start insert mode completion
6334 */
6335 static int
6336ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006337{
glepnir19e1dd62025-05-08 22:50:38 +02006338 char_u *line = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006339 int startcol = 0; // column where searched text starts
6340 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006341 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006342 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02006343 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006344
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006345 // First time we hit ^N or ^P (in a row, I mean)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006346 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006347 did_si = FALSE;
6348 can_si = FALSE;
6349 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006350 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006351 return FAIL;
6352
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006353 line = ml_get(curwin->w_cursor.lnum);
6354 curs_col = curwin->w_cursor.col;
6355 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01006356 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006357
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006358 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
6359 && compl_cont_mode == ctrl_x_mode)
6360 // this same ctrl-x_mode was interrupted previously. Continue the
6361 // completion.
6362 ins_compl_continue_search(line);
6363 else
6364 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006365
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006366 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006367 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006368 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006369 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006370 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
6371 compl_cont_status = 0;
6372 compl_cont_status |= CONT_N_ADDS;
6373 compl_startpos = curwin->w_cursor;
6374 startcol = (int)curs_col;
6375 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006376 }
6377
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006378 // Work out completion pattern and original text -- webb
6379 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
6380 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006381 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
6382 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006383 // restore did_ai, so that adding comment leader works
6384 did_ai = save_did_ai;
6385 return FAIL;
6386 }
6387 // If "line" was changed while getting completion info get it again.
6388 if (line_invalid)
6389 line = ml_get(curwin->w_cursor.lnum);
6390
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006391 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006392 {
6393 edit_submode_pre = (char_u *)_(" Adding");
6394 if (ctrl_x_mode_line_or_eval())
6395 {
6396 // Insert a new line, keep indentation but ignore 'comments'.
6397 char_u *old = curbuf->b_p_com;
6398
6399 curbuf->b_p_com = (char_u *)"";
6400 compl_startpos.lnum = curwin->w_cursor.lnum;
6401 compl_startpos.col = compl_col;
6402 ins_eol('\r');
6403 curbuf->b_p_com = old;
6404 compl_length = 0;
6405 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01006406 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006407 }
glepnircfe502c2025-04-17 20:17:53 +02006408 else if (ctrl_x_mode_normal() && cfc_has_mode())
glepnir7cfe6932024-09-15 20:06:28 +02006409 {
6410 compl_startpos = curwin->w_cursor;
6411 compl_cont_status &= CONT_S_IPOS;
6412 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006413 }
6414 else
6415 {
6416 edit_submode_pre = NULL;
6417 compl_startpos.col = compl_col;
6418 }
6419
6420 if (compl_cont_status & CONT_LOCAL)
6421 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
6422 else
6423 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
6424
6425 // If any of the original typed text has been changed we need to fix
6426 // the redo buffer.
6427 ins_compl_fixRedoBufForLeader(NULL);
6428
6429 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01006430 VIM_CLEAR_STRING(compl_orig_text);
6431 compl_orig_text.length = (size_t)compl_length;
6432 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006433 if (p_ic)
6434 flags |= CP_ICASE;
zeertzjq4422de62025-03-07 19:06:02 +01006435 if (compl_orig_text.string == NULL
6436 || ins_compl_add(compl_orig_text.string,
6437 (int)compl_orig_text.length,
6438 NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006439 {
John Marriott5e6ea922024-11-23 14:01:57 +01006440 VIM_CLEAR_STRING(compl_pattern);
6441 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006442 return FAIL;
6443 }
6444
6445 // showmode might reset the internal line pointers, so it must
6446 // be called before line = ml_get(), or when this address is no
6447 // longer needed. -- Acevedo.
6448 edit_submode_extra = (char_u *)_("-- Searching...");
6449 edit_submode_highl = HLF_COUNT;
6450 showmode();
6451 edit_submode_extra = NULL;
6452 out_flush();
6453
6454 return OK;
6455}
6456
6457/*
6458 * display the completion status message
6459 */
6460 static void
6461ins_compl_show_statusmsg(void)
6462{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006463 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006464 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006465 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006466 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00006467 ? (char_u *)_("Hit end of paragraph")
6468 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006469 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006470 }
6471
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006472 if (edit_submode_extra == NULL)
6473 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006474 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006475 {
6476 edit_submode_extra = (char_u *)_("Back at original");
6477 edit_submode_highl = HLF_W;
6478 }
6479 else if (compl_cont_status & CONT_S_IPOS)
6480 {
6481 edit_submode_extra = (char_u *)_("Word from other line");
6482 edit_submode_highl = HLF_COUNT;
6483 }
6484 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
6485 {
6486 edit_submode_extra = (char_u *)_("The only match");
6487 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01006488 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006489 }
6490 else
6491 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01006492#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006493 // Update completion sequence number when needed.
6494 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01006495 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01006496#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006497 // The match should always have a sequence number now, this is
6498 // just a safety check.
6499 if (compl_curr_match->cp_number != -1)
6500 {
6501 // Space for 10 text chars. + 2x10-digit no.s = 31.
6502 // Translations may need more than twice that.
6503 static char_u match_ref[81];
6504
6505 if (compl_matches > 0)
6506 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006507 _("match %d of %d"),
6508 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006509 else
6510 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006511 _("match %d"),
6512 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006513 edit_submode_extra = match_ref;
6514 edit_submode_highl = HLF_R;
6515 if (dollar_vcol >= 0)
6516 curs_columns(FALSE);
6517 }
6518 }
6519 }
6520
6521 // Show a message about what (completion) mode we're in.
6522 if (!compl_opt_suppress_empty)
6523 {
6524 showmode();
6525 if (!shortmess(SHM_COMPLETIONMENU))
6526 {
6527 if (edit_submode_extra != NULL)
6528 {
6529 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01006530 {
6531 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006532 msg_attr((char *)edit_submode_extra,
6533 edit_submode_highl < HLF_COUNT
6534 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01006535 msg_hist_off = FALSE;
6536 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006537 }
6538 else
6539 msg_clr_cmdline(); // necessary for "noshowmode"
6540 }
6541 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006542}
6543
6544/*
6545 * Do Insert mode completion.
6546 * Called when character "c" was typed, which has a meaning for completion.
6547 * Returns OK if completion was done, FAIL if something failed (out of mem).
6548 */
6549 int
6550ins_complete(int c, int enable_pum)
6551{
6552 int n;
6553 int save_w_wrow;
6554 int save_w_leftcol;
6555 int insert_match;
6556
6557 compl_direction = ins_compl_key2dir(c);
6558 insert_match = ins_compl_use_match(c);
6559
6560 if (!compl_started)
6561 {
6562 if (ins_compl_start() == FAIL)
6563 return FAIL;
6564 }
6565 else if (insert_match && stop_arrow() == FAIL)
6566 return FAIL;
6567
glepnircf7f0122025-04-15 19:02:00 +02006568 compl_curr_win = curwin;
6569 compl_curr_buf = curwin->w_buffer;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006570 compl_shown_match = compl_curr_match;
6571 compl_shows_dir = compl_direction;
6572
6573 // Find next match (and following matches).
6574 save_w_wrow = curwin->w_wrow;
6575 save_w_leftcol = curwin->w_leftcol;
6576 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
6577
6578 // may undisplay the popup menu
6579 ins_compl_upd_pum();
6580
6581 if (n > 1) // all matches have been found
6582 compl_matches = n;
6583 compl_curr_match = compl_shown_match;
6584 compl_direction = compl_shows_dir;
6585
6586 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
6587 // mode.
6588 if (got_int && !global_busy)
6589 {
6590 (void)vgetc();
6591 got_int = FALSE;
6592 }
6593
6594 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006595 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006596 {
6597 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
6598 // because we couldn't expand anything at first place, but if we used
6599 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
6600 // (such as M in M'exico) if not tried already. -- Acevedo
6601 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006602 || compl_status_adding()
6603 || (ctrl_x_mode_not_default()
6604 && !ctrl_x_mode_path_patterns()
6605 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006606 compl_cont_status &= ~CONT_N_ADDS;
6607 }
6608
6609 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
6610 compl_cont_status |= CONT_S_IPOS;
6611 else
6612 compl_cont_status &= ~CONT_S_IPOS;
6613
6614 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006615
6616 // Show the popup menu, unless we got interrupted.
6617 if (enable_pum && !compl_interrupted)
6618 show_pum(save_w_wrow, save_w_leftcol);
6619
6620 compl_was_interrupted = compl_interrupted;
6621 compl_interrupted = FALSE;
6622
6623 return OK;
6624}
6625
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00006626/*
6627 * Remove (if needed) and show the popup menu
6628 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006629 static void
6630show_pum(int prev_w_wrow, int prev_w_leftcol)
6631{
6632 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006633 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006634 RedrawingDisabled = 0;
6635
6636 // If the cursor moved or the display scrolled we need to remove the pum
6637 // first.
6638 setcursor();
6639 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
6640 ins_compl_del_pum();
6641
6642 ins_compl_show_pum();
6643 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006644
6645 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006646}
6647
6648/*
6649 * Looks in the first "len" chars. of "src" for search-metachars.
6650 * If dest is not NULL the chars. are copied there quoting (with
6651 * a backslash) the metachars, and dest would be NUL terminated.
6652 * Returns the length (needed) of dest
6653 */
6654 static unsigned
6655quote_meta(char_u *dest, char_u *src, int len)
6656{
6657 unsigned m = (unsigned)len + 1; // one extra for the NUL
6658
6659 for ( ; --len >= 0; src++)
6660 {
6661 switch (*src)
6662 {
6663 case '.':
6664 case '*':
6665 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006666 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006667 break;
6668 // FALLTHROUGH
6669 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006670 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006671 break;
6672 // FALLTHROUGH
6673 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006674 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006675 break;
6676 // FALLTHROUGH
6677 case '^': // currently it's not needed.
6678 case '$':
6679 m++;
6680 if (dest != NULL)
6681 *dest++ = '\\';
6682 break;
6683 }
6684 if (dest != NULL)
6685 *dest++ = *src;
6686 // Copy remaining bytes of a multibyte character.
6687 if (has_mbyte)
6688 {
glepnir19e1dd62025-05-08 22:50:38 +02006689 int mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006690 if (mb_len > 0 && len >= mb_len)
glepnir19e1dd62025-05-08 22:50:38 +02006691 for (int i = 0; i < mb_len; ++i)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006692 {
6693 --len;
6694 ++src;
6695 if (dest != NULL)
6696 *dest++ = *src;
6697 }
6698 }
6699 }
6700 if (dest != NULL)
6701 *dest = NUL;
6702
6703 return m;
6704}
6705
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006706#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006707 void
6708free_insexpand_stuff(void)
6709{
John Marriott5e6ea922024-11-23 14:01:57 +01006710 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006711# ifdef FEAT_EVAL
6712 free_callback(&cfu_cb);
6713 free_callback(&ofu_cb);
6714 free_callback(&tsrfu_cb);
6715# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006716}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006717#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006718
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006719#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006720/*
6721 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6722 * spelled word, if there is one.
6723 */
6724 static void
6725spell_back_to_badword(void)
6726{
6727 pos_T tpos = curwin->w_cursor;
6728
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02006729 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006730 if (curwin->w_cursor.col != tpos.col)
6731 start_arrow(&tpos);
6732}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006733#endif
Girish Palyacbe53192025-04-14 22:13:15 +02006734
6735/*
6736 * Reset the info associated with completion sources.
6737 */
6738 static void
Girish Palya0ac1eb32025-04-16 20:18:33 +02006739cpt_sources_clear(void)
Girish Palyacbe53192025-04-14 22:13:15 +02006740{
Girish Palya0ac1eb32025-04-16 20:18:33 +02006741 VIM_CLEAR(cpt_sources_array);
6742 cpt_sources_index = -1;
6743 cpt_sources_count = 0;
Girish Palyacbe53192025-04-14 22:13:15 +02006744}
6745
6746/*
6747 * Initialize the info associated with completion sources.
6748 */
6749 static int
Girish Palya0ac1eb32025-04-16 20:18:33 +02006750cpt_sources_init(void)
Girish Palyacbe53192025-04-14 22:13:15 +02006751{
Girish Palya0ac1eb32025-04-16 20:18:33 +02006752 char_u buf[LSIZE];
6753 int slen;
Girish Palyacbe53192025-04-14 22:13:15 +02006754 int count = 0;
Girish Palya0ac1eb32025-04-16 20:18:33 +02006755 char_u *p;
Girish Palyacbe53192025-04-14 22:13:15 +02006756
Girish Palya0ac1eb32025-04-16 20:18:33 +02006757 for (p = curbuf->b_p_cpt; *p;)
Girish Palyacbe53192025-04-14 22:13:15 +02006758 {
6759 while (*p == ',' || *p == ' ') // Skip delimiters
6760 p++;
6761 if (*p) // If not end of string, count this segment
6762 {
Girish Palya0ac1eb32025-04-16 20:18:33 +02006763 (void)copy_option_part(&p, buf, LSIZE, ","); // Advance p
Girish Palyacbe53192025-04-14 22:13:15 +02006764 count++;
Girish Palyacbe53192025-04-14 22:13:15 +02006765 }
6766 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02006767 cpt_sources_clear();
6768 cpt_sources_count = count;
Girish Palyacbe53192025-04-14 22:13:15 +02006769 if (count > 0)
6770 {
Girish Palya0ac1eb32025-04-16 20:18:33 +02006771 cpt_sources_array = ALLOC_CLEAR_MULT(cpt_source_T, count);
6772 if (cpt_sources_array == NULL)
Girish Palyacbe53192025-04-14 22:13:15 +02006773 {
Girish Palya0ac1eb32025-04-16 20:18:33 +02006774 cpt_sources_count = 0;
Girish Palyacbe53192025-04-14 22:13:15 +02006775 return FAIL;
6776 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02006777 count = 0;
6778 for (p = curbuf->b_p_cpt; *p;)
6779 {
6780 while (*p == ',' || *p == ' ') // Skip delimiters
6781 p++;
6782 if (*p) // If not end of string, count this segment
6783 {
6784 char_u *t;
6785
6786 vim_memset(buf, 0, LSIZE);
6787 slen = copy_option_part(&p, buf, LSIZE, ","); // Advance p
6788 if (slen > 0 && (t = vim_strchr(buf, '^')) != NULL)
6789 cpt_sources_array[count].max_matches = atoi((char *)t + 1);
6790 count++;
6791 }
6792 }
Girish Palyacbe53192025-04-14 22:13:15 +02006793 }
6794 return OK;
6795}
6796
6797/*
6798 * Return TRUE if any of the completion sources have 'refresh' set to 'always'.
6799 */
6800 static int
6801is_cpt_func_refresh_always(void)
6802{
6803#ifdef FEAT_COMPL_FUNC
glepnir19e1dd62025-05-08 22:50:38 +02006804 for (int i = 0; i < cpt_sources_count; i++)
Girish Palya0ac1eb32025-04-16 20:18:33 +02006805 if (cpt_sources_array[i].refresh_always)
Girish Palyacbe53192025-04-14 22:13:15 +02006806 return TRUE;
6807#endif
6808 return FALSE;
6809}
6810
6811/*
6812 * Make the completion list non-cyclic.
6813 */
6814#ifdef FEAT_COMPL_FUNC
6815 static void
6816ins_compl_make_linear(void)
6817{
6818 compl_T *m;
6819
6820 if (compl_first_match == NULL || compl_first_match->cp_prev == NULL)
6821 return;
6822 m = compl_first_match->cp_prev;
6823 m->cp_next = NULL;
6824 compl_first_match->cp_prev = NULL;
6825}
6826#endif
6827
6828/*
6829 * Remove the matches linked to the current completion source (as indicated by
Girish Palya0ac1eb32025-04-16 20:18:33 +02006830 * cpt_sources_index) from the completion list.
Girish Palyacbe53192025-04-14 22:13:15 +02006831 */
6832#ifdef FEAT_COMPL_FUNC
6833 static compl_T *
6834remove_old_matches(void)
6835{
6836 compl_T *sublist_start = NULL, *sublist_end = NULL, *insert_at = NULL;
6837 compl_T *current, *next;
Girish Palya0ac1eb32025-04-16 20:18:33 +02006838 int compl_shown_removed = FALSE;
6839 int forward = (compl_first_match->cp_cpt_source_idx < 0);
6840
6841 compl_direction = forward ? FORWARD : BACKWARD;
6842 compl_shows_dir = compl_direction;
Girish Palyacbe53192025-04-14 22:13:15 +02006843
6844 // Identify the sublist of old matches that needs removal
6845 for (current = compl_first_match; current != NULL; current = current->cp_next)
6846 {
Girish Palya0ac1eb32025-04-16 20:18:33 +02006847 if (current->cp_cpt_source_idx < cpt_sources_index &&
6848 (forward || (!forward && !insert_at)))
Girish Palyacbe53192025-04-14 22:13:15 +02006849 insert_at = current;
6850
Girish Palya0ac1eb32025-04-16 20:18:33 +02006851 if (current->cp_cpt_source_idx == cpt_sources_index)
Girish Palyacbe53192025-04-14 22:13:15 +02006852 {
6853 if (!sublist_start)
6854 sublist_start = current;
6855 sublist_end = current;
6856 if (!compl_shown_removed && compl_shown_match == current)
6857 compl_shown_removed = TRUE;
6858 }
6859
Girish Palya0ac1eb32025-04-16 20:18:33 +02006860 if ((forward && current->cp_cpt_source_idx > cpt_sources_index)
6861 || (!forward && insert_at))
Girish Palyacbe53192025-04-14 22:13:15 +02006862 break;
6863 }
6864
6865 // Re-assign compl_shown_match if necessary
6866 if (compl_shown_removed)
6867 {
6868 if (forward)
6869 compl_shown_match = compl_first_match;
6870 else
6871 { // Last node will have the prefix that is being completed
Girish Palya0ac1eb32025-04-16 20:18:33 +02006872 for (current = compl_first_match; current->cp_next != NULL;
6873 current = current->cp_next)
Girish Palyacbe53192025-04-14 22:13:15 +02006874 ;
6875 compl_shown_match = current;
6876 }
6877 }
6878
6879 if (!sublist_start) // No nodes to remove
6880 return insert_at;
6881
6882 // Update links to remove sublist
6883 if (sublist_start->cp_prev)
6884 sublist_start->cp_prev->cp_next = sublist_end->cp_next;
6885 else
6886 compl_first_match = sublist_end->cp_next;
6887
6888 if (sublist_end->cp_next)
6889 sublist_end->cp_next->cp_prev = sublist_start->cp_prev;
6890
6891 // Free all nodes in the sublist
6892 sublist_end->cp_next = NULL;
6893 for (current = sublist_start; current != NULL; current = next)
6894 {
6895 next = current->cp_next;
6896 ins_compl_item_free(current);
6897 }
6898
6899 return insert_at;
6900}
6901#endif
6902
6903/*
6904 * Retrieve completion matches using the callback function "cb" and store the
6905 * 'refresh:always' flag.
6906 */
6907#ifdef FEAT_COMPL_FUNC
6908 static void
6909get_cpt_func_completion_matches(callback_T *cb UNUSED)
6910{
glepnir19e1dd62025-05-08 22:50:38 +02006911 int ret;
6912 int startcol;
Girish Palyacbe53192025-04-14 22:13:15 +02006913
6914 VIM_CLEAR_STRING(cpt_compl_pattern);
6915 ret = get_userdefined_compl_info(curwin->w_cursor.col, cb, &startcol);
6916 if (ret == FAIL && startcol == -3)
Girish Palya0ac1eb32025-04-16 20:18:33 +02006917 cpt_sources_array[cpt_sources_index].refresh_always = FALSE;
Girish Palyacbe53192025-04-14 22:13:15 +02006918 else if (ret == OK)
6919 {
6920 expand_by_function(0, cpt_compl_pattern.string, cb);
Girish Palya0ac1eb32025-04-16 20:18:33 +02006921 cpt_sources_array[cpt_sources_index].refresh_always =
6922 compl_opt_refresh_always;
Girish Palyacbe53192025-04-14 22:13:15 +02006923 compl_opt_refresh_always = FALSE;
6924 }
6925}
6926#endif
6927
6928/*
6929 * Retrieve completion matches from functions in the 'cpt' option where the
6930 * 'refresh:always' flag is set.
6931 */
6932 static void
6933cpt_compl_refresh(void)
6934{
6935#ifdef FEAT_COMPL_FUNC
6936 char_u *cpt;
6937 char_u *p;
Christian Brabandtd2079cf2025-04-15 18:10:26 +02006938 callback_T *cb = NULL;
Girish Palyacbe53192025-04-14 22:13:15 +02006939
6940 // Make the completion list linear (non-cyclic)
6941 ins_compl_make_linear();
6942 // Make a copy of 'cpt' in case the buffer gets wiped out
6943 cpt = vim_strsave(curbuf->b_p_cpt);
Girish Palya0ac1eb32025-04-16 20:18:33 +02006944 strip_caret_numbers_in_place(cpt);
Girish Palyacbe53192025-04-14 22:13:15 +02006945
Girish Palya0ac1eb32025-04-16 20:18:33 +02006946 cpt_sources_index = 0;
Girish Palya7c621052025-05-26 19:41:59 +02006947 for (p = cpt; *p;)
Girish Palyacbe53192025-04-14 22:13:15 +02006948 {
6949 while (*p == ',' || *p == ' ') // Skip delimiters
6950 p++;
6951
Girish Palya0ac1eb32025-04-16 20:18:33 +02006952 if (cpt_sources_array[cpt_sources_index].refresh_always)
Girish Palyacbe53192025-04-14 22:13:15 +02006953 {
6954 if (*p == 'o')
6955 cb = &curbuf->b_ofu_cb;
Girish Palya14f6da52025-05-26 19:04:25 +02006956 else if (*p == 'F')
Girish Palyacbe53192025-04-14 22:13:15 +02006957 cb = (*(p + 1) != ',' && *(p + 1) != NUL)
6958 ? get_cpt_func_callback(p + 1) : &curbuf->b_cfu_cb;
6959 if (cb)
6960 {
6961 compl_curr_match = remove_old_matches();
6962 get_cpt_func_completion_matches(cb);
6963 }
6964 }
6965
Girish Palya7c621052025-05-26 19:41:59 +02006966 (void)copy_option_part(&p, IObuff, IOSIZE, ","); // Advance p
6967 if (may_advance_cpt_index(p))
6968 (void)advance_cpt_sources_index_safe();
Girish Palyacbe53192025-04-14 22:13:15 +02006969 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02006970 cpt_sources_index = -1;
Girish Palyacbe53192025-04-14 22:13:15 +02006971
6972 vim_free(cpt);
6973 // Make the list cyclic
6974 compl_matches = ins_compl_make_cyclic();
6975#endif
6976}