blob: e33e15d43bca00268b839a2069f17470fa19af17 [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;
1267 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001268 compl_T *match_head = NULL;
1269 compl_T *match_tail = NULL;
1270 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001271
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001272 // Need to build the popup menu list.
1273 compl_match_arraysize = 0;
1274 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001275
glepnira49c0772024-11-30 10:56:30 +01001276 // If the current match is the original text don't find the first
1277 // match after it, don't highlight anything.
1278 if (match_at_original_text(compl_shown_match))
1279 shown_match_ok = TRUE;
1280
1281 if (compl_leader.string != NULL
1282 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1283 && shown_match_ok == FALSE)
1284 compl_shown_match = compl_no_select ? compl_first_match
1285 : compl_first_match->cp_next;
1286
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001287 do
1288 {
glepnird4088ed2024-12-31 10:55:22 +01001289 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001290 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1291 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001292 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1293 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001294
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001295 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001296 && (compl_leader.string == NULL
1297 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001298 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001299 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001300 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001301 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001302 if (match_head == NULL)
1303 match_head = compl;
1304 else
glepnira49c0772024-11-30 10:56:30 +01001305 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001306 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001307
1308 if (!shown_match_ok && !compl_fuzzy_match)
1309 {
1310 if (compl == compl_shown_match || did_find_shown_match)
1311 {
1312 // This item is the shown match or this is the
1313 // first displayed item after the shown match.
1314 compl_shown_match = compl;
1315 did_find_shown_match = TRUE;
1316 shown_match_ok = TRUE;
1317 }
1318 else
1319 // Remember this displayed match for when the
1320 // shown match is just below it.
1321 shown_compl = compl;
1322 cur = i;
1323 }
1324 else if (compl_fuzzy_match)
1325 {
1326 if (i == 0)
1327 shown_compl = compl;
1328 // Update the maximum fuzzy score and the shown match
1329 // if the current item's score is higher
1330 if (compl->cp_score > max_fuzzy_score)
1331 {
1332 did_find_shown_match = TRUE;
1333 max_fuzzy_score = compl->cp_score;
1334 if (!compl_no_select)
1335 compl_shown_match = compl;
1336 }
1337
1338 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1339 {
1340 cur = i;
1341 shown_match_ok = TRUE;
1342 }
1343 }
1344 i++;
glepnir80b66202024-11-27 21:53:53 +01001345 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001346
glepnira218cc62024-06-03 19:32:39 +02001347 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 {
1349 did_find_shown_match = TRUE;
1350
1351 // When the original text is the shown match don't set
1352 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001353 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001354 shown_match_ok = TRUE;
1355
1356 if (!shown_match_ok && shown_compl != NULL)
1357 {
1358 // The shown match isn't displayed, set it to the
1359 // previously displayed match.
1360 compl_shown_match = shown_compl;
1361 shown_match_ok = TRUE;
1362 }
1363 }
glepnira49c0772024-11-30 10:56:30 +01001364 compl = compl->cp_next;
1365 } while (compl != NULL && !is_first_match(compl));
1366
1367 if (compl_match_arraysize == 0)
1368 return -1;
1369
1370 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1371 if (compl_match_array == NULL)
1372 return -1;
1373
1374 compl = match_head;
1375 i = 0;
1376 while (compl != NULL)
1377 {
glepnir6e199932024-12-14 21:13:27 +01001378 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1379 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001380 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1381 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1382 compl_match_array[i].pum_score = compl->cp_score;
1383 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1384 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001385 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1386 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001387 match_next = compl->cp_match_next;
1388 compl->cp_match_next = NULL;
1389 compl = match_next;
1390 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001391
John Marriott5e6ea922024-11-23 14:01:57 +01001392 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001393 {
1394 for (i = 0; i < compl_match_arraysize; i++)
1395 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001396 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001397 qsort(compl_match_array, (size_t)compl_match_arraysize,
1398 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001399 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001400 }
glepnira218cc62024-06-03 19:32:39 +02001401
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001402 if (!shown_match_ok) // no displayed match at all
1403 cur = -1;
1404
1405 return cur;
1406}
1407
1408/*
1409 * Show the popup menu for the list of matches.
1410 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1411 */
1412 void
1413ins_compl_show_pum(void)
1414{
1415 int i;
1416 int cur = -1;
1417 colnr_T col;
1418
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001419 if (!pum_wanted() || !pum_enough_matches())
1420 return;
1421
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 // Update the screen later, before drawing the popup menu over it.
1423 pum_call_update_screen();
1424
1425 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001426 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001427 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001428 else
1429 {
1430 // popup menu already exists, only need to find the current item.
1431 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001432 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001433 || compl_match_array[i].pum_text
1434 == compl_shown_match->cp_text[CPT_ABBR])
1435 {
1436 cur = i;
1437 break;
1438 }
1439 }
1440
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001441 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001442 {
1443#ifdef FEAT_EVAL
1444 if (compl_started && has_completechanged())
1445 trigger_complete_changed_event(cur);
1446#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001447 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001448 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001449
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001450 // In Replace mode when a $ is displayed at the end of the line only
1451 // part of the screen would be updated. We do need to redraw here.
1452 dollar_vcol = -1;
1453
1454 // Compute the screen column of the start of the completed text.
1455 // Use the cursor to get all wrapping and other settings right.
1456 col = curwin->w_cursor.col;
1457 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001458 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001459 pum_display(compl_match_array, compl_match_arraysize, cur);
1460 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001461
glepnircbb46b42024-02-03 18:11:13 +01001462 // After adding leader, set the current match to shown match.
1463 if (compl_started && compl_curr_match != compl_shown_match)
1464 compl_curr_match = compl_shown_match;
1465
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001466#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001467 if (has_completechanged())
1468 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001469#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001470}
1471
1472#define DICT_FIRST (1) // use just first element in "dict"
1473#define DICT_EXACT (2) // "dict" is the exact name of a file
1474
1475/*
glepnir40c1c332024-06-11 19:37:04 +02001476 * Get current completion leader
1477 */
1478 char_u *
1479ins_compl_leader(void)
1480{
John Marriott5e6ea922024-11-23 14:01:57 +01001481 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1482}
1483
1484/*
1485 * Get current completion leader length
1486 */
1487 size_t
1488ins_compl_leader_len(void)
1489{
1490 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001491}
1492
1493/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001494 * Add any identifiers that match the given pattern "pat" in the list of
1495 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001496 */
1497 static void
1498ins_compl_dictionaries(
1499 char_u *dict_start,
1500 char_u *pat,
1501 int flags, // DICT_FIRST and/or DICT_EXACT
1502 int thesaurus) // Thesaurus completion
1503{
1504 char_u *dict = dict_start;
1505 char_u *ptr;
1506 char_u *buf;
1507 regmatch_T regmatch;
1508 char_u **files;
1509 int count;
1510 int save_p_scs;
1511 int dir = compl_direction;
1512
1513 if (*dict == NUL)
1514 {
1515#ifdef FEAT_SPELL
1516 // When 'dictionary' is empty and spell checking is enabled use
1517 // "spell".
1518 if (!thesaurus && curwin->w_p_spell)
1519 dict = (char_u *)"spell";
1520 else
1521#endif
1522 return;
1523 }
1524
1525 buf = alloc(LSIZE);
1526 if (buf == NULL)
1527 return;
1528 regmatch.regprog = NULL; // so that we can goto theend
1529
1530 // If 'infercase' is set, don't use 'smartcase' here
1531 save_p_scs = p_scs;
1532 if (curbuf->b_p_inf)
1533 p_scs = FALSE;
1534
1535 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1536 // to only match at the start of a line. Otherwise just match the
1537 // pattern. Also need to double backslashes.
1538 if (ctrl_x_mode_line_or_eval())
1539 {
1540 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1541 size_t len;
1542
1543 if (pat_esc == NULL)
1544 goto theend;
1545 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001546 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001547 if (ptr == NULL)
1548 {
1549 vim_free(pat_esc);
1550 goto theend;
1551 }
1552 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1553 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1554 vim_free(pat_esc);
1555 vim_free(ptr);
1556 }
1557 else
1558 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001559 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001560 if (regmatch.regprog == NULL)
1561 goto theend;
1562 }
1563
1564 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1565 regmatch.rm_ic = ignorecase(pat);
1566 while (*dict != NUL && !got_int && !compl_interrupted)
1567 {
1568 // copy one dictionary file name into buf
1569 if (flags == DICT_EXACT)
1570 {
1571 count = 1;
1572 files = &dict;
1573 }
1574 else
1575 {
1576 // Expand wildcards in the dictionary name, but do not allow
1577 // backticks (for security, the 'dict' option may have been set in
1578 // a modeline).
1579 copy_option_part(&dict, buf, LSIZE, ",");
1580# ifdef FEAT_SPELL
1581 if (!thesaurus && STRCMP(buf, "spell") == 0)
1582 count = -1;
1583 else
1584# endif
1585 if (vim_strchr(buf, '`') != NULL
1586 || expand_wildcards(1, &buf, &count, &files,
1587 EW_FILE|EW_SILENT) != OK)
1588 count = 0;
1589 }
1590
1591# ifdef FEAT_SPELL
1592 if (count == -1)
1593 {
1594 // Complete from active spelling. Skip "\<" in the pattern, we
1595 // don't use it as a RE.
1596 if (pat[0] == '\\' && pat[1] == '<')
1597 ptr = pat + 2;
1598 else
1599 ptr = pat;
1600 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1601 }
1602 else
1603# endif
1604 if (count > 0) // avoid warning for using "files" uninit
1605 {
1606 ins_compl_files(count, files, thesaurus, flags,
1607 &regmatch, buf, &dir);
1608 if (flags != DICT_EXACT)
1609 FreeWild(count, files);
1610 }
1611 if (flags != 0)
1612 break;
1613 }
1614
1615theend:
1616 p_scs = save_p_scs;
1617 vim_regfree(regmatch.regprog);
1618 vim_free(buf);
1619}
1620
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001621/*
1622 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1623 * skipping the word at 'skip_word'. Returns OK on success.
1624 */
1625 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001626thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001627 char_u *fname,
1628 char_u **buf_arg,
1629 int dir,
1630 char_u *skip_word)
1631{
1632 int status = OK;
1633 char_u *ptr;
1634 char_u *wstart;
1635
1636 // Add the other matches on the line
1637 ptr = *buf_arg;
1638 while (!got_int)
1639 {
1640 // Find start of the next word. Skip white
1641 // space and punctuation.
1642 ptr = find_word_start(ptr);
1643 if (*ptr == NUL || *ptr == NL)
1644 break;
1645 wstart = ptr;
1646
1647 // Find end of the word.
1648 if (has_mbyte)
1649 // Japanese words may have characters in
1650 // different classes, only separate words
1651 // with single-byte non-word characters.
1652 while (*ptr != NUL)
1653 {
1654 int l = (*mb_ptr2len)(ptr);
1655
1656 if (l < 2 && !vim_iswordc(*ptr))
1657 break;
1658 ptr += l;
1659 }
1660 else
1661 ptr = find_word_end(ptr);
1662
1663 // Add the word. Skip the regexp match.
1664 if (wstart != skip_word)
1665 {
1666 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1667 fname, dir, FALSE);
1668 if (status == FAIL)
1669 break;
1670 }
1671 }
1672
1673 *buf_arg = ptr;
1674 return status;
1675}
1676
1677/*
1678 * Process "count" dictionary/thesaurus "files" and add the text matching
1679 * "regmatch".
1680 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 static void
1682ins_compl_files(
1683 int count,
1684 char_u **files,
1685 int thesaurus,
1686 int flags,
1687 regmatch_T *regmatch,
1688 char_u *buf,
1689 int *dir)
1690{
1691 char_u *ptr;
1692 int i;
1693 FILE *fp;
1694 int add_r;
1695
1696 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1697 {
1698 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001699 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001700 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001701 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001702 vim_snprintf((char *)IObuff, IOSIZE,
1703 _("Scanning dictionary: %s"), (char *)files[i]);
1704 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1705 }
1706
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001707 if (fp == NULL)
1708 continue;
1709
1710 // Read dictionary file line by line.
1711 // Check each line for a match.
1712 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 ptr = buf;
1715 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001716 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001717 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001718 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1719 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 add_r = ins_compl_add_infercase(regmatch->startp[0],
1721 (int)(ptr - regmatch->startp[0]),
1722 p_ic, files[i], *dir, FALSE);
1723 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001724 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001725 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001726 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001727 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001728 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001729 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001730 if (add_r == OK)
1731 // if dir was BACKWARD then honor it just once
1732 *dir = FORWARD;
1733 else if (add_r == FAIL)
1734 break;
1735 // avoid expensive call to vim_regexec() when at end
1736 // of line
1737 if (*ptr == '\n' || got_int)
1738 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001739 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001740 line_breakcheck();
1741 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001742 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001743 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001744 }
1745}
1746
1747/*
1748 * Find the start of the next word.
1749 * Returns a pointer to the first char of the word. Also stops at a NUL.
1750 */
1751 char_u *
1752find_word_start(char_u *ptr)
1753{
1754 if (has_mbyte)
1755 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1756 ptr += (*mb_ptr2len)(ptr);
1757 else
1758 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1759 ++ptr;
1760 return ptr;
1761}
1762
1763/*
1764 * Find the end of the word. Assumes it starts inside a word.
1765 * Returns a pointer to just after the word.
1766 */
1767 char_u *
1768find_word_end(char_u *ptr)
1769{
1770 int start_class;
1771
1772 if (has_mbyte)
1773 {
1774 start_class = mb_get_class(ptr);
1775 if (start_class > 1)
1776 while (*ptr != NUL)
1777 {
1778 ptr += (*mb_ptr2len)(ptr);
1779 if (mb_get_class(ptr) != start_class)
1780 break;
1781 }
1782 }
1783 else
1784 while (vim_iswordc(*ptr))
1785 ++ptr;
1786 return ptr;
1787}
1788
1789/*
1790 * Find the end of the line, omitting CR and NL at the end.
1791 * Returns a pointer to just after the line.
1792 */
1793 static char_u *
1794find_line_end(char_u *ptr)
1795{
1796 char_u *s;
1797
1798 s = ptr + STRLEN(ptr);
1799 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1800 --s;
1801 return s;
1802}
1803
1804/*
1805 * Free the list of completions
1806 */
1807 static void
1808ins_compl_free(void)
1809{
1810 compl_T *match;
1811 int i;
1812
John Marriott5e6ea922024-11-23 14:01:57 +01001813 VIM_CLEAR_STRING(compl_pattern);
1814 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001815
1816 if (compl_first_match == NULL)
1817 return;
1818
1819 ins_compl_del_pum();
1820 pum_clear();
1821
1822 compl_curr_match = compl_first_match;
1823 do
1824 {
1825 match = compl_curr_match;
1826 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001827 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001828 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001829 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001830 vim_free(match->cp_fname);
1831 for (i = 0; i < CPT_COUNT; ++i)
1832 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001833#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001834 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001835#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001836 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001837 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001838 compl_first_match = compl_curr_match = NULL;
1839 compl_shown_match = NULL;
1840 compl_old_match = NULL;
1841}
1842
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001843/*
1844 * Reset/clear the completion state.
1845 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001846 void
1847ins_compl_clear(void)
1848{
1849 compl_cont_status = 0;
1850 compl_started = FALSE;
1851 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001852 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001853 VIM_CLEAR_STRING(compl_pattern);
1854 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001855 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001856 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001857 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001858#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001859 // clear v:completed_item
1860 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001861#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001862}
1863
1864/*
1865 * Return TRUE when Insert completion is active.
1866 */
1867 int
1868ins_compl_active(void)
1869{
1870 return compl_started;
1871}
1872
1873/*
glepnir8d0bb6d2024-12-24 09:44:35 +01001874 * Return True when wp is the actual completion window
1875 */
1876 int
1877ins_compl_win_active(win_T *wp UNUSED)
1878{
1879 return ins_compl_active()
1880#if defined(FEAT_QUICKFIX)
1881 && (!wp->w_p_pvw
1882# ifdef FEAT_PROP_POPUP
1883 && !(wp->w_popup_flags & POPF_INFO)
1884# endif
1885 )
1886#endif
1887 ;
1888}
1889
1890/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001891 * Selected one of the matches. When FALSE the match was edited or using the
1892 * longest common string.
1893 */
1894 int
1895ins_compl_used_match(void)
1896{
1897 return compl_used_match;
1898}
1899
1900/*
1901 * Initialize get longest common string.
1902 */
1903 void
1904ins_compl_init_get_longest(void)
1905{
1906 compl_get_longest = FALSE;
1907}
1908
1909/*
1910 * Returns TRUE when insert completion is interrupted.
1911 */
1912 int
1913ins_compl_interrupted(void)
1914{
1915 return compl_interrupted;
1916}
1917
1918/*
1919 * Returns TRUE if the <Enter> key selects a match in the completion popup
1920 * menu.
1921 */
1922 int
1923ins_compl_enter_selects(void)
1924{
1925 return compl_enter_selects;
1926}
1927
1928/*
1929 * Return the column where the text starts that is being completed
1930 */
1931 colnr_T
1932ins_compl_col(void)
1933{
1934 return compl_col;
1935}
1936
1937/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001938 * Return the length in bytes of the text being completed
1939 */
1940 int
1941ins_compl_len(void)
1942{
1943 return compl_length;
1944}
1945
1946/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001947 * Delete one character before the cursor and show the subset of the matches
1948 * that match the word that is now before the cursor.
1949 * Returns the character to be used, NUL if the work is done and another char
1950 * to be got from the user.
1951 */
1952 int
1953ins_compl_bs(void)
1954{
1955 char_u *line;
1956 char_u *p;
1957
1958 line = ml_get_curline();
1959 p = line + curwin->w_cursor.col;
1960 MB_PTR_BACK(line, p);
1961
1962 // Stop completion when the whole word was deleted. For Omni completion
1963 // allow the word to be deleted, we won't match everything.
1964 // Respect the 'backspace' option.
1965 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001966 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1967 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001968 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1969 - compl_length < 0))
1970 return K_BS;
1971
1972 // Deleted more than what was used to find matches or didn't finish
1973 // finding all matches: need to look for matches all over again.
1974 if (curwin->w_cursor.col <= compl_col + compl_length
1975 || ins_compl_need_restart())
1976 ins_compl_restart();
1977
John Marriott5e6ea922024-11-23 14:01:57 +01001978 VIM_CLEAR_STRING(compl_leader);
1979 compl_leader.length = (size_t)((p - line) - compl_col);
1980 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1981 if (compl_leader.string == NULL)
1982 {
1983 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001984 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001985 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001986
1987 ins_compl_new_leader();
1988 if (compl_shown_match != NULL)
1989 // Make sure current match is not a hidden item.
1990 compl_curr_match = compl_shown_match;
1991 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992}
1993
1994/*
1995 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1996 * be called.
1997 */
1998 static int
1999ins_compl_need_restart(void)
2000{
2001 // Return TRUE if we didn't complete finding matches or when the
2002 // 'completefunc' returned "always" in the "refresh" dictionary item.
2003 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002004 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002005 && compl_opt_refresh_always);
2006}
2007
2008/*
2009 * Called after changing "compl_leader".
2010 * Show the popup menu with a different set of matches.
2011 * May also search for matches again if the previous search was interrupted.
2012 */
2013 static void
2014ins_compl_new_leader(void)
2015{
2016 ins_compl_del_pum();
2017 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002018 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002019 compl_used_match = FALSE;
2020
2021 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002022 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 else
2024 {
2025#ifdef FEAT_SPELL
2026 spell_bad_len = 0; // need to redetect bad word
2027#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002028 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002029 // the popup menu display the changed text before the cursor. Set
2030 // "compl_restarting" to avoid that the first match is inserted.
2031 pum_call_update_screen();
2032#ifdef FEAT_GUI
2033 if (gui.in_use)
2034 {
2035 // Show the cursor after the match, not after the redrawn text.
2036 setcursor();
2037 out_flush_cursor(FALSE, FALSE);
2038 }
2039#endif
2040 compl_restarting = TRUE;
2041 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2042 compl_cont_status = 0;
2043 compl_restarting = FALSE;
2044 }
2045
2046 compl_enter_selects = !compl_used_match;
2047
2048 // Show the popup menu with a different set of matches.
2049 ins_compl_show_pum();
2050
2051 // Don't let Enter select the original text when there is no popup menu.
2052 if (compl_match_array == NULL)
2053 compl_enter_selects = FALSE;
2054}
2055
2056/*
2057 * Return the length of the completion, from the completion start column to
2058 * the cursor column. Making sure it never goes below zero.
2059 */
2060 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002061get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002062{
2063 int off = (int)curwin->w_cursor.col - (int)compl_col;
2064
2065 if (off < 0)
2066 return 0;
2067 return off;
2068}
2069
2070/*
2071 * Append one character to the match leader. May reduce the number of
2072 * matches.
2073 */
2074 void
2075ins_compl_addleader(int c)
2076{
2077 int cc;
2078
2079 if (stop_arrow() == FAIL)
2080 return;
2081 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2082 {
2083 char_u buf[MB_MAXBYTES + 1];
2084
2085 (*mb_char2bytes)(c, buf);
2086 buf[cc] = NUL;
2087 ins_char_bytes(buf, cc);
2088 if (compl_opt_refresh_always)
2089 AppendToRedobuff(buf);
2090 }
2091 else
2092 {
2093 ins_char(c);
2094 if (compl_opt_refresh_always)
2095 AppendCharToRedobuff(c);
2096 }
2097
2098 // If we didn't complete finding matches we must search again.
2099 if (ins_compl_need_restart())
2100 ins_compl_restart();
2101
2102 // When 'always' is set, don't reset compl_leader. While completing,
2103 // cursor doesn't point original position, changing compl_leader would
2104 // break redo.
2105 if (!compl_opt_refresh_always)
2106 {
John Marriott5e6ea922024-11-23 14:01:57 +01002107 VIM_CLEAR_STRING(compl_leader);
2108 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2109 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2110 compl_leader.length);
2111 if (compl_leader.string == NULL)
2112 {
2113 compl_leader.length = 0;
2114 return;
2115 }
2116
2117 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002118 }
2119}
2120
2121/*
2122 * Setup for finding completions again without leaving CTRL-X mode. Used when
2123 * BS or a key was typed while still searching for matches.
2124 */
2125 static void
2126ins_compl_restart(void)
2127{
2128 ins_compl_free();
2129 compl_started = FALSE;
2130 compl_matches = 0;
2131 compl_cont_status = 0;
2132 compl_cont_mode = 0;
2133}
2134
2135/*
2136 * Set the first match, the original text.
2137 */
2138 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002139ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002140{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002142 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2143 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002144 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002145 {
John Marriott5e6ea922024-11-23 14:01:57 +01002146 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002147 if (p != NULL)
2148 {
John Marriott5e6ea922024-11-23 14:01:57 +01002149 VIM_CLEAR_STRING(compl_first_match->cp_str);
2150 compl_first_match->cp_str.string = p;
2151 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002152 }
2153 }
2154 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002155 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002156 {
John Marriott5e6ea922024-11-23 14:01:57 +01002157 char_u *p = vim_strnsave(str, len);
2158 if (p != NULL)
2159 {
2160 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2161 compl_first_match->cp_prev->cp_str.string = p;
2162 compl_first_match->cp_prev->cp_str.length = len;
2163 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002164 }
2165}
2166
2167/*
2168 * Append one character to the match leader. May reduce the number of
2169 * matches.
2170 */
2171 void
2172ins_compl_addfrommatch(void)
2173{
2174 char_u *p;
2175 int len = (int)curwin->w_cursor.col - (int)compl_col;
2176 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002177
John Marriott5e6ea922024-11-23 14:01:57 +01002178 p = compl_shown_match->cp_str.string;
2179 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002180 {
John Marriott5e6ea922024-11-23 14:01:57 +01002181 size_t plen;
2182 compl_T *cp;
2183
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002184 // When still at the original match use the first entry that matches
2185 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002186 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002187 return;
2188
2189 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002190 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002191 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002192 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002193 {
John Marriott5e6ea922024-11-23 14:01:57 +01002194 if (compl_leader.string == NULL
2195 || ins_compl_equal(cp, compl_leader.string,
2196 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002197 {
John Marriott5e6ea922024-11-23 14:01:57 +01002198 p = cp->cp_str.string;
2199 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002200 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002201 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002202 }
John Marriott5e6ea922024-11-23 14:01:57 +01002203 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002204 return;
2205 }
2206 p += len;
2207 c = PTR2CHAR(p);
2208 ins_compl_addleader(c);
2209}
2210
2211/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002212 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002213 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2214 * compl_cont_mode and compl_cont_status.
2215 * Returns TRUE when the character is not to be inserted.
2216 */
2217 static int
2218set_ctrl_x_mode(int c)
2219{
2220 int retval = FALSE;
2221
2222 switch (c)
2223 {
2224 case Ctrl_E:
2225 case Ctrl_Y:
2226 // scroll the window one line up or down
2227 ctrl_x_mode = CTRL_X_SCROLL;
2228 if (!(State & REPLACE_FLAG))
2229 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2230 else
2231 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2232 edit_submode_pre = NULL;
2233 showmode();
2234 break;
2235 case Ctrl_L:
2236 // complete whole line
2237 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2238 break;
2239 case Ctrl_F:
2240 // complete filenames
2241 ctrl_x_mode = CTRL_X_FILES;
2242 break;
2243 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002244 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002245 ctrl_x_mode = CTRL_X_DICTIONARY;
2246 break;
2247 case Ctrl_R:
2248 // Register insertion without exiting CTRL-X mode
2249 // Simply allow ^R to happen without affecting ^X mode
2250 break;
2251 case Ctrl_T:
2252 // complete words from a thesaurus
2253 ctrl_x_mode = CTRL_X_THESAURUS;
2254 break;
2255#ifdef FEAT_COMPL_FUNC
2256 case Ctrl_U:
2257 // user defined completion
2258 ctrl_x_mode = CTRL_X_FUNCTION;
2259 break;
2260 case Ctrl_O:
2261 // omni completion
2262 ctrl_x_mode = CTRL_X_OMNI;
2263 break;
2264#endif
2265 case 's':
2266 case Ctrl_S:
2267 // complete spelling suggestions
2268 ctrl_x_mode = CTRL_X_SPELL;
2269#ifdef FEAT_SPELL
2270 ++emsg_off; // Avoid getting the E756 error twice.
2271 spell_back_to_badword();
2272 --emsg_off;
2273#endif
2274 break;
2275 case Ctrl_RSB:
2276 // complete tag names
2277 ctrl_x_mode = CTRL_X_TAGS;
2278 break;
2279#ifdef FEAT_FIND_ID
2280 case Ctrl_I:
2281 case K_S_TAB:
2282 // complete keywords from included files
2283 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2284 break;
2285 case Ctrl_D:
2286 // complete definitions from included files
2287 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2288 break;
2289#endif
2290 case Ctrl_V:
2291 case Ctrl_Q:
2292 // complete vim commands
2293 ctrl_x_mode = CTRL_X_CMDLINE;
2294 break;
2295 case Ctrl_Z:
2296 // stop completion
2297 ctrl_x_mode = CTRL_X_NORMAL;
2298 edit_submode = NULL;
2299 showmode();
2300 retval = TRUE;
2301 break;
2302 case Ctrl_P:
2303 case Ctrl_N:
2304 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2305 // just started ^X mode, or there were enough ^X's to cancel
2306 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2307 // do normal expansion when interrupting a different mode (say
2308 // ^X^F^X^P or ^P^X^X^P, see below)
2309 // nothing changes if interrupting mode 0, (eg, the flag
2310 // doesn't change when going to ADDING mode -- Acevedo
2311 if (!(compl_cont_status & CONT_INTRPT))
2312 compl_cont_status |= CONT_LOCAL;
2313 else if (compl_cont_mode != 0)
2314 compl_cont_status &= ~CONT_LOCAL;
2315 // FALLTHROUGH
2316 default:
2317 // If we have typed at least 2 ^X's... for modes != 0, we set
2318 // compl_cont_status = 0 (eg, as if we had just started ^X
2319 // mode).
2320 // For mode 0, we set "compl_cont_mode" to an impossible
2321 // value, in both cases ^X^X can be used to restart the same
2322 // mode (avoiding ADDING mode).
2323 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2324 // 'complete' and local ^P expansions respectively.
2325 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2326 // mode -- Acevedo
2327 if (c == Ctrl_X)
2328 {
2329 if (compl_cont_mode != 0)
2330 compl_cont_status = 0;
2331 else
2332 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2333 }
2334 ctrl_x_mode = CTRL_X_NORMAL;
2335 edit_submode = NULL;
2336 showmode();
2337 break;
2338 }
2339
2340 return retval;
2341}
2342
2343/*
glepnir1c5a1202024-12-04 20:27:34 +01002344 * Trigger CompleteDone event and adds relevant information to v:event
2345 */
2346 static void
2347trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2348{
2349#if defined(FEAT_EVAL)
2350 save_v_event_T save_v_event;
2351 dict_T *v_event = get_v_event(&save_v_event);
2352 char_u *mode_str = NULL;
2353
2354 mode = mode & ~CTRL_X_WANT_IDENT;
2355 if (ctrl_x_mode_names[mode])
2356 mode_str = (char_u *)ctrl_x_mode_names[mode];
2357
2358 (void)dict_add_string(v_event, "complete_word",
2359 word == NULL ? (char_u *)"" : word);
2360 (void)dict_add_string(v_event, "complete_type",
2361 mode_str != NULL ? mode_str : (char_u *)"");
2362
2363 dict_set_items_ro(v_event);
2364#endif
2365 ins_apply_autocmds(EVENT_COMPLETEDONE);
2366
2367#if defined(FEAT_EVAL)
2368 restore_v_event(v_event, &save_v_event);
2369#endif
2370}
2371
2372/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002373 * Stop insert completion mode
2374 */
2375 static int
2376ins_compl_stop(int c, int prev_mode, int retval)
2377{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002378 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002379 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002380
2381 // Get here when we have finished typing a sequence of ^N and
2382 // ^P or other completion characters in CTRL-X mode. Free up
2383 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002384 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002385 {
John Marriott5e6ea922024-11-23 14:01:57 +01002386 char_u *ptr;
2387
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002388 // If any of the original typed text has been changed, eg when
2389 // ignorecase is set, we must add back-spaces to the redo
2390 // buffer. We add as few as necessary to delete just the part
2391 // of the original text that has changed.
2392 // When using the longest match, edited the match or used
2393 // CTRL-E then don't use the current match.
2394 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002395 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002396 else
2397 ptr = NULL;
2398 ins_compl_fixRedoBufForLeader(ptr);
2399 }
2400
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002401 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002402
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002403 // When completing whole lines: fix indent for 'cindent'.
2404 // Otherwise, break line if it's too long.
2405 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2406 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002407 // re-indent the current line
2408 if (want_cindent)
2409 {
2410 do_c_expr_indent();
2411 want_cindent = FALSE; // don't do it again
2412 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002413 }
2414 else
2415 {
2416 int prev_col = curwin->w_cursor.col;
2417
2418 // put the cursor on the last char, for 'tw' formatting
2419 if (prev_col > 0)
2420 dec_cursor();
2421 // only format when something was inserted
2422 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2423 insertchar(NUL, 0, -1);
2424 if (prev_col > 0
2425 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2426 inc_cursor();
2427 }
2428
2429 // If the popup menu is displayed pressing CTRL-Y means accepting
2430 // the selection without inserting anything. When
2431 // compl_enter_selects is set the Enter key does the same.
2432 if ((c == Ctrl_Y || (compl_enter_selects
2433 && (c == CAR || c == K_KENTER || c == NL)))
2434 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002435 {
2436 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002437 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002438 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002439
2440 // CTRL-E means completion is Ended, go back to the typed text.
2441 // but only do this, if the Popup is still visible
2442 if (c == Ctrl_E)
2443 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002444 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002445 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002446
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002447 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002448 if (compl_leader.string != NULL)
2449 {
2450 p = compl_leader.string;
2451 plen = compl_leader.length;
2452 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002453 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002454 {
2455 p = compl_orig_text.string;
2456 plen = compl_orig_text.length;
2457 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002458 if (p != NULL)
2459 {
2460 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002461
John Marriott5e6ea922024-11-23 14:01:57 +01002462 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002463 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002464 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002465 retval = TRUE;
2466 }
2467
2468 auto_format(FALSE, TRUE);
2469
2470 // Trigger the CompleteDonePre event to give scripts a chance to
2471 // act upon the completion before clearing the info, and restore
2472 // ctrl_x_mode, so that complete_info() can be used.
2473 ctrl_x_mode = prev_mode;
2474 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2475
2476 ins_compl_free();
2477 compl_started = FALSE;
2478 compl_matches = 0;
2479 if (!shortmess(SHM_COMPLETIONMENU))
2480 msg_clr_cmdline(); // necessary for "noshowmode"
2481 ctrl_x_mode = CTRL_X_NORMAL;
2482 compl_enter_selects = FALSE;
2483 if (edit_submode != NULL)
2484 {
2485 edit_submode = NULL;
2486 showmode();
2487 }
2488
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002489 if (c == Ctrl_C && cmdwin_type != 0)
2490 // Avoid the popup menu remains displayed when leaving the
2491 // command line window.
2492 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002493 // Indent now if a key was typed that is in 'cinkeys'.
2494 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2495 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002496 // Trigger the CompleteDone event to give scripts a chance to act
2497 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002498 trigger_complete_done_event(prev_mode, word);
2499 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002500
2501 return retval;
2502}
2503
2504/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002505 * Prepare for Insert mode completion, or stop it.
2506 * Called just after typing a character in Insert mode.
2507 * Returns TRUE when the character is not to be inserted;
2508 */
2509 int
2510ins_compl_prep(int c)
2511{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002512 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002513 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002514
2515 // Forget any previous 'special' messages if this is actually
2516 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2517 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2518 edit_submode_extra = NULL;
2519
zeertzjq440d4cb2023-03-02 17:51:32 +00002520 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002521 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002522 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002523 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002524 return retval;
2525
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002526#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002527 // Ignore mouse events in a popup window
2528 if (is_mouse_key(c))
2529 {
2530 // Ignore drag and release events, the position does not need to be in
2531 // the popup and it may have just closed.
2532 if (c == K_LEFTRELEASE
2533 || c == K_LEFTRELEASE_NM
2534 || c == K_MIDDLERELEASE
2535 || c == K_RIGHTRELEASE
2536 || c == K_X1RELEASE
2537 || c == K_X2RELEASE
2538 || c == K_LEFTDRAG
2539 || c == K_MIDDLEDRAG
2540 || c == K_RIGHTDRAG
2541 || c == K_X1DRAG
2542 || c == K_X2DRAG)
2543 return retval;
2544 if (popup_visible)
2545 {
2546 int row = mouse_row;
2547 int col = mouse_col;
2548 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2549
2550 if (wp != NULL && WIN_IS_POPUP(wp))
2551 return retval;
2552 }
2553 }
2554#endif
2555
zeertzjqdca29d92021-08-31 19:12:51 +02002556 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2557 {
2558 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2559 || !vim_is_ctrl_x_key(c))
2560 {
2561 // Not starting another completion mode.
2562 ctrl_x_mode = CTRL_X_CMDLINE;
2563
2564 // CTRL-X CTRL-Z should stop completion without inserting anything
2565 if (c == Ctrl_Z)
2566 retval = TRUE;
2567 }
2568 else
2569 {
2570 ctrl_x_mode = CTRL_X_CMDLINE;
2571
2572 // Other CTRL-X keys first stop completion, then start another
2573 // completion mode.
2574 ins_compl_prep(' ');
2575 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2576 }
2577 }
2578
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002579 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002580 if (ctrl_x_mode_not_defined_yet()
2581 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002582 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002583 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002584 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002585 }
2586
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002587 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002588 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2589 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002590 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002591 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002592 {
2593 // We're already in CTRL-X mode, do we stay in it?
2594 if (!vim_is_ctrl_x_key(c))
2595 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002596 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002597 ctrl_x_mode = CTRL_X_NORMAL;
2598 else
2599 ctrl_x_mode = CTRL_X_FINISHED;
2600 edit_submode = NULL;
2601 }
2602 showmode();
2603 }
2604
2605 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2606 {
2607 // Show error message from attempted keyword completion (probably
2608 // 'Pattern not found') until another key is hit, then go back to
2609 // showing what mode we are in.
2610 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002611 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002612 && c != Ctrl_R && !ins_compl_pum_key(c))
2613 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002614 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002615 }
2616 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2617 // Trigger the CompleteDone event to give scripts a chance to act
2618 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002619 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620
LemonBoy2bf52dd2022-04-09 18:17:34 +01002621 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002622
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002623 // reset continue_* if we left expansion-mode, if we stay they'll be
2624 // (re)set properly in ins_complete()
2625 if (!vim_is_ctrl_x_key(c))
2626 {
2627 compl_cont_status = 0;
2628 compl_cont_mode = 0;
2629 }
2630
2631 return retval;
2632}
2633
2634/*
2635 * Fix the redo buffer for the completion leader replacing some of the typed
2636 * text. This inserts backspaces and appends the changed text.
2637 * "ptr" is the known leader text or NUL.
2638 */
2639 static void
2640ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2641{
John Marriott5e6ea922024-11-23 14:01:57 +01002642 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002643 char_u *p;
2644 char_u *ptr = ptr_arg;
2645
2646 if (ptr == NULL)
2647 {
John Marriott5e6ea922024-11-23 14:01:57 +01002648 if (compl_leader.string != NULL)
2649 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002650 else
2651 return; // nothing to do
2652 }
John Marriott5e6ea922024-11-23 14:01:57 +01002653 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002654 {
John Marriott5e6ea922024-11-23 14:01:57 +01002655 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002656 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2657 ;
2658 if (len > 0)
2659 len -= (*mb_head_off)(p, p + len);
2660 for (p += len; *p != NUL; MB_PTR_ADV(p))
2661 AppendCharToRedobuff(K_BS);
2662 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002663 if (ptr != NULL)
2664 AppendToRedobuffLit(ptr + len, -1);
2665}
2666
2667/*
2668 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2669 * (depending on flag) starting from buf and looking for a non-scanned
2670 * buffer (other than curbuf). curbuf is special, if it is called with
2671 * buf=curbuf then it has to be the first call for a given flag/expansion.
2672 *
2673 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2674 */
2675 static buf_T *
2676ins_compl_next_buf(buf_T *buf, int flag)
2677{
2678 static win_T *wp = NULL;
2679
2680 if (flag == 'w') // just windows
2681 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002682 if (buf == curbuf || !win_valid(wp))
2683 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002684 wp = curwin;
2685 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2686 && wp->w_buffer->b_scanned)
2687 ;
2688 buf = wp->w_buffer;
2689 }
2690 else
2691 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2692 // (unlisted buffers)
2693 // When completing whole lines skip unloaded buffers.
2694 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2695 && ((flag == 'U'
2696 ? buf->b_p_bl
2697 : (!buf->b_p_bl
2698 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2699 || buf->b_scanned))
2700 ;
2701 return buf;
2702}
2703
2704#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002705
2706# ifdef FEAT_EVAL
2707static callback_T cfu_cb; // 'completefunc' callback function
2708static callback_T ofu_cb; // 'omnifunc' callback function
2709static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2710# endif
2711
2712/*
2713 * Copy a global callback function to a buffer local callback.
2714 */
2715 static void
2716copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2717{
2718 free_callback(bufcb);
2719 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2720 copy_callback(bufcb, globcb);
2721}
2722
2723/*
2724 * Parse the 'completefunc' option value and set the callback function.
2725 * Invoked when the 'completefunc' option is set. The option value can be a
2726 * name of a function (string), or function(<name>) or funcref(<name>) or a
2727 * lambda expression.
2728 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002729 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002730did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002731{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2733 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002734
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002736
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002737 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002738}
2739
2740/*
2741 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002742 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002743 */
2744 void
2745set_buflocal_cfu_callback(buf_T *buf UNUSED)
2746{
2747# ifdef FEAT_EVAL
2748 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2749# endif
2750}
2751
2752/*
2753 * Parse the 'omnifunc' option value and set the callback function.
2754 * Invoked when the 'omnifunc' option is set. The option value can be a
2755 * name of a function (string), or function(<name>) or funcref(<name>) or a
2756 * lambda expression.
2757 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002758 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002759did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002760{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2762 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002763
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002764 set_buflocal_ofu_callback(curbuf);
2765 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002766}
2767
2768/*
2769 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002770 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002771 */
2772 void
2773set_buflocal_ofu_callback(buf_T *buf UNUSED)
2774{
2775# ifdef FEAT_EVAL
2776 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2777# endif
2778}
2779
2780/*
2781 * Parse the 'thesaurusfunc' option value and set the callback function.
2782 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2783 * name of a function (string), or function(<name>) or funcref(<name>) or a
2784 * lambda expression.
2785 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002786 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002787did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002788{
2789 int retval;
2790
zeertzjq6eda2692024-11-03 09:23:33 +01002791 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002792 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002793 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2794 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002795 else
2796 {
2797 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002798 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002799 // when using :set, free the local callback
2800 if (!(args->os_flags & OPT_GLOBAL))
2801 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002802 }
2803
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002804 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002805}
2806
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002807/*
2808 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002809 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002810 */
2811 int
2812set_ref_in_insexpand_funcs(int copyID)
2813{
2814 int abort = FALSE;
2815
2816 abort = set_ref_in_callback(&cfu_cb, copyID);
2817 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2818 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2819
2820 return abort;
2821}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002822
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002824 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002825 */
2826 static char_u *
2827get_complete_funcname(int type)
2828{
2829 switch (type)
2830 {
2831 case CTRL_X_FUNCTION:
2832 return curbuf->b_p_cfu;
2833 case CTRL_X_OMNI:
2834 return curbuf->b_p_ofu;
2835 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002836 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002837 default:
2838 return (char_u *)"";
2839 }
2840}
2841
2842/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002843 * Get the callback to use for insert mode completion.
2844 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002845 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002846get_insert_callback(int type)
2847{
2848 if (type == CTRL_X_FUNCTION)
2849 return &curbuf->b_cfu_cb;
2850 if (type == CTRL_X_OMNI)
2851 return &curbuf->b_ofu_cb;
2852 // CTRL_X_THESAURUS
2853 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2854}
2855
2856/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002857 * Execute user defined complete function 'completefunc', 'omnifunc' or
2858 * 'thesaurusfunc', and get matches in "matches".
2859 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002860 */
2861 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002862expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863{
2864 list_T *matchlist = NULL;
2865 dict_T *matchdict = NULL;
2866 typval_T args[3];
2867 char_u *funcname;
2868 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002869 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870 typval_T rettv;
2871 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002872 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002874 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002875 if (*funcname == NUL)
2876 return;
2877
2878 // Call 'completefunc' to obtain the list of matches.
2879 args[0].v_type = VAR_NUMBER;
2880 args[0].vval.v_number = 0;
2881 args[1].v_type = VAR_STRING;
2882 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2883 args[2].v_type = VAR_UNKNOWN;
2884
2885 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002886 // Lock the text to avoid weird things from happening. Also disallow
2887 // switching to another window, it should not be needed and may end up in
2888 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002889 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002890
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002891 cb = get_insert_callback(type);
2892 retval = call_callback(cb, 0, &rettv, 2, args);
2893
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002894 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002895 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002896 {
2897 switch (rettv.v_type)
2898 {
2899 case VAR_LIST:
2900 matchlist = rettv.vval.v_list;
2901 break;
2902 case VAR_DICT:
2903 matchdict = rettv.vval.v_dict;
2904 break;
2905 case VAR_SPECIAL:
2906 if (rettv.vval.v_number == VVAL_NONE)
2907 compl_opt_suppress_empty = TRUE;
2908 // FALLTHROUGH
2909 default:
2910 // TODO: Give error message?
2911 clear_tv(&rettv);
2912 break;
2913 }
2914 }
zeertzjqcfe45652022-05-27 17:26:55 +01002915 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002916
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002917 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002918 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002919 validate_cursor();
2920 if (!EQUAL_POS(curwin->w_cursor, pos))
2921 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002922 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002923 goto theend;
2924 }
2925
2926 if (matchlist != NULL)
2927 ins_compl_add_list(matchlist);
2928 else if (matchdict != NULL)
2929 ins_compl_add_dict(matchdict);
2930
2931theend:
2932 // Restore State, it might have been changed.
2933 State = save_State;
2934
2935 if (matchdict != NULL)
2936 dict_unref(matchdict);
2937 if (matchlist != NULL)
2938 list_unref(matchlist);
2939}
2940#endif // FEAT_COMPL_FUNC
2941
2942#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002943
2944 static inline int
2945get_user_highlight_attr(char_u *hlname)
2946{
2947 if (hlname != NULL && *hlname != NUL)
2948 return syn_name2attr(hlname);
2949 return -1;
2950}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002951/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002952 * Add a match to the list of matches from a typeval_T.
2953 * If the given string is already in the list of completions, then return
2954 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2955 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002956 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002957 */
2958 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002959ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002960{
2961 char_u *word;
2962 int dup = FALSE;
2963 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002964 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002965 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002966 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002967 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002968 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002969 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002970 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002971
Bram Moolenaar08928322020-01-04 14:32:48 +01002972 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002973 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2974 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002975 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2976 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2977 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2978 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2979 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002980
glepnir0fe17f82024-10-08 22:26:44 +02002981 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002982 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002983
2984 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002985 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002986
Bram Moolenaard61efa52022-07-23 09:52:04 +01002987 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2988 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2989 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002990 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002991 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2992 dup = dict_get_number(tv->vval.v_dict, "dup");
2993 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2994 empty = dict_get_number(tv->vval.v_dict, "empty");
2995 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2996 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997 flags |= CP_EQUAL;
2998 }
2999 else
3000 {
3001 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003002 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003003 }
3004 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003005 {
3006 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003007 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003008 }
glepnir508e7852024-07-25 21:39:08 +02003009 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01003010 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003011 if (status != OK)
3012 clear_tv(&user_data);
3013 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003014}
3015
3016/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003017 * Add completions from a list.
3018 */
3019 static void
3020ins_compl_add_list(list_T *list)
3021{
3022 listitem_T *li;
3023 int dir = compl_direction;
3024
3025 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003026 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003027 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003028 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003029 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003030 // if dir was BACKWARD then honor it just once
3031 dir = FORWARD;
3032 else if (did_emsg)
3033 break;
3034 }
3035}
3036
3037/*
3038 * Add completions from a dict.
3039 */
3040 static void
3041ins_compl_add_dict(dict_T *dict)
3042{
3043 dictitem_T *di_refresh;
3044 dictitem_T *di_words;
3045
3046 // Check for optional "refresh" item.
3047 compl_opt_refresh_always = FALSE;
3048 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3049 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3050 {
3051 char_u *v = di_refresh->di_tv.vval.v_string;
3052
3053 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3054 compl_opt_refresh_always = TRUE;
3055 }
3056
3057 // Add completions from a "words" list.
3058 di_words = dict_find(dict, (char_u *)"words", 5);
3059 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3060 ins_compl_add_list(di_words->di_tv.vval.v_list);
3061}
3062
3063/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003064 * Start completion for the complete() function.
3065 * "startcol" is where the matched text starts (1 is first column).
3066 * "list" is the list of matches.
3067 */
3068 static void
3069set_completion(colnr_T startcol, list_T *list)
3070{
3071 int save_w_wrow = curwin->w_wrow;
3072 int save_w_leftcol = curwin->w_leftcol;
3073 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003074 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003075 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3076 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3077 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003078
3079 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003080 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081 ins_compl_prep(' ');
3082 ins_compl_clear();
3083 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003084 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003085
3086 compl_direction = FORWARD;
3087 if (startcol > curwin->w_cursor.col)
3088 startcol = curwin->w_cursor.col;
3089 compl_col = startcol;
3090 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3091 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003092 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003093 if (p_ic)
3094 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003095 if (compl_orig_text.string == NULL)
3096 {
3097 compl_orig_text.length = 0;
3098 return;
3099 }
3100 compl_orig_text.length = (size_t)compl_length;
3101 if (ins_compl_add(compl_orig_text.string,
3102 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3103 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003104 return;
3105
3106 ctrl_x_mode = CTRL_X_EVAL;
3107
3108 ins_compl_add_list(list);
3109 compl_matches = ins_compl_make_cyclic();
3110 compl_started = TRUE;
3111 compl_used_match = TRUE;
3112 compl_cont_status = 0;
3113
3114 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003115 int no_select = compl_no_select || compl_longest;
3116 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117 {
3118 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003119 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003120 // Down/Up has no real effect.
3121 ins_complete(K_UP, FALSE);
3122 }
3123 else
3124 ins_complete(Ctrl_N, FALSE);
3125 compl_enter_selects = compl_no_insert;
3126
3127 // Lazily show the popup menu, unless we got interrupted.
3128 if (!compl_interrupted)
3129 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003130 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003131 out_flush();
3132}
3133
3134/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003135 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003136 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003137 void
3138f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003139{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003140 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003141
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003142 if (in_vim9script()
3143 && (check_for_number_arg(argvars, 0) == FAIL
3144 || check_for_list_arg(argvars, 1) == FAIL))
3145 return;
3146
Bram Moolenaar24959102022-05-07 20:01:16 +01003147 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003148 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003149 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003150 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003151 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003152
3153 // Check for undo allowed here, because if something was already inserted
3154 // the line was already saved for undo and this check isn't done.
3155 if (!undo_allowed())
3156 return;
3157
Bram Moolenaard83392a2022-09-01 12:22:46 +01003158 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003159 {
3160 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3161 if (startcol > 0)
3162 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003163 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003164}
3165
3166/*
3167 * "complete_add()" function
3168 */
3169 void
3170f_complete_add(typval_T *argvars, typval_T *rettv)
3171{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003172 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003173 return;
3174
Bram Moolenaar440cf092021-04-03 20:13:30 +02003175 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003176}
3177
3178/*
3179 * "complete_check()" function
3180 */
3181 void
3182f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3183{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003184 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003185 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003186
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003187 ins_compl_check_keys(0, TRUE);
3188 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003189
3190 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003191}
3192
3193/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003194 * Return Insert completion mode name string
3195 */
3196 static char_u *
3197ins_compl_mode(void)
3198{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003199 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003200 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3201
3202 return (char_u *)"";
3203}
3204
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003205/*
3206 * Assign the sequence number to all the completion matches which don't have
3207 * one assigned yet.
3208 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003209 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003210ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003211{
3212 int number = 0;
3213 compl_T *match;
3214
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003215 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003216 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003217 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003218 // This should normally succeed already at the first loop
3219 // cycle, so it's fast!
3220 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003221 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003222 if (match->cp_number != -1)
3223 {
3224 number = match->cp_number;
3225 break;
3226 }
3227 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003228 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003229 for (match = match->cp_next;
3230 match != NULL && match->cp_number == -1;
3231 match = match->cp_next)
3232 match->cp_number = ++number;
3233 }
3234 else // BACKWARD
3235 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003236 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003237 // number. This should normally succeed already at the
3238 // first loop cycle, so it's fast!
3239 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003240 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003241 if (match->cp_number != -1)
3242 {
3243 number = match->cp_number;
3244 break;
3245 }
3246 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003247 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003248 for (match = match->cp_prev; match
3249 && match->cp_number == -1;
3250 match = match->cp_prev)
3251 match->cp_number = ++number;
3252 }
3253}
3254
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003255/*
glepnir037b0282025-01-16 14:37:44 +01003256 * Fill the dict of complete_info
3257 */
3258 static void
3259fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3260{
3261 dict_add_string(di, "word", match->cp_str.string);
3262 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3263 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3264 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3265 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3266 if (add_match)
3267 dict_add_bool(di, "match", match->cp_in_match_array);
3268 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3269 // Add an empty string for backwards compatibility
3270 dict_add_string(di, "user_data", (char_u *)"");
3271 else
3272 dict_add_tv(di, "user_data", &match->cp_user_data);
3273}
3274
3275/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003276 * Get complete information
3277 */
3278 static void
3279get_complete_info(list_T *what_list, dict_T *retdict)
3280{
3281 int ret = OK;
3282 listitem_T *item;
3283#define CI_WHAT_MODE 0x01
3284#define CI_WHAT_PUM_VISIBLE 0x02
3285#define CI_WHAT_ITEMS 0x04
3286#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003287#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003288#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003289#define CI_WHAT_ALL 0xff
3290 int what_flag;
3291
3292 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003293 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003294 else
3295 {
3296 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003297 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003298 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003299 {
3300 char_u *what = tv_get_string(&item->li_tv);
3301
3302 if (STRCMP(what, "mode") == 0)
3303 what_flag |= CI_WHAT_MODE;
3304 else if (STRCMP(what, "pum_visible") == 0)
3305 what_flag |= CI_WHAT_PUM_VISIBLE;
3306 else if (STRCMP(what, "items") == 0)
3307 what_flag |= CI_WHAT_ITEMS;
3308 else if (STRCMP(what, "selected") == 0)
3309 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003310 else if (STRCMP(what, "completed") == 0)
3311 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003312 else if (STRCMP(what, "matches") == 0)
3313 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003314 }
3315 }
3316
3317 if (ret == OK && (what_flag & CI_WHAT_MODE))
3318 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3319
3320 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3321 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3322
glepnir037b0282025-01-16 14:37:44 +01003323 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3324 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003325 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003326 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003327 dict_T *di;
3328 compl_T *match;
3329 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003330 int has_items = what_flag & CI_WHAT_ITEMS;
3331 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003332 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003333
glepnird4088ed2024-12-31 10:55:22 +01003334 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003335 {
3336 li = list_alloc();
3337 if (li == NULL)
3338 return;
glepnird4088ed2024-12-31 10:55:22 +01003339 ret = dict_add_list(retdict, (has_matches && !has_items)
3340 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003341 }
3342 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3343 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3344 ins_compl_update_sequence_numbers();
3345 if (ret == OK && compl_first_match != NULL)
3346 {
3347 int list_idx = 0;
3348 match = compl_first_match;
3349 do
3350 {
3351 if (!match_at_original_text(match))
3352 {
glepnird4088ed2024-12-31 10:55:22 +01003353 if (has_items
3354 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003355 {
3356 di = dict_alloc();
3357 if (di == NULL)
3358 return;
3359 ret = list_append_dict(li, di);
3360 if (ret != OK)
3361 return;
glepnir037b0282025-01-16 14:37:44 +01003362 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003363 }
glepnird4088ed2024-12-31 10:55:22 +01003364 if (compl_curr_match != NULL
3365 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003366 selected_idx = list_idx;
3367 list_idx += 1;
3368 }
3369 match = match->cp_next;
3370 }
3371 while (match != NULL && !is_first_match(match));
3372 }
3373 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3374 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003375
glepnir037b0282025-01-16 14:37:44 +01003376 if (ret == OK && selected_idx != -1 && has_completed)
3377 {
3378 di = dict_alloc();
3379 if (di == NULL)
3380 return;
3381 fill_complete_info_dict(di, compl_curr_match, FALSE);
3382 ret = dict_add_dict(retdict, "completed", di);
3383 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003384 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003385}
3386
3387/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003388 * "complete_info()" function
3389 */
3390 void
3391f_complete_info(typval_T *argvars, typval_T *rettv)
3392{
3393 list_T *what_list = NULL;
3394
Bram Moolenaar93a10962022-06-16 11:42:09 +01003395 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003396 return;
3397
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003398 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3399 return;
3400
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003401 if (argvars[0].v_type != VAR_UNKNOWN)
3402 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003403 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003404 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003405 what_list = argvars[0].vval.v_list;
3406 }
3407 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003408}
3409#endif
3410
3411/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003412 * Returns TRUE when using a user-defined function for thesaurus completion.
3413 */
3414 static int
3415thesaurus_func_complete(int type UNUSED)
3416{
3417#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003418 return type == CTRL_X_THESAURUS
3419 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003420#else
3421 return FALSE;
3422#endif
3423}
3424
3425/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003426 * Return value of process_next_cpt_value()
3427 */
3428enum
3429{
3430 INS_COMPL_CPT_OK = 1,
3431 INS_COMPL_CPT_CONT,
3432 INS_COMPL_CPT_END
3433};
3434
3435/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003436 * state information used for getting the next set of insert completion
3437 * matches.
3438 */
3439typedef struct
3440{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003441 char_u *e_cpt_copy; // copy of 'complete'
3442 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003443 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003444 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003445 pos_T prev_match_pos; // previous match position
3446 int set_match_pos; // save first_match_pos/last_match_pos
3447 pos_T first_match_pos; // first match position
3448 pos_T last_match_pos; // last match position
3449 int found_all; // found all matches of a certain type.
3450 char_u *dict; // dictionary file to search
3451 int dict_f; // "dict" is an exact file name or not
3452} ins_compl_next_state_T;
3453
3454/*
3455 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003456 *
3457 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003458 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003460 * st->found_all - all matches of this type are found
3461 * st->ins_buf - search for completions in this buffer
3462 * st->first_match_pos - position of the first completion match
3463 * st->last_match_pos - position of the last completion match
3464 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003465 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003466 * st->dict - name of the dictionary or thesaurus file to search
3467 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003468 *
3469 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003470 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3471 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003472 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003473 */
3474 static int
3475process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003476 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003477 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003478 pos_T *start_match_pos,
3479 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003480{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003481 int compl_type = -1;
3482 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003483
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003484 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003486 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3487 st->e_cpt++;
3488
3489 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003490 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003491 st->ins_buf = curbuf;
3492 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003493 // Move the cursor back one character so that ^N can match the
3494 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003495 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003496 {
3497 // Move the cursor to after the last character in the
3498 // buffer, so that word at start of buffer is found
3499 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003500 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003501 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003502 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003503 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003504 compl_type = 0;
3505
3506 // Remember the first match so that the loop stops when we
3507 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003508 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003509 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003510 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003511 && (st->ins_buf = ins_compl_next_buf(
3512 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003513 {
3514 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003515 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003516 {
3517 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003518 st->first_match_pos.col = st->last_match_pos.col = 0;
3519 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3520 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003521 compl_type = 0;
3522 }
3523 else // unloaded buffer, scan like dictionary
3524 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003525 st->found_all = TRUE;
3526 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003527 {
3528 status = INS_COMPL_CPT_CONT;
3529 goto done;
3530 }
3531 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003532 st->dict = st->ins_buf->b_fname;
3533 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003534 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003535 if (!shortmess(SHM_COMPLETIONSCAN))
3536 {
3537 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3538 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3539 st->ins_buf->b_fname == NULL
3540 ? buf_spname(st->ins_buf)
3541 : st->ins_buf->b_sfname == NULL
3542 ? st->ins_buf->b_fname
3543 : st->ins_buf->b_sfname);
3544 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3545 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003546 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003547 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003548 status = INS_COMPL_CPT_END;
3549 else
3550 {
3551 if (ctrl_x_mode_line_or_eval())
3552 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003553 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003554 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003555 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003556 compl_type = CTRL_X_DICTIONARY;
3557 else
3558 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003559 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003560 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003561 st->dict = st->e_cpt;
3562 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 }
3564 }
3565#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003566 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003567 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003568 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003569 compl_type = CTRL_X_PATH_DEFINES;
3570#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003571 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003573 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003574 if (!shortmess(SHM_COMPLETIONSCAN))
3575 {
3576 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3577 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3578 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3579 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003580 }
3581 else
3582 compl_type = -1;
3583
3584 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003585 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003586
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003587 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003588 if (compl_type == -1)
3589 status = INS_COMPL_CPT_CONT;
3590 }
3591
3592done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003594 return status;
3595}
3596
3597#ifdef FEAT_FIND_ID
3598/*
3599 * Get the next set of identifiers or defines matching "compl_pattern" in
3600 * included files.
3601 */
3602 static void
3603get_next_include_file_completion(int compl_type)
3604{
John Marriott5e6ea922024-11-23 14:01:57 +01003605 find_pattern_in_path(compl_pattern.string, compl_direction,
3606 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003607 (compl_type == CTRL_X_PATH_DEFINES
3608 && !(compl_cont_status & CONT_SOL))
3609 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003610 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003611}
3612#endif
3613
3614/*
3615 * Get the next set of words matching "compl_pattern" in dictionary or
3616 * thesaurus files.
3617 */
3618 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003619get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003620{
3621#ifdef FEAT_COMPL_FUNC
3622 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003623 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 else
3625#endif
3626 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003627 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003628 : (compl_type == CTRL_X_THESAURUS
3629 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3630 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003631 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003632 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003633 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003634}
3635
3636/*
3637 * Get the next set of tag names matching "compl_pattern".
3638 */
3639 static void
3640get_next_tag_completion(void)
3641{
3642 int save_p_ic;
3643 char_u **matches;
3644 int num_matches;
3645
3646 // set p_ic according to p_ic, p_scs and pat for find_tags().
3647 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003648 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003649
3650 // Find up to TAG_MANY matches. Avoids that an enormous number
3651 // of matches is found when compl_pattern is empty
3652 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003653 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003655 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003656 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3657 ins_compl_add_matches(num_matches, matches, p_ic);
3658 g_tag_at_cursor = FALSE;
3659 p_ic = save_p_ic;
3660}
3661
3662/*
glepnir8159fb12024-07-17 20:32:54 +02003663 * Compare function for qsort
3664 */
3665static int compare_scores(const void *a, const void *b)
3666{
3667 int idx_a = *(const int *)a;
3668 int idx_b = *(const int *)b;
3669 int score_a = compl_fuzzy_scores[idx_a];
3670 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003671 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3672 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003673}
3674
3675/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 * Get the next set of filename matching "compl_pattern".
3677 */
3678 static void
3679get_next_filename_completion(void)
3680{
3681 char_u **matches;
3682 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003683 char_u *ptr;
3684 garray_T fuzzy_indices;
3685 int i;
3686 int score;
3687 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003688 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003689 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3690 char_u **sorted_matches;
3691 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003692 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003693
glepnirb9de1a02024-08-02 19:14:38 +02003694#ifdef BACKSLASH_IN_FILENAME
3695 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3696 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3697#else
3698 char pathsep = PATHSEP;
3699#endif
3700
glepnir8159fb12024-07-17 20:32:54 +02003701 if (in_fuzzy)
3702 {
glepnirb9de1a02024-08-02 19:14:38 +02003703#ifdef BACKSLASH_IN_FILENAME
3704 if (curbuf->b_p_csl[0] == 's')
3705 {
John Marriott5e6ea922024-11-23 14:01:57 +01003706 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003707 {
3708 if (leader[i] == '\\')
3709 leader[i] = '/';
3710 }
3711 }
3712 else if (curbuf->b_p_csl[0] == 'b')
3713 {
John Marriott5e6ea922024-11-23 14:01:57 +01003714 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003715 {
3716 if (leader[i] == '/')
3717 leader[i] = '\\';
3718 }
3719 }
3720#endif
3721 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003722 if (last_sep == NULL)
3723 {
3724 // No path separator or separator is the last character,
3725 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003726 VIM_CLEAR_STRING(compl_pattern);
3727 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3728 if (compl_pattern.string == NULL)
3729 return;
3730 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003731 }
3732 else if (*(last_sep + 1) == '\0')
3733 in_fuzzy = FALSE;
3734 else
3735 {
3736 // Split leader into path and file parts
3737 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003738 char_u *path_with_wildcard;
3739
3740 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003741 if (path_with_wildcard != NULL)
3742 {
John Marriott5e6ea922024-11-23 14:01:57 +01003743 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3744 VIM_CLEAR_STRING(compl_pattern);
3745 compl_pattern.string = path_with_wildcard;
3746 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003747
3748 // Move leader to the file part
3749 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003750 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003751 }
3752 }
glepnir8159fb12024-07-17 20:32:54 +02003753 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003754
John Marriott5e6ea922024-11-23 14:01:57 +01003755 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003756 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3757 return;
3758
3759 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003760 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003761#ifdef BACKSLASH_IN_FILENAME
3762 if (curbuf->b_p_csl[0] != NUL)
3763 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003764 for (i = 0; i < num_matches; ++i)
3765 {
glepnir8159fb12024-07-17 20:32:54 +02003766 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003767 while (*ptr != NUL)
3768 {
3769 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3770 *ptr = '/';
3771 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3772 *ptr = '\\';
3773 ptr += (*mb_ptr2len)(ptr);
3774 }
3775 }
3776 }
3777#endif
glepnir8159fb12024-07-17 20:32:54 +02003778
3779 if (in_fuzzy)
3780 {
3781 ga_init2(&fuzzy_indices, sizeof(int), 10);
3782 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3783
3784 for (i = 0; i < num_matches; i++)
3785 {
3786 ptr = matches[i];
3787 score = fuzzy_match_str(ptr, leader);
3788 if (score > 0)
3789 {
3790 if (ga_grow(&fuzzy_indices, 1) == OK)
3791 {
3792 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3793 compl_fuzzy_scores[i] = score;
3794 fuzzy_indices.ga_len++;
3795 }
3796 }
3797 }
3798
Christian Brabandt220474d2024-07-20 13:26:44 +02003799 // prevent qsort from deref NULL pointer
3800 if (fuzzy_indices.ga_len > 0)
3801 {
3802 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3803 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003804
Christian Brabandt220474d2024-07-20 13:26:44 +02003805 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3806 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3807 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003808
Christian Brabandt220474d2024-07-20 13:26:44 +02003809 FreeWild(num_matches, matches);
3810 matches = sorted_matches;
3811 num_matches = fuzzy_indices.ga_len;
3812 }
glepnir6b6280c2024-07-27 16:25:45 +02003813 else if (leader_len > 0)
3814 {
3815 FreeWild(num_matches, matches);
3816 num_matches = 0;
3817 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003818
glepnir8159fb12024-07-17 20:32:54 +02003819 vim_free(compl_fuzzy_scores);
3820 ga_clear(&fuzzy_indices);
3821 }
3822
glepnir6b6280c2024-07-27 16:25:45 +02003823 if (num_matches > 0)
3824 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003825}
3826
3827/*
3828 * Get the next set of command-line completions matching "compl_pattern".
3829 */
3830 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003831get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003832{
3833 char_u **matches;
3834 int num_matches;
3835
John Marriott5e6ea922024-11-23 14:01:57 +01003836 if (expand_cmdline(&compl_xp, compl_pattern.string,
3837 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003838 ins_compl_add_matches(num_matches, matches, FALSE);
3839}
3840
3841/*
3842 * Get the next set of spell suggestions matching "compl_pattern".
3843 */
3844 static void
3845get_next_spell_completion(linenr_T lnum UNUSED)
3846{
3847#ifdef FEAT_SPELL
3848 char_u **matches;
3849 int num_matches;
3850
John Marriott5e6ea922024-11-23 14:01:57 +01003851 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003852 if (num_matches > 0)
3853 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003854 else
3855 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003856#endif
3857}
3858
3859/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003860 * Return the next word or line from buffer "ins_buf" at position
3861 * "cur_match_pos" for completion. The length of the match is set in "len".
3862 */
3863 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003864ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003865 buf_T *ins_buf, // buffer being scanned
3866 pos_T *cur_match_pos, // current match position
3867 int *match_len,
3868 int *cont_s_ipos) // next ^X<> will set initial_pos
3869{
3870 char_u *ptr;
3871 int len;
3872
3873 *match_len = 0;
3874 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3875 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003876 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003877 if (ctrl_x_mode_line_or_eval())
3878 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003879 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003880 {
3881 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3882 return NULL;
3883 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003884 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003885 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003886 {
3887 char_u *tmp_ptr = ptr;
3888
3889 ptr = skipwhite(tmp_ptr);
3890 len -= (int)(ptr - tmp_ptr);
3891 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003892 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003893 }
3894 else
3895 {
3896 char_u *tmp_ptr = ptr;
3897
John Marriott5e6ea922024-11-23 14:01:57 +01003898 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003899 {
3900 tmp_ptr += compl_length;
3901 // Skip if already inside a word.
3902 if (vim_iswordp(tmp_ptr))
3903 return NULL;
3904 // Find start of next word.
3905 tmp_ptr = find_word_start(tmp_ptr);
3906 }
3907 // Find end of this word.
3908 tmp_ptr = find_word_end(tmp_ptr);
3909 len = (int)(tmp_ptr - ptr);
3910
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003911 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003912 {
3913 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3914 {
3915 // Try next line, if any. the new word will be
3916 // "join" as if the normal command "J" was used.
3917 // IOSIZE is always greater than
3918 // compl_length, so the next STRNCPY always
3919 // works -- Acevedo
3920 STRNCPY(IObuff, ptr, len);
3921 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3922 tmp_ptr = ptr = skipwhite(ptr);
3923 // Find start of next word.
3924 tmp_ptr = find_word_start(tmp_ptr);
3925 // Find end of next word.
3926 tmp_ptr = find_word_end(tmp_ptr);
3927 if (tmp_ptr > ptr)
3928 {
3929 if (*ptr != ')' && IObuff[len - 1] != TAB)
3930 {
3931 if (IObuff[len - 1] != ' ')
3932 IObuff[len++] = ' ';
3933 // IObuf =~ "\k.* ", thus len >= 2
3934 if (p_js
3935 && (IObuff[len - 2] == '.'
3936 || (vim_strchr(p_cpo, CPO_JOINSP)
3937 == NULL
3938 && (IObuff[len - 2] == '?'
3939 || IObuff[len - 2] == '!'))))
3940 IObuff[len++] = ' ';
3941 }
3942 // copy as much as possible of the new word
3943 if (tmp_ptr - ptr >= IOSIZE - len)
3944 tmp_ptr = ptr + IOSIZE - len - 1;
3945 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3946 len += (int)(tmp_ptr - ptr);
3947 *cont_s_ipos = TRUE;
3948 }
3949 IObuff[len] = NUL;
3950 ptr = IObuff;
3951 }
3952 if (len == compl_length)
3953 return NULL;
3954 }
3955 }
3956
3957 *match_len = len;
3958 return ptr;
3959}
3960
3961/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003962 * Get the next set of words matching "compl_pattern" for default completion(s)
3963 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003964 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3965 * position "st->start_pos" in the "compl_direction" direction. If
3966 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3967 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003968 * Returns OK if a new next match is found, otherwise returns FAIL.
3969 */
3970 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003971get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003972{
3973 int found_new_match = FAIL;
3974 int save_p_scs;
3975 int save_p_ws;
3976 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003977 char_u *ptr = NULL;
3978 int len = 0;
3979 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3980 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003981
3982 // If 'infercase' is set, don't use 'smartcase' here
3983 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003984 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003985 p_scs = FALSE;
3986
3987 // Buffers other than curbuf are scanned from the beginning or the
3988 // end but never from the middle, thus setting nowrapscan in this
3989 // buffer is a good idea, on the other hand, we always set
3990 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3991 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003992 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003993 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003994 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003995 p_ws = TRUE;
3996 looped_around = FALSE;
3997 for (;;)
3998 {
3999 int cont_s_ipos = FALSE;
4000
4001 ++msg_silent; // Don't want messages for wrapscan.
4002
4003 // ctrl_x_mode_line_or_eval() || word-wise search that
4004 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02004005 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004006 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01004007 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02004008 else if (in_fuzzy)
4009 found_new_match = search_for_fuzzy_match(st->ins_buf,
4010 st->cur_match_pos, leader, compl_direction,
4011 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004012 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004013 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004014 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004015 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004016 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004017 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004018 {
4019 // set "compl_started" even on fail
4020 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004021 st->first_match_pos = *st->cur_match_pos;
4022 st->last_match_pos = *st->cur_match_pos;
4023 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004024 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004025 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4026 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004027 {
4028 found_new_match = FAIL;
4029 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004030 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004031 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4032 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4033 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004034 {
4035 if (looped_around)
4036 found_new_match = FAIL;
4037 else
4038 looped_around = TRUE;
4039 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004040 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004041 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4042 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4043 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004044 {
4045 if (looped_around)
4046 found_new_match = FAIL;
4047 else
4048 looped_around = TRUE;
4049 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004050 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004051 if (found_new_match == FAIL)
4052 break;
4053
4054 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004055 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004056 && start_pos->lnum == st->cur_match_pos->lnum
4057 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004058 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004059
glepnir8159fb12024-07-17 20:32:54 +02004060 if (!in_fuzzy)
4061 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4062 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004063 if (ptr == NULL)
4064 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004065
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004066 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004067 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004068 0, cont_s_ipos) != NOTDONE)
4069 {
4070 found_new_match = OK;
4071 break;
4072 }
4073 }
4074 p_scs = save_p_scs;
4075 p_ws = save_p_ws;
4076
4077 return found_new_match;
4078}
4079
4080/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004081 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004082 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004083 */
4084 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004085get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004086{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004087 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004088
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004089 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004090 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004091 case -1:
4092 break;
4093#ifdef FEAT_FIND_ID
4094 case CTRL_X_PATH_PATTERNS:
4095 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004096 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004097 break;
4098#endif
4099
4100 case CTRL_X_DICTIONARY:
4101 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004102 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4103 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004104 break;
4105
4106 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004107 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004108 break;
4109
4110 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004111 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004112 break;
4113
4114 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004115 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004116 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004117 break;
4118
4119#ifdef FEAT_COMPL_FUNC
4120 case CTRL_X_FUNCTION:
4121 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004122 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004123 break;
4124#endif
4125
4126 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004127 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004128 break;
4129
4130 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004131 found_new_match = get_next_default_completion(st, ini);
4132 if (found_new_match == FAIL && st->ins_buf == curbuf)
4133 st->found_all = TRUE;
4134 }
4135
4136 // check if compl_curr_match has changed, (e.g. other type of
4137 // expansion added something)
4138 if (type != 0 && compl_curr_match != compl_old_match)
4139 found_new_match = OK;
4140
4141 return found_new_match;
4142}
4143
4144/*
4145 * Get the next expansion(s), using "compl_pattern".
4146 * The search starts at position "ini" in curbuf and in the direction
4147 * compl_direction.
4148 * When "compl_started" is FALSE start at that position, otherwise continue
4149 * where we stopped searching before.
4150 * This may return before finding all the matches.
4151 * Return the total number of matches or -1 if still unknown -- Acevedo
4152 */
4153 static int
4154ins_compl_get_exp(pos_T *ini)
4155{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004156 static ins_compl_next_state_T st;
4157 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004158 int i;
4159 int found_new_match;
4160 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004161 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004162
4163 if (!compl_started)
4164 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004165 buf_T *buf;
4166
4167 FOR_ALL_BUFFERS(buf)
4168 buf->b_scanned = 0;
4169 if (!st_cleared)
4170 {
4171 CLEAR_FIELD(st);
4172 st_cleared = TRUE;
4173 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004174 st.found_all = FALSE;
4175 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004176 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004177 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004178 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4179 ? (char_u *)"." : curbuf->b_p_cpt);
4180 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004181 st.last_match_pos = st.first_match_pos = *ini;
4182 }
4183 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4184 st.ins_buf = curbuf; // In case the buffer was wiped out.
4185
4186 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004187 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004188 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004189
4190 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4191 for (;;)
4192 {
4193 found_new_match = FAIL;
4194 st.set_match_pos = FALSE;
4195
4196 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4197 // or if found_all says this entry is done. For ^X^L only use the
4198 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004199 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004200 && (!compl_started || st.found_all))
4201 {
glepnir8159fb12024-07-17 20:32:54 +02004202 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004203
4204 if (status == INS_COMPL_CPT_END)
4205 break;
4206 if (status == INS_COMPL_CPT_CONT)
4207 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004208 }
4209
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004210 // If complete() was called then compl_pattern has been reset. The
4211 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004212 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004213 break;
4214
4215 // get the next set of completion matches
4216 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004217
4218 // break the loop for specialized modes (use 'complete' just for the
4219 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4220 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004221 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4222 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004223 {
4224 if (got_int)
4225 break;
4226 // Fill the popup menu as soon as possible.
4227 if (type != -1)
4228 ins_compl_check_keys(0, FALSE);
4229
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004230 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004231 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4232 break;
4233 compl_started = TRUE;
4234 }
4235 else
4236 {
4237 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004238 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004239 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004240
4241 compl_started = FALSE;
4242 }
4243 }
4244 compl_started = TRUE;
4245
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004246 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004247 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004248 found_new_match = FAIL;
4249
4250 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004251 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004252 && !ctrl_x_mode_line_or_eval()))
4253 i = ins_compl_make_cyclic();
4254
4255 if (compl_old_match != NULL)
4256 {
4257 // If several matches were added (FORWARD) or the search failed and has
4258 // just been made cyclic then we have to move compl_curr_match to the
4259 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004260 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004261 : compl_old_match->cp_prev;
4262 if (compl_curr_match == NULL)
4263 compl_curr_match = compl_old_match;
4264 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004265 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004266
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004267 return i;
4268}
4269
4270/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004271 * Update "compl_shown_match" to the actually shown match, it may differ when
4272 * "compl_leader" is used to omit some of the matches.
4273 */
4274 static void
4275ins_compl_update_shown_match(void)
4276{
4277 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004278 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004279 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004280 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004281 compl_shown_match = compl_shown_match->cp_next;
4282
4283 // If we didn't find it searching forward, and compl_shows_dir is
4284 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004285 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004286 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004287 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004288 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004289 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004290 {
4291 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004292 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004293 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004294 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004295 compl_shown_match = compl_shown_match->cp_prev;
4296 }
4297}
4298
4299/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004300 * Delete the old text being completed.
4301 */
4302 void
4303ins_compl_delete(void)
4304{
4305 int col;
4306
4307 // In insert mode: Delete the typed part.
4308 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004309 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004310 if ((int)curwin->w_cursor.col > col)
4311 {
4312 if (stop_arrow() == FAIL)
4313 return;
4314 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004315 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004316 }
4317
4318 // TODO: is this sufficient for redrawing? Redrawing everything causes
4319 // flicker, thus we can't do that.
4320 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004321#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004322 // clear v:completed_item
4323 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004324#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004325}
4326
4327/*
4328 * Insert the new text being completed.
4329 * "in_compl_func" is TRUE when called from complete_check().
4330 */
4331 void
4332ins_compl_insert(int in_compl_func)
4333{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004334 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004335
4336 // Make sure we don't go over the end of the string, this can happen with
4337 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004338 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004339 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004340 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004341 compl_used_match = FALSE;
4342 else
4343 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004344#ifdef FEAT_EVAL
4345 {
4346 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4347
4348 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4349 }
4350#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004351 if (!in_compl_func)
4352 compl_curr_match = compl_shown_match;
4353}
4354
4355/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004356 * show the file name for the completion match (if any). Truncate the file
4357 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004358 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004359 static void
4360ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004361{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004362 char *lead = _("match in file");
4363 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4364 char_u *s;
4365 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004366
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004367 if (space <= 0)
4368 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004369
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004370 // We need the tail that fits. With double-byte encoding going
4371 // back from the end is very slow, thus go from the start and keep
4372 // the text that fits in "space" between "s" and "e".
4373 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004374 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004375 space -= ptr2cells(e);
4376 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004377 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004378 space += ptr2cells(s);
4379 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004380 }
4381 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004382 msg_hist_off = TRUE;
4383 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4384 s > compl_shown_match->cp_fname ? "<" : "", s);
4385 msg((char *)IObuff);
4386 msg_hist_off = FALSE;
4387 redraw_cmdline = FALSE; // don't overwrite!
4388}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004389
glepnirdca57fb2024-06-04 22:01:21 +02004390/*
zeertzjq551d8c32024-06-05 19:53:32 +02004391 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004392 */
glepnira218cc62024-06-03 19:32:39 +02004393 static compl_T *
4394find_comp_when_fuzzy(void)
4395{
4396 int score;
4397 char_u* str;
4398 int target_idx = -1;
4399 int is_forward = compl_shows_dir_forward();
4400 int is_backward = compl_shows_dir_backward();
4401 compl_T *comp = NULL;
4402
glepnir43eef882024-06-19 20:20:48 +02004403 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004404 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004405 return compl_first_match != compl_shown_match ? compl_first_match :
4406 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004407
4408 if (is_forward)
4409 target_idx = compl_selected_item + 1;
4410 else if (is_backward)
4411 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004412 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004413
4414 score = compl_match_array[target_idx].pum_score;
4415 str = compl_match_array[target_idx].pum_text;
4416
4417 comp = compl_first_match;
4418 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004419 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004420 return comp;
4421 comp = comp->cp_next;
4422 } while (comp != NULL && !is_first_match(comp));
4423
4424 return NULL;
4425}
4426
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004427/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004428 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004429 * times. The number of matches found is returned in 'num_matches'.
4430 *
4431 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4432 * get more completions. If it is FALSE, then do nothing when there are no more
4433 * completions in the given direction.
4434 *
4435 * If "advance" is TRUE, then completion will move to the first match.
4436 * Otherwise, the original text will be shown.
4437 *
4438 * Returns OK on success and -1 if the number of matches are unknown.
4439 */
4440 static int
4441find_next_completion_match(
4442 int allow_get_expansion,
4443 int todo, // repeat completion this many times
4444 int advance,
4445 int *num_matches)
4446{
4447 int found_end = FALSE;
4448 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004449 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004450 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4451 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004452
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004453 while (--todo >= 0)
4454 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004455 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004456 {
glepniraced8c22024-06-15 15:13:05 +02004457 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4458 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004459 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004460 && (is_first_match(compl_shown_match->cp_next)
4461 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004462 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004463 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004464 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004465 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004466 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004467 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4468 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004469 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004470 }
4471 else
4472 {
4473 if (!allow_get_expansion)
4474 {
4475 if (advance)
4476 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004477 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004478 compl_pending -= todo + 1;
4479 else
4480 compl_pending += todo + 1;
4481 }
4482 return -1;
4483 }
4484
4485 if (!compl_no_select && advance)
4486 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004487 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004488 --compl_pending;
4489 else
4490 ++compl_pending;
4491 }
4492
4493 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004494 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004495
4496 // handle any pending completions
4497 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004498 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004499 {
4500 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4501 {
4502 compl_shown_match = compl_shown_match->cp_next;
4503 --compl_pending;
4504 }
4505 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4506 {
4507 compl_shown_match = compl_shown_match->cp_prev;
4508 ++compl_pending;
4509 }
4510 else
4511 break;
4512 }
4513 found_end = FALSE;
4514 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004515 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004516 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004517 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004518 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004519 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004520 ++todo;
4521 else
4522 // Remember a matching item.
4523 found_compl = compl_shown_match;
4524
4525 // Stop at the end of the list when we found a usable match.
4526 if (found_end)
4527 {
4528 if (found_compl != NULL)
4529 {
4530 compl_shown_match = found_compl;
4531 break;
4532 }
4533 todo = 1; // use first usable match after wrapping around
4534 }
4535 }
4536
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004537 return OK;
4538}
4539
4540/*
4541 * Fill in the next completion in the current direction.
4542 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4543 * get more completions. If it is FALSE, then we just do nothing when there
4544 * are no more completions in a given direction. The latter case is used when
4545 * we are still in the middle of finding completions, to allow browsing
4546 * through the ones found so far.
4547 * Return the total number of matches, or -1 if still unknown -- webb.
4548 *
4549 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4550 * compl_shown_match here.
4551 *
4552 * Note that this function may be called recursively once only. First with
4553 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4554 * calls this function with "allow_get_expansion" FALSE.
4555 */
4556 static int
4557ins_compl_next(
4558 int allow_get_expansion,
4559 int count, // repeat completion this many times; should
4560 // be at least 1
4561 int insert_match, // Insert the newly selected match
4562 int in_compl_func) // called from complete_check()
4563{
4564 int num_matches = -1;
4565 int todo = count;
4566 int advance;
4567 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004568 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004569 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004570 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4571 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004572
4573 // When user complete function return -1 for findstart which is next
4574 // time of 'always', compl_shown_match become NULL.
4575 if (compl_shown_match == NULL)
4576 return -1;
4577
John Marriott5e6ea922024-11-23 14:01:57 +01004578 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004579 && !match_at_original_text(compl_shown_match)
4580 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004581 // Update "compl_shown_match" to the actually shown match
4582 ins_compl_update_shown_match();
4583
4584 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004585 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004586 // Delete old text to be replaced
4587 ins_compl_delete();
4588
4589 // When finding the longest common text we stick at the original text,
4590 // don't let CTRL-N or CTRL-P move to the first match.
4591 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4592
4593 // When restarting the search don't insert the first match either.
4594 if (compl_restarting)
4595 {
4596 advance = FALSE;
4597 compl_restarting = FALSE;
4598 }
4599
4600 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4601 // around.
4602 if (find_next_completion_match(allow_get_expansion, todo, advance,
4603 &num_matches) == -1)
4604 return -1;
4605
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004606 if (curbuf != orig_curbuf)
4607 {
4608 // In case some completion function switched buffer, don't want to
4609 // insert the completion elsewhere.
4610 return -1;
4611 }
4612
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004613 // Insert the text of the new completion, or the compl_leader.
4614 if (compl_no_insert && !started)
4615 {
glepnir6a38aff2024-12-16 21:56:16 +01004616 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004617 compl_used_match = FALSE;
4618 }
4619 else if (insert_match)
4620 {
4621 if (!compl_get_longest || compl_used_match)
4622 ins_compl_insert(in_compl_func);
4623 else
glepnir6a38aff2024-12-16 21:56:16 +01004624 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004625 }
4626 else
4627 compl_used_match = FALSE;
4628
4629 if (!allow_get_expansion)
4630 {
4631 // may undisplay the popup menu first
4632 ins_compl_upd_pum();
4633
4634 if (pum_enough_matches())
4635 // Will display the popup menu, don't redraw yet to avoid flicker.
4636 pum_call_update_screen();
4637 else
4638 // Not showing the popup menu yet, redraw to show the user what was
4639 // inserted.
4640 update_screen(0);
4641
4642 // display the updated popup menu
4643 ins_compl_show_pum();
4644#ifdef FEAT_GUI
4645 if (gui.in_use)
4646 {
4647 // Show the cursor after the match, not after the redrawn text.
4648 setcursor();
4649 out_flush_cursor(FALSE, FALSE);
4650 }
4651#endif
4652
4653 // Delete old text to be replaced, since we're still searching and
4654 // don't want to match ourselves!
4655 ins_compl_delete();
4656 }
4657
4658 // Enter will select a match when the match wasn't inserted and the popup
4659 // menu is visible.
4660 if (compl_no_insert && !started)
4661 compl_enter_selects = TRUE;
4662 else
4663 compl_enter_selects = !insert_match && compl_match_array != NULL;
4664
4665 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004666 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004667 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004668
4669 return num_matches;
4670}
4671
4672/*
4673 * Call this while finding completions, to check whether the user has hit a key
4674 * that should change the currently displayed completion, or exit completion
4675 * mode. Also, when compl_pending is not zero, show a completion as soon as
4676 * possible. -- webb
4677 * "frequency" specifies out of how many calls we actually check.
4678 * "in_compl_func" is TRUE when called from complete_check(), don't set
4679 * compl_curr_match.
4680 */
4681 void
4682ins_compl_check_keys(int frequency, int in_compl_func)
4683{
4684 static int count = 0;
4685 int c;
4686
4687 // Don't check when reading keys from a script, :normal or feedkeys().
4688 // That would break the test scripts. But do check for keys when called
4689 // from complete_check().
4690 if (!in_compl_func && (using_script() || ex_normal_busy))
4691 return;
4692
4693 // Only do this at regular intervals
4694 if (++count < frequency)
4695 return;
4696 count = 0;
4697
4698 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4699 // can't do its work correctly.
4700 c = vpeekc_any();
4701 if (c != NUL)
4702 {
4703 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4704 {
4705 c = safe_vgetc(); // Eat the character
4706 compl_shows_dir = ins_compl_key2dir(c);
4707 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4708 c != K_UP && c != K_DOWN, in_compl_func);
4709 }
4710 else
4711 {
4712 // Need to get the character to have KeyTyped set. We'll put it
4713 // back with vungetc() below. But skip K_IGNORE.
4714 c = safe_vgetc();
4715 if (c != K_IGNORE)
4716 {
4717 // Don't interrupt completion when the character wasn't typed,
4718 // e.g., when doing @q to replay keys.
4719 if (c != Ctrl_R && KeyTyped)
4720 compl_interrupted = TRUE;
4721
4722 vungetc(c);
4723 }
4724 }
4725 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004726 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004727 {
4728 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4729
4730 compl_pending = 0;
4731 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4732 }
4733}
4734
4735/*
4736 * Decide the direction of Insert mode complete from the key typed.
4737 * Returns BACKWARD or FORWARD.
4738 */
4739 static int
4740ins_compl_key2dir(int c)
4741{
4742 if (c == Ctrl_P || c == Ctrl_L
4743 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4744 return BACKWARD;
4745 return FORWARD;
4746}
4747
4748/*
4749 * Return TRUE for keys that are used for completion only when the popup menu
4750 * is visible.
4751 */
4752 static int
4753ins_compl_pum_key(int c)
4754{
4755 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4756 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4757 || c == K_UP || c == K_DOWN);
4758}
4759
4760/*
4761 * Decide the number of completions to move forward.
4762 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4763 */
4764 static int
4765ins_compl_key2count(int c)
4766{
4767 int h;
4768
4769 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4770 {
4771 h = pum_get_height();
4772 if (h > 3)
4773 h -= 2; // keep some context
4774 return h;
4775 }
4776 return 1;
4777}
4778
4779/*
4780 * Return TRUE if completion with "c" should insert the match, FALSE if only
4781 * to change the currently selected completion.
4782 */
4783 static int
4784ins_compl_use_match(int c)
4785{
4786 switch (c)
4787 {
4788 case K_UP:
4789 case K_DOWN:
4790 case K_PAGEDOWN:
4791 case K_KPAGEDOWN:
4792 case K_S_DOWN:
4793 case K_PAGEUP:
4794 case K_KPAGEUP:
4795 case K_S_UP:
4796 return FALSE;
4797 }
4798 return TRUE;
4799}
4800
4801/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004802 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4803 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004804 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004805 * Uses the global variables: compl_cont_status and ctrl_x_mode
4806 */
4807 static int
4808get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4809{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004810 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004811 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004812 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004813 {
4814 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4815 ;
4816 compl_col += ++startcol;
4817 compl_length = curs_col - startcol;
4818 }
4819 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004820 {
John Marriott5e6ea922024-11-23 14:01:57 +01004821 compl_pattern.string = str_foldcase(line + compl_col,
4822 compl_length, NULL, 0);
4823 if (compl_pattern.string == NULL)
4824 {
4825 compl_pattern.length = 0;
4826 return FAIL;
4827 }
4828 compl_pattern.length = STRLEN(compl_pattern.string);
4829 }
4830 else
4831 {
4832 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4833 if (compl_pattern.string == NULL)
4834 {
4835 compl_pattern.length = 0;
4836 return FAIL;
4837 }
4838 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004839 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004840 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004841 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004842 {
4843 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004844 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004845 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004846
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004847 if (!vim_iswordp(line + compl_col)
4848 || (compl_col > 0
4849 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004850 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004851 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004852 prefixlen = 0;
4853 }
John Marriott5e6ea922024-11-23 14:01:57 +01004854
4855 // we need up to 2 extra chars for the prefix
4856 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4857 compl_pattern.string = alloc(n);
4858 if (compl_pattern.string == NULL)
4859 {
4860 compl_pattern.length = 0;
4861 return FAIL;
4862 }
4863 STRCPY((char *)compl_pattern.string, prefix);
4864 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004865 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004866 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004867 }
4868 else if (--startcol < 0
4869 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4870 {
John Marriott5e6ea922024-11-23 14:01:57 +01004871 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4872
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004873 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004874 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4875 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004876 {
John Marriott5e6ea922024-11-23 14:01:57 +01004877 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004878 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004879 }
John Marriott5e6ea922024-11-23 14:01:57 +01004880 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004881 compl_col += curs_col;
4882 compl_length = 0;
4883 }
4884 else
4885 {
4886 // Search the point of change class of multibyte character
4887 // or not a word single byte character backward.
4888 if (has_mbyte)
4889 {
4890 int base_class;
4891 int head_off;
4892
4893 startcol -= (*mb_head_off)(line, line + startcol);
4894 base_class = mb_get_class(line + startcol);
4895 while (--startcol >= 0)
4896 {
4897 head_off = (*mb_head_off)(line, line + startcol);
4898 if (base_class != mb_get_class(line + startcol
4899 - head_off))
4900 break;
4901 startcol -= head_off;
4902 }
4903 }
4904 else
4905 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4906 ;
4907 compl_col += ++startcol;
4908 compl_length = (int)curs_col - startcol;
4909 if (compl_length == 1)
4910 {
4911 // Only match word with at least two chars -- webb
4912 // there's no need to call quote_meta,
4913 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004914 compl_pattern.string = alloc(7);
4915 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004916 {
John Marriott5e6ea922024-11-23 14:01:57 +01004917 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004918 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004919 }
John Marriott5e6ea922024-11-23 14:01:57 +01004920 STRCPY((char *)compl_pattern.string, "\\<");
4921 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4922 STRCAT((char *)compl_pattern.string, "\\k");
4923 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004924 }
4925 else
4926 {
John Marriott5e6ea922024-11-23 14:01:57 +01004927 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4928
4929 compl_pattern.string = alloc(n);
4930 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004931 {
John Marriott5e6ea922024-11-23 14:01:57 +01004932 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004933 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004934 }
John Marriott5e6ea922024-11-23 14:01:57 +01004935 STRCPY((char *)compl_pattern.string, "\\<");
4936 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004937 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004938 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004939 }
4940 }
4941
4942 return OK;
4943}
4944
4945/*
4946 * Get the pattern, column and length for whole line completion or for the
4947 * complete() function.
4948 * Sets the global variables: compl_col, compl_length and compl_pattern.
4949 */
4950 static int
4951get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4952{
4953 compl_col = (colnr_T)getwhitecols(line);
4954 compl_length = (int)curs_col - (int)compl_col;
4955 if (compl_length < 0) // cursor in indent: empty pattern
4956 compl_length = 0;
4957 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004958 {
John Marriott5e6ea922024-11-23 14:01:57 +01004959 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4960 NULL, 0);
4961 if (compl_pattern.string == NULL)
4962 {
4963 compl_pattern.length = 0;
4964 return FAIL;
4965 }
4966 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004967 }
John Marriott5e6ea922024-11-23 14:01:57 +01004968 else
4969 {
4970 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4971 if (compl_pattern.string == NULL)
4972 {
4973 compl_pattern.length = 0;
4974 return FAIL;
4975 }
4976 compl_pattern.length = (size_t)compl_length;
4977 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004978
4979 return OK;
4980}
4981
4982/*
4983 * Get the pattern, column and length for filename completion.
4984 * Sets the global variables: compl_col, compl_length and compl_pattern.
4985 */
4986 static int
4987get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4988{
4989 // Go back to just before the first filename character.
4990 if (startcol > 0)
4991 {
4992 char_u *p = line + startcol;
4993
4994 MB_PTR_BACK(line, p);
4995 while (p > line && vim_isfilec(PTR2CHAR(p)))
4996 MB_PTR_BACK(line, p);
4997 if (p == line && vim_isfilec(PTR2CHAR(p)))
4998 startcol = 0;
4999 else
5000 startcol = (int)(p - line) + 1;
5001 }
5002
5003 compl_col += startcol;
5004 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005005 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5006 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005007 {
John Marriott5e6ea922024-11-23 14:01:57 +01005008 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005009 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005010 }
5011
John Marriott5e6ea922024-11-23 14:01:57 +01005012 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005013
5014 return OK;
5015}
5016
5017/*
5018 * Get the pattern, column and length for command-line completion.
5019 * Sets the global variables: compl_col, compl_length and compl_pattern.
5020 */
5021 static int
5022get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5023{
John Marriott5e6ea922024-11-23 14:01:57 +01005024 compl_pattern.string = vim_strnsave(line, curs_col);
5025 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005026 {
John Marriott5e6ea922024-11-23 14:01:57 +01005027 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005028 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005029 }
John Marriott5e6ea922024-11-23 14:01:57 +01005030 compl_pattern.length = curs_col;
5031 set_cmd_context(&compl_xp, compl_pattern.string,
5032 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005033 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5034 || compl_xp.xp_context == EXPAND_NOTHING)
5035 // No completion possible, use an empty pattern to get a
5036 // "pattern not found" message.
5037 compl_col = curs_col;
5038 else
John Marriott5e6ea922024-11-23 14:01:57 +01005039 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005040 compl_length = curs_col - compl_col;
5041
5042 return OK;
5043}
5044
5045/*
5046 * Get the pattern, column and length for user defined completion ('omnifunc',
5047 * 'completefunc' and 'thesaurusfunc')
5048 * Sets the global variables: compl_col, compl_length and compl_pattern.
5049 * Uses the global variable: spell_bad_len
5050 */
5051 static int
5052get_userdefined_compl_info(colnr_T curs_col UNUSED)
5053{
5054 int ret = FAIL;
5055
5056#ifdef FEAT_COMPL_FUNC
5057 // Call user defined function 'completefunc' with "a:findstart"
5058 // set to 1 to obtain the length of text to use for completion.
5059 char_u *line;
5060 typval_T args[3];
5061 int col;
5062 char_u *funcname;
5063 pos_T pos;
5064 int save_State = State;
5065 callback_T *cb;
5066
5067 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5068 // length as a string
5069 funcname = get_complete_funcname(ctrl_x_mode);
5070 if (*funcname == NUL)
5071 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005072 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005073 ? "completefunc" : "omnifunc");
5074 return FAIL;
5075 }
5076
5077 args[0].v_type = VAR_NUMBER;
5078 args[0].vval.v_number = 1;
5079 args[1].v_type = VAR_STRING;
5080 args[1].vval.v_string = (char_u *)"";
5081 args[2].v_type = VAR_UNKNOWN;
5082 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005083 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005084 cb = get_insert_callback(ctrl_x_mode);
5085 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005086 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005087
5088 State = save_State;
5089 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005090 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005091 validate_cursor();
5092 if (!EQUAL_POS(curwin->w_cursor, pos))
5093 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005094 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005095 return FAIL;
5096 }
5097
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005098 // Return value -2 means the user complete function wants to cancel the
5099 // complete without an error, do the same if the function did not execute
5100 // successfully.
5101 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005102 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005103 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005104 if (col == -3)
5105 {
5106 ctrl_x_mode = CTRL_X_NORMAL;
5107 edit_submode = NULL;
5108 if (!shortmess(SHM_COMPLETIONMENU))
5109 msg_clr_cmdline();
5110 return FAIL;
5111 }
5112
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005113 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005114 // completion.
5115 compl_opt_refresh_always = FALSE;
5116 compl_opt_suppress_empty = FALSE;
5117
5118 if (col < 0)
5119 col = curs_col;
5120 compl_col = col;
5121 if (compl_col > curs_col)
5122 compl_col = curs_col;
5123
5124 // Setup variables for completion. Need to obtain "line" again,
5125 // it may have become invalid.
5126 line = ml_get(curwin->w_cursor.lnum);
5127 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005128 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5129 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005130 {
John Marriott5e6ea922024-11-23 14:01:57 +01005131 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005132 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005133 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005134
John Marriott5e6ea922024-11-23 14:01:57 +01005135 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005136 ret = OK;
5137#endif
5138
5139 return ret;
5140}
5141
5142/*
5143 * Get the pattern, column and length for spell completion.
5144 * Sets the global variables: compl_col, compl_length and compl_pattern.
5145 * Uses the global variable: spell_bad_len
5146 */
5147 static int
5148get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5149{
5150 int ret = FAIL;
5151#ifdef FEAT_SPELL
5152 char_u *line;
5153
5154 if (spell_bad_len > 0)
5155 compl_col = curs_col - spell_bad_len;
5156 else
5157 compl_col = spell_word_start(startcol);
5158 if (compl_col >= (colnr_T)startcol)
5159 {
5160 compl_length = 0;
5161 compl_col = curs_col;
5162 }
5163 else
5164 {
5165 spell_expand_check_cap(compl_col);
5166 compl_length = (int)curs_col - compl_col;
5167 }
5168 // Need to obtain "line" again, it may have become invalid.
5169 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005170 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5171 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005172 {
John Marriott5e6ea922024-11-23 14:01:57 +01005173 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005174 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005175 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005176
John Marriott5e6ea922024-11-23 14:01:57 +01005177 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005178 ret = OK;
5179#endif
5180
5181 return ret;
5182}
5183
5184/*
5185 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005186 * "startcol" - start column number of the completion pattern/text
5187 * "cur_col" - current cursor column
5188 * On return, "line_invalid" is set to TRUE, if the current line may have
5189 * become invalid and needs to be fetched again.
5190 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005191 */
5192 static int
5193compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5194{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005195 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005196 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5197 && !thesaurus_func_complete(ctrl_x_mode)))
5198 {
5199 return get_normal_compl_info(line, startcol, curs_col);
5200 }
5201 else if (ctrl_x_mode_line_or_eval())
5202 {
5203 return get_wholeline_compl_info(line, curs_col);
5204 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005205 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005206 {
5207 return get_filename_compl_info(line, startcol, curs_col);
5208 }
5209 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5210 {
5211 return get_cmdline_compl_info(line, curs_col);
5212 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005213 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005214 || thesaurus_func_complete(ctrl_x_mode))
5215 {
5216 if (get_userdefined_compl_info(curs_col) == FAIL)
5217 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005218 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005219 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005220 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005221 {
5222 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5223 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005224 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005225 }
5226 else
5227 {
5228 internal_error("ins_complete()");
5229 return FAIL;
5230 }
5231
5232 return OK;
5233}
5234
5235/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005236 * Continue an interrupted completion mode search in "line".
5237 *
5238 * If this same ctrl_x_mode has been interrupted use the text from
5239 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5240 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5241 * the same line as the cursor then fix it (the line has been split because it
5242 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5243 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005244 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005245 static void
5246ins_compl_continue_search(char_u *line)
5247{
5248 // it is a continued search
5249 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005250 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5251 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005252 {
5253 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5254 {
5255 // line (probably) wrapped, set compl_startpos to the
5256 // first non_blank in the line, if it is not a wordchar
5257 // include it to get a better pattern, but then we don't
5258 // want the "\\<" prefix, check it below
5259 compl_col = (colnr_T)getwhitecols(line);
5260 compl_startpos.col = compl_col;
5261 compl_startpos.lnum = curwin->w_cursor.lnum;
5262 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5263 }
5264 else
5265 {
5266 // S_IPOS was set when we inserted a word that was at the
5267 // beginning of the line, which means that we'll go to SOL
5268 // mode but first we need to redefine compl_startpos
5269 if (compl_cont_status & CONT_S_IPOS)
5270 {
5271 compl_cont_status |= CONT_SOL;
5272 compl_startpos.col = (colnr_T)(skipwhite(
5273 line + compl_length
5274 + compl_startpos.col) - line);
5275 }
5276 compl_col = compl_startpos.col;
5277 }
5278 compl_length = curwin->w_cursor.col - (int)compl_col;
5279 // IObuff is used to add a "word from the next line" would we
5280 // have enough space? just being paranoid
5281#define MIN_SPACE 75
5282 if (compl_length > (IOSIZE - MIN_SPACE))
5283 {
5284 compl_cont_status &= ~CONT_SOL;
5285 compl_length = (IOSIZE - MIN_SPACE);
5286 compl_col = curwin->w_cursor.col - compl_length;
5287 }
5288 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5289 if (compl_length < 1)
5290 compl_cont_status &= CONT_LOCAL;
5291 }
5292 else if (ctrl_x_mode_line_or_eval())
5293 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5294 else
5295 compl_cont_status = 0;
5296}
5297
5298/*
5299 * start insert mode completion
5300 */
5301 static int
5302ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005303{
5304 char_u *line;
5305 int startcol = 0; // column where searched text starts
5306 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005307 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005308 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005309 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005310
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005311 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005312
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005313 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005314 did_si = FALSE;
5315 can_si = FALSE;
5316 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005317 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005318 return FAIL;
5319
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005320 line = ml_get(curwin->w_cursor.lnum);
5321 curs_col = curwin->w_cursor.col;
5322 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005323
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005324 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5325 && compl_cont_mode == ctrl_x_mode)
5326 // this same ctrl-x_mode was interrupted previously. Continue the
5327 // completion.
5328 ins_compl_continue_search(line);
5329 else
5330 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005331
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005332 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005333 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005334 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005335 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005336 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5337 compl_cont_status = 0;
5338 compl_cont_status |= CONT_N_ADDS;
5339 compl_startpos = curwin->w_cursor;
5340 startcol = (int)curs_col;
5341 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005342 }
5343
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005344 // Work out completion pattern and original text -- webb
5345 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5346 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005347 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5348 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005349 // restore did_ai, so that adding comment leader works
5350 did_ai = save_did_ai;
5351 return FAIL;
5352 }
5353 // If "line" was changed while getting completion info get it again.
5354 if (line_invalid)
5355 line = ml_get(curwin->w_cursor.lnum);
5356
glepnir7cfe6932024-09-15 20:06:28 +02005357 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005358 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005359 {
5360 edit_submode_pre = (char_u *)_(" Adding");
5361 if (ctrl_x_mode_line_or_eval())
5362 {
5363 // Insert a new line, keep indentation but ignore 'comments'.
5364 char_u *old = curbuf->b_p_com;
5365
5366 curbuf->b_p_com = (char_u *)"";
5367 compl_startpos.lnum = curwin->w_cursor.lnum;
5368 compl_startpos.col = compl_col;
5369 ins_eol('\r');
5370 curbuf->b_p_com = old;
5371 compl_length = 0;
5372 compl_col = curwin->w_cursor.col;
5373 }
glepnir7cfe6932024-09-15 20:06:28 +02005374 else if (ctrl_x_mode_normal() && in_fuzzy)
5375 {
5376 compl_startpos = curwin->w_cursor;
5377 compl_cont_status &= CONT_S_IPOS;
5378 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005379 }
5380 else
5381 {
5382 edit_submode_pre = NULL;
5383 compl_startpos.col = compl_col;
5384 }
5385
5386 if (compl_cont_status & CONT_LOCAL)
5387 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5388 else
5389 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5390
5391 // If any of the original typed text has been changed we need to fix
5392 // the redo buffer.
5393 ins_compl_fixRedoBufForLeader(NULL);
5394
5395 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005396 VIM_CLEAR_STRING(compl_orig_text);
5397 compl_orig_text.length = (size_t)compl_length;
5398 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005399 if (p_ic)
5400 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005401 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5402 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005403 {
John Marriott5e6ea922024-11-23 14:01:57 +01005404 VIM_CLEAR_STRING(compl_pattern);
5405 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005406 return FAIL;
5407 }
5408
5409 // showmode might reset the internal line pointers, so it must
5410 // be called before line = ml_get(), or when this address is no
5411 // longer needed. -- Acevedo.
5412 edit_submode_extra = (char_u *)_("-- Searching...");
5413 edit_submode_highl = HLF_COUNT;
5414 showmode();
5415 edit_submode_extra = NULL;
5416 out_flush();
5417
5418 return OK;
5419}
5420
5421/*
5422 * display the completion status message
5423 */
5424 static void
5425ins_compl_show_statusmsg(void)
5426{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005427 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005428 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005429 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005430 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005431 ? (char_u *)_("Hit end of paragraph")
5432 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005433 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005434 }
5435
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005436 if (edit_submode_extra == NULL)
5437 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005438 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005439 {
5440 edit_submode_extra = (char_u *)_("Back at original");
5441 edit_submode_highl = HLF_W;
5442 }
5443 else if (compl_cont_status & CONT_S_IPOS)
5444 {
5445 edit_submode_extra = (char_u *)_("Word from other line");
5446 edit_submode_highl = HLF_COUNT;
5447 }
5448 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5449 {
5450 edit_submode_extra = (char_u *)_("The only match");
5451 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005452 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005453 }
5454 else
5455 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005456#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005457 // Update completion sequence number when needed.
5458 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005459 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005460#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005461 // The match should always have a sequence number now, this is
5462 // just a safety check.
5463 if (compl_curr_match->cp_number != -1)
5464 {
5465 // Space for 10 text chars. + 2x10-digit no.s = 31.
5466 // Translations may need more than twice that.
5467 static char_u match_ref[81];
5468
5469 if (compl_matches > 0)
5470 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005471 _("match %d of %d"),
5472 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005473 else
5474 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005475 _("match %d"),
5476 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005477 edit_submode_extra = match_ref;
5478 edit_submode_highl = HLF_R;
5479 if (dollar_vcol >= 0)
5480 curs_columns(FALSE);
5481 }
5482 }
5483 }
5484
5485 // Show a message about what (completion) mode we're in.
5486 if (!compl_opt_suppress_empty)
5487 {
5488 showmode();
5489 if (!shortmess(SHM_COMPLETIONMENU))
5490 {
5491 if (edit_submode_extra != NULL)
5492 {
5493 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005494 {
5495 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005496 msg_attr((char *)edit_submode_extra,
5497 edit_submode_highl < HLF_COUNT
5498 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005499 msg_hist_off = FALSE;
5500 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005501 }
5502 else
5503 msg_clr_cmdline(); // necessary for "noshowmode"
5504 }
5505 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005506}
5507
5508/*
5509 * Do Insert mode completion.
5510 * Called when character "c" was typed, which has a meaning for completion.
5511 * Returns OK if completion was done, FAIL if something failed (out of mem).
5512 */
5513 int
5514ins_complete(int c, int enable_pum)
5515{
5516 int n;
5517 int save_w_wrow;
5518 int save_w_leftcol;
5519 int insert_match;
5520
5521 compl_direction = ins_compl_key2dir(c);
5522 insert_match = ins_compl_use_match(c);
5523
5524 if (!compl_started)
5525 {
5526 if (ins_compl_start() == FAIL)
5527 return FAIL;
5528 }
5529 else if (insert_match && stop_arrow() == FAIL)
5530 return FAIL;
5531
5532 compl_shown_match = compl_curr_match;
5533 compl_shows_dir = compl_direction;
5534
5535 // Find next match (and following matches).
5536 save_w_wrow = curwin->w_wrow;
5537 save_w_leftcol = curwin->w_leftcol;
5538 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5539
5540 // may undisplay the popup menu
5541 ins_compl_upd_pum();
5542
5543 if (n > 1) // all matches have been found
5544 compl_matches = n;
5545 compl_curr_match = compl_shown_match;
5546 compl_direction = compl_shows_dir;
5547
5548 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5549 // mode.
5550 if (got_int && !global_busy)
5551 {
5552 (void)vgetc();
5553 got_int = FALSE;
5554 }
5555
5556 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005557 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005558 {
5559 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5560 // because we couldn't expand anything at first place, but if we used
5561 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5562 // (such as M in M'exico) if not tried already. -- Acevedo
5563 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005564 || compl_status_adding()
5565 || (ctrl_x_mode_not_default()
5566 && !ctrl_x_mode_path_patterns()
5567 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005568 compl_cont_status &= ~CONT_N_ADDS;
5569 }
5570
5571 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5572 compl_cont_status |= CONT_S_IPOS;
5573 else
5574 compl_cont_status &= ~CONT_S_IPOS;
5575
5576 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005577
5578 // Show the popup menu, unless we got interrupted.
5579 if (enable_pum && !compl_interrupted)
5580 show_pum(save_w_wrow, save_w_leftcol);
5581
5582 compl_was_interrupted = compl_interrupted;
5583 compl_interrupted = FALSE;
5584
5585 return OK;
5586}
5587
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005588/*
5589 * Remove (if needed) and show the popup menu
5590 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005591 static void
5592show_pum(int prev_w_wrow, int prev_w_leftcol)
5593{
5594 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005595 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005596 RedrawingDisabled = 0;
5597
5598 // If the cursor moved or the display scrolled we need to remove the pum
5599 // first.
5600 setcursor();
5601 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5602 ins_compl_del_pum();
5603
5604 ins_compl_show_pum();
5605 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005606
5607 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005608}
5609
5610/*
5611 * Looks in the first "len" chars. of "src" for search-metachars.
5612 * If dest is not NULL the chars. are copied there quoting (with
5613 * a backslash) the metachars, and dest would be NUL terminated.
5614 * Returns the length (needed) of dest
5615 */
5616 static unsigned
5617quote_meta(char_u *dest, char_u *src, int len)
5618{
5619 unsigned m = (unsigned)len + 1; // one extra for the NUL
5620
5621 for ( ; --len >= 0; src++)
5622 {
5623 switch (*src)
5624 {
5625 case '.':
5626 case '*':
5627 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005628 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005629 break;
5630 // FALLTHROUGH
5631 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005632 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005633 break;
5634 // FALLTHROUGH
5635 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005636 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005637 break;
5638 // FALLTHROUGH
5639 case '^': // currently it's not needed.
5640 case '$':
5641 m++;
5642 if (dest != NULL)
5643 *dest++ = '\\';
5644 break;
5645 }
5646 if (dest != NULL)
5647 *dest++ = *src;
5648 // Copy remaining bytes of a multibyte character.
5649 if (has_mbyte)
5650 {
5651 int i, mb_len;
5652
5653 mb_len = (*mb_ptr2len)(src) - 1;
5654 if (mb_len > 0 && len >= mb_len)
5655 for (i = 0; i < mb_len; ++i)
5656 {
5657 --len;
5658 ++src;
5659 if (dest != NULL)
5660 *dest++ = *src;
5661 }
5662 }
5663 }
5664 if (dest != NULL)
5665 *dest = NUL;
5666
5667 return m;
5668}
5669
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005670#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005671 void
5672free_insexpand_stuff(void)
5673{
John Marriott5e6ea922024-11-23 14:01:57 +01005674 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005675# ifdef FEAT_EVAL
5676 free_callback(&cfu_cb);
5677 free_callback(&ofu_cb);
5678 free_callback(&tsrfu_cb);
5679# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005680}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005681#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005682
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005683#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005684/*
5685 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5686 * spelled word, if there is one.
5687 */
5688 static void
5689spell_back_to_badword(void)
5690{
5691 pos_T tpos = curwin->w_cursor;
5692
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005693 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005694 if (curwin->w_cursor.col != tpos.col)
5695 start_arrow(&tpos);
5696}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005697#endif