blob: 818b1b96a1f7602b0512aec261d0f44c96e521eb [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +010091 * Structure used to store one match for insert completion.
92 */
93typedef struct compl_S compl_T;
94struct compl_S
95{
96 compl_T *cp_next;
97 compl_T *cp_prev;
glepnir80b66202024-11-27 21:53:53 +010098 compl_T *cp_match_next; // matched next compl_T
John Marriott5e6ea922024-11-23 14:01:57 +010099 string_T cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200100 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100101#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100102 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100103#endif
glepnir0fe17f82024-10-08 22:26:44 +0200104 char_u *cp_fname; // file containing the match, allocated when
105 // cp_flags has CP_FREE_FNAME
106 int cp_flags; // CP_ values
107 int cp_number; // sequence number
108 int cp_score; // fuzzy match score
glepnird4088ed2024-12-31 10:55:22 +0100109 int cp_in_match_array; // collected by compl_match_array
glepnir7baa0142024-10-09 20:19:25 +0200110 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200111 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100112};
113
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200114// values for cp_flags
115# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
116# define CP_FREE_FNAME 2 // cp_fname is allocated
117# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
118# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
119# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200120# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100122/*
123 * All the current matches are stored in a list.
124 * "compl_first_match" points to the start of the list.
125 * "compl_curr_match" points to the currently selected entry.
126 * "compl_shown_match" is different from compl_curr_match during
127 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000128 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100129 */
130static compl_T *compl_first_match = NULL;
131static compl_T *compl_curr_match = NULL;
132static compl_T *compl_shown_match = NULL;
133static compl_T *compl_old_match = NULL;
134
135// After using a cursor key <Enter> selects a match in the popup menu,
136// otherwise it inserts a line break.
137static int compl_enter_selects = FALSE;
138
139// When "compl_leader" is not NULL only matches that start with this string
140// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100141static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100142
143static int compl_get_longest = FALSE; // put longest common string
144 // in compl_leader
145
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100146// Selected one of the matches. When FALSE the match was edited or using the
147// longest common string.
148static int compl_used_match;
149
150// didn't finish finding completions.
151static int compl_was_interrupted = FALSE;
152
153// Set when character typed while looking for matches and it means we should
154// stop looking for matches.
155static int compl_interrupted = FALSE;
156
157static int compl_restarting = FALSE; // don't insert match
158
159// When the first completion is done "compl_started" is set. When it's
160// FALSE the word to be completed must be located.
161static int compl_started = FALSE;
162
163// Which Ctrl-X mode are we in?
164static int ctrl_x_mode = CTRL_X_NORMAL;
165
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000166static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100167static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100168static int compl_direction = FORWARD;
169static int compl_shows_dir = FORWARD;
170static int compl_pending = 0; // > 1 for postponed CTRL-N
171static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000172// Length in bytes of the text being completed (this is deleted to be replaced
173// by the match.)
174static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100175static colnr_T compl_col = 0; // column where the text starts
176 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100177static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100178static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100179 // completion started
180static int compl_cont_mode = 0;
181static expand_T compl_xp;
182
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// List of flags for method of completion.
184static int compl_cont_status = 0;
185# define CONT_ADDING 1 // "normal" or "adding" expansion
186# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
187 // it's set only iff N_ADDS is set
188# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
189# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
190 // if so, word-wise-expansion will set SOL
191# define CONT_SOL 16 // pattern includes start of line, just for
192 // word-wise expansion, not set for ^X^L
193# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
194 // expansion, (eg use complete=.)
195
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100196static int compl_opt_refresh_always = FALSE;
197static int compl_opt_suppress_empty = FALSE;
198
glepnira218cc62024-06-03 19:32:39 +0200199static int compl_selected_item = -1;
200
glepnir8159fb12024-07-17 20:32:54 +0200201static int *compl_fuzzy_scores;
202
glepnir6a38aff2024-12-16 21:56:16 +0100203// "compl_match_array" points the currently displayed list of entries in the
204// popup menu. It is NULL when there is no popup menu.
205static pumitem_T *compl_match_array = NULL;
206static int compl_match_arraysize;
207
glepnir80b66202024-11-27 21:53:53 +0100208static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int *user_hl);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100209static void ins_compl_longest_match(compl_T *match);
210static void ins_compl_del_pum(void);
211static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
212static char_u *find_line_end(char_u *ptr);
213static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100214static int ins_compl_need_restart(void);
215static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000216static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100217static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100218static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100219static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
220# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
221static void ins_compl_add_list(list_T *list);
222static void ins_compl_add_dict(dict_T *dict);
223# endif
224static int ins_compl_key2dir(int c);
225static int ins_compl_pum_key(int c);
226static int ins_compl_key2count(int c);
227static void show_pum(int prev_w_wrow, int prev_w_leftcol);
228static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100229
230#ifdef FEAT_SPELL
231static void spell_back_to_badword(void);
232static int spell_bad_len = 0; // length of located bad word
233#endif
234
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100235/*
236 * CTRL-X pressed in Insert mode.
237 */
238 void
239ins_ctrl_x(void)
240{
zeertzjqdca29d92021-08-31 19:12:51 +0200241 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100242 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000243 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100244 if (compl_cont_status & CONT_N_ADDS)
245 compl_cont_status |= CONT_INTRPT;
246 else
247 compl_cont_status = 0;
248 // We're not sure which CTRL-X mode it will be yet
249 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
250 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
251 edit_submode_pre = NULL;
252 showmode();
253 }
zeertzjqdca29d92021-08-31 19:12:51 +0200254 else
255 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
256 // CTRL-V look like CTRL-N
257 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100258
LemonBoy2bf52dd2022-04-09 18:17:34 +0100259 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100260}
261
262/*
263 * Functions to check the current CTRL-X mode.
264 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000265int ctrl_x_mode_none(void)
266 { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void)
268 { return ctrl_x_mode == CTRL_X_NORMAL; }
269int ctrl_x_mode_scroll(void)
270 { return ctrl_x_mode == CTRL_X_SCROLL; }
271int ctrl_x_mode_whole_line(void)
272 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
273int ctrl_x_mode_files(void)
274 { return ctrl_x_mode == CTRL_X_FILES; }
275int ctrl_x_mode_tags(void)
276 { return ctrl_x_mode == CTRL_X_TAGS; }
277int ctrl_x_mode_path_patterns(void)
278 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
279int ctrl_x_mode_path_defines(void)
280 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
281int ctrl_x_mode_dictionary(void)
282 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
283int ctrl_x_mode_thesaurus(void)
284 { return ctrl_x_mode == CTRL_X_THESAURUS; }
285int ctrl_x_mode_cmdline(void)
286 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200287 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000288int ctrl_x_mode_function(void)
289 { return ctrl_x_mode == CTRL_X_FUNCTION; }
290int ctrl_x_mode_omni(void)
291 { return ctrl_x_mode == CTRL_X_OMNI; }
292int ctrl_x_mode_spell(void)
293 { return ctrl_x_mode == CTRL_X_SPELL; }
294static int ctrl_x_mode_eval(void)
295 { return ctrl_x_mode == CTRL_X_EVAL; }
296int ctrl_x_mode_line_or_eval(void)
297 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100298
299/*
300 * Whether other than default completion has been selected.
301 */
302 int
303ctrl_x_mode_not_default(void)
304{
305 return ctrl_x_mode != CTRL_X_NORMAL;
306}
307
308/*
zeertzjqdca29d92021-08-31 19:12:51 +0200309 * Whether CTRL-X was typed without a following character,
310 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100311 */
312 int
313ctrl_x_mode_not_defined_yet(void)
314{
315 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
316}
317
318/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000319 * Return TRUE if currently in "normal" or "adding" insert completion matches
320 * state
321 */
322 int
323compl_status_adding(void)
324{
325 return compl_cont_status & CONT_ADDING;
326}
327
328/*
329 * Return TRUE if the completion pattern includes start of line, just for
330 * word-wise expansion.
331 */
332 int
333compl_status_sol(void)
334{
335 return compl_cont_status & CONT_SOL;
336}
337
338/*
339 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
340 */
341 int
342compl_status_local(void)
343{
344 return compl_cont_status & CONT_LOCAL;
345}
346
347/*
348 * Clear the completion status flags
349 */
350 void
351compl_status_clear(void)
352{
353 compl_cont_status = 0;
354}
355
356/*
357 * Return TRUE if completion is using the forward direction matches
358 */
359 static int
360compl_dir_forward(void)
361{
362 return compl_direction == FORWARD;
363}
364
365/*
366 * Return TRUE if currently showing forward completion matches
367 */
368 static int
369compl_shows_dir_forward(void)
370{
371 return compl_shows_dir == FORWARD;
372}
373
374/*
375 * Return TRUE if currently showing backward completion matches
376 */
377 static int
378compl_shows_dir_backward(void)
379{
380 return compl_shows_dir == BACKWARD;
381}
382
383/*
384 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100385 */
386 int
387has_compl_option(int dict_opt)
388{
389 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200390#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200392#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100393 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100394 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
395#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100396 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100397#endif
398 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100399 {
400 ctrl_x_mode = CTRL_X_NORMAL;
401 edit_submode = NULL;
402 msg_attr(dict_opt ? _("'dictionary' option is empty")
403 : _("'thesaurus' option is empty"),
404 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100405 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100406 {
407 vim_beep(BO_COMPL);
408 setcursor();
409 out_flush();
410#ifdef FEAT_EVAL
411 if (!get_vim_var_nr(VV_TESTING))
412#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100413 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100414 }
415 return FALSE;
416 }
417 return TRUE;
418}
419
420/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000421 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100422 * This depends on the current mode.
423 */
424 int
425vim_is_ctrl_x_key(int c)
426{
427 // Always allow ^R - let its results then be checked
428 if (c == Ctrl_R)
429 return TRUE;
430
431 // Accept <PageUp> and <PageDown> if the popup menu is visible.
432 if (ins_compl_pum_key(c))
433 return TRUE;
434
435 switch (ctrl_x_mode)
436 {
437 case 0: // Not in any CTRL-X mode
438 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
439 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200440 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100441 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
442 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
443 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
444 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
445 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200446 || c == Ctrl_S || c == Ctrl_K || c == 's'
447 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100448 case CTRL_X_SCROLL:
449 return (c == Ctrl_Y || c == Ctrl_E);
450 case CTRL_X_WHOLE_LINE:
451 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_FILES:
453 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_DICTIONARY:
455 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_THESAURUS:
457 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_TAGS:
459 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
460#ifdef FEAT_FIND_ID
461 case CTRL_X_PATH_PATTERNS:
462 return (c == Ctrl_P || c == Ctrl_N);
463 case CTRL_X_PATH_DEFINES:
464 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
465#endif
466 case CTRL_X_CMDLINE:
467 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
468 || c == Ctrl_X);
469#ifdef FEAT_COMPL_FUNC
470 case CTRL_X_FUNCTION:
471 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
472 case CTRL_X_OMNI:
473 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
474#endif
475 case CTRL_X_SPELL:
476 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
477 case CTRL_X_EVAL:
478 return (c == Ctrl_P || c == Ctrl_N);
479 }
480 internal_error("vim_is_ctrl_x_key()");
481 return FALSE;
482}
483
484/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000485 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000486 */
487 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000488match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000489{
490 return match->cp_flags & CP_ORIGINAL_TEXT;
491}
492
493/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000494 * Returns TRUE if "match" is the first match in the completion list.
495 */
496 static int
497is_first_match(compl_T *match)
498{
499 return match == compl_first_match;
500}
501
502/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100503 * Return TRUE when character "c" is part of the item currently being
504 * completed. Used to decide whether to abandon complete mode when the menu
505 * is visible.
506 */
507 int
508ins_compl_accept_char(int c)
509{
510 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
511 // When expanding an identifier only accept identifier chars.
512 return vim_isIDc(c);
513
514 switch (ctrl_x_mode)
515 {
516 case CTRL_X_FILES:
517 // When expanding file name only accept file name chars. But not
518 // path separators, so that "proto/<Tab>" expands files in
519 // "proto", not "proto/" as a whole
520 return vim_isfilec(c) && !vim_ispathsep(c);
521
522 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200523 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100524 case CTRL_X_OMNI:
525 // Command line and Omni completion can work with just about any
526 // printable character, but do stop at white space.
527 return vim_isprintc(c) && !VIM_ISWHITE(c);
528
529 case CTRL_X_WHOLE_LINE:
530 // For while line completion a space can be part of the line.
531 return vim_isprintc(c);
532 }
533 return vim_iswordc(c);
534}
535
536/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000537 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100538 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000539 */
540 static char_u *
541ins_compl_infercase_gettext(
542 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100543 int char_len,
544 int compl_char_len,
545 int min_len,
546 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000547{
548 int *wca; // Wide character array.
549 char_u *p;
550 int i, c;
551 int has_lower = FALSE;
552 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100553 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000554
555 IObuff[0] = NUL;
556
557 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100558 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000559 if (wca == NULL)
560 return IObuff;
561
562 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100563 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100564 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (has_mbyte)
566 wca[i] = mb_ptr2char_adv(&p);
567 else
568 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100569 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000570
571 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100572 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000573 for (i = 0; i < min_len; ++i)
574 {
glepnir6e199932024-12-14 21:13:27 +0100575 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000576 if (MB_ISLOWER(c))
577 {
578 has_lower = TRUE;
579 if (MB_ISUPPER(wca[i]))
580 {
581 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100582 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000583 wca[i] = MB_TOLOWER(wca[i]);
584 break;
585 }
586 }
587 }
588
589 // Rule 2: No lower case, 2nd consecutive letter converted to
590 // upper case.
591 if (!has_lower)
592 {
John Marriott5e6ea922024-11-23 14:01:57 +0100593 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 for (i = 0; i < min_len; ++i)
595 {
glepnir6e199932024-12-14 21:13:27 +0100596 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000597 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
598 {
599 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100600 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000601 wca[i] = MB_TOUPPER(wca[i]);
602 break;
603 }
604 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
605 }
606 }
607
608 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100609 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000610 for (i = 0; i < min_len; ++i)
611 {
glepnir6e199932024-12-14 21:13:27 +0100612 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000613 if (MB_ISLOWER(c))
614 wca[i] = MB_TOLOWER(wca[i]);
615 else if (MB_ISUPPER(c))
616 wca[i] = MB_TOUPPER(wca[i]);
617 }
618
619 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000620 p = IObuff;
621 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100622 ga_init2(&gap, 1, 500);
623 while (i < char_len)
624 {
625 if (gap.ga_data != NULL)
626 {
627 if (ga_grow(&gap, 10) == FAIL)
628 {
629 ga_clear(&gap);
630 return (char_u *)"[failed]";
631 }
632 p = (char_u *)gap.ga_data + gap.ga_len;
633 if (has_mbyte)
634 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
635 else
636 {
637 *p = wca[i++];
638 ++gap.ga_len;
639 }
640 }
641 else if ((p - IObuff) + 6 >= IOSIZE)
642 {
643 // Multi-byte characters can occupy up to five bytes more than
644 // ASCII characters, and we also need one byte for NUL, so when
645 // getting to six bytes from the edge of IObuff switch to using a
646 // growarray. Add the character in the next round.
647 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100648 {
649 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100650 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100651 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100652 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100653 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100654 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100655 }
656 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000657 p += (*mb_char2bytes)(wca[i++], p);
658 else
659 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000661 vim_free(wca);
662
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 if (gap.ga_data != NULL)
664 {
665 *tofree = gap.ga_data;
666 return gap.ga_data;
667 }
668
669 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000670 return IObuff;
671}
672
673/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100674 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
675 * case of the originally typed text is used, and the case of the completed
676 * text is inferred, ie this tries to work out what case you probably wanted
677 * the rest of the word to be in -- webb
678 */
679 int
680ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200681 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 int len,
683 int icase,
684 char_u *fname,
685 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200686 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200688 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100689 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100690 int char_len; // count multi-byte characters
691 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200693 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100694 int res;
695 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100696
697 if (p_ic && curbuf->b_p_inf && len > 0)
698 {
699 // Infer case of completed part.
700
701 // Find actual length of completion.
702 if (has_mbyte)
703 {
704 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100705 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100706 while (*p != NUL)
707 {
708 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100709 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100710 }
711 }
712 else
glepnir6e199932024-12-14 21:13:27 +0100713 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100715 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716
717 // Find actual length of original text.
718 if (has_mbyte)
719 {
John Marriott5e6ea922024-11-23 14:01:57 +0100720 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722 while (*p != NUL)
723 {
724 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100725 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100726 }
727 }
728 else
glepnir6e199932024-12-14 21:13:27 +0100729 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100730 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100731 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100734 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100735 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 str = ins_compl_infercase_gettext(str, char_len,
738 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200740 if (cont_s_ipos)
741 flags |= CP_CONT_S_IPOS;
742 if (icase)
743 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200744
glepnir5c66e232024-11-15 19:58:27 +0100745 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100746 vim_free(tofree);
747 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100748}
749
750/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000751 * Add a match to the list of matches. The arguments are:
752 * str - text of the match to add
753 * len - length of "str". If -1, then the length of "str" is
754 * computed.
755 * fname - file name to associate with this match.
756 * cptext - list of strings to use with this match (for abbr, menu, info
757 * and kind)
758 * user_data - user supplied data (any vim type) for this match
759 * cdir - match direction. If 0, use "compl_direction".
760 * flags_arg - match flags (cp_flags)
761 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100762 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000763 * If "cdir" is FORWARD, then the match is added after the current match.
764 * Otherwise, it is added before the current match.
765 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100766 * If the given string is already in the list of completions, then return
767 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
768 * maybe because alloc() returns NULL, then FAIL is returned.
769 */
770 static int
771ins_compl_add(
772 char_u *str,
773 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 char_u **cptext, // extra text for popup menu or NULL
776 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200778 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200779 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100780 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100781{
782 compl_T *match;
783 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200784 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100785
Bram Moolenaarceb06192021-04-04 15:05:22 +0200786 if (flags & CP_FAST)
787 fast_breakcheck();
788 else
789 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100790 if (got_int)
791 return FAIL;
792 if (len < 0)
793 len = (int)STRLEN(str);
794
795 // If the same match is already present, don't add it.
796 if (compl_first_match != NULL && !adup)
797 {
798 match = compl_first_match;
799 do
800 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000801 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100802 && STRNCMP(match->cp_str.string, str, len) == 0
803 && ((int)match->cp_str.length <= len
804 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 return NOTDONE;
806 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000807 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 }
809
810 // Remove any popup menu before changing the list of matches.
811 ins_compl_del_pum();
812
813 // Allocate a new match structure.
814 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200815 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100816 if (match == NULL)
817 return FAIL;
818 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200819 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100820 match->cp_number = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100821 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100822 {
823 vim_free(match);
824 return FAIL;
825 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100826
John Marriott5e6ea922024-11-23 14:01:57 +0100827 match->cp_str.length = len;
828
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 // match-fname is:
830 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200831 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100832 // - NULL otherwise. --Acevedo
833 if (fname != NULL
834 && compl_curr_match != NULL
835 && compl_curr_match->cp_fname != NULL
836 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
837 match->cp_fname = compl_curr_match->cp_fname;
838 else if (fname != NULL)
839 {
840 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200841 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100842 }
843 else
844 match->cp_fname = NULL;
845 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100846 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
847 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100848
849 if (cptext != NULL)
850 {
851 int i;
852
853 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100854 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100855 if (cptext[i] != NULL && *cptext[i] != NUL)
856 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100857 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100858 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100859#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100860 if (user_data != NULL)
861 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100862#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000864 // Link the new match structure after (FORWARD) or before (BACKWARD) the
865 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100866 if (compl_first_match == NULL)
867 match->cp_next = match->cp_prev = NULL;
868 else if (dir == FORWARD)
869 {
870 match->cp_next = compl_curr_match->cp_next;
871 match->cp_prev = compl_curr_match;
872 }
873 else // BACKWARD
874 {
875 match->cp_next = compl_curr_match;
876 match->cp_prev = compl_curr_match->cp_prev;
877 }
878 if (match->cp_next)
879 match->cp_next->cp_prev = match;
880 if (match->cp_prev)
881 match->cp_prev->cp_next = match;
882 else // if there's nothing before, it is the first match
883 compl_first_match = match;
884 compl_curr_match = match;
885
886 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200887 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100888 ins_compl_longest_match(match);
889
890 return OK;
891}
892
893/*
894 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100896 */
897 static int
898ins_compl_equal(compl_T *match, char_u *str, int len)
899{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200900 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200901 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200902 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100903 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
904 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100905}
906
907/*
glepnir6a38aff2024-12-16 21:56:16 +0100908 * when len is -1 mean use whole length of p otherwise part of p
909 */
910 static void
911ins_compl_insert_bytes(char_u *p, int len)
912{
913 if (len == -1)
914 len = (int)STRLEN(p);
915 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100916 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100917}
918
919/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100920 * Checks if the column is within the currently inserted completion text
921 * column range. If it is, it returns a special highlight attribute.
922 * -1 mean normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100923 */
924 int
925ins_compl_col_range_attr(int col)
926{
927 if (col >= compl_col && col < compl_ins_end_col)
928 return syn_name2attr((char_u *)"ComplMatchIns");
929
930 return -1;
931}
932
933/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100934 * Reduce the longest common string for match "match".
935 */
936 static void
937ins_compl_longest_match(compl_T *match)
938{
939 char_u *p, *s;
940 int c1, c2;
941 int had_match;
942
John Marriott5e6ea922024-11-23 14:01:57 +0100943 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100944 {
945 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100946 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
947 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000948 return;
949
John Marriott5e6ea922024-11-23 14:01:57 +0100950 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000951 had_match = (curwin->w_cursor.col > compl_col);
952 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100953 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000954 ins_redraw(FALSE);
955
956 // When the match isn't there (to avoid matching itself) remove it
957 // again after redrawing.
958 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100959 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100960 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000961
962 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100963 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000964
965 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100966 p = compl_leader.string;
967 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000968 while (*p != NUL)
969 {
970 if (has_mbyte)
971 {
972 c1 = mb_ptr2char(p);
973 c2 = mb_ptr2char(s);
974 }
975 else
976 {
977 c1 = *p;
978 c2 = *s;
979 }
980 if ((match->cp_flags & CP_ICASE)
981 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
982 break;
983 if (has_mbyte)
984 {
985 MB_PTR_ADV(p);
986 MB_PTR_ADV(s);
987 }
988 else
989 {
990 ++p;
991 ++s;
992 }
993 }
994
995 if (*p != NUL)
996 {
997 // Leader was shortened, need to change the inserted text.
998 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +0100999 compl_leader.length = (size_t)(p - compl_leader.string);
1000
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001001 had_match = (curwin->w_cursor.col > compl_col);
1002 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001003 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001004 ins_redraw(FALSE);
1005
1006 // When the match isn't there (to avoid matching itself) remove it
1007 // again after redrawing.
1008 if (!had_match)
1009 ins_compl_delete();
1010 }
1011
1012 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001013}
1014
1015/*
1016 * Add an array of matches to the list of matches.
1017 * Frees matches[].
1018 */
1019 static void
1020ins_compl_add_matches(
1021 int num_matches,
1022 char_u **matches,
1023 int icase)
1024{
1025 int i;
1026 int add_r = OK;
1027 int dir = compl_direction;
1028
1029 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001030 {
1031 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1032 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1033 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001034 // if dir was BACKWARD then honor it just once
1035 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001036 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001037 FreeWild(num_matches, matches);
1038}
1039
1040/*
1041 * Make the completion list cyclic.
1042 * Return the number of matches (excluding the original).
1043 */
1044 static int
1045ins_compl_make_cyclic(void)
1046{
1047 compl_T *match;
1048 int count = 0;
1049
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001050 if (compl_first_match == NULL)
1051 return 0;
1052
1053 // Find the end of the list.
1054 match = compl_first_match;
1055 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001056 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001058 match = match->cp_next;
1059 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001060 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001061 match->cp_next = compl_first_match;
1062 compl_first_match->cp_prev = match;
1063
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001064 return count;
1065}
1066
1067/*
1068 * Return whether there currently is a shown match.
1069 */
1070 int
1071ins_compl_has_shown_match(void)
1072{
1073 return compl_shown_match == NULL
1074 || compl_shown_match != compl_shown_match->cp_next;
1075}
1076
1077/*
1078 * Return whether the shown match is long enough.
1079 */
1080 int
1081ins_compl_long_shown_match(void)
1082{
John Marriott5e6ea922024-11-23 14:01:57 +01001083 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001084 > curwin->w_cursor.col - compl_col;
1085}
1086
1087/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001088 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001089 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001090 unsigned int
1091get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092{
zeertzjq529b9ad2024-06-05 20:27:06 +02001093 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001094}
1095
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001096/*
1097 * Update the screen and when there is any scrolling remove the popup menu.
1098 */
1099 static void
1100ins_compl_upd_pum(void)
1101{
1102 int h;
1103
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001104 if (compl_match_array == NULL)
1105 return;
1106
1107 h = curwin->w_cline_height;
1108 // Update the screen later, before drawing the popup menu over it.
1109 pum_call_update_screen();
1110 if (h != curwin->w_cline_height)
1111 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001112}
1113
1114/*
1115 * Remove any popup menu.
1116 */
1117 static void
1118ins_compl_del_pum(void)
1119{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001120 if (compl_match_array == NULL)
1121 return;
1122
1123 pum_undisplay();
1124 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001125}
1126
1127/*
1128 * Return TRUE if the popup menu should be displayed.
1129 */
1130 int
1131pum_wanted(void)
1132{
1133 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001134 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001135 return FALSE;
1136
1137 // The display looks bad on a B&W display.
1138 if (t_colors < 8
1139#ifdef FEAT_GUI
1140 && !gui.in_use
1141#endif
1142 )
1143 return FALSE;
1144 return TRUE;
1145}
1146
1147/*
1148 * Return TRUE if there are two or more matches to be shown in the popup menu.
1149 * One if 'completopt' contains "menuone".
1150 */
1151 static int
1152pum_enough_matches(void)
1153{
1154 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001155 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156
1157 // Don't display the popup menu if there are no matches or there is only
1158 // one (ignoring the original text).
1159 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001160 do
1161 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001162 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163 break;
1164 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001165 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001166
zeertzjq529b9ad2024-06-05 20:27:06 +02001167 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001168 return (i >= 1);
1169 return (i >= 2);
1170}
1171
Bram Moolenaar3075a452021-11-17 15:51:52 +00001172#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001173/*
1174 * Allocate Dict for the completed item.
1175 * { word, abbr, menu, kind, info }
1176 */
1177 static dict_T *
1178ins_compl_dict_alloc(compl_T *match)
1179{
1180 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1181
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001182 if (dict == NULL)
1183 return NULL;
1184
John Marriott5e6ea922024-11-23 14:01:57 +01001185 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001186 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1187 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1188 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1189 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1190 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1191 dict_add_string(dict, "user_data", (char_u *)"");
1192 else
1193 dict_add_tv(dict, "user_data", &match->cp_user_data);
1194
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001195 return dict;
1196}
1197
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001198/*
1199 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1200 * completion menu is changed.
1201 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001202 static void
1203trigger_complete_changed_event(int cur)
1204{
1205 dict_T *v_event;
1206 dict_T *item;
1207 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001208 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001209
1210 if (recursive)
1211 return;
1212
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001213 if (cur < 0)
1214 item = dict_alloc();
1215 else
1216 item = ins_compl_dict_alloc(compl_curr_match);
1217 if (item == NULL)
1218 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001219 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001220 dict_add_dict(v_event, "completed_item", item);
1221 pum_set_event_info(v_event);
1222 dict_set_items_ro(v_event);
1223
1224 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001225 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001226 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001227 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001228 recursive = FALSE;
1229
Bram Moolenaar3075a452021-11-17 15:51:52 +00001230 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001231}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001232#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001233
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001234/*
glepnira218cc62024-06-03 19:32:39 +02001235 * pumitem qsort compare func
1236 */
1237 static int
zeertzjq8e567472024-06-14 20:04:42 +02001238ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001239{
1240 const int sa = (*(pumitem_T *)a).pum_score;
1241 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001242 const int ia = (*(pumitem_T *)a).pum_idx;
1243 const int ib = (*(pumitem_T *)b).pum_idx;
1244 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001245}
1246
1247/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001248 * Build a popup menu to show the completion matches.
1249 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1250 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001251 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001252 static int
1253ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001254{
1255 compl_T *compl;
1256 compl_T *shown_compl = NULL;
1257 int did_find_shown_match = FALSE;
1258 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001259 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001260 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001261 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001262 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001263 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1264 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001265 compl_T *match_head = NULL;
1266 compl_T *match_tail = NULL;
1267 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001268
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001269 // Need to build the popup menu list.
1270 compl_match_arraysize = 0;
1271 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001272
glepnira49c0772024-11-30 10:56:30 +01001273 // If the current match is the original text don't find the first
1274 // match after it, don't highlight anything.
1275 if (match_at_original_text(compl_shown_match))
1276 shown_match_ok = TRUE;
1277
1278 if (compl_leader.string != NULL
1279 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1280 && shown_match_ok == FALSE)
1281 compl_shown_match = compl_no_select ? compl_first_match
1282 : compl_first_match->cp_next;
1283
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001284 do
1285 {
glepnird4088ed2024-12-31 10:55:22 +01001286 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001287 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1288 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001289 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1290 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001291
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001292 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001293 && (compl_leader.string == NULL
1294 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001295 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001296 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001297 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001298 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001299 if (match_head == NULL)
1300 match_head = compl;
1301 else
glepnira49c0772024-11-30 10:56:30 +01001302 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001303 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001304
1305 if (!shown_match_ok && !compl_fuzzy_match)
1306 {
1307 if (compl == compl_shown_match || did_find_shown_match)
1308 {
1309 // This item is the shown match or this is the
1310 // first displayed item after the shown match.
1311 compl_shown_match = compl;
1312 did_find_shown_match = TRUE;
1313 shown_match_ok = TRUE;
1314 }
1315 else
1316 // Remember this displayed match for when the
1317 // shown match is just below it.
1318 shown_compl = compl;
1319 cur = i;
1320 }
1321 else if (compl_fuzzy_match)
1322 {
1323 if (i == 0)
1324 shown_compl = compl;
1325 // Update the maximum fuzzy score and the shown match
1326 // if the current item's score is higher
1327 if (compl->cp_score > max_fuzzy_score)
1328 {
1329 did_find_shown_match = TRUE;
1330 max_fuzzy_score = compl->cp_score;
1331 if (!compl_no_select)
1332 compl_shown_match = compl;
1333 }
1334
1335 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1336 {
1337 cur = i;
1338 shown_match_ok = TRUE;
1339 }
1340 }
1341 i++;
glepnir80b66202024-11-27 21:53:53 +01001342 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001343
glepnira218cc62024-06-03 19:32:39 +02001344 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001345 {
1346 did_find_shown_match = TRUE;
1347
1348 // When the original text is the shown match don't set
1349 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001350 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001351 shown_match_ok = TRUE;
1352
1353 if (!shown_match_ok && shown_compl != NULL)
1354 {
1355 // The shown match isn't displayed, set it to the
1356 // previously displayed match.
1357 compl_shown_match = shown_compl;
1358 shown_match_ok = TRUE;
1359 }
1360 }
glepnira49c0772024-11-30 10:56:30 +01001361 compl = compl->cp_next;
1362 } while (compl != NULL && !is_first_match(compl));
1363
1364 if (compl_match_arraysize == 0)
1365 return -1;
1366
1367 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1368 if (compl_match_array == NULL)
1369 return -1;
1370
1371 compl = match_head;
1372 i = 0;
1373 while (compl != NULL)
1374 {
glepnir6e199932024-12-14 21:13:27 +01001375 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1376 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001377 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1378 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1379 compl_match_array[i].pum_score = compl->cp_score;
1380 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1381 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001382 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1383 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001384 match_next = compl->cp_match_next;
1385 compl->cp_match_next = NULL;
1386 compl = match_next;
1387 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001388
John Marriott5e6ea922024-11-23 14:01:57 +01001389 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001390 {
1391 for (i = 0; i < compl_match_arraysize; i++)
1392 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001393 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001394 qsort(compl_match_array, (size_t)compl_match_arraysize,
1395 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001396 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001397 }
glepnira218cc62024-06-03 19:32:39 +02001398
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001399 if (!shown_match_ok) // no displayed match at all
1400 cur = -1;
1401
1402 return cur;
1403}
1404
1405/*
1406 * Show the popup menu for the list of matches.
1407 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1408 */
1409 void
1410ins_compl_show_pum(void)
1411{
1412 int i;
1413 int cur = -1;
1414 colnr_T col;
1415
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001416 if (!pum_wanted() || !pum_enough_matches())
1417 return;
1418
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001419 // Update the screen later, before drawing the popup menu over it.
1420 pum_call_update_screen();
1421
1422 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001423 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001424 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001425 else
1426 {
1427 // popup menu already exists, only need to find the current item.
1428 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001429 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001430 || compl_match_array[i].pum_text
1431 == compl_shown_match->cp_text[CPT_ABBR])
1432 {
1433 cur = i;
1434 break;
1435 }
1436 }
1437
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001438 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001439 {
1440#ifdef FEAT_EVAL
1441 if (compl_started && has_completechanged())
1442 trigger_complete_changed_event(cur);
1443#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001445 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001446
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001447 // In Replace mode when a $ is displayed at the end of the line only
1448 // part of the screen would be updated. We do need to redraw here.
1449 dollar_vcol = -1;
1450
1451 // Compute the screen column of the start of the completed text.
1452 // Use the cursor to get all wrapping and other settings right.
1453 col = curwin->w_cursor.col;
1454 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001455 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001456 pum_display(compl_match_array, compl_match_arraysize, cur);
1457 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001458
glepnircbb46b42024-02-03 18:11:13 +01001459 // After adding leader, set the current match to shown match.
1460 if (compl_started && compl_curr_match != compl_shown_match)
1461 compl_curr_match = compl_shown_match;
1462
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001463#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001464 if (has_completechanged())
1465 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001466#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001467}
1468
1469#define DICT_FIRST (1) // use just first element in "dict"
1470#define DICT_EXACT (2) // "dict" is the exact name of a file
1471
1472/*
glepnir40c1c332024-06-11 19:37:04 +02001473 * Get current completion leader
1474 */
1475 char_u *
1476ins_compl_leader(void)
1477{
John Marriott5e6ea922024-11-23 14:01:57 +01001478 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1479}
1480
1481/*
1482 * Get current completion leader length
1483 */
1484 size_t
1485ins_compl_leader_len(void)
1486{
1487 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001488}
1489
1490/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001491 * Add any identifiers that match the given pattern "pat" in the list of
1492 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001493 */
1494 static void
1495ins_compl_dictionaries(
1496 char_u *dict_start,
1497 char_u *pat,
1498 int flags, // DICT_FIRST and/or DICT_EXACT
1499 int thesaurus) // Thesaurus completion
1500{
1501 char_u *dict = dict_start;
1502 char_u *ptr;
1503 char_u *buf;
1504 regmatch_T regmatch;
1505 char_u **files;
1506 int count;
1507 int save_p_scs;
1508 int dir = compl_direction;
1509
1510 if (*dict == NUL)
1511 {
1512#ifdef FEAT_SPELL
1513 // When 'dictionary' is empty and spell checking is enabled use
1514 // "spell".
1515 if (!thesaurus && curwin->w_p_spell)
1516 dict = (char_u *)"spell";
1517 else
1518#endif
1519 return;
1520 }
1521
1522 buf = alloc(LSIZE);
1523 if (buf == NULL)
1524 return;
1525 regmatch.regprog = NULL; // so that we can goto theend
1526
1527 // If 'infercase' is set, don't use 'smartcase' here
1528 save_p_scs = p_scs;
1529 if (curbuf->b_p_inf)
1530 p_scs = FALSE;
1531
1532 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1533 // to only match at the start of a line. Otherwise just match the
1534 // pattern. Also need to double backslashes.
1535 if (ctrl_x_mode_line_or_eval())
1536 {
1537 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1538 size_t len;
1539
1540 if (pat_esc == NULL)
1541 goto theend;
1542 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001543 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001544 if (ptr == NULL)
1545 {
1546 vim_free(pat_esc);
1547 goto theend;
1548 }
1549 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1550 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1551 vim_free(pat_esc);
1552 vim_free(ptr);
1553 }
1554 else
1555 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001556 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001557 if (regmatch.regprog == NULL)
1558 goto theend;
1559 }
1560
1561 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1562 regmatch.rm_ic = ignorecase(pat);
1563 while (*dict != NUL && !got_int && !compl_interrupted)
1564 {
1565 // copy one dictionary file name into buf
1566 if (flags == DICT_EXACT)
1567 {
1568 count = 1;
1569 files = &dict;
1570 }
1571 else
1572 {
1573 // Expand wildcards in the dictionary name, but do not allow
1574 // backticks (for security, the 'dict' option may have been set in
1575 // a modeline).
1576 copy_option_part(&dict, buf, LSIZE, ",");
1577# ifdef FEAT_SPELL
1578 if (!thesaurus && STRCMP(buf, "spell") == 0)
1579 count = -1;
1580 else
1581# endif
1582 if (vim_strchr(buf, '`') != NULL
1583 || expand_wildcards(1, &buf, &count, &files,
1584 EW_FILE|EW_SILENT) != OK)
1585 count = 0;
1586 }
1587
1588# ifdef FEAT_SPELL
1589 if (count == -1)
1590 {
1591 // Complete from active spelling. Skip "\<" in the pattern, we
1592 // don't use it as a RE.
1593 if (pat[0] == '\\' && pat[1] == '<')
1594 ptr = pat + 2;
1595 else
1596 ptr = pat;
1597 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1598 }
1599 else
1600# endif
1601 if (count > 0) // avoid warning for using "files" uninit
1602 {
1603 ins_compl_files(count, files, thesaurus, flags,
1604 &regmatch, buf, &dir);
1605 if (flags != DICT_EXACT)
1606 FreeWild(count, files);
1607 }
1608 if (flags != 0)
1609 break;
1610 }
1611
1612theend:
1613 p_scs = save_p_scs;
1614 vim_regfree(regmatch.regprog);
1615 vim_free(buf);
1616}
1617
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001618/*
1619 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1620 * skipping the word at 'skip_word'. Returns OK on success.
1621 */
1622 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001623thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001624 char_u *fname,
1625 char_u **buf_arg,
1626 int dir,
1627 char_u *skip_word)
1628{
1629 int status = OK;
1630 char_u *ptr;
1631 char_u *wstart;
1632
1633 // Add the other matches on the line
1634 ptr = *buf_arg;
1635 while (!got_int)
1636 {
1637 // Find start of the next word. Skip white
1638 // space and punctuation.
1639 ptr = find_word_start(ptr);
1640 if (*ptr == NUL || *ptr == NL)
1641 break;
1642 wstart = ptr;
1643
1644 // Find end of the word.
1645 if (has_mbyte)
1646 // Japanese words may have characters in
1647 // different classes, only separate words
1648 // with single-byte non-word characters.
1649 while (*ptr != NUL)
1650 {
1651 int l = (*mb_ptr2len)(ptr);
1652
1653 if (l < 2 && !vim_iswordc(*ptr))
1654 break;
1655 ptr += l;
1656 }
1657 else
1658 ptr = find_word_end(ptr);
1659
1660 // Add the word. Skip the regexp match.
1661 if (wstart != skip_word)
1662 {
1663 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1664 fname, dir, FALSE);
1665 if (status == FAIL)
1666 break;
1667 }
1668 }
1669
1670 *buf_arg = ptr;
1671 return status;
1672}
1673
1674/*
1675 * Process "count" dictionary/thesaurus "files" and add the text matching
1676 * "regmatch".
1677 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001678 static void
1679ins_compl_files(
1680 int count,
1681 char_u **files,
1682 int thesaurus,
1683 int flags,
1684 regmatch_T *regmatch,
1685 char_u *buf,
1686 int *dir)
1687{
1688 char_u *ptr;
1689 int i;
1690 FILE *fp;
1691 int add_r;
1692
1693 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1694 {
1695 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001696 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001697 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001698 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001699 vim_snprintf((char *)IObuff, IOSIZE,
1700 _("Scanning dictionary: %s"), (char *)files[i]);
1701 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1702 }
1703
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001704 if (fp == NULL)
1705 continue;
1706
1707 // Read dictionary file line by line.
1708 // Check each line for a match.
1709 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001710 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001711 ptr = buf;
1712 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001715 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1716 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 add_r = ins_compl_add_infercase(regmatch->startp[0],
1718 (int)(ptr - regmatch->startp[0]),
1719 p_ic, files[i], *dir, FALSE);
1720 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001721 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001722 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001723 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001724 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001725 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001726 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001727 if (add_r == OK)
1728 // if dir was BACKWARD then honor it just once
1729 *dir = FORWARD;
1730 else if (add_r == FAIL)
1731 break;
1732 // avoid expensive call to vim_regexec() when at end
1733 // of line
1734 if (*ptr == '\n' || got_int)
1735 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001736 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001737 line_breakcheck();
1738 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001739 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001740 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001741 }
1742}
1743
1744/*
1745 * Find the start of the next word.
1746 * Returns a pointer to the first char of the word. Also stops at a NUL.
1747 */
1748 char_u *
1749find_word_start(char_u *ptr)
1750{
1751 if (has_mbyte)
1752 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1753 ptr += (*mb_ptr2len)(ptr);
1754 else
1755 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1756 ++ptr;
1757 return ptr;
1758}
1759
1760/*
1761 * Find the end of the word. Assumes it starts inside a word.
1762 * Returns a pointer to just after the word.
1763 */
1764 char_u *
1765find_word_end(char_u *ptr)
1766{
1767 int start_class;
1768
1769 if (has_mbyte)
1770 {
1771 start_class = mb_get_class(ptr);
1772 if (start_class > 1)
1773 while (*ptr != NUL)
1774 {
1775 ptr += (*mb_ptr2len)(ptr);
1776 if (mb_get_class(ptr) != start_class)
1777 break;
1778 }
1779 }
1780 else
1781 while (vim_iswordc(*ptr))
1782 ++ptr;
1783 return ptr;
1784}
1785
1786/*
1787 * Find the end of the line, omitting CR and NL at the end.
1788 * Returns a pointer to just after the line.
1789 */
1790 static char_u *
1791find_line_end(char_u *ptr)
1792{
1793 char_u *s;
1794
1795 s = ptr + STRLEN(ptr);
1796 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1797 --s;
1798 return s;
1799}
1800
1801/*
1802 * Free the list of completions
1803 */
1804 static void
1805ins_compl_free(void)
1806{
1807 compl_T *match;
1808 int i;
1809
John Marriott5e6ea922024-11-23 14:01:57 +01001810 VIM_CLEAR_STRING(compl_pattern);
1811 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001812
1813 if (compl_first_match == NULL)
1814 return;
1815
1816 ins_compl_del_pum();
1817 pum_clear();
1818
1819 compl_curr_match = compl_first_match;
1820 do
1821 {
1822 match = compl_curr_match;
1823 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001824 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001825 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001826 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001827 vim_free(match->cp_fname);
1828 for (i = 0; i < CPT_COUNT; ++i)
1829 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001830#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001831 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001832#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001833 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001834 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001835 compl_first_match = compl_curr_match = NULL;
1836 compl_shown_match = NULL;
1837 compl_old_match = NULL;
1838}
1839
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001840/*
1841 * Reset/clear the completion state.
1842 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001843 void
1844ins_compl_clear(void)
1845{
1846 compl_cont_status = 0;
1847 compl_started = FALSE;
1848 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001849 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001850 VIM_CLEAR_STRING(compl_pattern);
1851 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001852 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001853 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001854 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001855#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856 // clear v:completed_item
1857 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001858#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001859}
1860
1861/*
1862 * Return TRUE when Insert completion is active.
1863 */
1864 int
1865ins_compl_active(void)
1866{
1867 return compl_started;
1868}
1869
1870/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001871 * Return True when wp is the actual completion window
1872 */
1873 int
1874ins_compl_win_active(win_T *wp UNUSED)
1875{
1876 return ins_compl_active()
1877#if defined(FEAT_QUICKFIX)
1878 && (!wp->w_p_pvw
1879# ifdef FEAT_PROP_POPUP
1880 && !(wp->w_popup_flags & POPF_INFO)
1881# endif
1882 )
1883#endif
1884 ;
1885}
1886
1887/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001888 * Selected one of the matches. When FALSE the match was edited or using the
1889 * longest common string.
1890 */
1891 int
1892ins_compl_used_match(void)
1893{
1894 return compl_used_match;
1895}
1896
1897/*
1898 * Initialize get longest common string.
1899 */
1900 void
1901ins_compl_init_get_longest(void)
1902{
1903 compl_get_longest = FALSE;
1904}
1905
1906/*
1907 * Returns TRUE when insert completion is interrupted.
1908 */
1909 int
1910ins_compl_interrupted(void)
1911{
1912 return compl_interrupted;
1913}
1914
1915/*
1916 * Returns TRUE if the <Enter> key selects a match in the completion popup
1917 * menu.
1918 */
1919 int
1920ins_compl_enter_selects(void)
1921{
1922 return compl_enter_selects;
1923}
1924
1925/*
1926 * Return the column where the text starts that is being completed
1927 */
1928 colnr_T
1929ins_compl_col(void)
1930{
1931 return compl_col;
1932}
1933
1934/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001935 * Return the length in bytes of the text being completed
1936 */
1937 int
1938ins_compl_len(void)
1939{
1940 return compl_length;
1941}
1942
1943/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001944 * Delete one character before the cursor and show the subset of the matches
1945 * that match the word that is now before the cursor.
1946 * Returns the character to be used, NUL if the work is done and another char
1947 * to be got from the user.
1948 */
1949 int
1950ins_compl_bs(void)
1951{
1952 char_u *line;
1953 char_u *p;
1954
1955 line = ml_get_curline();
1956 p = line + curwin->w_cursor.col;
1957 MB_PTR_BACK(line, p);
1958
1959 // Stop completion when the whole word was deleted. For Omni completion
1960 // allow the word to be deleted, we won't match everything.
1961 // Respect the 'backspace' option.
1962 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001963 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1964 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001965 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1966 - compl_length < 0))
1967 return K_BS;
1968
1969 // Deleted more than what was used to find matches or didn't finish
1970 // finding all matches: need to look for matches all over again.
1971 if (curwin->w_cursor.col <= compl_col + compl_length
1972 || ins_compl_need_restart())
1973 ins_compl_restart();
1974
John Marriott5e6ea922024-11-23 14:01:57 +01001975 VIM_CLEAR_STRING(compl_leader);
1976 compl_leader.length = (size_t)((p - line) - compl_col);
1977 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1978 if (compl_leader.string == NULL)
1979 {
1980 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001981 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001982 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001983
1984 ins_compl_new_leader();
1985 if (compl_shown_match != NULL)
1986 // Make sure current match is not a hidden item.
1987 compl_curr_match = compl_shown_match;
1988 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001989}
1990
1991/*
1992 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1993 * be called.
1994 */
1995 static int
1996ins_compl_need_restart(void)
1997{
1998 // Return TRUE if we didn't complete finding matches or when the
1999 // 'completefunc' returned "always" in the "refresh" dictionary item.
2000 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002001 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002002 && compl_opt_refresh_always);
2003}
2004
2005/*
2006 * Called after changing "compl_leader".
2007 * Show the popup menu with a different set of matches.
2008 * May also search for matches again if the previous search was interrupted.
2009 */
2010 static void
2011ins_compl_new_leader(void)
2012{
2013 ins_compl_del_pum();
2014 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002015 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002016 compl_used_match = FALSE;
2017
2018 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002019 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002020 else
2021 {
2022#ifdef FEAT_SPELL
2023 spell_bad_len = 0; // need to redetect bad word
2024#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002025 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002026 // the popup menu display the changed text before the cursor. Set
2027 // "compl_restarting" to avoid that the first match is inserted.
2028 pum_call_update_screen();
2029#ifdef FEAT_GUI
2030 if (gui.in_use)
2031 {
2032 // Show the cursor after the match, not after the redrawn text.
2033 setcursor();
2034 out_flush_cursor(FALSE, FALSE);
2035 }
2036#endif
2037 compl_restarting = TRUE;
2038 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2039 compl_cont_status = 0;
2040 compl_restarting = FALSE;
2041 }
2042
2043 compl_enter_selects = !compl_used_match;
2044
2045 // Show the popup menu with a different set of matches.
2046 ins_compl_show_pum();
2047
2048 // Don't let Enter select the original text when there is no popup menu.
2049 if (compl_match_array == NULL)
2050 compl_enter_selects = FALSE;
2051}
2052
2053/*
2054 * Return the length of the completion, from the completion start column to
2055 * the cursor column. Making sure it never goes below zero.
2056 */
2057 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002058get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002059{
2060 int off = (int)curwin->w_cursor.col - (int)compl_col;
2061
2062 if (off < 0)
2063 return 0;
2064 return off;
2065}
2066
2067/*
2068 * Append one character to the match leader. May reduce the number of
2069 * matches.
2070 */
2071 void
2072ins_compl_addleader(int c)
2073{
2074 int cc;
2075
2076 if (stop_arrow() == FAIL)
2077 return;
2078 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2079 {
2080 char_u buf[MB_MAXBYTES + 1];
2081
2082 (*mb_char2bytes)(c, buf);
2083 buf[cc] = NUL;
2084 ins_char_bytes(buf, cc);
2085 if (compl_opt_refresh_always)
2086 AppendToRedobuff(buf);
2087 }
2088 else
2089 {
2090 ins_char(c);
2091 if (compl_opt_refresh_always)
2092 AppendCharToRedobuff(c);
2093 }
2094
2095 // If we didn't complete finding matches we must search again.
2096 if (ins_compl_need_restart())
2097 ins_compl_restart();
2098
2099 // When 'always' is set, don't reset compl_leader. While completing,
2100 // cursor doesn't point original position, changing compl_leader would
2101 // break redo.
2102 if (!compl_opt_refresh_always)
2103 {
John Marriott5e6ea922024-11-23 14:01:57 +01002104 VIM_CLEAR_STRING(compl_leader);
2105 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2106 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2107 compl_leader.length);
2108 if (compl_leader.string == NULL)
2109 {
2110 compl_leader.length = 0;
2111 return;
2112 }
2113
2114 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002115 }
2116}
2117
2118/*
2119 * Setup for finding completions again without leaving CTRL-X mode. Used when
2120 * BS or a key was typed while still searching for matches.
2121 */
2122 static void
2123ins_compl_restart(void)
2124{
2125 ins_compl_free();
2126 compl_started = FALSE;
2127 compl_matches = 0;
2128 compl_cont_status = 0;
2129 compl_cont_mode = 0;
2130}
2131
2132/*
2133 * Set the first match, the original text.
2134 */
2135 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002136ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002137{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002138 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002139 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2140 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002141 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002142 {
John Marriott5e6ea922024-11-23 14:01:57 +01002143 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002144 if (p != NULL)
2145 {
John Marriott5e6ea922024-11-23 14:01:57 +01002146 VIM_CLEAR_STRING(compl_first_match->cp_str);
2147 compl_first_match->cp_str.string = p;
2148 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002149 }
2150 }
2151 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002152 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002153 {
John Marriott5e6ea922024-11-23 14:01:57 +01002154 char_u *p = vim_strnsave(str, len);
2155 if (p != NULL)
2156 {
2157 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2158 compl_first_match->cp_prev->cp_str.string = p;
2159 compl_first_match->cp_prev->cp_str.length = len;
2160 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002161 }
2162}
2163
2164/*
2165 * Append one character to the match leader. May reduce the number of
2166 * matches.
2167 */
2168 void
2169ins_compl_addfrommatch(void)
2170{
2171 char_u *p;
2172 int len = (int)curwin->w_cursor.col - (int)compl_col;
2173 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002174
John Marriott5e6ea922024-11-23 14:01:57 +01002175 p = compl_shown_match->cp_str.string;
2176 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002177 {
John Marriott5e6ea922024-11-23 14:01:57 +01002178 size_t plen;
2179 compl_T *cp;
2180
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002181 // When still at the original match use the first entry that matches
2182 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002183 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002184 return;
2185
2186 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002187 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002188 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002189 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002190 {
John Marriott5e6ea922024-11-23 14:01:57 +01002191 if (compl_leader.string == NULL
2192 || ins_compl_equal(cp, compl_leader.string,
2193 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002194 {
John Marriott5e6ea922024-11-23 14:01:57 +01002195 p = cp->cp_str.string;
2196 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002197 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002198 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002199 }
John Marriott5e6ea922024-11-23 14:01:57 +01002200 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002201 return;
2202 }
2203 p += len;
2204 c = PTR2CHAR(p);
2205 ins_compl_addleader(c);
2206}
2207
2208/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002209 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002210 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2211 * compl_cont_mode and compl_cont_status.
2212 * Returns TRUE when the character is not to be inserted.
2213 */
2214 static int
2215set_ctrl_x_mode(int c)
2216{
2217 int retval = FALSE;
2218
2219 switch (c)
2220 {
2221 case Ctrl_E:
2222 case Ctrl_Y:
2223 // scroll the window one line up or down
2224 ctrl_x_mode = CTRL_X_SCROLL;
2225 if (!(State & REPLACE_FLAG))
2226 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2227 else
2228 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2229 edit_submode_pre = NULL;
2230 showmode();
2231 break;
2232 case Ctrl_L:
2233 // complete whole line
2234 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2235 break;
2236 case Ctrl_F:
2237 // complete filenames
2238 ctrl_x_mode = CTRL_X_FILES;
2239 break;
2240 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002241 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002242 ctrl_x_mode = CTRL_X_DICTIONARY;
2243 break;
2244 case Ctrl_R:
2245 // Register insertion without exiting CTRL-X mode
2246 // Simply allow ^R to happen without affecting ^X mode
2247 break;
2248 case Ctrl_T:
2249 // complete words from a thesaurus
2250 ctrl_x_mode = CTRL_X_THESAURUS;
2251 break;
2252#ifdef FEAT_COMPL_FUNC
2253 case Ctrl_U:
2254 // user defined completion
2255 ctrl_x_mode = CTRL_X_FUNCTION;
2256 break;
2257 case Ctrl_O:
2258 // omni completion
2259 ctrl_x_mode = CTRL_X_OMNI;
2260 break;
2261#endif
2262 case 's':
2263 case Ctrl_S:
2264 // complete spelling suggestions
2265 ctrl_x_mode = CTRL_X_SPELL;
2266#ifdef FEAT_SPELL
2267 ++emsg_off; // Avoid getting the E756 error twice.
2268 spell_back_to_badword();
2269 --emsg_off;
2270#endif
2271 break;
2272 case Ctrl_RSB:
2273 // complete tag names
2274 ctrl_x_mode = CTRL_X_TAGS;
2275 break;
2276#ifdef FEAT_FIND_ID
2277 case Ctrl_I:
2278 case K_S_TAB:
2279 // complete keywords from included files
2280 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2281 break;
2282 case Ctrl_D:
2283 // complete definitions from included files
2284 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2285 break;
2286#endif
2287 case Ctrl_V:
2288 case Ctrl_Q:
2289 // complete vim commands
2290 ctrl_x_mode = CTRL_X_CMDLINE;
2291 break;
2292 case Ctrl_Z:
2293 // stop completion
2294 ctrl_x_mode = CTRL_X_NORMAL;
2295 edit_submode = NULL;
2296 showmode();
2297 retval = TRUE;
2298 break;
2299 case Ctrl_P:
2300 case Ctrl_N:
2301 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2302 // just started ^X mode, or there were enough ^X's to cancel
2303 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2304 // do normal expansion when interrupting a different mode (say
2305 // ^X^F^X^P or ^P^X^X^P, see below)
2306 // nothing changes if interrupting mode 0, (eg, the flag
2307 // doesn't change when going to ADDING mode -- Acevedo
2308 if (!(compl_cont_status & CONT_INTRPT))
2309 compl_cont_status |= CONT_LOCAL;
2310 else if (compl_cont_mode != 0)
2311 compl_cont_status &= ~CONT_LOCAL;
2312 // FALLTHROUGH
2313 default:
2314 // If we have typed at least 2 ^X's... for modes != 0, we set
2315 // compl_cont_status = 0 (eg, as if we had just started ^X
2316 // mode).
2317 // For mode 0, we set "compl_cont_mode" to an impossible
2318 // value, in both cases ^X^X can be used to restart the same
2319 // mode (avoiding ADDING mode).
2320 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2321 // 'complete' and local ^P expansions respectively.
2322 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2323 // mode -- Acevedo
2324 if (c == Ctrl_X)
2325 {
2326 if (compl_cont_mode != 0)
2327 compl_cont_status = 0;
2328 else
2329 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2330 }
2331 ctrl_x_mode = CTRL_X_NORMAL;
2332 edit_submode = NULL;
2333 showmode();
2334 break;
2335 }
2336
2337 return retval;
2338}
2339
2340/*
glepnir1c5a1202024-12-04 20:27:34 +01002341 * Trigger CompleteDone event and adds relevant information to v:event
2342 */
2343 static void
2344trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2345{
2346#if defined(FEAT_EVAL)
2347 save_v_event_T save_v_event;
2348 dict_T *v_event = get_v_event(&save_v_event);
2349 char_u *mode_str = NULL;
2350
2351 mode = mode & ~CTRL_X_WANT_IDENT;
2352 if (ctrl_x_mode_names[mode])
2353 mode_str = (char_u *)ctrl_x_mode_names[mode];
2354
2355 (void)dict_add_string(v_event, "complete_word",
2356 word == NULL ? (char_u *)"" : word);
2357 (void)dict_add_string(v_event, "complete_type",
2358 mode_str != NULL ? mode_str : (char_u *)"");
2359
2360 dict_set_items_ro(v_event);
2361#endif
2362 ins_apply_autocmds(EVENT_COMPLETEDONE);
2363
2364#if defined(FEAT_EVAL)
2365 restore_v_event(v_event, &save_v_event);
2366#endif
2367}
2368
2369/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002370 * Stop insert completion mode
2371 */
2372 static int
2373ins_compl_stop(int c, int prev_mode, int retval)
2374{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002375 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002376 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002377
2378 // Get here when we have finished typing a sequence of ^N and
2379 // ^P or other completion characters in CTRL-X mode. Free up
2380 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002381 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002382 {
John Marriott5e6ea922024-11-23 14:01:57 +01002383 char_u *ptr;
2384
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002385 // If any of the original typed text has been changed, eg when
2386 // ignorecase is set, we must add back-spaces to the redo
2387 // buffer. We add as few as necessary to delete just the part
2388 // of the original text that has changed.
2389 // When using the longest match, edited the match or used
2390 // CTRL-E then don't use the current match.
2391 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002392 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002393 else
2394 ptr = NULL;
2395 ins_compl_fixRedoBufForLeader(ptr);
2396 }
2397
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002398 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002399
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002400 // When completing whole lines: fix indent for 'cindent'.
2401 // Otherwise, break line if it's too long.
2402 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2403 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002404 // re-indent the current line
2405 if (want_cindent)
2406 {
2407 do_c_expr_indent();
2408 want_cindent = FALSE; // don't do it again
2409 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002410 }
2411 else
2412 {
2413 int prev_col = curwin->w_cursor.col;
2414
2415 // put the cursor on the last char, for 'tw' formatting
2416 if (prev_col > 0)
2417 dec_cursor();
2418 // only format when something was inserted
2419 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2420 insertchar(NUL, 0, -1);
2421 if (prev_col > 0
2422 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2423 inc_cursor();
2424 }
2425
2426 // If the popup menu is displayed pressing CTRL-Y means accepting
2427 // the selection without inserting anything. When
2428 // compl_enter_selects is set the Enter key does the same.
2429 if ((c == Ctrl_Y || (compl_enter_selects
2430 && (c == CAR || c == K_KENTER || c == NL)))
2431 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002432 {
2433 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002434 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002435 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002436
2437 // CTRL-E means completion is Ended, go back to the typed text.
2438 // but only do this, if the Popup is still visible
2439 if (c == Ctrl_E)
2440 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002441 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002442 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002443
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002444 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002445 if (compl_leader.string != NULL)
2446 {
2447 p = compl_leader.string;
2448 plen = compl_leader.length;
2449 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002450 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002451 {
2452 p = compl_orig_text.string;
2453 plen = compl_orig_text.length;
2454 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002455 if (p != NULL)
2456 {
2457 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002458
John Marriott5e6ea922024-11-23 14:01:57 +01002459 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002460 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002461 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002462 retval = TRUE;
2463 }
2464
2465 auto_format(FALSE, TRUE);
2466
2467 // Trigger the CompleteDonePre event to give scripts a chance to
2468 // act upon the completion before clearing the info, and restore
2469 // ctrl_x_mode, so that complete_info() can be used.
2470 ctrl_x_mode = prev_mode;
2471 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2472
2473 ins_compl_free();
2474 compl_started = FALSE;
2475 compl_matches = 0;
2476 if (!shortmess(SHM_COMPLETIONMENU))
2477 msg_clr_cmdline(); // necessary for "noshowmode"
2478 ctrl_x_mode = CTRL_X_NORMAL;
2479 compl_enter_selects = FALSE;
2480 if (edit_submode != NULL)
2481 {
2482 edit_submode = NULL;
2483 showmode();
2484 }
2485
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002486 if (c == Ctrl_C && cmdwin_type != 0)
2487 // Avoid the popup menu remains displayed when leaving the
2488 // command line window.
2489 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002490 // Indent now if a key was typed that is in 'cinkeys'.
2491 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2492 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002493 // Trigger the CompleteDone event to give scripts a chance to act
2494 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002495 trigger_complete_done_event(prev_mode, word);
2496 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002497
2498 return retval;
2499}
2500
2501/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002502 * Prepare for Insert mode completion, or stop it.
2503 * Called just after typing a character in Insert mode.
2504 * Returns TRUE when the character is not to be inserted;
2505 */
2506 int
2507ins_compl_prep(int c)
2508{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002509 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002510 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002511
2512 // Forget any previous 'special' messages if this is actually
2513 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2514 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2515 edit_submode_extra = NULL;
2516
zeertzjq440d4cb2023-03-02 17:51:32 +00002517 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002518 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002519 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002520 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002521 return retval;
2522
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002523#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002524 // Ignore mouse events in a popup window
2525 if (is_mouse_key(c))
2526 {
2527 // Ignore drag and release events, the position does not need to be in
2528 // the popup and it may have just closed.
2529 if (c == K_LEFTRELEASE
2530 || c == K_LEFTRELEASE_NM
2531 || c == K_MIDDLERELEASE
2532 || c == K_RIGHTRELEASE
2533 || c == K_X1RELEASE
2534 || c == K_X2RELEASE
2535 || c == K_LEFTDRAG
2536 || c == K_MIDDLEDRAG
2537 || c == K_RIGHTDRAG
2538 || c == K_X1DRAG
2539 || c == K_X2DRAG)
2540 return retval;
2541 if (popup_visible)
2542 {
2543 int row = mouse_row;
2544 int col = mouse_col;
2545 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2546
2547 if (wp != NULL && WIN_IS_POPUP(wp))
2548 return retval;
2549 }
2550 }
2551#endif
2552
zeertzjqdca29d92021-08-31 19:12:51 +02002553 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2554 {
2555 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2556 || !vim_is_ctrl_x_key(c))
2557 {
2558 // Not starting another completion mode.
2559 ctrl_x_mode = CTRL_X_CMDLINE;
2560
2561 // CTRL-X CTRL-Z should stop completion without inserting anything
2562 if (c == Ctrl_Z)
2563 retval = TRUE;
2564 }
2565 else
2566 {
2567 ctrl_x_mode = CTRL_X_CMDLINE;
2568
2569 // Other CTRL-X keys first stop completion, then start another
2570 // completion mode.
2571 ins_compl_prep(' ');
2572 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2573 }
2574 }
2575
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002576 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002577 if (ctrl_x_mode_not_defined_yet()
2578 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002579 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002580 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002581 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582 }
2583
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002584 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002585 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2586 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002587 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002588 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002589 {
2590 // We're already in CTRL-X mode, do we stay in it?
2591 if (!vim_is_ctrl_x_key(c))
2592 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002593 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002594 ctrl_x_mode = CTRL_X_NORMAL;
2595 else
2596 ctrl_x_mode = CTRL_X_FINISHED;
2597 edit_submode = NULL;
2598 }
2599 showmode();
2600 }
2601
2602 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2603 {
2604 // Show error message from attempted keyword completion (probably
2605 // 'Pattern not found') until another key is hit, then go back to
2606 // showing what mode we are in.
2607 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002608 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002609 && c != Ctrl_R && !ins_compl_pum_key(c))
2610 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002611 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002612 }
2613 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2614 // Trigger the CompleteDone event to give scripts a chance to act
2615 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002616 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002617
LemonBoy2bf52dd2022-04-09 18:17:34 +01002618 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002619
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620 // reset continue_* if we left expansion-mode, if we stay they'll be
2621 // (re)set properly in ins_complete()
2622 if (!vim_is_ctrl_x_key(c))
2623 {
2624 compl_cont_status = 0;
2625 compl_cont_mode = 0;
2626 }
2627
2628 return retval;
2629}
2630
2631/*
2632 * Fix the redo buffer for the completion leader replacing some of the typed
2633 * text. This inserts backspaces and appends the changed text.
2634 * "ptr" is the known leader text or NUL.
2635 */
2636 static void
2637ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2638{
John Marriott5e6ea922024-11-23 14:01:57 +01002639 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002640 char_u *p;
2641 char_u *ptr = ptr_arg;
2642
2643 if (ptr == NULL)
2644 {
John Marriott5e6ea922024-11-23 14:01:57 +01002645 if (compl_leader.string != NULL)
2646 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002647 else
2648 return; // nothing to do
2649 }
John Marriott5e6ea922024-11-23 14:01:57 +01002650 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002651 {
John Marriott5e6ea922024-11-23 14:01:57 +01002652 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2654 ;
2655 if (len > 0)
2656 len -= (*mb_head_off)(p, p + len);
2657 for (p += len; *p != NUL; MB_PTR_ADV(p))
2658 AppendCharToRedobuff(K_BS);
2659 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002660 if (ptr != NULL)
2661 AppendToRedobuffLit(ptr + len, -1);
2662}
2663
2664/*
2665 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2666 * (depending on flag) starting from buf and looking for a non-scanned
2667 * buffer (other than curbuf). curbuf is special, if it is called with
2668 * buf=curbuf then it has to be the first call for a given flag/expansion.
2669 *
2670 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2671 */
2672 static buf_T *
2673ins_compl_next_buf(buf_T *buf, int flag)
2674{
2675 static win_T *wp = NULL;
2676
2677 if (flag == 'w') // just windows
2678 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002679 if (buf == curbuf || !win_valid(wp))
2680 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002681 wp = curwin;
2682 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2683 && wp->w_buffer->b_scanned)
2684 ;
2685 buf = wp->w_buffer;
2686 }
2687 else
2688 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2689 // (unlisted buffers)
2690 // When completing whole lines skip unloaded buffers.
2691 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2692 && ((flag == 'U'
2693 ? buf->b_p_bl
2694 : (!buf->b_p_bl
2695 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2696 || buf->b_scanned))
2697 ;
2698 return buf;
2699}
2700
2701#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002702
2703# ifdef FEAT_EVAL
2704static callback_T cfu_cb; // 'completefunc' callback function
2705static callback_T ofu_cb; // 'omnifunc' callback function
2706static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2707# endif
2708
2709/*
2710 * Copy a global callback function to a buffer local callback.
2711 */
2712 static void
2713copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2714{
2715 free_callback(bufcb);
2716 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2717 copy_callback(bufcb, globcb);
2718}
2719
2720/*
2721 * Parse the 'completefunc' option value and set the callback function.
2722 * Invoked when the 'completefunc' option is set. The option value can be a
2723 * name of a function (string), or function(<name>) or funcref(<name>) or a
2724 * lambda expression.
2725 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002726 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002727did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002728{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002729 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2730 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002731
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002733
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002734 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002735}
2736
2737/*
2738 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002739 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002740 */
2741 void
2742set_buflocal_cfu_callback(buf_T *buf UNUSED)
2743{
2744# ifdef FEAT_EVAL
2745 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2746# endif
2747}
2748
2749/*
2750 * Parse the 'omnifunc' option value and set the callback function.
2751 * Invoked when the 'omnifunc' option is set. The option value can be a
2752 * name of a function (string), or function(<name>) or funcref(<name>) or a
2753 * lambda expression.
2754 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002755 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002756did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002757{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002758 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2759 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002760
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761 set_buflocal_ofu_callback(curbuf);
2762 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002763}
2764
2765/*
2766 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002767 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002768 */
2769 void
2770set_buflocal_ofu_callback(buf_T *buf UNUSED)
2771{
2772# ifdef FEAT_EVAL
2773 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2774# endif
2775}
2776
2777/*
2778 * Parse the 'thesaurusfunc' option value and set the callback function.
2779 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2780 * name of a function (string), or function(<name>) or funcref(<name>) or a
2781 * lambda expression.
2782 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002783 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002784did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002785{
2786 int retval;
2787
zeertzjq6eda2692024-11-03 09:23:33 +01002788 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002789 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002790 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2791 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002792 else
2793 {
2794 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002796 // when using :set, free the local callback
2797 if (!(args->os_flags & OPT_GLOBAL))
2798 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002799 }
2800
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002801 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002802}
2803
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002804/*
2805 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002806 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002807 */
2808 int
2809set_ref_in_insexpand_funcs(int copyID)
2810{
2811 int abort = FALSE;
2812
2813 abort = set_ref_in_callback(&cfu_cb, copyID);
2814 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2815 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2816
2817 return abort;
2818}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002819
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002821 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002822 */
2823 static char_u *
2824get_complete_funcname(int type)
2825{
2826 switch (type)
2827 {
2828 case CTRL_X_FUNCTION:
2829 return curbuf->b_p_cfu;
2830 case CTRL_X_OMNI:
2831 return curbuf->b_p_ofu;
2832 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002833 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002834 default:
2835 return (char_u *)"";
2836 }
2837}
2838
2839/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002840 * Get the callback to use for insert mode completion.
2841 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002842 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002843get_insert_callback(int type)
2844{
2845 if (type == CTRL_X_FUNCTION)
2846 return &curbuf->b_cfu_cb;
2847 if (type == CTRL_X_OMNI)
2848 return &curbuf->b_ofu_cb;
2849 // CTRL_X_THESAURUS
2850 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2851}
2852
2853/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002854 * Execute user defined complete function 'completefunc', 'omnifunc' or
2855 * 'thesaurusfunc', and get matches in "matches".
2856 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002857 */
2858 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002859expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002860{
2861 list_T *matchlist = NULL;
2862 dict_T *matchdict = NULL;
2863 typval_T args[3];
2864 char_u *funcname;
2865 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002866 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002867 typval_T rettv;
2868 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002869 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002871 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002872 if (*funcname == NUL)
2873 return;
2874
2875 // Call 'completefunc' to obtain the list of matches.
2876 args[0].v_type = VAR_NUMBER;
2877 args[0].vval.v_number = 0;
2878 args[1].v_type = VAR_STRING;
2879 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2880 args[2].v_type = VAR_UNKNOWN;
2881
2882 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002883 // Lock the text to avoid weird things from happening. Also disallow
2884 // switching to another window, it should not be needed and may end up in
2885 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002886 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002887
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002888 cb = get_insert_callback(type);
2889 retval = call_callback(cb, 0, &rettv, 2, args);
2890
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002891 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002892 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893 {
2894 switch (rettv.v_type)
2895 {
2896 case VAR_LIST:
2897 matchlist = rettv.vval.v_list;
2898 break;
2899 case VAR_DICT:
2900 matchdict = rettv.vval.v_dict;
2901 break;
2902 case VAR_SPECIAL:
2903 if (rettv.vval.v_number == VVAL_NONE)
2904 compl_opt_suppress_empty = TRUE;
2905 // FALLTHROUGH
2906 default:
2907 // TODO: Give error message?
2908 clear_tv(&rettv);
2909 break;
2910 }
2911 }
zeertzjqcfe45652022-05-27 17:26:55 +01002912 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002913
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002914 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002915 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002916 validate_cursor();
2917 if (!EQUAL_POS(curwin->w_cursor, pos))
2918 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002919 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002920 goto theend;
2921 }
2922
2923 if (matchlist != NULL)
2924 ins_compl_add_list(matchlist);
2925 else if (matchdict != NULL)
2926 ins_compl_add_dict(matchdict);
2927
2928theend:
2929 // Restore State, it might have been changed.
2930 State = save_State;
2931
2932 if (matchdict != NULL)
2933 dict_unref(matchdict);
2934 if (matchlist != NULL)
2935 list_unref(matchlist);
2936}
2937#endif // FEAT_COMPL_FUNC
2938
2939#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002940
2941 static inline int
2942get_user_highlight_attr(char_u *hlname)
2943{
2944 if (hlname != NULL && *hlname != NUL)
2945 return syn_name2attr(hlname);
2946 return -1;
2947}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002948/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002949 * Add a match to the list of matches from a typeval_T.
2950 * If the given string is already in the list of completions, then return
2951 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2952 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002953 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002954 */
2955 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002956ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002957{
2958 char_u *word;
2959 int dup = FALSE;
2960 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002961 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002962 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002963 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002964 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002965 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002966 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002967 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002968
Bram Moolenaar08928322020-01-04 14:32:48 +01002969 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002970 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2971 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002972 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2973 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2974 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2975 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2976 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002977
glepnir0fe17f82024-10-08 22:26:44 +02002978 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002979 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002980
2981 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002982 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002983
Bram Moolenaard61efa52022-07-23 09:52:04 +01002984 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2985 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2986 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002987 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002988 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2989 dup = dict_get_number(tv->vval.v_dict, "dup");
2990 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2991 empty = dict_get_number(tv->vval.v_dict, "empty");
2992 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2993 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002994 flags |= CP_EQUAL;
2995 }
2996 else
2997 {
2998 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002999 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003000 }
3001 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003002 {
3003 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003004 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003005 }
glepnir508e7852024-07-25 21:39:08 +02003006 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003007 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003008 if (status != OK)
3009 clear_tv(&user_data);
3010 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003011}
3012
3013/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003014 * Add completions from a list.
3015 */
3016 static void
3017ins_compl_add_list(list_T *list)
3018{
3019 listitem_T *li;
3020 int dir = compl_direction;
3021
3022 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003023 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003024 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003025 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003026 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003027 // if dir was BACKWARD then honor it just once
3028 dir = FORWARD;
3029 else if (did_emsg)
3030 break;
3031 }
3032}
3033
3034/*
3035 * Add completions from a dict.
3036 */
3037 static void
3038ins_compl_add_dict(dict_T *dict)
3039{
3040 dictitem_T *di_refresh;
3041 dictitem_T *di_words;
3042
3043 // Check for optional "refresh" item.
3044 compl_opt_refresh_always = FALSE;
3045 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3046 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3047 {
3048 char_u *v = di_refresh->di_tv.vval.v_string;
3049
3050 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3051 compl_opt_refresh_always = TRUE;
3052 }
3053
3054 // Add completions from a "words" list.
3055 di_words = dict_find(dict, (char_u *)"words", 5);
3056 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3057 ins_compl_add_list(di_words->di_tv.vval.v_list);
3058}
3059
3060/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003061 * Start completion for the complete() function.
3062 * "startcol" is where the matched text starts (1 is first column).
3063 * "list" is the list of matches.
3064 */
3065 static void
3066set_completion(colnr_T startcol, list_T *list)
3067{
3068 int save_w_wrow = curwin->w_wrow;
3069 int save_w_leftcol = curwin->w_leftcol;
3070 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003071 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003072 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3073 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3074 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003075
3076 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003077 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003078 ins_compl_prep(' ');
3079 ins_compl_clear();
3080 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003081 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003082
3083 compl_direction = FORWARD;
3084 if (startcol > curwin->w_cursor.col)
3085 startcol = curwin->w_cursor.col;
3086 compl_col = startcol;
3087 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3088 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003089 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003090 if (p_ic)
3091 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003092 if (compl_orig_text.string == NULL)
3093 {
3094 compl_orig_text.length = 0;
3095 return;
3096 }
3097 compl_orig_text.length = (size_t)compl_length;
3098 if (ins_compl_add(compl_orig_text.string,
3099 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3100 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003101 return;
3102
3103 ctrl_x_mode = CTRL_X_EVAL;
3104
3105 ins_compl_add_list(list);
3106 compl_matches = ins_compl_make_cyclic();
3107 compl_started = TRUE;
3108 compl_used_match = TRUE;
3109 compl_cont_status = 0;
3110
3111 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003112 int no_select = compl_no_select || compl_longest;
3113 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003114 {
3115 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003116 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117 // Down/Up has no real effect.
3118 ins_complete(K_UP, FALSE);
3119 }
3120 else
3121 ins_complete(Ctrl_N, FALSE);
3122 compl_enter_selects = compl_no_insert;
3123
3124 // Lazily show the popup menu, unless we got interrupted.
3125 if (!compl_interrupted)
3126 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003127 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003128 out_flush();
3129}
3130
3131/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003132 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003133 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003134 void
3135f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003136{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003137 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003138
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003139 if (in_vim9script()
3140 && (check_for_number_arg(argvars, 0) == FAIL
3141 || check_for_list_arg(argvars, 1) == FAIL))
3142 return;
3143
Bram Moolenaar24959102022-05-07 20:01:16 +01003144 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003145 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003146 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003147 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003148 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003149
3150 // Check for undo allowed here, because if something was already inserted
3151 // the line was already saved for undo and this check isn't done.
3152 if (!undo_allowed())
3153 return;
3154
Bram Moolenaard83392a2022-09-01 12:22:46 +01003155 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003156 {
3157 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3158 if (startcol > 0)
3159 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003160 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003161}
3162
3163/*
3164 * "complete_add()" function
3165 */
3166 void
3167f_complete_add(typval_T *argvars, typval_T *rettv)
3168{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003169 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003170 return;
3171
Bram Moolenaar440cf092021-04-03 20:13:30 +02003172 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003173}
3174
3175/*
3176 * "complete_check()" function
3177 */
3178 void
3179f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3180{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003181 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003182 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003183
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003184 ins_compl_check_keys(0, TRUE);
3185 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003186
3187 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003188}
3189
3190/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003191 * Return Insert completion mode name string
3192 */
3193 static char_u *
3194ins_compl_mode(void)
3195{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003196 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003197 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3198
3199 return (char_u *)"";
3200}
3201
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003202/*
3203 * Assign the sequence number to all the completion matches which don't have
3204 * one assigned yet.
3205 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003206 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003207ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003208{
3209 int number = 0;
3210 compl_T *match;
3211
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003212 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003213 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003214 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003215 // This should normally succeed already at the first loop
3216 // cycle, so it's fast!
3217 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003218 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003219 if (match->cp_number != -1)
3220 {
3221 number = match->cp_number;
3222 break;
3223 }
3224 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003225 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003226 for (match = match->cp_next;
3227 match != NULL && match->cp_number == -1;
3228 match = match->cp_next)
3229 match->cp_number = ++number;
3230 }
3231 else // BACKWARD
3232 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003233 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003234 // number. This should normally succeed already at the
3235 // first loop cycle, so it's fast!
3236 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003237 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003238 if (match->cp_number != -1)
3239 {
3240 number = match->cp_number;
3241 break;
3242 }
3243 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003244 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003245 for (match = match->cp_prev; match
3246 && match->cp_number == -1;
3247 match = match->cp_prev)
3248 match->cp_number = ++number;
3249 }
3250}
3251
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003252/*
3253 * Get complete information
3254 */
3255 static void
3256get_complete_info(list_T *what_list, dict_T *retdict)
3257{
3258 int ret = OK;
3259 listitem_T *item;
3260#define CI_WHAT_MODE 0x01
3261#define CI_WHAT_PUM_VISIBLE 0x02
3262#define CI_WHAT_ITEMS 0x04
3263#define CI_WHAT_SELECTED 0x08
3264#define CI_WHAT_INSERTED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003265#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003266#define CI_WHAT_ALL 0xff
3267 int what_flag;
3268
3269 if (what_list == NULL)
glepnird4088ed2024-12-31 10:55:22 +01003270 what_flag = CI_WHAT_ALL & ~CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003271 else
3272 {
3273 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003274 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003275 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003276 {
3277 char_u *what = tv_get_string(&item->li_tv);
3278
3279 if (STRCMP(what, "mode") == 0)
3280 what_flag |= CI_WHAT_MODE;
3281 else if (STRCMP(what, "pum_visible") == 0)
3282 what_flag |= CI_WHAT_PUM_VISIBLE;
3283 else if (STRCMP(what, "items") == 0)
3284 what_flag |= CI_WHAT_ITEMS;
3285 else if (STRCMP(what, "selected") == 0)
3286 what_flag |= CI_WHAT_SELECTED;
3287 else if (STRCMP(what, "inserted") == 0)
3288 what_flag |= CI_WHAT_INSERTED;
glepnird4088ed2024-12-31 10:55:22 +01003289 else if (STRCMP(what, "matches") == 0)
3290 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003291 }
3292 }
3293
3294 if (ret == OK && (what_flag & CI_WHAT_MODE))
3295 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3296
3297 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3298 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3299
glepnird4088ed2024-12-31 10:55:22 +01003300 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED
3301 || what_flag & CI_WHAT_MATCHES))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003302 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003303 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003304 dict_T *di;
3305 compl_T *match;
3306 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003307 int has_items = what_flag & CI_WHAT_ITEMS;
3308 int has_matches = what_flag & CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003309
glepnird4088ed2024-12-31 10:55:22 +01003310 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003311 {
3312 li = list_alloc();
3313 if (li == NULL)
3314 return;
glepnird4088ed2024-12-31 10:55:22 +01003315 ret = dict_add_list(retdict, (has_matches && !has_items)
3316 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003317 }
3318 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3319 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3320 ins_compl_update_sequence_numbers();
3321 if (ret == OK && compl_first_match != NULL)
3322 {
3323 int list_idx = 0;
3324 match = compl_first_match;
3325 do
3326 {
3327 if (!match_at_original_text(match))
3328 {
glepnird4088ed2024-12-31 10:55:22 +01003329 if (has_items
3330 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003331 {
3332 di = dict_alloc();
3333 if (di == NULL)
3334 return;
3335 ret = list_append_dict(li, di);
3336 if (ret != OK)
3337 return;
John Marriott5e6ea922024-11-23 14:01:57 +01003338 dict_add_string(di, "word", match->cp_str.string);
Girish Palya8950bf72024-03-20 20:07:29 +01003339 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3340 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3341 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3342 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
glepnird4088ed2024-12-31 10:55:22 +01003343 if (has_matches && has_items)
3344 dict_add_bool(di, "match", match->cp_in_match_array);
Girish Palya8950bf72024-03-20 20:07:29 +01003345 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3346 // Add an empty string for backwards compatibility
3347 dict_add_string(di, "user_data", (char_u *)"");
3348 else
3349 dict_add_tv(di, "user_data", &match->cp_user_data);
3350 }
glepnird4088ed2024-12-31 10:55:22 +01003351 if (compl_curr_match != NULL
3352 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003353 selected_idx = list_idx;
3354 list_idx += 1;
3355 }
3356 match = match->cp_next;
3357 }
3358 while (match != NULL && !is_first_match(match));
3359 }
3360 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3361 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003362 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003363
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003364 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3365 {
3366 // TODO
3367 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003368}
3369
3370/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003371 * "complete_info()" function
3372 */
3373 void
3374f_complete_info(typval_T *argvars, typval_T *rettv)
3375{
3376 list_T *what_list = NULL;
3377
Bram Moolenaar93a10962022-06-16 11:42:09 +01003378 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003379 return;
3380
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003381 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3382 return;
3383
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003384 if (argvars[0].v_type != VAR_UNKNOWN)
3385 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003386 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003387 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003388 what_list = argvars[0].vval.v_list;
3389 }
3390 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003391}
3392#endif
3393
3394/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003395 * Returns TRUE when using a user-defined function for thesaurus completion.
3396 */
3397 static int
3398thesaurus_func_complete(int type UNUSED)
3399{
3400#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003401 return type == CTRL_X_THESAURUS
3402 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003403#else
3404 return FALSE;
3405#endif
3406}
3407
3408/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003409 * Return value of process_next_cpt_value()
3410 */
3411enum
3412{
3413 INS_COMPL_CPT_OK = 1,
3414 INS_COMPL_CPT_CONT,
3415 INS_COMPL_CPT_END
3416};
3417
3418/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003419 * state information used for getting the next set of insert completion
3420 * matches.
3421 */
3422typedef struct
3423{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003424 char_u *e_cpt_copy; // copy of 'complete'
3425 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003426 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003427 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003428 pos_T prev_match_pos; // previous match position
3429 int set_match_pos; // save first_match_pos/last_match_pos
3430 pos_T first_match_pos; // first match position
3431 pos_T last_match_pos; // last match position
3432 int found_all; // found all matches of a certain type.
3433 char_u *dict; // dictionary file to search
3434 int dict_f; // "dict" is an exact file name or not
3435} ins_compl_next_state_T;
3436
3437/*
3438 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003439 *
3440 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003441 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003443 * st->found_all - all matches of this type are found
3444 * st->ins_buf - search for completions in this buffer
3445 * st->first_match_pos - position of the first completion match
3446 * st->last_match_pos - position of the last completion match
3447 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003448 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003449 * st->dict - name of the dictionary or thesaurus file to search
3450 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003451 *
3452 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003453 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3454 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003455 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003456 */
3457 static int
3458process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003459 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003460 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003461 pos_T *start_match_pos,
3462 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003463{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003464 int compl_type = -1;
3465 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003466
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003467 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003468
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003469 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3470 st->e_cpt++;
3471
3472 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003473 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003474 st->ins_buf = curbuf;
3475 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003476 // Move the cursor back one character so that ^N can match the
3477 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003478 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003479 {
3480 // Move the cursor to after the last character in the
3481 // buffer, so that word at start of buffer is found
3482 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003483 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003484 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003486 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003487 compl_type = 0;
3488
3489 // Remember the first match so that the loop stops when we
3490 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003491 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003492 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003493 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003494 && (st->ins_buf = ins_compl_next_buf(
3495 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003496 {
3497 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003498 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003499 {
3500 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003501 st->first_match_pos.col = st->last_match_pos.col = 0;
3502 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3503 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003504 compl_type = 0;
3505 }
3506 else // unloaded buffer, scan like dictionary
3507 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003508 st->found_all = TRUE;
3509 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003510 {
3511 status = INS_COMPL_CPT_CONT;
3512 goto done;
3513 }
3514 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003515 st->dict = st->ins_buf->b_fname;
3516 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003517 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003518 if (!shortmess(SHM_COMPLETIONSCAN))
3519 {
3520 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3521 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3522 st->ins_buf->b_fname == NULL
3523 ? buf_spname(st->ins_buf)
3524 : st->ins_buf->b_sfname == NULL
3525 ? st->ins_buf->b_fname
3526 : st->ins_buf->b_sfname);
3527 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3528 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003529 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003530 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003531 status = INS_COMPL_CPT_END;
3532 else
3533 {
3534 if (ctrl_x_mode_line_or_eval())
3535 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003536 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003537 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003538 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003539 compl_type = CTRL_X_DICTIONARY;
3540 else
3541 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003542 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003543 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003544 st->dict = st->e_cpt;
3545 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003546 }
3547 }
3548#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003549 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003550 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003551 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003552 compl_type = CTRL_X_PATH_DEFINES;
3553#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003554 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003555 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003556 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003557 if (!shortmess(SHM_COMPLETIONSCAN))
3558 {
3559 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3560 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3561 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3562 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 }
3564 else
3565 compl_type = -1;
3566
3567 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003568 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003569
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003570 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003571 if (compl_type == -1)
3572 status = INS_COMPL_CPT_CONT;
3573 }
3574
3575done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003577 return status;
3578}
3579
3580#ifdef FEAT_FIND_ID
3581/*
3582 * Get the next set of identifiers or defines matching "compl_pattern" in
3583 * included files.
3584 */
3585 static void
3586get_next_include_file_completion(int compl_type)
3587{
John Marriott5e6ea922024-11-23 14:01:57 +01003588 find_pattern_in_path(compl_pattern.string, compl_direction,
3589 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003590 (compl_type == CTRL_X_PATH_DEFINES
3591 && !(compl_cont_status & CONT_SOL))
3592 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003593 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003594}
3595#endif
3596
3597/*
3598 * Get the next set of words matching "compl_pattern" in dictionary or
3599 * thesaurus files.
3600 */
3601 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003602get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003603{
3604#ifdef FEAT_COMPL_FUNC
3605 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003606 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003607 else
3608#endif
3609 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003610 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003611 : (compl_type == CTRL_X_THESAURUS
3612 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3613 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003614 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003615 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003616 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003617}
3618
3619/*
3620 * Get the next set of tag names matching "compl_pattern".
3621 */
3622 static void
3623get_next_tag_completion(void)
3624{
3625 int save_p_ic;
3626 char_u **matches;
3627 int num_matches;
3628
3629 // set p_ic according to p_ic, p_scs and pat for find_tags().
3630 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003631 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003632
3633 // Find up to TAG_MANY matches. Avoids that an enormous number
3634 // of matches is found when compl_pattern is empty
3635 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003636 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003637 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003638 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003639 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3640 ins_compl_add_matches(num_matches, matches, p_ic);
3641 g_tag_at_cursor = FALSE;
3642 p_ic = save_p_ic;
3643}
3644
3645/*
glepnir8159fb12024-07-17 20:32:54 +02003646 * Compare function for qsort
3647 */
3648static int compare_scores(const void *a, const void *b)
3649{
3650 int idx_a = *(const int *)a;
3651 int idx_b = *(const int *)b;
3652 int score_a = compl_fuzzy_scores[idx_a];
3653 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003654 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3655 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003656}
3657
3658/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 * Get the next set of filename matching "compl_pattern".
3660 */
3661 static void
3662get_next_filename_completion(void)
3663{
3664 char_u **matches;
3665 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003666 char_u *ptr;
3667 garray_T fuzzy_indices;
3668 int i;
3669 int score;
3670 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003671 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003672 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3673 char_u **sorted_matches;
3674 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003675 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003676
glepnirb9de1a02024-08-02 19:14:38 +02003677#ifdef BACKSLASH_IN_FILENAME
3678 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3679 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3680#else
3681 char pathsep = PATHSEP;
3682#endif
3683
glepnir8159fb12024-07-17 20:32:54 +02003684 if (in_fuzzy)
3685 {
glepnirb9de1a02024-08-02 19:14:38 +02003686#ifdef BACKSLASH_IN_FILENAME
3687 if (curbuf->b_p_csl[0] == 's')
3688 {
John Marriott5e6ea922024-11-23 14:01:57 +01003689 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003690 {
3691 if (leader[i] == '\\')
3692 leader[i] = '/';
3693 }
3694 }
3695 else if (curbuf->b_p_csl[0] == 'b')
3696 {
John Marriott5e6ea922024-11-23 14:01:57 +01003697 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003698 {
3699 if (leader[i] == '/')
3700 leader[i] = '\\';
3701 }
3702 }
3703#endif
3704 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003705 if (last_sep == NULL)
3706 {
3707 // No path separator or separator is the last character,
3708 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003709 VIM_CLEAR_STRING(compl_pattern);
3710 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3711 if (compl_pattern.string == NULL)
3712 return;
3713 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003714 }
3715 else if (*(last_sep + 1) == '\0')
3716 in_fuzzy = FALSE;
3717 else
3718 {
3719 // Split leader into path and file parts
3720 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003721 char_u *path_with_wildcard;
3722
3723 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003724 if (path_with_wildcard != NULL)
3725 {
John Marriott5e6ea922024-11-23 14:01:57 +01003726 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3727 VIM_CLEAR_STRING(compl_pattern);
3728 compl_pattern.string = path_with_wildcard;
3729 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003730
3731 // Move leader to the file part
3732 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003733 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003734 }
3735 }
glepnir8159fb12024-07-17 20:32:54 +02003736 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003737
John Marriott5e6ea922024-11-23 14:01:57 +01003738 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003739 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3740 return;
3741
3742 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003743 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003744#ifdef BACKSLASH_IN_FILENAME
3745 if (curbuf->b_p_csl[0] != NUL)
3746 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003747 for (i = 0; i < num_matches; ++i)
3748 {
glepnir8159fb12024-07-17 20:32:54 +02003749 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750 while (*ptr != NUL)
3751 {
3752 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3753 *ptr = '/';
3754 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3755 *ptr = '\\';
3756 ptr += (*mb_ptr2len)(ptr);
3757 }
3758 }
3759 }
3760#endif
glepnir8159fb12024-07-17 20:32:54 +02003761
3762 if (in_fuzzy)
3763 {
3764 ga_init2(&fuzzy_indices, sizeof(int), 10);
3765 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3766
3767 for (i = 0; i < num_matches; i++)
3768 {
3769 ptr = matches[i];
3770 score = fuzzy_match_str(ptr, leader);
3771 if (score > 0)
3772 {
3773 if (ga_grow(&fuzzy_indices, 1) == OK)
3774 {
3775 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3776 compl_fuzzy_scores[i] = score;
3777 fuzzy_indices.ga_len++;
3778 }
3779 }
3780 }
3781
Christian Brabandt220474d2024-07-20 13:26:44 +02003782 // prevent qsort from deref NULL pointer
3783 if (fuzzy_indices.ga_len > 0)
3784 {
3785 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3786 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003787
Christian Brabandt220474d2024-07-20 13:26:44 +02003788 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3789 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3790 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003791
Christian Brabandt220474d2024-07-20 13:26:44 +02003792 FreeWild(num_matches, matches);
3793 matches = sorted_matches;
3794 num_matches = fuzzy_indices.ga_len;
3795 }
glepnir6b6280c2024-07-27 16:25:45 +02003796 else if (leader_len > 0)
3797 {
3798 FreeWild(num_matches, matches);
3799 num_matches = 0;
3800 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003801
glepnir8159fb12024-07-17 20:32:54 +02003802 vim_free(compl_fuzzy_scores);
3803 ga_clear(&fuzzy_indices);
3804 }
3805
glepnir6b6280c2024-07-27 16:25:45 +02003806 if (num_matches > 0)
3807 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003808}
3809
3810/*
3811 * Get the next set of command-line completions matching "compl_pattern".
3812 */
3813 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003814get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003815{
3816 char_u **matches;
3817 int num_matches;
3818
John Marriott5e6ea922024-11-23 14:01:57 +01003819 if (expand_cmdline(&compl_xp, compl_pattern.string,
3820 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003821 ins_compl_add_matches(num_matches, matches, FALSE);
3822}
3823
3824/*
3825 * Get the next set of spell suggestions matching "compl_pattern".
3826 */
3827 static void
3828get_next_spell_completion(linenr_T lnum UNUSED)
3829{
3830#ifdef FEAT_SPELL
3831 char_u **matches;
3832 int num_matches;
3833
John Marriott5e6ea922024-11-23 14:01:57 +01003834 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003835 if (num_matches > 0)
3836 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003837 else
3838 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003839#endif
3840}
3841
3842/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003843 * Return the next word or line from buffer "ins_buf" at position
3844 * "cur_match_pos" for completion. The length of the match is set in "len".
3845 */
3846 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003847ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003848 buf_T *ins_buf, // buffer being scanned
3849 pos_T *cur_match_pos, // current match position
3850 int *match_len,
3851 int *cont_s_ipos) // next ^X<> will set initial_pos
3852{
3853 char_u *ptr;
3854 int len;
3855
3856 *match_len = 0;
3857 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3858 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003859 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003860 if (ctrl_x_mode_line_or_eval())
3861 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003862 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003863 {
3864 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3865 return NULL;
3866 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003867 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003868 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003869 {
3870 char_u *tmp_ptr = ptr;
3871
3872 ptr = skipwhite(tmp_ptr);
3873 len -= (int)(ptr - tmp_ptr);
3874 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003875 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003876 }
3877 else
3878 {
3879 char_u *tmp_ptr = ptr;
3880
John Marriott5e6ea922024-11-23 14:01:57 +01003881 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003882 {
3883 tmp_ptr += compl_length;
3884 // Skip if already inside a word.
3885 if (vim_iswordp(tmp_ptr))
3886 return NULL;
3887 // Find start of next word.
3888 tmp_ptr = find_word_start(tmp_ptr);
3889 }
3890 // Find end of this word.
3891 tmp_ptr = find_word_end(tmp_ptr);
3892 len = (int)(tmp_ptr - ptr);
3893
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003894 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003895 {
3896 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3897 {
3898 // Try next line, if any. the new word will be
3899 // "join" as if the normal command "J" was used.
3900 // IOSIZE is always greater than
3901 // compl_length, so the next STRNCPY always
3902 // works -- Acevedo
3903 STRNCPY(IObuff, ptr, len);
3904 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3905 tmp_ptr = ptr = skipwhite(ptr);
3906 // Find start of next word.
3907 tmp_ptr = find_word_start(tmp_ptr);
3908 // Find end of next word.
3909 tmp_ptr = find_word_end(tmp_ptr);
3910 if (tmp_ptr > ptr)
3911 {
3912 if (*ptr != ')' && IObuff[len - 1] != TAB)
3913 {
3914 if (IObuff[len - 1] != ' ')
3915 IObuff[len++] = ' ';
3916 // IObuf =~ "\k.* ", thus len >= 2
3917 if (p_js
3918 && (IObuff[len - 2] == '.'
3919 || (vim_strchr(p_cpo, CPO_JOINSP)
3920 == NULL
3921 && (IObuff[len - 2] == '?'
3922 || IObuff[len - 2] == '!'))))
3923 IObuff[len++] = ' ';
3924 }
3925 // copy as much as possible of the new word
3926 if (tmp_ptr - ptr >= IOSIZE - len)
3927 tmp_ptr = ptr + IOSIZE - len - 1;
3928 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3929 len += (int)(tmp_ptr - ptr);
3930 *cont_s_ipos = TRUE;
3931 }
3932 IObuff[len] = NUL;
3933 ptr = IObuff;
3934 }
3935 if (len == compl_length)
3936 return NULL;
3937 }
3938 }
3939
3940 *match_len = len;
3941 return ptr;
3942}
3943
3944/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003945 * Get the next set of words matching "compl_pattern" for default completion(s)
3946 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003947 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3948 * position "st->start_pos" in the "compl_direction" direction. If
3949 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3950 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003951 * Returns OK if a new next match is found, otherwise returns FAIL.
3952 */
3953 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003954get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003955{
3956 int found_new_match = FAIL;
3957 int save_p_scs;
3958 int save_p_ws;
3959 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003960 char_u *ptr = NULL;
3961 int len = 0;
3962 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3963 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003964
3965 // If 'infercase' is set, don't use 'smartcase' here
3966 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003967 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003968 p_scs = FALSE;
3969
3970 // Buffers other than curbuf are scanned from the beginning or the
3971 // end but never from the middle, thus setting nowrapscan in this
3972 // buffer is a good idea, on the other hand, we always set
3973 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3974 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003975 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003976 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003977 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003978 p_ws = TRUE;
3979 looped_around = FALSE;
3980 for (;;)
3981 {
3982 int cont_s_ipos = FALSE;
3983
3984 ++msg_silent; // Don't want messages for wrapscan.
3985
3986 // ctrl_x_mode_line_or_eval() || word-wise search that
3987 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003988 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003989 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01003990 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02003991 else if (in_fuzzy)
3992 found_new_match = search_for_fuzzy_match(st->ins_buf,
3993 st->cur_match_pos, leader, compl_direction,
3994 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003995 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003996 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01003997 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02003998 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003999 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004000 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004001 {
4002 // set "compl_started" even on fail
4003 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004004 st->first_match_pos = *st->cur_match_pos;
4005 st->last_match_pos = *st->cur_match_pos;
4006 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004007 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004008 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4009 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004010 {
4011 found_new_match = FAIL;
4012 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004013 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004014 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4015 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4016 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004017 {
4018 if (looped_around)
4019 found_new_match = FAIL;
4020 else
4021 looped_around = TRUE;
4022 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004023 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004024 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4025 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4026 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004027 {
4028 if (looped_around)
4029 found_new_match = FAIL;
4030 else
4031 looped_around = TRUE;
4032 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004033 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004034 if (found_new_match == FAIL)
4035 break;
4036
4037 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004038 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004039 && start_pos->lnum == st->cur_match_pos->lnum
4040 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004041 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004042
glepnir8159fb12024-07-17 20:32:54 +02004043 if (!in_fuzzy)
4044 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4045 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004046 if (ptr == NULL)
4047 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004048
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004049 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004050 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004051 0, cont_s_ipos) != NOTDONE)
4052 {
4053 found_new_match = OK;
4054 break;
4055 }
4056 }
4057 p_scs = save_p_scs;
4058 p_ws = save_p_ws;
4059
4060 return found_new_match;
4061}
4062
4063/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004064 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004065 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 */
4067 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004068get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004070 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004071
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004072 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004073 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 case -1:
4075 break;
4076#ifdef FEAT_FIND_ID
4077 case CTRL_X_PATH_PATTERNS:
4078 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004079 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004080 break;
4081#endif
4082
4083 case CTRL_X_DICTIONARY:
4084 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004085 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4086 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004087 break;
4088
4089 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004090 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004091 break;
4092
4093 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004094 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004095 break;
4096
4097 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004098 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004099 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004100 break;
4101
4102#ifdef FEAT_COMPL_FUNC
4103 case CTRL_X_FUNCTION:
4104 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004105 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106 break;
4107#endif
4108
4109 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004110 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004111 break;
4112
4113 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004114 found_new_match = get_next_default_completion(st, ini);
4115 if (found_new_match == FAIL && st->ins_buf == curbuf)
4116 st->found_all = TRUE;
4117 }
4118
4119 // check if compl_curr_match has changed, (e.g. other type of
4120 // expansion added something)
4121 if (type != 0 && compl_curr_match != compl_old_match)
4122 found_new_match = OK;
4123
4124 return found_new_match;
4125}
4126
4127/*
4128 * Get the next expansion(s), using "compl_pattern".
4129 * The search starts at position "ini" in curbuf and in the direction
4130 * compl_direction.
4131 * When "compl_started" is FALSE start at that position, otherwise continue
4132 * where we stopped searching before.
4133 * This may return before finding all the matches.
4134 * Return the total number of matches or -1 if still unknown -- Acevedo
4135 */
4136 static int
4137ins_compl_get_exp(pos_T *ini)
4138{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004139 static ins_compl_next_state_T st;
4140 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004141 int i;
4142 int found_new_match;
4143 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004144 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004145
4146 if (!compl_started)
4147 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004148 buf_T *buf;
4149
4150 FOR_ALL_BUFFERS(buf)
4151 buf->b_scanned = 0;
4152 if (!st_cleared)
4153 {
4154 CLEAR_FIELD(st);
4155 st_cleared = TRUE;
4156 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004157 st.found_all = FALSE;
4158 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004159 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004160 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004161 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4162 ? (char_u *)"." : curbuf->b_p_cpt);
4163 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004164 st.last_match_pos = st.first_match_pos = *ini;
4165 }
4166 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4167 st.ins_buf = curbuf; // In case the buffer was wiped out.
4168
4169 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004170 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004171 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004172
4173 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4174 for (;;)
4175 {
4176 found_new_match = FAIL;
4177 st.set_match_pos = FALSE;
4178
4179 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4180 // or if found_all says this entry is done. For ^X^L only use the
4181 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004182 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004183 && (!compl_started || st.found_all))
4184 {
glepnir8159fb12024-07-17 20:32:54 +02004185 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004186
4187 if (status == INS_COMPL_CPT_END)
4188 break;
4189 if (status == INS_COMPL_CPT_CONT)
4190 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004191 }
4192
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004193 // If complete() was called then compl_pattern has been reset. The
4194 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004195 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004196 break;
4197
4198 // get the next set of completion matches
4199 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004200
4201 // break the loop for specialized modes (use 'complete' just for the
4202 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4203 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004204 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4205 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004206 {
4207 if (got_int)
4208 break;
4209 // Fill the popup menu as soon as possible.
4210 if (type != -1)
4211 ins_compl_check_keys(0, FALSE);
4212
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004213 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004214 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4215 break;
4216 compl_started = TRUE;
4217 }
4218 else
4219 {
4220 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004221 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004222 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004223
4224 compl_started = FALSE;
4225 }
4226 }
4227 compl_started = TRUE;
4228
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004229 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004230 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004231 found_new_match = FAIL;
4232
4233 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004234 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004235 && !ctrl_x_mode_line_or_eval()))
4236 i = ins_compl_make_cyclic();
4237
4238 if (compl_old_match != NULL)
4239 {
4240 // If several matches were added (FORWARD) or the search failed and has
4241 // just been made cyclic then we have to move compl_curr_match to the
4242 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004243 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004244 : compl_old_match->cp_prev;
4245 if (compl_curr_match == NULL)
4246 compl_curr_match = compl_old_match;
4247 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004248 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004249
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004250 return i;
4251}
4252
4253/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004254 * Update "compl_shown_match" to the actually shown match, it may differ when
4255 * "compl_leader" is used to omit some of the matches.
4256 */
4257 static void
4258ins_compl_update_shown_match(void)
4259{
4260 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004261 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004262 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004263 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004264 compl_shown_match = compl_shown_match->cp_next;
4265
4266 // If we didn't find it searching forward, and compl_shows_dir is
4267 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004268 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004269 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004270 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004271 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004272 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004273 {
4274 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004275 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004276 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004277 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004278 compl_shown_match = compl_shown_match->cp_prev;
4279 }
4280}
4281
4282/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004283 * Delete the old text being completed.
4284 */
4285 void
4286ins_compl_delete(void)
4287{
4288 int col;
4289
4290 // In insert mode: Delete the typed part.
4291 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004292 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004293 if ((int)curwin->w_cursor.col > col)
4294 {
4295 if (stop_arrow() == FAIL)
4296 return;
4297 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004298 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004299 }
4300
4301 // TODO: is this sufficient for redrawing? Redrawing everything causes
4302 // flicker, thus we can't do that.
4303 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004304#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004305 // clear v:completed_item
4306 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004307#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004308}
4309
4310/*
4311 * Insert the new text being completed.
4312 * "in_compl_func" is TRUE when called from complete_check().
4313 */
4314 void
4315ins_compl_insert(int in_compl_func)
4316{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004317 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004318
4319 // Make sure we don't go over the end of the string, this can happen with
4320 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004321 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004322 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004323 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004324 compl_used_match = FALSE;
4325 else
4326 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004327#ifdef FEAT_EVAL
4328 {
4329 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4330
4331 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4332 }
4333#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004334 if (!in_compl_func)
4335 compl_curr_match = compl_shown_match;
4336}
4337
4338/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004339 * show the file name for the completion match (if any). Truncate the file
4340 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004341 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004342 static void
4343ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004344{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004345 char *lead = _("match in file");
4346 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4347 char_u *s;
4348 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004349
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004350 if (space <= 0)
4351 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004352
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004353 // We need the tail that fits. With double-byte encoding going
4354 // back from the end is very slow, thus go from the start and keep
4355 // the text that fits in "space" between "s" and "e".
4356 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004357 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004358 space -= ptr2cells(e);
4359 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004360 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004361 space += ptr2cells(s);
4362 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004363 }
4364 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004365 msg_hist_off = TRUE;
4366 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4367 s > compl_shown_match->cp_fname ? "<" : "", s);
4368 msg((char *)IObuff);
4369 msg_hist_off = FALSE;
4370 redraw_cmdline = FALSE; // don't overwrite!
4371}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004372
glepnirdca57fb2024-06-04 22:01:21 +02004373/*
zeertzjq551d8c32024-06-05 19:53:32 +02004374 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004375 */
glepnira218cc62024-06-03 19:32:39 +02004376 static compl_T *
4377find_comp_when_fuzzy(void)
4378{
4379 int score;
4380 char_u* str;
4381 int target_idx = -1;
4382 int is_forward = compl_shows_dir_forward();
4383 int is_backward = compl_shows_dir_backward();
4384 compl_T *comp = NULL;
4385
glepnir43eef882024-06-19 20:20:48 +02004386 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004387 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004388 return compl_first_match != compl_shown_match ? compl_first_match :
4389 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004390
4391 if (is_forward)
4392 target_idx = compl_selected_item + 1;
4393 else if (is_backward)
4394 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004395 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004396
4397 score = compl_match_array[target_idx].pum_score;
4398 str = compl_match_array[target_idx].pum_text;
4399
4400 comp = compl_first_match;
4401 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004402 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004403 return comp;
4404 comp = comp->cp_next;
4405 } while (comp != NULL && !is_first_match(comp));
4406
4407 return NULL;
4408}
4409
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004410/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004411 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004412 * times. The number of matches found is returned in 'num_matches'.
4413 *
4414 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4415 * get more completions. If it is FALSE, then do nothing when there are no more
4416 * completions in the given direction.
4417 *
4418 * If "advance" is TRUE, then completion will move to the first match.
4419 * Otherwise, the original text will be shown.
4420 *
4421 * Returns OK on success and -1 if the number of matches are unknown.
4422 */
4423 static int
4424find_next_completion_match(
4425 int allow_get_expansion,
4426 int todo, // repeat completion this many times
4427 int advance,
4428 int *num_matches)
4429{
4430 int found_end = FALSE;
4431 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004432 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004433 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4434 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004435
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004436 while (--todo >= 0)
4437 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004438 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004439 {
glepniraced8c22024-06-15 15:13:05 +02004440 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4441 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004442 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004443 && (is_first_match(compl_shown_match->cp_next)
4444 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004445 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004446 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004447 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004448 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004449 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004450 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4451 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004452 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004453 }
4454 else
4455 {
4456 if (!allow_get_expansion)
4457 {
4458 if (advance)
4459 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004460 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004461 compl_pending -= todo + 1;
4462 else
4463 compl_pending += todo + 1;
4464 }
4465 return -1;
4466 }
4467
4468 if (!compl_no_select && advance)
4469 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004470 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004471 --compl_pending;
4472 else
4473 ++compl_pending;
4474 }
4475
4476 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004477 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004478
4479 // handle any pending completions
4480 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004481 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004482 {
4483 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4484 {
4485 compl_shown_match = compl_shown_match->cp_next;
4486 --compl_pending;
4487 }
4488 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4489 {
4490 compl_shown_match = compl_shown_match->cp_prev;
4491 ++compl_pending;
4492 }
4493 else
4494 break;
4495 }
4496 found_end = FALSE;
4497 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004498 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004499 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004500 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004501 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004502 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004503 ++todo;
4504 else
4505 // Remember a matching item.
4506 found_compl = compl_shown_match;
4507
4508 // Stop at the end of the list when we found a usable match.
4509 if (found_end)
4510 {
4511 if (found_compl != NULL)
4512 {
4513 compl_shown_match = found_compl;
4514 break;
4515 }
4516 todo = 1; // use first usable match after wrapping around
4517 }
4518 }
4519
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004520 return OK;
4521}
4522
4523/*
4524 * Fill in the next completion in the current direction.
4525 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4526 * get more completions. If it is FALSE, then we just do nothing when there
4527 * are no more completions in a given direction. The latter case is used when
4528 * we are still in the middle of finding completions, to allow browsing
4529 * through the ones found so far.
4530 * Return the total number of matches, or -1 if still unknown -- webb.
4531 *
4532 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4533 * compl_shown_match here.
4534 *
4535 * Note that this function may be called recursively once only. First with
4536 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4537 * calls this function with "allow_get_expansion" FALSE.
4538 */
4539 static int
4540ins_compl_next(
4541 int allow_get_expansion,
4542 int count, // repeat completion this many times; should
4543 // be at least 1
4544 int insert_match, // Insert the newly selected match
4545 int in_compl_func) // called from complete_check()
4546{
4547 int num_matches = -1;
4548 int todo = count;
4549 int advance;
4550 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004551 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004552 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004553 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4554 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004555
4556 // When user complete function return -1 for findstart which is next
4557 // time of 'always', compl_shown_match become NULL.
4558 if (compl_shown_match == NULL)
4559 return -1;
4560
John Marriott5e6ea922024-11-23 14:01:57 +01004561 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004562 && !match_at_original_text(compl_shown_match)
4563 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004564 // Update "compl_shown_match" to the actually shown match
4565 ins_compl_update_shown_match();
4566
4567 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004568 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004569 // Delete old text to be replaced
4570 ins_compl_delete();
4571
4572 // When finding the longest common text we stick at the original text,
4573 // don't let CTRL-N or CTRL-P move to the first match.
4574 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4575
4576 // When restarting the search don't insert the first match either.
4577 if (compl_restarting)
4578 {
4579 advance = FALSE;
4580 compl_restarting = FALSE;
4581 }
4582
4583 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4584 // around.
4585 if (find_next_completion_match(allow_get_expansion, todo, advance,
4586 &num_matches) == -1)
4587 return -1;
4588
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004589 if (curbuf != orig_curbuf)
4590 {
4591 // In case some completion function switched buffer, don't want to
4592 // insert the completion elsewhere.
4593 return -1;
4594 }
4595
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004596 // Insert the text of the new completion, or the compl_leader.
4597 if (compl_no_insert && !started)
4598 {
glepnir6a38aff2024-12-16 21:56:16 +01004599 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004600 compl_used_match = FALSE;
4601 }
4602 else if (insert_match)
4603 {
4604 if (!compl_get_longest || compl_used_match)
4605 ins_compl_insert(in_compl_func);
4606 else
glepnir6a38aff2024-12-16 21:56:16 +01004607 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004608 }
4609 else
4610 compl_used_match = FALSE;
4611
4612 if (!allow_get_expansion)
4613 {
4614 // may undisplay the popup menu first
4615 ins_compl_upd_pum();
4616
4617 if (pum_enough_matches())
4618 // Will display the popup menu, don't redraw yet to avoid flicker.
4619 pum_call_update_screen();
4620 else
4621 // Not showing the popup menu yet, redraw to show the user what was
4622 // inserted.
4623 update_screen(0);
4624
4625 // display the updated popup menu
4626 ins_compl_show_pum();
4627#ifdef FEAT_GUI
4628 if (gui.in_use)
4629 {
4630 // Show the cursor after the match, not after the redrawn text.
4631 setcursor();
4632 out_flush_cursor(FALSE, FALSE);
4633 }
4634#endif
4635
4636 // Delete old text to be replaced, since we're still searching and
4637 // don't want to match ourselves!
4638 ins_compl_delete();
4639 }
4640
4641 // Enter will select a match when the match wasn't inserted and the popup
4642 // menu is visible.
4643 if (compl_no_insert && !started)
4644 compl_enter_selects = TRUE;
4645 else
4646 compl_enter_selects = !insert_match && compl_match_array != NULL;
4647
4648 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004649 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004650 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004651
4652 return num_matches;
4653}
4654
4655/*
4656 * Call this while finding completions, to check whether the user has hit a key
4657 * that should change the currently displayed completion, or exit completion
4658 * mode. Also, when compl_pending is not zero, show a completion as soon as
4659 * possible. -- webb
4660 * "frequency" specifies out of how many calls we actually check.
4661 * "in_compl_func" is TRUE when called from complete_check(), don't set
4662 * compl_curr_match.
4663 */
4664 void
4665ins_compl_check_keys(int frequency, int in_compl_func)
4666{
4667 static int count = 0;
4668 int c;
4669
4670 // Don't check when reading keys from a script, :normal or feedkeys().
4671 // That would break the test scripts. But do check for keys when called
4672 // from complete_check().
4673 if (!in_compl_func && (using_script() || ex_normal_busy))
4674 return;
4675
4676 // Only do this at regular intervals
4677 if (++count < frequency)
4678 return;
4679 count = 0;
4680
4681 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4682 // can't do its work correctly.
4683 c = vpeekc_any();
4684 if (c != NUL)
4685 {
4686 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4687 {
4688 c = safe_vgetc(); // Eat the character
4689 compl_shows_dir = ins_compl_key2dir(c);
4690 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4691 c != K_UP && c != K_DOWN, in_compl_func);
4692 }
4693 else
4694 {
4695 // Need to get the character to have KeyTyped set. We'll put it
4696 // back with vungetc() below. But skip K_IGNORE.
4697 c = safe_vgetc();
4698 if (c != K_IGNORE)
4699 {
4700 // Don't interrupt completion when the character wasn't typed,
4701 // e.g., when doing @q to replay keys.
4702 if (c != Ctrl_R && KeyTyped)
4703 compl_interrupted = TRUE;
4704
4705 vungetc(c);
4706 }
4707 }
4708 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004709 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004710 {
4711 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4712
4713 compl_pending = 0;
4714 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4715 }
4716}
4717
4718/*
4719 * Decide the direction of Insert mode complete from the key typed.
4720 * Returns BACKWARD or FORWARD.
4721 */
4722 static int
4723ins_compl_key2dir(int c)
4724{
4725 if (c == Ctrl_P || c == Ctrl_L
4726 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4727 return BACKWARD;
4728 return FORWARD;
4729}
4730
4731/*
4732 * Return TRUE for keys that are used for completion only when the popup menu
4733 * is visible.
4734 */
4735 static int
4736ins_compl_pum_key(int c)
4737{
4738 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4739 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4740 || c == K_UP || c == K_DOWN);
4741}
4742
4743/*
4744 * Decide the number of completions to move forward.
4745 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4746 */
4747 static int
4748ins_compl_key2count(int c)
4749{
4750 int h;
4751
4752 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4753 {
4754 h = pum_get_height();
4755 if (h > 3)
4756 h -= 2; // keep some context
4757 return h;
4758 }
4759 return 1;
4760}
4761
4762/*
4763 * Return TRUE if completion with "c" should insert the match, FALSE if only
4764 * to change the currently selected completion.
4765 */
4766 static int
4767ins_compl_use_match(int c)
4768{
4769 switch (c)
4770 {
4771 case K_UP:
4772 case K_DOWN:
4773 case K_PAGEDOWN:
4774 case K_KPAGEDOWN:
4775 case K_S_DOWN:
4776 case K_PAGEUP:
4777 case K_KPAGEUP:
4778 case K_S_UP:
4779 return FALSE;
4780 }
4781 return TRUE;
4782}
4783
4784/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004785 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4786 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004787 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004788 * Uses the global variables: compl_cont_status and ctrl_x_mode
4789 */
4790 static int
4791get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4792{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004793 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004794 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004795 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004796 {
4797 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4798 ;
4799 compl_col += ++startcol;
4800 compl_length = curs_col - startcol;
4801 }
4802 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004803 {
John Marriott5e6ea922024-11-23 14:01:57 +01004804 compl_pattern.string = str_foldcase(line + compl_col,
4805 compl_length, NULL, 0);
4806 if (compl_pattern.string == NULL)
4807 {
4808 compl_pattern.length = 0;
4809 return FAIL;
4810 }
4811 compl_pattern.length = STRLEN(compl_pattern.string);
4812 }
4813 else
4814 {
4815 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4816 if (compl_pattern.string == NULL)
4817 {
4818 compl_pattern.length = 0;
4819 return FAIL;
4820 }
4821 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004822 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004823 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004824 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004825 {
4826 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004827 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004828 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004829
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004830 if (!vim_iswordp(line + compl_col)
4831 || (compl_col > 0
4832 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004833 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004834 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004835 prefixlen = 0;
4836 }
John Marriott5e6ea922024-11-23 14:01:57 +01004837
4838 // we need up to 2 extra chars for the prefix
4839 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4840 compl_pattern.string = alloc(n);
4841 if (compl_pattern.string == NULL)
4842 {
4843 compl_pattern.length = 0;
4844 return FAIL;
4845 }
4846 STRCPY((char *)compl_pattern.string, prefix);
4847 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004848 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004849 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004850 }
4851 else if (--startcol < 0
4852 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4853 {
John Marriott5e6ea922024-11-23 14:01:57 +01004854 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4855
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004856 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004857 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4858 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004859 {
John Marriott5e6ea922024-11-23 14:01:57 +01004860 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004861 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004862 }
John Marriott5e6ea922024-11-23 14:01:57 +01004863 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004864 compl_col += curs_col;
4865 compl_length = 0;
4866 }
4867 else
4868 {
4869 // Search the point of change class of multibyte character
4870 // or not a word single byte character backward.
4871 if (has_mbyte)
4872 {
4873 int base_class;
4874 int head_off;
4875
4876 startcol -= (*mb_head_off)(line, line + startcol);
4877 base_class = mb_get_class(line + startcol);
4878 while (--startcol >= 0)
4879 {
4880 head_off = (*mb_head_off)(line, line + startcol);
4881 if (base_class != mb_get_class(line + startcol
4882 - head_off))
4883 break;
4884 startcol -= head_off;
4885 }
4886 }
4887 else
4888 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4889 ;
4890 compl_col += ++startcol;
4891 compl_length = (int)curs_col - startcol;
4892 if (compl_length == 1)
4893 {
4894 // Only match word with at least two chars -- webb
4895 // there's no need to call quote_meta,
4896 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004897 compl_pattern.string = alloc(7);
4898 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004899 {
John Marriott5e6ea922024-11-23 14:01:57 +01004900 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004901 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004902 }
John Marriott5e6ea922024-11-23 14:01:57 +01004903 STRCPY((char *)compl_pattern.string, "\\<");
4904 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4905 STRCAT((char *)compl_pattern.string, "\\k");
4906 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004907 }
4908 else
4909 {
John Marriott5e6ea922024-11-23 14:01:57 +01004910 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4911
4912 compl_pattern.string = alloc(n);
4913 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004914 {
John Marriott5e6ea922024-11-23 14:01:57 +01004915 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004916 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004917 }
John Marriott5e6ea922024-11-23 14:01:57 +01004918 STRCPY((char *)compl_pattern.string, "\\<");
4919 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004920 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004921 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004922 }
4923 }
4924
4925 return OK;
4926}
4927
4928/*
4929 * Get the pattern, column and length for whole line completion or for the
4930 * complete() function.
4931 * Sets the global variables: compl_col, compl_length and compl_pattern.
4932 */
4933 static int
4934get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4935{
4936 compl_col = (colnr_T)getwhitecols(line);
4937 compl_length = (int)curs_col - (int)compl_col;
4938 if (compl_length < 0) // cursor in indent: empty pattern
4939 compl_length = 0;
4940 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004941 {
John Marriott5e6ea922024-11-23 14:01:57 +01004942 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4943 NULL, 0);
4944 if (compl_pattern.string == NULL)
4945 {
4946 compl_pattern.length = 0;
4947 return FAIL;
4948 }
4949 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004950 }
John Marriott5e6ea922024-11-23 14:01:57 +01004951 else
4952 {
4953 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4954 if (compl_pattern.string == NULL)
4955 {
4956 compl_pattern.length = 0;
4957 return FAIL;
4958 }
4959 compl_pattern.length = (size_t)compl_length;
4960 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004961
4962 return OK;
4963}
4964
4965/*
4966 * Get the pattern, column and length for filename completion.
4967 * Sets the global variables: compl_col, compl_length and compl_pattern.
4968 */
4969 static int
4970get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4971{
4972 // Go back to just before the first filename character.
4973 if (startcol > 0)
4974 {
4975 char_u *p = line + startcol;
4976
4977 MB_PTR_BACK(line, p);
4978 while (p > line && vim_isfilec(PTR2CHAR(p)))
4979 MB_PTR_BACK(line, p);
4980 if (p == line && vim_isfilec(PTR2CHAR(p)))
4981 startcol = 0;
4982 else
4983 startcol = (int)(p - line) + 1;
4984 }
4985
4986 compl_col += startcol;
4987 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01004988 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
4989 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004990 {
John Marriott5e6ea922024-11-23 14:01:57 +01004991 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004992 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004993 }
4994
John Marriott5e6ea922024-11-23 14:01:57 +01004995 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004996
4997 return OK;
4998}
4999
5000/*
5001 * Get the pattern, column and length for command-line completion.
5002 * Sets the global variables: compl_col, compl_length and compl_pattern.
5003 */
5004 static int
5005get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5006{
John Marriott5e6ea922024-11-23 14:01:57 +01005007 compl_pattern.string = vim_strnsave(line, curs_col);
5008 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005009 {
John Marriott5e6ea922024-11-23 14:01:57 +01005010 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005011 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005012 }
John Marriott5e6ea922024-11-23 14:01:57 +01005013 compl_pattern.length = curs_col;
5014 set_cmd_context(&compl_xp, compl_pattern.string,
5015 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005016 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5017 || compl_xp.xp_context == EXPAND_NOTHING)
5018 // No completion possible, use an empty pattern to get a
5019 // "pattern not found" message.
5020 compl_col = curs_col;
5021 else
John Marriott5e6ea922024-11-23 14:01:57 +01005022 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005023 compl_length = curs_col - compl_col;
5024
5025 return OK;
5026}
5027
5028/*
5029 * Get the pattern, column and length for user defined completion ('omnifunc',
5030 * 'completefunc' and 'thesaurusfunc')
5031 * Sets the global variables: compl_col, compl_length and compl_pattern.
5032 * Uses the global variable: spell_bad_len
5033 */
5034 static int
5035get_userdefined_compl_info(colnr_T curs_col UNUSED)
5036{
5037 int ret = FAIL;
5038
5039#ifdef FEAT_COMPL_FUNC
5040 // Call user defined function 'completefunc' with "a:findstart"
5041 // set to 1 to obtain the length of text to use for completion.
5042 char_u *line;
5043 typval_T args[3];
5044 int col;
5045 char_u *funcname;
5046 pos_T pos;
5047 int save_State = State;
5048 callback_T *cb;
5049
5050 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5051 // length as a string
5052 funcname = get_complete_funcname(ctrl_x_mode);
5053 if (*funcname == NUL)
5054 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005055 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005056 ? "completefunc" : "omnifunc");
5057 return FAIL;
5058 }
5059
5060 args[0].v_type = VAR_NUMBER;
5061 args[0].vval.v_number = 1;
5062 args[1].v_type = VAR_STRING;
5063 args[1].vval.v_string = (char_u *)"";
5064 args[2].v_type = VAR_UNKNOWN;
5065 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005066 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005067 cb = get_insert_callback(ctrl_x_mode);
5068 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005069 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005070
5071 State = save_State;
5072 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005073 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005074 validate_cursor();
5075 if (!EQUAL_POS(curwin->w_cursor, pos))
5076 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005077 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005078 return FAIL;
5079 }
5080
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005081 // Return value -2 means the user complete function wants to cancel the
5082 // complete without an error, do the same if the function did not execute
5083 // successfully.
5084 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005085 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005086 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005087 if (col == -3)
5088 {
5089 ctrl_x_mode = CTRL_X_NORMAL;
5090 edit_submode = NULL;
5091 if (!shortmess(SHM_COMPLETIONMENU))
5092 msg_clr_cmdline();
5093 return FAIL;
5094 }
5095
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005096 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005097 // completion.
5098 compl_opt_refresh_always = FALSE;
5099 compl_opt_suppress_empty = FALSE;
5100
5101 if (col < 0)
5102 col = curs_col;
5103 compl_col = col;
5104 if (compl_col > curs_col)
5105 compl_col = curs_col;
5106
5107 // Setup variables for completion. Need to obtain "line" again,
5108 // it may have become invalid.
5109 line = ml_get(curwin->w_cursor.lnum);
5110 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005111 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5112 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005113 {
John Marriott5e6ea922024-11-23 14:01:57 +01005114 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005115 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005116 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005117
John Marriott5e6ea922024-11-23 14:01:57 +01005118 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005119 ret = OK;
5120#endif
5121
5122 return ret;
5123}
5124
5125/*
5126 * Get the pattern, column and length for spell completion.
5127 * Sets the global variables: compl_col, compl_length and compl_pattern.
5128 * Uses the global variable: spell_bad_len
5129 */
5130 static int
5131get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5132{
5133 int ret = FAIL;
5134#ifdef FEAT_SPELL
5135 char_u *line;
5136
5137 if (spell_bad_len > 0)
5138 compl_col = curs_col - spell_bad_len;
5139 else
5140 compl_col = spell_word_start(startcol);
5141 if (compl_col >= (colnr_T)startcol)
5142 {
5143 compl_length = 0;
5144 compl_col = curs_col;
5145 }
5146 else
5147 {
5148 spell_expand_check_cap(compl_col);
5149 compl_length = (int)curs_col - compl_col;
5150 }
5151 // Need to obtain "line" again, it may have become invalid.
5152 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005153 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5154 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005155 {
John Marriott5e6ea922024-11-23 14:01:57 +01005156 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005157 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005158 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005159
John Marriott5e6ea922024-11-23 14:01:57 +01005160 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005161 ret = OK;
5162#endif
5163
5164 return ret;
5165}
5166
5167/*
5168 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005169 * "startcol" - start column number of the completion pattern/text
5170 * "cur_col" - current cursor column
5171 * On return, "line_invalid" is set to TRUE, if the current line may have
5172 * become invalid and needs to be fetched again.
5173 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005174 */
5175 static int
5176compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5177{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005178 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005179 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5180 && !thesaurus_func_complete(ctrl_x_mode)))
5181 {
5182 return get_normal_compl_info(line, startcol, curs_col);
5183 }
5184 else if (ctrl_x_mode_line_or_eval())
5185 {
5186 return get_wholeline_compl_info(line, curs_col);
5187 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005188 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005189 {
5190 return get_filename_compl_info(line, startcol, curs_col);
5191 }
5192 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5193 {
5194 return get_cmdline_compl_info(line, curs_col);
5195 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005196 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005197 || thesaurus_func_complete(ctrl_x_mode))
5198 {
5199 if (get_userdefined_compl_info(curs_col) == FAIL)
5200 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005201 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005202 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005203 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005204 {
5205 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5206 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005207 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005208 }
5209 else
5210 {
5211 internal_error("ins_complete()");
5212 return FAIL;
5213 }
5214
5215 return OK;
5216}
5217
5218/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005219 * Continue an interrupted completion mode search in "line".
5220 *
5221 * If this same ctrl_x_mode has been interrupted use the text from
5222 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5223 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5224 * the same line as the cursor then fix it (the line has been split because it
5225 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5226 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005227 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005228 static void
5229ins_compl_continue_search(char_u *line)
5230{
5231 // it is a continued search
5232 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005233 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5234 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005235 {
5236 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5237 {
5238 // line (probably) wrapped, set compl_startpos to the
5239 // first non_blank in the line, if it is not a wordchar
5240 // include it to get a better pattern, but then we don't
5241 // want the "\\<" prefix, check it below
5242 compl_col = (colnr_T)getwhitecols(line);
5243 compl_startpos.col = compl_col;
5244 compl_startpos.lnum = curwin->w_cursor.lnum;
5245 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5246 }
5247 else
5248 {
5249 // S_IPOS was set when we inserted a word that was at the
5250 // beginning of the line, which means that we'll go to SOL
5251 // mode but first we need to redefine compl_startpos
5252 if (compl_cont_status & CONT_S_IPOS)
5253 {
5254 compl_cont_status |= CONT_SOL;
5255 compl_startpos.col = (colnr_T)(skipwhite(
5256 line + compl_length
5257 + compl_startpos.col) - line);
5258 }
5259 compl_col = compl_startpos.col;
5260 }
5261 compl_length = curwin->w_cursor.col - (int)compl_col;
5262 // IObuff is used to add a "word from the next line" would we
5263 // have enough space? just being paranoid
5264#define MIN_SPACE 75
5265 if (compl_length > (IOSIZE - MIN_SPACE))
5266 {
5267 compl_cont_status &= ~CONT_SOL;
5268 compl_length = (IOSIZE - MIN_SPACE);
5269 compl_col = curwin->w_cursor.col - compl_length;
5270 }
5271 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5272 if (compl_length < 1)
5273 compl_cont_status &= CONT_LOCAL;
5274 }
5275 else if (ctrl_x_mode_line_or_eval())
5276 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5277 else
5278 compl_cont_status = 0;
5279}
5280
5281/*
5282 * start insert mode completion
5283 */
5284 static int
5285ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005286{
5287 char_u *line;
5288 int startcol = 0; // column where searched text starts
5289 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005290 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005291 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005292 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005293
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005294 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005295
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005296 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005297 did_si = FALSE;
5298 can_si = FALSE;
5299 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005300 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005301 return FAIL;
5302
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005303 line = ml_get(curwin->w_cursor.lnum);
5304 curs_col = curwin->w_cursor.col;
5305 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005306
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005307 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5308 && compl_cont_mode == ctrl_x_mode)
5309 // this same ctrl-x_mode was interrupted previously. Continue the
5310 // completion.
5311 ins_compl_continue_search(line);
5312 else
5313 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005314
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005315 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005316 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005317 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005318 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005319 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5320 compl_cont_status = 0;
5321 compl_cont_status |= CONT_N_ADDS;
5322 compl_startpos = curwin->w_cursor;
5323 startcol = (int)curs_col;
5324 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005325 }
5326
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005327 // Work out completion pattern and original text -- webb
5328 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5329 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005330 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5331 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005332 // restore did_ai, so that adding comment leader works
5333 did_ai = save_did_ai;
5334 return FAIL;
5335 }
5336 // If "line" was changed while getting completion info get it again.
5337 if (line_invalid)
5338 line = ml_get(curwin->w_cursor.lnum);
5339
glepnir7cfe6932024-09-15 20:06:28 +02005340 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005341 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005342 {
5343 edit_submode_pre = (char_u *)_(" Adding");
5344 if (ctrl_x_mode_line_or_eval())
5345 {
5346 // Insert a new line, keep indentation but ignore 'comments'.
5347 char_u *old = curbuf->b_p_com;
5348
5349 curbuf->b_p_com = (char_u *)"";
5350 compl_startpos.lnum = curwin->w_cursor.lnum;
5351 compl_startpos.col = compl_col;
5352 ins_eol('\r');
5353 curbuf->b_p_com = old;
5354 compl_length = 0;
5355 compl_col = curwin->w_cursor.col;
5356 }
glepnir7cfe6932024-09-15 20:06:28 +02005357 else if (ctrl_x_mode_normal() && in_fuzzy)
5358 {
5359 compl_startpos = curwin->w_cursor;
5360 compl_cont_status &= CONT_S_IPOS;
5361 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005362 }
5363 else
5364 {
5365 edit_submode_pre = NULL;
5366 compl_startpos.col = compl_col;
5367 }
5368
5369 if (compl_cont_status & CONT_LOCAL)
5370 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5371 else
5372 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5373
5374 // If any of the original typed text has been changed we need to fix
5375 // the redo buffer.
5376 ins_compl_fixRedoBufForLeader(NULL);
5377
5378 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005379 VIM_CLEAR_STRING(compl_orig_text);
5380 compl_orig_text.length = (size_t)compl_length;
5381 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005382 if (p_ic)
5383 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005384 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5385 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005386 {
John Marriott5e6ea922024-11-23 14:01:57 +01005387 VIM_CLEAR_STRING(compl_pattern);
5388 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005389 return FAIL;
5390 }
5391
5392 // showmode might reset the internal line pointers, so it must
5393 // be called before line = ml_get(), or when this address is no
5394 // longer needed. -- Acevedo.
5395 edit_submode_extra = (char_u *)_("-- Searching...");
5396 edit_submode_highl = HLF_COUNT;
5397 showmode();
5398 edit_submode_extra = NULL;
5399 out_flush();
5400
5401 return OK;
5402}
5403
5404/*
5405 * display the completion status message
5406 */
5407 static void
5408ins_compl_show_statusmsg(void)
5409{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005410 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005411 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005412 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005413 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005414 ? (char_u *)_("Hit end of paragraph")
5415 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005416 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005417 }
5418
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005419 if (edit_submode_extra == NULL)
5420 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005421 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005422 {
5423 edit_submode_extra = (char_u *)_("Back at original");
5424 edit_submode_highl = HLF_W;
5425 }
5426 else if (compl_cont_status & CONT_S_IPOS)
5427 {
5428 edit_submode_extra = (char_u *)_("Word from other line");
5429 edit_submode_highl = HLF_COUNT;
5430 }
5431 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5432 {
5433 edit_submode_extra = (char_u *)_("The only match");
5434 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005435 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005436 }
5437 else
5438 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005439#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005440 // Update completion sequence number when needed.
5441 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005442 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005443#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005444 // The match should always have a sequence number now, this is
5445 // just a safety check.
5446 if (compl_curr_match->cp_number != -1)
5447 {
5448 // Space for 10 text chars. + 2x10-digit no.s = 31.
5449 // Translations may need more than twice that.
5450 static char_u match_ref[81];
5451
5452 if (compl_matches > 0)
5453 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005454 _("match %d of %d"),
5455 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005456 else
5457 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005458 _("match %d"),
5459 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005460 edit_submode_extra = match_ref;
5461 edit_submode_highl = HLF_R;
5462 if (dollar_vcol >= 0)
5463 curs_columns(FALSE);
5464 }
5465 }
5466 }
5467
5468 // Show a message about what (completion) mode we're in.
5469 if (!compl_opt_suppress_empty)
5470 {
5471 showmode();
5472 if (!shortmess(SHM_COMPLETIONMENU))
5473 {
5474 if (edit_submode_extra != NULL)
5475 {
5476 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005477 {
5478 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005479 msg_attr((char *)edit_submode_extra,
5480 edit_submode_highl < HLF_COUNT
5481 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005482 msg_hist_off = FALSE;
5483 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005484 }
5485 else
5486 msg_clr_cmdline(); // necessary for "noshowmode"
5487 }
5488 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005489}
5490
5491/*
5492 * Do Insert mode completion.
5493 * Called when character "c" was typed, which has a meaning for completion.
5494 * Returns OK if completion was done, FAIL if something failed (out of mem).
5495 */
5496 int
5497ins_complete(int c, int enable_pum)
5498{
5499 int n;
5500 int save_w_wrow;
5501 int save_w_leftcol;
5502 int insert_match;
5503
5504 compl_direction = ins_compl_key2dir(c);
5505 insert_match = ins_compl_use_match(c);
5506
5507 if (!compl_started)
5508 {
5509 if (ins_compl_start() == FAIL)
5510 return FAIL;
5511 }
5512 else if (insert_match && stop_arrow() == FAIL)
5513 return FAIL;
5514
5515 compl_shown_match = compl_curr_match;
5516 compl_shows_dir = compl_direction;
5517
5518 // Find next match (and following matches).
5519 save_w_wrow = curwin->w_wrow;
5520 save_w_leftcol = curwin->w_leftcol;
5521 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5522
5523 // may undisplay the popup menu
5524 ins_compl_upd_pum();
5525
5526 if (n > 1) // all matches have been found
5527 compl_matches = n;
5528 compl_curr_match = compl_shown_match;
5529 compl_direction = compl_shows_dir;
5530
5531 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5532 // mode.
5533 if (got_int && !global_busy)
5534 {
5535 (void)vgetc();
5536 got_int = FALSE;
5537 }
5538
5539 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005540 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005541 {
5542 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5543 // because we couldn't expand anything at first place, but if we used
5544 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5545 // (such as M in M'exico) if not tried already. -- Acevedo
5546 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005547 || compl_status_adding()
5548 || (ctrl_x_mode_not_default()
5549 && !ctrl_x_mode_path_patterns()
5550 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005551 compl_cont_status &= ~CONT_N_ADDS;
5552 }
5553
5554 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5555 compl_cont_status |= CONT_S_IPOS;
5556 else
5557 compl_cont_status &= ~CONT_S_IPOS;
5558
5559 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005560
5561 // Show the popup menu, unless we got interrupted.
5562 if (enable_pum && !compl_interrupted)
5563 show_pum(save_w_wrow, save_w_leftcol);
5564
5565 compl_was_interrupted = compl_interrupted;
5566 compl_interrupted = FALSE;
5567
5568 return OK;
5569}
5570
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005571/*
5572 * Remove (if needed) and show the popup menu
5573 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005574 static void
5575show_pum(int prev_w_wrow, int prev_w_leftcol)
5576{
5577 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005578 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005579 RedrawingDisabled = 0;
5580
5581 // If the cursor moved or the display scrolled we need to remove the pum
5582 // first.
5583 setcursor();
5584 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5585 ins_compl_del_pum();
5586
5587 ins_compl_show_pum();
5588 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005589
5590 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005591}
5592
5593/*
5594 * Looks in the first "len" chars. of "src" for search-metachars.
5595 * If dest is not NULL the chars. are copied there quoting (with
5596 * a backslash) the metachars, and dest would be NUL terminated.
5597 * Returns the length (needed) of dest
5598 */
5599 static unsigned
5600quote_meta(char_u *dest, char_u *src, int len)
5601{
5602 unsigned m = (unsigned)len + 1; // one extra for the NUL
5603
5604 for ( ; --len >= 0; src++)
5605 {
5606 switch (*src)
5607 {
5608 case '.':
5609 case '*':
5610 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005611 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005612 break;
5613 // FALLTHROUGH
5614 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005615 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005616 break;
5617 // FALLTHROUGH
5618 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005619 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005620 break;
5621 // FALLTHROUGH
5622 case '^': // currently it's not needed.
5623 case '$':
5624 m++;
5625 if (dest != NULL)
5626 *dest++ = '\\';
5627 break;
5628 }
5629 if (dest != NULL)
5630 *dest++ = *src;
5631 // Copy remaining bytes of a multibyte character.
5632 if (has_mbyte)
5633 {
5634 int i, mb_len;
5635
5636 mb_len = (*mb_ptr2len)(src) - 1;
5637 if (mb_len > 0 && len >= mb_len)
5638 for (i = 0; i < mb_len; ++i)
5639 {
5640 --len;
5641 ++src;
5642 if (dest != NULL)
5643 *dest++ = *src;
5644 }
5645 }
5646 }
5647 if (dest != NULL)
5648 *dest = NUL;
5649
5650 return m;
5651}
5652
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005653#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005654 void
5655free_insexpand_stuff(void)
5656{
John Marriott5e6ea922024-11-23 14:01:57 +01005657 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005658# ifdef FEAT_EVAL
5659 free_callback(&cfu_cb);
5660 free_callback(&ofu_cb);
5661 free_callback(&tsrfu_cb);
5662# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005663}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005664#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005665
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005666#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005667/*
5668 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5669 * spelled word, if there is one.
5670 */
5671 static void
5672spell_back_to_badword(void)
5673{
5674 pos_T tpos = curwin->w_cursor;
5675
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005676 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005677 if (curwin->w_cursor.col != tpos.col)
5678 start_arrow(&tpos);
5679}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005680#endif