blob: 522b7f73ea1579283b9ce65228a35256300d46ee [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010091 * Structure used to store one match for insert completion.
92 */
93typedef struct compl_S compl_T;
94struct compl_S
95{
96 compl_T *cp_next;
97 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +010098 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +010099 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200100 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100101#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100102 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100103#endif
glepnir0fe17f82024-10-08 22:26:44 +0200104 char_u *cp_fname; // file containing the match, allocated when
105 // cp_flags has CP_FREE_FNAME
106 int cp_flags; // CP_ values
107 int cp_number; // sequence number
108 int cp_score; // fuzzy match score
glepnir7baa0142024-10-09 20:19:25 +0200109 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200110 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100111};
112
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113// values for cp_flags
114# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
115# define CP_FREE_FNAME 2 // cp_fname is allocated
116# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
117# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
118# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200119# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100120
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121/*
122 * All the current matches are stored in a list.
123 * "compl_first_match" points to the start of the list.
124 * "compl_curr_match" points to the currently selected entry.
125 * "compl_shown_match" is different from compl_curr_match during
126 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000127 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128 */
129static compl_T *compl_first_match = NULL;
130static compl_T *compl_curr_match = NULL;
131static compl_T *compl_shown_match = NULL;
132static compl_T *compl_old_match = NULL;
133
134// After using a cursor key <Enter> selects a match in the popup menu,
135// otherwise it inserts a line break.
136static int compl_enter_selects = FALSE;
137
138// When "compl_leader" is not NULL only matches that start with this string
139// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100140static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100141
142static int compl_get_longest = FALSE; // put longest common string
143 // in compl_leader
144
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100145// Selected one of the matches. When FALSE the match was edited or using the
146// longest common string.
147static int compl_used_match;
148
149// didn't finish finding completions.
150static int compl_was_interrupted = FALSE;
151
152// Set when character typed while looking for matches and it means we should
153// stop looking for matches.
154static int compl_interrupted = FALSE;
155
156static int compl_restarting = FALSE; // don't insert match
157
158// When the first completion is done "compl_started" is set. When it's
159// FALSE the word to be completed must be located.
160static int compl_started = FALSE;
161
162// Which Ctrl-X mode are we in?
163static int ctrl_x_mode = CTRL_X_NORMAL;
164
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000165static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100166static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100167static int compl_direction = FORWARD;
168static int compl_shows_dir = FORWARD;
169static int compl_pending = 0; // > 1 for postponed CTRL-N
170static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000171// Length in bytes of the text being completed (this is deleted to be replaced
172// by the match.)
173static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100174static colnr_T compl_col = 0; // column where the text starts
175 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100176static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100177static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178 // completion started
179static int compl_cont_mode = 0;
180static expand_T compl_xp;
181
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000182// List of flags for method of completion.
183static int compl_cont_status = 0;
184# define CONT_ADDING 1 // "normal" or "adding" expansion
185# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
186 // it's set only iff N_ADDS is set
187# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
188# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
189 // if so, word-wise-expansion will set SOL
190# define CONT_SOL 16 // pattern includes start of line, just for
191 // word-wise expansion, not set for ^X^L
192# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
193 // expansion, (eg use complete=.)
194
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100195static int compl_opt_refresh_always = FALSE;
196static int compl_opt_suppress_empty = FALSE;
197
glepnira218cc62024-06-03 19:32:39 +0200198static int compl_selected_item = -1;
199
glepnir8159fb12024-07-17 20:32:54 +0200200static int *compl_fuzzy_scores;
201
glepnir6a38aff2024-12-16 21:56:16 +0100202// "compl_match_array" points the currently displayed list of entries in the
203// popup menu. It is NULL when there is no popup menu.
204static pumitem_T *compl_match_array = NULL;
205static int compl_match_arraysize;
206
glepnir80b66202024-11-27 21:53:53 +0100207static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int *user_hl);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100208static void ins_compl_longest_match(compl_T *match);
209static void ins_compl_del_pum(void);
210static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
211static char_u *find_line_end(char_u *ptr);
212static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100213static int ins_compl_need_restart(void);
214static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000215static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100216static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100217static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
219# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
220static void ins_compl_add_list(list_T *list);
221static void ins_compl_add_dict(dict_T *dict);
222# endif
223static int ins_compl_key2dir(int c);
224static int ins_compl_pum_key(int c);
225static int ins_compl_key2count(int c);
226static void show_pum(int prev_w_wrow, int prev_w_leftcol);
227static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100228
229#ifdef FEAT_SPELL
230static void spell_back_to_badword(void);
231static int spell_bad_len = 0; // length of located bad word
232#endif
233
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100234/*
235 * CTRL-X pressed in Insert mode.
236 */
237 void
238ins_ctrl_x(void)
239{
zeertzjqdca29d92021-08-31 19:12:51 +0200240 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100241 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000242 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 if (compl_cont_status & CONT_N_ADDS)
244 compl_cont_status |= CONT_INTRPT;
245 else
246 compl_cont_status = 0;
247 // We're not sure which CTRL-X mode it will be yet
248 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
249 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
250 edit_submode_pre = NULL;
251 showmode();
252 }
zeertzjqdca29d92021-08-31 19:12:51 +0200253 else
254 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
255 // CTRL-V look like CTRL-N
256 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100257
LemonBoy2bf52dd2022-04-09 18:17:34 +0100258 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100259}
260
261/*
262 * Functions to check the current CTRL-X mode.
263 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000264int ctrl_x_mode_none(void)
265 { return ctrl_x_mode == 0; }
266int ctrl_x_mode_normal(void)
267 { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void)
269 { return ctrl_x_mode == CTRL_X_SCROLL; }
270int ctrl_x_mode_whole_line(void)
271 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
272int ctrl_x_mode_files(void)
273 { return ctrl_x_mode == CTRL_X_FILES; }
274int ctrl_x_mode_tags(void)
275 { return ctrl_x_mode == CTRL_X_TAGS; }
276int ctrl_x_mode_path_patterns(void)
277 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
278int ctrl_x_mode_path_defines(void)
279 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
280int ctrl_x_mode_dictionary(void)
281 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
282int ctrl_x_mode_thesaurus(void)
283 { return ctrl_x_mode == CTRL_X_THESAURUS; }
284int ctrl_x_mode_cmdline(void)
285 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200286 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000287int ctrl_x_mode_function(void)
288 { return ctrl_x_mode == CTRL_X_FUNCTION; }
289int ctrl_x_mode_omni(void)
290 { return ctrl_x_mode == CTRL_X_OMNI; }
291int ctrl_x_mode_spell(void)
292 { return ctrl_x_mode == CTRL_X_SPELL; }
293static int ctrl_x_mode_eval(void)
294 { return ctrl_x_mode == CTRL_X_EVAL; }
295int ctrl_x_mode_line_or_eval(void)
296 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100297
298/*
299 * Whether other than default completion has been selected.
300 */
301 int
302ctrl_x_mode_not_default(void)
303{
304 return ctrl_x_mode != CTRL_X_NORMAL;
305}
306
307/*
zeertzjqdca29d92021-08-31 19:12:51 +0200308 * Whether CTRL-X was typed without a following character,
309 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100310 */
311 int
312ctrl_x_mode_not_defined_yet(void)
313{
314 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
315}
316
317/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000318 * Return TRUE if currently in "normal" or "adding" insert completion matches
319 * state
320 */
321 int
322compl_status_adding(void)
323{
324 return compl_cont_status & CONT_ADDING;
325}
326
327/*
328 * Return TRUE if the completion pattern includes start of line, just for
329 * word-wise expansion.
330 */
331 int
332compl_status_sol(void)
333{
334 return compl_cont_status & CONT_SOL;
335}
336
337/*
338 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
339 */
340 int
341compl_status_local(void)
342{
343 return compl_cont_status & CONT_LOCAL;
344}
345
346/*
347 * Clear the completion status flags
348 */
349 void
350compl_status_clear(void)
351{
352 compl_cont_status = 0;
353}
354
355/*
356 * Return TRUE if completion is using the forward direction matches
357 */
358 static int
359compl_dir_forward(void)
360{
361 return compl_direction == FORWARD;
362}
363
364/*
365 * Return TRUE if currently showing forward completion matches
366 */
367 static int
368compl_shows_dir_forward(void)
369{
370 return compl_shows_dir == FORWARD;
371}
372
373/*
374 * Return TRUE if currently showing backward completion matches
375 */
376 static int
377compl_shows_dir_backward(void)
378{
379 return compl_shows_dir == BACKWARD;
380}
381
382/*
383 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100384 */
385 int
386has_compl_option(int dict_opt)
387{
388 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200389#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100390 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200391#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100393 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
394#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100395 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100396#endif
397 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100398 {
399 ctrl_x_mode = CTRL_X_NORMAL;
400 edit_submode = NULL;
401 msg_attr(dict_opt ? _("'dictionary' option is empty")
402 : _("'thesaurus' option is empty"),
403 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100404 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100405 {
406 vim_beep(BO_COMPL);
407 setcursor();
408 out_flush();
409#ifdef FEAT_EVAL
410 if (!get_vim_var_nr(VV_TESTING))
411#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100412 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100413 }
414 return FALSE;
415 }
416 return TRUE;
417}
418
419/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000420 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100421 * This depends on the current mode.
422 */
423 int
424vim_is_ctrl_x_key(int c)
425{
426 // Always allow ^R - let its results then be checked
427 if (c == Ctrl_R)
428 return TRUE;
429
430 // Accept <PageUp> and <PageDown> if the popup menu is visible.
431 if (ins_compl_pum_key(c))
432 return TRUE;
433
434 switch (ctrl_x_mode)
435 {
436 case 0: // Not in any CTRL-X mode
437 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
438 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200439 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100440 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
441 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
442 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
443 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
444 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200445 || c == Ctrl_S || c == Ctrl_K || c == 's'
446 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100447 case CTRL_X_SCROLL:
448 return (c == Ctrl_Y || c == Ctrl_E);
449 case CTRL_X_WHOLE_LINE:
450 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
451 case CTRL_X_FILES:
452 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
453 case CTRL_X_DICTIONARY:
454 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_THESAURUS:
456 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_TAGS:
458 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
459#ifdef FEAT_FIND_ID
460 case CTRL_X_PATH_PATTERNS:
461 return (c == Ctrl_P || c == Ctrl_N);
462 case CTRL_X_PATH_DEFINES:
463 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
464#endif
465 case CTRL_X_CMDLINE:
466 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
467 || c == Ctrl_X);
468#ifdef FEAT_COMPL_FUNC
469 case CTRL_X_FUNCTION:
470 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
471 case CTRL_X_OMNI:
472 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
473#endif
474 case CTRL_X_SPELL:
475 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
476 case CTRL_X_EVAL:
477 return (c == Ctrl_P || c == Ctrl_N);
478 }
479 internal_error("vim_is_ctrl_x_key()");
480 return FALSE;
481}
482
483/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000484 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000485 */
486 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000487match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000488{
489 return match->cp_flags & CP_ORIGINAL_TEXT;
490}
491
492/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000493 * Returns TRUE if "match" is the first match in the completion list.
494 */
495 static int
496is_first_match(compl_T *match)
497{
498 return match == compl_first_match;
499}
500
501/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100502 * Return TRUE when character "c" is part of the item currently being
503 * completed. Used to decide whether to abandon complete mode when the menu
504 * is visible.
505 */
506 int
507ins_compl_accept_char(int c)
508{
509 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
510 // When expanding an identifier only accept identifier chars.
511 return vim_isIDc(c);
512
513 switch (ctrl_x_mode)
514 {
515 case CTRL_X_FILES:
516 // When expanding file name only accept file name chars. But not
517 // path separators, so that "proto/<Tab>" expands files in
518 // "proto", not "proto/" as a whole
519 return vim_isfilec(c) && !vim_ispathsep(c);
520
521 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200522 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100523 case CTRL_X_OMNI:
524 // Command line and Omni completion can work with just about any
525 // printable character, but do stop at white space.
526 return vim_isprintc(c) && !VIM_ISWHITE(c);
527
528 case CTRL_X_WHOLE_LINE:
529 // For while line completion a space can be part of the line.
530 return vim_isprintc(c);
531 }
532 return vim_iswordc(c);
533}
534
535/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000536 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100537 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000538 */
539 static char_u *
540ins_compl_infercase_gettext(
541 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100542 int char_len,
543 int compl_char_len,
544 int min_len,
545 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000546{
547 int *wca; // Wide character array.
548 char_u *p;
549 int i, c;
550 int has_lower = FALSE;
551 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100552 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000553
554 IObuff[0] = NUL;
555
556 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100557 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000558 if (wca == NULL)
559 return IObuff;
560
561 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100562 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100563 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000564 if (has_mbyte)
565 wca[i] = mb_ptr2char_adv(&p);
566 else
567 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100568 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000569
570 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100571 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000572 for (i = 0; i < min_len; ++i)
573 {
glepnir6e199932024-12-14 21:13:27 +0100574 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000575 if (MB_ISLOWER(c))
576 {
577 has_lower = TRUE;
578 if (MB_ISUPPER(wca[i]))
579 {
580 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100581 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000582 wca[i] = MB_TOLOWER(wca[i]);
583 break;
584 }
585 }
586 }
587
588 // Rule 2: No lower case, 2nd consecutive letter converted to
589 // upper case.
590 if (!has_lower)
591 {
John Marriott5e6ea922024-11-23 14:01:57 +0100592 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000593 for (i = 0; i < min_len; ++i)
594 {
glepnir6e199932024-12-14 21:13:27 +0100595 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000596 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
597 {
598 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100599 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000600 wca[i] = MB_TOUPPER(wca[i]);
601 break;
602 }
603 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
604 }
605 }
606
607 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100608 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000609 for (i = 0; i < min_len; ++i)
610 {
glepnir6e199932024-12-14 21:13:27 +0100611 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000612 if (MB_ISLOWER(c))
613 wca[i] = MB_TOLOWER(wca[i]);
614 else if (MB_ISUPPER(c))
615 wca[i] = MB_TOUPPER(wca[i]);
616 }
617
618 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000619 p = IObuff;
620 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100621 ga_init2(&gap, 1, 500);
622 while (i < char_len)
623 {
624 if (gap.ga_data != NULL)
625 {
626 if (ga_grow(&gap, 10) == FAIL)
627 {
628 ga_clear(&gap);
629 return (char_u *)"[failed]";
630 }
631 p = (char_u *)gap.ga_data + gap.ga_len;
632 if (has_mbyte)
633 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
634 else
635 {
636 *p = wca[i++];
637 ++gap.ga_len;
638 }
639 }
640 else if ((p - IObuff) + 6 >= IOSIZE)
641 {
642 // Multi-byte characters can occupy up to five bytes more than
643 // ASCII characters, and we also need one byte for NUL, so when
644 // getting to six bytes from the edge of IObuff switch to using a
645 // growarray. Add the character in the next round.
646 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100647 {
648 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100649 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100650 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100651 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100652 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100653 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100654 }
655 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000656 p += (*mb_char2bytes)(wca[i++], p);
657 else
658 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100659 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000660 vim_free(wca);
661
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100662 if (gap.ga_data != NULL)
663 {
664 *tofree = gap.ga_data;
665 return gap.ga_data;
666 }
667
668 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000669 return IObuff;
670}
671
672/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
674 * case of the originally typed text is used, and the case of the completed
675 * text is inferred, ie this tries to work out what case you probably wanted
676 * the rest of the word to be in -- webb
677 */
678 int
679ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200680 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100681 int len,
682 int icase,
683 char_u *fname,
684 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200685 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100686{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200687 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100688 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100689 int char_len; // count multi-byte characters
690 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200692 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100693 int res;
694 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695
696 if (p_ic && curbuf->b_p_inf && len > 0)
697 {
698 // Infer case of completed part.
699
700 // Find actual length of completion.
701 if (has_mbyte)
702 {
703 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100704 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100705 while (*p != NUL)
706 {
707 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100708 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709 }
710 }
711 else
glepnir6e199932024-12-14 21:13:27 +0100712 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100713 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100714 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715
716 // Find actual length of original text.
717 if (has_mbyte)
718 {
John Marriott5e6ea922024-11-23 14:01:57 +0100719 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100720 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100721 while (*p != NUL)
722 {
723 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 }
726 }
727 else
glepnir6e199932024-12-14 21:13:27 +0100728 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100729 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100730 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100731
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100734 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 str = ins_compl_infercase_gettext(str, char_len,
737 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200739 if (cont_s_ipos)
740 flags |= CP_CONT_S_IPOS;
741 if (icase)
742 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200743
glepnir5c66e232024-11-15 19:58:27 +0100744 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100745 vim_free(tofree);
746 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100747}
748
749/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000750 * Add a match to the list of matches. The arguments are:
751 * str - text of the match to add
752 * len - length of "str". If -1, then the length of "str" is
753 * computed.
754 * fname - file name to associate with this match.
755 * cptext - list of strings to use with this match (for abbr, menu, info
756 * and kind)
757 * user_data - user supplied data (any vim type) for this match
758 * cdir - match direction. If 0, use "compl_direction".
759 * flags_arg - match flags (cp_flags)
760 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100761 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000762 * If "cdir" is FORWARD, then the match is added after the current match.
763 * Otherwise, it is added before the current match.
764 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100765 * If the given string is already in the list of completions, then return
766 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
767 * maybe because alloc() returns NULL, then FAIL is returned.
768 */
769 static int
770ins_compl_add(
771 char_u *str,
772 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100773 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 char_u **cptext, // extra text for popup menu or NULL
775 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100776 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200777 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200778 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100779 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780{
781 compl_T *match;
782 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200783 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784
Bram Moolenaarceb06192021-04-04 15:05:22 +0200785 if (flags & CP_FAST)
786 fast_breakcheck();
787 else
788 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100789 if (got_int)
790 return FAIL;
791 if (len < 0)
792 len = (int)STRLEN(str);
793
794 // If the same match is already present, don't add it.
795 if (compl_first_match != NULL && !adup)
796 {
797 match = compl_first_match;
798 do
799 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000800 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100801 && STRNCMP(match->cp_str.string, str, len) == 0
802 && ((int)match->cp_str.length <= len
803 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 return NOTDONE;
805 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000806 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 }
808
809 // Remove any popup menu before changing the list of matches.
810 ins_compl_del_pum();
811
812 // Allocate a new match structure.
813 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200814 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 if (match == NULL)
816 return FAIL;
817 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200818 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 match->cp_number = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100820 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100821 {
822 vim_free(match);
823 return FAIL;
824 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100825
John Marriott5e6ea922024-11-23 14:01:57 +0100826 match->cp_str.length = len;
827
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100828 // match-fname is:
829 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200830 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100831 // - NULL otherwise. --Acevedo
832 if (fname != NULL
833 && compl_curr_match != NULL
834 && compl_curr_match->cp_fname != NULL
835 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
836 match->cp_fname = compl_curr_match->cp_fname;
837 else if (fname != NULL)
838 {
839 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200840 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100841 }
842 else
843 match->cp_fname = NULL;
844 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100845 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
846 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100847
848 if (cptext != NULL)
849 {
850 int i;
851
852 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100853 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100854 if (cptext[i] != NULL && *cptext[i] != NUL)
855 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100856 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100857 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100858#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100859 if (user_data != NULL)
860 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100861#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000863 // Link the new match structure after (FORWARD) or before (BACKWARD) the
864 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100865 if (compl_first_match == NULL)
866 match->cp_next = match->cp_prev = NULL;
867 else if (dir == FORWARD)
868 {
869 match->cp_next = compl_curr_match->cp_next;
870 match->cp_prev = compl_curr_match;
871 }
872 else // BACKWARD
873 {
874 match->cp_next = compl_curr_match;
875 match->cp_prev = compl_curr_match->cp_prev;
876 }
877 if (match->cp_next)
878 match->cp_next->cp_prev = match;
879 if (match->cp_prev)
880 match->cp_prev->cp_next = match;
881 else // if there's nothing before, it is the first match
882 compl_first_match = match;
883 compl_curr_match = match;
884
885 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200886 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100887 ins_compl_longest_match(match);
888
889 return OK;
890}
891
892/*
893 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200894 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100895 */
896 static int
897ins_compl_equal(compl_T *match, char_u *str, int len)
898{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200899 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200900 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200901 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100902 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
903 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100904}
905
906/*
glepnir6a38aff2024-12-16 21:56:16 +0100907 * when len is -1 mean use whole length of p otherwise part of p
908 */
909 static void
910ins_compl_insert_bytes(char_u *p, int len)
911{
912 if (len == -1)
913 len = (int)STRLEN(p);
914 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100915 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100916}
917
918/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100919 * Checks if the column is within the currently inserted completion text
920 * column range. If it is, it returns a special highlight attribute.
921 * -1 mean normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100922 */
923 int
924ins_compl_col_range_attr(int col)
925{
926 if (col >= compl_col && col < compl_ins_end_col)
927 return syn_name2attr((char_u *)"ComplMatchIns");
928
929 return -1;
930}
931
932/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 * Reduce the longest common string for match "match".
934 */
935 static void
936ins_compl_longest_match(compl_T *match)
937{
938 char_u *p, *s;
939 int c1, c2;
940 int had_match;
941
John Marriott5e6ea922024-11-23 14:01:57 +0100942 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100943 {
944 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100945 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
946 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000947 return;
948
John Marriott5e6ea922024-11-23 14:01:57 +0100949 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000950 had_match = (curwin->w_cursor.col > compl_col);
951 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100952 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000953 ins_redraw(FALSE);
954
955 // When the match isn't there (to avoid matching itself) remove it
956 // again after redrawing.
957 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100958 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100959 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000960
961 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100962 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000963
964 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100965 p = compl_leader.string;
966 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967 while (*p != NUL)
968 {
969 if (has_mbyte)
970 {
971 c1 = mb_ptr2char(p);
972 c2 = mb_ptr2char(s);
973 }
974 else
975 {
976 c1 = *p;
977 c2 = *s;
978 }
979 if ((match->cp_flags & CP_ICASE)
980 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
981 break;
982 if (has_mbyte)
983 {
984 MB_PTR_ADV(p);
985 MB_PTR_ADV(s);
986 }
987 else
988 {
989 ++p;
990 ++s;
991 }
992 }
993
994 if (*p != NUL)
995 {
996 // Leader was shortened, need to change the inserted text.
997 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +0100998 compl_leader.length = (size_t)(p - compl_leader.string);
999
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001000 had_match = (curwin->w_cursor.col > compl_col);
1001 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001002 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001003 ins_redraw(FALSE);
1004
1005 // When the match isn't there (to avoid matching itself) remove it
1006 // again after redrawing.
1007 if (!had_match)
1008 ins_compl_delete();
1009 }
1010
1011 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001012}
1013
1014/*
1015 * Add an array of matches to the list of matches.
1016 * Frees matches[].
1017 */
1018 static void
1019ins_compl_add_matches(
1020 int num_matches,
1021 char_u **matches,
1022 int icase)
1023{
1024 int i;
1025 int add_r = OK;
1026 int dir = compl_direction;
1027
1028 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001029 {
1030 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1031 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1032 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033 // if dir was BACKWARD then honor it just once
1034 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001035 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001036 FreeWild(num_matches, matches);
1037}
1038
1039/*
1040 * Make the completion list cyclic.
1041 * Return the number of matches (excluding the original).
1042 */
1043 static int
1044ins_compl_make_cyclic(void)
1045{
1046 compl_T *match;
1047 int count = 0;
1048
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001049 if (compl_first_match == NULL)
1050 return 0;
1051
1052 // Find the end of the list.
1053 match = compl_first_match;
1054 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001055 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001056 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001057 match = match->cp_next;
1058 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001059 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001060 match->cp_next = compl_first_match;
1061 compl_first_match->cp_prev = match;
1062
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001063 return count;
1064}
1065
1066/*
1067 * Return whether there currently is a shown match.
1068 */
1069 int
1070ins_compl_has_shown_match(void)
1071{
1072 return compl_shown_match == NULL
1073 || compl_shown_match != compl_shown_match->cp_next;
1074}
1075
1076/*
1077 * Return whether the shown match is long enough.
1078 */
1079 int
1080ins_compl_long_shown_match(void)
1081{
John Marriott5e6ea922024-11-23 14:01:57 +01001082 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001083 > curwin->w_cursor.col - compl_col;
1084}
1085
1086/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001087 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001089 unsigned int
1090get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001091{
zeertzjq529b9ad2024-06-05 20:27:06 +02001092 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001093}
1094
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001095/*
1096 * Update the screen and when there is any scrolling remove the popup menu.
1097 */
1098 static void
1099ins_compl_upd_pum(void)
1100{
1101 int h;
1102
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001103 if (compl_match_array == NULL)
1104 return;
1105
1106 h = curwin->w_cline_height;
1107 // Update the screen later, before drawing the popup menu over it.
1108 pum_call_update_screen();
1109 if (h != curwin->w_cline_height)
1110 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001111}
1112
1113/*
1114 * Remove any popup menu.
1115 */
1116 static void
1117ins_compl_del_pum(void)
1118{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001119 if (compl_match_array == NULL)
1120 return;
1121
1122 pum_undisplay();
1123 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001124}
1125
1126/*
1127 * Return TRUE if the popup menu should be displayed.
1128 */
1129 int
1130pum_wanted(void)
1131{
1132 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001133 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001134 return FALSE;
1135
1136 // The display looks bad on a B&W display.
1137 if (t_colors < 8
1138#ifdef FEAT_GUI
1139 && !gui.in_use
1140#endif
1141 )
1142 return FALSE;
1143 return TRUE;
1144}
1145
1146/*
1147 * Return TRUE if there are two or more matches to be shown in the popup menu.
1148 * One if 'completopt' contains "menuone".
1149 */
1150 static int
1151pum_enough_matches(void)
1152{
1153 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001154 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001155
1156 // Don't display the popup menu if there are no matches or there is only
1157 // one (ignoring the original text).
1158 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159 do
1160 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001161 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001162 break;
1163 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001164 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001165
zeertzjq529b9ad2024-06-05 20:27:06 +02001166 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001167 return (i >= 1);
1168 return (i >= 2);
1169}
1170
Bram Moolenaar3075a452021-11-17 15:51:52 +00001171#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001172/*
1173 * Allocate Dict for the completed item.
1174 * { word, abbr, menu, kind, info }
1175 */
1176 static dict_T *
1177ins_compl_dict_alloc(compl_T *match)
1178{
1179 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1180
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001181 if (dict == NULL)
1182 return NULL;
1183
John Marriott5e6ea922024-11-23 14:01:57 +01001184 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001185 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1186 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1187 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1188 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1189 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1190 dict_add_string(dict, "user_data", (char_u *)"");
1191 else
1192 dict_add_tv(dict, "user_data", &match->cp_user_data);
1193
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001194 return dict;
1195}
1196
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001197/*
1198 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1199 * completion menu is changed.
1200 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 static void
1202trigger_complete_changed_event(int cur)
1203{
1204 dict_T *v_event;
1205 dict_T *item;
1206 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001207 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001208
1209 if (recursive)
1210 return;
1211
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212 if (cur < 0)
1213 item = dict_alloc();
1214 else
1215 item = ins_compl_dict_alloc(compl_curr_match);
1216 if (item == NULL)
1217 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001218 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001219 dict_add_dict(v_event, "completed_item", item);
1220 pum_set_event_info(v_event);
1221 dict_set_items_ro(v_event);
1222
1223 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001224 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001225 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001226 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001227 recursive = FALSE;
1228
Bram Moolenaar3075a452021-11-17 15:51:52 +00001229 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001230}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001231#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001232
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001233/*
glepnira218cc62024-06-03 19:32:39 +02001234 * pumitem qsort compare func
1235 */
1236 static int
zeertzjq8e567472024-06-14 20:04:42 +02001237ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001238{
1239 const int sa = (*(pumitem_T *)a).pum_score;
1240 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001241 const int ia = (*(pumitem_T *)a).pum_idx;
1242 const int ib = (*(pumitem_T *)b).pum_idx;
1243 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001244}
1245
1246/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001247 * Build a popup menu to show the completion matches.
1248 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1249 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001250 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001251 static int
1252ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001253{
1254 compl_T *compl;
1255 compl_T *shown_compl = NULL;
1256 int did_find_shown_match = FALSE;
1257 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001258 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001259 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001260 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001261 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001262 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1263 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001264 compl_T *match_head = NULL;
1265 compl_T *match_tail = NULL;
1266 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001267
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001268 // Need to build the popup menu list.
1269 compl_match_arraysize = 0;
1270 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001271
glepnira49c0772024-11-30 10:56:30 +01001272 // If the current match is the original text don't find the first
1273 // match after it, don't highlight anything.
1274 if (match_at_original_text(compl_shown_match))
1275 shown_match_ok = TRUE;
1276
1277 if (compl_leader.string != NULL
1278 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1279 && shown_match_ok == FALSE)
1280 compl_shown_match = compl_no_select ? compl_first_match
1281 : compl_first_match->cp_next;
1282
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001283 do
1284 {
zeertzjq551d8c32024-06-05 19:53:32 +02001285 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1286 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001287 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1288 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001289
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001290 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001291 && (compl_leader.string == NULL
1292 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001293 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001294 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001295 ++compl_match_arraysize;
glepnir80b66202024-11-27 21:53:53 +01001296 if (match_head == NULL)
1297 match_head = compl;
1298 else
glepnira49c0772024-11-30 10:56:30 +01001299 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001300 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001301
1302 if (!shown_match_ok && !compl_fuzzy_match)
1303 {
1304 if (compl == compl_shown_match || did_find_shown_match)
1305 {
1306 // This item is the shown match or this is the
1307 // first displayed item after the shown match.
1308 compl_shown_match = compl;
1309 did_find_shown_match = TRUE;
1310 shown_match_ok = TRUE;
1311 }
1312 else
1313 // Remember this displayed match for when the
1314 // shown match is just below it.
1315 shown_compl = compl;
1316 cur = i;
1317 }
1318 else if (compl_fuzzy_match)
1319 {
1320 if (i == 0)
1321 shown_compl = compl;
1322 // Update the maximum fuzzy score and the shown match
1323 // if the current item's score is higher
1324 if (compl->cp_score > max_fuzzy_score)
1325 {
1326 did_find_shown_match = TRUE;
1327 max_fuzzy_score = compl->cp_score;
1328 if (!compl_no_select)
1329 compl_shown_match = compl;
1330 }
1331
1332 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1333 {
1334 cur = i;
1335 shown_match_ok = TRUE;
1336 }
1337 }
1338 i++;
glepnir80b66202024-11-27 21:53:53 +01001339 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001340
glepnira218cc62024-06-03 19:32:39 +02001341 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001342 {
1343 did_find_shown_match = TRUE;
1344
1345 // When the original text is the shown match don't set
1346 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001347 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 shown_match_ok = TRUE;
1349
1350 if (!shown_match_ok && shown_compl != NULL)
1351 {
1352 // The shown match isn't displayed, set it to the
1353 // previously displayed match.
1354 compl_shown_match = shown_compl;
1355 shown_match_ok = TRUE;
1356 }
1357 }
glepnira49c0772024-11-30 10:56:30 +01001358 compl = compl->cp_next;
1359 } while (compl != NULL && !is_first_match(compl));
1360
1361 if (compl_match_arraysize == 0)
1362 return -1;
1363
1364 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1365 if (compl_match_array == NULL)
1366 return -1;
1367
1368 compl = match_head;
1369 i = 0;
1370 while (compl != NULL)
1371 {
glepnir6e199932024-12-14 21:13:27 +01001372 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1373 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001374 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1375 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1376 compl_match_array[i].pum_score = compl->cp_score;
1377 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1378 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001379 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1380 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001381 match_next = compl->cp_match_next;
1382 compl->cp_match_next = NULL;
1383 compl = match_next;
1384 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001385
John Marriott5e6ea922024-11-23 14:01:57 +01001386 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001387 {
1388 for (i = 0; i < compl_match_arraysize; i++)
1389 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001390 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001391 qsort(compl_match_array, (size_t)compl_match_arraysize,
1392 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001393 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001394 }
glepnira218cc62024-06-03 19:32:39 +02001395
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001396 if (!shown_match_ok) // no displayed match at all
1397 cur = -1;
1398
1399 return cur;
1400}
1401
1402/*
1403 * Show the popup menu for the list of matches.
1404 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1405 */
1406 void
1407ins_compl_show_pum(void)
1408{
1409 int i;
1410 int cur = -1;
1411 colnr_T col;
1412
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001413 if (!pum_wanted() || !pum_enough_matches())
1414 return;
1415
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001416 // Update the screen later, before drawing the popup menu over it.
1417 pum_call_update_screen();
1418
1419 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001420 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001421 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 else
1423 {
1424 // popup menu already exists, only need to find the current item.
1425 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001426 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001427 || compl_match_array[i].pum_text
1428 == compl_shown_match->cp_text[CPT_ABBR])
1429 {
1430 cur = i;
1431 break;
1432 }
1433 }
1434
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001435 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001436 {
1437#ifdef FEAT_EVAL
1438 if (compl_started && has_completechanged())
1439 trigger_complete_changed_event(cur);
1440#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001441 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001442 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001443
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 // In Replace mode when a $ is displayed at the end of the line only
1445 // part of the screen would be updated. We do need to redraw here.
1446 dollar_vcol = -1;
1447
1448 // Compute the screen column of the start of the completed text.
1449 // Use the cursor to get all wrapping and other settings right.
1450 col = curwin->w_cursor.col;
1451 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001452 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001453 pum_display(compl_match_array, compl_match_arraysize, cur);
1454 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001455
glepnircbb46b42024-02-03 18:11:13 +01001456 // After adding leader, set the current match to shown match.
1457 if (compl_started && compl_curr_match != compl_shown_match)
1458 compl_curr_match = compl_shown_match;
1459
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001460#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001461 if (has_completechanged())
1462 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001463#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001464}
1465
1466#define DICT_FIRST (1) // use just first element in "dict"
1467#define DICT_EXACT (2) // "dict" is the exact name of a file
1468
1469/*
glepnir40c1c332024-06-11 19:37:04 +02001470 * Get current completion leader
1471 */
1472 char_u *
1473ins_compl_leader(void)
1474{
John Marriott5e6ea922024-11-23 14:01:57 +01001475 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1476}
1477
1478/*
1479 * Get current completion leader length
1480 */
1481 size_t
1482ins_compl_leader_len(void)
1483{
1484 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001485}
1486
1487/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001488 * Add any identifiers that match the given pattern "pat" in the list of
1489 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001490 */
1491 static void
1492ins_compl_dictionaries(
1493 char_u *dict_start,
1494 char_u *pat,
1495 int flags, // DICT_FIRST and/or DICT_EXACT
1496 int thesaurus) // Thesaurus completion
1497{
1498 char_u *dict = dict_start;
1499 char_u *ptr;
1500 char_u *buf;
1501 regmatch_T regmatch;
1502 char_u **files;
1503 int count;
1504 int save_p_scs;
1505 int dir = compl_direction;
1506
1507 if (*dict == NUL)
1508 {
1509#ifdef FEAT_SPELL
1510 // When 'dictionary' is empty and spell checking is enabled use
1511 // "spell".
1512 if (!thesaurus && curwin->w_p_spell)
1513 dict = (char_u *)"spell";
1514 else
1515#endif
1516 return;
1517 }
1518
1519 buf = alloc(LSIZE);
1520 if (buf == NULL)
1521 return;
1522 regmatch.regprog = NULL; // so that we can goto theend
1523
1524 // If 'infercase' is set, don't use 'smartcase' here
1525 save_p_scs = p_scs;
1526 if (curbuf->b_p_inf)
1527 p_scs = FALSE;
1528
1529 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1530 // to only match at the start of a line. Otherwise just match the
1531 // pattern. Also need to double backslashes.
1532 if (ctrl_x_mode_line_or_eval())
1533 {
1534 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1535 size_t len;
1536
1537 if (pat_esc == NULL)
1538 goto theend;
1539 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001540 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001541 if (ptr == NULL)
1542 {
1543 vim_free(pat_esc);
1544 goto theend;
1545 }
1546 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1547 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1548 vim_free(pat_esc);
1549 vim_free(ptr);
1550 }
1551 else
1552 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001553 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001554 if (regmatch.regprog == NULL)
1555 goto theend;
1556 }
1557
1558 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1559 regmatch.rm_ic = ignorecase(pat);
1560 while (*dict != NUL && !got_int && !compl_interrupted)
1561 {
1562 // copy one dictionary file name into buf
1563 if (flags == DICT_EXACT)
1564 {
1565 count = 1;
1566 files = &dict;
1567 }
1568 else
1569 {
1570 // Expand wildcards in the dictionary name, but do not allow
1571 // backticks (for security, the 'dict' option may have been set in
1572 // a modeline).
1573 copy_option_part(&dict, buf, LSIZE, ",");
1574# ifdef FEAT_SPELL
1575 if (!thesaurus && STRCMP(buf, "spell") == 0)
1576 count = -1;
1577 else
1578# endif
1579 if (vim_strchr(buf, '`') != NULL
1580 || expand_wildcards(1, &buf, &count, &files,
1581 EW_FILE|EW_SILENT) != OK)
1582 count = 0;
1583 }
1584
1585# ifdef FEAT_SPELL
1586 if (count == -1)
1587 {
1588 // Complete from active spelling. Skip "\<" in the pattern, we
1589 // don't use it as a RE.
1590 if (pat[0] == '\\' && pat[1] == '<')
1591 ptr = pat + 2;
1592 else
1593 ptr = pat;
1594 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1595 }
1596 else
1597# endif
1598 if (count > 0) // avoid warning for using "files" uninit
1599 {
1600 ins_compl_files(count, files, thesaurus, flags,
1601 &regmatch, buf, &dir);
1602 if (flags != DICT_EXACT)
1603 FreeWild(count, files);
1604 }
1605 if (flags != 0)
1606 break;
1607 }
1608
1609theend:
1610 p_scs = save_p_scs;
1611 vim_regfree(regmatch.regprog);
1612 vim_free(buf);
1613}
1614
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001615/*
1616 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1617 * skipping the word at 'skip_word'. Returns OK on success.
1618 */
1619 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001620thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001621 char_u *fname,
1622 char_u **buf_arg,
1623 int dir,
1624 char_u *skip_word)
1625{
1626 int status = OK;
1627 char_u *ptr;
1628 char_u *wstart;
1629
1630 // Add the other matches on the line
1631 ptr = *buf_arg;
1632 while (!got_int)
1633 {
1634 // Find start of the next word. Skip white
1635 // space and punctuation.
1636 ptr = find_word_start(ptr);
1637 if (*ptr == NUL || *ptr == NL)
1638 break;
1639 wstart = ptr;
1640
1641 // Find end of the word.
1642 if (has_mbyte)
1643 // Japanese words may have characters in
1644 // different classes, only separate words
1645 // with single-byte non-word characters.
1646 while (*ptr != NUL)
1647 {
1648 int l = (*mb_ptr2len)(ptr);
1649
1650 if (l < 2 && !vim_iswordc(*ptr))
1651 break;
1652 ptr += l;
1653 }
1654 else
1655 ptr = find_word_end(ptr);
1656
1657 // Add the word. Skip the regexp match.
1658 if (wstart != skip_word)
1659 {
1660 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1661 fname, dir, FALSE);
1662 if (status == FAIL)
1663 break;
1664 }
1665 }
1666
1667 *buf_arg = ptr;
1668 return status;
1669}
1670
1671/*
1672 * Process "count" dictionary/thesaurus "files" and add the text matching
1673 * "regmatch".
1674 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001675 static void
1676ins_compl_files(
1677 int count,
1678 char_u **files,
1679 int thesaurus,
1680 int flags,
1681 regmatch_T *regmatch,
1682 char_u *buf,
1683 int *dir)
1684{
1685 char_u *ptr;
1686 int i;
1687 FILE *fp;
1688 int add_r;
1689
1690 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1691 {
1692 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001693 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001694 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001695 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001696 vim_snprintf((char *)IObuff, IOSIZE,
1697 _("Scanning dictionary: %s"), (char *)files[i]);
1698 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1699 }
1700
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001701 if (fp == NULL)
1702 continue;
1703
1704 // Read dictionary file line by line.
1705 // Check each line for a match.
1706 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001708 ptr = buf;
1709 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001710 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001711 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001712 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1713 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 add_r = ins_compl_add_infercase(regmatch->startp[0],
1715 (int)(ptr - regmatch->startp[0]),
1716 p_ic, files[i], *dir, FALSE);
1717 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001718 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001719 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001721 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001722 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001723 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001724 if (add_r == OK)
1725 // if dir was BACKWARD then honor it just once
1726 *dir = FORWARD;
1727 else if (add_r == FAIL)
1728 break;
1729 // avoid expensive call to vim_regexec() when at end
1730 // of line
1731 if (*ptr == '\n' || got_int)
1732 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001733 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001734 line_breakcheck();
1735 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001736 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001737 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001738 }
1739}
1740
1741/*
1742 * Find the start of the next word.
1743 * Returns a pointer to the first char of the word. Also stops at a NUL.
1744 */
1745 char_u *
1746find_word_start(char_u *ptr)
1747{
1748 if (has_mbyte)
1749 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1750 ptr += (*mb_ptr2len)(ptr);
1751 else
1752 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1753 ++ptr;
1754 return ptr;
1755}
1756
1757/*
1758 * Find the end of the word. Assumes it starts inside a word.
1759 * Returns a pointer to just after the word.
1760 */
1761 char_u *
1762find_word_end(char_u *ptr)
1763{
1764 int start_class;
1765
1766 if (has_mbyte)
1767 {
1768 start_class = mb_get_class(ptr);
1769 if (start_class > 1)
1770 while (*ptr != NUL)
1771 {
1772 ptr += (*mb_ptr2len)(ptr);
1773 if (mb_get_class(ptr) != start_class)
1774 break;
1775 }
1776 }
1777 else
1778 while (vim_iswordc(*ptr))
1779 ++ptr;
1780 return ptr;
1781}
1782
1783/*
1784 * Find the end of the line, omitting CR and NL at the end.
1785 * Returns a pointer to just after the line.
1786 */
1787 static char_u *
1788find_line_end(char_u *ptr)
1789{
1790 char_u *s;
1791
1792 s = ptr + STRLEN(ptr);
1793 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1794 --s;
1795 return s;
1796}
1797
1798/*
1799 * Free the list of completions
1800 */
1801 static void
1802ins_compl_free(void)
1803{
1804 compl_T *match;
1805 int i;
1806
John Marriott5e6ea922024-11-23 14:01:57 +01001807 VIM_CLEAR_STRING(compl_pattern);
1808 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001809
1810 if (compl_first_match == NULL)
1811 return;
1812
1813 ins_compl_del_pum();
1814 pum_clear();
1815
1816 compl_curr_match = compl_first_match;
1817 do
1818 {
1819 match = compl_curr_match;
1820 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001821 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001822 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001823 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001824 vim_free(match->cp_fname);
1825 for (i = 0; i < CPT_COUNT; ++i)
1826 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001827#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001828 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001829#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001830 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001831 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001832 compl_first_match = compl_curr_match = NULL;
1833 compl_shown_match = NULL;
1834 compl_old_match = NULL;
1835}
1836
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001837/*
1838 * Reset/clear the completion state.
1839 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001840 void
1841ins_compl_clear(void)
1842{
1843 compl_cont_status = 0;
1844 compl_started = FALSE;
1845 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001846 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001847 VIM_CLEAR_STRING(compl_pattern);
1848 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001849 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001850 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001851 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001852#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001853 // clear v:completed_item
1854 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856}
1857
1858/*
1859 * Return TRUE when Insert completion is active.
1860 */
1861 int
1862ins_compl_active(void)
1863{
1864 return compl_started;
1865}
1866
1867/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001868 * Return True when wp is the actual completion window
1869 */
1870 int
1871ins_compl_win_active(win_T *wp UNUSED)
1872{
1873 return ins_compl_active()
1874#if defined(FEAT_QUICKFIX)
1875 && (!wp->w_p_pvw
1876# ifdef FEAT_PROP_POPUP
1877 && !(wp->w_popup_flags & POPF_INFO)
1878# endif
1879 )
1880#endif
1881 ;
1882}
1883
1884/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001885 * Selected one of the matches. When FALSE the match was edited or using the
1886 * longest common string.
1887 */
1888 int
1889ins_compl_used_match(void)
1890{
1891 return compl_used_match;
1892}
1893
1894/*
1895 * Initialize get longest common string.
1896 */
1897 void
1898ins_compl_init_get_longest(void)
1899{
1900 compl_get_longest = FALSE;
1901}
1902
1903/*
1904 * Returns TRUE when insert completion is interrupted.
1905 */
1906 int
1907ins_compl_interrupted(void)
1908{
1909 return compl_interrupted;
1910}
1911
1912/*
1913 * Returns TRUE if the <Enter> key selects a match in the completion popup
1914 * menu.
1915 */
1916 int
1917ins_compl_enter_selects(void)
1918{
1919 return compl_enter_selects;
1920}
1921
1922/*
1923 * Return the column where the text starts that is being completed
1924 */
1925 colnr_T
1926ins_compl_col(void)
1927{
1928 return compl_col;
1929}
1930
1931/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001932 * Return the length in bytes of the text being completed
1933 */
1934 int
1935ins_compl_len(void)
1936{
1937 return compl_length;
1938}
1939
1940/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001941 * Delete one character before the cursor and show the subset of the matches
1942 * that match the word that is now before the cursor.
1943 * Returns the character to be used, NUL if the work is done and another char
1944 * to be got from the user.
1945 */
1946 int
1947ins_compl_bs(void)
1948{
1949 char_u *line;
1950 char_u *p;
1951
1952 line = ml_get_curline();
1953 p = line + curwin->w_cursor.col;
1954 MB_PTR_BACK(line, p);
1955
1956 // Stop completion when the whole word was deleted. For Omni completion
1957 // allow the word to be deleted, we won't match everything.
1958 // Respect the 'backspace' option.
1959 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001960 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1961 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001962 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1963 - compl_length < 0))
1964 return K_BS;
1965
1966 // Deleted more than what was used to find matches or didn't finish
1967 // finding all matches: need to look for matches all over again.
1968 if (curwin->w_cursor.col <= compl_col + compl_length
1969 || ins_compl_need_restart())
1970 ins_compl_restart();
1971
John Marriott5e6ea922024-11-23 14:01:57 +01001972 VIM_CLEAR_STRING(compl_leader);
1973 compl_leader.length = (size_t)((p - line) - compl_col);
1974 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1975 if (compl_leader.string == NULL)
1976 {
1977 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001978 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001979 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001980
1981 ins_compl_new_leader();
1982 if (compl_shown_match != NULL)
1983 // Make sure current match is not a hidden item.
1984 compl_curr_match = compl_shown_match;
1985 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001986}
1987
1988/*
1989 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1990 * be called.
1991 */
1992 static int
1993ins_compl_need_restart(void)
1994{
1995 // Return TRUE if we didn't complete finding matches or when the
1996 // 'completefunc' returned "always" in the "refresh" dictionary item.
1997 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001998 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001999 && compl_opt_refresh_always);
2000}
2001
2002/*
2003 * Called after changing "compl_leader".
2004 * Show the popup menu with a different set of matches.
2005 * May also search for matches again if the previous search was interrupted.
2006 */
2007 static void
2008ins_compl_new_leader(void)
2009{
2010 ins_compl_del_pum();
2011 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002012 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002013 compl_used_match = FALSE;
2014
2015 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002016 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002017 else
2018 {
2019#ifdef FEAT_SPELL
2020 spell_bad_len = 0; // need to redetect bad word
2021#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002022 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 // the popup menu display the changed text before the cursor. Set
2024 // "compl_restarting" to avoid that the first match is inserted.
2025 pum_call_update_screen();
2026#ifdef FEAT_GUI
2027 if (gui.in_use)
2028 {
2029 // Show the cursor after the match, not after the redrawn text.
2030 setcursor();
2031 out_flush_cursor(FALSE, FALSE);
2032 }
2033#endif
2034 compl_restarting = TRUE;
2035 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2036 compl_cont_status = 0;
2037 compl_restarting = FALSE;
2038 }
2039
2040 compl_enter_selects = !compl_used_match;
2041
2042 // Show the popup menu with a different set of matches.
2043 ins_compl_show_pum();
2044
2045 // Don't let Enter select the original text when there is no popup menu.
2046 if (compl_match_array == NULL)
2047 compl_enter_selects = FALSE;
2048}
2049
2050/*
2051 * Return the length of the completion, from the completion start column to
2052 * the cursor column. Making sure it never goes below zero.
2053 */
2054 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002055get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002056{
2057 int off = (int)curwin->w_cursor.col - (int)compl_col;
2058
2059 if (off < 0)
2060 return 0;
2061 return off;
2062}
2063
2064/*
2065 * Append one character to the match leader. May reduce the number of
2066 * matches.
2067 */
2068 void
2069ins_compl_addleader(int c)
2070{
2071 int cc;
2072
2073 if (stop_arrow() == FAIL)
2074 return;
2075 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2076 {
2077 char_u buf[MB_MAXBYTES + 1];
2078
2079 (*mb_char2bytes)(c, buf);
2080 buf[cc] = NUL;
2081 ins_char_bytes(buf, cc);
2082 if (compl_opt_refresh_always)
2083 AppendToRedobuff(buf);
2084 }
2085 else
2086 {
2087 ins_char(c);
2088 if (compl_opt_refresh_always)
2089 AppendCharToRedobuff(c);
2090 }
2091
2092 // If we didn't complete finding matches we must search again.
2093 if (ins_compl_need_restart())
2094 ins_compl_restart();
2095
2096 // When 'always' is set, don't reset compl_leader. While completing,
2097 // cursor doesn't point original position, changing compl_leader would
2098 // break redo.
2099 if (!compl_opt_refresh_always)
2100 {
John Marriott5e6ea922024-11-23 14:01:57 +01002101 VIM_CLEAR_STRING(compl_leader);
2102 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2103 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2104 compl_leader.length);
2105 if (compl_leader.string == NULL)
2106 {
2107 compl_leader.length = 0;
2108 return;
2109 }
2110
2111 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002112 }
2113}
2114
2115/*
2116 * Setup for finding completions again without leaving CTRL-X mode. Used when
2117 * BS or a key was typed while still searching for matches.
2118 */
2119 static void
2120ins_compl_restart(void)
2121{
2122 ins_compl_free();
2123 compl_started = FALSE;
2124 compl_matches = 0;
2125 compl_cont_status = 0;
2126 compl_cont_mode = 0;
2127}
2128
2129/*
2130 * Set the first match, the original text.
2131 */
2132 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002133ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002134{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002135 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002136 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2137 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002138 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002139 {
John Marriott5e6ea922024-11-23 14:01:57 +01002140 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 if (p != NULL)
2142 {
John Marriott5e6ea922024-11-23 14:01:57 +01002143 VIM_CLEAR_STRING(compl_first_match->cp_str);
2144 compl_first_match->cp_str.string = p;
2145 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002146 }
2147 }
2148 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002149 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002150 {
John Marriott5e6ea922024-11-23 14:01:57 +01002151 char_u *p = vim_strnsave(str, len);
2152 if (p != NULL)
2153 {
2154 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2155 compl_first_match->cp_prev->cp_str.string = p;
2156 compl_first_match->cp_prev->cp_str.length = len;
2157 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002158 }
2159}
2160
2161/*
2162 * Append one character to the match leader. May reduce the number of
2163 * matches.
2164 */
2165 void
2166ins_compl_addfrommatch(void)
2167{
2168 char_u *p;
2169 int len = (int)curwin->w_cursor.col - (int)compl_col;
2170 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002171
John Marriott5e6ea922024-11-23 14:01:57 +01002172 p = compl_shown_match->cp_str.string;
2173 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002174 {
John Marriott5e6ea922024-11-23 14:01:57 +01002175 size_t plen;
2176 compl_T *cp;
2177
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002178 // When still at the original match use the first entry that matches
2179 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002180 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002181 return;
2182
2183 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002184 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002185 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002186 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002187 {
John Marriott5e6ea922024-11-23 14:01:57 +01002188 if (compl_leader.string == NULL
2189 || ins_compl_equal(cp, compl_leader.string,
2190 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002191 {
John Marriott5e6ea922024-11-23 14:01:57 +01002192 p = cp->cp_str.string;
2193 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002194 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002195 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002196 }
John Marriott5e6ea922024-11-23 14:01:57 +01002197 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002198 return;
2199 }
2200 p += len;
2201 c = PTR2CHAR(p);
2202 ins_compl_addleader(c);
2203}
2204
2205/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002206 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002207 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2208 * compl_cont_mode and compl_cont_status.
2209 * Returns TRUE when the character is not to be inserted.
2210 */
2211 static int
2212set_ctrl_x_mode(int c)
2213{
2214 int retval = FALSE;
2215
2216 switch (c)
2217 {
2218 case Ctrl_E:
2219 case Ctrl_Y:
2220 // scroll the window one line up or down
2221 ctrl_x_mode = CTRL_X_SCROLL;
2222 if (!(State & REPLACE_FLAG))
2223 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2224 else
2225 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2226 edit_submode_pre = NULL;
2227 showmode();
2228 break;
2229 case Ctrl_L:
2230 // complete whole line
2231 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2232 break;
2233 case Ctrl_F:
2234 // complete filenames
2235 ctrl_x_mode = CTRL_X_FILES;
2236 break;
2237 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002238 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002239 ctrl_x_mode = CTRL_X_DICTIONARY;
2240 break;
2241 case Ctrl_R:
2242 // Register insertion without exiting CTRL-X mode
2243 // Simply allow ^R to happen without affecting ^X mode
2244 break;
2245 case Ctrl_T:
2246 // complete words from a thesaurus
2247 ctrl_x_mode = CTRL_X_THESAURUS;
2248 break;
2249#ifdef FEAT_COMPL_FUNC
2250 case Ctrl_U:
2251 // user defined completion
2252 ctrl_x_mode = CTRL_X_FUNCTION;
2253 break;
2254 case Ctrl_O:
2255 // omni completion
2256 ctrl_x_mode = CTRL_X_OMNI;
2257 break;
2258#endif
2259 case 's':
2260 case Ctrl_S:
2261 // complete spelling suggestions
2262 ctrl_x_mode = CTRL_X_SPELL;
2263#ifdef FEAT_SPELL
2264 ++emsg_off; // Avoid getting the E756 error twice.
2265 spell_back_to_badword();
2266 --emsg_off;
2267#endif
2268 break;
2269 case Ctrl_RSB:
2270 // complete tag names
2271 ctrl_x_mode = CTRL_X_TAGS;
2272 break;
2273#ifdef FEAT_FIND_ID
2274 case Ctrl_I:
2275 case K_S_TAB:
2276 // complete keywords from included files
2277 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2278 break;
2279 case Ctrl_D:
2280 // complete definitions from included files
2281 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2282 break;
2283#endif
2284 case Ctrl_V:
2285 case Ctrl_Q:
2286 // complete vim commands
2287 ctrl_x_mode = CTRL_X_CMDLINE;
2288 break;
2289 case Ctrl_Z:
2290 // stop completion
2291 ctrl_x_mode = CTRL_X_NORMAL;
2292 edit_submode = NULL;
2293 showmode();
2294 retval = TRUE;
2295 break;
2296 case Ctrl_P:
2297 case Ctrl_N:
2298 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2299 // just started ^X mode, or there were enough ^X's to cancel
2300 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2301 // do normal expansion when interrupting a different mode (say
2302 // ^X^F^X^P or ^P^X^X^P, see below)
2303 // nothing changes if interrupting mode 0, (eg, the flag
2304 // doesn't change when going to ADDING mode -- Acevedo
2305 if (!(compl_cont_status & CONT_INTRPT))
2306 compl_cont_status |= CONT_LOCAL;
2307 else if (compl_cont_mode != 0)
2308 compl_cont_status &= ~CONT_LOCAL;
2309 // FALLTHROUGH
2310 default:
2311 // If we have typed at least 2 ^X's... for modes != 0, we set
2312 // compl_cont_status = 0 (eg, as if we had just started ^X
2313 // mode).
2314 // For mode 0, we set "compl_cont_mode" to an impossible
2315 // value, in both cases ^X^X can be used to restart the same
2316 // mode (avoiding ADDING mode).
2317 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2318 // 'complete' and local ^P expansions respectively.
2319 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2320 // mode -- Acevedo
2321 if (c == Ctrl_X)
2322 {
2323 if (compl_cont_mode != 0)
2324 compl_cont_status = 0;
2325 else
2326 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2327 }
2328 ctrl_x_mode = CTRL_X_NORMAL;
2329 edit_submode = NULL;
2330 showmode();
2331 break;
2332 }
2333
2334 return retval;
2335}
2336
2337/*
glepnir1c5a1202024-12-04 20:27:34 +01002338 * Trigger CompleteDone event and adds relevant information to v:event
2339 */
2340 static void
2341trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2342{
2343#if defined(FEAT_EVAL)
2344 save_v_event_T save_v_event;
2345 dict_T *v_event = get_v_event(&save_v_event);
2346 char_u *mode_str = NULL;
2347
2348 mode = mode & ~CTRL_X_WANT_IDENT;
2349 if (ctrl_x_mode_names[mode])
2350 mode_str = (char_u *)ctrl_x_mode_names[mode];
2351
2352 (void)dict_add_string(v_event, "complete_word",
2353 word == NULL ? (char_u *)"" : word);
2354 (void)dict_add_string(v_event, "complete_type",
2355 mode_str != NULL ? mode_str : (char_u *)"");
2356
2357 dict_set_items_ro(v_event);
2358#endif
2359 ins_apply_autocmds(EVENT_COMPLETEDONE);
2360
2361#if defined(FEAT_EVAL)
2362 restore_v_event(v_event, &save_v_event);
2363#endif
2364}
2365
2366/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002367 * Stop insert completion mode
2368 */
2369 static int
2370ins_compl_stop(int c, int prev_mode, int retval)
2371{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002372 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002373 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002374
2375 // Get here when we have finished typing a sequence of ^N and
2376 // ^P or other completion characters in CTRL-X mode. Free up
2377 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002378 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002379 {
John Marriott5e6ea922024-11-23 14:01:57 +01002380 char_u *ptr;
2381
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002382 // If any of the original typed text has been changed, eg when
2383 // ignorecase is set, we must add back-spaces to the redo
2384 // buffer. We add as few as necessary to delete just the part
2385 // of the original text that has changed.
2386 // When using the longest match, edited the match or used
2387 // CTRL-E then don't use the current match.
2388 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002389 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002390 else
2391 ptr = NULL;
2392 ins_compl_fixRedoBufForLeader(ptr);
2393 }
2394
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002395 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002396
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002397 // When completing whole lines: fix indent for 'cindent'.
2398 // Otherwise, break line if it's too long.
2399 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2400 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002401 // re-indent the current line
2402 if (want_cindent)
2403 {
2404 do_c_expr_indent();
2405 want_cindent = FALSE; // don't do it again
2406 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002407 }
2408 else
2409 {
2410 int prev_col = curwin->w_cursor.col;
2411
2412 // put the cursor on the last char, for 'tw' formatting
2413 if (prev_col > 0)
2414 dec_cursor();
2415 // only format when something was inserted
2416 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2417 insertchar(NUL, 0, -1);
2418 if (prev_col > 0
2419 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2420 inc_cursor();
2421 }
2422
2423 // If the popup menu is displayed pressing CTRL-Y means accepting
2424 // the selection without inserting anything. When
2425 // compl_enter_selects is set the Enter key does the same.
2426 if ((c == Ctrl_Y || (compl_enter_selects
2427 && (c == CAR || c == K_KENTER || c == NL)))
2428 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002429 {
2430 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002431 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002432 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002433
2434 // CTRL-E means completion is Ended, go back to the typed text.
2435 // but only do this, if the Popup is still visible
2436 if (c == Ctrl_E)
2437 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002438 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002439 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002440
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002441 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002442 if (compl_leader.string != NULL)
2443 {
2444 p = compl_leader.string;
2445 plen = compl_leader.length;
2446 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002447 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002448 {
2449 p = compl_orig_text.string;
2450 plen = compl_orig_text.length;
2451 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002452 if (p != NULL)
2453 {
2454 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002455
John Marriott5e6ea922024-11-23 14:01:57 +01002456 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002457 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002458 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002459 retval = TRUE;
2460 }
2461
2462 auto_format(FALSE, TRUE);
2463
2464 // Trigger the CompleteDonePre event to give scripts a chance to
2465 // act upon the completion before clearing the info, and restore
2466 // ctrl_x_mode, so that complete_info() can be used.
2467 ctrl_x_mode = prev_mode;
2468 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2469
2470 ins_compl_free();
2471 compl_started = FALSE;
2472 compl_matches = 0;
2473 if (!shortmess(SHM_COMPLETIONMENU))
2474 msg_clr_cmdline(); // necessary for "noshowmode"
2475 ctrl_x_mode = CTRL_X_NORMAL;
2476 compl_enter_selects = FALSE;
2477 if (edit_submode != NULL)
2478 {
2479 edit_submode = NULL;
2480 showmode();
2481 }
2482
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002483 if (c == Ctrl_C && cmdwin_type != 0)
2484 // Avoid the popup menu remains displayed when leaving the
2485 // command line window.
2486 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002487 // Indent now if a key was typed that is in 'cinkeys'.
2488 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2489 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002490 // Trigger the CompleteDone event to give scripts a chance to act
2491 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002492 trigger_complete_done_event(prev_mode, word);
2493 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002494
2495 return retval;
2496}
2497
2498/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002499 * Prepare for Insert mode completion, or stop it.
2500 * Called just after typing a character in Insert mode.
2501 * Returns TRUE when the character is not to be inserted;
2502 */
2503 int
2504ins_compl_prep(int c)
2505{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002506 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002507 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002508
2509 // Forget any previous 'special' messages if this is actually
2510 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2511 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2512 edit_submode_extra = NULL;
2513
zeertzjq440d4cb2023-03-02 17:51:32 +00002514 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002515 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002516 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002517 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002518 return retval;
2519
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002520#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002521 // Ignore mouse events in a popup window
2522 if (is_mouse_key(c))
2523 {
2524 // Ignore drag and release events, the position does not need to be in
2525 // the popup and it may have just closed.
2526 if (c == K_LEFTRELEASE
2527 || c == K_LEFTRELEASE_NM
2528 || c == K_MIDDLERELEASE
2529 || c == K_RIGHTRELEASE
2530 || c == K_X1RELEASE
2531 || c == K_X2RELEASE
2532 || c == K_LEFTDRAG
2533 || c == K_MIDDLEDRAG
2534 || c == K_RIGHTDRAG
2535 || c == K_X1DRAG
2536 || c == K_X2DRAG)
2537 return retval;
2538 if (popup_visible)
2539 {
2540 int row = mouse_row;
2541 int col = mouse_col;
2542 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2543
2544 if (wp != NULL && WIN_IS_POPUP(wp))
2545 return retval;
2546 }
2547 }
2548#endif
2549
zeertzjqdca29d92021-08-31 19:12:51 +02002550 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2551 {
2552 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2553 || !vim_is_ctrl_x_key(c))
2554 {
2555 // Not starting another completion mode.
2556 ctrl_x_mode = CTRL_X_CMDLINE;
2557
2558 // CTRL-X CTRL-Z should stop completion without inserting anything
2559 if (c == Ctrl_Z)
2560 retval = TRUE;
2561 }
2562 else
2563 {
2564 ctrl_x_mode = CTRL_X_CMDLINE;
2565
2566 // Other CTRL-X keys first stop completion, then start another
2567 // completion mode.
2568 ins_compl_prep(' ');
2569 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2570 }
2571 }
2572
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002573 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002574 if (ctrl_x_mode_not_defined_yet()
2575 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002576 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002577 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002578 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002579 }
2580
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002581 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2583 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002584 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002585 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002586 {
2587 // We're already in CTRL-X mode, do we stay in it?
2588 if (!vim_is_ctrl_x_key(c))
2589 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002590 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002591 ctrl_x_mode = CTRL_X_NORMAL;
2592 else
2593 ctrl_x_mode = CTRL_X_FINISHED;
2594 edit_submode = NULL;
2595 }
2596 showmode();
2597 }
2598
2599 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2600 {
2601 // Show error message from attempted keyword completion (probably
2602 // 'Pattern not found') until another key is hit, then go back to
2603 // showing what mode we are in.
2604 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002605 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002606 && c != Ctrl_R && !ins_compl_pum_key(c))
2607 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002608 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002609 }
2610 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2611 // Trigger the CompleteDone event to give scripts a chance to act
2612 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002613 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002614
LemonBoy2bf52dd2022-04-09 18:17:34 +01002615 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002616
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002617 // reset continue_* if we left expansion-mode, if we stay they'll be
2618 // (re)set properly in ins_complete()
2619 if (!vim_is_ctrl_x_key(c))
2620 {
2621 compl_cont_status = 0;
2622 compl_cont_mode = 0;
2623 }
2624
2625 return retval;
2626}
2627
2628/*
2629 * Fix the redo buffer for the completion leader replacing some of the typed
2630 * text. This inserts backspaces and appends the changed text.
2631 * "ptr" is the known leader text or NUL.
2632 */
2633 static void
2634ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2635{
John Marriott5e6ea922024-11-23 14:01:57 +01002636 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002637 char_u *p;
2638 char_u *ptr = ptr_arg;
2639
2640 if (ptr == NULL)
2641 {
John Marriott5e6ea922024-11-23 14:01:57 +01002642 if (compl_leader.string != NULL)
2643 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002644 else
2645 return; // nothing to do
2646 }
John Marriott5e6ea922024-11-23 14:01:57 +01002647 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002648 {
John Marriott5e6ea922024-11-23 14:01:57 +01002649 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002650 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2651 ;
2652 if (len > 0)
2653 len -= (*mb_head_off)(p, p + len);
2654 for (p += len; *p != NUL; MB_PTR_ADV(p))
2655 AppendCharToRedobuff(K_BS);
2656 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002657 if (ptr != NULL)
2658 AppendToRedobuffLit(ptr + len, -1);
2659}
2660
2661/*
2662 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2663 * (depending on flag) starting from buf and looking for a non-scanned
2664 * buffer (other than curbuf). curbuf is special, if it is called with
2665 * buf=curbuf then it has to be the first call for a given flag/expansion.
2666 *
2667 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2668 */
2669 static buf_T *
2670ins_compl_next_buf(buf_T *buf, int flag)
2671{
2672 static win_T *wp = NULL;
2673
2674 if (flag == 'w') // just windows
2675 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002676 if (buf == curbuf || !win_valid(wp))
2677 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002678 wp = curwin;
2679 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2680 && wp->w_buffer->b_scanned)
2681 ;
2682 buf = wp->w_buffer;
2683 }
2684 else
2685 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2686 // (unlisted buffers)
2687 // When completing whole lines skip unloaded buffers.
2688 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2689 && ((flag == 'U'
2690 ? buf->b_p_bl
2691 : (!buf->b_p_bl
2692 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2693 || buf->b_scanned))
2694 ;
2695 return buf;
2696}
2697
2698#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002699
2700# ifdef FEAT_EVAL
2701static callback_T cfu_cb; // 'completefunc' callback function
2702static callback_T ofu_cb; // 'omnifunc' callback function
2703static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2704# endif
2705
2706/*
2707 * Copy a global callback function to a buffer local callback.
2708 */
2709 static void
2710copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2711{
2712 free_callback(bufcb);
2713 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2714 copy_callback(bufcb, globcb);
2715}
2716
2717/*
2718 * Parse the 'completefunc' option value and set the callback function.
2719 * Invoked when the 'completefunc' option is set. The option value can be a
2720 * name of a function (string), or function(<name>) or funcref(<name>) or a
2721 * lambda expression.
2722 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002723 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002724did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002725{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002726 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2727 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002728
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002729 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002730
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002731 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002732}
2733
2734/*
2735 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002736 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002737 */
2738 void
2739set_buflocal_cfu_callback(buf_T *buf UNUSED)
2740{
2741# ifdef FEAT_EVAL
2742 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2743# endif
2744}
2745
2746/*
2747 * Parse the 'omnifunc' option value and set the callback function.
2748 * Invoked when the 'omnifunc' option is set. The option value can be a
2749 * name of a function (string), or function(<name>) or funcref(<name>) or a
2750 * lambda expression.
2751 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002752 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002753did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002754{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002755 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2756 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002757
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002758 set_buflocal_ofu_callback(curbuf);
2759 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002760}
2761
2762/*
2763 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002764 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002765 */
2766 void
2767set_buflocal_ofu_callback(buf_T *buf UNUSED)
2768{
2769# ifdef FEAT_EVAL
2770 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2771# endif
2772}
2773
2774/*
2775 * Parse the 'thesaurusfunc' option value and set the callback function.
2776 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2777 * name of a function (string), or function(<name>) or funcref(<name>) or a
2778 * lambda expression.
2779 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002780 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002781did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002782{
2783 int retval;
2784
zeertzjq6eda2692024-11-03 09:23:33 +01002785 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002786 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002787 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2788 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002789 else
2790 {
2791 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002792 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002793 // when using :set, free the local callback
2794 if (!(args->os_flags & OPT_GLOBAL))
2795 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002796 }
2797
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002798 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002799}
2800
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002801/*
2802 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002803 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002804 */
2805 int
2806set_ref_in_insexpand_funcs(int copyID)
2807{
2808 int abort = FALSE;
2809
2810 abort = set_ref_in_callback(&cfu_cb, copyID);
2811 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2812 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2813
2814 return abort;
2815}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002816
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002817/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002818 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002819 */
2820 static char_u *
2821get_complete_funcname(int type)
2822{
2823 switch (type)
2824 {
2825 case CTRL_X_FUNCTION:
2826 return curbuf->b_p_cfu;
2827 case CTRL_X_OMNI:
2828 return curbuf->b_p_ofu;
2829 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002830 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002831 default:
2832 return (char_u *)"";
2833 }
2834}
2835
2836/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002837 * Get the callback to use for insert mode completion.
2838 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002839 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002840get_insert_callback(int type)
2841{
2842 if (type == CTRL_X_FUNCTION)
2843 return &curbuf->b_cfu_cb;
2844 if (type == CTRL_X_OMNI)
2845 return &curbuf->b_ofu_cb;
2846 // CTRL_X_THESAURUS
2847 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2848}
2849
2850/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002851 * Execute user defined complete function 'completefunc', 'omnifunc' or
2852 * 'thesaurusfunc', and get matches in "matches".
2853 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002854 */
2855 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002856expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002857{
2858 list_T *matchlist = NULL;
2859 dict_T *matchdict = NULL;
2860 typval_T args[3];
2861 char_u *funcname;
2862 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002863 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002864 typval_T rettv;
2865 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002866 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002867
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002868 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002869 if (*funcname == NUL)
2870 return;
2871
2872 // Call 'completefunc' to obtain the list of matches.
2873 args[0].v_type = VAR_NUMBER;
2874 args[0].vval.v_number = 0;
2875 args[1].v_type = VAR_STRING;
2876 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2877 args[2].v_type = VAR_UNKNOWN;
2878
2879 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002880 // Lock the text to avoid weird things from happening. Also disallow
2881 // switching to another window, it should not be needed and may end up in
2882 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002883 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002884
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002885 cb = get_insert_callback(type);
2886 retval = call_callback(cb, 0, &rettv, 2, args);
2887
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002888 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002889 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002890 {
2891 switch (rettv.v_type)
2892 {
2893 case VAR_LIST:
2894 matchlist = rettv.vval.v_list;
2895 break;
2896 case VAR_DICT:
2897 matchdict = rettv.vval.v_dict;
2898 break;
2899 case VAR_SPECIAL:
2900 if (rettv.vval.v_number == VVAL_NONE)
2901 compl_opt_suppress_empty = TRUE;
2902 // FALLTHROUGH
2903 default:
2904 // TODO: Give error message?
2905 clear_tv(&rettv);
2906 break;
2907 }
2908 }
zeertzjqcfe45652022-05-27 17:26:55 +01002909 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002910
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002911 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002912 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002913 validate_cursor();
2914 if (!EQUAL_POS(curwin->w_cursor, pos))
2915 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002916 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002917 goto theend;
2918 }
2919
2920 if (matchlist != NULL)
2921 ins_compl_add_list(matchlist);
2922 else if (matchdict != NULL)
2923 ins_compl_add_dict(matchdict);
2924
2925theend:
2926 // Restore State, it might have been changed.
2927 State = save_State;
2928
2929 if (matchdict != NULL)
2930 dict_unref(matchdict);
2931 if (matchlist != NULL)
2932 list_unref(matchlist);
2933}
2934#endif // FEAT_COMPL_FUNC
2935
2936#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002937
2938 static inline int
2939get_user_highlight_attr(char_u *hlname)
2940{
2941 if (hlname != NULL && *hlname != NUL)
2942 return syn_name2attr(hlname);
2943 return -1;
2944}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002945/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002946 * Add a match to the list of matches from a typeval_T.
2947 * If the given string is already in the list of completions, then return
2948 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2949 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002950 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002951 */
2952 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002953ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002954{
2955 char_u *word;
2956 int dup = FALSE;
2957 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002958 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002959 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002960 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002961 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002962 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002963 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002964 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002965
Bram Moolenaar08928322020-01-04 14:32:48 +01002966 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002967 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2968 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002969 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2970 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2971 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2972 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2973 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002974
glepnir0fe17f82024-10-08 22:26:44 +02002975 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002976 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002977
2978 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002979 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002980
Bram Moolenaard61efa52022-07-23 09:52:04 +01002981 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2982 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2983 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002984 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002985 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2986 dup = dict_get_number(tv->vval.v_dict, "dup");
2987 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2988 empty = dict_get_number(tv->vval.v_dict, "empty");
2989 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2990 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002991 flags |= CP_EQUAL;
2992 }
2993 else
2994 {
2995 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002996 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997 }
2998 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002999 {
3000 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003001 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003002 }
glepnir508e7852024-07-25 21:39:08 +02003003 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003004 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003005 if (status != OK)
3006 clear_tv(&user_data);
3007 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003008}
3009
3010/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003011 * Add completions from a list.
3012 */
3013 static void
3014ins_compl_add_list(list_T *list)
3015{
3016 listitem_T *li;
3017 int dir = compl_direction;
3018
3019 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003020 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003021 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003022 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003023 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003024 // if dir was BACKWARD then honor it just once
3025 dir = FORWARD;
3026 else if (did_emsg)
3027 break;
3028 }
3029}
3030
3031/*
3032 * Add completions from a dict.
3033 */
3034 static void
3035ins_compl_add_dict(dict_T *dict)
3036{
3037 dictitem_T *di_refresh;
3038 dictitem_T *di_words;
3039
3040 // Check for optional "refresh" item.
3041 compl_opt_refresh_always = FALSE;
3042 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3043 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3044 {
3045 char_u *v = di_refresh->di_tv.vval.v_string;
3046
3047 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3048 compl_opt_refresh_always = TRUE;
3049 }
3050
3051 // Add completions from a "words" list.
3052 di_words = dict_find(dict, (char_u *)"words", 5);
3053 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3054 ins_compl_add_list(di_words->di_tv.vval.v_list);
3055}
3056
3057/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003058 * Start completion for the complete() function.
3059 * "startcol" is where the matched text starts (1 is first column).
3060 * "list" is the list of matches.
3061 */
3062 static void
3063set_completion(colnr_T startcol, list_T *list)
3064{
3065 int save_w_wrow = curwin->w_wrow;
3066 int save_w_leftcol = curwin->w_leftcol;
3067 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003068 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003069 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3070 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3071 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003072
3073 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003074 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003075 ins_compl_prep(' ');
3076 ins_compl_clear();
3077 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003078 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003079
3080 compl_direction = FORWARD;
3081 if (startcol > curwin->w_cursor.col)
3082 startcol = curwin->w_cursor.col;
3083 compl_col = startcol;
3084 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3085 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003086 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003087 if (p_ic)
3088 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003089 if (compl_orig_text.string == NULL)
3090 {
3091 compl_orig_text.length = 0;
3092 return;
3093 }
3094 compl_orig_text.length = (size_t)compl_length;
3095 if (ins_compl_add(compl_orig_text.string,
3096 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3097 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003098 return;
3099
3100 ctrl_x_mode = CTRL_X_EVAL;
3101
3102 ins_compl_add_list(list);
3103 compl_matches = ins_compl_make_cyclic();
3104 compl_started = TRUE;
3105 compl_used_match = TRUE;
3106 compl_cont_status = 0;
3107
3108 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003109 int no_select = compl_no_select || compl_longest;
3110 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003111 {
3112 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003113 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003114 // Down/Up has no real effect.
3115 ins_complete(K_UP, FALSE);
3116 }
3117 else
3118 ins_complete(Ctrl_N, FALSE);
3119 compl_enter_selects = compl_no_insert;
3120
3121 // Lazily show the popup menu, unless we got interrupted.
3122 if (!compl_interrupted)
3123 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003124 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003125 out_flush();
3126}
3127
3128/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003129 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003130 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003131 void
3132f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003133{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003134 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003135
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003136 if (in_vim9script()
3137 && (check_for_number_arg(argvars, 0) == FAIL
3138 || check_for_list_arg(argvars, 1) == FAIL))
3139 return;
3140
Bram Moolenaar24959102022-05-07 20:01:16 +01003141 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003142 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003143 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003144 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003145 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003146
3147 // Check for undo allowed here, because if something was already inserted
3148 // the line was already saved for undo and this check isn't done.
3149 if (!undo_allowed())
3150 return;
3151
Bram Moolenaard83392a2022-09-01 12:22:46 +01003152 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003153 {
3154 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3155 if (startcol > 0)
3156 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003157 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003158}
3159
3160/*
3161 * "complete_add()" function
3162 */
3163 void
3164f_complete_add(typval_T *argvars, typval_T *rettv)
3165{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003166 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003167 return;
3168
Bram Moolenaar440cf092021-04-03 20:13:30 +02003169 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003170}
3171
3172/*
3173 * "complete_check()" function
3174 */
3175 void
3176f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3177{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003178 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003179 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003180
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003181 ins_compl_check_keys(0, TRUE);
3182 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003183
3184 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003185}
3186
3187/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003188 * Return Insert completion mode name string
3189 */
3190 static char_u *
3191ins_compl_mode(void)
3192{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003193 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003194 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3195
3196 return (char_u *)"";
3197}
3198
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003199/*
3200 * Assign the sequence number to all the completion matches which don't have
3201 * one assigned yet.
3202 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003203 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003204ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003205{
3206 int number = 0;
3207 compl_T *match;
3208
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003209 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003210 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003211 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003212 // This should normally succeed already at the first loop
3213 // cycle, so it's fast!
3214 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003215 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003216 if (match->cp_number != -1)
3217 {
3218 number = match->cp_number;
3219 break;
3220 }
3221 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003222 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003223 for (match = match->cp_next;
3224 match != NULL && match->cp_number == -1;
3225 match = match->cp_next)
3226 match->cp_number = ++number;
3227 }
3228 else // BACKWARD
3229 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003230 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003231 // number. This should normally succeed already at the
3232 // first loop cycle, so it's fast!
3233 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003234 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003235 if (match->cp_number != -1)
3236 {
3237 number = match->cp_number;
3238 break;
3239 }
3240 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003241 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003242 for (match = match->cp_prev; match
3243 && match->cp_number == -1;
3244 match = match->cp_prev)
3245 match->cp_number = ++number;
3246 }
3247}
3248
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003249/*
3250 * Get complete information
3251 */
3252 static void
3253get_complete_info(list_T *what_list, dict_T *retdict)
3254{
3255 int ret = OK;
3256 listitem_T *item;
3257#define CI_WHAT_MODE 0x01
3258#define CI_WHAT_PUM_VISIBLE 0x02
3259#define CI_WHAT_ITEMS 0x04
3260#define CI_WHAT_SELECTED 0x08
3261#define CI_WHAT_INSERTED 0x10
3262#define CI_WHAT_ALL 0xff
3263 int what_flag;
3264
3265 if (what_list == NULL)
3266 what_flag = CI_WHAT_ALL;
3267 else
3268 {
3269 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003270 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003271 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003272 {
3273 char_u *what = tv_get_string(&item->li_tv);
3274
3275 if (STRCMP(what, "mode") == 0)
3276 what_flag |= CI_WHAT_MODE;
3277 else if (STRCMP(what, "pum_visible") == 0)
3278 what_flag |= CI_WHAT_PUM_VISIBLE;
3279 else if (STRCMP(what, "items") == 0)
3280 what_flag |= CI_WHAT_ITEMS;
3281 else if (STRCMP(what, "selected") == 0)
3282 what_flag |= CI_WHAT_SELECTED;
3283 else if (STRCMP(what, "inserted") == 0)
3284 what_flag |= CI_WHAT_INSERTED;
3285 }
3286 }
3287
3288 if (ret == OK && (what_flag & CI_WHAT_MODE))
3289 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3290
3291 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3292 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3293
Girish Palya8950bf72024-03-20 20:07:29 +01003294 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003295 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003296 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003297 dict_T *di;
3298 compl_T *match;
3299 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003300
Girish Palya8950bf72024-03-20 20:07:29 +01003301 if (what_flag & CI_WHAT_ITEMS)
3302 {
3303 li = list_alloc();
3304 if (li == NULL)
3305 return;
3306 ret = dict_add_list(retdict, "items", li);
3307 }
3308 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3309 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3310 ins_compl_update_sequence_numbers();
3311 if (ret == OK && compl_first_match != NULL)
3312 {
3313 int list_idx = 0;
3314 match = compl_first_match;
3315 do
3316 {
3317 if (!match_at_original_text(match))
3318 {
3319 if (what_flag & CI_WHAT_ITEMS)
3320 {
3321 di = dict_alloc();
3322 if (di == NULL)
3323 return;
3324 ret = list_append_dict(li, di);
3325 if (ret != OK)
3326 return;
John Marriott5e6ea922024-11-23 14:01:57 +01003327 dict_add_string(di, "word", match->cp_str.string);
Girish Palya8950bf72024-03-20 20:07:29 +01003328 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3329 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3330 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3331 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3332 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3333 // Add an empty string for backwards compatibility
3334 dict_add_string(di, "user_data", (char_u *)"");
3335 else
3336 dict_add_tv(di, "user_data", &match->cp_user_data);
3337 }
3338 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3339 selected_idx = list_idx;
3340 list_idx += 1;
3341 }
3342 match = match->cp_next;
3343 }
3344 while (match != NULL && !is_first_match(match));
3345 }
3346 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3347 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003348 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003349
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003350 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3351 {
3352 // TODO
3353 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003354}
3355
3356/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003357 * "complete_info()" function
3358 */
3359 void
3360f_complete_info(typval_T *argvars, typval_T *rettv)
3361{
3362 list_T *what_list = NULL;
3363
Bram Moolenaar93a10962022-06-16 11:42:09 +01003364 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003365 return;
3366
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003367 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3368 return;
3369
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003370 if (argvars[0].v_type != VAR_UNKNOWN)
3371 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003372 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003373 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003374 what_list = argvars[0].vval.v_list;
3375 }
3376 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003377}
3378#endif
3379
3380/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003381 * Returns TRUE when using a user-defined function for thesaurus completion.
3382 */
3383 static int
3384thesaurus_func_complete(int type UNUSED)
3385{
3386#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003387 return type == CTRL_X_THESAURUS
3388 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003389#else
3390 return FALSE;
3391#endif
3392}
3393
3394/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003395 * Return value of process_next_cpt_value()
3396 */
3397enum
3398{
3399 INS_COMPL_CPT_OK = 1,
3400 INS_COMPL_CPT_CONT,
3401 INS_COMPL_CPT_END
3402};
3403
3404/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003405 * state information used for getting the next set of insert completion
3406 * matches.
3407 */
3408typedef struct
3409{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003410 char_u *e_cpt_copy; // copy of 'complete'
3411 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003412 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003413 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003414 pos_T prev_match_pos; // previous match position
3415 int set_match_pos; // save first_match_pos/last_match_pos
3416 pos_T first_match_pos; // first match position
3417 pos_T last_match_pos; // last match position
3418 int found_all; // found all matches of a certain type.
3419 char_u *dict; // dictionary file to search
3420 int dict_f; // "dict" is an exact file name or not
3421} ins_compl_next_state_T;
3422
3423/*
3424 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003425 *
3426 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003427 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003428 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003429 * st->found_all - all matches of this type are found
3430 * st->ins_buf - search for completions in this buffer
3431 * st->first_match_pos - position of the first completion match
3432 * st->last_match_pos - position of the last completion match
3433 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003434 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003435 * st->dict - name of the dictionary or thesaurus file to search
3436 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003437 *
3438 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003439 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3440 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003441 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442 */
3443 static int
3444process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003445 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003446 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003447 pos_T *start_match_pos,
3448 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003449{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003450 int compl_type = -1;
3451 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003452
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003453 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003454
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003455 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3456 st->e_cpt++;
3457
3458 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003460 st->ins_buf = curbuf;
3461 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003462 // Move the cursor back one character so that ^N can match the
3463 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003464 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003465 {
3466 // Move the cursor to after the last character in the
3467 // buffer, so that word at start of buffer is found
3468 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003469 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003470 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003472 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003473 compl_type = 0;
3474
3475 // Remember the first match so that the loop stops when we
3476 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003477 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003478 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003479 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003480 && (st->ins_buf = ins_compl_next_buf(
3481 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003482 {
3483 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003484 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485 {
3486 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003487 st->first_match_pos.col = st->last_match_pos.col = 0;
3488 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3489 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003490 compl_type = 0;
3491 }
3492 else // unloaded buffer, scan like dictionary
3493 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003494 st->found_all = TRUE;
3495 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003496 {
3497 status = INS_COMPL_CPT_CONT;
3498 goto done;
3499 }
3500 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003501 st->dict = st->ins_buf->b_fname;
3502 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003503 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003504 if (!shortmess(SHM_COMPLETIONSCAN))
3505 {
3506 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3507 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3508 st->ins_buf->b_fname == NULL
3509 ? buf_spname(st->ins_buf)
3510 : st->ins_buf->b_sfname == NULL
3511 ? st->ins_buf->b_fname
3512 : st->ins_buf->b_sfname);
3513 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3514 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003515 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003516 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003517 status = INS_COMPL_CPT_END;
3518 else
3519 {
3520 if (ctrl_x_mode_line_or_eval())
3521 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003522 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003523 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003524 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003525 compl_type = CTRL_X_DICTIONARY;
3526 else
3527 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003528 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003529 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003530 st->dict = st->e_cpt;
3531 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003532 }
3533 }
3534#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003535 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003536 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003537 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003538 compl_type = CTRL_X_PATH_DEFINES;
3539#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003540 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003541 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003542 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003543 if (!shortmess(SHM_COMPLETIONSCAN))
3544 {
3545 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3546 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3547 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3548 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003549 }
3550 else
3551 compl_type = -1;
3552
3553 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003554 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003555
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003556 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003557 if (compl_type == -1)
3558 status = INS_COMPL_CPT_CONT;
3559 }
3560
3561done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003562 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 return status;
3564}
3565
3566#ifdef FEAT_FIND_ID
3567/*
3568 * Get the next set of identifiers or defines matching "compl_pattern" in
3569 * included files.
3570 */
3571 static void
3572get_next_include_file_completion(int compl_type)
3573{
John Marriott5e6ea922024-11-23 14:01:57 +01003574 find_pattern_in_path(compl_pattern.string, compl_direction,
3575 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576 (compl_type == CTRL_X_PATH_DEFINES
3577 && !(compl_cont_status & CONT_SOL))
3578 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003579 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003580}
3581#endif
3582
3583/*
3584 * Get the next set of words matching "compl_pattern" in dictionary or
3585 * thesaurus files.
3586 */
3587 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003588get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003589{
3590#ifdef FEAT_COMPL_FUNC
3591 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003592 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 else
3594#endif
3595 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003596 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003597 : (compl_type == CTRL_X_THESAURUS
3598 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3599 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003600 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003601 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003602 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003603}
3604
3605/*
3606 * Get the next set of tag names matching "compl_pattern".
3607 */
3608 static void
3609get_next_tag_completion(void)
3610{
3611 int save_p_ic;
3612 char_u **matches;
3613 int num_matches;
3614
3615 // set p_ic according to p_ic, p_scs and pat for find_tags().
3616 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003617 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618
3619 // Find up to TAG_MANY matches. Avoids that an enormous number
3620 // of matches is found when compl_pattern is empty
3621 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003622 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003623 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003624 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003625 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3626 ins_compl_add_matches(num_matches, matches, p_ic);
3627 g_tag_at_cursor = FALSE;
3628 p_ic = save_p_ic;
3629}
3630
3631/*
glepnir8159fb12024-07-17 20:32:54 +02003632 * Compare function for qsort
3633 */
3634static int compare_scores(const void *a, const void *b)
3635{
3636 int idx_a = *(const int *)a;
3637 int idx_b = *(const int *)b;
3638 int score_a = compl_fuzzy_scores[idx_a];
3639 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003640 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3641 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003642}
3643
3644/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003645 * Get the next set of filename matching "compl_pattern".
3646 */
3647 static void
3648get_next_filename_completion(void)
3649{
3650 char_u **matches;
3651 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003652 char_u *ptr;
3653 garray_T fuzzy_indices;
3654 int i;
3655 int score;
3656 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003657 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003658 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3659 char_u **sorted_matches;
3660 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003661 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003662
glepnirb9de1a02024-08-02 19:14:38 +02003663#ifdef BACKSLASH_IN_FILENAME
3664 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3665 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3666#else
3667 char pathsep = PATHSEP;
3668#endif
3669
glepnir8159fb12024-07-17 20:32:54 +02003670 if (in_fuzzy)
3671 {
glepnirb9de1a02024-08-02 19:14:38 +02003672#ifdef BACKSLASH_IN_FILENAME
3673 if (curbuf->b_p_csl[0] == 's')
3674 {
John Marriott5e6ea922024-11-23 14:01:57 +01003675 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003676 {
3677 if (leader[i] == '\\')
3678 leader[i] = '/';
3679 }
3680 }
3681 else if (curbuf->b_p_csl[0] == 'b')
3682 {
John Marriott5e6ea922024-11-23 14:01:57 +01003683 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003684 {
3685 if (leader[i] == '/')
3686 leader[i] = '\\';
3687 }
3688 }
3689#endif
3690 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003691 if (last_sep == NULL)
3692 {
3693 // No path separator or separator is the last character,
3694 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003695 VIM_CLEAR_STRING(compl_pattern);
3696 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3697 if (compl_pattern.string == NULL)
3698 return;
3699 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003700 }
3701 else if (*(last_sep + 1) == '\0')
3702 in_fuzzy = FALSE;
3703 else
3704 {
3705 // Split leader into path and file parts
3706 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003707 char_u *path_with_wildcard;
3708
3709 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003710 if (path_with_wildcard != NULL)
3711 {
John Marriott5e6ea922024-11-23 14:01:57 +01003712 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3713 VIM_CLEAR_STRING(compl_pattern);
3714 compl_pattern.string = path_with_wildcard;
3715 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003716
3717 // Move leader to the file part
3718 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003719 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003720 }
3721 }
glepnir8159fb12024-07-17 20:32:54 +02003722 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003723
John Marriott5e6ea922024-11-23 14:01:57 +01003724 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003725 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3726 return;
3727
3728 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003729 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003730#ifdef BACKSLASH_IN_FILENAME
3731 if (curbuf->b_p_csl[0] != NUL)
3732 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003733 for (i = 0; i < num_matches; ++i)
3734 {
glepnir8159fb12024-07-17 20:32:54 +02003735 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003736 while (*ptr != NUL)
3737 {
3738 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3739 *ptr = '/';
3740 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3741 *ptr = '\\';
3742 ptr += (*mb_ptr2len)(ptr);
3743 }
3744 }
3745 }
3746#endif
glepnir8159fb12024-07-17 20:32:54 +02003747
3748 if (in_fuzzy)
3749 {
3750 ga_init2(&fuzzy_indices, sizeof(int), 10);
3751 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3752
3753 for (i = 0; i < num_matches; i++)
3754 {
3755 ptr = matches[i];
3756 score = fuzzy_match_str(ptr, leader);
3757 if (score > 0)
3758 {
3759 if (ga_grow(&fuzzy_indices, 1) == OK)
3760 {
3761 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3762 compl_fuzzy_scores[i] = score;
3763 fuzzy_indices.ga_len++;
3764 }
3765 }
3766 }
3767
Christian Brabandt220474d2024-07-20 13:26:44 +02003768 // prevent qsort from deref NULL pointer
3769 if (fuzzy_indices.ga_len > 0)
3770 {
3771 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3772 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003773
Christian Brabandt220474d2024-07-20 13:26:44 +02003774 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3775 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3776 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003777
Christian Brabandt220474d2024-07-20 13:26:44 +02003778 FreeWild(num_matches, matches);
3779 matches = sorted_matches;
3780 num_matches = fuzzy_indices.ga_len;
3781 }
glepnir6b6280c2024-07-27 16:25:45 +02003782 else if (leader_len > 0)
3783 {
3784 FreeWild(num_matches, matches);
3785 num_matches = 0;
3786 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003787
glepnir8159fb12024-07-17 20:32:54 +02003788 vim_free(compl_fuzzy_scores);
3789 ga_clear(&fuzzy_indices);
3790 }
3791
glepnir6b6280c2024-07-27 16:25:45 +02003792 if (num_matches > 0)
3793 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003794}
3795
3796/*
3797 * Get the next set of command-line completions matching "compl_pattern".
3798 */
3799 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003800get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003801{
3802 char_u **matches;
3803 int num_matches;
3804
John Marriott5e6ea922024-11-23 14:01:57 +01003805 if (expand_cmdline(&compl_xp, compl_pattern.string,
3806 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003807 ins_compl_add_matches(num_matches, matches, FALSE);
3808}
3809
3810/*
3811 * Get the next set of spell suggestions matching "compl_pattern".
3812 */
3813 static void
3814get_next_spell_completion(linenr_T lnum UNUSED)
3815{
3816#ifdef FEAT_SPELL
3817 char_u **matches;
3818 int num_matches;
3819
John Marriott5e6ea922024-11-23 14:01:57 +01003820 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003821 if (num_matches > 0)
3822 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003823 else
3824 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003825#endif
3826}
3827
3828/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003829 * Return the next word or line from buffer "ins_buf" at position
3830 * "cur_match_pos" for completion. The length of the match is set in "len".
3831 */
3832 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003833ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003834 buf_T *ins_buf, // buffer being scanned
3835 pos_T *cur_match_pos, // current match position
3836 int *match_len,
3837 int *cont_s_ipos) // next ^X<> will set initial_pos
3838{
3839 char_u *ptr;
3840 int len;
3841
3842 *match_len = 0;
3843 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3844 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003845 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003846 if (ctrl_x_mode_line_or_eval())
3847 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003848 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003849 {
3850 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3851 return NULL;
3852 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003853 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003854 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003855 {
3856 char_u *tmp_ptr = ptr;
3857
3858 ptr = skipwhite(tmp_ptr);
3859 len -= (int)(ptr - tmp_ptr);
3860 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003861 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003862 }
3863 else
3864 {
3865 char_u *tmp_ptr = ptr;
3866
John Marriott5e6ea922024-11-23 14:01:57 +01003867 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003868 {
3869 tmp_ptr += compl_length;
3870 // Skip if already inside a word.
3871 if (vim_iswordp(tmp_ptr))
3872 return NULL;
3873 // Find start of next word.
3874 tmp_ptr = find_word_start(tmp_ptr);
3875 }
3876 // Find end of this word.
3877 tmp_ptr = find_word_end(tmp_ptr);
3878 len = (int)(tmp_ptr - ptr);
3879
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003880 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003881 {
3882 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3883 {
3884 // Try next line, if any. the new word will be
3885 // "join" as if the normal command "J" was used.
3886 // IOSIZE is always greater than
3887 // compl_length, so the next STRNCPY always
3888 // works -- Acevedo
3889 STRNCPY(IObuff, ptr, len);
3890 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3891 tmp_ptr = ptr = skipwhite(ptr);
3892 // Find start of next word.
3893 tmp_ptr = find_word_start(tmp_ptr);
3894 // Find end of next word.
3895 tmp_ptr = find_word_end(tmp_ptr);
3896 if (tmp_ptr > ptr)
3897 {
3898 if (*ptr != ')' && IObuff[len - 1] != TAB)
3899 {
3900 if (IObuff[len - 1] != ' ')
3901 IObuff[len++] = ' ';
3902 // IObuf =~ "\k.* ", thus len >= 2
3903 if (p_js
3904 && (IObuff[len - 2] == '.'
3905 || (vim_strchr(p_cpo, CPO_JOINSP)
3906 == NULL
3907 && (IObuff[len - 2] == '?'
3908 || IObuff[len - 2] == '!'))))
3909 IObuff[len++] = ' ';
3910 }
3911 // copy as much as possible of the new word
3912 if (tmp_ptr - ptr >= IOSIZE - len)
3913 tmp_ptr = ptr + IOSIZE - len - 1;
3914 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3915 len += (int)(tmp_ptr - ptr);
3916 *cont_s_ipos = TRUE;
3917 }
3918 IObuff[len] = NUL;
3919 ptr = IObuff;
3920 }
3921 if (len == compl_length)
3922 return NULL;
3923 }
3924 }
3925
3926 *match_len = len;
3927 return ptr;
3928}
3929
3930/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003931 * Get the next set of words matching "compl_pattern" for default completion(s)
3932 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003933 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3934 * position "st->start_pos" in the "compl_direction" direction. If
3935 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3936 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003937 * Returns OK if a new next match is found, otherwise returns FAIL.
3938 */
3939 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003940get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003941{
3942 int found_new_match = FAIL;
3943 int save_p_scs;
3944 int save_p_ws;
3945 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003946 char_u *ptr = NULL;
3947 int len = 0;
3948 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3949 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003950
3951 // If 'infercase' is set, don't use 'smartcase' here
3952 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003953 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003954 p_scs = FALSE;
3955
3956 // Buffers other than curbuf are scanned from the beginning or the
3957 // end but never from the middle, thus setting nowrapscan in this
3958 // buffer is a good idea, on the other hand, we always set
3959 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3960 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003961 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003962 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003963 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003964 p_ws = TRUE;
3965 looped_around = FALSE;
3966 for (;;)
3967 {
3968 int cont_s_ipos = FALSE;
3969
3970 ++msg_silent; // Don't want messages for wrapscan.
3971
3972 // ctrl_x_mode_line_or_eval() || word-wise search that
3973 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003974 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003975 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01003976 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02003977 else if (in_fuzzy)
3978 found_new_match = search_for_fuzzy_match(st->ins_buf,
3979 st->cur_match_pos, leader, compl_direction,
3980 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003981 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003982 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01003983 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02003984 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003985 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003986 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003987 {
3988 // set "compl_started" even on fail
3989 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003990 st->first_match_pos = *st->cur_match_pos;
3991 st->last_match_pos = *st->cur_match_pos;
3992 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003993 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003994 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3995 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003996 {
3997 found_new_match = FAIL;
3998 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003999 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004000 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4001 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4002 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004003 {
4004 if (looped_around)
4005 found_new_match = FAIL;
4006 else
4007 looped_around = TRUE;
4008 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004009 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004010 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4011 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4012 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004013 {
4014 if (looped_around)
4015 found_new_match = FAIL;
4016 else
4017 looped_around = TRUE;
4018 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004019 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004020 if (found_new_match == FAIL)
4021 break;
4022
4023 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004024 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004025 && start_pos->lnum == st->cur_match_pos->lnum
4026 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004027 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004028
glepnir8159fb12024-07-17 20:32:54 +02004029 if (!in_fuzzy)
4030 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4031 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004032 if (ptr == NULL)
4033 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004034
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004035 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004036 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004037 0, cont_s_ipos) != NOTDONE)
4038 {
4039 found_new_match = OK;
4040 break;
4041 }
4042 }
4043 p_scs = save_p_scs;
4044 p_ws = save_p_ws;
4045
4046 return found_new_match;
4047}
4048
4049/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004050 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004051 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004052 */
4053 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004054get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004055{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004056 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004057
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004058 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004059 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060 case -1:
4061 break;
4062#ifdef FEAT_FIND_ID
4063 case CTRL_X_PATH_PATTERNS:
4064 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004065 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 break;
4067#endif
4068
4069 case CTRL_X_DICTIONARY:
4070 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004071 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4072 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004073 break;
4074
4075 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004076 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077 break;
4078
4079 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004080 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004081 break;
4082
4083 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004084 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004085 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004086 break;
4087
4088#ifdef FEAT_COMPL_FUNC
4089 case CTRL_X_FUNCTION:
4090 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004091 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004092 break;
4093#endif
4094
4095 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004096 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004097 break;
4098
4099 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004100 found_new_match = get_next_default_completion(st, ini);
4101 if (found_new_match == FAIL && st->ins_buf == curbuf)
4102 st->found_all = TRUE;
4103 }
4104
4105 // check if compl_curr_match has changed, (e.g. other type of
4106 // expansion added something)
4107 if (type != 0 && compl_curr_match != compl_old_match)
4108 found_new_match = OK;
4109
4110 return found_new_match;
4111}
4112
4113/*
4114 * Get the next expansion(s), using "compl_pattern".
4115 * The search starts at position "ini" in curbuf and in the direction
4116 * compl_direction.
4117 * When "compl_started" is FALSE start at that position, otherwise continue
4118 * where we stopped searching before.
4119 * This may return before finding all the matches.
4120 * Return the total number of matches or -1 if still unknown -- Acevedo
4121 */
4122 static int
4123ins_compl_get_exp(pos_T *ini)
4124{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004125 static ins_compl_next_state_T st;
4126 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004127 int i;
4128 int found_new_match;
4129 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004130 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004131
4132 if (!compl_started)
4133 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004134 buf_T *buf;
4135
4136 FOR_ALL_BUFFERS(buf)
4137 buf->b_scanned = 0;
4138 if (!st_cleared)
4139 {
4140 CLEAR_FIELD(st);
4141 st_cleared = TRUE;
4142 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004143 st.found_all = FALSE;
4144 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004145 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004146 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004147 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4148 ? (char_u *)"." : curbuf->b_p_cpt);
4149 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004150 st.last_match_pos = st.first_match_pos = *ini;
4151 }
4152 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4153 st.ins_buf = curbuf; // In case the buffer was wiped out.
4154
4155 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004156 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004157 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004158
4159 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4160 for (;;)
4161 {
4162 found_new_match = FAIL;
4163 st.set_match_pos = FALSE;
4164
4165 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4166 // or if found_all says this entry is done. For ^X^L only use the
4167 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004168 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004169 && (!compl_started || st.found_all))
4170 {
glepnir8159fb12024-07-17 20:32:54 +02004171 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004172
4173 if (status == INS_COMPL_CPT_END)
4174 break;
4175 if (status == INS_COMPL_CPT_CONT)
4176 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004177 }
4178
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004179 // If complete() was called then compl_pattern has been reset. The
4180 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004181 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004182 break;
4183
4184 // get the next set of completion matches
4185 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004186
4187 // break the loop for specialized modes (use 'complete' just for the
4188 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4189 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004190 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4191 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004192 {
4193 if (got_int)
4194 break;
4195 // Fill the popup menu as soon as possible.
4196 if (type != -1)
4197 ins_compl_check_keys(0, FALSE);
4198
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004199 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004200 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4201 break;
4202 compl_started = TRUE;
4203 }
4204 else
4205 {
4206 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004207 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004208 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209
4210 compl_started = FALSE;
4211 }
4212 }
4213 compl_started = TRUE;
4214
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004215 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004216 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004217 found_new_match = FAIL;
4218
4219 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004220 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004221 && !ctrl_x_mode_line_or_eval()))
4222 i = ins_compl_make_cyclic();
4223
4224 if (compl_old_match != NULL)
4225 {
4226 // If several matches were added (FORWARD) or the search failed and has
4227 // just been made cyclic then we have to move compl_curr_match to the
4228 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004229 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004230 : compl_old_match->cp_prev;
4231 if (compl_curr_match == NULL)
4232 compl_curr_match = compl_old_match;
4233 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004234 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004235
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004236 return i;
4237}
4238
4239/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004240 * Update "compl_shown_match" to the actually shown match, it may differ when
4241 * "compl_leader" is used to omit some of the matches.
4242 */
4243 static void
4244ins_compl_update_shown_match(void)
4245{
4246 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004247 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004248 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004249 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004250 compl_shown_match = compl_shown_match->cp_next;
4251
4252 // If we didn't find it searching forward, and compl_shows_dir is
4253 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004254 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004255 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004256 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004257 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004258 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004259 {
4260 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004261 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004262 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004263 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004264 compl_shown_match = compl_shown_match->cp_prev;
4265 }
4266}
4267
4268/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004269 * Delete the old text being completed.
4270 */
4271 void
4272ins_compl_delete(void)
4273{
4274 int col;
4275
4276 // In insert mode: Delete the typed part.
4277 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004278 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004279 if ((int)curwin->w_cursor.col > col)
4280 {
4281 if (stop_arrow() == FAIL)
4282 return;
4283 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004284 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004285 }
4286
4287 // TODO: is this sufficient for redrawing? Redrawing everything causes
4288 // flicker, thus we can't do that.
4289 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004290#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004291 // clear v:completed_item
4292 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004293#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004294}
4295
4296/*
4297 * Insert the new text being completed.
4298 * "in_compl_func" is TRUE when called from complete_check().
4299 */
4300 void
4301ins_compl_insert(int in_compl_func)
4302{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004303 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004304
4305 // Make sure we don't go over the end of the string, this can happen with
4306 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004307 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004308 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004309 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004310 compl_used_match = FALSE;
4311 else
4312 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004313#ifdef FEAT_EVAL
4314 {
4315 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4316
4317 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4318 }
4319#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004320 if (!in_compl_func)
4321 compl_curr_match = compl_shown_match;
4322}
4323
4324/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004325 * show the file name for the completion match (if any). Truncate the file
4326 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004327 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004328 static void
4329ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004330{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004331 char *lead = _("match in file");
4332 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4333 char_u *s;
4334 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004335
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004336 if (space <= 0)
4337 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004338
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004339 // We need the tail that fits. With double-byte encoding going
4340 // back from the end is very slow, thus go from the start and keep
4341 // the text that fits in "space" between "s" and "e".
4342 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004343 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004344 space -= ptr2cells(e);
4345 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004346 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004347 space += ptr2cells(s);
4348 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004349 }
4350 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004351 msg_hist_off = TRUE;
4352 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4353 s > compl_shown_match->cp_fname ? "<" : "", s);
4354 msg((char *)IObuff);
4355 msg_hist_off = FALSE;
4356 redraw_cmdline = FALSE; // don't overwrite!
4357}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004358
glepnirdca57fb2024-06-04 22:01:21 +02004359/*
zeertzjq551d8c32024-06-05 19:53:32 +02004360 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004361 */
glepnira218cc62024-06-03 19:32:39 +02004362 static compl_T *
4363find_comp_when_fuzzy(void)
4364{
4365 int score;
4366 char_u* str;
4367 int target_idx = -1;
4368 int is_forward = compl_shows_dir_forward();
4369 int is_backward = compl_shows_dir_backward();
4370 compl_T *comp = NULL;
4371
glepnir43eef882024-06-19 20:20:48 +02004372 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004373 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004374 return compl_first_match != compl_shown_match ? compl_first_match :
4375 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004376
4377 if (is_forward)
4378 target_idx = compl_selected_item + 1;
4379 else if (is_backward)
4380 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004381 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004382
4383 score = compl_match_array[target_idx].pum_score;
4384 str = compl_match_array[target_idx].pum_text;
4385
4386 comp = compl_first_match;
4387 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004388 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004389 return comp;
4390 comp = comp->cp_next;
4391 } while (comp != NULL && !is_first_match(comp));
4392
4393 return NULL;
4394}
4395
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004396/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004397 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004398 * times. The number of matches found is returned in 'num_matches'.
4399 *
4400 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4401 * get more completions. If it is FALSE, then do nothing when there are no more
4402 * completions in the given direction.
4403 *
4404 * If "advance" is TRUE, then completion will move to the first match.
4405 * Otherwise, the original text will be shown.
4406 *
4407 * Returns OK on success and -1 if the number of matches are unknown.
4408 */
4409 static int
4410find_next_completion_match(
4411 int allow_get_expansion,
4412 int todo, // repeat completion this many times
4413 int advance,
4414 int *num_matches)
4415{
4416 int found_end = FALSE;
4417 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004418 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004419 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4420 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004422 while (--todo >= 0)
4423 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004424 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004425 {
glepniraced8c22024-06-15 15:13:05 +02004426 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4427 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004428 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004429 && (is_first_match(compl_shown_match->cp_next)
4430 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004431 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004432 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004433 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004434 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004435 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004436 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4437 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004438 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004439 }
4440 else
4441 {
4442 if (!allow_get_expansion)
4443 {
4444 if (advance)
4445 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004446 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004447 compl_pending -= todo + 1;
4448 else
4449 compl_pending += todo + 1;
4450 }
4451 return -1;
4452 }
4453
4454 if (!compl_no_select && advance)
4455 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004456 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004457 --compl_pending;
4458 else
4459 ++compl_pending;
4460 }
4461
4462 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004463 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004464
4465 // handle any pending completions
4466 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004467 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004468 {
4469 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4470 {
4471 compl_shown_match = compl_shown_match->cp_next;
4472 --compl_pending;
4473 }
4474 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4475 {
4476 compl_shown_match = compl_shown_match->cp_prev;
4477 ++compl_pending;
4478 }
4479 else
4480 break;
4481 }
4482 found_end = FALSE;
4483 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004484 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004485 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004486 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004487 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004488 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004489 ++todo;
4490 else
4491 // Remember a matching item.
4492 found_compl = compl_shown_match;
4493
4494 // Stop at the end of the list when we found a usable match.
4495 if (found_end)
4496 {
4497 if (found_compl != NULL)
4498 {
4499 compl_shown_match = found_compl;
4500 break;
4501 }
4502 todo = 1; // use first usable match after wrapping around
4503 }
4504 }
4505
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004506 return OK;
4507}
4508
4509/*
4510 * Fill in the next completion in the current direction.
4511 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4512 * get more completions. If it is FALSE, then we just do nothing when there
4513 * are no more completions in a given direction. The latter case is used when
4514 * we are still in the middle of finding completions, to allow browsing
4515 * through the ones found so far.
4516 * Return the total number of matches, or -1 if still unknown -- webb.
4517 *
4518 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4519 * compl_shown_match here.
4520 *
4521 * Note that this function may be called recursively once only. First with
4522 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4523 * calls this function with "allow_get_expansion" FALSE.
4524 */
4525 static int
4526ins_compl_next(
4527 int allow_get_expansion,
4528 int count, // repeat completion this many times; should
4529 // be at least 1
4530 int insert_match, // Insert the newly selected match
4531 int in_compl_func) // called from complete_check()
4532{
4533 int num_matches = -1;
4534 int todo = count;
4535 int advance;
4536 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004537 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004538 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004539 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4540 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004541
4542 // When user complete function return -1 for findstart which is next
4543 // time of 'always', compl_shown_match become NULL.
4544 if (compl_shown_match == NULL)
4545 return -1;
4546
John Marriott5e6ea922024-11-23 14:01:57 +01004547 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004548 && !match_at_original_text(compl_shown_match)
4549 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004550 // Update "compl_shown_match" to the actually shown match
4551 ins_compl_update_shown_match();
4552
4553 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004554 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004555 // Delete old text to be replaced
4556 ins_compl_delete();
4557
4558 // When finding the longest common text we stick at the original text,
4559 // don't let CTRL-N or CTRL-P move to the first match.
4560 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4561
4562 // When restarting the search don't insert the first match either.
4563 if (compl_restarting)
4564 {
4565 advance = FALSE;
4566 compl_restarting = FALSE;
4567 }
4568
4569 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4570 // around.
4571 if (find_next_completion_match(allow_get_expansion, todo, advance,
4572 &num_matches) == -1)
4573 return -1;
4574
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004575 if (curbuf != orig_curbuf)
4576 {
4577 // In case some completion function switched buffer, don't want to
4578 // insert the completion elsewhere.
4579 return -1;
4580 }
4581
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004582 // Insert the text of the new completion, or the compl_leader.
4583 if (compl_no_insert && !started)
4584 {
glepnir6a38aff2024-12-16 21:56:16 +01004585 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004586 compl_used_match = FALSE;
4587 }
4588 else if (insert_match)
4589 {
4590 if (!compl_get_longest || compl_used_match)
4591 ins_compl_insert(in_compl_func);
4592 else
glepnir6a38aff2024-12-16 21:56:16 +01004593 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004594 }
4595 else
4596 compl_used_match = FALSE;
4597
4598 if (!allow_get_expansion)
4599 {
4600 // may undisplay the popup menu first
4601 ins_compl_upd_pum();
4602
4603 if (pum_enough_matches())
4604 // Will display the popup menu, don't redraw yet to avoid flicker.
4605 pum_call_update_screen();
4606 else
4607 // Not showing the popup menu yet, redraw to show the user what was
4608 // inserted.
4609 update_screen(0);
4610
4611 // display the updated popup menu
4612 ins_compl_show_pum();
4613#ifdef FEAT_GUI
4614 if (gui.in_use)
4615 {
4616 // Show the cursor after the match, not after the redrawn text.
4617 setcursor();
4618 out_flush_cursor(FALSE, FALSE);
4619 }
4620#endif
4621
4622 // Delete old text to be replaced, since we're still searching and
4623 // don't want to match ourselves!
4624 ins_compl_delete();
4625 }
4626
4627 // Enter will select a match when the match wasn't inserted and the popup
4628 // menu is visible.
4629 if (compl_no_insert && !started)
4630 compl_enter_selects = TRUE;
4631 else
4632 compl_enter_selects = !insert_match && compl_match_array != NULL;
4633
4634 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004635 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004636 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004637
4638 return num_matches;
4639}
4640
4641/*
4642 * Call this while finding completions, to check whether the user has hit a key
4643 * that should change the currently displayed completion, or exit completion
4644 * mode. Also, when compl_pending is not zero, show a completion as soon as
4645 * possible. -- webb
4646 * "frequency" specifies out of how many calls we actually check.
4647 * "in_compl_func" is TRUE when called from complete_check(), don't set
4648 * compl_curr_match.
4649 */
4650 void
4651ins_compl_check_keys(int frequency, int in_compl_func)
4652{
4653 static int count = 0;
4654 int c;
4655
4656 // Don't check when reading keys from a script, :normal or feedkeys().
4657 // That would break the test scripts. But do check for keys when called
4658 // from complete_check().
4659 if (!in_compl_func && (using_script() || ex_normal_busy))
4660 return;
4661
4662 // Only do this at regular intervals
4663 if (++count < frequency)
4664 return;
4665 count = 0;
4666
4667 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4668 // can't do its work correctly.
4669 c = vpeekc_any();
4670 if (c != NUL)
4671 {
4672 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4673 {
4674 c = safe_vgetc(); // Eat the character
4675 compl_shows_dir = ins_compl_key2dir(c);
4676 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4677 c != K_UP && c != K_DOWN, in_compl_func);
4678 }
4679 else
4680 {
4681 // Need to get the character to have KeyTyped set. We'll put it
4682 // back with vungetc() below. But skip K_IGNORE.
4683 c = safe_vgetc();
4684 if (c != K_IGNORE)
4685 {
4686 // Don't interrupt completion when the character wasn't typed,
4687 // e.g., when doing @q to replay keys.
4688 if (c != Ctrl_R && KeyTyped)
4689 compl_interrupted = TRUE;
4690
4691 vungetc(c);
4692 }
4693 }
4694 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004695 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004696 {
4697 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4698
4699 compl_pending = 0;
4700 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4701 }
4702}
4703
4704/*
4705 * Decide the direction of Insert mode complete from the key typed.
4706 * Returns BACKWARD or FORWARD.
4707 */
4708 static int
4709ins_compl_key2dir(int c)
4710{
4711 if (c == Ctrl_P || c == Ctrl_L
4712 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4713 return BACKWARD;
4714 return FORWARD;
4715}
4716
4717/*
4718 * Return TRUE for keys that are used for completion only when the popup menu
4719 * is visible.
4720 */
4721 static int
4722ins_compl_pum_key(int c)
4723{
4724 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4725 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4726 || c == K_UP || c == K_DOWN);
4727}
4728
4729/*
4730 * Decide the number of completions to move forward.
4731 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4732 */
4733 static int
4734ins_compl_key2count(int c)
4735{
4736 int h;
4737
4738 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4739 {
4740 h = pum_get_height();
4741 if (h > 3)
4742 h -= 2; // keep some context
4743 return h;
4744 }
4745 return 1;
4746}
4747
4748/*
4749 * Return TRUE if completion with "c" should insert the match, FALSE if only
4750 * to change the currently selected completion.
4751 */
4752 static int
4753ins_compl_use_match(int c)
4754{
4755 switch (c)
4756 {
4757 case K_UP:
4758 case K_DOWN:
4759 case K_PAGEDOWN:
4760 case K_KPAGEDOWN:
4761 case K_S_DOWN:
4762 case K_PAGEUP:
4763 case K_KPAGEUP:
4764 case K_S_UP:
4765 return FALSE;
4766 }
4767 return TRUE;
4768}
4769
4770/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004771 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4772 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004773 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004774 * Uses the global variables: compl_cont_status and ctrl_x_mode
4775 */
4776 static int
4777get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4778{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004779 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004780 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004781 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004782 {
4783 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4784 ;
4785 compl_col += ++startcol;
4786 compl_length = curs_col - startcol;
4787 }
4788 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004789 {
John Marriott5e6ea922024-11-23 14:01:57 +01004790 compl_pattern.string = str_foldcase(line + compl_col,
4791 compl_length, NULL, 0);
4792 if (compl_pattern.string == NULL)
4793 {
4794 compl_pattern.length = 0;
4795 return FAIL;
4796 }
4797 compl_pattern.length = STRLEN(compl_pattern.string);
4798 }
4799 else
4800 {
4801 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4802 if (compl_pattern.string == NULL)
4803 {
4804 compl_pattern.length = 0;
4805 return FAIL;
4806 }
4807 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004808 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004809 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004810 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004811 {
4812 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004813 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004814 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004815
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004816 if (!vim_iswordp(line + compl_col)
4817 || (compl_col > 0
4818 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004819 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004820 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004821 prefixlen = 0;
4822 }
John Marriott5e6ea922024-11-23 14:01:57 +01004823
4824 // we need up to 2 extra chars for the prefix
4825 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4826 compl_pattern.string = alloc(n);
4827 if (compl_pattern.string == NULL)
4828 {
4829 compl_pattern.length = 0;
4830 return FAIL;
4831 }
4832 STRCPY((char *)compl_pattern.string, prefix);
4833 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004834 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004835 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004836 }
4837 else if (--startcol < 0
4838 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4839 {
John Marriott5e6ea922024-11-23 14:01:57 +01004840 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4841
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004842 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004843 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4844 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004845 {
John Marriott5e6ea922024-11-23 14:01:57 +01004846 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004847 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004848 }
John Marriott5e6ea922024-11-23 14:01:57 +01004849 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004850 compl_col += curs_col;
4851 compl_length = 0;
4852 }
4853 else
4854 {
4855 // Search the point of change class of multibyte character
4856 // or not a word single byte character backward.
4857 if (has_mbyte)
4858 {
4859 int base_class;
4860 int head_off;
4861
4862 startcol -= (*mb_head_off)(line, line + startcol);
4863 base_class = mb_get_class(line + startcol);
4864 while (--startcol >= 0)
4865 {
4866 head_off = (*mb_head_off)(line, line + startcol);
4867 if (base_class != mb_get_class(line + startcol
4868 - head_off))
4869 break;
4870 startcol -= head_off;
4871 }
4872 }
4873 else
4874 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4875 ;
4876 compl_col += ++startcol;
4877 compl_length = (int)curs_col - startcol;
4878 if (compl_length == 1)
4879 {
4880 // Only match word with at least two chars -- webb
4881 // there's no need to call quote_meta,
4882 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004883 compl_pattern.string = alloc(7);
4884 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004885 {
John Marriott5e6ea922024-11-23 14:01:57 +01004886 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004887 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004888 }
John Marriott5e6ea922024-11-23 14:01:57 +01004889 STRCPY((char *)compl_pattern.string, "\\<");
4890 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4891 STRCAT((char *)compl_pattern.string, "\\k");
4892 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004893 }
4894 else
4895 {
John Marriott5e6ea922024-11-23 14:01:57 +01004896 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4897
4898 compl_pattern.string = alloc(n);
4899 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004900 {
John Marriott5e6ea922024-11-23 14:01:57 +01004901 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004902 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004903 }
John Marriott5e6ea922024-11-23 14:01:57 +01004904 STRCPY((char *)compl_pattern.string, "\\<");
4905 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004906 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004907 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004908 }
4909 }
4910
4911 return OK;
4912}
4913
4914/*
4915 * Get the pattern, column and length for whole line completion or for the
4916 * complete() function.
4917 * Sets the global variables: compl_col, compl_length and compl_pattern.
4918 */
4919 static int
4920get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4921{
4922 compl_col = (colnr_T)getwhitecols(line);
4923 compl_length = (int)curs_col - (int)compl_col;
4924 if (compl_length < 0) // cursor in indent: empty pattern
4925 compl_length = 0;
4926 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004927 {
John Marriott5e6ea922024-11-23 14:01:57 +01004928 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4929 NULL, 0);
4930 if (compl_pattern.string == NULL)
4931 {
4932 compl_pattern.length = 0;
4933 return FAIL;
4934 }
4935 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004936 }
John Marriott5e6ea922024-11-23 14:01:57 +01004937 else
4938 {
4939 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4940 if (compl_pattern.string == NULL)
4941 {
4942 compl_pattern.length = 0;
4943 return FAIL;
4944 }
4945 compl_pattern.length = (size_t)compl_length;
4946 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004947
4948 return OK;
4949}
4950
4951/*
4952 * Get the pattern, column and length for filename completion.
4953 * Sets the global variables: compl_col, compl_length and compl_pattern.
4954 */
4955 static int
4956get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4957{
4958 // Go back to just before the first filename character.
4959 if (startcol > 0)
4960 {
4961 char_u *p = line + startcol;
4962
4963 MB_PTR_BACK(line, p);
4964 while (p > line && vim_isfilec(PTR2CHAR(p)))
4965 MB_PTR_BACK(line, p);
4966 if (p == line && vim_isfilec(PTR2CHAR(p)))
4967 startcol = 0;
4968 else
4969 startcol = (int)(p - line) + 1;
4970 }
4971
4972 compl_col += startcol;
4973 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01004974 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
4975 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004976 {
John Marriott5e6ea922024-11-23 14:01:57 +01004977 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004978 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004979 }
4980
John Marriott5e6ea922024-11-23 14:01:57 +01004981 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004982
4983 return OK;
4984}
4985
4986/*
4987 * Get the pattern, column and length for command-line completion.
4988 * Sets the global variables: compl_col, compl_length and compl_pattern.
4989 */
4990 static int
4991get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4992{
John Marriott5e6ea922024-11-23 14:01:57 +01004993 compl_pattern.string = vim_strnsave(line, curs_col);
4994 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004995 {
John Marriott5e6ea922024-11-23 14:01:57 +01004996 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004997 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004998 }
John Marriott5e6ea922024-11-23 14:01:57 +01004999 compl_pattern.length = curs_col;
5000 set_cmd_context(&compl_xp, compl_pattern.string,
5001 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005002 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5003 || compl_xp.xp_context == EXPAND_NOTHING)
5004 // No completion possible, use an empty pattern to get a
5005 // "pattern not found" message.
5006 compl_col = curs_col;
5007 else
John Marriott5e6ea922024-11-23 14:01:57 +01005008 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005009 compl_length = curs_col - compl_col;
5010
5011 return OK;
5012}
5013
5014/*
5015 * Get the pattern, column and length for user defined completion ('omnifunc',
5016 * 'completefunc' and 'thesaurusfunc')
5017 * Sets the global variables: compl_col, compl_length and compl_pattern.
5018 * Uses the global variable: spell_bad_len
5019 */
5020 static int
5021get_userdefined_compl_info(colnr_T curs_col UNUSED)
5022{
5023 int ret = FAIL;
5024
5025#ifdef FEAT_COMPL_FUNC
5026 // Call user defined function 'completefunc' with "a:findstart"
5027 // set to 1 to obtain the length of text to use for completion.
5028 char_u *line;
5029 typval_T args[3];
5030 int col;
5031 char_u *funcname;
5032 pos_T pos;
5033 int save_State = State;
5034 callback_T *cb;
5035
5036 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5037 // length as a string
5038 funcname = get_complete_funcname(ctrl_x_mode);
5039 if (*funcname == NUL)
5040 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005041 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005042 ? "completefunc" : "omnifunc");
5043 return FAIL;
5044 }
5045
5046 args[0].v_type = VAR_NUMBER;
5047 args[0].vval.v_number = 1;
5048 args[1].v_type = VAR_STRING;
5049 args[1].vval.v_string = (char_u *)"";
5050 args[2].v_type = VAR_UNKNOWN;
5051 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005052 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005053 cb = get_insert_callback(ctrl_x_mode);
5054 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005055 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005056
5057 State = save_State;
5058 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005059 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005060 validate_cursor();
5061 if (!EQUAL_POS(curwin->w_cursor, pos))
5062 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005063 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005064 return FAIL;
5065 }
5066
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005067 // Return value -2 means the user complete function wants to cancel the
5068 // complete without an error, do the same if the function did not execute
5069 // successfully.
5070 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005071 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005072 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005073 if (col == -3)
5074 {
5075 ctrl_x_mode = CTRL_X_NORMAL;
5076 edit_submode = NULL;
5077 if (!shortmess(SHM_COMPLETIONMENU))
5078 msg_clr_cmdline();
5079 return FAIL;
5080 }
5081
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005082 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005083 // completion.
5084 compl_opt_refresh_always = FALSE;
5085 compl_opt_suppress_empty = FALSE;
5086
5087 if (col < 0)
5088 col = curs_col;
5089 compl_col = col;
5090 if (compl_col > curs_col)
5091 compl_col = curs_col;
5092
5093 // Setup variables for completion. Need to obtain "line" again,
5094 // it may have become invalid.
5095 line = ml_get(curwin->w_cursor.lnum);
5096 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005097 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5098 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005099 {
John Marriott5e6ea922024-11-23 14:01:57 +01005100 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005101 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005102 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005103
John Marriott5e6ea922024-11-23 14:01:57 +01005104 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005105 ret = OK;
5106#endif
5107
5108 return ret;
5109}
5110
5111/*
5112 * Get the pattern, column and length for spell completion.
5113 * Sets the global variables: compl_col, compl_length and compl_pattern.
5114 * Uses the global variable: spell_bad_len
5115 */
5116 static int
5117get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5118{
5119 int ret = FAIL;
5120#ifdef FEAT_SPELL
5121 char_u *line;
5122
5123 if (spell_bad_len > 0)
5124 compl_col = curs_col - spell_bad_len;
5125 else
5126 compl_col = spell_word_start(startcol);
5127 if (compl_col >= (colnr_T)startcol)
5128 {
5129 compl_length = 0;
5130 compl_col = curs_col;
5131 }
5132 else
5133 {
5134 spell_expand_check_cap(compl_col);
5135 compl_length = (int)curs_col - compl_col;
5136 }
5137 // Need to obtain "line" again, it may have become invalid.
5138 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005139 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5140 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005141 {
John Marriott5e6ea922024-11-23 14:01:57 +01005142 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005143 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005144 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005145
John Marriott5e6ea922024-11-23 14:01:57 +01005146 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005147 ret = OK;
5148#endif
5149
5150 return ret;
5151}
5152
5153/*
5154 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005155 * "startcol" - start column number of the completion pattern/text
5156 * "cur_col" - current cursor column
5157 * On return, "line_invalid" is set to TRUE, if the current line may have
5158 * become invalid and needs to be fetched again.
5159 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005160 */
5161 static int
5162compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5163{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005164 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005165 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5166 && !thesaurus_func_complete(ctrl_x_mode)))
5167 {
5168 return get_normal_compl_info(line, startcol, curs_col);
5169 }
5170 else if (ctrl_x_mode_line_or_eval())
5171 {
5172 return get_wholeline_compl_info(line, curs_col);
5173 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005174 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005175 {
5176 return get_filename_compl_info(line, startcol, curs_col);
5177 }
5178 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5179 {
5180 return get_cmdline_compl_info(line, curs_col);
5181 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005182 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005183 || thesaurus_func_complete(ctrl_x_mode))
5184 {
5185 if (get_userdefined_compl_info(curs_col) == FAIL)
5186 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005187 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005188 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005189 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005190 {
5191 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5192 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005193 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005194 }
5195 else
5196 {
5197 internal_error("ins_complete()");
5198 return FAIL;
5199 }
5200
5201 return OK;
5202}
5203
5204/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005205 * Continue an interrupted completion mode search in "line".
5206 *
5207 * If this same ctrl_x_mode has been interrupted use the text from
5208 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5209 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5210 * the same line as the cursor then fix it (the line has been split because it
5211 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5212 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005213 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005214 static void
5215ins_compl_continue_search(char_u *line)
5216{
5217 // it is a continued search
5218 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005219 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5220 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005221 {
5222 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5223 {
5224 // line (probably) wrapped, set compl_startpos to the
5225 // first non_blank in the line, if it is not a wordchar
5226 // include it to get a better pattern, but then we don't
5227 // want the "\\<" prefix, check it below
5228 compl_col = (colnr_T)getwhitecols(line);
5229 compl_startpos.col = compl_col;
5230 compl_startpos.lnum = curwin->w_cursor.lnum;
5231 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5232 }
5233 else
5234 {
5235 // S_IPOS was set when we inserted a word that was at the
5236 // beginning of the line, which means that we'll go to SOL
5237 // mode but first we need to redefine compl_startpos
5238 if (compl_cont_status & CONT_S_IPOS)
5239 {
5240 compl_cont_status |= CONT_SOL;
5241 compl_startpos.col = (colnr_T)(skipwhite(
5242 line + compl_length
5243 + compl_startpos.col) - line);
5244 }
5245 compl_col = compl_startpos.col;
5246 }
5247 compl_length = curwin->w_cursor.col - (int)compl_col;
5248 // IObuff is used to add a "word from the next line" would we
5249 // have enough space? just being paranoid
5250#define MIN_SPACE 75
5251 if (compl_length > (IOSIZE - MIN_SPACE))
5252 {
5253 compl_cont_status &= ~CONT_SOL;
5254 compl_length = (IOSIZE - MIN_SPACE);
5255 compl_col = curwin->w_cursor.col - compl_length;
5256 }
5257 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5258 if (compl_length < 1)
5259 compl_cont_status &= CONT_LOCAL;
5260 }
5261 else if (ctrl_x_mode_line_or_eval())
5262 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5263 else
5264 compl_cont_status = 0;
5265}
5266
5267/*
5268 * start insert mode completion
5269 */
5270 static int
5271ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005272{
5273 char_u *line;
5274 int startcol = 0; // column where searched text starts
5275 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005276 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005277 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005278 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005279
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005280 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005281
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005282 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005283 did_si = FALSE;
5284 can_si = FALSE;
5285 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005286 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005287 return FAIL;
5288
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005289 line = ml_get(curwin->w_cursor.lnum);
5290 curs_col = curwin->w_cursor.col;
5291 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005292
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005293 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5294 && compl_cont_mode == ctrl_x_mode)
5295 // this same ctrl-x_mode was interrupted previously. Continue the
5296 // completion.
5297 ins_compl_continue_search(line);
5298 else
5299 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005300
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005301 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005302 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005303 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005304 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005305 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5306 compl_cont_status = 0;
5307 compl_cont_status |= CONT_N_ADDS;
5308 compl_startpos = curwin->w_cursor;
5309 startcol = (int)curs_col;
5310 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005311 }
5312
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005313 // Work out completion pattern and original text -- webb
5314 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5315 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005316 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5317 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005318 // restore did_ai, so that adding comment leader works
5319 did_ai = save_did_ai;
5320 return FAIL;
5321 }
5322 // If "line" was changed while getting completion info get it again.
5323 if (line_invalid)
5324 line = ml_get(curwin->w_cursor.lnum);
5325
glepnir7cfe6932024-09-15 20:06:28 +02005326 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005327 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005328 {
5329 edit_submode_pre = (char_u *)_(" Adding");
5330 if (ctrl_x_mode_line_or_eval())
5331 {
5332 // Insert a new line, keep indentation but ignore 'comments'.
5333 char_u *old = curbuf->b_p_com;
5334
5335 curbuf->b_p_com = (char_u *)"";
5336 compl_startpos.lnum = curwin->w_cursor.lnum;
5337 compl_startpos.col = compl_col;
5338 ins_eol('\r');
5339 curbuf->b_p_com = old;
5340 compl_length = 0;
5341 compl_col = curwin->w_cursor.col;
5342 }
glepnir7cfe6932024-09-15 20:06:28 +02005343 else if (ctrl_x_mode_normal() && in_fuzzy)
5344 {
5345 compl_startpos = curwin->w_cursor;
5346 compl_cont_status &= CONT_S_IPOS;
5347 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005348 }
5349 else
5350 {
5351 edit_submode_pre = NULL;
5352 compl_startpos.col = compl_col;
5353 }
5354
5355 if (compl_cont_status & CONT_LOCAL)
5356 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5357 else
5358 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5359
5360 // If any of the original typed text has been changed we need to fix
5361 // the redo buffer.
5362 ins_compl_fixRedoBufForLeader(NULL);
5363
5364 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005365 VIM_CLEAR_STRING(compl_orig_text);
5366 compl_orig_text.length = (size_t)compl_length;
5367 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005368 if (p_ic)
5369 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005370 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5371 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005372 {
John Marriott5e6ea922024-11-23 14:01:57 +01005373 VIM_CLEAR_STRING(compl_pattern);
5374 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005375 return FAIL;
5376 }
5377
5378 // showmode might reset the internal line pointers, so it must
5379 // be called before line = ml_get(), or when this address is no
5380 // longer needed. -- Acevedo.
5381 edit_submode_extra = (char_u *)_("-- Searching...");
5382 edit_submode_highl = HLF_COUNT;
5383 showmode();
5384 edit_submode_extra = NULL;
5385 out_flush();
5386
5387 return OK;
5388}
5389
5390/*
5391 * display the completion status message
5392 */
5393 static void
5394ins_compl_show_statusmsg(void)
5395{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005396 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005397 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005398 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005399 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005400 ? (char_u *)_("Hit end of paragraph")
5401 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005402 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005403 }
5404
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005405 if (edit_submode_extra == NULL)
5406 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005407 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005408 {
5409 edit_submode_extra = (char_u *)_("Back at original");
5410 edit_submode_highl = HLF_W;
5411 }
5412 else if (compl_cont_status & CONT_S_IPOS)
5413 {
5414 edit_submode_extra = (char_u *)_("Word from other line");
5415 edit_submode_highl = HLF_COUNT;
5416 }
5417 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5418 {
5419 edit_submode_extra = (char_u *)_("The only match");
5420 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005421 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005422 }
5423 else
5424 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005425#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005426 // Update completion sequence number when needed.
5427 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005428 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005429#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005430 // The match should always have a sequence number now, this is
5431 // just a safety check.
5432 if (compl_curr_match->cp_number != -1)
5433 {
5434 // Space for 10 text chars. + 2x10-digit no.s = 31.
5435 // Translations may need more than twice that.
5436 static char_u match_ref[81];
5437
5438 if (compl_matches > 0)
5439 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005440 _("match %d of %d"),
5441 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005442 else
5443 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005444 _("match %d"),
5445 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005446 edit_submode_extra = match_ref;
5447 edit_submode_highl = HLF_R;
5448 if (dollar_vcol >= 0)
5449 curs_columns(FALSE);
5450 }
5451 }
5452 }
5453
5454 // Show a message about what (completion) mode we're in.
5455 if (!compl_opt_suppress_empty)
5456 {
5457 showmode();
5458 if (!shortmess(SHM_COMPLETIONMENU))
5459 {
5460 if (edit_submode_extra != NULL)
5461 {
5462 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005463 {
5464 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005465 msg_attr((char *)edit_submode_extra,
5466 edit_submode_highl < HLF_COUNT
5467 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005468 msg_hist_off = FALSE;
5469 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005470 }
5471 else
5472 msg_clr_cmdline(); // necessary for "noshowmode"
5473 }
5474 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005475}
5476
5477/*
5478 * Do Insert mode completion.
5479 * Called when character "c" was typed, which has a meaning for completion.
5480 * Returns OK if completion was done, FAIL if something failed (out of mem).
5481 */
5482 int
5483ins_complete(int c, int enable_pum)
5484{
5485 int n;
5486 int save_w_wrow;
5487 int save_w_leftcol;
5488 int insert_match;
5489
5490 compl_direction = ins_compl_key2dir(c);
5491 insert_match = ins_compl_use_match(c);
5492
5493 if (!compl_started)
5494 {
5495 if (ins_compl_start() == FAIL)
5496 return FAIL;
5497 }
5498 else if (insert_match && stop_arrow() == FAIL)
5499 return FAIL;
5500
5501 compl_shown_match = compl_curr_match;
5502 compl_shows_dir = compl_direction;
5503
5504 // Find next match (and following matches).
5505 save_w_wrow = curwin->w_wrow;
5506 save_w_leftcol = curwin->w_leftcol;
5507 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5508
5509 // may undisplay the popup menu
5510 ins_compl_upd_pum();
5511
5512 if (n > 1) // all matches have been found
5513 compl_matches = n;
5514 compl_curr_match = compl_shown_match;
5515 compl_direction = compl_shows_dir;
5516
5517 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5518 // mode.
5519 if (got_int && !global_busy)
5520 {
5521 (void)vgetc();
5522 got_int = FALSE;
5523 }
5524
5525 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005526 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005527 {
5528 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5529 // because we couldn't expand anything at first place, but if we used
5530 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5531 // (such as M in M'exico) if not tried already. -- Acevedo
5532 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005533 || compl_status_adding()
5534 || (ctrl_x_mode_not_default()
5535 && !ctrl_x_mode_path_patterns()
5536 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005537 compl_cont_status &= ~CONT_N_ADDS;
5538 }
5539
5540 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5541 compl_cont_status |= CONT_S_IPOS;
5542 else
5543 compl_cont_status &= ~CONT_S_IPOS;
5544
5545 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005546
5547 // Show the popup menu, unless we got interrupted.
5548 if (enable_pum && !compl_interrupted)
5549 show_pum(save_w_wrow, save_w_leftcol);
5550
5551 compl_was_interrupted = compl_interrupted;
5552 compl_interrupted = FALSE;
5553
5554 return OK;
5555}
5556
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005557/*
5558 * Remove (if needed) and show the popup menu
5559 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005560 static void
5561show_pum(int prev_w_wrow, int prev_w_leftcol)
5562{
5563 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005564 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005565 RedrawingDisabled = 0;
5566
5567 // If the cursor moved or the display scrolled we need to remove the pum
5568 // first.
5569 setcursor();
5570 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5571 ins_compl_del_pum();
5572
5573 ins_compl_show_pum();
5574 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005575
5576 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005577}
5578
5579/*
5580 * Looks in the first "len" chars. of "src" for search-metachars.
5581 * If dest is not NULL the chars. are copied there quoting (with
5582 * a backslash) the metachars, and dest would be NUL terminated.
5583 * Returns the length (needed) of dest
5584 */
5585 static unsigned
5586quote_meta(char_u *dest, char_u *src, int len)
5587{
5588 unsigned m = (unsigned)len + 1; // one extra for the NUL
5589
5590 for ( ; --len >= 0; src++)
5591 {
5592 switch (*src)
5593 {
5594 case '.':
5595 case '*':
5596 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005597 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005598 break;
5599 // FALLTHROUGH
5600 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005601 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005602 break;
5603 // FALLTHROUGH
5604 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005605 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005606 break;
5607 // FALLTHROUGH
5608 case '^': // currently it's not needed.
5609 case '$':
5610 m++;
5611 if (dest != NULL)
5612 *dest++ = '\\';
5613 break;
5614 }
5615 if (dest != NULL)
5616 *dest++ = *src;
5617 // Copy remaining bytes of a multibyte character.
5618 if (has_mbyte)
5619 {
5620 int i, mb_len;
5621
5622 mb_len = (*mb_ptr2len)(src) - 1;
5623 if (mb_len > 0 && len >= mb_len)
5624 for (i = 0; i < mb_len; ++i)
5625 {
5626 --len;
5627 ++src;
5628 if (dest != NULL)
5629 *dest++ = *src;
5630 }
5631 }
5632 }
5633 if (dest != NULL)
5634 *dest = NUL;
5635
5636 return m;
5637}
5638
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005639#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005640 void
5641free_insexpand_stuff(void)
5642{
John Marriott5e6ea922024-11-23 14:01:57 +01005643 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005644# ifdef FEAT_EVAL
5645 free_callback(&cfu_cb);
5646 free_callback(&ofu_cb);
5647 free_callback(&tsrfu_cb);
5648# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005649}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005650#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005651
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005652#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005653/*
5654 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5655 * spelled word, if there is one.
5656 */
5657 static void
5658spell_back_to_badword(void)
5659{
5660 pos_T tpos = curwin->w_cursor;
5661
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005662 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005663 if (curwin->w_cursor.col != tpos.col)
5664 start_arrow(&tpos);
5665}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005666#endif