blob: 49c7ee27f2a60b85b874f19e093b6bf62d07fb29 [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{
glepnir58760162025-03-13 21:39:51 +0100767 if (ctrl_x_mode_normal() || ctrl_x_mode_dictionary())
768 return (cfc_flags & CFC_KEYWORD) != 0;
769 else if (ctrl_x_mode_files())
770 return (cfc_flags & CFC_FILES) != 0;
771 else if (ctrl_x_mode_whole_line())
772 return (cfc_flags & CFC_WHOLELINE) != 0;
773 else
774 return FALSE;
glepnirf31cfa22025-03-06 21:59:13 +0100775}
776
777/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000778 * Add a match to the list of matches. The arguments are:
779 * str - text of the match to add
780 * len - length of "str". If -1, then the length of "str" is
781 * computed.
782 * fname - file name to associate with this match.
783 * cptext - list of strings to use with this match (for abbr, menu, info
784 * and kind)
785 * user_data - user supplied data (any vim type) for this match
786 * cdir - match direction. If 0, use "compl_direction".
787 * flags_arg - match flags (cp_flags)
788 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100789 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000790 * If "cdir" is FORWARD, then the match is added after the current match.
791 * Otherwise, it is added before the current match.
792 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100793 * If the given string is already in the list of completions, then return
794 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
795 * maybe because alloc() returns NULL, then FAIL is returned.
796 */
797 static int
798ins_compl_add(
799 char_u *str,
800 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100801 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 char_u **cptext, // extra text for popup menu or NULL
803 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200805 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200806 int adup, // accept duplicate match
glepnirf31cfa22025-03-06 21:59:13 +0100807 int *user_hl, // user abbr/kind hlattr
808 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100809{
glepnirf31cfa22025-03-06 21:59:13 +0100810 compl_T *match, *current, *prev;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100811 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200812 int flags = flags_arg;
glepnirf31cfa22025-03-06 21:59:13 +0100813 int inserted = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100814
Bram Moolenaarceb06192021-04-04 15:05:22 +0200815 if (flags & CP_FAST)
816 fast_breakcheck();
817 else
818 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 if (got_int)
820 return FAIL;
821 if (len < 0)
822 len = (int)STRLEN(str);
823
824 // If the same match is already present, don't add it.
825 if (compl_first_match != NULL && !adup)
826 {
827 match = compl_first_match;
828 do
829 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000830 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100831 && STRNCMP(match->cp_str.string, str, len) == 0
832 && ((int)match->cp_str.length <= len
833 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100834 return NOTDONE;
835 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000836 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100837 }
838
839 // Remove any popup menu before changing the list of matches.
840 ins_compl_del_pum();
841
842 // Allocate a new match structure.
843 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200844 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100845 if (match == NULL)
846 return FAIL;
glepnir40891ba2025-02-10 22:18:00 +0100847 match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
John Marriott5e6ea922024-11-23 14:01:57 +0100848 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849 {
850 vim_free(match);
851 return FAIL;
852 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100853
John Marriott5e6ea922024-11-23 14:01:57 +0100854 match->cp_str.length = len;
855
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856 // match-fname is:
857 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200858 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 // - NULL otherwise. --Acevedo
860 if (fname != NULL
861 && compl_curr_match != NULL
862 && compl_curr_match->cp_fname != NULL
863 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
864 match->cp_fname = compl_curr_match->cp_fname;
865 else if (fname != NULL)
866 {
867 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200868 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100869 }
870 else
871 match->cp_fname = NULL;
872 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100873 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
874 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
glepnirf31cfa22025-03-06 21:59:13 +0100875 match->cp_score = score;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100876
877 if (cptext != NULL)
878 {
879 int i;
880
881 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100882 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100883 if (cptext[i] != NULL && *cptext[i] != NUL)
884 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100885 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100886 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100887#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100888 if (user_data != NULL)
889 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100890#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100891
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000892 // Link the new match structure after (FORWARD) or before (BACKWARD) the
893 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100894 if (compl_first_match == NULL)
895 match->cp_next = match->cp_prev = NULL;
glepnirf31cfa22025-03-06 21:59:13 +0100896 else if (cfc_has_mode() && score > 0 && compl_get_longest)
897 {
898 current = compl_first_match->cp_next;
899 prev = compl_first_match;
900 inserted = FALSE;
901 // The direction is ignored when using longest and
902 // completefuzzycollect, because matches are inserted
903 // and sorted by score.
904 while (current != NULL && current != compl_first_match)
905 {
906 if (current->cp_score < score)
907 {
908 match->cp_next = current;
909 match->cp_prev = current->cp_prev;
910 if (current->cp_prev)
911 current->cp_prev->cp_next = match;
912 current->cp_prev = match;
913 inserted = TRUE;
914 break;
915 }
916 prev = current;
917 current = current->cp_next;
918 }
919 if (!inserted)
920 {
921 prev->cp_next = match;
922 match->cp_prev = prev;
923 match->cp_next = compl_first_match;
924 compl_first_match->cp_prev = match;
925 }
926 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100927 else if (dir == FORWARD)
928 {
929 match->cp_next = compl_curr_match->cp_next;
930 match->cp_prev = compl_curr_match;
931 }
932 else // BACKWARD
933 {
934 match->cp_next = compl_curr_match;
935 match->cp_prev = compl_curr_match->cp_prev;
936 }
937 if (match->cp_next)
938 match->cp_next->cp_prev = match;
939 if (match->cp_prev)
940 match->cp_prev->cp_next = match;
941 else // if there's nothing before, it is the first match
942 compl_first_match = match;
943 compl_curr_match = match;
944
945 // Find the longest common string if still doing that.
glepnirf31cfa22025-03-06 21:59:13 +0100946 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100947 ins_compl_longest_match(match);
948
949 return OK;
950}
951
952/*
953 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200954 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100955 */
956 static int
957ins_compl_equal(compl_T *match, char_u *str, int len)
958{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200959 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200960 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200961 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100962 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
963 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100964}
965
966/*
glepnir6a38aff2024-12-16 21:56:16 +0100967 * when len is -1 mean use whole length of p otherwise part of p
968 */
969 static void
970ins_compl_insert_bytes(char_u *p, int len)
971{
972 if (len == -1)
973 len = (int)STRLEN(p);
974 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100975 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100976}
977
978/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100979 * Checks if the column is within the currently inserted completion text
980 * column range. If it is, it returns a special highlight attribute.
glepnir76bdb822025-02-08 19:04:51 +0100981 * -1 means normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100982 */
983 int
glepnir76bdb822025-02-08 19:04:51 +0100984ins_compl_col_range_attr(linenr_T lnum, int col)
glepnir6a38aff2024-12-16 21:56:16 +0100985{
glepnir76bdb822025-02-08 19:04:51 +0100986 int start_col;
987 int attr;
988
989 if ((get_cot_flags() & COT_FUZZY)
990 || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0)
glepnire8908872025-01-08 18:30:45 +0100991 return -1;
992
glepnir76bdb822025-02-08 19:04:51 +0100993 start_col = compl_col + (int)ins_compl_leader_len();
994 if (!ins_compl_has_multiple())
995 return (col >= start_col && col < compl_ins_end_col) ? attr : -1;
996
997 // Multiple lines
998 if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) ||
999 (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) ||
1000 (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col))
1001 return attr;
glepnir6a38aff2024-12-16 21:56:16 +01001002
1003 return -1;
1004}
1005
1006/*
glepnir76bdb822025-02-08 19:04:51 +01001007 * Returns TRUE if the current completion string contains newline characters,
1008 * indicating it's a multi-line completion.
1009 */
1010 static int
1011ins_compl_has_multiple(void)
1012{
1013 return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL;
1014}
1015
1016/*
1017 * Returns TRUE if the given line number falls within the range of a multi-line
1018 * completion, i.e. between the starting line (compl_lnum) and current cursor
1019 * line. Always returns FALSE for single-line completions.
1020 */
1021 int
1022ins_compl_lnum_in_range(linenr_T lnum)
1023{
1024 if (!ins_compl_has_multiple())
1025 return FALSE;
1026 return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum;
1027}
1028
1029/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001030 * Reduce the longest common string for match "match".
1031 */
1032 static void
1033ins_compl_longest_match(compl_T *match)
1034{
1035 char_u *p, *s;
1036 int c1, c2;
1037 int had_match;
1038
John Marriott5e6ea922024-11-23 14:01:57 +01001039 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001040 {
1041 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +01001042 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
1043 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001044 return;
1045
John Marriott5e6ea922024-11-23 14:01:57 +01001046 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001047 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001048 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001049
1050 // When the match isn't there (to avoid matching itself) remove it
1051 // again after redrawing.
1052 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001053 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001054 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001055
1056 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001058
1059 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +01001060 p = compl_leader.string;
1061 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001062 while (*p != NUL)
1063 {
1064 if (has_mbyte)
1065 {
1066 c1 = mb_ptr2char(p);
1067 c2 = mb_ptr2char(s);
1068 }
1069 else
1070 {
1071 c1 = *p;
1072 c2 = *s;
1073 }
1074 if ((match->cp_flags & CP_ICASE)
1075 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
1076 break;
1077 if (has_mbyte)
1078 {
1079 MB_PTR_ADV(p);
1080 MB_PTR_ADV(s);
1081 }
1082 else
1083 {
1084 ++p;
1085 ++s;
1086 }
1087 }
1088
1089 if (*p != NUL)
1090 {
1091 // Leader was shortened, need to change the inserted text.
1092 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001093 compl_leader.length = (size_t)(p - compl_leader.string);
1094
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001095 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001096 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001097
1098 // When the match isn't there (to avoid matching itself) remove it
1099 // again after redrawing.
1100 if (!had_match)
1101 ins_compl_delete();
1102 }
1103
1104 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001105}
1106
1107/*
1108 * Add an array of matches to the list of matches.
1109 * Frees matches[].
1110 */
1111 static void
1112ins_compl_add_matches(
1113 int num_matches,
1114 char_u **matches,
1115 int icase)
1116{
1117 int i;
1118 int add_r = OK;
1119 int dir = compl_direction;
1120
1121 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001122 {
1123 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
glepnirf31cfa22025-03-06 21:59:13 +01001124 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0);
glepnir6e199932024-12-14 21:13:27 +01001125 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001126 // if dir was BACKWARD then honor it just once
1127 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001128 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001129 FreeWild(num_matches, matches);
1130}
1131
1132/*
1133 * Make the completion list cyclic.
1134 * Return the number of matches (excluding the original).
1135 */
1136 static int
1137ins_compl_make_cyclic(void)
1138{
1139 compl_T *match;
1140 int count = 0;
1141
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001142 if (compl_first_match == NULL)
1143 return 0;
1144
1145 // Find the end of the list.
1146 match = compl_first_match;
1147 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001148 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001149 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001150 match = match->cp_next;
1151 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001152 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001153 match->cp_next = compl_first_match;
1154 compl_first_match->cp_prev = match;
1155
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156 return count;
1157}
1158
1159/*
1160 * Return whether there currently is a shown match.
1161 */
1162 int
1163ins_compl_has_shown_match(void)
1164{
1165 return compl_shown_match == NULL
1166 || compl_shown_match != compl_shown_match->cp_next;
1167}
1168
1169/*
1170 * Return whether the shown match is long enough.
1171 */
1172 int
1173ins_compl_long_shown_match(void)
1174{
John Marriott5e6ea922024-11-23 14:01:57 +01001175 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001176 > curwin->w_cursor.col - compl_col;
1177}
1178
1179/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001180 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001181 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001182 unsigned int
1183get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001184{
zeertzjq529b9ad2024-06-05 20:27:06 +02001185 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001186}
1187
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001188/*
1189 * Update the screen and when there is any scrolling remove the popup menu.
1190 */
1191 static void
1192ins_compl_upd_pum(void)
1193{
1194 int h;
1195
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001196 if (compl_match_array == NULL)
1197 return;
1198
1199 h = curwin->w_cline_height;
1200 // Update the screen later, before drawing the popup menu over it.
1201 pum_call_update_screen();
1202 if (h != curwin->w_cline_height)
1203 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001204}
1205
1206/*
1207 * Remove any popup menu.
1208 */
1209 static void
1210ins_compl_del_pum(void)
1211{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001212 if (compl_match_array == NULL)
1213 return;
1214
1215 pum_undisplay();
1216 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001217}
1218
1219/*
1220 * Return TRUE if the popup menu should be displayed.
1221 */
1222 int
1223pum_wanted(void)
1224{
1225 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001226 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001227 return FALSE;
1228
1229 // The display looks bad on a B&W display.
1230 if (t_colors < 8
1231#ifdef FEAT_GUI
1232 && !gui.in_use
1233#endif
1234 )
1235 return FALSE;
1236 return TRUE;
1237}
1238
1239/*
1240 * Return TRUE if there are two or more matches to be shown in the popup menu.
1241 * One if 'completopt' contains "menuone".
1242 */
1243 static int
1244pum_enough_matches(void)
1245{
1246 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001247 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001248
1249 // Don't display the popup menu if there are no matches or there is only
1250 // one (ignoring the original text).
1251 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001252 do
1253 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001254 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001255 break;
1256 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001257 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001258
zeertzjq529b9ad2024-06-05 20:27:06 +02001259 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001260 return (i >= 1);
1261 return (i >= 2);
1262}
1263
Bram Moolenaar3075a452021-11-17 15:51:52 +00001264#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001265/*
1266 * Allocate Dict for the completed item.
1267 * { word, abbr, menu, kind, info }
1268 */
1269 static dict_T *
1270ins_compl_dict_alloc(compl_T *match)
1271{
1272 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1273
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001274 if (dict == NULL)
1275 return NULL;
1276
John Marriott5e6ea922024-11-23 14:01:57 +01001277 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001278 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1279 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1280 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1281 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1282 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1283 dict_add_string(dict, "user_data", (char_u *)"");
1284 else
1285 dict_add_tv(dict, "user_data", &match->cp_user_data);
1286
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001287 return dict;
1288}
1289
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001290/*
1291 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1292 * completion menu is changed.
1293 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001294 static void
1295trigger_complete_changed_event(int cur)
1296{
1297 dict_T *v_event;
1298 dict_T *item;
1299 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001300 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001301
1302 if (recursive)
1303 return;
1304
glepnir40891ba2025-02-10 22:18:00 +01001305 item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001306 if (item == NULL)
1307 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001308 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001309 dict_add_dict(v_event, "completed_item", item);
1310 pum_set_event_info(v_event);
1311 dict_set_items_ro(v_event);
1312
1313 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001314 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001315 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001316 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001317 recursive = FALSE;
1318
Bram Moolenaar3075a452021-11-17 15:51:52 +00001319 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001320}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001321#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001322
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001323/*
glepnira218cc62024-06-03 19:32:39 +02001324 * pumitem qsort compare func
1325 */
1326 static int
zeertzjq8e567472024-06-14 20:04:42 +02001327ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001328{
1329 const int sa = (*(pumitem_T *)a).pum_score;
1330 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001331 const int ia = (*(pumitem_T *)a).pum_idx;
1332 const int ib = (*(pumitem_T *)b).pum_idx;
1333 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001334}
1335
1336/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001337 * Build a popup menu to show the completion matches.
1338 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1339 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001340 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001341 static int
1342ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001343{
1344 compl_T *compl;
1345 compl_T *shown_compl = NULL;
1346 int did_find_shown_match = FALSE;
1347 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001348 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001349 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001350 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001351 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001352 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001353 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1354 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnirf31cfa22025-03-06 21:59:13 +01001355
glepnir80b66202024-11-27 21:53:53 +01001356 compl_T *match_head = NULL;
1357 compl_T *match_tail = NULL;
1358 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001359
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001360 // Need to build the popup menu list.
1361 compl_match_arraysize = 0;
1362 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001363
glepnira49c0772024-11-30 10:56:30 +01001364 // If the current match is the original text don't find the first
1365 // match after it, don't highlight anything.
1366 if (match_at_original_text(compl_shown_match))
1367 shown_match_ok = TRUE;
1368
1369 if (compl_leader.string != NULL
1370 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1371 && shown_match_ok == FALSE)
1372 compl_shown_match = compl_no_select ? compl_first_match
1373 : compl_first_match->cp_next;
1374
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001375 do
1376 {
glepnird4088ed2024-12-31 10:55:22 +01001377 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001378 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1379 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001380 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001381 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001382
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001383 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001384 && (compl_leader.string == NULL
1385 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001386 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001387 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001388 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001389 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001390 if (match_head == NULL)
1391 match_head = compl;
1392 else
glepnira49c0772024-11-30 10:56:30 +01001393 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001394 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001395
glepnirf400a0c2025-01-23 19:55:14 +01001396 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001397 {
1398 if (compl == compl_shown_match || did_find_shown_match)
1399 {
1400 // This item is the shown match or this is the
1401 // first displayed item after the shown match.
1402 compl_shown_match = compl;
1403 did_find_shown_match = TRUE;
1404 shown_match_ok = TRUE;
1405 }
1406 else
1407 // Remember this displayed match for when the
1408 // shown match is just below it.
1409 shown_compl = compl;
1410 cur = i;
1411 }
glepnirf400a0c2025-01-23 19:55:14 +01001412 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001413 {
1414 if (i == 0)
1415 shown_compl = compl;
1416 // Update the maximum fuzzy score and the shown match
1417 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001418 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001419 {
1420 did_find_shown_match = TRUE;
1421 max_fuzzy_score = compl->cp_score;
1422 if (!compl_no_select)
1423 compl_shown_match = compl;
1424 }
1425
glepnir3af0a8d2025-02-20 22:06:16 +01001426 if (!shown_match_ok && compl == compl_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001427 {
1428 cur = i;
1429 shown_match_ok = TRUE;
1430 }
1431 }
1432 i++;
glepnir80b66202024-11-27 21:53:53 +01001433 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001434
glepnirf400a0c2025-01-23 19:55:14 +01001435 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001436 {
1437 did_find_shown_match = TRUE;
1438
1439 // When the original text is the shown match don't set
1440 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001441 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001442 shown_match_ok = TRUE;
1443
1444 if (!shown_match_ok && shown_compl != NULL)
1445 {
1446 // The shown match isn't displayed, set it to the
1447 // previously displayed match.
1448 compl_shown_match = shown_compl;
1449 shown_match_ok = TRUE;
1450 }
1451 }
glepnira49c0772024-11-30 10:56:30 +01001452 compl = compl->cp_next;
1453 } while (compl != NULL && !is_first_match(compl));
1454
1455 if (compl_match_arraysize == 0)
1456 return -1;
1457
glepnirc0b7ca42025-02-13 20:27:44 +01001458 if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok)
1459 {
1460 compl_shown_match = shown_compl;
1461 shown_match_ok = TRUE;
1462 cur = 0;
1463 }
1464
glepnira49c0772024-11-30 10:56:30 +01001465 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1466 if (compl_match_array == NULL)
1467 return -1;
1468
1469 compl = match_head;
1470 i = 0;
1471 while (compl != NULL)
1472 {
glepnir6e199932024-12-14 21:13:27 +01001473 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1474 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001475 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1476 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1477 compl_match_array[i].pum_score = compl->cp_score;
1478 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1479 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001480 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1481 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001482 match_next = compl->cp_match_next;
1483 compl->cp_match_next = NULL;
1484 compl = match_next;
1485 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001486
zeertzjqd65aa1b2025-01-25 15:29:03 +01001487 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001488 {
1489 for (i = 0; i < compl_match_arraysize; i++)
1490 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001491 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001492 qsort(compl_match_array, (size_t)compl_match_arraysize,
1493 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001494 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001495 }
glepnira218cc62024-06-03 19:32:39 +02001496
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001497 if (!shown_match_ok) // no displayed match at all
1498 cur = -1;
1499
1500 return cur;
1501}
1502
1503/*
1504 * Show the popup menu for the list of matches.
1505 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1506 */
1507 void
1508ins_compl_show_pum(void)
1509{
1510 int i;
1511 int cur = -1;
1512 colnr_T col;
1513
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001514 if (!pum_wanted() || !pum_enough_matches())
1515 return;
1516
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001517 // Update the screen later, before drawing the popup menu over it.
1518 pum_call_update_screen();
1519
1520 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001521 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001522 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001523 else
1524 {
1525 // popup menu already exists, only need to find the current item.
1526 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001527 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001528 || compl_match_array[i].pum_text
1529 == compl_shown_match->cp_text[CPT_ABBR])
1530 {
1531 cur = i;
1532 break;
1533 }
1534 }
1535
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001536 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001537 {
1538#ifdef FEAT_EVAL
1539 if (compl_started && has_completechanged())
1540 trigger_complete_changed_event(cur);
1541#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001542 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001543 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001544
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001545 // In Replace mode when a $ is displayed at the end of the line only
1546 // part of the screen would be updated. We do need to redraw here.
1547 dollar_vcol = -1;
1548
1549 // Compute the screen column of the start of the completed text.
1550 // Use the cursor to get all wrapping and other settings right.
1551 col = curwin->w_cursor.col;
1552 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001553 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001554 pum_display(compl_match_array, compl_match_arraysize, cur);
1555 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001556
glepnircbb46b42024-02-03 18:11:13 +01001557 // After adding leader, set the current match to shown match.
1558 if (compl_started && compl_curr_match != compl_shown_match)
1559 compl_curr_match = compl_shown_match;
1560
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001561#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001562 if (has_completechanged())
1563 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001564#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001565}
1566
1567#define DICT_FIRST (1) // use just first element in "dict"
1568#define DICT_EXACT (2) // "dict" is the exact name of a file
1569
1570/*
glepnir40c1c332024-06-11 19:37:04 +02001571 * Get current completion leader
1572 */
1573 char_u *
1574ins_compl_leader(void)
1575{
John Marriott5e6ea922024-11-23 14:01:57 +01001576 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1577}
1578
1579/*
1580 * Get current completion leader length
1581 */
1582 size_t
1583ins_compl_leader_len(void)
1584{
1585 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001586}
1587
1588/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001589 * Add any identifiers that match the given pattern "pat" in the list of
1590 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001591 */
1592 static void
1593ins_compl_dictionaries(
1594 char_u *dict_start,
1595 char_u *pat,
1596 int flags, // DICT_FIRST and/or DICT_EXACT
1597 int thesaurus) // Thesaurus completion
1598{
1599 char_u *dict = dict_start;
1600 char_u *ptr;
1601 char_u *buf;
1602 regmatch_T regmatch;
1603 char_u **files;
1604 int count;
1605 int save_p_scs;
1606 int dir = compl_direction;
1607
1608 if (*dict == NUL)
1609 {
1610#ifdef FEAT_SPELL
1611 // When 'dictionary' is empty and spell checking is enabled use
1612 // "spell".
1613 if (!thesaurus && curwin->w_p_spell)
1614 dict = (char_u *)"spell";
1615 else
1616#endif
1617 return;
1618 }
1619
1620 buf = alloc(LSIZE);
1621 if (buf == NULL)
1622 return;
1623 regmatch.regprog = NULL; // so that we can goto theend
1624
1625 // If 'infercase' is set, don't use 'smartcase' here
1626 save_p_scs = p_scs;
1627 if (curbuf->b_p_inf)
1628 p_scs = FALSE;
1629
1630 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1631 // to only match at the start of a line. Otherwise just match the
1632 // pattern. Also need to double backslashes.
1633 if (ctrl_x_mode_line_or_eval())
1634 {
1635 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1636 size_t len;
1637
1638 if (pat_esc == NULL)
1639 goto theend;
1640 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001641 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001642 if (ptr == NULL)
1643 {
1644 vim_free(pat_esc);
1645 goto theend;
1646 }
1647 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1648 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1649 vim_free(pat_esc);
1650 vim_free(ptr);
1651 }
1652 else
1653 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001654 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001655 if (regmatch.regprog == NULL)
1656 goto theend;
1657 }
1658
1659 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1660 regmatch.rm_ic = ignorecase(pat);
1661 while (*dict != NUL && !got_int && !compl_interrupted)
1662 {
1663 // copy one dictionary file name into buf
1664 if (flags == DICT_EXACT)
1665 {
1666 count = 1;
1667 files = &dict;
1668 }
1669 else
1670 {
1671 // Expand wildcards in the dictionary name, but do not allow
1672 // backticks (for security, the 'dict' option may have been set in
1673 // a modeline).
1674 copy_option_part(&dict, buf, LSIZE, ",");
1675# ifdef FEAT_SPELL
1676 if (!thesaurus && STRCMP(buf, "spell") == 0)
1677 count = -1;
1678 else
1679# endif
1680 if (vim_strchr(buf, '`') != NULL
1681 || expand_wildcards(1, &buf, &count, &files,
1682 EW_FILE|EW_SILENT) != OK)
1683 count = 0;
1684 }
1685
1686# ifdef FEAT_SPELL
1687 if (count == -1)
1688 {
1689 // Complete from active spelling. Skip "\<" in the pattern, we
1690 // don't use it as a RE.
1691 if (pat[0] == '\\' && pat[1] == '<')
1692 ptr = pat + 2;
1693 else
1694 ptr = pat;
1695 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1696 }
1697 else
1698# endif
1699 if (count > 0) // avoid warning for using "files" uninit
1700 {
1701 ins_compl_files(count, files, thesaurus, flags,
glepnirf31cfa22025-03-06 21:59:13 +01001702 (cfc_has_mode() ? NULL : &regmatch), buf, &dir);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001703 if (flags != DICT_EXACT)
1704 FreeWild(count, files);
1705 }
1706 if (flags != 0)
1707 break;
1708 }
1709
1710theend:
1711 p_scs = save_p_scs;
1712 vim_regfree(regmatch.regprog);
1713 vim_free(buf);
1714}
1715
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001716/*
1717 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1718 * skipping the word at 'skip_word'. Returns OK on success.
1719 */
1720 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001721thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001722 char_u *fname,
1723 char_u **buf_arg,
1724 int dir,
1725 char_u *skip_word)
1726{
1727 int status = OK;
1728 char_u *ptr;
1729 char_u *wstart;
1730
1731 // Add the other matches on the line
1732 ptr = *buf_arg;
1733 while (!got_int)
1734 {
1735 // Find start of the next word. Skip white
1736 // space and punctuation.
1737 ptr = find_word_start(ptr);
1738 if (*ptr == NUL || *ptr == NL)
1739 break;
1740 wstart = ptr;
1741
1742 // Find end of the word.
1743 if (has_mbyte)
1744 // Japanese words may have characters in
1745 // different classes, only separate words
1746 // with single-byte non-word characters.
1747 while (*ptr != NUL)
1748 {
1749 int l = (*mb_ptr2len)(ptr);
1750
1751 if (l < 2 && !vim_iswordc(*ptr))
1752 break;
1753 ptr += l;
1754 }
1755 else
1756 ptr = find_word_end(ptr);
1757
1758 // Add the word. Skip the regexp match.
1759 if (wstart != skip_word)
1760 {
1761 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01001762 fname, dir, FALSE, 0);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001763 if (status == FAIL)
1764 break;
1765 }
1766 }
1767
1768 *buf_arg = ptr;
1769 return status;
1770}
1771
1772/*
1773 * Process "count" dictionary/thesaurus "files" and add the text matching
1774 * "regmatch".
1775 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001776 static void
1777ins_compl_files(
1778 int count,
1779 char_u **files,
1780 int thesaurus,
1781 int flags,
1782 regmatch_T *regmatch,
1783 char_u *buf,
1784 int *dir)
1785{
1786 char_u *ptr;
1787 int i;
1788 FILE *fp;
1789 int add_r;
glepnirf31cfa22025-03-06 21:59:13 +01001790 char_u *leader = NULL;
1791 int leader_len = 0;
glepnir58760162025-03-13 21:39:51 +01001792 int in_fuzzy_collect = cfc_has_mode();
glepnirf31cfa22025-03-06 21:59:13 +01001793 int score = 0;
1794 int len = 0;
1795 char_u *line_end = NULL;
1796
1797 if (in_fuzzy_collect)
1798 {
1799 leader = ins_compl_leader();
1800 leader_len = ins_compl_leader_len();
1801 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001802
1803 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1804 {
1805 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001806 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001807 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001808 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001809 vim_snprintf((char *)IObuff, IOSIZE,
1810 _("Scanning dictionary: %s"), (char *)files[i]);
1811 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1812 }
1813
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001814 if (fp == NULL)
1815 continue;
1816
1817 // Read dictionary file line by line.
1818 // Check each line for a match.
1819 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001821 ptr = buf;
glepnirf31cfa22025-03-06 21:59:13 +01001822 if (regmatch != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001823 {
glepnirf31cfa22025-03-06 21:59:13 +01001824 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001825 {
glepnirf31cfa22025-03-06 21:59:13 +01001826 ptr = regmatch->startp[0];
1827 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1828 : find_word_end(ptr);
1829 add_r = ins_compl_add_infercase(regmatch->startp[0],
1830 (int)(ptr - regmatch->startp[0]),
1831 p_ic, files[i], *dir, FALSE, 0);
1832 if (thesaurus)
1833 {
1834 // For a thesaurus, add all the words in the line
1835 ptr = buf;
1836 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
1837 regmatch->startp[0]);
1838 }
1839 if (add_r == OK)
1840 // if dir was BACKWARD then honor it just once
1841 *dir = FORWARD;
1842 else if (add_r == FAIL)
1843 break;
1844 // avoid expensive call to vim_regexec() when at end
1845 // of line
1846 if (*ptr == '\n' || got_int)
1847 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001848 }
glepnirf31cfa22025-03-06 21:59:13 +01001849 }
1850 else if (in_fuzzy_collect && leader_len > 0)
1851 {
1852 line_end = find_line_end(ptr);
1853 while (ptr < line_end)
1854 {
1855 if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score))
1856 {
1857 char_u *end_ptr = ctrl_x_mode_line_or_eval()
1858 ? find_line_end(ptr) : find_word_end(ptr);
1859 add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr),
1860 p_ic, files[i], *dir, FALSE, score);
1861 if (add_r == FAIL)
1862 break;
1863 ptr = end_ptr; // start from next word
1864 if (compl_get_longest && ctrl_x_mode_normal()
1865 && compl_first_match->cp_next
1866 && score == compl_first_match->cp_next->cp_score)
1867 compl_num_bests++;
1868 }
glepnirf31cfa22025-03-06 21:59:13 +01001869 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001870 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001871 line_breakcheck();
1872 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001873 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001874 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001875 }
1876}
1877
1878/*
1879 * Find the start of the next word.
1880 * Returns a pointer to the first char of the word. Also stops at a NUL.
1881 */
1882 char_u *
1883find_word_start(char_u *ptr)
1884{
1885 if (has_mbyte)
1886 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1887 ptr += (*mb_ptr2len)(ptr);
1888 else
1889 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1890 ++ptr;
1891 return ptr;
1892}
1893
1894/*
1895 * Find the end of the word. Assumes it starts inside a word.
1896 * Returns a pointer to just after the word.
1897 */
1898 char_u *
1899find_word_end(char_u *ptr)
1900{
1901 int start_class;
1902
1903 if (has_mbyte)
1904 {
1905 start_class = mb_get_class(ptr);
1906 if (start_class > 1)
1907 while (*ptr != NUL)
1908 {
1909 ptr += (*mb_ptr2len)(ptr);
1910 if (mb_get_class(ptr) != start_class)
1911 break;
1912 }
1913 }
1914 else
1915 while (vim_iswordc(*ptr))
1916 ++ptr;
1917 return ptr;
1918}
1919
1920/*
1921 * Find the end of the line, omitting CR and NL at the end.
1922 * Returns a pointer to just after the line.
1923 */
glepnirdd42b052025-03-08 16:52:55 +01001924 char_u *
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001925find_line_end(char_u *ptr)
1926{
1927 char_u *s;
1928
1929 s = ptr + STRLEN(ptr);
1930 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1931 --s;
1932 return s;
1933}
1934
1935/*
1936 * Free the list of completions
1937 */
1938 static void
1939ins_compl_free(void)
1940{
1941 compl_T *match;
1942 int i;
1943
John Marriott5e6ea922024-11-23 14:01:57 +01001944 VIM_CLEAR_STRING(compl_pattern);
1945 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001946
1947 if (compl_first_match == NULL)
1948 return;
1949
1950 ins_compl_del_pum();
1951 pum_clear();
1952
1953 compl_curr_match = compl_first_match;
1954 do
1955 {
1956 match = compl_curr_match;
1957 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001958 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001959 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001960 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001961 vim_free(match->cp_fname);
1962 for (i = 0; i < CPT_COUNT; ++i)
1963 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001964#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001965 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001966#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001967 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001968 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969 compl_first_match = compl_curr_match = NULL;
1970 compl_shown_match = NULL;
1971 compl_old_match = NULL;
1972}
1973
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001974/*
1975 * Reset/clear the completion state.
1976 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001977 void
1978ins_compl_clear(void)
1979{
1980 compl_cont_status = 0;
1981 compl_started = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01001982 compl_cfc_longest_ins = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001983 compl_matches = 0;
glepnir07f0dbe2025-02-18 20:27:30 +01001984 compl_selected_item = -1;
glepnir6a38aff2024-12-16 21:56:16 +01001985 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001986 VIM_CLEAR_STRING(compl_pattern);
1987 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001988 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001989 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001990 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001991#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992 // clear v:completed_item
1993 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001994#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001995}
1996
1997/*
1998 * Return TRUE when Insert completion is active.
1999 */
2000 int
2001ins_compl_active(void)
2002{
2003 return compl_started;
2004}
2005
2006/*
glepnir8d0bb6d2024-12-24 09:44:35 +01002007 * Return True when wp is the actual completion window
2008 */
2009 int
2010ins_compl_win_active(win_T *wp UNUSED)
2011{
2012 return ins_compl_active()
2013#if defined(FEAT_QUICKFIX)
2014 && (!wp->w_p_pvw
2015# ifdef FEAT_PROP_POPUP
2016 && !(wp->w_popup_flags & POPF_INFO)
2017# endif
2018 )
2019#endif
2020 ;
2021}
2022
2023/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002024 * Selected one of the matches. When FALSE the match was edited or using the
2025 * longest common string.
2026 */
2027 int
2028ins_compl_used_match(void)
2029{
2030 return compl_used_match;
2031}
2032
2033/*
2034 * Initialize get longest common string.
2035 */
2036 void
2037ins_compl_init_get_longest(void)
2038{
2039 compl_get_longest = FALSE;
2040}
2041
2042/*
2043 * Returns TRUE when insert completion is interrupted.
2044 */
2045 int
2046ins_compl_interrupted(void)
2047{
2048 return compl_interrupted;
2049}
2050
2051/*
2052 * Returns TRUE if the <Enter> key selects a match in the completion popup
2053 * menu.
2054 */
2055 int
2056ins_compl_enter_selects(void)
2057{
2058 return compl_enter_selects;
2059}
2060
2061/*
2062 * Return the column where the text starts that is being completed
2063 */
2064 colnr_T
2065ins_compl_col(void)
2066{
2067 return compl_col;
2068}
2069
2070/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002071 * Return the length in bytes of the text being completed
2072 */
2073 int
2074ins_compl_len(void)
2075{
2076 return compl_length;
2077}
2078
2079/*
glepnir94a045e2025-03-01 16:12:23 +01002080 * Return TRUE when the 'completeopt' "preinsert" flag is in effect,
2081 * otherwise return FALSE.
glepniredd4ac32025-01-29 18:53:51 +01002082 */
2083 static int
2084ins_compl_has_preinsert(void)
2085{
glepnira2c55592025-02-28 17:43:42 +01002086 int cur_cot_flags = get_cot_flags();
glepnir94a045e2025-03-01 16:12:23 +01002087 return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE))
2088 == (COT_PREINSERT | COT_MENUONE);
glepniredd4ac32025-01-29 18:53:51 +01002089}
2090
2091/*
2092 * Returns TRUE if the pre-insert effect is valid and the cursor is within
2093 * the `compl_ins_end_col` range.
2094 */
2095 int
2096ins_compl_preinsert_effect(void)
2097{
2098 if (!ins_compl_has_preinsert())
2099 return FALSE;
2100
2101 return curwin->w_cursor.col < compl_ins_end_col;
2102}
2103
2104/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002105 * Delete one character before the cursor and show the subset of the matches
2106 * that match the word that is now before the cursor.
2107 * Returns the character to be used, NUL if the work is done and another char
2108 * to be got from the user.
2109 */
2110 int
2111ins_compl_bs(void)
2112{
2113 char_u *line;
2114 char_u *p;
2115
glepniredd4ac32025-01-29 18:53:51 +01002116 if (ins_compl_preinsert_effect())
2117 ins_compl_delete();
2118
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002119 line = ml_get_curline();
2120 p = line + curwin->w_cursor.col;
2121 MB_PTR_BACK(line, p);
2122
2123 // Stop completion when the whole word was deleted. For Omni completion
2124 // allow the word to be deleted, we won't match everything.
2125 // Respect the 'backspace' option.
2126 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002127 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2128 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002129 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2130 - compl_length < 0))
2131 return K_BS;
2132
2133 // Deleted more than what was used to find matches or didn't finish
2134 // finding all matches: need to look for matches all over again.
2135 if (curwin->w_cursor.col <= compl_col + compl_length
2136 || ins_compl_need_restart())
2137 ins_compl_restart();
2138
John Marriott5e6ea922024-11-23 14:01:57 +01002139 VIM_CLEAR_STRING(compl_leader);
2140 compl_leader.length = (size_t)((p - line) - compl_col);
2141 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2142 if (compl_leader.string == NULL)
2143 {
2144 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002145 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002146 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002147
2148 ins_compl_new_leader();
2149 if (compl_shown_match != NULL)
2150 // Make sure current match is not a hidden item.
2151 compl_curr_match = compl_shown_match;
2152 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002153}
2154
2155/*
2156 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2157 * be called.
2158 */
2159 static int
2160ins_compl_need_restart(void)
2161{
2162 // Return TRUE if we didn't complete finding matches or when the
2163 // 'completefunc' returned "always" in the "refresh" dictionary item.
2164 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002165 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002166 && compl_opt_refresh_always);
2167}
2168
2169/*
2170 * Called after changing "compl_leader".
2171 * Show the popup menu with a different set of matches.
2172 * May also search for matches again if the previous search was interrupted.
2173 */
2174 static void
2175ins_compl_new_leader(void)
2176{
2177 ins_compl_del_pum();
2178 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002179 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002180 compl_used_match = FALSE;
2181
2182 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002183 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002184 else
2185 {
2186#ifdef FEAT_SPELL
2187 spell_bad_len = 0; // need to redetect bad word
2188#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002189 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002190 // the popup menu display the changed text before the cursor. Set
2191 // "compl_restarting" to avoid that the first match is inserted.
2192 pum_call_update_screen();
2193#ifdef FEAT_GUI
2194 if (gui.in_use)
2195 {
2196 // Show the cursor after the match, not after the redrawn text.
2197 setcursor();
2198 out_flush_cursor(FALSE, FALSE);
2199 }
2200#endif
2201 compl_restarting = TRUE;
2202 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2203 compl_cont_status = 0;
2204 compl_restarting = FALSE;
2205 }
2206
glepnir44180412025-02-20 22:09:48 +01002207 compl_enter_selects = !compl_used_match && compl_selected_item != -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002208
2209 // Show the popup menu with a different set of matches.
2210 ins_compl_show_pum();
2211
2212 // Don't let Enter select the original text when there is no popup menu.
2213 if (compl_match_array == NULL)
2214 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002215 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2216 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002217}
2218
2219/*
2220 * Return the length of the completion, from the completion start column to
2221 * the cursor column. Making sure it never goes below zero.
2222 */
2223 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002224get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002225{
2226 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002227 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002228}
2229
2230/*
2231 * Append one character to the match leader. May reduce the number of
2232 * matches.
2233 */
2234 void
2235ins_compl_addleader(int c)
2236{
2237 int cc;
2238
glepniredd4ac32025-01-29 18:53:51 +01002239 if (ins_compl_preinsert_effect())
2240 ins_compl_delete();
2241
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002242 if (stop_arrow() == FAIL)
2243 return;
2244 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2245 {
2246 char_u buf[MB_MAXBYTES + 1];
2247
2248 (*mb_char2bytes)(c, buf);
2249 buf[cc] = NUL;
2250 ins_char_bytes(buf, cc);
2251 if (compl_opt_refresh_always)
2252 AppendToRedobuff(buf);
2253 }
2254 else
2255 {
2256 ins_char(c);
2257 if (compl_opt_refresh_always)
2258 AppendCharToRedobuff(c);
2259 }
2260
2261 // If we didn't complete finding matches we must search again.
2262 if (ins_compl_need_restart())
2263 ins_compl_restart();
2264
2265 // When 'always' is set, don't reset compl_leader. While completing,
2266 // cursor doesn't point original position, changing compl_leader would
2267 // break redo.
2268 if (!compl_opt_refresh_always)
2269 {
John Marriott5e6ea922024-11-23 14:01:57 +01002270 VIM_CLEAR_STRING(compl_leader);
2271 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2272 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2273 compl_leader.length);
2274 if (compl_leader.string == NULL)
2275 {
2276 compl_leader.length = 0;
2277 return;
2278 }
2279
2280 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002281 }
2282}
2283
2284/*
2285 * Setup for finding completions again without leaving CTRL-X mode. Used when
2286 * BS or a key was typed while still searching for matches.
2287 */
2288 static void
2289ins_compl_restart(void)
2290{
2291 ins_compl_free();
2292 compl_started = FALSE;
2293 compl_matches = 0;
2294 compl_cont_status = 0;
2295 compl_cont_mode = 0;
2296}
2297
2298/*
2299 * Set the first match, the original text.
2300 */
2301 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002302ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002303{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002304 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002305 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2306 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002307 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002308 {
John Marriott5e6ea922024-11-23 14:01:57 +01002309 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002310 if (p != NULL)
2311 {
John Marriott5e6ea922024-11-23 14:01:57 +01002312 VIM_CLEAR_STRING(compl_first_match->cp_str);
2313 compl_first_match->cp_str.string = p;
2314 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002315 }
2316 }
2317 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002318 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002319 {
John Marriott5e6ea922024-11-23 14:01:57 +01002320 char_u *p = vim_strnsave(str, len);
2321 if (p != NULL)
2322 {
2323 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2324 compl_first_match->cp_prev->cp_str.string = p;
2325 compl_first_match->cp_prev->cp_str.length = len;
2326 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002327 }
2328}
2329
2330/*
2331 * Append one character to the match leader. May reduce the number of
2332 * matches.
2333 */
2334 void
2335ins_compl_addfrommatch(void)
2336{
2337 char_u *p;
2338 int len = (int)curwin->w_cursor.col - (int)compl_col;
2339 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002340
John Marriott5e6ea922024-11-23 14:01:57 +01002341 p = compl_shown_match->cp_str.string;
2342 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002343 {
John Marriott5e6ea922024-11-23 14:01:57 +01002344 size_t plen;
2345 compl_T *cp;
2346
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002347 // When still at the original match use the first entry that matches
2348 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002349 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002350 return;
2351
2352 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002353 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002354 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002355 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002356 {
John Marriott5e6ea922024-11-23 14:01:57 +01002357 if (compl_leader.string == NULL
2358 || ins_compl_equal(cp, compl_leader.string,
2359 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002360 {
John Marriott5e6ea922024-11-23 14:01:57 +01002361 p = cp->cp_str.string;
2362 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002363 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002364 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002365 }
John Marriott5e6ea922024-11-23 14:01:57 +01002366 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002367 return;
2368 }
2369 p += len;
2370 c = PTR2CHAR(p);
2371 ins_compl_addleader(c);
2372}
2373
2374/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002375 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002376 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2377 * compl_cont_mode and compl_cont_status.
2378 * Returns TRUE when the character is not to be inserted.
2379 */
2380 static int
2381set_ctrl_x_mode(int c)
2382{
2383 int retval = FALSE;
2384
2385 switch (c)
2386 {
2387 case Ctrl_E:
2388 case Ctrl_Y:
2389 // scroll the window one line up or down
2390 ctrl_x_mode = CTRL_X_SCROLL;
2391 if (!(State & REPLACE_FLAG))
2392 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2393 else
2394 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2395 edit_submode_pre = NULL;
2396 showmode();
2397 break;
2398 case Ctrl_L:
2399 // complete whole line
2400 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2401 break;
2402 case Ctrl_F:
2403 // complete filenames
2404 ctrl_x_mode = CTRL_X_FILES;
2405 break;
2406 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002407 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002408 ctrl_x_mode = CTRL_X_DICTIONARY;
2409 break;
2410 case Ctrl_R:
2411 // Register insertion without exiting CTRL-X mode
2412 // Simply allow ^R to happen without affecting ^X mode
2413 break;
2414 case Ctrl_T:
2415 // complete words from a thesaurus
2416 ctrl_x_mode = CTRL_X_THESAURUS;
2417 break;
2418#ifdef FEAT_COMPL_FUNC
2419 case Ctrl_U:
2420 // user defined completion
2421 ctrl_x_mode = CTRL_X_FUNCTION;
2422 break;
2423 case Ctrl_O:
2424 // omni completion
2425 ctrl_x_mode = CTRL_X_OMNI;
2426 break;
2427#endif
2428 case 's':
2429 case Ctrl_S:
2430 // complete spelling suggestions
2431 ctrl_x_mode = CTRL_X_SPELL;
2432#ifdef FEAT_SPELL
2433 ++emsg_off; // Avoid getting the E756 error twice.
2434 spell_back_to_badword();
2435 --emsg_off;
2436#endif
2437 break;
2438 case Ctrl_RSB:
2439 // complete tag names
2440 ctrl_x_mode = CTRL_X_TAGS;
2441 break;
2442#ifdef FEAT_FIND_ID
2443 case Ctrl_I:
2444 case K_S_TAB:
2445 // complete keywords from included files
2446 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2447 break;
2448 case Ctrl_D:
2449 // complete definitions from included files
2450 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2451 break;
2452#endif
2453 case Ctrl_V:
2454 case Ctrl_Q:
2455 // complete vim commands
2456 ctrl_x_mode = CTRL_X_CMDLINE;
2457 break;
2458 case Ctrl_Z:
2459 // stop completion
2460 ctrl_x_mode = CTRL_X_NORMAL;
2461 edit_submode = NULL;
2462 showmode();
2463 retval = TRUE;
2464 break;
2465 case Ctrl_P:
2466 case Ctrl_N:
2467 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2468 // just started ^X mode, or there were enough ^X's to cancel
2469 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2470 // do normal expansion when interrupting a different mode (say
2471 // ^X^F^X^P or ^P^X^X^P, see below)
2472 // nothing changes if interrupting mode 0, (eg, the flag
2473 // doesn't change when going to ADDING mode -- Acevedo
2474 if (!(compl_cont_status & CONT_INTRPT))
2475 compl_cont_status |= CONT_LOCAL;
2476 else if (compl_cont_mode != 0)
2477 compl_cont_status &= ~CONT_LOCAL;
2478 // FALLTHROUGH
2479 default:
2480 // If we have typed at least 2 ^X's... for modes != 0, we set
2481 // compl_cont_status = 0 (eg, as if we had just started ^X
2482 // mode).
2483 // For mode 0, we set "compl_cont_mode" to an impossible
2484 // value, in both cases ^X^X can be used to restart the same
2485 // mode (avoiding ADDING mode).
2486 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2487 // 'complete' and local ^P expansions respectively.
2488 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2489 // mode -- Acevedo
2490 if (c == Ctrl_X)
2491 {
2492 if (compl_cont_mode != 0)
2493 compl_cont_status = 0;
2494 else
2495 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2496 }
2497 ctrl_x_mode = CTRL_X_NORMAL;
2498 edit_submode = NULL;
2499 showmode();
2500 break;
2501 }
2502
2503 return retval;
2504}
2505
2506/*
glepnir1c5a1202024-12-04 20:27:34 +01002507 * Trigger CompleteDone event and adds relevant information to v:event
2508 */
2509 static void
2510trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2511{
2512#if defined(FEAT_EVAL)
2513 save_v_event_T save_v_event;
2514 dict_T *v_event = get_v_event(&save_v_event);
2515 char_u *mode_str = NULL;
2516
2517 mode = mode & ~CTRL_X_WANT_IDENT;
2518 if (ctrl_x_mode_names[mode])
2519 mode_str = (char_u *)ctrl_x_mode_names[mode];
2520
2521 (void)dict_add_string(v_event, "complete_word",
2522 word == NULL ? (char_u *)"" : word);
2523 (void)dict_add_string(v_event, "complete_type",
2524 mode_str != NULL ? mode_str : (char_u *)"");
2525
2526 dict_set_items_ro(v_event);
2527#endif
2528 ins_apply_autocmds(EVENT_COMPLETEDONE);
2529
2530#if defined(FEAT_EVAL)
2531 restore_v_event(v_event, &save_v_event);
2532#endif
2533}
2534
2535/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002536 * Stop insert completion mode
2537 */
2538 static int
2539ins_compl_stop(int c, int prev_mode, int retval)
2540{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002541 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002542 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002543
2544 // Get here when we have finished typing a sequence of ^N and
2545 // ^P or other completion characters in CTRL-X mode. Free up
2546 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002547 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002548 {
glepnir40891ba2025-02-10 22:18:00 +01002549 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002550
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002551 // If any of the original typed text has been changed, eg when
2552 // ignorecase is set, we must add back-spaces to the redo
2553 // buffer. We add as few as necessary to delete just the part
2554 // of the original text that has changed.
2555 // When using the longest match, edited the match or used
2556 // CTRL-E then don't use the current match.
2557 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002558 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002559 ins_compl_fixRedoBufForLeader(ptr);
2560 }
2561
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002562 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002563
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002564 // When completing whole lines: fix indent for 'cindent'.
2565 // Otherwise, break line if it's too long.
2566 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2567 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002568 // re-indent the current line
2569 if (want_cindent)
2570 {
2571 do_c_expr_indent();
2572 want_cindent = FALSE; // don't do it again
2573 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002574 }
2575 else
2576 {
2577 int prev_col = curwin->w_cursor.col;
2578
2579 // put the cursor on the last char, for 'tw' formatting
2580 if (prev_col > 0)
2581 dec_cursor();
2582 // only format when something was inserted
2583 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2584 insertchar(NUL, 0, -1);
2585 if (prev_col > 0
2586 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2587 inc_cursor();
2588 }
2589
2590 // If the popup menu is displayed pressing CTRL-Y means accepting
2591 // the selection without inserting anything. When
2592 // compl_enter_selects is set the Enter key does the same.
2593 if ((c == Ctrl_Y || (compl_enter_selects
2594 && (c == CAR || c == K_KENTER || c == NL)))
2595 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002596 {
2597 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002598 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002599 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002600
2601 // CTRL-E means completion is Ended, go back to the typed text.
2602 // but only do this, if the Popup is still visible
2603 if (c == Ctrl_E)
2604 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002605 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002606 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002607
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002608 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002609 if (compl_leader.string != NULL)
2610 {
2611 p = compl_leader.string;
2612 plen = compl_leader.length;
2613 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002614 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002615 {
2616 p = compl_orig_text.string;
2617 plen = compl_orig_text.length;
2618 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002619 if (p != NULL)
2620 {
2621 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002622
John Marriott5e6ea922024-11-23 14:01:57 +01002623 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002624 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002625 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002626 retval = TRUE;
2627 }
2628
glepnir001c26c2025-02-02 09:36:22 +01002629 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2630 ins_compl_delete();
2631
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002632 auto_format(FALSE, TRUE);
2633
2634 // Trigger the CompleteDonePre event to give scripts a chance to
2635 // act upon the completion before clearing the info, and restore
2636 // ctrl_x_mode, so that complete_info() can be used.
2637 ctrl_x_mode = prev_mode;
2638 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2639
2640 ins_compl_free();
2641 compl_started = FALSE;
2642 compl_matches = 0;
2643 if (!shortmess(SHM_COMPLETIONMENU))
2644 msg_clr_cmdline(); // necessary for "noshowmode"
2645 ctrl_x_mode = CTRL_X_NORMAL;
2646 compl_enter_selects = FALSE;
2647 if (edit_submode != NULL)
2648 {
2649 edit_submode = NULL;
2650 showmode();
2651 }
2652
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002653 if (c == Ctrl_C && cmdwin_type != 0)
2654 // Avoid the popup menu remains displayed when leaving the
2655 // command line window.
2656 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002657 // Indent now if a key was typed that is in 'cinkeys'.
2658 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2659 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002660 // Trigger the CompleteDone event to give scripts a chance to act
2661 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002662 trigger_complete_done_event(prev_mode, word);
2663 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002664
2665 return retval;
2666}
2667
2668/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002669 * Prepare for Insert mode completion, or stop it.
2670 * Called just after typing a character in Insert mode.
2671 * Returns TRUE when the character is not to be inserted;
2672 */
2673 int
2674ins_compl_prep(int c)
2675{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002676 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002677 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002678
2679 // Forget any previous 'special' messages if this is actually
2680 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2681 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2682 edit_submode_extra = NULL;
2683
zeertzjq440d4cb2023-03-02 17:51:32 +00002684 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002685 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002686 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002687 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002688 return retval;
2689
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002690#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002691 // Ignore mouse events in a popup window
2692 if (is_mouse_key(c))
2693 {
2694 // Ignore drag and release events, the position does not need to be in
2695 // the popup and it may have just closed.
2696 if (c == K_LEFTRELEASE
2697 || c == K_LEFTRELEASE_NM
2698 || c == K_MIDDLERELEASE
2699 || c == K_RIGHTRELEASE
2700 || c == K_X1RELEASE
2701 || c == K_X2RELEASE
2702 || c == K_LEFTDRAG
2703 || c == K_MIDDLEDRAG
2704 || c == K_RIGHTDRAG
2705 || c == K_X1DRAG
2706 || c == K_X2DRAG)
2707 return retval;
2708 if (popup_visible)
2709 {
2710 int row = mouse_row;
2711 int col = mouse_col;
2712 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2713
2714 if (wp != NULL && WIN_IS_POPUP(wp))
2715 return retval;
2716 }
2717 }
2718#endif
2719
zeertzjqdca29d92021-08-31 19:12:51 +02002720 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2721 {
2722 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2723 || !vim_is_ctrl_x_key(c))
2724 {
2725 // Not starting another completion mode.
2726 ctrl_x_mode = CTRL_X_CMDLINE;
2727
2728 // CTRL-X CTRL-Z should stop completion without inserting anything
2729 if (c == Ctrl_Z)
2730 retval = TRUE;
2731 }
2732 else
2733 {
2734 ctrl_x_mode = CTRL_X_CMDLINE;
2735
2736 // Other CTRL-X keys first stop completion, then start another
2737 // completion mode.
2738 ins_compl_prep(' ');
2739 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2740 }
2741 }
2742
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002743 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002744 if (ctrl_x_mode_not_defined_yet()
2745 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002746 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002747 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002748 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002749 }
2750
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002751 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2753 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002754 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002755 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002756 {
2757 // We're already in CTRL-X mode, do we stay in it?
2758 if (!vim_is_ctrl_x_key(c))
2759 {
glepnir40891ba2025-02-10 22:18:00 +01002760 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002761 edit_submode = NULL;
2762 }
2763 showmode();
2764 }
2765
2766 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2767 {
2768 // Show error message from attempted keyword completion (probably
2769 // 'Pattern not found') until another key is hit, then go back to
2770 // showing what mode we are in.
2771 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002772 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002773 && c != Ctrl_R && !ins_compl_pum_key(c))
2774 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002775 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002776 }
2777 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2778 // Trigger the CompleteDone event to give scripts a chance to act
2779 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002780 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002781
LemonBoy2bf52dd2022-04-09 18:17:34 +01002782 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002783
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002784 // reset continue_* if we left expansion-mode, if we stay they'll be
2785 // (re)set properly in ins_complete()
2786 if (!vim_is_ctrl_x_key(c))
2787 {
2788 compl_cont_status = 0;
2789 compl_cont_mode = 0;
2790 }
2791
2792 return retval;
2793}
2794
2795/*
2796 * Fix the redo buffer for the completion leader replacing some of the typed
2797 * text. This inserts backspaces and appends the changed text.
2798 * "ptr" is the known leader text or NUL.
2799 */
2800 static void
2801ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2802{
John Marriott5e6ea922024-11-23 14:01:57 +01002803 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002804 char_u *p;
2805 char_u *ptr = ptr_arg;
2806
2807 if (ptr == NULL)
2808 {
John Marriott5e6ea922024-11-23 14:01:57 +01002809 if (compl_leader.string != NULL)
2810 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002811 else
2812 return; // nothing to do
2813 }
John Marriott5e6ea922024-11-23 14:01:57 +01002814 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002815 {
John Marriott5e6ea922024-11-23 14:01:57 +01002816 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002817 // Find length of common prefix between original text and new completion
2818 while (p[len] != NUL && p[len] == ptr[len])
2819 len++;
2820 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002821 if (len > 0)
2822 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002823 // Add backspace characters for each remaining character in
2824 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002825 for (p += len; *p != NUL; MB_PTR_ADV(p))
2826 AppendCharToRedobuff(K_BS);
2827 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002828 if (ptr != NULL)
2829 AppendToRedobuffLit(ptr + len, -1);
2830}
2831
2832/*
2833 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2834 * (depending on flag) starting from buf and looking for a non-scanned
2835 * buffer (other than curbuf). curbuf is special, if it is called with
2836 * buf=curbuf then it has to be the first call for a given flag/expansion.
2837 *
2838 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2839 */
2840 static buf_T *
2841ins_compl_next_buf(buf_T *buf, int flag)
2842{
2843 static win_T *wp = NULL;
glepnir40891ba2025-02-10 22:18:00 +01002844 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002845
2846 if (flag == 'w') // just windows
2847 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002848 if (buf == curbuf || !win_valid(wp))
2849 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002850 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01002851
2852 while (TRUE)
2853 {
2854 // Move to next window (wrap to first window if at the end)
2855 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2856 // Break if we're back at start or found an unscanned buffer
2857 if (wp == curwin || !wp->w_buffer->b_scanned)
2858 break;
2859 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002860 buf = wp->w_buffer;
2861 }
2862 else
glepnir40891ba2025-02-10 22:18:00 +01002863 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002864 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2865 // (unlisted buffers)
2866 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01002867 while (TRUE)
2868 {
2869 // Move to next buffer (wrap to first buffer if at the end)
2870 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2871 // Break if we're back at start buffer
2872 if (buf == curbuf)
2873 break;
2874
2875 // Check buffer conditions based on flag
2876 if (flag == 'U')
2877 skip_buffer = buf->b_p_bl;
2878 else
2879 skip_buffer = !buf->b_p_bl ||
2880 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2881
2882 // Break if we found a buffer that matches our criteria
2883 if (!skip_buffer && !buf->b_scanned)
2884 break;
2885 }
2886 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002887 return buf;
2888}
2889
2890#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002891
2892# ifdef FEAT_EVAL
2893static callback_T cfu_cb; // 'completefunc' callback function
2894static callback_T ofu_cb; // 'omnifunc' callback function
2895static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2896# endif
2897
2898/*
2899 * Copy a global callback function to a buffer local callback.
2900 */
2901 static void
2902copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2903{
2904 free_callback(bufcb);
2905 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2906 copy_callback(bufcb, globcb);
2907}
2908
2909/*
2910 * Parse the 'completefunc' option value and set the callback function.
2911 * Invoked when the 'completefunc' option is set. The option value can be a
2912 * name of a function (string), or function(<name>) or funcref(<name>) or a
2913 * lambda expression.
2914 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002915 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002916did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002917{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002918 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2919 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002920
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002921 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002922
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002923 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002924}
2925
2926/*
2927 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002928 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002929 */
2930 void
2931set_buflocal_cfu_callback(buf_T *buf UNUSED)
2932{
2933# ifdef FEAT_EVAL
2934 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2935# endif
2936}
2937
2938/*
2939 * Parse the 'omnifunc' option value and set the callback function.
2940 * Invoked when the 'omnifunc' option is set. The option value can be a
2941 * name of a function (string), or function(<name>) or funcref(<name>) or a
2942 * lambda expression.
2943 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002944 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002945did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002946{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002947 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2948 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002949
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002950 set_buflocal_ofu_callback(curbuf);
2951 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002952}
2953
2954/*
2955 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002956 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002957 */
2958 void
2959set_buflocal_ofu_callback(buf_T *buf UNUSED)
2960{
2961# ifdef FEAT_EVAL
2962 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2963# endif
2964}
2965
2966/*
2967 * Parse the 'thesaurusfunc' option value and set the callback function.
2968 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2969 * name of a function (string), or function(<name>) or funcref(<name>) or a
2970 * lambda expression.
2971 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002972 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002973did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002974{
2975 int retval;
2976
zeertzjq6eda2692024-11-03 09:23:33 +01002977 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002978 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002979 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2980 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002981 else
2982 {
2983 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002984 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002985 // when using :set, free the local callback
2986 if (!(args->os_flags & OPT_GLOBAL))
2987 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002988 }
2989
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002990 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002991}
2992
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002993/*
2994 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002995 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002996 */
2997 int
2998set_ref_in_insexpand_funcs(int copyID)
2999{
3000 int abort = FALSE;
3001
3002 abort = set_ref_in_callback(&cfu_cb, copyID);
3003 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
3004 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
3005
3006 return abort;
3007}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003008
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003009/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003010 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003011 */
3012 static char_u *
3013get_complete_funcname(int type)
3014{
3015 switch (type)
3016 {
3017 case CTRL_X_FUNCTION:
3018 return curbuf->b_p_cfu;
3019 case CTRL_X_OMNI:
3020 return curbuf->b_p_ofu;
3021 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003022 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003023 default:
3024 return (char_u *)"";
3025 }
3026}
3027
3028/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003029 * Get the callback to use for insert mode completion.
3030 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003031 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003032get_insert_callback(int type)
3033{
3034 if (type == CTRL_X_FUNCTION)
3035 return &curbuf->b_cfu_cb;
3036 if (type == CTRL_X_OMNI)
3037 return &curbuf->b_ofu_cb;
3038 // CTRL_X_THESAURUS
3039 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
3040}
3041
3042/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003043 * Execute user defined complete function 'completefunc', 'omnifunc' or
3044 * 'thesaurusfunc', and get matches in "matches".
3045 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003046 */
3047 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003048expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003049{
3050 list_T *matchlist = NULL;
3051 dict_T *matchdict = NULL;
3052 typval_T args[3];
3053 char_u *funcname;
3054 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003055 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003056 typval_T rettv;
3057 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003058 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003059
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003060 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003061 if (*funcname == NUL)
3062 return;
3063
3064 // Call 'completefunc' to obtain the list of matches.
3065 args[0].v_type = VAR_NUMBER;
3066 args[0].vval.v_number = 0;
3067 args[1].v_type = VAR_STRING;
3068 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
3069 args[2].v_type = VAR_UNKNOWN;
3070
3071 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01003072 // Lock the text to avoid weird things from happening. Also disallow
3073 // switching to another window, it should not be needed and may end up in
3074 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01003075 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003076
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003077 cb = get_insert_callback(type);
3078 retval = call_callback(cb, 0, &rettv, 2, args);
3079
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003080 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003081 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003082 {
3083 switch (rettv.v_type)
3084 {
3085 case VAR_LIST:
3086 matchlist = rettv.vval.v_list;
3087 break;
3088 case VAR_DICT:
3089 matchdict = rettv.vval.v_dict;
3090 break;
3091 case VAR_SPECIAL:
3092 if (rettv.vval.v_number == VVAL_NONE)
3093 compl_opt_suppress_empty = TRUE;
3094 // FALLTHROUGH
3095 default:
3096 // TODO: Give error message?
3097 clear_tv(&rettv);
3098 break;
3099 }
3100 }
zeertzjqcfe45652022-05-27 17:26:55 +01003101 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003102
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003103 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003104 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003105 validate_cursor();
3106 if (!EQUAL_POS(curwin->w_cursor, pos))
3107 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003108 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003109 goto theend;
3110 }
3111
3112 if (matchlist != NULL)
3113 ins_compl_add_list(matchlist);
3114 else if (matchdict != NULL)
3115 ins_compl_add_dict(matchdict);
3116
3117theend:
3118 // Restore State, it might have been changed.
3119 State = save_State;
3120
3121 if (matchdict != NULL)
3122 dict_unref(matchdict);
3123 if (matchlist != NULL)
3124 list_unref(matchlist);
3125}
3126#endif // FEAT_COMPL_FUNC
3127
3128#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003129
3130 static inline int
3131get_user_highlight_attr(char_u *hlname)
3132{
3133 if (hlname != NULL && *hlname != NUL)
3134 return syn_name2attr(hlname);
3135 return -1;
3136}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003137/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003138 * Add a match to the list of matches from a typeval_T.
3139 * If the given string is already in the list of completions, then return
3140 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3141 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003142 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003143 */
3144 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003145ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003146{
3147 char_u *word;
3148 int dup = FALSE;
3149 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003150 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003151 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003152 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003153 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003154 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003155 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003156 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003157
Bram Moolenaar08928322020-01-04 14:32:48 +01003158 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003159 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3160 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003161 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3162 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3163 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3164 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3165 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003166
glepnir0fe17f82024-10-08 22:26:44 +02003167 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003168 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003169
3170 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003171 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003172
Bram Moolenaard61efa52022-07-23 09:52:04 +01003173 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3174 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3175 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003176 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003177 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3178 dup = dict_get_number(tv->vval.v_dict, "dup");
3179 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3180 empty = dict_get_number(tv->vval.v_dict, "empty");
3181 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3182 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003183 flags |= CP_EQUAL;
3184 }
3185 else
3186 {
3187 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003188 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003189 }
3190 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003191 {
3192 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003193 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003194 }
glepnir508e7852024-07-25 21:39:08 +02003195 status = ins_compl_add(word, -1, NULL, cptext,
glepnirf31cfa22025-03-06 21:59:13 +01003196 &user_data, dir, flags, dup, user_hl, 0);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003197 if (status != OK)
3198 clear_tv(&user_data);
3199 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003200}
3201
3202/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003203 * Add completions from a list.
3204 */
3205 static void
3206ins_compl_add_list(list_T *list)
3207{
3208 listitem_T *li;
3209 int dir = compl_direction;
3210
3211 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003212 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003213 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003214 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003215 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003216 // if dir was BACKWARD then honor it just once
3217 dir = FORWARD;
3218 else if (did_emsg)
3219 break;
3220 }
3221}
3222
3223/*
3224 * Add completions from a dict.
3225 */
3226 static void
3227ins_compl_add_dict(dict_T *dict)
3228{
3229 dictitem_T *di_refresh;
3230 dictitem_T *di_words;
3231
3232 // Check for optional "refresh" item.
3233 compl_opt_refresh_always = FALSE;
3234 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3235 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3236 {
3237 char_u *v = di_refresh->di_tv.vval.v_string;
3238
3239 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3240 compl_opt_refresh_always = TRUE;
3241 }
3242
3243 // Add completions from a "words" list.
3244 di_words = dict_find(dict, (char_u *)"words", 5);
3245 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3246 ins_compl_add_list(di_words->di_tv.vval.v_list);
3247}
3248
3249/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003250 * Start completion for the complete() function.
3251 * "startcol" is where the matched text starts (1 is first column).
3252 * "list" is the list of matches.
3253 */
3254 static void
3255set_completion(colnr_T startcol, list_T *list)
3256{
3257 int save_w_wrow = curwin->w_wrow;
3258 int save_w_leftcol = curwin->w_leftcol;
3259 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003260 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003261 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3262 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3263 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003264
3265 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003266 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003267 ins_compl_prep(' ');
3268 ins_compl_clear();
3269 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003270 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003271
3272 compl_direction = FORWARD;
3273 if (startcol > curwin->w_cursor.col)
3274 startcol = curwin->w_cursor.col;
3275 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003276 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003277 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3278 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003279 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3280 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003281 if (p_ic)
3282 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003283 if (compl_orig_text.string == NULL)
3284 {
3285 compl_orig_text.length = 0;
3286 return;
3287 }
3288 compl_orig_text.length = (size_t)compl_length;
3289 if (ins_compl_add(compl_orig_text.string,
3290 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
glepnirf31cfa22025-03-06 21:59:13 +01003291 flags | CP_FAST, FALSE, NULL, 0) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003292 return;
3293
3294 ctrl_x_mode = CTRL_X_EVAL;
3295
3296 ins_compl_add_list(list);
3297 compl_matches = ins_compl_make_cyclic();
3298 compl_started = TRUE;
3299 compl_used_match = TRUE;
3300 compl_cont_status = 0;
3301
3302 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003303 int no_select = compl_no_select || compl_longest;
3304 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003305 {
3306 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003307 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003308 // Down/Up has no real effect.
3309 ins_complete(K_UP, FALSE);
3310 }
3311 else
3312 ins_complete(Ctrl_N, FALSE);
3313 compl_enter_selects = compl_no_insert;
3314
3315 // Lazily show the popup menu, unless we got interrupted.
3316 if (!compl_interrupted)
3317 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003318 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003319 out_flush();
3320}
3321
3322/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003323 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003324 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003325 void
3326f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003327{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003328 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003329
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003330 if (in_vim9script()
3331 && (check_for_number_arg(argvars, 0) == FAIL
3332 || check_for_list_arg(argvars, 1) == FAIL))
3333 return;
3334
Bram Moolenaar24959102022-05-07 20:01:16 +01003335 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003336 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003337 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003338 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003339 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003340
3341 // Check for undo allowed here, because if something was already inserted
3342 // the line was already saved for undo and this check isn't done.
3343 if (!undo_allowed())
3344 return;
3345
Bram Moolenaard83392a2022-09-01 12:22:46 +01003346 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003347 {
3348 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3349 if (startcol > 0)
3350 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003351 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003352}
3353
3354/*
3355 * "complete_add()" function
3356 */
3357 void
3358f_complete_add(typval_T *argvars, typval_T *rettv)
3359{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003360 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003361 return;
3362
Bram Moolenaar440cf092021-04-03 20:13:30 +02003363 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003364}
3365
3366/*
3367 * "complete_check()" function
3368 */
3369 void
3370f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3371{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003372 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003373 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003374
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003375 ins_compl_check_keys(0, TRUE);
3376 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003377
3378 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003379}
3380
3381/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003382 * Return Insert completion mode name string
3383 */
3384 static char_u *
3385ins_compl_mode(void)
3386{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003387 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003388 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3389
3390 return (char_u *)"";
3391}
3392
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003393/*
3394 * Assign the sequence number to all the completion matches which don't have
3395 * one assigned yet.
3396 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003397 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003398ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003399{
3400 int number = 0;
3401 compl_T *match;
3402
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003403 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003404 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003405 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003406 // This should normally succeed already at the first loop
3407 // cycle, so it's fast!
3408 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003409 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003410 if (match->cp_number != -1)
3411 {
3412 number = match->cp_number;
3413 break;
3414 }
3415 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003416 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003417 for (match = match->cp_next;
3418 match != NULL && match->cp_number == -1;
3419 match = match->cp_next)
3420 match->cp_number = ++number;
3421 }
3422 else // BACKWARD
3423 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003424 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003425 // number. This should normally succeed already at the
3426 // first loop cycle, so it's fast!
3427 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003428 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003429 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003430 if (match->cp_number != -1)
3431 {
3432 number = match->cp_number;
3433 break;
3434 }
glepnir40891ba2025-02-10 22:18:00 +01003435 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003436 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003437 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003438 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003439 for (match = match->cp_prev; match
3440 && match->cp_number == -1;
3441 match = match->cp_prev)
3442 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003443 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003444 }
3445}
3446
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003447/*
glepnir037b0282025-01-16 14:37:44 +01003448 * Fill the dict of complete_info
3449 */
3450 static void
3451fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3452{
3453 dict_add_string(di, "word", match->cp_str.string);
3454 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3455 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3456 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3457 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3458 if (add_match)
3459 dict_add_bool(di, "match", match->cp_in_match_array);
3460 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3461 // Add an empty string for backwards compatibility
3462 dict_add_string(di, "user_data", (char_u *)"");
3463 else
3464 dict_add_tv(di, "user_data", &match->cp_user_data);
3465}
3466
3467/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003468 * Get complete information
3469 */
3470 static void
3471get_complete_info(list_T *what_list, dict_T *retdict)
3472{
3473 int ret = OK;
3474 listitem_T *item;
3475#define CI_WHAT_MODE 0x01
3476#define CI_WHAT_PUM_VISIBLE 0x02
3477#define CI_WHAT_ITEMS 0x04
3478#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003479#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003480#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003481#define CI_WHAT_ALL 0xff
3482 int what_flag;
3483
3484 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003485 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003486 else
3487 {
3488 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003489 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003490 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003491 {
3492 char_u *what = tv_get_string(&item->li_tv);
3493
3494 if (STRCMP(what, "mode") == 0)
3495 what_flag |= CI_WHAT_MODE;
3496 else if (STRCMP(what, "pum_visible") == 0)
3497 what_flag |= CI_WHAT_PUM_VISIBLE;
3498 else if (STRCMP(what, "items") == 0)
3499 what_flag |= CI_WHAT_ITEMS;
3500 else if (STRCMP(what, "selected") == 0)
3501 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003502 else if (STRCMP(what, "completed") == 0)
3503 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003504 else if (STRCMP(what, "matches") == 0)
3505 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003506 }
3507 }
3508
3509 if (ret == OK && (what_flag & CI_WHAT_MODE))
3510 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3511
3512 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3513 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3514
glepnir037b0282025-01-16 14:37:44 +01003515 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3516 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003517 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003518 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003519 dict_T *di;
3520 compl_T *match;
3521 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003522 int has_items = what_flag & CI_WHAT_ITEMS;
3523 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003524 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003525
glepnird4088ed2024-12-31 10:55:22 +01003526 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003527 {
3528 li = list_alloc();
3529 if (li == NULL)
3530 return;
glepnird4088ed2024-12-31 10:55:22 +01003531 ret = dict_add_list(retdict, (has_matches && !has_items)
3532 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003533 }
3534 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3535 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3536 ins_compl_update_sequence_numbers();
3537 if (ret == OK && compl_first_match != NULL)
3538 {
3539 int list_idx = 0;
3540 match = compl_first_match;
3541 do
3542 {
3543 if (!match_at_original_text(match))
3544 {
glepnird4088ed2024-12-31 10:55:22 +01003545 if (has_items
3546 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003547 {
3548 di = dict_alloc();
3549 if (di == NULL)
3550 return;
3551 ret = list_append_dict(li, di);
3552 if (ret != OK)
3553 return;
glepnir037b0282025-01-16 14:37:44 +01003554 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003555 }
glepnird4088ed2024-12-31 10:55:22 +01003556 if (compl_curr_match != NULL
3557 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003558 selected_idx = list_idx;
3559 list_idx += 1;
3560 }
3561 match = match->cp_next;
3562 }
3563 while (match != NULL && !is_first_match(match));
3564 }
3565 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3566 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003567
glepnir037b0282025-01-16 14:37:44 +01003568 if (ret == OK && selected_idx != -1 && has_completed)
3569 {
3570 di = dict_alloc();
3571 if (di == NULL)
3572 return;
3573 fill_complete_info_dict(di, compl_curr_match, FALSE);
3574 ret = dict_add_dict(retdict, "completed", di);
3575 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003576 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003577}
3578
3579/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003580 * "complete_info()" function
3581 */
3582 void
3583f_complete_info(typval_T *argvars, typval_T *rettv)
3584{
3585 list_T *what_list = NULL;
3586
Bram Moolenaar93a10962022-06-16 11:42:09 +01003587 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003588 return;
3589
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003590 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3591 return;
3592
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003593 if (argvars[0].v_type != VAR_UNKNOWN)
3594 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003595 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003596 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003597 what_list = argvars[0].vval.v_list;
3598 }
3599 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003600}
3601#endif
3602
3603/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003604 * Returns TRUE when using a user-defined function for thesaurus completion.
3605 */
3606 static int
3607thesaurus_func_complete(int type UNUSED)
3608{
3609#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003610 return type == CTRL_X_THESAURUS
3611 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003612#else
3613 return FALSE;
3614#endif
3615}
3616
3617/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618 * Return value of process_next_cpt_value()
3619 */
3620enum
3621{
3622 INS_COMPL_CPT_OK = 1,
3623 INS_COMPL_CPT_CONT,
3624 INS_COMPL_CPT_END
3625};
3626
3627/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003628 * state information used for getting the next set of insert completion
3629 * matches.
3630 */
3631typedef struct
3632{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003633 char_u *e_cpt_copy; // copy of 'complete'
3634 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003635 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003636 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003637 pos_T prev_match_pos; // previous match position
3638 int set_match_pos; // save first_match_pos/last_match_pos
3639 pos_T first_match_pos; // first match position
3640 pos_T last_match_pos; // last match position
3641 int found_all; // found all matches of a certain type.
3642 char_u *dict; // dictionary file to search
3643 int dict_f; // "dict" is an exact file name or not
3644} ins_compl_next_state_T;
3645
3646/*
3647 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003648 *
3649 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003650 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003651 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003652 * st->found_all - all matches of this type are found
3653 * st->ins_buf - search for completions in this buffer
3654 * st->first_match_pos - position of the first completion match
3655 * st->last_match_pos - position of the last completion match
3656 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003657 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003658 * st->dict - name of the dictionary or thesaurus file to search
3659 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 *
3661 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003662 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3663 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003664 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 */
3666 static int
3667process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003668 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003670 pos_T *start_match_pos,
glepnir53b14572025-03-12 21:28:39 +01003671 int fuzzy_collect)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003673 int compl_type = -1;
3674 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003676 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003677
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003678 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3679 st->e_cpt++;
3680
3681 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003682 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003683 st->ins_buf = curbuf;
3684 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003685 // Move the cursor back one character so that ^N can match the
3686 // word immediately after the cursor.
glepnir53b14572025-03-12 21:28:39 +01003687 if (ctrl_x_mode_normal() && (!fuzzy_collect && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003688 {
3689 // Move the cursor to after the last character in the
3690 // buffer, so that word at start of buffer is found
3691 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003692 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003693 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003694 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003695 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003696 compl_type = 0;
3697
3698 // Remember the first match so that the loop stops when we
3699 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003700 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003701 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003702 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003703 && (st->ins_buf = ins_compl_next_buf(
3704 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003705 {
3706 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003707 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708 {
3709 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003710 st->first_match_pos.col = st->last_match_pos.col = 0;
3711 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3712 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003713 compl_type = 0;
3714 }
3715 else // unloaded buffer, scan like dictionary
3716 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003717 st->found_all = TRUE;
3718 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003719 {
3720 status = INS_COMPL_CPT_CONT;
3721 goto done;
3722 }
3723 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003724 st->dict = st->ins_buf->b_fname;
3725 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003726 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003727 if (!shortmess(SHM_COMPLETIONSCAN))
3728 {
3729 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3730 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3731 st->ins_buf->b_fname == NULL
3732 ? buf_spname(st->ins_buf)
3733 : st->ins_buf->b_sfname == NULL
3734 ? st->ins_buf->b_fname
3735 : st->ins_buf->b_sfname);
3736 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3737 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003738 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003739 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003740 status = INS_COMPL_CPT_END;
3741 else
3742 {
3743 if (ctrl_x_mode_line_or_eval())
3744 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003745 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003746 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003747 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003748 compl_type = CTRL_X_DICTIONARY;
3749 else
3750 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003751 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003752 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003753 st->dict = st->e_cpt;
3754 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003755 }
3756 }
3757#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003758 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003759 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003760 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003761 compl_type = CTRL_X_PATH_DEFINES;
3762#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003763 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003764 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003765 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003766 if (!shortmess(SHM_COMPLETIONSCAN))
3767 {
3768 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3769 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3770 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3771 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003772 }
3773 else
3774 compl_type = -1;
3775
3776 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003777 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003778
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003779 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003780 if (compl_type == -1)
3781 status = INS_COMPL_CPT_CONT;
3782 }
3783
3784done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003785 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003786 return status;
3787}
3788
3789#ifdef FEAT_FIND_ID
3790/*
3791 * Get the next set of identifiers or defines matching "compl_pattern" in
3792 * included files.
3793 */
3794 static void
3795get_next_include_file_completion(int compl_type)
3796{
John Marriott5e6ea922024-11-23 14:01:57 +01003797 find_pattern_in_path(compl_pattern.string, compl_direction,
3798 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003799 (compl_type == CTRL_X_PATH_DEFINES
3800 && !(compl_cont_status & CONT_SOL))
3801 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003802 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003803}
3804#endif
3805
3806/*
3807 * Get the next set of words matching "compl_pattern" in dictionary or
3808 * thesaurus files.
3809 */
3810 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003811get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003812{
3813#ifdef FEAT_COMPL_FUNC
3814 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003815 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003816 else
3817#endif
3818 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003819 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003820 : (compl_type == CTRL_X_THESAURUS
3821 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3822 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003823 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003824 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003825 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003826}
3827
3828/*
3829 * Get the next set of tag names matching "compl_pattern".
3830 */
3831 static void
3832get_next_tag_completion(void)
3833{
3834 int save_p_ic;
3835 char_u **matches;
3836 int num_matches;
3837
3838 // set p_ic according to p_ic, p_scs and pat for find_tags().
3839 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003840 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003841
3842 // Find up to TAG_MANY matches. Avoids that an enormous number
3843 // of matches is found when compl_pattern is empty
3844 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003845 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003846 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003847 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003848 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3849 ins_compl_add_matches(num_matches, matches, p_ic);
3850 g_tag_at_cursor = FALSE;
3851 p_ic = save_p_ic;
3852}
3853
3854/*
glepnir8159fb12024-07-17 20:32:54 +02003855 * Compare function for qsort
3856 */
glepnirf31cfa22025-03-06 21:59:13 +01003857 static int
3858compare_scores(const void *a, const void *b)
glepnir8159fb12024-07-17 20:32:54 +02003859{
3860 int idx_a = *(const int *)a;
3861 int idx_b = *(const int *)b;
3862 int score_a = compl_fuzzy_scores[idx_a];
3863 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003864 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3865 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003866}
3867
3868/*
glepnirf31cfa22025-03-06 21:59:13 +01003869 * insert prefix with redraw
3870 */
3871 static void
3872ins_compl_longest_insert(char_u *prefix)
3873{
3874 ins_compl_delete();
3875 ins_compl_insert_bytes(prefix + get_compl_len(), -1);
3876 ins_redraw(FALSE);
3877}
3878
3879/*
3880 * Calculate the longest common prefix among the best fuzzy matches
3881 * stored in compl_best_matches, and insert it as the longest.
3882 */
3883 static void
3884fuzzy_longest_match(void)
3885{
3886 char_u *prefix = NULL;
3887 int prefix_len = 0;
3888 int i = 0;
3889 int j = 0;
3890 char_u *match_str = NULL;
3891 char_u *prefix_ptr = NULL;
3892 char_u *match_ptr = NULL;
3893 char_u *leader = NULL;
3894 size_t leader_len = 0;
3895 compl_T *compl = NULL;
3896 int more_candidates = FALSE;
3897 compl_T *nn_compl = NULL;
3898
3899 if (compl_num_bests == 0)
3900 return;
3901
3902 nn_compl = compl_first_match->cp_next->cp_next;
3903 if (nn_compl && nn_compl != compl_first_match)
3904 more_candidates = TRUE;
3905
3906 compl = ctrl_x_mode_whole_line() ? compl_first_match
3907 : compl_first_match->cp_next;
3908 if (compl_num_bests == 1)
3909 {
3910 // no more candidates insert the match str
3911 if (!more_candidates)
3912 {
3913 ins_compl_longest_insert(compl->cp_str.string);
3914 compl_num_bests = 0;
3915 }
3916 compl_num_bests = 0;
3917 return;
3918 }
3919
3920 compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *));
3921 if (compl_best_matches == NULL)
3922 return;
3923 while (compl != NULL && i < compl_num_bests)
3924 {
3925 compl_best_matches[i] = compl;
3926 compl = compl->cp_next;
3927 i++;
3928 }
3929
3930 prefix = compl_best_matches[0]->cp_str.string;
zeertzjq4422de62025-03-07 19:06:02 +01003931 prefix_len = (int)compl_best_matches[0]->cp_str.length;
glepnirf31cfa22025-03-06 21:59:13 +01003932
3933 for (i = 1; i < compl_num_bests; i++)
3934 {
3935 match_str = compl_best_matches[i]->cp_str.string;
3936 prefix_ptr = prefix;
3937 match_ptr = match_str;
3938 j = 0;
3939
3940 while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL)
3941 {
3942 if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0)
3943 break;
3944
3945 MB_PTR_ADV(prefix_ptr);
3946 MB_PTR_ADV(match_ptr);
3947 j++;
3948 }
3949
3950 if (j > 0)
3951 prefix_len = j;
3952 }
3953
3954 leader = ins_compl_leader();
zeertzjq4422de62025-03-07 19:06:02 +01003955 leader_len = ins_compl_leader_len();
glepnirf31cfa22025-03-06 21:59:13 +01003956
3957 // skip non-consecutive prefixes
zeertzjq4422de62025-03-07 19:06:02 +01003958 if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0)
glepnirf31cfa22025-03-06 21:59:13 +01003959 goto end;
3960
zeertzjq4422de62025-03-07 19:06:02 +01003961 prefix = vim_strnsave(prefix, prefix_len);
glepnirf31cfa22025-03-06 21:59:13 +01003962 if (prefix != NULL)
3963 {
3964 ins_compl_longest_insert(prefix);
3965 compl_cfc_longest_ins = TRUE;
3966 vim_free(prefix);
3967 }
3968
3969end:
3970 vim_free(compl_best_matches);
3971 compl_best_matches = NULL;
3972 compl_num_bests = 0;
3973}
3974
3975/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003976 * Get the next set of filename matching "compl_pattern".
3977 */
3978 static void
3979get_next_filename_completion(void)
3980{
3981 char_u **matches;
3982 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003983 char_u *ptr;
3984 garray_T fuzzy_indices;
3985 int i;
3986 int score;
3987 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003988 size_t leader_len = ins_compl_leader_len();;
glepnirf31cfa22025-03-06 21:59:13 +01003989 int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0);
glepnir8159fb12024-07-17 20:32:54 +02003990 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003991 char_u *last_sep = NULL;
glepnirf31cfa22025-03-06 21:59:13 +01003992 int need_collect_bests = in_fuzzy_collect && compl_get_longest;
3993 int max_score = 0;
3994 int current_score = 0;
3995 int dir = compl_direction;
glepnir8159fb12024-07-17 20:32:54 +02003996
glepnirb9de1a02024-08-02 19:14:38 +02003997#ifdef BACKSLASH_IN_FILENAME
3998 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3999 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
4000#else
4001 char pathsep = PATHSEP;
4002#endif
4003
glepnirf31cfa22025-03-06 21:59:13 +01004004 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004005 {
glepnirb9de1a02024-08-02 19:14:38 +02004006#ifdef BACKSLASH_IN_FILENAME
4007 if (curbuf->b_p_csl[0] == 's')
4008 {
John Marriott5e6ea922024-11-23 14:01:57 +01004009 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004010 {
4011 if (leader[i] == '\\')
4012 leader[i] = '/';
4013 }
4014 }
4015 else if (curbuf->b_p_csl[0] == 'b')
4016 {
John Marriott5e6ea922024-11-23 14:01:57 +01004017 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004018 {
4019 if (leader[i] == '/')
4020 leader[i] = '\\';
4021 }
4022 }
4023#endif
4024 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02004025 if (last_sep == NULL)
4026 {
4027 // No path separator or separator is the last character,
4028 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01004029 VIM_CLEAR_STRING(compl_pattern);
4030 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
4031 if (compl_pattern.string == NULL)
4032 return;
4033 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02004034 }
4035 else if (*(last_sep + 1) == '\0')
glepnirf31cfa22025-03-06 21:59:13 +01004036 in_fuzzy_collect = FALSE;
glepnir0be03e12024-07-19 16:45:05 +02004037 else
4038 {
4039 // Split leader into path and file parts
4040 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004041 char_u *path_with_wildcard;
4042
4043 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02004044 if (path_with_wildcard != NULL)
4045 {
John Marriott5e6ea922024-11-23 14:01:57 +01004046 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
4047 VIM_CLEAR_STRING(compl_pattern);
4048 compl_pattern.string = path_with_wildcard;
4049 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02004050
4051 // Move leader to the file part
4052 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004053 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02004054 }
4055 }
glepnir8159fb12024-07-17 20:32:54 +02004056 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004057
John Marriott5e6ea922024-11-23 14:01:57 +01004058 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004059 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
4060 return;
4061
4062 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01004063 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004064#ifdef BACKSLASH_IN_FILENAME
4065 if (curbuf->b_p_csl[0] != NUL)
4066 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004067 for (i = 0; i < num_matches; ++i)
4068 {
glepnir8159fb12024-07-17 20:32:54 +02004069 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004070 while (*ptr != NUL)
4071 {
4072 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
4073 *ptr = '/';
4074 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
4075 *ptr = '\\';
4076 ptr += (*mb_ptr2len)(ptr);
4077 }
4078 }
4079 }
4080#endif
glepnir8159fb12024-07-17 20:32:54 +02004081
glepnirf31cfa22025-03-06 21:59:13 +01004082 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004083 {
4084 ga_init2(&fuzzy_indices, sizeof(int), 10);
4085 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
4086
4087 for (i = 0; i < num_matches; i++)
4088 {
4089 ptr = matches[i];
4090 score = fuzzy_match_str(ptr, leader);
4091 if (score > 0)
4092 {
4093 if (ga_grow(&fuzzy_indices, 1) == OK)
4094 {
4095 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
4096 compl_fuzzy_scores[i] = score;
4097 fuzzy_indices.ga_len++;
4098 }
4099 }
4100 }
4101
Christian Brabandt220474d2024-07-20 13:26:44 +02004102 // prevent qsort from deref NULL pointer
4103 if (fuzzy_indices.ga_len > 0)
4104 {
glepnirf31cfa22025-03-06 21:59:13 +01004105 char_u *match = NULL;
Christian Brabandt220474d2024-07-20 13:26:44 +02004106 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
4107 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02004108
Christian Brabandt220474d2024-07-20 13:26:44 +02004109 for (i = 0; i < fuzzy_indices.ga_len; ++i)
glepnirf31cfa22025-03-06 21:59:13 +01004110 {
4111 match = matches[fuzzy_indices_data[i]];
4112 current_score = compl_fuzzy_scores[fuzzy_indices_data[i]];
4113 if (ins_compl_add(match, -1, NULL, NULL, NULL, dir,
4114 CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0),
4115 FALSE, NULL, current_score) == OK)
4116 dir = FORWARD;
4117
4118 if (need_collect_bests)
4119 {
4120 if (i == 0 || current_score == max_score)
4121 {
4122 compl_num_bests++;
4123 max_score = current_score;
4124 }
4125 }
4126 }
glepnir8159fb12024-07-17 20:32:54 +02004127
Christian Brabandt220474d2024-07-20 13:26:44 +02004128 FreeWild(num_matches, matches);
Christian Brabandt220474d2024-07-20 13:26:44 +02004129 }
glepnir6b6280c2024-07-27 16:25:45 +02004130 else if (leader_len > 0)
4131 {
4132 FreeWild(num_matches, matches);
4133 num_matches = 0;
4134 }
Christian Brabandt220474d2024-07-20 13:26:44 +02004135
glepnir8159fb12024-07-17 20:32:54 +02004136 vim_free(compl_fuzzy_scores);
4137 ga_clear(&fuzzy_indices);
glepnirf31cfa22025-03-06 21:59:13 +01004138
4139 if (compl_num_bests > 0 && compl_get_longest)
4140 fuzzy_longest_match();
4141 return;
glepnir8159fb12024-07-17 20:32:54 +02004142 }
4143
glepnir6b6280c2024-07-27 16:25:45 +02004144 if (num_matches > 0)
4145 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004146}
4147
4148/*
4149 * Get the next set of command-line completions matching "compl_pattern".
4150 */
4151 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004152get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004153{
4154 char_u **matches;
4155 int num_matches;
4156
John Marriott5e6ea922024-11-23 14:01:57 +01004157 if (expand_cmdline(&compl_xp, compl_pattern.string,
4158 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004159 ins_compl_add_matches(num_matches, matches, FALSE);
4160}
4161
4162/*
4163 * Get the next set of spell suggestions matching "compl_pattern".
4164 */
4165 static void
4166get_next_spell_completion(linenr_T lnum UNUSED)
4167{
4168#ifdef FEAT_SPELL
4169 char_u **matches;
4170 int num_matches;
4171
John Marriott5e6ea922024-11-23 14:01:57 +01004172 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004173 if (num_matches > 0)
4174 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00004175 else
4176 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004177#endif
4178}
4179
4180/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004181 * Return the next word or line from buffer "ins_buf" at position
4182 * "cur_match_pos" for completion. The length of the match is set in "len".
4183 */
4184 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00004185ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004186 buf_T *ins_buf, // buffer being scanned
4187 pos_T *cur_match_pos, // current match position
4188 int *match_len,
4189 int *cont_s_ipos) // next ^X<> will set initial_pos
4190{
4191 char_u *ptr;
4192 int len;
4193
4194 *match_len = 0;
4195 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
4196 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01004197 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004198 if (ctrl_x_mode_line_or_eval())
4199 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004200 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004201 {
4202 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
4203 return NULL;
4204 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01004205 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004206 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01004207 {
4208 char_u *tmp_ptr = ptr;
4209
4210 ptr = skipwhite(tmp_ptr);
4211 len -= (int)(ptr - tmp_ptr);
4212 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004213 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004214 }
4215 else
4216 {
4217 char_u *tmp_ptr = ptr;
4218
John Marriott5e6ea922024-11-23 14:01:57 +01004219 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004220 {
4221 tmp_ptr += compl_length;
4222 // Skip if already inside a word.
4223 if (vim_iswordp(tmp_ptr))
4224 return NULL;
4225 // Find start of next word.
4226 tmp_ptr = find_word_start(tmp_ptr);
4227 }
4228 // Find end of this word.
4229 tmp_ptr = find_word_end(tmp_ptr);
4230 len = (int)(tmp_ptr - ptr);
4231
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004232 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004233 {
4234 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4235 {
4236 // Try next line, if any. the new word will be
4237 // "join" as if the normal command "J" was used.
4238 // IOSIZE is always greater than
4239 // compl_length, so the next STRNCPY always
4240 // works -- Acevedo
4241 STRNCPY(IObuff, ptr, len);
4242 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4243 tmp_ptr = ptr = skipwhite(ptr);
4244 // Find start of next word.
4245 tmp_ptr = find_word_start(tmp_ptr);
4246 // Find end of next word.
4247 tmp_ptr = find_word_end(tmp_ptr);
4248 if (tmp_ptr > ptr)
4249 {
4250 if (*ptr != ')' && IObuff[len - 1] != TAB)
4251 {
4252 if (IObuff[len - 1] != ' ')
4253 IObuff[len++] = ' ';
4254 // IObuf =~ "\k.* ", thus len >= 2
4255 if (p_js
4256 && (IObuff[len - 2] == '.'
4257 || (vim_strchr(p_cpo, CPO_JOINSP)
4258 == NULL
4259 && (IObuff[len - 2] == '?'
4260 || IObuff[len - 2] == '!'))))
4261 IObuff[len++] = ' ';
4262 }
4263 // copy as much as possible of the new word
4264 if (tmp_ptr - ptr >= IOSIZE - len)
4265 tmp_ptr = ptr + IOSIZE - len - 1;
4266 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4267 len += (int)(tmp_ptr - ptr);
4268 *cont_s_ipos = TRUE;
4269 }
4270 IObuff[len] = NUL;
4271 ptr = IObuff;
4272 }
4273 if (len == compl_length)
4274 return NULL;
4275 }
4276 }
4277
4278 *match_len = len;
4279 return ptr;
4280}
4281
4282/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004283 * Get the next set of words matching "compl_pattern" for default completion(s)
4284 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004285 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4286 * position "st->start_pos" in the "compl_direction" direction. If
4287 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4288 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004289 * Returns OK if a new next match is found, otherwise returns FAIL.
4290 */
4291 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004292get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004293{
4294 int found_new_match = FAIL;
4295 int save_p_scs;
4296 int save_p_ws;
4297 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004298 char_u *ptr = NULL;
4299 int len = 0;
glepnirf31cfa22025-03-06 21:59:13 +01004300 int in_collect = (cfc_has_mode() && compl_length > 0);
glepnir8159fb12024-07-17 20:32:54 +02004301 char_u *leader = ins_compl_leader();
glepnirf31cfa22025-03-06 21:59:13 +01004302 int score = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004303
4304 // If 'infercase' is set, don't use 'smartcase' here
4305 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004306 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004307 p_scs = FALSE;
4308
4309 // Buffers other than curbuf are scanned from the beginning or the
4310 // end but never from the middle, thus setting nowrapscan in this
4311 // buffer is a good idea, on the other hand, we always set
4312 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4313 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004314 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004315 p_ws = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01004316 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004317 p_ws = TRUE;
4318 looped_around = FALSE;
4319 for (;;)
4320 {
4321 int cont_s_ipos = FALSE;
4322
4323 ++msg_silent; // Don't want messages for wrapscan.
4324
glepnirf31cfa22025-03-06 21:59:13 +01004325 if (in_collect)
4326 {
glepnir8159fb12024-07-17 20:32:54 +02004327 found_new_match = search_for_fuzzy_match(st->ins_buf,
4328 st->cur_match_pos, leader, compl_direction,
glepnirf31cfa22025-03-06 21:59:13 +01004329 start_pos, &len, &ptr, &score);
4330 }
4331 // ctrl_x_mode_line_or_eval() || word-wise search that
4332 // has added a word that was at the beginning of the line
4333 else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
4334 found_new_match = search_for_exact_line(st->ins_buf,
4335 st->cur_match_pos, compl_direction, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004336 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004337 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004338 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004339 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004340 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004341 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004342 {
4343 // set "compl_started" even on fail
4344 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004345 st->first_match_pos = *st->cur_match_pos;
4346 st->last_match_pos = *st->cur_match_pos;
4347 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004348 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004349 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4350 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004351 {
4352 found_new_match = FAIL;
4353 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004354 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004355 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4356 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4357 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004358 {
4359 if (looped_around)
4360 found_new_match = FAIL;
4361 else
4362 looped_around = TRUE;
4363 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004364 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004365 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4366 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4367 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004368 {
4369 if (looped_around)
4370 found_new_match = FAIL;
4371 else
4372 looped_around = TRUE;
4373 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004374 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004375 if (found_new_match == FAIL)
4376 break;
4377
4378 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004379 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004380 && start_pos->lnum == st->cur_match_pos->lnum
4381 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004382 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004383
glepnirf31cfa22025-03-06 21:59:13 +01004384 if (!in_collect)
glepnir8159fb12024-07-17 20:32:54 +02004385 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4386 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004387 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004388 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004389
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004390 if (ins_compl_add_infercase(ptr, len, p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01004391 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
4392 0, cont_s_ipos, score) != NOTDONE)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004393 {
glepnirf31cfa22025-03-06 21:59:13 +01004394 if (in_collect && score == compl_first_match->cp_next->cp_score)
4395 compl_num_bests++;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004396 found_new_match = OK;
4397 break;
4398 }
4399 }
4400 p_scs = save_p_scs;
4401 p_ws = save_p_ws;
4402
4403 return found_new_match;
4404}
4405
4406/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004407 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004408 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004409 */
4410 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004411get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004412{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004413 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004414
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004415 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004416 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004417 case -1:
4418 break;
4419#ifdef FEAT_FIND_ID
4420 case CTRL_X_PATH_PATTERNS:
4421 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004422 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004423 break;
4424#endif
4425
4426 case CTRL_X_DICTIONARY:
4427 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004428 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4429 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004430 break;
4431
4432 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004433 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004434 break;
4435
4436 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004437 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004438 break;
4439
4440 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004441 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004442 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004443 break;
4444
4445#ifdef FEAT_COMPL_FUNC
4446 case CTRL_X_FUNCTION:
4447 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004448 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004449 break;
4450#endif
4451
4452 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004453 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004454 break;
4455
4456 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004457 found_new_match = get_next_default_completion(st, ini);
4458 if (found_new_match == FAIL && st->ins_buf == curbuf)
4459 st->found_all = TRUE;
4460 }
4461
4462 // check if compl_curr_match has changed, (e.g. other type of
4463 // expansion added something)
4464 if (type != 0 && compl_curr_match != compl_old_match)
4465 found_new_match = OK;
4466
4467 return found_new_match;
4468}
4469
4470/*
4471 * Get the next expansion(s), using "compl_pattern".
4472 * The search starts at position "ini" in curbuf and in the direction
4473 * compl_direction.
4474 * When "compl_started" is FALSE start at that position, otherwise continue
4475 * where we stopped searching before.
4476 * This may return before finding all the matches.
4477 * Return the total number of matches or -1 if still unknown -- Acevedo
4478 */
4479 static int
4480ins_compl_get_exp(pos_T *ini)
4481{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004482 static ins_compl_next_state_T st;
4483 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004484 int i;
4485 int found_new_match;
4486 int type = ctrl_x_mode;
4487
4488 if (!compl_started)
4489 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004490 buf_T *buf;
4491
4492 FOR_ALL_BUFFERS(buf)
4493 buf->b_scanned = 0;
4494 if (!st_cleared)
4495 {
4496 CLEAR_FIELD(st);
4497 st_cleared = TRUE;
4498 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004499 st.found_all = FALSE;
4500 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004501 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004502 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004503 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4504 ? (char_u *)"." : curbuf->b_p_cpt);
4505 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004506 st.last_match_pos = st.first_match_pos = *ini;
4507 }
4508 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4509 st.ins_buf = curbuf; // In case the buffer was wiped out.
4510
4511 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004512 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004513 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004514
4515 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4516 for (;;)
4517 {
4518 found_new_match = FAIL;
4519 st.set_match_pos = FALSE;
4520
4521 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4522 // or if found_all says this entry is done. For ^X^L only use the
4523 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004524 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004525 && (!compl_started || st.found_all))
4526 {
glepnir53b14572025-03-12 21:28:39 +01004527 int status = process_next_cpt_value(&st, &type, ini, cfc_has_mode());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004528
4529 if (status == INS_COMPL_CPT_END)
4530 break;
4531 if (status == INS_COMPL_CPT_CONT)
4532 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004533 }
4534
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004535 // If complete() was called then compl_pattern has been reset. The
4536 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004537 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004538 break;
4539
4540 // get the next set of completion matches
4541 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004542
4543 // break the loop for specialized modes (use 'complete' just for the
4544 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4545 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004546 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4547 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004548 {
4549 if (got_int)
4550 break;
4551 // Fill the popup menu as soon as possible.
4552 if (type != -1)
4553 ins_compl_check_keys(0, FALSE);
4554
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004555 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004556 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4557 break;
4558 compl_started = TRUE;
4559 }
4560 else
4561 {
4562 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004563 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004564 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004565
4566 compl_started = FALSE;
4567 }
4568 }
4569 compl_started = TRUE;
4570
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004571 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004572 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004573 found_new_match = FAIL;
4574
4575 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004576 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004577 && !ctrl_x_mode_line_or_eval()))
4578 i = ins_compl_make_cyclic();
4579
glepnirf31cfa22025-03-06 21:59:13 +01004580 if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0)
4581 fuzzy_longest_match();
4582
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004583 if (compl_old_match != NULL)
4584 {
4585 // If several matches were added (FORWARD) or the search failed and has
4586 // just been made cyclic then we have to move compl_curr_match to the
4587 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004588 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004589 : compl_old_match->cp_prev;
4590 if (compl_curr_match == NULL)
4591 compl_curr_match = compl_old_match;
4592 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004593 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004594
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004595 return i;
4596}
4597
4598/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004599 * Update "compl_shown_match" to the actually shown match, it may differ when
4600 * "compl_leader" is used to omit some of the matches.
4601 */
4602 static void
4603ins_compl_update_shown_match(void)
4604{
4605 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004606 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004607 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004608 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004609 compl_shown_match = compl_shown_match->cp_next;
4610
4611 // If we didn't find it searching forward, and compl_shows_dir is
4612 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004613 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004614 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004615 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004616 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004617 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004618 {
4619 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004620 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004621 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004622 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004623 compl_shown_match = compl_shown_match->cp_prev;
4624 }
4625}
4626
4627/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004628 * Delete the old text being completed.
4629 */
4630 void
4631ins_compl_delete(void)
4632{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004633 // In insert mode: Delete the typed part.
4634 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004635 int col = compl_col + (compl_status_adding() ? compl_length : 0);
John Marriottf4b36412025-02-23 09:09:59 +01004636 string_T remaining = {NULL, 0};
glepnir76bdb822025-02-08 19:04:51 +01004637 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01004638 int has_preinsert = ins_compl_preinsert_effect();
4639 if (has_preinsert)
4640 {
glepnirbfb4eea2025-01-31 15:28:29 +01004641 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004642 curwin->w_cursor.col = compl_ins_end_col;
4643 }
4644
glepnir76bdb822025-02-08 19:04:51 +01004645 if (curwin->w_cursor.lnum > compl_lnum)
4646 {
4647 if (curwin->w_cursor.col < ml_get_curline_len())
4648 {
John Marriottf4b36412025-02-23 09:09:59 +01004649 char_u *line = ml_get_cursor();
4650 remaining.length = ml_get_cursor_len();
4651 remaining.string = vim_strnsave(line, remaining.length);
4652 if (remaining.string == NULL)
glepnir76bdb822025-02-08 19:04:51 +01004653 return;
4654 }
4655 while (curwin->w_cursor.lnum > compl_lnum)
4656 {
4657 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
4658 {
John Marriottf4b36412025-02-23 09:09:59 +01004659 if (remaining.string)
4660 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004661 return;
4662 }
zeertzjq060e6552025-02-21 20:06:26 +01004663 deleted_lines_mark(curwin->w_cursor.lnum, 1L);
glepnir76bdb822025-02-08 19:04:51 +01004664 curwin->w_cursor.lnum--;
4665 }
4666 // move cursor to end of line
4667 curwin->w_cursor.col = ml_get_curline_len();
4668 }
4669
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004670 if ((int)curwin->w_cursor.col > col)
4671 {
4672 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01004673 {
John Marriottf4b36412025-02-23 09:09:59 +01004674 if (remaining.string)
4675 vim_free(remaining.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004676 return;
glepnire3647c82025-02-10 21:16:32 +01004677 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004678 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004679 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004680 }
4681
John Marriottf4b36412025-02-23 09:09:59 +01004682 if (remaining.string != NULL)
glepnir76bdb822025-02-08 19:04:51 +01004683 {
4684 orig_col = curwin->w_cursor.col;
John Marriottf4b36412025-02-23 09:09:59 +01004685 ins_str(remaining.string, remaining.length);
glepnir76bdb822025-02-08 19:04:51 +01004686 curwin->w_cursor.col = orig_col;
John Marriottf4b36412025-02-23 09:09:59 +01004687 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004688 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004689 // TODO: is this sufficient for redrawing? Redrawing everything causes
4690 // flicker, thus we can't do that.
4691 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004692#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004693 // clear v:completed_item
4694 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004695#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004696}
4697
4698/*
glepnir76bdb822025-02-08 19:04:51 +01004699 * Insert a completion string that contains newlines.
4700 * The string is split and inserted line by line.
4701 */
4702 static void
4703ins_compl_expand_multiple(char_u *str)
4704{
4705 char_u *start = str;
4706 char_u *curr = str;
glepnir5090a1f2025-02-24 19:10:37 +01004707 int base_indent = get_indent();
glepnir76bdb822025-02-08 19:04:51 +01004708
4709 while (*curr != NUL)
4710 {
4711 if (*curr == '\n')
4712 {
4713 // Insert the text chunk before newline
4714 if (curr > start)
4715 ins_char_bytes(start, (int)(curr - start));
4716
4717 // Handle newline
glepnir5090a1f2025-02-24 19:10:37 +01004718 open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL);
glepnir76bdb822025-02-08 19:04:51 +01004719 start = curr + 1;
4720 }
4721 curr++;
4722 }
4723
4724 // Handle remaining text after last newline (if any)
4725 if (curr > start)
4726 ins_char_bytes(start, (int)(curr - start));
4727
4728 compl_ins_end_col = curwin->w_cursor.col;
4729}
4730
4731/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004732 * Insert the new text being completed.
4733 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004734 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4735 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004736 */
4737 void
glepniredd4ac32025-01-29 18:53:51 +01004738ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004739{
glepnir76bdb822025-02-08 19:04:51 +01004740 int compl_len = get_compl_len();
4741 int preinsert = ins_compl_has_preinsert();
4742 char_u *cp_str = compl_shown_match->cp_str.string;
4743 size_t cp_str_len = compl_shown_match->cp_str.length;
4744 size_t leader_len = ins_compl_leader_len();
4745 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004746
4747 // Make sure we don't go over the end of the string, this can happen with
4748 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004749 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004750 {
glepnir76bdb822025-02-08 19:04:51 +01004751 if (has_multiple)
4752 ins_compl_expand_multiple(cp_str + compl_len);
4753 else
4754 {
4755 ins_compl_insert_bytes(cp_str + compl_len, -1);
4756 if (preinsert && move_cursor)
4757 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
4758 }
glepniredd4ac32025-01-29 18:53:51 +01004759 }
4760 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004761 compl_used_match = FALSE;
4762 else
4763 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004764#ifdef FEAT_EVAL
4765 {
4766 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4767
4768 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4769 }
4770#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004771 if (!in_compl_func)
4772 compl_curr_match = compl_shown_match;
4773}
4774
4775/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004776 * show the file name for the completion match (if any). Truncate the file
4777 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004778 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004779 static void
4780ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004781{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004782 char *lead = _("match in file");
4783 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4784 char_u *s;
4785 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004786
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004787 if (space <= 0)
4788 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004789
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004790 // We need the tail that fits. With double-byte encoding going
4791 // back from the end is very slow, thus go from the start and keep
4792 // the text that fits in "space" between "s" and "e".
4793 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004794 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004795 space -= ptr2cells(e);
4796 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004797 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004798 space += ptr2cells(s);
4799 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004800 }
4801 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004802 msg_hist_off = TRUE;
4803 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4804 s > compl_shown_match->cp_fname ? "<" : "", s);
4805 msg((char *)IObuff);
4806 msg_hist_off = FALSE;
4807 redraw_cmdline = FALSE; // don't overwrite!
4808}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004809
glepnirdca57fb2024-06-04 22:01:21 +02004810/*
zeertzjq551d8c32024-06-05 19:53:32 +02004811 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004812 */
glepnira218cc62024-06-03 19:32:39 +02004813 static compl_T *
4814find_comp_when_fuzzy(void)
4815{
4816 int score;
4817 char_u* str;
4818 int target_idx = -1;
4819 int is_forward = compl_shows_dir_forward();
4820 int is_backward = compl_shows_dir_backward();
4821 compl_T *comp = NULL;
4822
glepnir43eef882024-06-19 20:20:48 +02004823 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004824 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004825 return compl_first_match != compl_shown_match ? compl_first_match :
4826 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004827
4828 if (is_forward)
4829 target_idx = compl_selected_item + 1;
4830 else if (is_backward)
4831 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004832 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004833
4834 score = compl_match_array[target_idx].pum_score;
4835 str = compl_match_array[target_idx].pum_text;
4836
4837 comp = compl_first_match;
4838 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004839 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004840 return comp;
4841 comp = comp->cp_next;
4842 } while (comp != NULL && !is_first_match(comp));
4843
4844 return NULL;
4845}
4846
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004847/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004848 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004849 * times. The number of matches found is returned in 'num_matches'.
4850 *
4851 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4852 * get more completions. If it is FALSE, then do nothing when there are no more
4853 * completions in the given direction.
4854 *
4855 * If "advance" is TRUE, then completion will move to the first match.
4856 * Otherwise, the original text will be shown.
4857 *
4858 * Returns OK on success and -1 if the number of matches are unknown.
4859 */
4860 static int
4861find_next_completion_match(
4862 int allow_get_expansion,
4863 int todo, // repeat completion this many times
4864 int advance,
4865 int *num_matches)
4866{
4867 int found_end = FALSE;
4868 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004869 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004870 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4871 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004872
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004873 while (--todo >= 0)
4874 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004875 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876 {
glepniraced8c22024-06-15 15:13:05 +02004877 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4878 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004879 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004880 && (is_first_match(compl_shown_match->cp_next)
4881 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004882 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004883 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004884 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004885 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004886 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004887 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4888 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004889 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004890 }
4891 else
4892 {
4893 if (!allow_get_expansion)
4894 {
4895 if (advance)
4896 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004897 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004898 compl_pending -= todo + 1;
4899 else
4900 compl_pending += todo + 1;
4901 }
4902 return -1;
4903 }
4904
4905 if (!compl_no_select && advance)
4906 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004907 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004908 --compl_pending;
4909 else
4910 ++compl_pending;
4911 }
4912
4913 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004914 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004915
4916 // handle any pending completions
4917 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004918 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004919 {
4920 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4921 {
4922 compl_shown_match = compl_shown_match->cp_next;
4923 --compl_pending;
4924 }
4925 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4926 {
4927 compl_shown_match = compl_shown_match->cp_prev;
4928 ++compl_pending;
4929 }
4930 else
4931 break;
4932 }
4933 found_end = FALSE;
4934 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004935 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004936 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004937 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004938 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004939 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004940 ++todo;
4941 else
4942 // Remember a matching item.
4943 found_compl = compl_shown_match;
4944
4945 // Stop at the end of the list when we found a usable match.
4946 if (found_end)
4947 {
4948 if (found_compl != NULL)
4949 {
4950 compl_shown_match = found_compl;
4951 break;
4952 }
4953 todo = 1; // use first usable match after wrapping around
4954 }
4955 }
4956
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004957 return OK;
4958}
4959
4960/*
4961 * Fill in the next completion in the current direction.
4962 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4963 * get more completions. If it is FALSE, then we just do nothing when there
4964 * are no more completions in a given direction. The latter case is used when
4965 * we are still in the middle of finding completions, to allow browsing
4966 * through the ones found so far.
4967 * Return the total number of matches, or -1 if still unknown -- webb.
4968 *
4969 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4970 * compl_shown_match here.
4971 *
4972 * Note that this function may be called recursively once only. First with
4973 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4974 * calls this function with "allow_get_expansion" FALSE.
4975 */
4976 static int
4977ins_compl_next(
4978 int allow_get_expansion,
4979 int count, // repeat completion this many times; should
4980 // be at least 1
4981 int insert_match, // Insert the newly selected match
4982 int in_compl_func) // called from complete_check()
4983{
4984 int num_matches = -1;
4985 int todo = count;
4986 int advance;
4987 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004988 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004989 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004990 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4991 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004992 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004993
4994 // When user complete function return -1 for findstart which is next
4995 // time of 'always', compl_shown_match become NULL.
4996 if (compl_shown_match == NULL)
4997 return -1;
4998
John Marriott5e6ea922024-11-23 14:01:57 +01004999 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02005000 && !match_at_original_text(compl_shown_match)
5001 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005002 // Update "compl_shown_match" to the actually shown match
5003 ins_compl_update_shown_match();
5004
5005 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02005006 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005007 // Delete old text to be replaced
5008 ins_compl_delete();
5009
5010 // When finding the longest common text we stick at the original text,
5011 // don't let CTRL-N or CTRL-P move to the first match.
5012 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
5013
5014 // When restarting the search don't insert the first match either.
5015 if (compl_restarting)
5016 {
5017 advance = FALSE;
5018 compl_restarting = FALSE;
5019 }
5020
5021 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
5022 // around.
5023 if (find_next_completion_match(allow_get_expansion, todo, advance,
5024 &num_matches) == -1)
5025 return -1;
5026
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005027 if (curbuf != orig_curbuf)
5028 {
5029 // In case some completion function switched buffer, don't want to
5030 // insert the completion elsewhere.
5031 return -1;
5032 }
5033
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005034 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01005035 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005036 {
glepnir6a38aff2024-12-16 21:56:16 +01005037 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005038 compl_used_match = FALSE;
5039 }
5040 else if (insert_match)
5041 {
5042 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01005043 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005044 else
glepnir6a38aff2024-12-16 21:56:16 +01005045 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005046 }
5047 else
5048 compl_used_match = FALSE;
5049
5050 if (!allow_get_expansion)
5051 {
5052 // may undisplay the popup menu first
5053 ins_compl_upd_pum();
5054
5055 if (pum_enough_matches())
5056 // Will display the popup menu, don't redraw yet to avoid flicker.
5057 pum_call_update_screen();
5058 else
5059 // Not showing the popup menu yet, redraw to show the user what was
5060 // inserted.
5061 update_screen(0);
5062
5063 // display the updated popup menu
5064 ins_compl_show_pum();
5065#ifdef FEAT_GUI
5066 if (gui.in_use)
5067 {
5068 // Show the cursor after the match, not after the redrawn text.
5069 setcursor();
5070 out_flush_cursor(FALSE, FALSE);
5071 }
5072#endif
5073
5074 // Delete old text to be replaced, since we're still searching and
5075 // don't want to match ourselves!
5076 ins_compl_delete();
5077 }
5078
5079 // Enter will select a match when the match wasn't inserted and the popup
5080 // menu is visible.
5081 if (compl_no_insert && !started)
5082 compl_enter_selects = TRUE;
5083 else
5084 compl_enter_selects = !insert_match && compl_match_array != NULL;
5085
5086 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005087 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005088 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005089
5090 return num_matches;
5091}
5092
5093/*
5094 * Call this while finding completions, to check whether the user has hit a key
5095 * that should change the currently displayed completion, or exit completion
5096 * mode. Also, when compl_pending is not zero, show a completion as soon as
5097 * possible. -- webb
5098 * "frequency" specifies out of how many calls we actually check.
5099 * "in_compl_func" is TRUE when called from complete_check(), don't set
5100 * compl_curr_match.
5101 */
5102 void
5103ins_compl_check_keys(int frequency, int in_compl_func)
5104{
5105 static int count = 0;
5106 int c;
5107
5108 // Don't check when reading keys from a script, :normal or feedkeys().
5109 // That would break the test scripts. But do check for keys when called
5110 // from complete_check().
5111 if (!in_compl_func && (using_script() || ex_normal_busy))
5112 return;
5113
5114 // Only do this at regular intervals
5115 if (++count < frequency)
5116 return;
5117 count = 0;
5118
5119 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5120 // can't do its work correctly.
5121 c = vpeekc_any();
5122 if (c != NUL)
5123 {
5124 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5125 {
5126 c = safe_vgetc(); // Eat the character
5127 compl_shows_dir = ins_compl_key2dir(c);
5128 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
5129 c != K_UP && c != K_DOWN, in_compl_func);
5130 }
5131 else
5132 {
5133 // Need to get the character to have KeyTyped set. We'll put it
5134 // back with vungetc() below. But skip K_IGNORE.
5135 c = safe_vgetc();
5136 if (c != K_IGNORE)
5137 {
5138 // Don't interrupt completion when the character wasn't typed,
5139 // e.g., when doing @q to replay keys.
5140 if (c != Ctrl_R && KeyTyped)
5141 compl_interrupted = TRUE;
5142
5143 vungetc(c);
5144 }
5145 }
5146 }
zeertzjq529b9ad2024-06-05 20:27:06 +02005147 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005148 {
5149 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5150
5151 compl_pending = 0;
5152 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
5153 }
5154}
5155
5156/*
5157 * Decide the direction of Insert mode complete from the key typed.
5158 * Returns BACKWARD or FORWARD.
5159 */
5160 static int
5161ins_compl_key2dir(int c)
5162{
5163 if (c == Ctrl_P || c == Ctrl_L
5164 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
5165 return BACKWARD;
5166 return FORWARD;
5167}
5168
5169/*
5170 * Return TRUE for keys that are used for completion only when the popup menu
5171 * is visible.
5172 */
5173 static int
5174ins_compl_pum_key(int c)
5175{
5176 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
5177 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5178 || c == K_UP || c == K_DOWN);
5179}
5180
5181/*
5182 * Decide the number of completions to move forward.
5183 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5184 */
5185 static int
5186ins_compl_key2count(int c)
5187{
5188 int h;
5189
5190 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
5191 {
5192 h = pum_get_height();
5193 if (h > 3)
5194 h -= 2; // keep some context
5195 return h;
5196 }
5197 return 1;
5198}
5199
5200/*
5201 * Return TRUE if completion with "c" should insert the match, FALSE if only
5202 * to change the currently selected completion.
5203 */
5204 static int
5205ins_compl_use_match(int c)
5206{
5207 switch (c)
5208 {
5209 case K_UP:
5210 case K_DOWN:
5211 case K_PAGEDOWN:
5212 case K_KPAGEDOWN:
5213 case K_S_DOWN:
5214 case K_PAGEUP:
5215 case K_KPAGEUP:
5216 case K_S_UP:
5217 return FALSE;
5218 }
5219 return TRUE;
5220}
5221
5222/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005223 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
5224 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01005225 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005226 * Uses the global variables: compl_cont_status and ctrl_x_mode
5227 */
5228 static int
5229get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
5230{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005231 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005232 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005233 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005234 {
5235 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5236 ;
5237 compl_col += ++startcol;
5238 compl_length = curs_col - startcol;
5239 }
5240 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005241 {
John Marriott5e6ea922024-11-23 14:01:57 +01005242 compl_pattern.string = str_foldcase(line + compl_col,
5243 compl_length, NULL, 0);
5244 if (compl_pattern.string == NULL)
5245 {
5246 compl_pattern.length = 0;
5247 return FAIL;
5248 }
5249 compl_pattern.length = STRLEN(compl_pattern.string);
5250 }
5251 else
5252 {
5253 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5254 if (compl_pattern.string == NULL)
5255 {
5256 compl_pattern.length = 0;
5257 return FAIL;
5258 }
5259 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005260 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005261 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005262 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005263 {
5264 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005265 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005266 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005267
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005268 if (!vim_iswordp(line + compl_col)
5269 || (compl_col > 0
5270 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005271 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005272 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005273 prefixlen = 0;
5274 }
John Marriott5e6ea922024-11-23 14:01:57 +01005275
5276 // we need up to 2 extra chars for the prefix
5277 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5278 compl_pattern.string = alloc(n);
5279 if (compl_pattern.string == NULL)
5280 {
5281 compl_pattern.length = 0;
5282 return FAIL;
5283 }
5284 STRCPY((char *)compl_pattern.string, prefix);
5285 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005286 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005287 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005288 }
5289 else if (--startcol < 0
5290 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5291 {
John Marriott5e6ea922024-11-23 14:01:57 +01005292 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5293
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005294 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005295 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5296 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005297 {
John Marriott5e6ea922024-11-23 14:01:57 +01005298 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005299 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005300 }
John Marriott5e6ea922024-11-23 14:01:57 +01005301 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005302 compl_col += curs_col;
5303 compl_length = 0;
5304 }
5305 else
5306 {
5307 // Search the point of change class of multibyte character
5308 // or not a word single byte character backward.
5309 if (has_mbyte)
5310 {
5311 int base_class;
5312 int head_off;
5313
5314 startcol -= (*mb_head_off)(line, line + startcol);
5315 base_class = mb_get_class(line + startcol);
5316 while (--startcol >= 0)
5317 {
5318 head_off = (*mb_head_off)(line, line + startcol);
5319 if (base_class != mb_get_class(line + startcol
5320 - head_off))
5321 break;
5322 startcol -= head_off;
5323 }
5324 }
5325 else
5326 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5327 ;
5328 compl_col += ++startcol;
5329 compl_length = (int)curs_col - startcol;
5330 if (compl_length == 1)
5331 {
5332 // Only match word with at least two chars -- webb
5333 // there's no need to call quote_meta,
5334 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005335 compl_pattern.string = alloc(7);
5336 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005337 {
John Marriott5e6ea922024-11-23 14:01:57 +01005338 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005339 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005340 }
John Marriott5e6ea922024-11-23 14:01:57 +01005341 STRCPY((char *)compl_pattern.string, "\\<");
5342 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5343 STRCAT((char *)compl_pattern.string, "\\k");
5344 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005345 }
5346 else
5347 {
John Marriott5e6ea922024-11-23 14:01:57 +01005348 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5349
5350 compl_pattern.string = alloc(n);
5351 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005352 {
John Marriott5e6ea922024-11-23 14:01:57 +01005353 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005354 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005355 }
John Marriott5e6ea922024-11-23 14:01:57 +01005356 STRCPY((char *)compl_pattern.string, "\\<");
5357 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005358 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005359 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005360 }
5361 }
5362
5363 return OK;
5364}
5365
5366/*
5367 * Get the pattern, column and length for whole line completion or for the
5368 * complete() function.
5369 * Sets the global variables: compl_col, compl_length and compl_pattern.
5370 */
5371 static int
5372get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5373{
5374 compl_col = (colnr_T)getwhitecols(line);
5375 compl_length = (int)curs_col - (int)compl_col;
5376 if (compl_length < 0) // cursor in indent: empty pattern
5377 compl_length = 0;
5378 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005379 {
John Marriott5e6ea922024-11-23 14:01:57 +01005380 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5381 NULL, 0);
5382 if (compl_pattern.string == NULL)
5383 {
5384 compl_pattern.length = 0;
5385 return FAIL;
5386 }
5387 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005388 }
John Marriott5e6ea922024-11-23 14:01:57 +01005389 else
5390 {
5391 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5392 if (compl_pattern.string == NULL)
5393 {
5394 compl_pattern.length = 0;
5395 return FAIL;
5396 }
5397 compl_pattern.length = (size_t)compl_length;
5398 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005399
5400 return OK;
5401}
5402
5403/*
5404 * Get the pattern, column and length for filename completion.
5405 * Sets the global variables: compl_col, compl_length and compl_pattern.
5406 */
5407 static int
5408get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5409{
5410 // Go back to just before the first filename character.
5411 if (startcol > 0)
5412 {
5413 char_u *p = line + startcol;
5414
5415 MB_PTR_BACK(line, p);
5416 while (p > line && vim_isfilec(PTR2CHAR(p)))
5417 MB_PTR_BACK(line, p);
5418 if (p == line && vim_isfilec(PTR2CHAR(p)))
5419 startcol = 0;
5420 else
5421 startcol = (int)(p - line) + 1;
5422 }
5423
5424 compl_col += startcol;
5425 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005426 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5427 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005428 {
John Marriott5e6ea922024-11-23 14:01:57 +01005429 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005430 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005431 }
5432
John Marriott5e6ea922024-11-23 14:01:57 +01005433 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005434
5435 return OK;
5436}
5437
5438/*
5439 * Get the pattern, column and length for command-line completion.
5440 * Sets the global variables: compl_col, compl_length and compl_pattern.
5441 */
5442 static int
5443get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5444{
John Marriott5e6ea922024-11-23 14:01:57 +01005445 compl_pattern.string = vim_strnsave(line, curs_col);
5446 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005447 {
John Marriott5e6ea922024-11-23 14:01:57 +01005448 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005449 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005450 }
John Marriott5e6ea922024-11-23 14:01:57 +01005451 compl_pattern.length = curs_col;
5452 set_cmd_context(&compl_xp, compl_pattern.string,
5453 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005454 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5455 || compl_xp.xp_context == EXPAND_NOTHING)
5456 // No completion possible, use an empty pattern to get a
5457 // "pattern not found" message.
5458 compl_col = curs_col;
5459 else
John Marriott5e6ea922024-11-23 14:01:57 +01005460 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005461 compl_length = curs_col - compl_col;
5462
5463 return OK;
5464}
5465
5466/*
5467 * Get the pattern, column and length for user defined completion ('omnifunc',
5468 * 'completefunc' and 'thesaurusfunc')
5469 * Sets the global variables: compl_col, compl_length and compl_pattern.
5470 * Uses the global variable: spell_bad_len
5471 */
5472 static int
5473get_userdefined_compl_info(colnr_T curs_col UNUSED)
5474{
5475 int ret = FAIL;
5476
5477#ifdef FEAT_COMPL_FUNC
5478 // Call user defined function 'completefunc' with "a:findstart"
5479 // set to 1 to obtain the length of text to use for completion.
5480 char_u *line;
5481 typval_T args[3];
5482 int col;
5483 char_u *funcname;
5484 pos_T pos;
5485 int save_State = State;
5486 callback_T *cb;
5487
5488 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5489 // length as a string
5490 funcname = get_complete_funcname(ctrl_x_mode);
5491 if (*funcname == NUL)
5492 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005493 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005494 ? "completefunc" : "omnifunc");
5495 return FAIL;
5496 }
5497
5498 args[0].v_type = VAR_NUMBER;
5499 args[0].vval.v_number = 1;
5500 args[1].v_type = VAR_STRING;
5501 args[1].vval.v_string = (char_u *)"";
5502 args[2].v_type = VAR_UNKNOWN;
5503 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005504 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005505 cb = get_insert_callback(ctrl_x_mode);
5506 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005507 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005508
5509 State = save_State;
5510 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005511 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005512 validate_cursor();
5513 if (!EQUAL_POS(curwin->w_cursor, pos))
5514 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005515 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005516 return FAIL;
5517 }
5518
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005519 // Return value -2 means the user complete function wants to cancel the
5520 // complete without an error, do the same if the function did not execute
5521 // successfully.
5522 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005523 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005524 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005525 if (col == -3)
5526 {
5527 ctrl_x_mode = CTRL_X_NORMAL;
5528 edit_submode = NULL;
5529 if (!shortmess(SHM_COMPLETIONMENU))
5530 msg_clr_cmdline();
5531 return FAIL;
5532 }
5533
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005534 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005535 // completion.
5536 compl_opt_refresh_always = FALSE;
5537 compl_opt_suppress_empty = FALSE;
5538
5539 if (col < 0)
5540 col = curs_col;
5541 compl_col = col;
5542 if (compl_col > curs_col)
5543 compl_col = curs_col;
5544
5545 // Setup variables for completion. Need to obtain "line" again,
5546 // it may have become invalid.
5547 line = ml_get(curwin->w_cursor.lnum);
5548 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005549 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5550 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005551 {
John Marriott5e6ea922024-11-23 14:01:57 +01005552 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005553 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005554 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005555
John Marriott5e6ea922024-11-23 14:01:57 +01005556 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005557 ret = OK;
5558#endif
5559
5560 return ret;
5561}
5562
5563/*
5564 * Get the pattern, column and length for spell completion.
5565 * Sets the global variables: compl_col, compl_length and compl_pattern.
5566 * Uses the global variable: spell_bad_len
5567 */
5568 static int
5569get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5570{
5571 int ret = FAIL;
5572#ifdef FEAT_SPELL
5573 char_u *line;
5574
5575 if (spell_bad_len > 0)
5576 compl_col = curs_col - spell_bad_len;
5577 else
5578 compl_col = spell_word_start(startcol);
5579 if (compl_col >= (colnr_T)startcol)
5580 {
5581 compl_length = 0;
5582 compl_col = curs_col;
5583 }
5584 else
5585 {
5586 spell_expand_check_cap(compl_col);
5587 compl_length = (int)curs_col - compl_col;
5588 }
5589 // Need to obtain "line" again, it may have become invalid.
5590 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005591 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5592 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005593 {
John Marriott5e6ea922024-11-23 14:01:57 +01005594 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005595 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005596 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005597
John Marriott5e6ea922024-11-23 14:01:57 +01005598 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005599 ret = OK;
5600#endif
5601
5602 return ret;
5603}
5604
5605/*
5606 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005607 * "startcol" - start column number of the completion pattern/text
5608 * "cur_col" - current cursor column
5609 * On return, "line_invalid" is set to TRUE, if the current line may have
5610 * become invalid and needs to be fetched again.
5611 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005612 */
5613 static int
5614compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5615{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005616 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005617 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5618 && !thesaurus_func_complete(ctrl_x_mode)))
5619 {
5620 return get_normal_compl_info(line, startcol, curs_col);
5621 }
5622 else if (ctrl_x_mode_line_or_eval())
5623 {
5624 return get_wholeline_compl_info(line, curs_col);
5625 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005626 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005627 {
5628 return get_filename_compl_info(line, startcol, curs_col);
5629 }
5630 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5631 {
5632 return get_cmdline_compl_info(line, curs_col);
5633 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005634 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005635 || thesaurus_func_complete(ctrl_x_mode))
5636 {
5637 if (get_userdefined_compl_info(curs_col) == FAIL)
5638 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005639 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005640 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005641 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005642 {
5643 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5644 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005645 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005646 }
5647 else
5648 {
5649 internal_error("ins_complete()");
5650 return FAIL;
5651 }
5652
5653 return OK;
5654}
5655
5656/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005657 * Continue an interrupted completion mode search in "line".
5658 *
5659 * If this same ctrl_x_mode has been interrupted use the text from
5660 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5661 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5662 * the same line as the cursor then fix it (the line has been split because it
5663 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5664 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005665 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005666 static void
5667ins_compl_continue_search(char_u *line)
5668{
5669 // it is a continued search
5670 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005671 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5672 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005673 {
5674 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5675 {
5676 // line (probably) wrapped, set compl_startpos to the
5677 // first non_blank in the line, if it is not a wordchar
5678 // include it to get a better pattern, but then we don't
5679 // want the "\\<" prefix, check it below
5680 compl_col = (colnr_T)getwhitecols(line);
5681 compl_startpos.col = compl_col;
5682 compl_startpos.lnum = curwin->w_cursor.lnum;
5683 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5684 }
5685 else
5686 {
5687 // S_IPOS was set when we inserted a word that was at the
5688 // beginning of the line, which means that we'll go to SOL
5689 // mode but first we need to redefine compl_startpos
5690 if (compl_cont_status & CONT_S_IPOS)
5691 {
5692 compl_cont_status |= CONT_SOL;
5693 compl_startpos.col = (colnr_T)(skipwhite(
5694 line + compl_length
5695 + compl_startpos.col) - line);
5696 }
5697 compl_col = compl_startpos.col;
5698 }
5699 compl_length = curwin->w_cursor.col - (int)compl_col;
5700 // IObuff is used to add a "word from the next line" would we
5701 // have enough space? just being paranoid
5702#define MIN_SPACE 75
5703 if (compl_length > (IOSIZE - MIN_SPACE))
5704 {
5705 compl_cont_status &= ~CONT_SOL;
5706 compl_length = (IOSIZE - MIN_SPACE);
5707 compl_col = curwin->w_cursor.col - compl_length;
5708 }
5709 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5710 if (compl_length < 1)
5711 compl_cont_status &= CONT_LOCAL;
5712 }
5713 else if (ctrl_x_mode_line_or_eval())
5714 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5715 else
5716 compl_cont_status = 0;
5717}
5718
5719/*
5720 * start insert mode completion
5721 */
5722 static int
5723ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005724{
5725 char_u *line;
5726 int startcol = 0; // column where searched text starts
5727 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005728 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005729 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005730 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005731
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005732 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005733
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005734 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005735 did_si = FALSE;
5736 can_si = FALSE;
5737 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005738 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005739 return FAIL;
5740
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005741 line = ml_get(curwin->w_cursor.lnum);
5742 curs_col = curwin->w_cursor.col;
5743 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01005744 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005745
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005746 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5747 && compl_cont_mode == ctrl_x_mode)
5748 // this same ctrl-x_mode was interrupted previously. Continue the
5749 // completion.
5750 ins_compl_continue_search(line);
5751 else
5752 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005753
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005754 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005755 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005756 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005757 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005758 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5759 compl_cont_status = 0;
5760 compl_cont_status |= CONT_N_ADDS;
5761 compl_startpos = curwin->w_cursor;
5762 startcol = (int)curs_col;
5763 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005764 }
5765
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005766 // Work out completion pattern and original text -- webb
5767 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5768 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005769 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5770 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005771 // restore did_ai, so that adding comment leader works
5772 did_ai = save_did_ai;
5773 return FAIL;
5774 }
5775 // If "line" was changed while getting completion info get it again.
5776 if (line_invalid)
5777 line = ml_get(curwin->w_cursor.lnum);
5778
glepnir7cfe6932024-09-15 20:06:28 +02005779 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005780 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005781 {
5782 edit_submode_pre = (char_u *)_(" Adding");
5783 if (ctrl_x_mode_line_or_eval())
5784 {
5785 // Insert a new line, keep indentation but ignore 'comments'.
5786 char_u *old = curbuf->b_p_com;
5787
5788 curbuf->b_p_com = (char_u *)"";
5789 compl_startpos.lnum = curwin->w_cursor.lnum;
5790 compl_startpos.col = compl_col;
5791 ins_eol('\r');
5792 curbuf->b_p_com = old;
5793 compl_length = 0;
5794 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01005795 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005796 }
glepnir7cfe6932024-09-15 20:06:28 +02005797 else if (ctrl_x_mode_normal() && in_fuzzy)
5798 {
5799 compl_startpos = curwin->w_cursor;
5800 compl_cont_status &= CONT_S_IPOS;
5801 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005802 }
5803 else
5804 {
5805 edit_submode_pre = NULL;
5806 compl_startpos.col = compl_col;
5807 }
5808
5809 if (compl_cont_status & CONT_LOCAL)
5810 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5811 else
5812 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5813
5814 // If any of the original typed text has been changed we need to fix
5815 // the redo buffer.
5816 ins_compl_fixRedoBufForLeader(NULL);
5817
5818 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005819 VIM_CLEAR_STRING(compl_orig_text);
5820 compl_orig_text.length = (size_t)compl_length;
5821 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005822 if (p_ic)
5823 flags |= CP_ICASE;
zeertzjq4422de62025-03-07 19:06:02 +01005824 if (compl_orig_text.string == NULL
5825 || ins_compl_add(compl_orig_text.string,
5826 (int)compl_orig_text.length,
5827 NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005828 {
John Marriott5e6ea922024-11-23 14:01:57 +01005829 VIM_CLEAR_STRING(compl_pattern);
5830 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005831 return FAIL;
5832 }
5833
5834 // showmode might reset the internal line pointers, so it must
5835 // be called before line = ml_get(), or when this address is no
5836 // longer needed. -- Acevedo.
5837 edit_submode_extra = (char_u *)_("-- Searching...");
5838 edit_submode_highl = HLF_COUNT;
5839 showmode();
5840 edit_submode_extra = NULL;
5841 out_flush();
5842
5843 return OK;
5844}
5845
5846/*
5847 * display the completion status message
5848 */
5849 static void
5850ins_compl_show_statusmsg(void)
5851{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005852 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005853 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005854 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005855 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005856 ? (char_u *)_("Hit end of paragraph")
5857 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005858 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005859 }
5860
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005861 if (edit_submode_extra == NULL)
5862 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005863 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005864 {
5865 edit_submode_extra = (char_u *)_("Back at original");
5866 edit_submode_highl = HLF_W;
5867 }
5868 else if (compl_cont_status & CONT_S_IPOS)
5869 {
5870 edit_submode_extra = (char_u *)_("Word from other line");
5871 edit_submode_highl = HLF_COUNT;
5872 }
5873 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5874 {
5875 edit_submode_extra = (char_u *)_("The only match");
5876 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005877 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005878 }
5879 else
5880 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005881#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005882 // Update completion sequence number when needed.
5883 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005884 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005885#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005886 // The match should always have a sequence number now, this is
5887 // just a safety check.
5888 if (compl_curr_match->cp_number != -1)
5889 {
5890 // Space for 10 text chars. + 2x10-digit no.s = 31.
5891 // Translations may need more than twice that.
5892 static char_u match_ref[81];
5893
5894 if (compl_matches > 0)
5895 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005896 _("match %d of %d"),
5897 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005898 else
5899 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005900 _("match %d"),
5901 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005902 edit_submode_extra = match_ref;
5903 edit_submode_highl = HLF_R;
5904 if (dollar_vcol >= 0)
5905 curs_columns(FALSE);
5906 }
5907 }
5908 }
5909
5910 // Show a message about what (completion) mode we're in.
5911 if (!compl_opt_suppress_empty)
5912 {
5913 showmode();
5914 if (!shortmess(SHM_COMPLETIONMENU))
5915 {
5916 if (edit_submode_extra != NULL)
5917 {
5918 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005919 {
5920 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005921 msg_attr((char *)edit_submode_extra,
5922 edit_submode_highl < HLF_COUNT
5923 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005924 msg_hist_off = FALSE;
5925 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005926 }
5927 else
5928 msg_clr_cmdline(); // necessary for "noshowmode"
5929 }
5930 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005931}
5932
5933/*
5934 * Do Insert mode completion.
5935 * Called when character "c" was typed, which has a meaning for completion.
5936 * Returns OK if completion was done, FAIL if something failed (out of mem).
5937 */
5938 int
5939ins_complete(int c, int enable_pum)
5940{
5941 int n;
5942 int save_w_wrow;
5943 int save_w_leftcol;
5944 int insert_match;
5945
5946 compl_direction = ins_compl_key2dir(c);
5947 insert_match = ins_compl_use_match(c);
5948
5949 if (!compl_started)
5950 {
5951 if (ins_compl_start() == FAIL)
5952 return FAIL;
5953 }
5954 else if (insert_match && stop_arrow() == FAIL)
5955 return FAIL;
5956
5957 compl_shown_match = compl_curr_match;
5958 compl_shows_dir = compl_direction;
5959
5960 // Find next match (and following matches).
5961 save_w_wrow = curwin->w_wrow;
5962 save_w_leftcol = curwin->w_leftcol;
5963 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5964
5965 // may undisplay the popup menu
5966 ins_compl_upd_pum();
5967
5968 if (n > 1) // all matches have been found
5969 compl_matches = n;
5970 compl_curr_match = compl_shown_match;
5971 compl_direction = compl_shows_dir;
5972
5973 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5974 // mode.
5975 if (got_int && !global_busy)
5976 {
5977 (void)vgetc();
5978 got_int = FALSE;
5979 }
5980
5981 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005982 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005983 {
5984 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5985 // because we couldn't expand anything at first place, but if we used
5986 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5987 // (such as M in M'exico) if not tried already. -- Acevedo
5988 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005989 || compl_status_adding()
5990 || (ctrl_x_mode_not_default()
5991 && !ctrl_x_mode_path_patterns()
5992 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005993 compl_cont_status &= ~CONT_N_ADDS;
5994 }
5995
5996 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5997 compl_cont_status |= CONT_S_IPOS;
5998 else
5999 compl_cont_status &= ~CONT_S_IPOS;
6000
6001 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006002
6003 // Show the popup menu, unless we got interrupted.
6004 if (enable_pum && !compl_interrupted)
6005 show_pum(save_w_wrow, save_w_leftcol);
6006
6007 compl_was_interrupted = compl_interrupted;
6008 compl_interrupted = FALSE;
6009
6010 return OK;
6011}
6012
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00006013/*
6014 * Remove (if needed) and show the popup menu
6015 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006016 static void
6017show_pum(int prev_w_wrow, int prev_w_leftcol)
6018{
6019 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006020 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006021 RedrawingDisabled = 0;
6022
6023 // If the cursor moved or the display scrolled we need to remove the pum
6024 // first.
6025 setcursor();
6026 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
6027 ins_compl_del_pum();
6028
6029 ins_compl_show_pum();
6030 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006031
6032 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006033}
6034
6035/*
6036 * Looks in the first "len" chars. of "src" for search-metachars.
6037 * If dest is not NULL the chars. are copied there quoting (with
6038 * a backslash) the metachars, and dest would be NUL terminated.
6039 * Returns the length (needed) of dest
6040 */
6041 static unsigned
6042quote_meta(char_u *dest, char_u *src, int len)
6043{
6044 unsigned m = (unsigned)len + 1; // one extra for the NUL
6045
6046 for ( ; --len >= 0; src++)
6047 {
6048 switch (*src)
6049 {
6050 case '.':
6051 case '*':
6052 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006053 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006054 break;
6055 // FALLTHROUGH
6056 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006057 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006058 break;
6059 // FALLTHROUGH
6060 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006061 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006062 break;
6063 // FALLTHROUGH
6064 case '^': // currently it's not needed.
6065 case '$':
6066 m++;
6067 if (dest != NULL)
6068 *dest++ = '\\';
6069 break;
6070 }
6071 if (dest != NULL)
6072 *dest++ = *src;
6073 // Copy remaining bytes of a multibyte character.
6074 if (has_mbyte)
6075 {
6076 int i, mb_len;
6077
6078 mb_len = (*mb_ptr2len)(src) - 1;
6079 if (mb_len > 0 && len >= mb_len)
6080 for (i = 0; i < mb_len; ++i)
6081 {
6082 --len;
6083 ++src;
6084 if (dest != NULL)
6085 *dest++ = *src;
6086 }
6087 }
6088 }
6089 if (dest != NULL)
6090 *dest = NUL;
6091
6092 return m;
6093}
6094
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006095#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006096 void
6097free_insexpand_stuff(void)
6098{
John Marriott5e6ea922024-11-23 14:01:57 +01006099 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006100# ifdef FEAT_EVAL
6101 free_callback(&cfu_cb);
6102 free_callback(&ofu_cb);
6103 free_callback(&tsrfu_cb);
6104# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006105}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006106#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006107
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006108#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006109/*
6110 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6111 * spelled word, if there is one.
6112 */
6113 static void
6114spell_back_to_badword(void)
6115{
6116 pos_T tpos = curwin->w_cursor;
6117
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02006118 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006119 if (curwin->w_cursor.col != tpos.col)
6120 start_arrow(&tpos);
6121}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006122#endif