blob: 22f021de7ef81bf22c66631729d492910cae8c01 [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{
glepnire8908872025-01-08 18:30:45 +0100927 if ((get_cot_flags() & COT_FUZZY))
928 return -1;
929
930 if (col >= (compl_col + (int)compl_leader.length) && col < compl_ins_end_col)
glepnir6a38aff2024-12-16 21:56:16 +0100931 return syn_name2attr((char_u *)"ComplMatchIns");
932
933 return -1;
934}
935
936/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100937 * Reduce the longest common string for match "match".
938 */
939 static void
940ins_compl_longest_match(compl_T *match)
941{
942 char_u *p, *s;
943 int c1, c2;
944 int had_match;
945
John Marriott5e6ea922024-11-23 14:01:57 +0100946 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100947 {
948 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100949 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
950 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000951 return;
952
John Marriott5e6ea922024-11-23 14:01:57 +0100953 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000954 had_match = (curwin->w_cursor.col > compl_col);
955 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100956 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000957 ins_redraw(FALSE);
958
959 // When the match isn't there (to avoid matching itself) remove it
960 // again after redrawing.
961 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100962 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100963 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000964
965 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100966 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967
968 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100969 p = compl_leader.string;
970 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000971 while (*p != NUL)
972 {
973 if (has_mbyte)
974 {
975 c1 = mb_ptr2char(p);
976 c2 = mb_ptr2char(s);
977 }
978 else
979 {
980 c1 = *p;
981 c2 = *s;
982 }
983 if ((match->cp_flags & CP_ICASE)
984 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
985 break;
986 if (has_mbyte)
987 {
988 MB_PTR_ADV(p);
989 MB_PTR_ADV(s);
990 }
991 else
992 {
993 ++p;
994 ++s;
995 }
996 }
997
998 if (*p != NUL)
999 {
1000 // Leader was shortened, need to change the inserted text.
1001 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001002 compl_leader.length = (size_t)(p - compl_leader.string);
1003
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001004 had_match = (curwin->w_cursor.col > compl_col);
1005 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001006 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001007 ins_redraw(FALSE);
1008
1009 // When the match isn't there (to avoid matching itself) remove it
1010 // again after redrawing.
1011 if (!had_match)
1012 ins_compl_delete();
1013 }
1014
1015 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001016}
1017
1018/*
1019 * Add an array of matches to the list of matches.
1020 * Frees matches[].
1021 */
1022 static void
1023ins_compl_add_matches(
1024 int num_matches,
1025 char_u **matches,
1026 int icase)
1027{
1028 int i;
1029 int add_r = OK;
1030 int dir = compl_direction;
1031
1032 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001033 {
1034 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1035 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1036 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001037 // if dir was BACKWARD then honor it just once
1038 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001039 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001040 FreeWild(num_matches, matches);
1041}
1042
1043/*
1044 * Make the completion list cyclic.
1045 * Return the number of matches (excluding the original).
1046 */
1047 static int
1048ins_compl_make_cyclic(void)
1049{
1050 compl_T *match;
1051 int count = 0;
1052
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001053 if (compl_first_match == NULL)
1054 return 0;
1055
1056 // Find the end of the list.
1057 match = compl_first_match;
1058 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001059 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001060 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001061 match = match->cp_next;
1062 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001063 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001064 match->cp_next = compl_first_match;
1065 compl_first_match->cp_prev = match;
1066
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001067 return count;
1068}
1069
1070/*
1071 * Return whether there currently is a shown match.
1072 */
1073 int
1074ins_compl_has_shown_match(void)
1075{
1076 return compl_shown_match == NULL
1077 || compl_shown_match != compl_shown_match->cp_next;
1078}
1079
1080/*
1081 * Return whether the shown match is long enough.
1082 */
1083 int
1084ins_compl_long_shown_match(void)
1085{
John Marriott5e6ea922024-11-23 14:01:57 +01001086 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001087 > curwin->w_cursor.col - compl_col;
1088}
1089
1090/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001091 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001093 unsigned int
1094get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001095{
zeertzjq529b9ad2024-06-05 20:27:06 +02001096 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001097}
1098
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001099/*
1100 * Update the screen and when there is any scrolling remove the popup menu.
1101 */
1102 static void
1103ins_compl_upd_pum(void)
1104{
1105 int h;
1106
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001107 if (compl_match_array == NULL)
1108 return;
1109
1110 h = curwin->w_cline_height;
1111 // Update the screen later, before drawing the popup menu over it.
1112 pum_call_update_screen();
1113 if (h != curwin->w_cline_height)
1114 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001115}
1116
1117/*
1118 * Remove any popup menu.
1119 */
1120 static void
1121ins_compl_del_pum(void)
1122{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001123 if (compl_match_array == NULL)
1124 return;
1125
1126 pum_undisplay();
1127 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001128}
1129
1130/*
1131 * Return TRUE if the popup menu should be displayed.
1132 */
1133 int
1134pum_wanted(void)
1135{
1136 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001137 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001138 return FALSE;
1139
1140 // The display looks bad on a B&W display.
1141 if (t_colors < 8
1142#ifdef FEAT_GUI
1143 && !gui.in_use
1144#endif
1145 )
1146 return FALSE;
1147 return TRUE;
1148}
1149
1150/*
1151 * Return TRUE if there are two or more matches to be shown in the popup menu.
1152 * One if 'completopt' contains "menuone".
1153 */
1154 static int
1155pum_enough_matches(void)
1156{
1157 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001158 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159
1160 // Don't display the popup menu if there are no matches or there is only
1161 // one (ignoring the original text).
1162 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163 do
1164 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001165 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001166 break;
1167 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001168 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001169
zeertzjq529b9ad2024-06-05 20:27:06 +02001170 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001171 return (i >= 1);
1172 return (i >= 2);
1173}
1174
Bram Moolenaar3075a452021-11-17 15:51:52 +00001175#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001176/*
1177 * Allocate Dict for the completed item.
1178 * { word, abbr, menu, kind, info }
1179 */
1180 static dict_T *
1181ins_compl_dict_alloc(compl_T *match)
1182{
1183 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1184
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001185 if (dict == NULL)
1186 return NULL;
1187
John Marriott5e6ea922024-11-23 14:01:57 +01001188 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001189 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1190 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1191 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1192 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1193 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1194 dict_add_string(dict, "user_data", (char_u *)"");
1195 else
1196 dict_add_tv(dict, "user_data", &match->cp_user_data);
1197
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001198 return dict;
1199}
1200
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001201/*
1202 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1203 * completion menu is changed.
1204 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001205 static void
1206trigger_complete_changed_event(int cur)
1207{
1208 dict_T *v_event;
1209 dict_T *item;
1210 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001211 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212
1213 if (recursive)
1214 return;
1215
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001216 if (cur < 0)
1217 item = dict_alloc();
1218 else
1219 item = ins_compl_dict_alloc(compl_curr_match);
1220 if (item == NULL)
1221 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001222 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001223 dict_add_dict(v_event, "completed_item", item);
1224 pum_set_event_info(v_event);
1225 dict_set_items_ro(v_event);
1226
1227 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001228 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001229 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001230 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001231 recursive = FALSE;
1232
Bram Moolenaar3075a452021-11-17 15:51:52 +00001233 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001234}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001235#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001236
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001237/*
glepnira218cc62024-06-03 19:32:39 +02001238 * pumitem qsort compare func
1239 */
1240 static int
zeertzjq8e567472024-06-14 20:04:42 +02001241ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001242{
1243 const int sa = (*(pumitem_T *)a).pum_score;
1244 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001245 const int ia = (*(pumitem_T *)a).pum_idx;
1246 const int ib = (*(pumitem_T *)b).pum_idx;
1247 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001248}
1249
1250/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001251 * Build a popup menu to show the completion matches.
1252 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1253 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001254 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001255 static int
1256ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001257{
1258 compl_T *compl;
1259 compl_T *shown_compl = NULL;
1260 int did_find_shown_match = FALSE;
1261 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001262 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001263 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001264 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001265 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001266 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1267 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001268 compl_T *match_head = NULL;
1269 compl_T *match_tail = NULL;
1270 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001271
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001272 // Need to build the popup menu list.
1273 compl_match_arraysize = 0;
1274 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001275
glepnira49c0772024-11-30 10:56:30 +01001276 // If the current match is the original text don't find the first
1277 // match after it, don't highlight anything.
1278 if (match_at_original_text(compl_shown_match))
1279 shown_match_ok = TRUE;
1280
1281 if (compl_leader.string != NULL
1282 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1283 && shown_match_ok == FALSE)
1284 compl_shown_match = compl_no_select ? compl_first_match
1285 : compl_first_match->cp_next;
1286
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001287 do
1288 {
glepnird4088ed2024-12-31 10:55:22 +01001289 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001290 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1291 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001292 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1293 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001294
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001295 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001296 && (compl_leader.string == NULL
1297 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001298 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001299 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001300 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001301 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001302 if (match_head == NULL)
1303 match_head = compl;
1304 else
glepnira49c0772024-11-30 10:56:30 +01001305 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001306 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001307
1308 if (!shown_match_ok && !compl_fuzzy_match)
1309 {
1310 if (compl == compl_shown_match || did_find_shown_match)
1311 {
1312 // This item is the shown match or this is the
1313 // first displayed item after the shown match.
1314 compl_shown_match = compl;
1315 did_find_shown_match = TRUE;
1316 shown_match_ok = TRUE;
1317 }
1318 else
1319 // Remember this displayed match for when the
1320 // shown match is just below it.
1321 shown_compl = compl;
1322 cur = i;
1323 }
1324 else if (compl_fuzzy_match)
1325 {
1326 if (i == 0)
1327 shown_compl = compl;
1328 // Update the maximum fuzzy score and the shown match
1329 // if the current item's score is higher
1330 if (compl->cp_score > max_fuzzy_score)
1331 {
1332 did_find_shown_match = TRUE;
1333 max_fuzzy_score = compl->cp_score;
1334 if (!compl_no_select)
1335 compl_shown_match = compl;
1336 }
1337
1338 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1339 {
1340 cur = i;
1341 shown_match_ok = TRUE;
1342 }
1343 }
1344 i++;
glepnir80b66202024-11-27 21:53:53 +01001345 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001346
glepnira218cc62024-06-03 19:32:39 +02001347 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 {
1349 did_find_shown_match = TRUE;
1350
1351 // When the original text is the shown match don't set
1352 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001353 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001354 shown_match_ok = TRUE;
1355
1356 if (!shown_match_ok && shown_compl != NULL)
1357 {
1358 // The shown match isn't displayed, set it to the
1359 // previously displayed match.
1360 compl_shown_match = shown_compl;
1361 shown_match_ok = TRUE;
1362 }
1363 }
glepnira49c0772024-11-30 10:56:30 +01001364 compl = compl->cp_next;
1365 } while (compl != NULL && !is_first_match(compl));
1366
1367 if (compl_match_arraysize == 0)
1368 return -1;
1369
1370 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1371 if (compl_match_array == NULL)
1372 return -1;
1373
1374 compl = match_head;
1375 i = 0;
1376 while (compl != NULL)
1377 {
glepnir6e199932024-12-14 21:13:27 +01001378 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1379 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001380 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1381 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1382 compl_match_array[i].pum_score = compl->cp_score;
1383 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1384 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001385 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1386 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001387 match_next = compl->cp_match_next;
1388 compl->cp_match_next = NULL;
1389 compl = match_next;
1390 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001391
John Marriott5e6ea922024-11-23 14:01:57 +01001392 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001393 {
1394 for (i = 0; i < compl_match_arraysize; i++)
1395 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001396 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001397 qsort(compl_match_array, (size_t)compl_match_arraysize,
1398 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001399 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001400 }
glepnira218cc62024-06-03 19:32:39 +02001401
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001402 if (!shown_match_ok) // no displayed match at all
1403 cur = -1;
1404
1405 return cur;
1406}
1407
1408/*
1409 * Show the popup menu for the list of matches.
1410 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1411 */
1412 void
1413ins_compl_show_pum(void)
1414{
1415 int i;
1416 int cur = -1;
1417 colnr_T col;
1418
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001419 if (!pum_wanted() || !pum_enough_matches())
1420 return;
1421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 // Update the screen later, before drawing the popup menu over it.
1423 pum_call_update_screen();
1424
1425 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001426 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001427 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001428 else
1429 {
1430 // popup menu already exists, only need to find the current item.
1431 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001432 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001433 || compl_match_array[i].pum_text
1434 == compl_shown_match->cp_text[CPT_ABBR])
1435 {
1436 cur = i;
1437 break;
1438 }
1439 }
1440
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001441 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001442 {
1443#ifdef FEAT_EVAL
1444 if (compl_started && has_completechanged())
1445 trigger_complete_changed_event(cur);
1446#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001447 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001448 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001449
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001450 // In Replace mode when a $ is displayed at the end of the line only
1451 // part of the screen would be updated. We do need to redraw here.
1452 dollar_vcol = -1;
1453
1454 // Compute the screen column of the start of the completed text.
1455 // Use the cursor to get all wrapping and other settings right.
1456 col = curwin->w_cursor.col;
1457 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001458 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001459 pum_display(compl_match_array, compl_match_arraysize, cur);
1460 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001461
glepnircbb46b42024-02-03 18:11:13 +01001462 // After adding leader, set the current match to shown match.
1463 if (compl_started && compl_curr_match != compl_shown_match)
1464 compl_curr_match = compl_shown_match;
1465
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001466#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001467 if (has_completechanged())
1468 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001469#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001470}
1471
1472#define DICT_FIRST (1) // use just first element in "dict"
1473#define DICT_EXACT (2) // "dict" is the exact name of a file
1474
1475/*
glepnir40c1c332024-06-11 19:37:04 +02001476 * Get current completion leader
1477 */
1478 char_u *
1479ins_compl_leader(void)
1480{
John Marriott5e6ea922024-11-23 14:01:57 +01001481 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1482}
1483
1484/*
1485 * Get current completion leader length
1486 */
1487 size_t
1488ins_compl_leader_len(void)
1489{
1490 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001491}
1492
1493/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001494 * Add any identifiers that match the given pattern "pat" in the list of
1495 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001496 */
1497 static void
1498ins_compl_dictionaries(
1499 char_u *dict_start,
1500 char_u *pat,
1501 int flags, // DICT_FIRST and/or DICT_EXACT
1502 int thesaurus) // Thesaurus completion
1503{
1504 char_u *dict = dict_start;
1505 char_u *ptr;
1506 char_u *buf;
1507 regmatch_T regmatch;
1508 char_u **files;
1509 int count;
1510 int save_p_scs;
1511 int dir = compl_direction;
1512
1513 if (*dict == NUL)
1514 {
1515#ifdef FEAT_SPELL
1516 // When 'dictionary' is empty and spell checking is enabled use
1517 // "spell".
1518 if (!thesaurus && curwin->w_p_spell)
1519 dict = (char_u *)"spell";
1520 else
1521#endif
1522 return;
1523 }
1524
1525 buf = alloc(LSIZE);
1526 if (buf == NULL)
1527 return;
1528 regmatch.regprog = NULL; // so that we can goto theend
1529
1530 // If 'infercase' is set, don't use 'smartcase' here
1531 save_p_scs = p_scs;
1532 if (curbuf->b_p_inf)
1533 p_scs = FALSE;
1534
1535 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1536 // to only match at the start of a line. Otherwise just match the
1537 // pattern. Also need to double backslashes.
1538 if (ctrl_x_mode_line_or_eval())
1539 {
1540 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1541 size_t len;
1542
1543 if (pat_esc == NULL)
1544 goto theend;
1545 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001546 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001547 if (ptr == NULL)
1548 {
1549 vim_free(pat_esc);
1550 goto theend;
1551 }
1552 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1553 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1554 vim_free(pat_esc);
1555 vim_free(ptr);
1556 }
1557 else
1558 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001559 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001560 if (regmatch.regprog == NULL)
1561 goto theend;
1562 }
1563
1564 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1565 regmatch.rm_ic = ignorecase(pat);
1566 while (*dict != NUL && !got_int && !compl_interrupted)
1567 {
1568 // copy one dictionary file name into buf
1569 if (flags == DICT_EXACT)
1570 {
1571 count = 1;
1572 files = &dict;
1573 }
1574 else
1575 {
1576 // Expand wildcards in the dictionary name, but do not allow
1577 // backticks (for security, the 'dict' option may have been set in
1578 // a modeline).
1579 copy_option_part(&dict, buf, LSIZE, ",");
1580# ifdef FEAT_SPELL
1581 if (!thesaurus && STRCMP(buf, "spell") == 0)
1582 count = -1;
1583 else
1584# endif
1585 if (vim_strchr(buf, '`') != NULL
1586 || expand_wildcards(1, &buf, &count, &files,
1587 EW_FILE|EW_SILENT) != OK)
1588 count = 0;
1589 }
1590
1591# ifdef FEAT_SPELL
1592 if (count == -1)
1593 {
1594 // Complete from active spelling. Skip "\<" in the pattern, we
1595 // don't use it as a RE.
1596 if (pat[0] == '\\' && pat[1] == '<')
1597 ptr = pat + 2;
1598 else
1599 ptr = pat;
1600 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1601 }
1602 else
1603# endif
1604 if (count > 0) // avoid warning for using "files" uninit
1605 {
1606 ins_compl_files(count, files, thesaurus, flags,
1607 &regmatch, buf, &dir);
1608 if (flags != DICT_EXACT)
1609 FreeWild(count, files);
1610 }
1611 if (flags != 0)
1612 break;
1613 }
1614
1615theend:
1616 p_scs = save_p_scs;
1617 vim_regfree(regmatch.regprog);
1618 vim_free(buf);
1619}
1620
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001621/*
1622 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1623 * skipping the word at 'skip_word'. Returns OK on success.
1624 */
1625 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001626thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001627 char_u *fname,
1628 char_u **buf_arg,
1629 int dir,
1630 char_u *skip_word)
1631{
1632 int status = OK;
1633 char_u *ptr;
1634 char_u *wstart;
1635
1636 // Add the other matches on the line
1637 ptr = *buf_arg;
1638 while (!got_int)
1639 {
1640 // Find start of the next word. Skip white
1641 // space and punctuation.
1642 ptr = find_word_start(ptr);
1643 if (*ptr == NUL || *ptr == NL)
1644 break;
1645 wstart = ptr;
1646
1647 // Find end of the word.
1648 if (has_mbyte)
1649 // Japanese words may have characters in
1650 // different classes, only separate words
1651 // with single-byte non-word characters.
1652 while (*ptr != NUL)
1653 {
1654 int l = (*mb_ptr2len)(ptr);
1655
1656 if (l < 2 && !vim_iswordc(*ptr))
1657 break;
1658 ptr += l;
1659 }
1660 else
1661 ptr = find_word_end(ptr);
1662
1663 // Add the word. Skip the regexp match.
1664 if (wstart != skip_word)
1665 {
1666 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1667 fname, dir, FALSE);
1668 if (status == FAIL)
1669 break;
1670 }
1671 }
1672
1673 *buf_arg = ptr;
1674 return status;
1675}
1676
1677/*
1678 * Process "count" dictionary/thesaurus "files" and add the text matching
1679 * "regmatch".
1680 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 static void
1682ins_compl_files(
1683 int count,
1684 char_u **files,
1685 int thesaurus,
1686 int flags,
1687 regmatch_T *regmatch,
1688 char_u *buf,
1689 int *dir)
1690{
1691 char_u *ptr;
1692 int i;
1693 FILE *fp;
1694 int add_r;
1695
1696 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1697 {
1698 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001699 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001700 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001701 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001702 vim_snprintf((char *)IObuff, IOSIZE,
1703 _("Scanning dictionary: %s"), (char *)files[i]);
1704 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1705 }
1706
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001707 if (fp == NULL)
1708 continue;
1709
1710 // Read dictionary file line by line.
1711 // Check each line for a match.
1712 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 ptr = buf;
1715 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001716 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001718 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1719 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 add_r = ins_compl_add_infercase(regmatch->startp[0],
1721 (int)(ptr - regmatch->startp[0]),
1722 p_ic, files[i], *dir, FALSE);
1723 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001724 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001725 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001726 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001727 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001728 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001729 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001730 if (add_r == OK)
1731 // if dir was BACKWARD then honor it just once
1732 *dir = FORWARD;
1733 else if (add_r == FAIL)
1734 break;
1735 // avoid expensive call to vim_regexec() when at end
1736 // of line
1737 if (*ptr == '\n' || got_int)
1738 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001739 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001740 line_breakcheck();
1741 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001742 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001743 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001744 }
1745}
1746
1747/*
1748 * Find the start of the next word.
1749 * Returns a pointer to the first char of the word. Also stops at a NUL.
1750 */
1751 char_u *
1752find_word_start(char_u *ptr)
1753{
1754 if (has_mbyte)
1755 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1756 ptr += (*mb_ptr2len)(ptr);
1757 else
1758 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1759 ++ptr;
1760 return ptr;
1761}
1762
1763/*
1764 * Find the end of the word. Assumes it starts inside a word.
1765 * Returns a pointer to just after the word.
1766 */
1767 char_u *
1768find_word_end(char_u *ptr)
1769{
1770 int start_class;
1771
1772 if (has_mbyte)
1773 {
1774 start_class = mb_get_class(ptr);
1775 if (start_class > 1)
1776 while (*ptr != NUL)
1777 {
1778 ptr += (*mb_ptr2len)(ptr);
1779 if (mb_get_class(ptr) != start_class)
1780 break;
1781 }
1782 }
1783 else
1784 while (vim_iswordc(*ptr))
1785 ++ptr;
1786 return ptr;
1787}
1788
1789/*
1790 * Find the end of the line, omitting CR and NL at the end.
1791 * Returns a pointer to just after the line.
1792 */
1793 static char_u *
1794find_line_end(char_u *ptr)
1795{
1796 char_u *s;
1797
1798 s = ptr + STRLEN(ptr);
1799 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1800 --s;
1801 return s;
1802}
1803
1804/*
1805 * Free the list of completions
1806 */
1807 static void
1808ins_compl_free(void)
1809{
1810 compl_T *match;
1811 int i;
1812
John Marriott5e6ea922024-11-23 14:01:57 +01001813 VIM_CLEAR_STRING(compl_pattern);
1814 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001815
1816 if (compl_first_match == NULL)
1817 return;
1818
1819 ins_compl_del_pum();
1820 pum_clear();
1821
1822 compl_curr_match = compl_first_match;
1823 do
1824 {
1825 match = compl_curr_match;
1826 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001827 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001828 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001829 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001830 vim_free(match->cp_fname);
1831 for (i = 0; i < CPT_COUNT; ++i)
1832 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001833#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001834 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001835#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001836 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001837 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001838 compl_first_match = compl_curr_match = NULL;
1839 compl_shown_match = NULL;
1840 compl_old_match = NULL;
1841}
1842
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001843/*
1844 * Reset/clear the completion state.
1845 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001846 void
1847ins_compl_clear(void)
1848{
1849 compl_cont_status = 0;
1850 compl_started = FALSE;
1851 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001852 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001853 VIM_CLEAR_STRING(compl_pattern);
1854 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001855 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001856 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001857 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001858#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001859 // clear v:completed_item
1860 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001861#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001862}
1863
1864/*
1865 * Return TRUE when Insert completion is active.
1866 */
1867 int
1868ins_compl_active(void)
1869{
1870 return compl_started;
1871}
1872
1873/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001874 * Return True when wp is the actual completion window
1875 */
1876 int
1877ins_compl_win_active(win_T *wp UNUSED)
1878{
1879 return ins_compl_active()
1880#if defined(FEAT_QUICKFIX)
1881 && (!wp->w_p_pvw
1882# ifdef FEAT_PROP_POPUP
1883 && !(wp->w_popup_flags & POPF_INFO)
1884# endif
1885 )
1886#endif
1887 ;
1888}
1889
1890/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001891 * Selected one of the matches. When FALSE the match was edited or using the
1892 * longest common string.
1893 */
1894 int
1895ins_compl_used_match(void)
1896{
1897 return compl_used_match;
1898}
1899
1900/*
1901 * Initialize get longest common string.
1902 */
1903 void
1904ins_compl_init_get_longest(void)
1905{
1906 compl_get_longest = FALSE;
1907}
1908
1909/*
1910 * Returns TRUE when insert completion is interrupted.
1911 */
1912 int
1913ins_compl_interrupted(void)
1914{
1915 return compl_interrupted;
1916}
1917
1918/*
1919 * Returns TRUE if the <Enter> key selects a match in the completion popup
1920 * menu.
1921 */
1922 int
1923ins_compl_enter_selects(void)
1924{
1925 return compl_enter_selects;
1926}
1927
1928/*
1929 * Return the column where the text starts that is being completed
1930 */
1931 colnr_T
1932ins_compl_col(void)
1933{
1934 return compl_col;
1935}
1936
1937/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001938 * Return the length in bytes of the text being completed
1939 */
1940 int
1941ins_compl_len(void)
1942{
1943 return compl_length;
1944}
1945
1946/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001947 * Delete one character before the cursor and show the subset of the matches
1948 * that match the word that is now before the cursor.
1949 * Returns the character to be used, NUL if the work is done and another char
1950 * to be got from the user.
1951 */
1952 int
1953ins_compl_bs(void)
1954{
1955 char_u *line;
1956 char_u *p;
1957
1958 line = ml_get_curline();
1959 p = line + curwin->w_cursor.col;
1960 MB_PTR_BACK(line, p);
1961
1962 // Stop completion when the whole word was deleted. For Omni completion
1963 // allow the word to be deleted, we won't match everything.
1964 // Respect the 'backspace' option.
1965 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001966 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1967 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001968 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1969 - compl_length < 0))
1970 return K_BS;
1971
1972 // Deleted more than what was used to find matches or didn't finish
1973 // finding all matches: need to look for matches all over again.
1974 if (curwin->w_cursor.col <= compl_col + compl_length
1975 || ins_compl_need_restart())
1976 ins_compl_restart();
1977
John Marriott5e6ea922024-11-23 14:01:57 +01001978 VIM_CLEAR_STRING(compl_leader);
1979 compl_leader.length = (size_t)((p - line) - compl_col);
1980 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1981 if (compl_leader.string == NULL)
1982 {
1983 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001984 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001985 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001986
1987 ins_compl_new_leader();
1988 if (compl_shown_match != NULL)
1989 // Make sure current match is not a hidden item.
1990 compl_curr_match = compl_shown_match;
1991 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992}
1993
1994/*
1995 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1996 * be called.
1997 */
1998 static int
1999ins_compl_need_restart(void)
2000{
2001 // Return TRUE if we didn't complete finding matches or when the
2002 // 'completefunc' returned "always" in the "refresh" dictionary item.
2003 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002004 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002005 && compl_opt_refresh_always);
2006}
2007
2008/*
2009 * Called after changing "compl_leader".
2010 * Show the popup menu with a different set of matches.
2011 * May also search for matches again if the previous search was interrupted.
2012 */
2013 static void
2014ins_compl_new_leader(void)
2015{
2016 ins_compl_del_pum();
2017 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002018 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002019 compl_used_match = FALSE;
2020
2021 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002022 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 else
2024 {
2025#ifdef FEAT_SPELL
2026 spell_bad_len = 0; // need to redetect bad word
2027#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002028 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002029 // the popup menu display the changed text before the cursor. Set
2030 // "compl_restarting" to avoid that the first match is inserted.
2031 pum_call_update_screen();
2032#ifdef FEAT_GUI
2033 if (gui.in_use)
2034 {
2035 // Show the cursor after the match, not after the redrawn text.
2036 setcursor();
2037 out_flush_cursor(FALSE, FALSE);
2038 }
2039#endif
2040 compl_restarting = TRUE;
2041 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2042 compl_cont_status = 0;
2043 compl_restarting = FALSE;
2044 }
2045
2046 compl_enter_selects = !compl_used_match;
2047
2048 // Show the popup menu with a different set of matches.
2049 ins_compl_show_pum();
2050
2051 // Don't let Enter select the original text when there is no popup menu.
2052 if (compl_match_array == NULL)
2053 compl_enter_selects = FALSE;
2054}
2055
2056/*
2057 * Return the length of the completion, from the completion start column to
2058 * the cursor column. Making sure it never goes below zero.
2059 */
2060 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002061get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002062{
2063 int off = (int)curwin->w_cursor.col - (int)compl_col;
2064
2065 if (off < 0)
2066 return 0;
2067 return off;
2068}
2069
2070/*
2071 * Append one character to the match leader. May reduce the number of
2072 * matches.
2073 */
2074 void
2075ins_compl_addleader(int c)
2076{
2077 int cc;
2078
2079 if (stop_arrow() == FAIL)
2080 return;
2081 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2082 {
2083 char_u buf[MB_MAXBYTES + 1];
2084
2085 (*mb_char2bytes)(c, buf);
2086 buf[cc] = NUL;
2087 ins_char_bytes(buf, cc);
2088 if (compl_opt_refresh_always)
2089 AppendToRedobuff(buf);
2090 }
2091 else
2092 {
2093 ins_char(c);
2094 if (compl_opt_refresh_always)
2095 AppendCharToRedobuff(c);
2096 }
2097
2098 // If we didn't complete finding matches we must search again.
2099 if (ins_compl_need_restart())
2100 ins_compl_restart();
2101
2102 // When 'always' is set, don't reset compl_leader. While completing,
2103 // cursor doesn't point original position, changing compl_leader would
2104 // break redo.
2105 if (!compl_opt_refresh_always)
2106 {
John Marriott5e6ea922024-11-23 14:01:57 +01002107 VIM_CLEAR_STRING(compl_leader);
2108 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2109 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2110 compl_leader.length);
2111 if (compl_leader.string == NULL)
2112 {
2113 compl_leader.length = 0;
2114 return;
2115 }
2116
2117 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002118 }
2119}
2120
2121/*
2122 * Setup for finding completions again without leaving CTRL-X mode. Used when
2123 * BS or a key was typed while still searching for matches.
2124 */
2125 static void
2126ins_compl_restart(void)
2127{
2128 ins_compl_free();
2129 compl_started = FALSE;
2130 compl_matches = 0;
2131 compl_cont_status = 0;
2132 compl_cont_mode = 0;
2133}
2134
2135/*
2136 * Set the first match, the original text.
2137 */
2138 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002139ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002140{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002142 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2143 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002144 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002145 {
John Marriott5e6ea922024-11-23 14:01:57 +01002146 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002147 if (p != NULL)
2148 {
John Marriott5e6ea922024-11-23 14:01:57 +01002149 VIM_CLEAR_STRING(compl_first_match->cp_str);
2150 compl_first_match->cp_str.string = p;
2151 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002152 }
2153 }
2154 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002155 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002156 {
John Marriott5e6ea922024-11-23 14:01:57 +01002157 char_u *p = vim_strnsave(str, len);
2158 if (p != NULL)
2159 {
2160 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2161 compl_first_match->cp_prev->cp_str.string = p;
2162 compl_first_match->cp_prev->cp_str.length = len;
2163 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002164 }
2165}
2166
2167/*
2168 * Append one character to the match leader. May reduce the number of
2169 * matches.
2170 */
2171 void
2172ins_compl_addfrommatch(void)
2173{
2174 char_u *p;
2175 int len = (int)curwin->w_cursor.col - (int)compl_col;
2176 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002177
John Marriott5e6ea922024-11-23 14:01:57 +01002178 p = compl_shown_match->cp_str.string;
2179 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002180 {
John Marriott5e6ea922024-11-23 14:01:57 +01002181 size_t plen;
2182 compl_T *cp;
2183
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002184 // When still at the original match use the first entry that matches
2185 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002186 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002187 return;
2188
2189 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002190 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002191 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002192 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002193 {
John Marriott5e6ea922024-11-23 14:01:57 +01002194 if (compl_leader.string == NULL
2195 || ins_compl_equal(cp, compl_leader.string,
2196 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002197 {
John Marriott5e6ea922024-11-23 14:01:57 +01002198 p = cp->cp_str.string;
2199 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002200 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002201 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002202 }
John Marriott5e6ea922024-11-23 14:01:57 +01002203 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002204 return;
2205 }
2206 p += len;
2207 c = PTR2CHAR(p);
2208 ins_compl_addleader(c);
2209}
2210
2211/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002212 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002213 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2214 * compl_cont_mode and compl_cont_status.
2215 * Returns TRUE when the character is not to be inserted.
2216 */
2217 static int
2218set_ctrl_x_mode(int c)
2219{
2220 int retval = FALSE;
2221
2222 switch (c)
2223 {
2224 case Ctrl_E:
2225 case Ctrl_Y:
2226 // scroll the window one line up or down
2227 ctrl_x_mode = CTRL_X_SCROLL;
2228 if (!(State & REPLACE_FLAG))
2229 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2230 else
2231 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2232 edit_submode_pre = NULL;
2233 showmode();
2234 break;
2235 case Ctrl_L:
2236 // complete whole line
2237 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2238 break;
2239 case Ctrl_F:
2240 // complete filenames
2241 ctrl_x_mode = CTRL_X_FILES;
2242 break;
2243 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002244 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002245 ctrl_x_mode = CTRL_X_DICTIONARY;
2246 break;
2247 case Ctrl_R:
2248 // Register insertion without exiting CTRL-X mode
2249 // Simply allow ^R to happen without affecting ^X mode
2250 break;
2251 case Ctrl_T:
2252 // complete words from a thesaurus
2253 ctrl_x_mode = CTRL_X_THESAURUS;
2254 break;
2255#ifdef FEAT_COMPL_FUNC
2256 case Ctrl_U:
2257 // user defined completion
2258 ctrl_x_mode = CTRL_X_FUNCTION;
2259 break;
2260 case Ctrl_O:
2261 // omni completion
2262 ctrl_x_mode = CTRL_X_OMNI;
2263 break;
2264#endif
2265 case 's':
2266 case Ctrl_S:
2267 // complete spelling suggestions
2268 ctrl_x_mode = CTRL_X_SPELL;
2269#ifdef FEAT_SPELL
2270 ++emsg_off; // Avoid getting the E756 error twice.
2271 spell_back_to_badword();
2272 --emsg_off;
2273#endif
2274 break;
2275 case Ctrl_RSB:
2276 // complete tag names
2277 ctrl_x_mode = CTRL_X_TAGS;
2278 break;
2279#ifdef FEAT_FIND_ID
2280 case Ctrl_I:
2281 case K_S_TAB:
2282 // complete keywords from included files
2283 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2284 break;
2285 case Ctrl_D:
2286 // complete definitions from included files
2287 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2288 break;
2289#endif
2290 case Ctrl_V:
2291 case Ctrl_Q:
2292 // complete vim commands
2293 ctrl_x_mode = CTRL_X_CMDLINE;
2294 break;
2295 case Ctrl_Z:
2296 // stop completion
2297 ctrl_x_mode = CTRL_X_NORMAL;
2298 edit_submode = NULL;
2299 showmode();
2300 retval = TRUE;
2301 break;
2302 case Ctrl_P:
2303 case Ctrl_N:
2304 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2305 // just started ^X mode, or there were enough ^X's to cancel
2306 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2307 // do normal expansion when interrupting a different mode (say
2308 // ^X^F^X^P or ^P^X^X^P, see below)
2309 // nothing changes if interrupting mode 0, (eg, the flag
2310 // doesn't change when going to ADDING mode -- Acevedo
2311 if (!(compl_cont_status & CONT_INTRPT))
2312 compl_cont_status |= CONT_LOCAL;
2313 else if (compl_cont_mode != 0)
2314 compl_cont_status &= ~CONT_LOCAL;
2315 // FALLTHROUGH
2316 default:
2317 // If we have typed at least 2 ^X's... for modes != 0, we set
2318 // compl_cont_status = 0 (eg, as if we had just started ^X
2319 // mode).
2320 // For mode 0, we set "compl_cont_mode" to an impossible
2321 // value, in both cases ^X^X can be used to restart the same
2322 // mode (avoiding ADDING mode).
2323 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2324 // 'complete' and local ^P expansions respectively.
2325 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2326 // mode -- Acevedo
2327 if (c == Ctrl_X)
2328 {
2329 if (compl_cont_mode != 0)
2330 compl_cont_status = 0;
2331 else
2332 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2333 }
2334 ctrl_x_mode = CTRL_X_NORMAL;
2335 edit_submode = NULL;
2336 showmode();
2337 break;
2338 }
2339
2340 return retval;
2341}
2342
2343/*
glepnir1c5a1202024-12-04 20:27:34 +01002344 * Trigger CompleteDone event and adds relevant information to v:event
2345 */
2346 static void
2347trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2348{
2349#if defined(FEAT_EVAL)
2350 save_v_event_T save_v_event;
2351 dict_T *v_event = get_v_event(&save_v_event);
2352 char_u *mode_str = NULL;
2353
2354 mode = mode & ~CTRL_X_WANT_IDENT;
2355 if (ctrl_x_mode_names[mode])
2356 mode_str = (char_u *)ctrl_x_mode_names[mode];
2357
2358 (void)dict_add_string(v_event, "complete_word",
2359 word == NULL ? (char_u *)"" : word);
2360 (void)dict_add_string(v_event, "complete_type",
2361 mode_str != NULL ? mode_str : (char_u *)"");
2362
2363 dict_set_items_ro(v_event);
2364#endif
2365 ins_apply_autocmds(EVENT_COMPLETEDONE);
2366
2367#if defined(FEAT_EVAL)
2368 restore_v_event(v_event, &save_v_event);
2369#endif
2370}
2371
2372/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002373 * Stop insert completion mode
2374 */
2375 static int
2376ins_compl_stop(int c, int prev_mode, int retval)
2377{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002378 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002379 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002380
2381 // Get here when we have finished typing a sequence of ^N and
2382 // ^P or other completion characters in CTRL-X mode. Free up
2383 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002384 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002385 {
John Marriott5e6ea922024-11-23 14:01:57 +01002386 char_u *ptr;
2387
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002388 // If any of the original typed text has been changed, eg when
2389 // ignorecase is set, we must add back-spaces to the redo
2390 // buffer. We add as few as necessary to delete just the part
2391 // of the original text that has changed.
2392 // When using the longest match, edited the match or used
2393 // CTRL-E then don't use the current match.
2394 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002395 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002396 else
2397 ptr = NULL;
2398 ins_compl_fixRedoBufForLeader(ptr);
2399 }
2400
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002401 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002402
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002403 // When completing whole lines: fix indent for 'cindent'.
2404 // Otherwise, break line if it's too long.
2405 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2406 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002407 // re-indent the current line
2408 if (want_cindent)
2409 {
2410 do_c_expr_indent();
2411 want_cindent = FALSE; // don't do it again
2412 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002413 }
2414 else
2415 {
2416 int prev_col = curwin->w_cursor.col;
2417
2418 // put the cursor on the last char, for 'tw' formatting
2419 if (prev_col > 0)
2420 dec_cursor();
2421 // only format when something was inserted
2422 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2423 insertchar(NUL, 0, -1);
2424 if (prev_col > 0
2425 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2426 inc_cursor();
2427 }
2428
2429 // If the popup menu is displayed pressing CTRL-Y means accepting
2430 // the selection without inserting anything. When
2431 // compl_enter_selects is set the Enter key does the same.
2432 if ((c == Ctrl_Y || (compl_enter_selects
2433 && (c == CAR || c == K_KENTER || c == NL)))
2434 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002435 {
2436 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002437 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002438 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002439
2440 // CTRL-E means completion is Ended, go back to the typed text.
2441 // but only do this, if the Popup is still visible
2442 if (c == Ctrl_E)
2443 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002444 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002445 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002446
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002447 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002448 if (compl_leader.string != NULL)
2449 {
2450 p = compl_leader.string;
2451 plen = compl_leader.length;
2452 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002453 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002454 {
2455 p = compl_orig_text.string;
2456 plen = compl_orig_text.length;
2457 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002458 if (p != NULL)
2459 {
2460 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002461
John Marriott5e6ea922024-11-23 14:01:57 +01002462 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002463 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002464 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002465 retval = TRUE;
2466 }
2467
2468 auto_format(FALSE, TRUE);
2469
2470 // Trigger the CompleteDonePre event to give scripts a chance to
2471 // act upon the completion before clearing the info, and restore
2472 // ctrl_x_mode, so that complete_info() can be used.
2473 ctrl_x_mode = prev_mode;
2474 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2475
2476 ins_compl_free();
2477 compl_started = FALSE;
2478 compl_matches = 0;
2479 if (!shortmess(SHM_COMPLETIONMENU))
2480 msg_clr_cmdline(); // necessary for "noshowmode"
2481 ctrl_x_mode = CTRL_X_NORMAL;
2482 compl_enter_selects = FALSE;
2483 if (edit_submode != NULL)
2484 {
2485 edit_submode = NULL;
2486 showmode();
2487 }
2488
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002489 if (c == Ctrl_C && cmdwin_type != 0)
2490 // Avoid the popup menu remains displayed when leaving the
2491 // command line window.
2492 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002493 // Indent now if a key was typed that is in 'cinkeys'.
2494 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2495 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002496 // Trigger the CompleteDone event to give scripts a chance to act
2497 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002498 trigger_complete_done_event(prev_mode, word);
2499 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002500
2501 return retval;
2502}
2503
2504/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002505 * Prepare for Insert mode completion, or stop it.
2506 * Called just after typing a character in Insert mode.
2507 * Returns TRUE when the character is not to be inserted;
2508 */
2509 int
2510ins_compl_prep(int c)
2511{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002512 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002513 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002514
2515 // Forget any previous 'special' messages if this is actually
2516 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2517 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2518 edit_submode_extra = NULL;
2519
zeertzjq440d4cb2023-03-02 17:51:32 +00002520 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002521 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002522 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002523 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002524 return retval;
2525
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002526#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002527 // Ignore mouse events in a popup window
2528 if (is_mouse_key(c))
2529 {
2530 // Ignore drag and release events, the position does not need to be in
2531 // the popup and it may have just closed.
2532 if (c == K_LEFTRELEASE
2533 || c == K_LEFTRELEASE_NM
2534 || c == K_MIDDLERELEASE
2535 || c == K_RIGHTRELEASE
2536 || c == K_X1RELEASE
2537 || c == K_X2RELEASE
2538 || c == K_LEFTDRAG
2539 || c == K_MIDDLEDRAG
2540 || c == K_RIGHTDRAG
2541 || c == K_X1DRAG
2542 || c == K_X2DRAG)
2543 return retval;
2544 if (popup_visible)
2545 {
2546 int row = mouse_row;
2547 int col = mouse_col;
2548 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2549
2550 if (wp != NULL && WIN_IS_POPUP(wp))
2551 return retval;
2552 }
2553 }
2554#endif
2555
zeertzjqdca29d92021-08-31 19:12:51 +02002556 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2557 {
2558 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2559 || !vim_is_ctrl_x_key(c))
2560 {
2561 // Not starting another completion mode.
2562 ctrl_x_mode = CTRL_X_CMDLINE;
2563
2564 // CTRL-X CTRL-Z should stop completion without inserting anything
2565 if (c == Ctrl_Z)
2566 retval = TRUE;
2567 }
2568 else
2569 {
2570 ctrl_x_mode = CTRL_X_CMDLINE;
2571
2572 // Other CTRL-X keys first stop completion, then start another
2573 // completion mode.
2574 ins_compl_prep(' ');
2575 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2576 }
2577 }
2578
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002579 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002580 if (ctrl_x_mode_not_defined_yet()
2581 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002583 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002584 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002585 }
2586
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002587 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002588 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2589 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002590 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002591 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002592 {
2593 // We're already in CTRL-X mode, do we stay in it?
2594 if (!vim_is_ctrl_x_key(c))
2595 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002596 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002597 ctrl_x_mode = CTRL_X_NORMAL;
2598 else
2599 ctrl_x_mode = CTRL_X_FINISHED;
2600 edit_submode = NULL;
2601 }
2602 showmode();
2603 }
2604
2605 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2606 {
2607 // Show error message from attempted keyword completion (probably
2608 // 'Pattern not found') until another key is hit, then go back to
2609 // showing what mode we are in.
2610 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002611 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002612 && c != Ctrl_R && !ins_compl_pum_key(c))
2613 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002614 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002615 }
2616 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2617 // Trigger the CompleteDone event to give scripts a chance to act
2618 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002619 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620
LemonBoy2bf52dd2022-04-09 18:17:34 +01002621 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002622
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002623 // reset continue_* if we left expansion-mode, if we stay they'll be
2624 // (re)set properly in ins_complete()
2625 if (!vim_is_ctrl_x_key(c))
2626 {
2627 compl_cont_status = 0;
2628 compl_cont_mode = 0;
2629 }
2630
2631 return retval;
2632}
2633
2634/*
2635 * Fix the redo buffer for the completion leader replacing some of the typed
2636 * text. This inserts backspaces and appends the changed text.
2637 * "ptr" is the known leader text or NUL.
2638 */
2639 static void
2640ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2641{
John Marriott5e6ea922024-11-23 14:01:57 +01002642 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002643 char_u *p;
2644 char_u *ptr = ptr_arg;
2645
2646 if (ptr == NULL)
2647 {
John Marriott5e6ea922024-11-23 14:01:57 +01002648 if (compl_leader.string != NULL)
2649 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002650 else
2651 return; // nothing to do
2652 }
John Marriott5e6ea922024-11-23 14:01:57 +01002653 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002654 {
John Marriott5e6ea922024-11-23 14:01:57 +01002655 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002656 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2657 ;
2658 if (len > 0)
2659 len -= (*mb_head_off)(p, p + len);
2660 for (p += len; *p != NUL; MB_PTR_ADV(p))
2661 AppendCharToRedobuff(K_BS);
2662 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002663 if (ptr != NULL)
2664 AppendToRedobuffLit(ptr + len, -1);
2665}
2666
2667/*
2668 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2669 * (depending on flag) starting from buf and looking for a non-scanned
2670 * buffer (other than curbuf). curbuf is special, if it is called with
2671 * buf=curbuf then it has to be the first call for a given flag/expansion.
2672 *
2673 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2674 */
2675 static buf_T *
2676ins_compl_next_buf(buf_T *buf, int flag)
2677{
2678 static win_T *wp = NULL;
2679
2680 if (flag == 'w') // just windows
2681 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002682 if (buf == curbuf || !win_valid(wp))
2683 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002684 wp = curwin;
2685 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2686 && wp->w_buffer->b_scanned)
2687 ;
2688 buf = wp->w_buffer;
2689 }
2690 else
2691 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2692 // (unlisted buffers)
2693 // When completing whole lines skip unloaded buffers.
2694 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2695 && ((flag == 'U'
2696 ? buf->b_p_bl
2697 : (!buf->b_p_bl
2698 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2699 || buf->b_scanned))
2700 ;
2701 return buf;
2702}
2703
2704#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002705
2706# ifdef FEAT_EVAL
2707static callback_T cfu_cb; // 'completefunc' callback function
2708static callback_T ofu_cb; // 'omnifunc' callback function
2709static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2710# endif
2711
2712/*
2713 * Copy a global callback function to a buffer local callback.
2714 */
2715 static void
2716copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2717{
2718 free_callback(bufcb);
2719 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2720 copy_callback(bufcb, globcb);
2721}
2722
2723/*
2724 * Parse the 'completefunc' option value and set the callback function.
2725 * Invoked when the 'completefunc' option is set. The option value can be a
2726 * name of a function (string), or function(<name>) or funcref(<name>) or a
2727 * lambda expression.
2728 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002729 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002730did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002731{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2733 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002734
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002736
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002737 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002738}
2739
2740/*
2741 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002742 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002743 */
2744 void
2745set_buflocal_cfu_callback(buf_T *buf UNUSED)
2746{
2747# ifdef FEAT_EVAL
2748 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2749# endif
2750}
2751
2752/*
2753 * Parse the 'omnifunc' option value and set the callback function.
2754 * Invoked when the 'omnifunc' option is set. The option value can be a
2755 * name of a function (string), or function(<name>) or funcref(<name>) or a
2756 * lambda expression.
2757 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002758 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002759did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002760{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2762 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002763
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002764 set_buflocal_ofu_callback(curbuf);
2765 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002766}
2767
2768/*
2769 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002770 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002771 */
2772 void
2773set_buflocal_ofu_callback(buf_T *buf UNUSED)
2774{
2775# ifdef FEAT_EVAL
2776 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2777# endif
2778}
2779
2780/*
2781 * Parse the 'thesaurusfunc' option value and set the callback function.
2782 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2783 * name of a function (string), or function(<name>) or funcref(<name>) or a
2784 * lambda expression.
2785 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002786 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002787did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002788{
2789 int retval;
2790
zeertzjq6eda2692024-11-03 09:23:33 +01002791 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002792 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002793 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2794 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795 else
2796 {
2797 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002798 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002799 // when using :set, free the local callback
2800 if (!(args->os_flags & OPT_GLOBAL))
2801 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002802 }
2803
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002804 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002805}
2806
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002807/*
2808 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002809 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002810 */
2811 int
2812set_ref_in_insexpand_funcs(int copyID)
2813{
2814 int abort = FALSE;
2815
2816 abort = set_ref_in_callback(&cfu_cb, copyID);
2817 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2818 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2819
2820 return abort;
2821}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002822
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002824 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002825 */
2826 static char_u *
2827get_complete_funcname(int type)
2828{
2829 switch (type)
2830 {
2831 case CTRL_X_FUNCTION:
2832 return curbuf->b_p_cfu;
2833 case CTRL_X_OMNI:
2834 return curbuf->b_p_ofu;
2835 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002836 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002837 default:
2838 return (char_u *)"";
2839 }
2840}
2841
2842/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002843 * Get the callback to use for insert mode completion.
2844 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002845 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002846get_insert_callback(int type)
2847{
2848 if (type == CTRL_X_FUNCTION)
2849 return &curbuf->b_cfu_cb;
2850 if (type == CTRL_X_OMNI)
2851 return &curbuf->b_ofu_cb;
2852 // CTRL_X_THESAURUS
2853 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2854}
2855
2856/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002857 * Execute user defined complete function 'completefunc', 'omnifunc' or
2858 * 'thesaurusfunc', and get matches in "matches".
2859 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002860 */
2861 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002862expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863{
2864 list_T *matchlist = NULL;
2865 dict_T *matchdict = NULL;
2866 typval_T args[3];
2867 char_u *funcname;
2868 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002869 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870 typval_T rettv;
2871 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002872 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002874 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002875 if (*funcname == NUL)
2876 return;
2877
2878 // Call 'completefunc' to obtain the list of matches.
2879 args[0].v_type = VAR_NUMBER;
2880 args[0].vval.v_number = 0;
2881 args[1].v_type = VAR_STRING;
2882 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2883 args[2].v_type = VAR_UNKNOWN;
2884
2885 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002886 // Lock the text to avoid weird things from happening. Also disallow
2887 // switching to another window, it should not be needed and may end up in
2888 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002889 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002890
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002891 cb = get_insert_callback(type);
2892 retval = call_callback(cb, 0, &rettv, 2, args);
2893
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002894 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002895 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002896 {
2897 switch (rettv.v_type)
2898 {
2899 case VAR_LIST:
2900 matchlist = rettv.vval.v_list;
2901 break;
2902 case VAR_DICT:
2903 matchdict = rettv.vval.v_dict;
2904 break;
2905 case VAR_SPECIAL:
2906 if (rettv.vval.v_number == VVAL_NONE)
2907 compl_opt_suppress_empty = TRUE;
2908 // FALLTHROUGH
2909 default:
2910 // TODO: Give error message?
2911 clear_tv(&rettv);
2912 break;
2913 }
2914 }
zeertzjqcfe45652022-05-27 17:26:55 +01002915 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002916
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002917 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002918 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002919 validate_cursor();
2920 if (!EQUAL_POS(curwin->w_cursor, pos))
2921 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002922 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002923 goto theend;
2924 }
2925
2926 if (matchlist != NULL)
2927 ins_compl_add_list(matchlist);
2928 else if (matchdict != NULL)
2929 ins_compl_add_dict(matchdict);
2930
2931theend:
2932 // Restore State, it might have been changed.
2933 State = save_State;
2934
2935 if (matchdict != NULL)
2936 dict_unref(matchdict);
2937 if (matchlist != NULL)
2938 list_unref(matchlist);
2939}
2940#endif // FEAT_COMPL_FUNC
2941
2942#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002943
2944 static inline int
2945get_user_highlight_attr(char_u *hlname)
2946{
2947 if (hlname != NULL && *hlname != NUL)
2948 return syn_name2attr(hlname);
2949 return -1;
2950}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002951/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002952 * Add a match to the list of matches from a typeval_T.
2953 * If the given string is already in the list of completions, then return
2954 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2955 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002956 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002957 */
2958 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002959ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002960{
2961 char_u *word;
2962 int dup = FALSE;
2963 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002964 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002965 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002966 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002967 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002968 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002969 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002970 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002971
Bram Moolenaar08928322020-01-04 14:32:48 +01002972 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002973 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2974 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002975 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2976 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2977 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2978 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2979 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002980
glepnir0fe17f82024-10-08 22:26:44 +02002981 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002982 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002983
2984 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002985 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002986
Bram Moolenaard61efa52022-07-23 09:52:04 +01002987 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2988 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2989 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002990 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002991 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2992 dup = dict_get_number(tv->vval.v_dict, "dup");
2993 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2994 empty = dict_get_number(tv->vval.v_dict, "empty");
2995 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2996 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997 flags |= CP_EQUAL;
2998 }
2999 else
3000 {
3001 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003002 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003003 }
3004 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003005 {
3006 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003007 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003008 }
glepnir508e7852024-07-25 21:39:08 +02003009 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003010 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003011 if (status != OK)
3012 clear_tv(&user_data);
3013 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003014}
3015
3016/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003017 * Add completions from a list.
3018 */
3019 static void
3020ins_compl_add_list(list_T *list)
3021{
3022 listitem_T *li;
3023 int dir = compl_direction;
3024
3025 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003026 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003027 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003028 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003029 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003030 // if dir was BACKWARD then honor it just once
3031 dir = FORWARD;
3032 else if (did_emsg)
3033 break;
3034 }
3035}
3036
3037/*
3038 * Add completions from a dict.
3039 */
3040 static void
3041ins_compl_add_dict(dict_T *dict)
3042{
3043 dictitem_T *di_refresh;
3044 dictitem_T *di_words;
3045
3046 // Check for optional "refresh" item.
3047 compl_opt_refresh_always = FALSE;
3048 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3049 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3050 {
3051 char_u *v = di_refresh->di_tv.vval.v_string;
3052
3053 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3054 compl_opt_refresh_always = TRUE;
3055 }
3056
3057 // Add completions from a "words" list.
3058 di_words = dict_find(dict, (char_u *)"words", 5);
3059 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3060 ins_compl_add_list(di_words->di_tv.vval.v_list);
3061}
3062
3063/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003064 * Start completion for the complete() function.
3065 * "startcol" is where the matched text starts (1 is first column).
3066 * "list" is the list of matches.
3067 */
3068 static void
3069set_completion(colnr_T startcol, list_T *list)
3070{
3071 int save_w_wrow = curwin->w_wrow;
3072 int save_w_leftcol = curwin->w_leftcol;
3073 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003074 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003075 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3076 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3077 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003078
3079 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003080 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081 ins_compl_prep(' ');
3082 ins_compl_clear();
3083 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003084 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003085
3086 compl_direction = FORWARD;
3087 if (startcol > curwin->w_cursor.col)
3088 startcol = curwin->w_cursor.col;
3089 compl_col = startcol;
3090 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3091 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003092 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003093 if (p_ic)
3094 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003095 if (compl_orig_text.string == NULL)
3096 {
3097 compl_orig_text.length = 0;
3098 return;
3099 }
3100 compl_orig_text.length = (size_t)compl_length;
3101 if (ins_compl_add(compl_orig_text.string,
3102 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3103 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003104 return;
3105
3106 ctrl_x_mode = CTRL_X_EVAL;
3107
3108 ins_compl_add_list(list);
3109 compl_matches = ins_compl_make_cyclic();
3110 compl_started = TRUE;
3111 compl_used_match = TRUE;
3112 compl_cont_status = 0;
3113
3114 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003115 int no_select = compl_no_select || compl_longest;
3116 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117 {
3118 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003119 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003120 // Down/Up has no real effect.
3121 ins_complete(K_UP, FALSE);
3122 }
3123 else
3124 ins_complete(Ctrl_N, FALSE);
3125 compl_enter_selects = compl_no_insert;
3126
3127 // Lazily show the popup menu, unless we got interrupted.
3128 if (!compl_interrupted)
3129 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003130 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003131 out_flush();
3132}
3133
3134/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003135 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003136 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003137 void
3138f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003139{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003140 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003141
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003142 if (in_vim9script()
3143 && (check_for_number_arg(argvars, 0) == FAIL
3144 || check_for_list_arg(argvars, 1) == FAIL))
3145 return;
3146
Bram Moolenaar24959102022-05-07 20:01:16 +01003147 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003148 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003149 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003150 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003151 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003152
3153 // Check for undo allowed here, because if something was already inserted
3154 // the line was already saved for undo and this check isn't done.
3155 if (!undo_allowed())
3156 return;
3157
Bram Moolenaard83392a2022-09-01 12:22:46 +01003158 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003159 {
3160 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3161 if (startcol > 0)
3162 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003163 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003164}
3165
3166/*
3167 * "complete_add()" function
3168 */
3169 void
3170f_complete_add(typval_T *argvars, typval_T *rettv)
3171{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003172 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003173 return;
3174
Bram Moolenaar440cf092021-04-03 20:13:30 +02003175 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003176}
3177
3178/*
3179 * "complete_check()" function
3180 */
3181 void
3182f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3183{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003184 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003185 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003186
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003187 ins_compl_check_keys(0, TRUE);
3188 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003189
3190 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003191}
3192
3193/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003194 * Return Insert completion mode name string
3195 */
3196 static char_u *
3197ins_compl_mode(void)
3198{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003199 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003200 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3201
3202 return (char_u *)"";
3203}
3204
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003205/*
3206 * Assign the sequence number to all the completion matches which don't have
3207 * one assigned yet.
3208 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003209 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003210ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003211{
3212 int number = 0;
3213 compl_T *match;
3214
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003215 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003216 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003217 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003218 // This should normally succeed already at the first loop
3219 // cycle, so it's fast!
3220 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003221 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003222 if (match->cp_number != -1)
3223 {
3224 number = match->cp_number;
3225 break;
3226 }
3227 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003228 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003229 for (match = match->cp_next;
3230 match != NULL && match->cp_number == -1;
3231 match = match->cp_next)
3232 match->cp_number = ++number;
3233 }
3234 else // BACKWARD
3235 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003236 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003237 // number. This should normally succeed already at the
3238 // first loop cycle, so it's fast!
3239 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003240 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003241 if (match->cp_number != -1)
3242 {
3243 number = match->cp_number;
3244 break;
3245 }
3246 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003247 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003248 for (match = match->cp_prev; match
3249 && match->cp_number == -1;
3250 match = match->cp_prev)
3251 match->cp_number = ++number;
3252 }
3253}
3254
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003255/*
3256 * Get complete information
3257 */
3258 static void
3259get_complete_info(list_T *what_list, dict_T *retdict)
3260{
3261 int ret = OK;
3262 listitem_T *item;
3263#define CI_WHAT_MODE 0x01
3264#define CI_WHAT_PUM_VISIBLE 0x02
3265#define CI_WHAT_ITEMS 0x04
3266#define CI_WHAT_SELECTED 0x08
3267#define CI_WHAT_INSERTED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003268#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003269#define CI_WHAT_ALL 0xff
3270 int what_flag;
3271
3272 if (what_list == NULL)
glepnird4088ed2024-12-31 10:55:22 +01003273 what_flag = CI_WHAT_ALL & ~CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003274 else
3275 {
3276 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003277 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003278 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003279 {
3280 char_u *what = tv_get_string(&item->li_tv);
3281
3282 if (STRCMP(what, "mode") == 0)
3283 what_flag |= CI_WHAT_MODE;
3284 else if (STRCMP(what, "pum_visible") == 0)
3285 what_flag |= CI_WHAT_PUM_VISIBLE;
3286 else if (STRCMP(what, "items") == 0)
3287 what_flag |= CI_WHAT_ITEMS;
3288 else if (STRCMP(what, "selected") == 0)
3289 what_flag |= CI_WHAT_SELECTED;
3290 else if (STRCMP(what, "inserted") == 0)
3291 what_flag |= CI_WHAT_INSERTED;
glepnird4088ed2024-12-31 10:55:22 +01003292 else if (STRCMP(what, "matches") == 0)
3293 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003294 }
3295 }
3296
3297 if (ret == OK && (what_flag & CI_WHAT_MODE))
3298 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3299
3300 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3301 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3302
glepnird4088ed2024-12-31 10:55:22 +01003303 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED
3304 || what_flag & CI_WHAT_MATCHES))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003305 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003306 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003307 dict_T *di;
3308 compl_T *match;
3309 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003310 int has_items = what_flag & CI_WHAT_ITEMS;
3311 int has_matches = what_flag & CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003312
glepnird4088ed2024-12-31 10:55:22 +01003313 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003314 {
3315 li = list_alloc();
3316 if (li == NULL)
3317 return;
glepnird4088ed2024-12-31 10:55:22 +01003318 ret = dict_add_list(retdict, (has_matches && !has_items)
3319 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003320 }
3321 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3322 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3323 ins_compl_update_sequence_numbers();
3324 if (ret == OK && compl_first_match != NULL)
3325 {
3326 int list_idx = 0;
3327 match = compl_first_match;
3328 do
3329 {
3330 if (!match_at_original_text(match))
3331 {
glepnird4088ed2024-12-31 10:55:22 +01003332 if (has_items
3333 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003334 {
3335 di = dict_alloc();
3336 if (di == NULL)
3337 return;
3338 ret = list_append_dict(li, di);
3339 if (ret != OK)
3340 return;
John Marriott5e6ea922024-11-23 14:01:57 +01003341 dict_add_string(di, "word", match->cp_str.string);
Girish Palya8950bf72024-03-20 20:07:29 +01003342 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3343 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3344 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3345 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
glepnird4088ed2024-12-31 10:55:22 +01003346 if (has_matches && has_items)
3347 dict_add_bool(di, "match", match->cp_in_match_array);
Girish Palya8950bf72024-03-20 20:07:29 +01003348 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3349 // Add an empty string for backwards compatibility
3350 dict_add_string(di, "user_data", (char_u *)"");
3351 else
3352 dict_add_tv(di, "user_data", &match->cp_user_data);
3353 }
glepnird4088ed2024-12-31 10:55:22 +01003354 if (compl_curr_match != NULL
3355 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003356 selected_idx = list_idx;
3357 list_idx += 1;
3358 }
3359 match = match->cp_next;
3360 }
3361 while (match != NULL && !is_first_match(match));
3362 }
3363 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3364 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003365 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003366
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003367 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3368 {
3369 // TODO
3370 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003371}
3372
3373/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003374 * "complete_info()" function
3375 */
3376 void
3377f_complete_info(typval_T *argvars, typval_T *rettv)
3378{
3379 list_T *what_list = NULL;
3380
Bram Moolenaar93a10962022-06-16 11:42:09 +01003381 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003382 return;
3383
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003384 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3385 return;
3386
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003387 if (argvars[0].v_type != VAR_UNKNOWN)
3388 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003389 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003390 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003391 what_list = argvars[0].vval.v_list;
3392 }
3393 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003394}
3395#endif
3396
3397/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003398 * Returns TRUE when using a user-defined function for thesaurus completion.
3399 */
3400 static int
3401thesaurus_func_complete(int type UNUSED)
3402{
3403#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003404 return type == CTRL_X_THESAURUS
3405 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003406#else
3407 return FALSE;
3408#endif
3409}
3410
3411/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003412 * Return value of process_next_cpt_value()
3413 */
3414enum
3415{
3416 INS_COMPL_CPT_OK = 1,
3417 INS_COMPL_CPT_CONT,
3418 INS_COMPL_CPT_END
3419};
3420
3421/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003422 * state information used for getting the next set of insert completion
3423 * matches.
3424 */
3425typedef struct
3426{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003427 char_u *e_cpt_copy; // copy of 'complete'
3428 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003429 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003430 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003431 pos_T prev_match_pos; // previous match position
3432 int set_match_pos; // save first_match_pos/last_match_pos
3433 pos_T first_match_pos; // first match position
3434 pos_T last_match_pos; // last match position
3435 int found_all; // found all matches of a certain type.
3436 char_u *dict; // dictionary file to search
3437 int dict_f; // "dict" is an exact file name or not
3438} ins_compl_next_state_T;
3439
3440/*
3441 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442 *
3443 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003444 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003445 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003446 * st->found_all - all matches of this type are found
3447 * st->ins_buf - search for completions in this buffer
3448 * st->first_match_pos - position of the first completion match
3449 * st->last_match_pos - position of the last completion match
3450 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003451 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003452 * st->dict - name of the dictionary or thesaurus file to search
3453 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003454 *
3455 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003456 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3457 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003458 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 */
3460 static int
3461process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003462 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003463 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003464 pos_T *start_match_pos,
3465 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003466{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003467 int compl_type = -1;
3468 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003469
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003470 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003472 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3473 st->e_cpt++;
3474
3475 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003476 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003477 st->ins_buf = curbuf;
3478 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003479 // Move the cursor back one character so that ^N can match the
3480 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003481 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003482 {
3483 // Move the cursor to after the last character in the
3484 // buffer, so that word at start of buffer is found
3485 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003486 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003487 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003488 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003489 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003490 compl_type = 0;
3491
3492 // Remember the first match so that the loop stops when we
3493 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003494 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003495 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003496 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003497 && (st->ins_buf = ins_compl_next_buf(
3498 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003499 {
3500 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003501 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003502 {
3503 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003504 st->first_match_pos.col = st->last_match_pos.col = 0;
3505 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3506 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003507 compl_type = 0;
3508 }
3509 else // unloaded buffer, scan like dictionary
3510 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003511 st->found_all = TRUE;
3512 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003513 {
3514 status = INS_COMPL_CPT_CONT;
3515 goto done;
3516 }
3517 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003518 st->dict = st->ins_buf->b_fname;
3519 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003520 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003521 if (!shortmess(SHM_COMPLETIONSCAN))
3522 {
3523 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3524 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3525 st->ins_buf->b_fname == NULL
3526 ? buf_spname(st->ins_buf)
3527 : st->ins_buf->b_sfname == NULL
3528 ? st->ins_buf->b_fname
3529 : st->ins_buf->b_sfname);
3530 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3531 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003532 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003533 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003534 status = INS_COMPL_CPT_END;
3535 else
3536 {
3537 if (ctrl_x_mode_line_or_eval())
3538 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003539 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003540 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003541 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003542 compl_type = CTRL_X_DICTIONARY;
3543 else
3544 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003545 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003546 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003547 st->dict = st->e_cpt;
3548 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003549 }
3550 }
3551#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003552 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003553 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003554 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003555 compl_type = CTRL_X_PATH_DEFINES;
3556#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003557 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003558 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003560 if (!shortmess(SHM_COMPLETIONSCAN))
3561 {
3562 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3563 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3564 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3565 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003566 }
3567 else
3568 compl_type = -1;
3569
3570 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003571 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003573 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003574 if (compl_type == -1)
3575 status = INS_COMPL_CPT_CONT;
3576 }
3577
3578done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003579 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003580 return status;
3581}
3582
3583#ifdef FEAT_FIND_ID
3584/*
3585 * Get the next set of identifiers or defines matching "compl_pattern" in
3586 * included files.
3587 */
3588 static void
3589get_next_include_file_completion(int compl_type)
3590{
John Marriott5e6ea922024-11-23 14:01:57 +01003591 find_pattern_in_path(compl_pattern.string, compl_direction,
3592 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 (compl_type == CTRL_X_PATH_DEFINES
3594 && !(compl_cont_status & CONT_SOL))
3595 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003596 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003597}
3598#endif
3599
3600/*
3601 * Get the next set of words matching "compl_pattern" in dictionary or
3602 * thesaurus files.
3603 */
3604 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606{
3607#ifdef FEAT_COMPL_FUNC
3608 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003609 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 else
3611#endif
3612 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003613 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003614 : (compl_type == CTRL_X_THESAURUS
3615 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3616 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003617 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003618 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003619 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003620}
3621
3622/*
3623 * Get the next set of tag names matching "compl_pattern".
3624 */
3625 static void
3626get_next_tag_completion(void)
3627{
3628 int save_p_ic;
3629 char_u **matches;
3630 int num_matches;
3631
3632 // set p_ic according to p_ic, p_scs and pat for find_tags().
3633 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003634 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003635
3636 // Find up to TAG_MANY matches. Avoids that an enormous number
3637 // of matches is found when compl_pattern is empty
3638 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003639 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003640 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003641 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003642 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3643 ins_compl_add_matches(num_matches, matches, p_ic);
3644 g_tag_at_cursor = FALSE;
3645 p_ic = save_p_ic;
3646}
3647
3648/*
glepnir8159fb12024-07-17 20:32:54 +02003649 * Compare function for qsort
3650 */
3651static int compare_scores(const void *a, const void *b)
3652{
3653 int idx_a = *(const int *)a;
3654 int idx_b = *(const int *)b;
3655 int score_a = compl_fuzzy_scores[idx_a];
3656 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003657 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3658 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003659}
3660
3661/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003662 * Get the next set of filename matching "compl_pattern".
3663 */
3664 static void
3665get_next_filename_completion(void)
3666{
3667 char_u **matches;
3668 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003669 char_u *ptr;
3670 garray_T fuzzy_indices;
3671 int i;
3672 int score;
3673 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003674 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003675 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3676 char_u **sorted_matches;
3677 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003678 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003679
glepnirb9de1a02024-08-02 19:14:38 +02003680#ifdef BACKSLASH_IN_FILENAME
3681 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3682 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3683#else
3684 char pathsep = PATHSEP;
3685#endif
3686
glepnir8159fb12024-07-17 20:32:54 +02003687 if (in_fuzzy)
3688 {
glepnirb9de1a02024-08-02 19:14:38 +02003689#ifdef BACKSLASH_IN_FILENAME
3690 if (curbuf->b_p_csl[0] == 's')
3691 {
John Marriott5e6ea922024-11-23 14:01:57 +01003692 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003693 {
3694 if (leader[i] == '\\')
3695 leader[i] = '/';
3696 }
3697 }
3698 else if (curbuf->b_p_csl[0] == 'b')
3699 {
John Marriott5e6ea922024-11-23 14:01:57 +01003700 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003701 {
3702 if (leader[i] == '/')
3703 leader[i] = '\\';
3704 }
3705 }
3706#endif
3707 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003708 if (last_sep == NULL)
3709 {
3710 // No path separator or separator is the last character,
3711 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003712 VIM_CLEAR_STRING(compl_pattern);
3713 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3714 if (compl_pattern.string == NULL)
3715 return;
3716 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003717 }
3718 else if (*(last_sep + 1) == '\0')
3719 in_fuzzy = FALSE;
3720 else
3721 {
3722 // Split leader into path and file parts
3723 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003724 char_u *path_with_wildcard;
3725
3726 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003727 if (path_with_wildcard != NULL)
3728 {
John Marriott5e6ea922024-11-23 14:01:57 +01003729 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3730 VIM_CLEAR_STRING(compl_pattern);
3731 compl_pattern.string = path_with_wildcard;
3732 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003733
3734 // Move leader to the file part
3735 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003736 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003737 }
3738 }
glepnir8159fb12024-07-17 20:32:54 +02003739 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003740
John Marriott5e6ea922024-11-23 14:01:57 +01003741 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003742 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3743 return;
3744
3745 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003746 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003747#ifdef BACKSLASH_IN_FILENAME
3748 if (curbuf->b_p_csl[0] != NUL)
3749 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750 for (i = 0; i < num_matches; ++i)
3751 {
glepnir8159fb12024-07-17 20:32:54 +02003752 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003753 while (*ptr != NUL)
3754 {
3755 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3756 *ptr = '/';
3757 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3758 *ptr = '\\';
3759 ptr += (*mb_ptr2len)(ptr);
3760 }
3761 }
3762 }
3763#endif
glepnir8159fb12024-07-17 20:32:54 +02003764
3765 if (in_fuzzy)
3766 {
3767 ga_init2(&fuzzy_indices, sizeof(int), 10);
3768 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3769
3770 for (i = 0; i < num_matches; i++)
3771 {
3772 ptr = matches[i];
3773 score = fuzzy_match_str(ptr, leader);
3774 if (score > 0)
3775 {
3776 if (ga_grow(&fuzzy_indices, 1) == OK)
3777 {
3778 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3779 compl_fuzzy_scores[i] = score;
3780 fuzzy_indices.ga_len++;
3781 }
3782 }
3783 }
3784
Christian Brabandt220474d2024-07-20 13:26:44 +02003785 // prevent qsort from deref NULL pointer
3786 if (fuzzy_indices.ga_len > 0)
3787 {
3788 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3789 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003790
Christian Brabandt220474d2024-07-20 13:26:44 +02003791 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3792 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3793 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003794
Christian Brabandt220474d2024-07-20 13:26:44 +02003795 FreeWild(num_matches, matches);
3796 matches = sorted_matches;
3797 num_matches = fuzzy_indices.ga_len;
3798 }
glepnir6b6280c2024-07-27 16:25:45 +02003799 else if (leader_len > 0)
3800 {
3801 FreeWild(num_matches, matches);
3802 num_matches = 0;
3803 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003804
glepnir8159fb12024-07-17 20:32:54 +02003805 vim_free(compl_fuzzy_scores);
3806 ga_clear(&fuzzy_indices);
3807 }
3808
glepnir6b6280c2024-07-27 16:25:45 +02003809 if (num_matches > 0)
3810 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003811}
3812
3813/*
3814 * Get the next set of command-line completions matching "compl_pattern".
3815 */
3816 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003817get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003818{
3819 char_u **matches;
3820 int num_matches;
3821
John Marriott5e6ea922024-11-23 14:01:57 +01003822 if (expand_cmdline(&compl_xp, compl_pattern.string,
3823 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003824 ins_compl_add_matches(num_matches, matches, FALSE);
3825}
3826
3827/*
3828 * Get the next set of spell suggestions matching "compl_pattern".
3829 */
3830 static void
3831get_next_spell_completion(linenr_T lnum UNUSED)
3832{
3833#ifdef FEAT_SPELL
3834 char_u **matches;
3835 int num_matches;
3836
John Marriott5e6ea922024-11-23 14:01:57 +01003837 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003838 if (num_matches > 0)
3839 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003840 else
3841 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003842#endif
3843}
3844
3845/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003846 * Return the next word or line from buffer "ins_buf" at position
3847 * "cur_match_pos" for completion. The length of the match is set in "len".
3848 */
3849 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003850ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003851 buf_T *ins_buf, // buffer being scanned
3852 pos_T *cur_match_pos, // current match position
3853 int *match_len,
3854 int *cont_s_ipos) // next ^X<> will set initial_pos
3855{
3856 char_u *ptr;
3857 int len;
3858
3859 *match_len = 0;
3860 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3861 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003862 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003863 if (ctrl_x_mode_line_or_eval())
3864 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003865 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003866 {
3867 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3868 return NULL;
3869 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003870 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003871 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003872 {
3873 char_u *tmp_ptr = ptr;
3874
3875 ptr = skipwhite(tmp_ptr);
3876 len -= (int)(ptr - tmp_ptr);
3877 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003878 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003879 }
3880 else
3881 {
3882 char_u *tmp_ptr = ptr;
3883
John Marriott5e6ea922024-11-23 14:01:57 +01003884 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003885 {
3886 tmp_ptr += compl_length;
3887 // Skip if already inside a word.
3888 if (vim_iswordp(tmp_ptr))
3889 return NULL;
3890 // Find start of next word.
3891 tmp_ptr = find_word_start(tmp_ptr);
3892 }
3893 // Find end of this word.
3894 tmp_ptr = find_word_end(tmp_ptr);
3895 len = (int)(tmp_ptr - ptr);
3896
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003897 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003898 {
3899 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3900 {
3901 // Try next line, if any. the new word will be
3902 // "join" as if the normal command "J" was used.
3903 // IOSIZE is always greater than
3904 // compl_length, so the next STRNCPY always
3905 // works -- Acevedo
3906 STRNCPY(IObuff, ptr, len);
3907 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3908 tmp_ptr = ptr = skipwhite(ptr);
3909 // Find start of next word.
3910 tmp_ptr = find_word_start(tmp_ptr);
3911 // Find end of next word.
3912 tmp_ptr = find_word_end(tmp_ptr);
3913 if (tmp_ptr > ptr)
3914 {
3915 if (*ptr != ')' && IObuff[len - 1] != TAB)
3916 {
3917 if (IObuff[len - 1] != ' ')
3918 IObuff[len++] = ' ';
3919 // IObuf =~ "\k.* ", thus len >= 2
3920 if (p_js
3921 && (IObuff[len - 2] == '.'
3922 || (vim_strchr(p_cpo, CPO_JOINSP)
3923 == NULL
3924 && (IObuff[len - 2] == '?'
3925 || IObuff[len - 2] == '!'))))
3926 IObuff[len++] = ' ';
3927 }
3928 // copy as much as possible of the new word
3929 if (tmp_ptr - ptr >= IOSIZE - len)
3930 tmp_ptr = ptr + IOSIZE - len - 1;
3931 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3932 len += (int)(tmp_ptr - ptr);
3933 *cont_s_ipos = TRUE;
3934 }
3935 IObuff[len] = NUL;
3936 ptr = IObuff;
3937 }
3938 if (len == compl_length)
3939 return NULL;
3940 }
3941 }
3942
3943 *match_len = len;
3944 return ptr;
3945}
3946
3947/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003948 * Get the next set of words matching "compl_pattern" for default completion(s)
3949 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003950 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3951 * position "st->start_pos" in the "compl_direction" direction. If
3952 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3953 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003954 * Returns OK if a new next match is found, otherwise returns FAIL.
3955 */
3956 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003957get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003958{
3959 int found_new_match = FAIL;
3960 int save_p_scs;
3961 int save_p_ws;
3962 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003963 char_u *ptr = NULL;
3964 int len = 0;
3965 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3966 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003967
3968 // If 'infercase' is set, don't use 'smartcase' here
3969 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003970 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003971 p_scs = FALSE;
3972
3973 // Buffers other than curbuf are scanned from the beginning or the
3974 // end but never from the middle, thus setting nowrapscan in this
3975 // buffer is a good idea, on the other hand, we always set
3976 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3977 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003978 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003979 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003980 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003981 p_ws = TRUE;
3982 looped_around = FALSE;
3983 for (;;)
3984 {
3985 int cont_s_ipos = FALSE;
3986
3987 ++msg_silent; // Don't want messages for wrapscan.
3988
3989 // ctrl_x_mode_line_or_eval() || word-wise search that
3990 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003991 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003992 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01003993 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02003994 else if (in_fuzzy)
3995 found_new_match = search_for_fuzzy_match(st->ins_buf,
3996 st->cur_match_pos, leader, compl_direction,
3997 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003998 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003999 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004000 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004001 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004002 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004003 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004004 {
4005 // set "compl_started" even on fail
4006 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004007 st->first_match_pos = *st->cur_match_pos;
4008 st->last_match_pos = *st->cur_match_pos;
4009 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004010 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004011 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4012 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004013 {
4014 found_new_match = FAIL;
4015 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004016 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004017 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4018 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4019 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004020 {
4021 if (looped_around)
4022 found_new_match = FAIL;
4023 else
4024 looped_around = TRUE;
4025 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004026 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004027 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4028 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4029 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004030 {
4031 if (looped_around)
4032 found_new_match = FAIL;
4033 else
4034 looped_around = TRUE;
4035 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004036 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004037 if (found_new_match == FAIL)
4038 break;
4039
4040 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004041 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004042 && start_pos->lnum == st->cur_match_pos->lnum
4043 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004044 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004045
glepnir8159fb12024-07-17 20:32:54 +02004046 if (!in_fuzzy)
4047 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4048 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004049 if (ptr == NULL)
4050 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004051
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004052 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004053 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004054 0, cont_s_ipos) != NOTDONE)
4055 {
4056 found_new_match = OK;
4057 break;
4058 }
4059 }
4060 p_scs = save_p_scs;
4061 p_ws = save_p_ws;
4062
4063 return found_new_match;
4064}
4065
4066/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004067 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004068 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069 */
4070 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004071get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004072{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004073 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004075 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004076 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077 case -1:
4078 break;
4079#ifdef FEAT_FIND_ID
4080 case CTRL_X_PATH_PATTERNS:
4081 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004082 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004083 break;
4084#endif
4085
4086 case CTRL_X_DICTIONARY:
4087 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004088 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4089 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004090 break;
4091
4092 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004093 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004094 break;
4095
4096 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004097 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004098 break;
4099
4100 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004101 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004102 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004103 break;
4104
4105#ifdef FEAT_COMPL_FUNC
4106 case CTRL_X_FUNCTION:
4107 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004108 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004109 break;
4110#endif
4111
4112 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004113 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004114 break;
4115
4116 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004117 found_new_match = get_next_default_completion(st, ini);
4118 if (found_new_match == FAIL && st->ins_buf == curbuf)
4119 st->found_all = TRUE;
4120 }
4121
4122 // check if compl_curr_match has changed, (e.g. other type of
4123 // expansion added something)
4124 if (type != 0 && compl_curr_match != compl_old_match)
4125 found_new_match = OK;
4126
4127 return found_new_match;
4128}
4129
4130/*
4131 * Get the next expansion(s), using "compl_pattern".
4132 * The search starts at position "ini" in curbuf and in the direction
4133 * compl_direction.
4134 * When "compl_started" is FALSE start at that position, otherwise continue
4135 * where we stopped searching before.
4136 * This may return before finding all the matches.
4137 * Return the total number of matches or -1 if still unknown -- Acevedo
4138 */
4139 static int
4140ins_compl_get_exp(pos_T *ini)
4141{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004142 static ins_compl_next_state_T st;
4143 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004144 int i;
4145 int found_new_match;
4146 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004147 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004148
4149 if (!compl_started)
4150 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004151 buf_T *buf;
4152
4153 FOR_ALL_BUFFERS(buf)
4154 buf->b_scanned = 0;
4155 if (!st_cleared)
4156 {
4157 CLEAR_FIELD(st);
4158 st_cleared = TRUE;
4159 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004160 st.found_all = FALSE;
4161 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004162 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004163 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004164 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4165 ? (char_u *)"." : curbuf->b_p_cpt);
4166 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004167 st.last_match_pos = st.first_match_pos = *ini;
4168 }
4169 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4170 st.ins_buf = curbuf; // In case the buffer was wiped out.
4171
4172 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004173 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004174 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004175
4176 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4177 for (;;)
4178 {
4179 found_new_match = FAIL;
4180 st.set_match_pos = FALSE;
4181
4182 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4183 // or if found_all says this entry is done. For ^X^L only use the
4184 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004185 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004186 && (!compl_started || st.found_all))
4187 {
glepnir8159fb12024-07-17 20:32:54 +02004188 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004189
4190 if (status == INS_COMPL_CPT_END)
4191 break;
4192 if (status == INS_COMPL_CPT_CONT)
4193 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004194 }
4195
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004196 // If complete() was called then compl_pattern has been reset. The
4197 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004198 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004199 break;
4200
4201 // get the next set of completion matches
4202 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004203
4204 // break the loop for specialized modes (use 'complete' just for the
4205 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4206 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004207 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4208 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209 {
4210 if (got_int)
4211 break;
4212 // Fill the popup menu as soon as possible.
4213 if (type != -1)
4214 ins_compl_check_keys(0, FALSE);
4215
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004216 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004217 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4218 break;
4219 compl_started = TRUE;
4220 }
4221 else
4222 {
4223 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004224 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004225 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004226
4227 compl_started = FALSE;
4228 }
4229 }
4230 compl_started = TRUE;
4231
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004232 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004233 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004234 found_new_match = FAIL;
4235
4236 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004237 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004238 && !ctrl_x_mode_line_or_eval()))
4239 i = ins_compl_make_cyclic();
4240
4241 if (compl_old_match != NULL)
4242 {
4243 // If several matches were added (FORWARD) or the search failed and has
4244 // just been made cyclic then we have to move compl_curr_match to the
4245 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004246 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004247 : compl_old_match->cp_prev;
4248 if (compl_curr_match == NULL)
4249 compl_curr_match = compl_old_match;
4250 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004251 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004252
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004253 return i;
4254}
4255
4256/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004257 * Update "compl_shown_match" to the actually shown match, it may differ when
4258 * "compl_leader" is used to omit some of the matches.
4259 */
4260 static void
4261ins_compl_update_shown_match(void)
4262{
4263 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004264 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004265 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004266 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004267 compl_shown_match = compl_shown_match->cp_next;
4268
4269 // If we didn't find it searching forward, and compl_shows_dir is
4270 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004271 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004272 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004273 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004274 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004275 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004276 {
4277 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004278 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004279 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004280 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004281 compl_shown_match = compl_shown_match->cp_prev;
4282 }
4283}
4284
4285/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004286 * Delete the old text being completed.
4287 */
4288 void
4289ins_compl_delete(void)
4290{
4291 int col;
4292
4293 // In insert mode: Delete the typed part.
4294 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004295 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004296 if ((int)curwin->w_cursor.col > col)
4297 {
4298 if (stop_arrow() == FAIL)
4299 return;
4300 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004301 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004302 }
4303
4304 // TODO: is this sufficient for redrawing? Redrawing everything causes
4305 // flicker, thus we can't do that.
4306 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004307#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004308 // clear v:completed_item
4309 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004310#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004311}
4312
4313/*
4314 * Insert the new text being completed.
4315 * "in_compl_func" is TRUE when called from complete_check().
4316 */
4317 void
4318ins_compl_insert(int in_compl_func)
4319{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004320 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004321
4322 // Make sure we don't go over the end of the string, this can happen with
4323 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004324 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004325 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004326 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004327 compl_used_match = FALSE;
4328 else
4329 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004330#ifdef FEAT_EVAL
4331 {
4332 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4333
4334 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4335 }
4336#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004337 if (!in_compl_func)
4338 compl_curr_match = compl_shown_match;
4339}
4340
4341/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004342 * show the file name for the completion match (if any). Truncate the file
4343 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004344 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004345 static void
4346ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004347{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004348 char *lead = _("match in file");
4349 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4350 char_u *s;
4351 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004352
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004353 if (space <= 0)
4354 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004355
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004356 // We need the tail that fits. With double-byte encoding going
4357 // back from the end is very slow, thus go from the start and keep
4358 // the text that fits in "space" between "s" and "e".
4359 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004360 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004361 space -= ptr2cells(e);
4362 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004363 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004364 space += ptr2cells(s);
4365 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004366 }
4367 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004368 msg_hist_off = TRUE;
4369 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4370 s > compl_shown_match->cp_fname ? "<" : "", s);
4371 msg((char *)IObuff);
4372 msg_hist_off = FALSE;
4373 redraw_cmdline = FALSE; // don't overwrite!
4374}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004375
glepnirdca57fb2024-06-04 22:01:21 +02004376/*
zeertzjq551d8c32024-06-05 19:53:32 +02004377 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004378 */
glepnira218cc62024-06-03 19:32:39 +02004379 static compl_T *
4380find_comp_when_fuzzy(void)
4381{
4382 int score;
4383 char_u* str;
4384 int target_idx = -1;
4385 int is_forward = compl_shows_dir_forward();
4386 int is_backward = compl_shows_dir_backward();
4387 compl_T *comp = NULL;
4388
glepnir43eef882024-06-19 20:20:48 +02004389 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004390 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004391 return compl_first_match != compl_shown_match ? compl_first_match :
4392 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004393
4394 if (is_forward)
4395 target_idx = compl_selected_item + 1;
4396 else if (is_backward)
4397 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004398 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004399
4400 score = compl_match_array[target_idx].pum_score;
4401 str = compl_match_array[target_idx].pum_text;
4402
4403 comp = compl_first_match;
4404 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004405 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004406 return comp;
4407 comp = comp->cp_next;
4408 } while (comp != NULL && !is_first_match(comp));
4409
4410 return NULL;
4411}
4412
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004413/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004414 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004415 * times. The number of matches found is returned in 'num_matches'.
4416 *
4417 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4418 * get more completions. If it is FALSE, then do nothing when there are no more
4419 * completions in the given direction.
4420 *
4421 * If "advance" is TRUE, then completion will move to the first match.
4422 * Otherwise, the original text will be shown.
4423 *
4424 * Returns OK on success and -1 if the number of matches are unknown.
4425 */
4426 static int
4427find_next_completion_match(
4428 int allow_get_expansion,
4429 int todo, // repeat completion this many times
4430 int advance,
4431 int *num_matches)
4432{
4433 int found_end = FALSE;
4434 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004435 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004436 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4437 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004438
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004439 while (--todo >= 0)
4440 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004441 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004442 {
glepniraced8c22024-06-15 15:13:05 +02004443 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4444 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004445 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004446 && (is_first_match(compl_shown_match->cp_next)
4447 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004448 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004449 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004450 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004451 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004452 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004453 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4454 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004455 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004456 }
4457 else
4458 {
4459 if (!allow_get_expansion)
4460 {
4461 if (advance)
4462 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004463 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004464 compl_pending -= todo + 1;
4465 else
4466 compl_pending += todo + 1;
4467 }
4468 return -1;
4469 }
4470
4471 if (!compl_no_select && advance)
4472 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004473 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004474 --compl_pending;
4475 else
4476 ++compl_pending;
4477 }
4478
4479 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004480 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004481
4482 // handle any pending completions
4483 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004484 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004485 {
4486 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4487 {
4488 compl_shown_match = compl_shown_match->cp_next;
4489 --compl_pending;
4490 }
4491 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4492 {
4493 compl_shown_match = compl_shown_match->cp_prev;
4494 ++compl_pending;
4495 }
4496 else
4497 break;
4498 }
4499 found_end = FALSE;
4500 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004501 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004502 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004503 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004504 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004505 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004506 ++todo;
4507 else
4508 // Remember a matching item.
4509 found_compl = compl_shown_match;
4510
4511 // Stop at the end of the list when we found a usable match.
4512 if (found_end)
4513 {
4514 if (found_compl != NULL)
4515 {
4516 compl_shown_match = found_compl;
4517 break;
4518 }
4519 todo = 1; // use first usable match after wrapping around
4520 }
4521 }
4522
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004523 return OK;
4524}
4525
4526/*
4527 * Fill in the next completion in the current direction.
4528 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4529 * get more completions. If it is FALSE, then we just do nothing when there
4530 * are no more completions in a given direction. The latter case is used when
4531 * we are still in the middle of finding completions, to allow browsing
4532 * through the ones found so far.
4533 * Return the total number of matches, or -1 if still unknown -- webb.
4534 *
4535 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4536 * compl_shown_match here.
4537 *
4538 * Note that this function may be called recursively once only. First with
4539 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4540 * calls this function with "allow_get_expansion" FALSE.
4541 */
4542 static int
4543ins_compl_next(
4544 int allow_get_expansion,
4545 int count, // repeat completion this many times; should
4546 // be at least 1
4547 int insert_match, // Insert the newly selected match
4548 int in_compl_func) // called from complete_check()
4549{
4550 int num_matches = -1;
4551 int todo = count;
4552 int advance;
4553 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004554 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004555 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004556 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4557 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004558
4559 // When user complete function return -1 for findstart which is next
4560 // time of 'always', compl_shown_match become NULL.
4561 if (compl_shown_match == NULL)
4562 return -1;
4563
John Marriott5e6ea922024-11-23 14:01:57 +01004564 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004565 && !match_at_original_text(compl_shown_match)
4566 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004567 // Update "compl_shown_match" to the actually shown match
4568 ins_compl_update_shown_match();
4569
4570 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004571 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004572 // Delete old text to be replaced
4573 ins_compl_delete();
4574
4575 // When finding the longest common text we stick at the original text,
4576 // don't let CTRL-N or CTRL-P move to the first match.
4577 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4578
4579 // When restarting the search don't insert the first match either.
4580 if (compl_restarting)
4581 {
4582 advance = FALSE;
4583 compl_restarting = FALSE;
4584 }
4585
4586 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4587 // around.
4588 if (find_next_completion_match(allow_get_expansion, todo, advance,
4589 &num_matches) == -1)
4590 return -1;
4591
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004592 if (curbuf != orig_curbuf)
4593 {
4594 // In case some completion function switched buffer, don't want to
4595 // insert the completion elsewhere.
4596 return -1;
4597 }
4598
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004599 // Insert the text of the new completion, or the compl_leader.
4600 if (compl_no_insert && !started)
4601 {
glepnir6a38aff2024-12-16 21:56:16 +01004602 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004603 compl_used_match = FALSE;
4604 }
4605 else if (insert_match)
4606 {
4607 if (!compl_get_longest || compl_used_match)
4608 ins_compl_insert(in_compl_func);
4609 else
glepnir6a38aff2024-12-16 21:56:16 +01004610 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004611 }
4612 else
4613 compl_used_match = FALSE;
4614
4615 if (!allow_get_expansion)
4616 {
4617 // may undisplay the popup menu first
4618 ins_compl_upd_pum();
4619
4620 if (pum_enough_matches())
4621 // Will display the popup menu, don't redraw yet to avoid flicker.
4622 pum_call_update_screen();
4623 else
4624 // Not showing the popup menu yet, redraw to show the user what was
4625 // inserted.
4626 update_screen(0);
4627
4628 // display the updated popup menu
4629 ins_compl_show_pum();
4630#ifdef FEAT_GUI
4631 if (gui.in_use)
4632 {
4633 // Show the cursor after the match, not after the redrawn text.
4634 setcursor();
4635 out_flush_cursor(FALSE, FALSE);
4636 }
4637#endif
4638
4639 // Delete old text to be replaced, since we're still searching and
4640 // don't want to match ourselves!
4641 ins_compl_delete();
4642 }
4643
4644 // Enter will select a match when the match wasn't inserted and the popup
4645 // menu is visible.
4646 if (compl_no_insert && !started)
4647 compl_enter_selects = TRUE;
4648 else
4649 compl_enter_selects = !insert_match && compl_match_array != NULL;
4650
4651 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004652 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004653 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004654
4655 return num_matches;
4656}
4657
4658/*
4659 * Call this while finding completions, to check whether the user has hit a key
4660 * that should change the currently displayed completion, or exit completion
4661 * mode. Also, when compl_pending is not zero, show a completion as soon as
4662 * possible. -- webb
4663 * "frequency" specifies out of how many calls we actually check.
4664 * "in_compl_func" is TRUE when called from complete_check(), don't set
4665 * compl_curr_match.
4666 */
4667 void
4668ins_compl_check_keys(int frequency, int in_compl_func)
4669{
4670 static int count = 0;
4671 int c;
4672
4673 // Don't check when reading keys from a script, :normal or feedkeys().
4674 // That would break the test scripts. But do check for keys when called
4675 // from complete_check().
4676 if (!in_compl_func && (using_script() || ex_normal_busy))
4677 return;
4678
4679 // Only do this at regular intervals
4680 if (++count < frequency)
4681 return;
4682 count = 0;
4683
4684 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4685 // can't do its work correctly.
4686 c = vpeekc_any();
4687 if (c != NUL)
4688 {
4689 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4690 {
4691 c = safe_vgetc(); // Eat the character
4692 compl_shows_dir = ins_compl_key2dir(c);
4693 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4694 c != K_UP && c != K_DOWN, in_compl_func);
4695 }
4696 else
4697 {
4698 // Need to get the character to have KeyTyped set. We'll put it
4699 // back with vungetc() below. But skip K_IGNORE.
4700 c = safe_vgetc();
4701 if (c != K_IGNORE)
4702 {
4703 // Don't interrupt completion when the character wasn't typed,
4704 // e.g., when doing @q to replay keys.
4705 if (c != Ctrl_R && KeyTyped)
4706 compl_interrupted = TRUE;
4707
4708 vungetc(c);
4709 }
4710 }
4711 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004712 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004713 {
4714 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4715
4716 compl_pending = 0;
4717 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4718 }
4719}
4720
4721/*
4722 * Decide the direction of Insert mode complete from the key typed.
4723 * Returns BACKWARD or FORWARD.
4724 */
4725 static int
4726ins_compl_key2dir(int c)
4727{
4728 if (c == Ctrl_P || c == Ctrl_L
4729 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4730 return BACKWARD;
4731 return FORWARD;
4732}
4733
4734/*
4735 * Return TRUE for keys that are used for completion only when the popup menu
4736 * is visible.
4737 */
4738 static int
4739ins_compl_pum_key(int c)
4740{
4741 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4742 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4743 || c == K_UP || c == K_DOWN);
4744}
4745
4746/*
4747 * Decide the number of completions to move forward.
4748 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4749 */
4750 static int
4751ins_compl_key2count(int c)
4752{
4753 int h;
4754
4755 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4756 {
4757 h = pum_get_height();
4758 if (h > 3)
4759 h -= 2; // keep some context
4760 return h;
4761 }
4762 return 1;
4763}
4764
4765/*
4766 * Return TRUE if completion with "c" should insert the match, FALSE if only
4767 * to change the currently selected completion.
4768 */
4769 static int
4770ins_compl_use_match(int c)
4771{
4772 switch (c)
4773 {
4774 case K_UP:
4775 case K_DOWN:
4776 case K_PAGEDOWN:
4777 case K_KPAGEDOWN:
4778 case K_S_DOWN:
4779 case K_PAGEUP:
4780 case K_KPAGEUP:
4781 case K_S_UP:
4782 return FALSE;
4783 }
4784 return TRUE;
4785}
4786
4787/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004788 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4789 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004790 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004791 * Uses the global variables: compl_cont_status and ctrl_x_mode
4792 */
4793 static int
4794get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4795{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004796 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004797 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004798 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004799 {
4800 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4801 ;
4802 compl_col += ++startcol;
4803 compl_length = curs_col - startcol;
4804 }
4805 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004806 {
John Marriott5e6ea922024-11-23 14:01:57 +01004807 compl_pattern.string = str_foldcase(line + compl_col,
4808 compl_length, NULL, 0);
4809 if (compl_pattern.string == NULL)
4810 {
4811 compl_pattern.length = 0;
4812 return FAIL;
4813 }
4814 compl_pattern.length = STRLEN(compl_pattern.string);
4815 }
4816 else
4817 {
4818 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4819 if (compl_pattern.string == NULL)
4820 {
4821 compl_pattern.length = 0;
4822 return FAIL;
4823 }
4824 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004825 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004826 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004827 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004828 {
4829 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004830 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004831 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004832
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004833 if (!vim_iswordp(line + compl_col)
4834 || (compl_col > 0
4835 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004836 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004837 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004838 prefixlen = 0;
4839 }
John Marriott5e6ea922024-11-23 14:01:57 +01004840
4841 // we need up to 2 extra chars for the prefix
4842 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4843 compl_pattern.string = alloc(n);
4844 if (compl_pattern.string == NULL)
4845 {
4846 compl_pattern.length = 0;
4847 return FAIL;
4848 }
4849 STRCPY((char *)compl_pattern.string, prefix);
4850 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004851 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004852 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004853 }
4854 else if (--startcol < 0
4855 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4856 {
John Marriott5e6ea922024-11-23 14:01:57 +01004857 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4858
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004859 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004860 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4861 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004862 {
John Marriott5e6ea922024-11-23 14:01:57 +01004863 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004864 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004865 }
John Marriott5e6ea922024-11-23 14:01:57 +01004866 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004867 compl_col += curs_col;
4868 compl_length = 0;
4869 }
4870 else
4871 {
4872 // Search the point of change class of multibyte character
4873 // or not a word single byte character backward.
4874 if (has_mbyte)
4875 {
4876 int base_class;
4877 int head_off;
4878
4879 startcol -= (*mb_head_off)(line, line + startcol);
4880 base_class = mb_get_class(line + startcol);
4881 while (--startcol >= 0)
4882 {
4883 head_off = (*mb_head_off)(line, line + startcol);
4884 if (base_class != mb_get_class(line + startcol
4885 - head_off))
4886 break;
4887 startcol -= head_off;
4888 }
4889 }
4890 else
4891 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4892 ;
4893 compl_col += ++startcol;
4894 compl_length = (int)curs_col - startcol;
4895 if (compl_length == 1)
4896 {
4897 // Only match word with at least two chars -- webb
4898 // there's no need to call quote_meta,
4899 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004900 compl_pattern.string = alloc(7);
4901 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004902 {
John Marriott5e6ea922024-11-23 14:01:57 +01004903 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004904 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004905 }
John Marriott5e6ea922024-11-23 14:01:57 +01004906 STRCPY((char *)compl_pattern.string, "\\<");
4907 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4908 STRCAT((char *)compl_pattern.string, "\\k");
4909 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004910 }
4911 else
4912 {
John Marriott5e6ea922024-11-23 14:01:57 +01004913 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4914
4915 compl_pattern.string = alloc(n);
4916 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004917 {
John Marriott5e6ea922024-11-23 14:01:57 +01004918 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004919 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004920 }
John Marriott5e6ea922024-11-23 14:01:57 +01004921 STRCPY((char *)compl_pattern.string, "\\<");
4922 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004923 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004924 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004925 }
4926 }
4927
4928 return OK;
4929}
4930
4931/*
4932 * Get the pattern, column and length for whole line completion or for the
4933 * complete() function.
4934 * Sets the global variables: compl_col, compl_length and compl_pattern.
4935 */
4936 static int
4937get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4938{
4939 compl_col = (colnr_T)getwhitecols(line);
4940 compl_length = (int)curs_col - (int)compl_col;
4941 if (compl_length < 0) // cursor in indent: empty pattern
4942 compl_length = 0;
4943 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004944 {
John Marriott5e6ea922024-11-23 14:01:57 +01004945 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4946 NULL, 0);
4947 if (compl_pattern.string == NULL)
4948 {
4949 compl_pattern.length = 0;
4950 return FAIL;
4951 }
4952 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004953 }
John Marriott5e6ea922024-11-23 14:01:57 +01004954 else
4955 {
4956 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4957 if (compl_pattern.string == NULL)
4958 {
4959 compl_pattern.length = 0;
4960 return FAIL;
4961 }
4962 compl_pattern.length = (size_t)compl_length;
4963 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004964
4965 return OK;
4966}
4967
4968/*
4969 * Get the pattern, column and length for filename completion.
4970 * Sets the global variables: compl_col, compl_length and compl_pattern.
4971 */
4972 static int
4973get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4974{
4975 // Go back to just before the first filename character.
4976 if (startcol > 0)
4977 {
4978 char_u *p = line + startcol;
4979
4980 MB_PTR_BACK(line, p);
4981 while (p > line && vim_isfilec(PTR2CHAR(p)))
4982 MB_PTR_BACK(line, p);
4983 if (p == line && vim_isfilec(PTR2CHAR(p)))
4984 startcol = 0;
4985 else
4986 startcol = (int)(p - line) + 1;
4987 }
4988
4989 compl_col += startcol;
4990 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01004991 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
4992 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004993 {
John Marriott5e6ea922024-11-23 14:01:57 +01004994 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004995 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004996 }
4997
John Marriott5e6ea922024-11-23 14:01:57 +01004998 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004999
5000 return OK;
5001}
5002
5003/*
5004 * Get the pattern, column and length for command-line completion.
5005 * Sets the global variables: compl_col, compl_length and compl_pattern.
5006 */
5007 static int
5008get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5009{
John Marriott5e6ea922024-11-23 14:01:57 +01005010 compl_pattern.string = vim_strnsave(line, curs_col);
5011 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005012 {
John Marriott5e6ea922024-11-23 14:01:57 +01005013 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005014 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005015 }
John Marriott5e6ea922024-11-23 14:01:57 +01005016 compl_pattern.length = curs_col;
5017 set_cmd_context(&compl_xp, compl_pattern.string,
5018 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005019 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5020 || compl_xp.xp_context == EXPAND_NOTHING)
5021 // No completion possible, use an empty pattern to get a
5022 // "pattern not found" message.
5023 compl_col = curs_col;
5024 else
John Marriott5e6ea922024-11-23 14:01:57 +01005025 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005026 compl_length = curs_col - compl_col;
5027
5028 return OK;
5029}
5030
5031/*
5032 * Get the pattern, column and length for user defined completion ('omnifunc',
5033 * 'completefunc' and 'thesaurusfunc')
5034 * Sets the global variables: compl_col, compl_length and compl_pattern.
5035 * Uses the global variable: spell_bad_len
5036 */
5037 static int
5038get_userdefined_compl_info(colnr_T curs_col UNUSED)
5039{
5040 int ret = FAIL;
5041
5042#ifdef FEAT_COMPL_FUNC
5043 // Call user defined function 'completefunc' with "a:findstart"
5044 // set to 1 to obtain the length of text to use for completion.
5045 char_u *line;
5046 typval_T args[3];
5047 int col;
5048 char_u *funcname;
5049 pos_T pos;
5050 int save_State = State;
5051 callback_T *cb;
5052
5053 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5054 // length as a string
5055 funcname = get_complete_funcname(ctrl_x_mode);
5056 if (*funcname == NUL)
5057 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005058 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005059 ? "completefunc" : "omnifunc");
5060 return FAIL;
5061 }
5062
5063 args[0].v_type = VAR_NUMBER;
5064 args[0].vval.v_number = 1;
5065 args[1].v_type = VAR_STRING;
5066 args[1].vval.v_string = (char_u *)"";
5067 args[2].v_type = VAR_UNKNOWN;
5068 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005069 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005070 cb = get_insert_callback(ctrl_x_mode);
5071 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005072 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005073
5074 State = save_State;
5075 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005076 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005077 validate_cursor();
5078 if (!EQUAL_POS(curwin->w_cursor, pos))
5079 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005080 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005081 return FAIL;
5082 }
5083
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005084 // Return value -2 means the user complete function wants to cancel the
5085 // complete without an error, do the same if the function did not execute
5086 // successfully.
5087 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005088 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005089 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005090 if (col == -3)
5091 {
5092 ctrl_x_mode = CTRL_X_NORMAL;
5093 edit_submode = NULL;
5094 if (!shortmess(SHM_COMPLETIONMENU))
5095 msg_clr_cmdline();
5096 return FAIL;
5097 }
5098
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005099 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005100 // completion.
5101 compl_opt_refresh_always = FALSE;
5102 compl_opt_suppress_empty = FALSE;
5103
5104 if (col < 0)
5105 col = curs_col;
5106 compl_col = col;
5107 if (compl_col > curs_col)
5108 compl_col = curs_col;
5109
5110 // Setup variables for completion. Need to obtain "line" again,
5111 // it may have become invalid.
5112 line = ml_get(curwin->w_cursor.lnum);
5113 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005114 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5115 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005116 {
John Marriott5e6ea922024-11-23 14:01:57 +01005117 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005118 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005119 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005120
John Marriott5e6ea922024-11-23 14:01:57 +01005121 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005122 ret = OK;
5123#endif
5124
5125 return ret;
5126}
5127
5128/*
5129 * Get the pattern, column and length for spell completion.
5130 * Sets the global variables: compl_col, compl_length and compl_pattern.
5131 * Uses the global variable: spell_bad_len
5132 */
5133 static int
5134get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5135{
5136 int ret = FAIL;
5137#ifdef FEAT_SPELL
5138 char_u *line;
5139
5140 if (spell_bad_len > 0)
5141 compl_col = curs_col - spell_bad_len;
5142 else
5143 compl_col = spell_word_start(startcol);
5144 if (compl_col >= (colnr_T)startcol)
5145 {
5146 compl_length = 0;
5147 compl_col = curs_col;
5148 }
5149 else
5150 {
5151 spell_expand_check_cap(compl_col);
5152 compl_length = (int)curs_col - compl_col;
5153 }
5154 // Need to obtain "line" again, it may have become invalid.
5155 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005156 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5157 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005158 {
John Marriott5e6ea922024-11-23 14:01:57 +01005159 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005160 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005161 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005162
John Marriott5e6ea922024-11-23 14:01:57 +01005163 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005164 ret = OK;
5165#endif
5166
5167 return ret;
5168}
5169
5170/*
5171 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005172 * "startcol" - start column number of the completion pattern/text
5173 * "cur_col" - current cursor column
5174 * On return, "line_invalid" is set to TRUE, if the current line may have
5175 * become invalid and needs to be fetched again.
5176 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005177 */
5178 static int
5179compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5180{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005181 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005182 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5183 && !thesaurus_func_complete(ctrl_x_mode)))
5184 {
5185 return get_normal_compl_info(line, startcol, curs_col);
5186 }
5187 else if (ctrl_x_mode_line_or_eval())
5188 {
5189 return get_wholeline_compl_info(line, curs_col);
5190 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005191 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005192 {
5193 return get_filename_compl_info(line, startcol, curs_col);
5194 }
5195 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5196 {
5197 return get_cmdline_compl_info(line, curs_col);
5198 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005199 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005200 || thesaurus_func_complete(ctrl_x_mode))
5201 {
5202 if (get_userdefined_compl_info(curs_col) == FAIL)
5203 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005204 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005205 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005206 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005207 {
5208 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5209 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005210 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005211 }
5212 else
5213 {
5214 internal_error("ins_complete()");
5215 return FAIL;
5216 }
5217
5218 return OK;
5219}
5220
5221/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005222 * Continue an interrupted completion mode search in "line".
5223 *
5224 * If this same ctrl_x_mode has been interrupted use the text from
5225 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5226 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5227 * the same line as the cursor then fix it (the line has been split because it
5228 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5229 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005230 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005231 static void
5232ins_compl_continue_search(char_u *line)
5233{
5234 // it is a continued search
5235 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005236 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5237 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005238 {
5239 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5240 {
5241 // line (probably) wrapped, set compl_startpos to the
5242 // first non_blank in the line, if it is not a wordchar
5243 // include it to get a better pattern, but then we don't
5244 // want the "\\<" prefix, check it below
5245 compl_col = (colnr_T)getwhitecols(line);
5246 compl_startpos.col = compl_col;
5247 compl_startpos.lnum = curwin->w_cursor.lnum;
5248 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5249 }
5250 else
5251 {
5252 // S_IPOS was set when we inserted a word that was at the
5253 // beginning of the line, which means that we'll go to SOL
5254 // mode but first we need to redefine compl_startpos
5255 if (compl_cont_status & CONT_S_IPOS)
5256 {
5257 compl_cont_status |= CONT_SOL;
5258 compl_startpos.col = (colnr_T)(skipwhite(
5259 line + compl_length
5260 + compl_startpos.col) - line);
5261 }
5262 compl_col = compl_startpos.col;
5263 }
5264 compl_length = curwin->w_cursor.col - (int)compl_col;
5265 // IObuff is used to add a "word from the next line" would we
5266 // have enough space? just being paranoid
5267#define MIN_SPACE 75
5268 if (compl_length > (IOSIZE - MIN_SPACE))
5269 {
5270 compl_cont_status &= ~CONT_SOL;
5271 compl_length = (IOSIZE - MIN_SPACE);
5272 compl_col = curwin->w_cursor.col - compl_length;
5273 }
5274 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5275 if (compl_length < 1)
5276 compl_cont_status &= CONT_LOCAL;
5277 }
5278 else if (ctrl_x_mode_line_or_eval())
5279 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5280 else
5281 compl_cont_status = 0;
5282}
5283
5284/*
5285 * start insert mode completion
5286 */
5287 static int
5288ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005289{
5290 char_u *line;
5291 int startcol = 0; // column where searched text starts
5292 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005293 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005294 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005295 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005296
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005297 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005298
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005299 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005300 did_si = FALSE;
5301 can_si = FALSE;
5302 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005303 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005304 return FAIL;
5305
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005306 line = ml_get(curwin->w_cursor.lnum);
5307 curs_col = curwin->w_cursor.col;
5308 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005309
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005310 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5311 && compl_cont_mode == ctrl_x_mode)
5312 // this same ctrl-x_mode was interrupted previously. Continue the
5313 // completion.
5314 ins_compl_continue_search(line);
5315 else
5316 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005317
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005318 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005319 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005320 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005321 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005322 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5323 compl_cont_status = 0;
5324 compl_cont_status |= CONT_N_ADDS;
5325 compl_startpos = curwin->w_cursor;
5326 startcol = (int)curs_col;
5327 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005328 }
5329
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005330 // Work out completion pattern and original text -- webb
5331 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5332 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005333 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5334 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005335 // restore did_ai, so that adding comment leader works
5336 did_ai = save_did_ai;
5337 return FAIL;
5338 }
5339 // If "line" was changed while getting completion info get it again.
5340 if (line_invalid)
5341 line = ml_get(curwin->w_cursor.lnum);
5342
glepnir7cfe6932024-09-15 20:06:28 +02005343 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005344 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005345 {
5346 edit_submode_pre = (char_u *)_(" Adding");
5347 if (ctrl_x_mode_line_or_eval())
5348 {
5349 // Insert a new line, keep indentation but ignore 'comments'.
5350 char_u *old = curbuf->b_p_com;
5351
5352 curbuf->b_p_com = (char_u *)"";
5353 compl_startpos.lnum = curwin->w_cursor.lnum;
5354 compl_startpos.col = compl_col;
5355 ins_eol('\r');
5356 curbuf->b_p_com = old;
5357 compl_length = 0;
5358 compl_col = curwin->w_cursor.col;
5359 }
glepnir7cfe6932024-09-15 20:06:28 +02005360 else if (ctrl_x_mode_normal() && in_fuzzy)
5361 {
5362 compl_startpos = curwin->w_cursor;
5363 compl_cont_status &= CONT_S_IPOS;
5364 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005365 }
5366 else
5367 {
5368 edit_submode_pre = NULL;
5369 compl_startpos.col = compl_col;
5370 }
5371
5372 if (compl_cont_status & CONT_LOCAL)
5373 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5374 else
5375 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5376
5377 // If any of the original typed text has been changed we need to fix
5378 // the redo buffer.
5379 ins_compl_fixRedoBufForLeader(NULL);
5380
5381 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005382 VIM_CLEAR_STRING(compl_orig_text);
5383 compl_orig_text.length = (size_t)compl_length;
5384 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005385 if (p_ic)
5386 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005387 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5388 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005389 {
John Marriott5e6ea922024-11-23 14:01:57 +01005390 VIM_CLEAR_STRING(compl_pattern);
5391 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005392 return FAIL;
5393 }
5394
5395 // showmode might reset the internal line pointers, so it must
5396 // be called before line = ml_get(), or when this address is no
5397 // longer needed. -- Acevedo.
5398 edit_submode_extra = (char_u *)_("-- Searching...");
5399 edit_submode_highl = HLF_COUNT;
5400 showmode();
5401 edit_submode_extra = NULL;
5402 out_flush();
5403
5404 return OK;
5405}
5406
5407/*
5408 * display the completion status message
5409 */
5410 static void
5411ins_compl_show_statusmsg(void)
5412{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005413 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005414 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005415 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005416 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005417 ? (char_u *)_("Hit end of paragraph")
5418 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005419 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005420 }
5421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005422 if (edit_submode_extra == NULL)
5423 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005424 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005425 {
5426 edit_submode_extra = (char_u *)_("Back at original");
5427 edit_submode_highl = HLF_W;
5428 }
5429 else if (compl_cont_status & CONT_S_IPOS)
5430 {
5431 edit_submode_extra = (char_u *)_("Word from other line");
5432 edit_submode_highl = HLF_COUNT;
5433 }
5434 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5435 {
5436 edit_submode_extra = (char_u *)_("The only match");
5437 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005438 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005439 }
5440 else
5441 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005442#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005443 // Update completion sequence number when needed.
5444 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005445 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005446#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005447 // The match should always have a sequence number now, this is
5448 // just a safety check.
5449 if (compl_curr_match->cp_number != -1)
5450 {
5451 // Space for 10 text chars. + 2x10-digit no.s = 31.
5452 // Translations may need more than twice that.
5453 static char_u match_ref[81];
5454
5455 if (compl_matches > 0)
5456 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005457 _("match %d of %d"),
5458 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005459 else
5460 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005461 _("match %d"),
5462 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005463 edit_submode_extra = match_ref;
5464 edit_submode_highl = HLF_R;
5465 if (dollar_vcol >= 0)
5466 curs_columns(FALSE);
5467 }
5468 }
5469 }
5470
5471 // Show a message about what (completion) mode we're in.
5472 if (!compl_opt_suppress_empty)
5473 {
5474 showmode();
5475 if (!shortmess(SHM_COMPLETIONMENU))
5476 {
5477 if (edit_submode_extra != NULL)
5478 {
5479 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005480 {
5481 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005482 msg_attr((char *)edit_submode_extra,
5483 edit_submode_highl < HLF_COUNT
5484 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005485 msg_hist_off = FALSE;
5486 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005487 }
5488 else
5489 msg_clr_cmdline(); // necessary for "noshowmode"
5490 }
5491 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005492}
5493
5494/*
5495 * Do Insert mode completion.
5496 * Called when character "c" was typed, which has a meaning for completion.
5497 * Returns OK if completion was done, FAIL if something failed (out of mem).
5498 */
5499 int
5500ins_complete(int c, int enable_pum)
5501{
5502 int n;
5503 int save_w_wrow;
5504 int save_w_leftcol;
5505 int insert_match;
5506
5507 compl_direction = ins_compl_key2dir(c);
5508 insert_match = ins_compl_use_match(c);
5509
5510 if (!compl_started)
5511 {
5512 if (ins_compl_start() == FAIL)
5513 return FAIL;
5514 }
5515 else if (insert_match && stop_arrow() == FAIL)
5516 return FAIL;
5517
5518 compl_shown_match = compl_curr_match;
5519 compl_shows_dir = compl_direction;
5520
5521 // Find next match (and following matches).
5522 save_w_wrow = curwin->w_wrow;
5523 save_w_leftcol = curwin->w_leftcol;
5524 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5525
5526 // may undisplay the popup menu
5527 ins_compl_upd_pum();
5528
5529 if (n > 1) // all matches have been found
5530 compl_matches = n;
5531 compl_curr_match = compl_shown_match;
5532 compl_direction = compl_shows_dir;
5533
5534 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5535 // mode.
5536 if (got_int && !global_busy)
5537 {
5538 (void)vgetc();
5539 got_int = FALSE;
5540 }
5541
5542 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005543 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005544 {
5545 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5546 // because we couldn't expand anything at first place, but if we used
5547 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5548 // (such as M in M'exico) if not tried already. -- Acevedo
5549 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005550 || compl_status_adding()
5551 || (ctrl_x_mode_not_default()
5552 && !ctrl_x_mode_path_patterns()
5553 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005554 compl_cont_status &= ~CONT_N_ADDS;
5555 }
5556
5557 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5558 compl_cont_status |= CONT_S_IPOS;
5559 else
5560 compl_cont_status &= ~CONT_S_IPOS;
5561
5562 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005563
5564 // Show the popup menu, unless we got interrupted.
5565 if (enable_pum && !compl_interrupted)
5566 show_pum(save_w_wrow, save_w_leftcol);
5567
5568 compl_was_interrupted = compl_interrupted;
5569 compl_interrupted = FALSE;
5570
5571 return OK;
5572}
5573
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005574/*
5575 * Remove (if needed) and show the popup menu
5576 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005577 static void
5578show_pum(int prev_w_wrow, int prev_w_leftcol)
5579{
5580 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005581 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005582 RedrawingDisabled = 0;
5583
5584 // If the cursor moved or the display scrolled we need to remove the pum
5585 // first.
5586 setcursor();
5587 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5588 ins_compl_del_pum();
5589
5590 ins_compl_show_pum();
5591 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005592
5593 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005594}
5595
5596/*
5597 * Looks in the first "len" chars. of "src" for search-metachars.
5598 * If dest is not NULL the chars. are copied there quoting (with
5599 * a backslash) the metachars, and dest would be NUL terminated.
5600 * Returns the length (needed) of dest
5601 */
5602 static unsigned
5603quote_meta(char_u *dest, char_u *src, int len)
5604{
5605 unsigned m = (unsigned)len + 1; // one extra for the NUL
5606
5607 for ( ; --len >= 0; src++)
5608 {
5609 switch (*src)
5610 {
5611 case '.':
5612 case '*':
5613 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005614 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005615 break;
5616 // FALLTHROUGH
5617 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005618 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005619 break;
5620 // FALLTHROUGH
5621 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005622 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005623 break;
5624 // FALLTHROUGH
5625 case '^': // currently it's not needed.
5626 case '$':
5627 m++;
5628 if (dest != NULL)
5629 *dest++ = '\\';
5630 break;
5631 }
5632 if (dest != NULL)
5633 *dest++ = *src;
5634 // Copy remaining bytes of a multibyte character.
5635 if (has_mbyte)
5636 {
5637 int i, mb_len;
5638
5639 mb_len = (*mb_ptr2len)(src) - 1;
5640 if (mb_len > 0 && len >= mb_len)
5641 for (i = 0; i < mb_len; ++i)
5642 {
5643 --len;
5644 ++src;
5645 if (dest != NULL)
5646 *dest++ = *src;
5647 }
5648 }
5649 }
5650 if (dest != NULL)
5651 *dest = NUL;
5652
5653 return m;
5654}
5655
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005656#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005657 void
5658free_insexpand_stuff(void)
5659{
John Marriott5e6ea922024-11-23 14:01:57 +01005660 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005661# ifdef FEAT_EVAL
5662 free_callback(&cfu_cb);
5663 free_callback(&ofu_cb);
5664 free_callback(&tsrfu_cb);
5665# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005666}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005667#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005668
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005669#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005670/*
5671 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5672 * spelled word, if there is one.
5673 */
5674 static void
5675spell_back_to_badword(void)
5676{
5677 pos_T tpos = curwin->w_cursor;
5678
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005679 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005680 if (curwin->w_cursor.col != tpos.col)
5681 start_arrow(&tpos);
5682}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005683#endif