blob: 95076b7cd2793312d0ff24d9bce67edf19da7f33 [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;
glepnir753794b2024-08-20 19:58:44 +02001314 if (!compl_no_select)
1315 compl_shown_match = compl;
glepnir65407ce2024-07-06 16:09:19 +02001316 }
1317
1318 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1319 {
1320 cur = i;
glepnira218cc62024-06-03 19:32:39 +02001321 shown_match_ok = TRUE;
1322 }
1323
glepnirdca57fb2024-06-04 22:01:21 +02001324 // If there is no "no select" condition and the max fuzzy
1325 // score is positive, or there is no completion leader or the
1326 // leader length is zero, mark the shown match as valid and
1327 // reset the current index.
glepnira218cc62024-06-03 19:32:39 +02001328 if (!compl_no_select
1329 && (max_fuzzy_score > 0
1330 || (compl_leader == NULL || lead_len == 0)))
1331 {
glepnirf94c9c42024-06-14 21:11:56 +02001332 if (match_at_original_text(compl_shown_match))
glepnir105f7412024-06-15 15:32:22 +02001333 compl_shown_match = shown_compl;
glepnira218cc62024-06-03 19:32:39 +02001334 }
1335 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001336
1337 if (compl->cp_text[CPT_ABBR] != NULL)
1338 compl_match_array[i].pum_text =
1339 compl->cp_text[CPT_ABBR];
1340 else
1341 compl_match_array[i].pum_text = compl->cp_str;
1342 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1343 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
glepnira218cc62024-06-03 19:32:39 +02001344 compl_match_array[i].pum_score = compl->cp_score;
zeertzjq41008522024-07-27 13:21:49 +02001345 compl_match_array[i].pum_user_hlattr = compl->cp_user_hlattr;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001346 if (compl->cp_text[CPT_MENU] != NULL)
1347 compl_match_array[i++].pum_extra =
1348 compl->cp_text[CPT_MENU];
1349 else
1350 compl_match_array[i++].pum_extra = compl->cp_fname;
1351 }
1352
glepnira218cc62024-06-03 19:32:39 +02001353 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001354 {
1355 did_find_shown_match = TRUE;
1356
1357 // When the original text is the shown match don't set
1358 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001359 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001360 shown_match_ok = TRUE;
1361
1362 if (!shown_match_ok && shown_compl != NULL)
1363 {
1364 // The shown match isn't displayed, set it to the
1365 // previously displayed match.
1366 compl_shown_match = shown_compl;
1367 shown_match_ok = TRUE;
1368 }
1369 }
1370 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001371 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001372
glepnira218cc62024-06-03 19:32:39 +02001373 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001374 {
1375 for (i = 0; i < compl_match_arraysize; i++)
1376 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001377 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001378 qsort(compl_match_array, (size_t)compl_match_arraysize,
1379 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001380 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001381 }
glepnira218cc62024-06-03 19:32:39 +02001382
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001383 if (!shown_match_ok) // no displayed match at all
1384 cur = -1;
1385
1386 return cur;
1387}
1388
1389/*
1390 * Show the popup menu for the list of matches.
1391 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1392 */
1393 void
1394ins_compl_show_pum(void)
1395{
1396 int i;
1397 int cur = -1;
1398 colnr_T col;
1399
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001400 if (!pum_wanted() || !pum_enough_matches())
1401 return;
1402
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001403 // Update the screen later, before drawing the popup menu over it.
1404 pum_call_update_screen();
1405
1406 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001407 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001408 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001409 else
1410 {
1411 // popup menu already exists, only need to find the current item.
1412 for (i = 0; i < compl_match_arraysize; ++i)
1413 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1414 || compl_match_array[i].pum_text
1415 == compl_shown_match->cp_text[CPT_ABBR])
1416 {
1417 cur = i;
1418 break;
1419 }
1420 }
1421
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001422 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001423 {
1424#ifdef FEAT_EVAL
1425 if (compl_started && has_completechanged())
1426 trigger_complete_changed_event(cur);
1427#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001428 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001429 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001430
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001431 // In Replace mode when a $ is displayed at the end of the line only
1432 // part of the screen would be updated. We do need to redraw here.
1433 dollar_vcol = -1;
1434
1435 // Compute the screen column of the start of the completed text.
1436 // Use the cursor to get all wrapping and other settings right.
1437 col = curwin->w_cursor.col;
1438 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001439 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001440 pum_display(compl_match_array, compl_match_arraysize, cur);
1441 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001442
glepnircbb46b42024-02-03 18:11:13 +01001443 // After adding leader, set the current match to shown match.
1444 if (compl_started && compl_curr_match != compl_shown_match)
1445 compl_curr_match = compl_shown_match;
1446
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001447#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001448 if (has_completechanged())
1449 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001450#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001451}
1452
1453#define DICT_FIRST (1) // use just first element in "dict"
1454#define DICT_EXACT (2) // "dict" is the exact name of a file
1455
1456/*
glepnir40c1c332024-06-11 19:37:04 +02001457 * Get current completion leader
1458 */
1459 char_u *
1460ins_compl_leader(void)
1461{
glepnirf1891382024-06-17 18:35:25 +02001462 return compl_leader != NULL ? compl_leader : compl_orig_text;
glepnir40c1c332024-06-11 19:37:04 +02001463}
1464
1465/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001466 * Add any identifiers that match the given pattern "pat" in the list of
1467 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001468 */
1469 static void
1470ins_compl_dictionaries(
1471 char_u *dict_start,
1472 char_u *pat,
1473 int flags, // DICT_FIRST and/or DICT_EXACT
1474 int thesaurus) // Thesaurus completion
1475{
1476 char_u *dict = dict_start;
1477 char_u *ptr;
1478 char_u *buf;
1479 regmatch_T regmatch;
1480 char_u **files;
1481 int count;
1482 int save_p_scs;
1483 int dir = compl_direction;
1484
1485 if (*dict == NUL)
1486 {
1487#ifdef FEAT_SPELL
1488 // When 'dictionary' is empty and spell checking is enabled use
1489 // "spell".
1490 if (!thesaurus && curwin->w_p_spell)
1491 dict = (char_u *)"spell";
1492 else
1493#endif
1494 return;
1495 }
1496
1497 buf = alloc(LSIZE);
1498 if (buf == NULL)
1499 return;
1500 regmatch.regprog = NULL; // so that we can goto theend
1501
1502 // If 'infercase' is set, don't use 'smartcase' here
1503 save_p_scs = p_scs;
1504 if (curbuf->b_p_inf)
1505 p_scs = FALSE;
1506
1507 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1508 // to only match at the start of a line. Otherwise just match the
1509 // pattern. Also need to double backslashes.
1510 if (ctrl_x_mode_line_or_eval())
1511 {
1512 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1513 size_t len;
1514
1515 if (pat_esc == NULL)
1516 goto theend;
1517 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001518 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001519 if (ptr == NULL)
1520 {
1521 vim_free(pat_esc);
1522 goto theend;
1523 }
1524 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1525 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1526 vim_free(pat_esc);
1527 vim_free(ptr);
1528 }
1529 else
1530 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001531 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001532 if (regmatch.regprog == NULL)
1533 goto theend;
1534 }
1535
1536 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1537 regmatch.rm_ic = ignorecase(pat);
1538 while (*dict != NUL && !got_int && !compl_interrupted)
1539 {
1540 // copy one dictionary file name into buf
1541 if (flags == DICT_EXACT)
1542 {
1543 count = 1;
1544 files = &dict;
1545 }
1546 else
1547 {
1548 // Expand wildcards in the dictionary name, but do not allow
1549 // backticks (for security, the 'dict' option may have been set in
1550 // a modeline).
1551 copy_option_part(&dict, buf, LSIZE, ",");
1552# ifdef FEAT_SPELL
1553 if (!thesaurus && STRCMP(buf, "spell") == 0)
1554 count = -1;
1555 else
1556# endif
1557 if (vim_strchr(buf, '`') != NULL
1558 || expand_wildcards(1, &buf, &count, &files,
1559 EW_FILE|EW_SILENT) != OK)
1560 count = 0;
1561 }
1562
1563# ifdef FEAT_SPELL
1564 if (count == -1)
1565 {
1566 // Complete from active spelling. Skip "\<" in the pattern, we
1567 // don't use it as a RE.
1568 if (pat[0] == '\\' && pat[1] == '<')
1569 ptr = pat + 2;
1570 else
1571 ptr = pat;
1572 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1573 }
1574 else
1575# endif
1576 if (count > 0) // avoid warning for using "files" uninit
1577 {
1578 ins_compl_files(count, files, thesaurus, flags,
1579 &regmatch, buf, &dir);
1580 if (flags != DICT_EXACT)
1581 FreeWild(count, files);
1582 }
1583 if (flags != 0)
1584 break;
1585 }
1586
1587theend:
1588 p_scs = save_p_scs;
1589 vim_regfree(regmatch.regprog);
1590 vim_free(buf);
1591}
1592
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001593/*
1594 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1595 * skipping the word at 'skip_word'. Returns OK on success.
1596 */
1597 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001598thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001599 char_u *fname,
1600 char_u **buf_arg,
1601 int dir,
1602 char_u *skip_word)
1603{
1604 int status = OK;
1605 char_u *ptr;
1606 char_u *wstart;
1607
1608 // Add the other matches on the line
1609 ptr = *buf_arg;
1610 while (!got_int)
1611 {
1612 // Find start of the next word. Skip white
1613 // space and punctuation.
1614 ptr = find_word_start(ptr);
1615 if (*ptr == NUL || *ptr == NL)
1616 break;
1617 wstart = ptr;
1618
1619 // Find end of the word.
1620 if (has_mbyte)
1621 // Japanese words may have characters in
1622 // different classes, only separate words
1623 // with single-byte non-word characters.
1624 while (*ptr != NUL)
1625 {
1626 int l = (*mb_ptr2len)(ptr);
1627
1628 if (l < 2 && !vim_iswordc(*ptr))
1629 break;
1630 ptr += l;
1631 }
1632 else
1633 ptr = find_word_end(ptr);
1634
1635 // Add the word. Skip the regexp match.
1636 if (wstart != skip_word)
1637 {
1638 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1639 fname, dir, FALSE);
1640 if (status == FAIL)
1641 break;
1642 }
1643 }
1644
1645 *buf_arg = ptr;
1646 return status;
1647}
1648
1649/*
1650 * Process "count" dictionary/thesaurus "files" and add the text matching
1651 * "regmatch".
1652 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001653 static void
1654ins_compl_files(
1655 int count,
1656 char_u **files,
1657 int thesaurus,
1658 int flags,
1659 regmatch_T *regmatch,
1660 char_u *buf,
1661 int *dir)
1662{
1663 char_u *ptr;
1664 int i;
1665 FILE *fp;
1666 int add_r;
1667
1668 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1669 {
1670 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001671 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001672 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001673 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001674 vim_snprintf((char *)IObuff, IOSIZE,
1675 _("Scanning dictionary: %s"), (char *)files[i]);
1676 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1677 }
1678
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001679 if (fp == NULL)
1680 continue;
1681
1682 // Read dictionary file line by line.
1683 // Check each line for a match.
1684 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001685 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001686 ptr = buf;
1687 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001688 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001689 ptr = regmatch->startp[0];
1690 if (ctrl_x_mode_line_or_eval())
1691 ptr = find_line_end(ptr);
1692 else
1693 ptr = find_word_end(ptr);
1694 add_r = ins_compl_add_infercase(regmatch->startp[0],
1695 (int)(ptr - regmatch->startp[0]),
1696 p_ic, files[i], *dir, FALSE);
1697 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001698 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001699 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001700 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001701 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001702 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001703 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001704 if (add_r == OK)
1705 // if dir was BACKWARD then honor it just once
1706 *dir = FORWARD;
1707 else if (add_r == FAIL)
1708 break;
1709 // avoid expensive call to vim_regexec() when at end
1710 // of line
1711 if (*ptr == '\n' || got_int)
1712 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 line_breakcheck();
1715 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001716 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001718 }
1719}
1720
1721/*
1722 * Find the start of the next word.
1723 * Returns a pointer to the first char of the word. Also stops at a NUL.
1724 */
1725 char_u *
1726find_word_start(char_u *ptr)
1727{
1728 if (has_mbyte)
1729 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1730 ptr += (*mb_ptr2len)(ptr);
1731 else
1732 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1733 ++ptr;
1734 return ptr;
1735}
1736
1737/*
1738 * Find the end of the word. Assumes it starts inside a word.
1739 * Returns a pointer to just after the word.
1740 */
1741 char_u *
1742find_word_end(char_u *ptr)
1743{
1744 int start_class;
1745
1746 if (has_mbyte)
1747 {
1748 start_class = mb_get_class(ptr);
1749 if (start_class > 1)
1750 while (*ptr != NUL)
1751 {
1752 ptr += (*mb_ptr2len)(ptr);
1753 if (mb_get_class(ptr) != start_class)
1754 break;
1755 }
1756 }
1757 else
1758 while (vim_iswordc(*ptr))
1759 ++ptr;
1760 return ptr;
1761}
1762
1763/*
1764 * Find the end of the line, omitting CR and NL at the end.
1765 * Returns a pointer to just after the line.
1766 */
1767 static char_u *
1768find_line_end(char_u *ptr)
1769{
1770 char_u *s;
1771
1772 s = ptr + STRLEN(ptr);
1773 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1774 --s;
1775 return s;
1776}
1777
1778/*
1779 * Free the list of completions
1780 */
1781 static void
1782ins_compl_free(void)
1783{
1784 compl_T *match;
1785 int i;
1786
1787 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001788 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001789 VIM_CLEAR(compl_leader);
1790
1791 if (compl_first_match == NULL)
1792 return;
1793
1794 ins_compl_del_pum();
1795 pum_clear();
1796
1797 compl_curr_match = compl_first_match;
1798 do
1799 {
1800 match = compl_curr_match;
1801 compl_curr_match = compl_curr_match->cp_next;
1802 vim_free(match->cp_str);
1803 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001804 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001805 vim_free(match->cp_fname);
1806 for (i = 0; i < CPT_COUNT; ++i)
1807 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001808#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001809 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001810#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001811 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001812 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001813 compl_first_match = compl_curr_match = NULL;
1814 compl_shown_match = NULL;
1815 compl_old_match = NULL;
1816}
1817
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001818/*
1819 * Reset/clear the completion state.
1820 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001821 void
1822ins_compl_clear(void)
1823{
1824 compl_cont_status = 0;
1825 compl_started = FALSE;
1826 compl_matches = 0;
1827 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001828 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 VIM_CLEAR(compl_leader);
1830 edit_submode_extra = NULL;
1831 VIM_CLEAR(compl_orig_text);
1832 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001833#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001834 // clear v:completed_item
1835 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001836#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001837}
1838
1839/*
1840 * Return TRUE when Insert completion is active.
1841 */
1842 int
1843ins_compl_active(void)
1844{
1845 return compl_started;
1846}
1847
1848/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001849 * Selected one of the matches. When FALSE the match was edited or using the
1850 * longest common string.
1851 */
1852 int
1853ins_compl_used_match(void)
1854{
1855 return compl_used_match;
1856}
1857
1858/*
1859 * Initialize get longest common string.
1860 */
1861 void
1862ins_compl_init_get_longest(void)
1863{
1864 compl_get_longest = FALSE;
1865}
1866
1867/*
1868 * Returns TRUE when insert completion is interrupted.
1869 */
1870 int
1871ins_compl_interrupted(void)
1872{
1873 return compl_interrupted;
1874}
1875
1876/*
1877 * Returns TRUE if the <Enter> key selects a match in the completion popup
1878 * menu.
1879 */
1880 int
1881ins_compl_enter_selects(void)
1882{
1883 return compl_enter_selects;
1884}
1885
1886/*
1887 * Return the column where the text starts that is being completed
1888 */
1889 colnr_T
1890ins_compl_col(void)
1891{
1892 return compl_col;
1893}
1894
1895/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001896 * Return the length in bytes of the text being completed
1897 */
1898 int
1899ins_compl_len(void)
1900{
1901 return compl_length;
1902}
1903
1904/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001905 * Delete one character before the cursor and show the subset of the matches
1906 * that match the word that is now before the cursor.
1907 * Returns the character to be used, NUL if the work is done and another char
1908 * to be got from the user.
1909 */
1910 int
1911ins_compl_bs(void)
1912{
1913 char_u *line;
1914 char_u *p;
1915
1916 line = ml_get_curline();
1917 p = line + curwin->w_cursor.col;
1918 MB_PTR_BACK(line, p);
1919
1920 // Stop completion when the whole word was deleted. For Omni completion
1921 // allow the word to be deleted, we won't match everything.
1922 // Respect the 'backspace' option.
1923 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001924 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1925 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001926 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1927 - compl_length < 0))
1928 return K_BS;
1929
1930 // Deleted more than what was used to find matches or didn't finish
1931 // finding all matches: need to look for matches all over again.
1932 if (curwin->w_cursor.col <= compl_col + compl_length
1933 || ins_compl_need_restart())
1934 ins_compl_restart();
1935
1936 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001937 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001938 if (compl_leader == NULL)
1939 return K_BS;
1940
1941 ins_compl_new_leader();
1942 if (compl_shown_match != NULL)
1943 // Make sure current match is not a hidden item.
1944 compl_curr_match = compl_shown_match;
1945 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001946}
1947
1948/*
1949 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1950 * be called.
1951 */
1952 static int
1953ins_compl_need_restart(void)
1954{
1955 // Return TRUE if we didn't complete finding matches or when the
1956 // 'completefunc' returned "always" in the "refresh" dictionary item.
1957 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001958 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001959 && compl_opt_refresh_always);
1960}
1961
1962/*
1963 * Called after changing "compl_leader".
1964 * Show the popup menu with a different set of matches.
1965 * May also search for matches again if the previous search was interrupted.
1966 */
1967 static void
1968ins_compl_new_leader(void)
1969{
1970 ins_compl_del_pum();
1971 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001972 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001973 compl_used_match = FALSE;
1974
1975 if (compl_started)
1976 ins_compl_set_original_text(compl_leader);
1977 else
1978 {
1979#ifdef FEAT_SPELL
1980 spell_bad_len = 0; // need to redetect bad word
1981#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001982 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001983 // the popup menu display the changed text before the cursor. Set
1984 // "compl_restarting" to avoid that the first match is inserted.
1985 pum_call_update_screen();
1986#ifdef FEAT_GUI
1987 if (gui.in_use)
1988 {
1989 // Show the cursor after the match, not after the redrawn text.
1990 setcursor();
1991 out_flush_cursor(FALSE, FALSE);
1992 }
1993#endif
1994 compl_restarting = TRUE;
1995 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1996 compl_cont_status = 0;
1997 compl_restarting = FALSE;
1998 }
1999
2000 compl_enter_selects = !compl_used_match;
2001
2002 // Show the popup menu with a different set of matches.
2003 ins_compl_show_pum();
2004
2005 // Don't let Enter select the original text when there is no popup menu.
2006 if (compl_match_array == NULL)
2007 compl_enter_selects = FALSE;
2008}
2009
2010/*
2011 * Return the length of the completion, from the completion start column to
2012 * the cursor column. Making sure it never goes below zero.
2013 */
2014 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002015get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002016{
2017 int off = (int)curwin->w_cursor.col - (int)compl_col;
2018
2019 if (off < 0)
2020 return 0;
2021 return off;
2022}
2023
2024/*
2025 * Append one character to the match leader. May reduce the number of
2026 * matches.
2027 */
2028 void
2029ins_compl_addleader(int c)
2030{
2031 int cc;
2032
2033 if (stop_arrow() == FAIL)
2034 return;
2035 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2036 {
2037 char_u buf[MB_MAXBYTES + 1];
2038
2039 (*mb_char2bytes)(c, buf);
2040 buf[cc] = NUL;
2041 ins_char_bytes(buf, cc);
2042 if (compl_opt_refresh_always)
2043 AppendToRedobuff(buf);
2044 }
2045 else
2046 {
2047 ins_char(c);
2048 if (compl_opt_refresh_always)
2049 AppendCharToRedobuff(c);
2050 }
2051
2052 // If we didn't complete finding matches we must search again.
2053 if (ins_compl_need_restart())
2054 ins_compl_restart();
2055
2056 // When 'always' is set, don't reset compl_leader. While completing,
2057 // cursor doesn't point original position, changing compl_leader would
2058 // break redo.
2059 if (!compl_opt_refresh_always)
2060 {
2061 vim_free(compl_leader);
2062 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002063 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002064 if (compl_leader != NULL)
2065 ins_compl_new_leader();
2066 }
2067}
2068
2069/*
2070 * Setup for finding completions again without leaving CTRL-X mode. Used when
2071 * BS or a key was typed while still searching for matches.
2072 */
2073 static void
2074ins_compl_restart(void)
2075{
2076 ins_compl_free();
2077 compl_started = FALSE;
2078 compl_matches = 0;
2079 compl_cont_status = 0;
2080 compl_cont_mode = 0;
2081}
2082
2083/*
2084 * Set the first match, the original text.
2085 */
2086 static void
2087ins_compl_set_original_text(char_u *str)
2088{
2089 char_u *p;
2090
2091 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002092 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2093 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002094 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002095 {
2096 p = vim_strsave(str);
2097 if (p != NULL)
2098 {
2099 vim_free(compl_first_match->cp_str);
2100 compl_first_match->cp_str = p;
2101 }
2102 }
2103 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002104 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002105 {
2106 p = vim_strsave(str);
2107 if (p != NULL)
2108 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002109 vim_free(compl_first_match->cp_prev->cp_str);
2110 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002111 }
2112 }
2113}
2114
2115/*
2116 * Append one character to the match leader. May reduce the number of
2117 * matches.
2118 */
2119 void
2120ins_compl_addfrommatch(void)
2121{
2122 char_u *p;
2123 int len = (int)curwin->w_cursor.col - (int)compl_col;
2124 int c;
2125 compl_T *cp;
2126
2127 p = compl_shown_match->cp_str;
2128 if ((int)STRLEN(p) <= len) // the match is too short
2129 {
2130 // When still at the original match use the first entry that matches
2131 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002132 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002133 return;
2134
2135 p = NULL;
2136 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002137 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002138 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002139 if (compl_leader == NULL
2140 || ins_compl_equal(cp, compl_leader,
2141 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002142 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002143 p = cp->cp_str;
2144 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002145 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002146 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002147 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002148 return;
2149 }
2150 p += len;
2151 c = PTR2CHAR(p);
2152 ins_compl_addleader(c);
2153}
2154
2155/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002156 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002157 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2158 * compl_cont_mode and compl_cont_status.
2159 * Returns TRUE when the character is not to be inserted.
2160 */
2161 static int
2162set_ctrl_x_mode(int c)
2163{
2164 int retval = FALSE;
2165
2166 switch (c)
2167 {
2168 case Ctrl_E:
2169 case Ctrl_Y:
2170 // scroll the window one line up or down
2171 ctrl_x_mode = CTRL_X_SCROLL;
2172 if (!(State & REPLACE_FLAG))
2173 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2174 else
2175 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2176 edit_submode_pre = NULL;
2177 showmode();
2178 break;
2179 case Ctrl_L:
2180 // complete whole line
2181 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2182 break;
2183 case Ctrl_F:
2184 // complete filenames
2185 ctrl_x_mode = CTRL_X_FILES;
2186 break;
2187 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002188 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002189 ctrl_x_mode = CTRL_X_DICTIONARY;
2190 break;
2191 case Ctrl_R:
2192 // Register insertion without exiting CTRL-X mode
2193 // Simply allow ^R to happen without affecting ^X mode
2194 break;
2195 case Ctrl_T:
2196 // complete words from a thesaurus
2197 ctrl_x_mode = CTRL_X_THESAURUS;
2198 break;
2199#ifdef FEAT_COMPL_FUNC
2200 case Ctrl_U:
2201 // user defined completion
2202 ctrl_x_mode = CTRL_X_FUNCTION;
2203 break;
2204 case Ctrl_O:
2205 // omni completion
2206 ctrl_x_mode = CTRL_X_OMNI;
2207 break;
2208#endif
2209 case 's':
2210 case Ctrl_S:
2211 // complete spelling suggestions
2212 ctrl_x_mode = CTRL_X_SPELL;
2213#ifdef FEAT_SPELL
2214 ++emsg_off; // Avoid getting the E756 error twice.
2215 spell_back_to_badword();
2216 --emsg_off;
2217#endif
2218 break;
2219 case Ctrl_RSB:
2220 // complete tag names
2221 ctrl_x_mode = CTRL_X_TAGS;
2222 break;
2223#ifdef FEAT_FIND_ID
2224 case Ctrl_I:
2225 case K_S_TAB:
2226 // complete keywords from included files
2227 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2228 break;
2229 case Ctrl_D:
2230 // complete definitions from included files
2231 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2232 break;
2233#endif
2234 case Ctrl_V:
2235 case Ctrl_Q:
2236 // complete vim commands
2237 ctrl_x_mode = CTRL_X_CMDLINE;
2238 break;
2239 case Ctrl_Z:
2240 // stop completion
2241 ctrl_x_mode = CTRL_X_NORMAL;
2242 edit_submode = NULL;
2243 showmode();
2244 retval = TRUE;
2245 break;
2246 case Ctrl_P:
2247 case Ctrl_N:
2248 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2249 // just started ^X mode, or there were enough ^X's to cancel
2250 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2251 // do normal expansion when interrupting a different mode (say
2252 // ^X^F^X^P or ^P^X^X^P, see below)
2253 // nothing changes if interrupting mode 0, (eg, the flag
2254 // doesn't change when going to ADDING mode -- Acevedo
2255 if (!(compl_cont_status & CONT_INTRPT))
2256 compl_cont_status |= CONT_LOCAL;
2257 else if (compl_cont_mode != 0)
2258 compl_cont_status &= ~CONT_LOCAL;
2259 // FALLTHROUGH
2260 default:
2261 // If we have typed at least 2 ^X's... for modes != 0, we set
2262 // compl_cont_status = 0 (eg, as if we had just started ^X
2263 // mode).
2264 // For mode 0, we set "compl_cont_mode" to an impossible
2265 // value, in both cases ^X^X can be used to restart the same
2266 // mode (avoiding ADDING mode).
2267 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2268 // 'complete' and local ^P expansions respectively.
2269 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2270 // mode -- Acevedo
2271 if (c == Ctrl_X)
2272 {
2273 if (compl_cont_mode != 0)
2274 compl_cont_status = 0;
2275 else
2276 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2277 }
2278 ctrl_x_mode = CTRL_X_NORMAL;
2279 edit_submode = NULL;
2280 showmode();
2281 break;
2282 }
2283
2284 return retval;
2285}
2286
2287/*
2288 * Stop insert completion mode
2289 */
2290 static int
2291ins_compl_stop(int c, int prev_mode, int retval)
2292{
2293 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002294 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002295
2296 // Get here when we have finished typing a sequence of ^N and
2297 // ^P or other completion characters in CTRL-X mode. Free up
2298 // memory that was used, and make sure we can redo the insert.
2299 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2300 {
2301 // If any of the original typed text has been changed, eg when
2302 // ignorecase is set, we must add back-spaces to the redo
2303 // buffer. We add as few as necessary to delete just the part
2304 // of the original text that has changed.
2305 // When using the longest match, edited the match or used
2306 // CTRL-E then don't use the current match.
2307 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2308 ptr = compl_curr_match->cp_str;
2309 else
2310 ptr = NULL;
2311 ins_compl_fixRedoBufForLeader(ptr);
2312 }
2313
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002314 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002315
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002316 // When completing whole lines: fix indent for 'cindent'.
2317 // Otherwise, break line if it's too long.
2318 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2319 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002320 // re-indent the current line
2321 if (want_cindent)
2322 {
2323 do_c_expr_indent();
2324 want_cindent = FALSE; // don't do it again
2325 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002326 }
2327 else
2328 {
2329 int prev_col = curwin->w_cursor.col;
2330
2331 // put the cursor on the last char, for 'tw' formatting
2332 if (prev_col > 0)
2333 dec_cursor();
2334 // only format when something was inserted
2335 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2336 insertchar(NUL, 0, -1);
2337 if (prev_col > 0
2338 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2339 inc_cursor();
2340 }
2341
2342 // If the popup menu is displayed pressing CTRL-Y means accepting
2343 // the selection without inserting anything. When
2344 // compl_enter_selects is set the Enter key does the same.
2345 if ((c == Ctrl_Y || (compl_enter_selects
2346 && (c == CAR || c == K_KENTER || c == NL)))
2347 && pum_visible())
2348 retval = TRUE;
2349
2350 // CTRL-E means completion is Ended, go back to the typed text.
2351 // but only do this, if the Popup is still visible
2352 if (c == Ctrl_E)
2353 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002354 char_u *p = NULL;
2355
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002356 ins_compl_delete();
2357 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002358 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002359 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002360 p = compl_orig_text;
2361 if (p != NULL)
2362 {
2363 int compl_len = get_compl_len();
2364 int len = (int)STRLEN(p);
2365
2366 if (len > compl_len)
2367 ins_bytes_len(p + compl_len, len - compl_len);
2368 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002369 retval = TRUE;
2370 }
2371
2372 auto_format(FALSE, TRUE);
2373
2374 // Trigger the CompleteDonePre event to give scripts a chance to
2375 // act upon the completion before clearing the info, and restore
2376 // ctrl_x_mode, so that complete_info() can be used.
2377 ctrl_x_mode = prev_mode;
2378 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2379
2380 ins_compl_free();
2381 compl_started = FALSE;
2382 compl_matches = 0;
2383 if (!shortmess(SHM_COMPLETIONMENU))
2384 msg_clr_cmdline(); // necessary for "noshowmode"
2385 ctrl_x_mode = CTRL_X_NORMAL;
2386 compl_enter_selects = FALSE;
2387 if (edit_submode != NULL)
2388 {
2389 edit_submode = NULL;
2390 showmode();
2391 }
2392
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002393 if (c == Ctrl_C && cmdwin_type != 0)
2394 // Avoid the popup menu remains displayed when leaving the
2395 // command line window.
2396 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002397 // Indent now if a key was typed that is in 'cinkeys'.
2398 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2399 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002400 // Trigger the CompleteDone event to give scripts a chance to act
2401 // upon the end of completion.
2402 ins_apply_autocmds(EVENT_COMPLETEDONE);
2403
2404 return retval;
2405}
2406
2407/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002408 * Prepare for Insert mode completion, or stop it.
2409 * Called just after typing a character in Insert mode.
2410 * Returns TRUE when the character is not to be inserted;
2411 */
2412 int
2413ins_compl_prep(int c)
2414{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002415 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002416 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002417
2418 // Forget any previous 'special' messages if this is actually
2419 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2420 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2421 edit_submode_extra = NULL;
2422
zeertzjq440d4cb2023-03-02 17:51:32 +00002423 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002424 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002425 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002426 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002427 return retval;
2428
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002429#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002430 // Ignore mouse events in a popup window
2431 if (is_mouse_key(c))
2432 {
2433 // Ignore drag and release events, the position does not need to be in
2434 // the popup and it may have just closed.
2435 if (c == K_LEFTRELEASE
2436 || c == K_LEFTRELEASE_NM
2437 || c == K_MIDDLERELEASE
2438 || c == K_RIGHTRELEASE
2439 || c == K_X1RELEASE
2440 || c == K_X2RELEASE
2441 || c == K_LEFTDRAG
2442 || c == K_MIDDLEDRAG
2443 || c == K_RIGHTDRAG
2444 || c == K_X1DRAG
2445 || c == K_X2DRAG)
2446 return retval;
2447 if (popup_visible)
2448 {
2449 int row = mouse_row;
2450 int col = mouse_col;
2451 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2452
2453 if (wp != NULL && WIN_IS_POPUP(wp))
2454 return retval;
2455 }
2456 }
2457#endif
2458
zeertzjqdca29d92021-08-31 19:12:51 +02002459 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2460 {
2461 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2462 || !vim_is_ctrl_x_key(c))
2463 {
2464 // Not starting another completion mode.
2465 ctrl_x_mode = CTRL_X_CMDLINE;
2466
2467 // CTRL-X CTRL-Z should stop completion without inserting anything
2468 if (c == Ctrl_Z)
2469 retval = TRUE;
2470 }
2471 else
2472 {
2473 ctrl_x_mode = CTRL_X_CMDLINE;
2474
2475 // Other CTRL-X keys first stop completion, then start another
2476 // completion mode.
2477 ins_compl_prep(' ');
2478 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2479 }
2480 }
2481
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002482 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002483 if (ctrl_x_mode_not_defined_yet()
2484 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002485 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002486 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002487 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002488 }
2489
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002490 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002491 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2492 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002493 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002494 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002495 {
2496 // We're already in CTRL-X mode, do we stay in it?
2497 if (!vim_is_ctrl_x_key(c))
2498 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002499 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002500 ctrl_x_mode = CTRL_X_NORMAL;
2501 else
2502 ctrl_x_mode = CTRL_X_FINISHED;
2503 edit_submode = NULL;
2504 }
2505 showmode();
2506 }
2507
2508 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2509 {
2510 // Show error message from attempted keyword completion (probably
2511 // 'Pattern not found') until another key is hit, then go back to
2512 // showing what mode we are in.
2513 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002514 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002515 && c != Ctrl_R && !ins_compl_pum_key(c))
2516 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002517 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002518 }
2519 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2520 // Trigger the CompleteDone event to give scripts a chance to act
2521 // upon the (possibly failed) completion.
2522 ins_apply_autocmds(EVENT_COMPLETEDONE);
2523
LemonBoy2bf52dd2022-04-09 18:17:34 +01002524 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002525
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002526 // reset continue_* if we left expansion-mode, if we stay they'll be
2527 // (re)set properly in ins_complete()
2528 if (!vim_is_ctrl_x_key(c))
2529 {
2530 compl_cont_status = 0;
2531 compl_cont_mode = 0;
2532 }
2533
2534 return retval;
2535}
2536
2537/*
2538 * Fix the redo buffer for the completion leader replacing some of the typed
2539 * text. This inserts backspaces and appends the changed text.
2540 * "ptr" is the known leader text or NUL.
2541 */
2542 static void
2543ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2544{
2545 int len;
2546 char_u *p;
2547 char_u *ptr = ptr_arg;
2548
2549 if (ptr == NULL)
2550 {
2551 if (compl_leader != NULL)
2552 ptr = compl_leader;
2553 else
2554 return; // nothing to do
2555 }
2556 if (compl_orig_text != NULL)
2557 {
2558 p = compl_orig_text;
2559 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2560 ;
2561 if (len > 0)
2562 len -= (*mb_head_off)(p, p + len);
2563 for (p += len; *p != NUL; MB_PTR_ADV(p))
2564 AppendCharToRedobuff(K_BS);
2565 }
2566 else
2567 len = 0;
2568 if (ptr != NULL)
2569 AppendToRedobuffLit(ptr + len, -1);
2570}
2571
2572/*
2573 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2574 * (depending on flag) starting from buf and looking for a non-scanned
2575 * buffer (other than curbuf). curbuf is special, if it is called with
2576 * buf=curbuf then it has to be the first call for a given flag/expansion.
2577 *
2578 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2579 */
2580 static buf_T *
2581ins_compl_next_buf(buf_T *buf, int flag)
2582{
2583 static win_T *wp = NULL;
2584
2585 if (flag == 'w') // just windows
2586 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002587 if (buf == curbuf || !win_valid(wp))
2588 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002589 wp = curwin;
2590 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2591 && wp->w_buffer->b_scanned)
2592 ;
2593 buf = wp->w_buffer;
2594 }
2595 else
2596 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2597 // (unlisted buffers)
2598 // When completing whole lines skip unloaded buffers.
2599 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2600 && ((flag == 'U'
2601 ? buf->b_p_bl
2602 : (!buf->b_p_bl
2603 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2604 || buf->b_scanned))
2605 ;
2606 return buf;
2607}
2608
2609#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002610
2611# ifdef FEAT_EVAL
2612static callback_T cfu_cb; // 'completefunc' callback function
2613static callback_T ofu_cb; // 'omnifunc' callback function
2614static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2615# endif
2616
2617/*
2618 * Copy a global callback function to a buffer local callback.
2619 */
2620 static void
2621copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2622{
2623 free_callback(bufcb);
2624 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2625 copy_callback(bufcb, globcb);
2626}
2627
2628/*
2629 * Parse the 'completefunc' option value and set the callback function.
2630 * Invoked when the 'completefunc' option is set. The option value can be a
2631 * name of a function (string), or function(<name>) or funcref(<name>) or a
2632 * lambda expression.
2633 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002634 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002635did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002636{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002637 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2638 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002639
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002640 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002641
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002642 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002643}
2644
2645/*
2646 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002647 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002648 */
2649 void
2650set_buflocal_cfu_callback(buf_T *buf UNUSED)
2651{
2652# ifdef FEAT_EVAL
2653 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2654# endif
2655}
2656
2657/*
2658 * Parse the 'omnifunc' option value and set the callback function.
2659 * Invoked when the 'omnifunc' option is set. The option value can be a
2660 * name of a function (string), or function(<name>) or funcref(<name>) or a
2661 * lambda expression.
2662 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002663 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002664did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002665{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002666 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2667 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002668
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002669 set_buflocal_ofu_callback(curbuf);
2670 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002671}
2672
2673/*
2674 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002675 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002676 */
2677 void
2678set_buflocal_ofu_callback(buf_T *buf UNUSED)
2679{
2680# ifdef FEAT_EVAL
2681 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2682# endif
2683}
2684
2685/*
2686 * Parse the 'thesaurusfunc' option value and set the callback function.
2687 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2688 * name of a function (string), or function(<name>) or funcref(<name>) or a
2689 * lambda expression.
2690 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002691 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002692did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002693{
2694 int retval;
2695
2696 if (*curbuf->b_p_tsrfu != NUL)
2697 {
2698 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002699 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2700 &curbuf->b_tsrfu_cb);
2701 }
2702 else
2703 {
2704 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002705 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2706 }
2707
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002708 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002709}
2710
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002711/*
2712 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002713 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002714 */
2715 int
2716set_ref_in_insexpand_funcs(int copyID)
2717{
2718 int abort = FALSE;
2719
2720 abort = set_ref_in_callback(&cfu_cb, copyID);
2721 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2722 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2723
2724 return abort;
2725}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002726
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002727/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002728 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002729 */
2730 static char_u *
2731get_complete_funcname(int type)
2732{
2733 switch (type)
2734 {
2735 case CTRL_X_FUNCTION:
2736 return curbuf->b_p_cfu;
2737 case CTRL_X_OMNI:
2738 return curbuf->b_p_ofu;
2739 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002740 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002741 default:
2742 return (char_u *)"";
2743 }
2744}
2745
2746/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002747 * Get the callback to use for insert mode completion.
2748 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002749 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002750get_insert_callback(int type)
2751{
2752 if (type == CTRL_X_FUNCTION)
2753 return &curbuf->b_cfu_cb;
2754 if (type == CTRL_X_OMNI)
2755 return &curbuf->b_ofu_cb;
2756 // CTRL_X_THESAURUS
2757 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2758}
2759
2760/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002761 * Execute user defined complete function 'completefunc', 'omnifunc' or
2762 * 'thesaurusfunc', and get matches in "matches".
2763 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002764 */
2765 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002766expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002767{
2768 list_T *matchlist = NULL;
2769 dict_T *matchdict = NULL;
2770 typval_T args[3];
2771 char_u *funcname;
2772 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002773 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002774 typval_T rettv;
2775 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002776 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002777
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002778 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002779 if (*funcname == NUL)
2780 return;
2781
2782 // Call 'completefunc' to obtain the list of matches.
2783 args[0].v_type = VAR_NUMBER;
2784 args[0].vval.v_number = 0;
2785 args[1].v_type = VAR_STRING;
2786 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2787 args[2].v_type = VAR_UNKNOWN;
2788
2789 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002790 // Lock the text to avoid weird things from happening. Also disallow
2791 // switching to another window, it should not be needed and may end up in
2792 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002793 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002794
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795 cb = get_insert_callback(type);
2796 retval = call_callback(cb, 0, &rettv, 2, args);
2797
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002798 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002799 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002800 {
2801 switch (rettv.v_type)
2802 {
2803 case VAR_LIST:
2804 matchlist = rettv.vval.v_list;
2805 break;
2806 case VAR_DICT:
2807 matchdict = rettv.vval.v_dict;
2808 break;
2809 case VAR_SPECIAL:
2810 if (rettv.vval.v_number == VVAL_NONE)
2811 compl_opt_suppress_empty = TRUE;
2812 // FALLTHROUGH
2813 default:
2814 // TODO: Give error message?
2815 clear_tv(&rettv);
2816 break;
2817 }
2818 }
zeertzjqcfe45652022-05-27 17:26:55 +01002819 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002821 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002822 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823 validate_cursor();
2824 if (!EQUAL_POS(curwin->w_cursor, pos))
2825 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002826 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002827 goto theend;
2828 }
2829
2830 if (matchlist != NULL)
2831 ins_compl_add_list(matchlist);
2832 else if (matchdict != NULL)
2833 ins_compl_add_dict(matchdict);
2834
2835theend:
2836 // Restore State, it might have been changed.
2837 State = save_State;
2838
2839 if (matchdict != NULL)
2840 dict_unref(matchdict);
2841 if (matchlist != NULL)
2842 list_unref(matchlist);
2843}
2844#endif // FEAT_COMPL_FUNC
2845
2846#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2847/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002848 * Add a match to the list of matches from a typeval_T.
2849 * If the given string is already in the list of completions, then return
2850 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2851 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002852 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002853 */
2854 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002855ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002856{
2857 char_u *word;
2858 int dup = FALSE;
2859 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002860 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002861 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002862 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002863 int status;
zeertzjq41008522024-07-27 13:21:49 +02002864 char_u *user_hlname;
2865 int user_hlattr = -1;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002866
Bram Moolenaar08928322020-01-04 14:32:48 +01002867 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002868 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2869 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002870 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2871 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2872 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2873 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2874 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
zeertzjq41008522024-07-27 13:21:49 +02002875 user_hlname = dict_get_string(tv->vval.v_dict, "hl_group", FALSE);
2876 if (user_hlname != NULL && *user_hlname != NUL)
2877 user_hlattr = syn_name2attr(user_hlname);
glepnir508e7852024-07-25 21:39:08 +02002878
Bram Moolenaard61efa52022-07-23 09:52:04 +01002879 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2880 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2881 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002882 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002883 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2884 dup = dict_get_number(tv->vval.v_dict, "dup");
2885 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2886 empty = dict_get_number(tv->vval.v_dict, "empty");
2887 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2888 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002889 flags |= CP_EQUAL;
2890 }
2891 else
2892 {
2893 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002894 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002895 }
2896 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002897 {
2898 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002899 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002900 }
glepnir508e7852024-07-25 21:39:08 +02002901 status = ins_compl_add(word, -1, NULL, cptext,
zeertzjq41008522024-07-27 13:21:49 +02002902 &user_data, dir, flags, dup, user_hlattr);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002903 if (status != OK)
2904 clear_tv(&user_data);
2905 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002906}
2907
2908/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002909 * Add completions from a list.
2910 */
2911 static void
2912ins_compl_add_list(list_T *list)
2913{
2914 listitem_T *li;
2915 int dir = compl_direction;
2916
2917 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002918 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002919 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002920 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002921 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002922 // if dir was BACKWARD then honor it just once
2923 dir = FORWARD;
2924 else if (did_emsg)
2925 break;
2926 }
2927}
2928
2929/*
2930 * Add completions from a dict.
2931 */
2932 static void
2933ins_compl_add_dict(dict_T *dict)
2934{
2935 dictitem_T *di_refresh;
2936 dictitem_T *di_words;
2937
2938 // Check for optional "refresh" item.
2939 compl_opt_refresh_always = FALSE;
2940 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2941 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2942 {
2943 char_u *v = di_refresh->di_tv.vval.v_string;
2944
2945 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2946 compl_opt_refresh_always = TRUE;
2947 }
2948
2949 // Add completions from a "words" list.
2950 di_words = dict_find(dict, (char_u *)"words", 5);
2951 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2952 ins_compl_add_list(di_words->di_tv.vval.v_list);
2953}
2954
2955/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002956 * Start completion for the complete() function.
2957 * "startcol" is where the matched text starts (1 is first column).
2958 * "list" is the list of matches.
2959 */
2960 static void
2961set_completion(colnr_T startcol, list_T *list)
2962{
2963 int save_w_wrow = curwin->w_wrow;
2964 int save_w_leftcol = curwin->w_leftcol;
2965 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02002966 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02002967 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
2968 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
2969 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002970
2971 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002972 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002973 ins_compl_prep(' ');
2974 ins_compl_clear();
2975 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002976 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002977
2978 compl_direction = FORWARD;
2979 if (startcol > curwin->w_cursor.col)
2980 startcol = curwin->w_cursor.col;
2981 compl_col = startcol;
2982 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2983 // compl_pattern doesn't need to be set
2984 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2985 if (p_ic)
2986 flags |= CP_ICASE;
2987 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002988 -1, NULL, NULL, NULL, 0,
glepnir508e7852024-07-25 21:39:08 +02002989 flags | CP_FAST, FALSE, -1) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002990 return;
2991
2992 ctrl_x_mode = CTRL_X_EVAL;
2993
2994 ins_compl_add_list(list);
2995 compl_matches = ins_compl_make_cyclic();
2996 compl_started = TRUE;
2997 compl_used_match = TRUE;
2998 compl_cont_status = 0;
2999
3000 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003001 int no_select = compl_no_select || compl_longest;
3002 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003003 {
3004 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003005 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003006 // Down/Up has no real effect.
3007 ins_complete(K_UP, FALSE);
3008 }
3009 else
3010 ins_complete(Ctrl_N, FALSE);
3011 compl_enter_selects = compl_no_insert;
3012
3013 // Lazily show the popup menu, unless we got interrupted.
3014 if (!compl_interrupted)
3015 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003016 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003017 out_flush();
3018}
3019
3020/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003021 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003022 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003023 void
3024f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003025{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003026 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003027
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003028 if (in_vim9script()
3029 && (check_for_number_arg(argvars, 0) == FAIL
3030 || check_for_list_arg(argvars, 1) == FAIL))
3031 return;
3032
Bram Moolenaar24959102022-05-07 20:01:16 +01003033 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003034 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003035 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003036 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003037 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003038
3039 // Check for undo allowed here, because if something was already inserted
3040 // the line was already saved for undo and this check isn't done.
3041 if (!undo_allowed())
3042 return;
3043
Bram Moolenaard83392a2022-09-01 12:22:46 +01003044 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003045 {
3046 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3047 if (startcol > 0)
3048 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003049 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003050}
3051
3052/*
3053 * "complete_add()" function
3054 */
3055 void
3056f_complete_add(typval_T *argvars, typval_T *rettv)
3057{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003058 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003059 return;
3060
Bram Moolenaar440cf092021-04-03 20:13:30 +02003061 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003062}
3063
3064/*
3065 * "complete_check()" function
3066 */
3067 void
3068f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3069{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003070 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003071 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003072
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003073 ins_compl_check_keys(0, TRUE);
3074 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003075
3076 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003077}
3078
3079/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003080 * Return Insert completion mode name string
3081 */
3082 static char_u *
3083ins_compl_mode(void)
3084{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003085 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003086 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3087
3088 return (char_u *)"";
3089}
3090
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003091/*
3092 * Assign the sequence number to all the completion matches which don't have
3093 * one assigned yet.
3094 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003095 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003096ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003097{
3098 int number = 0;
3099 compl_T *match;
3100
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003101 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003102 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003103 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003104 // This should normally succeed already at the first loop
3105 // cycle, so it's fast!
3106 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003107 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003108 if (match->cp_number != -1)
3109 {
3110 number = match->cp_number;
3111 break;
3112 }
3113 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003114 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003115 for (match = match->cp_next;
3116 match != NULL && match->cp_number == -1;
3117 match = match->cp_next)
3118 match->cp_number = ++number;
3119 }
3120 else // BACKWARD
3121 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003122 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003123 // number. This should normally succeed already at the
3124 // first loop cycle, so it's fast!
3125 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003126 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003127 if (match->cp_number != -1)
3128 {
3129 number = match->cp_number;
3130 break;
3131 }
3132 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003133 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003134 for (match = match->cp_prev; match
3135 && match->cp_number == -1;
3136 match = match->cp_prev)
3137 match->cp_number = ++number;
3138 }
3139}
3140
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003141/*
3142 * Get complete information
3143 */
3144 static void
3145get_complete_info(list_T *what_list, dict_T *retdict)
3146{
3147 int ret = OK;
3148 listitem_T *item;
3149#define CI_WHAT_MODE 0x01
3150#define CI_WHAT_PUM_VISIBLE 0x02
3151#define CI_WHAT_ITEMS 0x04
3152#define CI_WHAT_SELECTED 0x08
3153#define CI_WHAT_INSERTED 0x10
3154#define CI_WHAT_ALL 0xff
3155 int what_flag;
3156
3157 if (what_list == NULL)
3158 what_flag = CI_WHAT_ALL;
3159 else
3160 {
3161 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003162 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003163 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003164 {
3165 char_u *what = tv_get_string(&item->li_tv);
3166
3167 if (STRCMP(what, "mode") == 0)
3168 what_flag |= CI_WHAT_MODE;
3169 else if (STRCMP(what, "pum_visible") == 0)
3170 what_flag |= CI_WHAT_PUM_VISIBLE;
3171 else if (STRCMP(what, "items") == 0)
3172 what_flag |= CI_WHAT_ITEMS;
3173 else if (STRCMP(what, "selected") == 0)
3174 what_flag |= CI_WHAT_SELECTED;
3175 else if (STRCMP(what, "inserted") == 0)
3176 what_flag |= CI_WHAT_INSERTED;
3177 }
3178 }
3179
3180 if (ret == OK && (what_flag & CI_WHAT_MODE))
3181 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3182
3183 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3184 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3185
Girish Palya8950bf72024-03-20 20:07:29 +01003186 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003187 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003188 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003189 dict_T *di;
3190 compl_T *match;
3191 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003192
Girish Palya8950bf72024-03-20 20:07:29 +01003193 if (what_flag & CI_WHAT_ITEMS)
3194 {
3195 li = list_alloc();
3196 if (li == NULL)
3197 return;
3198 ret = dict_add_list(retdict, "items", li);
3199 }
3200 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3201 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3202 ins_compl_update_sequence_numbers();
3203 if (ret == OK && compl_first_match != NULL)
3204 {
3205 int list_idx = 0;
3206 match = compl_first_match;
3207 do
3208 {
3209 if (!match_at_original_text(match))
3210 {
3211 if (what_flag & CI_WHAT_ITEMS)
3212 {
3213 di = dict_alloc();
3214 if (di == NULL)
3215 return;
3216 ret = list_append_dict(li, di);
3217 if (ret != OK)
3218 return;
3219 dict_add_string(di, "word", match->cp_str);
3220 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3221 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3222 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3223 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3224 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3225 // Add an empty string for backwards compatibility
3226 dict_add_string(di, "user_data", (char_u *)"");
3227 else
3228 dict_add_tv(di, "user_data", &match->cp_user_data);
3229 }
3230 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3231 selected_idx = list_idx;
3232 list_idx += 1;
3233 }
3234 match = match->cp_next;
3235 }
3236 while (match != NULL && !is_first_match(match));
3237 }
3238 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3239 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003240 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003241
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003242 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3243 {
3244 // TODO
3245 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003246}
3247
3248/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003249 * "complete_info()" function
3250 */
3251 void
3252f_complete_info(typval_T *argvars, typval_T *rettv)
3253{
3254 list_T *what_list = NULL;
3255
Bram Moolenaar93a10962022-06-16 11:42:09 +01003256 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003257 return;
3258
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003259 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3260 return;
3261
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003262 if (argvars[0].v_type != VAR_UNKNOWN)
3263 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003264 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003265 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003266 what_list = argvars[0].vval.v_list;
3267 }
3268 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003269}
3270#endif
3271
3272/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003273 * Returns TRUE when using a user-defined function for thesaurus completion.
3274 */
3275 static int
3276thesaurus_func_complete(int type UNUSED)
3277{
3278#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003279 return type == CTRL_X_THESAURUS
3280 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003281#else
3282 return FALSE;
3283#endif
3284}
3285
3286/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003287 * Return value of process_next_cpt_value()
3288 */
3289enum
3290{
3291 INS_COMPL_CPT_OK = 1,
3292 INS_COMPL_CPT_CONT,
3293 INS_COMPL_CPT_END
3294};
3295
3296/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003297 * state information used for getting the next set of insert completion
3298 * matches.
3299 */
3300typedef struct
3301{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003302 char_u *e_cpt_copy; // copy of 'complete'
3303 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003304 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003305 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003306 pos_T prev_match_pos; // previous match position
3307 int set_match_pos; // save first_match_pos/last_match_pos
3308 pos_T first_match_pos; // first match position
3309 pos_T last_match_pos; // last match position
3310 int found_all; // found all matches of a certain type.
3311 char_u *dict; // dictionary file to search
3312 int dict_f; // "dict" is an exact file name or not
3313} ins_compl_next_state_T;
3314
3315/*
3316 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003317 *
3318 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003319 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003321 * st->found_all - all matches of this type are found
3322 * st->ins_buf - search for completions in this buffer
3323 * st->first_match_pos - position of the first completion match
3324 * st->last_match_pos - position of the last completion match
3325 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003326 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003327 * st->dict - name of the dictionary or thesaurus file to search
3328 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003329 *
3330 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003331 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3332 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 */
3335 static int
3336process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003337 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003338 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003339 pos_T *start_match_pos,
3340 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003341{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003342 int compl_type = -1;
3343 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003344
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003345 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003346
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003347 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3348 st->e_cpt++;
3349
3350 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003351 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003352 st->ins_buf = curbuf;
3353 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003354 // Move the cursor back one character so that ^N can match the
3355 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003356 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003357 {
3358 // Move the cursor to after the last character in the
3359 // buffer, so that word at start of buffer is found
3360 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003361 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003362 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003363 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003364 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003365 compl_type = 0;
3366
3367 // Remember the first match so that the loop stops when we
3368 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003369 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003370 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003371 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003372 && (st->ins_buf = ins_compl_next_buf(
3373 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003374 {
3375 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003376 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003377 {
3378 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003379 st->first_match_pos.col = st->last_match_pos.col = 0;
3380 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3381 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003382 compl_type = 0;
3383 }
3384 else // unloaded buffer, scan like dictionary
3385 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003386 st->found_all = TRUE;
3387 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003388 {
3389 status = INS_COMPL_CPT_CONT;
3390 goto done;
3391 }
3392 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003393 st->dict = st->ins_buf->b_fname;
3394 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003395 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003396 if (!shortmess(SHM_COMPLETIONSCAN))
3397 {
3398 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3399 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3400 st->ins_buf->b_fname == NULL
3401 ? buf_spname(st->ins_buf)
3402 : st->ins_buf->b_sfname == NULL
3403 ? st->ins_buf->b_fname
3404 : st->ins_buf->b_sfname);
3405 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3406 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003407 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003408 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003409 status = INS_COMPL_CPT_END;
3410 else
3411 {
3412 if (ctrl_x_mode_line_or_eval())
3413 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003414 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003415 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003416 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003417 compl_type = CTRL_X_DICTIONARY;
3418 else
3419 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003420 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003421 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003422 st->dict = st->e_cpt;
3423 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003424 }
3425 }
3426#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003427 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003428 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003429 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003430 compl_type = CTRL_X_PATH_DEFINES;
3431#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003432 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003433 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003434 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003435 if (!shortmess(SHM_COMPLETIONSCAN))
3436 {
3437 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3438 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3439 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3440 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003441 }
3442 else
3443 compl_type = -1;
3444
3445 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003446 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003447
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003448 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003449 if (compl_type == -1)
3450 status = INS_COMPL_CPT_CONT;
3451 }
3452
3453done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003454 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003455 return status;
3456}
3457
3458#ifdef FEAT_FIND_ID
3459/*
3460 * Get the next set of identifiers or defines matching "compl_pattern" in
3461 * included files.
3462 */
3463 static void
3464get_next_include_file_completion(int compl_type)
3465{
3466 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003467 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003468 (compl_type == CTRL_X_PATH_DEFINES
3469 && !(compl_cont_status & CONT_SOL))
3470 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003471 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003472}
3473#endif
3474
3475/*
3476 * Get the next set of words matching "compl_pattern" in dictionary or
3477 * thesaurus files.
3478 */
3479 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003480get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003481{
3482#ifdef FEAT_COMPL_FUNC
3483 if (thesaurus_func_complete(compl_type))
3484 expand_by_function(compl_type, compl_pattern);
3485 else
3486#endif
3487 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003488 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003489 : (compl_type == CTRL_X_THESAURUS
3490 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3491 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3492 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003493 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003494 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003495}
3496
3497/*
3498 * Get the next set of tag names matching "compl_pattern".
3499 */
3500 static void
3501get_next_tag_completion(void)
3502{
3503 int save_p_ic;
3504 char_u **matches;
3505 int num_matches;
3506
3507 // set p_ic according to p_ic, p_scs and pat for find_tags().
3508 save_p_ic = p_ic;
3509 p_ic = ignorecase(compl_pattern);
3510
3511 // Find up to TAG_MANY matches. Avoids that an enormous number
3512 // of matches is found when compl_pattern is empty
3513 g_tag_at_cursor = TRUE;
3514 if (find_tags(compl_pattern, &num_matches, &matches,
3515 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003516 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003517 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3518 ins_compl_add_matches(num_matches, matches, p_ic);
3519 g_tag_at_cursor = FALSE;
3520 p_ic = save_p_ic;
3521}
3522
3523/*
glepnir8159fb12024-07-17 20:32:54 +02003524 * Compare function for qsort
3525 */
3526static int compare_scores(const void *a, const void *b)
3527{
3528 int idx_a = *(const int *)a;
3529 int idx_b = *(const int *)b;
3530 int score_a = compl_fuzzy_scores[idx_a];
3531 int score_b = compl_fuzzy_scores[idx_b];
3532 return (score_a > score_b) ? -1 : (score_a < score_b) ? 1 : 0;
3533}
3534
3535/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003536 * Get the next set of filename matching "compl_pattern".
3537 */
3538 static void
3539get_next_filename_completion(void)
3540{
3541 char_u **matches;
3542 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003543 char_u *ptr;
3544 garray_T fuzzy_indices;
3545 int i;
3546 int score;
3547 char_u *leader = ins_compl_leader();
Ken Takata073cb022024-07-28 17:08:15 +02003548 size_t leader_len = STRLEN(leader);
glepnir8159fb12024-07-17 20:32:54 +02003549 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3550 char_u **sorted_matches;
3551 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003552 char_u *last_sep = NULL;
3553 size_t path_with_wildcard_len;
3554 char_u *path_with_wildcard;
glepnir8159fb12024-07-17 20:32:54 +02003555
glepnirb9de1a02024-08-02 19:14:38 +02003556#ifdef BACKSLASH_IN_FILENAME
3557 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3558 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3559#else
3560 char pathsep = PATHSEP;
3561#endif
3562
glepnir8159fb12024-07-17 20:32:54 +02003563 if (in_fuzzy)
3564 {
glepnirb9de1a02024-08-02 19:14:38 +02003565#ifdef BACKSLASH_IN_FILENAME
3566 if (curbuf->b_p_csl[0] == 's')
3567 {
3568 for (i = 0; i < leader_len; i++)
3569 {
3570 if (leader[i] == '\\')
3571 leader[i] = '/';
3572 }
3573 }
3574 else if (curbuf->b_p_csl[0] == 'b')
3575 {
3576 for (i = 0; i < leader_len; i++)
3577 {
3578 if (leader[i] == '/')
3579 leader[i] = '\\';
3580 }
3581 }
3582#endif
3583 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003584 if (last_sep == NULL)
3585 {
3586 // No path separator or separator is the last character,
3587 // fuzzy match the whole leader
3588 vim_free(compl_pattern);
3589 compl_pattern = vim_strsave((char_u *)"*");
3590 compl_patternlen = STRLEN(compl_pattern);
3591 }
3592 else if (*(last_sep + 1) == '\0')
3593 in_fuzzy = FALSE;
3594 else
3595 {
3596 // Split leader into path and file parts
3597 int path_len = last_sep - leader + 1;
3598 path_with_wildcard_len = path_len + 2;
3599 path_with_wildcard = alloc(path_with_wildcard_len);
3600 if (path_with_wildcard != NULL)
3601 {
3602 vim_strncpy(path_with_wildcard, leader, path_len);
3603 vim_strcat(path_with_wildcard, (char_u *)"*", path_with_wildcard_len);
3604 vim_free(compl_pattern);
3605 compl_pattern = path_with_wildcard;
3606 compl_patternlen = STRLEN(compl_pattern);
3607
3608 // Move leader to the file part
3609 leader = last_sep + 1;
glepnir6b6280c2024-07-27 16:25:45 +02003610 leader_len = STRLEN(leader);
glepnir0be03e12024-07-19 16:45:05 +02003611 }
3612 }
glepnir8159fb12024-07-17 20:32:54 +02003613 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003614
3615 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3616 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3617 return;
3618
3619 // May change home directory back to "~".
3620 tilde_replace(compl_pattern, num_matches, matches);
3621#ifdef BACKSLASH_IN_FILENAME
3622 if (curbuf->b_p_csl[0] != NUL)
3623 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 for (i = 0; i < num_matches; ++i)
3625 {
glepnir8159fb12024-07-17 20:32:54 +02003626 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003627 while (*ptr != NUL)
3628 {
3629 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3630 *ptr = '/';
3631 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3632 *ptr = '\\';
3633 ptr += (*mb_ptr2len)(ptr);
3634 }
3635 }
3636 }
3637#endif
glepnir8159fb12024-07-17 20:32:54 +02003638
3639 if (in_fuzzy)
3640 {
3641 ga_init2(&fuzzy_indices, sizeof(int), 10);
3642 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3643
3644 for (i = 0; i < num_matches; i++)
3645 {
3646 ptr = matches[i];
3647 score = fuzzy_match_str(ptr, leader);
3648 if (score > 0)
3649 {
3650 if (ga_grow(&fuzzy_indices, 1) == OK)
3651 {
3652 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3653 compl_fuzzy_scores[i] = score;
3654 fuzzy_indices.ga_len++;
3655 }
3656 }
3657 }
3658
Christian Brabandt220474d2024-07-20 13:26:44 +02003659 // prevent qsort from deref NULL pointer
3660 if (fuzzy_indices.ga_len > 0)
3661 {
3662 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3663 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003664
Christian Brabandt220474d2024-07-20 13:26:44 +02003665 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3666 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3667 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003668
Christian Brabandt220474d2024-07-20 13:26:44 +02003669 FreeWild(num_matches, matches);
3670 matches = sorted_matches;
3671 num_matches = fuzzy_indices.ga_len;
3672 }
glepnir6b6280c2024-07-27 16:25:45 +02003673 else if (leader_len > 0)
3674 {
3675 FreeWild(num_matches, matches);
3676 num_matches = 0;
3677 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003678
glepnir8159fb12024-07-17 20:32:54 +02003679 vim_free(compl_fuzzy_scores);
3680 ga_clear(&fuzzy_indices);
3681 }
3682
glepnir6b6280c2024-07-27 16:25:45 +02003683 if (num_matches > 0)
3684 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003685}
3686
3687/*
3688 * Get the next set of command-line completions matching "compl_pattern".
3689 */
3690 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003691get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003692{
3693 char_u **matches;
3694 int num_matches;
3695
3696 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003697 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003698 ins_compl_add_matches(num_matches, matches, FALSE);
3699}
3700
3701/*
3702 * Get the next set of spell suggestions matching "compl_pattern".
3703 */
3704 static void
3705get_next_spell_completion(linenr_T lnum UNUSED)
3706{
3707#ifdef FEAT_SPELL
3708 char_u **matches;
3709 int num_matches;
3710
3711 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3712 if (num_matches > 0)
3713 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003714 else
3715 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003716#endif
3717}
3718
3719/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003720 * Return the next word or line from buffer "ins_buf" at position
3721 * "cur_match_pos" for completion. The length of the match is set in "len".
3722 */
3723 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003724ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003725 buf_T *ins_buf, // buffer being scanned
3726 pos_T *cur_match_pos, // current match position
3727 int *match_len,
3728 int *cont_s_ipos) // next ^X<> will set initial_pos
3729{
3730 char_u *ptr;
3731 int len;
3732
3733 *match_len = 0;
3734 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3735 cur_match_pos->col;
3736 if (ctrl_x_mode_line_or_eval())
3737 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003738 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003739 {
3740 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3741 return NULL;
3742 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3743 if (!p_paste)
3744 ptr = skipwhite(ptr);
3745 }
3746 len = (int)STRLEN(ptr);
3747 }
3748 else
3749 {
3750 char_u *tmp_ptr = ptr;
3751
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003752 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003753 {
3754 tmp_ptr += compl_length;
3755 // Skip if already inside a word.
3756 if (vim_iswordp(tmp_ptr))
3757 return NULL;
3758 // Find start of next word.
3759 tmp_ptr = find_word_start(tmp_ptr);
3760 }
3761 // Find end of this word.
3762 tmp_ptr = find_word_end(tmp_ptr);
3763 len = (int)(tmp_ptr - ptr);
3764
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003765 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003766 {
3767 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3768 {
3769 // Try next line, if any. the new word will be
3770 // "join" as if the normal command "J" was used.
3771 // IOSIZE is always greater than
3772 // compl_length, so the next STRNCPY always
3773 // works -- Acevedo
3774 STRNCPY(IObuff, ptr, len);
3775 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3776 tmp_ptr = ptr = skipwhite(ptr);
3777 // Find start of next word.
3778 tmp_ptr = find_word_start(tmp_ptr);
3779 // Find end of next word.
3780 tmp_ptr = find_word_end(tmp_ptr);
3781 if (tmp_ptr > ptr)
3782 {
3783 if (*ptr != ')' && IObuff[len - 1] != TAB)
3784 {
3785 if (IObuff[len - 1] != ' ')
3786 IObuff[len++] = ' ';
3787 // IObuf =~ "\k.* ", thus len >= 2
3788 if (p_js
3789 && (IObuff[len - 2] == '.'
3790 || (vim_strchr(p_cpo, CPO_JOINSP)
3791 == NULL
3792 && (IObuff[len - 2] == '?'
3793 || IObuff[len - 2] == '!'))))
3794 IObuff[len++] = ' ';
3795 }
3796 // copy as much as possible of the new word
3797 if (tmp_ptr - ptr >= IOSIZE - len)
3798 tmp_ptr = ptr + IOSIZE - len - 1;
3799 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3800 len += (int)(tmp_ptr - ptr);
3801 *cont_s_ipos = TRUE;
3802 }
3803 IObuff[len] = NUL;
3804 ptr = IObuff;
3805 }
3806 if (len == compl_length)
3807 return NULL;
3808 }
3809 }
3810
3811 *match_len = len;
3812 return ptr;
3813}
3814
3815/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003816 * Get the next set of words matching "compl_pattern" for default completion(s)
3817 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003818 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3819 * position "st->start_pos" in the "compl_direction" direction. If
3820 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3821 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003822 * Returns OK if a new next match is found, otherwise returns FAIL.
3823 */
3824 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003825get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003826{
3827 int found_new_match = FAIL;
3828 int save_p_scs;
3829 int save_p_ws;
3830 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003831 char_u *ptr = NULL;
3832 int len = 0;
3833 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3834 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003835
3836 // If 'infercase' is set, don't use 'smartcase' here
3837 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003838 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003839 p_scs = FALSE;
3840
3841 // Buffers other than curbuf are scanned from the beginning or the
3842 // end but never from the middle, thus setting nowrapscan in this
3843 // buffer is a good idea, on the other hand, we always set
3844 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3845 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003846 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003847 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003848 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003849 p_ws = TRUE;
3850 looped_around = FALSE;
3851 for (;;)
3852 {
3853 int cont_s_ipos = FALSE;
3854
3855 ++msg_silent; // Don't want messages for wrapscan.
3856
3857 // ctrl_x_mode_line_or_eval() || word-wise search that
3858 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003859 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003860 found_new_match = search_for_exact_line(st->ins_buf,
3861 st->cur_match_pos, compl_direction, compl_pattern);
glepnir8159fb12024-07-17 20:32:54 +02003862 else if (in_fuzzy)
3863 found_new_match = search_for_fuzzy_match(st->ins_buf,
3864 st->cur_match_pos, leader, compl_direction,
3865 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003866 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003867 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003868 NULL, compl_direction, compl_pattern, compl_patternlen,
3869 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003870 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003871 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003872 {
3873 // set "compl_started" even on fail
3874 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003875 st->first_match_pos = *st->cur_match_pos;
3876 st->last_match_pos = *st->cur_match_pos;
3877 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003878 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003879 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3880 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003881 {
3882 found_new_match = FAIL;
3883 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003884 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003885 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3886 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3887 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003888 {
3889 if (looped_around)
3890 found_new_match = FAIL;
3891 else
3892 looped_around = TRUE;
3893 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003894 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003895 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3896 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3897 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003898 {
3899 if (looped_around)
3900 found_new_match = FAIL;
3901 else
3902 looped_around = TRUE;
3903 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003904 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003905 if (found_new_match == FAIL)
3906 break;
3907
3908 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003909 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003910 && start_pos->lnum == st->cur_match_pos->lnum
3911 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003912 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003913
glepnir8159fb12024-07-17 20:32:54 +02003914 if (!in_fuzzy)
3915 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3916 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003917 if (ptr == NULL)
3918 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003919
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003920 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003921 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003922 0, cont_s_ipos) != NOTDONE)
3923 {
3924 found_new_match = OK;
3925 break;
3926 }
3927 }
3928 p_scs = save_p_scs;
3929 p_ws = save_p_ws;
3930
3931 return found_new_match;
3932}
3933
3934/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003935 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003936 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003937 */
3938 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003939get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003940{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003941 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003942
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003943 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003944 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003945 case -1:
3946 break;
3947#ifdef FEAT_FIND_ID
3948 case CTRL_X_PATH_PATTERNS:
3949 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003950 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003951 break;
3952#endif
3953
3954 case CTRL_X_DICTIONARY:
3955 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003956 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3957 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003958 break;
3959
3960 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003961 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003962 break;
3963
3964 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003965 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003966 break;
3967
3968 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003969 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003970 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003971 break;
3972
3973#ifdef FEAT_COMPL_FUNC
3974 case CTRL_X_FUNCTION:
3975 case CTRL_X_OMNI:
3976 expand_by_function(type, compl_pattern);
3977 break;
3978#endif
3979
3980 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003981 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003982 break;
3983
3984 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003985 found_new_match = get_next_default_completion(st, ini);
3986 if (found_new_match == FAIL && st->ins_buf == curbuf)
3987 st->found_all = TRUE;
3988 }
3989
3990 // check if compl_curr_match has changed, (e.g. other type of
3991 // expansion added something)
3992 if (type != 0 && compl_curr_match != compl_old_match)
3993 found_new_match = OK;
3994
3995 return found_new_match;
3996}
3997
3998/*
3999 * Get the next expansion(s), using "compl_pattern".
4000 * The search starts at position "ini" in curbuf and in the direction
4001 * compl_direction.
4002 * When "compl_started" is FALSE start at that position, otherwise continue
4003 * where we stopped searching before.
4004 * This may return before finding all the matches.
4005 * Return the total number of matches or -1 if still unknown -- Acevedo
4006 */
4007 static int
4008ins_compl_get_exp(pos_T *ini)
4009{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004010 static ins_compl_next_state_T st;
4011 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004012 int i;
4013 int found_new_match;
4014 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004015 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004016
4017 if (!compl_started)
4018 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004019 buf_T *buf;
4020
4021 FOR_ALL_BUFFERS(buf)
4022 buf->b_scanned = 0;
4023 if (!st_cleared)
4024 {
4025 CLEAR_FIELD(st);
4026 st_cleared = TRUE;
4027 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004028 st.found_all = FALSE;
4029 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004030 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004031 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004032 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4033 ? (char_u *)"." : curbuf->b_p_cpt);
4034 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004035 st.last_match_pos = st.first_match_pos = *ini;
4036 }
4037 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4038 st.ins_buf = curbuf; // In case the buffer was wiped out.
4039
4040 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004041 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004042 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004043
4044 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4045 for (;;)
4046 {
4047 found_new_match = FAIL;
4048 st.set_match_pos = FALSE;
4049
4050 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4051 // or if found_all says this entry is done. For ^X^L only use the
4052 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004053 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004054 && (!compl_started || st.found_all))
4055 {
glepnir8159fb12024-07-17 20:32:54 +02004056 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004057
4058 if (status == INS_COMPL_CPT_END)
4059 break;
4060 if (status == INS_COMPL_CPT_CONT)
4061 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004062 }
4063
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004064 // If complete() was called then compl_pattern has been reset. The
4065 // following won't work then, bail out.
4066 if (compl_pattern == NULL)
4067 break;
4068
4069 // get the next set of completion matches
4070 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004071
4072 // break the loop for specialized modes (use 'complete' just for the
4073 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4074 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004075 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4076 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077 {
4078 if (got_int)
4079 break;
4080 // Fill the popup menu as soon as possible.
4081 if (type != -1)
4082 ins_compl_check_keys(0, FALSE);
4083
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004084 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004085 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4086 break;
4087 compl_started = TRUE;
4088 }
4089 else
4090 {
4091 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004092 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004093 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004094
4095 compl_started = FALSE;
4096 }
4097 }
4098 compl_started = TRUE;
4099
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004100 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004101 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004102 found_new_match = FAIL;
4103
4104 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004105 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106 && !ctrl_x_mode_line_or_eval()))
4107 i = ins_compl_make_cyclic();
4108
4109 if (compl_old_match != NULL)
4110 {
4111 // If several matches were added (FORWARD) or the search failed and has
4112 // just been made cyclic then we have to move compl_curr_match to the
4113 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004114 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004115 : compl_old_match->cp_prev;
4116 if (compl_curr_match == NULL)
4117 compl_curr_match = compl_old_match;
4118 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004119 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004120
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004121 return i;
4122}
4123
4124/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004125 * Update "compl_shown_match" to the actually shown match, it may differ when
4126 * "compl_leader" is used to omit some of the matches.
4127 */
4128 static void
4129ins_compl_update_shown_match(void)
4130{
4131 while (!ins_compl_equal(compl_shown_match,
4132 compl_leader, (int)STRLEN(compl_leader))
4133 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004134 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004135 compl_shown_match = compl_shown_match->cp_next;
4136
4137 // If we didn't find it searching forward, and compl_shows_dir is
4138 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004139 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004140 && !ins_compl_equal(compl_shown_match,
4141 compl_leader, (int)STRLEN(compl_leader))
4142 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004143 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004144 {
4145 while (!ins_compl_equal(compl_shown_match,
4146 compl_leader, (int)STRLEN(compl_leader))
4147 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004148 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004149 compl_shown_match = compl_shown_match->cp_prev;
4150 }
4151}
4152
4153/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004154 * Delete the old text being completed.
4155 */
4156 void
4157ins_compl_delete(void)
4158{
4159 int col;
4160
4161 // In insert mode: Delete the typed part.
4162 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004163 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004164 if ((int)curwin->w_cursor.col > col)
4165 {
4166 if (stop_arrow() == FAIL)
4167 return;
4168 backspace_until_column(col);
4169 }
4170
4171 // TODO: is this sufficient for redrawing? Redrawing everything causes
4172 // flicker, thus we can't do that.
4173 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004174#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004175 // clear v:completed_item
4176 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004177#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004178}
4179
4180/*
4181 * Insert the new text being completed.
4182 * "in_compl_func" is TRUE when called from complete_check().
4183 */
4184 void
4185ins_compl_insert(int in_compl_func)
4186{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004187 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004188
4189 // Make sure we don't go over the end of the string, this can happen with
4190 // illegal bytes.
4191 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4192 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004193 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004194 compl_used_match = FALSE;
4195 else
4196 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004197#ifdef FEAT_EVAL
4198 {
4199 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4200
4201 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4202 }
4203#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004204 if (!in_compl_func)
4205 compl_curr_match = compl_shown_match;
4206}
4207
4208/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004209 * show the file name for the completion match (if any). Truncate the file
4210 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004211 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004212 static void
4213ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004214{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004215 char *lead = _("match in file");
4216 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4217 char_u *s;
4218 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004219
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004220 if (space <= 0)
4221 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004222
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004223 // We need the tail that fits. With double-byte encoding going
4224 // back from the end is very slow, thus go from the start and keep
4225 // the text that fits in "space" between "s" and "e".
4226 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004227 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004228 space -= ptr2cells(e);
4229 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004230 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004231 space += ptr2cells(s);
4232 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004233 }
4234 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004235 msg_hist_off = TRUE;
4236 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4237 s > compl_shown_match->cp_fname ? "<" : "", s);
4238 msg((char *)IObuff);
4239 msg_hist_off = FALSE;
4240 redraw_cmdline = FALSE; // don't overwrite!
4241}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004242
glepnirdca57fb2024-06-04 22:01:21 +02004243/*
zeertzjq551d8c32024-06-05 19:53:32 +02004244 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004245 */
glepnira218cc62024-06-03 19:32:39 +02004246 static compl_T *
4247find_comp_when_fuzzy(void)
4248{
4249 int score;
4250 char_u* str;
4251 int target_idx = -1;
4252 int is_forward = compl_shows_dir_forward();
4253 int is_backward = compl_shows_dir_backward();
4254 compl_T *comp = NULL;
4255
glepnir43eef882024-06-19 20:20:48 +02004256 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004257 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004258 return compl_first_match != compl_shown_match ? compl_first_match :
4259 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004260
4261 if (is_forward)
4262 target_idx = compl_selected_item + 1;
4263 else if (is_backward)
4264 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004265 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004266
4267 score = compl_match_array[target_idx].pum_score;
4268 str = compl_match_array[target_idx].pum_text;
4269
4270 comp = compl_first_match;
4271 do {
4272 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4273 return comp;
4274 comp = comp->cp_next;
4275 } while (comp != NULL && !is_first_match(comp));
4276
4277 return NULL;
4278}
4279
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004280/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004281 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004282 * times. The number of matches found is returned in 'num_matches'.
4283 *
4284 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4285 * get more completions. If it is FALSE, then do nothing when there are no more
4286 * completions in the given direction.
4287 *
4288 * If "advance" is TRUE, then completion will move to the first match.
4289 * Otherwise, the original text will be shown.
4290 *
4291 * Returns OK on success and -1 if the number of matches are unknown.
4292 */
4293 static int
4294find_next_completion_match(
4295 int allow_get_expansion,
4296 int todo, // repeat completion this many times
4297 int advance,
4298 int *num_matches)
4299{
4300 int found_end = FALSE;
4301 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004302 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004303 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4304 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004305
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004306 while (--todo >= 0)
4307 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004308 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004309 {
glepniraced8c22024-06-15 15:13:05 +02004310 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4311 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004312 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004313 && (is_first_match(compl_shown_match->cp_next)
4314 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004315 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004316 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004317 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004318 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004319 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004320 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4321 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004322 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004323 }
4324 else
4325 {
4326 if (!allow_get_expansion)
4327 {
4328 if (advance)
4329 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004330 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004331 compl_pending -= todo + 1;
4332 else
4333 compl_pending += todo + 1;
4334 }
4335 return -1;
4336 }
4337
4338 if (!compl_no_select && advance)
4339 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004340 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004341 --compl_pending;
4342 else
4343 ++compl_pending;
4344 }
4345
4346 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004347 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004348
4349 // handle any pending completions
4350 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004351 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004352 {
4353 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4354 {
4355 compl_shown_match = compl_shown_match->cp_next;
4356 --compl_pending;
4357 }
4358 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4359 {
4360 compl_shown_match = compl_shown_match->cp_prev;
4361 ++compl_pending;
4362 }
4363 else
4364 break;
4365 }
4366 found_end = FALSE;
4367 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004368 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004369 && compl_leader != NULL
4370 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004371 compl_leader, (int)STRLEN(compl_leader))
4372 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004373 ++todo;
4374 else
4375 // Remember a matching item.
4376 found_compl = compl_shown_match;
4377
4378 // Stop at the end of the list when we found a usable match.
4379 if (found_end)
4380 {
4381 if (found_compl != NULL)
4382 {
4383 compl_shown_match = found_compl;
4384 break;
4385 }
4386 todo = 1; // use first usable match after wrapping around
4387 }
4388 }
4389
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004390 return OK;
4391}
4392
4393/*
4394 * Fill in the next completion in the current direction.
4395 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4396 * get more completions. If it is FALSE, then we just do nothing when there
4397 * are no more completions in a given direction. The latter case is used when
4398 * we are still in the middle of finding completions, to allow browsing
4399 * through the ones found so far.
4400 * Return the total number of matches, or -1 if still unknown -- webb.
4401 *
4402 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4403 * compl_shown_match here.
4404 *
4405 * Note that this function may be called recursively once only. First with
4406 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4407 * calls this function with "allow_get_expansion" FALSE.
4408 */
4409 static int
4410ins_compl_next(
4411 int allow_get_expansion,
4412 int count, // repeat completion this many times; should
4413 // be at least 1
4414 int insert_match, // Insert the newly selected match
4415 int in_compl_func) // called from complete_check()
4416{
4417 int num_matches = -1;
4418 int todo = count;
4419 int advance;
4420 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004421 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004422 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004423 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4424 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004425
4426 // When user complete function return -1 for findstart which is next
4427 // time of 'always', compl_shown_match become NULL.
4428 if (compl_shown_match == NULL)
4429 return -1;
4430
glepnira218cc62024-06-03 19:32:39 +02004431 if (compl_leader != NULL
4432 && !match_at_original_text(compl_shown_match)
4433 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004434 // Update "compl_shown_match" to the actually shown match
4435 ins_compl_update_shown_match();
4436
4437 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004438 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004439 // Delete old text to be replaced
4440 ins_compl_delete();
4441
4442 // When finding the longest common text we stick at the original text,
4443 // don't let CTRL-N or CTRL-P move to the first match.
4444 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4445
4446 // When restarting the search don't insert the first match either.
4447 if (compl_restarting)
4448 {
4449 advance = FALSE;
4450 compl_restarting = FALSE;
4451 }
4452
4453 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4454 // around.
4455 if (find_next_completion_match(allow_get_expansion, todo, advance,
4456 &num_matches) == -1)
4457 return -1;
4458
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004459 if (curbuf != orig_curbuf)
4460 {
4461 // In case some completion function switched buffer, don't want to
4462 // insert the completion elsewhere.
4463 return -1;
4464 }
4465
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004466 // Insert the text of the new completion, or the compl_leader.
4467 if (compl_no_insert && !started)
4468 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004469 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004470 compl_used_match = FALSE;
4471 }
4472 else if (insert_match)
4473 {
4474 if (!compl_get_longest || compl_used_match)
4475 ins_compl_insert(in_compl_func);
4476 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004477 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004478 }
4479 else
4480 compl_used_match = FALSE;
4481
4482 if (!allow_get_expansion)
4483 {
4484 // may undisplay the popup menu first
4485 ins_compl_upd_pum();
4486
4487 if (pum_enough_matches())
4488 // Will display the popup menu, don't redraw yet to avoid flicker.
4489 pum_call_update_screen();
4490 else
4491 // Not showing the popup menu yet, redraw to show the user what was
4492 // inserted.
4493 update_screen(0);
4494
4495 // display the updated popup menu
4496 ins_compl_show_pum();
4497#ifdef FEAT_GUI
4498 if (gui.in_use)
4499 {
4500 // Show the cursor after the match, not after the redrawn text.
4501 setcursor();
4502 out_flush_cursor(FALSE, FALSE);
4503 }
4504#endif
4505
4506 // Delete old text to be replaced, since we're still searching and
4507 // don't want to match ourselves!
4508 ins_compl_delete();
4509 }
4510
4511 // Enter will select a match when the match wasn't inserted and the popup
4512 // menu is visible.
4513 if (compl_no_insert && !started)
4514 compl_enter_selects = TRUE;
4515 else
4516 compl_enter_selects = !insert_match && compl_match_array != NULL;
4517
4518 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004519 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004520 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004521
4522 return num_matches;
4523}
4524
4525/*
4526 * Call this while finding completions, to check whether the user has hit a key
4527 * that should change the currently displayed completion, or exit completion
4528 * mode. Also, when compl_pending is not zero, show a completion as soon as
4529 * possible. -- webb
4530 * "frequency" specifies out of how many calls we actually check.
4531 * "in_compl_func" is TRUE when called from complete_check(), don't set
4532 * compl_curr_match.
4533 */
4534 void
4535ins_compl_check_keys(int frequency, int in_compl_func)
4536{
4537 static int count = 0;
4538 int c;
4539
4540 // Don't check when reading keys from a script, :normal or feedkeys().
4541 // That would break the test scripts. But do check for keys when called
4542 // from complete_check().
4543 if (!in_compl_func && (using_script() || ex_normal_busy))
4544 return;
4545
4546 // Only do this at regular intervals
4547 if (++count < frequency)
4548 return;
4549 count = 0;
4550
4551 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4552 // can't do its work correctly.
4553 c = vpeekc_any();
4554 if (c != NUL)
4555 {
4556 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4557 {
4558 c = safe_vgetc(); // Eat the character
4559 compl_shows_dir = ins_compl_key2dir(c);
4560 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4561 c != K_UP && c != K_DOWN, in_compl_func);
4562 }
4563 else
4564 {
4565 // Need to get the character to have KeyTyped set. We'll put it
4566 // back with vungetc() below. But skip K_IGNORE.
4567 c = safe_vgetc();
4568 if (c != K_IGNORE)
4569 {
4570 // Don't interrupt completion when the character wasn't typed,
4571 // e.g., when doing @q to replay keys.
4572 if (c != Ctrl_R && KeyTyped)
4573 compl_interrupted = TRUE;
4574
4575 vungetc(c);
4576 }
4577 }
4578 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004579 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004580 {
4581 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4582
4583 compl_pending = 0;
4584 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4585 }
4586}
4587
4588/*
4589 * Decide the direction of Insert mode complete from the key typed.
4590 * Returns BACKWARD or FORWARD.
4591 */
4592 static int
4593ins_compl_key2dir(int c)
4594{
4595 if (c == Ctrl_P || c == Ctrl_L
4596 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4597 return BACKWARD;
4598 return FORWARD;
4599}
4600
4601/*
4602 * Return TRUE for keys that are used for completion only when the popup menu
4603 * is visible.
4604 */
4605 static int
4606ins_compl_pum_key(int c)
4607{
4608 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4609 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4610 || c == K_UP || c == K_DOWN);
4611}
4612
4613/*
4614 * Decide the number of completions to move forward.
4615 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4616 */
4617 static int
4618ins_compl_key2count(int c)
4619{
4620 int h;
4621
4622 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4623 {
4624 h = pum_get_height();
4625 if (h > 3)
4626 h -= 2; // keep some context
4627 return h;
4628 }
4629 return 1;
4630}
4631
4632/*
4633 * Return TRUE if completion with "c" should insert the match, FALSE if only
4634 * to change the currently selected completion.
4635 */
4636 static int
4637ins_compl_use_match(int c)
4638{
4639 switch (c)
4640 {
4641 case K_UP:
4642 case K_DOWN:
4643 case K_PAGEDOWN:
4644 case K_KPAGEDOWN:
4645 case K_S_DOWN:
4646 case K_PAGEUP:
4647 case K_KPAGEUP:
4648 case K_S_UP:
4649 return FALSE;
4650 }
4651 return TRUE;
4652}
4653
4654/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004655 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4656 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004657 * Sets the global variables: compl_col, compl_length, compl_pattern and
4658 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004659 * Uses the global variables: compl_cont_status and ctrl_x_mode
4660 */
4661 static int
4662get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4663{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004664 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004665 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004666 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004667 {
4668 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4669 ;
4670 compl_col += ++startcol;
4671 compl_length = curs_col - startcol;
4672 }
4673 if (p_ic)
4674 compl_pattern = str_foldcase(line + compl_col,
4675 compl_length, NULL, 0);
4676 else
4677 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4678 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004679 {
4680 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004681 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004682 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004683 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004684 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004685 {
4686 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004687 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004688
4689 // we need up to 2 extra chars for the prefix
4690 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004691 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004692 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004693 {
4694 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004695 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004696 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004697 if (!vim_iswordp(line + compl_col)
4698 || (compl_col > 0
4699 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004700 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004701 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004702 prefixlen = 0;
4703 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004704 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004705 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004706 line + compl_col, compl_length);
4707 }
4708 else if (--startcol < 0
4709 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4710 {
4711 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004712 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004713 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004714 {
4715 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004716 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004717 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004718 compl_col += curs_col;
4719 compl_length = 0;
4720 }
4721 else
4722 {
4723 // Search the point of change class of multibyte character
4724 // or not a word single byte character backward.
4725 if (has_mbyte)
4726 {
4727 int base_class;
4728 int head_off;
4729
4730 startcol -= (*mb_head_off)(line, line + startcol);
4731 base_class = mb_get_class(line + startcol);
4732 while (--startcol >= 0)
4733 {
4734 head_off = (*mb_head_off)(line, line + startcol);
4735 if (base_class != mb_get_class(line + startcol
4736 - head_off))
4737 break;
4738 startcol -= head_off;
4739 }
4740 }
4741 else
4742 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4743 ;
4744 compl_col += ++startcol;
4745 compl_length = (int)curs_col - startcol;
4746 if (compl_length == 1)
4747 {
4748 // Only match word with at least two chars -- webb
4749 // there's no need to call quote_meta,
4750 // alloc(7) is enough -- Acevedo
4751 compl_pattern = alloc(7);
4752 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004753 {
4754 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004755 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004756 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004757 STRCPY((char *)compl_pattern, "\\<");
4758 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4759 STRCAT((char *)compl_pattern, "\\k");
4760 }
4761 else
4762 {
4763 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4764 compl_length) + 2);
4765 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004766 {
4767 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004768 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004769 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004770 STRCPY((char *)compl_pattern, "\\<");
4771 (void)quote_meta(compl_pattern + 2, line + compl_col,
4772 compl_length);
4773 }
4774 }
4775
John Marriott8c85a2a2024-05-20 19:18:26 +02004776 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004777 return OK;
4778}
4779
4780/*
4781 * Get the pattern, column and length for whole line completion or for the
4782 * complete() function.
4783 * Sets the global variables: compl_col, compl_length and compl_pattern.
4784 */
4785 static int
4786get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4787{
4788 compl_col = (colnr_T)getwhitecols(line);
4789 compl_length = (int)curs_col - (int)compl_col;
4790 if (compl_length < 0) // cursor in indent: empty pattern
4791 compl_length = 0;
4792 if (p_ic)
4793 compl_pattern = str_foldcase(line + compl_col, compl_length,
4794 NULL, 0);
4795 else
4796 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4797 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004798 {
4799 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004800 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004801 }
4802
4803 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004804
4805 return OK;
4806}
4807
4808/*
4809 * Get the pattern, column and length for filename completion.
4810 * Sets the global variables: compl_col, compl_length and compl_pattern.
4811 */
4812 static int
4813get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4814{
4815 // Go back to just before the first filename character.
4816 if (startcol > 0)
4817 {
4818 char_u *p = line + startcol;
4819
4820 MB_PTR_BACK(line, p);
4821 while (p > line && vim_isfilec(PTR2CHAR(p)))
4822 MB_PTR_BACK(line, p);
4823 if (p == line && vim_isfilec(PTR2CHAR(p)))
4824 startcol = 0;
4825 else
4826 startcol = (int)(p - line) + 1;
4827 }
4828
4829 compl_col += startcol;
4830 compl_length = (int)curs_col - startcol;
4831 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4832 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004833 {
4834 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004835 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004836 }
4837
4838 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004839
4840 return OK;
4841}
4842
4843/*
4844 * Get the pattern, column and length for command-line completion.
4845 * Sets the global variables: compl_col, compl_length and compl_pattern.
4846 */
4847 static int
4848get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4849{
4850 compl_pattern = vim_strnsave(line, curs_col);
4851 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004852 {
4853 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004854 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004855 }
4856 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004857 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004858 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004859 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4860 || compl_xp.xp_context == EXPAND_NOTHING)
4861 // No completion possible, use an empty pattern to get a
4862 // "pattern not found" message.
4863 compl_col = curs_col;
4864 else
4865 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4866 compl_length = curs_col - compl_col;
4867
4868 return OK;
4869}
4870
4871/*
4872 * Get the pattern, column and length for user defined completion ('omnifunc',
4873 * 'completefunc' and 'thesaurusfunc')
4874 * Sets the global variables: compl_col, compl_length and compl_pattern.
4875 * Uses the global variable: spell_bad_len
4876 */
4877 static int
4878get_userdefined_compl_info(colnr_T curs_col UNUSED)
4879{
4880 int ret = FAIL;
4881
4882#ifdef FEAT_COMPL_FUNC
4883 // Call user defined function 'completefunc' with "a:findstart"
4884 // set to 1 to obtain the length of text to use for completion.
4885 char_u *line;
4886 typval_T args[3];
4887 int col;
4888 char_u *funcname;
4889 pos_T pos;
4890 int save_State = State;
4891 callback_T *cb;
4892
4893 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4894 // length as a string
4895 funcname = get_complete_funcname(ctrl_x_mode);
4896 if (*funcname == NUL)
4897 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004898 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004899 ? "completefunc" : "omnifunc");
4900 return FAIL;
4901 }
4902
4903 args[0].v_type = VAR_NUMBER;
4904 args[0].vval.v_number = 1;
4905 args[1].v_type = VAR_STRING;
4906 args[1].vval.v_string = (char_u *)"";
4907 args[2].v_type = VAR_UNKNOWN;
4908 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004909 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004910 cb = get_insert_callback(ctrl_x_mode);
4911 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004912 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004913
4914 State = save_State;
4915 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004916 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004917 validate_cursor();
4918 if (!EQUAL_POS(curwin->w_cursor, pos))
4919 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004920 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004921 return FAIL;
4922 }
4923
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004924 // Return value -2 means the user complete function wants to cancel the
4925 // complete without an error, do the same if the function did not execute
4926 // successfully.
4927 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004928 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004929 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004930 if (col == -3)
4931 {
4932 ctrl_x_mode = CTRL_X_NORMAL;
4933 edit_submode = NULL;
4934 if (!shortmess(SHM_COMPLETIONMENU))
4935 msg_clr_cmdline();
4936 return FAIL;
4937 }
4938
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004939 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004940 // completion.
4941 compl_opt_refresh_always = FALSE;
4942 compl_opt_suppress_empty = FALSE;
4943
4944 if (col < 0)
4945 col = curs_col;
4946 compl_col = col;
4947 if (compl_col > curs_col)
4948 compl_col = curs_col;
4949
4950 // Setup variables for completion. Need to obtain "line" again,
4951 // it may have become invalid.
4952 line = ml_get(curwin->w_cursor.lnum);
4953 compl_length = curs_col - compl_col;
4954 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4955 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004956 {
4957 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004958 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004959 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004960
John Marriott8c85a2a2024-05-20 19:18:26 +02004961 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004962 ret = OK;
4963#endif
4964
4965 return ret;
4966}
4967
4968/*
4969 * Get the pattern, column and length for spell completion.
4970 * Sets the global variables: compl_col, compl_length and compl_pattern.
4971 * Uses the global variable: spell_bad_len
4972 */
4973 static int
4974get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4975{
4976 int ret = FAIL;
4977#ifdef FEAT_SPELL
4978 char_u *line;
4979
4980 if (spell_bad_len > 0)
4981 compl_col = curs_col - spell_bad_len;
4982 else
4983 compl_col = spell_word_start(startcol);
4984 if (compl_col >= (colnr_T)startcol)
4985 {
4986 compl_length = 0;
4987 compl_col = curs_col;
4988 }
4989 else
4990 {
4991 spell_expand_check_cap(compl_col);
4992 compl_length = (int)curs_col - compl_col;
4993 }
4994 // Need to obtain "line" again, it may have become invalid.
4995 line = ml_get(curwin->w_cursor.lnum);
4996 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4997 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004998 {
4999 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005000 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005001 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005002
John Marriott8c85a2a2024-05-20 19:18:26 +02005003 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005004 ret = OK;
5005#endif
5006
5007 return ret;
5008}
5009
5010/*
5011 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005012 * "startcol" - start column number of the completion pattern/text
5013 * "cur_col" - current cursor column
5014 * On return, "line_invalid" is set to TRUE, if the current line may have
5015 * become invalid and needs to be fetched again.
5016 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005017 */
5018 static int
5019compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5020{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005021 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005022 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5023 && !thesaurus_func_complete(ctrl_x_mode)))
5024 {
5025 return get_normal_compl_info(line, startcol, curs_col);
5026 }
5027 else if (ctrl_x_mode_line_or_eval())
5028 {
5029 return get_wholeline_compl_info(line, curs_col);
5030 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005031 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005032 {
5033 return get_filename_compl_info(line, startcol, curs_col);
5034 }
5035 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5036 {
5037 return get_cmdline_compl_info(line, curs_col);
5038 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005039 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005040 || thesaurus_func_complete(ctrl_x_mode))
5041 {
5042 if (get_userdefined_compl_info(curs_col) == FAIL)
5043 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005044 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005045 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005046 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005047 {
5048 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5049 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005050 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005051 }
5052 else
5053 {
5054 internal_error("ins_complete()");
5055 return FAIL;
5056 }
5057
5058 return OK;
5059}
5060
5061/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005062 * Continue an interrupted completion mode search in "line".
5063 *
5064 * If this same ctrl_x_mode has been interrupted use the text from
5065 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5066 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5067 * the same line as the cursor then fix it (the line has been split because it
5068 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5069 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005070 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005071 static void
5072ins_compl_continue_search(char_u *line)
5073{
5074 // it is a continued search
5075 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005076 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5077 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005078 {
5079 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5080 {
5081 // line (probably) wrapped, set compl_startpos to the
5082 // first non_blank in the line, if it is not a wordchar
5083 // include it to get a better pattern, but then we don't
5084 // want the "\\<" prefix, check it below
5085 compl_col = (colnr_T)getwhitecols(line);
5086 compl_startpos.col = compl_col;
5087 compl_startpos.lnum = curwin->w_cursor.lnum;
5088 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5089 }
5090 else
5091 {
5092 // S_IPOS was set when we inserted a word that was at the
5093 // beginning of the line, which means that we'll go to SOL
5094 // mode but first we need to redefine compl_startpos
5095 if (compl_cont_status & CONT_S_IPOS)
5096 {
5097 compl_cont_status |= CONT_SOL;
5098 compl_startpos.col = (colnr_T)(skipwhite(
5099 line + compl_length
5100 + compl_startpos.col) - line);
5101 }
5102 compl_col = compl_startpos.col;
5103 }
5104 compl_length = curwin->w_cursor.col - (int)compl_col;
5105 // IObuff is used to add a "word from the next line" would we
5106 // have enough space? just being paranoid
5107#define MIN_SPACE 75
5108 if (compl_length > (IOSIZE - MIN_SPACE))
5109 {
5110 compl_cont_status &= ~CONT_SOL;
5111 compl_length = (IOSIZE - MIN_SPACE);
5112 compl_col = curwin->w_cursor.col - compl_length;
5113 }
5114 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5115 if (compl_length < 1)
5116 compl_cont_status &= CONT_LOCAL;
5117 }
5118 else if (ctrl_x_mode_line_or_eval())
5119 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5120 else
5121 compl_cont_status = 0;
5122}
5123
5124/*
5125 * start insert mode completion
5126 */
5127 static int
5128ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005129{
5130 char_u *line;
5131 int startcol = 0; // column where searched text starts
5132 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005133 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005134 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005135 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005136
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005137 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005138
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005139 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005140 did_si = FALSE;
5141 can_si = FALSE;
5142 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005143 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005144 return FAIL;
5145
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005146 line = ml_get(curwin->w_cursor.lnum);
5147 curs_col = curwin->w_cursor.col;
5148 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005149
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005150 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5151 && compl_cont_mode == ctrl_x_mode)
5152 // this same ctrl-x_mode was interrupted previously. Continue the
5153 // completion.
5154 ins_compl_continue_search(line);
5155 else
5156 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005157
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005158 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005159 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005160 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005161 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005162 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5163 compl_cont_status = 0;
5164 compl_cont_status |= CONT_N_ADDS;
5165 compl_startpos = curwin->w_cursor;
5166 startcol = (int)curs_col;
5167 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005168 }
5169
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005170 // Work out completion pattern and original text -- webb
5171 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5172 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005173 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5174 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005175 // restore did_ai, so that adding comment leader works
5176 did_ai = save_did_ai;
5177 return FAIL;
5178 }
5179 // If "line" was changed while getting completion info get it again.
5180 if (line_invalid)
5181 line = ml_get(curwin->w_cursor.lnum);
5182
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005183 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005184 {
5185 edit_submode_pre = (char_u *)_(" Adding");
5186 if (ctrl_x_mode_line_or_eval())
5187 {
5188 // Insert a new line, keep indentation but ignore 'comments'.
5189 char_u *old = curbuf->b_p_com;
5190
5191 curbuf->b_p_com = (char_u *)"";
5192 compl_startpos.lnum = curwin->w_cursor.lnum;
5193 compl_startpos.col = compl_col;
5194 ins_eol('\r');
5195 curbuf->b_p_com = old;
5196 compl_length = 0;
5197 compl_col = curwin->w_cursor.col;
5198 }
5199 }
5200 else
5201 {
5202 edit_submode_pre = NULL;
5203 compl_startpos.col = compl_col;
5204 }
5205
5206 if (compl_cont_status & CONT_LOCAL)
5207 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5208 else
5209 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5210
5211 // If any of the original typed text has been changed we need to fix
5212 // the redo buffer.
5213 ins_compl_fixRedoBufForLeader(NULL);
5214
5215 // Always add completion for the original text.
5216 vim_free(compl_orig_text);
5217 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5218 if (p_ic)
5219 flags |= CP_ICASE;
5220 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
glepnir508e7852024-07-25 21:39:08 +02005221 -1, NULL, NULL, NULL, 0, flags, FALSE, -1) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005222 {
5223 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005224 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005225 VIM_CLEAR(compl_orig_text);
5226 return FAIL;
5227 }
5228
5229 // showmode might reset the internal line pointers, so it must
5230 // be called before line = ml_get(), or when this address is no
5231 // longer needed. -- Acevedo.
5232 edit_submode_extra = (char_u *)_("-- Searching...");
5233 edit_submode_highl = HLF_COUNT;
5234 showmode();
5235 edit_submode_extra = NULL;
5236 out_flush();
5237
5238 return OK;
5239}
5240
5241/*
5242 * display the completion status message
5243 */
5244 static void
5245ins_compl_show_statusmsg(void)
5246{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005248 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005249 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005250 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005251 ? (char_u *)_("Hit end of paragraph")
5252 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005253 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005254 }
5255
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005256 if (edit_submode_extra == NULL)
5257 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005258 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005259 {
5260 edit_submode_extra = (char_u *)_("Back at original");
5261 edit_submode_highl = HLF_W;
5262 }
5263 else if (compl_cont_status & CONT_S_IPOS)
5264 {
5265 edit_submode_extra = (char_u *)_("Word from other line");
5266 edit_submode_highl = HLF_COUNT;
5267 }
5268 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5269 {
5270 edit_submode_extra = (char_u *)_("The only match");
5271 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005272 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005273 }
5274 else
5275 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005276#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005277 // Update completion sequence number when needed.
5278 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005279 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005280#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005281 // The match should always have a sequence number now, this is
5282 // just a safety check.
5283 if (compl_curr_match->cp_number != -1)
5284 {
5285 // Space for 10 text chars. + 2x10-digit no.s = 31.
5286 // Translations may need more than twice that.
5287 static char_u match_ref[81];
5288
5289 if (compl_matches > 0)
5290 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005291 _("match %d of %d"),
5292 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005293 else
5294 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005295 _("match %d"),
5296 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005297 edit_submode_extra = match_ref;
5298 edit_submode_highl = HLF_R;
5299 if (dollar_vcol >= 0)
5300 curs_columns(FALSE);
5301 }
5302 }
5303 }
5304
5305 // Show a message about what (completion) mode we're in.
5306 if (!compl_opt_suppress_empty)
5307 {
5308 showmode();
5309 if (!shortmess(SHM_COMPLETIONMENU))
5310 {
5311 if (edit_submode_extra != NULL)
5312 {
5313 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005314 {
5315 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005316 msg_attr((char *)edit_submode_extra,
5317 edit_submode_highl < HLF_COUNT
5318 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005319 msg_hist_off = FALSE;
5320 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005321 }
5322 else
5323 msg_clr_cmdline(); // necessary for "noshowmode"
5324 }
5325 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005326}
5327
5328/*
5329 * Do Insert mode completion.
5330 * Called when character "c" was typed, which has a meaning for completion.
5331 * Returns OK if completion was done, FAIL if something failed (out of mem).
5332 */
5333 int
5334ins_complete(int c, int enable_pum)
5335{
5336 int n;
5337 int save_w_wrow;
5338 int save_w_leftcol;
5339 int insert_match;
5340
5341 compl_direction = ins_compl_key2dir(c);
5342 insert_match = ins_compl_use_match(c);
5343
5344 if (!compl_started)
5345 {
5346 if (ins_compl_start() == FAIL)
5347 return FAIL;
5348 }
5349 else if (insert_match && stop_arrow() == FAIL)
5350 return FAIL;
5351
5352 compl_shown_match = compl_curr_match;
5353 compl_shows_dir = compl_direction;
5354
5355 // Find next match (and following matches).
5356 save_w_wrow = curwin->w_wrow;
5357 save_w_leftcol = curwin->w_leftcol;
5358 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5359
5360 // may undisplay the popup menu
5361 ins_compl_upd_pum();
5362
5363 if (n > 1) // all matches have been found
5364 compl_matches = n;
5365 compl_curr_match = compl_shown_match;
5366 compl_direction = compl_shows_dir;
5367
5368 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5369 // mode.
5370 if (got_int && !global_busy)
5371 {
5372 (void)vgetc();
5373 got_int = FALSE;
5374 }
5375
5376 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005377 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005378 {
5379 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5380 // because we couldn't expand anything at first place, but if we used
5381 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5382 // (such as M in M'exico) if not tried already. -- Acevedo
5383 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005384 || compl_status_adding()
5385 || (ctrl_x_mode_not_default()
5386 && !ctrl_x_mode_path_patterns()
5387 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005388 compl_cont_status &= ~CONT_N_ADDS;
5389 }
5390
5391 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5392 compl_cont_status |= CONT_S_IPOS;
5393 else
5394 compl_cont_status &= ~CONT_S_IPOS;
5395
5396 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005397
5398 // Show the popup menu, unless we got interrupted.
5399 if (enable_pum && !compl_interrupted)
5400 show_pum(save_w_wrow, save_w_leftcol);
5401
5402 compl_was_interrupted = compl_interrupted;
5403 compl_interrupted = FALSE;
5404
5405 return OK;
5406}
5407
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005408/*
5409 * Remove (if needed) and show the popup menu
5410 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005411 static void
5412show_pum(int prev_w_wrow, int prev_w_leftcol)
5413{
5414 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005415 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005416 RedrawingDisabled = 0;
5417
5418 // If the cursor moved or the display scrolled we need to remove the pum
5419 // first.
5420 setcursor();
5421 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5422 ins_compl_del_pum();
5423
5424 ins_compl_show_pum();
5425 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005426
5427 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005428}
5429
5430/*
5431 * Looks in the first "len" chars. of "src" for search-metachars.
5432 * If dest is not NULL the chars. are copied there quoting (with
5433 * a backslash) the metachars, and dest would be NUL terminated.
5434 * Returns the length (needed) of dest
5435 */
5436 static unsigned
5437quote_meta(char_u *dest, char_u *src, int len)
5438{
5439 unsigned m = (unsigned)len + 1; // one extra for the NUL
5440
5441 for ( ; --len >= 0; src++)
5442 {
5443 switch (*src)
5444 {
5445 case '.':
5446 case '*':
5447 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005448 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005449 break;
5450 // FALLTHROUGH
5451 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005452 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005453 break;
5454 // FALLTHROUGH
5455 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005456 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005457 break;
5458 // FALLTHROUGH
5459 case '^': // currently it's not needed.
5460 case '$':
5461 m++;
5462 if (dest != NULL)
5463 *dest++ = '\\';
5464 break;
5465 }
5466 if (dest != NULL)
5467 *dest++ = *src;
5468 // Copy remaining bytes of a multibyte character.
5469 if (has_mbyte)
5470 {
5471 int i, mb_len;
5472
5473 mb_len = (*mb_ptr2len)(src) - 1;
5474 if (mb_len > 0 && len >= mb_len)
5475 for (i = 0; i < mb_len; ++i)
5476 {
5477 --len;
5478 ++src;
5479 if (dest != NULL)
5480 *dest++ = *src;
5481 }
5482 }
5483 }
5484 if (dest != NULL)
5485 *dest = NUL;
5486
5487 return m;
5488}
5489
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005490#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005491 void
5492free_insexpand_stuff(void)
5493{
5494 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005495# ifdef FEAT_EVAL
5496 free_callback(&cfu_cb);
5497 free_callback(&ofu_cb);
5498 free_callback(&tsrfu_cb);
5499# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005500}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005501#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005502
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005503#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005504/*
5505 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5506 * spelled word, if there is one.
5507 */
5508 static void
5509spell_back_to_badword(void)
5510{
5511 pos_T tpos = curwin->w_cursor;
5512
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005513 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005514 if (curwin->w_cursor.col != tpos.col)
5515 start_arrow(&tpos);
5516}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005517#endif