blob: d8bb0814d3f40a99877e5a59f00c957e0ea1e4b4 [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010091 * Structure used to store one match for insert completion.
92 */
93typedef struct compl_S compl_T;
94struct compl_S
95{
96 compl_T *cp_next;
97 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +010098 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +010099 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200100 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100101#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100102 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100103#endif
glepnir0fe17f82024-10-08 22:26:44 +0200104 char_u *cp_fname; // file containing the match, allocated when
105 // cp_flags has CP_FREE_FNAME
106 int cp_flags; // CP_ values
107 int cp_number; // sequence number
108 int cp_score; // fuzzy match score
glepnird4088ed2024-12-31 10:55:22 +0100109 int cp_in_match_array; // collected by compl_match_array
glepnir7baa0142024-10-09 20:19:25 +0200110 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200111 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100112};
113
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200114// values for cp_flags
115# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
116# define CP_FREE_FNAME 2 // cp_fname is allocated
117# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
118# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
119# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200120# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100122/*
123 * All the current matches are stored in a list.
124 * "compl_first_match" points to the start of the list.
125 * "compl_curr_match" points to the currently selected entry.
126 * "compl_shown_match" is different from compl_curr_match during
127 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000128 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100129 */
130static compl_T *compl_first_match = NULL;
131static compl_T *compl_curr_match = NULL;
132static compl_T *compl_shown_match = NULL;
133static compl_T *compl_old_match = NULL;
134
glepnirf31cfa22025-03-06 21:59:13 +0100135// list used to store the compl_T which have the max score
136// used for completefuzzycollect
137static compl_T **compl_best_matches = NULL;
138static int compl_num_bests = 0;
139// inserted a longest when completefuzzycollect enabled
140static int compl_cfc_longest_ins = FALSE;
141
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100142// After using a cursor key <Enter> selects a match in the popup menu,
143// otherwise it inserts a line break.
144static int compl_enter_selects = FALSE;
145
146// When "compl_leader" is not NULL only matches that start with this string
147// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100148static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100149
150static int compl_get_longest = FALSE; // put longest common string
151 // in compl_leader
152
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100153// Selected one of the matches. When FALSE the match was edited or using the
154// longest common string.
155static int compl_used_match;
156
157// didn't finish finding completions.
158static int compl_was_interrupted = FALSE;
159
160// Set when character typed while looking for matches and it means we should
161// stop looking for matches.
162static int compl_interrupted = FALSE;
163
164static int compl_restarting = FALSE; // don't insert match
165
166// When the first completion is done "compl_started" is set. When it's
167// FALSE the word to be completed must be located.
168static int compl_started = FALSE;
169
170// Which Ctrl-X mode are we in?
171static int ctrl_x_mode = CTRL_X_NORMAL;
172
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000173static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100174static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100175static int compl_direction = FORWARD;
176static int compl_shows_dir = FORWARD;
177static int compl_pending = 0; // > 1 for postponed CTRL-N
178static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000179// Length in bytes of the text being completed (this is deleted to be replaced
180// by the match.)
181static int compl_length = 0;
glepnir76bdb822025-02-08 19:04:51 +0100182static linenr_T compl_lnum = 0; // lnum where the completion start
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100183static colnr_T compl_col = 0; // column where the text starts
184 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100185static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100186static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100187 // completion started
188static int compl_cont_mode = 0;
189static expand_T compl_xp;
190
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000191// List of flags for method of completion.
192static int compl_cont_status = 0;
193# define CONT_ADDING 1 // "normal" or "adding" expansion
194# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
195 // it's set only iff N_ADDS is set
196# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
197# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
198 // if so, word-wise-expansion will set SOL
199# define CONT_SOL 16 // pattern includes start of line, just for
200 // word-wise expansion, not set for ^X^L
201# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
202 // expansion, (eg use complete=.)
203
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100204static int compl_opt_refresh_always = FALSE;
205static int compl_opt_suppress_empty = FALSE;
206
glepnira218cc62024-06-03 19:32:39 +0200207static int compl_selected_item = -1;
208
glepnir8159fb12024-07-17 20:32:54 +0200209static int *compl_fuzzy_scores;
210
glepnir6a38aff2024-12-16 21:56:16 +0100211// "compl_match_array" points the currently displayed list of entries in the
212// popup menu. It is NULL when there is no popup menu.
213static pumitem_T *compl_match_array = NULL;
214static int compl_match_arraysize;
215
glepnirf31cfa22025-03-06 21:59:13 +0100216static 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 +0100217static void ins_compl_longest_match(compl_T *match);
218static void ins_compl_del_pum(void);
219static 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 +0100220static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100221static int ins_compl_need_restart(void);
222static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000223static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100224static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100225static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100226static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
227# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
228static void ins_compl_add_list(list_T *list);
229static void ins_compl_add_dict(dict_T *dict);
230# endif
231static int ins_compl_key2dir(int c);
232static int ins_compl_pum_key(int c);
233static int ins_compl_key2count(int c);
234static void show_pum(int prev_w_wrow, int prev_w_leftcol);
235static unsigned quote_meta(char_u *dest, char_u *str, int len);
glepnir76bdb822025-02-08 19:04:51 +0100236static int ins_compl_has_multiple(void);
237static void ins_compl_expand_multiple(char_u *str);
glepnirf31cfa22025-03-06 21:59:13 +0100238static void ins_compl_longest_insert(char_u *prefix);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100239
240#ifdef FEAT_SPELL
241static void spell_back_to_badword(void);
242static int spell_bad_len = 0; // length of located bad word
243#endif
244
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245/*
246 * CTRL-X pressed in Insert mode.
247 */
248 void
249ins_ctrl_x(void)
250{
zeertzjqdca29d92021-08-31 19:12:51 +0200251 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100252 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000253 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100254 if (compl_cont_status & CONT_N_ADDS)
255 compl_cont_status |= CONT_INTRPT;
256 else
257 compl_cont_status = 0;
258 // We're not sure which CTRL-X mode it will be yet
259 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
260 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
261 edit_submode_pre = NULL;
262 showmode();
263 }
zeertzjqdca29d92021-08-31 19:12:51 +0200264 else
265 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
266 // CTRL-V look like CTRL-N
267 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100268
LemonBoy2bf52dd2022-04-09 18:17:34 +0100269 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100270}
271
272/*
273 * Functions to check the current CTRL-X mode.
274 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000275int ctrl_x_mode_none(void)
276 { return ctrl_x_mode == 0; }
277int ctrl_x_mode_normal(void)
278 { return ctrl_x_mode == CTRL_X_NORMAL; }
279int ctrl_x_mode_scroll(void)
280 { return ctrl_x_mode == CTRL_X_SCROLL; }
281int ctrl_x_mode_whole_line(void)
282 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
283int ctrl_x_mode_files(void)
284 { return ctrl_x_mode == CTRL_X_FILES; }
285int ctrl_x_mode_tags(void)
286 { return ctrl_x_mode == CTRL_X_TAGS; }
287int ctrl_x_mode_path_patterns(void)
288 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
289int ctrl_x_mode_path_defines(void)
290 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
291int ctrl_x_mode_dictionary(void)
292 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
293int ctrl_x_mode_thesaurus(void)
294 { return ctrl_x_mode == CTRL_X_THESAURUS; }
295int ctrl_x_mode_cmdline(void)
296 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200297 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000298int ctrl_x_mode_function(void)
299 { return ctrl_x_mode == CTRL_X_FUNCTION; }
300int ctrl_x_mode_omni(void)
301 { return ctrl_x_mode == CTRL_X_OMNI; }
302int ctrl_x_mode_spell(void)
303 { return ctrl_x_mode == CTRL_X_SPELL; }
304static int ctrl_x_mode_eval(void)
305 { return ctrl_x_mode == CTRL_X_EVAL; }
306int ctrl_x_mode_line_or_eval(void)
307 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100308
309/*
310 * Whether other than default completion has been selected.
311 */
312 int
313ctrl_x_mode_not_default(void)
314{
315 return ctrl_x_mode != CTRL_X_NORMAL;
316}
317
318/*
zeertzjqdca29d92021-08-31 19:12:51 +0200319 * Whether CTRL-X was typed without a following character,
320 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100321 */
322 int
323ctrl_x_mode_not_defined_yet(void)
324{
325 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
326}
327
328/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000329 * Return TRUE if currently in "normal" or "adding" insert completion matches
330 * state
331 */
332 int
333compl_status_adding(void)
334{
335 return compl_cont_status & CONT_ADDING;
336}
337
338/*
339 * Return TRUE if the completion pattern includes start of line, just for
340 * word-wise expansion.
341 */
342 int
343compl_status_sol(void)
344{
345 return compl_cont_status & CONT_SOL;
346}
347
348/*
349 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
350 */
351 int
352compl_status_local(void)
353{
354 return compl_cont_status & CONT_LOCAL;
355}
356
357/*
358 * Clear the completion status flags
359 */
360 void
361compl_status_clear(void)
362{
363 compl_cont_status = 0;
364}
365
366/*
367 * Return TRUE if completion is using the forward direction matches
368 */
369 static int
370compl_dir_forward(void)
371{
372 return compl_direction == FORWARD;
373}
374
375/*
376 * Return TRUE if currently showing forward completion matches
377 */
378 static int
379compl_shows_dir_forward(void)
380{
381 return compl_shows_dir == FORWARD;
382}
383
384/*
385 * Return TRUE if currently showing backward completion matches
386 */
387 static int
388compl_shows_dir_backward(void)
389{
390 return compl_shows_dir == BACKWARD;
391}
392
393/*
394 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 */
396 int
397has_compl_option(int dict_opt)
398{
399 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200400#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100401 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200402#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100404 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
405#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100406 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100407#endif
408 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100409 {
410 ctrl_x_mode = CTRL_X_NORMAL;
411 edit_submode = NULL;
412 msg_attr(dict_opt ? _("'dictionary' option is empty")
413 : _("'thesaurus' option is empty"),
414 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100415 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100416 {
417 vim_beep(BO_COMPL);
418 setcursor();
419 out_flush();
420#ifdef FEAT_EVAL
421 if (!get_vim_var_nr(VV_TESTING))
422#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100423 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100424 }
425 return FALSE;
426 }
427 return TRUE;
428}
429
430/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000431 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100432 * This depends on the current mode.
433 */
434 int
435vim_is_ctrl_x_key(int c)
436{
437 // Always allow ^R - let its results then be checked
438 if (c == Ctrl_R)
439 return TRUE;
440
441 // Accept <PageUp> and <PageDown> if the popup menu is visible.
442 if (ins_compl_pum_key(c))
443 return TRUE;
444
445 switch (ctrl_x_mode)
446 {
447 case 0: // Not in any CTRL-X mode
448 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
449 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200450 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100451 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
452 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
453 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
454 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
455 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200456 || c == Ctrl_S || c == Ctrl_K || c == 's'
457 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100458 case CTRL_X_SCROLL:
459 return (c == Ctrl_Y || c == Ctrl_E);
460 case CTRL_X_WHOLE_LINE:
461 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
462 case CTRL_X_FILES:
463 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_DICTIONARY:
465 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_THESAURUS:
467 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
468 case CTRL_X_TAGS:
469 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
470#ifdef FEAT_FIND_ID
471 case CTRL_X_PATH_PATTERNS:
472 return (c == Ctrl_P || c == Ctrl_N);
473 case CTRL_X_PATH_DEFINES:
474 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
475#endif
476 case CTRL_X_CMDLINE:
477 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
478 || c == Ctrl_X);
479#ifdef FEAT_COMPL_FUNC
480 case CTRL_X_FUNCTION:
481 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
482 case CTRL_X_OMNI:
483 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
484#endif
485 case CTRL_X_SPELL:
486 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
487 case CTRL_X_EVAL:
488 return (c == Ctrl_P || c == Ctrl_N);
489 }
490 internal_error("vim_is_ctrl_x_key()");
491 return FALSE;
492}
493
494/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000495 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000496 */
497 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000498match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000499{
500 return match->cp_flags & CP_ORIGINAL_TEXT;
501}
502
503/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000504 * Returns TRUE if "match" is the first match in the completion list.
505 */
506 static int
507is_first_match(compl_T *match)
508{
509 return match == compl_first_match;
510}
511
512/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 * Return TRUE when character "c" is part of the item currently being
514 * completed. Used to decide whether to abandon complete mode when the menu
515 * is visible.
516 */
517 int
518ins_compl_accept_char(int c)
519{
520 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
521 // When expanding an identifier only accept identifier chars.
522 return vim_isIDc(c);
523
524 switch (ctrl_x_mode)
525 {
526 case CTRL_X_FILES:
527 // When expanding file name only accept file name chars. But not
528 // path separators, so that "proto/<Tab>" expands files in
529 // "proto", not "proto/" as a whole
530 return vim_isfilec(c) && !vim_ispathsep(c);
531
532 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200533 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100534 case CTRL_X_OMNI:
535 // Command line and Omni completion can work with just about any
536 // printable character, but do stop at white space.
537 return vim_isprintc(c) && !VIM_ISWHITE(c);
538
539 case CTRL_X_WHOLE_LINE:
540 // For while line completion a space can be part of the line.
541 return vim_isprintc(c);
542 }
543 return vim_iswordc(c);
544}
545
546/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000547 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100548 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000549 */
550 static char_u *
551ins_compl_infercase_gettext(
552 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100553 int char_len,
554 int compl_char_len,
555 int min_len,
556 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000557{
558 int *wca; // Wide character array.
559 char_u *p;
560 int i, c;
561 int has_lower = FALSE;
562 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100563 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000564
565 IObuff[0] = NUL;
566
567 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100568 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000569 if (wca == NULL)
570 return IObuff;
571
572 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100573 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100574 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000575 if (has_mbyte)
576 wca[i] = mb_ptr2char_adv(&p);
577 else
578 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100579 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000580
581 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100582 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000583 for (i = 0; i < min_len; ++i)
584 {
glepnir6e199932024-12-14 21:13:27 +0100585 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000586 if (MB_ISLOWER(c))
587 {
588 has_lower = TRUE;
589 if (MB_ISUPPER(wca[i]))
590 {
591 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100592 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000593 wca[i] = MB_TOLOWER(wca[i]);
594 break;
595 }
596 }
597 }
598
599 // Rule 2: No lower case, 2nd consecutive letter converted to
600 // upper case.
601 if (!has_lower)
602 {
John Marriott5e6ea922024-11-23 14:01:57 +0100603 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000604 for (i = 0; i < min_len; ++i)
605 {
glepnir6e199932024-12-14 21:13:27 +0100606 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000607 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
608 {
609 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100610 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000611 wca[i] = MB_TOUPPER(wca[i]);
612 break;
613 }
614 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
615 }
616 }
617
618 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100619 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000620 for (i = 0; i < min_len; ++i)
621 {
glepnir6e199932024-12-14 21:13:27 +0100622 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000623 if (MB_ISLOWER(c))
624 wca[i] = MB_TOLOWER(wca[i]);
625 else if (MB_ISUPPER(c))
626 wca[i] = MB_TOUPPER(wca[i]);
627 }
628
629 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000630 p = IObuff;
631 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100632 ga_init2(&gap, 1, 500);
633 while (i < char_len)
634 {
635 if (gap.ga_data != NULL)
636 {
637 if (ga_grow(&gap, 10) == FAIL)
638 {
639 ga_clear(&gap);
640 return (char_u *)"[failed]";
641 }
642 p = (char_u *)gap.ga_data + gap.ga_len;
643 if (has_mbyte)
644 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
645 else
646 {
647 *p = wca[i++];
648 ++gap.ga_len;
649 }
650 }
651 else if ((p - IObuff) + 6 >= IOSIZE)
652 {
653 // Multi-byte characters can occupy up to five bytes more than
654 // ASCII characters, and we also need one byte for NUL, so when
655 // getting to six bytes from the edge of IObuff switch to using a
656 // growarray. Add the character in the next round.
657 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100658 {
659 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100661 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100662 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100664 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100665 }
666 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000667 p += (*mb_char2bytes)(wca[i++], p);
668 else
669 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100670 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000671 vim_free(wca);
672
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100673 if (gap.ga_data != NULL)
674 {
675 *tofree = gap.ga_data;
676 return gap.ga_data;
677 }
678
679 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000680 return IObuff;
681}
682
683/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100684 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
685 * case of the originally typed text is used, and the case of the completed
686 * text is inferred, ie this tries to work out what case you probably wanted
687 * the rest of the word to be in -- webb
688 */
689 int
690ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200691 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692 int len,
693 int icase,
694 char_u *fname,
695 int dir,
glepnirf31cfa22025-03-06 21:59:13 +0100696 int cont_s_ipos, // next ^X<> will set initial_pos
697 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200699 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100700 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100701 int char_len; // count multi-byte characters
702 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100703 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200704 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100705 int res;
706 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100707
708 if (p_ic && curbuf->b_p_inf && len > 0)
709 {
710 // Infer case of completed part.
711
712 // Find actual length of completion.
713 if (has_mbyte)
714 {
715 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100716 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100717 while (*p != NUL)
718 {
719 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100720 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100721 }
722 }
723 else
glepnir6e199932024-12-14 21:13:27 +0100724 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100725 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100726 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100727
728 // Find actual length of original text.
729 if (has_mbyte)
730 {
John Marriott5e6ea922024-11-23 14:01:57 +0100731 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 while (*p != NUL)
734 {
735 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737 }
738 }
739 else
glepnir6e199932024-12-14 21:13:27 +0100740 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100741 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100742 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100743
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100744 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100745 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100746 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100747
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100748 str = ins_compl_infercase_gettext(str, char_len,
749 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100750 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200751 if (cont_s_ipos)
752 flags |= CP_CONT_S_IPOS;
753 if (icase)
754 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200755
glepnirf31cfa22025-03-06 21:59:13 +0100756 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL, score);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100757 vim_free(tofree);
758 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100759}
760
761/*
glepnirf31cfa22025-03-06 21:59:13 +0100762 * Check if ctrl_x_mode has been configured in 'completefuzzycollect'
763 */
764 static int
765cfc_has_mode(void)
766{
767 switch (ctrl_x_mode)
768 {
769 case CTRL_X_NORMAL:
770 return (cfc_flags & CFC_KEYWORD) != 0;
771 case CTRL_X_FILES:
772 return (cfc_flags & CFC_FILES) != 0;
773 case CTRL_X_WHOLE_LINE:
774 return (cfc_flags & CFC_WHOLELINE) != 0;
775 default:
776 return FALSE;
777 }
778}
779
780/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000781 * Add a match to the list of matches. The arguments are:
782 * str - text of the match to add
783 * len - length of "str". If -1, then the length of "str" is
784 * computed.
785 * fname - file name to associate with this match.
786 * cptext - list of strings to use with this match (for abbr, menu, info
787 * and kind)
788 * user_data - user supplied data (any vim type) for this match
789 * cdir - match direction. If 0, use "compl_direction".
790 * flags_arg - match flags (cp_flags)
791 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100792 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000793 * If "cdir" is FORWARD, then the match is added after the current match.
794 * Otherwise, it is added before the current match.
795 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100796 * If the given string is already in the list of completions, then return
797 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
798 * maybe because alloc() returns NULL, then FAIL is returned.
799 */
800 static int
801ins_compl_add(
802 char_u *str,
803 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 char_u **cptext, // extra text for popup menu or NULL
806 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200808 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200809 int adup, // accept duplicate match
glepnirf31cfa22025-03-06 21:59:13 +0100810 int *user_hl, // user abbr/kind hlattr
811 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100812{
glepnirf31cfa22025-03-06 21:59:13 +0100813 compl_T *match, *current, *prev;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100814 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200815 int flags = flags_arg;
glepnirf31cfa22025-03-06 21:59:13 +0100816 int inserted = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100817
Bram Moolenaarceb06192021-04-04 15:05:22 +0200818 if (flags & CP_FAST)
819 fast_breakcheck();
820 else
821 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100822 if (got_int)
823 return FAIL;
824 if (len < 0)
825 len = (int)STRLEN(str);
826
827 // If the same match is already present, don't add it.
828 if (compl_first_match != NULL && !adup)
829 {
830 match = compl_first_match;
831 do
832 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000833 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100834 && STRNCMP(match->cp_str.string, str, len) == 0
835 && ((int)match->cp_str.length <= len
836 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100837 return NOTDONE;
838 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000839 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100840 }
841
842 // Remove any popup menu before changing the list of matches.
843 ins_compl_del_pum();
844
845 // Allocate a new match structure.
846 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200847 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100848 if (match == NULL)
849 return FAIL;
glepnir40891ba2025-02-10 22:18:00 +0100850 match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
John Marriott5e6ea922024-11-23 14:01:57 +0100851 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100852 {
853 vim_free(match);
854 return FAIL;
855 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856
John Marriott5e6ea922024-11-23 14:01:57 +0100857 match->cp_str.length = len;
858
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 // match-fname is:
860 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200861 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862 // - NULL otherwise. --Acevedo
863 if (fname != NULL
864 && compl_curr_match != NULL
865 && compl_curr_match->cp_fname != NULL
866 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
867 match->cp_fname = compl_curr_match->cp_fname;
868 else if (fname != NULL)
869 {
870 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200871 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100872 }
873 else
874 match->cp_fname = NULL;
875 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100876 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
877 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
glepnirf31cfa22025-03-06 21:59:13 +0100878 match->cp_score = score;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100879
880 if (cptext != NULL)
881 {
882 int i;
883
884 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100885 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100886 if (cptext[i] != NULL && *cptext[i] != NUL)
887 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100888 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100890#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100891 if (user_data != NULL)
892 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100893#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100894
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000895 // Link the new match structure after (FORWARD) or before (BACKWARD) the
896 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100897 if (compl_first_match == NULL)
898 match->cp_next = match->cp_prev = NULL;
glepnirf31cfa22025-03-06 21:59:13 +0100899 else if (cfc_has_mode() && score > 0 && compl_get_longest)
900 {
901 current = compl_first_match->cp_next;
902 prev = compl_first_match;
903 inserted = FALSE;
904 // The direction is ignored when using longest and
905 // completefuzzycollect, because matches are inserted
906 // and sorted by score.
907 while (current != NULL && current != compl_first_match)
908 {
909 if (current->cp_score < score)
910 {
911 match->cp_next = current;
912 match->cp_prev = current->cp_prev;
913 if (current->cp_prev)
914 current->cp_prev->cp_next = match;
915 current->cp_prev = match;
916 inserted = TRUE;
917 break;
918 }
919 prev = current;
920 current = current->cp_next;
921 }
922 if (!inserted)
923 {
924 prev->cp_next = match;
925 match->cp_prev = prev;
926 match->cp_next = compl_first_match;
927 compl_first_match->cp_prev = match;
928 }
929 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 else if (dir == FORWARD)
931 {
932 match->cp_next = compl_curr_match->cp_next;
933 match->cp_prev = compl_curr_match;
934 }
935 else // BACKWARD
936 {
937 match->cp_next = compl_curr_match;
938 match->cp_prev = compl_curr_match->cp_prev;
939 }
940 if (match->cp_next)
941 match->cp_next->cp_prev = match;
942 if (match->cp_prev)
943 match->cp_prev->cp_next = match;
944 else // if there's nothing before, it is the first match
945 compl_first_match = match;
946 compl_curr_match = match;
947
948 // Find the longest common string if still doing that.
glepnirf31cfa22025-03-06 21:59:13 +0100949 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100950 ins_compl_longest_match(match);
951
952 return OK;
953}
954
955/*
956 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200957 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100958 */
959 static int
960ins_compl_equal(compl_T *match, char_u *str, int len)
961{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200962 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200963 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200964 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100965 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
966 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100967}
968
969/*
glepnir6a38aff2024-12-16 21:56:16 +0100970 * when len is -1 mean use whole length of p otherwise part of p
971 */
972 static void
973ins_compl_insert_bytes(char_u *p, int len)
974{
975 if (len == -1)
976 len = (int)STRLEN(p);
977 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100978 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100979}
980
981/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100982 * Checks if the column is within the currently inserted completion text
983 * column range. If it is, it returns a special highlight attribute.
glepnir76bdb822025-02-08 19:04:51 +0100984 * -1 means normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100985 */
986 int
glepnir76bdb822025-02-08 19:04:51 +0100987ins_compl_col_range_attr(linenr_T lnum, int col)
glepnir6a38aff2024-12-16 21:56:16 +0100988{
glepnir76bdb822025-02-08 19:04:51 +0100989 int start_col;
990 int attr;
991
992 if ((get_cot_flags() & COT_FUZZY)
993 || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0)
glepnire8908872025-01-08 18:30:45 +0100994 return -1;
995
glepnir76bdb822025-02-08 19:04:51 +0100996 start_col = compl_col + (int)ins_compl_leader_len();
997 if (!ins_compl_has_multiple())
998 return (col >= start_col && col < compl_ins_end_col) ? attr : -1;
999
1000 // Multiple lines
1001 if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) ||
1002 (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) ||
1003 (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col))
1004 return attr;
glepnir6a38aff2024-12-16 21:56:16 +01001005
1006 return -1;
1007}
1008
1009/*
glepnir76bdb822025-02-08 19:04:51 +01001010 * Returns TRUE if the current completion string contains newline characters,
1011 * indicating it's a multi-line completion.
1012 */
1013 static int
1014ins_compl_has_multiple(void)
1015{
1016 return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL;
1017}
1018
1019/*
1020 * Returns TRUE if the given line number falls within the range of a multi-line
1021 * completion, i.e. between the starting line (compl_lnum) and current cursor
1022 * line. Always returns FALSE for single-line completions.
1023 */
1024 int
1025ins_compl_lnum_in_range(linenr_T lnum)
1026{
1027 if (!ins_compl_has_multiple())
1028 return FALSE;
1029 return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum;
1030}
1031
1032/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033 * Reduce the longest common string for match "match".
1034 */
1035 static void
1036ins_compl_longest_match(compl_T *match)
1037{
1038 char_u *p, *s;
1039 int c1, c2;
1040 int had_match;
1041
John Marriott5e6ea922024-11-23 14:01:57 +01001042 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001043 {
1044 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +01001045 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
1046 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001047 return;
1048
John Marriott5e6ea922024-11-23 14:01:57 +01001049 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001050 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001051 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001052
1053 // When the match isn't there (to avoid matching itself) remove it
1054 // again after redrawing.
1055 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001056 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001058
1059 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001060 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001061
1062 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +01001063 p = compl_leader.string;
1064 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001065 while (*p != NUL)
1066 {
1067 if (has_mbyte)
1068 {
1069 c1 = mb_ptr2char(p);
1070 c2 = mb_ptr2char(s);
1071 }
1072 else
1073 {
1074 c1 = *p;
1075 c2 = *s;
1076 }
1077 if ((match->cp_flags & CP_ICASE)
1078 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
1079 break;
1080 if (has_mbyte)
1081 {
1082 MB_PTR_ADV(p);
1083 MB_PTR_ADV(s);
1084 }
1085 else
1086 {
1087 ++p;
1088 ++s;
1089 }
1090 }
1091
1092 if (*p != NUL)
1093 {
1094 // Leader was shortened, need to change the inserted text.
1095 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001096 compl_leader.length = (size_t)(p - compl_leader.string);
1097
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001098 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001099 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001100
1101 // When the match isn't there (to avoid matching itself) remove it
1102 // again after redrawing.
1103 if (!had_match)
1104 ins_compl_delete();
1105 }
1106
1107 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001108}
1109
1110/*
1111 * Add an array of matches to the list of matches.
1112 * Frees matches[].
1113 */
1114 static void
1115ins_compl_add_matches(
1116 int num_matches,
1117 char_u **matches,
1118 int icase)
1119{
1120 int i;
1121 int add_r = OK;
1122 int dir = compl_direction;
1123
1124 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001125 {
1126 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
glepnirf31cfa22025-03-06 21:59:13 +01001127 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0);
glepnir6e199932024-12-14 21:13:27 +01001128 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001129 // if dir was BACKWARD then honor it just once
1130 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001131 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001132 FreeWild(num_matches, matches);
1133}
1134
1135/*
1136 * Make the completion list cyclic.
1137 * Return the number of matches (excluding the original).
1138 */
1139 static int
1140ins_compl_make_cyclic(void)
1141{
1142 compl_T *match;
1143 int count = 0;
1144
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001145 if (compl_first_match == NULL)
1146 return 0;
1147
1148 // Find the end of the list.
1149 match = compl_first_match;
1150 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001151 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001152 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001153 match = match->cp_next;
1154 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001155 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001156 match->cp_next = compl_first_match;
1157 compl_first_match->cp_prev = match;
1158
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159 return count;
1160}
1161
1162/*
1163 * Return whether there currently is a shown match.
1164 */
1165 int
1166ins_compl_has_shown_match(void)
1167{
1168 return compl_shown_match == NULL
1169 || compl_shown_match != compl_shown_match->cp_next;
1170}
1171
1172/*
1173 * Return whether the shown match is long enough.
1174 */
1175 int
1176ins_compl_long_shown_match(void)
1177{
John Marriott5e6ea922024-11-23 14:01:57 +01001178 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001179 > curwin->w_cursor.col - compl_col;
1180}
1181
1182/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001183 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001184 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001185 unsigned int
1186get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001187{
zeertzjq529b9ad2024-06-05 20:27:06 +02001188 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001189}
1190
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001191/*
1192 * Update the screen and when there is any scrolling remove the popup menu.
1193 */
1194 static void
1195ins_compl_upd_pum(void)
1196{
1197 int h;
1198
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001199 if (compl_match_array == NULL)
1200 return;
1201
1202 h = curwin->w_cline_height;
1203 // Update the screen later, before drawing the popup menu over it.
1204 pum_call_update_screen();
1205 if (h != curwin->w_cline_height)
1206 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001207}
1208
1209/*
1210 * Remove any popup menu.
1211 */
1212 static void
1213ins_compl_del_pum(void)
1214{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001215 if (compl_match_array == NULL)
1216 return;
1217
1218 pum_undisplay();
1219 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001220}
1221
1222/*
1223 * Return TRUE if the popup menu should be displayed.
1224 */
1225 int
1226pum_wanted(void)
1227{
1228 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001229 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001230 return FALSE;
1231
1232 // The display looks bad on a B&W display.
1233 if (t_colors < 8
1234#ifdef FEAT_GUI
1235 && !gui.in_use
1236#endif
1237 )
1238 return FALSE;
1239 return TRUE;
1240}
1241
1242/*
1243 * Return TRUE if there are two or more matches to be shown in the popup menu.
1244 * One if 'completopt' contains "menuone".
1245 */
1246 static int
1247pum_enough_matches(void)
1248{
1249 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001250 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001251
1252 // Don't display the popup menu if there are no matches or there is only
1253 // one (ignoring the original text).
1254 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001255 do
1256 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001257 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001258 break;
1259 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001260 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001261
zeertzjq529b9ad2024-06-05 20:27:06 +02001262 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001263 return (i >= 1);
1264 return (i >= 2);
1265}
1266
Bram Moolenaar3075a452021-11-17 15:51:52 +00001267#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001268/*
1269 * Allocate Dict for the completed item.
1270 * { word, abbr, menu, kind, info }
1271 */
1272 static dict_T *
1273ins_compl_dict_alloc(compl_T *match)
1274{
1275 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1276
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001277 if (dict == NULL)
1278 return NULL;
1279
John Marriott5e6ea922024-11-23 14:01:57 +01001280 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001281 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1282 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1283 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1284 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1285 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1286 dict_add_string(dict, "user_data", (char_u *)"");
1287 else
1288 dict_add_tv(dict, "user_data", &match->cp_user_data);
1289
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001290 return dict;
1291}
1292
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001293/*
1294 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1295 * completion menu is changed.
1296 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001297 static void
1298trigger_complete_changed_event(int cur)
1299{
1300 dict_T *v_event;
1301 dict_T *item;
1302 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001303 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001304
1305 if (recursive)
1306 return;
1307
glepnir40891ba2025-02-10 22:18:00 +01001308 item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001309 if (item == NULL)
1310 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001311 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001312 dict_add_dict(v_event, "completed_item", item);
1313 pum_set_event_info(v_event);
1314 dict_set_items_ro(v_event);
1315
1316 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001317 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001318 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001319 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001320 recursive = FALSE;
1321
Bram Moolenaar3075a452021-11-17 15:51:52 +00001322 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001323}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001324#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001325
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001326/*
glepnira218cc62024-06-03 19:32:39 +02001327 * pumitem qsort compare func
1328 */
1329 static int
zeertzjq8e567472024-06-14 20:04:42 +02001330ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001331{
1332 const int sa = (*(pumitem_T *)a).pum_score;
1333 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001334 const int ia = (*(pumitem_T *)a).pum_idx;
1335 const int ib = (*(pumitem_T *)b).pum_idx;
1336 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001337}
1338
1339/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001340 * Build a popup menu to show the completion matches.
1341 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1342 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001343 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001344 static int
1345ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001346{
1347 compl_T *compl;
1348 compl_T *shown_compl = NULL;
1349 int did_find_shown_match = FALSE;
1350 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001351 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001352 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001353 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001354 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001355 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001356 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1357 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnirf31cfa22025-03-06 21:59:13 +01001358
glepnir80b66202024-11-27 21:53:53 +01001359 compl_T *match_head = NULL;
1360 compl_T *match_tail = NULL;
1361 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001362
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001363 // Need to build the popup menu list.
1364 compl_match_arraysize = 0;
1365 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001366
glepnira49c0772024-11-30 10:56:30 +01001367 // If the current match is the original text don't find the first
1368 // match after it, don't highlight anything.
1369 if (match_at_original_text(compl_shown_match))
1370 shown_match_ok = TRUE;
1371
1372 if (compl_leader.string != NULL
1373 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1374 && shown_match_ok == FALSE)
1375 compl_shown_match = compl_no_select ? compl_first_match
1376 : compl_first_match->cp_next;
1377
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001378 do
1379 {
glepnird4088ed2024-12-31 10:55:22 +01001380 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001381 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1382 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001383 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001384 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001385
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001386 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001387 && (compl_leader.string == NULL
1388 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001389 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001390 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001391 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001392 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001393 if (match_head == NULL)
1394 match_head = compl;
1395 else
glepnira49c0772024-11-30 10:56:30 +01001396 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001397 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001398
glepnirf400a0c2025-01-23 19:55:14 +01001399 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001400 {
1401 if (compl == compl_shown_match || did_find_shown_match)
1402 {
1403 // This item is the shown match or this is the
1404 // first displayed item after the shown match.
1405 compl_shown_match = compl;
1406 did_find_shown_match = TRUE;
1407 shown_match_ok = TRUE;
1408 }
1409 else
1410 // Remember this displayed match for when the
1411 // shown match is just below it.
1412 shown_compl = compl;
1413 cur = i;
1414 }
glepnirf400a0c2025-01-23 19:55:14 +01001415 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001416 {
1417 if (i == 0)
1418 shown_compl = compl;
1419 // Update the maximum fuzzy score and the shown match
1420 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001421 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001422 {
1423 did_find_shown_match = TRUE;
1424 max_fuzzy_score = compl->cp_score;
1425 if (!compl_no_select)
1426 compl_shown_match = compl;
1427 }
1428
glepnir3af0a8d2025-02-20 22:06:16 +01001429 if (!shown_match_ok && compl == compl_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001430 {
1431 cur = i;
1432 shown_match_ok = TRUE;
1433 }
1434 }
1435 i++;
glepnir80b66202024-11-27 21:53:53 +01001436 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001437
glepnirf400a0c2025-01-23 19:55:14 +01001438 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001439 {
1440 did_find_shown_match = TRUE;
1441
1442 // When the original text is the shown match don't set
1443 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001444 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001445 shown_match_ok = TRUE;
1446
1447 if (!shown_match_ok && shown_compl != NULL)
1448 {
1449 // The shown match isn't displayed, set it to the
1450 // previously displayed match.
1451 compl_shown_match = shown_compl;
1452 shown_match_ok = TRUE;
1453 }
1454 }
glepnira49c0772024-11-30 10:56:30 +01001455 compl = compl->cp_next;
1456 } while (compl != NULL && !is_first_match(compl));
1457
1458 if (compl_match_arraysize == 0)
1459 return -1;
1460
glepnirc0b7ca42025-02-13 20:27:44 +01001461 if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok)
1462 {
1463 compl_shown_match = shown_compl;
1464 shown_match_ok = TRUE;
1465 cur = 0;
1466 }
1467
glepnira49c0772024-11-30 10:56:30 +01001468 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1469 if (compl_match_array == NULL)
1470 return -1;
1471
1472 compl = match_head;
1473 i = 0;
1474 while (compl != NULL)
1475 {
glepnir6e199932024-12-14 21:13:27 +01001476 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1477 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001478 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1479 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1480 compl_match_array[i].pum_score = compl->cp_score;
1481 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1482 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001483 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1484 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001485 match_next = compl->cp_match_next;
1486 compl->cp_match_next = NULL;
1487 compl = match_next;
1488 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001489
zeertzjqd65aa1b2025-01-25 15:29:03 +01001490 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001491 {
1492 for (i = 0; i < compl_match_arraysize; i++)
1493 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001494 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001495 qsort(compl_match_array, (size_t)compl_match_arraysize,
1496 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001497 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001498 }
glepnira218cc62024-06-03 19:32:39 +02001499
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001500 if (!shown_match_ok) // no displayed match at all
1501 cur = -1;
1502
1503 return cur;
1504}
1505
1506/*
1507 * Show the popup menu for the list of matches.
1508 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1509 */
1510 void
1511ins_compl_show_pum(void)
1512{
1513 int i;
1514 int cur = -1;
1515 colnr_T col;
1516
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001517 if (!pum_wanted() || !pum_enough_matches())
1518 return;
1519
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001520 // Update the screen later, before drawing the popup menu over it.
1521 pum_call_update_screen();
1522
1523 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001524 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001525 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001526 else
1527 {
1528 // popup menu already exists, only need to find the current item.
1529 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001530 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001531 || compl_match_array[i].pum_text
1532 == compl_shown_match->cp_text[CPT_ABBR])
1533 {
1534 cur = i;
1535 break;
1536 }
1537 }
1538
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001539 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001540 {
1541#ifdef FEAT_EVAL
1542 if (compl_started && has_completechanged())
1543 trigger_complete_changed_event(cur);
1544#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001545 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001546 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001547
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001548 // In Replace mode when a $ is displayed at the end of the line only
1549 // part of the screen would be updated. We do need to redraw here.
1550 dollar_vcol = -1;
1551
1552 // Compute the screen column of the start of the completed text.
1553 // Use the cursor to get all wrapping and other settings right.
1554 col = curwin->w_cursor.col;
1555 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001556 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001557 pum_display(compl_match_array, compl_match_arraysize, cur);
1558 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001559
glepnircbb46b42024-02-03 18:11:13 +01001560 // After adding leader, set the current match to shown match.
1561 if (compl_started && compl_curr_match != compl_shown_match)
1562 compl_curr_match = compl_shown_match;
1563
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001564#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001565 if (has_completechanged())
1566 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001567#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001568}
1569
1570#define DICT_FIRST (1) // use just first element in "dict"
1571#define DICT_EXACT (2) // "dict" is the exact name of a file
1572
1573/*
glepnir40c1c332024-06-11 19:37:04 +02001574 * Get current completion leader
1575 */
1576 char_u *
1577ins_compl_leader(void)
1578{
John Marriott5e6ea922024-11-23 14:01:57 +01001579 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1580}
1581
1582/*
1583 * Get current completion leader length
1584 */
1585 size_t
1586ins_compl_leader_len(void)
1587{
1588 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001589}
1590
1591/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001592 * Add any identifiers that match the given pattern "pat" in the list of
1593 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001594 */
1595 static void
1596ins_compl_dictionaries(
1597 char_u *dict_start,
1598 char_u *pat,
1599 int flags, // DICT_FIRST and/or DICT_EXACT
1600 int thesaurus) // Thesaurus completion
1601{
1602 char_u *dict = dict_start;
1603 char_u *ptr;
1604 char_u *buf;
1605 regmatch_T regmatch;
1606 char_u **files;
1607 int count;
1608 int save_p_scs;
1609 int dir = compl_direction;
1610
1611 if (*dict == NUL)
1612 {
1613#ifdef FEAT_SPELL
1614 // When 'dictionary' is empty and spell checking is enabled use
1615 // "spell".
1616 if (!thesaurus && curwin->w_p_spell)
1617 dict = (char_u *)"spell";
1618 else
1619#endif
1620 return;
1621 }
1622
1623 buf = alloc(LSIZE);
1624 if (buf == NULL)
1625 return;
1626 regmatch.regprog = NULL; // so that we can goto theend
1627
1628 // If 'infercase' is set, don't use 'smartcase' here
1629 save_p_scs = p_scs;
1630 if (curbuf->b_p_inf)
1631 p_scs = FALSE;
1632
1633 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1634 // to only match at the start of a line. Otherwise just match the
1635 // pattern. Also need to double backslashes.
1636 if (ctrl_x_mode_line_or_eval())
1637 {
1638 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1639 size_t len;
1640
1641 if (pat_esc == NULL)
1642 goto theend;
1643 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001644 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001645 if (ptr == NULL)
1646 {
1647 vim_free(pat_esc);
1648 goto theend;
1649 }
1650 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1651 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1652 vim_free(pat_esc);
1653 vim_free(ptr);
1654 }
1655 else
1656 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001657 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001658 if (regmatch.regprog == NULL)
1659 goto theend;
1660 }
1661
1662 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1663 regmatch.rm_ic = ignorecase(pat);
1664 while (*dict != NUL && !got_int && !compl_interrupted)
1665 {
1666 // copy one dictionary file name into buf
1667 if (flags == DICT_EXACT)
1668 {
1669 count = 1;
1670 files = &dict;
1671 }
1672 else
1673 {
1674 // Expand wildcards in the dictionary name, but do not allow
1675 // backticks (for security, the 'dict' option may have been set in
1676 // a modeline).
1677 copy_option_part(&dict, buf, LSIZE, ",");
1678# ifdef FEAT_SPELL
1679 if (!thesaurus && STRCMP(buf, "spell") == 0)
1680 count = -1;
1681 else
1682# endif
1683 if (vim_strchr(buf, '`') != NULL
1684 || expand_wildcards(1, &buf, &count, &files,
1685 EW_FILE|EW_SILENT) != OK)
1686 count = 0;
1687 }
1688
1689# ifdef FEAT_SPELL
1690 if (count == -1)
1691 {
1692 // Complete from active spelling. Skip "\<" in the pattern, we
1693 // don't use it as a RE.
1694 if (pat[0] == '\\' && pat[1] == '<')
1695 ptr = pat + 2;
1696 else
1697 ptr = pat;
1698 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1699 }
1700 else
1701# endif
1702 if (count > 0) // avoid warning for using "files" uninit
1703 {
1704 ins_compl_files(count, files, thesaurus, flags,
glepnirf31cfa22025-03-06 21:59:13 +01001705 (cfc_has_mode() ? NULL : &regmatch), buf, &dir);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001706 if (flags != DICT_EXACT)
1707 FreeWild(count, files);
1708 }
1709 if (flags != 0)
1710 break;
1711 }
1712
1713theend:
1714 p_scs = save_p_scs;
1715 vim_regfree(regmatch.regprog);
1716 vim_free(buf);
1717}
1718
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001719/*
1720 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1721 * skipping the word at 'skip_word'. Returns OK on success.
1722 */
1723 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001724thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001725 char_u *fname,
1726 char_u **buf_arg,
1727 int dir,
1728 char_u *skip_word)
1729{
1730 int status = OK;
1731 char_u *ptr;
1732 char_u *wstart;
1733
1734 // Add the other matches on the line
1735 ptr = *buf_arg;
1736 while (!got_int)
1737 {
1738 // Find start of the next word. Skip white
1739 // space and punctuation.
1740 ptr = find_word_start(ptr);
1741 if (*ptr == NUL || *ptr == NL)
1742 break;
1743 wstart = ptr;
1744
1745 // Find end of the word.
1746 if (has_mbyte)
1747 // Japanese words may have characters in
1748 // different classes, only separate words
1749 // with single-byte non-word characters.
1750 while (*ptr != NUL)
1751 {
1752 int l = (*mb_ptr2len)(ptr);
1753
1754 if (l < 2 && !vim_iswordc(*ptr))
1755 break;
1756 ptr += l;
1757 }
1758 else
1759 ptr = find_word_end(ptr);
1760
1761 // Add the word. Skip the regexp match.
1762 if (wstart != skip_word)
1763 {
1764 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01001765 fname, dir, FALSE, 0);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001766 if (status == FAIL)
1767 break;
1768 }
1769 }
1770
1771 *buf_arg = ptr;
1772 return status;
1773}
1774
1775/*
1776 * Process "count" dictionary/thesaurus "files" and add the text matching
1777 * "regmatch".
1778 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001779 static void
1780ins_compl_files(
1781 int count,
1782 char_u **files,
1783 int thesaurus,
1784 int flags,
1785 regmatch_T *regmatch,
1786 char_u *buf,
1787 int *dir)
1788{
1789 char_u *ptr;
1790 int i;
1791 FILE *fp;
1792 int add_r;
glepnirf31cfa22025-03-06 21:59:13 +01001793 char_u *leader = NULL;
1794 int leader_len = 0;
1795 int in_fuzzy_collect = cfc_has_mode() && ctrl_x_mode_normal();
1796 int score = 0;
1797 int len = 0;
1798 char_u *line_end = NULL;
1799
1800 if (in_fuzzy_collect)
1801 {
1802 leader = ins_compl_leader();
1803 leader_len = ins_compl_leader_len();
1804 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001805
1806 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1807 {
1808 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001809 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001810 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001811 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001812 vim_snprintf((char *)IObuff, IOSIZE,
1813 _("Scanning dictionary: %s"), (char *)files[i]);
1814 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1815 }
1816
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001817 if (fp == NULL)
1818 continue;
1819
1820 // Read dictionary file line by line.
1821 // Check each line for a match.
1822 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001823 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001824 ptr = buf;
glepnirf31cfa22025-03-06 21:59:13 +01001825 if (regmatch != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001826 {
glepnirf31cfa22025-03-06 21:59:13 +01001827 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001828 {
glepnirf31cfa22025-03-06 21:59:13 +01001829 ptr = regmatch->startp[0];
1830 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1831 : find_word_end(ptr);
1832 add_r = ins_compl_add_infercase(regmatch->startp[0],
1833 (int)(ptr - regmatch->startp[0]),
1834 p_ic, files[i], *dir, FALSE, 0);
1835 if (thesaurus)
1836 {
1837 // For a thesaurus, add all the words in the line
1838 ptr = buf;
1839 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
1840 regmatch->startp[0]);
1841 }
1842 if (add_r == OK)
1843 // if dir was BACKWARD then honor it just once
1844 *dir = FORWARD;
1845 else if (add_r == FAIL)
1846 break;
1847 // avoid expensive call to vim_regexec() when at end
1848 // of line
1849 if (*ptr == '\n' || got_int)
1850 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001851 }
glepnirf31cfa22025-03-06 21:59:13 +01001852 }
1853 else if (in_fuzzy_collect && leader_len > 0)
1854 {
1855 line_end = find_line_end(ptr);
1856 while (ptr < line_end)
1857 {
1858 if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score))
1859 {
1860 char_u *end_ptr = ctrl_x_mode_line_or_eval()
1861 ? find_line_end(ptr) : find_word_end(ptr);
1862 add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr),
1863 p_ic, files[i], *dir, FALSE, score);
1864 if (add_r == FAIL)
1865 break;
1866 ptr = end_ptr; // start from next word
1867 if (compl_get_longest && ctrl_x_mode_normal()
1868 && compl_first_match->cp_next
1869 && score == compl_first_match->cp_next->cp_score)
1870 compl_num_bests++;
1871 }
glepnirf31cfa22025-03-06 21:59:13 +01001872 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001873 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001874 line_breakcheck();
1875 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001876 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001877 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001878 }
1879}
1880
1881/*
1882 * Find the start of the next word.
1883 * Returns a pointer to the first char of the word. Also stops at a NUL.
1884 */
1885 char_u *
1886find_word_start(char_u *ptr)
1887{
1888 if (has_mbyte)
1889 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1890 ptr += (*mb_ptr2len)(ptr);
1891 else
1892 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1893 ++ptr;
1894 return ptr;
1895}
1896
1897/*
1898 * Find the end of the word. Assumes it starts inside a word.
1899 * Returns a pointer to just after the word.
1900 */
1901 char_u *
1902find_word_end(char_u *ptr)
1903{
1904 int start_class;
1905
1906 if (has_mbyte)
1907 {
1908 start_class = mb_get_class(ptr);
1909 if (start_class > 1)
1910 while (*ptr != NUL)
1911 {
1912 ptr += (*mb_ptr2len)(ptr);
1913 if (mb_get_class(ptr) != start_class)
1914 break;
1915 }
1916 }
1917 else
1918 while (vim_iswordc(*ptr))
1919 ++ptr;
1920 return ptr;
1921}
1922
1923/*
1924 * Find the end of the line, omitting CR and NL at the end.
1925 * Returns a pointer to just after the line.
1926 */
glepnirdd42b052025-03-08 16:52:55 +01001927 char_u *
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001928find_line_end(char_u *ptr)
1929{
1930 char_u *s;
1931
1932 s = ptr + STRLEN(ptr);
1933 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1934 --s;
1935 return s;
1936}
1937
1938/*
1939 * Free the list of completions
1940 */
1941 static void
1942ins_compl_free(void)
1943{
1944 compl_T *match;
1945 int i;
1946
John Marriott5e6ea922024-11-23 14:01:57 +01001947 VIM_CLEAR_STRING(compl_pattern);
1948 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001949
1950 if (compl_first_match == NULL)
1951 return;
1952
1953 ins_compl_del_pum();
1954 pum_clear();
1955
1956 compl_curr_match = compl_first_match;
1957 do
1958 {
1959 match = compl_curr_match;
1960 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001961 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001962 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001963 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001964 vim_free(match->cp_fname);
1965 for (i = 0; i < CPT_COUNT; ++i)
1966 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001967#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001968 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001969#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001970 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001971 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001972 compl_first_match = compl_curr_match = NULL;
1973 compl_shown_match = NULL;
1974 compl_old_match = NULL;
1975}
1976
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001977/*
1978 * Reset/clear the completion state.
1979 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001980 void
1981ins_compl_clear(void)
1982{
1983 compl_cont_status = 0;
1984 compl_started = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01001985 compl_cfc_longest_ins = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001986 compl_matches = 0;
glepnir07f0dbe2025-02-18 20:27:30 +01001987 compl_selected_item = -1;
glepnir6a38aff2024-12-16 21:56:16 +01001988 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001989 VIM_CLEAR_STRING(compl_pattern);
1990 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001991 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001992 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001993 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001994#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001995 // clear v:completed_item
1996 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001997#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001998}
1999
2000/*
2001 * Return TRUE when Insert completion is active.
2002 */
2003 int
2004ins_compl_active(void)
2005{
2006 return compl_started;
2007}
2008
2009/*
glepnir8d0bb6d2024-12-24 09:44:35 +01002010 * Return True when wp is the actual completion window
2011 */
2012 int
2013ins_compl_win_active(win_T *wp UNUSED)
2014{
2015 return ins_compl_active()
2016#if defined(FEAT_QUICKFIX)
2017 && (!wp->w_p_pvw
2018# ifdef FEAT_PROP_POPUP
2019 && !(wp->w_popup_flags & POPF_INFO)
2020# endif
2021 )
2022#endif
2023 ;
2024}
2025
2026/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002027 * Selected one of the matches. When FALSE the match was edited or using the
2028 * longest common string.
2029 */
2030 int
2031ins_compl_used_match(void)
2032{
2033 return compl_used_match;
2034}
2035
2036/*
2037 * Initialize get longest common string.
2038 */
2039 void
2040ins_compl_init_get_longest(void)
2041{
2042 compl_get_longest = FALSE;
2043}
2044
2045/*
2046 * Returns TRUE when insert completion is interrupted.
2047 */
2048 int
2049ins_compl_interrupted(void)
2050{
2051 return compl_interrupted;
2052}
2053
2054/*
2055 * Returns TRUE if the <Enter> key selects a match in the completion popup
2056 * menu.
2057 */
2058 int
2059ins_compl_enter_selects(void)
2060{
2061 return compl_enter_selects;
2062}
2063
2064/*
2065 * Return the column where the text starts that is being completed
2066 */
2067 colnr_T
2068ins_compl_col(void)
2069{
2070 return compl_col;
2071}
2072
2073/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002074 * Return the length in bytes of the text being completed
2075 */
2076 int
2077ins_compl_len(void)
2078{
2079 return compl_length;
2080}
2081
2082/*
glepnir94a045e2025-03-01 16:12:23 +01002083 * Return TRUE when the 'completeopt' "preinsert" flag is in effect,
2084 * otherwise return FALSE.
glepniredd4ac32025-01-29 18:53:51 +01002085 */
2086 static int
2087ins_compl_has_preinsert(void)
2088{
glepnira2c55592025-02-28 17:43:42 +01002089 int cur_cot_flags = get_cot_flags();
glepnir94a045e2025-03-01 16:12:23 +01002090 return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE))
2091 == (COT_PREINSERT | COT_MENUONE);
glepniredd4ac32025-01-29 18:53:51 +01002092}
2093
2094/*
2095 * Returns TRUE if the pre-insert effect is valid and the cursor is within
2096 * the `compl_ins_end_col` range.
2097 */
2098 int
2099ins_compl_preinsert_effect(void)
2100{
2101 if (!ins_compl_has_preinsert())
2102 return FALSE;
2103
2104 return curwin->w_cursor.col < compl_ins_end_col;
2105}
2106
2107/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002108 * Delete one character before the cursor and show the subset of the matches
2109 * that match the word that is now before the cursor.
2110 * Returns the character to be used, NUL if the work is done and another char
2111 * to be got from the user.
2112 */
2113 int
2114ins_compl_bs(void)
2115{
2116 char_u *line;
2117 char_u *p;
2118
glepniredd4ac32025-01-29 18:53:51 +01002119 if (ins_compl_preinsert_effect())
2120 ins_compl_delete();
2121
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002122 line = ml_get_curline();
2123 p = line + curwin->w_cursor.col;
2124 MB_PTR_BACK(line, p);
2125
2126 // Stop completion when the whole word was deleted. For Omni completion
2127 // allow the word to be deleted, we won't match everything.
2128 // Respect the 'backspace' option.
2129 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002130 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2131 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002132 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2133 - compl_length < 0))
2134 return K_BS;
2135
2136 // Deleted more than what was used to find matches or didn't finish
2137 // finding all matches: need to look for matches all over again.
2138 if (curwin->w_cursor.col <= compl_col + compl_length
2139 || ins_compl_need_restart())
2140 ins_compl_restart();
2141
John Marriott5e6ea922024-11-23 14:01:57 +01002142 VIM_CLEAR_STRING(compl_leader);
2143 compl_leader.length = (size_t)((p - line) - compl_col);
2144 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2145 if (compl_leader.string == NULL)
2146 {
2147 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002148 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002149 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002150
2151 ins_compl_new_leader();
2152 if (compl_shown_match != NULL)
2153 // Make sure current match is not a hidden item.
2154 compl_curr_match = compl_shown_match;
2155 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002156}
2157
2158/*
2159 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2160 * be called.
2161 */
2162 static int
2163ins_compl_need_restart(void)
2164{
2165 // Return TRUE if we didn't complete finding matches or when the
2166 // 'completefunc' returned "always" in the "refresh" dictionary item.
2167 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002168 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002169 && compl_opt_refresh_always);
2170}
2171
2172/*
2173 * Called after changing "compl_leader".
2174 * Show the popup menu with a different set of matches.
2175 * May also search for matches again if the previous search was interrupted.
2176 */
2177 static void
2178ins_compl_new_leader(void)
2179{
2180 ins_compl_del_pum();
2181 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002182 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002183 compl_used_match = FALSE;
2184
2185 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002186 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002187 else
2188 {
2189#ifdef FEAT_SPELL
2190 spell_bad_len = 0; // need to redetect bad word
2191#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002192 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002193 // the popup menu display the changed text before the cursor. Set
2194 // "compl_restarting" to avoid that the first match is inserted.
2195 pum_call_update_screen();
2196#ifdef FEAT_GUI
2197 if (gui.in_use)
2198 {
2199 // Show the cursor after the match, not after the redrawn text.
2200 setcursor();
2201 out_flush_cursor(FALSE, FALSE);
2202 }
2203#endif
2204 compl_restarting = TRUE;
2205 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2206 compl_cont_status = 0;
2207 compl_restarting = FALSE;
2208 }
2209
glepnir44180412025-02-20 22:09:48 +01002210 compl_enter_selects = !compl_used_match && compl_selected_item != -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002211
2212 // Show the popup menu with a different set of matches.
2213 ins_compl_show_pum();
2214
2215 // Don't let Enter select the original text when there is no popup menu.
2216 if (compl_match_array == NULL)
2217 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002218 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2219 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002220}
2221
2222/*
2223 * Return the length of the completion, from the completion start column to
2224 * the cursor column. Making sure it never goes below zero.
2225 */
2226 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002227get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002228{
2229 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002230 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002231}
2232
2233/*
2234 * Append one character to the match leader. May reduce the number of
2235 * matches.
2236 */
2237 void
2238ins_compl_addleader(int c)
2239{
2240 int cc;
2241
glepniredd4ac32025-01-29 18:53:51 +01002242 if (ins_compl_preinsert_effect())
2243 ins_compl_delete();
2244
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002245 if (stop_arrow() == FAIL)
2246 return;
2247 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2248 {
2249 char_u buf[MB_MAXBYTES + 1];
2250
2251 (*mb_char2bytes)(c, buf);
2252 buf[cc] = NUL;
2253 ins_char_bytes(buf, cc);
2254 if (compl_opt_refresh_always)
2255 AppendToRedobuff(buf);
2256 }
2257 else
2258 {
2259 ins_char(c);
2260 if (compl_opt_refresh_always)
2261 AppendCharToRedobuff(c);
2262 }
2263
2264 // If we didn't complete finding matches we must search again.
2265 if (ins_compl_need_restart())
2266 ins_compl_restart();
2267
2268 // When 'always' is set, don't reset compl_leader. While completing,
2269 // cursor doesn't point original position, changing compl_leader would
2270 // break redo.
2271 if (!compl_opt_refresh_always)
2272 {
John Marriott5e6ea922024-11-23 14:01:57 +01002273 VIM_CLEAR_STRING(compl_leader);
2274 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2275 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2276 compl_leader.length);
2277 if (compl_leader.string == NULL)
2278 {
2279 compl_leader.length = 0;
2280 return;
2281 }
2282
2283 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002284 }
2285}
2286
2287/*
2288 * Setup for finding completions again without leaving CTRL-X mode. Used when
2289 * BS or a key was typed while still searching for matches.
2290 */
2291 static void
2292ins_compl_restart(void)
2293{
2294 ins_compl_free();
2295 compl_started = FALSE;
2296 compl_matches = 0;
2297 compl_cont_status = 0;
2298 compl_cont_mode = 0;
2299}
2300
2301/*
2302 * Set the first match, the original text.
2303 */
2304 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002305ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002306{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002307 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002308 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2309 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002310 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002311 {
John Marriott5e6ea922024-11-23 14:01:57 +01002312 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002313 if (p != NULL)
2314 {
John Marriott5e6ea922024-11-23 14:01:57 +01002315 VIM_CLEAR_STRING(compl_first_match->cp_str);
2316 compl_first_match->cp_str.string = p;
2317 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002318 }
2319 }
2320 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002321 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002322 {
John Marriott5e6ea922024-11-23 14:01:57 +01002323 char_u *p = vim_strnsave(str, len);
2324 if (p != NULL)
2325 {
2326 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2327 compl_first_match->cp_prev->cp_str.string = p;
2328 compl_first_match->cp_prev->cp_str.length = len;
2329 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002330 }
2331}
2332
2333/*
2334 * Append one character to the match leader. May reduce the number of
2335 * matches.
2336 */
2337 void
2338ins_compl_addfrommatch(void)
2339{
2340 char_u *p;
2341 int len = (int)curwin->w_cursor.col - (int)compl_col;
2342 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002343
John Marriott5e6ea922024-11-23 14:01:57 +01002344 p = compl_shown_match->cp_str.string;
2345 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002346 {
John Marriott5e6ea922024-11-23 14:01:57 +01002347 size_t plen;
2348 compl_T *cp;
2349
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002350 // When still at the original match use the first entry that matches
2351 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002352 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002353 return;
2354
2355 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002356 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002357 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002358 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002359 {
John Marriott5e6ea922024-11-23 14:01:57 +01002360 if (compl_leader.string == NULL
2361 || ins_compl_equal(cp, compl_leader.string,
2362 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002363 {
John Marriott5e6ea922024-11-23 14:01:57 +01002364 p = cp->cp_str.string;
2365 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002366 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002367 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002368 }
John Marriott5e6ea922024-11-23 14:01:57 +01002369 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002370 return;
2371 }
2372 p += len;
2373 c = PTR2CHAR(p);
2374 ins_compl_addleader(c);
2375}
2376
2377/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002378 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002379 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2380 * compl_cont_mode and compl_cont_status.
2381 * Returns TRUE when the character is not to be inserted.
2382 */
2383 static int
2384set_ctrl_x_mode(int c)
2385{
2386 int retval = FALSE;
2387
2388 switch (c)
2389 {
2390 case Ctrl_E:
2391 case Ctrl_Y:
2392 // scroll the window one line up or down
2393 ctrl_x_mode = CTRL_X_SCROLL;
2394 if (!(State & REPLACE_FLAG))
2395 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2396 else
2397 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2398 edit_submode_pre = NULL;
2399 showmode();
2400 break;
2401 case Ctrl_L:
2402 // complete whole line
2403 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2404 break;
2405 case Ctrl_F:
2406 // complete filenames
2407 ctrl_x_mode = CTRL_X_FILES;
2408 break;
2409 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002410 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002411 ctrl_x_mode = CTRL_X_DICTIONARY;
2412 break;
2413 case Ctrl_R:
2414 // Register insertion without exiting CTRL-X mode
2415 // Simply allow ^R to happen without affecting ^X mode
2416 break;
2417 case Ctrl_T:
2418 // complete words from a thesaurus
2419 ctrl_x_mode = CTRL_X_THESAURUS;
2420 break;
2421#ifdef FEAT_COMPL_FUNC
2422 case Ctrl_U:
2423 // user defined completion
2424 ctrl_x_mode = CTRL_X_FUNCTION;
2425 break;
2426 case Ctrl_O:
2427 // omni completion
2428 ctrl_x_mode = CTRL_X_OMNI;
2429 break;
2430#endif
2431 case 's':
2432 case Ctrl_S:
2433 // complete spelling suggestions
2434 ctrl_x_mode = CTRL_X_SPELL;
2435#ifdef FEAT_SPELL
2436 ++emsg_off; // Avoid getting the E756 error twice.
2437 spell_back_to_badword();
2438 --emsg_off;
2439#endif
2440 break;
2441 case Ctrl_RSB:
2442 // complete tag names
2443 ctrl_x_mode = CTRL_X_TAGS;
2444 break;
2445#ifdef FEAT_FIND_ID
2446 case Ctrl_I:
2447 case K_S_TAB:
2448 // complete keywords from included files
2449 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2450 break;
2451 case Ctrl_D:
2452 // complete definitions from included files
2453 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2454 break;
2455#endif
2456 case Ctrl_V:
2457 case Ctrl_Q:
2458 // complete vim commands
2459 ctrl_x_mode = CTRL_X_CMDLINE;
2460 break;
2461 case Ctrl_Z:
2462 // stop completion
2463 ctrl_x_mode = CTRL_X_NORMAL;
2464 edit_submode = NULL;
2465 showmode();
2466 retval = TRUE;
2467 break;
2468 case Ctrl_P:
2469 case Ctrl_N:
2470 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2471 // just started ^X mode, or there were enough ^X's to cancel
2472 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2473 // do normal expansion when interrupting a different mode (say
2474 // ^X^F^X^P or ^P^X^X^P, see below)
2475 // nothing changes if interrupting mode 0, (eg, the flag
2476 // doesn't change when going to ADDING mode -- Acevedo
2477 if (!(compl_cont_status & CONT_INTRPT))
2478 compl_cont_status |= CONT_LOCAL;
2479 else if (compl_cont_mode != 0)
2480 compl_cont_status &= ~CONT_LOCAL;
2481 // FALLTHROUGH
2482 default:
2483 // If we have typed at least 2 ^X's... for modes != 0, we set
2484 // compl_cont_status = 0 (eg, as if we had just started ^X
2485 // mode).
2486 // For mode 0, we set "compl_cont_mode" to an impossible
2487 // value, in both cases ^X^X can be used to restart the same
2488 // mode (avoiding ADDING mode).
2489 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2490 // 'complete' and local ^P expansions respectively.
2491 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2492 // mode -- Acevedo
2493 if (c == Ctrl_X)
2494 {
2495 if (compl_cont_mode != 0)
2496 compl_cont_status = 0;
2497 else
2498 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2499 }
2500 ctrl_x_mode = CTRL_X_NORMAL;
2501 edit_submode = NULL;
2502 showmode();
2503 break;
2504 }
2505
2506 return retval;
2507}
2508
2509/*
glepnir1c5a1202024-12-04 20:27:34 +01002510 * Trigger CompleteDone event and adds relevant information to v:event
2511 */
2512 static void
2513trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2514{
2515#if defined(FEAT_EVAL)
2516 save_v_event_T save_v_event;
2517 dict_T *v_event = get_v_event(&save_v_event);
2518 char_u *mode_str = NULL;
2519
2520 mode = mode & ~CTRL_X_WANT_IDENT;
2521 if (ctrl_x_mode_names[mode])
2522 mode_str = (char_u *)ctrl_x_mode_names[mode];
2523
2524 (void)dict_add_string(v_event, "complete_word",
2525 word == NULL ? (char_u *)"" : word);
2526 (void)dict_add_string(v_event, "complete_type",
2527 mode_str != NULL ? mode_str : (char_u *)"");
2528
2529 dict_set_items_ro(v_event);
2530#endif
2531 ins_apply_autocmds(EVENT_COMPLETEDONE);
2532
2533#if defined(FEAT_EVAL)
2534 restore_v_event(v_event, &save_v_event);
2535#endif
2536}
2537
2538/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002539 * Stop insert completion mode
2540 */
2541 static int
2542ins_compl_stop(int c, int prev_mode, int retval)
2543{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002544 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002545 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002546
2547 // Get here when we have finished typing a sequence of ^N and
2548 // ^P or other completion characters in CTRL-X mode. Free up
2549 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002550 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002551 {
glepnir40891ba2025-02-10 22:18:00 +01002552 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002553
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002554 // If any of the original typed text has been changed, eg when
2555 // ignorecase is set, we must add back-spaces to the redo
2556 // buffer. We add as few as necessary to delete just the part
2557 // of the original text that has changed.
2558 // When using the longest match, edited the match or used
2559 // CTRL-E then don't use the current match.
2560 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002561 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002562 ins_compl_fixRedoBufForLeader(ptr);
2563 }
2564
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002565 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002566
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002567 // When completing whole lines: fix indent for 'cindent'.
2568 // Otherwise, break line if it's too long.
2569 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2570 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002571 // re-indent the current line
2572 if (want_cindent)
2573 {
2574 do_c_expr_indent();
2575 want_cindent = FALSE; // don't do it again
2576 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002577 }
2578 else
2579 {
2580 int prev_col = curwin->w_cursor.col;
2581
2582 // put the cursor on the last char, for 'tw' formatting
2583 if (prev_col > 0)
2584 dec_cursor();
2585 // only format when something was inserted
2586 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2587 insertchar(NUL, 0, -1);
2588 if (prev_col > 0
2589 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2590 inc_cursor();
2591 }
2592
2593 // If the popup menu is displayed pressing CTRL-Y means accepting
2594 // the selection without inserting anything. When
2595 // compl_enter_selects is set the Enter key does the same.
2596 if ((c == Ctrl_Y || (compl_enter_selects
2597 && (c == CAR || c == K_KENTER || c == NL)))
2598 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002599 {
2600 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002601 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002602 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002603
2604 // CTRL-E means completion is Ended, go back to the typed text.
2605 // but only do this, if the Popup is still visible
2606 if (c == Ctrl_E)
2607 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002608 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002609 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002610
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002611 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002612 if (compl_leader.string != NULL)
2613 {
2614 p = compl_leader.string;
2615 plen = compl_leader.length;
2616 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002617 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002618 {
2619 p = compl_orig_text.string;
2620 plen = compl_orig_text.length;
2621 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002622 if (p != NULL)
2623 {
2624 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002625
John Marriott5e6ea922024-11-23 14:01:57 +01002626 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002627 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002628 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002629 retval = TRUE;
2630 }
2631
glepnir001c26c2025-02-02 09:36:22 +01002632 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2633 ins_compl_delete();
2634
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002635 auto_format(FALSE, TRUE);
2636
2637 // Trigger the CompleteDonePre event to give scripts a chance to
2638 // act upon the completion before clearing the info, and restore
2639 // ctrl_x_mode, so that complete_info() can be used.
2640 ctrl_x_mode = prev_mode;
2641 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2642
2643 ins_compl_free();
2644 compl_started = FALSE;
2645 compl_matches = 0;
2646 if (!shortmess(SHM_COMPLETIONMENU))
2647 msg_clr_cmdline(); // necessary for "noshowmode"
2648 ctrl_x_mode = CTRL_X_NORMAL;
2649 compl_enter_selects = FALSE;
2650 if (edit_submode != NULL)
2651 {
2652 edit_submode = NULL;
2653 showmode();
2654 }
2655
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002656 if (c == Ctrl_C && cmdwin_type != 0)
2657 // Avoid the popup menu remains displayed when leaving the
2658 // command line window.
2659 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002660 // Indent now if a key was typed that is in 'cinkeys'.
2661 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2662 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002663 // Trigger the CompleteDone event to give scripts a chance to act
2664 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002665 trigger_complete_done_event(prev_mode, word);
2666 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002667
2668 return retval;
2669}
2670
2671/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002672 * Prepare for Insert mode completion, or stop it.
2673 * Called just after typing a character in Insert mode.
2674 * Returns TRUE when the character is not to be inserted;
2675 */
2676 int
2677ins_compl_prep(int c)
2678{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002680 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002681
2682 // Forget any previous 'special' messages if this is actually
2683 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2684 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2685 edit_submode_extra = NULL;
2686
zeertzjq440d4cb2023-03-02 17:51:32 +00002687 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002688 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002689 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002690 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002691 return retval;
2692
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002693#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002694 // Ignore mouse events in a popup window
2695 if (is_mouse_key(c))
2696 {
2697 // Ignore drag and release events, the position does not need to be in
2698 // the popup and it may have just closed.
2699 if (c == K_LEFTRELEASE
2700 || c == K_LEFTRELEASE_NM
2701 || c == K_MIDDLERELEASE
2702 || c == K_RIGHTRELEASE
2703 || c == K_X1RELEASE
2704 || c == K_X2RELEASE
2705 || c == K_LEFTDRAG
2706 || c == K_MIDDLEDRAG
2707 || c == K_RIGHTDRAG
2708 || c == K_X1DRAG
2709 || c == K_X2DRAG)
2710 return retval;
2711 if (popup_visible)
2712 {
2713 int row = mouse_row;
2714 int col = mouse_col;
2715 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2716
2717 if (wp != NULL && WIN_IS_POPUP(wp))
2718 return retval;
2719 }
2720 }
2721#endif
2722
zeertzjqdca29d92021-08-31 19:12:51 +02002723 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2724 {
2725 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2726 || !vim_is_ctrl_x_key(c))
2727 {
2728 // Not starting another completion mode.
2729 ctrl_x_mode = CTRL_X_CMDLINE;
2730
2731 // CTRL-X CTRL-Z should stop completion without inserting anything
2732 if (c == Ctrl_Z)
2733 retval = TRUE;
2734 }
2735 else
2736 {
2737 ctrl_x_mode = CTRL_X_CMDLINE;
2738
2739 // Other CTRL-X keys first stop completion, then start another
2740 // completion mode.
2741 ins_compl_prep(' ');
2742 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2743 }
2744 }
2745
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002746 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002747 if (ctrl_x_mode_not_defined_yet()
2748 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002749 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002750 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002751 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 }
2753
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002754 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2756 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002757 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002758 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002759 {
2760 // We're already in CTRL-X mode, do we stay in it?
2761 if (!vim_is_ctrl_x_key(c))
2762 {
glepnir40891ba2025-02-10 22:18:00 +01002763 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002764 edit_submode = NULL;
2765 }
2766 showmode();
2767 }
2768
2769 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2770 {
2771 // Show error message from attempted keyword completion (probably
2772 // 'Pattern not found') until another key is hit, then go back to
2773 // showing what mode we are in.
2774 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002775 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002776 && c != Ctrl_R && !ins_compl_pum_key(c))
2777 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002778 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002779 }
2780 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2781 // Trigger the CompleteDone event to give scripts a chance to act
2782 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002783 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002784
LemonBoy2bf52dd2022-04-09 18:17:34 +01002785 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002786
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002787 // reset continue_* if we left expansion-mode, if we stay they'll be
2788 // (re)set properly in ins_complete()
2789 if (!vim_is_ctrl_x_key(c))
2790 {
2791 compl_cont_status = 0;
2792 compl_cont_mode = 0;
2793 }
2794
2795 return retval;
2796}
2797
2798/*
2799 * Fix the redo buffer for the completion leader replacing some of the typed
2800 * text. This inserts backspaces and appends the changed text.
2801 * "ptr" is the known leader text or NUL.
2802 */
2803 static void
2804ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2805{
John Marriott5e6ea922024-11-23 14:01:57 +01002806 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002807 char_u *p;
2808 char_u *ptr = ptr_arg;
2809
2810 if (ptr == NULL)
2811 {
John Marriott5e6ea922024-11-23 14:01:57 +01002812 if (compl_leader.string != NULL)
2813 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002814 else
2815 return; // nothing to do
2816 }
John Marriott5e6ea922024-11-23 14:01:57 +01002817 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002818 {
John Marriott5e6ea922024-11-23 14:01:57 +01002819 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002820 // Find length of common prefix between original text and new completion
2821 while (p[len] != NUL && p[len] == ptr[len])
2822 len++;
2823 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002824 if (len > 0)
2825 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002826 // Add backspace characters for each remaining character in
2827 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002828 for (p += len; *p != NUL; MB_PTR_ADV(p))
2829 AppendCharToRedobuff(K_BS);
2830 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002831 if (ptr != NULL)
2832 AppendToRedobuffLit(ptr + len, -1);
2833}
2834
2835/*
2836 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2837 * (depending on flag) starting from buf and looking for a non-scanned
2838 * buffer (other than curbuf). curbuf is special, if it is called with
2839 * buf=curbuf then it has to be the first call for a given flag/expansion.
2840 *
2841 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2842 */
2843 static buf_T *
2844ins_compl_next_buf(buf_T *buf, int flag)
2845{
2846 static win_T *wp = NULL;
glepnir40891ba2025-02-10 22:18:00 +01002847 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002848
2849 if (flag == 'w') // just windows
2850 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002851 if (buf == curbuf || !win_valid(wp))
2852 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002853 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01002854
2855 while (TRUE)
2856 {
2857 // Move to next window (wrap to first window if at the end)
2858 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2859 // Break if we're back at start or found an unscanned buffer
2860 if (wp == curwin || !wp->w_buffer->b_scanned)
2861 break;
2862 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863 buf = wp->w_buffer;
2864 }
2865 else
glepnir40891ba2025-02-10 22:18:00 +01002866 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002867 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2868 // (unlisted buffers)
2869 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01002870 while (TRUE)
2871 {
2872 // Move to next buffer (wrap to first buffer if at the end)
2873 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2874 // Break if we're back at start buffer
2875 if (buf == curbuf)
2876 break;
2877
2878 // Check buffer conditions based on flag
2879 if (flag == 'U')
2880 skip_buffer = buf->b_p_bl;
2881 else
2882 skip_buffer = !buf->b_p_bl ||
2883 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2884
2885 // Break if we found a buffer that matches our criteria
2886 if (!skip_buffer && !buf->b_scanned)
2887 break;
2888 }
2889 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002890 return buf;
2891}
2892
2893#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002894
2895# ifdef FEAT_EVAL
2896static callback_T cfu_cb; // 'completefunc' callback function
2897static callback_T ofu_cb; // 'omnifunc' callback function
2898static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2899# endif
2900
2901/*
2902 * Copy a global callback function to a buffer local callback.
2903 */
2904 static void
2905copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2906{
2907 free_callback(bufcb);
2908 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2909 copy_callback(bufcb, globcb);
2910}
2911
2912/*
2913 * Parse the 'completefunc' option value and set the callback function.
2914 * Invoked when the 'completefunc' option is set. The option value can be a
2915 * name of a function (string), or function(<name>) or funcref(<name>) or a
2916 * lambda expression.
2917 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002918 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002919did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002920{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002921 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2922 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002923
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002924 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002925
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002926 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002927}
2928
2929/*
2930 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002931 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002932 */
2933 void
2934set_buflocal_cfu_callback(buf_T *buf UNUSED)
2935{
2936# ifdef FEAT_EVAL
2937 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2938# endif
2939}
2940
2941/*
2942 * Parse the 'omnifunc' option value and set the callback function.
2943 * Invoked when the 'omnifunc' option is set. The option value can be a
2944 * name of a function (string), or function(<name>) or funcref(<name>) or a
2945 * lambda expression.
2946 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002947 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002948did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002949{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002950 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2951 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002952
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002953 set_buflocal_ofu_callback(curbuf);
2954 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002955}
2956
2957/*
2958 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002959 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002960 */
2961 void
2962set_buflocal_ofu_callback(buf_T *buf UNUSED)
2963{
2964# ifdef FEAT_EVAL
2965 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2966# endif
2967}
2968
2969/*
2970 * Parse the 'thesaurusfunc' option value and set the callback function.
2971 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2972 * name of a function (string), or function(<name>) or funcref(<name>) or a
2973 * lambda expression.
2974 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002975 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002976did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002977{
2978 int retval;
2979
zeertzjq6eda2692024-11-03 09:23:33 +01002980 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002981 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002982 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2983 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002984 else
2985 {
2986 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002987 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002988 // when using :set, free the local callback
2989 if (!(args->os_flags & OPT_GLOBAL))
2990 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002991 }
2992
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002993 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002994}
2995
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002996/*
2997 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002998 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002999 */
3000 int
3001set_ref_in_insexpand_funcs(int copyID)
3002{
3003 int abort = FALSE;
3004
3005 abort = set_ref_in_callback(&cfu_cb, copyID);
3006 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
3007 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
3008
3009 return abort;
3010}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003011
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003012/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003013 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003014 */
3015 static char_u *
3016get_complete_funcname(int type)
3017{
3018 switch (type)
3019 {
3020 case CTRL_X_FUNCTION:
3021 return curbuf->b_p_cfu;
3022 case CTRL_X_OMNI:
3023 return curbuf->b_p_ofu;
3024 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003025 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003026 default:
3027 return (char_u *)"";
3028 }
3029}
3030
3031/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003032 * Get the callback to use for insert mode completion.
3033 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003034 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003035get_insert_callback(int type)
3036{
3037 if (type == CTRL_X_FUNCTION)
3038 return &curbuf->b_cfu_cb;
3039 if (type == CTRL_X_OMNI)
3040 return &curbuf->b_ofu_cb;
3041 // CTRL_X_THESAURUS
3042 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
3043}
3044
3045/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003046 * Execute user defined complete function 'completefunc', 'omnifunc' or
3047 * 'thesaurusfunc', and get matches in "matches".
3048 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003049 */
3050 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003051expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003052{
3053 list_T *matchlist = NULL;
3054 dict_T *matchdict = NULL;
3055 typval_T args[3];
3056 char_u *funcname;
3057 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003058 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003059 typval_T rettv;
3060 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003061 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003062
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003063 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003064 if (*funcname == NUL)
3065 return;
3066
3067 // Call 'completefunc' to obtain the list of matches.
3068 args[0].v_type = VAR_NUMBER;
3069 args[0].vval.v_number = 0;
3070 args[1].v_type = VAR_STRING;
3071 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
3072 args[2].v_type = VAR_UNKNOWN;
3073
3074 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01003075 // Lock the text to avoid weird things from happening. Also disallow
3076 // switching to another window, it should not be needed and may end up in
3077 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01003078 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003079
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003080 cb = get_insert_callback(type);
3081 retval = call_callback(cb, 0, &rettv, 2, args);
3082
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003083 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003084 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003085 {
3086 switch (rettv.v_type)
3087 {
3088 case VAR_LIST:
3089 matchlist = rettv.vval.v_list;
3090 break;
3091 case VAR_DICT:
3092 matchdict = rettv.vval.v_dict;
3093 break;
3094 case VAR_SPECIAL:
3095 if (rettv.vval.v_number == VVAL_NONE)
3096 compl_opt_suppress_empty = TRUE;
3097 // FALLTHROUGH
3098 default:
3099 // TODO: Give error message?
3100 clear_tv(&rettv);
3101 break;
3102 }
3103 }
zeertzjqcfe45652022-05-27 17:26:55 +01003104 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003105
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003106 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003107 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108 validate_cursor();
3109 if (!EQUAL_POS(curwin->w_cursor, pos))
3110 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003111 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003112 goto theend;
3113 }
3114
3115 if (matchlist != NULL)
3116 ins_compl_add_list(matchlist);
3117 else if (matchdict != NULL)
3118 ins_compl_add_dict(matchdict);
3119
3120theend:
3121 // Restore State, it might have been changed.
3122 State = save_State;
3123
3124 if (matchdict != NULL)
3125 dict_unref(matchdict);
3126 if (matchlist != NULL)
3127 list_unref(matchlist);
3128}
3129#endif // FEAT_COMPL_FUNC
3130
3131#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003132
3133 static inline int
3134get_user_highlight_attr(char_u *hlname)
3135{
3136 if (hlname != NULL && *hlname != NUL)
3137 return syn_name2attr(hlname);
3138 return -1;
3139}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003140/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003141 * Add a match to the list of matches from a typeval_T.
3142 * If the given string is already in the list of completions, then return
3143 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3144 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003145 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003146 */
3147 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003148ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003149{
3150 char_u *word;
3151 int dup = FALSE;
3152 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003153 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003154 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003155 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003156 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003157 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003158 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003159 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003160
Bram Moolenaar08928322020-01-04 14:32:48 +01003161 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003162 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3163 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003164 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3165 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3166 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3167 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3168 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003169
glepnir0fe17f82024-10-08 22:26:44 +02003170 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003171 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003172
3173 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003174 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003175
Bram Moolenaard61efa52022-07-23 09:52:04 +01003176 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3177 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3178 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003179 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003180 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3181 dup = dict_get_number(tv->vval.v_dict, "dup");
3182 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3183 empty = dict_get_number(tv->vval.v_dict, "empty");
3184 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3185 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003186 flags |= CP_EQUAL;
3187 }
3188 else
3189 {
3190 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003191 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003192 }
3193 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003194 {
3195 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003196 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003197 }
glepnir508e7852024-07-25 21:39:08 +02003198 status = ins_compl_add(word, -1, NULL, cptext,
glepnirf31cfa22025-03-06 21:59:13 +01003199 &user_data, dir, flags, dup, user_hl, 0);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003200 if (status != OK)
3201 clear_tv(&user_data);
3202 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003203}
3204
3205/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003206 * Add completions from a list.
3207 */
3208 static void
3209ins_compl_add_list(list_T *list)
3210{
3211 listitem_T *li;
3212 int dir = compl_direction;
3213
3214 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003215 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003216 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003217 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003218 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003219 // if dir was BACKWARD then honor it just once
3220 dir = FORWARD;
3221 else if (did_emsg)
3222 break;
3223 }
3224}
3225
3226/*
3227 * Add completions from a dict.
3228 */
3229 static void
3230ins_compl_add_dict(dict_T *dict)
3231{
3232 dictitem_T *di_refresh;
3233 dictitem_T *di_words;
3234
3235 // Check for optional "refresh" item.
3236 compl_opt_refresh_always = FALSE;
3237 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3238 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3239 {
3240 char_u *v = di_refresh->di_tv.vval.v_string;
3241
3242 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3243 compl_opt_refresh_always = TRUE;
3244 }
3245
3246 // Add completions from a "words" list.
3247 di_words = dict_find(dict, (char_u *)"words", 5);
3248 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3249 ins_compl_add_list(di_words->di_tv.vval.v_list);
3250}
3251
3252/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003253 * Start completion for the complete() function.
3254 * "startcol" is where the matched text starts (1 is first column).
3255 * "list" is the list of matches.
3256 */
3257 static void
3258set_completion(colnr_T startcol, list_T *list)
3259{
3260 int save_w_wrow = curwin->w_wrow;
3261 int save_w_leftcol = curwin->w_leftcol;
3262 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003263 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003264 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3265 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3266 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003267
3268 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003269 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003270 ins_compl_prep(' ');
3271 ins_compl_clear();
3272 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003273 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003274
3275 compl_direction = FORWARD;
3276 if (startcol > curwin->w_cursor.col)
3277 startcol = curwin->w_cursor.col;
3278 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003279 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003280 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3281 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003282 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3283 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003284 if (p_ic)
3285 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003286 if (compl_orig_text.string == NULL)
3287 {
3288 compl_orig_text.length = 0;
3289 return;
3290 }
3291 compl_orig_text.length = (size_t)compl_length;
3292 if (ins_compl_add(compl_orig_text.string,
3293 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
glepnirf31cfa22025-03-06 21:59:13 +01003294 flags | CP_FAST, FALSE, NULL, 0) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003295 return;
3296
3297 ctrl_x_mode = CTRL_X_EVAL;
3298
3299 ins_compl_add_list(list);
3300 compl_matches = ins_compl_make_cyclic();
3301 compl_started = TRUE;
3302 compl_used_match = TRUE;
3303 compl_cont_status = 0;
3304
3305 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003306 int no_select = compl_no_select || compl_longest;
3307 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003308 {
3309 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003310 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003311 // Down/Up has no real effect.
3312 ins_complete(K_UP, FALSE);
3313 }
3314 else
3315 ins_complete(Ctrl_N, FALSE);
3316 compl_enter_selects = compl_no_insert;
3317
3318 // Lazily show the popup menu, unless we got interrupted.
3319 if (!compl_interrupted)
3320 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003321 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003322 out_flush();
3323}
3324
3325/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003326 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003327 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003328 void
3329f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003330{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003331 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003332
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003333 if (in_vim9script()
3334 && (check_for_number_arg(argvars, 0) == FAIL
3335 || check_for_list_arg(argvars, 1) == FAIL))
3336 return;
3337
Bram Moolenaar24959102022-05-07 20:01:16 +01003338 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003339 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003340 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003341 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003342 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003343
3344 // Check for undo allowed here, because if something was already inserted
3345 // the line was already saved for undo and this check isn't done.
3346 if (!undo_allowed())
3347 return;
3348
Bram Moolenaard83392a2022-09-01 12:22:46 +01003349 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003350 {
3351 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3352 if (startcol > 0)
3353 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003354 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003355}
3356
3357/*
3358 * "complete_add()" function
3359 */
3360 void
3361f_complete_add(typval_T *argvars, typval_T *rettv)
3362{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003363 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003364 return;
3365
Bram Moolenaar440cf092021-04-03 20:13:30 +02003366 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003367}
3368
3369/*
3370 * "complete_check()" function
3371 */
3372 void
3373f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3374{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003375 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003376 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003377
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003378 ins_compl_check_keys(0, TRUE);
3379 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003380
3381 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003382}
3383
3384/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003385 * Return Insert completion mode name string
3386 */
3387 static char_u *
3388ins_compl_mode(void)
3389{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003390 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003391 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3392
3393 return (char_u *)"";
3394}
3395
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003396/*
3397 * Assign the sequence number to all the completion matches which don't have
3398 * one assigned yet.
3399 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003400 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003401ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003402{
3403 int number = 0;
3404 compl_T *match;
3405
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003406 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003407 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003408 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003409 // This should normally succeed already at the first loop
3410 // cycle, so it's fast!
3411 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003412 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003413 if (match->cp_number != -1)
3414 {
3415 number = match->cp_number;
3416 break;
3417 }
3418 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003419 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003420 for (match = match->cp_next;
3421 match != NULL && match->cp_number == -1;
3422 match = match->cp_next)
3423 match->cp_number = ++number;
3424 }
3425 else // BACKWARD
3426 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003427 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003428 // number. This should normally succeed already at the
3429 // first loop cycle, so it's fast!
3430 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003431 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003432 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003433 if (match->cp_number != -1)
3434 {
3435 number = match->cp_number;
3436 break;
3437 }
glepnir40891ba2025-02-10 22:18:00 +01003438 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003439 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003440 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003441 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003442 for (match = match->cp_prev; match
3443 && match->cp_number == -1;
3444 match = match->cp_prev)
3445 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003446 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003447 }
3448}
3449
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003450/*
glepnir037b0282025-01-16 14:37:44 +01003451 * Fill the dict of complete_info
3452 */
3453 static void
3454fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3455{
3456 dict_add_string(di, "word", match->cp_str.string);
3457 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3458 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3459 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3460 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3461 if (add_match)
3462 dict_add_bool(di, "match", match->cp_in_match_array);
3463 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3464 // Add an empty string for backwards compatibility
3465 dict_add_string(di, "user_data", (char_u *)"");
3466 else
3467 dict_add_tv(di, "user_data", &match->cp_user_data);
3468}
3469
3470/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003471 * Get complete information
3472 */
3473 static void
3474get_complete_info(list_T *what_list, dict_T *retdict)
3475{
3476 int ret = OK;
3477 listitem_T *item;
3478#define CI_WHAT_MODE 0x01
3479#define CI_WHAT_PUM_VISIBLE 0x02
3480#define CI_WHAT_ITEMS 0x04
3481#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003482#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003483#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003484#define CI_WHAT_ALL 0xff
3485 int what_flag;
3486
3487 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003488 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003489 else
3490 {
3491 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003492 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003493 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003494 {
3495 char_u *what = tv_get_string(&item->li_tv);
3496
3497 if (STRCMP(what, "mode") == 0)
3498 what_flag |= CI_WHAT_MODE;
3499 else if (STRCMP(what, "pum_visible") == 0)
3500 what_flag |= CI_WHAT_PUM_VISIBLE;
3501 else if (STRCMP(what, "items") == 0)
3502 what_flag |= CI_WHAT_ITEMS;
3503 else if (STRCMP(what, "selected") == 0)
3504 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003505 else if (STRCMP(what, "completed") == 0)
3506 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003507 else if (STRCMP(what, "matches") == 0)
3508 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003509 }
3510 }
3511
3512 if (ret == OK && (what_flag & CI_WHAT_MODE))
3513 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3514
3515 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3516 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3517
glepnir037b0282025-01-16 14:37:44 +01003518 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3519 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003520 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003521 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003522 dict_T *di;
3523 compl_T *match;
3524 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003525 int has_items = what_flag & CI_WHAT_ITEMS;
3526 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003527 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003528
glepnird4088ed2024-12-31 10:55:22 +01003529 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003530 {
3531 li = list_alloc();
3532 if (li == NULL)
3533 return;
glepnird4088ed2024-12-31 10:55:22 +01003534 ret = dict_add_list(retdict, (has_matches && !has_items)
3535 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003536 }
3537 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3538 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3539 ins_compl_update_sequence_numbers();
3540 if (ret == OK && compl_first_match != NULL)
3541 {
3542 int list_idx = 0;
3543 match = compl_first_match;
3544 do
3545 {
3546 if (!match_at_original_text(match))
3547 {
glepnird4088ed2024-12-31 10:55:22 +01003548 if (has_items
3549 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003550 {
3551 di = dict_alloc();
3552 if (di == NULL)
3553 return;
3554 ret = list_append_dict(li, di);
3555 if (ret != OK)
3556 return;
glepnir037b0282025-01-16 14:37:44 +01003557 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003558 }
glepnird4088ed2024-12-31 10:55:22 +01003559 if (compl_curr_match != NULL
3560 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003561 selected_idx = list_idx;
3562 list_idx += 1;
3563 }
3564 match = match->cp_next;
3565 }
3566 while (match != NULL && !is_first_match(match));
3567 }
3568 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3569 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003570
glepnir037b0282025-01-16 14:37:44 +01003571 if (ret == OK && selected_idx != -1 && has_completed)
3572 {
3573 di = dict_alloc();
3574 if (di == NULL)
3575 return;
3576 fill_complete_info_dict(di, compl_curr_match, FALSE);
3577 ret = dict_add_dict(retdict, "completed", di);
3578 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003579 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003580}
3581
3582/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003583 * "complete_info()" function
3584 */
3585 void
3586f_complete_info(typval_T *argvars, typval_T *rettv)
3587{
3588 list_T *what_list = NULL;
3589
Bram Moolenaar93a10962022-06-16 11:42:09 +01003590 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003591 return;
3592
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003593 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3594 return;
3595
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003596 if (argvars[0].v_type != VAR_UNKNOWN)
3597 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003598 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003599 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003600 what_list = argvars[0].vval.v_list;
3601 }
3602 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003603}
3604#endif
3605
3606/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003607 * Returns TRUE when using a user-defined function for thesaurus completion.
3608 */
3609 static int
3610thesaurus_func_complete(int type UNUSED)
3611{
3612#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003613 return type == CTRL_X_THESAURUS
3614 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003615#else
3616 return FALSE;
3617#endif
3618}
3619
3620/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003621 * Return value of process_next_cpt_value()
3622 */
3623enum
3624{
3625 INS_COMPL_CPT_OK = 1,
3626 INS_COMPL_CPT_CONT,
3627 INS_COMPL_CPT_END
3628};
3629
3630/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003631 * state information used for getting the next set of insert completion
3632 * matches.
3633 */
3634typedef struct
3635{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003636 char_u *e_cpt_copy; // copy of 'complete'
3637 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003638 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003639 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003640 pos_T prev_match_pos; // previous match position
3641 int set_match_pos; // save first_match_pos/last_match_pos
3642 pos_T first_match_pos; // first match position
3643 pos_T last_match_pos; // last match position
3644 int found_all; // found all matches of a certain type.
3645 char_u *dict; // dictionary file to search
3646 int dict_f; // "dict" is an exact file name or not
3647} ins_compl_next_state_T;
3648
3649/*
3650 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003651 *
3652 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003653 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003655 * st->found_all - all matches of this type are found
3656 * st->ins_buf - search for completions in this buffer
3657 * st->first_match_pos - position of the first completion match
3658 * st->last_match_pos - position of the last completion match
3659 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003660 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003661 * st->dict - name of the dictionary or thesaurus file to search
3662 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003663 *
3664 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003665 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3666 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003667 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668 */
3669 static int
3670process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003671 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003673 pos_T *start_match_pos,
3674 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 int compl_type = -1;
3677 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003678
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003679 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003680
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003681 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3682 st->e_cpt++;
3683
3684 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003685 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003686 st->ins_buf = curbuf;
3687 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003688 // Move the cursor back one character so that ^N can match the
3689 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003690 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691 {
3692 // Move the cursor to after the last character in the
3693 // buffer, so that word at start of buffer is found
3694 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003695 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003696 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003697 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003698 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003699 compl_type = 0;
3700
3701 // Remember the first match so that the loop stops when we
3702 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003703 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003704 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003705 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003706 && (st->ins_buf = ins_compl_next_buf(
3707 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708 {
3709 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003710 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003711 {
3712 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003713 st->first_match_pos.col = st->last_match_pos.col = 0;
3714 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3715 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003716 compl_type = 0;
3717 }
3718 else // unloaded buffer, scan like dictionary
3719 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003720 st->found_all = TRUE;
3721 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003722 {
3723 status = INS_COMPL_CPT_CONT;
3724 goto done;
3725 }
3726 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003727 st->dict = st->ins_buf->b_fname;
3728 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003729 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003730 if (!shortmess(SHM_COMPLETIONSCAN))
3731 {
3732 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3733 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3734 st->ins_buf->b_fname == NULL
3735 ? buf_spname(st->ins_buf)
3736 : st->ins_buf->b_sfname == NULL
3737 ? st->ins_buf->b_fname
3738 : st->ins_buf->b_sfname);
3739 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3740 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003741 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003742 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003743 status = INS_COMPL_CPT_END;
3744 else
3745 {
3746 if (ctrl_x_mode_line_or_eval())
3747 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003748 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003749 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003750 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003751 compl_type = CTRL_X_DICTIONARY;
3752 else
3753 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003754 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003755 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003756 st->dict = st->e_cpt;
3757 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003758 }
3759 }
3760#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003761 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003762 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003763 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003764 compl_type = CTRL_X_PATH_DEFINES;
3765#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003766 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003767 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003768 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003769 if (!shortmess(SHM_COMPLETIONSCAN))
3770 {
3771 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3772 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3773 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3774 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003775 }
3776 else
3777 compl_type = -1;
3778
3779 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003780 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003781
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003782 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003783 if (compl_type == -1)
3784 status = INS_COMPL_CPT_CONT;
3785 }
3786
3787done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003788 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003789 return status;
3790}
3791
3792#ifdef FEAT_FIND_ID
3793/*
3794 * Get the next set of identifiers or defines matching "compl_pattern" in
3795 * included files.
3796 */
3797 static void
3798get_next_include_file_completion(int compl_type)
3799{
John Marriott5e6ea922024-11-23 14:01:57 +01003800 find_pattern_in_path(compl_pattern.string, compl_direction,
3801 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003802 (compl_type == CTRL_X_PATH_DEFINES
3803 && !(compl_cont_status & CONT_SOL))
3804 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003805 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003806}
3807#endif
3808
3809/*
3810 * Get the next set of words matching "compl_pattern" in dictionary or
3811 * thesaurus files.
3812 */
3813 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003814get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003815{
3816#ifdef FEAT_COMPL_FUNC
3817 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003818 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003819 else
3820#endif
3821 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003822 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003823 : (compl_type == CTRL_X_THESAURUS
3824 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3825 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003826 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003827 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003828 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003829}
3830
3831/*
3832 * Get the next set of tag names matching "compl_pattern".
3833 */
3834 static void
3835get_next_tag_completion(void)
3836{
3837 int save_p_ic;
3838 char_u **matches;
3839 int num_matches;
3840
3841 // set p_ic according to p_ic, p_scs and pat for find_tags().
3842 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003843 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003844
3845 // Find up to TAG_MANY matches. Avoids that an enormous number
3846 // of matches is found when compl_pattern is empty
3847 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003848 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003849 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003850 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003851 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3852 ins_compl_add_matches(num_matches, matches, p_ic);
3853 g_tag_at_cursor = FALSE;
3854 p_ic = save_p_ic;
3855}
3856
3857/*
glepnir8159fb12024-07-17 20:32:54 +02003858 * Compare function for qsort
3859 */
glepnirf31cfa22025-03-06 21:59:13 +01003860 static int
3861compare_scores(const void *a, const void *b)
glepnir8159fb12024-07-17 20:32:54 +02003862{
3863 int idx_a = *(const int *)a;
3864 int idx_b = *(const int *)b;
3865 int score_a = compl_fuzzy_scores[idx_a];
3866 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003867 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3868 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003869}
3870
3871/*
glepnirf31cfa22025-03-06 21:59:13 +01003872 * insert prefix with redraw
3873 */
3874 static void
3875ins_compl_longest_insert(char_u *prefix)
3876{
3877 ins_compl_delete();
3878 ins_compl_insert_bytes(prefix + get_compl_len(), -1);
3879 ins_redraw(FALSE);
3880}
3881
3882/*
3883 * Calculate the longest common prefix among the best fuzzy matches
3884 * stored in compl_best_matches, and insert it as the longest.
3885 */
3886 static void
3887fuzzy_longest_match(void)
3888{
3889 char_u *prefix = NULL;
3890 int prefix_len = 0;
3891 int i = 0;
3892 int j = 0;
3893 char_u *match_str = NULL;
3894 char_u *prefix_ptr = NULL;
3895 char_u *match_ptr = NULL;
3896 char_u *leader = NULL;
3897 size_t leader_len = 0;
3898 compl_T *compl = NULL;
3899 int more_candidates = FALSE;
3900 compl_T *nn_compl = NULL;
3901
3902 if (compl_num_bests == 0)
3903 return;
3904
3905 nn_compl = compl_first_match->cp_next->cp_next;
3906 if (nn_compl && nn_compl != compl_first_match)
3907 more_candidates = TRUE;
3908
3909 compl = ctrl_x_mode_whole_line() ? compl_first_match
3910 : compl_first_match->cp_next;
3911 if (compl_num_bests == 1)
3912 {
3913 // no more candidates insert the match str
3914 if (!more_candidates)
3915 {
3916 ins_compl_longest_insert(compl->cp_str.string);
3917 compl_num_bests = 0;
3918 }
3919 compl_num_bests = 0;
3920 return;
3921 }
3922
3923 compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *));
3924 if (compl_best_matches == NULL)
3925 return;
3926 while (compl != NULL && i < compl_num_bests)
3927 {
3928 compl_best_matches[i] = compl;
3929 compl = compl->cp_next;
3930 i++;
3931 }
3932
3933 prefix = compl_best_matches[0]->cp_str.string;
zeertzjq4422de62025-03-07 19:06:02 +01003934 prefix_len = (int)compl_best_matches[0]->cp_str.length;
glepnirf31cfa22025-03-06 21:59:13 +01003935
3936 for (i = 1; i < compl_num_bests; i++)
3937 {
3938 match_str = compl_best_matches[i]->cp_str.string;
3939 prefix_ptr = prefix;
3940 match_ptr = match_str;
3941 j = 0;
3942
3943 while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL)
3944 {
3945 if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0)
3946 break;
3947
3948 MB_PTR_ADV(prefix_ptr);
3949 MB_PTR_ADV(match_ptr);
3950 j++;
3951 }
3952
3953 if (j > 0)
3954 prefix_len = j;
3955 }
3956
3957 leader = ins_compl_leader();
zeertzjq4422de62025-03-07 19:06:02 +01003958 leader_len = ins_compl_leader_len();
glepnirf31cfa22025-03-06 21:59:13 +01003959
3960 // skip non-consecutive prefixes
zeertzjq4422de62025-03-07 19:06:02 +01003961 if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0)
glepnirf31cfa22025-03-06 21:59:13 +01003962 goto end;
3963
zeertzjq4422de62025-03-07 19:06:02 +01003964 prefix = vim_strnsave(prefix, prefix_len);
glepnirf31cfa22025-03-06 21:59:13 +01003965 if (prefix != NULL)
3966 {
3967 ins_compl_longest_insert(prefix);
3968 compl_cfc_longest_ins = TRUE;
3969 vim_free(prefix);
3970 }
3971
3972end:
3973 vim_free(compl_best_matches);
3974 compl_best_matches = NULL;
3975 compl_num_bests = 0;
3976}
3977
3978/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003979 * Get the next set of filename matching "compl_pattern".
3980 */
3981 static void
3982get_next_filename_completion(void)
3983{
3984 char_u **matches;
3985 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003986 char_u *ptr;
3987 garray_T fuzzy_indices;
3988 int i;
3989 int score;
3990 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003991 size_t leader_len = ins_compl_leader_len();;
glepnirf31cfa22025-03-06 21:59:13 +01003992 int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0);
glepnir8159fb12024-07-17 20:32:54 +02003993 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003994 char_u *last_sep = NULL;
glepnirf31cfa22025-03-06 21:59:13 +01003995 int need_collect_bests = in_fuzzy_collect && compl_get_longest;
3996 int max_score = 0;
3997 int current_score = 0;
3998 int dir = compl_direction;
glepnir8159fb12024-07-17 20:32:54 +02003999
glepnirb9de1a02024-08-02 19:14:38 +02004000#ifdef BACKSLASH_IN_FILENAME
4001 char pathsep = (curbuf->b_p_csl[0] == 's') ?
4002 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
4003#else
4004 char pathsep = PATHSEP;
4005#endif
4006
glepnirf31cfa22025-03-06 21:59:13 +01004007 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004008 {
glepnirb9de1a02024-08-02 19:14:38 +02004009#ifdef BACKSLASH_IN_FILENAME
4010 if (curbuf->b_p_csl[0] == 's')
4011 {
John Marriott5e6ea922024-11-23 14:01:57 +01004012 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004013 {
4014 if (leader[i] == '\\')
4015 leader[i] = '/';
4016 }
4017 }
4018 else if (curbuf->b_p_csl[0] == 'b')
4019 {
John Marriott5e6ea922024-11-23 14:01:57 +01004020 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004021 {
4022 if (leader[i] == '/')
4023 leader[i] = '\\';
4024 }
4025 }
4026#endif
4027 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02004028 if (last_sep == NULL)
4029 {
4030 // No path separator or separator is the last character,
4031 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01004032 VIM_CLEAR_STRING(compl_pattern);
4033 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
4034 if (compl_pattern.string == NULL)
4035 return;
4036 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02004037 }
4038 else if (*(last_sep + 1) == '\0')
glepnirf31cfa22025-03-06 21:59:13 +01004039 in_fuzzy_collect = FALSE;
glepnir0be03e12024-07-19 16:45:05 +02004040 else
4041 {
4042 // Split leader into path and file parts
4043 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004044 char_u *path_with_wildcard;
4045
4046 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02004047 if (path_with_wildcard != NULL)
4048 {
John Marriott5e6ea922024-11-23 14:01:57 +01004049 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
4050 VIM_CLEAR_STRING(compl_pattern);
4051 compl_pattern.string = path_with_wildcard;
4052 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02004053
4054 // Move leader to the file part
4055 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004056 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02004057 }
4058 }
glepnir8159fb12024-07-17 20:32:54 +02004059 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004060
John Marriott5e6ea922024-11-23 14:01:57 +01004061 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004062 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
4063 return;
4064
4065 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01004066 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004067#ifdef BACKSLASH_IN_FILENAME
4068 if (curbuf->b_p_csl[0] != NUL)
4069 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004070 for (i = 0; i < num_matches; ++i)
4071 {
glepnir8159fb12024-07-17 20:32:54 +02004072 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004073 while (*ptr != NUL)
4074 {
4075 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
4076 *ptr = '/';
4077 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
4078 *ptr = '\\';
4079 ptr += (*mb_ptr2len)(ptr);
4080 }
4081 }
4082 }
4083#endif
glepnir8159fb12024-07-17 20:32:54 +02004084
glepnirf31cfa22025-03-06 21:59:13 +01004085 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004086 {
4087 ga_init2(&fuzzy_indices, sizeof(int), 10);
4088 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
4089
4090 for (i = 0; i < num_matches; i++)
4091 {
4092 ptr = matches[i];
4093 score = fuzzy_match_str(ptr, leader);
4094 if (score > 0)
4095 {
4096 if (ga_grow(&fuzzy_indices, 1) == OK)
4097 {
4098 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
4099 compl_fuzzy_scores[i] = score;
4100 fuzzy_indices.ga_len++;
4101 }
4102 }
4103 }
4104
Christian Brabandt220474d2024-07-20 13:26:44 +02004105 // prevent qsort from deref NULL pointer
4106 if (fuzzy_indices.ga_len > 0)
4107 {
glepnirf31cfa22025-03-06 21:59:13 +01004108 char_u *match = NULL;
Christian Brabandt220474d2024-07-20 13:26:44 +02004109 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
4110 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02004111
Christian Brabandt220474d2024-07-20 13:26:44 +02004112 for (i = 0; i < fuzzy_indices.ga_len; ++i)
glepnirf31cfa22025-03-06 21:59:13 +01004113 {
4114 match = matches[fuzzy_indices_data[i]];
4115 current_score = compl_fuzzy_scores[fuzzy_indices_data[i]];
4116 if (ins_compl_add(match, -1, NULL, NULL, NULL, dir,
4117 CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0),
4118 FALSE, NULL, current_score) == OK)
4119 dir = FORWARD;
4120
4121 if (need_collect_bests)
4122 {
4123 if (i == 0 || current_score == max_score)
4124 {
4125 compl_num_bests++;
4126 max_score = current_score;
4127 }
4128 }
4129 }
glepnir8159fb12024-07-17 20:32:54 +02004130
Christian Brabandt220474d2024-07-20 13:26:44 +02004131 FreeWild(num_matches, matches);
Christian Brabandt220474d2024-07-20 13:26:44 +02004132 }
glepnir6b6280c2024-07-27 16:25:45 +02004133 else if (leader_len > 0)
4134 {
4135 FreeWild(num_matches, matches);
4136 num_matches = 0;
4137 }
Christian Brabandt220474d2024-07-20 13:26:44 +02004138
glepnir8159fb12024-07-17 20:32:54 +02004139 vim_free(compl_fuzzy_scores);
4140 ga_clear(&fuzzy_indices);
glepnirf31cfa22025-03-06 21:59:13 +01004141
4142 if (compl_num_bests > 0 && compl_get_longest)
4143 fuzzy_longest_match();
4144 return;
glepnir8159fb12024-07-17 20:32:54 +02004145 }
4146
glepnir6b6280c2024-07-27 16:25:45 +02004147 if (num_matches > 0)
4148 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004149}
4150
4151/*
4152 * Get the next set of command-line completions matching "compl_pattern".
4153 */
4154 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004155get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004156{
4157 char_u **matches;
4158 int num_matches;
4159
John Marriott5e6ea922024-11-23 14:01:57 +01004160 if (expand_cmdline(&compl_xp, compl_pattern.string,
4161 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004162 ins_compl_add_matches(num_matches, matches, FALSE);
4163}
4164
4165/*
4166 * Get the next set of spell suggestions matching "compl_pattern".
4167 */
4168 static void
4169get_next_spell_completion(linenr_T lnum UNUSED)
4170{
4171#ifdef FEAT_SPELL
4172 char_u **matches;
4173 int num_matches;
4174
John Marriott5e6ea922024-11-23 14:01:57 +01004175 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004176 if (num_matches > 0)
4177 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00004178 else
4179 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004180#endif
4181}
4182
4183/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004184 * Return the next word or line from buffer "ins_buf" at position
4185 * "cur_match_pos" for completion. The length of the match is set in "len".
4186 */
4187 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00004188ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004189 buf_T *ins_buf, // buffer being scanned
4190 pos_T *cur_match_pos, // current match position
4191 int *match_len,
4192 int *cont_s_ipos) // next ^X<> will set initial_pos
4193{
4194 char_u *ptr;
4195 int len;
4196
4197 *match_len = 0;
4198 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
4199 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01004200 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004201 if (ctrl_x_mode_line_or_eval())
4202 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004203 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004204 {
4205 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
4206 return NULL;
4207 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01004208 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004209 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01004210 {
4211 char_u *tmp_ptr = ptr;
4212
4213 ptr = skipwhite(tmp_ptr);
4214 len -= (int)(ptr - tmp_ptr);
4215 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004216 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004217 }
4218 else
4219 {
4220 char_u *tmp_ptr = ptr;
4221
John Marriott5e6ea922024-11-23 14:01:57 +01004222 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004223 {
4224 tmp_ptr += compl_length;
4225 // Skip if already inside a word.
4226 if (vim_iswordp(tmp_ptr))
4227 return NULL;
4228 // Find start of next word.
4229 tmp_ptr = find_word_start(tmp_ptr);
4230 }
4231 // Find end of this word.
4232 tmp_ptr = find_word_end(tmp_ptr);
4233 len = (int)(tmp_ptr - ptr);
4234
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004235 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004236 {
4237 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4238 {
4239 // Try next line, if any. the new word will be
4240 // "join" as if the normal command "J" was used.
4241 // IOSIZE is always greater than
4242 // compl_length, so the next STRNCPY always
4243 // works -- Acevedo
4244 STRNCPY(IObuff, ptr, len);
4245 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4246 tmp_ptr = ptr = skipwhite(ptr);
4247 // Find start of next word.
4248 tmp_ptr = find_word_start(tmp_ptr);
4249 // Find end of next word.
4250 tmp_ptr = find_word_end(tmp_ptr);
4251 if (tmp_ptr > ptr)
4252 {
4253 if (*ptr != ')' && IObuff[len - 1] != TAB)
4254 {
4255 if (IObuff[len - 1] != ' ')
4256 IObuff[len++] = ' ';
4257 // IObuf =~ "\k.* ", thus len >= 2
4258 if (p_js
4259 && (IObuff[len - 2] == '.'
4260 || (vim_strchr(p_cpo, CPO_JOINSP)
4261 == NULL
4262 && (IObuff[len - 2] == '?'
4263 || IObuff[len - 2] == '!'))))
4264 IObuff[len++] = ' ';
4265 }
4266 // copy as much as possible of the new word
4267 if (tmp_ptr - ptr >= IOSIZE - len)
4268 tmp_ptr = ptr + IOSIZE - len - 1;
4269 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4270 len += (int)(tmp_ptr - ptr);
4271 *cont_s_ipos = TRUE;
4272 }
4273 IObuff[len] = NUL;
4274 ptr = IObuff;
4275 }
4276 if (len == compl_length)
4277 return NULL;
4278 }
4279 }
4280
4281 *match_len = len;
4282 return ptr;
4283}
4284
4285/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004286 * Get the next set of words matching "compl_pattern" for default completion(s)
4287 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004288 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4289 * position "st->start_pos" in the "compl_direction" direction. If
4290 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4291 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004292 * Returns OK if a new next match is found, otherwise returns FAIL.
4293 */
4294 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004295get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004296{
4297 int found_new_match = FAIL;
4298 int save_p_scs;
4299 int save_p_ws;
4300 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004301 char_u *ptr = NULL;
4302 int len = 0;
glepnirf31cfa22025-03-06 21:59:13 +01004303 int in_collect = (cfc_has_mode() && compl_length > 0);
glepnir8159fb12024-07-17 20:32:54 +02004304 char_u *leader = ins_compl_leader();
glepnirf31cfa22025-03-06 21:59:13 +01004305 int score = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004306
4307 // If 'infercase' is set, don't use 'smartcase' here
4308 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004309 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004310 p_scs = FALSE;
4311
4312 // Buffers other than curbuf are scanned from the beginning or the
4313 // end but never from the middle, thus setting nowrapscan in this
4314 // buffer is a good idea, on the other hand, we always set
4315 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4316 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004317 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004318 p_ws = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01004319 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004320 p_ws = TRUE;
4321 looped_around = FALSE;
4322 for (;;)
4323 {
4324 int cont_s_ipos = FALSE;
4325
4326 ++msg_silent; // Don't want messages for wrapscan.
4327
glepnirf31cfa22025-03-06 21:59:13 +01004328 if (in_collect)
4329 {
glepnir8159fb12024-07-17 20:32:54 +02004330 found_new_match = search_for_fuzzy_match(st->ins_buf,
4331 st->cur_match_pos, leader, compl_direction,
glepnirf31cfa22025-03-06 21:59:13 +01004332 start_pos, &len, &ptr, &score);
4333 }
4334 // ctrl_x_mode_line_or_eval() || word-wise search that
4335 // has added a word that was at the beginning of the line
4336 else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
4337 found_new_match = search_for_exact_line(st->ins_buf,
4338 st->cur_match_pos, compl_direction, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004339 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004340 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004341 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004342 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004343 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004344 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004345 {
4346 // set "compl_started" even on fail
4347 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004348 st->first_match_pos = *st->cur_match_pos;
4349 st->last_match_pos = *st->cur_match_pos;
4350 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004351 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004352 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4353 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004354 {
4355 found_new_match = FAIL;
4356 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004357 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004358 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4359 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4360 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004361 {
4362 if (looped_around)
4363 found_new_match = FAIL;
4364 else
4365 looped_around = TRUE;
4366 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004367 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004368 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4369 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4370 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004371 {
4372 if (looped_around)
4373 found_new_match = FAIL;
4374 else
4375 looped_around = TRUE;
4376 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004377 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004378 if (found_new_match == FAIL)
4379 break;
4380
4381 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004382 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004383 && start_pos->lnum == st->cur_match_pos->lnum
4384 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004385 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004386
glepnirf31cfa22025-03-06 21:59:13 +01004387 if (!in_collect)
glepnir8159fb12024-07-17 20:32:54 +02004388 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4389 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004390 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004391 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004392
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004393 if (ins_compl_add_infercase(ptr, len, p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01004394 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
4395 0, cont_s_ipos, score) != NOTDONE)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004396 {
glepnirf31cfa22025-03-06 21:59:13 +01004397 if (in_collect && score == compl_first_match->cp_next->cp_score)
4398 compl_num_bests++;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004399 found_new_match = OK;
4400 break;
4401 }
4402 }
4403 p_scs = save_p_scs;
4404 p_ws = save_p_ws;
4405
4406 return found_new_match;
4407}
4408
4409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004410 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004411 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004412 */
4413 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004414get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004415{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004416 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004417
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004418 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004419 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004420 case -1:
4421 break;
4422#ifdef FEAT_FIND_ID
4423 case CTRL_X_PATH_PATTERNS:
4424 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004425 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004426 break;
4427#endif
4428
4429 case CTRL_X_DICTIONARY:
4430 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004431 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4432 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004433 break;
4434
4435 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004436 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004437 break;
4438
4439 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004440 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004441 break;
4442
4443 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004444 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004445 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004446 break;
4447
4448#ifdef FEAT_COMPL_FUNC
4449 case CTRL_X_FUNCTION:
4450 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004451 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004452 break;
4453#endif
4454
4455 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004456 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004457 break;
4458
4459 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004460 found_new_match = get_next_default_completion(st, ini);
4461 if (found_new_match == FAIL && st->ins_buf == curbuf)
4462 st->found_all = TRUE;
4463 }
4464
4465 // check if compl_curr_match has changed, (e.g. other type of
4466 // expansion added something)
4467 if (type != 0 && compl_curr_match != compl_old_match)
4468 found_new_match = OK;
4469
4470 return found_new_match;
4471}
4472
4473/*
4474 * Get the next expansion(s), using "compl_pattern".
4475 * The search starts at position "ini" in curbuf and in the direction
4476 * compl_direction.
4477 * When "compl_started" is FALSE start at that position, otherwise continue
4478 * where we stopped searching before.
4479 * This may return before finding all the matches.
4480 * Return the total number of matches or -1 if still unknown -- Acevedo
4481 */
4482 static int
4483ins_compl_get_exp(pos_T *ini)
4484{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004485 static ins_compl_next_state_T st;
4486 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004487 int i;
4488 int found_new_match;
4489 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004490 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004491
4492 if (!compl_started)
4493 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004494 buf_T *buf;
4495
4496 FOR_ALL_BUFFERS(buf)
4497 buf->b_scanned = 0;
4498 if (!st_cleared)
4499 {
4500 CLEAR_FIELD(st);
4501 st_cleared = TRUE;
4502 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004503 st.found_all = FALSE;
4504 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004505 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004506 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004507 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4508 ? (char_u *)"." : curbuf->b_p_cpt);
4509 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004510 st.last_match_pos = st.first_match_pos = *ini;
4511 }
4512 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4513 st.ins_buf = curbuf; // In case the buffer was wiped out.
4514
4515 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004516 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004517 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004518
4519 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4520 for (;;)
4521 {
4522 found_new_match = FAIL;
4523 st.set_match_pos = FALSE;
4524
4525 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4526 // or if found_all says this entry is done. For ^X^L only use the
4527 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004528 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004529 && (!compl_started || st.found_all))
4530 {
glepnir8159fb12024-07-17 20:32:54 +02004531 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004532
4533 if (status == INS_COMPL_CPT_END)
4534 break;
4535 if (status == INS_COMPL_CPT_CONT)
4536 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004537 }
4538
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004539 // If complete() was called then compl_pattern has been reset. The
4540 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004541 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004542 break;
4543
4544 // get the next set of completion matches
4545 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004546
4547 // break the loop for specialized modes (use 'complete' just for the
4548 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4549 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004550 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4551 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004552 {
4553 if (got_int)
4554 break;
4555 // Fill the popup menu as soon as possible.
4556 if (type != -1)
4557 ins_compl_check_keys(0, FALSE);
4558
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004559 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004560 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4561 break;
4562 compl_started = TRUE;
4563 }
4564 else
4565 {
4566 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004567 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004568 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004569
4570 compl_started = FALSE;
4571 }
4572 }
4573 compl_started = TRUE;
4574
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004575 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004576 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004577 found_new_match = FAIL;
4578
4579 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004580 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004581 && !ctrl_x_mode_line_or_eval()))
4582 i = ins_compl_make_cyclic();
4583
glepnirf31cfa22025-03-06 21:59:13 +01004584 if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0)
4585 fuzzy_longest_match();
4586
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004587 if (compl_old_match != NULL)
4588 {
4589 // If several matches were added (FORWARD) or the search failed and has
4590 // just been made cyclic then we have to move compl_curr_match to the
4591 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004592 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004593 : compl_old_match->cp_prev;
4594 if (compl_curr_match == NULL)
4595 compl_curr_match = compl_old_match;
4596 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004597 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004598
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004599 return i;
4600}
4601
4602/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004603 * Update "compl_shown_match" to the actually shown match, it may differ when
4604 * "compl_leader" is used to omit some of the matches.
4605 */
4606 static void
4607ins_compl_update_shown_match(void)
4608{
4609 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004610 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004611 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004612 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004613 compl_shown_match = compl_shown_match->cp_next;
4614
4615 // If we didn't find it searching forward, and compl_shows_dir is
4616 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004617 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004618 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004619 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004620 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004621 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004622 {
4623 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004624 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004625 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004626 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004627 compl_shown_match = compl_shown_match->cp_prev;
4628 }
4629}
4630
4631/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004632 * Delete the old text being completed.
4633 */
4634 void
4635ins_compl_delete(void)
4636{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004637 // In insert mode: Delete the typed part.
4638 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004639 int col = compl_col + (compl_status_adding() ? compl_length : 0);
John Marriottf4b36412025-02-23 09:09:59 +01004640 string_T remaining = {NULL, 0};
glepnir76bdb822025-02-08 19:04:51 +01004641 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01004642 int has_preinsert = ins_compl_preinsert_effect();
4643 if (has_preinsert)
4644 {
glepnirbfb4eea2025-01-31 15:28:29 +01004645 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004646 curwin->w_cursor.col = compl_ins_end_col;
4647 }
4648
glepnir76bdb822025-02-08 19:04:51 +01004649 if (curwin->w_cursor.lnum > compl_lnum)
4650 {
4651 if (curwin->w_cursor.col < ml_get_curline_len())
4652 {
John Marriottf4b36412025-02-23 09:09:59 +01004653 char_u *line = ml_get_cursor();
4654 remaining.length = ml_get_cursor_len();
4655 remaining.string = vim_strnsave(line, remaining.length);
4656 if (remaining.string == NULL)
glepnir76bdb822025-02-08 19:04:51 +01004657 return;
4658 }
4659 while (curwin->w_cursor.lnum > compl_lnum)
4660 {
4661 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
4662 {
John Marriottf4b36412025-02-23 09:09:59 +01004663 if (remaining.string)
4664 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004665 return;
4666 }
zeertzjq060e6552025-02-21 20:06:26 +01004667 deleted_lines_mark(curwin->w_cursor.lnum, 1L);
glepnir76bdb822025-02-08 19:04:51 +01004668 curwin->w_cursor.lnum--;
4669 }
4670 // move cursor to end of line
4671 curwin->w_cursor.col = ml_get_curline_len();
4672 }
4673
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004674 if ((int)curwin->w_cursor.col > col)
4675 {
4676 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01004677 {
John Marriottf4b36412025-02-23 09:09:59 +01004678 if (remaining.string)
4679 vim_free(remaining.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004680 return;
glepnire3647c82025-02-10 21:16:32 +01004681 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004682 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004683 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004684 }
4685
John Marriottf4b36412025-02-23 09:09:59 +01004686 if (remaining.string != NULL)
glepnir76bdb822025-02-08 19:04:51 +01004687 {
4688 orig_col = curwin->w_cursor.col;
John Marriottf4b36412025-02-23 09:09:59 +01004689 ins_str(remaining.string, remaining.length);
glepnir76bdb822025-02-08 19:04:51 +01004690 curwin->w_cursor.col = orig_col;
John Marriottf4b36412025-02-23 09:09:59 +01004691 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004692 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004693 // TODO: is this sufficient for redrawing? Redrawing everything causes
4694 // flicker, thus we can't do that.
4695 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004696#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004697 // clear v:completed_item
4698 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004699#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004700}
4701
4702/*
glepnir76bdb822025-02-08 19:04:51 +01004703 * Insert a completion string that contains newlines.
4704 * The string is split and inserted line by line.
4705 */
4706 static void
4707ins_compl_expand_multiple(char_u *str)
4708{
4709 char_u *start = str;
4710 char_u *curr = str;
glepnir5090a1f2025-02-24 19:10:37 +01004711 int base_indent = get_indent();
glepnir76bdb822025-02-08 19:04:51 +01004712
4713 while (*curr != NUL)
4714 {
4715 if (*curr == '\n')
4716 {
4717 // Insert the text chunk before newline
4718 if (curr > start)
4719 ins_char_bytes(start, (int)(curr - start));
4720
4721 // Handle newline
glepnir5090a1f2025-02-24 19:10:37 +01004722 open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL);
glepnir76bdb822025-02-08 19:04:51 +01004723 start = curr + 1;
4724 }
4725 curr++;
4726 }
4727
4728 // Handle remaining text after last newline (if any)
4729 if (curr > start)
4730 ins_char_bytes(start, (int)(curr - start));
4731
4732 compl_ins_end_col = curwin->w_cursor.col;
4733}
4734
4735/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004736 * Insert the new text being completed.
4737 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004738 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4739 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004740 */
4741 void
glepniredd4ac32025-01-29 18:53:51 +01004742ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004743{
glepnir76bdb822025-02-08 19:04:51 +01004744 int compl_len = get_compl_len();
4745 int preinsert = ins_compl_has_preinsert();
4746 char_u *cp_str = compl_shown_match->cp_str.string;
4747 size_t cp_str_len = compl_shown_match->cp_str.length;
4748 size_t leader_len = ins_compl_leader_len();
4749 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004750
4751 // Make sure we don't go over the end of the string, this can happen with
4752 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004753 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004754 {
glepnir76bdb822025-02-08 19:04:51 +01004755 if (has_multiple)
4756 ins_compl_expand_multiple(cp_str + compl_len);
4757 else
4758 {
4759 ins_compl_insert_bytes(cp_str + compl_len, -1);
4760 if (preinsert && move_cursor)
4761 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
4762 }
glepniredd4ac32025-01-29 18:53:51 +01004763 }
4764 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004765 compl_used_match = FALSE;
4766 else
4767 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004768#ifdef FEAT_EVAL
4769 {
4770 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4771
4772 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4773 }
4774#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004775 if (!in_compl_func)
4776 compl_curr_match = compl_shown_match;
4777}
4778
4779/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004780 * show the file name for the completion match (if any). Truncate the file
4781 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004782 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004783 static void
4784ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004785{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004786 char *lead = _("match in file");
4787 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4788 char_u *s;
4789 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004790
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004791 if (space <= 0)
4792 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004793
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004794 // We need the tail that fits. With double-byte encoding going
4795 // back from the end is very slow, thus go from the start and keep
4796 // the text that fits in "space" between "s" and "e".
4797 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004798 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004799 space -= ptr2cells(e);
4800 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004801 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004802 space += ptr2cells(s);
4803 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004804 }
4805 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004806 msg_hist_off = TRUE;
4807 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4808 s > compl_shown_match->cp_fname ? "<" : "", s);
4809 msg((char *)IObuff);
4810 msg_hist_off = FALSE;
4811 redraw_cmdline = FALSE; // don't overwrite!
4812}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004813
glepnirdca57fb2024-06-04 22:01:21 +02004814/*
zeertzjq551d8c32024-06-05 19:53:32 +02004815 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004816 */
glepnira218cc62024-06-03 19:32:39 +02004817 static compl_T *
4818find_comp_when_fuzzy(void)
4819{
4820 int score;
4821 char_u* str;
4822 int target_idx = -1;
4823 int is_forward = compl_shows_dir_forward();
4824 int is_backward = compl_shows_dir_backward();
4825 compl_T *comp = NULL;
4826
glepnir43eef882024-06-19 20:20:48 +02004827 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004828 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004829 return compl_first_match != compl_shown_match ? compl_first_match :
4830 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004831
4832 if (is_forward)
4833 target_idx = compl_selected_item + 1;
4834 else if (is_backward)
4835 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004836 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004837
4838 score = compl_match_array[target_idx].pum_score;
4839 str = compl_match_array[target_idx].pum_text;
4840
4841 comp = compl_first_match;
4842 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004843 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004844 return comp;
4845 comp = comp->cp_next;
4846 } while (comp != NULL && !is_first_match(comp));
4847
4848 return NULL;
4849}
4850
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004851/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004852 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004853 * times. The number of matches found is returned in 'num_matches'.
4854 *
4855 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4856 * get more completions. If it is FALSE, then do nothing when there are no more
4857 * completions in the given direction.
4858 *
4859 * If "advance" is TRUE, then completion will move to the first match.
4860 * Otherwise, the original text will be shown.
4861 *
4862 * Returns OK on success and -1 if the number of matches are unknown.
4863 */
4864 static int
4865find_next_completion_match(
4866 int allow_get_expansion,
4867 int todo, // repeat completion this many times
4868 int advance,
4869 int *num_matches)
4870{
4871 int found_end = FALSE;
4872 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004873 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004874 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4875 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004877 while (--todo >= 0)
4878 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004879 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004880 {
glepniraced8c22024-06-15 15:13:05 +02004881 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4882 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004883 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004884 && (is_first_match(compl_shown_match->cp_next)
4885 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004886 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004887 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004888 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004889 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004890 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004891 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4892 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004893 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004894 }
4895 else
4896 {
4897 if (!allow_get_expansion)
4898 {
4899 if (advance)
4900 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004901 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004902 compl_pending -= todo + 1;
4903 else
4904 compl_pending += todo + 1;
4905 }
4906 return -1;
4907 }
4908
4909 if (!compl_no_select && advance)
4910 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004911 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004912 --compl_pending;
4913 else
4914 ++compl_pending;
4915 }
4916
4917 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004918 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004919
4920 // handle any pending completions
4921 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004922 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004923 {
4924 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4925 {
4926 compl_shown_match = compl_shown_match->cp_next;
4927 --compl_pending;
4928 }
4929 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4930 {
4931 compl_shown_match = compl_shown_match->cp_prev;
4932 ++compl_pending;
4933 }
4934 else
4935 break;
4936 }
4937 found_end = FALSE;
4938 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004939 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004940 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004941 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004942 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004943 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004944 ++todo;
4945 else
4946 // Remember a matching item.
4947 found_compl = compl_shown_match;
4948
4949 // Stop at the end of the list when we found a usable match.
4950 if (found_end)
4951 {
4952 if (found_compl != NULL)
4953 {
4954 compl_shown_match = found_compl;
4955 break;
4956 }
4957 todo = 1; // use first usable match after wrapping around
4958 }
4959 }
4960
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004961 return OK;
4962}
4963
4964/*
4965 * Fill in the next completion in the current direction.
4966 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4967 * get more completions. If it is FALSE, then we just do nothing when there
4968 * are no more completions in a given direction. The latter case is used when
4969 * we are still in the middle of finding completions, to allow browsing
4970 * through the ones found so far.
4971 * Return the total number of matches, or -1 if still unknown -- webb.
4972 *
4973 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4974 * compl_shown_match here.
4975 *
4976 * Note that this function may be called recursively once only. First with
4977 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4978 * calls this function with "allow_get_expansion" FALSE.
4979 */
4980 static int
4981ins_compl_next(
4982 int allow_get_expansion,
4983 int count, // repeat completion this many times; should
4984 // be at least 1
4985 int insert_match, // Insert the newly selected match
4986 int in_compl_func) // called from complete_check()
4987{
4988 int num_matches = -1;
4989 int todo = count;
4990 int advance;
4991 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004992 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004993 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004994 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4995 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004996 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004997
4998 // When user complete function return -1 for findstart which is next
4999 // time of 'always', compl_shown_match become NULL.
5000 if (compl_shown_match == NULL)
5001 return -1;
5002
John Marriott5e6ea922024-11-23 14:01:57 +01005003 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02005004 && !match_at_original_text(compl_shown_match)
5005 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005006 // Update "compl_shown_match" to the actually shown match
5007 ins_compl_update_shown_match();
5008
5009 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02005010 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005011 // Delete old text to be replaced
5012 ins_compl_delete();
5013
5014 // When finding the longest common text we stick at the original text,
5015 // don't let CTRL-N or CTRL-P move to the first match.
5016 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
5017
5018 // When restarting the search don't insert the first match either.
5019 if (compl_restarting)
5020 {
5021 advance = FALSE;
5022 compl_restarting = FALSE;
5023 }
5024
5025 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
5026 // around.
5027 if (find_next_completion_match(allow_get_expansion, todo, advance,
5028 &num_matches) == -1)
5029 return -1;
5030
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005031 if (curbuf != orig_curbuf)
5032 {
5033 // In case some completion function switched buffer, don't want to
5034 // insert the completion elsewhere.
5035 return -1;
5036 }
5037
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005038 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01005039 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005040 {
glepnir6a38aff2024-12-16 21:56:16 +01005041 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005042 compl_used_match = FALSE;
5043 }
5044 else if (insert_match)
5045 {
5046 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01005047 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005048 else
glepnir6a38aff2024-12-16 21:56:16 +01005049 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005050 }
5051 else
5052 compl_used_match = FALSE;
5053
5054 if (!allow_get_expansion)
5055 {
5056 // may undisplay the popup menu first
5057 ins_compl_upd_pum();
5058
5059 if (pum_enough_matches())
5060 // Will display the popup menu, don't redraw yet to avoid flicker.
5061 pum_call_update_screen();
5062 else
5063 // Not showing the popup menu yet, redraw to show the user what was
5064 // inserted.
5065 update_screen(0);
5066
5067 // display the updated popup menu
5068 ins_compl_show_pum();
5069#ifdef FEAT_GUI
5070 if (gui.in_use)
5071 {
5072 // Show the cursor after the match, not after the redrawn text.
5073 setcursor();
5074 out_flush_cursor(FALSE, FALSE);
5075 }
5076#endif
5077
5078 // Delete old text to be replaced, since we're still searching and
5079 // don't want to match ourselves!
5080 ins_compl_delete();
5081 }
5082
5083 // Enter will select a match when the match wasn't inserted and the popup
5084 // menu is visible.
5085 if (compl_no_insert && !started)
5086 compl_enter_selects = TRUE;
5087 else
5088 compl_enter_selects = !insert_match && compl_match_array != NULL;
5089
5090 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005091 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005092 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005093
5094 return num_matches;
5095}
5096
5097/*
5098 * Call this while finding completions, to check whether the user has hit a key
5099 * that should change the currently displayed completion, or exit completion
5100 * mode. Also, when compl_pending is not zero, show a completion as soon as
5101 * possible. -- webb
5102 * "frequency" specifies out of how many calls we actually check.
5103 * "in_compl_func" is TRUE when called from complete_check(), don't set
5104 * compl_curr_match.
5105 */
5106 void
5107ins_compl_check_keys(int frequency, int in_compl_func)
5108{
5109 static int count = 0;
5110 int c;
5111
5112 // Don't check when reading keys from a script, :normal or feedkeys().
5113 // That would break the test scripts. But do check for keys when called
5114 // from complete_check().
5115 if (!in_compl_func && (using_script() || ex_normal_busy))
5116 return;
5117
5118 // Only do this at regular intervals
5119 if (++count < frequency)
5120 return;
5121 count = 0;
5122
5123 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5124 // can't do its work correctly.
5125 c = vpeekc_any();
5126 if (c != NUL)
5127 {
5128 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5129 {
5130 c = safe_vgetc(); // Eat the character
5131 compl_shows_dir = ins_compl_key2dir(c);
5132 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
5133 c != K_UP && c != K_DOWN, in_compl_func);
5134 }
5135 else
5136 {
5137 // Need to get the character to have KeyTyped set. We'll put it
5138 // back with vungetc() below. But skip K_IGNORE.
5139 c = safe_vgetc();
5140 if (c != K_IGNORE)
5141 {
5142 // Don't interrupt completion when the character wasn't typed,
5143 // e.g., when doing @q to replay keys.
5144 if (c != Ctrl_R && KeyTyped)
5145 compl_interrupted = TRUE;
5146
5147 vungetc(c);
5148 }
5149 }
5150 }
zeertzjq529b9ad2024-06-05 20:27:06 +02005151 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005152 {
5153 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5154
5155 compl_pending = 0;
5156 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
5157 }
5158}
5159
5160/*
5161 * Decide the direction of Insert mode complete from the key typed.
5162 * Returns BACKWARD or FORWARD.
5163 */
5164 static int
5165ins_compl_key2dir(int c)
5166{
5167 if (c == Ctrl_P || c == Ctrl_L
5168 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
5169 return BACKWARD;
5170 return FORWARD;
5171}
5172
5173/*
5174 * Return TRUE for keys that are used for completion only when the popup menu
5175 * is visible.
5176 */
5177 static int
5178ins_compl_pum_key(int c)
5179{
5180 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
5181 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5182 || c == K_UP || c == K_DOWN);
5183}
5184
5185/*
5186 * Decide the number of completions to move forward.
5187 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5188 */
5189 static int
5190ins_compl_key2count(int c)
5191{
5192 int h;
5193
5194 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
5195 {
5196 h = pum_get_height();
5197 if (h > 3)
5198 h -= 2; // keep some context
5199 return h;
5200 }
5201 return 1;
5202}
5203
5204/*
5205 * Return TRUE if completion with "c" should insert the match, FALSE if only
5206 * to change the currently selected completion.
5207 */
5208 static int
5209ins_compl_use_match(int c)
5210{
5211 switch (c)
5212 {
5213 case K_UP:
5214 case K_DOWN:
5215 case K_PAGEDOWN:
5216 case K_KPAGEDOWN:
5217 case K_S_DOWN:
5218 case K_PAGEUP:
5219 case K_KPAGEUP:
5220 case K_S_UP:
5221 return FALSE;
5222 }
5223 return TRUE;
5224}
5225
5226/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005227 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
5228 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01005229 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005230 * Uses the global variables: compl_cont_status and ctrl_x_mode
5231 */
5232 static int
5233get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
5234{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005235 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005236 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005237 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005238 {
5239 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5240 ;
5241 compl_col += ++startcol;
5242 compl_length = curs_col - startcol;
5243 }
5244 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005245 {
John Marriott5e6ea922024-11-23 14:01:57 +01005246 compl_pattern.string = str_foldcase(line + compl_col,
5247 compl_length, NULL, 0);
5248 if (compl_pattern.string == NULL)
5249 {
5250 compl_pattern.length = 0;
5251 return FAIL;
5252 }
5253 compl_pattern.length = STRLEN(compl_pattern.string);
5254 }
5255 else
5256 {
5257 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5258 if (compl_pattern.string == NULL)
5259 {
5260 compl_pattern.length = 0;
5261 return FAIL;
5262 }
5263 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005264 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005265 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005266 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005267 {
5268 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005269 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005270 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005271
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005272 if (!vim_iswordp(line + compl_col)
5273 || (compl_col > 0
5274 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005275 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005276 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005277 prefixlen = 0;
5278 }
John Marriott5e6ea922024-11-23 14:01:57 +01005279
5280 // we need up to 2 extra chars for the prefix
5281 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5282 compl_pattern.string = alloc(n);
5283 if (compl_pattern.string == NULL)
5284 {
5285 compl_pattern.length = 0;
5286 return FAIL;
5287 }
5288 STRCPY((char *)compl_pattern.string, prefix);
5289 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005290 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005291 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005292 }
5293 else if (--startcol < 0
5294 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5295 {
John Marriott5e6ea922024-11-23 14:01:57 +01005296 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5297
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005298 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005299 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5300 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005301 {
John Marriott5e6ea922024-11-23 14:01:57 +01005302 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005303 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005304 }
John Marriott5e6ea922024-11-23 14:01:57 +01005305 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005306 compl_col += curs_col;
5307 compl_length = 0;
5308 }
5309 else
5310 {
5311 // Search the point of change class of multibyte character
5312 // or not a word single byte character backward.
5313 if (has_mbyte)
5314 {
5315 int base_class;
5316 int head_off;
5317
5318 startcol -= (*mb_head_off)(line, line + startcol);
5319 base_class = mb_get_class(line + startcol);
5320 while (--startcol >= 0)
5321 {
5322 head_off = (*mb_head_off)(line, line + startcol);
5323 if (base_class != mb_get_class(line + startcol
5324 - head_off))
5325 break;
5326 startcol -= head_off;
5327 }
5328 }
5329 else
5330 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5331 ;
5332 compl_col += ++startcol;
5333 compl_length = (int)curs_col - startcol;
5334 if (compl_length == 1)
5335 {
5336 // Only match word with at least two chars -- webb
5337 // there's no need to call quote_meta,
5338 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005339 compl_pattern.string = alloc(7);
5340 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005341 {
John Marriott5e6ea922024-11-23 14:01:57 +01005342 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005343 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005344 }
John Marriott5e6ea922024-11-23 14:01:57 +01005345 STRCPY((char *)compl_pattern.string, "\\<");
5346 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5347 STRCAT((char *)compl_pattern.string, "\\k");
5348 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005349 }
5350 else
5351 {
John Marriott5e6ea922024-11-23 14:01:57 +01005352 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5353
5354 compl_pattern.string = alloc(n);
5355 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005356 {
John Marriott5e6ea922024-11-23 14:01:57 +01005357 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005358 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005359 }
John Marriott5e6ea922024-11-23 14:01:57 +01005360 STRCPY((char *)compl_pattern.string, "\\<");
5361 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005362 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005363 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005364 }
5365 }
5366
5367 return OK;
5368}
5369
5370/*
5371 * Get the pattern, column and length for whole line completion or for the
5372 * complete() function.
5373 * Sets the global variables: compl_col, compl_length and compl_pattern.
5374 */
5375 static int
5376get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5377{
5378 compl_col = (colnr_T)getwhitecols(line);
5379 compl_length = (int)curs_col - (int)compl_col;
5380 if (compl_length < 0) // cursor in indent: empty pattern
5381 compl_length = 0;
5382 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005383 {
John Marriott5e6ea922024-11-23 14:01:57 +01005384 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5385 NULL, 0);
5386 if (compl_pattern.string == NULL)
5387 {
5388 compl_pattern.length = 0;
5389 return FAIL;
5390 }
5391 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005392 }
John Marriott5e6ea922024-11-23 14:01:57 +01005393 else
5394 {
5395 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5396 if (compl_pattern.string == NULL)
5397 {
5398 compl_pattern.length = 0;
5399 return FAIL;
5400 }
5401 compl_pattern.length = (size_t)compl_length;
5402 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005403
5404 return OK;
5405}
5406
5407/*
5408 * Get the pattern, column and length for filename completion.
5409 * Sets the global variables: compl_col, compl_length and compl_pattern.
5410 */
5411 static int
5412get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5413{
5414 // Go back to just before the first filename character.
5415 if (startcol > 0)
5416 {
5417 char_u *p = line + startcol;
5418
5419 MB_PTR_BACK(line, p);
5420 while (p > line && vim_isfilec(PTR2CHAR(p)))
5421 MB_PTR_BACK(line, p);
5422 if (p == line && vim_isfilec(PTR2CHAR(p)))
5423 startcol = 0;
5424 else
5425 startcol = (int)(p - line) + 1;
5426 }
5427
5428 compl_col += startcol;
5429 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005430 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5431 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005432 {
John Marriott5e6ea922024-11-23 14:01:57 +01005433 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005434 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005435 }
5436
John Marriott5e6ea922024-11-23 14:01:57 +01005437 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005438
5439 return OK;
5440}
5441
5442/*
5443 * Get the pattern, column and length for command-line completion.
5444 * Sets the global variables: compl_col, compl_length and compl_pattern.
5445 */
5446 static int
5447get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5448{
John Marriott5e6ea922024-11-23 14:01:57 +01005449 compl_pattern.string = vim_strnsave(line, curs_col);
5450 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005451 {
John Marriott5e6ea922024-11-23 14:01:57 +01005452 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005453 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005454 }
John Marriott5e6ea922024-11-23 14:01:57 +01005455 compl_pattern.length = curs_col;
5456 set_cmd_context(&compl_xp, compl_pattern.string,
5457 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005458 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5459 || compl_xp.xp_context == EXPAND_NOTHING)
5460 // No completion possible, use an empty pattern to get a
5461 // "pattern not found" message.
5462 compl_col = curs_col;
5463 else
John Marriott5e6ea922024-11-23 14:01:57 +01005464 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005465 compl_length = curs_col - compl_col;
5466
5467 return OK;
5468}
5469
5470/*
5471 * Get the pattern, column and length for user defined completion ('omnifunc',
5472 * 'completefunc' and 'thesaurusfunc')
5473 * Sets the global variables: compl_col, compl_length and compl_pattern.
5474 * Uses the global variable: spell_bad_len
5475 */
5476 static int
5477get_userdefined_compl_info(colnr_T curs_col UNUSED)
5478{
5479 int ret = FAIL;
5480
5481#ifdef FEAT_COMPL_FUNC
5482 // Call user defined function 'completefunc' with "a:findstart"
5483 // set to 1 to obtain the length of text to use for completion.
5484 char_u *line;
5485 typval_T args[3];
5486 int col;
5487 char_u *funcname;
5488 pos_T pos;
5489 int save_State = State;
5490 callback_T *cb;
5491
5492 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5493 // length as a string
5494 funcname = get_complete_funcname(ctrl_x_mode);
5495 if (*funcname == NUL)
5496 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005497 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005498 ? "completefunc" : "omnifunc");
5499 return FAIL;
5500 }
5501
5502 args[0].v_type = VAR_NUMBER;
5503 args[0].vval.v_number = 1;
5504 args[1].v_type = VAR_STRING;
5505 args[1].vval.v_string = (char_u *)"";
5506 args[2].v_type = VAR_UNKNOWN;
5507 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005508 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005509 cb = get_insert_callback(ctrl_x_mode);
5510 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005511 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005512
5513 State = save_State;
5514 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005515 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005516 validate_cursor();
5517 if (!EQUAL_POS(curwin->w_cursor, pos))
5518 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005519 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005520 return FAIL;
5521 }
5522
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005523 // Return value -2 means the user complete function wants to cancel the
5524 // complete without an error, do the same if the function did not execute
5525 // successfully.
5526 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005527 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005528 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005529 if (col == -3)
5530 {
5531 ctrl_x_mode = CTRL_X_NORMAL;
5532 edit_submode = NULL;
5533 if (!shortmess(SHM_COMPLETIONMENU))
5534 msg_clr_cmdline();
5535 return FAIL;
5536 }
5537
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005538 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005539 // completion.
5540 compl_opt_refresh_always = FALSE;
5541 compl_opt_suppress_empty = FALSE;
5542
5543 if (col < 0)
5544 col = curs_col;
5545 compl_col = col;
5546 if (compl_col > curs_col)
5547 compl_col = curs_col;
5548
5549 // Setup variables for completion. Need to obtain "line" again,
5550 // it may have become invalid.
5551 line = ml_get(curwin->w_cursor.lnum);
5552 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005553 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5554 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005555 {
John Marriott5e6ea922024-11-23 14:01:57 +01005556 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005557 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005558 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005559
John Marriott5e6ea922024-11-23 14:01:57 +01005560 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005561 ret = OK;
5562#endif
5563
5564 return ret;
5565}
5566
5567/*
5568 * Get the pattern, column and length for spell completion.
5569 * Sets the global variables: compl_col, compl_length and compl_pattern.
5570 * Uses the global variable: spell_bad_len
5571 */
5572 static int
5573get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5574{
5575 int ret = FAIL;
5576#ifdef FEAT_SPELL
5577 char_u *line;
5578
5579 if (spell_bad_len > 0)
5580 compl_col = curs_col - spell_bad_len;
5581 else
5582 compl_col = spell_word_start(startcol);
5583 if (compl_col >= (colnr_T)startcol)
5584 {
5585 compl_length = 0;
5586 compl_col = curs_col;
5587 }
5588 else
5589 {
5590 spell_expand_check_cap(compl_col);
5591 compl_length = (int)curs_col - compl_col;
5592 }
5593 // Need to obtain "line" again, it may have become invalid.
5594 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005595 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5596 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005597 {
John Marriott5e6ea922024-11-23 14:01:57 +01005598 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005599 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005600 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005601
John Marriott5e6ea922024-11-23 14:01:57 +01005602 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005603 ret = OK;
5604#endif
5605
5606 return ret;
5607}
5608
5609/*
5610 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005611 * "startcol" - start column number of the completion pattern/text
5612 * "cur_col" - current cursor column
5613 * On return, "line_invalid" is set to TRUE, if the current line may have
5614 * become invalid and needs to be fetched again.
5615 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005616 */
5617 static int
5618compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5619{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005620 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005621 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5622 && !thesaurus_func_complete(ctrl_x_mode)))
5623 {
5624 return get_normal_compl_info(line, startcol, curs_col);
5625 }
5626 else if (ctrl_x_mode_line_or_eval())
5627 {
5628 return get_wholeline_compl_info(line, curs_col);
5629 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005630 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005631 {
5632 return get_filename_compl_info(line, startcol, curs_col);
5633 }
5634 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5635 {
5636 return get_cmdline_compl_info(line, curs_col);
5637 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005638 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005639 || thesaurus_func_complete(ctrl_x_mode))
5640 {
5641 if (get_userdefined_compl_info(curs_col) == FAIL)
5642 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005643 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005644 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005645 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005646 {
5647 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5648 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005649 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005650 }
5651 else
5652 {
5653 internal_error("ins_complete()");
5654 return FAIL;
5655 }
5656
5657 return OK;
5658}
5659
5660/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005661 * Continue an interrupted completion mode search in "line".
5662 *
5663 * If this same ctrl_x_mode has been interrupted use the text from
5664 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5665 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5666 * the same line as the cursor then fix it (the line has been split because it
5667 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5668 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005669 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005670 static void
5671ins_compl_continue_search(char_u *line)
5672{
5673 // it is a continued search
5674 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005675 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5676 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005677 {
5678 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5679 {
5680 // line (probably) wrapped, set compl_startpos to the
5681 // first non_blank in the line, if it is not a wordchar
5682 // include it to get a better pattern, but then we don't
5683 // want the "\\<" prefix, check it below
5684 compl_col = (colnr_T)getwhitecols(line);
5685 compl_startpos.col = compl_col;
5686 compl_startpos.lnum = curwin->w_cursor.lnum;
5687 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5688 }
5689 else
5690 {
5691 // S_IPOS was set when we inserted a word that was at the
5692 // beginning of the line, which means that we'll go to SOL
5693 // mode but first we need to redefine compl_startpos
5694 if (compl_cont_status & CONT_S_IPOS)
5695 {
5696 compl_cont_status |= CONT_SOL;
5697 compl_startpos.col = (colnr_T)(skipwhite(
5698 line + compl_length
5699 + compl_startpos.col) - line);
5700 }
5701 compl_col = compl_startpos.col;
5702 }
5703 compl_length = curwin->w_cursor.col - (int)compl_col;
5704 // IObuff is used to add a "word from the next line" would we
5705 // have enough space? just being paranoid
5706#define MIN_SPACE 75
5707 if (compl_length > (IOSIZE - MIN_SPACE))
5708 {
5709 compl_cont_status &= ~CONT_SOL;
5710 compl_length = (IOSIZE - MIN_SPACE);
5711 compl_col = curwin->w_cursor.col - compl_length;
5712 }
5713 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5714 if (compl_length < 1)
5715 compl_cont_status &= CONT_LOCAL;
5716 }
5717 else if (ctrl_x_mode_line_or_eval())
5718 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5719 else
5720 compl_cont_status = 0;
5721}
5722
5723/*
5724 * start insert mode completion
5725 */
5726 static int
5727ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005728{
5729 char_u *line;
5730 int startcol = 0; // column where searched text starts
5731 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005732 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005733 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005734 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005735
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005736 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005737
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005738 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005739 did_si = FALSE;
5740 can_si = FALSE;
5741 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005742 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005743 return FAIL;
5744
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005745 line = ml_get(curwin->w_cursor.lnum);
5746 curs_col = curwin->w_cursor.col;
5747 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01005748 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005749
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005750 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5751 && compl_cont_mode == ctrl_x_mode)
5752 // this same ctrl-x_mode was interrupted previously. Continue the
5753 // completion.
5754 ins_compl_continue_search(line);
5755 else
5756 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005757
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005758 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005759 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005760 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005761 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005762 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5763 compl_cont_status = 0;
5764 compl_cont_status |= CONT_N_ADDS;
5765 compl_startpos = curwin->w_cursor;
5766 startcol = (int)curs_col;
5767 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005768 }
5769
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005770 // Work out completion pattern and original text -- webb
5771 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5772 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005773 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5774 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005775 // restore did_ai, so that adding comment leader works
5776 did_ai = save_did_ai;
5777 return FAIL;
5778 }
5779 // If "line" was changed while getting completion info get it again.
5780 if (line_invalid)
5781 line = ml_get(curwin->w_cursor.lnum);
5782
glepnir7cfe6932024-09-15 20:06:28 +02005783 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005784 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005785 {
5786 edit_submode_pre = (char_u *)_(" Adding");
5787 if (ctrl_x_mode_line_or_eval())
5788 {
5789 // Insert a new line, keep indentation but ignore 'comments'.
5790 char_u *old = curbuf->b_p_com;
5791
5792 curbuf->b_p_com = (char_u *)"";
5793 compl_startpos.lnum = curwin->w_cursor.lnum;
5794 compl_startpos.col = compl_col;
5795 ins_eol('\r');
5796 curbuf->b_p_com = old;
5797 compl_length = 0;
5798 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01005799 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005800 }
glepnir7cfe6932024-09-15 20:06:28 +02005801 else if (ctrl_x_mode_normal() && in_fuzzy)
5802 {
5803 compl_startpos = curwin->w_cursor;
5804 compl_cont_status &= CONT_S_IPOS;
5805 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005806 }
5807 else
5808 {
5809 edit_submode_pre = NULL;
5810 compl_startpos.col = compl_col;
5811 }
5812
5813 if (compl_cont_status & CONT_LOCAL)
5814 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5815 else
5816 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5817
5818 // If any of the original typed text has been changed we need to fix
5819 // the redo buffer.
5820 ins_compl_fixRedoBufForLeader(NULL);
5821
5822 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005823 VIM_CLEAR_STRING(compl_orig_text);
5824 compl_orig_text.length = (size_t)compl_length;
5825 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005826 if (p_ic)
5827 flags |= CP_ICASE;
zeertzjq4422de62025-03-07 19:06:02 +01005828 if (compl_orig_text.string == NULL
5829 || ins_compl_add(compl_orig_text.string,
5830 (int)compl_orig_text.length,
5831 NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005832 {
John Marriott5e6ea922024-11-23 14:01:57 +01005833 VIM_CLEAR_STRING(compl_pattern);
5834 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005835 return FAIL;
5836 }
5837
5838 // showmode might reset the internal line pointers, so it must
5839 // be called before line = ml_get(), or when this address is no
5840 // longer needed. -- Acevedo.
5841 edit_submode_extra = (char_u *)_("-- Searching...");
5842 edit_submode_highl = HLF_COUNT;
5843 showmode();
5844 edit_submode_extra = NULL;
5845 out_flush();
5846
5847 return OK;
5848}
5849
5850/*
5851 * display the completion status message
5852 */
5853 static void
5854ins_compl_show_statusmsg(void)
5855{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005856 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005857 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005858 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005859 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005860 ? (char_u *)_("Hit end of paragraph")
5861 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005862 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005863 }
5864
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005865 if (edit_submode_extra == NULL)
5866 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005867 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005868 {
5869 edit_submode_extra = (char_u *)_("Back at original");
5870 edit_submode_highl = HLF_W;
5871 }
5872 else if (compl_cont_status & CONT_S_IPOS)
5873 {
5874 edit_submode_extra = (char_u *)_("Word from other line");
5875 edit_submode_highl = HLF_COUNT;
5876 }
5877 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5878 {
5879 edit_submode_extra = (char_u *)_("The only match");
5880 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005881 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005882 }
5883 else
5884 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005885#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005886 // Update completion sequence number when needed.
5887 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005888 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005889#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005890 // The match should always have a sequence number now, this is
5891 // just a safety check.
5892 if (compl_curr_match->cp_number != -1)
5893 {
5894 // Space for 10 text chars. + 2x10-digit no.s = 31.
5895 // Translations may need more than twice that.
5896 static char_u match_ref[81];
5897
5898 if (compl_matches > 0)
5899 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005900 _("match %d of %d"),
5901 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005902 else
5903 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005904 _("match %d"),
5905 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005906 edit_submode_extra = match_ref;
5907 edit_submode_highl = HLF_R;
5908 if (dollar_vcol >= 0)
5909 curs_columns(FALSE);
5910 }
5911 }
5912 }
5913
5914 // Show a message about what (completion) mode we're in.
5915 if (!compl_opt_suppress_empty)
5916 {
5917 showmode();
5918 if (!shortmess(SHM_COMPLETIONMENU))
5919 {
5920 if (edit_submode_extra != NULL)
5921 {
5922 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005923 {
5924 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005925 msg_attr((char *)edit_submode_extra,
5926 edit_submode_highl < HLF_COUNT
5927 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005928 msg_hist_off = FALSE;
5929 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005930 }
5931 else
5932 msg_clr_cmdline(); // necessary for "noshowmode"
5933 }
5934 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005935}
5936
5937/*
5938 * Do Insert mode completion.
5939 * Called when character "c" was typed, which has a meaning for completion.
5940 * Returns OK if completion was done, FAIL if something failed (out of mem).
5941 */
5942 int
5943ins_complete(int c, int enable_pum)
5944{
5945 int n;
5946 int save_w_wrow;
5947 int save_w_leftcol;
5948 int insert_match;
5949
5950 compl_direction = ins_compl_key2dir(c);
5951 insert_match = ins_compl_use_match(c);
5952
5953 if (!compl_started)
5954 {
5955 if (ins_compl_start() == FAIL)
5956 return FAIL;
5957 }
5958 else if (insert_match && stop_arrow() == FAIL)
5959 return FAIL;
5960
5961 compl_shown_match = compl_curr_match;
5962 compl_shows_dir = compl_direction;
5963
5964 // Find next match (and following matches).
5965 save_w_wrow = curwin->w_wrow;
5966 save_w_leftcol = curwin->w_leftcol;
5967 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5968
5969 // may undisplay the popup menu
5970 ins_compl_upd_pum();
5971
5972 if (n > 1) // all matches have been found
5973 compl_matches = n;
5974 compl_curr_match = compl_shown_match;
5975 compl_direction = compl_shows_dir;
5976
5977 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5978 // mode.
5979 if (got_int && !global_busy)
5980 {
5981 (void)vgetc();
5982 got_int = FALSE;
5983 }
5984
5985 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005986 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005987 {
5988 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5989 // because we couldn't expand anything at first place, but if we used
5990 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5991 // (such as M in M'exico) if not tried already. -- Acevedo
5992 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005993 || compl_status_adding()
5994 || (ctrl_x_mode_not_default()
5995 && !ctrl_x_mode_path_patterns()
5996 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005997 compl_cont_status &= ~CONT_N_ADDS;
5998 }
5999
6000 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
6001 compl_cont_status |= CONT_S_IPOS;
6002 else
6003 compl_cont_status &= ~CONT_S_IPOS;
6004
6005 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006006
6007 // Show the popup menu, unless we got interrupted.
6008 if (enable_pum && !compl_interrupted)
6009 show_pum(save_w_wrow, save_w_leftcol);
6010
6011 compl_was_interrupted = compl_interrupted;
6012 compl_interrupted = FALSE;
6013
6014 return OK;
6015}
6016
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00006017/*
6018 * Remove (if needed) and show the popup menu
6019 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006020 static void
6021show_pum(int prev_w_wrow, int prev_w_leftcol)
6022{
6023 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006024 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006025 RedrawingDisabled = 0;
6026
6027 // If the cursor moved or the display scrolled we need to remove the pum
6028 // first.
6029 setcursor();
6030 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
6031 ins_compl_del_pum();
6032
6033 ins_compl_show_pum();
6034 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006035
6036 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006037}
6038
6039/*
6040 * Looks in the first "len" chars. of "src" for search-metachars.
6041 * If dest is not NULL the chars. are copied there quoting (with
6042 * a backslash) the metachars, and dest would be NUL terminated.
6043 * Returns the length (needed) of dest
6044 */
6045 static unsigned
6046quote_meta(char_u *dest, char_u *src, int len)
6047{
6048 unsigned m = (unsigned)len + 1; // one extra for the NUL
6049
6050 for ( ; --len >= 0; src++)
6051 {
6052 switch (*src)
6053 {
6054 case '.':
6055 case '*':
6056 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006057 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006058 break;
6059 // FALLTHROUGH
6060 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006061 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006062 break;
6063 // FALLTHROUGH
6064 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006065 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006066 break;
6067 // FALLTHROUGH
6068 case '^': // currently it's not needed.
6069 case '$':
6070 m++;
6071 if (dest != NULL)
6072 *dest++ = '\\';
6073 break;
6074 }
6075 if (dest != NULL)
6076 *dest++ = *src;
6077 // Copy remaining bytes of a multibyte character.
6078 if (has_mbyte)
6079 {
6080 int i, mb_len;
6081
6082 mb_len = (*mb_ptr2len)(src) - 1;
6083 if (mb_len > 0 && len >= mb_len)
6084 for (i = 0; i < mb_len; ++i)
6085 {
6086 --len;
6087 ++src;
6088 if (dest != NULL)
6089 *dest++ = *src;
6090 }
6091 }
6092 }
6093 if (dest != NULL)
6094 *dest = NUL;
6095
6096 return m;
6097}
6098
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006099#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006100 void
6101free_insexpand_stuff(void)
6102{
John Marriott5e6ea922024-11-23 14:01:57 +01006103 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006104# ifdef FEAT_EVAL
6105 free_callback(&cfu_cb);
6106 free_callback(&ofu_cb);
6107 free_callback(&tsrfu_cb);
6108# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006109}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006110#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006111
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006112#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006113/*
6114 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6115 * spelled word, if there is one.
6116 */
6117 static void
6118spell_back_to_badword(void)
6119{
6120 pos_T tpos = curwin->w_cursor;
6121
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02006122 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006123 if (curwin->w_cursor.col != tpos.col)
6124 start_arrow(&tpos);
6125}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006126#endif