blob: bf8f215e9398046660222f49d6db0cc217583f46 [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
glepnir9fddb8a2025-01-11 16:42:50 +0100930 if (col >= (compl_col + (int)ins_compl_leader_len()) && 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;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001267 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1268 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnir80b66202024-11-27 21:53:53 +01001269 compl_T *match_head = NULL;
1270 compl_T *match_tail = NULL;
1271 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001272
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001273 // Need to build the popup menu list.
1274 compl_match_arraysize = 0;
1275 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001276
glepnira49c0772024-11-30 10:56:30 +01001277 // If the current match is the original text don't find the first
1278 // match after it, don't highlight anything.
1279 if (match_at_original_text(compl_shown_match))
1280 shown_match_ok = TRUE;
1281
1282 if (compl_leader.string != NULL
1283 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1284 && shown_match_ok == FALSE)
1285 compl_shown_match = compl_no_select ? compl_first_match
1286 : compl_first_match->cp_next;
1287
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 do
1289 {
glepnird4088ed2024-12-31 10:55:22 +01001290 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001291 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1292 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001293 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001294 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001295
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001296 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001297 && (compl_leader.string == NULL
1298 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001299 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001300 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001301 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001302 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001303 if (match_head == NULL)
1304 match_head = compl;
1305 else
glepnira49c0772024-11-30 10:56:30 +01001306 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001307 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001308
glepnirf400a0c2025-01-23 19:55:14 +01001309 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001310 {
1311 if (compl == compl_shown_match || did_find_shown_match)
1312 {
1313 // This item is the shown match or this is the
1314 // first displayed item after the shown match.
1315 compl_shown_match = compl;
1316 did_find_shown_match = TRUE;
1317 shown_match_ok = TRUE;
1318 }
1319 else
1320 // Remember this displayed match for when the
1321 // shown match is just below it.
1322 shown_compl = compl;
1323 cur = i;
1324 }
glepnirf400a0c2025-01-23 19:55:14 +01001325 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001326 {
1327 if (i == 0)
1328 shown_compl = compl;
1329 // Update the maximum fuzzy score and the shown match
1330 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001331 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001332 {
1333 did_find_shown_match = TRUE;
1334 max_fuzzy_score = compl->cp_score;
1335 if (!compl_no_select)
1336 compl_shown_match = compl;
1337 }
zeertzjqd65aa1b2025-01-25 15:29:03 +01001338 else if (!fuzzy_sort && i == 0 && !compl_no_select)
glepnirf400a0c2025-01-23 19:55:14 +01001339 compl_shown_match = shown_compl;
glepnira49c0772024-11-30 10:56:30 +01001340
1341 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1342 {
1343 cur = i;
1344 shown_match_ok = TRUE;
1345 }
1346 }
1347 i++;
glepnir80b66202024-11-27 21:53:53 +01001348 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001349
glepnirf400a0c2025-01-23 19:55:14 +01001350 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001351 {
1352 did_find_shown_match = TRUE;
1353
1354 // When the original text is the shown match don't set
1355 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001356 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001357 shown_match_ok = TRUE;
1358
1359 if (!shown_match_ok && shown_compl != NULL)
1360 {
1361 // The shown match isn't displayed, set it to the
1362 // previously displayed match.
1363 compl_shown_match = shown_compl;
1364 shown_match_ok = TRUE;
1365 }
1366 }
glepnira49c0772024-11-30 10:56:30 +01001367 compl = compl->cp_next;
1368 } while (compl != NULL && !is_first_match(compl));
1369
1370 if (compl_match_arraysize == 0)
1371 return -1;
1372
1373 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1374 if (compl_match_array == NULL)
1375 return -1;
1376
1377 compl = match_head;
1378 i = 0;
1379 while (compl != NULL)
1380 {
glepnir6e199932024-12-14 21:13:27 +01001381 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1382 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001383 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1384 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1385 compl_match_array[i].pum_score = compl->cp_score;
1386 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1387 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001388 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1389 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001390 match_next = compl->cp_match_next;
1391 compl->cp_match_next = NULL;
1392 compl = match_next;
1393 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001394
zeertzjqd65aa1b2025-01-25 15:29:03 +01001395 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001396 {
1397 for (i = 0; i < compl_match_arraysize; i++)
1398 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001399 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001400 qsort(compl_match_array, (size_t)compl_match_arraysize,
1401 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001402 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001403 }
glepnira218cc62024-06-03 19:32:39 +02001404
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001405 if (!shown_match_ok) // no displayed match at all
1406 cur = -1;
1407
1408 return cur;
1409}
1410
1411/*
1412 * Show the popup menu for the list of matches.
1413 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1414 */
1415 void
1416ins_compl_show_pum(void)
1417{
1418 int i;
1419 int cur = -1;
1420 colnr_T col;
1421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 if (!pum_wanted() || !pum_enough_matches())
1423 return;
1424
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001425 // Update the screen later, before drawing the popup menu over it.
1426 pum_call_update_screen();
1427
1428 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001429 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001430 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001431 else
1432 {
1433 // popup menu already exists, only need to find the current item.
1434 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001435 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001436 || compl_match_array[i].pum_text
1437 == compl_shown_match->cp_text[CPT_ABBR])
1438 {
1439 cur = i;
1440 break;
1441 }
1442 }
1443
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001445 {
1446#ifdef FEAT_EVAL
1447 if (compl_started && has_completechanged())
1448 trigger_complete_changed_event(cur);
1449#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001450 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001451 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001452
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001453 // In Replace mode when a $ is displayed at the end of the line only
1454 // part of the screen would be updated. We do need to redraw here.
1455 dollar_vcol = -1;
1456
1457 // Compute the screen column of the start of the completed text.
1458 // Use the cursor to get all wrapping and other settings right.
1459 col = curwin->w_cursor.col;
1460 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001461 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001462 pum_display(compl_match_array, compl_match_arraysize, cur);
1463 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001464
glepnircbb46b42024-02-03 18:11:13 +01001465 // After adding leader, set the current match to shown match.
1466 if (compl_started && compl_curr_match != compl_shown_match)
1467 compl_curr_match = compl_shown_match;
1468
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001469#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001470 if (has_completechanged())
1471 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001472#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001473}
1474
1475#define DICT_FIRST (1) // use just first element in "dict"
1476#define DICT_EXACT (2) // "dict" is the exact name of a file
1477
1478/*
glepnir40c1c332024-06-11 19:37:04 +02001479 * Get current completion leader
1480 */
1481 char_u *
1482ins_compl_leader(void)
1483{
John Marriott5e6ea922024-11-23 14:01:57 +01001484 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1485}
1486
1487/*
1488 * Get current completion leader length
1489 */
1490 size_t
1491ins_compl_leader_len(void)
1492{
1493 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001494}
1495
1496/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001497 * Add any identifiers that match the given pattern "pat" in the list of
1498 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001499 */
1500 static void
1501ins_compl_dictionaries(
1502 char_u *dict_start,
1503 char_u *pat,
1504 int flags, // DICT_FIRST and/or DICT_EXACT
1505 int thesaurus) // Thesaurus completion
1506{
1507 char_u *dict = dict_start;
1508 char_u *ptr;
1509 char_u *buf;
1510 regmatch_T regmatch;
1511 char_u **files;
1512 int count;
1513 int save_p_scs;
1514 int dir = compl_direction;
1515
1516 if (*dict == NUL)
1517 {
1518#ifdef FEAT_SPELL
1519 // When 'dictionary' is empty and spell checking is enabled use
1520 // "spell".
1521 if (!thesaurus && curwin->w_p_spell)
1522 dict = (char_u *)"spell";
1523 else
1524#endif
1525 return;
1526 }
1527
1528 buf = alloc(LSIZE);
1529 if (buf == NULL)
1530 return;
1531 regmatch.regprog = NULL; // so that we can goto theend
1532
1533 // If 'infercase' is set, don't use 'smartcase' here
1534 save_p_scs = p_scs;
1535 if (curbuf->b_p_inf)
1536 p_scs = FALSE;
1537
1538 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1539 // to only match at the start of a line. Otherwise just match the
1540 // pattern. Also need to double backslashes.
1541 if (ctrl_x_mode_line_or_eval())
1542 {
1543 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1544 size_t len;
1545
1546 if (pat_esc == NULL)
1547 goto theend;
1548 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001549 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001550 if (ptr == NULL)
1551 {
1552 vim_free(pat_esc);
1553 goto theend;
1554 }
1555 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1556 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1557 vim_free(pat_esc);
1558 vim_free(ptr);
1559 }
1560 else
1561 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001562 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001563 if (regmatch.regprog == NULL)
1564 goto theend;
1565 }
1566
1567 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1568 regmatch.rm_ic = ignorecase(pat);
1569 while (*dict != NUL && !got_int && !compl_interrupted)
1570 {
1571 // copy one dictionary file name into buf
1572 if (flags == DICT_EXACT)
1573 {
1574 count = 1;
1575 files = &dict;
1576 }
1577 else
1578 {
1579 // Expand wildcards in the dictionary name, but do not allow
1580 // backticks (for security, the 'dict' option may have been set in
1581 // a modeline).
1582 copy_option_part(&dict, buf, LSIZE, ",");
1583# ifdef FEAT_SPELL
1584 if (!thesaurus && STRCMP(buf, "spell") == 0)
1585 count = -1;
1586 else
1587# endif
1588 if (vim_strchr(buf, '`') != NULL
1589 || expand_wildcards(1, &buf, &count, &files,
1590 EW_FILE|EW_SILENT) != OK)
1591 count = 0;
1592 }
1593
1594# ifdef FEAT_SPELL
1595 if (count == -1)
1596 {
1597 // Complete from active spelling. Skip "\<" in the pattern, we
1598 // don't use it as a RE.
1599 if (pat[0] == '\\' && pat[1] == '<')
1600 ptr = pat + 2;
1601 else
1602 ptr = pat;
1603 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1604 }
1605 else
1606# endif
1607 if (count > 0) // avoid warning for using "files" uninit
1608 {
1609 ins_compl_files(count, files, thesaurus, flags,
1610 &regmatch, buf, &dir);
1611 if (flags != DICT_EXACT)
1612 FreeWild(count, files);
1613 }
1614 if (flags != 0)
1615 break;
1616 }
1617
1618theend:
1619 p_scs = save_p_scs;
1620 vim_regfree(regmatch.regprog);
1621 vim_free(buf);
1622}
1623
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001624/*
1625 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1626 * skipping the word at 'skip_word'. Returns OK on success.
1627 */
1628 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001629thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001630 char_u *fname,
1631 char_u **buf_arg,
1632 int dir,
1633 char_u *skip_word)
1634{
1635 int status = OK;
1636 char_u *ptr;
1637 char_u *wstart;
1638
1639 // Add the other matches on the line
1640 ptr = *buf_arg;
1641 while (!got_int)
1642 {
1643 // Find start of the next word. Skip white
1644 // space and punctuation.
1645 ptr = find_word_start(ptr);
1646 if (*ptr == NUL || *ptr == NL)
1647 break;
1648 wstart = ptr;
1649
1650 // Find end of the word.
1651 if (has_mbyte)
1652 // Japanese words may have characters in
1653 // different classes, only separate words
1654 // with single-byte non-word characters.
1655 while (*ptr != NUL)
1656 {
1657 int l = (*mb_ptr2len)(ptr);
1658
1659 if (l < 2 && !vim_iswordc(*ptr))
1660 break;
1661 ptr += l;
1662 }
1663 else
1664 ptr = find_word_end(ptr);
1665
1666 // Add the word. Skip the regexp match.
1667 if (wstart != skip_word)
1668 {
1669 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1670 fname, dir, FALSE);
1671 if (status == FAIL)
1672 break;
1673 }
1674 }
1675
1676 *buf_arg = ptr;
1677 return status;
1678}
1679
1680/*
1681 * Process "count" dictionary/thesaurus "files" and add the text matching
1682 * "regmatch".
1683 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684 static void
1685ins_compl_files(
1686 int count,
1687 char_u **files,
1688 int thesaurus,
1689 int flags,
1690 regmatch_T *regmatch,
1691 char_u *buf,
1692 int *dir)
1693{
1694 char_u *ptr;
1695 int i;
1696 FILE *fp;
1697 int add_r;
1698
1699 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1700 {
1701 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001702 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001703 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001704 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001705 vim_snprintf((char *)IObuff, IOSIZE,
1706 _("Scanning dictionary: %s"), (char *)files[i]);
1707 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1708 }
1709
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001710 if (fp == NULL)
1711 continue;
1712
1713 // Read dictionary file line by line.
1714 // Check each line for a match.
1715 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001716 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 ptr = buf;
1718 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001719 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001721 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1722 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001723 add_r = ins_compl_add_infercase(regmatch->startp[0],
1724 (int)(ptr - regmatch->startp[0]),
1725 p_ic, files[i], *dir, FALSE);
1726 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001727 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001728 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001729 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001730 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001731 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001732 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001733 if (add_r == OK)
1734 // if dir was BACKWARD then honor it just once
1735 *dir = FORWARD;
1736 else if (add_r == FAIL)
1737 break;
1738 // avoid expensive call to vim_regexec() when at end
1739 // of line
1740 if (*ptr == '\n' || got_int)
1741 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001742 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001743 line_breakcheck();
1744 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001745 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001746 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001747 }
1748}
1749
1750/*
1751 * Find the start of the next word.
1752 * Returns a pointer to the first char of the word. Also stops at a NUL.
1753 */
1754 char_u *
1755find_word_start(char_u *ptr)
1756{
1757 if (has_mbyte)
1758 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1759 ptr += (*mb_ptr2len)(ptr);
1760 else
1761 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1762 ++ptr;
1763 return ptr;
1764}
1765
1766/*
1767 * Find the end of the word. Assumes it starts inside a word.
1768 * Returns a pointer to just after the word.
1769 */
1770 char_u *
1771find_word_end(char_u *ptr)
1772{
1773 int start_class;
1774
1775 if (has_mbyte)
1776 {
1777 start_class = mb_get_class(ptr);
1778 if (start_class > 1)
1779 while (*ptr != NUL)
1780 {
1781 ptr += (*mb_ptr2len)(ptr);
1782 if (mb_get_class(ptr) != start_class)
1783 break;
1784 }
1785 }
1786 else
1787 while (vim_iswordc(*ptr))
1788 ++ptr;
1789 return ptr;
1790}
1791
1792/*
1793 * Find the end of the line, omitting CR and NL at the end.
1794 * Returns a pointer to just after the line.
1795 */
1796 static char_u *
1797find_line_end(char_u *ptr)
1798{
1799 char_u *s;
1800
1801 s = ptr + STRLEN(ptr);
1802 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1803 --s;
1804 return s;
1805}
1806
1807/*
1808 * Free the list of completions
1809 */
1810 static void
1811ins_compl_free(void)
1812{
1813 compl_T *match;
1814 int i;
1815
John Marriott5e6ea922024-11-23 14:01:57 +01001816 VIM_CLEAR_STRING(compl_pattern);
1817 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001818
1819 if (compl_first_match == NULL)
1820 return;
1821
1822 ins_compl_del_pum();
1823 pum_clear();
1824
1825 compl_curr_match = compl_first_match;
1826 do
1827 {
1828 match = compl_curr_match;
1829 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001830 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001832 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001833 vim_free(match->cp_fname);
1834 for (i = 0; i < CPT_COUNT; ++i)
1835 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001836#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001837 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001838#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001839 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001840 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001841 compl_first_match = compl_curr_match = NULL;
1842 compl_shown_match = NULL;
1843 compl_old_match = NULL;
1844}
1845
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001846/*
1847 * Reset/clear the completion state.
1848 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001849 void
1850ins_compl_clear(void)
1851{
1852 compl_cont_status = 0;
1853 compl_started = FALSE;
1854 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001855 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001856 VIM_CLEAR_STRING(compl_pattern);
1857 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001858 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001859 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001860 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001861#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001862 // clear v:completed_item
1863 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001864#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001865}
1866
1867/*
1868 * Return TRUE when Insert completion is active.
1869 */
1870 int
1871ins_compl_active(void)
1872{
1873 return compl_started;
1874}
1875
1876/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001877 * Return True when wp is the actual completion window
1878 */
1879 int
1880ins_compl_win_active(win_T *wp UNUSED)
1881{
1882 return ins_compl_active()
1883#if defined(FEAT_QUICKFIX)
1884 && (!wp->w_p_pvw
1885# ifdef FEAT_PROP_POPUP
1886 && !(wp->w_popup_flags & POPF_INFO)
1887# endif
1888 )
1889#endif
1890 ;
1891}
1892
1893/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001894 * Selected one of the matches. When FALSE the match was edited or using the
1895 * longest common string.
1896 */
1897 int
1898ins_compl_used_match(void)
1899{
1900 return compl_used_match;
1901}
1902
1903/*
1904 * Initialize get longest common string.
1905 */
1906 void
1907ins_compl_init_get_longest(void)
1908{
1909 compl_get_longest = FALSE;
1910}
1911
1912/*
1913 * Returns TRUE when insert completion is interrupted.
1914 */
1915 int
1916ins_compl_interrupted(void)
1917{
1918 return compl_interrupted;
1919}
1920
1921/*
1922 * Returns TRUE if the <Enter> key selects a match in the completion popup
1923 * menu.
1924 */
1925 int
1926ins_compl_enter_selects(void)
1927{
1928 return compl_enter_selects;
1929}
1930
1931/*
1932 * Return the column where the text starts that is being completed
1933 */
1934 colnr_T
1935ins_compl_col(void)
1936{
1937 return compl_col;
1938}
1939
1940/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001941 * Return the length in bytes of the text being completed
1942 */
1943 int
1944ins_compl_len(void)
1945{
1946 return compl_length;
1947}
1948
1949/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001950 * Delete one character before the cursor and show the subset of the matches
1951 * that match the word that is now before the cursor.
1952 * Returns the character to be used, NUL if the work is done and another char
1953 * to be got from the user.
1954 */
1955 int
1956ins_compl_bs(void)
1957{
1958 char_u *line;
1959 char_u *p;
1960
1961 line = ml_get_curline();
1962 p = line + curwin->w_cursor.col;
1963 MB_PTR_BACK(line, p);
1964
1965 // Stop completion when the whole word was deleted. For Omni completion
1966 // allow the word to be deleted, we won't match everything.
1967 // Respect the 'backspace' option.
1968 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001969 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1970 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001971 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1972 - compl_length < 0))
1973 return K_BS;
1974
1975 // Deleted more than what was used to find matches or didn't finish
1976 // finding all matches: need to look for matches all over again.
1977 if (curwin->w_cursor.col <= compl_col + compl_length
1978 || ins_compl_need_restart())
1979 ins_compl_restart();
1980
John Marriott5e6ea922024-11-23 14:01:57 +01001981 VIM_CLEAR_STRING(compl_leader);
1982 compl_leader.length = (size_t)((p - line) - compl_col);
1983 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1984 if (compl_leader.string == NULL)
1985 {
1986 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001987 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001988 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001989
1990 ins_compl_new_leader();
1991 if (compl_shown_match != NULL)
1992 // Make sure current match is not a hidden item.
1993 compl_curr_match = compl_shown_match;
1994 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001995}
1996
1997/*
1998 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1999 * be called.
2000 */
2001 static int
2002ins_compl_need_restart(void)
2003{
2004 // Return TRUE if we didn't complete finding matches or when the
2005 // 'completefunc' returned "always" in the "refresh" dictionary item.
2006 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002007 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002008 && compl_opt_refresh_always);
2009}
2010
2011/*
2012 * Called after changing "compl_leader".
2013 * Show the popup menu with a different set of matches.
2014 * May also search for matches again if the previous search was interrupted.
2015 */
2016 static void
2017ins_compl_new_leader(void)
2018{
2019 ins_compl_del_pum();
2020 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002021 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002022 compl_used_match = FALSE;
2023
2024 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002025 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002026 else
2027 {
2028#ifdef FEAT_SPELL
2029 spell_bad_len = 0; // need to redetect bad word
2030#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002031 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002032 // the popup menu display the changed text before the cursor. Set
2033 // "compl_restarting" to avoid that the first match is inserted.
2034 pum_call_update_screen();
2035#ifdef FEAT_GUI
2036 if (gui.in_use)
2037 {
2038 // Show the cursor after the match, not after the redrawn text.
2039 setcursor();
2040 out_flush_cursor(FALSE, FALSE);
2041 }
2042#endif
2043 compl_restarting = TRUE;
2044 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2045 compl_cont_status = 0;
2046 compl_restarting = FALSE;
2047 }
2048
2049 compl_enter_selects = !compl_used_match;
2050
2051 // Show the popup menu with a different set of matches.
2052 ins_compl_show_pum();
2053
2054 // Don't let Enter select the original text when there is no popup menu.
2055 if (compl_match_array == NULL)
2056 compl_enter_selects = FALSE;
2057}
2058
2059/*
2060 * Return the length of the completion, from the completion start column to
2061 * the cursor column. Making sure it never goes below zero.
2062 */
2063 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002064get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002065{
2066 int off = (int)curwin->w_cursor.col - (int)compl_col;
2067
2068 if (off < 0)
2069 return 0;
2070 return off;
2071}
2072
2073/*
2074 * Append one character to the match leader. May reduce the number of
2075 * matches.
2076 */
2077 void
2078ins_compl_addleader(int c)
2079{
2080 int cc;
2081
2082 if (stop_arrow() == FAIL)
2083 return;
2084 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2085 {
2086 char_u buf[MB_MAXBYTES + 1];
2087
2088 (*mb_char2bytes)(c, buf);
2089 buf[cc] = NUL;
2090 ins_char_bytes(buf, cc);
2091 if (compl_opt_refresh_always)
2092 AppendToRedobuff(buf);
2093 }
2094 else
2095 {
2096 ins_char(c);
2097 if (compl_opt_refresh_always)
2098 AppendCharToRedobuff(c);
2099 }
2100
2101 // If we didn't complete finding matches we must search again.
2102 if (ins_compl_need_restart())
2103 ins_compl_restart();
2104
2105 // When 'always' is set, don't reset compl_leader. While completing,
2106 // cursor doesn't point original position, changing compl_leader would
2107 // break redo.
2108 if (!compl_opt_refresh_always)
2109 {
John Marriott5e6ea922024-11-23 14:01:57 +01002110 VIM_CLEAR_STRING(compl_leader);
2111 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2112 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2113 compl_leader.length);
2114 if (compl_leader.string == NULL)
2115 {
2116 compl_leader.length = 0;
2117 return;
2118 }
2119
2120 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002121 }
2122}
2123
2124/*
2125 * Setup for finding completions again without leaving CTRL-X mode. Used when
2126 * BS or a key was typed while still searching for matches.
2127 */
2128 static void
2129ins_compl_restart(void)
2130{
2131 ins_compl_free();
2132 compl_started = FALSE;
2133 compl_matches = 0;
2134 compl_cont_status = 0;
2135 compl_cont_mode = 0;
2136}
2137
2138/*
2139 * Set the first match, the original text.
2140 */
2141 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002142ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002143{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002144 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002145 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2146 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002147 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002148 {
John Marriott5e6ea922024-11-23 14:01:57 +01002149 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002150 if (p != NULL)
2151 {
John Marriott5e6ea922024-11-23 14:01:57 +01002152 VIM_CLEAR_STRING(compl_first_match->cp_str);
2153 compl_first_match->cp_str.string = p;
2154 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002155 }
2156 }
2157 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002158 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002159 {
John Marriott5e6ea922024-11-23 14:01:57 +01002160 char_u *p = vim_strnsave(str, len);
2161 if (p != NULL)
2162 {
2163 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2164 compl_first_match->cp_prev->cp_str.string = p;
2165 compl_first_match->cp_prev->cp_str.length = len;
2166 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002167 }
2168}
2169
2170/*
2171 * Append one character to the match leader. May reduce the number of
2172 * matches.
2173 */
2174 void
2175ins_compl_addfrommatch(void)
2176{
2177 char_u *p;
2178 int len = (int)curwin->w_cursor.col - (int)compl_col;
2179 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002180
John Marriott5e6ea922024-11-23 14:01:57 +01002181 p = compl_shown_match->cp_str.string;
2182 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002183 {
John Marriott5e6ea922024-11-23 14:01:57 +01002184 size_t plen;
2185 compl_T *cp;
2186
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002187 // When still at the original match use the first entry that matches
2188 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002189 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002190 return;
2191
2192 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002193 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002194 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002195 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002196 {
John Marriott5e6ea922024-11-23 14:01:57 +01002197 if (compl_leader.string == NULL
2198 || ins_compl_equal(cp, compl_leader.string,
2199 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002200 {
John Marriott5e6ea922024-11-23 14:01:57 +01002201 p = cp->cp_str.string;
2202 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002203 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002204 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002205 }
John Marriott5e6ea922024-11-23 14:01:57 +01002206 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002207 return;
2208 }
2209 p += len;
2210 c = PTR2CHAR(p);
2211 ins_compl_addleader(c);
2212}
2213
2214/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002215 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002216 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2217 * compl_cont_mode and compl_cont_status.
2218 * Returns TRUE when the character is not to be inserted.
2219 */
2220 static int
2221set_ctrl_x_mode(int c)
2222{
2223 int retval = FALSE;
2224
2225 switch (c)
2226 {
2227 case Ctrl_E:
2228 case Ctrl_Y:
2229 // scroll the window one line up or down
2230 ctrl_x_mode = CTRL_X_SCROLL;
2231 if (!(State & REPLACE_FLAG))
2232 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2233 else
2234 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2235 edit_submode_pre = NULL;
2236 showmode();
2237 break;
2238 case Ctrl_L:
2239 // complete whole line
2240 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2241 break;
2242 case Ctrl_F:
2243 // complete filenames
2244 ctrl_x_mode = CTRL_X_FILES;
2245 break;
2246 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002247 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002248 ctrl_x_mode = CTRL_X_DICTIONARY;
2249 break;
2250 case Ctrl_R:
2251 // Register insertion without exiting CTRL-X mode
2252 // Simply allow ^R to happen without affecting ^X mode
2253 break;
2254 case Ctrl_T:
2255 // complete words from a thesaurus
2256 ctrl_x_mode = CTRL_X_THESAURUS;
2257 break;
2258#ifdef FEAT_COMPL_FUNC
2259 case Ctrl_U:
2260 // user defined completion
2261 ctrl_x_mode = CTRL_X_FUNCTION;
2262 break;
2263 case Ctrl_O:
2264 // omni completion
2265 ctrl_x_mode = CTRL_X_OMNI;
2266 break;
2267#endif
2268 case 's':
2269 case Ctrl_S:
2270 // complete spelling suggestions
2271 ctrl_x_mode = CTRL_X_SPELL;
2272#ifdef FEAT_SPELL
2273 ++emsg_off; // Avoid getting the E756 error twice.
2274 spell_back_to_badword();
2275 --emsg_off;
2276#endif
2277 break;
2278 case Ctrl_RSB:
2279 // complete tag names
2280 ctrl_x_mode = CTRL_X_TAGS;
2281 break;
2282#ifdef FEAT_FIND_ID
2283 case Ctrl_I:
2284 case K_S_TAB:
2285 // complete keywords from included files
2286 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2287 break;
2288 case Ctrl_D:
2289 // complete definitions from included files
2290 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2291 break;
2292#endif
2293 case Ctrl_V:
2294 case Ctrl_Q:
2295 // complete vim commands
2296 ctrl_x_mode = CTRL_X_CMDLINE;
2297 break;
2298 case Ctrl_Z:
2299 // stop completion
2300 ctrl_x_mode = CTRL_X_NORMAL;
2301 edit_submode = NULL;
2302 showmode();
2303 retval = TRUE;
2304 break;
2305 case Ctrl_P:
2306 case Ctrl_N:
2307 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2308 // just started ^X mode, or there were enough ^X's to cancel
2309 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2310 // do normal expansion when interrupting a different mode (say
2311 // ^X^F^X^P or ^P^X^X^P, see below)
2312 // nothing changes if interrupting mode 0, (eg, the flag
2313 // doesn't change when going to ADDING mode -- Acevedo
2314 if (!(compl_cont_status & CONT_INTRPT))
2315 compl_cont_status |= CONT_LOCAL;
2316 else if (compl_cont_mode != 0)
2317 compl_cont_status &= ~CONT_LOCAL;
2318 // FALLTHROUGH
2319 default:
2320 // If we have typed at least 2 ^X's... for modes != 0, we set
2321 // compl_cont_status = 0 (eg, as if we had just started ^X
2322 // mode).
2323 // For mode 0, we set "compl_cont_mode" to an impossible
2324 // value, in both cases ^X^X can be used to restart the same
2325 // mode (avoiding ADDING mode).
2326 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2327 // 'complete' and local ^P expansions respectively.
2328 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2329 // mode -- Acevedo
2330 if (c == Ctrl_X)
2331 {
2332 if (compl_cont_mode != 0)
2333 compl_cont_status = 0;
2334 else
2335 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2336 }
2337 ctrl_x_mode = CTRL_X_NORMAL;
2338 edit_submode = NULL;
2339 showmode();
2340 break;
2341 }
2342
2343 return retval;
2344}
2345
2346/*
glepnir1c5a1202024-12-04 20:27:34 +01002347 * Trigger CompleteDone event and adds relevant information to v:event
2348 */
2349 static void
2350trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2351{
2352#if defined(FEAT_EVAL)
2353 save_v_event_T save_v_event;
2354 dict_T *v_event = get_v_event(&save_v_event);
2355 char_u *mode_str = NULL;
2356
2357 mode = mode & ~CTRL_X_WANT_IDENT;
2358 if (ctrl_x_mode_names[mode])
2359 mode_str = (char_u *)ctrl_x_mode_names[mode];
2360
2361 (void)dict_add_string(v_event, "complete_word",
2362 word == NULL ? (char_u *)"" : word);
2363 (void)dict_add_string(v_event, "complete_type",
2364 mode_str != NULL ? mode_str : (char_u *)"");
2365
2366 dict_set_items_ro(v_event);
2367#endif
2368 ins_apply_autocmds(EVENT_COMPLETEDONE);
2369
2370#if defined(FEAT_EVAL)
2371 restore_v_event(v_event, &save_v_event);
2372#endif
2373}
2374
2375/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002376 * Stop insert completion mode
2377 */
2378 static int
2379ins_compl_stop(int c, int prev_mode, int retval)
2380{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002381 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002382 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002383
2384 // Get here when we have finished typing a sequence of ^N and
2385 // ^P or other completion characters in CTRL-X mode. Free up
2386 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002387 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002388 {
John Marriott5e6ea922024-11-23 14:01:57 +01002389 char_u *ptr;
2390
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002391 // If any of the original typed text has been changed, eg when
2392 // ignorecase is set, we must add back-spaces to the redo
2393 // buffer. We add as few as necessary to delete just the part
2394 // of the original text that has changed.
2395 // When using the longest match, edited the match or used
2396 // CTRL-E then don't use the current match.
2397 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002398 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002399 else
2400 ptr = NULL;
2401 ins_compl_fixRedoBufForLeader(ptr);
2402 }
2403
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002404 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002405
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002406 // When completing whole lines: fix indent for 'cindent'.
2407 // Otherwise, break line if it's too long.
2408 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2409 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002410 // re-indent the current line
2411 if (want_cindent)
2412 {
2413 do_c_expr_indent();
2414 want_cindent = FALSE; // don't do it again
2415 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002416 }
2417 else
2418 {
2419 int prev_col = curwin->w_cursor.col;
2420
2421 // put the cursor on the last char, for 'tw' formatting
2422 if (prev_col > 0)
2423 dec_cursor();
2424 // only format when something was inserted
2425 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2426 insertchar(NUL, 0, -1);
2427 if (prev_col > 0
2428 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2429 inc_cursor();
2430 }
2431
2432 // If the popup menu is displayed pressing CTRL-Y means accepting
2433 // the selection without inserting anything. When
2434 // compl_enter_selects is set the Enter key does the same.
2435 if ((c == Ctrl_Y || (compl_enter_selects
2436 && (c == CAR || c == K_KENTER || c == NL)))
2437 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002438 {
2439 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002440 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002441 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002442
2443 // CTRL-E means completion is Ended, go back to the typed text.
2444 // but only do this, if the Popup is still visible
2445 if (c == Ctrl_E)
2446 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002447 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002448 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002449
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002450 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002451 if (compl_leader.string != NULL)
2452 {
2453 p = compl_leader.string;
2454 plen = compl_leader.length;
2455 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002456 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002457 {
2458 p = compl_orig_text.string;
2459 plen = compl_orig_text.length;
2460 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002461 if (p != NULL)
2462 {
2463 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002464
John Marriott5e6ea922024-11-23 14:01:57 +01002465 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002466 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002467 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002468 retval = TRUE;
2469 }
2470
2471 auto_format(FALSE, TRUE);
2472
2473 // Trigger the CompleteDonePre event to give scripts a chance to
2474 // act upon the completion before clearing the info, and restore
2475 // ctrl_x_mode, so that complete_info() can be used.
2476 ctrl_x_mode = prev_mode;
2477 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2478
2479 ins_compl_free();
2480 compl_started = FALSE;
2481 compl_matches = 0;
2482 if (!shortmess(SHM_COMPLETIONMENU))
2483 msg_clr_cmdline(); // necessary for "noshowmode"
2484 ctrl_x_mode = CTRL_X_NORMAL;
2485 compl_enter_selects = FALSE;
2486 if (edit_submode != NULL)
2487 {
2488 edit_submode = NULL;
2489 showmode();
2490 }
2491
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002492 if (c == Ctrl_C && cmdwin_type != 0)
2493 // Avoid the popup menu remains displayed when leaving the
2494 // command line window.
2495 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002496 // Indent now if a key was typed that is in 'cinkeys'.
2497 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2498 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002499 // Trigger the CompleteDone event to give scripts a chance to act
2500 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002501 trigger_complete_done_event(prev_mode, word);
2502 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002503
2504 return retval;
2505}
2506
2507/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002508 * Prepare for Insert mode completion, or stop it.
2509 * Called just after typing a character in Insert mode.
2510 * Returns TRUE when the character is not to be inserted;
2511 */
2512 int
2513ins_compl_prep(int c)
2514{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002515 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002516 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002517
2518 // Forget any previous 'special' messages if this is actually
2519 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2520 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2521 edit_submode_extra = NULL;
2522
zeertzjq440d4cb2023-03-02 17:51:32 +00002523 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002524 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002525 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002526 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002527 return retval;
2528
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002529#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002530 // Ignore mouse events in a popup window
2531 if (is_mouse_key(c))
2532 {
2533 // Ignore drag and release events, the position does not need to be in
2534 // the popup and it may have just closed.
2535 if (c == K_LEFTRELEASE
2536 || c == K_LEFTRELEASE_NM
2537 || c == K_MIDDLERELEASE
2538 || c == K_RIGHTRELEASE
2539 || c == K_X1RELEASE
2540 || c == K_X2RELEASE
2541 || c == K_LEFTDRAG
2542 || c == K_MIDDLEDRAG
2543 || c == K_RIGHTDRAG
2544 || c == K_X1DRAG
2545 || c == K_X2DRAG)
2546 return retval;
2547 if (popup_visible)
2548 {
2549 int row = mouse_row;
2550 int col = mouse_col;
2551 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2552
2553 if (wp != NULL && WIN_IS_POPUP(wp))
2554 return retval;
2555 }
2556 }
2557#endif
2558
zeertzjqdca29d92021-08-31 19:12:51 +02002559 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2560 {
2561 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2562 || !vim_is_ctrl_x_key(c))
2563 {
2564 // Not starting another completion mode.
2565 ctrl_x_mode = CTRL_X_CMDLINE;
2566
2567 // CTRL-X CTRL-Z should stop completion without inserting anything
2568 if (c == Ctrl_Z)
2569 retval = TRUE;
2570 }
2571 else
2572 {
2573 ctrl_x_mode = CTRL_X_CMDLINE;
2574
2575 // Other CTRL-X keys first stop completion, then start another
2576 // completion mode.
2577 ins_compl_prep(' ');
2578 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2579 }
2580 }
2581
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002583 if (ctrl_x_mode_not_defined_yet()
2584 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002585 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002586 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002587 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002588 }
2589
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002590 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002591 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2592 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002593 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002594 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002595 {
2596 // We're already in CTRL-X mode, do we stay in it?
2597 if (!vim_is_ctrl_x_key(c))
2598 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002599 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002600 ctrl_x_mode = CTRL_X_NORMAL;
2601 else
2602 ctrl_x_mode = CTRL_X_FINISHED;
2603 edit_submode = NULL;
2604 }
2605 showmode();
2606 }
2607
2608 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2609 {
2610 // Show error message from attempted keyword completion (probably
2611 // 'Pattern not found') until another key is hit, then go back to
2612 // showing what mode we are in.
2613 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002614 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002615 && c != Ctrl_R && !ins_compl_pum_key(c))
2616 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002617 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002618 }
2619 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2620 // Trigger the CompleteDone event to give scripts a chance to act
2621 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002622 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002623
LemonBoy2bf52dd2022-04-09 18:17:34 +01002624 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002625
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002626 // reset continue_* if we left expansion-mode, if we stay they'll be
2627 // (re)set properly in ins_complete()
2628 if (!vim_is_ctrl_x_key(c))
2629 {
2630 compl_cont_status = 0;
2631 compl_cont_mode = 0;
2632 }
2633
2634 return retval;
2635}
2636
2637/*
2638 * Fix the redo buffer for the completion leader replacing some of the typed
2639 * text. This inserts backspaces and appends the changed text.
2640 * "ptr" is the known leader text or NUL.
2641 */
2642 static void
2643ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2644{
John Marriott5e6ea922024-11-23 14:01:57 +01002645 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002646 char_u *p;
2647 char_u *ptr = ptr_arg;
2648
2649 if (ptr == NULL)
2650 {
John Marriott5e6ea922024-11-23 14:01:57 +01002651 if (compl_leader.string != NULL)
2652 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 else
2654 return; // nothing to do
2655 }
John Marriott5e6ea922024-11-23 14:01:57 +01002656 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002657 {
John Marriott5e6ea922024-11-23 14:01:57 +01002658 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002659 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2660 ;
2661 if (len > 0)
2662 len -= (*mb_head_off)(p, p + len);
2663 for (p += len; *p != NUL; MB_PTR_ADV(p))
2664 AppendCharToRedobuff(K_BS);
2665 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002666 if (ptr != NULL)
2667 AppendToRedobuffLit(ptr + len, -1);
2668}
2669
2670/*
2671 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2672 * (depending on flag) starting from buf and looking for a non-scanned
2673 * buffer (other than curbuf). curbuf is special, if it is called with
2674 * buf=curbuf then it has to be the first call for a given flag/expansion.
2675 *
2676 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2677 */
2678 static buf_T *
2679ins_compl_next_buf(buf_T *buf, int flag)
2680{
2681 static win_T *wp = NULL;
2682
2683 if (flag == 'w') // just windows
2684 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002685 if (buf == curbuf || !win_valid(wp))
2686 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002687 wp = curwin;
2688 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2689 && wp->w_buffer->b_scanned)
2690 ;
2691 buf = wp->w_buffer;
2692 }
2693 else
2694 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2695 // (unlisted buffers)
2696 // When completing whole lines skip unloaded buffers.
2697 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2698 && ((flag == 'U'
2699 ? buf->b_p_bl
2700 : (!buf->b_p_bl
2701 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2702 || buf->b_scanned))
2703 ;
2704 return buf;
2705}
2706
2707#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002708
2709# ifdef FEAT_EVAL
2710static callback_T cfu_cb; // 'completefunc' callback function
2711static callback_T ofu_cb; // 'omnifunc' callback function
2712static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2713# endif
2714
2715/*
2716 * Copy a global callback function to a buffer local callback.
2717 */
2718 static void
2719copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2720{
2721 free_callback(bufcb);
2722 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2723 copy_callback(bufcb, globcb);
2724}
2725
2726/*
2727 * Parse the 'completefunc' option value and set the callback function.
2728 * Invoked when the 'completefunc' option is set. The option value can be a
2729 * name of a function (string), or function(<name>) or funcref(<name>) or a
2730 * lambda expression.
2731 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002733did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002734{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2736 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002737
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002738 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002739
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002740 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002741}
2742
2743/*
2744 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002745 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002746 */
2747 void
2748set_buflocal_cfu_callback(buf_T *buf UNUSED)
2749{
2750# ifdef FEAT_EVAL
2751 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2752# endif
2753}
2754
2755/*
2756 * Parse the 'omnifunc' option value and set the callback function.
2757 * Invoked when the 'omnifunc' option is set. The option value can be a
2758 * name of a function (string), or function(<name>) or funcref(<name>) or a
2759 * lambda expression.
2760 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002762did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002763{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002764 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2765 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002766
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002767 set_buflocal_ofu_callback(curbuf);
2768 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002769}
2770
2771/*
2772 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002773 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002774 */
2775 void
2776set_buflocal_ofu_callback(buf_T *buf UNUSED)
2777{
2778# ifdef FEAT_EVAL
2779 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2780# endif
2781}
2782
2783/*
2784 * Parse the 'thesaurusfunc' option value and set the callback function.
2785 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2786 * name of a function (string), or function(<name>) or funcref(<name>) or a
2787 * lambda expression.
2788 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002789 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002790did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002791{
2792 int retval;
2793
zeertzjq6eda2692024-11-03 09:23:33 +01002794 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002796 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2797 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002798 else
2799 {
2800 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002801 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002802 // when using :set, free the local callback
2803 if (!(args->os_flags & OPT_GLOBAL))
2804 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002805 }
2806
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002807 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002808}
2809
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002810/*
2811 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002812 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002813 */
2814 int
2815set_ref_in_insexpand_funcs(int copyID)
2816{
2817 int abort = FALSE;
2818
2819 abort = set_ref_in_callback(&cfu_cb, copyID);
2820 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2821 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2822
2823 return abort;
2824}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002825
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002826/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002827 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002828 */
2829 static char_u *
2830get_complete_funcname(int type)
2831{
2832 switch (type)
2833 {
2834 case CTRL_X_FUNCTION:
2835 return curbuf->b_p_cfu;
2836 case CTRL_X_OMNI:
2837 return curbuf->b_p_ofu;
2838 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002839 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002840 default:
2841 return (char_u *)"";
2842 }
2843}
2844
2845/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002846 * Get the callback to use for insert mode completion.
2847 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002848 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002849get_insert_callback(int type)
2850{
2851 if (type == CTRL_X_FUNCTION)
2852 return &curbuf->b_cfu_cb;
2853 if (type == CTRL_X_OMNI)
2854 return &curbuf->b_ofu_cb;
2855 // CTRL_X_THESAURUS
2856 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2857}
2858
2859/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002860 * Execute user defined complete function 'completefunc', 'omnifunc' or
2861 * 'thesaurusfunc', and get matches in "matches".
2862 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863 */
2864 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002865expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002866{
2867 list_T *matchlist = NULL;
2868 dict_T *matchdict = NULL;
2869 typval_T args[3];
2870 char_u *funcname;
2871 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002872 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873 typval_T rettv;
2874 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002875 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002876
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002877 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002878 if (*funcname == NUL)
2879 return;
2880
2881 // Call 'completefunc' to obtain the list of matches.
2882 args[0].v_type = VAR_NUMBER;
2883 args[0].vval.v_number = 0;
2884 args[1].v_type = VAR_STRING;
2885 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2886 args[2].v_type = VAR_UNKNOWN;
2887
2888 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002889 // Lock the text to avoid weird things from happening. Also disallow
2890 // switching to another window, it should not be needed and may end up in
2891 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002892 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002894 cb = get_insert_callback(type);
2895 retval = call_callback(cb, 0, &rettv, 2, args);
2896
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002897 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002898 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002899 {
2900 switch (rettv.v_type)
2901 {
2902 case VAR_LIST:
2903 matchlist = rettv.vval.v_list;
2904 break;
2905 case VAR_DICT:
2906 matchdict = rettv.vval.v_dict;
2907 break;
2908 case VAR_SPECIAL:
2909 if (rettv.vval.v_number == VVAL_NONE)
2910 compl_opt_suppress_empty = TRUE;
2911 // FALLTHROUGH
2912 default:
2913 // TODO: Give error message?
2914 clear_tv(&rettv);
2915 break;
2916 }
2917 }
zeertzjqcfe45652022-05-27 17:26:55 +01002918 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002919
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002920 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002921 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002922 validate_cursor();
2923 if (!EQUAL_POS(curwin->w_cursor, pos))
2924 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002925 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002926 goto theend;
2927 }
2928
2929 if (matchlist != NULL)
2930 ins_compl_add_list(matchlist);
2931 else if (matchdict != NULL)
2932 ins_compl_add_dict(matchdict);
2933
2934theend:
2935 // Restore State, it might have been changed.
2936 State = save_State;
2937
2938 if (matchdict != NULL)
2939 dict_unref(matchdict);
2940 if (matchlist != NULL)
2941 list_unref(matchlist);
2942}
2943#endif // FEAT_COMPL_FUNC
2944
2945#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002946
2947 static inline int
2948get_user_highlight_attr(char_u *hlname)
2949{
2950 if (hlname != NULL && *hlname != NUL)
2951 return syn_name2attr(hlname);
2952 return -1;
2953}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002954/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002955 * Add a match to the list of matches from a typeval_T.
2956 * If the given string is already in the list of completions, then return
2957 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2958 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002959 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002960 */
2961 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002962ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002963{
2964 char_u *word;
2965 int dup = FALSE;
2966 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002967 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002968 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002969 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002970 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002971 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002972 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002973 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002974
Bram Moolenaar08928322020-01-04 14:32:48 +01002975 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002976 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2977 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002978 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2979 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2980 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2981 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2982 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002983
glepnir0fe17f82024-10-08 22:26:44 +02002984 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002985 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002986
2987 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002988 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002989
Bram Moolenaard61efa52022-07-23 09:52:04 +01002990 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2991 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2992 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002993 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002994 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2995 dup = dict_get_number(tv->vval.v_dict, "dup");
2996 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2997 empty = dict_get_number(tv->vval.v_dict, "empty");
2998 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2999 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003000 flags |= CP_EQUAL;
3001 }
3002 else
3003 {
3004 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003005 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003006 }
3007 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003008 {
3009 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003010 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003011 }
glepnir508e7852024-07-25 21:39:08 +02003012 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003013 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003014 if (status != OK)
3015 clear_tv(&user_data);
3016 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003017}
3018
3019/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003020 * Add completions from a list.
3021 */
3022 static void
3023ins_compl_add_list(list_T *list)
3024{
3025 listitem_T *li;
3026 int dir = compl_direction;
3027
3028 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003029 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003030 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003031 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003032 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003033 // if dir was BACKWARD then honor it just once
3034 dir = FORWARD;
3035 else if (did_emsg)
3036 break;
3037 }
3038}
3039
3040/*
3041 * Add completions from a dict.
3042 */
3043 static void
3044ins_compl_add_dict(dict_T *dict)
3045{
3046 dictitem_T *di_refresh;
3047 dictitem_T *di_words;
3048
3049 // Check for optional "refresh" item.
3050 compl_opt_refresh_always = FALSE;
3051 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3052 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3053 {
3054 char_u *v = di_refresh->di_tv.vval.v_string;
3055
3056 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3057 compl_opt_refresh_always = TRUE;
3058 }
3059
3060 // Add completions from a "words" list.
3061 di_words = dict_find(dict, (char_u *)"words", 5);
3062 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3063 ins_compl_add_list(di_words->di_tv.vval.v_list);
3064}
3065
3066/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003067 * Start completion for the complete() function.
3068 * "startcol" is where the matched text starts (1 is first column).
3069 * "list" is the list of matches.
3070 */
3071 static void
3072set_completion(colnr_T startcol, list_T *list)
3073{
3074 int save_w_wrow = curwin->w_wrow;
3075 int save_w_leftcol = curwin->w_leftcol;
3076 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003077 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003078 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3079 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3080 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081
3082 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003083 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003084 ins_compl_prep(' ');
3085 ins_compl_clear();
3086 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003087 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003088
3089 compl_direction = FORWARD;
3090 if (startcol > curwin->w_cursor.col)
3091 startcol = curwin->w_cursor.col;
3092 compl_col = startcol;
3093 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3094 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003095 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003096 if (p_ic)
3097 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003098 if (compl_orig_text.string == NULL)
3099 {
3100 compl_orig_text.length = 0;
3101 return;
3102 }
3103 compl_orig_text.length = (size_t)compl_length;
3104 if (ins_compl_add(compl_orig_text.string,
3105 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3106 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003107 return;
3108
3109 ctrl_x_mode = CTRL_X_EVAL;
3110
3111 ins_compl_add_list(list);
3112 compl_matches = ins_compl_make_cyclic();
3113 compl_started = TRUE;
3114 compl_used_match = TRUE;
3115 compl_cont_status = 0;
3116
3117 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003118 int no_select = compl_no_select || compl_longest;
3119 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003120 {
3121 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003122 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003123 // Down/Up has no real effect.
3124 ins_complete(K_UP, FALSE);
3125 }
3126 else
3127 ins_complete(Ctrl_N, FALSE);
3128 compl_enter_selects = compl_no_insert;
3129
3130 // Lazily show the popup menu, unless we got interrupted.
3131 if (!compl_interrupted)
3132 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003133 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003134 out_flush();
3135}
3136
3137/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003138 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003139 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003140 void
3141f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003142{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003143 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003144
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003145 if (in_vim9script()
3146 && (check_for_number_arg(argvars, 0) == FAIL
3147 || check_for_list_arg(argvars, 1) == FAIL))
3148 return;
3149
Bram Moolenaar24959102022-05-07 20:01:16 +01003150 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003151 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003152 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003153 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003154 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003155
3156 // Check for undo allowed here, because if something was already inserted
3157 // the line was already saved for undo and this check isn't done.
3158 if (!undo_allowed())
3159 return;
3160
Bram Moolenaard83392a2022-09-01 12:22:46 +01003161 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003162 {
3163 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3164 if (startcol > 0)
3165 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003166 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003167}
3168
3169/*
3170 * "complete_add()" function
3171 */
3172 void
3173f_complete_add(typval_T *argvars, typval_T *rettv)
3174{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003175 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003176 return;
3177
Bram Moolenaar440cf092021-04-03 20:13:30 +02003178 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003179}
3180
3181/*
3182 * "complete_check()" function
3183 */
3184 void
3185f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3186{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003187 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003188 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003189
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003190 ins_compl_check_keys(0, TRUE);
3191 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003192
3193 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003194}
3195
3196/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003197 * Return Insert completion mode name string
3198 */
3199 static char_u *
3200ins_compl_mode(void)
3201{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003202 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003203 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3204
3205 return (char_u *)"";
3206}
3207
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003208/*
3209 * Assign the sequence number to all the completion matches which don't have
3210 * one assigned yet.
3211 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003212 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003213ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003214{
3215 int number = 0;
3216 compl_T *match;
3217
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003218 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003219 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003220 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003221 // This should normally succeed already at the first loop
3222 // cycle, so it's fast!
3223 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003224 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003225 if (match->cp_number != -1)
3226 {
3227 number = match->cp_number;
3228 break;
3229 }
3230 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003231 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003232 for (match = match->cp_next;
3233 match != NULL && match->cp_number == -1;
3234 match = match->cp_next)
3235 match->cp_number = ++number;
3236 }
3237 else // BACKWARD
3238 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003239 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003240 // number. This should normally succeed already at the
3241 // first loop cycle, so it's fast!
3242 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003243 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003244 if (match->cp_number != -1)
3245 {
3246 number = match->cp_number;
3247 break;
3248 }
3249 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003250 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003251 for (match = match->cp_prev; match
3252 && match->cp_number == -1;
3253 match = match->cp_prev)
3254 match->cp_number = ++number;
3255 }
3256}
3257
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003258/*
glepnir037b0282025-01-16 14:37:44 +01003259 * Fill the dict of complete_info
3260 */
3261 static void
3262fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3263{
3264 dict_add_string(di, "word", match->cp_str.string);
3265 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3266 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3267 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3268 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3269 if (add_match)
3270 dict_add_bool(di, "match", match->cp_in_match_array);
3271 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3272 // Add an empty string for backwards compatibility
3273 dict_add_string(di, "user_data", (char_u *)"");
3274 else
3275 dict_add_tv(di, "user_data", &match->cp_user_data);
3276}
3277
3278/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003279 * Get complete information
3280 */
3281 static void
3282get_complete_info(list_T *what_list, dict_T *retdict)
3283{
3284 int ret = OK;
3285 listitem_T *item;
3286#define CI_WHAT_MODE 0x01
3287#define CI_WHAT_PUM_VISIBLE 0x02
3288#define CI_WHAT_ITEMS 0x04
3289#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003290#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003291#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003292#define CI_WHAT_ALL 0xff
3293 int what_flag;
3294
3295 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003296 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003297 else
3298 {
3299 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003300 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003301 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003302 {
3303 char_u *what = tv_get_string(&item->li_tv);
3304
3305 if (STRCMP(what, "mode") == 0)
3306 what_flag |= CI_WHAT_MODE;
3307 else if (STRCMP(what, "pum_visible") == 0)
3308 what_flag |= CI_WHAT_PUM_VISIBLE;
3309 else if (STRCMP(what, "items") == 0)
3310 what_flag |= CI_WHAT_ITEMS;
3311 else if (STRCMP(what, "selected") == 0)
3312 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003313 else if (STRCMP(what, "completed") == 0)
3314 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003315 else if (STRCMP(what, "matches") == 0)
3316 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003317 }
3318 }
3319
3320 if (ret == OK && (what_flag & CI_WHAT_MODE))
3321 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3322
3323 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3324 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3325
glepnir037b0282025-01-16 14:37:44 +01003326 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3327 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003328 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003329 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003330 dict_T *di;
3331 compl_T *match;
3332 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003333 int has_items = what_flag & CI_WHAT_ITEMS;
3334 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003335 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003336
glepnird4088ed2024-12-31 10:55:22 +01003337 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003338 {
3339 li = list_alloc();
3340 if (li == NULL)
3341 return;
glepnird4088ed2024-12-31 10:55:22 +01003342 ret = dict_add_list(retdict, (has_matches && !has_items)
3343 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003344 }
3345 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3346 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3347 ins_compl_update_sequence_numbers();
3348 if (ret == OK && compl_first_match != NULL)
3349 {
3350 int list_idx = 0;
3351 match = compl_first_match;
3352 do
3353 {
3354 if (!match_at_original_text(match))
3355 {
glepnird4088ed2024-12-31 10:55:22 +01003356 if (has_items
3357 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003358 {
3359 di = dict_alloc();
3360 if (di == NULL)
3361 return;
3362 ret = list_append_dict(li, di);
3363 if (ret != OK)
3364 return;
glepnir037b0282025-01-16 14:37:44 +01003365 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003366 }
glepnird4088ed2024-12-31 10:55:22 +01003367 if (compl_curr_match != NULL
3368 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003369 selected_idx = list_idx;
3370 list_idx += 1;
3371 }
3372 match = match->cp_next;
3373 }
3374 while (match != NULL && !is_first_match(match));
3375 }
3376 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3377 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003378
glepnir037b0282025-01-16 14:37:44 +01003379 if (ret == OK && selected_idx != -1 && has_completed)
3380 {
3381 di = dict_alloc();
3382 if (di == NULL)
3383 return;
3384 fill_complete_info_dict(di, compl_curr_match, FALSE);
3385 ret = dict_add_dict(retdict, "completed", di);
3386 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003387 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003388}
3389
3390/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003391 * "complete_info()" function
3392 */
3393 void
3394f_complete_info(typval_T *argvars, typval_T *rettv)
3395{
3396 list_T *what_list = NULL;
3397
Bram Moolenaar93a10962022-06-16 11:42:09 +01003398 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003399 return;
3400
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003401 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3402 return;
3403
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003404 if (argvars[0].v_type != VAR_UNKNOWN)
3405 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003406 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003407 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003408 what_list = argvars[0].vval.v_list;
3409 }
3410 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003411}
3412#endif
3413
3414/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003415 * Returns TRUE when using a user-defined function for thesaurus completion.
3416 */
3417 static int
3418thesaurus_func_complete(int type UNUSED)
3419{
3420#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003421 return type == CTRL_X_THESAURUS
3422 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003423#else
3424 return FALSE;
3425#endif
3426}
3427
3428/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 * Return value of process_next_cpt_value()
3430 */
3431enum
3432{
3433 INS_COMPL_CPT_OK = 1,
3434 INS_COMPL_CPT_CONT,
3435 INS_COMPL_CPT_END
3436};
3437
3438/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003439 * state information used for getting the next set of insert completion
3440 * matches.
3441 */
3442typedef struct
3443{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003444 char_u *e_cpt_copy; // copy of 'complete'
3445 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003446 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003447 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003448 pos_T prev_match_pos; // previous match position
3449 int set_match_pos; // save first_match_pos/last_match_pos
3450 pos_T first_match_pos; // first match position
3451 pos_T last_match_pos; // last match position
3452 int found_all; // found all matches of a certain type.
3453 char_u *dict; // dictionary file to search
3454 int dict_f; // "dict" is an exact file name or not
3455} ins_compl_next_state_T;
3456
3457/*
3458 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 *
3460 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003461 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003462 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003463 * st->found_all - all matches of this type are found
3464 * st->ins_buf - search for completions in this buffer
3465 * st->first_match_pos - position of the first completion match
3466 * st->last_match_pos - position of the last completion match
3467 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003468 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003469 * st->dict - name of the dictionary or thesaurus file to search
3470 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471 *
3472 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003473 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3474 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003475 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003476 */
3477 static int
3478process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003479 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003480 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003481 pos_T *start_match_pos,
3482 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003483{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003484 int compl_type = -1;
3485 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003486
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003487 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003488
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003489 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3490 st->e_cpt++;
3491
3492 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003493 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003494 st->ins_buf = curbuf;
3495 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003496 // Move the cursor back one character so that ^N can match the
3497 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003498 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003499 {
3500 // Move the cursor to after the last character in the
3501 // buffer, so that word at start of buffer is found
3502 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003503 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003504 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003505 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003506 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003507 compl_type = 0;
3508
3509 // Remember the first match so that the loop stops when we
3510 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003511 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003512 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003513 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003514 && (st->ins_buf = ins_compl_next_buf(
3515 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003516 {
3517 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003518 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003519 {
3520 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003521 st->first_match_pos.col = st->last_match_pos.col = 0;
3522 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3523 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003524 compl_type = 0;
3525 }
3526 else // unloaded buffer, scan like dictionary
3527 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003528 st->found_all = TRUE;
3529 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003530 {
3531 status = INS_COMPL_CPT_CONT;
3532 goto done;
3533 }
3534 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003535 st->dict = st->ins_buf->b_fname;
3536 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003537 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003538 if (!shortmess(SHM_COMPLETIONSCAN))
3539 {
3540 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3541 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3542 st->ins_buf->b_fname == NULL
3543 ? buf_spname(st->ins_buf)
3544 : st->ins_buf->b_sfname == NULL
3545 ? st->ins_buf->b_fname
3546 : st->ins_buf->b_sfname);
3547 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3548 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003549 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003550 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003551 status = INS_COMPL_CPT_END;
3552 else
3553 {
3554 if (ctrl_x_mode_line_or_eval())
3555 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003556 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003557 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003558 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559 compl_type = CTRL_X_DICTIONARY;
3560 else
3561 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003562 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003564 st->dict = st->e_cpt;
3565 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003566 }
3567 }
3568#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003569 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003570 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003571 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 compl_type = CTRL_X_PATH_DEFINES;
3573#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003574 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003575 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003577 if (!shortmess(SHM_COMPLETIONSCAN))
3578 {
3579 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3580 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3581 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3582 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003583 }
3584 else
3585 compl_type = -1;
3586
3587 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003588 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003589
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003590 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003591 if (compl_type == -1)
3592 status = INS_COMPL_CPT_CONT;
3593 }
3594
3595done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003596 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003597 return status;
3598}
3599
3600#ifdef FEAT_FIND_ID
3601/*
3602 * Get the next set of identifiers or defines matching "compl_pattern" in
3603 * included files.
3604 */
3605 static void
3606get_next_include_file_completion(int compl_type)
3607{
John Marriott5e6ea922024-11-23 14:01:57 +01003608 find_pattern_in_path(compl_pattern.string, compl_direction,
3609 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 (compl_type == CTRL_X_PATH_DEFINES
3611 && !(compl_cont_status & CONT_SOL))
3612 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003613 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003614}
3615#endif
3616
3617/*
3618 * Get the next set of words matching "compl_pattern" in dictionary or
3619 * thesaurus files.
3620 */
3621 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003622get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003623{
3624#ifdef FEAT_COMPL_FUNC
3625 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003626 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003627 else
3628#endif
3629 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003630 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003631 : (compl_type == CTRL_X_THESAURUS
3632 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3633 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003634 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003635 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003636 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003637}
3638
3639/*
3640 * Get the next set of tag names matching "compl_pattern".
3641 */
3642 static void
3643get_next_tag_completion(void)
3644{
3645 int save_p_ic;
3646 char_u **matches;
3647 int num_matches;
3648
3649 // set p_ic according to p_ic, p_scs and pat for find_tags().
3650 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003651 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003652
3653 // Find up to TAG_MANY matches. Avoids that an enormous number
3654 // of matches is found when compl_pattern is empty
3655 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003656 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003657 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003658 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3660 ins_compl_add_matches(num_matches, matches, p_ic);
3661 g_tag_at_cursor = FALSE;
3662 p_ic = save_p_ic;
3663}
3664
3665/*
glepnir8159fb12024-07-17 20:32:54 +02003666 * Compare function for qsort
3667 */
3668static int compare_scores(const void *a, const void *b)
3669{
3670 int idx_a = *(const int *)a;
3671 int idx_b = *(const int *)b;
3672 int score_a = compl_fuzzy_scores[idx_a];
3673 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003674 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3675 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003676}
3677
3678/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003679 * Get the next set of filename matching "compl_pattern".
3680 */
3681 static void
3682get_next_filename_completion(void)
3683{
3684 char_u **matches;
3685 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003686 char_u *ptr;
3687 garray_T fuzzy_indices;
3688 int i;
3689 int score;
3690 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003691 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003692 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3693 char_u **sorted_matches;
3694 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003695 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003696
glepnirb9de1a02024-08-02 19:14:38 +02003697#ifdef BACKSLASH_IN_FILENAME
3698 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3699 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3700#else
3701 char pathsep = PATHSEP;
3702#endif
3703
glepnir8159fb12024-07-17 20:32:54 +02003704 if (in_fuzzy)
3705 {
glepnirb9de1a02024-08-02 19:14:38 +02003706#ifdef BACKSLASH_IN_FILENAME
3707 if (curbuf->b_p_csl[0] == 's')
3708 {
John Marriott5e6ea922024-11-23 14:01:57 +01003709 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003710 {
3711 if (leader[i] == '\\')
3712 leader[i] = '/';
3713 }
3714 }
3715 else if (curbuf->b_p_csl[0] == 'b')
3716 {
John Marriott5e6ea922024-11-23 14:01:57 +01003717 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003718 {
3719 if (leader[i] == '/')
3720 leader[i] = '\\';
3721 }
3722 }
3723#endif
3724 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003725 if (last_sep == NULL)
3726 {
3727 // No path separator or separator is the last character,
3728 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003729 VIM_CLEAR_STRING(compl_pattern);
3730 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3731 if (compl_pattern.string == NULL)
3732 return;
3733 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003734 }
3735 else if (*(last_sep + 1) == '\0')
3736 in_fuzzy = FALSE;
3737 else
3738 {
3739 // Split leader into path and file parts
3740 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003741 char_u *path_with_wildcard;
3742
3743 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003744 if (path_with_wildcard != NULL)
3745 {
John Marriott5e6ea922024-11-23 14:01:57 +01003746 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3747 VIM_CLEAR_STRING(compl_pattern);
3748 compl_pattern.string = path_with_wildcard;
3749 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003750
3751 // Move leader to the file part
3752 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003753 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003754 }
3755 }
glepnir8159fb12024-07-17 20:32:54 +02003756 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003757
John Marriott5e6ea922024-11-23 14:01:57 +01003758 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003759 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3760 return;
3761
3762 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003763 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003764#ifdef BACKSLASH_IN_FILENAME
3765 if (curbuf->b_p_csl[0] != NUL)
3766 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003767 for (i = 0; i < num_matches; ++i)
3768 {
glepnir8159fb12024-07-17 20:32:54 +02003769 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003770 while (*ptr != NUL)
3771 {
3772 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3773 *ptr = '/';
3774 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3775 *ptr = '\\';
3776 ptr += (*mb_ptr2len)(ptr);
3777 }
3778 }
3779 }
3780#endif
glepnir8159fb12024-07-17 20:32:54 +02003781
3782 if (in_fuzzy)
3783 {
3784 ga_init2(&fuzzy_indices, sizeof(int), 10);
3785 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3786
3787 for (i = 0; i < num_matches; i++)
3788 {
3789 ptr = matches[i];
3790 score = fuzzy_match_str(ptr, leader);
3791 if (score > 0)
3792 {
3793 if (ga_grow(&fuzzy_indices, 1) == OK)
3794 {
3795 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3796 compl_fuzzy_scores[i] = score;
3797 fuzzy_indices.ga_len++;
3798 }
3799 }
3800 }
3801
Christian Brabandt220474d2024-07-20 13:26:44 +02003802 // prevent qsort from deref NULL pointer
3803 if (fuzzy_indices.ga_len > 0)
3804 {
3805 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3806 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003807
Christian Brabandt220474d2024-07-20 13:26:44 +02003808 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3809 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3810 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003811
Christian Brabandt220474d2024-07-20 13:26:44 +02003812 FreeWild(num_matches, matches);
3813 matches = sorted_matches;
3814 num_matches = fuzzy_indices.ga_len;
3815 }
glepnir6b6280c2024-07-27 16:25:45 +02003816 else if (leader_len > 0)
3817 {
3818 FreeWild(num_matches, matches);
3819 num_matches = 0;
3820 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003821
glepnir8159fb12024-07-17 20:32:54 +02003822 vim_free(compl_fuzzy_scores);
3823 ga_clear(&fuzzy_indices);
3824 }
3825
glepnir6b6280c2024-07-27 16:25:45 +02003826 if (num_matches > 0)
3827 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003828}
3829
3830/*
3831 * Get the next set of command-line completions matching "compl_pattern".
3832 */
3833 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003834get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003835{
3836 char_u **matches;
3837 int num_matches;
3838
John Marriott5e6ea922024-11-23 14:01:57 +01003839 if (expand_cmdline(&compl_xp, compl_pattern.string,
3840 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003841 ins_compl_add_matches(num_matches, matches, FALSE);
3842}
3843
3844/*
3845 * Get the next set of spell suggestions matching "compl_pattern".
3846 */
3847 static void
3848get_next_spell_completion(linenr_T lnum UNUSED)
3849{
3850#ifdef FEAT_SPELL
3851 char_u **matches;
3852 int num_matches;
3853
John Marriott5e6ea922024-11-23 14:01:57 +01003854 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003855 if (num_matches > 0)
3856 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003857 else
3858 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003859#endif
3860}
3861
3862/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003863 * Return the next word or line from buffer "ins_buf" at position
3864 * "cur_match_pos" for completion. The length of the match is set in "len".
3865 */
3866 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003867ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003868 buf_T *ins_buf, // buffer being scanned
3869 pos_T *cur_match_pos, // current match position
3870 int *match_len,
3871 int *cont_s_ipos) // next ^X<> will set initial_pos
3872{
3873 char_u *ptr;
3874 int len;
3875
3876 *match_len = 0;
3877 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3878 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003879 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003880 if (ctrl_x_mode_line_or_eval())
3881 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003882 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003883 {
3884 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3885 return NULL;
3886 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003887 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003888 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003889 {
3890 char_u *tmp_ptr = ptr;
3891
3892 ptr = skipwhite(tmp_ptr);
3893 len -= (int)(ptr - tmp_ptr);
3894 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003895 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003896 }
3897 else
3898 {
3899 char_u *tmp_ptr = ptr;
3900
John Marriott5e6ea922024-11-23 14:01:57 +01003901 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003902 {
3903 tmp_ptr += compl_length;
3904 // Skip if already inside a word.
3905 if (vim_iswordp(tmp_ptr))
3906 return NULL;
3907 // Find start of next word.
3908 tmp_ptr = find_word_start(tmp_ptr);
3909 }
3910 // Find end of this word.
3911 tmp_ptr = find_word_end(tmp_ptr);
3912 len = (int)(tmp_ptr - ptr);
3913
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003914 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003915 {
3916 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3917 {
3918 // Try next line, if any. the new word will be
3919 // "join" as if the normal command "J" was used.
3920 // IOSIZE is always greater than
3921 // compl_length, so the next STRNCPY always
3922 // works -- Acevedo
3923 STRNCPY(IObuff, ptr, len);
3924 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3925 tmp_ptr = ptr = skipwhite(ptr);
3926 // Find start of next word.
3927 tmp_ptr = find_word_start(tmp_ptr);
3928 // Find end of next word.
3929 tmp_ptr = find_word_end(tmp_ptr);
3930 if (tmp_ptr > ptr)
3931 {
3932 if (*ptr != ')' && IObuff[len - 1] != TAB)
3933 {
3934 if (IObuff[len - 1] != ' ')
3935 IObuff[len++] = ' ';
3936 // IObuf =~ "\k.* ", thus len >= 2
3937 if (p_js
3938 && (IObuff[len - 2] == '.'
3939 || (vim_strchr(p_cpo, CPO_JOINSP)
3940 == NULL
3941 && (IObuff[len - 2] == '?'
3942 || IObuff[len - 2] == '!'))))
3943 IObuff[len++] = ' ';
3944 }
3945 // copy as much as possible of the new word
3946 if (tmp_ptr - ptr >= IOSIZE - len)
3947 tmp_ptr = ptr + IOSIZE - len - 1;
3948 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3949 len += (int)(tmp_ptr - ptr);
3950 *cont_s_ipos = TRUE;
3951 }
3952 IObuff[len] = NUL;
3953 ptr = IObuff;
3954 }
3955 if (len == compl_length)
3956 return NULL;
3957 }
3958 }
3959
3960 *match_len = len;
3961 return ptr;
3962}
3963
3964/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003965 * Get the next set of words matching "compl_pattern" for default completion(s)
3966 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003967 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3968 * position "st->start_pos" in the "compl_direction" direction. If
3969 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3970 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003971 * Returns OK if a new next match is found, otherwise returns FAIL.
3972 */
3973 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003974get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003975{
3976 int found_new_match = FAIL;
3977 int save_p_scs;
3978 int save_p_ws;
3979 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003980 char_u *ptr = NULL;
3981 int len = 0;
3982 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3983 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003984
3985 // If 'infercase' is set, don't use 'smartcase' here
3986 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003987 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003988 p_scs = FALSE;
3989
3990 // Buffers other than curbuf are scanned from the beginning or the
3991 // end but never from the middle, thus setting nowrapscan in this
3992 // buffer is a good idea, on the other hand, we always set
3993 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3994 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003995 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003996 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003997 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003998 p_ws = TRUE;
3999 looped_around = FALSE;
4000 for (;;)
4001 {
4002 int cont_s_ipos = FALSE;
4003
4004 ++msg_silent; // Don't want messages for wrapscan.
4005
4006 // ctrl_x_mode_line_or_eval() || word-wise search that
4007 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02004008 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004009 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01004010 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02004011 else if (in_fuzzy)
4012 found_new_match = search_for_fuzzy_match(st->ins_buf,
4013 st->cur_match_pos, leader, compl_direction,
4014 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004015 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004016 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004017 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004018 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004019 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004020 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004021 {
4022 // set "compl_started" even on fail
4023 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004024 st->first_match_pos = *st->cur_match_pos;
4025 st->last_match_pos = *st->cur_match_pos;
4026 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004027 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004028 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4029 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004030 {
4031 found_new_match = FAIL;
4032 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004033 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004034 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4035 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4036 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004037 {
4038 if (looped_around)
4039 found_new_match = FAIL;
4040 else
4041 looped_around = TRUE;
4042 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004043 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004044 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4045 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4046 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004047 {
4048 if (looped_around)
4049 found_new_match = FAIL;
4050 else
4051 looped_around = TRUE;
4052 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004053 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004054 if (found_new_match == FAIL)
4055 break;
4056
4057 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004058 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004059 && start_pos->lnum == st->cur_match_pos->lnum
4060 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004061 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004062
glepnir8159fb12024-07-17 20:32:54 +02004063 if (!in_fuzzy)
4064 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4065 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004066 if (ptr == NULL)
4067 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004068
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004069 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004070 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004071 0, cont_s_ipos) != NOTDONE)
4072 {
4073 found_new_match = OK;
4074 break;
4075 }
4076 }
4077 p_scs = save_p_scs;
4078 p_ws = save_p_ws;
4079
4080 return found_new_match;
4081}
4082
4083/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004084 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004085 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004086 */
4087 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004088get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004089{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004090 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004091
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004092 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004093 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004094 case -1:
4095 break;
4096#ifdef FEAT_FIND_ID
4097 case CTRL_X_PATH_PATTERNS:
4098 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004099 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004100 break;
4101#endif
4102
4103 case CTRL_X_DICTIONARY:
4104 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004105 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4106 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004107 break;
4108
4109 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004110 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004111 break;
4112
4113 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004114 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004115 break;
4116
4117 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004118 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004119 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004120 break;
4121
4122#ifdef FEAT_COMPL_FUNC
4123 case CTRL_X_FUNCTION:
4124 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004125 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004126 break;
4127#endif
4128
4129 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004130 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004131 break;
4132
4133 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004134 found_new_match = get_next_default_completion(st, ini);
4135 if (found_new_match == FAIL && st->ins_buf == curbuf)
4136 st->found_all = TRUE;
4137 }
4138
4139 // check if compl_curr_match has changed, (e.g. other type of
4140 // expansion added something)
4141 if (type != 0 && compl_curr_match != compl_old_match)
4142 found_new_match = OK;
4143
4144 return found_new_match;
4145}
4146
4147/*
4148 * Get the next expansion(s), using "compl_pattern".
4149 * The search starts at position "ini" in curbuf and in the direction
4150 * compl_direction.
4151 * When "compl_started" is FALSE start at that position, otherwise continue
4152 * where we stopped searching before.
4153 * This may return before finding all the matches.
4154 * Return the total number of matches or -1 if still unknown -- Acevedo
4155 */
4156 static int
4157ins_compl_get_exp(pos_T *ini)
4158{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004159 static ins_compl_next_state_T st;
4160 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004161 int i;
4162 int found_new_match;
4163 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004164 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004165
4166 if (!compl_started)
4167 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004168 buf_T *buf;
4169
4170 FOR_ALL_BUFFERS(buf)
4171 buf->b_scanned = 0;
4172 if (!st_cleared)
4173 {
4174 CLEAR_FIELD(st);
4175 st_cleared = TRUE;
4176 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004177 st.found_all = FALSE;
4178 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004179 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004180 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004181 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4182 ? (char_u *)"." : curbuf->b_p_cpt);
4183 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004184 st.last_match_pos = st.first_match_pos = *ini;
4185 }
4186 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4187 st.ins_buf = curbuf; // In case the buffer was wiped out.
4188
4189 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004190 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004191 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004192
4193 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4194 for (;;)
4195 {
4196 found_new_match = FAIL;
4197 st.set_match_pos = FALSE;
4198
4199 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4200 // or if found_all says this entry is done. For ^X^L only use the
4201 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004202 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004203 && (!compl_started || st.found_all))
4204 {
glepnir8159fb12024-07-17 20:32:54 +02004205 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004206
4207 if (status == INS_COMPL_CPT_END)
4208 break;
4209 if (status == INS_COMPL_CPT_CONT)
4210 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004211 }
4212
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004213 // If complete() was called then compl_pattern has been reset. The
4214 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004215 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004216 break;
4217
4218 // get the next set of completion matches
4219 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004220
4221 // break the loop for specialized modes (use 'complete' just for the
4222 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4223 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004224 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4225 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004226 {
4227 if (got_int)
4228 break;
4229 // Fill the popup menu as soon as possible.
4230 if (type != -1)
4231 ins_compl_check_keys(0, FALSE);
4232
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004233 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004234 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4235 break;
4236 compl_started = TRUE;
4237 }
4238 else
4239 {
4240 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004241 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004242 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004243
4244 compl_started = FALSE;
4245 }
4246 }
4247 compl_started = TRUE;
4248
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004249 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004250 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004251 found_new_match = FAIL;
4252
4253 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004254 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004255 && !ctrl_x_mode_line_or_eval()))
4256 i = ins_compl_make_cyclic();
4257
4258 if (compl_old_match != NULL)
4259 {
4260 // If several matches were added (FORWARD) or the search failed and has
4261 // just been made cyclic then we have to move compl_curr_match to the
4262 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004263 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004264 : compl_old_match->cp_prev;
4265 if (compl_curr_match == NULL)
4266 compl_curr_match = compl_old_match;
4267 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004268 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004269
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004270 return i;
4271}
4272
4273/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004274 * Update "compl_shown_match" to the actually shown match, it may differ when
4275 * "compl_leader" is used to omit some of the matches.
4276 */
4277 static void
4278ins_compl_update_shown_match(void)
4279{
4280 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004281 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004282 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004283 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004284 compl_shown_match = compl_shown_match->cp_next;
4285
4286 // If we didn't find it searching forward, and compl_shows_dir is
4287 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004288 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004289 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004290 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004291 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004292 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004293 {
4294 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004295 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004296 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004297 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004298 compl_shown_match = compl_shown_match->cp_prev;
4299 }
4300}
4301
4302/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004303 * Delete the old text being completed.
4304 */
4305 void
4306ins_compl_delete(void)
4307{
4308 int col;
4309
4310 // In insert mode: Delete the typed part.
4311 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004312 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004313 if ((int)curwin->w_cursor.col > col)
4314 {
4315 if (stop_arrow() == FAIL)
4316 return;
4317 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004318 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004319 }
4320
4321 // TODO: is this sufficient for redrawing? Redrawing everything causes
4322 // flicker, thus we can't do that.
4323 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004324#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004325 // clear v:completed_item
4326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004327#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004328}
4329
4330/*
4331 * Insert the new text being completed.
4332 * "in_compl_func" is TRUE when called from complete_check().
4333 */
4334 void
4335ins_compl_insert(int in_compl_func)
4336{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004337 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004338
4339 // Make sure we don't go over the end of the string, this can happen with
4340 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004341 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004342 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004343 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004344 compl_used_match = FALSE;
4345 else
4346 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004347#ifdef FEAT_EVAL
4348 {
4349 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4350
4351 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4352 }
4353#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004354 if (!in_compl_func)
4355 compl_curr_match = compl_shown_match;
4356}
4357
4358/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004359 * show the file name for the completion match (if any). Truncate the file
4360 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004361 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004362 static void
4363ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004364{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004365 char *lead = _("match in file");
4366 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4367 char_u *s;
4368 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004369
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004370 if (space <= 0)
4371 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004372
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004373 // We need the tail that fits. With double-byte encoding going
4374 // back from the end is very slow, thus go from the start and keep
4375 // the text that fits in "space" between "s" and "e".
4376 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004377 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004378 space -= ptr2cells(e);
4379 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004380 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004381 space += ptr2cells(s);
4382 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004383 }
4384 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004385 msg_hist_off = TRUE;
4386 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4387 s > compl_shown_match->cp_fname ? "<" : "", s);
4388 msg((char *)IObuff);
4389 msg_hist_off = FALSE;
4390 redraw_cmdline = FALSE; // don't overwrite!
4391}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004392
glepnirdca57fb2024-06-04 22:01:21 +02004393/*
zeertzjq551d8c32024-06-05 19:53:32 +02004394 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004395 */
glepnira218cc62024-06-03 19:32:39 +02004396 static compl_T *
4397find_comp_when_fuzzy(void)
4398{
4399 int score;
4400 char_u* str;
4401 int target_idx = -1;
4402 int is_forward = compl_shows_dir_forward();
4403 int is_backward = compl_shows_dir_backward();
4404 compl_T *comp = NULL;
4405
glepnir43eef882024-06-19 20:20:48 +02004406 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004407 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004408 return compl_first_match != compl_shown_match ? compl_first_match :
4409 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004410
4411 if (is_forward)
4412 target_idx = compl_selected_item + 1;
4413 else if (is_backward)
4414 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004415 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004416
4417 score = compl_match_array[target_idx].pum_score;
4418 str = compl_match_array[target_idx].pum_text;
4419
4420 comp = compl_first_match;
4421 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004422 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004423 return comp;
4424 comp = comp->cp_next;
4425 } while (comp != NULL && !is_first_match(comp));
4426
4427 return NULL;
4428}
4429
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004430/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004431 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004432 * times. The number of matches found is returned in 'num_matches'.
4433 *
4434 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4435 * get more completions. If it is FALSE, then do nothing when there are no more
4436 * completions in the given direction.
4437 *
4438 * If "advance" is TRUE, then completion will move to the first match.
4439 * Otherwise, the original text will be shown.
4440 *
4441 * Returns OK on success and -1 if the number of matches are unknown.
4442 */
4443 static int
4444find_next_completion_match(
4445 int allow_get_expansion,
4446 int todo, // repeat completion this many times
4447 int advance,
4448 int *num_matches)
4449{
4450 int found_end = FALSE;
4451 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004452 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004453 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4454 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004455
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004456 while (--todo >= 0)
4457 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004458 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004459 {
glepniraced8c22024-06-15 15:13:05 +02004460 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4461 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004462 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004463 && (is_first_match(compl_shown_match->cp_next)
4464 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004465 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004466 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004467 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004468 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004469 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004470 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4471 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004472 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004473 }
4474 else
4475 {
4476 if (!allow_get_expansion)
4477 {
4478 if (advance)
4479 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004480 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004481 compl_pending -= todo + 1;
4482 else
4483 compl_pending += todo + 1;
4484 }
4485 return -1;
4486 }
4487
4488 if (!compl_no_select && advance)
4489 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004490 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004491 --compl_pending;
4492 else
4493 ++compl_pending;
4494 }
4495
4496 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004497 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004498
4499 // handle any pending completions
4500 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004501 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004502 {
4503 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4504 {
4505 compl_shown_match = compl_shown_match->cp_next;
4506 --compl_pending;
4507 }
4508 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4509 {
4510 compl_shown_match = compl_shown_match->cp_prev;
4511 ++compl_pending;
4512 }
4513 else
4514 break;
4515 }
4516 found_end = FALSE;
4517 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004518 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004519 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004520 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004521 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004522 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004523 ++todo;
4524 else
4525 // Remember a matching item.
4526 found_compl = compl_shown_match;
4527
4528 // Stop at the end of the list when we found a usable match.
4529 if (found_end)
4530 {
4531 if (found_compl != NULL)
4532 {
4533 compl_shown_match = found_compl;
4534 break;
4535 }
4536 todo = 1; // use first usable match after wrapping around
4537 }
4538 }
4539
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004540 return OK;
4541}
4542
4543/*
4544 * Fill in the next completion in the current direction.
4545 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4546 * get more completions. If it is FALSE, then we just do nothing when there
4547 * are no more completions in a given direction. The latter case is used when
4548 * we are still in the middle of finding completions, to allow browsing
4549 * through the ones found so far.
4550 * Return the total number of matches, or -1 if still unknown -- webb.
4551 *
4552 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4553 * compl_shown_match here.
4554 *
4555 * Note that this function may be called recursively once only. First with
4556 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4557 * calls this function with "allow_get_expansion" FALSE.
4558 */
4559 static int
4560ins_compl_next(
4561 int allow_get_expansion,
4562 int count, // repeat completion this many times; should
4563 // be at least 1
4564 int insert_match, // Insert the newly selected match
4565 int in_compl_func) // called from complete_check()
4566{
4567 int num_matches = -1;
4568 int todo = count;
4569 int advance;
4570 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004571 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004572 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004573 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4574 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004575
4576 // When user complete function return -1 for findstart which is next
4577 // time of 'always', compl_shown_match become NULL.
4578 if (compl_shown_match == NULL)
4579 return -1;
4580
John Marriott5e6ea922024-11-23 14:01:57 +01004581 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004582 && !match_at_original_text(compl_shown_match)
4583 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004584 // Update "compl_shown_match" to the actually shown match
4585 ins_compl_update_shown_match();
4586
4587 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004588 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004589 // Delete old text to be replaced
4590 ins_compl_delete();
4591
4592 // When finding the longest common text we stick at the original text,
4593 // don't let CTRL-N or CTRL-P move to the first match.
4594 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4595
4596 // When restarting the search don't insert the first match either.
4597 if (compl_restarting)
4598 {
4599 advance = FALSE;
4600 compl_restarting = FALSE;
4601 }
4602
4603 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4604 // around.
4605 if (find_next_completion_match(allow_get_expansion, todo, advance,
4606 &num_matches) == -1)
4607 return -1;
4608
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004609 if (curbuf != orig_curbuf)
4610 {
4611 // In case some completion function switched buffer, don't want to
4612 // insert the completion elsewhere.
4613 return -1;
4614 }
4615
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004616 // Insert the text of the new completion, or the compl_leader.
4617 if (compl_no_insert && !started)
4618 {
glepnir6a38aff2024-12-16 21:56:16 +01004619 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004620 compl_used_match = FALSE;
4621 }
4622 else if (insert_match)
4623 {
4624 if (!compl_get_longest || compl_used_match)
4625 ins_compl_insert(in_compl_func);
4626 else
glepnir6a38aff2024-12-16 21:56:16 +01004627 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004628 }
4629 else
4630 compl_used_match = FALSE;
4631
4632 if (!allow_get_expansion)
4633 {
4634 // may undisplay the popup menu first
4635 ins_compl_upd_pum();
4636
4637 if (pum_enough_matches())
4638 // Will display the popup menu, don't redraw yet to avoid flicker.
4639 pum_call_update_screen();
4640 else
4641 // Not showing the popup menu yet, redraw to show the user what was
4642 // inserted.
4643 update_screen(0);
4644
4645 // display the updated popup menu
4646 ins_compl_show_pum();
4647#ifdef FEAT_GUI
4648 if (gui.in_use)
4649 {
4650 // Show the cursor after the match, not after the redrawn text.
4651 setcursor();
4652 out_flush_cursor(FALSE, FALSE);
4653 }
4654#endif
4655
4656 // Delete old text to be replaced, since we're still searching and
4657 // don't want to match ourselves!
4658 ins_compl_delete();
4659 }
4660
4661 // Enter will select a match when the match wasn't inserted and the popup
4662 // menu is visible.
4663 if (compl_no_insert && !started)
4664 compl_enter_selects = TRUE;
4665 else
4666 compl_enter_selects = !insert_match && compl_match_array != NULL;
4667
4668 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004669 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004670 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004671
4672 return num_matches;
4673}
4674
4675/*
4676 * Call this while finding completions, to check whether the user has hit a key
4677 * that should change the currently displayed completion, or exit completion
4678 * mode. Also, when compl_pending is not zero, show a completion as soon as
4679 * possible. -- webb
4680 * "frequency" specifies out of how many calls we actually check.
4681 * "in_compl_func" is TRUE when called from complete_check(), don't set
4682 * compl_curr_match.
4683 */
4684 void
4685ins_compl_check_keys(int frequency, int in_compl_func)
4686{
4687 static int count = 0;
4688 int c;
4689
4690 // Don't check when reading keys from a script, :normal or feedkeys().
4691 // That would break the test scripts. But do check for keys when called
4692 // from complete_check().
4693 if (!in_compl_func && (using_script() || ex_normal_busy))
4694 return;
4695
4696 // Only do this at regular intervals
4697 if (++count < frequency)
4698 return;
4699 count = 0;
4700
4701 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4702 // can't do its work correctly.
4703 c = vpeekc_any();
4704 if (c != NUL)
4705 {
4706 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4707 {
4708 c = safe_vgetc(); // Eat the character
4709 compl_shows_dir = ins_compl_key2dir(c);
4710 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4711 c != K_UP && c != K_DOWN, in_compl_func);
4712 }
4713 else
4714 {
4715 // Need to get the character to have KeyTyped set. We'll put it
4716 // back with vungetc() below. But skip K_IGNORE.
4717 c = safe_vgetc();
4718 if (c != K_IGNORE)
4719 {
4720 // Don't interrupt completion when the character wasn't typed,
4721 // e.g., when doing @q to replay keys.
4722 if (c != Ctrl_R && KeyTyped)
4723 compl_interrupted = TRUE;
4724
4725 vungetc(c);
4726 }
4727 }
4728 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004729 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004730 {
4731 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4732
4733 compl_pending = 0;
4734 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4735 }
4736}
4737
4738/*
4739 * Decide the direction of Insert mode complete from the key typed.
4740 * Returns BACKWARD or FORWARD.
4741 */
4742 static int
4743ins_compl_key2dir(int c)
4744{
4745 if (c == Ctrl_P || c == Ctrl_L
4746 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4747 return BACKWARD;
4748 return FORWARD;
4749}
4750
4751/*
4752 * Return TRUE for keys that are used for completion only when the popup menu
4753 * is visible.
4754 */
4755 static int
4756ins_compl_pum_key(int c)
4757{
4758 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4759 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4760 || c == K_UP || c == K_DOWN);
4761}
4762
4763/*
4764 * Decide the number of completions to move forward.
4765 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4766 */
4767 static int
4768ins_compl_key2count(int c)
4769{
4770 int h;
4771
4772 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4773 {
4774 h = pum_get_height();
4775 if (h > 3)
4776 h -= 2; // keep some context
4777 return h;
4778 }
4779 return 1;
4780}
4781
4782/*
4783 * Return TRUE if completion with "c" should insert the match, FALSE if only
4784 * to change the currently selected completion.
4785 */
4786 static int
4787ins_compl_use_match(int c)
4788{
4789 switch (c)
4790 {
4791 case K_UP:
4792 case K_DOWN:
4793 case K_PAGEDOWN:
4794 case K_KPAGEDOWN:
4795 case K_S_DOWN:
4796 case K_PAGEUP:
4797 case K_KPAGEUP:
4798 case K_S_UP:
4799 return FALSE;
4800 }
4801 return TRUE;
4802}
4803
4804/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004805 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4806 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004807 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004808 * Uses the global variables: compl_cont_status and ctrl_x_mode
4809 */
4810 static int
4811get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4812{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004813 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004814 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004815 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004816 {
4817 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4818 ;
4819 compl_col += ++startcol;
4820 compl_length = curs_col - startcol;
4821 }
4822 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004823 {
John Marriott5e6ea922024-11-23 14:01:57 +01004824 compl_pattern.string = str_foldcase(line + compl_col,
4825 compl_length, NULL, 0);
4826 if (compl_pattern.string == NULL)
4827 {
4828 compl_pattern.length = 0;
4829 return FAIL;
4830 }
4831 compl_pattern.length = STRLEN(compl_pattern.string);
4832 }
4833 else
4834 {
4835 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4836 if (compl_pattern.string == NULL)
4837 {
4838 compl_pattern.length = 0;
4839 return FAIL;
4840 }
4841 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004842 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004843 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004844 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004845 {
4846 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004847 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004848 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004849
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004850 if (!vim_iswordp(line + compl_col)
4851 || (compl_col > 0
4852 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004853 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004854 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004855 prefixlen = 0;
4856 }
John Marriott5e6ea922024-11-23 14:01:57 +01004857
4858 // we need up to 2 extra chars for the prefix
4859 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4860 compl_pattern.string = alloc(n);
4861 if (compl_pattern.string == NULL)
4862 {
4863 compl_pattern.length = 0;
4864 return FAIL;
4865 }
4866 STRCPY((char *)compl_pattern.string, prefix);
4867 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004868 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004869 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004870 }
4871 else if (--startcol < 0
4872 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4873 {
John Marriott5e6ea922024-11-23 14:01:57 +01004874 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4875
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004876 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004877 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4878 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004879 {
John Marriott5e6ea922024-11-23 14:01:57 +01004880 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004881 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004882 }
John Marriott5e6ea922024-11-23 14:01:57 +01004883 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004884 compl_col += curs_col;
4885 compl_length = 0;
4886 }
4887 else
4888 {
4889 // Search the point of change class of multibyte character
4890 // or not a word single byte character backward.
4891 if (has_mbyte)
4892 {
4893 int base_class;
4894 int head_off;
4895
4896 startcol -= (*mb_head_off)(line, line + startcol);
4897 base_class = mb_get_class(line + startcol);
4898 while (--startcol >= 0)
4899 {
4900 head_off = (*mb_head_off)(line, line + startcol);
4901 if (base_class != mb_get_class(line + startcol
4902 - head_off))
4903 break;
4904 startcol -= head_off;
4905 }
4906 }
4907 else
4908 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4909 ;
4910 compl_col += ++startcol;
4911 compl_length = (int)curs_col - startcol;
4912 if (compl_length == 1)
4913 {
4914 // Only match word with at least two chars -- webb
4915 // there's no need to call quote_meta,
4916 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004917 compl_pattern.string = alloc(7);
4918 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004919 {
John Marriott5e6ea922024-11-23 14:01:57 +01004920 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004921 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004922 }
John Marriott5e6ea922024-11-23 14:01:57 +01004923 STRCPY((char *)compl_pattern.string, "\\<");
4924 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4925 STRCAT((char *)compl_pattern.string, "\\k");
4926 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004927 }
4928 else
4929 {
John Marriott5e6ea922024-11-23 14:01:57 +01004930 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4931
4932 compl_pattern.string = alloc(n);
4933 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004934 {
John Marriott5e6ea922024-11-23 14:01:57 +01004935 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004936 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004937 }
John Marriott5e6ea922024-11-23 14:01:57 +01004938 STRCPY((char *)compl_pattern.string, "\\<");
4939 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004940 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004941 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004942 }
4943 }
4944
4945 return OK;
4946}
4947
4948/*
4949 * Get the pattern, column and length for whole line completion or for the
4950 * complete() function.
4951 * Sets the global variables: compl_col, compl_length and compl_pattern.
4952 */
4953 static int
4954get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4955{
4956 compl_col = (colnr_T)getwhitecols(line);
4957 compl_length = (int)curs_col - (int)compl_col;
4958 if (compl_length < 0) // cursor in indent: empty pattern
4959 compl_length = 0;
4960 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004961 {
John Marriott5e6ea922024-11-23 14:01:57 +01004962 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4963 NULL, 0);
4964 if (compl_pattern.string == NULL)
4965 {
4966 compl_pattern.length = 0;
4967 return FAIL;
4968 }
4969 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004970 }
John Marriott5e6ea922024-11-23 14:01:57 +01004971 else
4972 {
4973 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4974 if (compl_pattern.string == NULL)
4975 {
4976 compl_pattern.length = 0;
4977 return FAIL;
4978 }
4979 compl_pattern.length = (size_t)compl_length;
4980 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004981
4982 return OK;
4983}
4984
4985/*
4986 * Get the pattern, column and length for filename completion.
4987 * Sets the global variables: compl_col, compl_length and compl_pattern.
4988 */
4989 static int
4990get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4991{
4992 // Go back to just before the first filename character.
4993 if (startcol > 0)
4994 {
4995 char_u *p = line + startcol;
4996
4997 MB_PTR_BACK(line, p);
4998 while (p > line && vim_isfilec(PTR2CHAR(p)))
4999 MB_PTR_BACK(line, p);
5000 if (p == line && vim_isfilec(PTR2CHAR(p)))
5001 startcol = 0;
5002 else
5003 startcol = (int)(p - line) + 1;
5004 }
5005
5006 compl_col += startcol;
5007 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005008 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5009 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005010 {
John Marriott5e6ea922024-11-23 14:01:57 +01005011 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005012 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005013 }
5014
John Marriott5e6ea922024-11-23 14:01:57 +01005015 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005016
5017 return OK;
5018}
5019
5020/*
5021 * Get the pattern, column and length for command-line completion.
5022 * Sets the global variables: compl_col, compl_length and compl_pattern.
5023 */
5024 static int
5025get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5026{
John Marriott5e6ea922024-11-23 14:01:57 +01005027 compl_pattern.string = vim_strnsave(line, curs_col);
5028 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005029 {
John Marriott5e6ea922024-11-23 14:01:57 +01005030 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005031 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005032 }
John Marriott5e6ea922024-11-23 14:01:57 +01005033 compl_pattern.length = curs_col;
5034 set_cmd_context(&compl_xp, compl_pattern.string,
5035 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005036 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5037 || compl_xp.xp_context == EXPAND_NOTHING)
5038 // No completion possible, use an empty pattern to get a
5039 // "pattern not found" message.
5040 compl_col = curs_col;
5041 else
John Marriott5e6ea922024-11-23 14:01:57 +01005042 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005043 compl_length = curs_col - compl_col;
5044
5045 return OK;
5046}
5047
5048/*
5049 * Get the pattern, column and length for user defined completion ('omnifunc',
5050 * 'completefunc' and 'thesaurusfunc')
5051 * Sets the global variables: compl_col, compl_length and compl_pattern.
5052 * Uses the global variable: spell_bad_len
5053 */
5054 static int
5055get_userdefined_compl_info(colnr_T curs_col UNUSED)
5056{
5057 int ret = FAIL;
5058
5059#ifdef FEAT_COMPL_FUNC
5060 // Call user defined function 'completefunc' with "a:findstart"
5061 // set to 1 to obtain the length of text to use for completion.
5062 char_u *line;
5063 typval_T args[3];
5064 int col;
5065 char_u *funcname;
5066 pos_T pos;
5067 int save_State = State;
5068 callback_T *cb;
5069
5070 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5071 // length as a string
5072 funcname = get_complete_funcname(ctrl_x_mode);
5073 if (*funcname == NUL)
5074 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005075 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005076 ? "completefunc" : "omnifunc");
5077 return FAIL;
5078 }
5079
5080 args[0].v_type = VAR_NUMBER;
5081 args[0].vval.v_number = 1;
5082 args[1].v_type = VAR_STRING;
5083 args[1].vval.v_string = (char_u *)"";
5084 args[2].v_type = VAR_UNKNOWN;
5085 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005086 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005087 cb = get_insert_callback(ctrl_x_mode);
5088 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005089 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005090
5091 State = save_State;
5092 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005093 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005094 validate_cursor();
5095 if (!EQUAL_POS(curwin->w_cursor, pos))
5096 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005097 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005098 return FAIL;
5099 }
5100
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005101 // Return value -2 means the user complete function wants to cancel the
5102 // complete without an error, do the same if the function did not execute
5103 // successfully.
5104 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005105 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005106 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005107 if (col == -3)
5108 {
5109 ctrl_x_mode = CTRL_X_NORMAL;
5110 edit_submode = NULL;
5111 if (!shortmess(SHM_COMPLETIONMENU))
5112 msg_clr_cmdline();
5113 return FAIL;
5114 }
5115
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005116 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005117 // completion.
5118 compl_opt_refresh_always = FALSE;
5119 compl_opt_suppress_empty = FALSE;
5120
5121 if (col < 0)
5122 col = curs_col;
5123 compl_col = col;
5124 if (compl_col > curs_col)
5125 compl_col = curs_col;
5126
5127 // Setup variables for completion. Need to obtain "line" again,
5128 // it may have become invalid.
5129 line = ml_get(curwin->w_cursor.lnum);
5130 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005131 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5132 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005133 {
John Marriott5e6ea922024-11-23 14:01:57 +01005134 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005135 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005136 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005137
John Marriott5e6ea922024-11-23 14:01:57 +01005138 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005139 ret = OK;
5140#endif
5141
5142 return ret;
5143}
5144
5145/*
5146 * Get the pattern, column and length for spell completion.
5147 * Sets the global variables: compl_col, compl_length and compl_pattern.
5148 * Uses the global variable: spell_bad_len
5149 */
5150 static int
5151get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5152{
5153 int ret = FAIL;
5154#ifdef FEAT_SPELL
5155 char_u *line;
5156
5157 if (spell_bad_len > 0)
5158 compl_col = curs_col - spell_bad_len;
5159 else
5160 compl_col = spell_word_start(startcol);
5161 if (compl_col >= (colnr_T)startcol)
5162 {
5163 compl_length = 0;
5164 compl_col = curs_col;
5165 }
5166 else
5167 {
5168 spell_expand_check_cap(compl_col);
5169 compl_length = (int)curs_col - compl_col;
5170 }
5171 // Need to obtain "line" again, it may have become invalid.
5172 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005173 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5174 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005175 {
John Marriott5e6ea922024-11-23 14:01:57 +01005176 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005177 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005178 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005179
John Marriott5e6ea922024-11-23 14:01:57 +01005180 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005181 ret = OK;
5182#endif
5183
5184 return ret;
5185}
5186
5187/*
5188 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005189 * "startcol" - start column number of the completion pattern/text
5190 * "cur_col" - current cursor column
5191 * On return, "line_invalid" is set to TRUE, if the current line may have
5192 * become invalid and needs to be fetched again.
5193 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005194 */
5195 static int
5196compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5197{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005198 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005199 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5200 && !thesaurus_func_complete(ctrl_x_mode)))
5201 {
5202 return get_normal_compl_info(line, startcol, curs_col);
5203 }
5204 else if (ctrl_x_mode_line_or_eval())
5205 {
5206 return get_wholeline_compl_info(line, curs_col);
5207 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005208 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005209 {
5210 return get_filename_compl_info(line, startcol, curs_col);
5211 }
5212 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5213 {
5214 return get_cmdline_compl_info(line, curs_col);
5215 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005216 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005217 || thesaurus_func_complete(ctrl_x_mode))
5218 {
5219 if (get_userdefined_compl_info(curs_col) == FAIL)
5220 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005221 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005222 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005223 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005224 {
5225 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5226 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005227 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005228 }
5229 else
5230 {
5231 internal_error("ins_complete()");
5232 return FAIL;
5233 }
5234
5235 return OK;
5236}
5237
5238/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005239 * Continue an interrupted completion mode search in "line".
5240 *
5241 * If this same ctrl_x_mode has been interrupted use the text from
5242 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5243 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5244 * the same line as the cursor then fix it (the line has been split because it
5245 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5246 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005248 static void
5249ins_compl_continue_search(char_u *line)
5250{
5251 // it is a continued search
5252 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005253 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5254 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005255 {
5256 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5257 {
5258 // line (probably) wrapped, set compl_startpos to the
5259 // first non_blank in the line, if it is not a wordchar
5260 // include it to get a better pattern, but then we don't
5261 // want the "\\<" prefix, check it below
5262 compl_col = (colnr_T)getwhitecols(line);
5263 compl_startpos.col = compl_col;
5264 compl_startpos.lnum = curwin->w_cursor.lnum;
5265 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5266 }
5267 else
5268 {
5269 // S_IPOS was set when we inserted a word that was at the
5270 // beginning of the line, which means that we'll go to SOL
5271 // mode but first we need to redefine compl_startpos
5272 if (compl_cont_status & CONT_S_IPOS)
5273 {
5274 compl_cont_status |= CONT_SOL;
5275 compl_startpos.col = (colnr_T)(skipwhite(
5276 line + compl_length
5277 + compl_startpos.col) - line);
5278 }
5279 compl_col = compl_startpos.col;
5280 }
5281 compl_length = curwin->w_cursor.col - (int)compl_col;
5282 // IObuff is used to add a "word from the next line" would we
5283 // have enough space? just being paranoid
5284#define MIN_SPACE 75
5285 if (compl_length > (IOSIZE - MIN_SPACE))
5286 {
5287 compl_cont_status &= ~CONT_SOL;
5288 compl_length = (IOSIZE - MIN_SPACE);
5289 compl_col = curwin->w_cursor.col - compl_length;
5290 }
5291 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5292 if (compl_length < 1)
5293 compl_cont_status &= CONT_LOCAL;
5294 }
5295 else if (ctrl_x_mode_line_or_eval())
5296 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5297 else
5298 compl_cont_status = 0;
5299}
5300
5301/*
5302 * start insert mode completion
5303 */
5304 static int
5305ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005306{
5307 char_u *line;
5308 int startcol = 0; // column where searched text starts
5309 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005310 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005311 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005312 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005313
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005314 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005315
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005316 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005317 did_si = FALSE;
5318 can_si = FALSE;
5319 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005320 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005321 return FAIL;
5322
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005323 line = ml_get(curwin->w_cursor.lnum);
5324 curs_col = curwin->w_cursor.col;
5325 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005326
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005327 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5328 && compl_cont_mode == ctrl_x_mode)
5329 // this same ctrl-x_mode was interrupted previously. Continue the
5330 // completion.
5331 ins_compl_continue_search(line);
5332 else
5333 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005334
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005335 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005336 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005337 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005338 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005339 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5340 compl_cont_status = 0;
5341 compl_cont_status |= CONT_N_ADDS;
5342 compl_startpos = curwin->w_cursor;
5343 startcol = (int)curs_col;
5344 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005345 }
5346
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005347 // Work out completion pattern and original text -- webb
5348 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5349 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005350 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5351 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005352 // restore did_ai, so that adding comment leader works
5353 did_ai = save_did_ai;
5354 return FAIL;
5355 }
5356 // If "line" was changed while getting completion info get it again.
5357 if (line_invalid)
5358 line = ml_get(curwin->w_cursor.lnum);
5359
glepnir7cfe6932024-09-15 20:06:28 +02005360 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005361 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005362 {
5363 edit_submode_pre = (char_u *)_(" Adding");
5364 if (ctrl_x_mode_line_or_eval())
5365 {
5366 // Insert a new line, keep indentation but ignore 'comments'.
5367 char_u *old = curbuf->b_p_com;
5368
5369 curbuf->b_p_com = (char_u *)"";
5370 compl_startpos.lnum = curwin->w_cursor.lnum;
5371 compl_startpos.col = compl_col;
5372 ins_eol('\r');
5373 curbuf->b_p_com = old;
5374 compl_length = 0;
5375 compl_col = curwin->w_cursor.col;
5376 }
glepnir7cfe6932024-09-15 20:06:28 +02005377 else if (ctrl_x_mode_normal() && in_fuzzy)
5378 {
5379 compl_startpos = curwin->w_cursor;
5380 compl_cont_status &= CONT_S_IPOS;
5381 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005382 }
5383 else
5384 {
5385 edit_submode_pre = NULL;
5386 compl_startpos.col = compl_col;
5387 }
5388
5389 if (compl_cont_status & CONT_LOCAL)
5390 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5391 else
5392 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5393
5394 // If any of the original typed text has been changed we need to fix
5395 // the redo buffer.
5396 ins_compl_fixRedoBufForLeader(NULL);
5397
5398 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005399 VIM_CLEAR_STRING(compl_orig_text);
5400 compl_orig_text.length = (size_t)compl_length;
5401 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005402 if (p_ic)
5403 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005404 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5405 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005406 {
John Marriott5e6ea922024-11-23 14:01:57 +01005407 VIM_CLEAR_STRING(compl_pattern);
5408 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005409 return FAIL;
5410 }
5411
5412 // showmode might reset the internal line pointers, so it must
5413 // be called before line = ml_get(), or when this address is no
5414 // longer needed. -- Acevedo.
5415 edit_submode_extra = (char_u *)_("-- Searching...");
5416 edit_submode_highl = HLF_COUNT;
5417 showmode();
5418 edit_submode_extra = NULL;
5419 out_flush();
5420
5421 return OK;
5422}
5423
5424/*
5425 * display the completion status message
5426 */
5427 static void
5428ins_compl_show_statusmsg(void)
5429{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005430 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005431 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005432 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005433 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005434 ? (char_u *)_("Hit end of paragraph")
5435 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005436 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005437 }
5438
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005439 if (edit_submode_extra == NULL)
5440 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005441 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005442 {
5443 edit_submode_extra = (char_u *)_("Back at original");
5444 edit_submode_highl = HLF_W;
5445 }
5446 else if (compl_cont_status & CONT_S_IPOS)
5447 {
5448 edit_submode_extra = (char_u *)_("Word from other line");
5449 edit_submode_highl = HLF_COUNT;
5450 }
5451 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5452 {
5453 edit_submode_extra = (char_u *)_("The only match");
5454 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005455 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005456 }
5457 else
5458 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005459#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005460 // Update completion sequence number when needed.
5461 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005462 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005463#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005464 // The match should always have a sequence number now, this is
5465 // just a safety check.
5466 if (compl_curr_match->cp_number != -1)
5467 {
5468 // Space for 10 text chars. + 2x10-digit no.s = 31.
5469 // Translations may need more than twice that.
5470 static char_u match_ref[81];
5471
5472 if (compl_matches > 0)
5473 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005474 _("match %d of %d"),
5475 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005476 else
5477 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005478 _("match %d"),
5479 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005480 edit_submode_extra = match_ref;
5481 edit_submode_highl = HLF_R;
5482 if (dollar_vcol >= 0)
5483 curs_columns(FALSE);
5484 }
5485 }
5486 }
5487
5488 // Show a message about what (completion) mode we're in.
5489 if (!compl_opt_suppress_empty)
5490 {
5491 showmode();
5492 if (!shortmess(SHM_COMPLETIONMENU))
5493 {
5494 if (edit_submode_extra != NULL)
5495 {
5496 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005497 {
5498 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005499 msg_attr((char *)edit_submode_extra,
5500 edit_submode_highl < HLF_COUNT
5501 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005502 msg_hist_off = FALSE;
5503 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005504 }
5505 else
5506 msg_clr_cmdline(); // necessary for "noshowmode"
5507 }
5508 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005509}
5510
5511/*
5512 * Do Insert mode completion.
5513 * Called when character "c" was typed, which has a meaning for completion.
5514 * Returns OK if completion was done, FAIL if something failed (out of mem).
5515 */
5516 int
5517ins_complete(int c, int enable_pum)
5518{
5519 int n;
5520 int save_w_wrow;
5521 int save_w_leftcol;
5522 int insert_match;
5523
5524 compl_direction = ins_compl_key2dir(c);
5525 insert_match = ins_compl_use_match(c);
5526
5527 if (!compl_started)
5528 {
5529 if (ins_compl_start() == FAIL)
5530 return FAIL;
5531 }
5532 else if (insert_match && stop_arrow() == FAIL)
5533 return FAIL;
5534
5535 compl_shown_match = compl_curr_match;
5536 compl_shows_dir = compl_direction;
5537
5538 // Find next match (and following matches).
5539 save_w_wrow = curwin->w_wrow;
5540 save_w_leftcol = curwin->w_leftcol;
5541 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5542
5543 // may undisplay the popup menu
5544 ins_compl_upd_pum();
5545
5546 if (n > 1) // all matches have been found
5547 compl_matches = n;
5548 compl_curr_match = compl_shown_match;
5549 compl_direction = compl_shows_dir;
5550
5551 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5552 // mode.
5553 if (got_int && !global_busy)
5554 {
5555 (void)vgetc();
5556 got_int = FALSE;
5557 }
5558
5559 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005560 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005561 {
5562 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5563 // because we couldn't expand anything at first place, but if we used
5564 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5565 // (such as M in M'exico) if not tried already. -- Acevedo
5566 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005567 || compl_status_adding()
5568 || (ctrl_x_mode_not_default()
5569 && !ctrl_x_mode_path_patterns()
5570 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005571 compl_cont_status &= ~CONT_N_ADDS;
5572 }
5573
5574 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5575 compl_cont_status |= CONT_S_IPOS;
5576 else
5577 compl_cont_status &= ~CONT_S_IPOS;
5578
5579 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005580
5581 // Show the popup menu, unless we got interrupted.
5582 if (enable_pum && !compl_interrupted)
5583 show_pum(save_w_wrow, save_w_leftcol);
5584
5585 compl_was_interrupted = compl_interrupted;
5586 compl_interrupted = FALSE;
5587
5588 return OK;
5589}
5590
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005591/*
5592 * Remove (if needed) and show the popup menu
5593 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005594 static void
5595show_pum(int prev_w_wrow, int prev_w_leftcol)
5596{
5597 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005598 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005599 RedrawingDisabled = 0;
5600
5601 // If the cursor moved or the display scrolled we need to remove the pum
5602 // first.
5603 setcursor();
5604 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5605 ins_compl_del_pum();
5606
5607 ins_compl_show_pum();
5608 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005609
5610 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005611}
5612
5613/*
5614 * Looks in the first "len" chars. of "src" for search-metachars.
5615 * If dest is not NULL the chars. are copied there quoting (with
5616 * a backslash) the metachars, and dest would be NUL terminated.
5617 * Returns the length (needed) of dest
5618 */
5619 static unsigned
5620quote_meta(char_u *dest, char_u *src, int len)
5621{
5622 unsigned m = (unsigned)len + 1; // one extra for the NUL
5623
5624 for ( ; --len >= 0; src++)
5625 {
5626 switch (*src)
5627 {
5628 case '.':
5629 case '*':
5630 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005631 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005632 break;
5633 // FALLTHROUGH
5634 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005635 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005636 break;
5637 // FALLTHROUGH
5638 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005639 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005640 break;
5641 // FALLTHROUGH
5642 case '^': // currently it's not needed.
5643 case '$':
5644 m++;
5645 if (dest != NULL)
5646 *dest++ = '\\';
5647 break;
5648 }
5649 if (dest != NULL)
5650 *dest++ = *src;
5651 // Copy remaining bytes of a multibyte character.
5652 if (has_mbyte)
5653 {
5654 int i, mb_len;
5655
5656 mb_len = (*mb_ptr2len)(src) - 1;
5657 if (mb_len > 0 && len >= mb_len)
5658 for (i = 0; i < mb_len; ++i)
5659 {
5660 --len;
5661 ++src;
5662 if (dest != NULL)
5663 *dest++ = *src;
5664 }
5665 }
5666 }
5667 if (dest != NULL)
5668 *dest = NUL;
5669
5670 return m;
5671}
5672
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005673#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005674 void
5675free_insexpand_stuff(void)
5676{
John Marriott5e6ea922024-11-23 14:01:57 +01005677 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005678# ifdef FEAT_EVAL
5679 free_callback(&cfu_cb);
5680 free_callback(&ofu_cb);
5681 free_callback(&tsrfu_cb);
5682# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005683}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005684#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005685
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005686#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005687/*
5688 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5689 * spelled word, if there is one.
5690 */
5691 static void
5692spell_back_to_badword(void)
5693{
5694 pos_T tpos = curwin->w_cursor;
5695
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005696 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005697 if (curwin->w_cursor.col != tpos.col)
5698 start_arrow(&tpos);
5699}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005700#endif