blob: f7700413102e486fc9bd0ddbaf3f600d891b29dc [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
glepnirf31cfa22025-03-06 21:59:13 +0100135// list used to store the compl_T which have the max score
136// used for completefuzzycollect
137static compl_T **compl_best_matches = NULL;
138static int compl_num_bests = 0;
139// inserted a longest when completefuzzycollect enabled
140static int compl_cfc_longest_ins = FALSE;
141
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100142// After using a cursor key <Enter> selects a match in the popup menu,
143// otherwise it inserts a line break.
144static int compl_enter_selects = FALSE;
145
146// When "compl_leader" is not NULL only matches that start with this string
147// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100148static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100149
150static int compl_get_longest = FALSE; // put longest common string
151 // in compl_leader
152
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100153// Selected one of the matches. When FALSE the match was edited or using the
154// longest common string.
155static int compl_used_match;
156
157// didn't finish finding completions.
158static int compl_was_interrupted = FALSE;
159
160// Set when character typed while looking for matches and it means we should
161// stop looking for matches.
162static int compl_interrupted = FALSE;
163
164static int compl_restarting = FALSE; // don't insert match
165
166// When the first completion is done "compl_started" is set. When it's
167// FALSE the word to be completed must be located.
168static int compl_started = FALSE;
169
170// Which Ctrl-X mode are we in?
171static int ctrl_x_mode = CTRL_X_NORMAL;
172
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000173static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100174static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100175static int compl_direction = FORWARD;
176static int compl_shows_dir = FORWARD;
177static int compl_pending = 0; // > 1 for postponed CTRL-N
178static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000179// Length in bytes of the text being completed (this is deleted to be replaced
180// by the match.)
181static int compl_length = 0;
glepnir76bdb822025-02-08 19:04:51 +0100182static linenr_T compl_lnum = 0; // lnum where the completion start
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100183static colnr_T compl_col = 0; // column where the text starts
184 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100185static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100186static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100187 // completion started
188static int compl_cont_mode = 0;
189static expand_T compl_xp;
190
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000191// List of flags for method of completion.
192static int compl_cont_status = 0;
193# define CONT_ADDING 1 // "normal" or "adding" expansion
194# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
195 // it's set only iff N_ADDS is set
196# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
197# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
198 // if so, word-wise-expansion will set SOL
199# define CONT_SOL 16 // pattern includes start of line, just for
200 // word-wise expansion, not set for ^X^L
201# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
202 // expansion, (eg use complete=.)
203
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100204static int compl_opt_refresh_always = FALSE;
205static int compl_opt_suppress_empty = FALSE;
206
glepnira218cc62024-06-03 19:32:39 +0200207static int compl_selected_item = -1;
208
glepnir8159fb12024-07-17 20:32:54 +0200209static int *compl_fuzzy_scores;
210
glepnir6a38aff2024-12-16 21:56:16 +0100211// "compl_match_array" points the currently displayed list of entries in the
212// popup menu. It is NULL when there is no popup menu.
213static pumitem_T *compl_match_array = NULL;
214static int compl_match_arraysize;
215
glepnirf31cfa22025-03-06 21:59:13 +0100216static 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, int score);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100217static void ins_compl_longest_match(compl_T *match);
218static void ins_compl_del_pum(void);
219static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
220static char_u *find_line_end(char_u *ptr);
221static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100222static int ins_compl_need_restart(void);
223static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000224static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100225static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100226static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100227static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
228# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
229static void ins_compl_add_list(list_T *list);
230static void ins_compl_add_dict(dict_T *dict);
231# endif
232static int ins_compl_key2dir(int c);
233static int ins_compl_pum_key(int c);
234static int ins_compl_key2count(int c);
235static void show_pum(int prev_w_wrow, int prev_w_leftcol);
236static unsigned quote_meta(char_u *dest, char_u *str, int len);
glepnir76bdb822025-02-08 19:04:51 +0100237static int ins_compl_has_multiple(void);
238static void ins_compl_expand_multiple(char_u *str);
glepnirf31cfa22025-03-06 21:59:13 +0100239static void ins_compl_longest_insert(char_u *prefix);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100240
241#ifdef FEAT_SPELL
242static void spell_back_to_badword(void);
243static int spell_bad_len = 0; // length of located bad word
244#endif
245
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100246/*
247 * CTRL-X pressed in Insert mode.
248 */
249 void
250ins_ctrl_x(void)
251{
zeertzjqdca29d92021-08-31 19:12:51 +0200252 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100253 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000254 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100255 if (compl_cont_status & CONT_N_ADDS)
256 compl_cont_status |= CONT_INTRPT;
257 else
258 compl_cont_status = 0;
259 // We're not sure which CTRL-X mode it will be yet
260 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
261 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
262 edit_submode_pre = NULL;
263 showmode();
264 }
zeertzjqdca29d92021-08-31 19:12:51 +0200265 else
266 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
267 // CTRL-V look like CTRL-N
268 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100269
LemonBoy2bf52dd2022-04-09 18:17:34 +0100270 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100271}
272
273/*
274 * Functions to check the current CTRL-X mode.
275 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000276int ctrl_x_mode_none(void)
277 { return ctrl_x_mode == 0; }
278int ctrl_x_mode_normal(void)
279 { return ctrl_x_mode == CTRL_X_NORMAL; }
280int ctrl_x_mode_scroll(void)
281 { return ctrl_x_mode == CTRL_X_SCROLL; }
282int ctrl_x_mode_whole_line(void)
283 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
284int ctrl_x_mode_files(void)
285 { return ctrl_x_mode == CTRL_X_FILES; }
286int ctrl_x_mode_tags(void)
287 { return ctrl_x_mode == CTRL_X_TAGS; }
288int ctrl_x_mode_path_patterns(void)
289 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
290int ctrl_x_mode_path_defines(void)
291 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
292int ctrl_x_mode_dictionary(void)
293 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
294int ctrl_x_mode_thesaurus(void)
295 { return ctrl_x_mode == CTRL_X_THESAURUS; }
296int ctrl_x_mode_cmdline(void)
297 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200298 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000299int ctrl_x_mode_function(void)
300 { return ctrl_x_mode == CTRL_X_FUNCTION; }
301int ctrl_x_mode_omni(void)
302 { return ctrl_x_mode == CTRL_X_OMNI; }
303int ctrl_x_mode_spell(void)
304 { return ctrl_x_mode == CTRL_X_SPELL; }
305static int ctrl_x_mode_eval(void)
306 { return ctrl_x_mode == CTRL_X_EVAL; }
307int ctrl_x_mode_line_or_eval(void)
308 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100309
310/*
311 * Whether other than default completion has been selected.
312 */
313 int
314ctrl_x_mode_not_default(void)
315{
316 return ctrl_x_mode != CTRL_X_NORMAL;
317}
318
319/*
zeertzjqdca29d92021-08-31 19:12:51 +0200320 * Whether CTRL-X was typed without a following character,
321 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100322 */
323 int
324ctrl_x_mode_not_defined_yet(void)
325{
326 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
327}
328
329/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000330 * Return TRUE if currently in "normal" or "adding" insert completion matches
331 * state
332 */
333 int
334compl_status_adding(void)
335{
336 return compl_cont_status & CONT_ADDING;
337}
338
339/*
340 * Return TRUE if the completion pattern includes start of line, just for
341 * word-wise expansion.
342 */
343 int
344compl_status_sol(void)
345{
346 return compl_cont_status & CONT_SOL;
347}
348
349/*
350 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
351 */
352 int
353compl_status_local(void)
354{
355 return compl_cont_status & CONT_LOCAL;
356}
357
358/*
359 * Clear the completion status flags
360 */
361 void
362compl_status_clear(void)
363{
364 compl_cont_status = 0;
365}
366
367/*
368 * Return TRUE if completion is using the forward direction matches
369 */
370 static int
371compl_dir_forward(void)
372{
373 return compl_direction == FORWARD;
374}
375
376/*
377 * Return TRUE if currently showing forward completion matches
378 */
379 static int
380compl_shows_dir_forward(void)
381{
382 return compl_shows_dir == FORWARD;
383}
384
385/*
386 * Return TRUE if currently showing backward completion matches
387 */
388 static int
389compl_shows_dir_backward(void)
390{
391 return compl_shows_dir == BACKWARD;
392}
393
394/*
395 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100396 */
397 int
398has_compl_option(int dict_opt)
399{
400 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200401#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100402 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200403#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100404 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100405 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
406#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100407 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100408#endif
409 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100410 {
411 ctrl_x_mode = CTRL_X_NORMAL;
412 edit_submode = NULL;
413 msg_attr(dict_opt ? _("'dictionary' option is empty")
414 : _("'thesaurus' option is empty"),
415 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100416 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100417 {
418 vim_beep(BO_COMPL);
419 setcursor();
420 out_flush();
421#ifdef FEAT_EVAL
422 if (!get_vim_var_nr(VV_TESTING))
423#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100424 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100425 }
426 return FALSE;
427 }
428 return TRUE;
429}
430
431/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000432 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100433 * This depends on the current mode.
434 */
435 int
436vim_is_ctrl_x_key(int c)
437{
438 // Always allow ^R - let its results then be checked
439 if (c == Ctrl_R)
440 return TRUE;
441
442 // Accept <PageUp> and <PageDown> if the popup menu is visible.
443 if (ins_compl_pum_key(c))
444 return TRUE;
445
446 switch (ctrl_x_mode)
447 {
448 case 0: // Not in any CTRL-X mode
449 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
450 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200451 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100452 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
453 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
454 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
455 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
456 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200457 || c == Ctrl_S || c == Ctrl_K || c == 's'
458 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100459 case CTRL_X_SCROLL:
460 return (c == Ctrl_Y || c == Ctrl_E);
461 case CTRL_X_WHOLE_LINE:
462 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
463 case CTRL_X_FILES:
464 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
465 case CTRL_X_DICTIONARY:
466 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
467 case CTRL_X_THESAURUS:
468 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
469 case CTRL_X_TAGS:
470 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
471#ifdef FEAT_FIND_ID
472 case CTRL_X_PATH_PATTERNS:
473 return (c == Ctrl_P || c == Ctrl_N);
474 case CTRL_X_PATH_DEFINES:
475 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
476#endif
477 case CTRL_X_CMDLINE:
478 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
479 || c == Ctrl_X);
480#ifdef FEAT_COMPL_FUNC
481 case CTRL_X_FUNCTION:
482 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
483 case CTRL_X_OMNI:
484 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
485#endif
486 case CTRL_X_SPELL:
487 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
488 case CTRL_X_EVAL:
489 return (c == Ctrl_P || c == Ctrl_N);
490 }
491 internal_error("vim_is_ctrl_x_key()");
492 return FALSE;
493}
494
495/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000496 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000497 */
498 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000499match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000500{
501 return match->cp_flags & CP_ORIGINAL_TEXT;
502}
503
504/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000505 * Returns TRUE if "match" is the first match in the completion list.
506 */
507 static int
508is_first_match(compl_T *match)
509{
510 return match == compl_first_match;
511}
512
513/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100514 * Return TRUE when character "c" is part of the item currently being
515 * completed. Used to decide whether to abandon complete mode when the menu
516 * is visible.
517 */
518 int
519ins_compl_accept_char(int c)
520{
521 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
522 // When expanding an identifier only accept identifier chars.
523 return vim_isIDc(c);
524
525 switch (ctrl_x_mode)
526 {
527 case CTRL_X_FILES:
528 // When expanding file name only accept file name chars. But not
529 // path separators, so that "proto/<Tab>" expands files in
530 // "proto", not "proto/" as a whole
531 return vim_isfilec(c) && !vim_ispathsep(c);
532
533 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200534 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100535 case CTRL_X_OMNI:
536 // Command line and Omni completion can work with just about any
537 // printable character, but do stop at white space.
538 return vim_isprintc(c) && !VIM_ISWHITE(c);
539
540 case CTRL_X_WHOLE_LINE:
541 // For while line completion a space can be part of the line.
542 return vim_isprintc(c);
543 }
544 return vim_iswordc(c);
545}
546
547/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100549 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000550 */
551 static char_u *
552ins_compl_infercase_gettext(
553 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100554 int char_len,
555 int compl_char_len,
556 int min_len,
557 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000558{
559 int *wca; // Wide character array.
560 char_u *p;
561 int i, c;
562 int has_lower = FALSE;
563 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100564 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565
566 IObuff[0] = NUL;
567
568 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100569 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000570 if (wca == NULL)
571 return IObuff;
572
573 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100574 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100575 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000576 if (has_mbyte)
577 wca[i] = mb_ptr2char_adv(&p);
578 else
579 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100580 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000581
582 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100583 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000584 for (i = 0; i < min_len; ++i)
585 {
glepnir6e199932024-12-14 21:13:27 +0100586 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000587 if (MB_ISLOWER(c))
588 {
589 has_lower = TRUE;
590 if (MB_ISUPPER(wca[i]))
591 {
592 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100593 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 wca[i] = MB_TOLOWER(wca[i]);
595 break;
596 }
597 }
598 }
599
600 // Rule 2: No lower case, 2nd consecutive letter converted to
601 // upper case.
602 if (!has_lower)
603 {
John Marriott5e6ea922024-11-23 14:01:57 +0100604 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000605 for (i = 0; i < min_len; ++i)
606 {
glepnir6e199932024-12-14 21:13:27 +0100607 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000608 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
609 {
610 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100611 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000612 wca[i] = MB_TOUPPER(wca[i]);
613 break;
614 }
615 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
616 }
617 }
618
619 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100620 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000621 for (i = 0; i < min_len; ++i)
622 {
glepnir6e199932024-12-14 21:13:27 +0100623 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000624 if (MB_ISLOWER(c))
625 wca[i] = MB_TOLOWER(wca[i]);
626 else if (MB_ISUPPER(c))
627 wca[i] = MB_TOUPPER(wca[i]);
628 }
629
630 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000631 p = IObuff;
632 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100633 ga_init2(&gap, 1, 500);
634 while (i < char_len)
635 {
636 if (gap.ga_data != NULL)
637 {
638 if (ga_grow(&gap, 10) == FAIL)
639 {
640 ga_clear(&gap);
641 return (char_u *)"[failed]";
642 }
643 p = (char_u *)gap.ga_data + gap.ga_len;
644 if (has_mbyte)
645 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
646 else
647 {
648 *p = wca[i++];
649 ++gap.ga_len;
650 }
651 }
652 else if ((p - IObuff) + 6 >= IOSIZE)
653 {
654 // Multi-byte characters can occupy up to five bytes more than
655 // ASCII characters, and we also need one byte for NUL, so when
656 // getting to six bytes from the edge of IObuff switch to using a
657 // growarray. Add the character in the next round.
658 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100659 {
660 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100661 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100662 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100663 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100664 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100665 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100666 }
667 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000668 p += (*mb_char2bytes)(wca[i++], p);
669 else
670 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100671 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000672 vim_free(wca);
673
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100674 if (gap.ga_data != NULL)
675 {
676 *tofree = gap.ga_data;
677 return gap.ga_data;
678 }
679
680 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000681 return IObuff;
682}
683
684/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100685 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
686 * case of the originally typed text is used, and the case of the completed
687 * text is inferred, ie this tries to work out what case you probably wanted
688 * the rest of the word to be in -- webb
689 */
690 int
691ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200692 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100693 int len,
694 int icase,
695 char_u *fname,
696 int dir,
glepnirf31cfa22025-03-06 21:59:13 +0100697 int cont_s_ipos, // next ^X<> will set initial_pos
698 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100699{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200700 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 int char_len; // count multi-byte characters
703 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100704 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200705 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100706 int res;
707 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100708
709 if (p_ic && curbuf->b_p_inf && len > 0)
710 {
711 // Infer case of completed part.
712
713 // Find actual length of completion.
714 if (has_mbyte)
715 {
716 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100717 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100718 while (*p != NUL)
719 {
720 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722 }
723 }
724 else
glepnir6e199932024-12-14 21:13:27 +0100725 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100726 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100727 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728
729 // Find actual length of original text.
730 if (has_mbyte)
731 {
John Marriott5e6ea922024-11-23 14:01:57 +0100732 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100734 while (*p != NUL)
735 {
736 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 }
739 }
740 else
glepnir6e199932024-12-14 21:13:27 +0100741 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100742 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100743 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100744
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100745 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100746 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100747 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100748
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100749 str = ins_compl_infercase_gettext(str, char_len,
750 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100751 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200752 if (cont_s_ipos)
753 flags |= CP_CONT_S_IPOS;
754 if (icase)
755 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200756
glepnirf31cfa22025-03-06 21:59:13 +0100757 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL, score);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100758 vim_free(tofree);
759 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100760}
761
762/*
glepnirf31cfa22025-03-06 21:59:13 +0100763 * Check if ctrl_x_mode has been configured in 'completefuzzycollect'
764 */
765 static int
766cfc_has_mode(void)
767{
768 switch (ctrl_x_mode)
769 {
770 case CTRL_X_NORMAL:
771 return (cfc_flags & CFC_KEYWORD) != 0;
772 case CTRL_X_FILES:
773 return (cfc_flags & CFC_FILES) != 0;
774 case CTRL_X_WHOLE_LINE:
775 return (cfc_flags & CFC_WHOLELINE) != 0;
776 default:
777 return FALSE;
778 }
779}
780
781/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000782 * Add a match to the list of matches. The arguments are:
783 * str - text of the match to add
784 * len - length of "str". If -1, then the length of "str" is
785 * computed.
786 * fname - file name to associate with this match.
787 * cptext - list of strings to use with this match (for abbr, menu, info
788 * and kind)
789 * user_data - user supplied data (any vim type) for this match
790 * cdir - match direction. If 0, use "compl_direction".
791 * flags_arg - match flags (cp_flags)
792 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100793 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000794 * If "cdir" is FORWARD, then the match is added after the current match.
795 * Otherwise, it is added before the current match.
796 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100797 * If the given string is already in the list of completions, then return
798 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
799 * maybe because alloc() returns NULL, then FAIL is returned.
800 */
801 static int
802ins_compl_add(
803 char_u *str,
804 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 char_u **cptext, // extra text for popup menu or NULL
807 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200809 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200810 int adup, // accept duplicate match
glepnirf31cfa22025-03-06 21:59:13 +0100811 int *user_hl, // user abbr/kind hlattr
812 int score)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100813{
glepnirf31cfa22025-03-06 21:59:13 +0100814 compl_T *match, *current, *prev;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200816 int flags = flags_arg;
glepnirf31cfa22025-03-06 21:59:13 +0100817 int inserted = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100818
Bram Moolenaarceb06192021-04-04 15:05:22 +0200819 if (flags & CP_FAST)
820 fast_breakcheck();
821 else
822 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 if (got_int)
824 return FAIL;
825 if (len < 0)
826 len = (int)STRLEN(str);
827
828 // If the same match is already present, don't add it.
829 if (compl_first_match != NULL && !adup)
830 {
831 match = compl_first_match;
832 do
833 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000834 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100835 && STRNCMP(match->cp_str.string, str, len) == 0
836 && ((int)match->cp_str.length <= len
837 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100838 return NOTDONE;
839 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000840 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100841 }
842
843 // Remove any popup menu before changing the list of matches.
844 ins_compl_del_pum();
845
846 // Allocate a new match structure.
847 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200848 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849 if (match == NULL)
850 return FAIL;
glepnir40891ba2025-02-10 22:18:00 +0100851 match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
John Marriott5e6ea922024-11-23 14:01:57 +0100852 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100853 {
854 vim_free(match);
855 return FAIL;
856 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100857
John Marriott5e6ea922024-11-23 14:01:57 +0100858 match->cp_str.length = len;
859
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100860 // match-fname is:
861 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200862 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863 // - NULL otherwise. --Acevedo
864 if (fname != NULL
865 && compl_curr_match != NULL
866 && compl_curr_match->cp_fname != NULL
867 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
868 match->cp_fname = compl_curr_match->cp_fname;
869 else if (fname != NULL)
870 {
871 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200872 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100873 }
874 else
875 match->cp_fname = NULL;
876 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100877 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
878 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
glepnirf31cfa22025-03-06 21:59:13 +0100879 match->cp_score = score;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100880
881 if (cptext != NULL)
882 {
883 int i;
884
885 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100886 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100887 if (cptext[i] != NULL && *cptext[i] != NUL)
888 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100889 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100890 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100891#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100892 if (user_data != NULL)
893 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100894#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100895
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000896 // Link the new match structure after (FORWARD) or before (BACKWARD) the
897 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100898 if (compl_first_match == NULL)
899 match->cp_next = match->cp_prev = NULL;
glepnirf31cfa22025-03-06 21:59:13 +0100900 else if (cfc_has_mode() && score > 0 && compl_get_longest)
901 {
902 current = compl_first_match->cp_next;
903 prev = compl_first_match;
904 inserted = FALSE;
905 // The direction is ignored when using longest and
906 // completefuzzycollect, because matches are inserted
907 // and sorted by score.
908 while (current != NULL && current != compl_first_match)
909 {
910 if (current->cp_score < score)
911 {
912 match->cp_next = current;
913 match->cp_prev = current->cp_prev;
914 if (current->cp_prev)
915 current->cp_prev->cp_next = match;
916 current->cp_prev = match;
917 inserted = TRUE;
918 break;
919 }
920 prev = current;
921 current = current->cp_next;
922 }
923 if (!inserted)
924 {
925 prev->cp_next = match;
926 match->cp_prev = prev;
927 match->cp_next = compl_first_match;
928 compl_first_match->cp_prev = match;
929 }
930 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 else if (dir == FORWARD)
932 {
933 match->cp_next = compl_curr_match->cp_next;
934 match->cp_prev = compl_curr_match;
935 }
936 else // BACKWARD
937 {
938 match->cp_next = compl_curr_match;
939 match->cp_prev = compl_curr_match->cp_prev;
940 }
941 if (match->cp_next)
942 match->cp_next->cp_prev = match;
943 if (match->cp_prev)
944 match->cp_prev->cp_next = match;
945 else // if there's nothing before, it is the first match
946 compl_first_match = match;
947 compl_curr_match = match;
948
949 // Find the longest common string if still doing that.
glepnirf31cfa22025-03-06 21:59:13 +0100950 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100951 ins_compl_longest_match(match);
952
953 return OK;
954}
955
956/*
957 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200958 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100959 */
960 static int
961ins_compl_equal(compl_T *match, char_u *str, int len)
962{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200963 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200964 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200965 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100966 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
967 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100968}
969
970/*
glepnir6a38aff2024-12-16 21:56:16 +0100971 * when len is -1 mean use whole length of p otherwise part of p
972 */
973 static void
974ins_compl_insert_bytes(char_u *p, int len)
975{
976 if (len == -1)
977 len = (int)STRLEN(p);
978 ins_bytes_len(p, len);
zeertzjqf25d8f92024-12-18 21:12:25 +0100979 compl_ins_end_col = curwin->w_cursor.col;
glepnir6a38aff2024-12-16 21:56:16 +0100980}
981
982/*
zeertzjqd32bf0a2024-12-17 20:55:13 +0100983 * Checks if the column is within the currently inserted completion text
984 * column range. If it is, it returns a special highlight attribute.
glepnir76bdb822025-02-08 19:04:51 +0100985 * -1 means normal item.
glepnir6a38aff2024-12-16 21:56:16 +0100986 */
987 int
glepnir76bdb822025-02-08 19:04:51 +0100988ins_compl_col_range_attr(linenr_T lnum, int col)
glepnir6a38aff2024-12-16 21:56:16 +0100989{
glepnir76bdb822025-02-08 19:04:51 +0100990 int start_col;
991 int attr;
992
993 if ((get_cot_flags() & COT_FUZZY)
994 || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0)
glepnire8908872025-01-08 18:30:45 +0100995 return -1;
996
glepnir76bdb822025-02-08 19:04:51 +0100997 start_col = compl_col + (int)ins_compl_leader_len();
998 if (!ins_compl_has_multiple())
999 return (col >= start_col && col < compl_ins_end_col) ? attr : -1;
1000
1001 // Multiple lines
1002 if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) ||
1003 (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) ||
1004 (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col))
1005 return attr;
glepnir6a38aff2024-12-16 21:56:16 +01001006
1007 return -1;
1008}
1009
1010/*
glepnir76bdb822025-02-08 19:04:51 +01001011 * Returns TRUE if the current completion string contains newline characters,
1012 * indicating it's a multi-line completion.
1013 */
1014 static int
1015ins_compl_has_multiple(void)
1016{
1017 return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL;
1018}
1019
1020/*
1021 * Returns TRUE if the given line number falls within the range of a multi-line
1022 * completion, i.e. between the starting line (compl_lnum) and current cursor
1023 * line. Always returns FALSE for single-line completions.
1024 */
1025 int
1026ins_compl_lnum_in_range(linenr_T lnum)
1027{
1028 if (!ins_compl_has_multiple())
1029 return FALSE;
1030 return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum;
1031}
1032
1033/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001034 * Reduce the longest common string for match "match".
1035 */
1036 static void
1037ins_compl_longest_match(compl_T *match)
1038{
1039 char_u *p, *s;
1040 int c1, c2;
1041 int had_match;
1042
John Marriott5e6ea922024-11-23 14:01:57 +01001043 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001044 {
1045 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +01001046 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
1047 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001048 return;
1049
John Marriott5e6ea922024-11-23 14:01:57 +01001050 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001051 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001052 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001053
1054 // When the match isn't there (to avoid matching itself) remove it
1055 // again after redrawing.
1056 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001058 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001059
1060 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001061 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001062
1063 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +01001064 p = compl_leader.string;
1065 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001066 while (*p != NUL)
1067 {
1068 if (has_mbyte)
1069 {
1070 c1 = mb_ptr2char(p);
1071 c2 = mb_ptr2char(s);
1072 }
1073 else
1074 {
1075 c1 = *p;
1076 c2 = *s;
1077 }
1078 if ((match->cp_flags & CP_ICASE)
1079 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
1080 break;
1081 if (has_mbyte)
1082 {
1083 MB_PTR_ADV(p);
1084 MB_PTR_ADV(s);
1085 }
1086 else
1087 {
1088 ++p;
1089 ++s;
1090 }
1091 }
1092
1093 if (*p != NUL)
1094 {
1095 // Leader was shortened, need to change the inserted text.
1096 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +01001097 compl_leader.length = (size_t)(p - compl_leader.string);
1098
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001099 had_match = (curwin->w_cursor.col > compl_col);
glepnirf31cfa22025-03-06 21:59:13 +01001100 ins_compl_longest_insert(compl_leader.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001101
1102 // When the match isn't there (to avoid matching itself) remove it
1103 // again after redrawing.
1104 if (!had_match)
1105 ins_compl_delete();
1106 }
1107
1108 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001109}
1110
1111/*
1112 * Add an array of matches to the list of matches.
1113 * Frees matches[].
1114 */
1115 static void
1116ins_compl_add_matches(
1117 int num_matches,
1118 char_u **matches,
1119 int icase)
1120{
1121 int i;
1122 int add_r = OK;
1123 int dir = compl_direction;
1124
1125 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001126 {
1127 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
glepnirf31cfa22025-03-06 21:59:13 +01001128 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0);
glepnir6e199932024-12-14 21:13:27 +01001129 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001130 // if dir was BACKWARD then honor it just once
1131 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001132 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001133 FreeWild(num_matches, matches);
1134}
1135
1136/*
1137 * Make the completion list cyclic.
1138 * Return the number of matches (excluding the original).
1139 */
1140 static int
1141ins_compl_make_cyclic(void)
1142{
1143 compl_T *match;
1144 int count = 0;
1145
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001146 if (compl_first_match == NULL)
1147 return 0;
1148
1149 // Find the end of the list.
1150 match = compl_first_match;
1151 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001152 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001153 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001154 match = match->cp_next;
1155 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001157 match->cp_next = compl_first_match;
1158 compl_first_match->cp_prev = match;
1159
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001160 return count;
1161}
1162
1163/*
1164 * Return whether there currently is a shown match.
1165 */
1166 int
1167ins_compl_has_shown_match(void)
1168{
1169 return compl_shown_match == NULL
1170 || compl_shown_match != compl_shown_match->cp_next;
1171}
1172
1173/*
1174 * Return whether the shown match is long enough.
1175 */
1176 int
1177ins_compl_long_shown_match(void)
1178{
John Marriott5e6ea922024-11-23 14:01:57 +01001179 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001180 > curwin->w_cursor.col - compl_col;
1181}
1182
1183/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001184 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001185 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001186 unsigned int
1187get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001188{
zeertzjq529b9ad2024-06-05 20:27:06 +02001189 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001190}
1191
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001192/*
1193 * Update the screen and when there is any scrolling remove the popup menu.
1194 */
1195 static void
1196ins_compl_upd_pum(void)
1197{
1198 int h;
1199
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001200 if (compl_match_array == NULL)
1201 return;
1202
1203 h = curwin->w_cline_height;
1204 // Update the screen later, before drawing the popup menu over it.
1205 pum_call_update_screen();
1206 if (h != curwin->w_cline_height)
1207 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001208}
1209
1210/*
1211 * Remove any popup menu.
1212 */
1213 static void
1214ins_compl_del_pum(void)
1215{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001216 if (compl_match_array == NULL)
1217 return;
1218
1219 pum_undisplay();
1220 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001221}
1222
1223/*
1224 * Return TRUE if the popup menu should be displayed.
1225 */
1226 int
1227pum_wanted(void)
1228{
1229 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001230 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001231 return FALSE;
1232
1233 // The display looks bad on a B&W display.
1234 if (t_colors < 8
1235#ifdef FEAT_GUI
1236 && !gui.in_use
1237#endif
1238 )
1239 return FALSE;
1240 return TRUE;
1241}
1242
1243/*
1244 * Return TRUE if there are two or more matches to be shown in the popup menu.
1245 * One if 'completopt' contains "menuone".
1246 */
1247 static int
1248pum_enough_matches(void)
1249{
1250 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001251 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001252
1253 // Don't display the popup menu if there are no matches or there is only
1254 // one (ignoring the original text).
1255 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001256 do
1257 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001258 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001259 break;
1260 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001261 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001262
zeertzjq529b9ad2024-06-05 20:27:06 +02001263 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001264 return (i >= 1);
1265 return (i >= 2);
1266}
1267
Bram Moolenaar3075a452021-11-17 15:51:52 +00001268#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001269/*
1270 * Allocate Dict for the completed item.
1271 * { word, abbr, menu, kind, info }
1272 */
1273 static dict_T *
1274ins_compl_dict_alloc(compl_T *match)
1275{
1276 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1277
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001278 if (dict == NULL)
1279 return NULL;
1280
John Marriott5e6ea922024-11-23 14:01:57 +01001281 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001282 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1283 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1284 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1285 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1286 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1287 dict_add_string(dict, "user_data", (char_u *)"");
1288 else
1289 dict_add_tv(dict, "user_data", &match->cp_user_data);
1290
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001291 return dict;
1292}
1293
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001294/*
1295 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1296 * completion menu is changed.
1297 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001298 static void
1299trigger_complete_changed_event(int cur)
1300{
1301 dict_T *v_event;
1302 dict_T *item;
1303 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001304 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001305
1306 if (recursive)
1307 return;
1308
glepnir40891ba2025-02-10 22:18:00 +01001309 item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001310 if (item == NULL)
1311 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001312 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001313 dict_add_dict(v_event, "completed_item", item);
1314 pum_set_event_info(v_event);
1315 dict_set_items_ro(v_event);
1316
1317 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001318 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001319 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001320 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001321 recursive = FALSE;
1322
Bram Moolenaar3075a452021-11-17 15:51:52 +00001323 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001324}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001325#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001326
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001327/*
glepnira218cc62024-06-03 19:32:39 +02001328 * pumitem qsort compare func
1329 */
1330 static int
zeertzjq8e567472024-06-14 20:04:42 +02001331ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001332{
1333 const int sa = (*(pumitem_T *)a).pum_score;
1334 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001335 const int ia = (*(pumitem_T *)a).pum_idx;
1336 const int ib = (*(pumitem_T *)b).pum_idx;
1337 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001338}
1339
1340/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001341 * Build a popup menu to show the completion matches.
1342 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1343 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001344 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001345 static int
1346ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001347{
1348 compl_T *compl;
1349 compl_T *shown_compl = NULL;
1350 int did_find_shown_match = FALSE;
1351 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001352 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001353 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001354 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001355 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001356 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
zeertzjqd65aa1b2025-01-25 15:29:03 +01001357 int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
1358 int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT);
glepnirf31cfa22025-03-06 21:59:13 +01001359
glepnir80b66202024-11-27 21:53:53 +01001360 compl_T *match_head = NULL;
1361 compl_T *match_tail = NULL;
1362 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001363
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001364 // Need to build the popup menu list.
1365 compl_match_arraysize = 0;
1366 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001367
glepnira49c0772024-11-30 10:56:30 +01001368 // If the current match is the original text don't find the first
1369 // match after it, don't highlight anything.
1370 if (match_at_original_text(compl_shown_match))
1371 shown_match_ok = TRUE;
1372
1373 if (compl_leader.string != NULL
1374 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1375 && shown_match_ok == FALSE)
1376 compl_shown_match = compl_no_select ? compl_first_match
1377 : compl_first_match->cp_next;
1378
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001379 do
1380 {
glepnird4088ed2024-12-31 10:55:22 +01001381 compl->cp_in_match_array = FALSE;
zeertzjq551d8c32024-06-05 19:53:32 +02001382 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1383 // set the cp_score for later comparisons.
glepnirf400a0c2025-01-23 19:55:14 +01001384 if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
John Marriott5e6ea922024-11-23 14:01:57 +01001385 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001386
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001387 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001388 && (compl_leader.string == NULL
1389 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnirf400a0c2025-01-23 19:55:14 +01001390 || (fuzzy_filter && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001391 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001392 ++compl_match_arraysize;
glepnird4088ed2024-12-31 10:55:22 +01001393 compl->cp_in_match_array = TRUE;
glepnir80b66202024-11-27 21:53:53 +01001394 if (match_head == NULL)
1395 match_head = compl;
1396 else
glepnira49c0772024-11-30 10:56:30 +01001397 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001398 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001399
glepnirf400a0c2025-01-23 19:55:14 +01001400 if (!shown_match_ok && !fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001401 {
1402 if (compl == compl_shown_match || did_find_shown_match)
1403 {
1404 // This item is the shown match or this is the
1405 // first displayed item after the shown match.
1406 compl_shown_match = compl;
1407 did_find_shown_match = TRUE;
1408 shown_match_ok = TRUE;
1409 }
1410 else
1411 // Remember this displayed match for when the
1412 // shown match is just below it.
1413 shown_compl = compl;
1414 cur = i;
1415 }
glepnirf400a0c2025-01-23 19:55:14 +01001416 else if (fuzzy_filter)
glepnira49c0772024-11-30 10:56:30 +01001417 {
1418 if (i == 0)
1419 shown_compl = compl;
1420 // Update the maximum fuzzy score and the shown match
1421 // if the current item's score is higher
zeertzjqd65aa1b2025-01-25 15:29:03 +01001422 if (fuzzy_sort && compl->cp_score > max_fuzzy_score)
glepnira49c0772024-11-30 10:56:30 +01001423 {
1424 did_find_shown_match = TRUE;
1425 max_fuzzy_score = compl->cp_score;
1426 if (!compl_no_select)
1427 compl_shown_match = compl;
1428 }
1429
glepnir3af0a8d2025-02-20 22:06:16 +01001430 if (!shown_match_ok && compl == compl_shown_match)
glepnira49c0772024-11-30 10:56:30 +01001431 {
1432 cur = i;
1433 shown_match_ok = TRUE;
1434 }
1435 }
1436 i++;
glepnir80b66202024-11-27 21:53:53 +01001437 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001438
glepnirf400a0c2025-01-23 19:55:14 +01001439 if (compl == compl_shown_match && !fuzzy_filter)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001440 {
1441 did_find_shown_match = TRUE;
1442
1443 // When the original text is the shown match don't set
1444 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001445 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001446 shown_match_ok = TRUE;
1447
1448 if (!shown_match_ok && shown_compl != NULL)
1449 {
1450 // The shown match isn't displayed, set it to the
1451 // previously displayed match.
1452 compl_shown_match = shown_compl;
1453 shown_match_ok = TRUE;
1454 }
1455 }
glepnira49c0772024-11-30 10:56:30 +01001456 compl = compl->cp_next;
1457 } while (compl != NULL && !is_first_match(compl));
1458
1459 if (compl_match_arraysize == 0)
1460 return -1;
1461
glepnirc0b7ca42025-02-13 20:27:44 +01001462 if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok)
1463 {
1464 compl_shown_match = shown_compl;
1465 shown_match_ok = TRUE;
1466 cur = 0;
1467 }
1468
glepnira49c0772024-11-30 10:56:30 +01001469 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1470 if (compl_match_array == NULL)
1471 return -1;
1472
1473 compl = match_head;
1474 i = 0;
1475 while (compl != NULL)
1476 {
glepnir6e199932024-12-14 21:13:27 +01001477 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1478 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001479 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1480 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1481 compl_match_array[i].pum_score = compl->cp_score;
1482 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1483 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001484 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1485 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001486 match_next = compl->cp_match_next;
1487 compl->cp_match_next = NULL;
1488 compl = match_next;
1489 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001490
zeertzjqd65aa1b2025-01-25 15:29:03 +01001491 if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001492 {
1493 for (i = 0; i < compl_match_arraysize; i++)
1494 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001495 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001496 qsort(compl_match_array, (size_t)compl_match_arraysize,
1497 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001498 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001499 }
glepnira218cc62024-06-03 19:32:39 +02001500
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001501 if (!shown_match_ok) // no displayed match at all
1502 cur = -1;
1503
1504 return cur;
1505}
1506
1507/*
1508 * Show the popup menu for the list of matches.
1509 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1510 */
1511 void
1512ins_compl_show_pum(void)
1513{
1514 int i;
1515 int cur = -1;
1516 colnr_T col;
1517
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001518 if (!pum_wanted() || !pum_enough_matches())
1519 return;
1520
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001521 // Update the screen later, before drawing the popup menu over it.
1522 pum_call_update_screen();
1523
1524 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001525 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001526 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001527 else
1528 {
1529 // popup menu already exists, only need to find the current item.
1530 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001531 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001532 || compl_match_array[i].pum_text
1533 == compl_shown_match->cp_text[CPT_ABBR])
1534 {
1535 cur = i;
1536 break;
1537 }
1538 }
1539
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001540 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001541 {
1542#ifdef FEAT_EVAL
1543 if (compl_started && has_completechanged())
1544 trigger_complete_changed_event(cur);
1545#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001546 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001547 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001548
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001549 // In Replace mode when a $ is displayed at the end of the line only
1550 // part of the screen would be updated. We do need to redraw here.
1551 dollar_vcol = -1;
1552
1553 // Compute the screen column of the start of the completed text.
1554 // Use the cursor to get all wrapping and other settings right.
1555 col = curwin->w_cursor.col;
1556 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001557 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001558 pum_display(compl_match_array, compl_match_arraysize, cur);
1559 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001560
glepnircbb46b42024-02-03 18:11:13 +01001561 // After adding leader, set the current match to shown match.
1562 if (compl_started && compl_curr_match != compl_shown_match)
1563 compl_curr_match = compl_shown_match;
1564
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001565#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001566 if (has_completechanged())
1567 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001568#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001569}
1570
1571#define DICT_FIRST (1) // use just first element in "dict"
1572#define DICT_EXACT (2) // "dict" is the exact name of a file
1573
1574/*
glepnir40c1c332024-06-11 19:37:04 +02001575 * Get current completion leader
1576 */
1577 char_u *
1578ins_compl_leader(void)
1579{
John Marriott5e6ea922024-11-23 14:01:57 +01001580 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1581}
1582
1583/*
1584 * Get current completion leader length
1585 */
1586 size_t
1587ins_compl_leader_len(void)
1588{
1589 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001590}
1591
1592/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001593 * Add any identifiers that match the given pattern "pat" in the list of
1594 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001595 */
1596 static void
1597ins_compl_dictionaries(
1598 char_u *dict_start,
1599 char_u *pat,
1600 int flags, // DICT_FIRST and/or DICT_EXACT
1601 int thesaurus) // Thesaurus completion
1602{
1603 char_u *dict = dict_start;
1604 char_u *ptr;
1605 char_u *buf;
1606 regmatch_T regmatch;
1607 char_u **files;
1608 int count;
1609 int save_p_scs;
1610 int dir = compl_direction;
1611
1612 if (*dict == NUL)
1613 {
1614#ifdef FEAT_SPELL
1615 // When 'dictionary' is empty and spell checking is enabled use
1616 // "spell".
1617 if (!thesaurus && curwin->w_p_spell)
1618 dict = (char_u *)"spell";
1619 else
1620#endif
1621 return;
1622 }
1623
1624 buf = alloc(LSIZE);
1625 if (buf == NULL)
1626 return;
1627 regmatch.regprog = NULL; // so that we can goto theend
1628
1629 // If 'infercase' is set, don't use 'smartcase' here
1630 save_p_scs = p_scs;
1631 if (curbuf->b_p_inf)
1632 p_scs = FALSE;
1633
1634 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1635 // to only match at the start of a line. Otherwise just match the
1636 // pattern. Also need to double backslashes.
1637 if (ctrl_x_mode_line_or_eval())
1638 {
1639 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1640 size_t len;
1641
1642 if (pat_esc == NULL)
1643 goto theend;
1644 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001645 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001646 if (ptr == NULL)
1647 {
1648 vim_free(pat_esc);
1649 goto theend;
1650 }
1651 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1652 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1653 vim_free(pat_esc);
1654 vim_free(ptr);
1655 }
1656 else
1657 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001658 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001659 if (regmatch.regprog == NULL)
1660 goto theend;
1661 }
1662
1663 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1664 regmatch.rm_ic = ignorecase(pat);
1665 while (*dict != NUL && !got_int && !compl_interrupted)
1666 {
1667 // copy one dictionary file name into buf
1668 if (flags == DICT_EXACT)
1669 {
1670 count = 1;
1671 files = &dict;
1672 }
1673 else
1674 {
1675 // Expand wildcards in the dictionary name, but do not allow
1676 // backticks (for security, the 'dict' option may have been set in
1677 // a modeline).
1678 copy_option_part(&dict, buf, LSIZE, ",");
1679# ifdef FEAT_SPELL
1680 if (!thesaurus && STRCMP(buf, "spell") == 0)
1681 count = -1;
1682 else
1683# endif
1684 if (vim_strchr(buf, '`') != NULL
1685 || expand_wildcards(1, &buf, &count, &files,
1686 EW_FILE|EW_SILENT) != OK)
1687 count = 0;
1688 }
1689
1690# ifdef FEAT_SPELL
1691 if (count == -1)
1692 {
1693 // Complete from active spelling. Skip "\<" in the pattern, we
1694 // don't use it as a RE.
1695 if (pat[0] == '\\' && pat[1] == '<')
1696 ptr = pat + 2;
1697 else
1698 ptr = pat;
1699 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1700 }
1701 else
1702# endif
1703 if (count > 0) // avoid warning for using "files" uninit
1704 {
1705 ins_compl_files(count, files, thesaurus, flags,
glepnirf31cfa22025-03-06 21:59:13 +01001706 (cfc_has_mode() ? NULL : &regmatch), buf, &dir);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 if (flags != DICT_EXACT)
1708 FreeWild(count, files);
1709 }
1710 if (flags != 0)
1711 break;
1712 }
1713
1714theend:
1715 p_scs = save_p_scs;
1716 vim_regfree(regmatch.regprog);
1717 vim_free(buf);
1718}
1719
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001720/*
1721 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1722 * skipping the word at 'skip_word'. Returns OK on success.
1723 */
1724 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001725thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001726 char_u *fname,
1727 char_u **buf_arg,
1728 int dir,
1729 char_u *skip_word)
1730{
1731 int status = OK;
1732 char_u *ptr;
1733 char_u *wstart;
1734
1735 // Add the other matches on the line
1736 ptr = *buf_arg;
1737 while (!got_int)
1738 {
1739 // Find start of the next word. Skip white
1740 // space and punctuation.
1741 ptr = find_word_start(ptr);
1742 if (*ptr == NUL || *ptr == NL)
1743 break;
1744 wstart = ptr;
1745
1746 // Find end of the word.
1747 if (has_mbyte)
1748 // Japanese words may have characters in
1749 // different classes, only separate words
1750 // with single-byte non-word characters.
1751 while (*ptr != NUL)
1752 {
1753 int l = (*mb_ptr2len)(ptr);
1754
1755 if (l < 2 && !vim_iswordc(*ptr))
1756 break;
1757 ptr += l;
1758 }
1759 else
1760 ptr = find_word_end(ptr);
1761
1762 // Add the word. Skip the regexp match.
1763 if (wstart != skip_word)
1764 {
1765 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01001766 fname, dir, FALSE, 0);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001767 if (status == FAIL)
1768 break;
1769 }
1770 }
1771
1772 *buf_arg = ptr;
1773 return status;
1774}
1775
1776/*
1777 * Process "count" dictionary/thesaurus "files" and add the text matching
1778 * "regmatch".
1779 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001780 static void
1781ins_compl_files(
1782 int count,
1783 char_u **files,
1784 int thesaurus,
1785 int flags,
1786 regmatch_T *regmatch,
1787 char_u *buf,
1788 int *dir)
1789{
1790 char_u *ptr;
1791 int i;
1792 FILE *fp;
1793 int add_r;
glepnirf31cfa22025-03-06 21:59:13 +01001794 char_u *leader = NULL;
1795 int leader_len = 0;
1796 int in_fuzzy_collect = cfc_has_mode() && ctrl_x_mode_normal();
1797 int score = 0;
1798 int len = 0;
1799 char_u *line_end = NULL;
1800
1801 if (in_fuzzy_collect)
1802 {
1803 leader = ins_compl_leader();
1804 leader_len = ins_compl_leader_len();
1805 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001806
1807 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1808 {
1809 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001810 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001811 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001812 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001813 vim_snprintf((char *)IObuff, IOSIZE,
1814 _("Scanning dictionary: %s"), (char *)files[i]);
1815 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1816 }
1817
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001818 if (fp == NULL)
1819 continue;
1820
1821 // Read dictionary file line by line.
1822 // Check each line for a match.
1823 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001824 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001825 ptr = buf;
glepnirf31cfa22025-03-06 21:59:13 +01001826 if (regmatch != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001827 {
glepnirf31cfa22025-03-06 21:59:13 +01001828 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 {
glepnirf31cfa22025-03-06 21:59:13 +01001830 ptr = regmatch->startp[0];
1831 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1832 : find_word_end(ptr);
1833 add_r = ins_compl_add_infercase(regmatch->startp[0],
1834 (int)(ptr - regmatch->startp[0]),
1835 p_ic, files[i], *dir, FALSE, 0);
1836 if (thesaurus)
1837 {
1838 // For a thesaurus, add all the words in the line
1839 ptr = buf;
1840 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
1841 regmatch->startp[0]);
1842 }
1843 if (add_r == OK)
1844 // if dir was BACKWARD then honor it just once
1845 *dir = FORWARD;
1846 else if (add_r == FAIL)
1847 break;
1848 // avoid expensive call to vim_regexec() when at end
1849 // of line
1850 if (*ptr == '\n' || got_int)
1851 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001852 }
glepnirf31cfa22025-03-06 21:59:13 +01001853 }
1854 else if (in_fuzzy_collect && leader_len > 0)
1855 {
1856 line_end = find_line_end(ptr);
1857 while (ptr < line_end)
1858 {
1859 if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score))
1860 {
1861 char_u *end_ptr = ctrl_x_mode_line_or_eval()
1862 ? find_line_end(ptr) : find_word_end(ptr);
1863 add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr),
1864 p_ic, files[i], *dir, FALSE, score);
1865 if (add_r == FAIL)
1866 break;
1867 ptr = end_ptr; // start from next word
1868 if (compl_get_longest && ctrl_x_mode_normal()
1869 && compl_first_match->cp_next
1870 && score == compl_first_match->cp_next->cp_score)
1871 compl_num_bests++;
1872 }
1873 else if (find_word_end(ptr) == line_end)
1874 break;
1875 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001876 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001877 line_breakcheck();
1878 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001879 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001880 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001881 }
1882}
1883
1884/*
1885 * Find the start of the next word.
1886 * Returns a pointer to the first char of the word. Also stops at a NUL.
1887 */
1888 char_u *
1889find_word_start(char_u *ptr)
1890{
1891 if (has_mbyte)
1892 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1893 ptr += (*mb_ptr2len)(ptr);
1894 else
1895 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1896 ++ptr;
1897 return ptr;
1898}
1899
1900/*
1901 * Find the end of the word. Assumes it starts inside a word.
1902 * Returns a pointer to just after the word.
1903 */
1904 char_u *
1905find_word_end(char_u *ptr)
1906{
1907 int start_class;
1908
1909 if (has_mbyte)
1910 {
1911 start_class = mb_get_class(ptr);
1912 if (start_class > 1)
1913 while (*ptr != NUL)
1914 {
1915 ptr += (*mb_ptr2len)(ptr);
1916 if (mb_get_class(ptr) != start_class)
1917 break;
1918 }
1919 }
1920 else
1921 while (vim_iswordc(*ptr))
1922 ++ptr;
1923 return ptr;
1924}
1925
1926/*
1927 * Find the end of the line, omitting CR and NL at the end.
1928 * Returns a pointer to just after the line.
1929 */
1930 static char_u *
1931find_line_end(char_u *ptr)
1932{
1933 char_u *s;
1934
1935 s = ptr + STRLEN(ptr);
1936 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1937 --s;
1938 return s;
1939}
1940
1941/*
1942 * Free the list of completions
1943 */
1944 static void
1945ins_compl_free(void)
1946{
1947 compl_T *match;
1948 int i;
1949
John Marriott5e6ea922024-11-23 14:01:57 +01001950 VIM_CLEAR_STRING(compl_pattern);
1951 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001952
1953 if (compl_first_match == NULL)
1954 return;
1955
1956 ins_compl_del_pum();
1957 pum_clear();
1958
1959 compl_curr_match = compl_first_match;
1960 do
1961 {
1962 match = compl_curr_match;
1963 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001964 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001965 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001966 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001967 vim_free(match->cp_fname);
1968 for (i = 0; i < CPT_COUNT; ++i)
1969 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001970#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001971 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001972#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001973 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001974 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001975 compl_first_match = compl_curr_match = NULL;
1976 compl_shown_match = NULL;
1977 compl_old_match = NULL;
1978}
1979
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001980/*
1981 * Reset/clear the completion state.
1982 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001983 void
1984ins_compl_clear(void)
1985{
1986 compl_cont_status = 0;
1987 compl_started = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01001988 compl_cfc_longest_ins = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001989 compl_matches = 0;
glepnir07f0dbe2025-02-18 20:27:30 +01001990 compl_selected_item = -1;
glepnir6a38aff2024-12-16 21:56:16 +01001991 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001992 VIM_CLEAR_STRING(compl_pattern);
1993 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001994 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001995 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001997#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001998 // clear v:completed_item
1999 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002000#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002001}
2002
2003/*
2004 * Return TRUE when Insert completion is active.
2005 */
2006 int
2007ins_compl_active(void)
2008{
2009 return compl_started;
2010}
2011
2012/*
glepnir8d0bb6d2024-12-24 09:44:35 +01002013 * Return True when wp is the actual completion window
2014 */
2015 int
2016ins_compl_win_active(win_T *wp UNUSED)
2017{
2018 return ins_compl_active()
2019#if defined(FEAT_QUICKFIX)
2020 && (!wp->w_p_pvw
2021# ifdef FEAT_PROP_POPUP
2022 && !(wp->w_popup_flags & POPF_INFO)
2023# endif
2024 )
2025#endif
2026 ;
2027}
2028
2029/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002030 * Selected one of the matches. When FALSE the match was edited or using the
2031 * longest common string.
2032 */
2033 int
2034ins_compl_used_match(void)
2035{
2036 return compl_used_match;
2037}
2038
2039/*
2040 * Initialize get longest common string.
2041 */
2042 void
2043ins_compl_init_get_longest(void)
2044{
2045 compl_get_longest = FALSE;
2046}
2047
2048/*
2049 * Returns TRUE when insert completion is interrupted.
2050 */
2051 int
2052ins_compl_interrupted(void)
2053{
2054 return compl_interrupted;
2055}
2056
2057/*
2058 * Returns TRUE if the <Enter> key selects a match in the completion popup
2059 * menu.
2060 */
2061 int
2062ins_compl_enter_selects(void)
2063{
2064 return compl_enter_selects;
2065}
2066
2067/*
2068 * Return the column where the text starts that is being completed
2069 */
2070 colnr_T
2071ins_compl_col(void)
2072{
2073 return compl_col;
2074}
2075
2076/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002077 * Return the length in bytes of the text being completed
2078 */
2079 int
2080ins_compl_len(void)
2081{
2082 return compl_length;
2083}
2084
2085/*
glepnir94a045e2025-03-01 16:12:23 +01002086 * Return TRUE when the 'completeopt' "preinsert" flag is in effect,
2087 * otherwise return FALSE.
glepniredd4ac32025-01-29 18:53:51 +01002088 */
2089 static int
2090ins_compl_has_preinsert(void)
2091{
glepnira2c55592025-02-28 17:43:42 +01002092 int cur_cot_flags = get_cot_flags();
glepnir94a045e2025-03-01 16:12:23 +01002093 return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE))
2094 == (COT_PREINSERT | COT_MENUONE);
glepniredd4ac32025-01-29 18:53:51 +01002095}
2096
2097/*
2098 * Returns TRUE if the pre-insert effect is valid and the cursor is within
2099 * the `compl_ins_end_col` range.
2100 */
2101 int
2102ins_compl_preinsert_effect(void)
2103{
2104 if (!ins_compl_has_preinsert())
2105 return FALSE;
2106
2107 return curwin->w_cursor.col < compl_ins_end_col;
2108}
2109
2110/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002111 * Delete one character before the cursor and show the subset of the matches
2112 * that match the word that is now before the cursor.
2113 * Returns the character to be used, NUL if the work is done and another char
2114 * to be got from the user.
2115 */
2116 int
2117ins_compl_bs(void)
2118{
2119 char_u *line;
2120 char_u *p;
2121
glepniredd4ac32025-01-29 18:53:51 +01002122 if (ins_compl_preinsert_effect())
2123 ins_compl_delete();
2124
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002125 line = ml_get_curline();
2126 p = line + curwin->w_cursor.col;
2127 MB_PTR_BACK(line, p);
2128
2129 // Stop completion when the whole word was deleted. For Omni completion
2130 // allow the word to be deleted, we won't match everything.
2131 // Respect the 'backspace' option.
2132 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002133 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
2134 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002135 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
2136 - compl_length < 0))
2137 return K_BS;
2138
2139 // Deleted more than what was used to find matches or didn't finish
2140 // finding all matches: need to look for matches all over again.
2141 if (curwin->w_cursor.col <= compl_col + compl_length
2142 || ins_compl_need_restart())
2143 ins_compl_restart();
2144
John Marriott5e6ea922024-11-23 14:01:57 +01002145 VIM_CLEAR_STRING(compl_leader);
2146 compl_leader.length = (size_t)((p - line) - compl_col);
2147 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
2148 if (compl_leader.string == NULL)
2149 {
2150 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002151 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01002152 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002153
2154 ins_compl_new_leader();
2155 if (compl_shown_match != NULL)
2156 // Make sure current match is not a hidden item.
2157 compl_curr_match = compl_shown_match;
2158 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002159}
2160
2161/*
2162 * Return TRUE when we need to find matches again, ins_compl_restart() is to
2163 * be called.
2164 */
2165 static int
2166ins_compl_need_restart(void)
2167{
2168 // Return TRUE if we didn't complete finding matches or when the
2169 // 'completefunc' returned "always" in the "refresh" dictionary item.
2170 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002171 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002172 && compl_opt_refresh_always);
2173}
2174
2175/*
2176 * Called after changing "compl_leader".
2177 * Show the popup menu with a different set of matches.
2178 * May also search for matches again if the previous search was interrupted.
2179 */
2180 static void
2181ins_compl_new_leader(void)
2182{
2183 ins_compl_del_pum();
2184 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01002185 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002186 compl_used_match = FALSE;
2187
2188 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01002189 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002190 else
2191 {
2192#ifdef FEAT_SPELL
2193 spell_bad_len = 0; // need to redetect bad word
2194#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002195 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002196 // the popup menu display the changed text before the cursor. Set
2197 // "compl_restarting" to avoid that the first match is inserted.
2198 pum_call_update_screen();
2199#ifdef FEAT_GUI
2200 if (gui.in_use)
2201 {
2202 // Show the cursor after the match, not after the redrawn text.
2203 setcursor();
2204 out_flush_cursor(FALSE, FALSE);
2205 }
2206#endif
2207 compl_restarting = TRUE;
2208 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2209 compl_cont_status = 0;
2210 compl_restarting = FALSE;
2211 }
2212
glepnir44180412025-02-20 22:09:48 +01002213 compl_enter_selects = !compl_used_match && compl_selected_item != -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002214
2215 // Show the popup menu with a different set of matches.
2216 ins_compl_show_pum();
2217
2218 // Don't let Enter select the original text when there is no popup menu.
2219 if (compl_match_array == NULL)
2220 compl_enter_selects = FALSE;
glepniredd4ac32025-01-29 18:53:51 +01002221 else if (ins_compl_has_preinsert() && compl_leader.length > 0)
2222 ins_compl_insert(FALSE, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002223}
2224
2225/*
2226 * Return the length of the completion, from the completion start column to
2227 * the cursor column. Making sure it never goes below zero.
2228 */
2229 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002230get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002231{
2232 int off = (int)curwin->w_cursor.col - (int)compl_col;
glepnir40891ba2025-02-10 22:18:00 +01002233 return MAX(0, off);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002234}
2235
2236/*
2237 * Append one character to the match leader. May reduce the number of
2238 * matches.
2239 */
2240 void
2241ins_compl_addleader(int c)
2242{
2243 int cc;
2244
glepniredd4ac32025-01-29 18:53:51 +01002245 if (ins_compl_preinsert_effect())
2246 ins_compl_delete();
2247
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002248 if (stop_arrow() == FAIL)
2249 return;
2250 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2251 {
2252 char_u buf[MB_MAXBYTES + 1];
2253
2254 (*mb_char2bytes)(c, buf);
2255 buf[cc] = NUL;
2256 ins_char_bytes(buf, cc);
2257 if (compl_opt_refresh_always)
2258 AppendToRedobuff(buf);
2259 }
2260 else
2261 {
2262 ins_char(c);
2263 if (compl_opt_refresh_always)
2264 AppendCharToRedobuff(c);
2265 }
2266
2267 // If we didn't complete finding matches we must search again.
2268 if (ins_compl_need_restart())
2269 ins_compl_restart();
2270
2271 // When 'always' is set, don't reset compl_leader. While completing,
2272 // cursor doesn't point original position, changing compl_leader would
2273 // break redo.
2274 if (!compl_opt_refresh_always)
2275 {
John Marriott5e6ea922024-11-23 14:01:57 +01002276 VIM_CLEAR_STRING(compl_leader);
2277 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2278 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2279 compl_leader.length);
2280 if (compl_leader.string == NULL)
2281 {
2282 compl_leader.length = 0;
2283 return;
2284 }
2285
2286 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002287 }
2288}
2289
2290/*
2291 * Setup for finding completions again without leaving CTRL-X mode. Used when
2292 * BS or a key was typed while still searching for matches.
2293 */
2294 static void
2295ins_compl_restart(void)
2296{
2297 ins_compl_free();
2298 compl_started = FALSE;
2299 compl_matches = 0;
2300 compl_cont_status = 0;
2301 compl_cont_mode = 0;
2302}
2303
2304/*
2305 * Set the first match, the original text.
2306 */
2307 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002308ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002309{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002310 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002311 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2312 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002313 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002314 {
John Marriott5e6ea922024-11-23 14:01:57 +01002315 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002316 if (p != NULL)
2317 {
John Marriott5e6ea922024-11-23 14:01:57 +01002318 VIM_CLEAR_STRING(compl_first_match->cp_str);
2319 compl_first_match->cp_str.string = p;
2320 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002321 }
2322 }
2323 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002324 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002325 {
John Marriott5e6ea922024-11-23 14:01:57 +01002326 char_u *p = vim_strnsave(str, len);
2327 if (p != NULL)
2328 {
2329 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2330 compl_first_match->cp_prev->cp_str.string = p;
2331 compl_first_match->cp_prev->cp_str.length = len;
2332 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002333 }
2334}
2335
2336/*
2337 * Append one character to the match leader. May reduce the number of
2338 * matches.
2339 */
2340 void
2341ins_compl_addfrommatch(void)
2342{
2343 char_u *p;
2344 int len = (int)curwin->w_cursor.col - (int)compl_col;
2345 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002346
John Marriott5e6ea922024-11-23 14:01:57 +01002347 p = compl_shown_match->cp_str.string;
2348 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002349 {
John Marriott5e6ea922024-11-23 14:01:57 +01002350 size_t plen;
2351 compl_T *cp;
2352
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002353 // When still at the original match use the first entry that matches
2354 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002355 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002356 return;
2357
2358 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002359 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002360 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002361 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002362 {
John Marriott5e6ea922024-11-23 14:01:57 +01002363 if (compl_leader.string == NULL
2364 || ins_compl_equal(cp, compl_leader.string,
2365 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002366 {
John Marriott5e6ea922024-11-23 14:01:57 +01002367 p = cp->cp_str.string;
2368 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002369 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002370 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002371 }
John Marriott5e6ea922024-11-23 14:01:57 +01002372 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002373 return;
2374 }
2375 p += len;
2376 c = PTR2CHAR(p);
2377 ins_compl_addleader(c);
2378}
2379
2380/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002381 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002382 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2383 * compl_cont_mode and compl_cont_status.
2384 * Returns TRUE when the character is not to be inserted.
2385 */
2386 static int
2387set_ctrl_x_mode(int c)
2388{
2389 int retval = FALSE;
2390
2391 switch (c)
2392 {
2393 case Ctrl_E:
2394 case Ctrl_Y:
2395 // scroll the window one line up or down
2396 ctrl_x_mode = CTRL_X_SCROLL;
2397 if (!(State & REPLACE_FLAG))
2398 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2399 else
2400 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2401 edit_submode_pre = NULL;
2402 showmode();
2403 break;
2404 case Ctrl_L:
2405 // complete whole line
2406 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2407 break;
2408 case Ctrl_F:
2409 // complete filenames
2410 ctrl_x_mode = CTRL_X_FILES;
2411 break;
2412 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002413 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002414 ctrl_x_mode = CTRL_X_DICTIONARY;
2415 break;
2416 case Ctrl_R:
2417 // Register insertion without exiting CTRL-X mode
2418 // Simply allow ^R to happen without affecting ^X mode
2419 break;
2420 case Ctrl_T:
2421 // complete words from a thesaurus
2422 ctrl_x_mode = CTRL_X_THESAURUS;
2423 break;
2424#ifdef FEAT_COMPL_FUNC
2425 case Ctrl_U:
2426 // user defined completion
2427 ctrl_x_mode = CTRL_X_FUNCTION;
2428 break;
2429 case Ctrl_O:
2430 // omni completion
2431 ctrl_x_mode = CTRL_X_OMNI;
2432 break;
2433#endif
2434 case 's':
2435 case Ctrl_S:
2436 // complete spelling suggestions
2437 ctrl_x_mode = CTRL_X_SPELL;
2438#ifdef FEAT_SPELL
2439 ++emsg_off; // Avoid getting the E756 error twice.
2440 spell_back_to_badword();
2441 --emsg_off;
2442#endif
2443 break;
2444 case Ctrl_RSB:
2445 // complete tag names
2446 ctrl_x_mode = CTRL_X_TAGS;
2447 break;
2448#ifdef FEAT_FIND_ID
2449 case Ctrl_I:
2450 case K_S_TAB:
2451 // complete keywords from included files
2452 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2453 break;
2454 case Ctrl_D:
2455 // complete definitions from included files
2456 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2457 break;
2458#endif
2459 case Ctrl_V:
2460 case Ctrl_Q:
2461 // complete vim commands
2462 ctrl_x_mode = CTRL_X_CMDLINE;
2463 break;
2464 case Ctrl_Z:
2465 // stop completion
2466 ctrl_x_mode = CTRL_X_NORMAL;
2467 edit_submode = NULL;
2468 showmode();
2469 retval = TRUE;
2470 break;
2471 case Ctrl_P:
2472 case Ctrl_N:
2473 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2474 // just started ^X mode, or there were enough ^X's to cancel
2475 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2476 // do normal expansion when interrupting a different mode (say
2477 // ^X^F^X^P or ^P^X^X^P, see below)
2478 // nothing changes if interrupting mode 0, (eg, the flag
2479 // doesn't change when going to ADDING mode -- Acevedo
2480 if (!(compl_cont_status & CONT_INTRPT))
2481 compl_cont_status |= CONT_LOCAL;
2482 else if (compl_cont_mode != 0)
2483 compl_cont_status &= ~CONT_LOCAL;
2484 // FALLTHROUGH
2485 default:
2486 // If we have typed at least 2 ^X's... for modes != 0, we set
2487 // compl_cont_status = 0 (eg, as if we had just started ^X
2488 // mode).
2489 // For mode 0, we set "compl_cont_mode" to an impossible
2490 // value, in both cases ^X^X can be used to restart the same
2491 // mode (avoiding ADDING mode).
2492 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2493 // 'complete' and local ^P expansions respectively.
2494 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2495 // mode -- Acevedo
2496 if (c == Ctrl_X)
2497 {
2498 if (compl_cont_mode != 0)
2499 compl_cont_status = 0;
2500 else
2501 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2502 }
2503 ctrl_x_mode = CTRL_X_NORMAL;
2504 edit_submode = NULL;
2505 showmode();
2506 break;
2507 }
2508
2509 return retval;
2510}
2511
2512/*
glepnir1c5a1202024-12-04 20:27:34 +01002513 * Trigger CompleteDone event and adds relevant information to v:event
2514 */
2515 static void
2516trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2517{
2518#if defined(FEAT_EVAL)
2519 save_v_event_T save_v_event;
2520 dict_T *v_event = get_v_event(&save_v_event);
2521 char_u *mode_str = NULL;
2522
2523 mode = mode & ~CTRL_X_WANT_IDENT;
2524 if (ctrl_x_mode_names[mode])
2525 mode_str = (char_u *)ctrl_x_mode_names[mode];
2526
2527 (void)dict_add_string(v_event, "complete_word",
2528 word == NULL ? (char_u *)"" : word);
2529 (void)dict_add_string(v_event, "complete_type",
2530 mode_str != NULL ? mode_str : (char_u *)"");
2531
2532 dict_set_items_ro(v_event);
2533#endif
2534 ins_apply_autocmds(EVENT_COMPLETEDONE);
2535
2536#if defined(FEAT_EVAL)
2537 restore_v_event(v_event, &save_v_event);
2538#endif
2539}
2540
2541/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002542 * Stop insert completion mode
2543 */
2544 static int
2545ins_compl_stop(int c, int prev_mode, int retval)
2546{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002547 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002548 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002549
2550 // Get here when we have finished typing a sequence of ^N and
2551 // ^P or other completion characters in CTRL-X mode. Free up
2552 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002553 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002554 {
glepnir40891ba2025-02-10 22:18:00 +01002555 char_u *ptr = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002556
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002557 // If any of the original typed text has been changed, eg when
2558 // ignorecase is set, we must add back-spaces to the redo
2559 // buffer. We add as few as necessary to delete just the part
2560 // of the original text that has changed.
2561 // When using the longest match, edited the match or used
2562 // CTRL-E then don't use the current match.
2563 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002564 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002565 ins_compl_fixRedoBufForLeader(ptr);
2566 }
2567
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002568 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002569
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002570 // When completing whole lines: fix indent for 'cindent'.
2571 // Otherwise, break line if it's too long.
2572 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2573 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002574 // re-indent the current line
2575 if (want_cindent)
2576 {
2577 do_c_expr_indent();
2578 want_cindent = FALSE; // don't do it again
2579 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002580 }
2581 else
2582 {
2583 int prev_col = curwin->w_cursor.col;
2584
2585 // put the cursor on the last char, for 'tw' formatting
2586 if (prev_col > 0)
2587 dec_cursor();
2588 // only format when something was inserted
2589 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2590 insertchar(NUL, 0, -1);
2591 if (prev_col > 0
2592 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2593 inc_cursor();
2594 }
2595
2596 // If the popup menu is displayed pressing CTRL-Y means accepting
2597 // the selection without inserting anything. When
2598 // compl_enter_selects is set the Enter key does the same.
2599 if ((c == Ctrl_Y || (compl_enter_selects
2600 && (c == CAR || c == K_KENTER || c == NL)))
2601 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002602 {
2603 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002604 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002605 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002606
2607 // CTRL-E means completion is Ended, go back to the typed text.
2608 // but only do this, if the Popup is still visible
2609 if (c == Ctrl_E)
2610 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002611 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002612 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002613
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002614 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002615 if (compl_leader.string != NULL)
2616 {
2617 p = compl_leader.string;
2618 plen = compl_leader.length;
2619 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002620 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002621 {
2622 p = compl_orig_text.string;
2623 plen = compl_orig_text.length;
2624 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002625 if (p != NULL)
2626 {
2627 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002628
John Marriott5e6ea922024-11-23 14:01:57 +01002629 if ((int)plen > compl_len)
zeertzjqf25d8f92024-12-18 21:12:25 +01002630 ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002631 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002632 retval = TRUE;
2633 }
2634
glepnir001c26c2025-02-02 09:36:22 +01002635 if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect())
2636 ins_compl_delete();
2637
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002638 auto_format(FALSE, TRUE);
2639
2640 // Trigger the CompleteDonePre event to give scripts a chance to
2641 // act upon the completion before clearing the info, and restore
2642 // ctrl_x_mode, so that complete_info() can be used.
2643 ctrl_x_mode = prev_mode;
2644 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2645
2646 ins_compl_free();
2647 compl_started = FALSE;
2648 compl_matches = 0;
2649 if (!shortmess(SHM_COMPLETIONMENU))
2650 msg_clr_cmdline(); // necessary for "noshowmode"
2651 ctrl_x_mode = CTRL_X_NORMAL;
2652 compl_enter_selects = FALSE;
2653 if (edit_submode != NULL)
2654 {
2655 edit_submode = NULL;
2656 showmode();
2657 }
2658
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002659 if (c == Ctrl_C && cmdwin_type != 0)
2660 // Avoid the popup menu remains displayed when leaving the
2661 // command line window.
2662 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002663 // Indent now if a key was typed that is in 'cinkeys'.
2664 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2665 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002666 // Trigger the CompleteDone event to give scripts a chance to act
2667 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002668 trigger_complete_done_event(prev_mode, word);
2669 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002670
2671 return retval;
2672}
2673
2674/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002675 * Prepare for Insert mode completion, or stop it.
2676 * Called just after typing a character in Insert mode.
2677 * Returns TRUE when the character is not to be inserted;
2678 */
2679 int
2680ins_compl_prep(int c)
2681{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002682 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002683 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002684
2685 // Forget any previous 'special' messages if this is actually
2686 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2687 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2688 edit_submode_extra = NULL;
2689
zeertzjq440d4cb2023-03-02 17:51:32 +00002690 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002691 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002692 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002693 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002694 return retval;
2695
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002696#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002697 // Ignore mouse events in a popup window
2698 if (is_mouse_key(c))
2699 {
2700 // Ignore drag and release events, the position does not need to be in
2701 // the popup and it may have just closed.
2702 if (c == K_LEFTRELEASE
2703 || c == K_LEFTRELEASE_NM
2704 || c == K_MIDDLERELEASE
2705 || c == K_RIGHTRELEASE
2706 || c == K_X1RELEASE
2707 || c == K_X2RELEASE
2708 || c == K_LEFTDRAG
2709 || c == K_MIDDLEDRAG
2710 || c == K_RIGHTDRAG
2711 || c == K_X1DRAG
2712 || c == K_X2DRAG)
2713 return retval;
2714 if (popup_visible)
2715 {
2716 int row = mouse_row;
2717 int col = mouse_col;
2718 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2719
2720 if (wp != NULL && WIN_IS_POPUP(wp))
2721 return retval;
2722 }
2723 }
2724#endif
2725
zeertzjqdca29d92021-08-31 19:12:51 +02002726 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2727 {
2728 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2729 || !vim_is_ctrl_x_key(c))
2730 {
2731 // Not starting another completion mode.
2732 ctrl_x_mode = CTRL_X_CMDLINE;
2733
2734 // CTRL-X CTRL-Z should stop completion without inserting anything
2735 if (c == Ctrl_Z)
2736 retval = TRUE;
2737 }
2738 else
2739 {
2740 ctrl_x_mode = CTRL_X_CMDLINE;
2741
2742 // Other CTRL-X keys first stop completion, then start another
2743 // completion mode.
2744 ins_compl_prep(' ');
2745 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2746 }
2747 }
2748
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002749 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002750 if (ctrl_x_mode_not_defined_yet()
2751 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002753 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002754 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755 }
2756
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002757 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2759 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002760 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002761 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002762 {
2763 // We're already in CTRL-X mode, do we stay in it?
2764 if (!vim_is_ctrl_x_key(c))
2765 {
glepnir40891ba2025-02-10 22:18:00 +01002766 ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002767 edit_submode = NULL;
2768 }
2769 showmode();
2770 }
2771
2772 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2773 {
2774 // Show error message from attempted keyword completion (probably
2775 // 'Pattern not found') until another key is hit, then go back to
2776 // showing what mode we are in.
2777 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002778 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002779 && c != Ctrl_R && !ins_compl_pum_key(c))
2780 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002781 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002782 }
2783 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2784 // Trigger the CompleteDone event to give scripts a chance to act
2785 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002786 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002787
LemonBoy2bf52dd2022-04-09 18:17:34 +01002788 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002789
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002790 // reset continue_* if we left expansion-mode, if we stay they'll be
2791 // (re)set properly in ins_complete()
2792 if (!vim_is_ctrl_x_key(c))
2793 {
2794 compl_cont_status = 0;
2795 compl_cont_mode = 0;
2796 }
2797
2798 return retval;
2799}
2800
2801/*
2802 * Fix the redo buffer for the completion leader replacing some of the typed
2803 * text. This inserts backspaces and appends the changed text.
2804 * "ptr" is the known leader text or NUL.
2805 */
2806 static void
2807ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2808{
John Marriott5e6ea922024-11-23 14:01:57 +01002809 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002810 char_u *p;
2811 char_u *ptr = ptr_arg;
2812
2813 if (ptr == NULL)
2814 {
John Marriott5e6ea922024-11-23 14:01:57 +01002815 if (compl_leader.string != NULL)
2816 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002817 else
2818 return; // nothing to do
2819 }
John Marriott5e6ea922024-11-23 14:01:57 +01002820 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002821 {
John Marriott5e6ea922024-11-23 14:01:57 +01002822 p = compl_orig_text.string;
glepnir40891ba2025-02-10 22:18:00 +01002823 // Find length of common prefix between original text and new completion
2824 while (p[len] != NUL && p[len] == ptr[len])
2825 len++;
2826 // Adjust length to not break inside a multi-byte character
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002827 if (len > 0)
2828 len -= (*mb_head_off)(p, p + len);
glepnir40891ba2025-02-10 22:18:00 +01002829 // Add backspace characters for each remaining character in
2830 // original text
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002831 for (p += len; *p != NUL; MB_PTR_ADV(p))
2832 AppendCharToRedobuff(K_BS);
2833 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002834 if (ptr != NULL)
2835 AppendToRedobuffLit(ptr + len, -1);
2836}
2837
2838/*
2839 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2840 * (depending on flag) starting from buf and looking for a non-scanned
2841 * buffer (other than curbuf). curbuf is special, if it is called with
2842 * buf=curbuf then it has to be the first call for a given flag/expansion.
2843 *
2844 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2845 */
2846 static buf_T *
2847ins_compl_next_buf(buf_T *buf, int flag)
2848{
2849 static win_T *wp = NULL;
glepnir40891ba2025-02-10 22:18:00 +01002850 int skip_buffer;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002851
2852 if (flag == 'w') // just windows
2853 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002854 if (buf == curbuf || !win_valid(wp))
2855 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002856 wp = curwin;
glepnir40891ba2025-02-10 22:18:00 +01002857
2858 while (TRUE)
2859 {
2860 // Move to next window (wrap to first window if at the end)
2861 wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2862 // Break if we're back at start or found an unscanned buffer
2863 if (wp == curwin || !wp->w_buffer->b_scanned)
2864 break;
2865 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002866 buf = wp->w_buffer;
2867 }
2868 else
glepnir40891ba2025-02-10 22:18:00 +01002869 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2871 // (unlisted buffers)
2872 // When completing whole lines skip unloaded buffers.
glepnir40891ba2025-02-10 22:18:00 +01002873 while (TRUE)
2874 {
2875 // Move to next buffer (wrap to first buffer if at the end)
2876 buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2877 // Break if we're back at start buffer
2878 if (buf == curbuf)
2879 break;
2880
2881 // Check buffer conditions based on flag
2882 if (flag == 'U')
2883 skip_buffer = buf->b_p_bl;
2884 else
2885 skip_buffer = !buf->b_p_bl ||
2886 (buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2887
2888 // Break if we found a buffer that matches our criteria
2889 if (!skip_buffer && !buf->b_scanned)
2890 break;
2891 }
2892 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893 return buf;
2894}
2895
2896#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002897
2898# ifdef FEAT_EVAL
2899static callback_T cfu_cb; // 'completefunc' callback function
2900static callback_T ofu_cb; // 'omnifunc' callback function
2901static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2902# endif
2903
2904/*
2905 * Copy a global callback function to a buffer local callback.
2906 */
2907 static void
2908copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2909{
2910 free_callback(bufcb);
2911 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2912 copy_callback(bufcb, globcb);
2913}
2914
2915/*
2916 * Parse the 'completefunc' option value and set the callback function.
2917 * Invoked when the 'completefunc' option is set. The option value can be a
2918 * name of a function (string), or function(<name>) or funcref(<name>) or a
2919 * lambda expression.
2920 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002921 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002922did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002923{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002924 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2925 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002926
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002927 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002928
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002929 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002930}
2931
2932/*
2933 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002934 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002935 */
2936 void
2937set_buflocal_cfu_callback(buf_T *buf UNUSED)
2938{
2939# ifdef FEAT_EVAL
2940 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2941# endif
2942}
2943
2944/*
2945 * Parse the 'omnifunc' option value and set the callback function.
2946 * Invoked when the 'omnifunc' option is set. The option value can be a
2947 * name of a function (string), or function(<name>) or funcref(<name>) or a
2948 * lambda expression.
2949 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002950 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002951did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002952{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002953 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2954 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002955
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002956 set_buflocal_ofu_callback(curbuf);
2957 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002958}
2959
2960/*
2961 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002962 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002963 */
2964 void
2965set_buflocal_ofu_callback(buf_T *buf UNUSED)
2966{
2967# ifdef FEAT_EVAL
2968 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2969# endif
2970}
2971
2972/*
2973 * Parse the 'thesaurusfunc' option value and set the callback function.
2974 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2975 * name of a function (string), or function(<name>) or funcref(<name>) or a
2976 * lambda expression.
2977 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002978 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002979did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002980{
2981 int retval;
2982
zeertzjq6eda2692024-11-03 09:23:33 +01002983 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002984 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002985 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2986 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002987 else
2988 {
2989 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002990 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002991 // when using :set, free the local callback
2992 if (!(args->os_flags & OPT_GLOBAL))
2993 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002994 }
2995
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002997}
2998
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002999/*
3000 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003001 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003002 */
3003 int
3004set_ref_in_insexpand_funcs(int copyID)
3005{
3006 int abort = FALSE;
3007
3008 abort = set_ref_in_callback(&cfu_cb, copyID);
3009 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
3010 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
3011
3012 return abort;
3013}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003014
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003015/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003016 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003017 */
3018 static char_u *
3019get_complete_funcname(int type)
3020{
3021 switch (type)
3022 {
3023 case CTRL_X_FUNCTION:
3024 return curbuf->b_p_cfu;
3025 case CTRL_X_OMNI:
3026 return curbuf->b_p_ofu;
3027 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003028 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003029 default:
3030 return (char_u *)"";
3031 }
3032}
3033
3034/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003035 * Get the callback to use for insert mode completion.
3036 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003037 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003038get_insert_callback(int type)
3039{
3040 if (type == CTRL_X_FUNCTION)
3041 return &curbuf->b_cfu_cb;
3042 if (type == CTRL_X_OMNI)
3043 return &curbuf->b_ofu_cb;
3044 // CTRL_X_THESAURUS
3045 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
3046}
3047
3048/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003049 * Execute user defined complete function 'completefunc', 'omnifunc' or
3050 * 'thesaurusfunc', and get matches in "matches".
3051 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003052 */
3053 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00003054expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003055{
3056 list_T *matchlist = NULL;
3057 dict_T *matchdict = NULL;
3058 typval_T args[3];
3059 char_u *funcname;
3060 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003061 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003062 typval_T rettv;
3063 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003064 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003065
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003066 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003067 if (*funcname == NUL)
3068 return;
3069
3070 // Call 'completefunc' to obtain the list of matches.
3071 args[0].v_type = VAR_NUMBER;
3072 args[0].vval.v_number = 0;
3073 args[1].v_type = VAR_STRING;
3074 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
3075 args[2].v_type = VAR_UNKNOWN;
3076
3077 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01003078 // Lock the text to avoid weird things from happening. Also disallow
3079 // switching to another window, it should not be needed and may end up in
3080 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01003081 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003082
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003083 cb = get_insert_callback(type);
3084 retval = call_callback(cb, 0, &rettv, 2, args);
3085
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003086 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003087 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003088 {
3089 switch (rettv.v_type)
3090 {
3091 case VAR_LIST:
3092 matchlist = rettv.vval.v_list;
3093 break;
3094 case VAR_DICT:
3095 matchdict = rettv.vval.v_dict;
3096 break;
3097 case VAR_SPECIAL:
3098 if (rettv.vval.v_number == VVAL_NONE)
3099 compl_opt_suppress_empty = TRUE;
3100 // FALLTHROUGH
3101 default:
3102 // TODO: Give error message?
3103 clear_tv(&rettv);
3104 break;
3105 }
3106 }
zeertzjqcfe45652022-05-27 17:26:55 +01003107 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003109 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02003110 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003111 validate_cursor();
3112 if (!EQUAL_POS(curwin->w_cursor, pos))
3113 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003114 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003115 goto theend;
3116 }
3117
3118 if (matchlist != NULL)
3119 ins_compl_add_list(matchlist);
3120 else if (matchdict != NULL)
3121 ins_compl_add_dict(matchdict);
3122
3123theend:
3124 // Restore State, it might have been changed.
3125 State = save_State;
3126
3127 if (matchdict != NULL)
3128 dict_unref(matchdict);
3129 if (matchlist != NULL)
3130 list_unref(matchlist);
3131}
3132#endif // FEAT_COMPL_FUNC
3133
3134#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02003135
3136 static inline int
3137get_user_highlight_attr(char_u *hlname)
3138{
3139 if (hlname != NULL && *hlname != NUL)
3140 return syn_name2attr(hlname);
3141 return -1;
3142}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003143/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003144 * Add a match to the list of matches from a typeval_T.
3145 * If the given string is already in the list of completions, then return
3146 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3147 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02003148 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003149 */
3150 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02003151ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003152{
3153 char_u *word;
3154 int dup = FALSE;
3155 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02003156 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003157 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003158 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003159 int status;
glepnir0fe17f82024-10-08 22:26:44 +02003160 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02003161 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01003162 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003163
Bram Moolenaar08928322020-01-04 14:32:48 +01003164 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003165 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3166 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003167 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
3168 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
3169 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
3170 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
3171 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02003172
glepnir0fe17f82024-10-08 22:26:44 +02003173 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003174 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02003175
3176 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01003177 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02003178
Bram Moolenaard61efa52022-07-23 09:52:04 +01003179 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
3180 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
3181 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003182 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01003183 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
3184 dup = dict_get_number(tv->vval.v_dict, "dup");
3185 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
3186 empty = dict_get_number(tv->vval.v_dict, "empty");
3187 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
3188 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003189 flags |= CP_EQUAL;
3190 }
3191 else
3192 {
3193 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003194 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003195 }
3196 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003197 {
3198 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003199 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003200 }
glepnir508e7852024-07-25 21:39:08 +02003201 status = ins_compl_add(word, -1, NULL, cptext,
glepnirf31cfa22025-03-06 21:59:13 +01003202 &user_data, dir, flags, dup, user_hl, 0);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003203 if (status != OK)
3204 clear_tv(&user_data);
3205 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003206}
3207
3208/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003209 * Add completions from a list.
3210 */
3211 static void
3212ins_compl_add_list(list_T *list)
3213{
3214 listitem_T *li;
3215 int dir = compl_direction;
3216
3217 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003218 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003219 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003220 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003221 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003222 // if dir was BACKWARD then honor it just once
3223 dir = FORWARD;
3224 else if (did_emsg)
3225 break;
3226 }
3227}
3228
3229/*
3230 * Add completions from a dict.
3231 */
3232 static void
3233ins_compl_add_dict(dict_T *dict)
3234{
3235 dictitem_T *di_refresh;
3236 dictitem_T *di_words;
3237
3238 // Check for optional "refresh" item.
3239 compl_opt_refresh_always = FALSE;
3240 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3241 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3242 {
3243 char_u *v = di_refresh->di_tv.vval.v_string;
3244
3245 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3246 compl_opt_refresh_always = TRUE;
3247 }
3248
3249 // Add completions from a "words" list.
3250 di_words = dict_find(dict, (char_u *)"words", 5);
3251 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3252 ins_compl_add_list(di_words->di_tv.vval.v_list);
3253}
3254
3255/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003256 * Start completion for the complete() function.
3257 * "startcol" is where the matched text starts (1 is first column).
3258 * "list" is the list of matches.
3259 */
3260 static void
3261set_completion(colnr_T startcol, list_T *list)
3262{
3263 int save_w_wrow = curwin->w_wrow;
3264 int save_w_leftcol = curwin->w_leftcol;
3265 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003266 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003267 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3268 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3269 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003270
3271 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003272 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003273 ins_compl_prep(' ');
3274 ins_compl_clear();
3275 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003276 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003277
3278 compl_direction = FORWARD;
3279 if (startcol > curwin->w_cursor.col)
3280 startcol = curwin->w_cursor.col;
3281 compl_col = startcol;
glepnir76bdb822025-02-08 19:04:51 +01003282 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003283 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3284 // compl_pattern doesn't need to be set
glepniredd4ac32025-01-29 18:53:51 +01003285 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col,
3286 (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003287 if (p_ic)
3288 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003289 if (compl_orig_text.string == NULL)
3290 {
3291 compl_orig_text.length = 0;
3292 return;
3293 }
3294 compl_orig_text.length = (size_t)compl_length;
3295 if (ins_compl_add(compl_orig_text.string,
3296 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
glepnirf31cfa22025-03-06 21:59:13 +01003297 flags | CP_FAST, FALSE, NULL, 0) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003298 return;
3299
3300 ctrl_x_mode = CTRL_X_EVAL;
3301
3302 ins_compl_add_list(list);
3303 compl_matches = ins_compl_make_cyclic();
3304 compl_started = TRUE;
3305 compl_used_match = TRUE;
3306 compl_cont_status = 0;
3307
3308 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003309 int no_select = compl_no_select || compl_longest;
3310 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003311 {
3312 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003313 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003314 // Down/Up has no real effect.
3315 ins_complete(K_UP, FALSE);
3316 }
3317 else
3318 ins_complete(Ctrl_N, FALSE);
3319 compl_enter_selects = compl_no_insert;
3320
3321 // Lazily show the popup menu, unless we got interrupted.
3322 if (!compl_interrupted)
3323 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003324 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003325 out_flush();
3326}
3327
3328/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003329 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003330 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003331 void
3332f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003333{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003334 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003335
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003336 if (in_vim9script()
3337 && (check_for_number_arg(argvars, 0) == FAIL
3338 || check_for_list_arg(argvars, 1) == FAIL))
3339 return;
3340
Bram Moolenaar24959102022-05-07 20:01:16 +01003341 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003342 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003343 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003344 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003345 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003346
3347 // Check for undo allowed here, because if something was already inserted
3348 // the line was already saved for undo and this check isn't done.
3349 if (!undo_allowed())
3350 return;
3351
Bram Moolenaard83392a2022-09-01 12:22:46 +01003352 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003353 {
3354 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3355 if (startcol > 0)
3356 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003357 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003358}
3359
3360/*
3361 * "complete_add()" function
3362 */
3363 void
3364f_complete_add(typval_T *argvars, typval_T *rettv)
3365{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003366 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003367 return;
3368
Bram Moolenaar440cf092021-04-03 20:13:30 +02003369 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003370}
3371
3372/*
3373 * "complete_check()" function
3374 */
3375 void
3376f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3377{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003378 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003379 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003380
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003381 ins_compl_check_keys(0, TRUE);
3382 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003383
3384 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003385}
3386
3387/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003388 * Return Insert completion mode name string
3389 */
3390 static char_u *
3391ins_compl_mode(void)
3392{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003393 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003394 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3395
3396 return (char_u *)"";
3397}
3398
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003399/*
3400 * Assign the sequence number to all the completion matches which don't have
3401 * one assigned yet.
3402 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003403 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003404ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003405{
3406 int number = 0;
3407 compl_T *match;
3408
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003409 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003410 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003411 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003412 // This should normally succeed already at the first loop
3413 // cycle, so it's fast!
3414 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003415 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003416 if (match->cp_number != -1)
3417 {
3418 number = match->cp_number;
3419 break;
3420 }
3421 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003422 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003423 for (match = match->cp_next;
3424 match != NULL && match->cp_number == -1;
3425 match = match->cp_next)
3426 match->cp_number = ++number;
3427 }
3428 else // BACKWARD
3429 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003430 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003431 // number. This should normally succeed already at the
3432 // first loop cycle, so it's fast!
3433 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003434 && !is_first_match(match); match = match->cp_next)
glepnir40891ba2025-02-10 22:18:00 +01003435 {
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003436 if (match->cp_number != -1)
3437 {
3438 number = match->cp_number;
3439 break;
3440 }
glepnir40891ba2025-02-10 22:18:00 +01003441 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003442 if (match != NULL)
glepnir40891ba2025-02-10 22:18:00 +01003443 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003444 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003445 for (match = match->cp_prev; match
3446 && match->cp_number == -1;
3447 match = match->cp_prev)
3448 match->cp_number = ++number;
glepnir40891ba2025-02-10 22:18:00 +01003449 }
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003450 }
3451}
3452
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003453/*
glepnir037b0282025-01-16 14:37:44 +01003454 * Fill the dict of complete_info
3455 */
3456 static void
3457fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
3458{
3459 dict_add_string(di, "word", match->cp_str.string);
3460 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3461 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3462 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3463 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3464 if (add_match)
3465 dict_add_bool(di, "match", match->cp_in_match_array);
3466 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3467 // Add an empty string for backwards compatibility
3468 dict_add_string(di, "user_data", (char_u *)"");
3469 else
3470 dict_add_tv(di, "user_data", &match->cp_user_data);
3471}
3472
3473/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003474 * Get complete information
3475 */
3476 static void
3477get_complete_info(list_T *what_list, dict_T *retdict)
3478{
3479 int ret = OK;
3480 listitem_T *item;
3481#define CI_WHAT_MODE 0x01
3482#define CI_WHAT_PUM_VISIBLE 0x02
3483#define CI_WHAT_ITEMS 0x04
3484#define CI_WHAT_SELECTED 0x08
glepnir037b0282025-01-16 14:37:44 +01003485#define CI_WHAT_COMPLETED 0x10
glepnird4088ed2024-12-31 10:55:22 +01003486#define CI_WHAT_MATCHES 0x20
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003487#define CI_WHAT_ALL 0xff
3488 int what_flag;
3489
3490 if (what_list == NULL)
glepnir037b0282025-01-16 14:37:44 +01003491 what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003492 else
3493 {
3494 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003495 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003496 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003497 {
3498 char_u *what = tv_get_string(&item->li_tv);
3499
3500 if (STRCMP(what, "mode") == 0)
3501 what_flag |= CI_WHAT_MODE;
3502 else if (STRCMP(what, "pum_visible") == 0)
3503 what_flag |= CI_WHAT_PUM_VISIBLE;
3504 else if (STRCMP(what, "items") == 0)
3505 what_flag |= CI_WHAT_ITEMS;
3506 else if (STRCMP(what, "selected") == 0)
3507 what_flag |= CI_WHAT_SELECTED;
glepnir037b0282025-01-16 14:37:44 +01003508 else if (STRCMP(what, "completed") == 0)
3509 what_flag |= CI_WHAT_COMPLETED;
glepnird4088ed2024-12-31 10:55:22 +01003510 else if (STRCMP(what, "matches") == 0)
3511 what_flag |= CI_WHAT_MATCHES;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003512 }
3513 }
3514
3515 if (ret == OK && (what_flag & CI_WHAT_MODE))
3516 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3517
3518 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3519 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3520
glepnir037b0282025-01-16 14:37:44 +01003521 if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
3522 | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003523 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003524 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003525 dict_T *di;
3526 compl_T *match;
3527 int selected_idx = -1;
glepnird4088ed2024-12-31 10:55:22 +01003528 int has_items = what_flag & CI_WHAT_ITEMS;
3529 int has_matches = what_flag & CI_WHAT_MATCHES;
glepnir037b0282025-01-16 14:37:44 +01003530 int has_completed = what_flag & CI_WHAT_COMPLETED;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003531
glepnird4088ed2024-12-31 10:55:22 +01003532 if (has_items || has_matches)
Girish Palya8950bf72024-03-20 20:07:29 +01003533 {
3534 li = list_alloc();
3535 if (li == NULL)
3536 return;
glepnird4088ed2024-12-31 10:55:22 +01003537 ret = dict_add_list(retdict, (has_matches && !has_items)
3538 ? "matches" : "items", li);
Girish Palya8950bf72024-03-20 20:07:29 +01003539 }
3540 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3541 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3542 ins_compl_update_sequence_numbers();
3543 if (ret == OK && compl_first_match != NULL)
3544 {
3545 int list_idx = 0;
3546 match = compl_first_match;
3547 do
3548 {
3549 if (!match_at_original_text(match))
3550 {
glepnird4088ed2024-12-31 10:55:22 +01003551 if (has_items
3552 || (has_matches && match->cp_in_match_array))
Girish Palya8950bf72024-03-20 20:07:29 +01003553 {
3554 di = dict_alloc();
3555 if (di == NULL)
3556 return;
3557 ret = list_append_dict(li, di);
3558 if (ret != OK)
3559 return;
glepnir037b0282025-01-16 14:37:44 +01003560 fill_complete_info_dict(di, match, has_matches && has_items);
Girish Palya8950bf72024-03-20 20:07:29 +01003561 }
glepnird4088ed2024-12-31 10:55:22 +01003562 if (compl_curr_match != NULL
3563 && compl_curr_match->cp_number == match->cp_number)
Girish Palya8950bf72024-03-20 20:07:29 +01003564 selected_idx = list_idx;
3565 list_idx += 1;
3566 }
3567 match = match->cp_next;
3568 }
3569 while (match != NULL && !is_first_match(match));
3570 }
3571 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3572 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003573
glepnir037b0282025-01-16 14:37:44 +01003574 if (ret == OK && selected_idx != -1 && has_completed)
3575 {
3576 di = dict_alloc();
3577 if (di == NULL)
3578 return;
3579 fill_complete_info_dict(di, compl_curr_match, FALSE);
3580 ret = dict_add_dict(retdict, "completed", di);
3581 }
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003582 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003583}
3584
3585/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003586 * "complete_info()" function
3587 */
3588 void
3589f_complete_info(typval_T *argvars, typval_T *rettv)
3590{
3591 list_T *what_list = NULL;
3592
Bram Moolenaar93a10962022-06-16 11:42:09 +01003593 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003594 return;
3595
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003596 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3597 return;
3598
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003599 if (argvars[0].v_type != VAR_UNKNOWN)
3600 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003601 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003602 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003603 what_list = argvars[0].vval.v_list;
3604 }
3605 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003606}
3607#endif
3608
3609/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003610 * Returns TRUE when using a user-defined function for thesaurus completion.
3611 */
3612 static int
3613thesaurus_func_complete(int type UNUSED)
3614{
3615#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003616 return type == CTRL_X_THESAURUS
3617 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003618#else
3619 return FALSE;
3620#endif
3621}
3622
3623/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 * Return value of process_next_cpt_value()
3625 */
3626enum
3627{
3628 INS_COMPL_CPT_OK = 1,
3629 INS_COMPL_CPT_CONT,
3630 INS_COMPL_CPT_END
3631};
3632
3633/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003634 * state information used for getting the next set of insert completion
3635 * matches.
3636 */
3637typedef struct
3638{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003639 char_u *e_cpt_copy; // copy of 'complete'
3640 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003641 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003642 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003643 pos_T prev_match_pos; // previous match position
3644 int set_match_pos; // save first_match_pos/last_match_pos
3645 pos_T first_match_pos; // first match position
3646 pos_T last_match_pos; // last match position
3647 int found_all; // found all matches of a certain type.
3648 char_u *dict; // dictionary file to search
3649 int dict_f; // "dict" is an exact file name or not
3650} ins_compl_next_state_T;
3651
3652/*
3653 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 *
3655 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003656 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003657 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003658 * st->found_all - all matches of this type are found
3659 * st->ins_buf - search for completions in this buffer
3660 * st->first_match_pos - position of the first completion match
3661 * st->last_match_pos - position of the last completion match
3662 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003663 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003664 * st->dict - name of the dictionary or thesaurus file to search
3665 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666 *
3667 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003668 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3669 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003670 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003671 */
3672 static int
3673process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003674 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003676 pos_T *start_match_pos,
3677 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003678{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003679 int compl_type = -1;
3680 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003681
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003682 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003683
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003684 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3685 st->e_cpt++;
3686
3687 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003688 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003689 st->ins_buf = curbuf;
3690 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691 // Move the cursor back one character so that ^N can match the
3692 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003693 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003694 {
3695 // Move the cursor to after the last character in the
3696 // buffer, so that word at start of buffer is found
3697 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003698 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003699 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003700 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003701 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003702 compl_type = 0;
3703
3704 // Remember the first match so that the loop stops when we
3705 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003706 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003707 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003708 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003709 && (st->ins_buf = ins_compl_next_buf(
3710 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003711 {
3712 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003713 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003714 {
3715 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003716 st->first_match_pos.col = st->last_match_pos.col = 0;
3717 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3718 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003719 compl_type = 0;
3720 }
3721 else // unloaded buffer, scan like dictionary
3722 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003723 st->found_all = TRUE;
3724 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003725 {
3726 status = INS_COMPL_CPT_CONT;
3727 goto done;
3728 }
3729 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003730 st->dict = st->ins_buf->b_fname;
3731 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003732 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003733 if (!shortmess(SHM_COMPLETIONSCAN))
3734 {
3735 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3736 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3737 st->ins_buf->b_fname == NULL
3738 ? buf_spname(st->ins_buf)
3739 : st->ins_buf->b_sfname == NULL
3740 ? st->ins_buf->b_fname
3741 : st->ins_buf->b_sfname);
3742 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3743 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003744 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003745 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003746 status = INS_COMPL_CPT_END;
3747 else
3748 {
3749 if (ctrl_x_mode_line_or_eval())
3750 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003751 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003752 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003753 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003754 compl_type = CTRL_X_DICTIONARY;
3755 else
3756 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003757 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003758 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003759 st->dict = st->e_cpt;
3760 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003761 }
3762 }
3763#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003764 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003765 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003766 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003767 compl_type = CTRL_X_PATH_DEFINES;
3768#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003769 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003770 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003771 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003772 if (!shortmess(SHM_COMPLETIONSCAN))
3773 {
3774 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3775 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3776 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3777 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003778 }
3779 else
3780 compl_type = -1;
3781
3782 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003783 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003784
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003785 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003786 if (compl_type == -1)
3787 status = INS_COMPL_CPT_CONT;
3788 }
3789
3790done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003791 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003792 return status;
3793}
3794
3795#ifdef FEAT_FIND_ID
3796/*
3797 * Get the next set of identifiers or defines matching "compl_pattern" in
3798 * included files.
3799 */
3800 static void
3801get_next_include_file_completion(int compl_type)
3802{
John Marriott5e6ea922024-11-23 14:01:57 +01003803 find_pattern_in_path(compl_pattern.string, compl_direction,
3804 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003805 (compl_type == CTRL_X_PATH_DEFINES
3806 && !(compl_cont_status & CONT_SOL))
3807 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003808 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003809}
3810#endif
3811
3812/*
3813 * Get the next set of words matching "compl_pattern" in dictionary or
3814 * thesaurus files.
3815 */
3816 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003817get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003818{
3819#ifdef FEAT_COMPL_FUNC
3820 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003821 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003822 else
3823#endif
3824 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003825 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003826 : (compl_type == CTRL_X_THESAURUS
3827 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3828 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003829 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003830 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003831 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003832}
3833
3834/*
3835 * Get the next set of tag names matching "compl_pattern".
3836 */
3837 static void
3838get_next_tag_completion(void)
3839{
3840 int save_p_ic;
3841 char_u **matches;
3842 int num_matches;
3843
3844 // set p_ic according to p_ic, p_scs and pat for find_tags().
3845 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003846 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003847
3848 // Find up to TAG_MANY matches. Avoids that an enormous number
3849 // of matches is found when compl_pattern is empty
3850 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003851 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003852 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003853 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003854 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3855 ins_compl_add_matches(num_matches, matches, p_ic);
3856 g_tag_at_cursor = FALSE;
3857 p_ic = save_p_ic;
3858}
3859
3860/*
glepnir8159fb12024-07-17 20:32:54 +02003861 * Compare function for qsort
3862 */
glepnirf31cfa22025-03-06 21:59:13 +01003863 static int
3864compare_scores(const void *a, const void *b)
glepnir8159fb12024-07-17 20:32:54 +02003865{
3866 int idx_a = *(const int *)a;
3867 int idx_b = *(const int *)b;
3868 int score_a = compl_fuzzy_scores[idx_a];
3869 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003870 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3871 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003872}
3873
3874/*
glepnirf31cfa22025-03-06 21:59:13 +01003875 * insert prefix with redraw
3876 */
3877 static void
3878ins_compl_longest_insert(char_u *prefix)
3879{
3880 ins_compl_delete();
3881 ins_compl_insert_bytes(prefix + get_compl_len(), -1);
3882 ins_redraw(FALSE);
3883}
3884
3885/*
3886 * Calculate the longest common prefix among the best fuzzy matches
3887 * stored in compl_best_matches, and insert it as the longest.
3888 */
3889 static void
3890fuzzy_longest_match(void)
3891{
3892 char_u *prefix = NULL;
3893 int prefix_len = 0;
3894 int i = 0;
3895 int j = 0;
3896 char_u *match_str = NULL;
3897 char_u *prefix_ptr = NULL;
3898 char_u *match_ptr = NULL;
3899 char_u *leader = NULL;
3900 size_t leader_len = 0;
3901 compl_T *compl = NULL;
3902 int more_candidates = FALSE;
3903 compl_T *nn_compl = NULL;
3904
3905 if (compl_num_bests == 0)
3906 return;
3907
3908 nn_compl = compl_first_match->cp_next->cp_next;
3909 if (nn_compl && nn_compl != compl_first_match)
3910 more_candidates = TRUE;
3911
3912 compl = ctrl_x_mode_whole_line() ? compl_first_match
3913 : compl_first_match->cp_next;
3914 if (compl_num_bests == 1)
3915 {
3916 // no more candidates insert the match str
3917 if (!more_candidates)
3918 {
3919 ins_compl_longest_insert(compl->cp_str.string);
3920 compl_num_bests = 0;
3921 }
3922 compl_num_bests = 0;
3923 return;
3924 }
3925
3926 compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *));
3927 if (compl_best_matches == NULL)
3928 return;
3929 while (compl != NULL && i < compl_num_bests)
3930 {
3931 compl_best_matches[i] = compl;
3932 compl = compl->cp_next;
3933 i++;
3934 }
3935
3936 prefix = compl_best_matches[0]->cp_str.string;
zeertzjq4422de62025-03-07 19:06:02 +01003937 prefix_len = (int)compl_best_matches[0]->cp_str.length;
glepnirf31cfa22025-03-06 21:59:13 +01003938
3939 for (i = 1; i < compl_num_bests; i++)
3940 {
3941 match_str = compl_best_matches[i]->cp_str.string;
3942 prefix_ptr = prefix;
3943 match_ptr = match_str;
3944 j = 0;
3945
3946 while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL)
3947 {
3948 if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0)
3949 break;
3950
3951 MB_PTR_ADV(prefix_ptr);
3952 MB_PTR_ADV(match_ptr);
3953 j++;
3954 }
3955
3956 if (j > 0)
3957 prefix_len = j;
3958 }
3959
3960 leader = ins_compl_leader();
zeertzjq4422de62025-03-07 19:06:02 +01003961 leader_len = ins_compl_leader_len();
glepnirf31cfa22025-03-06 21:59:13 +01003962
3963 // skip non-consecutive prefixes
zeertzjq4422de62025-03-07 19:06:02 +01003964 if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0)
glepnirf31cfa22025-03-06 21:59:13 +01003965 goto end;
3966
zeertzjq4422de62025-03-07 19:06:02 +01003967 prefix = vim_strnsave(prefix, prefix_len);
glepnirf31cfa22025-03-06 21:59:13 +01003968 if (prefix != NULL)
3969 {
3970 ins_compl_longest_insert(prefix);
3971 compl_cfc_longest_ins = TRUE;
3972 vim_free(prefix);
3973 }
3974
3975end:
3976 vim_free(compl_best_matches);
3977 compl_best_matches = NULL;
3978 compl_num_bests = 0;
3979}
3980
3981/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003982 * Get the next set of filename matching "compl_pattern".
3983 */
3984 static void
3985get_next_filename_completion(void)
3986{
3987 char_u **matches;
3988 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003989 char_u *ptr;
3990 garray_T fuzzy_indices;
3991 int i;
3992 int score;
3993 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003994 size_t leader_len = ins_compl_leader_len();;
glepnirf31cfa22025-03-06 21:59:13 +01003995 int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0);
glepnir8159fb12024-07-17 20:32:54 +02003996 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003997 char_u *last_sep = NULL;
glepnirf31cfa22025-03-06 21:59:13 +01003998 int need_collect_bests = in_fuzzy_collect && compl_get_longest;
3999 int max_score = 0;
4000 int current_score = 0;
4001 int dir = compl_direction;
glepnir8159fb12024-07-17 20:32:54 +02004002
glepnirb9de1a02024-08-02 19:14:38 +02004003#ifdef BACKSLASH_IN_FILENAME
4004 char pathsep = (curbuf->b_p_csl[0] == 's') ?
4005 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
4006#else
4007 char pathsep = PATHSEP;
4008#endif
4009
glepnirf31cfa22025-03-06 21:59:13 +01004010 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004011 {
glepnirb9de1a02024-08-02 19:14:38 +02004012#ifdef BACKSLASH_IN_FILENAME
4013 if (curbuf->b_p_csl[0] == 's')
4014 {
John Marriott5e6ea922024-11-23 14:01:57 +01004015 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004016 {
4017 if (leader[i] == '\\')
4018 leader[i] = '/';
4019 }
4020 }
4021 else if (curbuf->b_p_csl[0] == 'b')
4022 {
John Marriott5e6ea922024-11-23 14:01:57 +01004023 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02004024 {
4025 if (leader[i] == '/')
4026 leader[i] = '\\';
4027 }
4028 }
4029#endif
4030 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02004031 if (last_sep == NULL)
4032 {
4033 // No path separator or separator is the last character,
4034 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01004035 VIM_CLEAR_STRING(compl_pattern);
4036 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
4037 if (compl_pattern.string == NULL)
4038 return;
4039 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02004040 }
4041 else if (*(last_sep + 1) == '\0')
glepnirf31cfa22025-03-06 21:59:13 +01004042 in_fuzzy_collect = FALSE;
glepnir0be03e12024-07-19 16:45:05 +02004043 else
4044 {
4045 // Split leader into path and file parts
4046 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004047 char_u *path_with_wildcard;
4048
4049 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02004050 if (path_with_wildcard != NULL)
4051 {
John Marriott5e6ea922024-11-23 14:01:57 +01004052 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
4053 VIM_CLEAR_STRING(compl_pattern);
4054 compl_pattern.string = path_with_wildcard;
4055 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02004056
4057 // Move leader to the file part
4058 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01004059 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02004060 }
4061 }
glepnir8159fb12024-07-17 20:32:54 +02004062 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004063
John Marriott5e6ea922024-11-23 14:01:57 +01004064 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004065 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
4066 return;
4067
4068 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01004069 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004070#ifdef BACKSLASH_IN_FILENAME
4071 if (curbuf->b_p_csl[0] != NUL)
4072 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004073 for (i = 0; i < num_matches; ++i)
4074 {
glepnir8159fb12024-07-17 20:32:54 +02004075 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004076 while (*ptr != NUL)
4077 {
4078 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
4079 *ptr = '/';
4080 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
4081 *ptr = '\\';
4082 ptr += (*mb_ptr2len)(ptr);
4083 }
4084 }
4085 }
4086#endif
glepnir8159fb12024-07-17 20:32:54 +02004087
glepnirf31cfa22025-03-06 21:59:13 +01004088 if (in_fuzzy_collect)
glepnir8159fb12024-07-17 20:32:54 +02004089 {
4090 ga_init2(&fuzzy_indices, sizeof(int), 10);
4091 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
4092
4093 for (i = 0; i < num_matches; i++)
4094 {
4095 ptr = matches[i];
4096 score = fuzzy_match_str(ptr, leader);
4097 if (score > 0)
4098 {
4099 if (ga_grow(&fuzzy_indices, 1) == OK)
4100 {
4101 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
4102 compl_fuzzy_scores[i] = score;
4103 fuzzy_indices.ga_len++;
4104 }
4105 }
4106 }
4107
Christian Brabandt220474d2024-07-20 13:26:44 +02004108 // prevent qsort from deref NULL pointer
4109 if (fuzzy_indices.ga_len > 0)
4110 {
glepnirf31cfa22025-03-06 21:59:13 +01004111 char_u *match = NULL;
Christian Brabandt220474d2024-07-20 13:26:44 +02004112 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
4113 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02004114
Christian Brabandt220474d2024-07-20 13:26:44 +02004115 for (i = 0; i < fuzzy_indices.ga_len; ++i)
glepnirf31cfa22025-03-06 21:59:13 +01004116 {
4117 match = matches[fuzzy_indices_data[i]];
4118 current_score = compl_fuzzy_scores[fuzzy_indices_data[i]];
4119 if (ins_compl_add(match, -1, NULL, NULL, NULL, dir,
4120 CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0),
4121 FALSE, NULL, current_score) == OK)
4122 dir = FORWARD;
4123
4124 if (need_collect_bests)
4125 {
4126 if (i == 0 || current_score == max_score)
4127 {
4128 compl_num_bests++;
4129 max_score = current_score;
4130 }
4131 }
4132 }
glepnir8159fb12024-07-17 20:32:54 +02004133
Christian Brabandt220474d2024-07-20 13:26:44 +02004134 FreeWild(num_matches, matches);
Christian Brabandt220474d2024-07-20 13:26:44 +02004135 }
glepnir6b6280c2024-07-27 16:25:45 +02004136 else if (leader_len > 0)
4137 {
4138 FreeWild(num_matches, matches);
4139 num_matches = 0;
4140 }
Christian Brabandt220474d2024-07-20 13:26:44 +02004141
glepnir8159fb12024-07-17 20:32:54 +02004142 vim_free(compl_fuzzy_scores);
4143 ga_clear(&fuzzy_indices);
glepnirf31cfa22025-03-06 21:59:13 +01004144
4145 if (compl_num_bests > 0 && compl_get_longest)
4146 fuzzy_longest_match();
4147 return;
glepnir8159fb12024-07-17 20:32:54 +02004148 }
4149
glepnir6b6280c2024-07-27 16:25:45 +02004150 if (num_matches > 0)
4151 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004152}
4153
4154/*
4155 * Get the next set of command-line completions matching "compl_pattern".
4156 */
4157 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004158get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004159{
4160 char_u **matches;
4161 int num_matches;
4162
John Marriott5e6ea922024-11-23 14:01:57 +01004163 if (expand_cmdline(&compl_xp, compl_pattern.string,
4164 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004165 ins_compl_add_matches(num_matches, matches, FALSE);
4166}
4167
4168/*
4169 * Get the next set of spell suggestions matching "compl_pattern".
4170 */
4171 static void
4172get_next_spell_completion(linenr_T lnum UNUSED)
4173{
4174#ifdef FEAT_SPELL
4175 char_u **matches;
4176 int num_matches;
4177
John Marriott5e6ea922024-11-23 14:01:57 +01004178 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004179 if (num_matches > 0)
4180 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00004181 else
4182 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004183#endif
4184}
4185
4186/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004187 * Return the next word or line from buffer "ins_buf" at position
4188 * "cur_match_pos" for completion. The length of the match is set in "len".
4189 */
4190 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00004191ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004192 buf_T *ins_buf, // buffer being scanned
4193 pos_T *cur_match_pos, // current match position
4194 int *match_len,
4195 int *cont_s_ipos) // next ^X<> will set initial_pos
4196{
4197 char_u *ptr;
4198 int len;
4199
4200 *match_len = 0;
4201 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
4202 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01004203 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004204 if (ctrl_x_mode_line_or_eval())
4205 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004206 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004207 {
4208 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
4209 return NULL;
4210 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01004211 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004212 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01004213 {
4214 char_u *tmp_ptr = ptr;
4215
4216 ptr = skipwhite(tmp_ptr);
4217 len -= (int)(ptr - tmp_ptr);
4218 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004219 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004220 }
4221 else
4222 {
4223 char_u *tmp_ptr = ptr;
4224
John Marriott5e6ea922024-11-23 14:01:57 +01004225 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004226 {
4227 tmp_ptr += compl_length;
4228 // Skip if already inside a word.
4229 if (vim_iswordp(tmp_ptr))
4230 return NULL;
4231 // Find start of next word.
4232 tmp_ptr = find_word_start(tmp_ptr);
4233 }
4234 // Find end of this word.
4235 tmp_ptr = find_word_end(tmp_ptr);
4236 len = (int)(tmp_ptr - ptr);
4237
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004238 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004239 {
4240 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
4241 {
4242 // Try next line, if any. the new word will be
4243 // "join" as if the normal command "J" was used.
4244 // IOSIZE is always greater than
4245 // compl_length, so the next STRNCPY always
4246 // works -- Acevedo
4247 STRNCPY(IObuff, ptr, len);
4248 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
4249 tmp_ptr = ptr = skipwhite(ptr);
4250 // Find start of next word.
4251 tmp_ptr = find_word_start(tmp_ptr);
4252 // Find end of next word.
4253 tmp_ptr = find_word_end(tmp_ptr);
4254 if (tmp_ptr > ptr)
4255 {
4256 if (*ptr != ')' && IObuff[len - 1] != TAB)
4257 {
4258 if (IObuff[len - 1] != ' ')
4259 IObuff[len++] = ' ';
4260 // IObuf =~ "\k.* ", thus len >= 2
4261 if (p_js
4262 && (IObuff[len - 2] == '.'
4263 || (vim_strchr(p_cpo, CPO_JOINSP)
4264 == NULL
4265 && (IObuff[len - 2] == '?'
4266 || IObuff[len - 2] == '!'))))
4267 IObuff[len++] = ' ';
4268 }
4269 // copy as much as possible of the new word
4270 if (tmp_ptr - ptr >= IOSIZE - len)
4271 tmp_ptr = ptr + IOSIZE - len - 1;
4272 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4273 len += (int)(tmp_ptr - ptr);
4274 *cont_s_ipos = TRUE;
4275 }
4276 IObuff[len] = NUL;
4277 ptr = IObuff;
4278 }
4279 if (len == compl_length)
4280 return NULL;
4281 }
4282 }
4283
4284 *match_len = len;
4285 return ptr;
4286}
4287
4288/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004289 * Get the next set of words matching "compl_pattern" for default completion(s)
4290 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004291 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
4292 * position "st->start_pos" in the "compl_direction" direction. If
4293 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
4294 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004295 * Returns OK if a new next match is found, otherwise returns FAIL.
4296 */
4297 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004298get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004299{
4300 int found_new_match = FAIL;
4301 int save_p_scs;
4302 int save_p_ws;
4303 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02004304 char_u *ptr = NULL;
4305 int len = 0;
glepnirf31cfa22025-03-06 21:59:13 +01004306 int in_collect = (cfc_has_mode() && compl_length > 0);
glepnir8159fb12024-07-17 20:32:54 +02004307 char_u *leader = ins_compl_leader();
glepnirf31cfa22025-03-06 21:59:13 +01004308 int score = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004309
4310 // If 'infercase' is set, don't use 'smartcase' here
4311 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004312 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004313 p_scs = FALSE;
4314
4315 // Buffers other than curbuf are scanned from the beginning or the
4316 // end but never from the middle, thus setting nowrapscan in this
4317 // buffer is a good idea, on the other hand, we always set
4318 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
4319 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004320 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004321 p_ws = FALSE;
glepnirf31cfa22025-03-06 21:59:13 +01004322 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004323 p_ws = TRUE;
4324 looped_around = FALSE;
4325 for (;;)
4326 {
4327 int cont_s_ipos = FALSE;
4328
4329 ++msg_silent; // Don't want messages for wrapscan.
4330
glepnirf31cfa22025-03-06 21:59:13 +01004331 if (in_collect)
4332 {
glepnir8159fb12024-07-17 20:32:54 +02004333 found_new_match = search_for_fuzzy_match(st->ins_buf,
4334 st->cur_match_pos, leader, compl_direction,
glepnirf31cfa22025-03-06 21:59:13 +01004335 start_pos, &len, &ptr, &score);
4336 }
4337 // ctrl_x_mode_line_or_eval() || word-wise search that
4338 // has added a word that was at the beginning of the line
4339 else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
4340 found_new_match = search_for_exact_line(st->ins_buf,
4341 st->cur_match_pos, compl_direction, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004342 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004343 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01004344 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02004345 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004346 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004347 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004348 {
4349 // set "compl_started" even on fail
4350 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004351 st->first_match_pos = *st->cur_match_pos;
4352 st->last_match_pos = *st->cur_match_pos;
4353 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004354 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004355 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
4356 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004357 {
4358 found_new_match = FAIL;
4359 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004360 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004361 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
4362 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4363 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004364 {
4365 if (looped_around)
4366 found_new_match = FAIL;
4367 else
4368 looped_around = TRUE;
4369 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004370 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004371 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
4372 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
4373 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004374 {
4375 if (looped_around)
4376 found_new_match = FAIL;
4377 else
4378 looped_around = TRUE;
4379 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004380 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004381 if (found_new_match == FAIL)
4382 break;
4383
4384 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004385 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004386 && start_pos->lnum == st->cur_match_pos->lnum
4387 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004388 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004389
glepnirf31cfa22025-03-06 21:59:13 +01004390 if (!in_collect)
glepnir8159fb12024-07-17 20:32:54 +02004391 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4392 &len, &cont_s_ipos);
glepnirbfb4eea2025-01-31 15:28:29 +01004393 if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004394 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004395
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004396 if (ins_compl_add_infercase(ptr, len, p_ic,
glepnirf31cfa22025-03-06 21:59:13 +01004397 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
4398 0, cont_s_ipos, score) != NOTDONE)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004399 {
glepnirf31cfa22025-03-06 21:59:13 +01004400 if (in_collect && score == compl_first_match->cp_next->cp_score)
4401 compl_num_bests++;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004402 found_new_match = OK;
4403 break;
4404 }
4405 }
4406 p_scs = save_p_scs;
4407 p_ws = save_p_ws;
4408
4409 return found_new_match;
4410}
4411
4412/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004413 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004414 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004415 */
4416 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004417get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004418{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004419 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004420
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004421 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004422 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004423 case -1:
4424 break;
4425#ifdef FEAT_FIND_ID
4426 case CTRL_X_PATH_PATTERNS:
4427 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004428 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004429 break;
4430#endif
4431
4432 case CTRL_X_DICTIONARY:
4433 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004434 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4435 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004436 break;
4437
4438 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004439 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004440 break;
4441
4442 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004443 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004444 break;
4445
4446 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004447 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004448 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004449 break;
4450
4451#ifdef FEAT_COMPL_FUNC
4452 case CTRL_X_FUNCTION:
4453 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004454 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004455 break;
4456#endif
4457
4458 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004459 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004460 break;
4461
4462 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004463 found_new_match = get_next_default_completion(st, ini);
4464 if (found_new_match == FAIL && st->ins_buf == curbuf)
4465 st->found_all = TRUE;
4466 }
4467
4468 // check if compl_curr_match has changed, (e.g. other type of
4469 // expansion added something)
4470 if (type != 0 && compl_curr_match != compl_old_match)
4471 found_new_match = OK;
4472
4473 return found_new_match;
4474}
4475
4476/*
4477 * Get the next expansion(s), using "compl_pattern".
4478 * The search starts at position "ini" in curbuf and in the direction
4479 * compl_direction.
4480 * When "compl_started" is FALSE start at that position, otherwise continue
4481 * where we stopped searching before.
4482 * This may return before finding all the matches.
4483 * Return the total number of matches or -1 if still unknown -- Acevedo
4484 */
4485 static int
4486ins_compl_get_exp(pos_T *ini)
4487{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004488 static ins_compl_next_state_T st;
4489 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004490 int i;
4491 int found_new_match;
4492 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004493 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004494
4495 if (!compl_started)
4496 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004497 buf_T *buf;
4498
4499 FOR_ALL_BUFFERS(buf)
4500 buf->b_scanned = 0;
4501 if (!st_cleared)
4502 {
4503 CLEAR_FIELD(st);
4504 st_cleared = TRUE;
4505 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004506 st.found_all = FALSE;
4507 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004508 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004509 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004510 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4511 ? (char_u *)"." : curbuf->b_p_cpt);
4512 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004513 st.last_match_pos = st.first_match_pos = *ini;
4514 }
4515 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4516 st.ins_buf = curbuf; // In case the buffer was wiped out.
4517
4518 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004519 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004520 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004521
4522 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4523 for (;;)
4524 {
4525 found_new_match = FAIL;
4526 st.set_match_pos = FALSE;
4527
4528 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4529 // or if found_all says this entry is done. For ^X^L only use the
4530 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004531 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004532 && (!compl_started || st.found_all))
4533 {
glepnir8159fb12024-07-17 20:32:54 +02004534 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004535
4536 if (status == INS_COMPL_CPT_END)
4537 break;
4538 if (status == INS_COMPL_CPT_CONT)
4539 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004540 }
4541
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004542 // If complete() was called then compl_pattern has been reset. The
4543 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004544 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004545 break;
4546
4547 // get the next set of completion matches
4548 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004549
4550 // break the loop for specialized modes (use 'complete' just for the
4551 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4552 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004553 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4554 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004555 {
4556 if (got_int)
4557 break;
4558 // Fill the popup menu as soon as possible.
4559 if (type != -1)
4560 ins_compl_check_keys(0, FALSE);
4561
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004562 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004563 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4564 break;
4565 compl_started = TRUE;
4566 }
4567 else
4568 {
4569 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004570 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004571 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004572
4573 compl_started = FALSE;
4574 }
4575 }
4576 compl_started = TRUE;
4577
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004578 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004579 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004580 found_new_match = FAIL;
4581
4582 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004583 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004584 && !ctrl_x_mode_line_or_eval()))
4585 i = ins_compl_make_cyclic();
4586
glepnirf31cfa22025-03-06 21:59:13 +01004587 if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0)
4588 fuzzy_longest_match();
4589
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004590 if (compl_old_match != NULL)
4591 {
4592 // If several matches were added (FORWARD) or the search failed and has
4593 // just been made cyclic then we have to move compl_curr_match to the
4594 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004595 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004596 : compl_old_match->cp_prev;
4597 if (compl_curr_match == NULL)
4598 compl_curr_match = compl_old_match;
4599 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004600 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004601
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004602 return i;
4603}
4604
4605/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004606 * Update "compl_shown_match" to the actually shown match, it may differ when
4607 * "compl_leader" is used to omit some of the matches.
4608 */
4609 static void
4610ins_compl_update_shown_match(void)
4611{
4612 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004613 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004614 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004615 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004616 compl_shown_match = compl_shown_match->cp_next;
4617
4618 // If we didn't find it searching forward, and compl_shows_dir is
4619 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004620 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004621 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004622 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004623 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004624 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004625 {
4626 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004627 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004628 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004629 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004630 compl_shown_match = compl_shown_match->cp_prev;
4631 }
4632}
4633
4634/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004635 * Delete the old text being completed.
4636 */
4637 void
4638ins_compl_delete(void)
4639{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004640 // In insert mode: Delete the typed part.
4641 // In replace mode: Put the old characters back, if any.
glepniredd4ac32025-01-29 18:53:51 +01004642 int col = compl_col + (compl_status_adding() ? compl_length : 0);
John Marriottf4b36412025-02-23 09:09:59 +01004643 string_T remaining = {NULL, 0};
glepnir76bdb822025-02-08 19:04:51 +01004644 int orig_col;
glepniredd4ac32025-01-29 18:53:51 +01004645 int has_preinsert = ins_compl_preinsert_effect();
4646 if (has_preinsert)
4647 {
glepnirbfb4eea2025-01-31 15:28:29 +01004648 col += ins_compl_leader_len();
glepniredd4ac32025-01-29 18:53:51 +01004649 curwin->w_cursor.col = compl_ins_end_col;
4650 }
4651
glepnir76bdb822025-02-08 19:04:51 +01004652 if (curwin->w_cursor.lnum > compl_lnum)
4653 {
4654 if (curwin->w_cursor.col < ml_get_curline_len())
4655 {
John Marriottf4b36412025-02-23 09:09:59 +01004656 char_u *line = ml_get_cursor();
4657 remaining.length = ml_get_cursor_len();
4658 remaining.string = vim_strnsave(line, remaining.length);
4659 if (remaining.string == NULL)
glepnir76bdb822025-02-08 19:04:51 +01004660 return;
4661 }
4662 while (curwin->w_cursor.lnum > compl_lnum)
4663 {
4664 if (ml_delete(curwin->w_cursor.lnum) == FAIL)
4665 {
John Marriottf4b36412025-02-23 09:09:59 +01004666 if (remaining.string)
4667 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004668 return;
4669 }
zeertzjq060e6552025-02-21 20:06:26 +01004670 deleted_lines_mark(curwin->w_cursor.lnum, 1L);
glepnir76bdb822025-02-08 19:04:51 +01004671 curwin->w_cursor.lnum--;
4672 }
4673 // move cursor to end of line
4674 curwin->w_cursor.col = ml_get_curline_len();
4675 }
4676
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004677 if ((int)curwin->w_cursor.col > col)
4678 {
4679 if (stop_arrow() == FAIL)
glepnire3647c82025-02-10 21:16:32 +01004680 {
John Marriottf4b36412025-02-23 09:09:59 +01004681 if (remaining.string)
4682 vim_free(remaining.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004683 return;
glepnire3647c82025-02-10 21:16:32 +01004684 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004685 backspace_until_column(col);
zeertzjqf25d8f92024-12-18 21:12:25 +01004686 compl_ins_end_col = curwin->w_cursor.col;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004687 }
4688
John Marriottf4b36412025-02-23 09:09:59 +01004689 if (remaining.string != NULL)
glepnir76bdb822025-02-08 19:04:51 +01004690 {
4691 orig_col = curwin->w_cursor.col;
John Marriottf4b36412025-02-23 09:09:59 +01004692 ins_str(remaining.string, remaining.length);
glepnir76bdb822025-02-08 19:04:51 +01004693 curwin->w_cursor.col = orig_col;
John Marriottf4b36412025-02-23 09:09:59 +01004694 vim_free(remaining.string);
glepnir76bdb822025-02-08 19:04:51 +01004695 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004696 // TODO: is this sufficient for redrawing? Redrawing everything causes
4697 // flicker, thus we can't do that.
4698 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004699#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004700 // clear v:completed_item
4701 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004702#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004703}
4704
4705/*
glepnir76bdb822025-02-08 19:04:51 +01004706 * Insert a completion string that contains newlines.
4707 * The string is split and inserted line by line.
4708 */
4709 static void
4710ins_compl_expand_multiple(char_u *str)
4711{
4712 char_u *start = str;
4713 char_u *curr = str;
glepnir5090a1f2025-02-24 19:10:37 +01004714 int base_indent = get_indent();
glepnir76bdb822025-02-08 19:04:51 +01004715
4716 while (*curr != NUL)
4717 {
4718 if (*curr == '\n')
4719 {
4720 // Insert the text chunk before newline
4721 if (curr > start)
4722 ins_char_bytes(start, (int)(curr - start));
4723
4724 // Handle newline
glepnir5090a1f2025-02-24 19:10:37 +01004725 open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL);
glepnir76bdb822025-02-08 19:04:51 +01004726 start = curr + 1;
4727 }
4728 curr++;
4729 }
4730
4731 // Handle remaining text after last newline (if any)
4732 if (curr > start)
4733 ins_char_bytes(start, (int)(curr - start));
4734
4735 compl_ins_end_col = curwin->w_cursor.col;
4736}
4737
4738/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004739 * Insert the new text being completed.
4740 * "in_compl_func" is TRUE when called from complete_check().
glepniredd4ac32025-01-29 18:53:51 +01004741 * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE
4742 * cursor needs to move back from the inserted text to the compl_leader.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004743 */
4744 void
glepniredd4ac32025-01-29 18:53:51 +01004745ins_compl_insert(int in_compl_func, int move_cursor)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004746{
glepnir76bdb822025-02-08 19:04:51 +01004747 int compl_len = get_compl_len();
4748 int preinsert = ins_compl_has_preinsert();
4749 char_u *cp_str = compl_shown_match->cp_str.string;
4750 size_t cp_str_len = compl_shown_match->cp_str.length;
4751 size_t leader_len = ins_compl_leader_len();
4752 char_u *has_multiple = vim_strchr(cp_str, '\n');
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004753
4754 // Make sure we don't go over the end of the string, this can happen with
4755 // illegal bytes.
zeertzjq8297e2c2025-01-30 11:04:47 +01004756 if (compl_len < (int)cp_str_len)
glepniredd4ac32025-01-29 18:53:51 +01004757 {
glepnir76bdb822025-02-08 19:04:51 +01004758 if (has_multiple)
4759 ins_compl_expand_multiple(cp_str + compl_len);
4760 else
4761 {
4762 ins_compl_insert_bytes(cp_str + compl_len, -1);
4763 if (preinsert && move_cursor)
4764 curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len);
4765 }
glepniredd4ac32025-01-29 18:53:51 +01004766 }
4767 if (match_at_original_text(compl_shown_match) || preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004768 compl_used_match = FALSE;
4769 else
4770 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004771#ifdef FEAT_EVAL
4772 {
4773 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4774
4775 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4776 }
4777#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004778 if (!in_compl_func)
4779 compl_curr_match = compl_shown_match;
4780}
4781
4782/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004783 * show the file name for the completion match (if any). Truncate the file
4784 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004785 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004786 static void
4787ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004788{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004789 char *lead = _("match in file");
4790 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4791 char_u *s;
4792 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004793
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004794 if (space <= 0)
4795 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004796
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004797 // We need the tail that fits. With double-byte encoding going
4798 // back from the end is very slow, thus go from the start and keep
4799 // the text that fits in "space" between "s" and "e".
4800 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004801 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004802 space -= ptr2cells(e);
4803 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004804 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004805 space += ptr2cells(s);
4806 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004807 }
4808 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004809 msg_hist_off = TRUE;
4810 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4811 s > compl_shown_match->cp_fname ? "<" : "", s);
4812 msg((char *)IObuff);
4813 msg_hist_off = FALSE;
4814 redraw_cmdline = FALSE; // don't overwrite!
4815}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004816
glepnirdca57fb2024-06-04 22:01:21 +02004817/*
zeertzjq551d8c32024-06-05 19:53:32 +02004818 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004819 */
glepnira218cc62024-06-03 19:32:39 +02004820 static compl_T *
4821find_comp_when_fuzzy(void)
4822{
4823 int score;
4824 char_u* str;
4825 int target_idx = -1;
4826 int is_forward = compl_shows_dir_forward();
4827 int is_backward = compl_shows_dir_backward();
4828 compl_T *comp = NULL;
4829
glepnir43eef882024-06-19 20:20:48 +02004830 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004831 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004832 return compl_first_match != compl_shown_match ? compl_first_match :
4833 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004834
4835 if (is_forward)
4836 target_idx = compl_selected_item + 1;
4837 else if (is_backward)
4838 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004839 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004840
4841 score = compl_match_array[target_idx].pum_score;
4842 str = compl_match_array[target_idx].pum_text;
4843
4844 comp = compl_first_match;
4845 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004846 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004847 return comp;
4848 comp = comp->cp_next;
4849 } while (comp != NULL && !is_first_match(comp));
4850
4851 return NULL;
4852}
4853
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004854/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004855 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004856 * times. The number of matches found is returned in 'num_matches'.
4857 *
4858 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4859 * get more completions. If it is FALSE, then do nothing when there are no more
4860 * completions in the given direction.
4861 *
4862 * If "advance" is TRUE, then completion will move to the first match.
4863 * Otherwise, the original text will be shown.
4864 *
4865 * Returns OK on success and -1 if the number of matches are unknown.
4866 */
4867 static int
4868find_next_completion_match(
4869 int allow_get_expansion,
4870 int todo, // repeat completion this many times
4871 int advance,
4872 int *num_matches)
4873{
4874 int found_end = FALSE;
4875 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004876 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004877 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4878 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004879
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004880 while (--todo >= 0)
4881 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004882 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004883 {
glepniraced8c22024-06-15 15:13:05 +02004884 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4885 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004886 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004887 && (is_first_match(compl_shown_match->cp_next)
4888 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004889 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004890 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004891 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004892 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004893 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004894 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4895 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004896 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004897 }
4898 else
4899 {
4900 if (!allow_get_expansion)
4901 {
4902 if (advance)
4903 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004904 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004905 compl_pending -= todo + 1;
4906 else
4907 compl_pending += todo + 1;
4908 }
4909 return -1;
4910 }
4911
4912 if (!compl_no_select && advance)
4913 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004914 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004915 --compl_pending;
4916 else
4917 ++compl_pending;
4918 }
4919
4920 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004921 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004922
4923 // handle any pending completions
4924 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004925 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004926 {
4927 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4928 {
4929 compl_shown_match = compl_shown_match->cp_next;
4930 --compl_pending;
4931 }
4932 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4933 {
4934 compl_shown_match = compl_shown_match->cp_prev;
4935 ++compl_pending;
4936 }
4937 else
4938 break;
4939 }
4940 found_end = FALSE;
4941 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004942 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004943 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004944 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004945 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004946 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004947 ++todo;
4948 else
4949 // Remember a matching item.
4950 found_compl = compl_shown_match;
4951
4952 // Stop at the end of the list when we found a usable match.
4953 if (found_end)
4954 {
4955 if (found_compl != NULL)
4956 {
4957 compl_shown_match = found_compl;
4958 break;
4959 }
4960 todo = 1; // use first usable match after wrapping around
4961 }
4962 }
4963
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004964 return OK;
4965}
4966
4967/*
4968 * Fill in the next completion in the current direction.
4969 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4970 * get more completions. If it is FALSE, then we just do nothing when there
4971 * are no more completions in a given direction. The latter case is used when
4972 * we are still in the middle of finding completions, to allow browsing
4973 * through the ones found so far.
4974 * Return the total number of matches, or -1 if still unknown -- webb.
4975 *
4976 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4977 * compl_shown_match here.
4978 *
4979 * Note that this function may be called recursively once only. First with
4980 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4981 * calls this function with "allow_get_expansion" FALSE.
4982 */
4983 static int
4984ins_compl_next(
4985 int allow_get_expansion,
4986 int count, // repeat completion this many times; should
4987 // be at least 1
4988 int insert_match, // Insert the newly selected match
4989 int in_compl_func) // called from complete_check()
4990{
4991 int num_matches = -1;
4992 int todo = count;
4993 int advance;
4994 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004995 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004996 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004997 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4998 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepniredd4ac32025-01-29 18:53:51 +01004999 int compl_preinsert = ins_compl_has_preinsert();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005000
5001 // When user complete function return -1 for findstart which is next
5002 // time of 'always', compl_shown_match become NULL.
5003 if (compl_shown_match == NULL)
5004 return -1;
5005
John Marriott5e6ea922024-11-23 14:01:57 +01005006 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02005007 && !match_at_original_text(compl_shown_match)
5008 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005009 // Update "compl_shown_match" to the actually shown match
5010 ins_compl_update_shown_match();
5011
5012 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02005013 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005014 // Delete old text to be replaced
5015 ins_compl_delete();
5016
5017 // When finding the longest common text we stick at the original text,
5018 // don't let CTRL-N or CTRL-P move to the first match.
5019 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
5020
5021 // When restarting the search don't insert the first match either.
5022 if (compl_restarting)
5023 {
5024 advance = FALSE;
5025 compl_restarting = FALSE;
5026 }
5027
5028 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
5029 // around.
5030 if (find_next_completion_match(allow_get_expansion, todo, advance,
5031 &num_matches) == -1)
5032 return -1;
5033
Bram Moolenaar0ff01832022-09-24 19:20:30 +01005034 if (curbuf != orig_curbuf)
5035 {
5036 // In case some completion function switched buffer, don't want to
5037 // insert the completion elsewhere.
5038 return -1;
5039 }
5040
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005041 // Insert the text of the new completion, or the compl_leader.
glepniredd4ac32025-01-29 18:53:51 +01005042 if (compl_no_insert && !started && !compl_preinsert)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005043 {
glepnir6a38aff2024-12-16 21:56:16 +01005044 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005045 compl_used_match = FALSE;
5046 }
5047 else if (insert_match)
5048 {
5049 if (!compl_get_longest || compl_used_match)
glepniredd4ac32025-01-29 18:53:51 +01005050 ins_compl_insert(in_compl_func, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005051 else
glepnir6a38aff2024-12-16 21:56:16 +01005052 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005053 }
5054 else
5055 compl_used_match = FALSE;
5056
5057 if (!allow_get_expansion)
5058 {
5059 // may undisplay the popup menu first
5060 ins_compl_upd_pum();
5061
5062 if (pum_enough_matches())
5063 // Will display the popup menu, don't redraw yet to avoid flicker.
5064 pum_call_update_screen();
5065 else
5066 // Not showing the popup menu yet, redraw to show the user what was
5067 // inserted.
5068 update_screen(0);
5069
5070 // display the updated popup menu
5071 ins_compl_show_pum();
5072#ifdef FEAT_GUI
5073 if (gui.in_use)
5074 {
5075 // Show the cursor after the match, not after the redrawn text.
5076 setcursor();
5077 out_flush_cursor(FALSE, FALSE);
5078 }
5079#endif
5080
5081 // Delete old text to be replaced, since we're still searching and
5082 // don't want to match ourselves!
5083 ins_compl_delete();
5084 }
5085
5086 // Enter will select a match when the match wasn't inserted and the popup
5087 // menu is visible.
5088 if (compl_no_insert && !started)
5089 compl_enter_selects = TRUE;
5090 else
5091 compl_enter_selects = !insert_match && compl_match_array != NULL;
5092
5093 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005094 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005095 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005096
5097 return num_matches;
5098}
5099
5100/*
5101 * Call this while finding completions, to check whether the user has hit a key
5102 * that should change the currently displayed completion, or exit completion
5103 * mode. Also, when compl_pending is not zero, show a completion as soon as
5104 * possible. -- webb
5105 * "frequency" specifies out of how many calls we actually check.
5106 * "in_compl_func" is TRUE when called from complete_check(), don't set
5107 * compl_curr_match.
5108 */
5109 void
5110ins_compl_check_keys(int frequency, int in_compl_func)
5111{
5112 static int count = 0;
5113 int c;
5114
5115 // Don't check when reading keys from a script, :normal or feedkeys().
5116 // That would break the test scripts. But do check for keys when called
5117 // from complete_check().
5118 if (!in_compl_func && (using_script() || ex_normal_busy))
5119 return;
5120
5121 // Only do this at regular intervals
5122 if (++count < frequency)
5123 return;
5124 count = 0;
5125
5126 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5127 // can't do its work correctly.
5128 c = vpeekc_any();
5129 if (c != NUL)
5130 {
5131 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5132 {
5133 c = safe_vgetc(); // Eat the character
5134 compl_shows_dir = ins_compl_key2dir(c);
5135 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
5136 c != K_UP && c != K_DOWN, in_compl_func);
5137 }
5138 else
5139 {
5140 // Need to get the character to have KeyTyped set. We'll put it
5141 // back with vungetc() below. But skip K_IGNORE.
5142 c = safe_vgetc();
5143 if (c != K_IGNORE)
5144 {
5145 // Don't interrupt completion when the character wasn't typed,
5146 // e.g., when doing @q to replay keys.
5147 if (c != Ctrl_R && KeyTyped)
5148 compl_interrupted = TRUE;
5149
5150 vungetc(c);
5151 }
5152 }
5153 }
zeertzjq529b9ad2024-06-05 20:27:06 +02005154 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005155 {
5156 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5157
5158 compl_pending = 0;
5159 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
5160 }
5161}
5162
5163/*
5164 * Decide the direction of Insert mode complete from the key typed.
5165 * Returns BACKWARD or FORWARD.
5166 */
5167 static int
5168ins_compl_key2dir(int c)
5169{
5170 if (c == Ctrl_P || c == Ctrl_L
5171 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
5172 return BACKWARD;
5173 return FORWARD;
5174}
5175
5176/*
5177 * Return TRUE for keys that are used for completion only when the popup menu
5178 * is visible.
5179 */
5180 static int
5181ins_compl_pum_key(int c)
5182{
5183 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
5184 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5185 || c == K_UP || c == K_DOWN);
5186}
5187
5188/*
5189 * Decide the number of completions to move forward.
5190 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5191 */
5192 static int
5193ins_compl_key2count(int c)
5194{
5195 int h;
5196
5197 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
5198 {
5199 h = pum_get_height();
5200 if (h > 3)
5201 h -= 2; // keep some context
5202 return h;
5203 }
5204 return 1;
5205}
5206
5207/*
5208 * Return TRUE if completion with "c" should insert the match, FALSE if only
5209 * to change the currently selected completion.
5210 */
5211 static int
5212ins_compl_use_match(int c)
5213{
5214 switch (c)
5215 {
5216 case K_UP:
5217 case K_DOWN:
5218 case K_PAGEDOWN:
5219 case K_KPAGEDOWN:
5220 case K_S_DOWN:
5221 case K_PAGEUP:
5222 case K_KPAGEUP:
5223 case K_S_UP:
5224 return FALSE;
5225 }
5226 return TRUE;
5227}
5228
5229/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005230 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
5231 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01005232 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005233 * Uses the global variables: compl_cont_status and ctrl_x_mode
5234 */
5235 static int
5236get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
5237{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005238 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005239 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005240 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005241 {
5242 while (--startcol >= 0 && vim_isIDc(line[startcol]))
5243 ;
5244 compl_col += ++startcol;
5245 compl_length = curs_col - startcol;
5246 }
5247 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005248 {
John Marriott5e6ea922024-11-23 14:01:57 +01005249 compl_pattern.string = str_foldcase(line + compl_col,
5250 compl_length, NULL, 0);
5251 if (compl_pattern.string == NULL)
5252 {
5253 compl_pattern.length = 0;
5254 return FAIL;
5255 }
5256 compl_pattern.length = STRLEN(compl_pattern.string);
5257 }
5258 else
5259 {
5260 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5261 if (compl_pattern.string == NULL)
5262 {
5263 compl_pattern.length = 0;
5264 return FAIL;
5265 }
5266 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02005267 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005268 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005269 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005270 {
5271 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02005272 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01005273 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005274
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005275 if (!vim_iswordp(line + compl_col)
5276 || (compl_col > 0
5277 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02005278 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005279 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02005280 prefixlen = 0;
5281 }
John Marriott5e6ea922024-11-23 14:01:57 +01005282
5283 // we need up to 2 extra chars for the prefix
5284 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
5285 compl_pattern.string = alloc(n);
5286 if (compl_pattern.string == NULL)
5287 {
5288 compl_pattern.length = 0;
5289 return FAIL;
5290 }
5291 STRCPY((char *)compl_pattern.string, prefix);
5292 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005293 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005294 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005295 }
5296 else if (--startcol < 0
5297 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
5298 {
John Marriott5e6ea922024-11-23 14:01:57 +01005299 size_t len = STRLEN_LITERAL("\\<\\k\\k");
5300
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005301 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01005302 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
5303 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005304 {
John Marriott5e6ea922024-11-23 14:01:57 +01005305 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005306 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005307 }
John Marriott5e6ea922024-11-23 14:01:57 +01005308 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005309 compl_col += curs_col;
5310 compl_length = 0;
5311 }
5312 else
5313 {
5314 // Search the point of change class of multibyte character
5315 // or not a word single byte character backward.
5316 if (has_mbyte)
5317 {
5318 int base_class;
5319 int head_off;
5320
5321 startcol -= (*mb_head_off)(line, line + startcol);
5322 base_class = mb_get_class(line + startcol);
5323 while (--startcol >= 0)
5324 {
5325 head_off = (*mb_head_off)(line, line + startcol);
5326 if (base_class != mb_get_class(line + startcol
5327 - head_off))
5328 break;
5329 startcol -= head_off;
5330 }
5331 }
5332 else
5333 while (--startcol >= 0 && vim_iswordc(line[startcol]))
5334 ;
5335 compl_col += ++startcol;
5336 compl_length = (int)curs_col - startcol;
5337 if (compl_length == 1)
5338 {
5339 // Only match word with at least two chars -- webb
5340 // there's no need to call quote_meta,
5341 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01005342 compl_pattern.string = alloc(7);
5343 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005344 {
John Marriott5e6ea922024-11-23 14:01:57 +01005345 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005346 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005347 }
John Marriott5e6ea922024-11-23 14:01:57 +01005348 STRCPY((char *)compl_pattern.string, "\\<");
5349 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
5350 STRCAT((char *)compl_pattern.string, "\\k");
5351 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005352 }
5353 else
5354 {
John Marriott5e6ea922024-11-23 14:01:57 +01005355 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
5356
5357 compl_pattern.string = alloc(n);
5358 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005359 {
John Marriott5e6ea922024-11-23 14:01:57 +01005360 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005361 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005362 }
John Marriott5e6ea922024-11-23 14:01:57 +01005363 STRCPY((char *)compl_pattern.string, "\\<");
5364 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005365 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01005366 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005367 }
5368 }
5369
5370 return OK;
5371}
5372
5373/*
5374 * Get the pattern, column and length for whole line completion or for the
5375 * complete() function.
5376 * Sets the global variables: compl_col, compl_length and compl_pattern.
5377 */
5378 static int
5379get_wholeline_compl_info(char_u *line, colnr_T curs_col)
5380{
5381 compl_col = (colnr_T)getwhitecols(line);
5382 compl_length = (int)curs_col - (int)compl_col;
5383 if (compl_length < 0) // cursor in indent: empty pattern
5384 compl_length = 0;
5385 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02005386 {
John Marriott5e6ea922024-11-23 14:01:57 +01005387 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
5388 NULL, 0);
5389 if (compl_pattern.string == NULL)
5390 {
5391 compl_pattern.length = 0;
5392 return FAIL;
5393 }
5394 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02005395 }
John Marriott5e6ea922024-11-23 14:01:57 +01005396 else
5397 {
5398 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5399 if (compl_pattern.string == NULL)
5400 {
5401 compl_pattern.length = 0;
5402 return FAIL;
5403 }
5404 compl_pattern.length = (size_t)compl_length;
5405 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005406
5407 return OK;
5408}
5409
5410/*
5411 * Get the pattern, column and length for filename completion.
5412 * Sets the global variables: compl_col, compl_length and compl_pattern.
5413 */
5414 static int
5415get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
5416{
5417 // Go back to just before the first filename character.
5418 if (startcol > 0)
5419 {
5420 char_u *p = line + startcol;
5421
5422 MB_PTR_BACK(line, p);
5423 while (p > line && vim_isfilec(PTR2CHAR(p)))
5424 MB_PTR_BACK(line, p);
5425 if (p == line && vim_isfilec(PTR2CHAR(p)))
5426 startcol = 0;
5427 else
5428 startcol = (int)(p - line) + 1;
5429 }
5430
5431 compl_col += startcol;
5432 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01005433 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
5434 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005435 {
John Marriott5e6ea922024-11-23 14:01:57 +01005436 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005437 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005438 }
5439
John Marriott5e6ea922024-11-23 14:01:57 +01005440 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005441
5442 return OK;
5443}
5444
5445/*
5446 * Get the pattern, column and length for command-line completion.
5447 * Sets the global variables: compl_col, compl_length and compl_pattern.
5448 */
5449 static int
5450get_cmdline_compl_info(char_u *line, colnr_T curs_col)
5451{
John Marriott5e6ea922024-11-23 14:01:57 +01005452 compl_pattern.string = vim_strnsave(line, curs_col);
5453 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005454 {
John Marriott5e6ea922024-11-23 14:01:57 +01005455 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005456 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005457 }
John Marriott5e6ea922024-11-23 14:01:57 +01005458 compl_pattern.length = curs_col;
5459 set_cmd_context(&compl_xp, compl_pattern.string,
5460 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005461 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5462 || compl_xp.xp_context == EXPAND_NOTHING)
5463 // No completion possible, use an empty pattern to get a
5464 // "pattern not found" message.
5465 compl_col = curs_col;
5466 else
John Marriott5e6ea922024-11-23 14:01:57 +01005467 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005468 compl_length = curs_col - compl_col;
5469
5470 return OK;
5471}
5472
5473/*
5474 * Get the pattern, column and length for user defined completion ('omnifunc',
5475 * 'completefunc' and 'thesaurusfunc')
5476 * Sets the global variables: compl_col, compl_length and compl_pattern.
5477 * Uses the global variable: spell_bad_len
5478 */
5479 static int
5480get_userdefined_compl_info(colnr_T curs_col UNUSED)
5481{
5482 int ret = FAIL;
5483
5484#ifdef FEAT_COMPL_FUNC
5485 // Call user defined function 'completefunc' with "a:findstart"
5486 // set to 1 to obtain the length of text to use for completion.
5487 char_u *line;
5488 typval_T args[3];
5489 int col;
5490 char_u *funcname;
5491 pos_T pos;
5492 int save_State = State;
5493 callback_T *cb;
5494
5495 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5496 // length as a string
5497 funcname = get_complete_funcname(ctrl_x_mode);
5498 if (*funcname == NUL)
5499 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005500 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005501 ? "completefunc" : "omnifunc");
5502 return FAIL;
5503 }
5504
5505 args[0].v_type = VAR_NUMBER;
5506 args[0].vval.v_number = 1;
5507 args[1].v_type = VAR_STRING;
5508 args[1].vval.v_string = (char_u *)"";
5509 args[2].v_type = VAR_UNKNOWN;
5510 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005511 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005512 cb = get_insert_callback(ctrl_x_mode);
5513 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005514 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005515
5516 State = save_State;
5517 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005518 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005519 validate_cursor();
5520 if (!EQUAL_POS(curwin->w_cursor, pos))
5521 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005522 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005523 return FAIL;
5524 }
5525
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005526 // Return value -2 means the user complete function wants to cancel the
5527 // complete without an error, do the same if the function did not execute
5528 // successfully.
5529 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005530 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005531 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005532 if (col == -3)
5533 {
5534 ctrl_x_mode = CTRL_X_NORMAL;
5535 edit_submode = NULL;
5536 if (!shortmess(SHM_COMPLETIONMENU))
5537 msg_clr_cmdline();
5538 return FAIL;
5539 }
5540
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005541 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005542 // completion.
5543 compl_opt_refresh_always = FALSE;
5544 compl_opt_suppress_empty = FALSE;
5545
5546 if (col < 0)
5547 col = curs_col;
5548 compl_col = col;
5549 if (compl_col > curs_col)
5550 compl_col = curs_col;
5551
5552 // Setup variables for completion. Need to obtain "line" again,
5553 // it may have become invalid.
5554 line = ml_get(curwin->w_cursor.lnum);
5555 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005556 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5557 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005558 {
John Marriott5e6ea922024-11-23 14:01:57 +01005559 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005560 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005561 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005562
John Marriott5e6ea922024-11-23 14:01:57 +01005563 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005564 ret = OK;
5565#endif
5566
5567 return ret;
5568}
5569
5570/*
5571 * Get the pattern, column and length for spell completion.
5572 * Sets the global variables: compl_col, compl_length and compl_pattern.
5573 * Uses the global variable: spell_bad_len
5574 */
5575 static int
5576get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5577{
5578 int ret = FAIL;
5579#ifdef FEAT_SPELL
5580 char_u *line;
5581
5582 if (spell_bad_len > 0)
5583 compl_col = curs_col - spell_bad_len;
5584 else
5585 compl_col = spell_word_start(startcol);
5586 if (compl_col >= (colnr_T)startcol)
5587 {
5588 compl_length = 0;
5589 compl_col = curs_col;
5590 }
5591 else
5592 {
5593 spell_expand_check_cap(compl_col);
5594 compl_length = (int)curs_col - compl_col;
5595 }
5596 // Need to obtain "line" again, it may have become invalid.
5597 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005598 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5599 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005600 {
John Marriott5e6ea922024-11-23 14:01:57 +01005601 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005602 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005603 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005604
John Marriott5e6ea922024-11-23 14:01:57 +01005605 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005606 ret = OK;
5607#endif
5608
5609 return ret;
5610}
5611
5612/*
5613 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005614 * "startcol" - start column number of the completion pattern/text
5615 * "cur_col" - current cursor column
5616 * On return, "line_invalid" is set to TRUE, if the current line may have
5617 * become invalid and needs to be fetched again.
5618 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005619 */
5620 static int
5621compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5622{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005623 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005624 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5625 && !thesaurus_func_complete(ctrl_x_mode)))
5626 {
5627 return get_normal_compl_info(line, startcol, curs_col);
5628 }
5629 else if (ctrl_x_mode_line_or_eval())
5630 {
5631 return get_wholeline_compl_info(line, curs_col);
5632 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005633 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005634 {
5635 return get_filename_compl_info(line, startcol, curs_col);
5636 }
5637 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5638 {
5639 return get_cmdline_compl_info(line, curs_col);
5640 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005641 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005642 || thesaurus_func_complete(ctrl_x_mode))
5643 {
5644 if (get_userdefined_compl_info(curs_col) == FAIL)
5645 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005646 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005647 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005648 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005649 {
5650 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5651 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005652 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005653 }
5654 else
5655 {
5656 internal_error("ins_complete()");
5657 return FAIL;
5658 }
5659
5660 return OK;
5661}
5662
5663/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005664 * Continue an interrupted completion mode search in "line".
5665 *
5666 * If this same ctrl_x_mode has been interrupted use the text from
5667 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5668 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5669 * the same line as the cursor then fix it (the line has been split because it
5670 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5671 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005672 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005673 static void
5674ins_compl_continue_search(char_u *line)
5675{
5676 // it is a continued search
5677 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005678 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5679 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005680 {
5681 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5682 {
5683 // line (probably) wrapped, set compl_startpos to the
5684 // first non_blank in the line, if it is not a wordchar
5685 // include it to get a better pattern, but then we don't
5686 // want the "\\<" prefix, check it below
5687 compl_col = (colnr_T)getwhitecols(line);
5688 compl_startpos.col = compl_col;
5689 compl_startpos.lnum = curwin->w_cursor.lnum;
5690 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5691 }
5692 else
5693 {
5694 // S_IPOS was set when we inserted a word that was at the
5695 // beginning of the line, which means that we'll go to SOL
5696 // mode but first we need to redefine compl_startpos
5697 if (compl_cont_status & CONT_S_IPOS)
5698 {
5699 compl_cont_status |= CONT_SOL;
5700 compl_startpos.col = (colnr_T)(skipwhite(
5701 line + compl_length
5702 + compl_startpos.col) - line);
5703 }
5704 compl_col = compl_startpos.col;
5705 }
5706 compl_length = curwin->w_cursor.col - (int)compl_col;
5707 // IObuff is used to add a "word from the next line" would we
5708 // have enough space? just being paranoid
5709#define MIN_SPACE 75
5710 if (compl_length > (IOSIZE - MIN_SPACE))
5711 {
5712 compl_cont_status &= ~CONT_SOL;
5713 compl_length = (IOSIZE - MIN_SPACE);
5714 compl_col = curwin->w_cursor.col - compl_length;
5715 }
5716 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5717 if (compl_length < 1)
5718 compl_cont_status &= CONT_LOCAL;
5719 }
5720 else if (ctrl_x_mode_line_or_eval())
5721 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5722 else
5723 compl_cont_status = 0;
5724}
5725
5726/*
5727 * start insert mode completion
5728 */
5729 static int
5730ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005731{
5732 char_u *line;
5733 int startcol = 0; // column where searched text starts
5734 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005735 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005736 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005737 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005738
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005739 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005740
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005741 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005742 did_si = FALSE;
5743 can_si = FALSE;
5744 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005745 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005746 return FAIL;
5747
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005748 line = ml_get(curwin->w_cursor.lnum);
5749 curs_col = curwin->w_cursor.col;
5750 compl_pending = 0;
glepnir76bdb822025-02-08 19:04:51 +01005751 compl_lnum = curwin->w_cursor.lnum;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005752
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005753 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5754 && compl_cont_mode == ctrl_x_mode)
5755 // this same ctrl-x_mode was interrupted previously. Continue the
5756 // completion.
5757 ins_compl_continue_search(line);
5758 else
5759 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005760
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005761 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005762 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005763 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005764 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005765 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5766 compl_cont_status = 0;
5767 compl_cont_status |= CONT_N_ADDS;
5768 compl_startpos = curwin->w_cursor;
5769 startcol = (int)curs_col;
5770 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005771 }
5772
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005773 // Work out completion pattern and original text -- webb
5774 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5775 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005776 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5777 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005778 // restore did_ai, so that adding comment leader works
5779 did_ai = save_did_ai;
5780 return FAIL;
5781 }
5782 // If "line" was changed while getting completion info get it again.
5783 if (line_invalid)
5784 line = ml_get(curwin->w_cursor.lnum);
5785
glepnir7cfe6932024-09-15 20:06:28 +02005786 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005787 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005788 {
5789 edit_submode_pre = (char_u *)_(" Adding");
5790 if (ctrl_x_mode_line_or_eval())
5791 {
5792 // Insert a new line, keep indentation but ignore 'comments'.
5793 char_u *old = curbuf->b_p_com;
5794
5795 curbuf->b_p_com = (char_u *)"";
5796 compl_startpos.lnum = curwin->w_cursor.lnum;
5797 compl_startpos.col = compl_col;
5798 ins_eol('\r');
5799 curbuf->b_p_com = old;
5800 compl_length = 0;
5801 compl_col = curwin->w_cursor.col;
glepnir76bdb822025-02-08 19:04:51 +01005802 compl_lnum = curwin->w_cursor.lnum;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005803 }
glepnir7cfe6932024-09-15 20:06:28 +02005804 else if (ctrl_x_mode_normal() && in_fuzzy)
5805 {
5806 compl_startpos = curwin->w_cursor;
5807 compl_cont_status &= CONT_S_IPOS;
5808 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005809 }
5810 else
5811 {
5812 edit_submode_pre = NULL;
5813 compl_startpos.col = compl_col;
5814 }
5815
5816 if (compl_cont_status & CONT_LOCAL)
5817 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5818 else
5819 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5820
5821 // If any of the original typed text has been changed we need to fix
5822 // the redo buffer.
5823 ins_compl_fixRedoBufForLeader(NULL);
5824
5825 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005826 VIM_CLEAR_STRING(compl_orig_text);
5827 compl_orig_text.length = (size_t)compl_length;
5828 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005829 if (p_ic)
5830 flags |= CP_ICASE;
zeertzjq4422de62025-03-07 19:06:02 +01005831 if (compl_orig_text.string == NULL
5832 || ins_compl_add(compl_orig_text.string,
5833 (int)compl_orig_text.length,
5834 NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005835 {
John Marriott5e6ea922024-11-23 14:01:57 +01005836 VIM_CLEAR_STRING(compl_pattern);
5837 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005838 return FAIL;
5839 }
5840
5841 // showmode might reset the internal line pointers, so it must
5842 // be called before line = ml_get(), or when this address is no
5843 // longer needed. -- Acevedo.
5844 edit_submode_extra = (char_u *)_("-- Searching...");
5845 edit_submode_highl = HLF_COUNT;
5846 showmode();
5847 edit_submode_extra = NULL;
5848 out_flush();
5849
5850 return OK;
5851}
5852
5853/*
5854 * display the completion status message
5855 */
5856 static void
5857ins_compl_show_statusmsg(void)
5858{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005859 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005860 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005861 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005862 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005863 ? (char_u *)_("Hit end of paragraph")
5864 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005865 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005866 }
5867
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005868 if (edit_submode_extra == NULL)
5869 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005870 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005871 {
5872 edit_submode_extra = (char_u *)_("Back at original");
5873 edit_submode_highl = HLF_W;
5874 }
5875 else if (compl_cont_status & CONT_S_IPOS)
5876 {
5877 edit_submode_extra = (char_u *)_("Word from other line");
5878 edit_submode_highl = HLF_COUNT;
5879 }
5880 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5881 {
5882 edit_submode_extra = (char_u *)_("The only match");
5883 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005884 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005885 }
5886 else
5887 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005888#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005889 // Update completion sequence number when needed.
5890 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005891 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005892#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005893 // The match should always have a sequence number now, this is
5894 // just a safety check.
5895 if (compl_curr_match->cp_number != -1)
5896 {
5897 // Space for 10 text chars. + 2x10-digit no.s = 31.
5898 // Translations may need more than twice that.
5899 static char_u match_ref[81];
5900
5901 if (compl_matches > 0)
5902 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005903 _("match %d of %d"),
5904 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005905 else
5906 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005907 _("match %d"),
5908 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005909 edit_submode_extra = match_ref;
5910 edit_submode_highl = HLF_R;
5911 if (dollar_vcol >= 0)
5912 curs_columns(FALSE);
5913 }
5914 }
5915 }
5916
5917 // Show a message about what (completion) mode we're in.
5918 if (!compl_opt_suppress_empty)
5919 {
5920 showmode();
5921 if (!shortmess(SHM_COMPLETIONMENU))
5922 {
5923 if (edit_submode_extra != NULL)
5924 {
5925 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005926 {
5927 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005928 msg_attr((char *)edit_submode_extra,
5929 edit_submode_highl < HLF_COUNT
5930 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005931 msg_hist_off = FALSE;
5932 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005933 }
5934 else
5935 msg_clr_cmdline(); // necessary for "noshowmode"
5936 }
5937 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005938}
5939
5940/*
5941 * Do Insert mode completion.
5942 * Called when character "c" was typed, which has a meaning for completion.
5943 * Returns OK if completion was done, FAIL if something failed (out of mem).
5944 */
5945 int
5946ins_complete(int c, int enable_pum)
5947{
5948 int n;
5949 int save_w_wrow;
5950 int save_w_leftcol;
5951 int insert_match;
5952
5953 compl_direction = ins_compl_key2dir(c);
5954 insert_match = ins_compl_use_match(c);
5955
5956 if (!compl_started)
5957 {
5958 if (ins_compl_start() == FAIL)
5959 return FAIL;
5960 }
5961 else if (insert_match && stop_arrow() == FAIL)
5962 return FAIL;
5963
5964 compl_shown_match = compl_curr_match;
5965 compl_shows_dir = compl_direction;
5966
5967 // Find next match (and following matches).
5968 save_w_wrow = curwin->w_wrow;
5969 save_w_leftcol = curwin->w_leftcol;
5970 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5971
5972 // may undisplay the popup menu
5973 ins_compl_upd_pum();
5974
5975 if (n > 1) // all matches have been found
5976 compl_matches = n;
5977 compl_curr_match = compl_shown_match;
5978 compl_direction = compl_shows_dir;
5979
5980 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5981 // mode.
5982 if (got_int && !global_busy)
5983 {
5984 (void)vgetc();
5985 got_int = FALSE;
5986 }
5987
5988 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005989 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005990 {
5991 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5992 // because we couldn't expand anything at first place, but if we used
5993 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5994 // (such as M in M'exico) if not tried already. -- Acevedo
5995 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005996 || compl_status_adding()
5997 || (ctrl_x_mode_not_default()
5998 && !ctrl_x_mode_path_patterns()
5999 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00006000 compl_cont_status &= ~CONT_N_ADDS;
6001 }
6002
6003 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
6004 compl_cont_status |= CONT_S_IPOS;
6005 else
6006 compl_cont_status &= ~CONT_S_IPOS;
6007
6008 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006009
6010 // Show the popup menu, unless we got interrupted.
6011 if (enable_pum && !compl_interrupted)
6012 show_pum(save_w_wrow, save_w_leftcol);
6013
6014 compl_was_interrupted = compl_interrupted;
6015 compl_interrupted = FALSE;
6016
6017 return OK;
6018}
6019
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00006020/*
6021 * Remove (if needed) and show the popup menu
6022 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006023 static void
6024show_pum(int prev_w_wrow, int prev_w_leftcol)
6025{
6026 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006027 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006028 RedrawingDisabled = 0;
6029
6030 // If the cursor moved or the display scrolled we need to remove the pum
6031 // first.
6032 setcursor();
6033 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
6034 ins_compl_del_pum();
6035
6036 ins_compl_show_pum();
6037 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01006038
6039 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006040}
6041
6042/*
6043 * Looks in the first "len" chars. of "src" for search-metachars.
6044 * If dest is not NULL the chars. are copied there quoting (with
6045 * a backslash) the metachars, and dest would be NUL terminated.
6046 * Returns the length (needed) of dest
6047 */
6048 static unsigned
6049quote_meta(char_u *dest, char_u *src, int len)
6050{
6051 unsigned m = (unsigned)len + 1; // one extra for the NUL
6052
6053 for ( ; --len >= 0; src++)
6054 {
6055 switch (*src)
6056 {
6057 case '.':
6058 case '*':
6059 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006060 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006061 break;
6062 // FALLTHROUGH
6063 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006064 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006065 break;
6066 // FALLTHROUGH
6067 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00006068 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006069 break;
6070 // FALLTHROUGH
6071 case '^': // currently it's not needed.
6072 case '$':
6073 m++;
6074 if (dest != NULL)
6075 *dest++ = '\\';
6076 break;
6077 }
6078 if (dest != NULL)
6079 *dest++ = *src;
6080 // Copy remaining bytes of a multibyte character.
6081 if (has_mbyte)
6082 {
6083 int i, mb_len;
6084
6085 mb_len = (*mb_ptr2len)(src) - 1;
6086 if (mb_len > 0 && len >= mb_len)
6087 for (i = 0; i < mb_len; ++i)
6088 {
6089 --len;
6090 ++src;
6091 if (dest != NULL)
6092 *dest++ = *src;
6093 }
6094 }
6095 }
6096 if (dest != NULL)
6097 *dest = NUL;
6098
6099 return m;
6100}
6101
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006102#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006103 void
6104free_insexpand_stuff(void)
6105{
John Marriott5e6ea922024-11-23 14:01:57 +01006106 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006107# ifdef FEAT_EVAL
6108 free_callback(&cfu_cb);
6109 free_callback(&ofu_cb);
6110 free_callback(&tsrfu_cb);
6111# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006112}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006113#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006114
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006115#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006116/*
6117 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6118 * spelled word, if there is one.
6119 */
6120 static void
6121spell_back_to_badword(void)
6122{
6123 pos_T tpos = curwin->w_cursor;
6124
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02006125 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006126 if (curwin->w_cursor.col != tpos.col)
6127 start_arrow(&tpos);
6128}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006129#endif