blob: 3a168848fed4efd77ba68182b8bd1bdd0e50714b [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/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
glepnira218cc62024-06-03 19:32:39 +0200116 int cp_score; // fuzzy match score
zeertzjq41008522024-07-27 13:21:49 +0200117 int cp_user_hlattr; // highlight attribute to combine with
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100118};
119
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200120// values for cp_flags
121# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
122# define CP_FREE_FNAME 2 // cp_fname is allocated
123# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
124# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
125# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200126# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000134 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100135 */
136static compl_T *compl_first_match = NULL;
137static compl_T *compl_curr_match = NULL;
138static compl_T *compl_shown_match = NULL;
139static compl_T *compl_old_match = NULL;
140
141// After using a cursor key <Enter> selects a match in the popup menu,
142// otherwise it inserts a line break.
143static int compl_enter_selects = FALSE;
144
145// When "compl_leader" is not NULL only matches that start with this string
146// are used.
147static char_u *compl_leader = NULL;
148
149static int compl_get_longest = FALSE; // put longest common string
150 // in compl_leader
151
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100152// Selected one of the matches. When FALSE the match was edited or using the
153// longest common string.
154static int compl_used_match;
155
156// didn't finish finding completions.
157static int compl_was_interrupted = FALSE;
158
159// Set when character typed while looking for matches and it means we should
160// stop looking for matches.
161static int compl_interrupted = FALSE;
162
163static int compl_restarting = FALSE; // don't insert match
164
165// When the first completion is done "compl_started" is set. When it's
166// FALSE the word to be completed must be located.
167static int compl_started = FALSE;
168
169// Which Ctrl-X mode are we in?
170static int ctrl_x_mode = CTRL_X_NORMAL;
171
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000172static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100173static char_u *compl_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200174static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100175static int compl_direction = FORWARD;
176static int compl_shows_dir = FORWARD;
177static int compl_pending = 0; // > 1 for postponed CTRL-N
178static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000179// Length in bytes of the text being completed (this is deleted to be replaced
180// by the match.)
181static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100182static colnr_T compl_col = 0; // column where the text starts
183 // that is being completed
184static char_u *compl_orig_text = NULL; // text as it was before
185 // completion started
186static int compl_cont_mode = 0;
187static expand_T compl_xp;
188
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000189// List of flags for method of completion.
190static int compl_cont_status = 0;
191# define CONT_ADDING 1 // "normal" or "adding" expansion
192# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
193 // it's set only iff N_ADDS is set
194# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
195# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
196 // if so, word-wise-expansion will set SOL
197# define CONT_SOL 16 // pattern includes start of line, just for
198 // word-wise expansion, not set for ^X^L
199# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
200 // expansion, (eg use complete=.)
201
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100202static int compl_opt_refresh_always = FALSE;
203static int compl_opt_suppress_empty = FALSE;
204
glepnira218cc62024-06-03 19:32:39 +0200205static int compl_selected_item = -1;
206
glepnir8159fb12024-07-17 20:32:54 +0200207static int *compl_fuzzy_scores;
208
zeertzjq41008522024-07-27 13:21:49 +0200209static 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_hlattr);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000266int ctrl_x_mode_none(void)
267 { return ctrl_x_mode == 0; }
268int ctrl_x_mode_normal(void)
269 { return ctrl_x_mode == CTRL_X_NORMAL; }
270int ctrl_x_mode_scroll(void)
271 { return ctrl_x_mode == CTRL_X_SCROLL; }
272int ctrl_x_mode_whole_line(void)
273 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
274int ctrl_x_mode_files(void)
275 { return ctrl_x_mode == CTRL_X_FILES; }
276int ctrl_x_mode_tags(void)
277 { return ctrl_x_mode == CTRL_X_TAGS; }
278int ctrl_x_mode_path_patterns(void)
279 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
280int ctrl_x_mode_path_defines(void)
281 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
282int ctrl_x_mode_dictionary(void)
283 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
284int ctrl_x_mode_thesaurus(void)
285 { return ctrl_x_mode == CTRL_X_THESAURUS; }
286int ctrl_x_mode_cmdline(void)
287 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200288 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000289int ctrl_x_mode_function(void)
290 { return ctrl_x_mode == CTRL_X_FUNCTION; }
291int ctrl_x_mode_omni(void)
292 { return ctrl_x_mode == CTRL_X_OMNI; }
293int ctrl_x_mode_spell(void)
294 { return ctrl_x_mode == CTRL_X_SPELL; }
295static int ctrl_x_mode_eval(void)
296 { return ctrl_x_mode == CTRL_X_EVAL; }
297int ctrl_x_mode_line_or_eval(void)
298 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100299
300/*
301 * Whether other than default completion has been selected.
302 */
303 int
304ctrl_x_mode_not_default(void)
305{
306 return ctrl_x_mode != CTRL_X_NORMAL;
307}
308
309/*
zeertzjqdca29d92021-08-31 19:12:51 +0200310 * Whether CTRL-X was typed without a following character,
311 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100312 */
313 int
314ctrl_x_mode_not_defined_yet(void)
315{
316 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
317}
318
319/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000320 * Return TRUE if currently in "normal" or "adding" insert completion matches
321 * state
322 */
323 int
324compl_status_adding(void)
325{
326 return compl_cont_status & CONT_ADDING;
327}
328
329/*
330 * Return TRUE if the completion pattern includes start of line, just for
331 * word-wise expansion.
332 */
333 int
334compl_status_sol(void)
335{
336 return compl_cont_status & CONT_SOL;
337}
338
339/*
340 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
341 */
342 int
343compl_status_local(void)
344{
345 return compl_cont_status & CONT_LOCAL;
346}
347
348/*
349 * Clear the completion status flags
350 */
351 void
352compl_status_clear(void)
353{
354 compl_cont_status = 0;
355}
356
357/*
358 * Return TRUE if completion is using the forward direction matches
359 */
360 static int
361compl_dir_forward(void)
362{
363 return compl_direction == FORWARD;
364}
365
366/*
367 * Return TRUE if currently showing forward completion matches
368 */
369 static int
370compl_shows_dir_forward(void)
371{
372 return compl_shows_dir == FORWARD;
373}
374
375/*
376 * Return TRUE if currently showing backward completion matches
377 */
378 static int
379compl_shows_dir_backward(void)
380{
381 return compl_shows_dir == BACKWARD;
382}
383
384/*
385 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100386 */
387 int
388has_compl_option(int dict_opt)
389{
390 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200391#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200393#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100394 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100395 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
396#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100397 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100398#endif
399 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100400 {
401 ctrl_x_mode = CTRL_X_NORMAL;
402 edit_submode = NULL;
403 msg_attr(dict_opt ? _("'dictionary' option is empty")
404 : _("'thesaurus' option is empty"),
405 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100406 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100407 {
408 vim_beep(BO_COMPL);
409 setcursor();
410 out_flush();
411#ifdef FEAT_EVAL
412 if (!get_vim_var_nr(VV_TESTING))
413#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100414 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100415 }
416 return FALSE;
417 }
418 return TRUE;
419}
420
421/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000422 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100423 * This depends on the current mode.
424 */
425 int
426vim_is_ctrl_x_key(int c)
427{
428 // Always allow ^R - let its results then be checked
429 if (c == Ctrl_R)
430 return TRUE;
431
432 // Accept <PageUp> and <PageDown> if the popup menu is visible.
433 if (ins_compl_pum_key(c))
434 return TRUE;
435
436 switch (ctrl_x_mode)
437 {
438 case 0: // Not in any CTRL-X mode
439 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
440 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200441 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100442 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
443 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
444 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
445 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
446 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200447 || c == Ctrl_S || c == Ctrl_K || c == 's'
448 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100449 case CTRL_X_SCROLL:
450 return (c == Ctrl_Y || c == Ctrl_E);
451 case CTRL_X_WHOLE_LINE:
452 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
453 case CTRL_X_FILES:
454 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_DICTIONARY:
456 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_THESAURUS:
458 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
459 case CTRL_X_TAGS:
460 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
461#ifdef FEAT_FIND_ID
462 case CTRL_X_PATH_PATTERNS:
463 return (c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_PATH_DEFINES:
465 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
466#endif
467 case CTRL_X_CMDLINE:
468 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
469 || c == Ctrl_X);
470#ifdef FEAT_COMPL_FUNC
471 case CTRL_X_FUNCTION:
472 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
473 case CTRL_X_OMNI:
474 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
475#endif
476 case CTRL_X_SPELL:
477 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
478 case CTRL_X_EVAL:
479 return (c == Ctrl_P || c == Ctrl_N);
480 }
481 internal_error("vim_is_ctrl_x_key()");
482 return FALSE;
483}
484
485/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000486 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000487 */
488 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000489match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000490{
491 return match->cp_flags & CP_ORIGINAL_TEXT;
492}
493
494/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000495 * Returns TRUE if "match" is the first match in the completion list.
496 */
497 static int
498is_first_match(compl_T *match)
499{
500 return match == compl_first_match;
501}
502
503/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100504 * Return TRUE when character "c" is part of the item currently being
505 * completed. Used to decide whether to abandon complete mode when the menu
506 * is visible.
507 */
508 int
509ins_compl_accept_char(int c)
510{
511 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
512 // When expanding an identifier only accept identifier chars.
513 return vim_isIDc(c);
514
515 switch (ctrl_x_mode)
516 {
517 case CTRL_X_FILES:
518 // When expanding file name only accept file name chars. But not
519 // path separators, so that "proto/<Tab>" expands files in
520 // "proto", not "proto/" as a whole
521 return vim_isfilec(c) && !vim_ispathsep(c);
522
523 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200524 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100525 case CTRL_X_OMNI:
526 // Command line and Omni completion can work with just about any
527 // printable character, but do stop at white space.
528 return vim_isprintc(c) && !VIM_ISWHITE(c);
529
530 case CTRL_X_WHOLE_LINE:
531 // For while line completion a space can be part of the line.
532 return vim_isprintc(c);
533 }
534 return vim_iswordc(c);
535}
536
537/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000538 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100539 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000540 */
541 static char_u *
542ins_compl_infercase_gettext(
543 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100544 int char_len,
545 int compl_char_len,
546 int min_len,
547 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548{
549 int *wca; // Wide character array.
550 char_u *p;
551 int i, c;
552 int has_lower = FALSE;
553 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100554 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000555
556 IObuff[0] = NUL;
557
558 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100559 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000560 if (wca == NULL)
561 return IObuff;
562
563 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100564 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (has_mbyte)
566 wca[i] = mb_ptr2char_adv(&p);
567 else
568 wca[i] = *(p++);
569
570 // Rule 1: Were any chars converted to lower?
571 p = compl_orig_text;
572 for (i = 0; i < min_len; ++i)
573 {
574 if (has_mbyte)
575 c = mb_ptr2char_adv(&p);
576 else
577 c = *(p++);
578 if (MB_ISLOWER(c))
579 {
580 has_lower = TRUE;
581 if (MB_ISUPPER(wca[i]))
582 {
583 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100584 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000585 wca[i] = MB_TOLOWER(wca[i]);
586 break;
587 }
588 }
589 }
590
591 // Rule 2: No lower case, 2nd consecutive letter converted to
592 // upper case.
593 if (!has_lower)
594 {
595 p = compl_orig_text;
596 for (i = 0; i < min_len; ++i)
597 {
598 if (has_mbyte)
599 c = mb_ptr2char_adv(&p);
600 else
601 c = *(p++);
602 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
603 {
604 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100605 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000606 wca[i] = MB_TOUPPER(wca[i]);
607 break;
608 }
609 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
610 }
611 }
612
613 // Copy the original case of the part we typed.
614 p = compl_orig_text;
615 for (i = 0; i < min_len; ++i)
616 {
617 if (has_mbyte)
618 c = mb_ptr2char_adv(&p);
619 else
620 c = *(p++);
621 if (MB_ISLOWER(c))
622 wca[i] = MB_TOLOWER(wca[i]);
623 else if (MB_ISUPPER(c))
624 wca[i] = MB_TOUPPER(wca[i]);
625 }
626
627 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000628 p = IObuff;
629 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100630 ga_init2(&gap, 1, 500);
631 while (i < char_len)
632 {
633 if (gap.ga_data != NULL)
634 {
635 if (ga_grow(&gap, 10) == FAIL)
636 {
637 ga_clear(&gap);
638 return (char_u *)"[failed]";
639 }
640 p = (char_u *)gap.ga_data + gap.ga_len;
641 if (has_mbyte)
642 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
643 else
644 {
645 *p = wca[i++];
646 ++gap.ga_len;
647 }
648 }
649 else if ((p - IObuff) + 6 >= IOSIZE)
650 {
651 // Multi-byte characters can occupy up to five bytes more than
652 // ASCII characters, and we also need one byte for NUL, so when
653 // getting to six bytes from the edge of IObuff switch to using a
654 // growarray. Add the character in the next round.
655 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100656 {
657 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100658 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100659 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100660 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100661 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100662 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 }
664 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000665 p += (*mb_char2bytes)(wca[i++], p);
666 else
667 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100668 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000669 vim_free(wca);
670
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100671 if (gap.ga_data != NULL)
672 {
673 *tofree = gap.ga_data;
674 return gap.ga_data;
675 }
676
677 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000678 return IObuff;
679}
680
681/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
683 * case of the originally typed text is used, and the case of the completed
684 * text is inferred, ie this tries to work out what case you probably wanted
685 * the rest of the word to be in -- webb
686 */
687 int
688ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200689 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100690 int len,
691 int icase,
692 char_u *fname,
693 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200694 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100697 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100698 int char_len; // count multi-byte characters
699 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100700 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200701 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 int res;
703 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100704
705 if (p_ic && curbuf->b_p_inf && len > 0)
706 {
707 // Infer case of completed part.
708
709 // Find actual length of completion.
710 if (has_mbyte)
711 {
712 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100713 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 while (*p != NUL)
715 {
716 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100717 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100718 }
719 }
720 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722
723 // Find actual length of original text.
724 if (has_mbyte)
725 {
726 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100727 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728 while (*p != NUL)
729 {
730 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100731 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732 }
733 }
734 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100735 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100739 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100740
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100741 str = ins_compl_infercase_gettext(str, char_len,
742 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100743 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200744 if (cont_s_ipos)
745 flags |= CP_CONT_S_IPOS;
746 if (icase)
747 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200748
glepnir508e7852024-07-25 21:39:08 +0200749 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, -1);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100750 vim_free(tofree);
751 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100752}
753
754/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000755 * Add a match to the list of matches. The arguments are:
756 * str - text of the match to add
757 * len - length of "str". If -1, then the length of "str" is
758 * computed.
759 * fname - file name to associate with this match.
760 * cptext - list of strings to use with this match (for abbr, menu, info
761 * and kind)
762 * user_data - user supplied data (any vim type) for this match
763 * cdir - match direction. If 0, use "compl_direction".
764 * flags_arg - match flags (cp_flags)
765 * adup - accept this match even if it is already present.
766 * If "cdir" is FORWARD, then the match is added after the current match.
767 * Otherwise, it is added before the current match.
768 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100769 * If the given string is already in the list of completions, then return
770 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
771 * maybe because alloc() returns NULL, then FAIL is returned.
772 */
773 static int
774ins_compl_add(
775 char_u *str,
776 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 char_u **cptext, // extra text for popup menu or NULL
779 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200781 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200782 int adup, // accept duplicate match
zeertzjq41008522024-07-27 13:21:49 +0200783 int user_hlattr)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784{
785 compl_T *match;
786 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200787 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788
Bram Moolenaarceb06192021-04-04 15:05:22 +0200789 if (flags & CP_FAST)
790 fast_breakcheck();
791 else
792 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100793 if (got_int)
794 return FAIL;
795 if (len < 0)
796 len = (int)STRLEN(str);
797
798 // If the same match is already present, don't add it.
799 if (compl_first_match != NULL && !adup)
800 {
801 match = compl_first_match;
802 do
803 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000804 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100806 && ((int)STRLEN(match->cp_str) <= len
807 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 return NOTDONE;
809 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000810 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100811 }
812
813 // Remove any popup menu before changing the list of matches.
814 ins_compl_del_pum();
815
816 // Allocate a new match structure.
817 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200818 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 if (match == NULL)
820 return FAIL;
821 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200822 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 match->cp_number = 0;
824 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
825 {
826 vim_free(match);
827 return FAIL;
828 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829
830 // match-fname is:
831 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200832 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833 // - NULL otherwise. --Acevedo
834 if (fname != NULL
835 && compl_curr_match != NULL
836 && compl_curr_match->cp_fname != NULL
837 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
838 match->cp_fname = compl_curr_match->cp_fname;
839 else if (fname != NULL)
840 {
841 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200842 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 }
844 else
845 match->cp_fname = NULL;
846 match->cp_flags = flags;
zeertzjq41008522024-07-27 13:21:49 +0200847 match->cp_user_hlattr = user_hlattr;
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)
854 if (cptext[i] != NULL && *cptext[i] != NUL)
855 match->cp_text[i] = vim_strsave(cptext[i]);
856 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100857#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100858 if (user_data != NULL)
859 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100860#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100861
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000862 // Link the new match structure after (FORWARD) or before (BACKWARD) the
863 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100864 if (compl_first_match == NULL)
865 match->cp_next = match->cp_prev = NULL;
866 else if (dir == FORWARD)
867 {
868 match->cp_next = compl_curr_match->cp_next;
869 match->cp_prev = compl_curr_match;
870 }
871 else // BACKWARD
872 {
873 match->cp_next = compl_curr_match;
874 match->cp_prev = compl_curr_match->cp_prev;
875 }
876 if (match->cp_next)
877 match->cp_next->cp_prev = match;
878 if (match->cp_prev)
879 match->cp_prev->cp_next = match;
880 else // if there's nothing before, it is the first match
881 compl_first_match = match;
882 compl_curr_match = match;
883
884 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200885 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100886 ins_compl_longest_match(match);
887
888 return OK;
889}
890
891/*
892 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200893 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100894 */
895 static int
896ins_compl_equal(compl_T *match, char_u *str, int len)
897{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200898 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200899 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200900 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100901 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
902 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
903}
904
905/*
906 * Reduce the longest common string for match "match".
907 */
908 static void
909ins_compl_longest_match(compl_T *match)
910{
911 char_u *p, *s;
912 int c1, c2;
913 int had_match;
914
915 if (compl_leader == NULL)
916 {
917 // First match, use it as a whole.
918 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000919 if (compl_leader == NULL)
920 return;
921
922 had_match = (curwin->w_cursor.col > compl_col);
923 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000924 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000925 ins_redraw(FALSE);
926
927 // When the match isn't there (to avoid matching itself) remove it
928 // again after redrawing.
929 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000932
933 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100934 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000935
936 // Reduce the text if this match differs from compl_leader.
937 p = compl_leader;
938 s = match->cp_str;
939 while (*p != NUL)
940 {
941 if (has_mbyte)
942 {
943 c1 = mb_ptr2char(p);
944 c2 = mb_ptr2char(s);
945 }
946 else
947 {
948 c1 = *p;
949 c2 = *s;
950 }
951 if ((match->cp_flags & CP_ICASE)
952 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
953 break;
954 if (has_mbyte)
955 {
956 MB_PTR_ADV(p);
957 MB_PTR_ADV(s);
958 }
959 else
960 {
961 ++p;
962 ++s;
963 }
964 }
965
966 if (*p != NUL)
967 {
968 // Leader was shortened, need to change the inserted text.
969 *p = NUL;
970 had_match = (curwin->w_cursor.col > compl_col);
971 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000972 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000973 ins_redraw(FALSE);
974
975 // When the match isn't there (to avoid matching itself) remove it
976 // again after redrawing.
977 if (!had_match)
978 ins_compl_delete();
979 }
980
981 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100982}
983
984/*
985 * Add an array of matches to the list of matches.
986 * Frees matches[].
987 */
988 static void
989ins_compl_add_matches(
990 int num_matches,
991 char_u **matches,
992 int icase)
993{
994 int i;
995 int add_r = OK;
996 int dir = compl_direction;
997
998 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100999 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
glepnir508e7852024-07-25 21:39:08 +02001000 CP_FAST | (icase ? CP_ICASE : 0), FALSE, -1)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001001 // if dir was BACKWARD then honor it just once
1002 dir = FORWARD;
1003 FreeWild(num_matches, matches);
1004}
1005
1006/*
1007 * Make the completion list cyclic.
1008 * Return the number of matches (excluding the original).
1009 */
1010 static int
1011ins_compl_make_cyclic(void)
1012{
1013 compl_T *match;
1014 int count = 0;
1015
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001016 if (compl_first_match == NULL)
1017 return 0;
1018
1019 // Find the end of the list.
1020 match = compl_first_match;
1021 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001022 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001023 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001024 match = match->cp_next;
1025 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001026 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001027 match->cp_next = compl_first_match;
1028 compl_first_match->cp_prev = match;
1029
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001030 return count;
1031}
1032
1033/*
1034 * Return whether there currently is a shown match.
1035 */
1036 int
1037ins_compl_has_shown_match(void)
1038{
1039 return compl_shown_match == NULL
1040 || compl_shown_match != compl_shown_match->cp_next;
1041}
1042
1043/*
1044 * Return whether the shown match is long enough.
1045 */
1046 int
1047ins_compl_long_shown_match(void)
1048{
1049 return (int)STRLEN(compl_shown_match->cp_str)
1050 > curwin->w_cursor.col - compl_col;
1051}
1052
1053/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001054 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001055 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001056 unsigned int
1057get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001058{
zeertzjq529b9ad2024-06-05 20:27:06 +02001059 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001060}
1061
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001062
1063// "compl_match_array" points the currently displayed list of entries in the
1064// popup menu. It is NULL when there is no popup menu.
1065static pumitem_T *compl_match_array = NULL;
1066static int compl_match_arraysize;
1067
1068/*
1069 * Update the screen and when there is any scrolling remove the popup menu.
1070 */
1071 static void
1072ins_compl_upd_pum(void)
1073{
1074 int h;
1075
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001076 if (compl_match_array == NULL)
1077 return;
1078
1079 h = curwin->w_cline_height;
1080 // Update the screen later, before drawing the popup menu over it.
1081 pum_call_update_screen();
1082 if (h != curwin->w_cline_height)
1083 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001084}
1085
1086/*
1087 * Remove any popup menu.
1088 */
1089 static void
1090ins_compl_del_pum(void)
1091{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001092 if (compl_match_array == NULL)
1093 return;
1094
1095 pum_undisplay();
1096 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001097}
1098
1099/*
1100 * Return TRUE if the popup menu should be displayed.
1101 */
1102 int
1103pum_wanted(void)
1104{
1105 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001106 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001107 return FALSE;
1108
1109 // The display looks bad on a B&W display.
1110 if (t_colors < 8
1111#ifdef FEAT_GUI
1112 && !gui.in_use
1113#endif
1114 )
1115 return FALSE;
1116 return TRUE;
1117}
1118
1119/*
1120 * Return TRUE if there are two or more matches to be shown in the popup menu.
1121 * One if 'completopt' contains "menuone".
1122 */
1123 static int
1124pum_enough_matches(void)
1125{
1126 compl_T *compl;
1127 int i;
1128
1129 // Don't display the popup menu if there are no matches or there is only
1130 // one (ignoring the original text).
1131 compl = compl_first_match;
1132 i = 0;
1133 do
1134 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001135 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001136 break;
1137 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001138 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001139
zeertzjq529b9ad2024-06-05 20:27:06 +02001140 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001141 return (i >= 1);
1142 return (i >= 2);
1143}
1144
Bram Moolenaar3075a452021-11-17 15:51:52 +00001145#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001146/*
1147 * Allocate Dict for the completed item.
1148 * { word, abbr, menu, kind, info }
1149 */
1150 static dict_T *
1151ins_compl_dict_alloc(compl_T *match)
1152{
1153 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1154
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001155 if (dict == NULL)
1156 return NULL;
1157
1158 dict_add_string(dict, "word", match->cp_str);
1159 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1160 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1161 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1162 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1163 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1164 dict_add_string(dict, "user_data", (char_u *)"");
1165 else
1166 dict_add_tv(dict, "user_data", &match->cp_user_data);
1167
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001168 return dict;
1169}
1170
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001171/*
1172 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1173 * completion menu is changed.
1174 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001175 static void
1176trigger_complete_changed_event(int cur)
1177{
1178 dict_T *v_event;
1179 dict_T *item;
1180 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001181 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001182
1183 if (recursive)
1184 return;
1185
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001186 if (cur < 0)
1187 item = dict_alloc();
1188 else
1189 item = ins_compl_dict_alloc(compl_curr_match);
1190 if (item == NULL)
1191 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001192 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001193 dict_add_dict(v_event, "completed_item", item);
1194 pum_set_event_info(v_event);
1195 dict_set_items_ro(v_event);
1196
1197 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001198 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001199 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001200 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 recursive = FALSE;
1202
Bram Moolenaar3075a452021-11-17 15:51:52 +00001203 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001204}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001205#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001206
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001207/*
glepnira218cc62024-06-03 19:32:39 +02001208 * pumitem qsort compare func
1209 */
1210 static int
zeertzjq8e567472024-06-14 20:04:42 +02001211ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001212{
1213 const int sa = (*(pumitem_T *)a).pum_score;
1214 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001215 const int ia = (*(pumitem_T *)a).pum_idx;
1216 const int ib = (*(pumitem_T *)b).pum_idx;
1217 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001218}
1219
1220/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001221 * Build a popup menu to show the completion matches.
1222 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1223 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001224 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001225 static int
1226ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001227{
1228 compl_T *compl;
1229 compl_T *shown_compl = NULL;
1230 int did_find_shown_match = FALSE;
1231 int shown_match_ok = FALSE;
1232 int i;
1233 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001234 int lead_len = 0;
glepnira218cc62024-06-03 19:32:39 +02001235 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001236 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001237 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1238 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001239
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001240 // Need to build the popup menu list.
1241 compl_match_arraysize = 0;
1242 compl = compl_first_match;
1243 if (compl_leader != NULL)
1244 lead_len = (int)STRLEN(compl_leader);
1245
1246 do
1247 {
zeertzjq551d8c32024-06-05 19:53:32 +02001248 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1249 // set the cp_score for later comparisons.
glepnira218cc62024-06-03 19:32:39 +02001250 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1251 compl->cp_score = fuzzy_match_str(compl->cp_str, compl_leader);
1252
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001253 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001254 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001255 || ins_compl_equal(compl, compl_leader, lead_len)
1256 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001257 ++compl_match_arraysize;
1258 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001259 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001260
1261 if (compl_match_arraysize == 0)
1262 return -1;
1263
1264 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1265 if (compl_match_array == NULL)
1266 return -1;
1267
1268 // If the current match is the original text don't find the first
1269 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001270 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001271 shown_match_ok = TRUE;
1272
glepnir53387c52024-05-27 15:11:01 +02001273 if (compl_leader != NULL
1274 && STRCMP(compl_leader, compl_orig_text) == 0
1275 && shown_match_ok == FALSE)
1276 compl_shown_match = compl_no_select ? compl_first_match
1277 : compl_first_match->cp_next;
1278
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001279 i = 0;
1280 compl = compl_first_match;
1281 do
1282 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001283 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001284 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001285 || ins_compl_equal(compl, compl_leader, lead_len)
1286 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001287 {
glepnira218cc62024-06-03 19:32:39 +02001288 if (!shown_match_ok && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001289 {
1290 if (compl == compl_shown_match || did_find_shown_match)
1291 {
1292 // This item is the shown match or this is the
1293 // first displayed item after the shown match.
1294 compl_shown_match = compl;
1295 did_find_shown_match = TRUE;
1296 shown_match_ok = TRUE;
1297 }
1298 else
1299 // Remember this displayed match for when the
1300 // shown match is just below it.
1301 shown_compl = compl;
1302 cur = i;
1303 }
glepnira218cc62024-06-03 19:32:39 +02001304 else if (compl_fuzzy_match)
1305 {
glepnirf94c9c42024-06-14 21:11:56 +02001306 if (i == 0)
glepnir105f7412024-06-15 15:32:22 +02001307 shown_compl = compl;
glepnirdca57fb2024-06-04 22:01:21 +02001308 // Update the maximum fuzzy score and the shown match
1309 // if the current item's score is higher
glepnira218cc62024-06-03 19:32:39 +02001310 if (compl->cp_score > max_fuzzy_score)
1311 {
1312 did_find_shown_match = TRUE;
1313 max_fuzzy_score = compl->cp_score;
1314 compl_shown_match = compl;
glepnir65407ce2024-07-06 16:09:19 +02001315 }
1316
1317 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1318 {
1319 cur = i;
glepnira218cc62024-06-03 19:32:39 +02001320 shown_match_ok = TRUE;
1321 }
1322
glepnirdca57fb2024-06-04 22:01:21 +02001323 // If there is no "no select" condition and the max fuzzy
1324 // score is positive, or there is no completion leader or the
1325 // leader length is zero, mark the shown match as valid and
1326 // reset the current index.
glepnira218cc62024-06-03 19:32:39 +02001327 if (!compl_no_select
1328 && (max_fuzzy_score > 0
1329 || (compl_leader == NULL || lead_len == 0)))
1330 {
glepnirf94c9c42024-06-14 21:11:56 +02001331 if (match_at_original_text(compl_shown_match))
glepnir105f7412024-06-15 15:32:22 +02001332 compl_shown_match = shown_compl;
glepnira218cc62024-06-03 19:32:39 +02001333 }
1334 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001335
1336 if (compl->cp_text[CPT_ABBR] != NULL)
1337 compl_match_array[i].pum_text =
1338 compl->cp_text[CPT_ABBR];
1339 else
1340 compl_match_array[i].pum_text = compl->cp_str;
1341 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1342 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
glepnira218cc62024-06-03 19:32:39 +02001343 compl_match_array[i].pum_score = compl->cp_score;
zeertzjq41008522024-07-27 13:21:49 +02001344 compl_match_array[i].pum_user_hlattr = compl->cp_user_hlattr;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001345 if (compl->cp_text[CPT_MENU] != NULL)
1346 compl_match_array[i++].pum_extra =
1347 compl->cp_text[CPT_MENU];
1348 else
1349 compl_match_array[i++].pum_extra = compl->cp_fname;
1350 }
1351
glepnira218cc62024-06-03 19:32:39 +02001352 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001353 {
1354 did_find_shown_match = TRUE;
1355
1356 // When the original text is the shown match don't set
1357 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001358 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001359 shown_match_ok = TRUE;
1360
1361 if (!shown_match_ok && shown_compl != NULL)
1362 {
1363 // The shown match isn't displayed, set it to the
1364 // previously displayed match.
1365 compl_shown_match = shown_compl;
1366 shown_match_ok = TRUE;
1367 }
1368 }
1369 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001370 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001371
glepnira218cc62024-06-03 19:32:39 +02001372 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001373 {
1374 for (i = 0; i < compl_match_arraysize; i++)
1375 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001376 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001377 qsort(compl_match_array, (size_t)compl_match_arraysize,
1378 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001379 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001380 }
glepnira218cc62024-06-03 19:32:39 +02001381
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001382 if (!shown_match_ok) // no displayed match at all
1383 cur = -1;
1384
1385 return cur;
1386}
1387
1388/*
1389 * Show the popup menu for the list of matches.
1390 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1391 */
1392 void
1393ins_compl_show_pum(void)
1394{
1395 int i;
1396 int cur = -1;
1397 colnr_T col;
1398
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 if (!pum_wanted() || !pum_enough_matches())
1400 return;
1401
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001402 // Update the screen later, before drawing the popup menu over it.
1403 pum_call_update_screen();
1404
1405 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001406 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001407 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001408 else
1409 {
1410 // popup menu already exists, only need to find the current item.
1411 for (i = 0; i < compl_match_arraysize; ++i)
1412 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1413 || compl_match_array[i].pum_text
1414 == compl_shown_match->cp_text[CPT_ABBR])
1415 {
1416 cur = i;
1417 break;
1418 }
1419 }
1420
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001421 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001422 {
1423#ifdef FEAT_EVAL
1424 if (compl_started && has_completechanged())
1425 trigger_complete_changed_event(cur);
1426#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001427 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001428 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001429
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001430 // In Replace mode when a $ is displayed at the end of the line only
1431 // part of the screen would be updated. We do need to redraw here.
1432 dollar_vcol = -1;
1433
1434 // Compute the screen column of the start of the completed text.
1435 // Use the cursor to get all wrapping and other settings right.
1436 col = curwin->w_cursor.col;
1437 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001438 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001439 pum_display(compl_match_array, compl_match_arraysize, cur);
1440 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001441
glepnircbb46b42024-02-03 18:11:13 +01001442 // After adding leader, set the current match to shown match.
1443 if (compl_started && compl_curr_match != compl_shown_match)
1444 compl_curr_match = compl_shown_match;
1445
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001446#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001447 if (has_completechanged())
1448 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001449#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001450}
1451
1452#define DICT_FIRST (1) // use just first element in "dict"
1453#define DICT_EXACT (2) // "dict" is the exact name of a file
1454
1455/*
glepnir40c1c332024-06-11 19:37:04 +02001456 * Get current completion leader
1457 */
1458 char_u *
1459ins_compl_leader(void)
1460{
glepnirf1891382024-06-17 18:35:25 +02001461 return compl_leader != NULL ? compl_leader : compl_orig_text;
glepnir40c1c332024-06-11 19:37:04 +02001462}
1463
1464/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001465 * Add any identifiers that match the given pattern "pat" in the list of
1466 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001467 */
1468 static void
1469ins_compl_dictionaries(
1470 char_u *dict_start,
1471 char_u *pat,
1472 int flags, // DICT_FIRST and/or DICT_EXACT
1473 int thesaurus) // Thesaurus completion
1474{
1475 char_u *dict = dict_start;
1476 char_u *ptr;
1477 char_u *buf;
1478 regmatch_T regmatch;
1479 char_u **files;
1480 int count;
1481 int save_p_scs;
1482 int dir = compl_direction;
1483
1484 if (*dict == NUL)
1485 {
1486#ifdef FEAT_SPELL
1487 // When 'dictionary' is empty and spell checking is enabled use
1488 // "spell".
1489 if (!thesaurus && curwin->w_p_spell)
1490 dict = (char_u *)"spell";
1491 else
1492#endif
1493 return;
1494 }
1495
1496 buf = alloc(LSIZE);
1497 if (buf == NULL)
1498 return;
1499 regmatch.regprog = NULL; // so that we can goto theend
1500
1501 // If 'infercase' is set, don't use 'smartcase' here
1502 save_p_scs = p_scs;
1503 if (curbuf->b_p_inf)
1504 p_scs = FALSE;
1505
1506 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1507 // to only match at the start of a line. Otherwise just match the
1508 // pattern. Also need to double backslashes.
1509 if (ctrl_x_mode_line_or_eval())
1510 {
1511 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1512 size_t len;
1513
1514 if (pat_esc == NULL)
1515 goto theend;
1516 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001517 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001518 if (ptr == NULL)
1519 {
1520 vim_free(pat_esc);
1521 goto theend;
1522 }
1523 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1524 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1525 vim_free(pat_esc);
1526 vim_free(ptr);
1527 }
1528 else
1529 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001530 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001531 if (regmatch.regprog == NULL)
1532 goto theend;
1533 }
1534
1535 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1536 regmatch.rm_ic = ignorecase(pat);
1537 while (*dict != NUL && !got_int && !compl_interrupted)
1538 {
1539 // copy one dictionary file name into buf
1540 if (flags == DICT_EXACT)
1541 {
1542 count = 1;
1543 files = &dict;
1544 }
1545 else
1546 {
1547 // Expand wildcards in the dictionary name, but do not allow
1548 // backticks (for security, the 'dict' option may have been set in
1549 // a modeline).
1550 copy_option_part(&dict, buf, LSIZE, ",");
1551# ifdef FEAT_SPELL
1552 if (!thesaurus && STRCMP(buf, "spell") == 0)
1553 count = -1;
1554 else
1555# endif
1556 if (vim_strchr(buf, '`') != NULL
1557 || expand_wildcards(1, &buf, &count, &files,
1558 EW_FILE|EW_SILENT) != OK)
1559 count = 0;
1560 }
1561
1562# ifdef FEAT_SPELL
1563 if (count == -1)
1564 {
1565 // Complete from active spelling. Skip "\<" in the pattern, we
1566 // don't use it as a RE.
1567 if (pat[0] == '\\' && pat[1] == '<')
1568 ptr = pat + 2;
1569 else
1570 ptr = pat;
1571 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1572 }
1573 else
1574# endif
1575 if (count > 0) // avoid warning for using "files" uninit
1576 {
1577 ins_compl_files(count, files, thesaurus, flags,
1578 &regmatch, buf, &dir);
1579 if (flags != DICT_EXACT)
1580 FreeWild(count, files);
1581 }
1582 if (flags != 0)
1583 break;
1584 }
1585
1586theend:
1587 p_scs = save_p_scs;
1588 vim_regfree(regmatch.regprog);
1589 vim_free(buf);
1590}
1591
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001592/*
1593 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1594 * skipping the word at 'skip_word'. Returns OK on success.
1595 */
1596 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001597thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001598 char_u *fname,
1599 char_u **buf_arg,
1600 int dir,
1601 char_u *skip_word)
1602{
1603 int status = OK;
1604 char_u *ptr;
1605 char_u *wstart;
1606
1607 // Add the other matches on the line
1608 ptr = *buf_arg;
1609 while (!got_int)
1610 {
1611 // Find start of the next word. Skip white
1612 // space and punctuation.
1613 ptr = find_word_start(ptr);
1614 if (*ptr == NUL || *ptr == NL)
1615 break;
1616 wstart = ptr;
1617
1618 // Find end of the word.
1619 if (has_mbyte)
1620 // Japanese words may have characters in
1621 // different classes, only separate words
1622 // with single-byte non-word characters.
1623 while (*ptr != NUL)
1624 {
1625 int l = (*mb_ptr2len)(ptr);
1626
1627 if (l < 2 && !vim_iswordc(*ptr))
1628 break;
1629 ptr += l;
1630 }
1631 else
1632 ptr = find_word_end(ptr);
1633
1634 // Add the word. Skip the regexp match.
1635 if (wstart != skip_word)
1636 {
1637 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1638 fname, dir, FALSE);
1639 if (status == FAIL)
1640 break;
1641 }
1642 }
1643
1644 *buf_arg = ptr;
1645 return status;
1646}
1647
1648/*
1649 * Process "count" dictionary/thesaurus "files" and add the text matching
1650 * "regmatch".
1651 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001652 static void
1653ins_compl_files(
1654 int count,
1655 char_u **files,
1656 int thesaurus,
1657 int flags,
1658 regmatch_T *regmatch,
1659 char_u *buf,
1660 int *dir)
1661{
1662 char_u *ptr;
1663 int i;
1664 FILE *fp;
1665 int add_r;
1666
1667 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1668 {
1669 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001670 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001671 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001672 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001673 vim_snprintf((char *)IObuff, IOSIZE,
1674 _("Scanning dictionary: %s"), (char *)files[i]);
1675 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1676 }
1677
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001678 if (fp == NULL)
1679 continue;
1680
1681 // Read dictionary file line by line.
1682 // Check each line for a match.
1683 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001685 ptr = buf;
1686 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001687 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001688 ptr = regmatch->startp[0];
1689 if (ctrl_x_mode_line_or_eval())
1690 ptr = find_line_end(ptr);
1691 else
1692 ptr = find_word_end(ptr);
1693 add_r = ins_compl_add_infercase(regmatch->startp[0],
1694 (int)(ptr - regmatch->startp[0]),
1695 p_ic, files[i], *dir, FALSE);
1696 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001697 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001698 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001699 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001700 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001701 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001702 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001703 if (add_r == OK)
1704 // if dir was BACKWARD then honor it just once
1705 *dir = FORWARD;
1706 else if (add_r == FAIL)
1707 break;
1708 // avoid expensive call to vim_regexec() when at end
1709 // of line
1710 if (*ptr == '\n' || got_int)
1711 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001712 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001713 line_breakcheck();
1714 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001715 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001716 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001717 }
1718}
1719
1720/*
1721 * Find the start of the next word.
1722 * Returns a pointer to the first char of the word. Also stops at a NUL.
1723 */
1724 char_u *
1725find_word_start(char_u *ptr)
1726{
1727 if (has_mbyte)
1728 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1729 ptr += (*mb_ptr2len)(ptr);
1730 else
1731 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1732 ++ptr;
1733 return ptr;
1734}
1735
1736/*
1737 * Find the end of the word. Assumes it starts inside a word.
1738 * Returns a pointer to just after the word.
1739 */
1740 char_u *
1741find_word_end(char_u *ptr)
1742{
1743 int start_class;
1744
1745 if (has_mbyte)
1746 {
1747 start_class = mb_get_class(ptr);
1748 if (start_class > 1)
1749 while (*ptr != NUL)
1750 {
1751 ptr += (*mb_ptr2len)(ptr);
1752 if (mb_get_class(ptr) != start_class)
1753 break;
1754 }
1755 }
1756 else
1757 while (vim_iswordc(*ptr))
1758 ++ptr;
1759 return ptr;
1760}
1761
1762/*
1763 * Find the end of the line, omitting CR and NL at the end.
1764 * Returns a pointer to just after the line.
1765 */
1766 static char_u *
1767find_line_end(char_u *ptr)
1768{
1769 char_u *s;
1770
1771 s = ptr + STRLEN(ptr);
1772 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1773 --s;
1774 return s;
1775}
1776
1777/*
1778 * Free the list of completions
1779 */
1780 static void
1781ins_compl_free(void)
1782{
1783 compl_T *match;
1784 int i;
1785
1786 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001787 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001788 VIM_CLEAR(compl_leader);
1789
1790 if (compl_first_match == NULL)
1791 return;
1792
1793 ins_compl_del_pum();
1794 pum_clear();
1795
1796 compl_curr_match = compl_first_match;
1797 do
1798 {
1799 match = compl_curr_match;
1800 compl_curr_match = compl_curr_match->cp_next;
1801 vim_free(match->cp_str);
1802 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001803 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001804 vim_free(match->cp_fname);
1805 for (i = 0; i < CPT_COUNT; ++i)
1806 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001807#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001808 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001809#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001810 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001811 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001812 compl_first_match = compl_curr_match = NULL;
1813 compl_shown_match = NULL;
1814 compl_old_match = NULL;
1815}
1816
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001817/*
1818 * Reset/clear the completion state.
1819 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 void
1821ins_compl_clear(void)
1822{
1823 compl_cont_status = 0;
1824 compl_started = FALSE;
1825 compl_matches = 0;
1826 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001827 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001828 VIM_CLEAR(compl_leader);
1829 edit_submode_extra = NULL;
1830 VIM_CLEAR(compl_orig_text);
1831 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001832#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001833 // clear v:completed_item
1834 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001835#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001836}
1837
1838/*
1839 * Return TRUE when Insert completion is active.
1840 */
1841 int
1842ins_compl_active(void)
1843{
1844 return compl_started;
1845}
1846
1847/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001848 * Selected one of the matches. When FALSE the match was edited or using the
1849 * longest common string.
1850 */
1851 int
1852ins_compl_used_match(void)
1853{
1854 return compl_used_match;
1855}
1856
1857/*
1858 * Initialize get longest common string.
1859 */
1860 void
1861ins_compl_init_get_longest(void)
1862{
1863 compl_get_longest = FALSE;
1864}
1865
1866/*
1867 * Returns TRUE when insert completion is interrupted.
1868 */
1869 int
1870ins_compl_interrupted(void)
1871{
1872 return compl_interrupted;
1873}
1874
1875/*
1876 * Returns TRUE if the <Enter> key selects a match in the completion popup
1877 * menu.
1878 */
1879 int
1880ins_compl_enter_selects(void)
1881{
1882 return compl_enter_selects;
1883}
1884
1885/*
1886 * Return the column where the text starts that is being completed
1887 */
1888 colnr_T
1889ins_compl_col(void)
1890{
1891 return compl_col;
1892}
1893
1894/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001895 * Return the length in bytes of the text being completed
1896 */
1897 int
1898ins_compl_len(void)
1899{
1900 return compl_length;
1901}
1902
1903/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001904 * Delete one character before the cursor and show the subset of the matches
1905 * that match the word that is now before the cursor.
1906 * Returns the character to be used, NUL if the work is done and another char
1907 * to be got from the user.
1908 */
1909 int
1910ins_compl_bs(void)
1911{
1912 char_u *line;
1913 char_u *p;
1914
1915 line = ml_get_curline();
1916 p = line + curwin->w_cursor.col;
1917 MB_PTR_BACK(line, p);
1918
1919 // Stop completion when the whole word was deleted. For Omni completion
1920 // allow the word to be deleted, we won't match everything.
1921 // Respect the 'backspace' option.
1922 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001923 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1924 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001925 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1926 - compl_length < 0))
1927 return K_BS;
1928
1929 // Deleted more than what was used to find matches or didn't finish
1930 // finding all matches: need to look for matches all over again.
1931 if (curwin->w_cursor.col <= compl_col + compl_length
1932 || ins_compl_need_restart())
1933 ins_compl_restart();
1934
1935 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001936 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001937 if (compl_leader == NULL)
1938 return K_BS;
1939
1940 ins_compl_new_leader();
1941 if (compl_shown_match != NULL)
1942 // Make sure current match is not a hidden item.
1943 compl_curr_match = compl_shown_match;
1944 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001945}
1946
1947/*
1948 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1949 * be called.
1950 */
1951 static int
1952ins_compl_need_restart(void)
1953{
1954 // Return TRUE if we didn't complete finding matches or when the
1955 // 'completefunc' returned "always" in the "refresh" dictionary item.
1956 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001957 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001958 && compl_opt_refresh_always);
1959}
1960
1961/*
1962 * Called after changing "compl_leader".
1963 * Show the popup menu with a different set of matches.
1964 * May also search for matches again if the previous search was interrupted.
1965 */
1966 static void
1967ins_compl_new_leader(void)
1968{
1969 ins_compl_del_pum();
1970 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001971 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001972 compl_used_match = FALSE;
1973
1974 if (compl_started)
1975 ins_compl_set_original_text(compl_leader);
1976 else
1977 {
1978#ifdef FEAT_SPELL
1979 spell_bad_len = 0; // need to redetect bad word
1980#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001981 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001982 // the popup menu display the changed text before the cursor. Set
1983 // "compl_restarting" to avoid that the first match is inserted.
1984 pum_call_update_screen();
1985#ifdef FEAT_GUI
1986 if (gui.in_use)
1987 {
1988 // Show the cursor after the match, not after the redrawn text.
1989 setcursor();
1990 out_flush_cursor(FALSE, FALSE);
1991 }
1992#endif
1993 compl_restarting = TRUE;
1994 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1995 compl_cont_status = 0;
1996 compl_restarting = FALSE;
1997 }
1998
1999 compl_enter_selects = !compl_used_match;
2000
2001 // Show the popup menu with a different set of matches.
2002 ins_compl_show_pum();
2003
2004 // Don't let Enter select the original text when there is no popup menu.
2005 if (compl_match_array == NULL)
2006 compl_enter_selects = FALSE;
2007}
2008
2009/*
2010 * Return the length of the completion, from the completion start column to
2011 * the cursor column. Making sure it never goes below zero.
2012 */
2013 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002014get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002015{
2016 int off = (int)curwin->w_cursor.col - (int)compl_col;
2017
2018 if (off < 0)
2019 return 0;
2020 return off;
2021}
2022
2023/*
2024 * Append one character to the match leader. May reduce the number of
2025 * matches.
2026 */
2027 void
2028ins_compl_addleader(int c)
2029{
2030 int cc;
2031
2032 if (stop_arrow() == FAIL)
2033 return;
2034 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2035 {
2036 char_u buf[MB_MAXBYTES + 1];
2037
2038 (*mb_char2bytes)(c, buf);
2039 buf[cc] = NUL;
2040 ins_char_bytes(buf, cc);
2041 if (compl_opt_refresh_always)
2042 AppendToRedobuff(buf);
2043 }
2044 else
2045 {
2046 ins_char(c);
2047 if (compl_opt_refresh_always)
2048 AppendCharToRedobuff(c);
2049 }
2050
2051 // If we didn't complete finding matches we must search again.
2052 if (ins_compl_need_restart())
2053 ins_compl_restart();
2054
2055 // When 'always' is set, don't reset compl_leader. While completing,
2056 // cursor doesn't point original position, changing compl_leader would
2057 // break redo.
2058 if (!compl_opt_refresh_always)
2059 {
2060 vim_free(compl_leader);
2061 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002062 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002063 if (compl_leader != NULL)
2064 ins_compl_new_leader();
2065 }
2066}
2067
2068/*
2069 * Setup for finding completions again without leaving CTRL-X mode. Used when
2070 * BS or a key was typed while still searching for matches.
2071 */
2072 static void
2073ins_compl_restart(void)
2074{
2075 ins_compl_free();
2076 compl_started = FALSE;
2077 compl_matches = 0;
2078 compl_cont_status = 0;
2079 compl_cont_mode = 0;
2080}
2081
2082/*
2083 * Set the first match, the original text.
2084 */
2085 static void
2086ins_compl_set_original_text(char_u *str)
2087{
2088 char_u *p;
2089
2090 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002091 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2092 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002093 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002094 {
2095 p = vim_strsave(str);
2096 if (p != NULL)
2097 {
2098 vim_free(compl_first_match->cp_str);
2099 compl_first_match->cp_str = p;
2100 }
2101 }
2102 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002103 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002104 {
2105 p = vim_strsave(str);
2106 if (p != NULL)
2107 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002108 vim_free(compl_first_match->cp_prev->cp_str);
2109 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002110 }
2111 }
2112}
2113
2114/*
2115 * Append one character to the match leader. May reduce the number of
2116 * matches.
2117 */
2118 void
2119ins_compl_addfrommatch(void)
2120{
2121 char_u *p;
2122 int len = (int)curwin->w_cursor.col - (int)compl_col;
2123 int c;
2124 compl_T *cp;
2125
2126 p = compl_shown_match->cp_str;
2127 if ((int)STRLEN(p) <= len) // the match is too short
2128 {
2129 // When still at the original match use the first entry that matches
2130 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002131 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002132 return;
2133
2134 p = NULL;
2135 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002136 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002137 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002138 if (compl_leader == NULL
2139 || ins_compl_equal(cp, compl_leader,
2140 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002142 p = cp->cp_str;
2143 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002144 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002145 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002146 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002147 return;
2148 }
2149 p += len;
2150 c = PTR2CHAR(p);
2151 ins_compl_addleader(c);
2152}
2153
2154/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002155 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002156 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2157 * compl_cont_mode and compl_cont_status.
2158 * Returns TRUE when the character is not to be inserted.
2159 */
2160 static int
2161set_ctrl_x_mode(int c)
2162{
2163 int retval = FALSE;
2164
2165 switch (c)
2166 {
2167 case Ctrl_E:
2168 case Ctrl_Y:
2169 // scroll the window one line up or down
2170 ctrl_x_mode = CTRL_X_SCROLL;
2171 if (!(State & REPLACE_FLAG))
2172 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2173 else
2174 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2175 edit_submode_pre = NULL;
2176 showmode();
2177 break;
2178 case Ctrl_L:
2179 // complete whole line
2180 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2181 break;
2182 case Ctrl_F:
2183 // complete filenames
2184 ctrl_x_mode = CTRL_X_FILES;
2185 break;
2186 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002187 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002188 ctrl_x_mode = CTRL_X_DICTIONARY;
2189 break;
2190 case Ctrl_R:
2191 // Register insertion without exiting CTRL-X mode
2192 // Simply allow ^R to happen without affecting ^X mode
2193 break;
2194 case Ctrl_T:
2195 // complete words from a thesaurus
2196 ctrl_x_mode = CTRL_X_THESAURUS;
2197 break;
2198#ifdef FEAT_COMPL_FUNC
2199 case Ctrl_U:
2200 // user defined completion
2201 ctrl_x_mode = CTRL_X_FUNCTION;
2202 break;
2203 case Ctrl_O:
2204 // omni completion
2205 ctrl_x_mode = CTRL_X_OMNI;
2206 break;
2207#endif
2208 case 's':
2209 case Ctrl_S:
2210 // complete spelling suggestions
2211 ctrl_x_mode = CTRL_X_SPELL;
2212#ifdef FEAT_SPELL
2213 ++emsg_off; // Avoid getting the E756 error twice.
2214 spell_back_to_badword();
2215 --emsg_off;
2216#endif
2217 break;
2218 case Ctrl_RSB:
2219 // complete tag names
2220 ctrl_x_mode = CTRL_X_TAGS;
2221 break;
2222#ifdef FEAT_FIND_ID
2223 case Ctrl_I:
2224 case K_S_TAB:
2225 // complete keywords from included files
2226 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2227 break;
2228 case Ctrl_D:
2229 // complete definitions from included files
2230 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2231 break;
2232#endif
2233 case Ctrl_V:
2234 case Ctrl_Q:
2235 // complete vim commands
2236 ctrl_x_mode = CTRL_X_CMDLINE;
2237 break;
2238 case Ctrl_Z:
2239 // stop completion
2240 ctrl_x_mode = CTRL_X_NORMAL;
2241 edit_submode = NULL;
2242 showmode();
2243 retval = TRUE;
2244 break;
2245 case Ctrl_P:
2246 case Ctrl_N:
2247 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2248 // just started ^X mode, or there were enough ^X's to cancel
2249 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2250 // do normal expansion when interrupting a different mode (say
2251 // ^X^F^X^P or ^P^X^X^P, see below)
2252 // nothing changes if interrupting mode 0, (eg, the flag
2253 // doesn't change when going to ADDING mode -- Acevedo
2254 if (!(compl_cont_status & CONT_INTRPT))
2255 compl_cont_status |= CONT_LOCAL;
2256 else if (compl_cont_mode != 0)
2257 compl_cont_status &= ~CONT_LOCAL;
2258 // FALLTHROUGH
2259 default:
2260 // If we have typed at least 2 ^X's... for modes != 0, we set
2261 // compl_cont_status = 0 (eg, as if we had just started ^X
2262 // mode).
2263 // For mode 0, we set "compl_cont_mode" to an impossible
2264 // value, in both cases ^X^X can be used to restart the same
2265 // mode (avoiding ADDING mode).
2266 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2267 // 'complete' and local ^P expansions respectively.
2268 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2269 // mode -- Acevedo
2270 if (c == Ctrl_X)
2271 {
2272 if (compl_cont_mode != 0)
2273 compl_cont_status = 0;
2274 else
2275 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2276 }
2277 ctrl_x_mode = CTRL_X_NORMAL;
2278 edit_submode = NULL;
2279 showmode();
2280 break;
2281 }
2282
2283 return retval;
2284}
2285
2286/*
2287 * Stop insert completion mode
2288 */
2289 static int
2290ins_compl_stop(int c, int prev_mode, int retval)
2291{
2292 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002293 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002294
2295 // Get here when we have finished typing a sequence of ^N and
2296 // ^P or other completion characters in CTRL-X mode. Free up
2297 // memory that was used, and make sure we can redo the insert.
2298 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2299 {
2300 // If any of the original typed text has been changed, eg when
2301 // ignorecase is set, we must add back-spaces to the redo
2302 // buffer. We add as few as necessary to delete just the part
2303 // of the original text that has changed.
2304 // When using the longest match, edited the match or used
2305 // CTRL-E then don't use the current match.
2306 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2307 ptr = compl_curr_match->cp_str;
2308 else
2309 ptr = NULL;
2310 ins_compl_fixRedoBufForLeader(ptr);
2311 }
2312
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002313 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002314
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002315 // When completing whole lines: fix indent for 'cindent'.
2316 // Otherwise, break line if it's too long.
2317 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2318 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002319 // re-indent the current line
2320 if (want_cindent)
2321 {
2322 do_c_expr_indent();
2323 want_cindent = FALSE; // don't do it again
2324 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002325 }
2326 else
2327 {
2328 int prev_col = curwin->w_cursor.col;
2329
2330 // put the cursor on the last char, for 'tw' formatting
2331 if (prev_col > 0)
2332 dec_cursor();
2333 // only format when something was inserted
2334 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2335 insertchar(NUL, 0, -1);
2336 if (prev_col > 0
2337 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2338 inc_cursor();
2339 }
2340
2341 // If the popup menu is displayed pressing CTRL-Y means accepting
2342 // the selection without inserting anything. When
2343 // compl_enter_selects is set the Enter key does the same.
2344 if ((c == Ctrl_Y || (compl_enter_selects
2345 && (c == CAR || c == K_KENTER || c == NL)))
2346 && pum_visible())
2347 retval = TRUE;
2348
2349 // CTRL-E means completion is Ended, go back to the typed text.
2350 // but only do this, if the Popup is still visible
2351 if (c == Ctrl_E)
2352 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002353 char_u *p = NULL;
2354
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002355 ins_compl_delete();
2356 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002357 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002358 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002359 p = compl_orig_text;
2360 if (p != NULL)
2361 {
2362 int compl_len = get_compl_len();
2363 int len = (int)STRLEN(p);
2364
2365 if (len > compl_len)
2366 ins_bytes_len(p + compl_len, len - compl_len);
2367 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002368 retval = TRUE;
2369 }
2370
2371 auto_format(FALSE, TRUE);
2372
2373 // Trigger the CompleteDonePre event to give scripts a chance to
2374 // act upon the completion before clearing the info, and restore
2375 // ctrl_x_mode, so that complete_info() can be used.
2376 ctrl_x_mode = prev_mode;
2377 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2378
2379 ins_compl_free();
2380 compl_started = FALSE;
2381 compl_matches = 0;
2382 if (!shortmess(SHM_COMPLETIONMENU))
2383 msg_clr_cmdline(); // necessary for "noshowmode"
2384 ctrl_x_mode = CTRL_X_NORMAL;
2385 compl_enter_selects = FALSE;
2386 if (edit_submode != NULL)
2387 {
2388 edit_submode = NULL;
2389 showmode();
2390 }
2391
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002392 if (c == Ctrl_C && cmdwin_type != 0)
2393 // Avoid the popup menu remains displayed when leaving the
2394 // command line window.
2395 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002396 // Indent now if a key was typed that is in 'cinkeys'.
2397 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2398 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002399 // Trigger the CompleteDone event to give scripts a chance to act
2400 // upon the end of completion.
2401 ins_apply_autocmds(EVENT_COMPLETEDONE);
2402
2403 return retval;
2404}
2405
2406/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002407 * Prepare for Insert mode completion, or stop it.
2408 * Called just after typing a character in Insert mode.
2409 * Returns TRUE when the character is not to be inserted;
2410 */
2411 int
2412ins_compl_prep(int c)
2413{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002414 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002415 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002416
2417 // Forget any previous 'special' messages if this is actually
2418 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2419 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2420 edit_submode_extra = NULL;
2421
zeertzjq440d4cb2023-03-02 17:51:32 +00002422 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002423 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002424 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002425 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002426 return retval;
2427
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002428#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002429 // Ignore mouse events in a popup window
2430 if (is_mouse_key(c))
2431 {
2432 // Ignore drag and release events, the position does not need to be in
2433 // the popup and it may have just closed.
2434 if (c == K_LEFTRELEASE
2435 || c == K_LEFTRELEASE_NM
2436 || c == K_MIDDLERELEASE
2437 || c == K_RIGHTRELEASE
2438 || c == K_X1RELEASE
2439 || c == K_X2RELEASE
2440 || c == K_LEFTDRAG
2441 || c == K_MIDDLEDRAG
2442 || c == K_RIGHTDRAG
2443 || c == K_X1DRAG
2444 || c == K_X2DRAG)
2445 return retval;
2446 if (popup_visible)
2447 {
2448 int row = mouse_row;
2449 int col = mouse_col;
2450 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2451
2452 if (wp != NULL && WIN_IS_POPUP(wp))
2453 return retval;
2454 }
2455 }
2456#endif
2457
zeertzjqdca29d92021-08-31 19:12:51 +02002458 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2459 {
2460 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2461 || !vim_is_ctrl_x_key(c))
2462 {
2463 // Not starting another completion mode.
2464 ctrl_x_mode = CTRL_X_CMDLINE;
2465
2466 // CTRL-X CTRL-Z should stop completion without inserting anything
2467 if (c == Ctrl_Z)
2468 retval = TRUE;
2469 }
2470 else
2471 {
2472 ctrl_x_mode = CTRL_X_CMDLINE;
2473
2474 // Other CTRL-X keys first stop completion, then start another
2475 // completion mode.
2476 ins_compl_prep(' ');
2477 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2478 }
2479 }
2480
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002481 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002482 if (ctrl_x_mode_not_defined_yet()
2483 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002484 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002485 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002486 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002487 }
2488
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002489 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002490 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2491 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002492 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002493 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002494 {
2495 // We're already in CTRL-X mode, do we stay in it?
2496 if (!vim_is_ctrl_x_key(c))
2497 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002498 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002499 ctrl_x_mode = CTRL_X_NORMAL;
2500 else
2501 ctrl_x_mode = CTRL_X_FINISHED;
2502 edit_submode = NULL;
2503 }
2504 showmode();
2505 }
2506
2507 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2508 {
2509 // Show error message from attempted keyword completion (probably
2510 // 'Pattern not found') until another key is hit, then go back to
2511 // showing what mode we are in.
2512 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002513 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002514 && c != Ctrl_R && !ins_compl_pum_key(c))
2515 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002516 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002517 }
2518 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2519 // Trigger the CompleteDone event to give scripts a chance to act
2520 // upon the (possibly failed) completion.
2521 ins_apply_autocmds(EVENT_COMPLETEDONE);
2522
LemonBoy2bf52dd2022-04-09 18:17:34 +01002523 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002524
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002525 // reset continue_* if we left expansion-mode, if we stay they'll be
2526 // (re)set properly in ins_complete()
2527 if (!vim_is_ctrl_x_key(c))
2528 {
2529 compl_cont_status = 0;
2530 compl_cont_mode = 0;
2531 }
2532
2533 return retval;
2534}
2535
2536/*
2537 * Fix the redo buffer for the completion leader replacing some of the typed
2538 * text. This inserts backspaces and appends the changed text.
2539 * "ptr" is the known leader text or NUL.
2540 */
2541 static void
2542ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2543{
2544 int len;
2545 char_u *p;
2546 char_u *ptr = ptr_arg;
2547
2548 if (ptr == NULL)
2549 {
2550 if (compl_leader != NULL)
2551 ptr = compl_leader;
2552 else
2553 return; // nothing to do
2554 }
2555 if (compl_orig_text != NULL)
2556 {
2557 p = compl_orig_text;
2558 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2559 ;
2560 if (len > 0)
2561 len -= (*mb_head_off)(p, p + len);
2562 for (p += len; *p != NUL; MB_PTR_ADV(p))
2563 AppendCharToRedobuff(K_BS);
2564 }
2565 else
2566 len = 0;
2567 if (ptr != NULL)
2568 AppendToRedobuffLit(ptr + len, -1);
2569}
2570
2571/*
2572 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2573 * (depending on flag) starting from buf and looking for a non-scanned
2574 * buffer (other than curbuf). curbuf is special, if it is called with
2575 * buf=curbuf then it has to be the first call for a given flag/expansion.
2576 *
2577 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2578 */
2579 static buf_T *
2580ins_compl_next_buf(buf_T *buf, int flag)
2581{
2582 static win_T *wp = NULL;
2583
2584 if (flag == 'w') // just windows
2585 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002586 if (buf == curbuf || !win_valid(wp))
2587 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002588 wp = curwin;
2589 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2590 && wp->w_buffer->b_scanned)
2591 ;
2592 buf = wp->w_buffer;
2593 }
2594 else
2595 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2596 // (unlisted buffers)
2597 // When completing whole lines skip unloaded buffers.
2598 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2599 && ((flag == 'U'
2600 ? buf->b_p_bl
2601 : (!buf->b_p_bl
2602 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2603 || buf->b_scanned))
2604 ;
2605 return buf;
2606}
2607
2608#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002609
2610# ifdef FEAT_EVAL
2611static callback_T cfu_cb; // 'completefunc' callback function
2612static callback_T ofu_cb; // 'omnifunc' callback function
2613static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2614# endif
2615
2616/*
2617 * Copy a global callback function to a buffer local callback.
2618 */
2619 static void
2620copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2621{
2622 free_callback(bufcb);
2623 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2624 copy_callback(bufcb, globcb);
2625}
2626
2627/*
2628 * Parse the 'completefunc' option value and set the callback function.
2629 * Invoked when the 'completefunc' option is set. The option value can be a
2630 * name of a function (string), or function(<name>) or funcref(<name>) or a
2631 * lambda expression.
2632 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002633 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002634did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002635{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002636 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2637 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002638
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002639 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002640
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002641 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002642}
2643
2644/*
2645 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002646 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002647 */
2648 void
2649set_buflocal_cfu_callback(buf_T *buf UNUSED)
2650{
2651# ifdef FEAT_EVAL
2652 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2653# endif
2654}
2655
2656/*
2657 * Parse the 'omnifunc' option value and set the callback function.
2658 * Invoked when the 'omnifunc' option is set. The option value can be a
2659 * name of a function (string), or function(<name>) or funcref(<name>) or a
2660 * lambda expression.
2661 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002662 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002663did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002664{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002665 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2666 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002667
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002668 set_buflocal_ofu_callback(curbuf);
2669 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002670}
2671
2672/*
2673 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002674 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002675 */
2676 void
2677set_buflocal_ofu_callback(buf_T *buf UNUSED)
2678{
2679# ifdef FEAT_EVAL
2680 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2681# endif
2682}
2683
2684/*
2685 * Parse the 'thesaurusfunc' option value and set the callback function.
2686 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2687 * name of a function (string), or function(<name>) or funcref(<name>) or a
2688 * lambda expression.
2689 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002690 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002691did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002692{
2693 int retval;
2694
2695 if (*curbuf->b_p_tsrfu != NUL)
2696 {
2697 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002698 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2699 &curbuf->b_tsrfu_cb);
2700 }
2701 else
2702 {
2703 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002704 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2705 }
2706
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002707 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002708}
2709
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002710/*
2711 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002712 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002713 */
2714 int
2715set_ref_in_insexpand_funcs(int copyID)
2716{
2717 int abort = FALSE;
2718
2719 abort = set_ref_in_callback(&cfu_cb, copyID);
2720 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2721 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2722
2723 return abort;
2724}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002725
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002726/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002727 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002728 */
2729 static char_u *
2730get_complete_funcname(int type)
2731{
2732 switch (type)
2733 {
2734 case CTRL_X_FUNCTION:
2735 return curbuf->b_p_cfu;
2736 case CTRL_X_OMNI:
2737 return curbuf->b_p_ofu;
2738 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002739 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002740 default:
2741 return (char_u *)"";
2742 }
2743}
2744
2745/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002746 * Get the callback to use for insert mode completion.
2747 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002748 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002749get_insert_callback(int type)
2750{
2751 if (type == CTRL_X_FUNCTION)
2752 return &curbuf->b_cfu_cb;
2753 if (type == CTRL_X_OMNI)
2754 return &curbuf->b_ofu_cb;
2755 // CTRL_X_THESAURUS
2756 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2757}
2758
2759/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002760 * Execute user defined complete function 'completefunc', 'omnifunc' or
2761 * 'thesaurusfunc', and get matches in "matches".
2762 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002763 */
2764 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002765expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002766{
2767 list_T *matchlist = NULL;
2768 dict_T *matchdict = NULL;
2769 typval_T args[3];
2770 char_u *funcname;
2771 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002772 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002773 typval_T rettv;
2774 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002775 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002776
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002777 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002778 if (*funcname == NUL)
2779 return;
2780
2781 // Call 'completefunc' to obtain the list of matches.
2782 args[0].v_type = VAR_NUMBER;
2783 args[0].vval.v_number = 0;
2784 args[1].v_type = VAR_STRING;
2785 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2786 args[2].v_type = VAR_UNKNOWN;
2787
2788 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002789 // Lock the text to avoid weird things from happening. Also disallow
2790 // switching to another window, it should not be needed and may end up in
2791 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002792 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002793
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002794 cb = get_insert_callback(type);
2795 retval = call_callback(cb, 0, &rettv, 2, args);
2796
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002797 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002798 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002799 {
2800 switch (rettv.v_type)
2801 {
2802 case VAR_LIST:
2803 matchlist = rettv.vval.v_list;
2804 break;
2805 case VAR_DICT:
2806 matchdict = rettv.vval.v_dict;
2807 break;
2808 case VAR_SPECIAL:
2809 if (rettv.vval.v_number == VVAL_NONE)
2810 compl_opt_suppress_empty = TRUE;
2811 // FALLTHROUGH
2812 default:
2813 // TODO: Give error message?
2814 clear_tv(&rettv);
2815 break;
2816 }
2817 }
zeertzjqcfe45652022-05-27 17:26:55 +01002818 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002819
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002821 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002822 validate_cursor();
2823 if (!EQUAL_POS(curwin->w_cursor, pos))
2824 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002825 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002826 goto theend;
2827 }
2828
2829 if (matchlist != NULL)
2830 ins_compl_add_list(matchlist);
2831 else if (matchdict != NULL)
2832 ins_compl_add_dict(matchdict);
2833
2834theend:
2835 // Restore State, it might have been changed.
2836 State = save_State;
2837
2838 if (matchdict != NULL)
2839 dict_unref(matchdict);
2840 if (matchlist != NULL)
2841 list_unref(matchlist);
2842}
2843#endif // FEAT_COMPL_FUNC
2844
2845#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2846/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002847 * Add a match to the list of matches from a typeval_T.
2848 * If the given string is already in the list of completions, then return
2849 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2850 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002851 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002852 */
2853 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002854ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002855{
2856 char_u *word;
2857 int dup = FALSE;
2858 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002859 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002860 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002861 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002862 int status;
zeertzjq41008522024-07-27 13:21:49 +02002863 char_u *user_hlname;
2864 int user_hlattr = -1;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002865
Bram Moolenaar08928322020-01-04 14:32:48 +01002866 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002867 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2868 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002869 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2870 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2871 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2872 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2873 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
zeertzjq41008522024-07-27 13:21:49 +02002874 user_hlname = dict_get_string(tv->vval.v_dict, "hl_group", FALSE);
2875 if (user_hlname != NULL && *user_hlname != NUL)
2876 user_hlattr = syn_name2attr(user_hlname);
glepnir508e7852024-07-25 21:39:08 +02002877
Bram Moolenaard61efa52022-07-23 09:52:04 +01002878 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2879 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2880 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002881 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002882 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2883 dup = dict_get_number(tv->vval.v_dict, "dup");
2884 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2885 empty = dict_get_number(tv->vval.v_dict, "empty");
2886 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2887 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002888 flags |= CP_EQUAL;
2889 }
2890 else
2891 {
2892 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002893 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002894 }
2895 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002896 {
2897 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002898 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002899 }
glepnir508e7852024-07-25 21:39:08 +02002900 status = ins_compl_add(word, -1, NULL, cptext,
zeertzjq41008522024-07-27 13:21:49 +02002901 &user_data, dir, flags, dup, user_hlattr);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002902 if (status != OK)
2903 clear_tv(&user_data);
2904 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002905}
2906
2907/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002908 * Add completions from a list.
2909 */
2910 static void
2911ins_compl_add_list(list_T *list)
2912{
2913 listitem_T *li;
2914 int dir = compl_direction;
2915
2916 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002917 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002918 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002919 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002920 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002921 // if dir was BACKWARD then honor it just once
2922 dir = FORWARD;
2923 else if (did_emsg)
2924 break;
2925 }
2926}
2927
2928/*
2929 * Add completions from a dict.
2930 */
2931 static void
2932ins_compl_add_dict(dict_T *dict)
2933{
2934 dictitem_T *di_refresh;
2935 dictitem_T *di_words;
2936
2937 // Check for optional "refresh" item.
2938 compl_opt_refresh_always = FALSE;
2939 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2940 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2941 {
2942 char_u *v = di_refresh->di_tv.vval.v_string;
2943
2944 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2945 compl_opt_refresh_always = TRUE;
2946 }
2947
2948 // Add completions from a "words" list.
2949 di_words = dict_find(dict, (char_u *)"words", 5);
2950 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2951 ins_compl_add_list(di_words->di_tv.vval.v_list);
2952}
2953
2954/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002955 * Start completion for the complete() function.
2956 * "startcol" is where the matched text starts (1 is first column).
2957 * "list" is the list of matches.
2958 */
2959 static void
2960set_completion(colnr_T startcol, list_T *list)
2961{
2962 int save_w_wrow = curwin->w_wrow;
2963 int save_w_leftcol = curwin->w_leftcol;
2964 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02002965 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02002966 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
2967 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
2968 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002969
2970 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002971 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002972 ins_compl_prep(' ');
2973 ins_compl_clear();
2974 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002975 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002976
2977 compl_direction = FORWARD;
2978 if (startcol > curwin->w_cursor.col)
2979 startcol = curwin->w_cursor.col;
2980 compl_col = startcol;
2981 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2982 // compl_pattern doesn't need to be set
2983 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2984 if (p_ic)
2985 flags |= CP_ICASE;
2986 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002987 -1, NULL, NULL, NULL, 0,
glepnir508e7852024-07-25 21:39:08 +02002988 flags | CP_FAST, FALSE, -1) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002989 return;
2990
2991 ctrl_x_mode = CTRL_X_EVAL;
2992
2993 ins_compl_add_list(list);
2994 compl_matches = ins_compl_make_cyclic();
2995 compl_started = TRUE;
2996 compl_used_match = TRUE;
2997 compl_cont_status = 0;
2998
2999 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003000 int no_select = compl_no_select || compl_longest;
3001 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003002 {
3003 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003004 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003005 // Down/Up has no real effect.
3006 ins_complete(K_UP, FALSE);
3007 }
3008 else
3009 ins_complete(Ctrl_N, FALSE);
3010 compl_enter_selects = compl_no_insert;
3011
3012 // Lazily show the popup menu, unless we got interrupted.
3013 if (!compl_interrupted)
3014 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003015 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003016 out_flush();
3017}
3018
3019/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003020 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003021 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003022 void
3023f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003024{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003025 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003026
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003027 if (in_vim9script()
3028 && (check_for_number_arg(argvars, 0) == FAIL
3029 || check_for_list_arg(argvars, 1) == FAIL))
3030 return;
3031
Bram Moolenaar24959102022-05-07 20:01:16 +01003032 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003033 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003034 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003035 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003036 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003037
3038 // Check for undo allowed here, because if something was already inserted
3039 // the line was already saved for undo and this check isn't done.
3040 if (!undo_allowed())
3041 return;
3042
Bram Moolenaard83392a2022-09-01 12:22:46 +01003043 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003044 {
3045 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3046 if (startcol > 0)
3047 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003048 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003049}
3050
3051/*
3052 * "complete_add()" function
3053 */
3054 void
3055f_complete_add(typval_T *argvars, typval_T *rettv)
3056{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003057 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003058 return;
3059
Bram Moolenaar440cf092021-04-03 20:13:30 +02003060 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003061}
3062
3063/*
3064 * "complete_check()" function
3065 */
3066 void
3067f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3068{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003069 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003070 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003071
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003072 ins_compl_check_keys(0, TRUE);
3073 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003074
3075 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003076}
3077
3078/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003079 * Return Insert completion mode name string
3080 */
3081 static char_u *
3082ins_compl_mode(void)
3083{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003084 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003085 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3086
3087 return (char_u *)"";
3088}
3089
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003090/*
3091 * Assign the sequence number to all the completion matches which don't have
3092 * one assigned yet.
3093 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003094 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003095ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003096{
3097 int number = 0;
3098 compl_T *match;
3099
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003100 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003101 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003102 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003103 // This should normally succeed already at the first loop
3104 // cycle, so it's fast!
3105 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003106 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003107 if (match->cp_number != -1)
3108 {
3109 number = match->cp_number;
3110 break;
3111 }
3112 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003113 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003114 for (match = match->cp_next;
3115 match != NULL && match->cp_number == -1;
3116 match = match->cp_next)
3117 match->cp_number = ++number;
3118 }
3119 else // BACKWARD
3120 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003121 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003122 // number. This should normally succeed already at the
3123 // first loop cycle, so it's fast!
3124 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003125 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003126 if (match->cp_number != -1)
3127 {
3128 number = match->cp_number;
3129 break;
3130 }
3131 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003132 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003133 for (match = match->cp_prev; match
3134 && match->cp_number == -1;
3135 match = match->cp_prev)
3136 match->cp_number = ++number;
3137 }
3138}
3139
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003140/*
3141 * Get complete information
3142 */
3143 static void
3144get_complete_info(list_T *what_list, dict_T *retdict)
3145{
3146 int ret = OK;
3147 listitem_T *item;
3148#define CI_WHAT_MODE 0x01
3149#define CI_WHAT_PUM_VISIBLE 0x02
3150#define CI_WHAT_ITEMS 0x04
3151#define CI_WHAT_SELECTED 0x08
3152#define CI_WHAT_INSERTED 0x10
3153#define CI_WHAT_ALL 0xff
3154 int what_flag;
3155
3156 if (what_list == NULL)
3157 what_flag = CI_WHAT_ALL;
3158 else
3159 {
3160 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003161 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003162 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003163 {
3164 char_u *what = tv_get_string(&item->li_tv);
3165
3166 if (STRCMP(what, "mode") == 0)
3167 what_flag |= CI_WHAT_MODE;
3168 else if (STRCMP(what, "pum_visible") == 0)
3169 what_flag |= CI_WHAT_PUM_VISIBLE;
3170 else if (STRCMP(what, "items") == 0)
3171 what_flag |= CI_WHAT_ITEMS;
3172 else if (STRCMP(what, "selected") == 0)
3173 what_flag |= CI_WHAT_SELECTED;
3174 else if (STRCMP(what, "inserted") == 0)
3175 what_flag |= CI_WHAT_INSERTED;
3176 }
3177 }
3178
3179 if (ret == OK && (what_flag & CI_WHAT_MODE))
3180 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3181
3182 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3183 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3184
Girish Palya8950bf72024-03-20 20:07:29 +01003185 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003186 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003187 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003188 dict_T *di;
3189 compl_T *match;
3190 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003191
Girish Palya8950bf72024-03-20 20:07:29 +01003192 if (what_flag & CI_WHAT_ITEMS)
3193 {
3194 li = list_alloc();
3195 if (li == NULL)
3196 return;
3197 ret = dict_add_list(retdict, "items", li);
3198 }
3199 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3200 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3201 ins_compl_update_sequence_numbers();
3202 if (ret == OK && compl_first_match != NULL)
3203 {
3204 int list_idx = 0;
3205 match = compl_first_match;
3206 do
3207 {
3208 if (!match_at_original_text(match))
3209 {
3210 if (what_flag & CI_WHAT_ITEMS)
3211 {
3212 di = dict_alloc();
3213 if (di == NULL)
3214 return;
3215 ret = list_append_dict(li, di);
3216 if (ret != OK)
3217 return;
3218 dict_add_string(di, "word", match->cp_str);
3219 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3220 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3221 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3222 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3223 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3224 // Add an empty string for backwards compatibility
3225 dict_add_string(di, "user_data", (char_u *)"");
3226 else
3227 dict_add_tv(di, "user_data", &match->cp_user_data);
3228 }
3229 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3230 selected_idx = list_idx;
3231 list_idx += 1;
3232 }
3233 match = match->cp_next;
3234 }
3235 while (match != NULL && !is_first_match(match));
3236 }
3237 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3238 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003239 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003240
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003241 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3242 {
3243 // TODO
3244 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003245}
3246
3247/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003248 * "complete_info()" function
3249 */
3250 void
3251f_complete_info(typval_T *argvars, typval_T *rettv)
3252{
3253 list_T *what_list = NULL;
3254
Bram Moolenaar93a10962022-06-16 11:42:09 +01003255 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003256 return;
3257
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003258 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3259 return;
3260
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003261 if (argvars[0].v_type != VAR_UNKNOWN)
3262 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003263 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003264 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003265 what_list = argvars[0].vval.v_list;
3266 }
3267 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003268}
3269#endif
3270
3271/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003272 * Returns TRUE when using a user-defined function for thesaurus completion.
3273 */
3274 static int
3275thesaurus_func_complete(int type UNUSED)
3276{
3277#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003278 return type == CTRL_X_THESAURUS
3279 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003280#else
3281 return FALSE;
3282#endif
3283}
3284
3285/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003286 * Return value of process_next_cpt_value()
3287 */
3288enum
3289{
3290 INS_COMPL_CPT_OK = 1,
3291 INS_COMPL_CPT_CONT,
3292 INS_COMPL_CPT_END
3293};
3294
3295/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003296 * state information used for getting the next set of insert completion
3297 * matches.
3298 */
3299typedef struct
3300{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003301 char_u *e_cpt_copy; // copy of 'complete'
3302 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003303 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003304 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003305 pos_T prev_match_pos; // previous match position
3306 int set_match_pos; // save first_match_pos/last_match_pos
3307 pos_T first_match_pos; // first match position
3308 pos_T last_match_pos; // last match position
3309 int found_all; // found all matches of a certain type.
3310 char_u *dict; // dictionary file to search
3311 int dict_f; // "dict" is an exact file name or not
3312} ins_compl_next_state_T;
3313
3314/*
3315 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316 *
3317 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003318 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003319 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003320 * st->found_all - all matches of this type are found
3321 * st->ins_buf - search for completions in this buffer
3322 * st->first_match_pos - position of the first completion match
3323 * st->last_match_pos - position of the last completion match
3324 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003325 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003326 * st->dict - name of the dictionary or thesaurus file to search
3327 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003328 *
3329 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003330 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3331 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003332 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003333 */
3334 static int
3335process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003336 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003337 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003338 pos_T *start_match_pos,
3339 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003341 int compl_type = -1;
3342 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003343
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003344 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003345
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003346 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3347 st->e_cpt++;
3348
3349 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003350 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003351 st->ins_buf = curbuf;
3352 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003353 // Move the cursor back one character so that ^N can match the
3354 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003355 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003356 {
3357 // Move the cursor to after the last character in the
3358 // buffer, so that word at start of buffer is found
3359 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003360 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003361 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003362 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003363 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003364 compl_type = 0;
3365
3366 // Remember the first match so that the loop stops when we
3367 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003368 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003369 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003370 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003371 && (st->ins_buf = ins_compl_next_buf(
3372 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003373 {
3374 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003375 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003376 {
3377 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003378 st->first_match_pos.col = st->last_match_pos.col = 0;
3379 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3380 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003381 compl_type = 0;
3382 }
3383 else // unloaded buffer, scan like dictionary
3384 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003385 st->found_all = TRUE;
3386 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003387 {
3388 status = INS_COMPL_CPT_CONT;
3389 goto done;
3390 }
3391 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003392 st->dict = st->ins_buf->b_fname;
3393 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003394 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003395 if (!shortmess(SHM_COMPLETIONSCAN))
3396 {
3397 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3398 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3399 st->ins_buf->b_fname == NULL
3400 ? buf_spname(st->ins_buf)
3401 : st->ins_buf->b_sfname == NULL
3402 ? st->ins_buf->b_fname
3403 : st->ins_buf->b_sfname);
3404 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3405 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003406 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003407 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408 status = INS_COMPL_CPT_END;
3409 else
3410 {
3411 if (ctrl_x_mode_line_or_eval())
3412 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003413 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003414 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003415 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003416 compl_type = CTRL_X_DICTIONARY;
3417 else
3418 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003419 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003420 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003421 st->dict = st->e_cpt;
3422 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003423 }
3424 }
3425#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003426 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003427 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003428 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 compl_type = CTRL_X_PATH_DEFINES;
3430#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003431 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003432 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003433 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003434 if (!shortmess(SHM_COMPLETIONSCAN))
3435 {
3436 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3437 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3438 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3439 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003440 }
3441 else
3442 compl_type = -1;
3443
3444 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003445 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003446
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003447 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003448 if (compl_type == -1)
3449 status = INS_COMPL_CPT_CONT;
3450 }
3451
3452done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003453 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003454 return status;
3455}
3456
3457#ifdef FEAT_FIND_ID
3458/*
3459 * Get the next set of identifiers or defines matching "compl_pattern" in
3460 * included files.
3461 */
3462 static void
3463get_next_include_file_completion(int compl_type)
3464{
3465 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003466 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003467 (compl_type == CTRL_X_PATH_DEFINES
3468 && !(compl_cont_status & CONT_SOL))
3469 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003470 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471}
3472#endif
3473
3474/*
3475 * Get the next set of words matching "compl_pattern" in dictionary or
3476 * thesaurus files.
3477 */
3478 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003479get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003480{
3481#ifdef FEAT_COMPL_FUNC
3482 if (thesaurus_func_complete(compl_type))
3483 expand_by_function(compl_type, compl_pattern);
3484 else
3485#endif
3486 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003487 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003488 : (compl_type == CTRL_X_THESAURUS
3489 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3490 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3491 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003492 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003493 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003494}
3495
3496/*
3497 * Get the next set of tag names matching "compl_pattern".
3498 */
3499 static void
3500get_next_tag_completion(void)
3501{
3502 int save_p_ic;
3503 char_u **matches;
3504 int num_matches;
3505
3506 // set p_ic according to p_ic, p_scs and pat for find_tags().
3507 save_p_ic = p_ic;
3508 p_ic = ignorecase(compl_pattern);
3509
3510 // Find up to TAG_MANY matches. Avoids that an enormous number
3511 // of matches is found when compl_pattern is empty
3512 g_tag_at_cursor = TRUE;
3513 if (find_tags(compl_pattern, &num_matches, &matches,
3514 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003515 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003516 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3517 ins_compl_add_matches(num_matches, matches, p_ic);
3518 g_tag_at_cursor = FALSE;
3519 p_ic = save_p_ic;
3520}
3521
3522/*
glepnir8159fb12024-07-17 20:32:54 +02003523 * Compare function for qsort
3524 */
3525static int compare_scores(const void *a, const void *b)
3526{
3527 int idx_a = *(const int *)a;
3528 int idx_b = *(const int *)b;
3529 int score_a = compl_fuzzy_scores[idx_a];
3530 int score_b = compl_fuzzy_scores[idx_b];
3531 return (score_a > score_b) ? -1 : (score_a < score_b) ? 1 : 0;
3532}
3533
3534/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003535 * Get the next set of filename matching "compl_pattern".
3536 */
3537 static void
3538get_next_filename_completion(void)
3539{
3540 char_u **matches;
3541 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003542 char_u *ptr;
3543 garray_T fuzzy_indices;
3544 int i;
3545 int score;
3546 char_u *leader = ins_compl_leader();
Ken Takata073cb022024-07-28 17:08:15 +02003547 size_t leader_len = STRLEN(leader);
glepnir8159fb12024-07-17 20:32:54 +02003548 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3549 char_u **sorted_matches;
3550 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003551 char_u *last_sep = NULL;
3552 size_t path_with_wildcard_len;
3553 char_u *path_with_wildcard;
glepnir8159fb12024-07-17 20:32:54 +02003554
3555 if (in_fuzzy)
3556 {
glepnir0be03e12024-07-19 16:45:05 +02003557 last_sep = vim_strrchr(leader, PATHSEP);
3558 if (last_sep == NULL)
3559 {
3560 // No path separator or separator is the last character,
3561 // fuzzy match the whole leader
3562 vim_free(compl_pattern);
3563 compl_pattern = vim_strsave((char_u *)"*");
3564 compl_patternlen = STRLEN(compl_pattern);
3565 }
3566 else if (*(last_sep + 1) == '\0')
3567 in_fuzzy = FALSE;
3568 else
3569 {
3570 // Split leader into path and file parts
3571 int path_len = last_sep - leader + 1;
3572 path_with_wildcard_len = path_len + 2;
3573 path_with_wildcard = alloc(path_with_wildcard_len);
3574 if (path_with_wildcard != NULL)
3575 {
3576 vim_strncpy(path_with_wildcard, leader, path_len);
3577 vim_strcat(path_with_wildcard, (char_u *)"*", path_with_wildcard_len);
3578 vim_free(compl_pattern);
3579 compl_pattern = path_with_wildcard;
3580 compl_patternlen = STRLEN(compl_pattern);
3581
3582 // Move leader to the file part
3583 leader = last_sep + 1;
glepnir6b6280c2024-07-27 16:25:45 +02003584 leader_len = STRLEN(leader);
glepnir0be03e12024-07-19 16:45:05 +02003585 }
3586 }
glepnir8159fb12024-07-17 20:32:54 +02003587 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003588
3589 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3590 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3591 return;
3592
3593 // May change home directory back to "~".
3594 tilde_replace(compl_pattern, num_matches, matches);
3595#ifdef BACKSLASH_IN_FILENAME
3596 if (curbuf->b_p_csl[0] != NUL)
3597 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003598 for (i = 0; i < num_matches; ++i)
3599 {
glepnir8159fb12024-07-17 20:32:54 +02003600 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003601 while (*ptr != NUL)
3602 {
3603 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3604 *ptr = '/';
3605 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3606 *ptr = '\\';
3607 ptr += (*mb_ptr2len)(ptr);
3608 }
3609 }
3610 }
3611#endif
glepnir8159fb12024-07-17 20:32:54 +02003612
3613 if (in_fuzzy)
3614 {
3615 ga_init2(&fuzzy_indices, sizeof(int), 10);
3616 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3617
3618 for (i = 0; i < num_matches; i++)
3619 {
3620 ptr = matches[i];
3621 score = fuzzy_match_str(ptr, leader);
3622 if (score > 0)
3623 {
3624 if (ga_grow(&fuzzy_indices, 1) == OK)
3625 {
3626 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3627 compl_fuzzy_scores[i] = score;
3628 fuzzy_indices.ga_len++;
3629 }
3630 }
3631 }
3632
Christian Brabandt220474d2024-07-20 13:26:44 +02003633 // prevent qsort from deref NULL pointer
3634 if (fuzzy_indices.ga_len > 0)
3635 {
3636 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3637 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003638
Christian Brabandt220474d2024-07-20 13:26:44 +02003639 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3640 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3641 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003642
Christian Brabandt220474d2024-07-20 13:26:44 +02003643 FreeWild(num_matches, matches);
3644 matches = sorted_matches;
3645 num_matches = fuzzy_indices.ga_len;
3646 }
glepnir6b6280c2024-07-27 16:25:45 +02003647 else if (leader_len > 0)
3648 {
3649 FreeWild(num_matches, matches);
3650 num_matches = 0;
3651 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003652
glepnir8159fb12024-07-17 20:32:54 +02003653 vim_free(compl_fuzzy_scores);
3654 ga_clear(&fuzzy_indices);
3655 }
3656
glepnir6b6280c2024-07-27 16:25:45 +02003657 if (num_matches > 0)
3658 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659}
3660
3661/*
3662 * Get the next set of command-line completions matching "compl_pattern".
3663 */
3664 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003665get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666{
3667 char_u **matches;
3668 int num_matches;
3669
3670 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003671 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672 ins_compl_add_matches(num_matches, matches, FALSE);
3673}
3674
3675/*
3676 * Get the next set of spell suggestions matching "compl_pattern".
3677 */
3678 static void
3679get_next_spell_completion(linenr_T lnum UNUSED)
3680{
3681#ifdef FEAT_SPELL
3682 char_u **matches;
3683 int num_matches;
3684
3685 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3686 if (num_matches > 0)
3687 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003688 else
3689 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690#endif
3691}
3692
3693/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003694 * Return the next word or line from buffer "ins_buf" at position
3695 * "cur_match_pos" for completion. The length of the match is set in "len".
3696 */
3697 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003698ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003699 buf_T *ins_buf, // buffer being scanned
3700 pos_T *cur_match_pos, // current match position
3701 int *match_len,
3702 int *cont_s_ipos) // next ^X<> will set initial_pos
3703{
3704 char_u *ptr;
3705 int len;
3706
3707 *match_len = 0;
3708 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3709 cur_match_pos->col;
3710 if (ctrl_x_mode_line_or_eval())
3711 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003712 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003713 {
3714 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3715 return NULL;
3716 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3717 if (!p_paste)
3718 ptr = skipwhite(ptr);
3719 }
3720 len = (int)STRLEN(ptr);
3721 }
3722 else
3723 {
3724 char_u *tmp_ptr = ptr;
3725
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003726 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003727 {
3728 tmp_ptr += compl_length;
3729 // Skip if already inside a word.
3730 if (vim_iswordp(tmp_ptr))
3731 return NULL;
3732 // Find start of next word.
3733 tmp_ptr = find_word_start(tmp_ptr);
3734 }
3735 // Find end of this word.
3736 tmp_ptr = find_word_end(tmp_ptr);
3737 len = (int)(tmp_ptr - ptr);
3738
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003739 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003740 {
3741 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3742 {
3743 // Try next line, if any. the new word will be
3744 // "join" as if the normal command "J" was used.
3745 // IOSIZE is always greater than
3746 // compl_length, so the next STRNCPY always
3747 // works -- Acevedo
3748 STRNCPY(IObuff, ptr, len);
3749 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3750 tmp_ptr = ptr = skipwhite(ptr);
3751 // Find start of next word.
3752 tmp_ptr = find_word_start(tmp_ptr);
3753 // Find end of next word.
3754 tmp_ptr = find_word_end(tmp_ptr);
3755 if (tmp_ptr > ptr)
3756 {
3757 if (*ptr != ')' && IObuff[len - 1] != TAB)
3758 {
3759 if (IObuff[len - 1] != ' ')
3760 IObuff[len++] = ' ';
3761 // IObuf =~ "\k.* ", thus len >= 2
3762 if (p_js
3763 && (IObuff[len - 2] == '.'
3764 || (vim_strchr(p_cpo, CPO_JOINSP)
3765 == NULL
3766 && (IObuff[len - 2] == '?'
3767 || IObuff[len - 2] == '!'))))
3768 IObuff[len++] = ' ';
3769 }
3770 // copy as much as possible of the new word
3771 if (tmp_ptr - ptr >= IOSIZE - len)
3772 tmp_ptr = ptr + IOSIZE - len - 1;
3773 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3774 len += (int)(tmp_ptr - ptr);
3775 *cont_s_ipos = TRUE;
3776 }
3777 IObuff[len] = NUL;
3778 ptr = IObuff;
3779 }
3780 if (len == compl_length)
3781 return NULL;
3782 }
3783 }
3784
3785 *match_len = len;
3786 return ptr;
3787}
3788
3789/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003790 * Get the next set of words matching "compl_pattern" for default completion(s)
3791 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003792 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3793 * position "st->start_pos" in the "compl_direction" direction. If
3794 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3795 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003796 * Returns OK if a new next match is found, otherwise returns FAIL.
3797 */
3798 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003799get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003800{
3801 int found_new_match = FAIL;
3802 int save_p_scs;
3803 int save_p_ws;
3804 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003805 char_u *ptr = NULL;
3806 int len = 0;
3807 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3808 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003809
3810 // If 'infercase' is set, don't use 'smartcase' here
3811 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003812 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003813 p_scs = FALSE;
3814
3815 // Buffers other than curbuf are scanned from the beginning or the
3816 // end but never from the middle, thus setting nowrapscan in this
3817 // buffer is a good idea, on the other hand, we always set
3818 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3819 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003820 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003821 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003822 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003823 p_ws = TRUE;
3824 looped_around = FALSE;
3825 for (;;)
3826 {
3827 int cont_s_ipos = FALSE;
3828
3829 ++msg_silent; // Don't want messages for wrapscan.
3830
3831 // ctrl_x_mode_line_or_eval() || word-wise search that
3832 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003833 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003834 found_new_match = search_for_exact_line(st->ins_buf,
3835 st->cur_match_pos, compl_direction, compl_pattern);
glepnir8159fb12024-07-17 20:32:54 +02003836 else if (in_fuzzy)
3837 found_new_match = search_for_fuzzy_match(st->ins_buf,
3838 st->cur_match_pos, leader, compl_direction,
3839 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003840 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003841 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003842 NULL, compl_direction, compl_pattern, compl_patternlen,
3843 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003844 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003845 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003846 {
3847 // set "compl_started" even on fail
3848 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003849 st->first_match_pos = *st->cur_match_pos;
3850 st->last_match_pos = *st->cur_match_pos;
3851 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003852 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003853 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3854 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003855 {
3856 found_new_match = FAIL;
3857 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003858 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003859 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3860 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3861 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003862 {
3863 if (looped_around)
3864 found_new_match = FAIL;
3865 else
3866 looped_around = TRUE;
3867 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003868 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003869 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3870 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3871 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003872 {
3873 if (looped_around)
3874 found_new_match = FAIL;
3875 else
3876 looped_around = TRUE;
3877 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003878 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003879 if (found_new_match == FAIL)
3880 break;
3881
3882 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003883 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003884 && start_pos->lnum == st->cur_match_pos->lnum
3885 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003886 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003887
glepnir8159fb12024-07-17 20:32:54 +02003888 if (!in_fuzzy)
3889 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3890 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003891 if (ptr == NULL)
3892 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003893
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003894 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003895 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003896 0, cont_s_ipos) != NOTDONE)
3897 {
3898 found_new_match = OK;
3899 break;
3900 }
3901 }
3902 p_scs = save_p_scs;
3903 p_ws = save_p_ws;
3904
3905 return found_new_match;
3906}
3907
3908/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003909 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003910 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003911 */
3912 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003913get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003914{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003915 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003916
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003917 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003918 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003919 case -1:
3920 break;
3921#ifdef FEAT_FIND_ID
3922 case CTRL_X_PATH_PATTERNS:
3923 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003924 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003925 break;
3926#endif
3927
3928 case CTRL_X_DICTIONARY:
3929 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003930 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3931 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003932 break;
3933
3934 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003935 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003936 break;
3937
3938 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003939 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003940 break;
3941
3942 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003943 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003944 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003945 break;
3946
3947#ifdef FEAT_COMPL_FUNC
3948 case CTRL_X_FUNCTION:
3949 case CTRL_X_OMNI:
3950 expand_by_function(type, compl_pattern);
3951 break;
3952#endif
3953
3954 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003955 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003956 break;
3957
3958 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003959 found_new_match = get_next_default_completion(st, ini);
3960 if (found_new_match == FAIL && st->ins_buf == curbuf)
3961 st->found_all = TRUE;
3962 }
3963
3964 // check if compl_curr_match has changed, (e.g. other type of
3965 // expansion added something)
3966 if (type != 0 && compl_curr_match != compl_old_match)
3967 found_new_match = OK;
3968
3969 return found_new_match;
3970}
3971
3972/*
3973 * Get the next expansion(s), using "compl_pattern".
3974 * The search starts at position "ini" in curbuf and in the direction
3975 * compl_direction.
3976 * When "compl_started" is FALSE start at that position, otherwise continue
3977 * where we stopped searching before.
3978 * This may return before finding all the matches.
3979 * Return the total number of matches or -1 if still unknown -- Acevedo
3980 */
3981 static int
3982ins_compl_get_exp(pos_T *ini)
3983{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003984 static ins_compl_next_state_T st;
3985 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003986 int i;
3987 int found_new_match;
3988 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02003989 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003990
3991 if (!compl_started)
3992 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003993 buf_T *buf;
3994
3995 FOR_ALL_BUFFERS(buf)
3996 buf->b_scanned = 0;
3997 if (!st_cleared)
3998 {
3999 CLEAR_FIELD(st);
4000 st_cleared = TRUE;
4001 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004002 st.found_all = FALSE;
4003 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004004 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004005 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004006 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4007 ? (char_u *)"." : curbuf->b_p_cpt);
4008 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004009 st.last_match_pos = st.first_match_pos = *ini;
4010 }
4011 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4012 st.ins_buf = curbuf; // In case the buffer was wiped out.
4013
4014 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004015 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004016 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004017
4018 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4019 for (;;)
4020 {
4021 found_new_match = FAIL;
4022 st.set_match_pos = FALSE;
4023
4024 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4025 // or if found_all says this entry is done. For ^X^L only use the
4026 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004027 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004028 && (!compl_started || st.found_all))
4029 {
glepnir8159fb12024-07-17 20:32:54 +02004030 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004031
4032 if (status == INS_COMPL_CPT_END)
4033 break;
4034 if (status == INS_COMPL_CPT_CONT)
4035 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004036 }
4037
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004038 // If complete() was called then compl_pattern has been reset. The
4039 // following won't work then, bail out.
4040 if (compl_pattern == NULL)
4041 break;
4042
4043 // get the next set of completion matches
4044 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004045
4046 // break the loop for specialized modes (use 'complete' just for the
4047 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4048 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004049 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4050 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004051 {
4052 if (got_int)
4053 break;
4054 // Fill the popup menu as soon as possible.
4055 if (type != -1)
4056 ins_compl_check_keys(0, FALSE);
4057
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004058 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004059 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4060 break;
4061 compl_started = TRUE;
4062 }
4063 else
4064 {
4065 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004066 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004067 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004068
4069 compl_started = FALSE;
4070 }
4071 }
4072 compl_started = TRUE;
4073
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004074 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004075 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004076 found_new_match = FAIL;
4077
4078 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004079 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004080 && !ctrl_x_mode_line_or_eval()))
4081 i = ins_compl_make_cyclic();
4082
4083 if (compl_old_match != NULL)
4084 {
4085 // If several matches were added (FORWARD) or the search failed and has
4086 // just been made cyclic then we have to move compl_curr_match to the
4087 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004088 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004089 : compl_old_match->cp_prev;
4090 if (compl_curr_match == NULL)
4091 compl_curr_match = compl_old_match;
4092 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004093 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004094
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004095 return i;
4096}
4097
4098/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004099 * Update "compl_shown_match" to the actually shown match, it may differ when
4100 * "compl_leader" is used to omit some of the matches.
4101 */
4102 static void
4103ins_compl_update_shown_match(void)
4104{
4105 while (!ins_compl_equal(compl_shown_match,
4106 compl_leader, (int)STRLEN(compl_leader))
4107 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004108 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004109 compl_shown_match = compl_shown_match->cp_next;
4110
4111 // If we didn't find it searching forward, and compl_shows_dir is
4112 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004113 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004114 && !ins_compl_equal(compl_shown_match,
4115 compl_leader, (int)STRLEN(compl_leader))
4116 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004117 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004118 {
4119 while (!ins_compl_equal(compl_shown_match,
4120 compl_leader, (int)STRLEN(compl_leader))
4121 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004122 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004123 compl_shown_match = compl_shown_match->cp_prev;
4124 }
4125}
4126
4127/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004128 * Delete the old text being completed.
4129 */
4130 void
4131ins_compl_delete(void)
4132{
4133 int col;
4134
4135 // In insert mode: Delete the typed part.
4136 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004137 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004138 if ((int)curwin->w_cursor.col > col)
4139 {
4140 if (stop_arrow() == FAIL)
4141 return;
4142 backspace_until_column(col);
4143 }
4144
4145 // TODO: is this sufficient for redrawing? Redrawing everything causes
4146 // flicker, thus we can't do that.
4147 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004148#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004149 // clear v:completed_item
4150 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004151#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004152}
4153
4154/*
4155 * Insert the new text being completed.
4156 * "in_compl_func" is TRUE when called from complete_check().
4157 */
4158 void
4159ins_compl_insert(int in_compl_func)
4160{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004161 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004162
4163 // Make sure we don't go over the end of the string, this can happen with
4164 // illegal bytes.
4165 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4166 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004167 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004168 compl_used_match = FALSE;
4169 else
4170 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004171#ifdef FEAT_EVAL
4172 {
4173 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4174
4175 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4176 }
4177#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004178 if (!in_compl_func)
4179 compl_curr_match = compl_shown_match;
4180}
4181
4182/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004183 * show the file name for the completion match (if any). Truncate the file
4184 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004185 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004186 static void
4187ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004188{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004189 char *lead = _("match in file");
4190 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4191 char_u *s;
4192 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004193
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004194 if (space <= 0)
4195 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004196
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004197 // We need the tail that fits. With double-byte encoding going
4198 // back from the end is very slow, thus go from the start and keep
4199 // the text that fits in "space" between "s" and "e".
4200 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004201 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004202 space -= ptr2cells(e);
4203 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004204 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004205 space += ptr2cells(s);
4206 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004207 }
4208 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004209 msg_hist_off = TRUE;
4210 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4211 s > compl_shown_match->cp_fname ? "<" : "", s);
4212 msg((char *)IObuff);
4213 msg_hist_off = FALSE;
4214 redraw_cmdline = FALSE; // don't overwrite!
4215}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004216
glepnirdca57fb2024-06-04 22:01:21 +02004217/*
zeertzjq551d8c32024-06-05 19:53:32 +02004218 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004219 */
glepnira218cc62024-06-03 19:32:39 +02004220 static compl_T *
4221find_comp_when_fuzzy(void)
4222{
4223 int score;
4224 char_u* str;
4225 int target_idx = -1;
4226 int is_forward = compl_shows_dir_forward();
4227 int is_backward = compl_shows_dir_backward();
4228 compl_T *comp = NULL;
4229
glepnir43eef882024-06-19 20:20:48 +02004230 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004231 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004232 return compl_first_match != compl_shown_match ? compl_first_match :
4233 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004234
4235 if (is_forward)
4236 target_idx = compl_selected_item + 1;
4237 else if (is_backward)
4238 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004239 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004240
4241 score = compl_match_array[target_idx].pum_score;
4242 str = compl_match_array[target_idx].pum_text;
4243
4244 comp = compl_first_match;
4245 do {
4246 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4247 return comp;
4248 comp = comp->cp_next;
4249 } while (comp != NULL && !is_first_match(comp));
4250
4251 return NULL;
4252}
4253
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004254/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004255 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004256 * times. The number of matches found is returned in 'num_matches'.
4257 *
4258 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4259 * get more completions. If it is FALSE, then do nothing when there are no more
4260 * completions in the given direction.
4261 *
4262 * If "advance" is TRUE, then completion will move to the first match.
4263 * Otherwise, the original text will be shown.
4264 *
4265 * Returns OK on success and -1 if the number of matches are unknown.
4266 */
4267 static int
4268find_next_completion_match(
4269 int allow_get_expansion,
4270 int todo, // repeat completion this many times
4271 int advance,
4272 int *num_matches)
4273{
4274 int found_end = FALSE;
4275 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004276 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004277 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4278 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004279
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004280 while (--todo >= 0)
4281 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004282 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004283 {
glepniraced8c22024-06-15 15:13:05 +02004284 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4285 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004286 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004287 && (is_first_match(compl_shown_match->cp_next)
4288 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004289 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004290 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004291 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004292 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004293 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004294 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4295 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004296 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004297 }
4298 else
4299 {
4300 if (!allow_get_expansion)
4301 {
4302 if (advance)
4303 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004304 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004305 compl_pending -= todo + 1;
4306 else
4307 compl_pending += todo + 1;
4308 }
4309 return -1;
4310 }
4311
4312 if (!compl_no_select && advance)
4313 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004314 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004315 --compl_pending;
4316 else
4317 ++compl_pending;
4318 }
4319
4320 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004321 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004322
4323 // handle any pending completions
4324 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004325 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004326 {
4327 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4328 {
4329 compl_shown_match = compl_shown_match->cp_next;
4330 --compl_pending;
4331 }
4332 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4333 {
4334 compl_shown_match = compl_shown_match->cp_prev;
4335 ++compl_pending;
4336 }
4337 else
4338 break;
4339 }
4340 found_end = FALSE;
4341 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004342 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004343 && compl_leader != NULL
4344 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004345 compl_leader, (int)STRLEN(compl_leader))
4346 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004347 ++todo;
4348 else
4349 // Remember a matching item.
4350 found_compl = compl_shown_match;
4351
4352 // Stop at the end of the list when we found a usable match.
4353 if (found_end)
4354 {
4355 if (found_compl != NULL)
4356 {
4357 compl_shown_match = found_compl;
4358 break;
4359 }
4360 todo = 1; // use first usable match after wrapping around
4361 }
4362 }
4363
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004364 return OK;
4365}
4366
4367/*
4368 * Fill in the next completion in the current direction.
4369 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4370 * get more completions. If it is FALSE, then we just do nothing when there
4371 * are no more completions in a given direction. The latter case is used when
4372 * we are still in the middle of finding completions, to allow browsing
4373 * through the ones found so far.
4374 * Return the total number of matches, or -1 if still unknown -- webb.
4375 *
4376 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4377 * compl_shown_match here.
4378 *
4379 * Note that this function may be called recursively once only. First with
4380 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4381 * calls this function with "allow_get_expansion" FALSE.
4382 */
4383 static int
4384ins_compl_next(
4385 int allow_get_expansion,
4386 int count, // repeat completion this many times; should
4387 // be at least 1
4388 int insert_match, // Insert the newly selected match
4389 int in_compl_func) // called from complete_check()
4390{
4391 int num_matches = -1;
4392 int todo = count;
4393 int advance;
4394 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004395 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004396 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004397 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4398 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004399
4400 // When user complete function return -1 for findstart which is next
4401 // time of 'always', compl_shown_match become NULL.
4402 if (compl_shown_match == NULL)
4403 return -1;
4404
glepnira218cc62024-06-03 19:32:39 +02004405 if (compl_leader != NULL
4406 && !match_at_original_text(compl_shown_match)
4407 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004408 // Update "compl_shown_match" to the actually shown match
4409 ins_compl_update_shown_match();
4410
4411 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004412 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004413 // Delete old text to be replaced
4414 ins_compl_delete();
4415
4416 // When finding the longest common text we stick at the original text,
4417 // don't let CTRL-N or CTRL-P move to the first match.
4418 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4419
4420 // When restarting the search don't insert the first match either.
4421 if (compl_restarting)
4422 {
4423 advance = FALSE;
4424 compl_restarting = FALSE;
4425 }
4426
4427 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4428 // around.
4429 if (find_next_completion_match(allow_get_expansion, todo, advance,
4430 &num_matches) == -1)
4431 return -1;
4432
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004433 if (curbuf != orig_curbuf)
4434 {
4435 // In case some completion function switched buffer, don't want to
4436 // insert the completion elsewhere.
4437 return -1;
4438 }
4439
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004440 // Insert the text of the new completion, or the compl_leader.
4441 if (compl_no_insert && !started)
4442 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004443 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004444 compl_used_match = FALSE;
4445 }
4446 else if (insert_match)
4447 {
4448 if (!compl_get_longest || compl_used_match)
4449 ins_compl_insert(in_compl_func);
4450 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004451 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004452 }
4453 else
4454 compl_used_match = FALSE;
4455
4456 if (!allow_get_expansion)
4457 {
4458 // may undisplay the popup menu first
4459 ins_compl_upd_pum();
4460
4461 if (pum_enough_matches())
4462 // Will display the popup menu, don't redraw yet to avoid flicker.
4463 pum_call_update_screen();
4464 else
4465 // Not showing the popup menu yet, redraw to show the user what was
4466 // inserted.
4467 update_screen(0);
4468
4469 // display the updated popup menu
4470 ins_compl_show_pum();
4471#ifdef FEAT_GUI
4472 if (gui.in_use)
4473 {
4474 // Show the cursor after the match, not after the redrawn text.
4475 setcursor();
4476 out_flush_cursor(FALSE, FALSE);
4477 }
4478#endif
4479
4480 // Delete old text to be replaced, since we're still searching and
4481 // don't want to match ourselves!
4482 ins_compl_delete();
4483 }
4484
4485 // Enter will select a match when the match wasn't inserted and the popup
4486 // menu is visible.
4487 if (compl_no_insert && !started)
4488 compl_enter_selects = TRUE;
4489 else
4490 compl_enter_selects = !insert_match && compl_match_array != NULL;
4491
4492 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004493 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004494 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004495
4496 return num_matches;
4497}
4498
4499/*
4500 * Call this while finding completions, to check whether the user has hit a key
4501 * that should change the currently displayed completion, or exit completion
4502 * mode. Also, when compl_pending is not zero, show a completion as soon as
4503 * possible. -- webb
4504 * "frequency" specifies out of how many calls we actually check.
4505 * "in_compl_func" is TRUE when called from complete_check(), don't set
4506 * compl_curr_match.
4507 */
4508 void
4509ins_compl_check_keys(int frequency, int in_compl_func)
4510{
4511 static int count = 0;
4512 int c;
4513
4514 // Don't check when reading keys from a script, :normal or feedkeys().
4515 // That would break the test scripts. But do check for keys when called
4516 // from complete_check().
4517 if (!in_compl_func && (using_script() || ex_normal_busy))
4518 return;
4519
4520 // Only do this at regular intervals
4521 if (++count < frequency)
4522 return;
4523 count = 0;
4524
4525 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4526 // can't do its work correctly.
4527 c = vpeekc_any();
4528 if (c != NUL)
4529 {
4530 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4531 {
4532 c = safe_vgetc(); // Eat the character
4533 compl_shows_dir = ins_compl_key2dir(c);
4534 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4535 c != K_UP && c != K_DOWN, in_compl_func);
4536 }
4537 else
4538 {
4539 // Need to get the character to have KeyTyped set. We'll put it
4540 // back with vungetc() below. But skip K_IGNORE.
4541 c = safe_vgetc();
4542 if (c != K_IGNORE)
4543 {
4544 // Don't interrupt completion when the character wasn't typed,
4545 // e.g., when doing @q to replay keys.
4546 if (c != Ctrl_R && KeyTyped)
4547 compl_interrupted = TRUE;
4548
4549 vungetc(c);
4550 }
4551 }
4552 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004553 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004554 {
4555 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4556
4557 compl_pending = 0;
4558 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4559 }
4560}
4561
4562/*
4563 * Decide the direction of Insert mode complete from the key typed.
4564 * Returns BACKWARD or FORWARD.
4565 */
4566 static int
4567ins_compl_key2dir(int c)
4568{
4569 if (c == Ctrl_P || c == Ctrl_L
4570 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4571 return BACKWARD;
4572 return FORWARD;
4573}
4574
4575/*
4576 * Return TRUE for keys that are used for completion only when the popup menu
4577 * is visible.
4578 */
4579 static int
4580ins_compl_pum_key(int c)
4581{
4582 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4583 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4584 || c == K_UP || c == K_DOWN);
4585}
4586
4587/*
4588 * Decide the number of completions to move forward.
4589 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4590 */
4591 static int
4592ins_compl_key2count(int c)
4593{
4594 int h;
4595
4596 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4597 {
4598 h = pum_get_height();
4599 if (h > 3)
4600 h -= 2; // keep some context
4601 return h;
4602 }
4603 return 1;
4604}
4605
4606/*
4607 * Return TRUE if completion with "c" should insert the match, FALSE if only
4608 * to change the currently selected completion.
4609 */
4610 static int
4611ins_compl_use_match(int c)
4612{
4613 switch (c)
4614 {
4615 case K_UP:
4616 case K_DOWN:
4617 case K_PAGEDOWN:
4618 case K_KPAGEDOWN:
4619 case K_S_DOWN:
4620 case K_PAGEUP:
4621 case K_KPAGEUP:
4622 case K_S_UP:
4623 return FALSE;
4624 }
4625 return TRUE;
4626}
4627
4628/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004629 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4630 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004631 * Sets the global variables: compl_col, compl_length, compl_pattern and
4632 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004633 * Uses the global variables: compl_cont_status and ctrl_x_mode
4634 */
4635 static int
4636get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4637{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004638 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004639 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004640 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004641 {
4642 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4643 ;
4644 compl_col += ++startcol;
4645 compl_length = curs_col - startcol;
4646 }
4647 if (p_ic)
4648 compl_pattern = str_foldcase(line + compl_col,
4649 compl_length, NULL, 0);
4650 else
4651 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4652 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004653 {
4654 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004655 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004656 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004657 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004658 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004659 {
4660 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004661 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004662
4663 // we need up to 2 extra chars for the prefix
4664 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004665 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004666 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004667 {
4668 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004669 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004670 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004671 if (!vim_iswordp(line + compl_col)
4672 || (compl_col > 0
4673 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004674 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004675 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004676 prefixlen = 0;
4677 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004678 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004679 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004680 line + compl_col, compl_length);
4681 }
4682 else if (--startcol < 0
4683 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4684 {
4685 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004686 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004687 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004688 {
4689 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004690 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004691 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004692 compl_col += curs_col;
4693 compl_length = 0;
4694 }
4695 else
4696 {
4697 // Search the point of change class of multibyte character
4698 // or not a word single byte character backward.
4699 if (has_mbyte)
4700 {
4701 int base_class;
4702 int head_off;
4703
4704 startcol -= (*mb_head_off)(line, line + startcol);
4705 base_class = mb_get_class(line + startcol);
4706 while (--startcol >= 0)
4707 {
4708 head_off = (*mb_head_off)(line, line + startcol);
4709 if (base_class != mb_get_class(line + startcol
4710 - head_off))
4711 break;
4712 startcol -= head_off;
4713 }
4714 }
4715 else
4716 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4717 ;
4718 compl_col += ++startcol;
4719 compl_length = (int)curs_col - startcol;
4720 if (compl_length == 1)
4721 {
4722 // Only match word with at least two chars -- webb
4723 // there's no need to call quote_meta,
4724 // alloc(7) is enough -- Acevedo
4725 compl_pattern = alloc(7);
4726 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004727 {
4728 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004729 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004730 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004731 STRCPY((char *)compl_pattern, "\\<");
4732 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4733 STRCAT((char *)compl_pattern, "\\k");
4734 }
4735 else
4736 {
4737 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4738 compl_length) + 2);
4739 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004740 {
4741 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004742 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004743 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004744 STRCPY((char *)compl_pattern, "\\<");
4745 (void)quote_meta(compl_pattern + 2, line + compl_col,
4746 compl_length);
4747 }
4748 }
4749
John Marriott8c85a2a2024-05-20 19:18:26 +02004750 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004751 return OK;
4752}
4753
4754/*
4755 * Get the pattern, column and length for whole line completion or for the
4756 * complete() function.
4757 * Sets the global variables: compl_col, compl_length and compl_pattern.
4758 */
4759 static int
4760get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4761{
4762 compl_col = (colnr_T)getwhitecols(line);
4763 compl_length = (int)curs_col - (int)compl_col;
4764 if (compl_length < 0) // cursor in indent: empty pattern
4765 compl_length = 0;
4766 if (p_ic)
4767 compl_pattern = str_foldcase(line + compl_col, compl_length,
4768 NULL, 0);
4769 else
4770 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4771 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004772 {
4773 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004774 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004775 }
4776
4777 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004778
4779 return OK;
4780}
4781
4782/*
4783 * Get the pattern, column and length for filename completion.
4784 * Sets the global variables: compl_col, compl_length and compl_pattern.
4785 */
4786 static int
4787get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4788{
4789 // Go back to just before the first filename character.
4790 if (startcol > 0)
4791 {
4792 char_u *p = line + startcol;
4793
4794 MB_PTR_BACK(line, p);
4795 while (p > line && vim_isfilec(PTR2CHAR(p)))
4796 MB_PTR_BACK(line, p);
4797 if (p == line && vim_isfilec(PTR2CHAR(p)))
4798 startcol = 0;
4799 else
4800 startcol = (int)(p - line) + 1;
4801 }
4802
4803 compl_col += startcol;
4804 compl_length = (int)curs_col - startcol;
4805 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4806 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004807 {
4808 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004809 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004810 }
4811
4812 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004813
4814 return OK;
4815}
4816
4817/*
4818 * Get the pattern, column and length for command-line completion.
4819 * Sets the global variables: compl_col, compl_length and compl_pattern.
4820 */
4821 static int
4822get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4823{
4824 compl_pattern = vim_strnsave(line, curs_col);
4825 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004826 {
4827 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004828 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004829 }
4830 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004831 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004832 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004833 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4834 || compl_xp.xp_context == EXPAND_NOTHING)
4835 // No completion possible, use an empty pattern to get a
4836 // "pattern not found" message.
4837 compl_col = curs_col;
4838 else
4839 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4840 compl_length = curs_col - compl_col;
4841
4842 return OK;
4843}
4844
4845/*
4846 * Get the pattern, column and length for user defined completion ('omnifunc',
4847 * 'completefunc' and 'thesaurusfunc')
4848 * Sets the global variables: compl_col, compl_length and compl_pattern.
4849 * Uses the global variable: spell_bad_len
4850 */
4851 static int
4852get_userdefined_compl_info(colnr_T curs_col UNUSED)
4853{
4854 int ret = FAIL;
4855
4856#ifdef FEAT_COMPL_FUNC
4857 // Call user defined function 'completefunc' with "a:findstart"
4858 // set to 1 to obtain the length of text to use for completion.
4859 char_u *line;
4860 typval_T args[3];
4861 int col;
4862 char_u *funcname;
4863 pos_T pos;
4864 int save_State = State;
4865 callback_T *cb;
4866
4867 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4868 // length as a string
4869 funcname = get_complete_funcname(ctrl_x_mode);
4870 if (*funcname == NUL)
4871 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004872 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004873 ? "completefunc" : "omnifunc");
4874 return FAIL;
4875 }
4876
4877 args[0].v_type = VAR_NUMBER;
4878 args[0].vval.v_number = 1;
4879 args[1].v_type = VAR_STRING;
4880 args[1].vval.v_string = (char_u *)"";
4881 args[2].v_type = VAR_UNKNOWN;
4882 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004883 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004884 cb = get_insert_callback(ctrl_x_mode);
4885 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004886 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004887
4888 State = save_State;
4889 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004890 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004891 validate_cursor();
4892 if (!EQUAL_POS(curwin->w_cursor, pos))
4893 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004894 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004895 return FAIL;
4896 }
4897
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004898 // Return value -2 means the user complete function wants to cancel the
4899 // complete without an error, do the same if the function did not execute
4900 // successfully.
4901 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004902 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004903 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004904 if (col == -3)
4905 {
4906 ctrl_x_mode = CTRL_X_NORMAL;
4907 edit_submode = NULL;
4908 if (!shortmess(SHM_COMPLETIONMENU))
4909 msg_clr_cmdline();
4910 return FAIL;
4911 }
4912
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004913 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004914 // completion.
4915 compl_opt_refresh_always = FALSE;
4916 compl_opt_suppress_empty = FALSE;
4917
4918 if (col < 0)
4919 col = curs_col;
4920 compl_col = col;
4921 if (compl_col > curs_col)
4922 compl_col = curs_col;
4923
4924 // Setup variables for completion. Need to obtain "line" again,
4925 // it may have become invalid.
4926 line = ml_get(curwin->w_cursor.lnum);
4927 compl_length = curs_col - compl_col;
4928 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4929 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004930 {
4931 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004932 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004933 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004934
John Marriott8c85a2a2024-05-20 19:18:26 +02004935 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004936 ret = OK;
4937#endif
4938
4939 return ret;
4940}
4941
4942/*
4943 * Get the pattern, column and length for spell completion.
4944 * Sets the global variables: compl_col, compl_length and compl_pattern.
4945 * Uses the global variable: spell_bad_len
4946 */
4947 static int
4948get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4949{
4950 int ret = FAIL;
4951#ifdef FEAT_SPELL
4952 char_u *line;
4953
4954 if (spell_bad_len > 0)
4955 compl_col = curs_col - spell_bad_len;
4956 else
4957 compl_col = spell_word_start(startcol);
4958 if (compl_col >= (colnr_T)startcol)
4959 {
4960 compl_length = 0;
4961 compl_col = curs_col;
4962 }
4963 else
4964 {
4965 spell_expand_check_cap(compl_col);
4966 compl_length = (int)curs_col - compl_col;
4967 }
4968 // Need to obtain "line" again, it may have become invalid.
4969 line = ml_get(curwin->w_cursor.lnum);
4970 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4971 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004972 {
4973 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004974 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004975 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004976
John Marriott8c85a2a2024-05-20 19:18:26 +02004977 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004978 ret = OK;
4979#endif
4980
4981 return ret;
4982}
4983
4984/*
4985 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004986 * "startcol" - start column number of the completion pattern/text
4987 * "cur_col" - current cursor column
4988 * On return, "line_invalid" is set to TRUE, if the current line may have
4989 * become invalid and needs to be fetched again.
4990 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004991 */
4992 static int
4993compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4994{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004995 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004996 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4997 && !thesaurus_func_complete(ctrl_x_mode)))
4998 {
4999 return get_normal_compl_info(line, startcol, curs_col);
5000 }
5001 else if (ctrl_x_mode_line_or_eval())
5002 {
5003 return get_wholeline_compl_info(line, curs_col);
5004 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005005 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005006 {
5007 return get_filename_compl_info(line, startcol, curs_col);
5008 }
5009 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5010 {
5011 return get_cmdline_compl_info(line, curs_col);
5012 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005013 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005014 || thesaurus_func_complete(ctrl_x_mode))
5015 {
5016 if (get_userdefined_compl_info(curs_col) == FAIL)
5017 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005018 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005019 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005020 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005021 {
5022 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5023 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005024 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005025 }
5026 else
5027 {
5028 internal_error("ins_complete()");
5029 return FAIL;
5030 }
5031
5032 return OK;
5033}
5034
5035/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005036 * Continue an interrupted completion mode search in "line".
5037 *
5038 * If this same ctrl_x_mode has been interrupted use the text from
5039 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5040 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5041 * the same line as the cursor then fix it (the line has been split because it
5042 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5043 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005044 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005045 static void
5046ins_compl_continue_search(char_u *line)
5047{
5048 // it is a continued search
5049 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005050 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5051 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005052 {
5053 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5054 {
5055 // line (probably) wrapped, set compl_startpos to the
5056 // first non_blank in the line, if it is not a wordchar
5057 // include it to get a better pattern, but then we don't
5058 // want the "\\<" prefix, check it below
5059 compl_col = (colnr_T)getwhitecols(line);
5060 compl_startpos.col = compl_col;
5061 compl_startpos.lnum = curwin->w_cursor.lnum;
5062 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5063 }
5064 else
5065 {
5066 // S_IPOS was set when we inserted a word that was at the
5067 // beginning of the line, which means that we'll go to SOL
5068 // mode but first we need to redefine compl_startpos
5069 if (compl_cont_status & CONT_S_IPOS)
5070 {
5071 compl_cont_status |= CONT_SOL;
5072 compl_startpos.col = (colnr_T)(skipwhite(
5073 line + compl_length
5074 + compl_startpos.col) - line);
5075 }
5076 compl_col = compl_startpos.col;
5077 }
5078 compl_length = curwin->w_cursor.col - (int)compl_col;
5079 // IObuff is used to add a "word from the next line" would we
5080 // have enough space? just being paranoid
5081#define MIN_SPACE 75
5082 if (compl_length > (IOSIZE - MIN_SPACE))
5083 {
5084 compl_cont_status &= ~CONT_SOL;
5085 compl_length = (IOSIZE - MIN_SPACE);
5086 compl_col = curwin->w_cursor.col - compl_length;
5087 }
5088 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5089 if (compl_length < 1)
5090 compl_cont_status &= CONT_LOCAL;
5091 }
5092 else if (ctrl_x_mode_line_or_eval())
5093 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5094 else
5095 compl_cont_status = 0;
5096}
5097
5098/*
5099 * start insert mode completion
5100 */
5101 static int
5102ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005103{
5104 char_u *line;
5105 int startcol = 0; // column where searched text starts
5106 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005107 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005108 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005109 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005110
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005111 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005112
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005113 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005114 did_si = FALSE;
5115 can_si = FALSE;
5116 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005117 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005118 return FAIL;
5119
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005120 line = ml_get(curwin->w_cursor.lnum);
5121 curs_col = curwin->w_cursor.col;
5122 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005123
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005124 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5125 && compl_cont_mode == ctrl_x_mode)
5126 // this same ctrl-x_mode was interrupted previously. Continue the
5127 // completion.
5128 ins_compl_continue_search(line);
5129 else
5130 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005131
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005132 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005133 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005134 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005135 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005136 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5137 compl_cont_status = 0;
5138 compl_cont_status |= CONT_N_ADDS;
5139 compl_startpos = curwin->w_cursor;
5140 startcol = (int)curs_col;
5141 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005142 }
5143
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005144 // Work out completion pattern and original text -- webb
5145 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5146 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005147 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5148 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005149 // restore did_ai, so that adding comment leader works
5150 did_ai = save_did_ai;
5151 return FAIL;
5152 }
5153 // If "line" was changed while getting completion info get it again.
5154 if (line_invalid)
5155 line = ml_get(curwin->w_cursor.lnum);
5156
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005157 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005158 {
5159 edit_submode_pre = (char_u *)_(" Adding");
5160 if (ctrl_x_mode_line_or_eval())
5161 {
5162 // Insert a new line, keep indentation but ignore 'comments'.
5163 char_u *old = curbuf->b_p_com;
5164
5165 curbuf->b_p_com = (char_u *)"";
5166 compl_startpos.lnum = curwin->w_cursor.lnum;
5167 compl_startpos.col = compl_col;
5168 ins_eol('\r');
5169 curbuf->b_p_com = old;
5170 compl_length = 0;
5171 compl_col = curwin->w_cursor.col;
5172 }
5173 }
5174 else
5175 {
5176 edit_submode_pre = NULL;
5177 compl_startpos.col = compl_col;
5178 }
5179
5180 if (compl_cont_status & CONT_LOCAL)
5181 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5182 else
5183 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5184
5185 // If any of the original typed text has been changed we need to fix
5186 // the redo buffer.
5187 ins_compl_fixRedoBufForLeader(NULL);
5188
5189 // Always add completion for the original text.
5190 vim_free(compl_orig_text);
5191 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5192 if (p_ic)
5193 flags |= CP_ICASE;
5194 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
glepnir508e7852024-07-25 21:39:08 +02005195 -1, NULL, NULL, NULL, 0, flags, FALSE, -1) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005196 {
5197 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005198 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005199 VIM_CLEAR(compl_orig_text);
5200 return FAIL;
5201 }
5202
5203 // showmode might reset the internal line pointers, so it must
5204 // be called before line = ml_get(), or when this address is no
5205 // longer needed. -- Acevedo.
5206 edit_submode_extra = (char_u *)_("-- Searching...");
5207 edit_submode_highl = HLF_COUNT;
5208 showmode();
5209 edit_submode_extra = NULL;
5210 out_flush();
5211
5212 return OK;
5213}
5214
5215/*
5216 * display the completion status message
5217 */
5218 static void
5219ins_compl_show_statusmsg(void)
5220{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005221 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005222 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005223 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005224 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005225 ? (char_u *)_("Hit end of paragraph")
5226 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005227 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005228 }
5229
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005230 if (edit_submode_extra == NULL)
5231 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005232 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005233 {
5234 edit_submode_extra = (char_u *)_("Back at original");
5235 edit_submode_highl = HLF_W;
5236 }
5237 else if (compl_cont_status & CONT_S_IPOS)
5238 {
5239 edit_submode_extra = (char_u *)_("Word from other line");
5240 edit_submode_highl = HLF_COUNT;
5241 }
5242 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5243 {
5244 edit_submode_extra = (char_u *)_("The only match");
5245 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005246 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 }
5248 else
5249 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005250#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005251 // Update completion sequence number when needed.
5252 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005253 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005254#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005255 // The match should always have a sequence number now, this is
5256 // just a safety check.
5257 if (compl_curr_match->cp_number != -1)
5258 {
5259 // Space for 10 text chars. + 2x10-digit no.s = 31.
5260 // Translations may need more than twice that.
5261 static char_u match_ref[81];
5262
5263 if (compl_matches > 0)
5264 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005265 _("match %d of %d"),
5266 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005267 else
5268 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005269 _("match %d"),
5270 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005271 edit_submode_extra = match_ref;
5272 edit_submode_highl = HLF_R;
5273 if (dollar_vcol >= 0)
5274 curs_columns(FALSE);
5275 }
5276 }
5277 }
5278
5279 // Show a message about what (completion) mode we're in.
5280 if (!compl_opt_suppress_empty)
5281 {
5282 showmode();
5283 if (!shortmess(SHM_COMPLETIONMENU))
5284 {
5285 if (edit_submode_extra != NULL)
5286 {
5287 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005288 {
5289 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005290 msg_attr((char *)edit_submode_extra,
5291 edit_submode_highl < HLF_COUNT
5292 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005293 msg_hist_off = FALSE;
5294 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005295 }
5296 else
5297 msg_clr_cmdline(); // necessary for "noshowmode"
5298 }
5299 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005300}
5301
5302/*
5303 * Do Insert mode completion.
5304 * Called when character "c" was typed, which has a meaning for completion.
5305 * Returns OK if completion was done, FAIL if something failed (out of mem).
5306 */
5307 int
5308ins_complete(int c, int enable_pum)
5309{
5310 int n;
5311 int save_w_wrow;
5312 int save_w_leftcol;
5313 int insert_match;
5314
5315 compl_direction = ins_compl_key2dir(c);
5316 insert_match = ins_compl_use_match(c);
5317
5318 if (!compl_started)
5319 {
5320 if (ins_compl_start() == FAIL)
5321 return FAIL;
5322 }
5323 else if (insert_match && stop_arrow() == FAIL)
5324 return FAIL;
5325
5326 compl_shown_match = compl_curr_match;
5327 compl_shows_dir = compl_direction;
5328
5329 // Find next match (and following matches).
5330 save_w_wrow = curwin->w_wrow;
5331 save_w_leftcol = curwin->w_leftcol;
5332 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5333
5334 // may undisplay the popup menu
5335 ins_compl_upd_pum();
5336
5337 if (n > 1) // all matches have been found
5338 compl_matches = n;
5339 compl_curr_match = compl_shown_match;
5340 compl_direction = compl_shows_dir;
5341
5342 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5343 // mode.
5344 if (got_int && !global_busy)
5345 {
5346 (void)vgetc();
5347 got_int = FALSE;
5348 }
5349
5350 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005351 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005352 {
5353 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5354 // because we couldn't expand anything at first place, but if we used
5355 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5356 // (such as M in M'exico) if not tried already. -- Acevedo
5357 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005358 || compl_status_adding()
5359 || (ctrl_x_mode_not_default()
5360 && !ctrl_x_mode_path_patterns()
5361 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005362 compl_cont_status &= ~CONT_N_ADDS;
5363 }
5364
5365 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5366 compl_cont_status |= CONT_S_IPOS;
5367 else
5368 compl_cont_status &= ~CONT_S_IPOS;
5369
5370 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005371
5372 // Show the popup menu, unless we got interrupted.
5373 if (enable_pum && !compl_interrupted)
5374 show_pum(save_w_wrow, save_w_leftcol);
5375
5376 compl_was_interrupted = compl_interrupted;
5377 compl_interrupted = FALSE;
5378
5379 return OK;
5380}
5381
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005382/*
5383 * Remove (if needed) and show the popup menu
5384 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005385 static void
5386show_pum(int prev_w_wrow, int prev_w_leftcol)
5387{
5388 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005389 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005390 RedrawingDisabled = 0;
5391
5392 // If the cursor moved or the display scrolled we need to remove the pum
5393 // first.
5394 setcursor();
5395 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5396 ins_compl_del_pum();
5397
5398 ins_compl_show_pum();
5399 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005400
5401 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005402}
5403
5404/*
5405 * Looks in the first "len" chars. of "src" for search-metachars.
5406 * If dest is not NULL the chars. are copied there quoting (with
5407 * a backslash) the metachars, and dest would be NUL terminated.
5408 * Returns the length (needed) of dest
5409 */
5410 static unsigned
5411quote_meta(char_u *dest, char_u *src, int len)
5412{
5413 unsigned m = (unsigned)len + 1; // one extra for the NUL
5414
5415 for ( ; --len >= 0; src++)
5416 {
5417 switch (*src)
5418 {
5419 case '.':
5420 case '*':
5421 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005422 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005423 break;
5424 // FALLTHROUGH
5425 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005426 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005427 break;
5428 // FALLTHROUGH
5429 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005430 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005431 break;
5432 // FALLTHROUGH
5433 case '^': // currently it's not needed.
5434 case '$':
5435 m++;
5436 if (dest != NULL)
5437 *dest++ = '\\';
5438 break;
5439 }
5440 if (dest != NULL)
5441 *dest++ = *src;
5442 // Copy remaining bytes of a multibyte character.
5443 if (has_mbyte)
5444 {
5445 int i, mb_len;
5446
5447 mb_len = (*mb_ptr2len)(src) - 1;
5448 if (mb_len > 0 && len >= mb_len)
5449 for (i = 0; i < mb_len; ++i)
5450 {
5451 --len;
5452 ++src;
5453 if (dest != NULL)
5454 *dest++ = *src;
5455 }
5456 }
5457 }
5458 if (dest != NULL)
5459 *dest = NUL;
5460
5461 return m;
5462}
5463
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005464#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005465 void
5466free_insexpand_stuff(void)
5467{
5468 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005469# ifdef FEAT_EVAL
5470 free_callback(&cfu_cb);
5471 free_callback(&ofu_cb);
5472 free_callback(&tsrfu_cb);
5473# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005474}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005475#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005476
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005477#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005478/*
5479 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5480 * spelled word, if there is one.
5481 */
5482 static void
5483spell_back_to_badword(void)
5484{
5485 pos_T tpos = curwin->w_cursor;
5486
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005487 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005488 if (curwin->w_cursor.col != tpos.col)
5489 start_arrow(&tpos);
5490}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005491#endif