blob: 856c06d593ef158ba2d2ccb89141e500f589da0d [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010091 * Structure used to store one match for insert completion.
92 */
93typedef struct compl_S compl_T;
94struct compl_S
95{
96 compl_T *cp_next;
97 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +010098 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +010099 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200100 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100101#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100102 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100103#endif
glepnir0fe17f82024-10-08 22:26:44 +0200104 char_u *cp_fname; // file containing the match, allocated when
105 // cp_flags has CP_FREE_FNAME
106 int cp_flags; // CP_ values
107 int cp_number; // sequence number
108 int cp_score; // fuzzy match score
glepnird4088ed2024-12-31 10:55:22 +0100109 int cp_in_match_array; // collected by compl_match_array
glepnir7baa0142024-10-09 20:19:25 +0200110 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200111 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100112};
113
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200114// values for cp_flags
115# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
116# define CP_FREE_FNAME 2 // cp_fname is allocated
117# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
118# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
119# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200120# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100122/*
123 * All the current matches are stored in a list.
124 * "compl_first_match" points to the start of the list.
125 * "compl_curr_match" points to the currently selected entry.
126 * "compl_shown_match" is different from compl_curr_match during
127 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000128 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100129 */
130static compl_T *compl_first_match = NULL;
131static compl_T *compl_curr_match = NULL;
132static compl_T *compl_shown_match = NULL;
133static compl_T *compl_old_match = NULL;
134
135// After using a cursor key <Enter> selects a match in the popup menu,
136// otherwise it inserts a line break.
137static int compl_enter_selects = FALSE;
138
139// When "compl_leader" is not NULL only matches that start with this string
140// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100141static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100142
143static int compl_get_longest = FALSE; // put longest common string
144 // in compl_leader
145
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100146// Selected one of the matches. When FALSE the match was edited or using the
147// longest common string.
148static int compl_used_match;
149
150// didn't finish finding completions.
151static int compl_was_interrupted = FALSE;
152
153// Set when character typed while looking for matches and it means we should
154// stop looking for matches.
155static int compl_interrupted = FALSE;
156
157static int compl_restarting = FALSE; // don't insert match
158
159// When the first completion is done "compl_started" is set. When it's
160// FALSE the word to be completed must be located.
161static int compl_started = FALSE;
162
163// Which Ctrl-X mode are we in?
164static int ctrl_x_mode = CTRL_X_NORMAL;
165
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000166static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100167static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100168static int compl_direction = FORWARD;
169static int compl_shows_dir = FORWARD;
170static int compl_pending = 0; // > 1 for postponed CTRL-N
171static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000172// Length in bytes of the text being completed (this is deleted to be replaced
173// by the match.)
174static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100175static colnr_T compl_col = 0; // column where the text starts
176 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100177static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100178static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100179 // completion started
180static int compl_cont_mode = 0;
181static expand_T compl_xp;
182
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// List of flags for method of completion.
184static int compl_cont_status = 0;
185# define CONT_ADDING 1 // "normal" or "adding" expansion
186# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
187 // it's set only iff N_ADDS is set
188# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
189# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
190 // if so, word-wise-expansion will set SOL
191# define CONT_SOL 16 // pattern includes start of line, just for
192 // word-wise expansion, not set for ^X^L
193# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
194 // expansion, (eg use complete=.)
195
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100196static int compl_opt_refresh_always = FALSE;
197static int compl_opt_suppress_empty = FALSE;
198
glepnira218cc62024-06-03 19:32:39 +0200199static int compl_selected_item = -1;
200
glepnir8159fb12024-07-17 20:32:54 +0200201static int *compl_fuzzy_scores;
202
glepnir6a38aff2024-12-16 21:56:16 +0100203// "compl_match_array" points the currently displayed list of entries in the
204// popup menu. It is NULL when there is no popup menu.
205static pumitem_T *compl_match_array = NULL;
206static int compl_match_arraysize;
207
glepnir80b66202024-11-27 21:53:53 +0100208static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int *user_hl);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100209static void ins_compl_longest_match(compl_T *match);
210static void ins_compl_del_pum(void);
211static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
212static char_u *find_line_end(char_u *ptr);
213static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100214static int ins_compl_need_restart(void);
215static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000216static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100217static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100218static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100219static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
220# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
221static void ins_compl_add_list(list_T *list);
222static void ins_compl_add_dict(dict_T *dict);
223# endif
224static int ins_compl_key2dir(int c);
225static int ins_compl_pum_key(int c);
226static int ins_compl_key2count(int c);
227static void show_pum(int prev_w_wrow, int prev_w_leftcol);
228static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100229
230#ifdef FEAT_SPELL
231static void spell_back_to_badword(void);
232static int spell_bad_len = 0; // length of located bad word
233#endif
234
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100235/*
236 * CTRL-X pressed in Insert mode.
237 */
238 void
239ins_ctrl_x(void)
240{
zeertzjqdca29d92021-08-31 19:12:51 +0200241 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100242 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000243 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100244 if (compl_cont_status & CONT_N_ADDS)
245 compl_cont_status |= CONT_INTRPT;
246 else
247 compl_cont_status = 0;
248 // We're not sure which CTRL-X mode it will be yet
249 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
250 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
251 edit_submode_pre = NULL;
252 showmode();
253 }
zeertzjqdca29d92021-08-31 19:12:51 +0200254 else
255 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
256 // CTRL-V look like CTRL-N
257 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100258
LemonBoy2bf52dd2022-04-09 18:17:34 +0100259 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100260}
261
262/*
263 * Functions to check the current CTRL-X mode.
264 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000265int ctrl_x_mode_none(void)
266 { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void)
268 { return ctrl_x_mode == CTRL_X_NORMAL; }
269int ctrl_x_mode_scroll(void)
270 { return ctrl_x_mode == CTRL_X_SCROLL; }
271int ctrl_x_mode_whole_line(void)
272 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
273int ctrl_x_mode_files(void)
274 { return ctrl_x_mode == CTRL_X_FILES; }
275int ctrl_x_mode_tags(void)
276 { return ctrl_x_mode == CTRL_X_TAGS; }
277int ctrl_x_mode_path_patterns(void)
278 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
279int ctrl_x_mode_path_defines(void)
280 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
281int ctrl_x_mode_dictionary(void)
282 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
283int ctrl_x_mode_thesaurus(void)
284 { return ctrl_x_mode == CTRL_X_THESAURUS; }
285int ctrl_x_mode_cmdline(void)
286 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200287 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000288int ctrl_x_mode_function(void)
289 { return ctrl_x_mode == CTRL_X_FUNCTION; }
290int ctrl_x_mode_omni(void)
291 { return ctrl_x_mode == CTRL_X_OMNI; }
292int ctrl_x_mode_spell(void)
293 { return ctrl_x_mode == CTRL_X_SPELL; }
294static int ctrl_x_mode_eval(void)
295 { return ctrl_x_mode == CTRL_X_EVAL; }
296int ctrl_x_mode_line_or_eval(void)
297 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100298
299/*
300 * Whether other than default completion has been selected.
301 */
302 int
303ctrl_x_mode_not_default(void)
304{
305 return ctrl_x_mode != CTRL_X_NORMAL;
306}
307
308/*
zeertzjqdca29d92021-08-31 19:12:51 +0200309 * Whether CTRL-X was typed without a following character,
310 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100311 */
312 int
313ctrl_x_mode_not_defined_yet(void)
314{
315 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
316}
317
318/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000319 * Return TRUE if currently in "normal" or "adding" insert completion matches
320 * state
321 */
322 int
323compl_status_adding(void)
324{
325 return compl_cont_status & CONT_ADDING;
326}
327
328/*
329 * Return TRUE if the completion pattern includes start of line, just for
330 * word-wise expansion.
331 */
332 int
333compl_status_sol(void)
334{
335 return compl_cont_status & CONT_SOL;
336}
337
338/*
339 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
340 */
341 int
342compl_status_local(void)
343{
344 return compl_cont_status & CONT_LOCAL;
345}
346
347/*
348 * Clear the completion status flags
349 */
350 void
351compl_status_clear(void)
352{
353 compl_cont_status = 0;
354}
355
356/*
357 * Return TRUE if completion is using the forward direction matches
358 */
359 static int
360compl_dir_forward(void)
361{
362 return compl_direction == FORWARD;
363}
364
365/*
366 * Return TRUE if currently showing forward completion matches
367 */
368 static int
369compl_shows_dir_forward(void)
370{
371 return compl_shows_dir == FORWARD;
372}
373
374/*
375 * Return TRUE if currently showing backward completion matches
376 */
377 static int
378compl_shows_dir_backward(void)
379{
380 return compl_shows_dir == BACKWARD;
381}
382
383/*
384 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100385 */
386 int
387has_compl_option(int dict_opt)
388{
389 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200390#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200392#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100393 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100394 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
395#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100396 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100397#endif
398 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100399 {
400 ctrl_x_mode = CTRL_X_NORMAL;
401 edit_submode = NULL;
402 msg_attr(dict_opt ? _("'dictionary' option is empty")
403 : _("'thesaurus' option is empty"),
404 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100405 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100406 {
407 vim_beep(BO_COMPL);
408 setcursor();
409 out_flush();
410#ifdef FEAT_EVAL
411 if (!get_vim_var_nr(VV_TESTING))
412#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100413 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100414 }
415 return FALSE;
416 }
417 return TRUE;
418}
419
420/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000421 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100422 * This depends on the current mode.
423 */
424 int
425vim_is_ctrl_x_key(int c)
426{
427 // Always allow ^R - let its results then be checked
428 if (c == Ctrl_R)
429 return TRUE;
430
431 // Accept <PageUp> and <PageDown> if the popup menu is visible.
432 if (ins_compl_pum_key(c))
433 return TRUE;
434
435 switch (ctrl_x_mode)
436 {
437 case 0: // Not in any CTRL-X mode
438 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
439 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200440 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100441 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
442 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
443 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
444 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
445 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200446 || c == Ctrl_S || c == Ctrl_K || c == 's'
447 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100448 case CTRL_X_SCROLL:
449 return (c == Ctrl_Y || c == Ctrl_E);
450 case CTRL_X_WHOLE_LINE:
451 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_FILES:
453 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_DICTIONARY:
455 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_THESAURUS:
457 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_TAGS:
459 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
460#ifdef FEAT_FIND_ID
461 case CTRL_X_PATH_PATTERNS:
462 return (c == Ctrl_P || c == Ctrl_N);
463 case CTRL_X_PATH_DEFINES:
464 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
465#endif
466 case CTRL_X_CMDLINE:
467 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
468 || c == Ctrl_X);
469#ifdef FEAT_COMPL_FUNC
470 case CTRL_X_FUNCTION:
471 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
472 case CTRL_X_OMNI:
473 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
474#endif
475 case CTRL_X_SPELL:
476 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
477 case CTRL_X_EVAL:
478 return (c == Ctrl_P || c == Ctrl_N);
479 }
480 internal_error("vim_is_ctrl_x_key()");
481 return FALSE;
482}
483
484/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000485 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000486 */
487 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000488match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000489{
490 return match->cp_flags & CP_ORIGINAL_TEXT;
491}
492
493/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000494 * Returns TRUE if "match" is the first match in the completion list.
495 */
496 static int
497is_first_match(compl_T *match)
498{
499 return match == compl_first_match;
500}
501
502/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100503 * Return TRUE when character "c" is part of the item currently being
504 * completed. Used to decide whether to abandon complete mode when the menu
505 * is visible.
506 */
507 int
508ins_compl_accept_char(int c)
509{
510 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
511 // When expanding an identifier only accept identifier chars.
512 return vim_isIDc(c);
513
514 switch (ctrl_x_mode)
515 {
516 case CTRL_X_FILES:
517 // When expanding file name only accept file name chars. But not
518 // path separators, so that "proto/<Tab>" expands files in
519 // "proto", not "proto/" as a whole
520 return vim_isfilec(c) && !vim_ispathsep(c);
521
522 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200523 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100524 case CTRL_X_OMNI:
525 // Command line and Omni completion can work with just about any
526 // printable character, but do stop at white space.
527 return vim_isprintc(c) && !VIM_ISWHITE(c);
528
529 case CTRL_X_WHOLE_LINE:
530 // For while line completion a space can be part of the line.
531 return vim_isprintc(c);
532 }
533 return vim_iswordc(c);
534}
535
536/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000537 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100538 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000539 */
540 static char_u *
541ins_compl_infercase_gettext(
542 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100543 int char_len,
544 int compl_char_len,
545 int min_len,
546 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000547{
548 int *wca; // Wide character array.
549 char_u *p;
550 int i, c;
551 int has_lower = FALSE;
552 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100553 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000554
555 IObuff[0] = NUL;
556
557 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100558 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000559 if (wca == NULL)
560 return IObuff;
561
562 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100563 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100564 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (has_mbyte)
566 wca[i] = mb_ptr2char_adv(&p);
567 else
568 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100569 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000570
571 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100572 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000573 for (i = 0; i < min_len; ++i)
574 {
glepnir6e199932024-12-14 21:13:27 +0100575 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000576 if (MB_ISLOWER(c))
577 {
578 has_lower = TRUE;
579 if (MB_ISUPPER(wca[i]))
580 {
581 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100582 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000583 wca[i] = MB_TOLOWER(wca[i]);
584 break;
585 }
586 }
587 }
588
589 // Rule 2: No lower case, 2nd consecutive letter converted to
590 // upper case.
591 if (!has_lower)
592 {
John Marriott5e6ea922024-11-23 14:01:57 +0100593 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 for (i = 0; i < min_len; ++i)
595 {
glepnir6e199932024-12-14 21:13:27 +0100596 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000597 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
598 {
599 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100600 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000601 wca[i] = MB_TOUPPER(wca[i]);
602 break;
603 }
604 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
605 }
606 }
607
608 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100609 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000610 for (i = 0; i < min_len; ++i)
611 {
glepnir6e199932024-12-14 21:13:27 +0100612 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000613 if (MB_ISLOWER(c))
614 wca[i] = MB_TOLOWER(wca[i]);
615 else if (MB_ISUPPER(c))
616 wca[i] = MB_TOUPPER(wca[i]);
617 }
618
619 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000620 p = IObuff;
621 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100622 ga_init2(&gap, 1, 500);
623 while (i < char_len)
624 {
625 if (gap.ga_data != NULL)
626 {
627 if (ga_grow(&gap, 10) == FAIL)
628 {
629 ga_clear(&gap);
630 return (char_u *)"[failed]";
631 }
632 p = (char_u *)gap.ga_data + gap.ga_len;
633 if (has_mbyte)
634 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
635 else
636 {
637 *p = wca[i++];
638 ++gap.ga_len;
639 }
640 }
641 else if ((p - IObuff) + 6 >= IOSIZE)
642 {
643 // Multi-byte characters can occupy up to five bytes more than
644 // ASCII characters, and we also need one byte for NUL, so when
645 // getting to six bytes from the edge of IObuff switch to using a
646 // growarray. Add the character in the next round.
647 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100648 {
649 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100650 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100651 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100652 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100653 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100654 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100655 }
656 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000657 p += (*mb_char2bytes)(wca[i++], p);
658 else
659 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000661 vim_free(wca);
662
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 if (gap.ga_data != NULL)
664 {
665 *tofree = gap.ga_data;
666 return gap.ga_data;
667 }
668
669 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000670 return IObuff;
671}
672
673/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100674 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
675 * case of the originally typed text is used, and the case of the completed
676 * text is inferred, ie this tries to work out what case you probably wanted
677 * the rest of the word to be in -- webb
678 */
679 int
680ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200681 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 int len,
683 int icase,
684 char_u *fname,
685 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200686 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200688 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100689 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100690 int char_len; // count multi-byte characters
691 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200693 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100694 int res;
695 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100696
697 if (p_ic && curbuf->b_p_inf && len > 0)
698 {
699 // Infer case of completed part.
700
701 // Find actual length of completion.
702 if (has_mbyte)
703 {
704 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100705 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100706 while (*p != NUL)
707 {
708 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100709 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100710 }
711 }
712 else
glepnir6e199932024-12-14 21:13:27 +0100713 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100715 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716
717 // Find actual length of original text.
718 if (has_mbyte)
719 {
John Marriott5e6ea922024-11-23 14:01:57 +0100720 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722 while (*p != NUL)
723 {
724 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100725 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100726 }
727 }
728 else
glepnir6e199932024-12-14 21:13:27 +0100729 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100730 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100731 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100734 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100735 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 str = ins_compl_infercase_gettext(str, char_len,
738 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200740 if (cont_s_ipos)
741 flags |= CP_CONT_S_IPOS;
742 if (icase)
743 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200744
glepnir5c66e232024-11-15 19:58:27 +0100745 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100746 vim_free(tofree);
747 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100748}
749
750/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000751 * Add a match to the list of matches. The arguments are:
752 * str - text of the match to add
753 * len - length of "str". If -1, then the length of "str" is
754 * computed.
755 * fname - file name to associate with this match.
756 * cptext - list of strings to use with this match (for abbr, menu, info
757 * and kind)
758 * user_data - user supplied data (any vim type) for this match
759 * cdir - match direction. If 0, use "compl_direction".
760 * flags_arg - match flags (cp_flags)
761 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100762 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000763 * If "cdir" is FORWARD, then the match is added after the current match.
764 * Otherwise, it is added before the current match.
765 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100766 * If the given string is already in the list of completions, then return
767 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
768 * maybe because alloc() returns NULL, then FAIL is returned.
769 */
770 static int
771ins_compl_add(
772 char_u *str,
773 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 char_u **cptext, // extra text for popup menu or NULL
776 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200778 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200779 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100780 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100781{
782 compl_T *match;
783 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200784 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100785
Bram Moolenaarceb06192021-04-04 15:05:22 +0200786 if (flags & CP_FAST)
787 fast_breakcheck();
788 else
789 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100790 if (got_int)
791 return FAIL;
792 if (len < 0)
793 len = (int)STRLEN(str);
794
795 // If the same match is already present, don't add it.
796 if (compl_first_match != NULL && !adup)
797 {
798 match = compl_first_match;
799 do
800 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000801 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100802 && STRNCMP(match->cp_str.string, str, len) == 0
803 && ((int)match->cp_str.length <= len
804 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 return NOTDONE;
806 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000807 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 }
809
810 // Remove any popup menu before changing the list of matches.
811 ins_compl_del_pum();
812
813 // Allocate a new match structure.
814 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200815 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100816 if (match == NULL)
817 return FAIL;
818 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200819 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100820 match->cp_number = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100821 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100822 {
823 vim_free(match);
824 return FAIL;
825 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100826
John Marriott5e6ea922024-11-23 14:01:57 +0100827 match->cp_str.length = len;
828
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 // match-fname is:
830 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200831 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100832 // - NULL otherwise. --Acevedo
833 if (fname != NULL
834 && compl_curr_match != NULL
835 && compl_curr_match->cp_fname != NULL
836 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
837 match->cp_fname = compl_curr_match->cp_fname;
838 else if (fname != NULL)
839 {
840 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200841 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100842 }
843 else
844 match->cp_fname = NULL;
845 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100846 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
847 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100848
849 if (cptext != NULL)
850 {
851 int i;
852
853 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100854 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100855 if (cptext[i] != NULL && *cptext[i] != NUL)
856 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100857 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100858 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100859#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100860 if (user_data != NULL)
861 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100862#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000864 // Link the new match structure after (FORWARD) or before (BACKWARD) the
865 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100866 if (compl_first_match == NULL)
867 match->cp_next = match->cp_prev = NULL;
868 else if (dir == FORWARD)
869 {
870 match->cp_next = compl_curr_match->cp_next;
871 match->cp_prev = compl_curr_match;
872 }
873 else // BACKWARD
874 {
875 match->cp_next = compl_curr_match;
876 match->cp_prev = compl_curr_match->cp_prev;
877 }
878 if (match->cp_next)
879 match->cp_next->cp_prev = match;
880 if (match->cp_prev)
881 match->cp_prev->cp_next = match;
882 else // if there's nothing before, it is the first match
883 compl_first_match = match;
884 compl_curr_match = match;
885
886 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200887 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100888 ins_compl_longest_match(match);
889
890 return OK;
891}
892
893/*
894 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100896 */
897 static int
898ins_compl_equal(compl_T *match, char_u *str, int len)
899{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200900 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200901 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200902 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100903 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
904 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100905}
906
907/*
glepnir6a38aff2024-12-16 21:56:16 +0100908 * when len is -1 mean use whole length of p otherwise part of p
909 */
910 static void
911ins_compl_insert_bytes(char_u *p, int len)
912{
913 if (len == -1)
914 len = (int)STRLEN(p);
915 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100916 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100917}
918
919/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100920 * Checks if the column is within the currently inserted completion text
921 * column range. If it is, it returns a special highlight attribute.
922 * -1 mean normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100923 */
924 int
925ins_compl_col_range_attr(int col)
926{
glepnire8908872025-01-08 18:30:45 +0100927 if ((get_cot_flags() & COT_FUZZY))
928 return -1;
929
glepnir9fddb8a2025-01-11 16:42:50 +0100930 if (col >= (compl_col + (int)ins_compl_leader_len()) && col < compl_ins_end_col)
glepnir6a38aff2024-12-16 21:56:16 +0100931 return syn_name2attr((char_u *)"ComplMatchIns");
932
933 return -1;
934}
935
936/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100937 * Reduce the longest common string for match "match".
938 */
939 static void
940ins_compl_longest_match(compl_T *match)
941{
942 char_u *p, *s;
943 int c1, c2;
944 int had_match;
945
John Marriott5e6ea922024-11-23 14:01:57 +0100946 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100947 {
948 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100949 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
950 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000951 return;
952
John Marriott5e6ea922024-11-23 14:01:57 +0100953 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000954 had_match = (curwin->w_cursor.col > compl_col);
955 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100956 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000957 ins_redraw(FALSE);
958
959 // When the match isn't there (to avoid matching itself) remove it
960 // again after redrawing.
961 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100962 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100963 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000964
965 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100966 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967
968 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100969 p = compl_leader.string;
970 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000971 while (*p != NUL)
972 {
973 if (has_mbyte)
974 {
975 c1 = mb_ptr2char(p);
976 c2 = mb_ptr2char(s);
977 }
978 else
979 {
980 c1 = *p;
981 c2 = *s;
982 }
983 if ((match->cp_flags & CP_ICASE)
984 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
985 break;
986 if (has_mbyte)
987 {
988 MB_PTR_ADV(p);
989 MB_PTR_ADV(s);
990 }
991 else
992 {
993 ++p;
994 ++s;
995 }
996 }
997
998 if (*p != NUL)
999 {
1000 // Leader was shortened, need to change the inserted text.
1001 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001002 compl_leader.length = (size_t)(p - compl_leader.string);
1003
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001004 had_match = (curwin->w_cursor.col > compl_col);
1005 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001006 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001007 ins_redraw(FALSE);
1008
1009 // When the match isn't there (to avoid matching itself) remove it
1010 // again after redrawing.
1011 if (!had_match)
1012 ins_compl_delete();
1013 }
1014
1015 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001016}
1017
1018/*
1019 * Add an array of matches to the list of matches.
1020 * Frees matches[].
1021 */
1022 static void
1023ins_compl_add_matches(
1024 int num_matches,
1025 char_u **matches,
1026 int icase)
1027{
1028 int i;
1029 int add_r = OK;
1030 int dir = compl_direction;
1031
1032 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001033 {
1034 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1035 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1036 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001037 // if dir was BACKWARD then honor it just once
1038 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001039 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001040 FreeWild(num_matches, matches);
1041}
1042
1043/*
1044 * Make the completion list cyclic.
1045 * Return the number of matches (excluding the original).
1046 */
1047 static int
1048ins_compl_make_cyclic(void)
1049{
1050 compl_T *match;
1051 int count = 0;
1052
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001053 if (compl_first_match == NULL)
1054 return 0;
1055
1056 // Find the end of the list.
1057 match = compl_first_match;
1058 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001059 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001060 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001061 match = match->cp_next;
1062 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001063 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001064 match->cp_next = compl_first_match;
1065 compl_first_match->cp_prev = match;
1066
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001067 return count;
1068}
1069
1070/*
1071 * Return whether there currently is a shown match.
1072 */
1073 int
1074ins_compl_has_shown_match(void)
1075{
1076 return compl_shown_match == NULL
1077 || compl_shown_match != compl_shown_match->cp_next;
1078}
1079
1080/*
1081 * Return whether the shown match is long enough.
1082 */
1083 int
1084ins_compl_long_shown_match(void)
1085{
John Marriott5e6ea922024-11-23 14:01:57 +01001086 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001087 > curwin->w_cursor.col - compl_col;
1088}
1089
1090/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001091 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001093 unsigned int
1094get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001095{
zeertzjq529b9ad2024-06-05 20:27:06 +02001096 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001097}
1098
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001099/*
1100 * Update the screen and when there is any scrolling remove the popup menu.
1101 */
1102 static void
1103ins_compl_upd_pum(void)
1104{
1105 int h;
1106
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001107 if (compl_match_array == NULL)
1108 return;
1109
1110 h = curwin->w_cline_height;
1111 // Update the screen later, before drawing the popup menu over it.
1112 pum_call_update_screen();
1113 if (h != curwin->w_cline_height)
1114 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001115}
1116
1117/*
1118 * Remove any popup menu.
1119 */
1120 static void
1121ins_compl_del_pum(void)
1122{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001123 if (compl_match_array == NULL)
1124 return;
1125
1126 pum_undisplay();
1127 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001128}
1129
1130/*
1131 * Return TRUE if the popup menu should be displayed.
1132 */
1133 int
1134pum_wanted(void)
1135{
1136 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001137 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001138 return FALSE;
1139
1140 // The display looks bad on a B&W display.
1141 if (t_colors < 8
1142#ifdef FEAT_GUI
1143 && !gui.in_use
1144#endif
1145 )
1146 return FALSE;
1147 return TRUE;
1148}
1149
1150/*
1151 * Return TRUE if there are two or more matches to be shown in the popup menu.
1152 * One if 'completopt' contains "menuone".
1153 */
1154 static int
1155pum_enough_matches(void)
1156{
1157 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001158 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159
1160 // Don't display the popup menu if there are no matches or there is only
1161 // one (ignoring the original text).
1162 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163 do
1164 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001165 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001166 break;
1167 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001168 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001169
zeertzjq529b9ad2024-06-05 20:27:06 +02001170 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001171 return (i >= 1);
1172 return (i >= 2);
1173}
1174
Bram Moolenaar3075a452021-11-17 15:51:52 +00001175#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001176/*
1177 * Allocate Dict for the completed item.
1178 * { word, abbr, menu, kind, info }
1179 */
1180 static dict_T *
1181ins_compl_dict_alloc(compl_T *match)
1182{
1183 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1184
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001185 if (dict == NULL)
1186 return NULL;
1187
John Marriott5e6ea922024-11-23 14:01:57 +01001188 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001189 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1190 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1191 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1192 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1193 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1194 dict_add_string(dict, "user_data", (char_u *)"");
1195 else
1196 dict_add_tv(dict, "user_data", &match->cp_user_data);
1197
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001198 return dict;
1199}
1200
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001201/*
1202 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1203 * completion menu is changed.
1204 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001205 static void
1206trigger_complete_changed_event(int cur)
1207{
1208 dict_T *v_event;
1209 dict_T *item;
1210 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001211 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212
1213 if (recursive)
1214 return;
1215
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001216 if (cur < 0)
1217 item = dict_alloc();
1218 else
1219 item = ins_compl_dict_alloc(compl_curr_match);
1220 if (item == NULL)
1221 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001222 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001223 dict_add_dict(v_event, "completed_item", item);
1224 pum_set_event_info(v_event);
1225 dict_set_items_ro(v_event);
1226
1227 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001228 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001229 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001230 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001231 recursive = FALSE;
1232
Bram Moolenaar3075a452021-11-17 15:51:52 +00001233 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001234}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001235#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001236
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001237/*
glepnira218cc62024-06-03 19:32:39 +02001238 * pumitem qsort compare func
1239 */
1240 static int
zeertzjq8e567472024-06-14 20:04:42 +02001241ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001242{
1243 const int sa = (*(pumitem_T *)a).pum_score;
1244 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001245 const int ia = (*(pumitem_T *)a).pum_idx;
1246 const int ib = (*(pumitem_T *)b).pum_idx;
1247 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001248}
1249
1250/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001251 * Build a popup menu to show the completion matches.
1252 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1253 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001254 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001255 static int
1256ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001257{
1258 compl_T *compl;
1259 compl_T *shown_compl = NULL;
1260 int did_find_shown_match = FALSE;
1261 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001262 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001263 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001264 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001265 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001266 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001267 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1268 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnir80b66202024-11-27 21:53:53 +01001269 compl_T *match_head = NULL;
1270 compl_T *match_tail = NULL;
1271 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001272
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001273 // Need to build the popup menu list.
1274 compl_match_arraysize = 0;
1275 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001276
glepnira49c0772024-11-30 10:56:30 +01001277 // If the current match is the original text don't find the first
1278 // match after it, don't highlight anything.
1279 if (match_at_original_text(compl_shown_match))
1280 shown_match_ok = TRUE;
1281
1282 if (compl_leader.string != NULL
1283 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1284 && shown_match_ok == FALSE)
1285 compl_shown_match = compl_no_select ? compl_first_match
1286 : compl_first_match->cp_next;
1287
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 do
1289 {
glepnird4088ed2024-12-31 10:55:22 +01001290 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001291 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1292 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001293 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001294 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001295
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001296 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001297 && (compl_leader.string == NULL
1298 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001299 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001300 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001301 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001302 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001303 if (match_head == NULL)
1304 match_head = compl;
1305 else
glepnira49c0772024-11-30 10:56:30 +01001306 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001307 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001308
glepnirf400a0c2025-01-23 19:55:14 +01001309 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001310 {
1311 if (compl == compl_shown_match || did_find_shown_match)
1312 {
1313 // This item is the shown match or this is the
1314 // first displayed item after the shown match.
1315 compl_shown_match = compl;
1316 did_find_shown_match = TRUE;
1317 shown_match_ok = TRUE;
1318 }
1319 else
1320 // Remember this displayed match for when the
1321 // shown match is just below it.
1322 shown_compl = compl;
1323 cur = i;
1324 }
glepnirf400a0c2025-01-23 19:55:14 +01001325 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001326 {
1327 if (i == 0)
1328 shown_compl = compl;
1329 // Update the maximum fuzzy score and the shown match
1330 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001331 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001332 {
1333 did_find_shown_match = TRUE;
1334 max_fuzzy_score = compl->cp_score;
1335 if (!compl_no_select)
1336 compl_shown_match = compl;
1337 }
zeertzjqd65aa1b2025-01-25 15:29:03 +01001338 else if (!fuzzy_sort && i == 0 && !compl_no_select)
glepnirf400a0c2025-01-23 19:55:14 +01001339 compl_shown_match = shown_compl;
glepnira49c0772024-11-30 10:56:30 +01001340
1341 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1342 {
1343 cur = i;
1344 shown_match_ok = TRUE;
1345 }
1346 }
1347 i++;
glepnir80b66202024-11-27 21:53:53 +01001348 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001349
glepnirf400a0c2025-01-23 19:55:14 +01001350 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001351 {
1352 did_find_shown_match = TRUE;
1353
1354 // When the original text is the shown match don't set
1355 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001356 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001357 shown_match_ok = TRUE;
1358
1359 if (!shown_match_ok && shown_compl != NULL)
1360 {
1361 // The shown match isn't displayed, set it to the
1362 // previously displayed match.
1363 compl_shown_match = shown_compl;
1364 shown_match_ok = TRUE;
1365 }
1366 }
glepnira49c0772024-11-30 10:56:30 +01001367 compl = compl->cp_next;
1368 } while (compl != NULL && !is_first_match(compl));
1369
1370 if (compl_match_arraysize == 0)
1371 return -1;
1372
1373 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1374 if (compl_match_array == NULL)
1375 return -1;
1376
1377 compl = match_head;
1378 i = 0;
1379 while (compl != NULL)
1380 {
glepnir6e199932024-12-14 21:13:27 +01001381 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1382 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001383 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1384 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1385 compl_match_array[i].pum_score = compl->cp_score;
1386 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1387 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001388 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1389 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001390 match_next = compl->cp_match_next;
1391 compl->cp_match_next = NULL;
1392 compl = match_next;
1393 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001394
zeertzjqd65aa1b2025-01-25 15:29:03 +01001395 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001396 {
1397 for (i = 0; i < compl_match_arraysize; i++)
1398 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001399 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001400 qsort(compl_match_array, (size_t)compl_match_arraysize,
1401 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001402 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001403 }
glepnira218cc62024-06-03 19:32:39 +02001404
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001405 if (!shown_match_ok) // no displayed match at all
1406 cur = -1;
1407
1408 return cur;
1409}
1410
1411/*
1412 * Show the popup menu for the list of matches.
1413 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1414 */
1415 void
1416ins_compl_show_pum(void)
1417{
1418 int i;
1419 int cur = -1;
1420 colnr_T col;
1421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 if (!pum_wanted() || !pum_enough_matches())
1423 return;
1424
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001425 // Update the screen later, before drawing the popup menu over it.
1426 pum_call_update_screen();
1427
1428 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001429 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001430 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001431 else
1432 {
1433 // popup menu already exists, only need to find the current item.
1434 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001435 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001436 || compl_match_array[i].pum_text
1437 == compl_shown_match->cp_text[CPT_ABBR])
1438 {
1439 cur = i;
1440 break;
1441 }
1442 }
1443
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001445 {
1446#ifdef FEAT_EVAL
1447 if (compl_started && has_completechanged())
1448 trigger_complete_changed_event(cur);
1449#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001450 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001451 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001452
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001453 // In Replace mode when a $ is displayed at the end of the line only
1454 // part of the screen would be updated. We do need to redraw here.
1455 dollar_vcol = -1;
1456
1457 // Compute the screen column of the start of the completed text.
1458 // Use the cursor to get all wrapping and other settings right.
1459 col = curwin->w_cursor.col;
1460 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001461 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001462 pum_display(compl_match_array, compl_match_arraysize, cur);
1463 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001464
glepnircbb46b42024-02-03 18:11:13 +01001465 // After adding leader, set the current match to shown match.
1466 if (compl_started && compl_curr_match != compl_shown_match)
1467 compl_curr_match = compl_shown_match;
1468
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001469#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001470 if (has_completechanged())
1471 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001472#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001473}
1474
1475#define DICT_FIRST (1) // use just first element in "dict"
1476#define DICT_EXACT (2) // "dict" is the exact name of a file
1477
1478/*
glepnir40c1c332024-06-11 19:37:04 +02001479 * Get current completion leader
1480 */
1481 char_u *
1482ins_compl_leader(void)
1483{
John Marriott5e6ea922024-11-23 14:01:57 +01001484 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1485}
1486
1487/*
1488 * Get current completion leader length
1489 */
1490 size_t
1491ins_compl_leader_len(void)
1492{
1493 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001494}
1495
1496/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001497 * Add any identifiers that match the given pattern "pat" in the list of
1498 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001499 */
1500 static void
1501ins_compl_dictionaries(
1502 char_u *dict_start,
1503 char_u *pat,
1504 int flags, // DICT_FIRST and/or DICT_EXACT
1505 int thesaurus) // Thesaurus completion
1506{
1507 char_u *dict = dict_start;
1508 char_u *ptr;
1509 char_u *buf;
1510 regmatch_T regmatch;
1511 char_u **files;
1512 int count;
1513 int save_p_scs;
1514 int dir = compl_direction;
1515
1516 if (*dict == NUL)
1517 {
1518#ifdef FEAT_SPELL
1519 // When 'dictionary' is empty and spell checking is enabled use
1520 // "spell".
1521 if (!thesaurus && curwin->w_p_spell)
1522 dict = (char_u *)"spell";
1523 else
1524#endif
1525 return;
1526 }
1527
1528 buf = alloc(LSIZE);
1529 if (buf == NULL)
1530 return;
1531 regmatch.regprog = NULL; // so that we can goto theend
1532
1533 // If 'infercase' is set, don't use 'smartcase' here
1534 save_p_scs = p_scs;
1535 if (curbuf->b_p_inf)
1536 p_scs = FALSE;
1537
1538 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1539 // to only match at the start of a line. Otherwise just match the
1540 // pattern. Also need to double backslashes.
1541 if (ctrl_x_mode_line_or_eval())
1542 {
1543 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1544 size_t len;
1545
1546 if (pat_esc == NULL)
1547 goto theend;
1548 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001549 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001550 if (ptr == NULL)
1551 {
1552 vim_free(pat_esc);
1553 goto theend;
1554 }
1555 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1556 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1557 vim_free(pat_esc);
1558 vim_free(ptr);
1559 }
1560 else
1561 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001562 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001563 if (regmatch.regprog == NULL)
1564 goto theend;
1565 }
1566
1567 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1568 regmatch.rm_ic = ignorecase(pat);
1569 while (*dict != NUL && !got_int && !compl_interrupted)
1570 {
1571 // copy one dictionary file name into buf
1572 if (flags == DICT_EXACT)
1573 {
1574 count = 1;
1575 files = &dict;
1576 }
1577 else
1578 {
1579 // Expand wildcards in the dictionary name, but do not allow
1580 // backticks (for security, the 'dict' option may have been set in
1581 // a modeline).
1582 copy_option_part(&dict, buf, LSIZE, ",");
1583# ifdef FEAT_SPELL
1584 if (!thesaurus && STRCMP(buf, "spell") == 0)
1585 count = -1;
1586 else
1587# endif
1588 if (vim_strchr(buf, '`') != NULL
1589 || expand_wildcards(1, &buf, &count, &files,
1590 EW_FILE|EW_SILENT) != OK)
1591 count = 0;
1592 }
1593
1594# ifdef FEAT_SPELL
1595 if (count == -1)
1596 {
1597 // Complete from active spelling. Skip "\<" in the pattern, we
1598 // don't use it as a RE.
1599 if (pat[0] == '\\' && pat[1] == '<')
1600 ptr = pat + 2;
1601 else
1602 ptr = pat;
1603 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1604 }
1605 else
1606# endif
1607 if (count > 0) // avoid warning for using "files" uninit
1608 {
1609 ins_compl_files(count, files, thesaurus, flags,
1610 &regmatch, buf, &dir);
1611 if (flags != DICT_EXACT)
1612 FreeWild(count, files);
1613 }
1614 if (flags != 0)
1615 break;
1616 }
1617
1618theend:
1619 p_scs = save_p_scs;
1620 vim_regfree(regmatch.regprog);
1621 vim_free(buf);
1622}
1623
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001624/*
1625 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1626 * skipping the word at 'skip_word'. Returns OK on success.
1627 */
1628 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001629thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001630 char_u *fname,
1631 char_u **buf_arg,
1632 int dir,
1633 char_u *skip_word)
1634{
1635 int status = OK;
1636 char_u *ptr;
1637 char_u *wstart;
1638
1639 // Add the other matches on the line
1640 ptr = *buf_arg;
1641 while (!got_int)
1642 {
1643 // Find start of the next word. Skip white
1644 // space and punctuation.
1645 ptr = find_word_start(ptr);
1646 if (*ptr == NUL || *ptr == NL)
1647 break;
1648 wstart = ptr;
1649
1650 // Find end of the word.
1651 if (has_mbyte)
1652 // Japanese words may have characters in
1653 // different classes, only separate words
1654 // with single-byte non-word characters.
1655 while (*ptr != NUL)
1656 {
1657 int l = (*mb_ptr2len)(ptr);
1658
1659 if (l < 2 && !vim_iswordc(*ptr))
1660 break;
1661 ptr += l;
1662 }
1663 else
1664 ptr = find_word_end(ptr);
1665
1666 // Add the word. Skip the regexp match.
1667 if (wstart != skip_word)
1668 {
1669 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1670 fname, dir, FALSE);
1671 if (status == FAIL)
1672 break;
1673 }
1674 }
1675
1676 *buf_arg = ptr;
1677 return status;
1678}
1679
1680/*
1681 * Process "count" dictionary/thesaurus "files" and add the text matching
1682 * "regmatch".
1683 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684 static void
1685ins_compl_files(
1686 int count,
1687 char_u **files,
1688 int thesaurus,
1689 int flags,
1690 regmatch_T *regmatch,
1691 char_u *buf,
1692 int *dir)
1693{
1694 char_u *ptr;
1695 int i;
1696 FILE *fp;
1697 int add_r;
1698
1699 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1700 {
1701 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001702 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001703 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001704 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001705 vim_snprintf((char *)IObuff, IOSIZE,
1706 _("Scanning dictionary: %s"), (char *)files[i]);
1707 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1708 }
1709
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001710 if (fp == NULL)
1711 continue;
1712
1713 // Read dictionary file line by line.
1714 // Check each line for a match.
1715 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001716 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 ptr = buf;
1718 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001719 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001721 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1722 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001723 add_r = ins_compl_add_infercase(regmatch->startp[0],
1724 (int)(ptr - regmatch->startp[0]),
1725 p_ic, files[i], *dir, FALSE);
1726 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001727 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001728 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001729 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001730 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001731 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001732 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001733 if (add_r == OK)
1734 // if dir was BACKWARD then honor it just once
1735 *dir = FORWARD;
1736 else if (add_r == FAIL)
1737 break;
1738 // avoid expensive call to vim_regexec() when at end
1739 // of line
1740 if (*ptr == '\n' || got_int)
1741 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001742 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001743 line_breakcheck();
1744 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001745 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001746 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001747 }
1748}
1749
1750/*
1751 * Find the start of the next word.
1752 * Returns a pointer to the first char of the word. Also stops at a NUL.
1753 */
1754 char_u *
1755find_word_start(char_u *ptr)
1756{
1757 if (has_mbyte)
1758 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1759 ptr += (*mb_ptr2len)(ptr);
1760 else
1761 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1762 ++ptr;
1763 return ptr;
1764}
1765
1766/*
1767 * Find the end of the word. Assumes it starts inside a word.
1768 * Returns a pointer to just after the word.
1769 */
1770 char_u *
1771find_word_end(char_u *ptr)
1772{
1773 int start_class;
1774
1775 if (has_mbyte)
1776 {
1777 start_class = mb_get_class(ptr);
1778 if (start_class > 1)
1779 while (*ptr != NUL)
1780 {
1781 ptr += (*mb_ptr2len)(ptr);
1782 if (mb_get_class(ptr) != start_class)
1783 break;
1784 }
1785 }
1786 else
1787 while (vim_iswordc(*ptr))
1788 ++ptr;
1789 return ptr;
1790}
1791
1792/*
1793 * Find the end of the line, omitting CR and NL at the end.
1794 * Returns a pointer to just after the line.
1795 */
1796 static char_u *
1797find_line_end(char_u *ptr)
1798{
1799 char_u *s;
1800
1801 s = ptr + STRLEN(ptr);
1802 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1803 --s;
1804 return s;
1805}
1806
1807/*
1808 * Free the list of completions
1809 */
1810 static void
1811ins_compl_free(void)
1812{
1813 compl_T *match;
1814 int i;
1815
John Marriott5e6ea922024-11-23 14:01:57 +01001816 VIM_CLEAR_STRING(compl_pattern);
1817 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001818
1819 if (compl_first_match == NULL)
1820 return;
1821
1822 ins_compl_del_pum();
1823 pum_clear();
1824
1825 compl_curr_match = compl_first_match;
1826 do
1827 {
1828 match = compl_curr_match;
1829 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001830 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001832 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001833 vim_free(match->cp_fname);
1834 for (i = 0; i < CPT_COUNT; ++i)
1835 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001836#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001837 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001838#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001839 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001840 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001841 compl_first_match = compl_curr_match = NULL;
1842 compl_shown_match = NULL;
1843 compl_old_match = NULL;
1844}
1845
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001846/*
1847 * Reset/clear the completion state.
1848 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001849 void
1850ins_compl_clear(void)
1851{
1852 compl_cont_status = 0;
1853 compl_started = FALSE;
1854 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001855 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001856 VIM_CLEAR_STRING(compl_pattern);
1857 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001858 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001859 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001860 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001861#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001862 // clear v:completed_item
1863 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001864#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001865}
1866
1867/*
1868 * Return TRUE when Insert completion is active.
1869 */
1870 int
1871ins_compl_active(void)
1872{
1873 return compl_started;
1874}
1875
1876/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001877 * Return True when wp is the actual completion window
1878 */
1879 int
1880ins_compl_win_active(win_T *wp UNUSED)
1881{
1882 return ins_compl_active()
1883#if defined(FEAT_QUICKFIX)
1884 && (!wp->w_p_pvw
1885# ifdef FEAT_PROP_POPUP
1886 && !(wp->w_popup_flags & POPF_INFO)
1887# endif
1888 )
1889#endif
1890 ;
1891}
1892
1893/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001894 * Selected one of the matches. When FALSE the match was edited or using the
1895 * longest common string.
1896 */
1897 int
1898ins_compl_used_match(void)
1899{
1900 return compl_used_match;
1901}
1902
1903/*
1904 * Initialize get longest common string.
1905 */
1906 void
1907ins_compl_init_get_longest(void)
1908{
1909 compl_get_longest = FALSE;
1910}
1911
1912/*
1913 * Returns TRUE when insert completion is interrupted.
1914 */
1915 int
1916ins_compl_interrupted(void)
1917{
1918 return compl_interrupted;
1919}
1920
1921/*
1922 * Returns TRUE if the <Enter> key selects a match in the completion popup
1923 * menu.
1924 */
1925 int
1926ins_compl_enter_selects(void)
1927{
1928 return compl_enter_selects;
1929}
1930
1931/*
1932 * Return the column where the text starts that is being completed
1933 */
1934 colnr_T
1935ins_compl_col(void)
1936{
1937 return compl_col;
1938}
1939
1940/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001941 * Return the length in bytes of the text being completed
1942 */
1943 int
1944ins_compl_len(void)
1945{
1946 return compl_length;
1947}
1948
1949/*
glepniredd4ac32025-01-29 18:53:51 +01001950 * Return TRUE when preinsert is set otherwise FALSE.
1951 */
1952 static int
1953ins_compl_has_preinsert(void)
1954{
1955 return (get_cot_flags() & (COT_PREINSERT | COT_FUZZY)) == COT_PREINSERT;
1956}
1957
1958/*
1959 * Returns TRUE if the pre-insert effect is valid and the cursor is within
1960 * the `compl_ins_end_col` range.
1961 */
1962 int
1963ins_compl_preinsert_effect(void)
1964{
1965 if (!ins_compl_has_preinsert())
1966 return FALSE;
1967
1968 return curwin->w_cursor.col < compl_ins_end_col;
1969}
1970
1971/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001972 * Delete one character before the cursor and show the subset of the matches
1973 * that match the word that is now before the cursor.
1974 * Returns the character to be used, NUL if the work is done and another char
1975 * to be got from the user.
1976 */
1977 int
1978ins_compl_bs(void)
1979{
1980 char_u *line;
1981 char_u *p;
1982
glepniredd4ac32025-01-29 18:53:51 +01001983 if (ins_compl_preinsert_effect())
1984 ins_compl_delete();
1985
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001986 line = ml_get_curline();
1987 p = line + curwin->w_cursor.col;
1988 MB_PTR_BACK(line, p);
1989
1990 // Stop completion when the whole word was deleted. For Omni completion
1991 // allow the word to be deleted, we won't match everything.
1992 // Respect the 'backspace' option.
1993 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001994 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1995 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1997 - compl_length < 0))
1998 return K_BS;
1999
2000 // Deleted more than what was used to find matches or didn't finish
2001 // finding all matches: need to look for matches all over again.
2002 if (curwin->w_cursor.col <= compl_col + compl_length
2003 || ins_compl_need_restart())
2004 ins_compl_restart();
2005
John Marriott5e6ea922024-11-23 14:01:57 +01002006 VIM_CLEAR_STRING(compl_leader);
2007 compl_leader.length = (size_t)((p - line) - compl_col);
2008 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2009 if (compl_leader.string == NULL)
2010 {
2011 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002012 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002013 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002014
2015 ins_compl_new_leader();
2016 if (compl_shown_match != NULL)
2017 // Make sure current match is not a hidden item.
2018 compl_curr_match = compl_shown_match;
2019 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002020}
2021
2022/*
2023 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2024 * be called.
2025 */
2026 static int
2027ins_compl_need_restart(void)
2028{
2029 // Return TRUE if we didn't complete finding matches or when the
2030 // 'completefunc' returned "always" in the "refresh" dictionary item.
2031 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002032 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002033 && compl_opt_refresh_always);
2034}
2035
2036/*
2037 * Called after changing "compl_leader".
2038 * Show the popup menu with a different set of matches.
2039 * May also search for matches again if the previous search was interrupted.
2040 */
2041 static void
2042ins_compl_new_leader(void)
2043{
2044 ins_compl_del_pum();
2045 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002046 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002047 compl_used_match = FALSE;
2048
2049 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002050 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002051 else
2052 {
2053#ifdef FEAT_SPELL
2054 spell_bad_len = 0; // need to redetect bad word
2055#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002056 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002057 // the popup menu display the changed text before the cursor. Set
2058 // "compl_restarting" to avoid that the first match is inserted.
2059 pum_call_update_screen();
2060#ifdef FEAT_GUI
2061 if (gui.in_use)
2062 {
2063 // Show the cursor after the match, not after the redrawn text.
2064 setcursor();
2065 out_flush_cursor(FALSE, FALSE);
2066 }
2067#endif
2068 compl_restarting = TRUE;
2069 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2070 compl_cont_status = 0;
2071 compl_restarting = FALSE;
2072 }
2073
2074 compl_enter_selects = !compl_used_match;
2075
2076 // Show the popup menu with a different set of matches.
2077 ins_compl_show_pum();
2078
2079 // Don't let Enter select the original text when there is no popup menu.
2080 if (compl_match_array == NULL)
2081 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002082 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2083 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002084}
2085
2086/*
2087 * Return the length of the completion, from the completion start column to
2088 * the cursor column. Making sure it never goes below zero.
2089 */
2090 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002091get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002092{
2093 int off = (int)curwin->w_cursor.col - (int)compl_col;
2094
2095 if (off < 0)
2096 return 0;
2097 return off;
2098}
2099
2100/*
2101 * Append one character to the match leader. May reduce the number of
2102 * matches.
2103 */
2104 void
2105ins_compl_addleader(int c)
2106{
2107 int cc;
2108
glepniredd4ac32025-01-29 18:53:51 +01002109 if (ins_compl_preinsert_effect())
2110 ins_compl_delete();
2111
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002112 if (stop_arrow() == FAIL)
2113 return;
2114 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2115 {
2116 char_u buf[MB_MAXBYTES + 1];
2117
2118 (*mb_char2bytes)(c, buf);
2119 buf[cc] = NUL;
2120 ins_char_bytes(buf, cc);
2121 if (compl_opt_refresh_always)
2122 AppendToRedobuff(buf);
2123 }
2124 else
2125 {
2126 ins_char(c);
2127 if (compl_opt_refresh_always)
2128 AppendCharToRedobuff(c);
2129 }
2130
2131 // If we didn't complete finding matches we must search again.
2132 if (ins_compl_need_restart())
2133 ins_compl_restart();
2134
2135 // When 'always' is set, don't reset compl_leader. While completing,
2136 // cursor doesn't point original position, changing compl_leader would
2137 // break redo.
2138 if (!compl_opt_refresh_always)
2139 {
John Marriott5e6ea922024-11-23 14:01:57 +01002140 VIM_CLEAR_STRING(compl_leader);
2141 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2142 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2143 compl_leader.length);
2144 if (compl_leader.string == NULL)
2145 {
2146 compl_leader.length = 0;
2147 return;
2148 }
2149
2150 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002151 }
2152}
2153
2154/*
2155 * Setup for finding completions again without leaving CTRL-X mode. Used when
2156 * BS or a key was typed while still searching for matches.
2157 */
2158 static void
2159ins_compl_restart(void)
2160{
2161 ins_compl_free();
2162 compl_started = FALSE;
2163 compl_matches = 0;
2164 compl_cont_status = 0;
2165 compl_cont_mode = 0;
2166}
2167
2168/*
2169 * Set the first match, the original text.
2170 */
2171 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002172ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002173{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002174 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002175 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2176 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002177 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002178 {
John Marriott5e6ea922024-11-23 14:01:57 +01002179 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002180 if (p != NULL)
2181 {
John Marriott5e6ea922024-11-23 14:01:57 +01002182 VIM_CLEAR_STRING(compl_first_match->cp_str);
2183 compl_first_match->cp_str.string = p;
2184 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002185 }
2186 }
2187 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002188 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002189 {
John Marriott5e6ea922024-11-23 14:01:57 +01002190 char_u *p = vim_strnsave(str, len);
2191 if (p != NULL)
2192 {
2193 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2194 compl_first_match->cp_prev->cp_str.string = p;
2195 compl_first_match->cp_prev->cp_str.length = len;
2196 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002197 }
2198}
2199
2200/*
2201 * Append one character to the match leader. May reduce the number of
2202 * matches.
2203 */
2204 void
2205ins_compl_addfrommatch(void)
2206{
2207 char_u *p;
2208 int len = (int)curwin->w_cursor.col - (int)compl_col;
2209 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002210
John Marriott5e6ea922024-11-23 14:01:57 +01002211 p = compl_shown_match->cp_str.string;
2212 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002213 {
John Marriott5e6ea922024-11-23 14:01:57 +01002214 size_t plen;
2215 compl_T *cp;
2216
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002217 // When still at the original match use the first entry that matches
2218 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002219 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002220 return;
2221
2222 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002223 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002224 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002225 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002226 {
John Marriott5e6ea922024-11-23 14:01:57 +01002227 if (compl_leader.string == NULL
2228 || ins_compl_equal(cp, compl_leader.string,
2229 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002230 {
John Marriott5e6ea922024-11-23 14:01:57 +01002231 p = cp->cp_str.string;
2232 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002233 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002234 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002235 }
John Marriott5e6ea922024-11-23 14:01:57 +01002236 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002237 return;
2238 }
2239 p += len;
2240 c = PTR2CHAR(p);
2241 ins_compl_addleader(c);
2242}
2243
2244/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002245 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002246 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2247 * compl_cont_mode and compl_cont_status.
2248 * Returns TRUE when the character is not to be inserted.
2249 */
2250 static int
2251set_ctrl_x_mode(int c)
2252{
2253 int retval = FALSE;
2254
2255 switch (c)
2256 {
2257 case Ctrl_E:
2258 case Ctrl_Y:
2259 // scroll the window one line up or down
2260 ctrl_x_mode = CTRL_X_SCROLL;
2261 if (!(State & REPLACE_FLAG))
2262 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2263 else
2264 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2265 edit_submode_pre = NULL;
2266 showmode();
2267 break;
2268 case Ctrl_L:
2269 // complete whole line
2270 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2271 break;
2272 case Ctrl_F:
2273 // complete filenames
2274 ctrl_x_mode = CTRL_X_FILES;
2275 break;
2276 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002277 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002278 ctrl_x_mode = CTRL_X_DICTIONARY;
2279 break;
2280 case Ctrl_R:
2281 // Register insertion without exiting CTRL-X mode
2282 // Simply allow ^R to happen without affecting ^X mode
2283 break;
2284 case Ctrl_T:
2285 // complete words from a thesaurus
2286 ctrl_x_mode = CTRL_X_THESAURUS;
2287 break;
2288#ifdef FEAT_COMPL_FUNC
2289 case Ctrl_U:
2290 // user defined completion
2291 ctrl_x_mode = CTRL_X_FUNCTION;
2292 break;
2293 case Ctrl_O:
2294 // omni completion
2295 ctrl_x_mode = CTRL_X_OMNI;
2296 break;
2297#endif
2298 case 's':
2299 case Ctrl_S:
2300 // complete spelling suggestions
2301 ctrl_x_mode = CTRL_X_SPELL;
2302#ifdef FEAT_SPELL
2303 ++emsg_off; // Avoid getting the E756 error twice.
2304 spell_back_to_badword();
2305 --emsg_off;
2306#endif
2307 break;
2308 case Ctrl_RSB:
2309 // complete tag names
2310 ctrl_x_mode = CTRL_X_TAGS;
2311 break;
2312#ifdef FEAT_FIND_ID
2313 case Ctrl_I:
2314 case K_S_TAB:
2315 // complete keywords from included files
2316 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2317 break;
2318 case Ctrl_D:
2319 // complete definitions from included files
2320 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2321 break;
2322#endif
2323 case Ctrl_V:
2324 case Ctrl_Q:
2325 // complete vim commands
2326 ctrl_x_mode = CTRL_X_CMDLINE;
2327 break;
2328 case Ctrl_Z:
2329 // stop completion
2330 ctrl_x_mode = CTRL_X_NORMAL;
2331 edit_submode = NULL;
2332 showmode();
2333 retval = TRUE;
2334 break;
2335 case Ctrl_P:
2336 case Ctrl_N:
2337 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2338 // just started ^X mode, or there were enough ^X's to cancel
2339 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2340 // do normal expansion when interrupting a different mode (say
2341 // ^X^F^X^P or ^P^X^X^P, see below)
2342 // nothing changes if interrupting mode 0, (eg, the flag
2343 // doesn't change when going to ADDING mode -- Acevedo
2344 if (!(compl_cont_status & CONT_INTRPT))
2345 compl_cont_status |= CONT_LOCAL;
2346 else if (compl_cont_mode != 0)
2347 compl_cont_status &= ~CONT_LOCAL;
2348 // FALLTHROUGH
2349 default:
2350 // If we have typed at least 2 ^X's... for modes != 0, we set
2351 // compl_cont_status = 0 (eg, as if we had just started ^X
2352 // mode).
2353 // For mode 0, we set "compl_cont_mode" to an impossible
2354 // value, in both cases ^X^X can be used to restart the same
2355 // mode (avoiding ADDING mode).
2356 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2357 // 'complete' and local ^P expansions respectively.
2358 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2359 // mode -- Acevedo
2360 if (c == Ctrl_X)
2361 {
2362 if (compl_cont_mode != 0)
2363 compl_cont_status = 0;
2364 else
2365 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2366 }
2367 ctrl_x_mode = CTRL_X_NORMAL;
2368 edit_submode = NULL;
2369 showmode();
2370 break;
2371 }
2372
2373 return retval;
2374}
2375
2376/*
glepnir1c5a1202024-12-04 20:27:34 +01002377 * Trigger CompleteDone event and adds relevant information to v:event
2378 */
2379 static void
2380trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2381{
2382#if defined(FEAT_EVAL)
2383 save_v_event_T save_v_event;
2384 dict_T *v_event = get_v_event(&save_v_event);
2385 char_u *mode_str = NULL;
2386
2387 mode = mode & ~CTRL_X_WANT_IDENT;
2388 if (ctrl_x_mode_names[mode])
2389 mode_str = (char_u *)ctrl_x_mode_names[mode];
2390
2391 (void)dict_add_string(v_event, "complete_word",
2392 word == NULL ? (char_u *)"" : word);
2393 (void)dict_add_string(v_event, "complete_type",
2394 mode_str != NULL ? mode_str : (char_u *)"");
2395
2396 dict_set_items_ro(v_event);
2397#endif
2398 ins_apply_autocmds(EVENT_COMPLETEDONE);
2399
2400#if defined(FEAT_EVAL)
2401 restore_v_event(v_event, &save_v_event);
2402#endif
2403}
2404
2405/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002406 * Stop insert completion mode
2407 */
2408 static int
2409ins_compl_stop(int c, int prev_mode, int retval)
2410{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002411 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002412 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002413
2414 // Get here when we have finished typing a sequence of ^N and
2415 // ^P or other completion characters in CTRL-X mode. Free up
2416 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002417 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002418 {
John Marriott5e6ea922024-11-23 14:01:57 +01002419 char_u *ptr;
2420
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002421 // If any of the original typed text has been changed, eg when
2422 // ignorecase is set, we must add back-spaces to the redo
2423 // buffer. We add as few as necessary to delete just the part
2424 // of the original text that has changed.
2425 // When using the longest match, edited the match or used
2426 // CTRL-E then don't use the current match.
2427 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002428 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002429 else
2430 ptr = NULL;
2431 ins_compl_fixRedoBufForLeader(ptr);
2432 }
2433
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002434 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002435
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002436 // When completing whole lines: fix indent for 'cindent'.
2437 // Otherwise, break line if it's too long.
2438 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2439 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002440 // re-indent the current line
2441 if (want_cindent)
2442 {
2443 do_c_expr_indent();
2444 want_cindent = FALSE; // don't do it again
2445 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002446 }
2447 else
2448 {
2449 int prev_col = curwin->w_cursor.col;
2450
2451 // put the cursor on the last char, for 'tw' formatting
2452 if (prev_col > 0)
2453 dec_cursor();
2454 // only format when something was inserted
2455 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2456 insertchar(NUL, 0, -1);
2457 if (prev_col > 0
2458 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2459 inc_cursor();
2460 }
2461
2462 // If the popup menu is displayed pressing CTRL-Y means accepting
2463 // the selection without inserting anything. When
2464 // compl_enter_selects is set the Enter key does the same.
2465 if ((c == Ctrl_Y || (compl_enter_selects
2466 && (c == CAR || c == K_KENTER || c == NL)))
2467 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002468 {
2469 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002470 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002471 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002472
2473 // CTRL-E means completion is Ended, go back to the typed text.
2474 // but only do this, if the Popup is still visible
2475 if (c == Ctrl_E)
2476 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002477 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002478 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002479
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002480 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002481 if (compl_leader.string != NULL)
2482 {
2483 p = compl_leader.string;
2484 plen = compl_leader.length;
2485 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002486 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002487 {
2488 p = compl_orig_text.string;
2489 plen = compl_orig_text.length;
2490 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002491 if (p != NULL)
2492 {
2493 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002494
John Marriott5e6ea922024-11-23 14:01:57 +01002495 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002496 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002497 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002498 retval = TRUE;
2499 }
2500
glepnir001c26c2025-02-02 09:36:22 +01002501 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2502 ins_compl_delete();
2503
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002504 auto_format(FALSE, TRUE);
2505
2506 // Trigger the CompleteDonePre event to give scripts a chance to
2507 // act upon the completion before clearing the info, and restore
2508 // ctrl_x_mode, so that complete_info() can be used.
2509 ctrl_x_mode = prev_mode;
2510 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2511
2512 ins_compl_free();
2513 compl_started = FALSE;
2514 compl_matches = 0;
2515 if (!shortmess(SHM_COMPLETIONMENU))
2516 msg_clr_cmdline(); // necessary for "noshowmode"
2517 ctrl_x_mode = CTRL_X_NORMAL;
2518 compl_enter_selects = FALSE;
2519 if (edit_submode != NULL)
2520 {
2521 edit_submode = NULL;
2522 showmode();
2523 }
2524
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002525 if (c == Ctrl_C && cmdwin_type != 0)
2526 // Avoid the popup menu remains displayed when leaving the
2527 // command line window.
2528 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002529 // Indent now if a key was typed that is in 'cinkeys'.
2530 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2531 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002532 // Trigger the CompleteDone event to give scripts a chance to act
2533 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002534 trigger_complete_done_event(prev_mode, word);
2535 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002536
2537 return retval;
2538}
2539
2540/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002541 * Prepare for Insert mode completion, or stop it.
2542 * Called just after typing a character in Insert mode.
2543 * Returns TRUE when the character is not to be inserted;
2544 */
2545 int
2546ins_compl_prep(int c)
2547{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002548 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002549 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002550
2551 // Forget any previous 'special' messages if this is actually
2552 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2553 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2554 edit_submode_extra = NULL;
2555
zeertzjq440d4cb2023-03-02 17:51:32 +00002556 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002557 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002558 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002559 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002560 return retval;
2561
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002562#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002563 // Ignore mouse events in a popup window
2564 if (is_mouse_key(c))
2565 {
2566 // Ignore drag and release events, the position does not need to be in
2567 // the popup and it may have just closed.
2568 if (c == K_LEFTRELEASE
2569 || c == K_LEFTRELEASE_NM
2570 || c == K_MIDDLERELEASE
2571 || c == K_RIGHTRELEASE
2572 || c == K_X1RELEASE
2573 || c == K_X2RELEASE
2574 || c == K_LEFTDRAG
2575 || c == K_MIDDLEDRAG
2576 || c == K_RIGHTDRAG
2577 || c == K_X1DRAG
2578 || c == K_X2DRAG)
2579 return retval;
2580 if (popup_visible)
2581 {
2582 int row = mouse_row;
2583 int col = mouse_col;
2584 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2585
2586 if (wp != NULL && WIN_IS_POPUP(wp))
2587 return retval;
2588 }
2589 }
2590#endif
2591
zeertzjqdca29d92021-08-31 19:12:51 +02002592 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2593 {
2594 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2595 || !vim_is_ctrl_x_key(c))
2596 {
2597 // Not starting another completion mode.
2598 ctrl_x_mode = CTRL_X_CMDLINE;
2599
2600 // CTRL-X CTRL-Z should stop completion without inserting anything
2601 if (c == Ctrl_Z)
2602 retval = TRUE;
2603 }
2604 else
2605 {
2606 ctrl_x_mode = CTRL_X_CMDLINE;
2607
2608 // Other CTRL-X keys first stop completion, then start another
2609 // completion mode.
2610 ins_compl_prep(' ');
2611 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2612 }
2613 }
2614
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002615 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002616 if (ctrl_x_mode_not_defined_yet()
2617 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002618 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002619 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002621 }
2622
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002623 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002624 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2625 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002626 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002627 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002628 {
2629 // We're already in CTRL-X mode, do we stay in it?
2630 if (!vim_is_ctrl_x_key(c))
2631 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002632 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002633 ctrl_x_mode = CTRL_X_NORMAL;
2634 else
2635 ctrl_x_mode = CTRL_X_FINISHED;
2636 edit_submode = NULL;
2637 }
2638 showmode();
2639 }
2640
2641 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2642 {
2643 // Show error message from attempted keyword completion (probably
2644 // 'Pattern not found') until another key is hit, then go back to
2645 // showing what mode we are in.
2646 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002647 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002648 && c != Ctrl_R && !ins_compl_pum_key(c))
2649 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002650 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002651 }
2652 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2653 // Trigger the CompleteDone event to give scripts a chance to act
2654 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002655 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002656
LemonBoy2bf52dd2022-04-09 18:17:34 +01002657 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002658
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002659 // reset continue_* if we left expansion-mode, if we stay they'll be
2660 // (re)set properly in ins_complete()
2661 if (!vim_is_ctrl_x_key(c))
2662 {
2663 compl_cont_status = 0;
2664 compl_cont_mode = 0;
2665 }
2666
2667 return retval;
2668}
2669
2670/*
2671 * Fix the redo buffer for the completion leader replacing some of the typed
2672 * text. This inserts backspaces and appends the changed text.
2673 * "ptr" is the known leader text or NUL.
2674 */
2675 static void
2676ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2677{
John Marriott5e6ea922024-11-23 14:01:57 +01002678 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679 char_u *p;
2680 char_u *ptr = ptr_arg;
2681
2682 if (ptr == NULL)
2683 {
John Marriott5e6ea922024-11-23 14:01:57 +01002684 if (compl_leader.string != NULL)
2685 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002686 else
2687 return; // nothing to do
2688 }
John Marriott5e6ea922024-11-23 14:01:57 +01002689 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002690 {
John Marriott5e6ea922024-11-23 14:01:57 +01002691 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002692 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2693 ;
2694 if (len > 0)
2695 len -= (*mb_head_off)(p, p + len);
2696 for (p += len; *p != NUL; MB_PTR_ADV(p))
2697 AppendCharToRedobuff(K_BS);
2698 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002699 if (ptr != NULL)
2700 AppendToRedobuffLit(ptr + len, -1);
2701}
2702
2703/*
2704 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2705 * (depending on flag) starting from buf and looking for a non-scanned
2706 * buffer (other than curbuf). curbuf is special, if it is called with
2707 * buf=curbuf then it has to be the first call for a given flag/expansion.
2708 *
2709 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2710 */
2711 static buf_T *
2712ins_compl_next_buf(buf_T *buf, int flag)
2713{
2714 static win_T *wp = NULL;
2715
2716 if (flag == 'w') // just windows
2717 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002718 if (buf == curbuf || !win_valid(wp))
2719 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720 wp = curwin;
2721 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2722 && wp->w_buffer->b_scanned)
2723 ;
2724 buf = wp->w_buffer;
2725 }
2726 else
2727 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2728 // (unlisted buffers)
2729 // When completing whole lines skip unloaded buffers.
2730 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2731 && ((flag == 'U'
2732 ? buf->b_p_bl
2733 : (!buf->b_p_bl
2734 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2735 || buf->b_scanned))
2736 ;
2737 return buf;
2738}
2739
2740#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002741
2742# ifdef FEAT_EVAL
2743static callback_T cfu_cb; // 'completefunc' callback function
2744static callback_T ofu_cb; // 'omnifunc' callback function
2745static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2746# endif
2747
2748/*
2749 * Copy a global callback function to a buffer local callback.
2750 */
2751 static void
2752copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2753{
2754 free_callback(bufcb);
2755 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2756 copy_callback(bufcb, globcb);
2757}
2758
2759/*
2760 * Parse the 'completefunc' option value and set the callback function.
2761 * Invoked when the 'completefunc' option is set. The option value can be a
2762 * name of a function (string), or function(<name>) or funcref(<name>) or a
2763 * lambda expression.
2764 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002765 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002766did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002767{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002768 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2769 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002770
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002771 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002772
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002773 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002774}
2775
2776/*
2777 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002778 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002779 */
2780 void
2781set_buflocal_cfu_callback(buf_T *buf UNUSED)
2782{
2783# ifdef FEAT_EVAL
2784 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2785# endif
2786}
2787
2788/*
2789 * Parse the 'omnifunc' option value and set the callback function.
2790 * Invoked when the 'omnifunc' option is set. The option value can be a
2791 * name of a function (string), or function(<name>) or funcref(<name>) or a
2792 * lambda expression.
2793 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002794 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002795did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002796{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002797 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2798 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002799
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002800 set_buflocal_ofu_callback(curbuf);
2801 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002802}
2803
2804/*
2805 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002806 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002807 */
2808 void
2809set_buflocal_ofu_callback(buf_T *buf UNUSED)
2810{
2811# ifdef FEAT_EVAL
2812 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2813# endif
2814}
2815
2816/*
2817 * Parse the 'thesaurusfunc' option value and set the callback function.
2818 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2819 * name of a function (string), or function(<name>) or funcref(<name>) or a
2820 * lambda expression.
2821 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002823did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002824{
2825 int retval;
2826
zeertzjq6eda2692024-11-03 09:23:33 +01002827 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002828 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002829 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2830 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002831 else
2832 {
2833 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002834 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002835 // when using :set, free the local callback
2836 if (!(args->os_flags & OPT_GLOBAL))
2837 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002838 }
2839
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002840 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002841}
2842
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002843/*
2844 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002845 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002846 */
2847 int
2848set_ref_in_insexpand_funcs(int copyID)
2849{
2850 int abort = FALSE;
2851
2852 abort = set_ref_in_callback(&cfu_cb, copyID);
2853 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2854 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2855
2856 return abort;
2857}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002858
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002859/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002860 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002861 */
2862 static char_u *
2863get_complete_funcname(int type)
2864{
2865 switch (type)
2866 {
2867 case CTRL_X_FUNCTION:
2868 return curbuf->b_p_cfu;
2869 case CTRL_X_OMNI:
2870 return curbuf->b_p_ofu;
2871 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002872 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002873 default:
2874 return (char_u *)"";
2875 }
2876}
2877
2878/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002879 * Get the callback to use for insert mode completion.
2880 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002881 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002882get_insert_callback(int type)
2883{
2884 if (type == CTRL_X_FUNCTION)
2885 return &curbuf->b_cfu_cb;
2886 if (type == CTRL_X_OMNI)
2887 return &curbuf->b_ofu_cb;
2888 // CTRL_X_THESAURUS
2889 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2890}
2891
2892/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002893 * Execute user defined complete function 'completefunc', 'omnifunc' or
2894 * 'thesaurusfunc', and get matches in "matches".
2895 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002896 */
2897 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002898expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002899{
2900 list_T *matchlist = NULL;
2901 dict_T *matchdict = NULL;
2902 typval_T args[3];
2903 char_u *funcname;
2904 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002905 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002906 typval_T rettv;
2907 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002908 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002909
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002910 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002911 if (*funcname == NUL)
2912 return;
2913
2914 // Call 'completefunc' to obtain the list of matches.
2915 args[0].v_type = VAR_NUMBER;
2916 args[0].vval.v_number = 0;
2917 args[1].v_type = VAR_STRING;
2918 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2919 args[2].v_type = VAR_UNKNOWN;
2920
2921 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002922 // Lock the text to avoid weird things from happening. Also disallow
2923 // switching to another window, it should not be needed and may end up in
2924 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002925 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002926
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002927 cb = get_insert_callback(type);
2928 retval = call_callback(cb, 0, &rettv, 2, args);
2929
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002930 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002931 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002932 {
2933 switch (rettv.v_type)
2934 {
2935 case VAR_LIST:
2936 matchlist = rettv.vval.v_list;
2937 break;
2938 case VAR_DICT:
2939 matchdict = rettv.vval.v_dict;
2940 break;
2941 case VAR_SPECIAL:
2942 if (rettv.vval.v_number == VVAL_NONE)
2943 compl_opt_suppress_empty = TRUE;
2944 // FALLTHROUGH
2945 default:
2946 // TODO: Give error message?
2947 clear_tv(&rettv);
2948 break;
2949 }
2950 }
zeertzjqcfe45652022-05-27 17:26:55 +01002951 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002952
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002953 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002954 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002955 validate_cursor();
2956 if (!EQUAL_POS(curwin->w_cursor, pos))
2957 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002958 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002959 goto theend;
2960 }
2961
2962 if (matchlist != NULL)
2963 ins_compl_add_list(matchlist);
2964 else if (matchdict != NULL)
2965 ins_compl_add_dict(matchdict);
2966
2967theend:
2968 // Restore State, it might have been changed.
2969 State = save_State;
2970
2971 if (matchdict != NULL)
2972 dict_unref(matchdict);
2973 if (matchlist != NULL)
2974 list_unref(matchlist);
2975}
2976#endif // FEAT_COMPL_FUNC
2977
2978#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002979
2980 static inline int
2981get_user_highlight_attr(char_u *hlname)
2982{
2983 if (hlname != NULL && *hlname != NUL)
2984 return syn_name2attr(hlname);
2985 return -1;
2986}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002987/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002988 * Add a match to the list of matches from a typeval_T.
2989 * If the given string is already in the list of completions, then return
2990 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2991 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002992 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002993 */
2994 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002995ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002996{
2997 char_u *word;
2998 int dup = FALSE;
2999 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003000 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003001 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003002 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003003 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003004 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003005 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003006 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003007
Bram Moolenaar08928322020-01-04 14:32:48 +01003008 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003009 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3010 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003011 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3012 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3013 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3014 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3015 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003016
glepnir0fe17f82024-10-08 22:26:44 +02003017 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003018 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003019
3020 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003021 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003022
Bram Moolenaard61efa52022-07-23 09:52:04 +01003023 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3024 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3025 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003026 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003027 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3028 dup = dict_get_number(tv->vval.v_dict, "dup");
3029 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3030 empty = dict_get_number(tv->vval.v_dict, "empty");
3031 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3032 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003033 flags |= CP_EQUAL;
3034 }
3035 else
3036 {
3037 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003038 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003039 }
3040 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003041 {
3042 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003043 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003044 }
glepnir508e7852024-07-25 21:39:08 +02003045 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003046 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003047 if (status != OK)
3048 clear_tv(&user_data);
3049 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003050}
3051
3052/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003053 * Add completions from a list.
3054 */
3055 static void
3056ins_compl_add_list(list_T *list)
3057{
3058 listitem_T *li;
3059 int dir = compl_direction;
3060
3061 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003062 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003063 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003064 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003065 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003066 // if dir was BACKWARD then honor it just once
3067 dir = FORWARD;
3068 else if (did_emsg)
3069 break;
3070 }
3071}
3072
3073/*
3074 * Add completions from a dict.
3075 */
3076 static void
3077ins_compl_add_dict(dict_T *dict)
3078{
3079 dictitem_T *di_refresh;
3080 dictitem_T *di_words;
3081
3082 // Check for optional "refresh" item.
3083 compl_opt_refresh_always = FALSE;
3084 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3085 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3086 {
3087 char_u *v = di_refresh->di_tv.vval.v_string;
3088
3089 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3090 compl_opt_refresh_always = TRUE;
3091 }
3092
3093 // Add completions from a "words" list.
3094 di_words = dict_find(dict, (char_u *)"words", 5);
3095 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3096 ins_compl_add_list(di_words->di_tv.vval.v_list);
3097}
3098
3099/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003100 * Start completion for the complete() function.
3101 * "startcol" is where the matched text starts (1 is first column).
3102 * "list" is the list of matches.
3103 */
3104 static void
3105set_completion(colnr_T startcol, list_T *list)
3106{
3107 int save_w_wrow = curwin->w_wrow;
3108 int save_w_leftcol = curwin->w_leftcol;
3109 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003110 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003111 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3112 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3113 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003114
3115 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003116 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117 ins_compl_prep(' ');
3118 ins_compl_clear();
3119 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003120 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003121
3122 compl_direction = FORWARD;
3123 if (startcol > curwin->w_cursor.col)
3124 startcol = curwin->w_cursor.col;
3125 compl_col = startcol;
3126 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3127 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003128 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3129 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003130 if (p_ic)
3131 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003132 if (compl_orig_text.string == NULL)
3133 {
3134 compl_orig_text.length = 0;
3135 return;
3136 }
3137 compl_orig_text.length = (size_t)compl_length;
3138 if (ins_compl_add(compl_orig_text.string,
3139 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3140 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003141 return;
3142
3143 ctrl_x_mode = CTRL_X_EVAL;
3144
3145 ins_compl_add_list(list);
3146 compl_matches = ins_compl_make_cyclic();
3147 compl_started = TRUE;
3148 compl_used_match = TRUE;
3149 compl_cont_status = 0;
3150
3151 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003152 int no_select = compl_no_select || compl_longest;
3153 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003154 {
3155 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003156 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003157 // Down/Up has no real effect.
3158 ins_complete(K_UP, FALSE);
3159 }
3160 else
3161 ins_complete(Ctrl_N, FALSE);
3162 compl_enter_selects = compl_no_insert;
3163
3164 // Lazily show the popup menu, unless we got interrupted.
3165 if (!compl_interrupted)
3166 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003167 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003168 out_flush();
3169}
3170
3171/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003172 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003173 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003174 void
3175f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003176{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003177 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003178
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003179 if (in_vim9script()
3180 && (check_for_number_arg(argvars, 0) == FAIL
3181 || check_for_list_arg(argvars, 1) == FAIL))
3182 return;
3183
Bram Moolenaar24959102022-05-07 20:01:16 +01003184 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003185 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003186 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003187 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003188 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003189
3190 // Check for undo allowed here, because if something was already inserted
3191 // the line was already saved for undo and this check isn't done.
3192 if (!undo_allowed())
3193 return;
3194
Bram Moolenaard83392a2022-09-01 12:22:46 +01003195 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003196 {
3197 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3198 if (startcol > 0)
3199 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003200 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003201}
3202
3203/*
3204 * "complete_add()" function
3205 */
3206 void
3207f_complete_add(typval_T *argvars, typval_T *rettv)
3208{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003209 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003210 return;
3211
Bram Moolenaar440cf092021-04-03 20:13:30 +02003212 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003213}
3214
3215/*
3216 * "complete_check()" function
3217 */
3218 void
3219f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3220{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003221 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003222 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003223
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003224 ins_compl_check_keys(0, TRUE);
3225 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003226
3227 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003228}
3229
3230/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003231 * Return Insert completion mode name string
3232 */
3233 static char_u *
3234ins_compl_mode(void)
3235{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003236 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003237 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3238
3239 return (char_u *)"";
3240}
3241
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003242/*
3243 * Assign the sequence number to all the completion matches which don't have
3244 * one assigned yet.
3245 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003246 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003247ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003248{
3249 int number = 0;
3250 compl_T *match;
3251
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003252 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003253 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003254 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003255 // This should normally succeed already at the first loop
3256 // cycle, so it's fast!
3257 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003258 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003259 if (match->cp_number != -1)
3260 {
3261 number = match->cp_number;
3262 break;
3263 }
3264 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003265 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003266 for (match = match->cp_next;
3267 match != NULL && match->cp_number == -1;
3268 match = match->cp_next)
3269 match->cp_number = ++number;
3270 }
3271 else // BACKWARD
3272 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003273 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003274 // number. This should normally succeed already at the
3275 // first loop cycle, so it's fast!
3276 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003277 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003278 if (match->cp_number != -1)
3279 {
3280 number = match->cp_number;
3281 break;
3282 }
3283 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003284 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003285 for (match = match->cp_prev; match
3286 && match->cp_number == -1;
3287 match = match->cp_prev)
3288 match->cp_number = ++number;
3289 }
3290}
3291
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003292/*
glepnir037b0282025-01-16 14:37:44 +01003293 * Fill the dict of complete_info
3294 */
3295 static void
3296fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3297{
3298 dict_add_string(di, "word", match->cp_str.string);
3299 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3300 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3301 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3302 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3303 if (add_match)
3304 dict_add_bool(di, "match", match->cp_in_match_array);
3305 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3306 // Add an empty string for backwards compatibility
3307 dict_add_string(di, "user_data", (char_u *)"");
3308 else
3309 dict_add_tv(di, "user_data", &match->cp_user_data);
3310}
3311
3312/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003313 * Get complete information
3314 */
3315 static void
3316get_complete_info(list_T *what_list, dict_T *retdict)
3317{
3318 int ret = OK;
3319 listitem_T *item;
3320#define CI_WHAT_MODE 0x01
3321#define CI_WHAT_PUM_VISIBLE 0x02
3322#define CI_WHAT_ITEMS 0x04
3323#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003324#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003325#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003326#define CI_WHAT_ALL 0xff
3327 int what_flag;
3328
3329 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003330 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003331 else
3332 {
3333 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003334 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003335 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003336 {
3337 char_u *what = tv_get_string(&item->li_tv);
3338
3339 if (STRCMP(what, "mode") == 0)
3340 what_flag |= CI_WHAT_MODE;
3341 else if (STRCMP(what, "pum_visible") == 0)
3342 what_flag |= CI_WHAT_PUM_VISIBLE;
3343 else if (STRCMP(what, "items") == 0)
3344 what_flag |= CI_WHAT_ITEMS;
3345 else if (STRCMP(what, "selected") == 0)
3346 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003347 else if (STRCMP(what, "completed") == 0)
3348 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003349 else if (STRCMP(what, "matches") == 0)
3350 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003351 }
3352 }
3353
3354 if (ret == OK && (what_flag & CI_WHAT_MODE))
3355 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3356
3357 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3358 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3359
glepnir037b0282025-01-16 14:37:44 +01003360 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3361 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003362 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003363 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003364 dict_T *di;
3365 compl_T *match;
3366 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003367 int has_items = what_flag & CI_WHAT_ITEMS;
3368 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003369 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003370
glepnird4088ed2024-12-31 10:55:22 +01003371 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003372 {
3373 li = list_alloc();
3374 if (li == NULL)
3375 return;
glepnird4088ed2024-12-31 10:55:22 +01003376 ret = dict_add_list(retdict, (has_matches && !has_items)
3377 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003378 }
3379 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3380 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3381 ins_compl_update_sequence_numbers();
3382 if (ret == OK && compl_first_match != NULL)
3383 {
3384 int list_idx = 0;
3385 match = compl_first_match;
3386 do
3387 {
3388 if (!match_at_original_text(match))
3389 {
glepnird4088ed2024-12-31 10:55:22 +01003390 if (has_items
3391 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003392 {
3393 di = dict_alloc();
3394 if (di == NULL)
3395 return;
3396 ret = list_append_dict(li, di);
3397 if (ret != OK)
3398 return;
glepnir037b0282025-01-16 14:37:44 +01003399 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003400 }
glepnird4088ed2024-12-31 10:55:22 +01003401 if (compl_curr_match != NULL
3402 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003403 selected_idx = list_idx;
3404 list_idx += 1;
3405 }
3406 match = match->cp_next;
3407 }
3408 while (match != NULL && !is_first_match(match));
3409 }
3410 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3411 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003412
glepnir037b0282025-01-16 14:37:44 +01003413 if (ret == OK && selected_idx != -1 && has_completed)
3414 {
3415 di = dict_alloc();
3416 if (di == NULL)
3417 return;
3418 fill_complete_info_dict(di, compl_curr_match, FALSE);
3419 ret = dict_add_dict(retdict, "completed", di);
3420 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003421 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003422}
3423
3424/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003425 * "complete_info()" function
3426 */
3427 void
3428f_complete_info(typval_T *argvars, typval_T *rettv)
3429{
3430 list_T *what_list = NULL;
3431
Bram Moolenaar93a10962022-06-16 11:42:09 +01003432 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003433 return;
3434
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003435 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3436 return;
3437
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003438 if (argvars[0].v_type != VAR_UNKNOWN)
3439 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003440 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003441 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003442 what_list = argvars[0].vval.v_list;
3443 }
3444 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003445}
3446#endif
3447
3448/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003449 * Returns TRUE when using a user-defined function for thesaurus completion.
3450 */
3451 static int
3452thesaurus_func_complete(int type UNUSED)
3453{
3454#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003455 return type == CTRL_X_THESAURUS
3456 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003457#else
3458 return FALSE;
3459#endif
3460}
3461
3462/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003463 * Return value of process_next_cpt_value()
3464 */
3465enum
3466{
3467 INS_COMPL_CPT_OK = 1,
3468 INS_COMPL_CPT_CONT,
3469 INS_COMPL_CPT_END
3470};
3471
3472/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003473 * state information used for getting the next set of insert completion
3474 * matches.
3475 */
3476typedef struct
3477{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003478 char_u *e_cpt_copy; // copy of 'complete'
3479 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003480 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003481 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003482 pos_T prev_match_pos; // previous match position
3483 int set_match_pos; // save first_match_pos/last_match_pos
3484 pos_T first_match_pos; // first match position
3485 pos_T last_match_pos; // last match position
3486 int found_all; // found all matches of a certain type.
3487 char_u *dict; // dictionary file to search
3488 int dict_f; // "dict" is an exact file name or not
3489} ins_compl_next_state_T;
3490
3491/*
3492 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003493 *
3494 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003495 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003496 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003497 * st->found_all - all matches of this type are found
3498 * st->ins_buf - search for completions in this buffer
3499 * st->first_match_pos - position of the first completion match
3500 * st->last_match_pos - position of the last completion match
3501 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003502 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003503 * st->dict - name of the dictionary or thesaurus file to search
3504 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003505 *
3506 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003507 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3508 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003509 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003510 */
3511 static int
3512process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003513 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003514 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003515 pos_T *start_match_pos,
3516 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003517{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003518 int compl_type = -1;
3519 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003520
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003521 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003522
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003523 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3524 st->e_cpt++;
3525
3526 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003527 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003528 st->ins_buf = curbuf;
3529 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003530 // Move the cursor back one character so that ^N can match the
3531 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003532 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003533 {
3534 // Move the cursor to after the last character in the
3535 // buffer, so that word at start of buffer is found
3536 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003537 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003538 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003539 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003540 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003541 compl_type = 0;
3542
3543 // Remember the first match so that the loop stops when we
3544 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003545 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003546 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003547 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003548 && (st->ins_buf = ins_compl_next_buf(
3549 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003550 {
3551 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003552 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003553 {
3554 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003555 st->first_match_pos.col = st->last_match_pos.col = 0;
3556 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3557 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003558 compl_type = 0;
3559 }
3560 else // unloaded buffer, scan like dictionary
3561 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003562 st->found_all = TRUE;
3563 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003564 {
3565 status = INS_COMPL_CPT_CONT;
3566 goto done;
3567 }
3568 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003569 st->dict = st->ins_buf->b_fname;
3570 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003571 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003572 if (!shortmess(SHM_COMPLETIONSCAN))
3573 {
3574 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3575 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3576 st->ins_buf->b_fname == NULL
3577 ? buf_spname(st->ins_buf)
3578 : st->ins_buf->b_sfname == NULL
3579 ? st->ins_buf->b_fname
3580 : st->ins_buf->b_sfname);
3581 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3582 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003583 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003584 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003585 status = INS_COMPL_CPT_END;
3586 else
3587 {
3588 if (ctrl_x_mode_line_or_eval())
3589 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003590 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003591 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003592 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 compl_type = CTRL_X_DICTIONARY;
3594 else
3595 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003596 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003597 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003598 st->dict = st->e_cpt;
3599 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003600 }
3601 }
3602#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003603 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003604 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 compl_type = CTRL_X_PATH_DEFINES;
3607#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003608 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003609 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003611 if (!shortmess(SHM_COMPLETIONSCAN))
3612 {
3613 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3614 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3615 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3616 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003617 }
3618 else
3619 compl_type = -1;
3620
3621 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003622 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003623
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003624 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003625 if (compl_type == -1)
3626 status = INS_COMPL_CPT_CONT;
3627 }
3628
3629done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003631 return status;
3632}
3633
3634#ifdef FEAT_FIND_ID
3635/*
3636 * Get the next set of identifiers or defines matching "compl_pattern" in
3637 * included files.
3638 */
3639 static void
3640get_next_include_file_completion(int compl_type)
3641{
John Marriott5e6ea922024-11-23 14:01:57 +01003642 find_pattern_in_path(compl_pattern.string, compl_direction,
3643 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003644 (compl_type == CTRL_X_PATH_DEFINES
3645 && !(compl_cont_status & CONT_SOL))
3646 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003647 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003648}
3649#endif
3650
3651/*
3652 * Get the next set of words matching "compl_pattern" in dictionary or
3653 * thesaurus files.
3654 */
3655 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003656get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003657{
3658#ifdef FEAT_COMPL_FUNC
3659 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003660 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003661 else
3662#endif
3663 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003664 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 : (compl_type == CTRL_X_THESAURUS
3666 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3667 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003668 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003669 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003670 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003671}
3672
3673/*
3674 * Get the next set of tag names matching "compl_pattern".
3675 */
3676 static void
3677get_next_tag_completion(void)
3678{
3679 int save_p_ic;
3680 char_u **matches;
3681 int num_matches;
3682
3683 // set p_ic according to p_ic, p_scs and pat for find_tags().
3684 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003685 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003686
3687 // Find up to TAG_MANY matches. Avoids that an enormous number
3688 // of matches is found when compl_pattern is empty
3689 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003690 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003692 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003693 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3694 ins_compl_add_matches(num_matches, matches, p_ic);
3695 g_tag_at_cursor = FALSE;
3696 p_ic = save_p_ic;
3697}
3698
3699/*
glepnir8159fb12024-07-17 20:32:54 +02003700 * Compare function for qsort
3701 */
3702static int compare_scores(const void *a, const void *b)
3703{
3704 int idx_a = *(const int *)a;
3705 int idx_b = *(const int *)b;
3706 int score_a = compl_fuzzy_scores[idx_a];
3707 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003708 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3709 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003710}
3711
3712/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003713 * Get the next set of filename matching "compl_pattern".
3714 */
3715 static void
3716get_next_filename_completion(void)
3717{
3718 char_u **matches;
3719 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003720 char_u *ptr;
3721 garray_T fuzzy_indices;
3722 int i;
3723 int score;
3724 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003725 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003726 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3727 char_u **sorted_matches;
3728 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003729 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003730
glepnirb9de1a02024-08-02 19:14:38 +02003731#ifdef BACKSLASH_IN_FILENAME
3732 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3733 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3734#else
3735 char pathsep = PATHSEP;
3736#endif
3737
glepnir8159fb12024-07-17 20:32:54 +02003738 if (in_fuzzy)
3739 {
glepnirb9de1a02024-08-02 19:14:38 +02003740#ifdef BACKSLASH_IN_FILENAME
3741 if (curbuf->b_p_csl[0] == 's')
3742 {
John Marriott5e6ea922024-11-23 14:01:57 +01003743 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003744 {
3745 if (leader[i] == '\\')
3746 leader[i] = '/';
3747 }
3748 }
3749 else if (curbuf->b_p_csl[0] == 'b')
3750 {
John Marriott5e6ea922024-11-23 14:01:57 +01003751 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003752 {
3753 if (leader[i] == '/')
3754 leader[i] = '\\';
3755 }
3756 }
3757#endif
3758 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003759 if (last_sep == NULL)
3760 {
3761 // No path separator or separator is the last character,
3762 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003763 VIM_CLEAR_STRING(compl_pattern);
3764 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3765 if (compl_pattern.string == NULL)
3766 return;
3767 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003768 }
3769 else if (*(last_sep + 1) == '\0')
3770 in_fuzzy = FALSE;
3771 else
3772 {
3773 // Split leader into path and file parts
3774 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003775 char_u *path_with_wildcard;
3776
3777 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003778 if (path_with_wildcard != NULL)
3779 {
John Marriott5e6ea922024-11-23 14:01:57 +01003780 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3781 VIM_CLEAR_STRING(compl_pattern);
3782 compl_pattern.string = path_with_wildcard;
3783 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003784
3785 // Move leader to the file part
3786 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003787 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003788 }
3789 }
glepnir8159fb12024-07-17 20:32:54 +02003790 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003791
John Marriott5e6ea922024-11-23 14:01:57 +01003792 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003793 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3794 return;
3795
3796 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003797 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003798#ifdef BACKSLASH_IN_FILENAME
3799 if (curbuf->b_p_csl[0] != NUL)
3800 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003801 for (i = 0; i < num_matches; ++i)
3802 {
glepnir8159fb12024-07-17 20:32:54 +02003803 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003804 while (*ptr != NUL)
3805 {
3806 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3807 *ptr = '/';
3808 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3809 *ptr = '\\';
3810 ptr += (*mb_ptr2len)(ptr);
3811 }
3812 }
3813 }
3814#endif
glepnir8159fb12024-07-17 20:32:54 +02003815
3816 if (in_fuzzy)
3817 {
3818 ga_init2(&fuzzy_indices, sizeof(int), 10);
3819 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3820
3821 for (i = 0; i < num_matches; i++)
3822 {
3823 ptr = matches[i];
3824 score = fuzzy_match_str(ptr, leader);
3825 if (score > 0)
3826 {
3827 if (ga_grow(&fuzzy_indices, 1) == OK)
3828 {
3829 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3830 compl_fuzzy_scores[i] = score;
3831 fuzzy_indices.ga_len++;
3832 }
3833 }
3834 }
3835
Christian Brabandt220474d2024-07-20 13:26:44 +02003836 // prevent qsort from deref NULL pointer
3837 if (fuzzy_indices.ga_len > 0)
3838 {
3839 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3840 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003841
Christian Brabandt220474d2024-07-20 13:26:44 +02003842 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3843 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3844 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003845
Christian Brabandt220474d2024-07-20 13:26:44 +02003846 FreeWild(num_matches, matches);
3847 matches = sorted_matches;
3848 num_matches = fuzzy_indices.ga_len;
3849 }
glepnir6b6280c2024-07-27 16:25:45 +02003850 else if (leader_len > 0)
3851 {
3852 FreeWild(num_matches, matches);
3853 num_matches = 0;
3854 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003855
glepnir8159fb12024-07-17 20:32:54 +02003856 vim_free(compl_fuzzy_scores);
3857 ga_clear(&fuzzy_indices);
3858 }
3859
glepnir6b6280c2024-07-27 16:25:45 +02003860 if (num_matches > 0)
3861 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003862}
3863
3864/*
3865 * Get the next set of command-line completions matching "compl_pattern".
3866 */
3867 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003868get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003869{
3870 char_u **matches;
3871 int num_matches;
3872
John Marriott5e6ea922024-11-23 14:01:57 +01003873 if (expand_cmdline(&compl_xp, compl_pattern.string,
3874 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003875 ins_compl_add_matches(num_matches, matches, FALSE);
3876}
3877
3878/*
3879 * Get the next set of spell suggestions matching "compl_pattern".
3880 */
3881 static void
3882get_next_spell_completion(linenr_T lnum UNUSED)
3883{
3884#ifdef FEAT_SPELL
3885 char_u **matches;
3886 int num_matches;
3887
John Marriott5e6ea922024-11-23 14:01:57 +01003888 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003889 if (num_matches > 0)
3890 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003891 else
3892 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003893#endif
3894}
3895
3896/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003897 * Return the next word or line from buffer "ins_buf" at position
3898 * "cur_match_pos" for completion. The length of the match is set in "len".
3899 */
3900 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003901ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003902 buf_T *ins_buf, // buffer being scanned
3903 pos_T *cur_match_pos, // current match position
3904 int *match_len,
3905 int *cont_s_ipos) // next ^X<> will set initial_pos
3906{
3907 char_u *ptr;
3908 int len;
3909
3910 *match_len = 0;
3911 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3912 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003913 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003914 if (ctrl_x_mode_line_or_eval())
3915 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003916 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003917 {
3918 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3919 return NULL;
3920 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003921 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003922 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003923 {
3924 char_u *tmp_ptr = ptr;
3925
3926 ptr = skipwhite(tmp_ptr);
3927 len -= (int)(ptr - tmp_ptr);
3928 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003929 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003930 }
3931 else
3932 {
3933 char_u *tmp_ptr = ptr;
3934
John Marriott5e6ea922024-11-23 14:01:57 +01003935 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003936 {
3937 tmp_ptr += compl_length;
3938 // Skip if already inside a word.
3939 if (vim_iswordp(tmp_ptr))
3940 return NULL;
3941 // Find start of next word.
3942 tmp_ptr = find_word_start(tmp_ptr);
3943 }
3944 // Find end of this word.
3945 tmp_ptr = find_word_end(tmp_ptr);
3946 len = (int)(tmp_ptr - ptr);
3947
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003948 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003949 {
3950 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3951 {
3952 // Try next line, if any. the new word will be
3953 // "join" as if the normal command "J" was used.
3954 // IOSIZE is always greater than
3955 // compl_length, so the next STRNCPY always
3956 // works -- Acevedo
3957 STRNCPY(IObuff, ptr, len);
3958 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3959 tmp_ptr = ptr = skipwhite(ptr);
3960 // Find start of next word.
3961 tmp_ptr = find_word_start(tmp_ptr);
3962 // Find end of next word.
3963 tmp_ptr = find_word_end(tmp_ptr);
3964 if (tmp_ptr > ptr)
3965 {
3966 if (*ptr != ')' && IObuff[len - 1] != TAB)
3967 {
3968 if (IObuff[len - 1] != ' ')
3969 IObuff[len++] = ' ';
3970 // IObuf =~ "\k.* ", thus len >= 2
3971 if (p_js
3972 && (IObuff[len - 2] == '.'
3973 || (vim_strchr(p_cpo, CPO_JOINSP)
3974 == NULL
3975 && (IObuff[len - 2] == '?'
3976 || IObuff[len - 2] == '!'))))
3977 IObuff[len++] = ' ';
3978 }
3979 // copy as much as possible of the new word
3980 if (tmp_ptr - ptr >= IOSIZE - len)
3981 tmp_ptr = ptr + IOSIZE - len - 1;
3982 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3983 len += (int)(tmp_ptr - ptr);
3984 *cont_s_ipos = TRUE;
3985 }
3986 IObuff[len] = NUL;
3987 ptr = IObuff;
3988 }
3989 if (len == compl_length)
3990 return NULL;
3991 }
3992 }
3993
3994 *match_len = len;
3995 return ptr;
3996}
3997
3998/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003999 * Get the next set of words matching "compl_pattern" for default completion(s)
4000 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004001 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4002 * position "st->start_pos" in the "compl_direction" direction. If
4003 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4004 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004005 * Returns OK if a new next match is found, otherwise returns FAIL.
4006 */
4007 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004008get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004009{
4010 int found_new_match = FAIL;
4011 int save_p_scs;
4012 int save_p_ws;
4013 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004014 char_u *ptr = NULL;
4015 int len = 0;
4016 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
4017 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004018
4019 // If 'infercase' is set, don't use 'smartcase' here
4020 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004021 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004022 p_scs = FALSE;
4023
4024 // Buffers other than curbuf are scanned from the beginning or the
4025 // end but never from the middle, thus setting nowrapscan in this
4026 // buffer is a good idea, on the other hand, we always set
4027 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4028 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004029 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004030 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004031 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004032 p_ws = TRUE;
4033 looped_around = FALSE;
4034 for (;;)
4035 {
4036 int cont_s_ipos = FALSE;
4037
4038 ++msg_silent; // Don't want messages for wrapscan.
4039
4040 // ctrl_x_mode_line_or_eval() || word-wise search that
4041 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02004042 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004043 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01004044 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02004045 else if (in_fuzzy)
4046 found_new_match = search_for_fuzzy_match(st->ins_buf,
4047 st->cur_match_pos, leader, compl_direction,
4048 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004049 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004050 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004051 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004052 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004053 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004054 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004055 {
4056 // set "compl_started" even on fail
4057 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004058 st->first_match_pos = *st->cur_match_pos;
4059 st->last_match_pos = *st->cur_match_pos;
4060 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004061 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004062 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4063 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004064 {
4065 found_new_match = FAIL;
4066 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004067 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004068 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4069 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4070 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004071 {
4072 if (looped_around)
4073 found_new_match = FAIL;
4074 else
4075 looped_around = TRUE;
4076 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004077 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004078 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4079 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4080 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004081 {
4082 if (looped_around)
4083 found_new_match = FAIL;
4084 else
4085 looped_around = TRUE;
4086 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004087 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004088 if (found_new_match == FAIL)
4089 break;
4090
4091 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004092 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004093 && start_pos->lnum == st->cur_match_pos->lnum
4094 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004095 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004096
glepnir8159fb12024-07-17 20:32:54 +02004097 if (!in_fuzzy)
4098 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4099 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004100 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004101 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004102
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004103 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004104 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004105 0, cont_s_ipos) != NOTDONE)
4106 {
4107 found_new_match = OK;
4108 break;
4109 }
4110 }
4111 p_scs = save_p_scs;
4112 p_ws = save_p_ws;
4113
4114 return found_new_match;
4115}
4116
4117/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004118 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004119 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004120 */
4121 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004122get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004123{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004124 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004125
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004126 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004127 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004128 case -1:
4129 break;
4130#ifdef FEAT_FIND_ID
4131 case CTRL_X_PATH_PATTERNS:
4132 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004133 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004134 break;
4135#endif
4136
4137 case CTRL_X_DICTIONARY:
4138 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004139 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4140 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004141 break;
4142
4143 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004144 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004145 break;
4146
4147 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004148 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004149 break;
4150
4151 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004152 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004153 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004154 break;
4155
4156#ifdef FEAT_COMPL_FUNC
4157 case CTRL_X_FUNCTION:
4158 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004159 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004160 break;
4161#endif
4162
4163 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004164 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004165 break;
4166
4167 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004168 found_new_match = get_next_default_completion(st, ini);
4169 if (found_new_match == FAIL && st->ins_buf == curbuf)
4170 st->found_all = TRUE;
4171 }
4172
4173 // check if compl_curr_match has changed, (e.g. other type of
4174 // expansion added something)
4175 if (type != 0 && compl_curr_match != compl_old_match)
4176 found_new_match = OK;
4177
4178 return found_new_match;
4179}
4180
4181/*
4182 * Get the next expansion(s), using "compl_pattern".
4183 * The search starts at position "ini" in curbuf and in the direction
4184 * compl_direction.
4185 * When "compl_started" is FALSE start at that position, otherwise continue
4186 * where we stopped searching before.
4187 * This may return before finding all the matches.
4188 * Return the total number of matches or -1 if still unknown -- Acevedo
4189 */
4190 static int
4191ins_compl_get_exp(pos_T *ini)
4192{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004193 static ins_compl_next_state_T st;
4194 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004195 int i;
4196 int found_new_match;
4197 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004198 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004199
4200 if (!compl_started)
4201 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004202 buf_T *buf;
4203
4204 FOR_ALL_BUFFERS(buf)
4205 buf->b_scanned = 0;
4206 if (!st_cleared)
4207 {
4208 CLEAR_FIELD(st);
4209 st_cleared = TRUE;
4210 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004211 st.found_all = FALSE;
4212 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004213 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004214 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004215 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4216 ? (char_u *)"." : curbuf->b_p_cpt);
4217 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004218 st.last_match_pos = st.first_match_pos = *ini;
4219 }
4220 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4221 st.ins_buf = curbuf; // In case the buffer was wiped out.
4222
4223 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004224 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004225 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004226
4227 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4228 for (;;)
4229 {
4230 found_new_match = FAIL;
4231 st.set_match_pos = FALSE;
4232
4233 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4234 // or if found_all says this entry is done. For ^X^L only use the
4235 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004236 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004237 && (!compl_started || st.found_all))
4238 {
glepnir8159fb12024-07-17 20:32:54 +02004239 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004240
4241 if (status == INS_COMPL_CPT_END)
4242 break;
4243 if (status == INS_COMPL_CPT_CONT)
4244 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004245 }
4246
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004247 // If complete() was called then compl_pattern has been reset. The
4248 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004249 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004250 break;
4251
4252 // get the next set of completion matches
4253 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004254
4255 // break the loop for specialized modes (use 'complete' just for the
4256 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4257 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004258 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4259 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004260 {
4261 if (got_int)
4262 break;
4263 // Fill the popup menu as soon as possible.
4264 if (type != -1)
4265 ins_compl_check_keys(0, FALSE);
4266
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004267 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004268 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4269 break;
4270 compl_started = TRUE;
4271 }
4272 else
4273 {
4274 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004275 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004276 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004277
4278 compl_started = FALSE;
4279 }
4280 }
4281 compl_started = TRUE;
4282
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004283 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004284 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004285 found_new_match = FAIL;
4286
4287 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004288 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004289 && !ctrl_x_mode_line_or_eval()))
4290 i = ins_compl_make_cyclic();
4291
4292 if (compl_old_match != NULL)
4293 {
4294 // If several matches were added (FORWARD) or the search failed and has
4295 // just been made cyclic then we have to move compl_curr_match to the
4296 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004297 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004298 : compl_old_match->cp_prev;
4299 if (compl_curr_match == NULL)
4300 compl_curr_match = compl_old_match;
4301 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004302 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004303
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004304 return i;
4305}
4306
4307/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004308 * Update "compl_shown_match" to the actually shown match, it may differ when
4309 * "compl_leader" is used to omit some of the matches.
4310 */
4311 static void
4312ins_compl_update_shown_match(void)
4313{
4314 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004315 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004316 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004317 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004318 compl_shown_match = compl_shown_match->cp_next;
4319
4320 // If we didn't find it searching forward, and compl_shows_dir is
4321 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004322 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004323 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004324 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004325 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004326 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004327 {
4328 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004329 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004330 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004331 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004332 compl_shown_match = compl_shown_match->cp_prev;
4333 }
4334}
4335
4336/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004337 * Delete the old text being completed.
4338 */
4339 void
4340ins_compl_delete(void)
4341{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004342 // In insert mode: Delete the typed part.
4343 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004344 int col = compl_col + (compl_status_adding() ? compl_length : 0);
4345 int has_preinsert = ins_compl_preinsert_effect();
4346 if (has_preinsert)
4347 {
glepnirbfb4eea2025-01-31 15:28:29 +01004348 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004349 curwin->w_cursor.col = compl_ins_end_col;
4350 }
4351
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004352 if ((int)curwin->w_cursor.col > col)
4353 {
4354 if (stop_arrow() == FAIL)
4355 return;
4356 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004357 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004358 }
4359
4360 // TODO: is this sufficient for redrawing? Redrawing everything causes
4361 // flicker, thus we can't do that.
4362 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004363#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004364 // clear v:completed_item
4365 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004366#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004367}
4368
4369/*
4370 * Insert the new text being completed.
4371 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004372 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4373 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004374 */
4375 void
glepniredd4ac32025-01-29 18:53:51 +01004376ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004377{
glepniredd4ac32025-01-29 18:53:51 +01004378 int compl_len = get_compl_len();
4379 int preinsert = ins_compl_has_preinsert();
zeertzjq8297e2c2025-01-30 11:04:47 +01004380 char_u *cp_str = compl_shown_match->cp_str.string;
4381 size_t cp_str_len = compl_shown_match->cp_str.length;
4382 size_t leader_len = ins_compl_leader_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004383
4384 // Make sure we don't go over the end of the string, this can happen with
4385 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004386 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004387 {
zeertzjq8297e2c2025-01-30 11:04:47 +01004388 ins_compl_insert_bytes(cp_str + compl_len, -1);
glepniredd4ac32025-01-29 18:53:51 +01004389 if (preinsert && move_cursor)
zeertzjq8297e2c2025-01-30 11:04:47 +01004390 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
glepniredd4ac32025-01-29 18:53:51 +01004391 }
4392 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004393 compl_used_match = FALSE;
4394 else
4395 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004396#ifdef FEAT_EVAL
4397 {
4398 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4399
4400 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4401 }
4402#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004403 if (!in_compl_func)
4404 compl_curr_match = compl_shown_match;
4405}
4406
4407/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004408 * show the file name for the completion match (if any). Truncate the file
4409 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004410 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004411 static void
4412ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004413{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004414 char *lead = _("match in file");
4415 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4416 char_u *s;
4417 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004418
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004419 if (space <= 0)
4420 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004421
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004422 // We need the tail that fits. With double-byte encoding going
4423 // back from the end is very slow, thus go from the start and keep
4424 // the text that fits in "space" between "s" and "e".
4425 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004426 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004427 space -= ptr2cells(e);
4428 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004429 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004430 space += ptr2cells(s);
4431 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004432 }
4433 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004434 msg_hist_off = TRUE;
4435 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4436 s > compl_shown_match->cp_fname ? "<" : "", s);
4437 msg((char *)IObuff);
4438 msg_hist_off = FALSE;
4439 redraw_cmdline = FALSE; // don't overwrite!
4440}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004441
glepnirdca57fb2024-06-04 22:01:21 +02004442/*
zeertzjq551d8c32024-06-05 19:53:32 +02004443 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004444 */
glepnira218cc62024-06-03 19:32:39 +02004445 static compl_T *
4446find_comp_when_fuzzy(void)
4447{
4448 int score;
4449 char_u* str;
4450 int target_idx = -1;
4451 int is_forward = compl_shows_dir_forward();
4452 int is_backward = compl_shows_dir_backward();
4453 compl_T *comp = NULL;
4454
glepnir43eef882024-06-19 20:20:48 +02004455 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004456 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004457 return compl_first_match != compl_shown_match ? compl_first_match :
4458 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004459
4460 if (is_forward)
4461 target_idx = compl_selected_item + 1;
4462 else if (is_backward)
4463 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004464 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004465
4466 score = compl_match_array[target_idx].pum_score;
4467 str = compl_match_array[target_idx].pum_text;
4468
4469 comp = compl_first_match;
4470 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004471 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004472 return comp;
4473 comp = comp->cp_next;
4474 } while (comp != NULL && !is_first_match(comp));
4475
4476 return NULL;
4477}
4478
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004479/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004480 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004481 * times. The number of matches found is returned in 'num_matches'.
4482 *
4483 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4484 * get more completions. If it is FALSE, then do nothing when there are no more
4485 * completions in the given direction.
4486 *
4487 * If "advance" is TRUE, then completion will move to the first match.
4488 * Otherwise, the original text will be shown.
4489 *
4490 * Returns OK on success and -1 if the number of matches are unknown.
4491 */
4492 static int
4493find_next_completion_match(
4494 int allow_get_expansion,
4495 int todo, // repeat completion this many times
4496 int advance,
4497 int *num_matches)
4498{
4499 int found_end = FALSE;
4500 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004501 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004502 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4503 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004504
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004505 while (--todo >= 0)
4506 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004507 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004508 {
glepniraced8c22024-06-15 15:13:05 +02004509 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4510 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004511 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004512 && (is_first_match(compl_shown_match->cp_next)
4513 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004514 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004515 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004516 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004517 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004518 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004519 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4520 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004521 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004522 }
4523 else
4524 {
4525 if (!allow_get_expansion)
4526 {
4527 if (advance)
4528 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004529 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004530 compl_pending -= todo + 1;
4531 else
4532 compl_pending += todo + 1;
4533 }
4534 return -1;
4535 }
4536
4537 if (!compl_no_select && advance)
4538 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004539 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004540 --compl_pending;
4541 else
4542 ++compl_pending;
4543 }
4544
4545 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004546 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004547
4548 // handle any pending completions
4549 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004550 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004551 {
4552 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4553 {
4554 compl_shown_match = compl_shown_match->cp_next;
4555 --compl_pending;
4556 }
4557 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4558 {
4559 compl_shown_match = compl_shown_match->cp_prev;
4560 ++compl_pending;
4561 }
4562 else
4563 break;
4564 }
4565 found_end = FALSE;
4566 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004567 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004568 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004569 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004570 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004571 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004572 ++todo;
4573 else
4574 // Remember a matching item.
4575 found_compl = compl_shown_match;
4576
4577 // Stop at the end of the list when we found a usable match.
4578 if (found_end)
4579 {
4580 if (found_compl != NULL)
4581 {
4582 compl_shown_match = found_compl;
4583 break;
4584 }
4585 todo = 1; // use first usable match after wrapping around
4586 }
4587 }
4588
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004589 return OK;
4590}
4591
4592/*
4593 * Fill in the next completion in the current direction.
4594 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4595 * get more completions. If it is FALSE, then we just do nothing when there
4596 * are no more completions in a given direction. The latter case is used when
4597 * we are still in the middle of finding completions, to allow browsing
4598 * through the ones found so far.
4599 * Return the total number of matches, or -1 if still unknown -- webb.
4600 *
4601 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4602 * compl_shown_match here.
4603 *
4604 * Note that this function may be called recursively once only. First with
4605 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4606 * calls this function with "allow_get_expansion" FALSE.
4607 */
4608 static int
4609ins_compl_next(
4610 int allow_get_expansion,
4611 int count, // repeat completion this many times; should
4612 // be at least 1
4613 int insert_match, // Insert the newly selected match
4614 int in_compl_func) // called from complete_check()
4615{
4616 int num_matches = -1;
4617 int todo = count;
4618 int advance;
4619 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004620 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004621 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004622 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4623 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004624 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004625
4626 // When user complete function return -1 for findstart which is next
4627 // time of 'always', compl_shown_match become NULL.
4628 if (compl_shown_match == NULL)
4629 return -1;
4630
John Marriott5e6ea922024-11-23 14:01:57 +01004631 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004632 && !match_at_original_text(compl_shown_match)
4633 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004634 // Update "compl_shown_match" to the actually shown match
4635 ins_compl_update_shown_match();
4636
4637 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004638 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004639 // Delete old text to be replaced
4640 ins_compl_delete();
4641
4642 // When finding the longest common text we stick at the original text,
4643 // don't let CTRL-N or CTRL-P move to the first match.
4644 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4645
4646 // When restarting the search don't insert the first match either.
4647 if (compl_restarting)
4648 {
4649 advance = FALSE;
4650 compl_restarting = FALSE;
4651 }
4652
4653 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4654 // around.
4655 if (find_next_completion_match(allow_get_expansion, todo, advance,
4656 &num_matches) == -1)
4657 return -1;
4658
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004659 if (curbuf != orig_curbuf)
4660 {
4661 // In case some completion function switched buffer, don't want to
4662 // insert the completion elsewhere.
4663 return -1;
4664 }
4665
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004666 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01004667 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004668 {
glepnir6a38aff2024-12-16 21:56:16 +01004669 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004670 compl_used_match = FALSE;
4671 }
4672 else if (insert_match)
4673 {
4674 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01004675 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004676 else
glepnir6a38aff2024-12-16 21:56:16 +01004677 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004678 }
4679 else
4680 compl_used_match = FALSE;
4681
4682 if (!allow_get_expansion)
4683 {
4684 // may undisplay the popup menu first
4685 ins_compl_upd_pum();
4686
4687 if (pum_enough_matches())
4688 // Will display the popup menu, don't redraw yet to avoid flicker.
4689 pum_call_update_screen();
4690 else
4691 // Not showing the popup menu yet, redraw to show the user what was
4692 // inserted.
4693 update_screen(0);
4694
4695 // display the updated popup menu
4696 ins_compl_show_pum();
4697#ifdef FEAT_GUI
4698 if (gui.in_use)
4699 {
4700 // Show the cursor after the match, not after the redrawn text.
4701 setcursor();
4702 out_flush_cursor(FALSE, FALSE);
4703 }
4704#endif
4705
4706 // Delete old text to be replaced, since we're still searching and
4707 // don't want to match ourselves!
4708 ins_compl_delete();
4709 }
4710
4711 // Enter will select a match when the match wasn't inserted and the popup
4712 // menu is visible.
4713 if (compl_no_insert && !started)
4714 compl_enter_selects = TRUE;
4715 else
4716 compl_enter_selects = !insert_match && compl_match_array != NULL;
4717
4718 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004719 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004720 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004721
4722 return num_matches;
4723}
4724
4725/*
4726 * Call this while finding completions, to check whether the user has hit a key
4727 * that should change the currently displayed completion, or exit completion
4728 * mode. Also, when compl_pending is not zero, show a completion as soon as
4729 * possible. -- webb
4730 * "frequency" specifies out of how many calls we actually check.
4731 * "in_compl_func" is TRUE when called from complete_check(), don't set
4732 * compl_curr_match.
4733 */
4734 void
4735ins_compl_check_keys(int frequency, int in_compl_func)
4736{
4737 static int count = 0;
4738 int c;
4739
4740 // Don't check when reading keys from a script, :normal or feedkeys().
4741 // That would break the test scripts. But do check for keys when called
4742 // from complete_check().
4743 if (!in_compl_func && (using_script() || ex_normal_busy))
4744 return;
4745
4746 // Only do this at regular intervals
4747 if (++count < frequency)
4748 return;
4749 count = 0;
4750
4751 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4752 // can't do its work correctly.
4753 c = vpeekc_any();
4754 if (c != NUL)
4755 {
4756 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4757 {
4758 c = safe_vgetc(); // Eat the character
4759 compl_shows_dir = ins_compl_key2dir(c);
4760 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4761 c != K_UP && c != K_DOWN, in_compl_func);
4762 }
4763 else
4764 {
4765 // Need to get the character to have KeyTyped set. We'll put it
4766 // back with vungetc() below. But skip K_IGNORE.
4767 c = safe_vgetc();
4768 if (c != K_IGNORE)
4769 {
4770 // Don't interrupt completion when the character wasn't typed,
4771 // e.g., when doing @q to replay keys.
4772 if (c != Ctrl_R && KeyTyped)
4773 compl_interrupted = TRUE;
4774
4775 vungetc(c);
4776 }
4777 }
4778 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004779 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004780 {
4781 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4782
4783 compl_pending = 0;
4784 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4785 }
4786}
4787
4788/*
4789 * Decide the direction of Insert mode complete from the key typed.
4790 * Returns BACKWARD or FORWARD.
4791 */
4792 static int
4793ins_compl_key2dir(int c)
4794{
4795 if (c == Ctrl_P || c == Ctrl_L
4796 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4797 return BACKWARD;
4798 return FORWARD;
4799}
4800
4801/*
4802 * Return TRUE for keys that are used for completion only when the popup menu
4803 * is visible.
4804 */
4805 static int
4806ins_compl_pum_key(int c)
4807{
4808 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4809 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4810 || c == K_UP || c == K_DOWN);
4811}
4812
4813/*
4814 * Decide the number of completions to move forward.
4815 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4816 */
4817 static int
4818ins_compl_key2count(int c)
4819{
4820 int h;
4821
4822 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4823 {
4824 h = pum_get_height();
4825 if (h > 3)
4826 h -= 2; // keep some context
4827 return h;
4828 }
4829 return 1;
4830}
4831
4832/*
4833 * Return TRUE if completion with "c" should insert the match, FALSE if only
4834 * to change the currently selected completion.
4835 */
4836 static int
4837ins_compl_use_match(int c)
4838{
4839 switch (c)
4840 {
4841 case K_UP:
4842 case K_DOWN:
4843 case K_PAGEDOWN:
4844 case K_KPAGEDOWN:
4845 case K_S_DOWN:
4846 case K_PAGEUP:
4847 case K_KPAGEUP:
4848 case K_S_UP:
4849 return FALSE;
4850 }
4851 return TRUE;
4852}
4853
4854/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004855 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4856 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004857 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004858 * Uses the global variables: compl_cont_status and ctrl_x_mode
4859 */
4860 static int
4861get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4862{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004863 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004864 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004865 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004866 {
4867 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4868 ;
4869 compl_col += ++startcol;
4870 compl_length = curs_col - startcol;
4871 }
4872 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004873 {
John Marriott5e6ea922024-11-23 14:01:57 +01004874 compl_pattern.string = str_foldcase(line + compl_col,
4875 compl_length, NULL, 0);
4876 if (compl_pattern.string == NULL)
4877 {
4878 compl_pattern.length = 0;
4879 return FAIL;
4880 }
4881 compl_pattern.length = STRLEN(compl_pattern.string);
4882 }
4883 else
4884 {
4885 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4886 if (compl_pattern.string == NULL)
4887 {
4888 compl_pattern.length = 0;
4889 return FAIL;
4890 }
4891 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004892 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004893 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004894 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004895 {
4896 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004897 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004898 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004899
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004900 if (!vim_iswordp(line + compl_col)
4901 || (compl_col > 0
4902 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004903 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004904 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004905 prefixlen = 0;
4906 }
John Marriott5e6ea922024-11-23 14:01:57 +01004907
4908 // we need up to 2 extra chars for the prefix
4909 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4910 compl_pattern.string = alloc(n);
4911 if (compl_pattern.string == NULL)
4912 {
4913 compl_pattern.length = 0;
4914 return FAIL;
4915 }
4916 STRCPY((char *)compl_pattern.string, prefix);
4917 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004918 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004919 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004920 }
4921 else if (--startcol < 0
4922 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4923 {
John Marriott5e6ea922024-11-23 14:01:57 +01004924 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4925
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004926 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004927 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4928 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004929 {
John Marriott5e6ea922024-11-23 14:01:57 +01004930 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004931 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004932 }
John Marriott5e6ea922024-11-23 14:01:57 +01004933 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004934 compl_col += curs_col;
4935 compl_length = 0;
4936 }
4937 else
4938 {
4939 // Search the point of change class of multibyte character
4940 // or not a word single byte character backward.
4941 if (has_mbyte)
4942 {
4943 int base_class;
4944 int head_off;
4945
4946 startcol -= (*mb_head_off)(line, line + startcol);
4947 base_class = mb_get_class(line + startcol);
4948 while (--startcol >= 0)
4949 {
4950 head_off = (*mb_head_off)(line, line + startcol);
4951 if (base_class != mb_get_class(line + startcol
4952 - head_off))
4953 break;
4954 startcol -= head_off;
4955 }
4956 }
4957 else
4958 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4959 ;
4960 compl_col += ++startcol;
4961 compl_length = (int)curs_col - startcol;
4962 if (compl_length == 1)
4963 {
4964 // Only match word with at least two chars -- webb
4965 // there's no need to call quote_meta,
4966 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004967 compl_pattern.string = alloc(7);
4968 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004969 {
John Marriott5e6ea922024-11-23 14:01:57 +01004970 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004971 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004972 }
John Marriott5e6ea922024-11-23 14:01:57 +01004973 STRCPY((char *)compl_pattern.string, "\\<");
4974 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4975 STRCAT((char *)compl_pattern.string, "\\k");
4976 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004977 }
4978 else
4979 {
John Marriott5e6ea922024-11-23 14:01:57 +01004980 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4981
4982 compl_pattern.string = alloc(n);
4983 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004984 {
John Marriott5e6ea922024-11-23 14:01:57 +01004985 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004986 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004987 }
John Marriott5e6ea922024-11-23 14:01:57 +01004988 STRCPY((char *)compl_pattern.string, "\\<");
4989 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004990 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004991 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004992 }
4993 }
4994
4995 return OK;
4996}
4997
4998/*
4999 * Get the pattern, column and length for whole line completion or for the
5000 * complete() function.
5001 * Sets the global variables: compl_col, compl_length and compl_pattern.
5002 */
5003 static int
5004get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5005{
5006 compl_col = (colnr_T)getwhitecols(line);
5007 compl_length = (int)curs_col - (int)compl_col;
5008 if (compl_length < 0) // cursor in indent: empty pattern
5009 compl_length = 0;
5010 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005011 {
John Marriott5e6ea922024-11-23 14:01:57 +01005012 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5013 NULL, 0);
5014 if (compl_pattern.string == NULL)
5015 {
5016 compl_pattern.length = 0;
5017 return FAIL;
5018 }
5019 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005020 }
John Marriott5e6ea922024-11-23 14:01:57 +01005021 else
5022 {
5023 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5024 if (compl_pattern.string == NULL)
5025 {
5026 compl_pattern.length = 0;
5027 return FAIL;
5028 }
5029 compl_pattern.length = (size_t)compl_length;
5030 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005031
5032 return OK;
5033}
5034
5035/*
5036 * Get the pattern, column and length for filename completion.
5037 * Sets the global variables: compl_col, compl_length and compl_pattern.
5038 */
5039 static int
5040get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5041{
5042 // Go back to just before the first filename character.
5043 if (startcol > 0)
5044 {
5045 char_u *p = line + startcol;
5046
5047 MB_PTR_BACK(line, p);
5048 while (p > line && vim_isfilec(PTR2CHAR(p)))
5049 MB_PTR_BACK(line, p);
5050 if (p == line && vim_isfilec(PTR2CHAR(p)))
5051 startcol = 0;
5052 else
5053 startcol = (int)(p - line) + 1;
5054 }
5055
5056 compl_col += startcol;
5057 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005058 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5059 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005060 {
John Marriott5e6ea922024-11-23 14:01:57 +01005061 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005062 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005063 }
5064
John Marriott5e6ea922024-11-23 14:01:57 +01005065 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005066
5067 return OK;
5068}
5069
5070/*
5071 * Get the pattern, column and length for command-line completion.
5072 * Sets the global variables: compl_col, compl_length and compl_pattern.
5073 */
5074 static int
5075get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5076{
John Marriott5e6ea922024-11-23 14:01:57 +01005077 compl_pattern.string = vim_strnsave(line, curs_col);
5078 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005079 {
John Marriott5e6ea922024-11-23 14:01:57 +01005080 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005081 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005082 }
John Marriott5e6ea922024-11-23 14:01:57 +01005083 compl_pattern.length = curs_col;
5084 set_cmd_context(&compl_xp, compl_pattern.string,
5085 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005086 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5087 || compl_xp.xp_context == EXPAND_NOTHING)
5088 // No completion possible, use an empty pattern to get a
5089 // "pattern not found" message.
5090 compl_col = curs_col;
5091 else
John Marriott5e6ea922024-11-23 14:01:57 +01005092 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005093 compl_length = curs_col - compl_col;
5094
5095 return OK;
5096}
5097
5098/*
5099 * Get the pattern, column and length for user defined completion ('omnifunc',
5100 * 'completefunc' and 'thesaurusfunc')
5101 * Sets the global variables: compl_col, compl_length and compl_pattern.
5102 * Uses the global variable: spell_bad_len
5103 */
5104 static int
5105get_userdefined_compl_info(colnr_T curs_col UNUSED)
5106{
5107 int ret = FAIL;
5108
5109#ifdef FEAT_COMPL_FUNC
5110 // Call user defined function 'completefunc' with "a:findstart"
5111 // set to 1 to obtain the length of text to use for completion.
5112 char_u *line;
5113 typval_T args[3];
5114 int col;
5115 char_u *funcname;
5116 pos_T pos;
5117 int save_State = State;
5118 callback_T *cb;
5119
5120 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5121 // length as a string
5122 funcname = get_complete_funcname(ctrl_x_mode);
5123 if (*funcname == NUL)
5124 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005125 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005126 ? "completefunc" : "omnifunc");
5127 return FAIL;
5128 }
5129
5130 args[0].v_type = VAR_NUMBER;
5131 args[0].vval.v_number = 1;
5132 args[1].v_type = VAR_STRING;
5133 args[1].vval.v_string = (char_u *)"";
5134 args[2].v_type = VAR_UNKNOWN;
5135 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005136 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005137 cb = get_insert_callback(ctrl_x_mode);
5138 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005139 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005140
5141 State = save_State;
5142 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005143 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005144 validate_cursor();
5145 if (!EQUAL_POS(curwin->w_cursor, pos))
5146 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005147 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005148 return FAIL;
5149 }
5150
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005151 // Return value -2 means the user complete function wants to cancel the
5152 // complete without an error, do the same if the function did not execute
5153 // successfully.
5154 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005155 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005156 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005157 if (col == -3)
5158 {
5159 ctrl_x_mode = CTRL_X_NORMAL;
5160 edit_submode = NULL;
5161 if (!shortmess(SHM_COMPLETIONMENU))
5162 msg_clr_cmdline();
5163 return FAIL;
5164 }
5165
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005166 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005167 // completion.
5168 compl_opt_refresh_always = FALSE;
5169 compl_opt_suppress_empty = FALSE;
5170
5171 if (col < 0)
5172 col = curs_col;
5173 compl_col = col;
5174 if (compl_col > curs_col)
5175 compl_col = curs_col;
5176
5177 // Setup variables for completion. Need to obtain "line" again,
5178 // it may have become invalid.
5179 line = ml_get(curwin->w_cursor.lnum);
5180 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005181 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5182 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005183 {
John Marriott5e6ea922024-11-23 14:01:57 +01005184 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005185 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005186 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005187
John Marriott5e6ea922024-11-23 14:01:57 +01005188 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005189 ret = OK;
5190#endif
5191
5192 return ret;
5193}
5194
5195/*
5196 * Get the pattern, column and length for spell completion.
5197 * Sets the global variables: compl_col, compl_length and compl_pattern.
5198 * Uses the global variable: spell_bad_len
5199 */
5200 static int
5201get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5202{
5203 int ret = FAIL;
5204#ifdef FEAT_SPELL
5205 char_u *line;
5206
5207 if (spell_bad_len > 0)
5208 compl_col = curs_col - spell_bad_len;
5209 else
5210 compl_col = spell_word_start(startcol);
5211 if (compl_col >= (colnr_T)startcol)
5212 {
5213 compl_length = 0;
5214 compl_col = curs_col;
5215 }
5216 else
5217 {
5218 spell_expand_check_cap(compl_col);
5219 compl_length = (int)curs_col - compl_col;
5220 }
5221 // Need to obtain "line" again, it may have become invalid.
5222 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005223 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5224 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005225 {
John Marriott5e6ea922024-11-23 14:01:57 +01005226 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005227 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005228 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005229
John Marriott5e6ea922024-11-23 14:01:57 +01005230 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005231 ret = OK;
5232#endif
5233
5234 return ret;
5235}
5236
5237/*
5238 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005239 * "startcol" - start column number of the completion pattern/text
5240 * "cur_col" - current cursor column
5241 * On return, "line_invalid" is set to TRUE, if the current line may have
5242 * become invalid and needs to be fetched again.
5243 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005244 */
5245 static int
5246compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5247{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005248 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005249 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5250 && !thesaurus_func_complete(ctrl_x_mode)))
5251 {
5252 return get_normal_compl_info(line, startcol, curs_col);
5253 }
5254 else if (ctrl_x_mode_line_or_eval())
5255 {
5256 return get_wholeline_compl_info(line, curs_col);
5257 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005258 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005259 {
5260 return get_filename_compl_info(line, startcol, curs_col);
5261 }
5262 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5263 {
5264 return get_cmdline_compl_info(line, curs_col);
5265 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005266 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005267 || thesaurus_func_complete(ctrl_x_mode))
5268 {
5269 if (get_userdefined_compl_info(curs_col) == FAIL)
5270 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005271 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005272 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005273 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005274 {
5275 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5276 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005277 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005278 }
5279 else
5280 {
5281 internal_error("ins_complete()");
5282 return FAIL;
5283 }
5284
5285 return OK;
5286}
5287
5288/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005289 * Continue an interrupted completion mode search in "line".
5290 *
5291 * If this same ctrl_x_mode has been interrupted use the text from
5292 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5293 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5294 * the same line as the cursor then fix it (the line has been split because it
5295 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5296 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005297 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005298 static void
5299ins_compl_continue_search(char_u *line)
5300{
5301 // it is a continued search
5302 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005303 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5304 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005305 {
5306 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5307 {
5308 // line (probably) wrapped, set compl_startpos to the
5309 // first non_blank in the line, if it is not a wordchar
5310 // include it to get a better pattern, but then we don't
5311 // want the "\\<" prefix, check it below
5312 compl_col = (colnr_T)getwhitecols(line);
5313 compl_startpos.col = compl_col;
5314 compl_startpos.lnum = curwin->w_cursor.lnum;
5315 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5316 }
5317 else
5318 {
5319 // S_IPOS was set when we inserted a word that was at the
5320 // beginning of the line, which means that we'll go to SOL
5321 // mode but first we need to redefine compl_startpos
5322 if (compl_cont_status & CONT_S_IPOS)
5323 {
5324 compl_cont_status |= CONT_SOL;
5325 compl_startpos.col = (colnr_T)(skipwhite(
5326 line + compl_length
5327 + compl_startpos.col) - line);
5328 }
5329 compl_col = compl_startpos.col;
5330 }
5331 compl_length = curwin->w_cursor.col - (int)compl_col;
5332 // IObuff is used to add a "word from the next line" would we
5333 // have enough space? just being paranoid
5334#define MIN_SPACE 75
5335 if (compl_length > (IOSIZE - MIN_SPACE))
5336 {
5337 compl_cont_status &= ~CONT_SOL;
5338 compl_length = (IOSIZE - MIN_SPACE);
5339 compl_col = curwin->w_cursor.col - compl_length;
5340 }
5341 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5342 if (compl_length < 1)
5343 compl_cont_status &= CONT_LOCAL;
5344 }
5345 else if (ctrl_x_mode_line_or_eval())
5346 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5347 else
5348 compl_cont_status = 0;
5349}
5350
5351/*
5352 * start insert mode completion
5353 */
5354 static int
5355ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005356{
5357 char_u *line;
5358 int startcol = 0; // column where searched text starts
5359 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005360 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005361 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005362 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005363
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005364 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005365
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005366 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005367 did_si = FALSE;
5368 can_si = FALSE;
5369 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005370 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005371 return FAIL;
5372
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005373 line = ml_get(curwin->w_cursor.lnum);
5374 curs_col = curwin->w_cursor.col;
5375 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005376
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005377 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5378 && compl_cont_mode == ctrl_x_mode)
5379 // this same ctrl-x_mode was interrupted previously. Continue the
5380 // completion.
5381 ins_compl_continue_search(line);
5382 else
5383 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005384
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005385 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005386 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005387 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005388 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005389 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5390 compl_cont_status = 0;
5391 compl_cont_status |= CONT_N_ADDS;
5392 compl_startpos = curwin->w_cursor;
5393 startcol = (int)curs_col;
5394 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005395 }
5396
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005397 // Work out completion pattern and original text -- webb
5398 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5399 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005400 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5401 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005402 // restore did_ai, so that adding comment leader works
5403 did_ai = save_did_ai;
5404 return FAIL;
5405 }
5406 // If "line" was changed while getting completion info get it again.
5407 if (line_invalid)
5408 line = ml_get(curwin->w_cursor.lnum);
5409
glepnir7cfe6932024-09-15 20:06:28 +02005410 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005411 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005412 {
5413 edit_submode_pre = (char_u *)_(" Adding");
5414 if (ctrl_x_mode_line_or_eval())
5415 {
5416 // Insert a new line, keep indentation but ignore 'comments'.
5417 char_u *old = curbuf->b_p_com;
5418
5419 curbuf->b_p_com = (char_u *)"";
5420 compl_startpos.lnum = curwin->w_cursor.lnum;
5421 compl_startpos.col = compl_col;
5422 ins_eol('\r');
5423 curbuf->b_p_com = old;
5424 compl_length = 0;
5425 compl_col = curwin->w_cursor.col;
5426 }
glepnir7cfe6932024-09-15 20:06:28 +02005427 else if (ctrl_x_mode_normal() && in_fuzzy)
5428 {
5429 compl_startpos = curwin->w_cursor;
5430 compl_cont_status &= CONT_S_IPOS;
5431 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005432 }
5433 else
5434 {
5435 edit_submode_pre = NULL;
5436 compl_startpos.col = compl_col;
5437 }
5438
5439 if (compl_cont_status & CONT_LOCAL)
5440 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5441 else
5442 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5443
5444 // If any of the original typed text has been changed we need to fix
5445 // the redo buffer.
5446 ins_compl_fixRedoBufForLeader(NULL);
5447
5448 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005449 VIM_CLEAR_STRING(compl_orig_text);
5450 compl_orig_text.length = (size_t)compl_length;
5451 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005452 if (p_ic)
5453 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005454 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5455 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005456 {
John Marriott5e6ea922024-11-23 14:01:57 +01005457 VIM_CLEAR_STRING(compl_pattern);
5458 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005459 return FAIL;
5460 }
5461
5462 // showmode might reset the internal line pointers, so it must
5463 // be called before line = ml_get(), or when this address is no
5464 // longer needed. -- Acevedo.
5465 edit_submode_extra = (char_u *)_("-- Searching...");
5466 edit_submode_highl = HLF_COUNT;
5467 showmode();
5468 edit_submode_extra = NULL;
5469 out_flush();
5470
5471 return OK;
5472}
5473
5474/*
5475 * display the completion status message
5476 */
5477 static void
5478ins_compl_show_statusmsg(void)
5479{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005480 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005481 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005482 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005483 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005484 ? (char_u *)_("Hit end of paragraph")
5485 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005486 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005487 }
5488
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005489 if (edit_submode_extra == NULL)
5490 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005491 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005492 {
5493 edit_submode_extra = (char_u *)_("Back at original");
5494 edit_submode_highl = HLF_W;
5495 }
5496 else if (compl_cont_status & CONT_S_IPOS)
5497 {
5498 edit_submode_extra = (char_u *)_("Word from other line");
5499 edit_submode_highl = HLF_COUNT;
5500 }
5501 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5502 {
5503 edit_submode_extra = (char_u *)_("The only match");
5504 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005505 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005506 }
5507 else
5508 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005509#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005510 // Update completion sequence number when needed.
5511 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005512 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005513#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005514 // The match should always have a sequence number now, this is
5515 // just a safety check.
5516 if (compl_curr_match->cp_number != -1)
5517 {
5518 // Space for 10 text chars. + 2x10-digit no.s = 31.
5519 // Translations may need more than twice that.
5520 static char_u match_ref[81];
5521
5522 if (compl_matches > 0)
5523 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005524 _("match %d of %d"),
5525 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005526 else
5527 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005528 _("match %d"),
5529 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005530 edit_submode_extra = match_ref;
5531 edit_submode_highl = HLF_R;
5532 if (dollar_vcol >= 0)
5533 curs_columns(FALSE);
5534 }
5535 }
5536 }
5537
5538 // Show a message about what (completion) mode we're in.
5539 if (!compl_opt_suppress_empty)
5540 {
5541 showmode();
5542 if (!shortmess(SHM_COMPLETIONMENU))
5543 {
5544 if (edit_submode_extra != NULL)
5545 {
5546 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005547 {
5548 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005549 msg_attr((char *)edit_submode_extra,
5550 edit_submode_highl < HLF_COUNT
5551 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005552 msg_hist_off = FALSE;
5553 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005554 }
5555 else
5556 msg_clr_cmdline(); // necessary for "noshowmode"
5557 }
5558 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005559}
5560
5561/*
5562 * Do Insert mode completion.
5563 * Called when character "c" was typed, which has a meaning for completion.
5564 * Returns OK if completion was done, FAIL if something failed (out of mem).
5565 */
5566 int
5567ins_complete(int c, int enable_pum)
5568{
5569 int n;
5570 int save_w_wrow;
5571 int save_w_leftcol;
5572 int insert_match;
5573
5574 compl_direction = ins_compl_key2dir(c);
5575 insert_match = ins_compl_use_match(c);
5576
5577 if (!compl_started)
5578 {
5579 if (ins_compl_start() == FAIL)
5580 return FAIL;
5581 }
5582 else if (insert_match && stop_arrow() == FAIL)
5583 return FAIL;
5584
5585 compl_shown_match = compl_curr_match;
5586 compl_shows_dir = compl_direction;
5587
5588 // Find next match (and following matches).
5589 save_w_wrow = curwin->w_wrow;
5590 save_w_leftcol = curwin->w_leftcol;
5591 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5592
5593 // may undisplay the popup menu
5594 ins_compl_upd_pum();
5595
5596 if (n > 1) // all matches have been found
5597 compl_matches = n;
5598 compl_curr_match = compl_shown_match;
5599 compl_direction = compl_shows_dir;
5600
5601 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5602 // mode.
5603 if (got_int && !global_busy)
5604 {
5605 (void)vgetc();
5606 got_int = FALSE;
5607 }
5608
5609 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005610 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005611 {
5612 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5613 // because we couldn't expand anything at first place, but if we used
5614 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5615 // (such as M in M'exico) if not tried already. -- Acevedo
5616 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005617 || compl_status_adding()
5618 || (ctrl_x_mode_not_default()
5619 && !ctrl_x_mode_path_patterns()
5620 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005621 compl_cont_status &= ~CONT_N_ADDS;
5622 }
5623
5624 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5625 compl_cont_status |= CONT_S_IPOS;
5626 else
5627 compl_cont_status &= ~CONT_S_IPOS;
5628
5629 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005630
5631 // Show the popup menu, unless we got interrupted.
5632 if (enable_pum && !compl_interrupted)
5633 show_pum(save_w_wrow, save_w_leftcol);
5634
5635 compl_was_interrupted = compl_interrupted;
5636 compl_interrupted = FALSE;
5637
5638 return OK;
5639}
5640
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005641/*
5642 * Remove (if needed) and show the popup menu
5643 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005644 static void
5645show_pum(int prev_w_wrow, int prev_w_leftcol)
5646{
5647 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005648 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005649 RedrawingDisabled = 0;
5650
5651 // If the cursor moved or the display scrolled we need to remove the pum
5652 // first.
5653 setcursor();
5654 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5655 ins_compl_del_pum();
5656
5657 ins_compl_show_pum();
5658 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005659
5660 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005661}
5662
5663/*
5664 * Looks in the first "len" chars. of "src" for search-metachars.
5665 * If dest is not NULL the chars. are copied there quoting (with
5666 * a backslash) the metachars, and dest would be NUL terminated.
5667 * Returns the length (needed) of dest
5668 */
5669 static unsigned
5670quote_meta(char_u *dest, char_u *src, int len)
5671{
5672 unsigned m = (unsigned)len + 1; // one extra for the NUL
5673
5674 for ( ; --len >= 0; src++)
5675 {
5676 switch (*src)
5677 {
5678 case '.':
5679 case '*':
5680 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005681 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005682 break;
5683 // FALLTHROUGH
5684 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005685 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005686 break;
5687 // FALLTHROUGH
5688 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005689 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005690 break;
5691 // FALLTHROUGH
5692 case '^': // currently it's not needed.
5693 case '$':
5694 m++;
5695 if (dest != NULL)
5696 *dest++ = '\\';
5697 break;
5698 }
5699 if (dest != NULL)
5700 *dest++ = *src;
5701 // Copy remaining bytes of a multibyte character.
5702 if (has_mbyte)
5703 {
5704 int i, mb_len;
5705
5706 mb_len = (*mb_ptr2len)(src) - 1;
5707 if (mb_len > 0 && len >= mb_len)
5708 for (i = 0; i < mb_len; ++i)
5709 {
5710 --len;
5711 ++src;
5712 if (dest != NULL)
5713 *dest++ = *src;
5714 }
5715 }
5716 }
5717 if (dest != NULL)
5718 *dest = NUL;
5719
5720 return m;
5721}
5722
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005723#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005724 void
5725free_insexpand_stuff(void)
5726{
John Marriott5e6ea922024-11-23 14:01:57 +01005727 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005728# ifdef FEAT_EVAL
5729 free_callback(&cfu_cb);
5730 free_callback(&ofu_cb);
5731 free_callback(&tsrfu_cb);
5732# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005733}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005734#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005735
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005736#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005737/*
5738 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5739 * spelled word, if there is one.
5740 */
5741 static void
5742spell_back_to_badword(void)
5743{
5744 pos_T tpos = curwin->w_cursor;
5745
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005746 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005747 if (curwin->w_cursor.col != tpos.col)
5748 start_arrow(&tpos);
5749}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005750#endif