blob: d3a6300a3557b6eef144bc9fcec53f41139915a8 [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
John Marriott5e6ea922024-11-23 14:01:57 +0100176static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100177 // completion started
178static int compl_cont_mode = 0;
179static expand_T compl_xp;
180
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000181// List of flags for method of completion.
182static int compl_cont_status = 0;
183# define CONT_ADDING 1 // "normal" or "adding" expansion
184# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
185 // it's set only iff N_ADDS is set
186# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
187# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
188 // if so, word-wise-expansion will set SOL
189# define CONT_SOL 16 // pattern includes start of line, just for
190 // word-wise expansion, not set for ^X^L
191# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
192 // expansion, (eg use complete=.)
193
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100194static int compl_opt_refresh_always = FALSE;
195static int compl_opt_suppress_empty = FALSE;
196
glepnira218cc62024-06-03 19:32:39 +0200197static int compl_selected_item = -1;
198
glepnir8159fb12024-07-17 20:32:54 +0200199static int *compl_fuzzy_scores;
200
glepnir80b66202024-11-27 21:53:53 +0100201static 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 +0100202static void ins_compl_longest_match(compl_T *match);
203static void ins_compl_del_pum(void);
204static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
205static char_u *find_line_end(char_u *ptr);
206static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100207static int ins_compl_need_restart(void);
208static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000209static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100211static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100212static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
213# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
214static void ins_compl_add_list(list_T *list);
215static void ins_compl_add_dict(dict_T *dict);
216# endif
217static int ins_compl_key2dir(int c);
218static int ins_compl_pum_key(int c);
219static int ins_compl_key2count(int c);
220static void show_pum(int prev_w_wrow, int prev_w_leftcol);
221static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100222
223#ifdef FEAT_SPELL
224static void spell_back_to_badword(void);
225static int spell_bad_len = 0; // length of located bad word
226#endif
227
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100228/*
229 * CTRL-X pressed in Insert mode.
230 */
231 void
232ins_ctrl_x(void)
233{
zeertzjqdca29d92021-08-31 19:12:51 +0200234 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100235 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000236 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100237 if (compl_cont_status & CONT_N_ADDS)
238 compl_cont_status |= CONT_INTRPT;
239 else
240 compl_cont_status = 0;
241 // We're not sure which CTRL-X mode it will be yet
242 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
243 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
244 edit_submode_pre = NULL;
245 showmode();
246 }
zeertzjqdca29d92021-08-31 19:12:51 +0200247 else
248 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
249 // CTRL-V look like CTRL-N
250 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100251
LemonBoy2bf52dd2022-04-09 18:17:34 +0100252 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100253}
254
255/*
256 * Functions to check the current CTRL-X mode.
257 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000258int ctrl_x_mode_none(void)
259 { return ctrl_x_mode == 0; }
260int ctrl_x_mode_normal(void)
261 { return ctrl_x_mode == CTRL_X_NORMAL; }
262int ctrl_x_mode_scroll(void)
263 { return ctrl_x_mode == CTRL_X_SCROLL; }
264int ctrl_x_mode_whole_line(void)
265 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
266int ctrl_x_mode_files(void)
267 { return ctrl_x_mode == CTRL_X_FILES; }
268int ctrl_x_mode_tags(void)
269 { return ctrl_x_mode == CTRL_X_TAGS; }
270int ctrl_x_mode_path_patterns(void)
271 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
272int ctrl_x_mode_path_defines(void)
273 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
274int ctrl_x_mode_dictionary(void)
275 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
276int ctrl_x_mode_thesaurus(void)
277 { return ctrl_x_mode == CTRL_X_THESAURUS; }
278int ctrl_x_mode_cmdline(void)
279 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000281int ctrl_x_mode_function(void)
282 { return ctrl_x_mode == CTRL_X_FUNCTION; }
283int ctrl_x_mode_omni(void)
284 { return ctrl_x_mode == CTRL_X_OMNI; }
285int ctrl_x_mode_spell(void)
286 { return ctrl_x_mode == CTRL_X_SPELL; }
287static int ctrl_x_mode_eval(void)
288 { return ctrl_x_mode == CTRL_X_EVAL; }
289int ctrl_x_mode_line_or_eval(void)
290 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100291
292/*
293 * Whether other than default completion has been selected.
294 */
295 int
296ctrl_x_mode_not_default(void)
297{
298 return ctrl_x_mode != CTRL_X_NORMAL;
299}
300
301/*
zeertzjqdca29d92021-08-31 19:12:51 +0200302 * Whether CTRL-X was typed without a following character,
303 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100304 */
305 int
306ctrl_x_mode_not_defined_yet(void)
307{
308 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
309}
310
311/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000312 * Return TRUE if currently in "normal" or "adding" insert completion matches
313 * state
314 */
315 int
316compl_status_adding(void)
317{
318 return compl_cont_status & CONT_ADDING;
319}
320
321/*
322 * Return TRUE if the completion pattern includes start of line, just for
323 * word-wise expansion.
324 */
325 int
326compl_status_sol(void)
327{
328 return compl_cont_status & CONT_SOL;
329}
330
331/*
332 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
333 */
334 int
335compl_status_local(void)
336{
337 return compl_cont_status & CONT_LOCAL;
338}
339
340/*
341 * Clear the completion status flags
342 */
343 void
344compl_status_clear(void)
345{
346 compl_cont_status = 0;
347}
348
349/*
350 * Return TRUE if completion is using the forward direction matches
351 */
352 static int
353compl_dir_forward(void)
354{
355 return compl_direction == FORWARD;
356}
357
358/*
359 * Return TRUE if currently showing forward completion matches
360 */
361 static int
362compl_shows_dir_forward(void)
363{
364 return compl_shows_dir == FORWARD;
365}
366
367/*
368 * Return TRUE if currently showing backward completion matches
369 */
370 static int
371compl_shows_dir_backward(void)
372{
373 return compl_shows_dir == BACKWARD;
374}
375
376/*
377 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100378 */
379 int
380has_compl_option(int dict_opt)
381{
382 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200383#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100384 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200385#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100386 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100387 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
388#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100389 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100390#endif
391 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 {
393 ctrl_x_mode = CTRL_X_NORMAL;
394 edit_submode = NULL;
395 msg_attr(dict_opt ? _("'dictionary' option is empty")
396 : _("'thesaurus' option is empty"),
397 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100398 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100399 {
400 vim_beep(BO_COMPL);
401 setcursor();
402 out_flush();
403#ifdef FEAT_EVAL
404 if (!get_vim_var_nr(VV_TESTING))
405#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100406 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100407 }
408 return FALSE;
409 }
410 return TRUE;
411}
412
413/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000414 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100415 * This depends on the current mode.
416 */
417 int
418vim_is_ctrl_x_key(int c)
419{
420 // Always allow ^R - let its results then be checked
421 if (c == Ctrl_R)
422 return TRUE;
423
424 // Accept <PageUp> and <PageDown> if the popup menu is visible.
425 if (ins_compl_pum_key(c))
426 return TRUE;
427
428 switch (ctrl_x_mode)
429 {
430 case 0: // Not in any CTRL-X mode
431 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
432 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200433 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100434 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
435 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
436 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
437 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
438 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200439 || c == Ctrl_S || c == Ctrl_K || c == 's'
440 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100441 case CTRL_X_SCROLL:
442 return (c == Ctrl_Y || c == Ctrl_E);
443 case CTRL_X_WHOLE_LINE:
444 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_FILES:
446 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_DICTIONARY:
448 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
449 case CTRL_X_THESAURUS:
450 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
451 case CTRL_X_TAGS:
452 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
453#ifdef FEAT_FIND_ID
454 case CTRL_X_PATH_PATTERNS:
455 return (c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_PATH_DEFINES:
457 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
458#endif
459 case CTRL_X_CMDLINE:
460 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
461 || c == Ctrl_X);
462#ifdef FEAT_COMPL_FUNC
463 case CTRL_X_FUNCTION:
464 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
465 case CTRL_X_OMNI:
466 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
467#endif
468 case CTRL_X_SPELL:
469 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
470 case CTRL_X_EVAL:
471 return (c == Ctrl_P || c == Ctrl_N);
472 }
473 internal_error("vim_is_ctrl_x_key()");
474 return FALSE;
475}
476
477/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000478 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000479 */
480 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000481match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000482{
483 return match->cp_flags & CP_ORIGINAL_TEXT;
484}
485
486/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000487 * Returns TRUE if "match" is the first match in the completion list.
488 */
489 static int
490is_first_match(compl_T *match)
491{
492 return match == compl_first_match;
493}
494
495/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100496 * Return TRUE when character "c" is part of the item currently being
497 * completed. Used to decide whether to abandon complete mode when the menu
498 * is visible.
499 */
500 int
501ins_compl_accept_char(int c)
502{
503 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
504 // When expanding an identifier only accept identifier chars.
505 return vim_isIDc(c);
506
507 switch (ctrl_x_mode)
508 {
509 case CTRL_X_FILES:
510 // When expanding file name only accept file name chars. But not
511 // path separators, so that "proto/<Tab>" expands files in
512 // "proto", not "proto/" as a whole
513 return vim_isfilec(c) && !vim_ispathsep(c);
514
515 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200516 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100517 case CTRL_X_OMNI:
518 // Command line and Omni completion can work with just about any
519 // printable character, but do stop at white space.
520 return vim_isprintc(c) && !VIM_ISWHITE(c);
521
522 case CTRL_X_WHOLE_LINE:
523 // For while line completion a space can be part of the line.
524 return vim_isprintc(c);
525 }
526 return vim_iswordc(c);
527}
528
529/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000530 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100531 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000532 */
533 static char_u *
534ins_compl_infercase_gettext(
535 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100536 int char_len,
537 int compl_char_len,
538 int min_len,
539 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000540{
541 int *wca; // Wide character array.
542 char_u *p;
543 int i, c;
544 int has_lower = FALSE;
545 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100546 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000547
548 IObuff[0] = NUL;
549
550 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100551 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000552 if (wca == NULL)
553 return IObuff;
554
555 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100556 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100557 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000558 if (has_mbyte)
559 wca[i] = mb_ptr2char_adv(&p);
560 else
561 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100562 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000563
564 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100565 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000566 for (i = 0; i < min_len; ++i)
567 {
glepnir6e199932024-12-14 21:13:27 +0100568 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000569 if (MB_ISLOWER(c))
570 {
571 has_lower = TRUE;
572 if (MB_ISUPPER(wca[i]))
573 {
574 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100575 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000576 wca[i] = MB_TOLOWER(wca[i]);
577 break;
578 }
579 }
580 }
581
582 // Rule 2: No lower case, 2nd consecutive letter converted to
583 // upper case.
584 if (!has_lower)
585 {
John Marriott5e6ea922024-11-23 14:01:57 +0100586 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000587 for (i = 0; i < min_len; ++i)
588 {
glepnir6e199932024-12-14 21:13:27 +0100589 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000590 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
591 {
592 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100593 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 wca[i] = MB_TOUPPER(wca[i]);
595 break;
596 }
597 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
598 }
599 }
600
601 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100602 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000603 for (i = 0; i < min_len; ++i)
604 {
glepnir6e199932024-12-14 21:13:27 +0100605 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000606 if (MB_ISLOWER(c))
607 wca[i] = MB_TOLOWER(wca[i]);
608 else if (MB_ISUPPER(c))
609 wca[i] = MB_TOUPPER(wca[i]);
610 }
611
612 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000613 p = IObuff;
614 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100615 ga_init2(&gap, 1, 500);
616 while (i < char_len)
617 {
618 if (gap.ga_data != NULL)
619 {
620 if (ga_grow(&gap, 10) == FAIL)
621 {
622 ga_clear(&gap);
623 return (char_u *)"[failed]";
624 }
625 p = (char_u *)gap.ga_data + gap.ga_len;
626 if (has_mbyte)
627 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
628 else
629 {
630 *p = wca[i++];
631 ++gap.ga_len;
632 }
633 }
634 else if ((p - IObuff) + 6 >= IOSIZE)
635 {
636 // Multi-byte characters can occupy up to five bytes more than
637 // ASCII characters, and we also need one byte for NUL, so when
638 // getting to six bytes from the edge of IObuff switch to using a
639 // growarray. Add the character in the next round.
640 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100641 {
642 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100643 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100644 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100645 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100646 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100647 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100648 }
649 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000650 p += (*mb_char2bytes)(wca[i++], p);
651 else
652 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100653 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000654 vim_free(wca);
655
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100656 if (gap.ga_data != NULL)
657 {
658 *tofree = gap.ga_data;
659 return gap.ga_data;
660 }
661
662 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000663 return IObuff;
664}
665
666/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100667 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
668 * case of the originally typed text is used, and the case of the completed
669 * text is inferred, ie this tries to work out what case you probably wanted
670 * the rest of the word to be in -- webb
671 */
672 int
673ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200674 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100675 int len,
676 int icase,
677 char_u *fname,
678 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200679 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100680{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200681 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100683 int char_len; // count multi-byte characters
684 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100685 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200686 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100687 int res;
688 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100689
690 if (p_ic && curbuf->b_p_inf && len > 0)
691 {
692 // Infer case of completed part.
693
694 // Find actual length of completion.
695 if (has_mbyte)
696 {
697 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100698 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100699 while (*p != NUL)
700 {
701 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100703 }
704 }
705 else
glepnir6e199932024-12-14 21:13:27 +0100706 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100707 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100708 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709
710 // Find actual length of original text.
711 if (has_mbyte)
712 {
John Marriott5e6ea922024-11-23 14:01:57 +0100713 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 while (*p != NUL)
716 {
717 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719 }
720 }
721 else
glepnir6e199932024-12-14 21:13:27 +0100722 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100723 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100724 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100726 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100727 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100728 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100730 str = ins_compl_infercase_gettext(str, char_len,
731 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200733 if (cont_s_ipos)
734 flags |= CP_CONT_S_IPOS;
735 if (icase)
736 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200737
glepnir5c66e232024-11-15 19:58:27 +0100738 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100739 vim_free(tofree);
740 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100741}
742
743/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000744 * Add a match to the list of matches. The arguments are:
745 * str - text of the match to add
746 * len - length of "str". If -1, then the length of "str" is
747 * computed.
748 * fname - file name to associate with this match.
749 * cptext - list of strings to use with this match (for abbr, menu, info
750 * and kind)
751 * user_data - user supplied data (any vim type) for this match
752 * cdir - match direction. If 0, use "compl_direction".
753 * flags_arg - match flags (cp_flags)
754 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100755 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000756 * If "cdir" is FORWARD, then the match is added after the current match.
757 * Otherwise, it is added before the current match.
758 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100759 * If the given string is already in the list of completions, then return
760 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
761 * maybe because alloc() returns NULL, then FAIL is returned.
762 */
763 static int
764ins_compl_add(
765 char_u *str,
766 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100767 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 char_u **cptext, // extra text for popup menu or NULL
769 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100770 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200771 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200772 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100773 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774{
775 compl_T *match;
776 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200777 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100778
Bram Moolenaarceb06192021-04-04 15:05:22 +0200779 if (flags & CP_FAST)
780 fast_breakcheck();
781 else
782 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100783 if (got_int)
784 return FAIL;
785 if (len < 0)
786 len = (int)STRLEN(str);
787
788 // If the same match is already present, don't add it.
789 if (compl_first_match != NULL && !adup)
790 {
791 match = compl_first_match;
792 do
793 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000794 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100795 && STRNCMP(match->cp_str.string, str, len) == 0
796 && ((int)match->cp_str.length <= len
797 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100798 return NOTDONE;
799 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000800 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100801 }
802
803 // Remove any popup menu before changing the list of matches.
804 ins_compl_del_pum();
805
806 // Allocate a new match structure.
807 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200808 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100809 if (match == NULL)
810 return FAIL;
811 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200812 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100813 match->cp_number = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100814 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 {
816 vim_free(match);
817 return FAIL;
818 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819
John Marriott5e6ea922024-11-23 14:01:57 +0100820 match->cp_str.length = len;
821
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100822 // match-fname is:
823 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200824 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100825 // - NULL otherwise. --Acevedo
826 if (fname != NULL
827 && compl_curr_match != NULL
828 && compl_curr_match->cp_fname != NULL
829 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
830 match->cp_fname = compl_curr_match->cp_fname;
831 else if (fname != NULL)
832 {
833 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200834 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100835 }
836 else
837 match->cp_fname = NULL;
838 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100839 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
840 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100841
842 if (cptext != NULL)
843 {
844 int i;
845
846 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100847 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100848 if (cptext[i] != NULL && *cptext[i] != NUL)
849 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100850 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100851 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100852#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100853 if (user_data != NULL)
854 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000857 // Link the new match structure after (FORWARD) or before (BACKWARD) the
858 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 if (compl_first_match == NULL)
860 match->cp_next = match->cp_prev = NULL;
861 else if (dir == FORWARD)
862 {
863 match->cp_next = compl_curr_match->cp_next;
864 match->cp_prev = compl_curr_match;
865 }
866 else // BACKWARD
867 {
868 match->cp_next = compl_curr_match;
869 match->cp_prev = compl_curr_match->cp_prev;
870 }
871 if (match->cp_next)
872 match->cp_next->cp_prev = match;
873 if (match->cp_prev)
874 match->cp_prev->cp_next = match;
875 else // if there's nothing before, it is the first match
876 compl_first_match = match;
877 compl_curr_match = match;
878
879 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200880 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100881 ins_compl_longest_match(match);
882
883 return OK;
884}
885
886/*
887 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200888 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 */
890 static int
891ins_compl_equal(compl_T *match, char_u *str, int len)
892{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200893 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200894 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100896 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
897 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100898}
899
900/*
901 * Reduce the longest common string for match "match".
902 */
903 static void
904ins_compl_longest_match(compl_T *match)
905{
906 char_u *p, *s;
907 int c1, c2;
908 int had_match;
909
John Marriott5e6ea922024-11-23 14:01:57 +0100910 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100911 {
912 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100913 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
914 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000915 return;
916
John Marriott5e6ea922024-11-23 14:01:57 +0100917 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000918 had_match = (curwin->w_cursor.col > compl_col);
919 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +0100920 ins_bytes(compl_leader.string + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000921 ins_redraw(FALSE);
922
923 // When the match isn't there (to avoid matching itself) remove it
924 // again after redrawing.
925 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100926 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100927 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000928
929 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000931
932 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100933 p = compl_leader.string;
934 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000935 while (*p != NUL)
936 {
937 if (has_mbyte)
938 {
939 c1 = mb_ptr2char(p);
940 c2 = mb_ptr2char(s);
941 }
942 else
943 {
944 c1 = *p;
945 c2 = *s;
946 }
947 if ((match->cp_flags & CP_ICASE)
948 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
949 break;
950 if (has_mbyte)
951 {
952 MB_PTR_ADV(p);
953 MB_PTR_ADV(s);
954 }
955 else
956 {
957 ++p;
958 ++s;
959 }
960 }
961
962 if (*p != NUL)
963 {
964 // Leader was shortened, need to change the inserted text.
965 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +0100966 compl_leader.length = (size_t)(p - compl_leader.string);
967
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000968 had_match = (curwin->w_cursor.col > compl_col);
969 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +0100970 ins_bytes(compl_leader.string + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000971 ins_redraw(FALSE);
972
973 // When the match isn't there (to avoid matching itself) remove it
974 // again after redrawing.
975 if (!had_match)
976 ins_compl_delete();
977 }
978
979 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100980}
981
982/*
983 * Add an array of matches to the list of matches.
984 * Frees matches[].
985 */
986 static void
987ins_compl_add_matches(
988 int num_matches,
989 char_u **matches,
990 int icase)
991{
992 int i;
993 int add_r = OK;
994 int dir = compl_direction;
995
996 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +0100997 {
998 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
999 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1000 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001001 // if dir was BACKWARD then honor it just once
1002 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001003 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001004 FreeWild(num_matches, matches);
1005}
1006
1007/*
1008 * Make the completion list cyclic.
1009 * Return the number of matches (excluding the original).
1010 */
1011 static int
1012ins_compl_make_cyclic(void)
1013{
1014 compl_T *match;
1015 int count = 0;
1016
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001017 if (compl_first_match == NULL)
1018 return 0;
1019
1020 // Find the end of the list.
1021 match = compl_first_match;
1022 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001023 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001024 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001025 match = match->cp_next;
1026 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001027 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001028 match->cp_next = compl_first_match;
1029 compl_first_match->cp_prev = match;
1030
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001031 return count;
1032}
1033
1034/*
1035 * Return whether there currently is a shown match.
1036 */
1037 int
1038ins_compl_has_shown_match(void)
1039{
1040 return compl_shown_match == NULL
1041 || compl_shown_match != compl_shown_match->cp_next;
1042}
1043
1044/*
1045 * Return whether the shown match is long enough.
1046 */
1047 int
1048ins_compl_long_shown_match(void)
1049{
John Marriott5e6ea922024-11-23 14:01:57 +01001050 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001051 > curwin->w_cursor.col - compl_col;
1052}
1053
1054/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001055 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001056 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001057 unsigned int
1058get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001059{
zeertzjq529b9ad2024-06-05 20:27:06 +02001060 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001061}
1062
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001063
1064// "compl_match_array" points the currently displayed list of entries in the
1065// popup menu. It is NULL when there is no popup menu.
1066static pumitem_T *compl_match_array = NULL;
1067static int compl_match_arraysize;
1068
1069/*
1070 * Update the screen and when there is any scrolling remove the popup menu.
1071 */
1072 static void
1073ins_compl_upd_pum(void)
1074{
1075 int h;
1076
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001077 if (compl_match_array == NULL)
1078 return;
1079
1080 h = curwin->w_cline_height;
1081 // Update the screen later, before drawing the popup menu over it.
1082 pum_call_update_screen();
1083 if (h != curwin->w_cline_height)
1084 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001085}
1086
1087/*
1088 * Remove any popup menu.
1089 */
1090 static void
1091ins_compl_del_pum(void)
1092{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001093 if (compl_match_array == NULL)
1094 return;
1095
1096 pum_undisplay();
1097 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001098}
1099
1100/*
1101 * Return TRUE if the popup menu should be displayed.
1102 */
1103 int
1104pum_wanted(void)
1105{
1106 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001107 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001108 return FALSE;
1109
1110 // The display looks bad on a B&W display.
1111 if (t_colors < 8
1112#ifdef FEAT_GUI
1113 && !gui.in_use
1114#endif
1115 )
1116 return FALSE;
1117 return TRUE;
1118}
1119
1120/*
1121 * Return TRUE if there are two or more matches to be shown in the popup menu.
1122 * One if 'completopt' contains "menuone".
1123 */
1124 static int
1125pum_enough_matches(void)
1126{
1127 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001128 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001129
1130 // Don't display the popup menu if there are no matches or there is only
1131 // one (ignoring the original text).
1132 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001133 do
1134 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001135 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001136 break;
1137 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001138 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001139
zeertzjq529b9ad2024-06-05 20:27:06 +02001140 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001141 return (i >= 1);
1142 return (i >= 2);
1143}
1144
Bram Moolenaar3075a452021-11-17 15:51:52 +00001145#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001146/*
1147 * Allocate Dict for the completed item.
1148 * { word, abbr, menu, kind, info }
1149 */
1150 static dict_T *
1151ins_compl_dict_alloc(compl_T *match)
1152{
1153 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1154
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001155 if (dict == NULL)
1156 return NULL;
1157
John Marriott5e6ea922024-11-23 14:01:57 +01001158 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001159 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1160 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1161 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1162 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1163 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1164 dict_add_string(dict, "user_data", (char_u *)"");
1165 else
1166 dict_add_tv(dict, "user_data", &match->cp_user_data);
1167
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001168 return dict;
1169}
1170
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001171/*
1172 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1173 * completion menu is changed.
1174 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001175 static void
1176trigger_complete_changed_event(int cur)
1177{
1178 dict_T *v_event;
1179 dict_T *item;
1180 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001181 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001182
1183 if (recursive)
1184 return;
1185
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001186 if (cur < 0)
1187 item = dict_alloc();
1188 else
1189 item = ins_compl_dict_alloc(compl_curr_match);
1190 if (item == NULL)
1191 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001192 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001193 dict_add_dict(v_event, "completed_item", item);
1194 pum_set_event_info(v_event);
1195 dict_set_items_ro(v_event);
1196
1197 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001198 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001199 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001200 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 recursive = FALSE;
1202
Bram Moolenaar3075a452021-11-17 15:51:52 +00001203 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001204}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001205#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001206
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001207/*
glepnira218cc62024-06-03 19:32:39 +02001208 * pumitem qsort compare func
1209 */
1210 static int
zeertzjq8e567472024-06-14 20:04:42 +02001211ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001212{
1213 const int sa = (*(pumitem_T *)a).pum_score;
1214 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001215 const int ia = (*(pumitem_T *)a).pum_idx;
1216 const int ib = (*(pumitem_T *)b).pum_idx;
1217 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001218}
1219
1220/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001221 * Build a popup menu to show the completion matches.
1222 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1223 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001224 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001225 static int
1226ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001227{
1228 compl_T *compl;
1229 compl_T *shown_compl = NULL;
1230 int did_find_shown_match = FALSE;
1231 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001232 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001233 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001234 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001235 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001236 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1237 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001238 compl_T *match_head = NULL;
1239 compl_T *match_tail = NULL;
1240 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001241
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001242 // Need to build the popup menu list.
1243 compl_match_arraysize = 0;
1244 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001245
glepnira49c0772024-11-30 10:56:30 +01001246 // If the current match is the original text don't find the first
1247 // match after it, don't highlight anything.
1248 if (match_at_original_text(compl_shown_match))
1249 shown_match_ok = TRUE;
1250
1251 if (compl_leader.string != NULL
1252 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1253 && shown_match_ok == FALSE)
1254 compl_shown_match = compl_no_select ? compl_first_match
1255 : compl_first_match->cp_next;
1256
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001257 do
1258 {
zeertzjq551d8c32024-06-05 19:53:32 +02001259 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1260 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001261 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1262 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001263
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001264 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001265 && (compl_leader.string == NULL
1266 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001267 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001268 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001269 ++compl_match_arraysize;
glepnir80b66202024-11-27 21:53:53 +01001270 if (match_head == NULL)
1271 match_head = compl;
1272 else
glepnira49c0772024-11-30 10:56:30 +01001273 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001274 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001275
1276 if (!shown_match_ok && !compl_fuzzy_match)
1277 {
1278 if (compl == compl_shown_match || did_find_shown_match)
1279 {
1280 // This item is the shown match or this is the
1281 // first displayed item after the shown match.
1282 compl_shown_match = compl;
1283 did_find_shown_match = TRUE;
1284 shown_match_ok = TRUE;
1285 }
1286 else
1287 // Remember this displayed match for when the
1288 // shown match is just below it.
1289 shown_compl = compl;
1290 cur = i;
1291 }
1292 else if (compl_fuzzy_match)
1293 {
1294 if (i == 0)
1295 shown_compl = compl;
1296 // Update the maximum fuzzy score and the shown match
1297 // if the current item's score is higher
1298 if (compl->cp_score > max_fuzzy_score)
1299 {
1300 did_find_shown_match = TRUE;
1301 max_fuzzy_score = compl->cp_score;
1302 if (!compl_no_select)
1303 compl_shown_match = compl;
1304 }
1305
1306 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1307 {
1308 cur = i;
1309 shown_match_ok = TRUE;
1310 }
1311 }
1312 i++;
glepnir80b66202024-11-27 21:53:53 +01001313 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001314
glepnira218cc62024-06-03 19:32:39 +02001315 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001316 {
1317 did_find_shown_match = TRUE;
1318
1319 // When the original text is the shown match don't set
1320 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001321 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001322 shown_match_ok = TRUE;
1323
1324 if (!shown_match_ok && shown_compl != NULL)
1325 {
1326 // The shown match isn't displayed, set it to the
1327 // previously displayed match.
1328 compl_shown_match = shown_compl;
1329 shown_match_ok = TRUE;
1330 }
1331 }
glepnira49c0772024-11-30 10:56:30 +01001332 compl = compl->cp_next;
1333 } while (compl != NULL && !is_first_match(compl));
1334
1335 if (compl_match_arraysize == 0)
1336 return -1;
1337
1338 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1339 if (compl_match_array == NULL)
1340 return -1;
1341
1342 compl = match_head;
1343 i = 0;
1344 while (compl != NULL)
1345 {
glepnir6e199932024-12-14 21:13:27 +01001346 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1347 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001348 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1349 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1350 compl_match_array[i].pum_score = compl->cp_score;
1351 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1352 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001353 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1354 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001355 match_next = compl->cp_match_next;
1356 compl->cp_match_next = NULL;
1357 compl = match_next;
1358 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001359
John Marriott5e6ea922024-11-23 14:01:57 +01001360 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001361 {
1362 for (i = 0; i < compl_match_arraysize; i++)
1363 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001364 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001365 qsort(compl_match_array, (size_t)compl_match_arraysize,
1366 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001367 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001368 }
glepnira218cc62024-06-03 19:32:39 +02001369
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001370 if (!shown_match_ok) // no displayed match at all
1371 cur = -1;
1372
1373 return cur;
1374}
1375
1376/*
1377 * Show the popup menu for the list of matches.
1378 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1379 */
1380 void
1381ins_compl_show_pum(void)
1382{
1383 int i;
1384 int cur = -1;
1385 colnr_T col;
1386
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001387 if (!pum_wanted() || !pum_enough_matches())
1388 return;
1389
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001390 // Update the screen later, before drawing the popup menu over it.
1391 pum_call_update_screen();
1392
1393 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001394 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001395 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001396 else
1397 {
1398 // popup menu already exists, only need to find the current item.
1399 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001400 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001401 || compl_match_array[i].pum_text
1402 == compl_shown_match->cp_text[CPT_ABBR])
1403 {
1404 cur = i;
1405 break;
1406 }
1407 }
1408
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001409 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001410 {
1411#ifdef FEAT_EVAL
1412 if (compl_started && has_completechanged())
1413 trigger_complete_changed_event(cur);
1414#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001415 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001416 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001417
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001418 // In Replace mode when a $ is displayed at the end of the line only
1419 // part of the screen would be updated. We do need to redraw here.
1420 dollar_vcol = -1;
1421
1422 // Compute the screen column of the start of the completed text.
1423 // Use the cursor to get all wrapping and other settings right.
1424 col = curwin->w_cursor.col;
1425 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001426 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001427 pum_display(compl_match_array, compl_match_arraysize, cur);
1428 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001429
glepnircbb46b42024-02-03 18:11:13 +01001430 // After adding leader, set the current match to shown match.
1431 if (compl_started && compl_curr_match != compl_shown_match)
1432 compl_curr_match = compl_shown_match;
1433
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001434#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001435 if (has_completechanged())
1436 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001437#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001438}
1439
1440#define DICT_FIRST (1) // use just first element in "dict"
1441#define DICT_EXACT (2) // "dict" is the exact name of a file
1442
1443/*
glepnir40c1c332024-06-11 19:37:04 +02001444 * Get current completion leader
1445 */
1446 char_u *
1447ins_compl_leader(void)
1448{
John Marriott5e6ea922024-11-23 14:01:57 +01001449 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1450}
1451
1452/*
1453 * Get current completion leader length
1454 */
1455 size_t
1456ins_compl_leader_len(void)
1457{
1458 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001459}
1460
1461/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001462 * Add any identifiers that match the given pattern "pat" in the list of
1463 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001464 */
1465 static void
1466ins_compl_dictionaries(
1467 char_u *dict_start,
1468 char_u *pat,
1469 int flags, // DICT_FIRST and/or DICT_EXACT
1470 int thesaurus) // Thesaurus completion
1471{
1472 char_u *dict = dict_start;
1473 char_u *ptr;
1474 char_u *buf;
1475 regmatch_T regmatch;
1476 char_u **files;
1477 int count;
1478 int save_p_scs;
1479 int dir = compl_direction;
1480
1481 if (*dict == NUL)
1482 {
1483#ifdef FEAT_SPELL
1484 // When 'dictionary' is empty and spell checking is enabled use
1485 // "spell".
1486 if (!thesaurus && curwin->w_p_spell)
1487 dict = (char_u *)"spell";
1488 else
1489#endif
1490 return;
1491 }
1492
1493 buf = alloc(LSIZE);
1494 if (buf == NULL)
1495 return;
1496 regmatch.regprog = NULL; // so that we can goto theend
1497
1498 // If 'infercase' is set, don't use 'smartcase' here
1499 save_p_scs = p_scs;
1500 if (curbuf->b_p_inf)
1501 p_scs = FALSE;
1502
1503 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1504 // to only match at the start of a line. Otherwise just match the
1505 // pattern. Also need to double backslashes.
1506 if (ctrl_x_mode_line_or_eval())
1507 {
1508 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1509 size_t len;
1510
1511 if (pat_esc == NULL)
1512 goto theend;
1513 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001514 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001515 if (ptr == NULL)
1516 {
1517 vim_free(pat_esc);
1518 goto theend;
1519 }
1520 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1521 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1522 vim_free(pat_esc);
1523 vim_free(ptr);
1524 }
1525 else
1526 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001527 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001528 if (regmatch.regprog == NULL)
1529 goto theend;
1530 }
1531
1532 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1533 regmatch.rm_ic = ignorecase(pat);
1534 while (*dict != NUL && !got_int && !compl_interrupted)
1535 {
1536 // copy one dictionary file name into buf
1537 if (flags == DICT_EXACT)
1538 {
1539 count = 1;
1540 files = &dict;
1541 }
1542 else
1543 {
1544 // Expand wildcards in the dictionary name, but do not allow
1545 // backticks (for security, the 'dict' option may have been set in
1546 // a modeline).
1547 copy_option_part(&dict, buf, LSIZE, ",");
1548# ifdef FEAT_SPELL
1549 if (!thesaurus && STRCMP(buf, "spell") == 0)
1550 count = -1;
1551 else
1552# endif
1553 if (vim_strchr(buf, '`') != NULL
1554 || expand_wildcards(1, &buf, &count, &files,
1555 EW_FILE|EW_SILENT) != OK)
1556 count = 0;
1557 }
1558
1559# ifdef FEAT_SPELL
1560 if (count == -1)
1561 {
1562 // Complete from active spelling. Skip "\<" in the pattern, we
1563 // don't use it as a RE.
1564 if (pat[0] == '\\' && pat[1] == '<')
1565 ptr = pat + 2;
1566 else
1567 ptr = pat;
1568 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1569 }
1570 else
1571# endif
1572 if (count > 0) // avoid warning for using "files" uninit
1573 {
1574 ins_compl_files(count, files, thesaurus, flags,
1575 &regmatch, buf, &dir);
1576 if (flags != DICT_EXACT)
1577 FreeWild(count, files);
1578 }
1579 if (flags != 0)
1580 break;
1581 }
1582
1583theend:
1584 p_scs = save_p_scs;
1585 vim_regfree(regmatch.regprog);
1586 vim_free(buf);
1587}
1588
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001589/*
1590 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1591 * skipping the word at 'skip_word'. Returns OK on success.
1592 */
1593 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001594thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001595 char_u *fname,
1596 char_u **buf_arg,
1597 int dir,
1598 char_u *skip_word)
1599{
1600 int status = OK;
1601 char_u *ptr;
1602 char_u *wstart;
1603
1604 // Add the other matches on the line
1605 ptr = *buf_arg;
1606 while (!got_int)
1607 {
1608 // Find start of the next word. Skip white
1609 // space and punctuation.
1610 ptr = find_word_start(ptr);
1611 if (*ptr == NUL || *ptr == NL)
1612 break;
1613 wstart = ptr;
1614
1615 // Find end of the word.
1616 if (has_mbyte)
1617 // Japanese words may have characters in
1618 // different classes, only separate words
1619 // with single-byte non-word characters.
1620 while (*ptr != NUL)
1621 {
1622 int l = (*mb_ptr2len)(ptr);
1623
1624 if (l < 2 && !vim_iswordc(*ptr))
1625 break;
1626 ptr += l;
1627 }
1628 else
1629 ptr = find_word_end(ptr);
1630
1631 // Add the word. Skip the regexp match.
1632 if (wstart != skip_word)
1633 {
1634 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1635 fname, dir, FALSE);
1636 if (status == FAIL)
1637 break;
1638 }
1639 }
1640
1641 *buf_arg = ptr;
1642 return status;
1643}
1644
1645/*
1646 * Process "count" dictionary/thesaurus "files" and add the text matching
1647 * "regmatch".
1648 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001649 static void
1650ins_compl_files(
1651 int count,
1652 char_u **files,
1653 int thesaurus,
1654 int flags,
1655 regmatch_T *regmatch,
1656 char_u *buf,
1657 int *dir)
1658{
1659 char_u *ptr;
1660 int i;
1661 FILE *fp;
1662 int add_r;
1663
1664 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1665 {
1666 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001667 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001668 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001669 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001670 vim_snprintf((char *)IObuff, IOSIZE,
1671 _("Scanning dictionary: %s"), (char *)files[i]);
1672 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1673 }
1674
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001675 if (fp == NULL)
1676 continue;
1677
1678 // Read dictionary file line by line.
1679 // Check each line for a match.
1680 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001682 ptr = buf;
1683 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001685 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001686 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1687 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001688 add_r = ins_compl_add_infercase(regmatch->startp[0],
1689 (int)(ptr - regmatch->startp[0]),
1690 p_ic, files[i], *dir, FALSE);
1691 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001692 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001693 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001694 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001695 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001696 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001697 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001698 if (add_r == OK)
1699 // if dir was BACKWARD then honor it just once
1700 *dir = FORWARD;
1701 else if (add_r == FAIL)
1702 break;
1703 // avoid expensive call to vim_regexec() when at end
1704 // of line
1705 if (*ptr == '\n' || got_int)
1706 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001708 line_breakcheck();
1709 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001710 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001711 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001712 }
1713}
1714
1715/*
1716 * Find the start of the next word.
1717 * Returns a pointer to the first char of the word. Also stops at a NUL.
1718 */
1719 char_u *
1720find_word_start(char_u *ptr)
1721{
1722 if (has_mbyte)
1723 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1724 ptr += (*mb_ptr2len)(ptr);
1725 else
1726 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1727 ++ptr;
1728 return ptr;
1729}
1730
1731/*
1732 * Find the end of the word. Assumes it starts inside a word.
1733 * Returns a pointer to just after the word.
1734 */
1735 char_u *
1736find_word_end(char_u *ptr)
1737{
1738 int start_class;
1739
1740 if (has_mbyte)
1741 {
1742 start_class = mb_get_class(ptr);
1743 if (start_class > 1)
1744 while (*ptr != NUL)
1745 {
1746 ptr += (*mb_ptr2len)(ptr);
1747 if (mb_get_class(ptr) != start_class)
1748 break;
1749 }
1750 }
1751 else
1752 while (vim_iswordc(*ptr))
1753 ++ptr;
1754 return ptr;
1755}
1756
1757/*
1758 * Find the end of the line, omitting CR and NL at the end.
1759 * Returns a pointer to just after the line.
1760 */
1761 static char_u *
1762find_line_end(char_u *ptr)
1763{
1764 char_u *s;
1765
1766 s = ptr + STRLEN(ptr);
1767 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1768 --s;
1769 return s;
1770}
1771
1772/*
1773 * Free the list of completions
1774 */
1775 static void
1776ins_compl_free(void)
1777{
1778 compl_T *match;
1779 int i;
1780
John Marriott5e6ea922024-11-23 14:01:57 +01001781 VIM_CLEAR_STRING(compl_pattern);
1782 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001783
1784 if (compl_first_match == NULL)
1785 return;
1786
1787 ins_compl_del_pum();
1788 pum_clear();
1789
1790 compl_curr_match = compl_first_match;
1791 do
1792 {
1793 match = compl_curr_match;
1794 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001795 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001796 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001797 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001798 vim_free(match->cp_fname);
1799 for (i = 0; i < CPT_COUNT; ++i)
1800 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001801#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001802 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001803#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001804 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001805 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001806 compl_first_match = compl_curr_match = NULL;
1807 compl_shown_match = NULL;
1808 compl_old_match = NULL;
1809}
1810
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001811/*
1812 * Reset/clear the completion state.
1813 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001814 void
1815ins_compl_clear(void)
1816{
1817 compl_cont_status = 0;
1818 compl_started = FALSE;
1819 compl_matches = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001820 VIM_CLEAR_STRING(compl_pattern);
1821 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001822 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001823 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001824 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001825#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001826 // clear v:completed_item
1827 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001828#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829}
1830
1831/*
1832 * Return TRUE when Insert completion is active.
1833 */
1834 int
1835ins_compl_active(void)
1836{
1837 return compl_started;
1838}
1839
1840/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001841 * Selected one of the matches. When FALSE the match was edited or using the
1842 * longest common string.
1843 */
1844 int
1845ins_compl_used_match(void)
1846{
1847 return compl_used_match;
1848}
1849
1850/*
1851 * Initialize get longest common string.
1852 */
1853 void
1854ins_compl_init_get_longest(void)
1855{
1856 compl_get_longest = FALSE;
1857}
1858
1859/*
1860 * Returns TRUE when insert completion is interrupted.
1861 */
1862 int
1863ins_compl_interrupted(void)
1864{
1865 return compl_interrupted;
1866}
1867
1868/*
1869 * Returns TRUE if the <Enter> key selects a match in the completion popup
1870 * menu.
1871 */
1872 int
1873ins_compl_enter_selects(void)
1874{
1875 return compl_enter_selects;
1876}
1877
1878/*
1879 * Return the column where the text starts that is being completed
1880 */
1881 colnr_T
1882ins_compl_col(void)
1883{
1884 return compl_col;
1885}
1886
1887/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001888 * Return the length in bytes of the text being completed
1889 */
1890 int
1891ins_compl_len(void)
1892{
1893 return compl_length;
1894}
1895
1896/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001897 * Delete one character before the cursor and show the subset of the matches
1898 * that match the word that is now before the cursor.
1899 * Returns the character to be used, NUL if the work is done and another char
1900 * to be got from the user.
1901 */
1902 int
1903ins_compl_bs(void)
1904{
1905 char_u *line;
1906 char_u *p;
1907
1908 line = ml_get_curline();
1909 p = line + curwin->w_cursor.col;
1910 MB_PTR_BACK(line, p);
1911
1912 // Stop completion when the whole word was deleted. For Omni completion
1913 // allow the word to be deleted, we won't match everything.
1914 // Respect the 'backspace' option.
1915 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001916 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1917 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001918 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1919 - compl_length < 0))
1920 return K_BS;
1921
1922 // Deleted more than what was used to find matches or didn't finish
1923 // finding all matches: need to look for matches all over again.
1924 if (curwin->w_cursor.col <= compl_col + compl_length
1925 || ins_compl_need_restart())
1926 ins_compl_restart();
1927
John Marriott5e6ea922024-11-23 14:01:57 +01001928 VIM_CLEAR_STRING(compl_leader);
1929 compl_leader.length = (size_t)((p - line) - compl_col);
1930 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1931 if (compl_leader.string == NULL)
1932 {
1933 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001934 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001935 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001936
1937 ins_compl_new_leader();
1938 if (compl_shown_match != NULL)
1939 // Make sure current match is not a hidden item.
1940 compl_curr_match = compl_shown_match;
1941 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001942}
1943
1944/*
1945 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1946 * be called.
1947 */
1948 static int
1949ins_compl_need_restart(void)
1950{
1951 // Return TRUE if we didn't complete finding matches or when the
1952 // 'completefunc' returned "always" in the "refresh" dictionary item.
1953 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001954 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001955 && compl_opt_refresh_always);
1956}
1957
1958/*
1959 * Called after changing "compl_leader".
1960 * Show the popup menu with a different set of matches.
1961 * May also search for matches again if the previous search was interrupted.
1962 */
1963 static void
1964ins_compl_new_leader(void)
1965{
1966 ins_compl_del_pum();
1967 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01001968 ins_bytes(compl_leader.string + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969 compl_used_match = FALSE;
1970
1971 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01001972 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001973 else
1974 {
1975#ifdef FEAT_SPELL
1976 spell_bad_len = 0; // need to redetect bad word
1977#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001978 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001979 // the popup menu display the changed text before the cursor. Set
1980 // "compl_restarting" to avoid that the first match is inserted.
1981 pum_call_update_screen();
1982#ifdef FEAT_GUI
1983 if (gui.in_use)
1984 {
1985 // Show the cursor after the match, not after the redrawn text.
1986 setcursor();
1987 out_flush_cursor(FALSE, FALSE);
1988 }
1989#endif
1990 compl_restarting = TRUE;
1991 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1992 compl_cont_status = 0;
1993 compl_restarting = FALSE;
1994 }
1995
1996 compl_enter_selects = !compl_used_match;
1997
1998 // Show the popup menu with a different set of matches.
1999 ins_compl_show_pum();
2000
2001 // Don't let Enter select the original text when there is no popup menu.
2002 if (compl_match_array == NULL)
2003 compl_enter_selects = FALSE;
2004}
2005
2006/*
2007 * Return the length of the completion, from the completion start column to
2008 * the cursor column. Making sure it never goes below zero.
2009 */
2010 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002011get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002012{
2013 int off = (int)curwin->w_cursor.col - (int)compl_col;
2014
2015 if (off < 0)
2016 return 0;
2017 return off;
2018}
2019
2020/*
2021 * Append one character to the match leader. May reduce the number of
2022 * matches.
2023 */
2024 void
2025ins_compl_addleader(int c)
2026{
2027 int cc;
2028
2029 if (stop_arrow() == FAIL)
2030 return;
2031 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2032 {
2033 char_u buf[MB_MAXBYTES + 1];
2034
2035 (*mb_char2bytes)(c, buf);
2036 buf[cc] = NUL;
2037 ins_char_bytes(buf, cc);
2038 if (compl_opt_refresh_always)
2039 AppendToRedobuff(buf);
2040 }
2041 else
2042 {
2043 ins_char(c);
2044 if (compl_opt_refresh_always)
2045 AppendCharToRedobuff(c);
2046 }
2047
2048 // If we didn't complete finding matches we must search again.
2049 if (ins_compl_need_restart())
2050 ins_compl_restart();
2051
2052 // When 'always' is set, don't reset compl_leader. While completing,
2053 // cursor doesn't point original position, changing compl_leader would
2054 // break redo.
2055 if (!compl_opt_refresh_always)
2056 {
John Marriott5e6ea922024-11-23 14:01:57 +01002057 VIM_CLEAR_STRING(compl_leader);
2058 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2059 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2060 compl_leader.length);
2061 if (compl_leader.string == NULL)
2062 {
2063 compl_leader.length = 0;
2064 return;
2065 }
2066
2067 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002068 }
2069}
2070
2071/*
2072 * Setup for finding completions again without leaving CTRL-X mode. Used when
2073 * BS or a key was typed while still searching for matches.
2074 */
2075 static void
2076ins_compl_restart(void)
2077{
2078 ins_compl_free();
2079 compl_started = FALSE;
2080 compl_matches = 0;
2081 compl_cont_status = 0;
2082 compl_cont_mode = 0;
2083}
2084
2085/*
2086 * Set the first match, the original text.
2087 */
2088 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002089ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002090{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002091 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002092 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2093 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002094 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002095 {
John Marriott5e6ea922024-11-23 14:01:57 +01002096 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002097 if (p != NULL)
2098 {
John Marriott5e6ea922024-11-23 14:01:57 +01002099 VIM_CLEAR_STRING(compl_first_match->cp_str);
2100 compl_first_match->cp_str.string = p;
2101 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002102 }
2103 }
2104 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002105 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002106 {
John Marriott5e6ea922024-11-23 14:01:57 +01002107 char_u *p = vim_strnsave(str, len);
2108 if (p != NULL)
2109 {
2110 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2111 compl_first_match->cp_prev->cp_str.string = p;
2112 compl_first_match->cp_prev->cp_str.length = len;
2113 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002114 }
2115}
2116
2117/*
2118 * Append one character to the match leader. May reduce the number of
2119 * matches.
2120 */
2121 void
2122ins_compl_addfrommatch(void)
2123{
2124 char_u *p;
2125 int len = (int)curwin->w_cursor.col - (int)compl_col;
2126 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002127
John Marriott5e6ea922024-11-23 14:01:57 +01002128 p = compl_shown_match->cp_str.string;
2129 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002130 {
John Marriott5e6ea922024-11-23 14:01:57 +01002131 size_t plen;
2132 compl_T *cp;
2133
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002134 // When still at the original match use the first entry that matches
2135 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002136 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002137 return;
2138
2139 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002140 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002141 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002142 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002143 {
John Marriott5e6ea922024-11-23 14:01:57 +01002144 if (compl_leader.string == NULL
2145 || ins_compl_equal(cp, compl_leader.string,
2146 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002147 {
John Marriott5e6ea922024-11-23 14:01:57 +01002148 p = cp->cp_str.string;
2149 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002150 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002151 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002152 }
John Marriott5e6ea922024-11-23 14:01:57 +01002153 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002154 return;
2155 }
2156 p += len;
2157 c = PTR2CHAR(p);
2158 ins_compl_addleader(c);
2159}
2160
2161/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002162 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002163 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2164 * compl_cont_mode and compl_cont_status.
2165 * Returns TRUE when the character is not to be inserted.
2166 */
2167 static int
2168set_ctrl_x_mode(int c)
2169{
2170 int retval = FALSE;
2171
2172 switch (c)
2173 {
2174 case Ctrl_E:
2175 case Ctrl_Y:
2176 // scroll the window one line up or down
2177 ctrl_x_mode = CTRL_X_SCROLL;
2178 if (!(State & REPLACE_FLAG))
2179 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2180 else
2181 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2182 edit_submode_pre = NULL;
2183 showmode();
2184 break;
2185 case Ctrl_L:
2186 // complete whole line
2187 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2188 break;
2189 case Ctrl_F:
2190 // complete filenames
2191 ctrl_x_mode = CTRL_X_FILES;
2192 break;
2193 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002194 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002195 ctrl_x_mode = CTRL_X_DICTIONARY;
2196 break;
2197 case Ctrl_R:
2198 // Register insertion without exiting CTRL-X mode
2199 // Simply allow ^R to happen without affecting ^X mode
2200 break;
2201 case Ctrl_T:
2202 // complete words from a thesaurus
2203 ctrl_x_mode = CTRL_X_THESAURUS;
2204 break;
2205#ifdef FEAT_COMPL_FUNC
2206 case Ctrl_U:
2207 // user defined completion
2208 ctrl_x_mode = CTRL_X_FUNCTION;
2209 break;
2210 case Ctrl_O:
2211 // omni completion
2212 ctrl_x_mode = CTRL_X_OMNI;
2213 break;
2214#endif
2215 case 's':
2216 case Ctrl_S:
2217 // complete spelling suggestions
2218 ctrl_x_mode = CTRL_X_SPELL;
2219#ifdef FEAT_SPELL
2220 ++emsg_off; // Avoid getting the E756 error twice.
2221 spell_back_to_badword();
2222 --emsg_off;
2223#endif
2224 break;
2225 case Ctrl_RSB:
2226 // complete tag names
2227 ctrl_x_mode = CTRL_X_TAGS;
2228 break;
2229#ifdef FEAT_FIND_ID
2230 case Ctrl_I:
2231 case K_S_TAB:
2232 // complete keywords from included files
2233 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2234 break;
2235 case Ctrl_D:
2236 // complete definitions from included files
2237 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2238 break;
2239#endif
2240 case Ctrl_V:
2241 case Ctrl_Q:
2242 // complete vim commands
2243 ctrl_x_mode = CTRL_X_CMDLINE;
2244 break;
2245 case Ctrl_Z:
2246 // stop completion
2247 ctrl_x_mode = CTRL_X_NORMAL;
2248 edit_submode = NULL;
2249 showmode();
2250 retval = TRUE;
2251 break;
2252 case Ctrl_P:
2253 case Ctrl_N:
2254 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2255 // just started ^X mode, or there were enough ^X's to cancel
2256 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2257 // do normal expansion when interrupting a different mode (say
2258 // ^X^F^X^P or ^P^X^X^P, see below)
2259 // nothing changes if interrupting mode 0, (eg, the flag
2260 // doesn't change when going to ADDING mode -- Acevedo
2261 if (!(compl_cont_status & CONT_INTRPT))
2262 compl_cont_status |= CONT_LOCAL;
2263 else if (compl_cont_mode != 0)
2264 compl_cont_status &= ~CONT_LOCAL;
2265 // FALLTHROUGH
2266 default:
2267 // If we have typed at least 2 ^X's... for modes != 0, we set
2268 // compl_cont_status = 0 (eg, as if we had just started ^X
2269 // mode).
2270 // For mode 0, we set "compl_cont_mode" to an impossible
2271 // value, in both cases ^X^X can be used to restart the same
2272 // mode (avoiding ADDING mode).
2273 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2274 // 'complete' and local ^P expansions respectively.
2275 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2276 // mode -- Acevedo
2277 if (c == Ctrl_X)
2278 {
2279 if (compl_cont_mode != 0)
2280 compl_cont_status = 0;
2281 else
2282 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2283 }
2284 ctrl_x_mode = CTRL_X_NORMAL;
2285 edit_submode = NULL;
2286 showmode();
2287 break;
2288 }
2289
2290 return retval;
2291}
2292
2293/*
glepnir1c5a1202024-12-04 20:27:34 +01002294 * Trigger CompleteDone event and adds relevant information to v:event
2295 */
2296 static void
2297trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2298{
2299#if defined(FEAT_EVAL)
2300 save_v_event_T save_v_event;
2301 dict_T *v_event = get_v_event(&save_v_event);
2302 char_u *mode_str = NULL;
2303
2304 mode = mode & ~CTRL_X_WANT_IDENT;
2305 if (ctrl_x_mode_names[mode])
2306 mode_str = (char_u *)ctrl_x_mode_names[mode];
2307
2308 (void)dict_add_string(v_event, "complete_word",
2309 word == NULL ? (char_u *)"" : word);
2310 (void)dict_add_string(v_event, "complete_type",
2311 mode_str != NULL ? mode_str : (char_u *)"");
2312
2313 dict_set_items_ro(v_event);
2314#endif
2315 ins_apply_autocmds(EVENT_COMPLETEDONE);
2316
2317#if defined(FEAT_EVAL)
2318 restore_v_event(v_event, &save_v_event);
2319#endif
2320}
2321
2322/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002323 * Stop insert completion mode
2324 */
2325 static int
2326ins_compl_stop(int c, int prev_mode, int retval)
2327{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002328 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002329 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002330
2331 // Get here when we have finished typing a sequence of ^N and
2332 // ^P or other completion characters in CTRL-X mode. Free up
2333 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002334 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002335 {
John Marriott5e6ea922024-11-23 14:01:57 +01002336 char_u *ptr;
2337
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002338 // If any of the original typed text has been changed, eg when
2339 // ignorecase is set, we must add back-spaces to the redo
2340 // buffer. We add as few as necessary to delete just the part
2341 // of the original text that has changed.
2342 // When using the longest match, edited the match or used
2343 // CTRL-E then don't use the current match.
2344 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002345 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002346 else
2347 ptr = NULL;
2348 ins_compl_fixRedoBufForLeader(ptr);
2349 }
2350
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002351 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002352
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002353 // When completing whole lines: fix indent for 'cindent'.
2354 // Otherwise, break line if it's too long.
2355 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2356 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002357 // re-indent the current line
2358 if (want_cindent)
2359 {
2360 do_c_expr_indent();
2361 want_cindent = FALSE; // don't do it again
2362 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002363 }
2364 else
2365 {
2366 int prev_col = curwin->w_cursor.col;
2367
2368 // put the cursor on the last char, for 'tw' formatting
2369 if (prev_col > 0)
2370 dec_cursor();
2371 // only format when something was inserted
2372 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2373 insertchar(NUL, 0, -1);
2374 if (prev_col > 0
2375 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2376 inc_cursor();
2377 }
2378
2379 // If the popup menu is displayed pressing CTRL-Y means accepting
2380 // the selection without inserting anything. When
2381 // compl_enter_selects is set the Enter key does the same.
2382 if ((c == Ctrl_Y || (compl_enter_selects
2383 && (c == CAR || c == K_KENTER || c == NL)))
2384 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002385 {
2386 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002387 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002388 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002389
2390 // CTRL-E means completion is Ended, go back to the typed text.
2391 // but only do this, if the Popup is still visible
2392 if (c == Ctrl_E)
2393 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002394 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002395 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002396
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002397 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002398 if (compl_leader.string != NULL)
2399 {
2400 p = compl_leader.string;
2401 plen = compl_leader.length;
2402 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002403 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002404 {
2405 p = compl_orig_text.string;
2406 plen = compl_orig_text.length;
2407 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002408 if (p != NULL)
2409 {
2410 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002411
John Marriott5e6ea922024-11-23 14:01:57 +01002412 if ((int)plen > compl_len)
2413 ins_bytes_len(p + compl_len, (int)(plen - compl_len));
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002414 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002415 retval = TRUE;
2416 }
2417
2418 auto_format(FALSE, TRUE);
2419
2420 // Trigger the CompleteDonePre event to give scripts a chance to
2421 // act upon the completion before clearing the info, and restore
2422 // ctrl_x_mode, so that complete_info() can be used.
2423 ctrl_x_mode = prev_mode;
2424 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2425
2426 ins_compl_free();
2427 compl_started = FALSE;
2428 compl_matches = 0;
2429 if (!shortmess(SHM_COMPLETIONMENU))
2430 msg_clr_cmdline(); // necessary for "noshowmode"
2431 ctrl_x_mode = CTRL_X_NORMAL;
2432 compl_enter_selects = FALSE;
2433 if (edit_submode != NULL)
2434 {
2435 edit_submode = NULL;
2436 showmode();
2437 }
2438
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002439 if (c == Ctrl_C && cmdwin_type != 0)
2440 // Avoid the popup menu remains displayed when leaving the
2441 // command line window.
2442 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002443 // Indent now if a key was typed that is in 'cinkeys'.
2444 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2445 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002446 // Trigger the CompleteDone event to give scripts a chance to act
2447 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002448 trigger_complete_done_event(prev_mode, word);
2449 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002450
2451 return retval;
2452}
2453
2454/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002455 * Prepare for Insert mode completion, or stop it.
2456 * Called just after typing a character in Insert mode.
2457 * Returns TRUE when the character is not to be inserted;
2458 */
2459 int
2460ins_compl_prep(int c)
2461{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002462 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002463 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002464
2465 // Forget any previous 'special' messages if this is actually
2466 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2467 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2468 edit_submode_extra = NULL;
2469
zeertzjq440d4cb2023-03-02 17:51:32 +00002470 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002471 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002472 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002473 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002474 return retval;
2475
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002476#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002477 // Ignore mouse events in a popup window
2478 if (is_mouse_key(c))
2479 {
2480 // Ignore drag and release events, the position does not need to be in
2481 // the popup and it may have just closed.
2482 if (c == K_LEFTRELEASE
2483 || c == K_LEFTRELEASE_NM
2484 || c == K_MIDDLERELEASE
2485 || c == K_RIGHTRELEASE
2486 || c == K_X1RELEASE
2487 || c == K_X2RELEASE
2488 || c == K_LEFTDRAG
2489 || c == K_MIDDLEDRAG
2490 || c == K_RIGHTDRAG
2491 || c == K_X1DRAG
2492 || c == K_X2DRAG)
2493 return retval;
2494 if (popup_visible)
2495 {
2496 int row = mouse_row;
2497 int col = mouse_col;
2498 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2499
2500 if (wp != NULL && WIN_IS_POPUP(wp))
2501 return retval;
2502 }
2503 }
2504#endif
2505
zeertzjqdca29d92021-08-31 19:12:51 +02002506 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2507 {
2508 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2509 || !vim_is_ctrl_x_key(c))
2510 {
2511 // Not starting another completion mode.
2512 ctrl_x_mode = CTRL_X_CMDLINE;
2513
2514 // CTRL-X CTRL-Z should stop completion without inserting anything
2515 if (c == Ctrl_Z)
2516 retval = TRUE;
2517 }
2518 else
2519 {
2520 ctrl_x_mode = CTRL_X_CMDLINE;
2521
2522 // Other CTRL-X keys first stop completion, then start another
2523 // completion mode.
2524 ins_compl_prep(' ');
2525 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2526 }
2527 }
2528
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002529 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002530 if (ctrl_x_mode_not_defined_yet()
2531 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002532 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002533 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002534 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002535 }
2536
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002537 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002538 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2539 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002540 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002541 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002542 {
2543 // We're already in CTRL-X mode, do we stay in it?
2544 if (!vim_is_ctrl_x_key(c))
2545 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002546 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002547 ctrl_x_mode = CTRL_X_NORMAL;
2548 else
2549 ctrl_x_mode = CTRL_X_FINISHED;
2550 edit_submode = NULL;
2551 }
2552 showmode();
2553 }
2554
2555 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2556 {
2557 // Show error message from attempted keyword completion (probably
2558 // 'Pattern not found') until another key is hit, then go back to
2559 // showing what mode we are in.
2560 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002561 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002562 && c != Ctrl_R && !ins_compl_pum_key(c))
2563 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002564 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002565 }
2566 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2567 // Trigger the CompleteDone event to give scripts a chance to act
2568 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002569 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002570
LemonBoy2bf52dd2022-04-09 18:17:34 +01002571 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002572
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002573 // reset continue_* if we left expansion-mode, if we stay they'll be
2574 // (re)set properly in ins_complete()
2575 if (!vim_is_ctrl_x_key(c))
2576 {
2577 compl_cont_status = 0;
2578 compl_cont_mode = 0;
2579 }
2580
2581 return retval;
2582}
2583
2584/*
2585 * Fix the redo buffer for the completion leader replacing some of the typed
2586 * text. This inserts backspaces and appends the changed text.
2587 * "ptr" is the known leader text or NUL.
2588 */
2589 static void
2590ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2591{
John Marriott5e6ea922024-11-23 14:01:57 +01002592 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002593 char_u *p;
2594 char_u *ptr = ptr_arg;
2595
2596 if (ptr == NULL)
2597 {
John Marriott5e6ea922024-11-23 14:01:57 +01002598 if (compl_leader.string != NULL)
2599 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002600 else
2601 return; // nothing to do
2602 }
John Marriott5e6ea922024-11-23 14:01:57 +01002603 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002604 {
John Marriott5e6ea922024-11-23 14:01:57 +01002605 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002606 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2607 ;
2608 if (len > 0)
2609 len -= (*mb_head_off)(p, p + len);
2610 for (p += len; *p != NUL; MB_PTR_ADV(p))
2611 AppendCharToRedobuff(K_BS);
2612 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002613 if (ptr != NULL)
2614 AppendToRedobuffLit(ptr + len, -1);
2615}
2616
2617/*
2618 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2619 * (depending on flag) starting from buf and looking for a non-scanned
2620 * buffer (other than curbuf). curbuf is special, if it is called with
2621 * buf=curbuf then it has to be the first call for a given flag/expansion.
2622 *
2623 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2624 */
2625 static buf_T *
2626ins_compl_next_buf(buf_T *buf, int flag)
2627{
2628 static win_T *wp = NULL;
2629
2630 if (flag == 'w') // just windows
2631 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002632 if (buf == curbuf || !win_valid(wp))
2633 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002634 wp = curwin;
2635 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2636 && wp->w_buffer->b_scanned)
2637 ;
2638 buf = wp->w_buffer;
2639 }
2640 else
2641 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2642 // (unlisted buffers)
2643 // When completing whole lines skip unloaded buffers.
2644 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2645 && ((flag == 'U'
2646 ? buf->b_p_bl
2647 : (!buf->b_p_bl
2648 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2649 || buf->b_scanned))
2650 ;
2651 return buf;
2652}
2653
2654#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002655
2656# ifdef FEAT_EVAL
2657static callback_T cfu_cb; // 'completefunc' callback function
2658static callback_T ofu_cb; // 'omnifunc' callback function
2659static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2660# endif
2661
2662/*
2663 * Copy a global callback function to a buffer local callback.
2664 */
2665 static void
2666copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2667{
2668 free_callback(bufcb);
2669 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2670 copy_callback(bufcb, globcb);
2671}
2672
2673/*
2674 * Parse the 'completefunc' option value and set the callback function.
2675 * Invoked when the 'completefunc' option is set. The option value can be a
2676 * name of a function (string), or function(<name>) or funcref(<name>) or a
2677 * lambda expression.
2678 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002679 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002680did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002681{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002682 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2683 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002684
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002685 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002686
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002687 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002688}
2689
2690/*
2691 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002692 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002693 */
2694 void
2695set_buflocal_cfu_callback(buf_T *buf UNUSED)
2696{
2697# ifdef FEAT_EVAL
2698 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2699# endif
2700}
2701
2702/*
2703 * Parse the 'omnifunc' option value and set the callback function.
2704 * Invoked when the 'omnifunc' option is set. The option value can be a
2705 * name of a function (string), or function(<name>) or funcref(<name>) or a
2706 * lambda expression.
2707 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002708 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002709did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002710{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002711 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2712 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002713
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002714 set_buflocal_ofu_callback(curbuf);
2715 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002716}
2717
2718/*
2719 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002720 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002721 */
2722 void
2723set_buflocal_ofu_callback(buf_T *buf UNUSED)
2724{
2725# ifdef FEAT_EVAL
2726 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2727# endif
2728}
2729
2730/*
2731 * Parse the 'thesaurusfunc' option value and set the callback function.
2732 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2733 * name of a function (string), or function(<name>) or funcref(<name>) or a
2734 * lambda expression.
2735 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002736 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002737did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002738{
2739 int retval;
2740
zeertzjq6eda2692024-11-03 09:23:33 +01002741 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002742 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002743 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2744 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002745 else
2746 {
2747 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002748 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002749 // when using :set, free the local callback
2750 if (!(args->os_flags & OPT_GLOBAL))
2751 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002752 }
2753
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002754 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002755}
2756
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002757/*
2758 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002759 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002760 */
2761 int
2762set_ref_in_insexpand_funcs(int copyID)
2763{
2764 int abort = FALSE;
2765
2766 abort = set_ref_in_callback(&cfu_cb, copyID);
2767 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2768 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2769
2770 return abort;
2771}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002772
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002773/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002774 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002775 */
2776 static char_u *
2777get_complete_funcname(int type)
2778{
2779 switch (type)
2780 {
2781 case CTRL_X_FUNCTION:
2782 return curbuf->b_p_cfu;
2783 case CTRL_X_OMNI:
2784 return curbuf->b_p_ofu;
2785 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002786 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002787 default:
2788 return (char_u *)"";
2789 }
2790}
2791
2792/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002793 * Get the callback to use for insert mode completion.
2794 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002795 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002796get_insert_callback(int type)
2797{
2798 if (type == CTRL_X_FUNCTION)
2799 return &curbuf->b_cfu_cb;
2800 if (type == CTRL_X_OMNI)
2801 return &curbuf->b_ofu_cb;
2802 // CTRL_X_THESAURUS
2803 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2804}
2805
2806/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002807 * Execute user defined complete function 'completefunc', 'omnifunc' or
2808 * 'thesaurusfunc', and get matches in "matches".
2809 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002810 */
2811 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002812expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002813{
2814 list_T *matchlist = NULL;
2815 dict_T *matchdict = NULL;
2816 typval_T args[3];
2817 char_u *funcname;
2818 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002819 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820 typval_T rettv;
2821 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002822 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002824 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002825 if (*funcname == NUL)
2826 return;
2827
2828 // Call 'completefunc' to obtain the list of matches.
2829 args[0].v_type = VAR_NUMBER;
2830 args[0].vval.v_number = 0;
2831 args[1].v_type = VAR_STRING;
2832 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2833 args[2].v_type = VAR_UNKNOWN;
2834
2835 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002836 // Lock the text to avoid weird things from happening. Also disallow
2837 // switching to another window, it should not be needed and may end up in
2838 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002839 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002840
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002841 cb = get_insert_callback(type);
2842 retval = call_callback(cb, 0, &rettv, 2, args);
2843
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002844 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002845 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002846 {
2847 switch (rettv.v_type)
2848 {
2849 case VAR_LIST:
2850 matchlist = rettv.vval.v_list;
2851 break;
2852 case VAR_DICT:
2853 matchdict = rettv.vval.v_dict;
2854 break;
2855 case VAR_SPECIAL:
2856 if (rettv.vval.v_number == VVAL_NONE)
2857 compl_opt_suppress_empty = TRUE;
2858 // FALLTHROUGH
2859 default:
2860 // TODO: Give error message?
2861 clear_tv(&rettv);
2862 break;
2863 }
2864 }
zeertzjqcfe45652022-05-27 17:26:55 +01002865 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002866
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002867 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002868 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002869 validate_cursor();
2870 if (!EQUAL_POS(curwin->w_cursor, pos))
2871 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002872 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873 goto theend;
2874 }
2875
2876 if (matchlist != NULL)
2877 ins_compl_add_list(matchlist);
2878 else if (matchdict != NULL)
2879 ins_compl_add_dict(matchdict);
2880
2881theend:
2882 // Restore State, it might have been changed.
2883 State = save_State;
2884
2885 if (matchdict != NULL)
2886 dict_unref(matchdict);
2887 if (matchlist != NULL)
2888 list_unref(matchlist);
2889}
2890#endif // FEAT_COMPL_FUNC
2891
2892#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002893
2894 static inline int
2895get_user_highlight_attr(char_u *hlname)
2896{
2897 if (hlname != NULL && *hlname != NUL)
2898 return syn_name2attr(hlname);
2899 return -1;
2900}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002901/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002902 * Add a match to the list of matches from a typeval_T.
2903 * If the given string is already in the list of completions, then return
2904 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2905 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002906 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002907 */
2908 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002909ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002910{
2911 char_u *word;
2912 int dup = FALSE;
2913 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002914 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002915 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002916 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002917 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002918 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002919 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002920 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002921
Bram Moolenaar08928322020-01-04 14:32:48 +01002922 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002923 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2924 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002925 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2926 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2927 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2928 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2929 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002930
glepnir0fe17f82024-10-08 22:26:44 +02002931 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002932 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002933
2934 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002935 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002936
Bram Moolenaard61efa52022-07-23 09:52:04 +01002937 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2938 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2939 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002940 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002941 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2942 dup = dict_get_number(tv->vval.v_dict, "dup");
2943 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2944 empty = dict_get_number(tv->vval.v_dict, "empty");
2945 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2946 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002947 flags |= CP_EQUAL;
2948 }
2949 else
2950 {
2951 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002952 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002953 }
2954 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002955 {
2956 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002957 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002958 }
glepnir508e7852024-07-25 21:39:08 +02002959 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01002960 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002961 if (status != OK)
2962 clear_tv(&user_data);
2963 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002964}
2965
2966/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002967 * Add completions from a list.
2968 */
2969 static void
2970ins_compl_add_list(list_T *list)
2971{
2972 listitem_T *li;
2973 int dir = compl_direction;
2974
2975 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002976 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002977 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002978 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002979 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002980 // if dir was BACKWARD then honor it just once
2981 dir = FORWARD;
2982 else if (did_emsg)
2983 break;
2984 }
2985}
2986
2987/*
2988 * Add completions from a dict.
2989 */
2990 static void
2991ins_compl_add_dict(dict_T *dict)
2992{
2993 dictitem_T *di_refresh;
2994 dictitem_T *di_words;
2995
2996 // Check for optional "refresh" item.
2997 compl_opt_refresh_always = FALSE;
2998 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2999 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3000 {
3001 char_u *v = di_refresh->di_tv.vval.v_string;
3002
3003 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3004 compl_opt_refresh_always = TRUE;
3005 }
3006
3007 // Add completions from a "words" list.
3008 di_words = dict_find(dict, (char_u *)"words", 5);
3009 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3010 ins_compl_add_list(di_words->di_tv.vval.v_list);
3011}
3012
3013/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003014 * Start completion for the complete() function.
3015 * "startcol" is where the matched text starts (1 is first column).
3016 * "list" is the list of matches.
3017 */
3018 static void
3019set_completion(colnr_T startcol, list_T *list)
3020{
3021 int save_w_wrow = curwin->w_wrow;
3022 int save_w_leftcol = curwin->w_leftcol;
3023 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003024 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003025 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3026 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3027 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003028
3029 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003030 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003031 ins_compl_prep(' ');
3032 ins_compl_clear();
3033 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003034 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003035
3036 compl_direction = FORWARD;
3037 if (startcol > curwin->w_cursor.col)
3038 startcol = curwin->w_cursor.col;
3039 compl_col = startcol;
3040 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3041 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003042 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003043 if (p_ic)
3044 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003045 if (compl_orig_text.string == NULL)
3046 {
3047 compl_orig_text.length = 0;
3048 return;
3049 }
3050 compl_orig_text.length = (size_t)compl_length;
3051 if (ins_compl_add(compl_orig_text.string,
3052 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3053 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003054 return;
3055
3056 ctrl_x_mode = CTRL_X_EVAL;
3057
3058 ins_compl_add_list(list);
3059 compl_matches = ins_compl_make_cyclic();
3060 compl_started = TRUE;
3061 compl_used_match = TRUE;
3062 compl_cont_status = 0;
3063
3064 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003065 int no_select = compl_no_select || compl_longest;
3066 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003067 {
3068 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003069 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003070 // Down/Up has no real effect.
3071 ins_complete(K_UP, FALSE);
3072 }
3073 else
3074 ins_complete(Ctrl_N, FALSE);
3075 compl_enter_selects = compl_no_insert;
3076
3077 // Lazily show the popup menu, unless we got interrupted.
3078 if (!compl_interrupted)
3079 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003080 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081 out_flush();
3082}
3083
3084/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003085 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003086 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003087 void
3088f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003089{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003090 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003091
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003092 if (in_vim9script()
3093 && (check_for_number_arg(argvars, 0) == FAIL
3094 || check_for_list_arg(argvars, 1) == FAIL))
3095 return;
3096
Bram Moolenaar24959102022-05-07 20:01:16 +01003097 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003098 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003099 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003100 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003101 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003102
3103 // Check for undo allowed here, because if something was already inserted
3104 // the line was already saved for undo and this check isn't done.
3105 if (!undo_allowed())
3106 return;
3107
Bram Moolenaard83392a2022-09-01 12:22:46 +01003108 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003109 {
3110 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3111 if (startcol > 0)
3112 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003113 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003114}
3115
3116/*
3117 * "complete_add()" function
3118 */
3119 void
3120f_complete_add(typval_T *argvars, typval_T *rettv)
3121{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003122 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003123 return;
3124
Bram Moolenaar440cf092021-04-03 20:13:30 +02003125 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003126}
3127
3128/*
3129 * "complete_check()" function
3130 */
3131 void
3132f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3133{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003134 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003135 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003136
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003137 ins_compl_check_keys(0, TRUE);
3138 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003139
3140 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003141}
3142
3143/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003144 * Return Insert completion mode name string
3145 */
3146 static char_u *
3147ins_compl_mode(void)
3148{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003149 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003150 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3151
3152 return (char_u *)"";
3153}
3154
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003155/*
3156 * Assign the sequence number to all the completion matches which don't have
3157 * one assigned yet.
3158 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003159 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003160ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003161{
3162 int number = 0;
3163 compl_T *match;
3164
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003165 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003166 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003167 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003168 // This should normally succeed already at the first loop
3169 // cycle, so it's fast!
3170 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003171 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003172 if (match->cp_number != -1)
3173 {
3174 number = match->cp_number;
3175 break;
3176 }
3177 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003178 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003179 for (match = match->cp_next;
3180 match != NULL && match->cp_number == -1;
3181 match = match->cp_next)
3182 match->cp_number = ++number;
3183 }
3184 else // BACKWARD
3185 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003186 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003187 // number. This should normally succeed already at the
3188 // first loop cycle, so it's fast!
3189 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003190 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003191 if (match->cp_number != -1)
3192 {
3193 number = match->cp_number;
3194 break;
3195 }
3196 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003197 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003198 for (match = match->cp_prev; match
3199 && match->cp_number == -1;
3200 match = match->cp_prev)
3201 match->cp_number = ++number;
3202 }
3203}
3204
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003205/*
3206 * Get complete information
3207 */
3208 static void
3209get_complete_info(list_T *what_list, dict_T *retdict)
3210{
3211 int ret = OK;
3212 listitem_T *item;
3213#define CI_WHAT_MODE 0x01
3214#define CI_WHAT_PUM_VISIBLE 0x02
3215#define CI_WHAT_ITEMS 0x04
3216#define CI_WHAT_SELECTED 0x08
3217#define CI_WHAT_INSERTED 0x10
3218#define CI_WHAT_ALL 0xff
3219 int what_flag;
3220
3221 if (what_list == NULL)
3222 what_flag = CI_WHAT_ALL;
3223 else
3224 {
3225 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003226 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003227 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003228 {
3229 char_u *what = tv_get_string(&item->li_tv);
3230
3231 if (STRCMP(what, "mode") == 0)
3232 what_flag |= CI_WHAT_MODE;
3233 else if (STRCMP(what, "pum_visible") == 0)
3234 what_flag |= CI_WHAT_PUM_VISIBLE;
3235 else if (STRCMP(what, "items") == 0)
3236 what_flag |= CI_WHAT_ITEMS;
3237 else if (STRCMP(what, "selected") == 0)
3238 what_flag |= CI_WHAT_SELECTED;
3239 else if (STRCMP(what, "inserted") == 0)
3240 what_flag |= CI_WHAT_INSERTED;
3241 }
3242 }
3243
3244 if (ret == OK && (what_flag & CI_WHAT_MODE))
3245 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3246
3247 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3248 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3249
Girish Palya8950bf72024-03-20 20:07:29 +01003250 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003251 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003252 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003253 dict_T *di;
3254 compl_T *match;
3255 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003256
Girish Palya8950bf72024-03-20 20:07:29 +01003257 if (what_flag & CI_WHAT_ITEMS)
3258 {
3259 li = list_alloc();
3260 if (li == NULL)
3261 return;
3262 ret = dict_add_list(retdict, "items", li);
3263 }
3264 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3265 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3266 ins_compl_update_sequence_numbers();
3267 if (ret == OK && compl_first_match != NULL)
3268 {
3269 int list_idx = 0;
3270 match = compl_first_match;
3271 do
3272 {
3273 if (!match_at_original_text(match))
3274 {
3275 if (what_flag & CI_WHAT_ITEMS)
3276 {
3277 di = dict_alloc();
3278 if (di == NULL)
3279 return;
3280 ret = list_append_dict(li, di);
3281 if (ret != OK)
3282 return;
John Marriott5e6ea922024-11-23 14:01:57 +01003283 dict_add_string(di, "word", match->cp_str.string);
Girish Palya8950bf72024-03-20 20:07:29 +01003284 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3285 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3286 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3287 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3288 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3289 // Add an empty string for backwards compatibility
3290 dict_add_string(di, "user_data", (char_u *)"");
3291 else
3292 dict_add_tv(di, "user_data", &match->cp_user_data);
3293 }
3294 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3295 selected_idx = list_idx;
3296 list_idx += 1;
3297 }
3298 match = match->cp_next;
3299 }
3300 while (match != NULL && !is_first_match(match));
3301 }
3302 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3303 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003304 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003305
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003306 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3307 {
3308 // TODO
3309 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003310}
3311
3312/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003313 * "complete_info()" function
3314 */
3315 void
3316f_complete_info(typval_T *argvars, typval_T *rettv)
3317{
3318 list_T *what_list = NULL;
3319
Bram Moolenaar93a10962022-06-16 11:42:09 +01003320 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003321 return;
3322
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003323 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3324 return;
3325
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003326 if (argvars[0].v_type != VAR_UNKNOWN)
3327 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003328 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003329 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003330 what_list = argvars[0].vval.v_list;
3331 }
3332 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003333}
3334#endif
3335
3336/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003337 * Returns TRUE when using a user-defined function for thesaurus completion.
3338 */
3339 static int
3340thesaurus_func_complete(int type UNUSED)
3341{
3342#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003343 return type == CTRL_X_THESAURUS
3344 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003345#else
3346 return FALSE;
3347#endif
3348}
3349
3350/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003351 * Return value of process_next_cpt_value()
3352 */
3353enum
3354{
3355 INS_COMPL_CPT_OK = 1,
3356 INS_COMPL_CPT_CONT,
3357 INS_COMPL_CPT_END
3358};
3359
3360/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003361 * state information used for getting the next set of insert completion
3362 * matches.
3363 */
3364typedef struct
3365{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003366 char_u *e_cpt_copy; // copy of 'complete'
3367 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003368 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003369 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003370 pos_T prev_match_pos; // previous match position
3371 int set_match_pos; // save first_match_pos/last_match_pos
3372 pos_T first_match_pos; // first match position
3373 pos_T last_match_pos; // last match position
3374 int found_all; // found all matches of a certain type.
3375 char_u *dict; // dictionary file to search
3376 int dict_f; // "dict" is an exact file name or not
3377} ins_compl_next_state_T;
3378
3379/*
3380 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003381 *
3382 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003383 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003384 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003385 * st->found_all - all matches of this type are found
3386 * st->ins_buf - search for completions in this buffer
3387 * st->first_match_pos - position of the first completion match
3388 * st->last_match_pos - position of the last completion match
3389 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003390 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003391 * st->dict - name of the dictionary or thesaurus file to search
3392 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003393 *
3394 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003395 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3396 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003397 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003398 */
3399 static int
3400process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003401 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003402 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003403 pos_T *start_match_pos,
3404 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003405{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003406 int compl_type = -1;
3407 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003409 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003410
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003411 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3412 st->e_cpt++;
3413
3414 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003415 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003416 st->ins_buf = curbuf;
3417 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003418 // Move the cursor back one character so that ^N can match the
3419 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003420 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003421 {
3422 // Move the cursor to after the last character in the
3423 // buffer, so that word at start of buffer is found
3424 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003425 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003426 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003427 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003428 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 compl_type = 0;
3430
3431 // Remember the first match so that the loop stops when we
3432 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003433 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003434 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003435 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003436 && (st->ins_buf = ins_compl_next_buf(
3437 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003438 {
3439 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003440 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003441 {
3442 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003443 st->first_match_pos.col = st->last_match_pos.col = 0;
3444 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3445 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003446 compl_type = 0;
3447 }
3448 else // unloaded buffer, scan like dictionary
3449 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003450 st->found_all = TRUE;
3451 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003452 {
3453 status = INS_COMPL_CPT_CONT;
3454 goto done;
3455 }
3456 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003457 st->dict = st->ins_buf->b_fname;
3458 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003460 if (!shortmess(SHM_COMPLETIONSCAN))
3461 {
3462 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3463 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3464 st->ins_buf->b_fname == NULL
3465 ? buf_spname(st->ins_buf)
3466 : st->ins_buf->b_sfname == NULL
3467 ? st->ins_buf->b_fname
3468 : st->ins_buf->b_sfname);
3469 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3470 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003472 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003473 status = INS_COMPL_CPT_END;
3474 else
3475 {
3476 if (ctrl_x_mode_line_or_eval())
3477 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003478 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003479 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003480 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003481 compl_type = CTRL_X_DICTIONARY;
3482 else
3483 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003484 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003486 st->dict = st->e_cpt;
3487 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003488 }
3489 }
3490#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003491 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003492 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003493 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003494 compl_type = CTRL_X_PATH_DEFINES;
3495#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003496 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003497 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003498 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003499 if (!shortmess(SHM_COMPLETIONSCAN))
3500 {
3501 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3502 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3503 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3504 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003505 }
3506 else
3507 compl_type = -1;
3508
3509 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003510 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003511
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003512 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003513 if (compl_type == -1)
3514 status = INS_COMPL_CPT_CONT;
3515 }
3516
3517done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003518 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003519 return status;
3520}
3521
3522#ifdef FEAT_FIND_ID
3523/*
3524 * Get the next set of identifiers or defines matching "compl_pattern" in
3525 * included files.
3526 */
3527 static void
3528get_next_include_file_completion(int compl_type)
3529{
John Marriott5e6ea922024-11-23 14:01:57 +01003530 find_pattern_in_path(compl_pattern.string, compl_direction,
3531 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003532 (compl_type == CTRL_X_PATH_DEFINES
3533 && !(compl_cont_status & CONT_SOL))
3534 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003535 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003536}
3537#endif
3538
3539/*
3540 * Get the next set of words matching "compl_pattern" in dictionary or
3541 * thesaurus files.
3542 */
3543 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003544get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003545{
3546#ifdef FEAT_COMPL_FUNC
3547 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003548 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003549 else
3550#endif
3551 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003552 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003553 : (compl_type == CTRL_X_THESAURUS
3554 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3555 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003556 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003557 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003558 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559}
3560
3561/*
3562 * Get the next set of tag names matching "compl_pattern".
3563 */
3564 static void
3565get_next_tag_completion(void)
3566{
3567 int save_p_ic;
3568 char_u **matches;
3569 int num_matches;
3570
3571 // set p_ic according to p_ic, p_scs and pat for find_tags().
3572 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003573 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003574
3575 // Find up to TAG_MANY matches. Avoids that an enormous number
3576 // of matches is found when compl_pattern is empty
3577 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003578 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003579 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003580 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003581 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3582 ins_compl_add_matches(num_matches, matches, p_ic);
3583 g_tag_at_cursor = FALSE;
3584 p_ic = save_p_ic;
3585}
3586
3587/*
glepnir8159fb12024-07-17 20:32:54 +02003588 * Compare function for qsort
3589 */
3590static int compare_scores(const void *a, const void *b)
3591{
3592 int idx_a = *(const int *)a;
3593 int idx_b = *(const int *)b;
3594 int score_a = compl_fuzzy_scores[idx_a];
3595 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003596 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3597 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003598}
3599
3600/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003601 * Get the next set of filename matching "compl_pattern".
3602 */
3603 static void
3604get_next_filename_completion(void)
3605{
3606 char_u **matches;
3607 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003608 char_u *ptr;
3609 garray_T fuzzy_indices;
3610 int i;
3611 int score;
3612 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003613 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003614 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3615 char_u **sorted_matches;
3616 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003617 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003618
glepnirb9de1a02024-08-02 19:14:38 +02003619#ifdef BACKSLASH_IN_FILENAME
3620 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3621 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3622#else
3623 char pathsep = PATHSEP;
3624#endif
3625
glepnir8159fb12024-07-17 20:32:54 +02003626 if (in_fuzzy)
3627 {
glepnirb9de1a02024-08-02 19:14:38 +02003628#ifdef BACKSLASH_IN_FILENAME
3629 if (curbuf->b_p_csl[0] == 's')
3630 {
John Marriott5e6ea922024-11-23 14:01:57 +01003631 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003632 {
3633 if (leader[i] == '\\')
3634 leader[i] = '/';
3635 }
3636 }
3637 else if (curbuf->b_p_csl[0] == 'b')
3638 {
John Marriott5e6ea922024-11-23 14:01:57 +01003639 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003640 {
3641 if (leader[i] == '/')
3642 leader[i] = '\\';
3643 }
3644 }
3645#endif
3646 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003647 if (last_sep == NULL)
3648 {
3649 // No path separator or separator is the last character,
3650 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003651 VIM_CLEAR_STRING(compl_pattern);
3652 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3653 if (compl_pattern.string == NULL)
3654 return;
3655 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003656 }
3657 else if (*(last_sep + 1) == '\0')
3658 in_fuzzy = FALSE;
3659 else
3660 {
3661 // Split leader into path and file parts
3662 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003663 char_u *path_with_wildcard;
3664
3665 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003666 if (path_with_wildcard != NULL)
3667 {
John Marriott5e6ea922024-11-23 14:01:57 +01003668 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3669 VIM_CLEAR_STRING(compl_pattern);
3670 compl_pattern.string = path_with_wildcard;
3671 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003672
3673 // Move leader to the file part
3674 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003675 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003676 }
3677 }
glepnir8159fb12024-07-17 20:32:54 +02003678 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003679
John Marriott5e6ea922024-11-23 14:01:57 +01003680 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003681 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3682 return;
3683
3684 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003685 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003686#ifdef BACKSLASH_IN_FILENAME
3687 if (curbuf->b_p_csl[0] != NUL)
3688 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003689 for (i = 0; i < num_matches; ++i)
3690 {
glepnir8159fb12024-07-17 20:32:54 +02003691 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003692 while (*ptr != NUL)
3693 {
3694 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3695 *ptr = '/';
3696 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3697 *ptr = '\\';
3698 ptr += (*mb_ptr2len)(ptr);
3699 }
3700 }
3701 }
3702#endif
glepnir8159fb12024-07-17 20:32:54 +02003703
3704 if (in_fuzzy)
3705 {
3706 ga_init2(&fuzzy_indices, sizeof(int), 10);
3707 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3708
3709 for (i = 0; i < num_matches; i++)
3710 {
3711 ptr = matches[i];
3712 score = fuzzy_match_str(ptr, leader);
3713 if (score > 0)
3714 {
3715 if (ga_grow(&fuzzy_indices, 1) == OK)
3716 {
3717 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3718 compl_fuzzy_scores[i] = score;
3719 fuzzy_indices.ga_len++;
3720 }
3721 }
3722 }
3723
Christian Brabandt220474d2024-07-20 13:26:44 +02003724 // prevent qsort from deref NULL pointer
3725 if (fuzzy_indices.ga_len > 0)
3726 {
3727 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3728 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003729
Christian Brabandt220474d2024-07-20 13:26:44 +02003730 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3731 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3732 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003733
Christian Brabandt220474d2024-07-20 13:26:44 +02003734 FreeWild(num_matches, matches);
3735 matches = sorted_matches;
3736 num_matches = fuzzy_indices.ga_len;
3737 }
glepnir6b6280c2024-07-27 16:25:45 +02003738 else if (leader_len > 0)
3739 {
3740 FreeWild(num_matches, matches);
3741 num_matches = 0;
3742 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003743
glepnir8159fb12024-07-17 20:32:54 +02003744 vim_free(compl_fuzzy_scores);
3745 ga_clear(&fuzzy_indices);
3746 }
3747
glepnir6b6280c2024-07-27 16:25:45 +02003748 if (num_matches > 0)
3749 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750}
3751
3752/*
3753 * Get the next set of command-line completions matching "compl_pattern".
3754 */
3755 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003756get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003757{
3758 char_u **matches;
3759 int num_matches;
3760
John Marriott5e6ea922024-11-23 14:01:57 +01003761 if (expand_cmdline(&compl_xp, compl_pattern.string,
3762 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003763 ins_compl_add_matches(num_matches, matches, FALSE);
3764}
3765
3766/*
3767 * Get the next set of spell suggestions matching "compl_pattern".
3768 */
3769 static void
3770get_next_spell_completion(linenr_T lnum UNUSED)
3771{
3772#ifdef FEAT_SPELL
3773 char_u **matches;
3774 int num_matches;
3775
John Marriott5e6ea922024-11-23 14:01:57 +01003776 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003777 if (num_matches > 0)
3778 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003779 else
3780 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003781#endif
3782}
3783
3784/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003785 * Return the next word or line from buffer "ins_buf" at position
3786 * "cur_match_pos" for completion. The length of the match is set in "len".
3787 */
3788 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003789ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003790 buf_T *ins_buf, // buffer being scanned
3791 pos_T *cur_match_pos, // current match position
3792 int *match_len,
3793 int *cont_s_ipos) // next ^X<> will set initial_pos
3794{
3795 char_u *ptr;
3796 int len;
3797
3798 *match_len = 0;
3799 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3800 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003801 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003802 if (ctrl_x_mode_line_or_eval())
3803 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003804 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003805 {
3806 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3807 return NULL;
3808 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003809 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003810 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003811 {
3812 char_u *tmp_ptr = ptr;
3813
3814 ptr = skipwhite(tmp_ptr);
3815 len -= (int)(ptr - tmp_ptr);
3816 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003817 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003818 }
3819 else
3820 {
3821 char_u *tmp_ptr = ptr;
3822
John Marriott5e6ea922024-11-23 14:01:57 +01003823 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003824 {
3825 tmp_ptr += compl_length;
3826 // Skip if already inside a word.
3827 if (vim_iswordp(tmp_ptr))
3828 return NULL;
3829 // Find start of next word.
3830 tmp_ptr = find_word_start(tmp_ptr);
3831 }
3832 // Find end of this word.
3833 tmp_ptr = find_word_end(tmp_ptr);
3834 len = (int)(tmp_ptr - ptr);
3835
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003836 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003837 {
3838 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3839 {
3840 // Try next line, if any. the new word will be
3841 // "join" as if the normal command "J" was used.
3842 // IOSIZE is always greater than
3843 // compl_length, so the next STRNCPY always
3844 // works -- Acevedo
3845 STRNCPY(IObuff, ptr, len);
3846 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3847 tmp_ptr = ptr = skipwhite(ptr);
3848 // Find start of next word.
3849 tmp_ptr = find_word_start(tmp_ptr);
3850 // Find end of next word.
3851 tmp_ptr = find_word_end(tmp_ptr);
3852 if (tmp_ptr > ptr)
3853 {
3854 if (*ptr != ')' && IObuff[len - 1] != TAB)
3855 {
3856 if (IObuff[len - 1] != ' ')
3857 IObuff[len++] = ' ';
3858 // IObuf =~ "\k.* ", thus len >= 2
3859 if (p_js
3860 && (IObuff[len - 2] == '.'
3861 || (vim_strchr(p_cpo, CPO_JOINSP)
3862 == NULL
3863 && (IObuff[len - 2] == '?'
3864 || IObuff[len - 2] == '!'))))
3865 IObuff[len++] = ' ';
3866 }
3867 // copy as much as possible of the new word
3868 if (tmp_ptr - ptr >= IOSIZE - len)
3869 tmp_ptr = ptr + IOSIZE - len - 1;
3870 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3871 len += (int)(tmp_ptr - ptr);
3872 *cont_s_ipos = TRUE;
3873 }
3874 IObuff[len] = NUL;
3875 ptr = IObuff;
3876 }
3877 if (len == compl_length)
3878 return NULL;
3879 }
3880 }
3881
3882 *match_len = len;
3883 return ptr;
3884}
3885
3886/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003887 * Get the next set of words matching "compl_pattern" for default completion(s)
3888 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003889 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3890 * position "st->start_pos" in the "compl_direction" direction. If
3891 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3892 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003893 * Returns OK if a new next match is found, otherwise returns FAIL.
3894 */
3895 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003896get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003897{
3898 int found_new_match = FAIL;
3899 int save_p_scs;
3900 int save_p_ws;
3901 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003902 char_u *ptr = NULL;
3903 int len = 0;
3904 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3905 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003906
3907 // If 'infercase' is set, don't use 'smartcase' here
3908 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003909 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003910 p_scs = FALSE;
3911
3912 // Buffers other than curbuf are scanned from the beginning or the
3913 // end but never from the middle, thus setting nowrapscan in this
3914 // buffer is a good idea, on the other hand, we always set
3915 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3916 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003917 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003918 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003919 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003920 p_ws = TRUE;
3921 looped_around = FALSE;
3922 for (;;)
3923 {
3924 int cont_s_ipos = FALSE;
3925
3926 ++msg_silent; // Don't want messages for wrapscan.
3927
3928 // ctrl_x_mode_line_or_eval() || word-wise search that
3929 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003930 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003931 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01003932 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02003933 else if (in_fuzzy)
3934 found_new_match = search_for_fuzzy_match(st->ins_buf,
3935 st->cur_match_pos, leader, compl_direction,
3936 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003937 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003938 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01003939 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02003940 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003941 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003942 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003943 {
3944 // set "compl_started" even on fail
3945 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003946 st->first_match_pos = *st->cur_match_pos;
3947 st->last_match_pos = *st->cur_match_pos;
3948 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003949 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003950 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3951 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003952 {
3953 found_new_match = FAIL;
3954 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003955 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003956 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3957 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3958 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003959 {
3960 if (looped_around)
3961 found_new_match = FAIL;
3962 else
3963 looped_around = TRUE;
3964 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003965 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003966 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3967 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3968 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003969 {
3970 if (looped_around)
3971 found_new_match = FAIL;
3972 else
3973 looped_around = TRUE;
3974 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003975 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003976 if (found_new_match == FAIL)
3977 break;
3978
3979 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003980 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003981 && start_pos->lnum == st->cur_match_pos->lnum
3982 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003983 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003984
glepnir8159fb12024-07-17 20:32:54 +02003985 if (!in_fuzzy)
3986 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3987 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 if (ptr == NULL)
3989 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003990
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003991 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003992 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003993 0, cont_s_ipos) != NOTDONE)
3994 {
3995 found_new_match = OK;
3996 break;
3997 }
3998 }
3999 p_scs = save_p_scs;
4000 p_ws = save_p_ws;
4001
4002 return found_new_match;
4003}
4004
4005/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004006 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004007 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004008 */
4009 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004010get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004011{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004012 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004013
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004014 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004015 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004016 case -1:
4017 break;
4018#ifdef FEAT_FIND_ID
4019 case CTRL_X_PATH_PATTERNS:
4020 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004021 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004022 break;
4023#endif
4024
4025 case CTRL_X_DICTIONARY:
4026 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004027 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4028 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004029 break;
4030
4031 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004032 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004033 break;
4034
4035 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004036 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004037 break;
4038
4039 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004040 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004041 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 break;
4043
4044#ifdef FEAT_COMPL_FUNC
4045 case CTRL_X_FUNCTION:
4046 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004047 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004048 break;
4049#endif
4050
4051 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004052 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053 break;
4054
4055 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004056 found_new_match = get_next_default_completion(st, ini);
4057 if (found_new_match == FAIL && st->ins_buf == curbuf)
4058 st->found_all = TRUE;
4059 }
4060
4061 // check if compl_curr_match has changed, (e.g. other type of
4062 // expansion added something)
4063 if (type != 0 && compl_curr_match != compl_old_match)
4064 found_new_match = OK;
4065
4066 return found_new_match;
4067}
4068
4069/*
4070 * Get the next expansion(s), using "compl_pattern".
4071 * The search starts at position "ini" in curbuf and in the direction
4072 * compl_direction.
4073 * When "compl_started" is FALSE start at that position, otherwise continue
4074 * where we stopped searching before.
4075 * This may return before finding all the matches.
4076 * Return the total number of matches or -1 if still unknown -- Acevedo
4077 */
4078 static int
4079ins_compl_get_exp(pos_T *ini)
4080{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004081 static ins_compl_next_state_T st;
4082 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004083 int i;
4084 int found_new_match;
4085 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004086 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004087
4088 if (!compl_started)
4089 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004090 buf_T *buf;
4091
4092 FOR_ALL_BUFFERS(buf)
4093 buf->b_scanned = 0;
4094 if (!st_cleared)
4095 {
4096 CLEAR_FIELD(st);
4097 st_cleared = TRUE;
4098 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004099 st.found_all = FALSE;
4100 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004101 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004102 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004103 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4104 ? (char_u *)"." : curbuf->b_p_cpt);
4105 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004106 st.last_match_pos = st.first_match_pos = *ini;
4107 }
4108 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4109 st.ins_buf = curbuf; // In case the buffer was wiped out.
4110
4111 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004112 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004113 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004114
4115 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4116 for (;;)
4117 {
4118 found_new_match = FAIL;
4119 st.set_match_pos = FALSE;
4120
4121 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4122 // or if found_all says this entry is done. For ^X^L only use the
4123 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004124 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004125 && (!compl_started || st.found_all))
4126 {
glepnir8159fb12024-07-17 20:32:54 +02004127 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004128
4129 if (status == INS_COMPL_CPT_END)
4130 break;
4131 if (status == INS_COMPL_CPT_CONT)
4132 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004133 }
4134
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004135 // If complete() was called then compl_pattern has been reset. The
4136 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004137 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004138 break;
4139
4140 // get the next set of completion matches
4141 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004142
4143 // break the loop for specialized modes (use 'complete' just for the
4144 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4145 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004146 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4147 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004148 {
4149 if (got_int)
4150 break;
4151 // Fill the popup menu as soon as possible.
4152 if (type != -1)
4153 ins_compl_check_keys(0, FALSE);
4154
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004155 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004156 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4157 break;
4158 compl_started = TRUE;
4159 }
4160 else
4161 {
4162 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004163 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004164 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004165
4166 compl_started = FALSE;
4167 }
4168 }
4169 compl_started = TRUE;
4170
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004171 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004172 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004173 found_new_match = FAIL;
4174
4175 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004176 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004177 && !ctrl_x_mode_line_or_eval()))
4178 i = ins_compl_make_cyclic();
4179
4180 if (compl_old_match != NULL)
4181 {
4182 // If several matches were added (FORWARD) or the search failed and has
4183 // just been made cyclic then we have to move compl_curr_match to the
4184 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004185 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004186 : compl_old_match->cp_prev;
4187 if (compl_curr_match == NULL)
4188 compl_curr_match = compl_old_match;
4189 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004190 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004191
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004192 return i;
4193}
4194
4195/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004196 * Update "compl_shown_match" to the actually shown match, it may differ when
4197 * "compl_leader" is used to omit some of the matches.
4198 */
4199 static void
4200ins_compl_update_shown_match(void)
4201{
4202 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004203 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004204 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004205 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004206 compl_shown_match = compl_shown_match->cp_next;
4207
4208 // If we didn't find it searching forward, and compl_shows_dir is
4209 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004210 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004211 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004212 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004213 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004214 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004215 {
4216 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004217 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004218 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004219 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004220 compl_shown_match = compl_shown_match->cp_prev;
4221 }
4222}
4223
4224/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004225 * Delete the old text being completed.
4226 */
4227 void
4228ins_compl_delete(void)
4229{
4230 int col;
4231
4232 // In insert mode: Delete the typed part.
4233 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004234 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004235 if ((int)curwin->w_cursor.col > col)
4236 {
4237 if (stop_arrow() == FAIL)
4238 return;
4239 backspace_until_column(col);
4240 }
4241
4242 // TODO: is this sufficient for redrawing? Redrawing everything causes
4243 // flicker, thus we can't do that.
4244 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004245#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004246 // clear v:completed_item
4247 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004248#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004249}
4250
4251/*
4252 * Insert the new text being completed.
4253 * "in_compl_func" is TRUE when called from complete_check().
4254 */
4255 void
4256ins_compl_insert(int in_compl_func)
4257{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004258 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004259
4260 // Make sure we don't go over the end of the string, this can happen with
4261 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004262 if (compl_len < (int)compl_shown_match->cp_str.length)
4263 ins_bytes(compl_shown_match->cp_str.string + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004264 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004265 compl_used_match = FALSE;
4266 else
4267 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004268#ifdef FEAT_EVAL
4269 {
4270 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4271
4272 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4273 }
4274#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004275 if (!in_compl_func)
4276 compl_curr_match = compl_shown_match;
4277}
4278
4279/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004280 * show the file name for the completion match (if any). Truncate the file
4281 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004282 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004283 static void
4284ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004285{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004286 char *lead = _("match in file");
4287 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4288 char_u *s;
4289 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004290
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004291 if (space <= 0)
4292 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004293
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004294 // We need the tail that fits. With double-byte encoding going
4295 // back from the end is very slow, thus go from the start and keep
4296 // the text that fits in "space" between "s" and "e".
4297 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004298 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004299 space -= ptr2cells(e);
4300 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004301 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004302 space += ptr2cells(s);
4303 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004304 }
4305 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004306 msg_hist_off = TRUE;
4307 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4308 s > compl_shown_match->cp_fname ? "<" : "", s);
4309 msg((char *)IObuff);
4310 msg_hist_off = FALSE;
4311 redraw_cmdline = FALSE; // don't overwrite!
4312}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004313
glepnirdca57fb2024-06-04 22:01:21 +02004314/*
zeertzjq551d8c32024-06-05 19:53:32 +02004315 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004316 */
glepnira218cc62024-06-03 19:32:39 +02004317 static compl_T *
4318find_comp_when_fuzzy(void)
4319{
4320 int score;
4321 char_u* str;
4322 int target_idx = -1;
4323 int is_forward = compl_shows_dir_forward();
4324 int is_backward = compl_shows_dir_backward();
4325 compl_T *comp = NULL;
4326
glepnir43eef882024-06-19 20:20:48 +02004327 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004328 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004329 return compl_first_match != compl_shown_match ? compl_first_match :
4330 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004331
4332 if (is_forward)
4333 target_idx = compl_selected_item + 1;
4334 else if (is_backward)
4335 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004336 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004337
4338 score = compl_match_array[target_idx].pum_score;
4339 str = compl_match_array[target_idx].pum_text;
4340
4341 comp = compl_first_match;
4342 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004343 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004344 return comp;
4345 comp = comp->cp_next;
4346 } while (comp != NULL && !is_first_match(comp));
4347
4348 return NULL;
4349}
4350
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004351/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004352 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004353 * times. The number of matches found is returned in 'num_matches'.
4354 *
4355 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4356 * get more completions. If it is FALSE, then do nothing when there are no more
4357 * completions in the given direction.
4358 *
4359 * If "advance" is TRUE, then completion will move to the first match.
4360 * Otherwise, the original text will be shown.
4361 *
4362 * Returns OK on success and -1 if the number of matches are unknown.
4363 */
4364 static int
4365find_next_completion_match(
4366 int allow_get_expansion,
4367 int todo, // repeat completion this many times
4368 int advance,
4369 int *num_matches)
4370{
4371 int found_end = FALSE;
4372 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004373 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004374 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4375 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004376
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004377 while (--todo >= 0)
4378 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004379 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004380 {
glepniraced8c22024-06-15 15:13:05 +02004381 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4382 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004383 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004384 && (is_first_match(compl_shown_match->cp_next)
4385 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004386 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004387 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004388 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004389 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004390 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004391 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4392 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004393 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004394 }
4395 else
4396 {
4397 if (!allow_get_expansion)
4398 {
4399 if (advance)
4400 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004401 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004402 compl_pending -= todo + 1;
4403 else
4404 compl_pending += todo + 1;
4405 }
4406 return -1;
4407 }
4408
4409 if (!compl_no_select && advance)
4410 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004411 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004412 --compl_pending;
4413 else
4414 ++compl_pending;
4415 }
4416
4417 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004418 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004419
4420 // handle any pending completions
4421 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004422 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004423 {
4424 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4425 {
4426 compl_shown_match = compl_shown_match->cp_next;
4427 --compl_pending;
4428 }
4429 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4430 {
4431 compl_shown_match = compl_shown_match->cp_prev;
4432 ++compl_pending;
4433 }
4434 else
4435 break;
4436 }
4437 found_end = FALSE;
4438 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004439 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004440 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004441 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004442 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004443 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004444 ++todo;
4445 else
4446 // Remember a matching item.
4447 found_compl = compl_shown_match;
4448
4449 // Stop at the end of the list when we found a usable match.
4450 if (found_end)
4451 {
4452 if (found_compl != NULL)
4453 {
4454 compl_shown_match = found_compl;
4455 break;
4456 }
4457 todo = 1; // use first usable match after wrapping around
4458 }
4459 }
4460
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004461 return OK;
4462}
4463
4464/*
4465 * Fill in the next completion in the current direction.
4466 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4467 * get more completions. If it is FALSE, then we just do nothing when there
4468 * are no more completions in a given direction. The latter case is used when
4469 * we are still in the middle of finding completions, to allow browsing
4470 * through the ones found so far.
4471 * Return the total number of matches, or -1 if still unknown -- webb.
4472 *
4473 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4474 * compl_shown_match here.
4475 *
4476 * Note that this function may be called recursively once only. First with
4477 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4478 * calls this function with "allow_get_expansion" FALSE.
4479 */
4480 static int
4481ins_compl_next(
4482 int allow_get_expansion,
4483 int count, // repeat completion this many times; should
4484 // be at least 1
4485 int insert_match, // Insert the newly selected match
4486 int in_compl_func) // called from complete_check()
4487{
4488 int num_matches = -1;
4489 int todo = count;
4490 int advance;
4491 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004492 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004493 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004494 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4495 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004496
4497 // When user complete function return -1 for findstart which is next
4498 // time of 'always', compl_shown_match become NULL.
4499 if (compl_shown_match == NULL)
4500 return -1;
4501
John Marriott5e6ea922024-11-23 14:01:57 +01004502 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004503 && !match_at_original_text(compl_shown_match)
4504 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004505 // Update "compl_shown_match" to the actually shown match
4506 ins_compl_update_shown_match();
4507
4508 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004509 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004510 // Delete old text to be replaced
4511 ins_compl_delete();
4512
4513 // When finding the longest common text we stick at the original text,
4514 // don't let CTRL-N or CTRL-P move to the first match.
4515 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4516
4517 // When restarting the search don't insert the first match either.
4518 if (compl_restarting)
4519 {
4520 advance = FALSE;
4521 compl_restarting = FALSE;
4522 }
4523
4524 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4525 // around.
4526 if (find_next_completion_match(allow_get_expansion, todo, advance,
4527 &num_matches) == -1)
4528 return -1;
4529
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004530 if (curbuf != orig_curbuf)
4531 {
4532 // In case some completion function switched buffer, don't want to
4533 // insert the completion elsewhere.
4534 return -1;
4535 }
4536
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004537 // Insert the text of the new completion, or the compl_leader.
4538 if (compl_no_insert && !started)
4539 {
John Marriott5e6ea922024-11-23 14:01:57 +01004540 ins_bytes(compl_orig_text.string + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004541 compl_used_match = FALSE;
4542 }
4543 else if (insert_match)
4544 {
4545 if (!compl_get_longest || compl_used_match)
4546 ins_compl_insert(in_compl_func);
4547 else
John Marriott5e6ea922024-11-23 14:01:57 +01004548 ins_bytes(compl_leader.string + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004549 }
4550 else
4551 compl_used_match = FALSE;
4552
4553 if (!allow_get_expansion)
4554 {
4555 // may undisplay the popup menu first
4556 ins_compl_upd_pum();
4557
4558 if (pum_enough_matches())
4559 // Will display the popup menu, don't redraw yet to avoid flicker.
4560 pum_call_update_screen();
4561 else
4562 // Not showing the popup menu yet, redraw to show the user what was
4563 // inserted.
4564 update_screen(0);
4565
4566 // display the updated popup menu
4567 ins_compl_show_pum();
4568#ifdef FEAT_GUI
4569 if (gui.in_use)
4570 {
4571 // Show the cursor after the match, not after the redrawn text.
4572 setcursor();
4573 out_flush_cursor(FALSE, FALSE);
4574 }
4575#endif
4576
4577 // Delete old text to be replaced, since we're still searching and
4578 // don't want to match ourselves!
4579 ins_compl_delete();
4580 }
4581
4582 // Enter will select a match when the match wasn't inserted and the popup
4583 // menu is visible.
4584 if (compl_no_insert && !started)
4585 compl_enter_selects = TRUE;
4586 else
4587 compl_enter_selects = !insert_match && compl_match_array != NULL;
4588
4589 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004590 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004591 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004592
4593 return num_matches;
4594}
4595
4596/*
4597 * Call this while finding completions, to check whether the user has hit a key
4598 * that should change the currently displayed completion, or exit completion
4599 * mode. Also, when compl_pending is not zero, show a completion as soon as
4600 * possible. -- webb
4601 * "frequency" specifies out of how many calls we actually check.
4602 * "in_compl_func" is TRUE when called from complete_check(), don't set
4603 * compl_curr_match.
4604 */
4605 void
4606ins_compl_check_keys(int frequency, int in_compl_func)
4607{
4608 static int count = 0;
4609 int c;
4610
4611 // Don't check when reading keys from a script, :normal or feedkeys().
4612 // That would break the test scripts. But do check for keys when called
4613 // from complete_check().
4614 if (!in_compl_func && (using_script() || ex_normal_busy))
4615 return;
4616
4617 // Only do this at regular intervals
4618 if (++count < frequency)
4619 return;
4620 count = 0;
4621
4622 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4623 // can't do its work correctly.
4624 c = vpeekc_any();
4625 if (c != NUL)
4626 {
4627 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4628 {
4629 c = safe_vgetc(); // Eat the character
4630 compl_shows_dir = ins_compl_key2dir(c);
4631 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4632 c != K_UP && c != K_DOWN, in_compl_func);
4633 }
4634 else
4635 {
4636 // Need to get the character to have KeyTyped set. We'll put it
4637 // back with vungetc() below. But skip K_IGNORE.
4638 c = safe_vgetc();
4639 if (c != K_IGNORE)
4640 {
4641 // Don't interrupt completion when the character wasn't typed,
4642 // e.g., when doing @q to replay keys.
4643 if (c != Ctrl_R && KeyTyped)
4644 compl_interrupted = TRUE;
4645
4646 vungetc(c);
4647 }
4648 }
4649 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004650 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004651 {
4652 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4653
4654 compl_pending = 0;
4655 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4656 }
4657}
4658
4659/*
4660 * Decide the direction of Insert mode complete from the key typed.
4661 * Returns BACKWARD or FORWARD.
4662 */
4663 static int
4664ins_compl_key2dir(int c)
4665{
4666 if (c == Ctrl_P || c == Ctrl_L
4667 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4668 return BACKWARD;
4669 return FORWARD;
4670}
4671
4672/*
4673 * Return TRUE for keys that are used for completion only when the popup menu
4674 * is visible.
4675 */
4676 static int
4677ins_compl_pum_key(int c)
4678{
4679 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4680 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4681 || c == K_UP || c == K_DOWN);
4682}
4683
4684/*
4685 * Decide the number of completions to move forward.
4686 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4687 */
4688 static int
4689ins_compl_key2count(int c)
4690{
4691 int h;
4692
4693 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4694 {
4695 h = pum_get_height();
4696 if (h > 3)
4697 h -= 2; // keep some context
4698 return h;
4699 }
4700 return 1;
4701}
4702
4703/*
4704 * Return TRUE if completion with "c" should insert the match, FALSE if only
4705 * to change the currently selected completion.
4706 */
4707 static int
4708ins_compl_use_match(int c)
4709{
4710 switch (c)
4711 {
4712 case K_UP:
4713 case K_DOWN:
4714 case K_PAGEDOWN:
4715 case K_KPAGEDOWN:
4716 case K_S_DOWN:
4717 case K_PAGEUP:
4718 case K_KPAGEUP:
4719 case K_S_UP:
4720 return FALSE;
4721 }
4722 return TRUE;
4723}
4724
4725/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004726 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4727 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004728 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004729 * Uses the global variables: compl_cont_status and ctrl_x_mode
4730 */
4731 static int
4732get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4733{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004734 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004735 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004736 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004737 {
4738 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4739 ;
4740 compl_col += ++startcol;
4741 compl_length = curs_col - startcol;
4742 }
4743 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004744 {
John Marriott5e6ea922024-11-23 14:01:57 +01004745 compl_pattern.string = str_foldcase(line + compl_col,
4746 compl_length, NULL, 0);
4747 if (compl_pattern.string == NULL)
4748 {
4749 compl_pattern.length = 0;
4750 return FAIL;
4751 }
4752 compl_pattern.length = STRLEN(compl_pattern.string);
4753 }
4754 else
4755 {
4756 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4757 if (compl_pattern.string == NULL)
4758 {
4759 compl_pattern.length = 0;
4760 return FAIL;
4761 }
4762 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004763 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004764 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004765 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004766 {
4767 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004768 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004769 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004770
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004771 if (!vim_iswordp(line + compl_col)
4772 || (compl_col > 0
4773 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004774 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004775 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004776 prefixlen = 0;
4777 }
John Marriott5e6ea922024-11-23 14:01:57 +01004778
4779 // we need up to 2 extra chars for the prefix
4780 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4781 compl_pattern.string = alloc(n);
4782 if (compl_pattern.string == NULL)
4783 {
4784 compl_pattern.length = 0;
4785 return FAIL;
4786 }
4787 STRCPY((char *)compl_pattern.string, prefix);
4788 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004789 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004790 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004791 }
4792 else if (--startcol < 0
4793 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4794 {
John Marriott5e6ea922024-11-23 14:01:57 +01004795 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4796
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004797 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004798 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4799 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004800 {
John Marriott5e6ea922024-11-23 14:01:57 +01004801 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004802 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004803 }
John Marriott5e6ea922024-11-23 14:01:57 +01004804 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004805 compl_col += curs_col;
4806 compl_length = 0;
4807 }
4808 else
4809 {
4810 // Search the point of change class of multibyte character
4811 // or not a word single byte character backward.
4812 if (has_mbyte)
4813 {
4814 int base_class;
4815 int head_off;
4816
4817 startcol -= (*mb_head_off)(line, line + startcol);
4818 base_class = mb_get_class(line + startcol);
4819 while (--startcol >= 0)
4820 {
4821 head_off = (*mb_head_off)(line, line + startcol);
4822 if (base_class != mb_get_class(line + startcol
4823 - head_off))
4824 break;
4825 startcol -= head_off;
4826 }
4827 }
4828 else
4829 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4830 ;
4831 compl_col += ++startcol;
4832 compl_length = (int)curs_col - startcol;
4833 if (compl_length == 1)
4834 {
4835 // Only match word with at least two chars -- webb
4836 // there's no need to call quote_meta,
4837 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004838 compl_pattern.string = alloc(7);
4839 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004840 {
John Marriott5e6ea922024-11-23 14:01:57 +01004841 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004842 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004843 }
John Marriott5e6ea922024-11-23 14:01:57 +01004844 STRCPY((char *)compl_pattern.string, "\\<");
4845 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4846 STRCAT((char *)compl_pattern.string, "\\k");
4847 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004848 }
4849 else
4850 {
John Marriott5e6ea922024-11-23 14:01:57 +01004851 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4852
4853 compl_pattern.string = alloc(n);
4854 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004855 {
John Marriott5e6ea922024-11-23 14:01:57 +01004856 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004857 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004858 }
John Marriott5e6ea922024-11-23 14:01:57 +01004859 STRCPY((char *)compl_pattern.string, "\\<");
4860 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004861 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004862 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004863 }
4864 }
4865
4866 return OK;
4867}
4868
4869/*
4870 * Get the pattern, column and length for whole line completion or for the
4871 * complete() function.
4872 * Sets the global variables: compl_col, compl_length and compl_pattern.
4873 */
4874 static int
4875get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4876{
4877 compl_col = (colnr_T)getwhitecols(line);
4878 compl_length = (int)curs_col - (int)compl_col;
4879 if (compl_length < 0) // cursor in indent: empty pattern
4880 compl_length = 0;
4881 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004882 {
John Marriott5e6ea922024-11-23 14:01:57 +01004883 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4884 NULL, 0);
4885 if (compl_pattern.string == NULL)
4886 {
4887 compl_pattern.length = 0;
4888 return FAIL;
4889 }
4890 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004891 }
John Marriott5e6ea922024-11-23 14:01:57 +01004892 else
4893 {
4894 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4895 if (compl_pattern.string == NULL)
4896 {
4897 compl_pattern.length = 0;
4898 return FAIL;
4899 }
4900 compl_pattern.length = (size_t)compl_length;
4901 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004902
4903 return OK;
4904}
4905
4906/*
4907 * Get the pattern, column and length for filename completion.
4908 * Sets the global variables: compl_col, compl_length and compl_pattern.
4909 */
4910 static int
4911get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4912{
4913 // Go back to just before the first filename character.
4914 if (startcol > 0)
4915 {
4916 char_u *p = line + startcol;
4917
4918 MB_PTR_BACK(line, p);
4919 while (p > line && vim_isfilec(PTR2CHAR(p)))
4920 MB_PTR_BACK(line, p);
4921 if (p == line && vim_isfilec(PTR2CHAR(p)))
4922 startcol = 0;
4923 else
4924 startcol = (int)(p - line) + 1;
4925 }
4926
4927 compl_col += startcol;
4928 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01004929 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
4930 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004931 {
John Marriott5e6ea922024-11-23 14:01:57 +01004932 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004933 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004934 }
4935
John Marriott5e6ea922024-11-23 14:01:57 +01004936 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004937
4938 return OK;
4939}
4940
4941/*
4942 * Get the pattern, column and length for command-line completion.
4943 * Sets the global variables: compl_col, compl_length and compl_pattern.
4944 */
4945 static int
4946get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4947{
John Marriott5e6ea922024-11-23 14:01:57 +01004948 compl_pattern.string = vim_strnsave(line, curs_col);
4949 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004950 {
John Marriott5e6ea922024-11-23 14:01:57 +01004951 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004952 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004953 }
John Marriott5e6ea922024-11-23 14:01:57 +01004954 compl_pattern.length = curs_col;
4955 set_cmd_context(&compl_xp, compl_pattern.string,
4956 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004957 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4958 || compl_xp.xp_context == EXPAND_NOTHING)
4959 // No completion possible, use an empty pattern to get a
4960 // "pattern not found" message.
4961 compl_col = curs_col;
4962 else
John Marriott5e6ea922024-11-23 14:01:57 +01004963 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004964 compl_length = curs_col - compl_col;
4965
4966 return OK;
4967}
4968
4969/*
4970 * Get the pattern, column and length for user defined completion ('omnifunc',
4971 * 'completefunc' and 'thesaurusfunc')
4972 * Sets the global variables: compl_col, compl_length and compl_pattern.
4973 * Uses the global variable: spell_bad_len
4974 */
4975 static int
4976get_userdefined_compl_info(colnr_T curs_col UNUSED)
4977{
4978 int ret = FAIL;
4979
4980#ifdef FEAT_COMPL_FUNC
4981 // Call user defined function 'completefunc' with "a:findstart"
4982 // set to 1 to obtain the length of text to use for completion.
4983 char_u *line;
4984 typval_T args[3];
4985 int col;
4986 char_u *funcname;
4987 pos_T pos;
4988 int save_State = State;
4989 callback_T *cb;
4990
4991 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4992 // length as a string
4993 funcname = get_complete_funcname(ctrl_x_mode);
4994 if (*funcname == NUL)
4995 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004996 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004997 ? "completefunc" : "omnifunc");
4998 return FAIL;
4999 }
5000
5001 args[0].v_type = VAR_NUMBER;
5002 args[0].vval.v_number = 1;
5003 args[1].v_type = VAR_STRING;
5004 args[1].vval.v_string = (char_u *)"";
5005 args[2].v_type = VAR_UNKNOWN;
5006 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005007 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005008 cb = get_insert_callback(ctrl_x_mode);
5009 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005010 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005011
5012 State = save_State;
5013 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005014 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005015 validate_cursor();
5016 if (!EQUAL_POS(curwin->w_cursor, pos))
5017 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005018 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005019 return FAIL;
5020 }
5021
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005022 // Return value -2 means the user complete function wants to cancel the
5023 // complete without an error, do the same if the function did not execute
5024 // successfully.
5025 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005026 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005027 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005028 if (col == -3)
5029 {
5030 ctrl_x_mode = CTRL_X_NORMAL;
5031 edit_submode = NULL;
5032 if (!shortmess(SHM_COMPLETIONMENU))
5033 msg_clr_cmdline();
5034 return FAIL;
5035 }
5036
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005037 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005038 // completion.
5039 compl_opt_refresh_always = FALSE;
5040 compl_opt_suppress_empty = FALSE;
5041
5042 if (col < 0)
5043 col = curs_col;
5044 compl_col = col;
5045 if (compl_col > curs_col)
5046 compl_col = curs_col;
5047
5048 // Setup variables for completion. Need to obtain "line" again,
5049 // it may have become invalid.
5050 line = ml_get(curwin->w_cursor.lnum);
5051 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005052 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5053 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005054 {
John Marriott5e6ea922024-11-23 14:01:57 +01005055 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005056 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005057 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005058
John Marriott5e6ea922024-11-23 14:01:57 +01005059 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005060 ret = OK;
5061#endif
5062
5063 return ret;
5064}
5065
5066/*
5067 * Get the pattern, column and length for spell completion.
5068 * Sets the global variables: compl_col, compl_length and compl_pattern.
5069 * Uses the global variable: spell_bad_len
5070 */
5071 static int
5072get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5073{
5074 int ret = FAIL;
5075#ifdef FEAT_SPELL
5076 char_u *line;
5077
5078 if (spell_bad_len > 0)
5079 compl_col = curs_col - spell_bad_len;
5080 else
5081 compl_col = spell_word_start(startcol);
5082 if (compl_col >= (colnr_T)startcol)
5083 {
5084 compl_length = 0;
5085 compl_col = curs_col;
5086 }
5087 else
5088 {
5089 spell_expand_check_cap(compl_col);
5090 compl_length = (int)curs_col - compl_col;
5091 }
5092 // Need to obtain "line" again, it may have become invalid.
5093 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005094 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5095 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005096 {
John Marriott5e6ea922024-11-23 14:01:57 +01005097 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005098 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005099 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005100
John Marriott5e6ea922024-11-23 14:01:57 +01005101 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005102 ret = OK;
5103#endif
5104
5105 return ret;
5106}
5107
5108/*
5109 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005110 * "startcol" - start column number of the completion pattern/text
5111 * "cur_col" - current cursor column
5112 * On return, "line_invalid" is set to TRUE, if the current line may have
5113 * become invalid and needs to be fetched again.
5114 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005115 */
5116 static int
5117compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5118{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005119 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005120 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5121 && !thesaurus_func_complete(ctrl_x_mode)))
5122 {
5123 return get_normal_compl_info(line, startcol, curs_col);
5124 }
5125 else if (ctrl_x_mode_line_or_eval())
5126 {
5127 return get_wholeline_compl_info(line, curs_col);
5128 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005129 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005130 {
5131 return get_filename_compl_info(line, startcol, curs_col);
5132 }
5133 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5134 {
5135 return get_cmdline_compl_info(line, curs_col);
5136 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005137 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005138 || thesaurus_func_complete(ctrl_x_mode))
5139 {
5140 if (get_userdefined_compl_info(curs_col) == FAIL)
5141 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005142 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005143 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005144 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005145 {
5146 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5147 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005148 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005149 }
5150 else
5151 {
5152 internal_error("ins_complete()");
5153 return FAIL;
5154 }
5155
5156 return OK;
5157}
5158
5159/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005160 * Continue an interrupted completion mode search in "line".
5161 *
5162 * If this same ctrl_x_mode has been interrupted use the text from
5163 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5164 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5165 * the same line as the cursor then fix it (the line has been split because it
5166 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5167 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005168 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005169 static void
5170ins_compl_continue_search(char_u *line)
5171{
5172 // it is a continued search
5173 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005174 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5175 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005176 {
5177 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5178 {
5179 // line (probably) wrapped, set compl_startpos to the
5180 // first non_blank in the line, if it is not a wordchar
5181 // include it to get a better pattern, but then we don't
5182 // want the "\\<" prefix, check it below
5183 compl_col = (colnr_T)getwhitecols(line);
5184 compl_startpos.col = compl_col;
5185 compl_startpos.lnum = curwin->w_cursor.lnum;
5186 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5187 }
5188 else
5189 {
5190 // S_IPOS was set when we inserted a word that was at the
5191 // beginning of the line, which means that we'll go to SOL
5192 // mode but first we need to redefine compl_startpos
5193 if (compl_cont_status & CONT_S_IPOS)
5194 {
5195 compl_cont_status |= CONT_SOL;
5196 compl_startpos.col = (colnr_T)(skipwhite(
5197 line + compl_length
5198 + compl_startpos.col) - line);
5199 }
5200 compl_col = compl_startpos.col;
5201 }
5202 compl_length = curwin->w_cursor.col - (int)compl_col;
5203 // IObuff is used to add a "word from the next line" would we
5204 // have enough space? just being paranoid
5205#define MIN_SPACE 75
5206 if (compl_length > (IOSIZE - MIN_SPACE))
5207 {
5208 compl_cont_status &= ~CONT_SOL;
5209 compl_length = (IOSIZE - MIN_SPACE);
5210 compl_col = curwin->w_cursor.col - compl_length;
5211 }
5212 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5213 if (compl_length < 1)
5214 compl_cont_status &= CONT_LOCAL;
5215 }
5216 else if (ctrl_x_mode_line_or_eval())
5217 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5218 else
5219 compl_cont_status = 0;
5220}
5221
5222/*
5223 * start insert mode completion
5224 */
5225 static int
5226ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005227{
5228 char_u *line;
5229 int startcol = 0; // column where searched text starts
5230 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005231 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005232 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005233 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005234
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005235 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005236
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005237 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005238 did_si = FALSE;
5239 can_si = FALSE;
5240 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005241 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005242 return FAIL;
5243
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005244 line = ml_get(curwin->w_cursor.lnum);
5245 curs_col = curwin->w_cursor.col;
5246 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005248 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5249 && compl_cont_mode == ctrl_x_mode)
5250 // this same ctrl-x_mode was interrupted previously. Continue the
5251 // completion.
5252 ins_compl_continue_search(line);
5253 else
5254 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005255
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005256 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005257 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005258 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005259 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005260 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5261 compl_cont_status = 0;
5262 compl_cont_status |= CONT_N_ADDS;
5263 compl_startpos = curwin->w_cursor;
5264 startcol = (int)curs_col;
5265 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005266 }
5267
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005268 // Work out completion pattern and original text -- webb
5269 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5270 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005271 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5272 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005273 // restore did_ai, so that adding comment leader works
5274 did_ai = save_did_ai;
5275 return FAIL;
5276 }
5277 // If "line" was changed while getting completion info get it again.
5278 if (line_invalid)
5279 line = ml_get(curwin->w_cursor.lnum);
5280
glepnir7cfe6932024-09-15 20:06:28 +02005281 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005282 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005283 {
5284 edit_submode_pre = (char_u *)_(" Adding");
5285 if (ctrl_x_mode_line_or_eval())
5286 {
5287 // Insert a new line, keep indentation but ignore 'comments'.
5288 char_u *old = curbuf->b_p_com;
5289
5290 curbuf->b_p_com = (char_u *)"";
5291 compl_startpos.lnum = curwin->w_cursor.lnum;
5292 compl_startpos.col = compl_col;
5293 ins_eol('\r');
5294 curbuf->b_p_com = old;
5295 compl_length = 0;
5296 compl_col = curwin->w_cursor.col;
5297 }
glepnir7cfe6932024-09-15 20:06:28 +02005298 else if (ctrl_x_mode_normal() && in_fuzzy)
5299 {
5300 compl_startpos = curwin->w_cursor;
5301 compl_cont_status &= CONT_S_IPOS;
5302 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005303 }
5304 else
5305 {
5306 edit_submode_pre = NULL;
5307 compl_startpos.col = compl_col;
5308 }
5309
5310 if (compl_cont_status & CONT_LOCAL)
5311 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5312 else
5313 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5314
5315 // If any of the original typed text has been changed we need to fix
5316 // the redo buffer.
5317 ins_compl_fixRedoBufForLeader(NULL);
5318
5319 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005320 VIM_CLEAR_STRING(compl_orig_text);
5321 compl_orig_text.length = (size_t)compl_length;
5322 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005323 if (p_ic)
5324 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005325 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5326 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005327 {
John Marriott5e6ea922024-11-23 14:01:57 +01005328 VIM_CLEAR_STRING(compl_pattern);
5329 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005330 return FAIL;
5331 }
5332
5333 // showmode might reset the internal line pointers, so it must
5334 // be called before line = ml_get(), or when this address is no
5335 // longer needed. -- Acevedo.
5336 edit_submode_extra = (char_u *)_("-- Searching...");
5337 edit_submode_highl = HLF_COUNT;
5338 showmode();
5339 edit_submode_extra = NULL;
5340 out_flush();
5341
5342 return OK;
5343}
5344
5345/*
5346 * display the completion status message
5347 */
5348 static void
5349ins_compl_show_statusmsg(void)
5350{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005351 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005352 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005353 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005354 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005355 ? (char_u *)_("Hit end of paragraph")
5356 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005357 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005358 }
5359
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005360 if (edit_submode_extra == NULL)
5361 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005362 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005363 {
5364 edit_submode_extra = (char_u *)_("Back at original");
5365 edit_submode_highl = HLF_W;
5366 }
5367 else if (compl_cont_status & CONT_S_IPOS)
5368 {
5369 edit_submode_extra = (char_u *)_("Word from other line");
5370 edit_submode_highl = HLF_COUNT;
5371 }
5372 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5373 {
5374 edit_submode_extra = (char_u *)_("The only match");
5375 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005376 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005377 }
5378 else
5379 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005380#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005381 // Update completion sequence number when needed.
5382 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005383 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005384#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005385 // The match should always have a sequence number now, this is
5386 // just a safety check.
5387 if (compl_curr_match->cp_number != -1)
5388 {
5389 // Space for 10 text chars. + 2x10-digit no.s = 31.
5390 // Translations may need more than twice that.
5391 static char_u match_ref[81];
5392
5393 if (compl_matches > 0)
5394 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005395 _("match %d of %d"),
5396 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005397 else
5398 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005399 _("match %d"),
5400 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005401 edit_submode_extra = match_ref;
5402 edit_submode_highl = HLF_R;
5403 if (dollar_vcol >= 0)
5404 curs_columns(FALSE);
5405 }
5406 }
5407 }
5408
5409 // Show a message about what (completion) mode we're in.
5410 if (!compl_opt_suppress_empty)
5411 {
5412 showmode();
5413 if (!shortmess(SHM_COMPLETIONMENU))
5414 {
5415 if (edit_submode_extra != NULL)
5416 {
5417 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005418 {
5419 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005420 msg_attr((char *)edit_submode_extra,
5421 edit_submode_highl < HLF_COUNT
5422 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005423 msg_hist_off = FALSE;
5424 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005425 }
5426 else
5427 msg_clr_cmdline(); // necessary for "noshowmode"
5428 }
5429 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005430}
5431
5432/*
5433 * Do Insert mode completion.
5434 * Called when character "c" was typed, which has a meaning for completion.
5435 * Returns OK if completion was done, FAIL if something failed (out of mem).
5436 */
5437 int
5438ins_complete(int c, int enable_pum)
5439{
5440 int n;
5441 int save_w_wrow;
5442 int save_w_leftcol;
5443 int insert_match;
5444
5445 compl_direction = ins_compl_key2dir(c);
5446 insert_match = ins_compl_use_match(c);
5447
5448 if (!compl_started)
5449 {
5450 if (ins_compl_start() == FAIL)
5451 return FAIL;
5452 }
5453 else if (insert_match && stop_arrow() == FAIL)
5454 return FAIL;
5455
5456 compl_shown_match = compl_curr_match;
5457 compl_shows_dir = compl_direction;
5458
5459 // Find next match (and following matches).
5460 save_w_wrow = curwin->w_wrow;
5461 save_w_leftcol = curwin->w_leftcol;
5462 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5463
5464 // may undisplay the popup menu
5465 ins_compl_upd_pum();
5466
5467 if (n > 1) // all matches have been found
5468 compl_matches = n;
5469 compl_curr_match = compl_shown_match;
5470 compl_direction = compl_shows_dir;
5471
5472 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5473 // mode.
5474 if (got_int && !global_busy)
5475 {
5476 (void)vgetc();
5477 got_int = FALSE;
5478 }
5479
5480 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005481 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005482 {
5483 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5484 // because we couldn't expand anything at first place, but if we used
5485 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5486 // (such as M in M'exico) if not tried already. -- Acevedo
5487 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005488 || compl_status_adding()
5489 || (ctrl_x_mode_not_default()
5490 && !ctrl_x_mode_path_patterns()
5491 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005492 compl_cont_status &= ~CONT_N_ADDS;
5493 }
5494
5495 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5496 compl_cont_status |= CONT_S_IPOS;
5497 else
5498 compl_cont_status &= ~CONT_S_IPOS;
5499
5500 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005501
5502 // Show the popup menu, unless we got interrupted.
5503 if (enable_pum && !compl_interrupted)
5504 show_pum(save_w_wrow, save_w_leftcol);
5505
5506 compl_was_interrupted = compl_interrupted;
5507 compl_interrupted = FALSE;
5508
5509 return OK;
5510}
5511
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005512/*
5513 * Remove (if needed) and show the popup menu
5514 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005515 static void
5516show_pum(int prev_w_wrow, int prev_w_leftcol)
5517{
5518 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005519 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005520 RedrawingDisabled = 0;
5521
5522 // If the cursor moved or the display scrolled we need to remove the pum
5523 // first.
5524 setcursor();
5525 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5526 ins_compl_del_pum();
5527
5528 ins_compl_show_pum();
5529 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005530
5531 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005532}
5533
5534/*
5535 * Looks in the first "len" chars. of "src" for search-metachars.
5536 * If dest is not NULL the chars. are copied there quoting (with
5537 * a backslash) the metachars, and dest would be NUL terminated.
5538 * Returns the length (needed) of dest
5539 */
5540 static unsigned
5541quote_meta(char_u *dest, char_u *src, int len)
5542{
5543 unsigned m = (unsigned)len + 1; // one extra for the NUL
5544
5545 for ( ; --len >= 0; src++)
5546 {
5547 switch (*src)
5548 {
5549 case '.':
5550 case '*':
5551 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005552 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005553 break;
5554 // FALLTHROUGH
5555 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005556 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005557 break;
5558 // FALLTHROUGH
5559 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005560 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005561 break;
5562 // FALLTHROUGH
5563 case '^': // currently it's not needed.
5564 case '$':
5565 m++;
5566 if (dest != NULL)
5567 *dest++ = '\\';
5568 break;
5569 }
5570 if (dest != NULL)
5571 *dest++ = *src;
5572 // Copy remaining bytes of a multibyte character.
5573 if (has_mbyte)
5574 {
5575 int i, mb_len;
5576
5577 mb_len = (*mb_ptr2len)(src) - 1;
5578 if (mb_len > 0 && len >= mb_len)
5579 for (i = 0; i < mb_len; ++i)
5580 {
5581 --len;
5582 ++src;
5583 if (dest != NULL)
5584 *dest++ = *src;
5585 }
5586 }
5587 }
5588 if (dest != NULL)
5589 *dest = NUL;
5590
5591 return m;
5592}
5593
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005594#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005595 void
5596free_insexpand_stuff(void)
5597{
John Marriott5e6ea922024-11-23 14:01:57 +01005598 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005599# ifdef FEAT_EVAL
5600 free_callback(&cfu_cb);
5601 free_callback(&ofu_cb);
5602 free_callback(&tsrfu_cb);
5603# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005604}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005605#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005606
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005607#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005608/*
5609 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5610 * spelled word, if there is one.
5611 */
5612 static void
5613spell_back_to_badword(void)
5614{
5615 pos_T tpos = curwin->w_cursor;
5616
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005617 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005618 if (curwin->w_cursor.col != tpos.col)
5619 start_arrow(&tpos);
5620}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005621#endif