blob: 700ed5478fcc583e2400db2c237146bbb1317f4a [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
glepnir7baa0142024-10-09 20:19:25 +0200109 int cp_user_abbr_hlattr; // highlight attribute for abbr
glepnir0fe17f82024-10-08 22:26:44 +0200110 int cp_user_kind_hlattr; // highlight attribute for kind
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100111};
112
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113// values for cp_flags
114# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
115# define CP_FREE_FNAME 2 // cp_fname is allocated
116# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
117# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
118# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200119# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100120
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100121/*
122 * All the current matches are stored in a list.
123 * "compl_first_match" points to the start of the list.
124 * "compl_curr_match" points to the currently selected entry.
125 * "compl_shown_match" is different from compl_curr_match during
126 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000127 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128 */
129static compl_T *compl_first_match = NULL;
130static compl_T *compl_curr_match = NULL;
131static compl_T *compl_shown_match = NULL;
132static compl_T *compl_old_match = NULL;
133
134// After using a cursor key <Enter> selects a match in the popup menu,
135// otherwise it inserts a line break.
136static int compl_enter_selects = FALSE;
137
138// When "compl_leader" is not NULL only matches that start with this string
139// are used.
John Marriott5e6ea922024-11-23 14:01:57 +0100140static string_T compl_leader = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100141
142static int compl_get_longest = FALSE; // put longest common string
143 // in compl_leader
144
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100145// Selected one of the matches. When FALSE the match was edited or using the
146// longest common string.
147static int compl_used_match;
148
149// didn't finish finding completions.
150static int compl_was_interrupted = FALSE;
151
152// Set when character typed while looking for matches and it means we should
153// stop looking for matches.
154static int compl_interrupted = FALSE;
155
156static int compl_restarting = FALSE; // don't insert match
157
158// When the first completion is done "compl_started" is set. When it's
159// FALSE the word to be completed must be located.
160static int compl_started = FALSE;
161
162// Which Ctrl-X mode are we in?
163static int ctrl_x_mode = CTRL_X_NORMAL;
164
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000165static int compl_matches = 0; // number of completion matches
John Marriott5e6ea922024-11-23 14:01:57 +0100166static string_T compl_pattern = {NULL, 0};
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100167static int compl_direction = FORWARD;
168static int compl_shows_dir = FORWARD;
169static int compl_pending = 0; // > 1 for postponed CTRL-N
170static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000171// Length in bytes of the text being completed (this is deleted to be replaced
172// by the match.)
173static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100174static colnr_T compl_col = 0; // column where the text starts
175 // that is being completed
glepnir6a38aff2024-12-16 21:56:16 +0100176static colnr_T compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100177static string_T compl_orig_text = {NULL, 0}; // text as it was before
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178 // completion started
179static int compl_cont_mode = 0;
180static expand_T compl_xp;
181
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000182// List of flags for method of completion.
183static int compl_cont_status = 0;
184# define CONT_ADDING 1 // "normal" or "adding" expansion
185# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
186 // it's set only iff N_ADDS is set
187# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
188# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
189 // if so, word-wise-expansion will set SOL
190# define CONT_SOL 16 // pattern includes start of line, just for
191 // word-wise expansion, not set for ^X^L
192# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
193 // expansion, (eg use complete=.)
194
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100195static int compl_opt_refresh_always = FALSE;
196static int compl_opt_suppress_empty = FALSE;
197
glepnira218cc62024-06-03 19:32:39 +0200198static int compl_selected_item = -1;
199
glepnir8159fb12024-07-17 20:32:54 +0200200static int *compl_fuzzy_scores;
201
glepnir6a38aff2024-12-16 21:56:16 +0100202// "compl_match_array" points the currently displayed list of entries in the
203// popup menu. It is NULL when there is no popup menu.
204static pumitem_T *compl_match_array = NULL;
205static int compl_match_arraysize;
206
glepnir80b66202024-11-27 21:53:53 +0100207static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int *user_hl);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100208static void ins_compl_longest_match(compl_T *match);
209static void ins_compl_del_pum(void);
210static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
211static char_u *find_line_end(char_u *ptr);
212static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100213static int ins_compl_need_restart(void);
214static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000215static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100216static void ins_compl_restart(void);
John Marriott5e6ea922024-11-23 14:01:57 +0100217static void ins_compl_set_original_text(char_u *str, size_t len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
219# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
220static void ins_compl_add_list(list_T *list);
221static void ins_compl_add_dict(dict_T *dict);
222# endif
223static int ins_compl_key2dir(int c);
224static int ins_compl_pum_key(int c);
225static int ins_compl_key2count(int c);
226static void show_pum(int prev_w_wrow, int prev_w_leftcol);
227static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100228
229#ifdef FEAT_SPELL
230static void spell_back_to_badword(void);
231static int spell_bad_len = 0; // length of located bad word
232#endif
233
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100234/*
235 * CTRL-X pressed in Insert mode.
236 */
237 void
238ins_ctrl_x(void)
239{
zeertzjqdca29d92021-08-31 19:12:51 +0200240 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100241 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000242 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 if (compl_cont_status & CONT_N_ADDS)
244 compl_cont_status |= CONT_INTRPT;
245 else
246 compl_cont_status = 0;
247 // We're not sure which CTRL-X mode it will be yet
248 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
249 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
250 edit_submode_pre = NULL;
251 showmode();
252 }
zeertzjqdca29d92021-08-31 19:12:51 +0200253 else
254 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
255 // CTRL-V look like CTRL-N
256 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100257
LemonBoy2bf52dd2022-04-09 18:17:34 +0100258 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100259}
260
261/*
262 * Functions to check the current CTRL-X mode.
263 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000264int ctrl_x_mode_none(void)
265 { return ctrl_x_mode == 0; }
266int ctrl_x_mode_normal(void)
267 { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void)
269 { return ctrl_x_mode == CTRL_X_SCROLL; }
270int ctrl_x_mode_whole_line(void)
271 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
272int ctrl_x_mode_files(void)
273 { return ctrl_x_mode == CTRL_X_FILES; }
274int ctrl_x_mode_tags(void)
275 { return ctrl_x_mode == CTRL_X_TAGS; }
276int ctrl_x_mode_path_patterns(void)
277 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
278int ctrl_x_mode_path_defines(void)
279 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
280int ctrl_x_mode_dictionary(void)
281 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
282int ctrl_x_mode_thesaurus(void)
283 { return ctrl_x_mode == CTRL_X_THESAURUS; }
284int ctrl_x_mode_cmdline(void)
285 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200286 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000287int ctrl_x_mode_function(void)
288 { return ctrl_x_mode == CTRL_X_FUNCTION; }
289int ctrl_x_mode_omni(void)
290 { return ctrl_x_mode == CTRL_X_OMNI; }
291int ctrl_x_mode_spell(void)
292 { return ctrl_x_mode == CTRL_X_SPELL; }
293static int ctrl_x_mode_eval(void)
294 { return ctrl_x_mode == CTRL_X_EVAL; }
295int ctrl_x_mode_line_or_eval(void)
296 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100297
298/*
299 * Whether other than default completion has been selected.
300 */
301 int
302ctrl_x_mode_not_default(void)
303{
304 return ctrl_x_mode != CTRL_X_NORMAL;
305}
306
307/*
zeertzjqdca29d92021-08-31 19:12:51 +0200308 * Whether CTRL-X was typed without a following character,
309 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100310 */
311 int
312ctrl_x_mode_not_defined_yet(void)
313{
314 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
315}
316
317/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000318 * Return TRUE if currently in "normal" or "adding" insert completion matches
319 * state
320 */
321 int
322compl_status_adding(void)
323{
324 return compl_cont_status & CONT_ADDING;
325}
326
327/*
328 * Return TRUE if the completion pattern includes start of line, just for
329 * word-wise expansion.
330 */
331 int
332compl_status_sol(void)
333{
334 return compl_cont_status & CONT_SOL;
335}
336
337/*
338 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
339 */
340 int
341compl_status_local(void)
342{
343 return compl_cont_status & CONT_LOCAL;
344}
345
346/*
347 * Clear the completion status flags
348 */
349 void
350compl_status_clear(void)
351{
352 compl_cont_status = 0;
353}
354
355/*
356 * Return TRUE if completion is using the forward direction matches
357 */
358 static int
359compl_dir_forward(void)
360{
361 return compl_direction == FORWARD;
362}
363
364/*
365 * Return TRUE if currently showing forward completion matches
366 */
367 static int
368compl_shows_dir_forward(void)
369{
370 return compl_shows_dir == FORWARD;
371}
372
373/*
374 * Return TRUE if currently showing backward completion matches
375 */
376 static int
377compl_shows_dir_backward(void)
378{
379 return compl_shows_dir == BACKWARD;
380}
381
382/*
383 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100384 */
385 int
386has_compl_option(int dict_opt)
387{
388 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200389#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100390 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200391#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100393 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
394#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100395 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100396#endif
397 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100398 {
399 ctrl_x_mode = CTRL_X_NORMAL;
400 edit_submode = NULL;
401 msg_attr(dict_opt ? _("'dictionary' option is empty")
402 : _("'thesaurus' option is empty"),
403 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100404 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100405 {
406 vim_beep(BO_COMPL);
407 setcursor();
408 out_flush();
409#ifdef FEAT_EVAL
410 if (!get_vim_var_nr(VV_TESTING))
411#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100412 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100413 }
414 return FALSE;
415 }
416 return TRUE;
417}
418
419/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000420 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100421 * This depends on the current mode.
422 */
423 int
424vim_is_ctrl_x_key(int c)
425{
426 // Always allow ^R - let its results then be checked
427 if (c == Ctrl_R)
428 return TRUE;
429
430 // Accept <PageUp> and <PageDown> if the popup menu is visible.
431 if (ins_compl_pum_key(c))
432 return TRUE;
433
434 switch (ctrl_x_mode)
435 {
436 case 0: // Not in any CTRL-X mode
437 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
438 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200439 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100440 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
441 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
442 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
443 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
444 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200445 || c == Ctrl_S || c == Ctrl_K || c == 's'
446 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100447 case CTRL_X_SCROLL:
448 return (c == Ctrl_Y || c == Ctrl_E);
449 case CTRL_X_WHOLE_LINE:
450 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
451 case CTRL_X_FILES:
452 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
453 case CTRL_X_DICTIONARY:
454 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_THESAURUS:
456 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_TAGS:
458 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
459#ifdef FEAT_FIND_ID
460 case CTRL_X_PATH_PATTERNS:
461 return (c == Ctrl_P || c == Ctrl_N);
462 case CTRL_X_PATH_DEFINES:
463 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
464#endif
465 case CTRL_X_CMDLINE:
466 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
467 || c == Ctrl_X);
468#ifdef FEAT_COMPL_FUNC
469 case CTRL_X_FUNCTION:
470 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
471 case CTRL_X_OMNI:
472 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
473#endif
474 case CTRL_X_SPELL:
475 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
476 case CTRL_X_EVAL:
477 return (c == Ctrl_P || c == Ctrl_N);
478 }
479 internal_error("vim_is_ctrl_x_key()");
480 return FALSE;
481}
482
483/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000484 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000485 */
486 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000487match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000488{
489 return match->cp_flags & CP_ORIGINAL_TEXT;
490}
491
492/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000493 * Returns TRUE if "match" is the first match in the completion list.
494 */
495 static int
496is_first_match(compl_T *match)
497{
498 return match == compl_first_match;
499}
500
501/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100502 * Return TRUE when character "c" is part of the item currently being
503 * completed. Used to decide whether to abandon complete mode when the menu
504 * is visible.
505 */
506 int
507ins_compl_accept_char(int c)
508{
509 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
510 // When expanding an identifier only accept identifier chars.
511 return vim_isIDc(c);
512
513 switch (ctrl_x_mode)
514 {
515 case CTRL_X_FILES:
516 // When expanding file name only accept file name chars. But not
517 // path separators, so that "proto/<Tab>" expands files in
518 // "proto", not "proto/" as a whole
519 return vim_isfilec(c) && !vim_ispathsep(c);
520
521 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200522 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100523 case CTRL_X_OMNI:
524 // Command line and Omni completion can work with just about any
525 // printable character, but do stop at white space.
526 return vim_isprintc(c) && !VIM_ISWHITE(c);
527
528 case CTRL_X_WHOLE_LINE:
529 // For while line completion a space can be part of the line.
530 return vim_isprintc(c);
531 }
532 return vim_iswordc(c);
533}
534
535/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000536 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100537 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000538 */
539 static char_u *
540ins_compl_infercase_gettext(
541 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100542 int char_len,
543 int compl_char_len,
544 int min_len,
545 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000546{
547 int *wca; // Wide character array.
548 char_u *p;
549 int i, c;
550 int has_lower = FALSE;
551 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100552 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000553
554 IObuff[0] = NUL;
555
556 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100557 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000558 if (wca == NULL)
559 return IObuff;
560
561 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100562 for (i = 0; i < char_len; ++i)
glepnir6e199932024-12-14 21:13:27 +0100563 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000564 if (has_mbyte)
565 wca[i] = mb_ptr2char_adv(&p);
566 else
567 wca[i] = *(p++);
glepnir6e199932024-12-14 21:13:27 +0100568 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000569
570 // Rule 1: Were any chars converted to lower?
John Marriott5e6ea922024-11-23 14:01:57 +0100571 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000572 for (i = 0; i < min_len; ++i)
573 {
glepnir6e199932024-12-14 21:13:27 +0100574 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000575 if (MB_ISLOWER(c))
576 {
577 has_lower = TRUE;
578 if (MB_ISUPPER(wca[i]))
579 {
580 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100581 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000582 wca[i] = MB_TOLOWER(wca[i]);
583 break;
584 }
585 }
586 }
587
588 // Rule 2: No lower case, 2nd consecutive letter converted to
589 // upper case.
590 if (!has_lower)
591 {
John Marriott5e6ea922024-11-23 14:01:57 +0100592 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000593 for (i = 0; i < min_len; ++i)
594 {
glepnir6e199932024-12-14 21:13:27 +0100595 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000596 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
597 {
598 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100599 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000600 wca[i] = MB_TOUPPER(wca[i]);
601 break;
602 }
603 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
604 }
605 }
606
607 // Copy the original case of the part we typed.
John Marriott5e6ea922024-11-23 14:01:57 +0100608 p = compl_orig_text.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000609 for (i = 0; i < min_len; ++i)
610 {
glepnir6e199932024-12-14 21:13:27 +0100611 c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000612 if (MB_ISLOWER(c))
613 wca[i] = MB_TOLOWER(wca[i]);
614 else if (MB_ISUPPER(c))
615 wca[i] = MB_TOUPPER(wca[i]);
616 }
617
618 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000619 p = IObuff;
620 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100621 ga_init2(&gap, 1, 500);
622 while (i < char_len)
623 {
624 if (gap.ga_data != NULL)
625 {
626 if (ga_grow(&gap, 10) == FAIL)
627 {
628 ga_clear(&gap);
629 return (char_u *)"[failed]";
630 }
631 p = (char_u *)gap.ga_data + gap.ga_len;
632 if (has_mbyte)
633 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
634 else
635 {
636 *p = wca[i++];
637 ++gap.ga_len;
638 }
639 }
640 else if ((p - IObuff) + 6 >= IOSIZE)
641 {
642 // Multi-byte characters can occupy up to five bytes more than
643 // ASCII characters, and we also need one byte for NUL, so when
644 // getting to six bytes from the edge of IObuff switch to using a
645 // growarray. Add the character in the next round.
646 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100647 {
648 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100649 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100650 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100651 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100652 STRCPY(gap.ga_data, IObuff);
John Marriott5e6ea922024-11-23 14:01:57 +0100653 gap.ga_len = (int)(p - IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100654 }
655 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000656 p += (*mb_char2bytes)(wca[i++], p);
657 else
658 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100659 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000660 vim_free(wca);
661
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100662 if (gap.ga_data != NULL)
663 {
664 *tofree = gap.ga_data;
665 return gap.ga_data;
666 }
667
668 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000669 return IObuff;
670}
671
672/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
674 * case of the originally typed text is used, and the case of the completed
675 * text is inferred, ie this tries to work out what case you probably wanted
676 * the rest of the word to be in -- webb
677 */
678 int
679ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200680 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100681 int len,
682 int icase,
683 char_u *fname,
684 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200685 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100686{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200687 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100688 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100689 int char_len; // count multi-byte characters
690 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200692 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100693 int res;
694 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695
696 if (p_ic && curbuf->b_p_inf && len > 0)
697 {
698 // Infer case of completed part.
699
700 // Find actual length of completion.
701 if (has_mbyte)
702 {
703 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100704 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100705 while (*p != NUL)
706 {
707 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100708 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709 }
710 }
711 else
glepnir6e199932024-12-14 21:13:27 +0100712 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100713 char_len = len;
glepnir6e199932024-12-14 21:13:27 +0100714 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715
716 // Find actual length of original text.
717 if (has_mbyte)
718 {
John Marriott5e6ea922024-11-23 14:01:57 +0100719 p = compl_orig_text.string;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100720 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100721 while (*p != NUL)
722 {
723 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 }
726 }
727 else
glepnir6e199932024-12-14 21:13:27 +0100728 {
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100729 compl_char_len = compl_length;
glepnir6e199932024-12-14 21:13:27 +0100730 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100731
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 // thesaurus, only use the minimum when comparing.
glepnir6e199932024-12-14 21:13:27 +0100734 min_len = MIN(char_len, compl_char_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 str = ins_compl_infercase_gettext(str, char_len,
737 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200739 if (cont_s_ipos)
740 flags |= CP_CONT_S_IPOS;
741 if (icase)
742 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200743
glepnir5c66e232024-11-15 19:58:27 +0100744 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100745 vim_free(tofree);
746 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100747}
748
749/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000750 * Add a match to the list of matches. The arguments are:
751 * str - text of the match to add
752 * len - length of "str". If -1, then the length of "str" is
753 * computed.
754 * fname - file name to associate with this match.
755 * cptext - list of strings to use with this match (for abbr, menu, info
756 * and kind)
757 * user_data - user supplied data (any vim type) for this match
758 * cdir - match direction. If 0, use "compl_direction".
759 * flags_arg - match flags (cp_flags)
760 * adup - accept this match even if it is already present.
glepnir80b66202024-11-27 21:53:53 +0100761 * *user_hl - list of extra highlight attributes for abbr kind.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000762 * If "cdir" is FORWARD, then the match is added after the current match.
763 * Otherwise, it is added before the current match.
764 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100765 * If the given string is already in the list of completions, then return
766 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
767 * maybe because alloc() returns NULL, then FAIL is returned.
768 */
769 static int
770ins_compl_add(
771 char_u *str,
772 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100773 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 char_u **cptext, // extra text for popup menu or NULL
775 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100776 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200777 int flags_arg,
glepnir508e7852024-07-25 21:39:08 +0200778 int adup, // accept duplicate match
glepnir80b66202024-11-27 21:53:53 +0100779 int *user_hl) // user abbr/kind hlattr
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780{
781 compl_T *match;
782 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200783 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784
Bram Moolenaarceb06192021-04-04 15:05:22 +0200785 if (flags & CP_FAST)
786 fast_breakcheck();
787 else
788 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100789 if (got_int)
790 return FAIL;
791 if (len < 0)
792 len = (int)STRLEN(str);
793
794 // If the same match is already present, don't add it.
795 if (compl_first_match != NULL && !adup)
796 {
797 match = compl_first_match;
798 do
799 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000800 if (!match_at_original_text(match)
John Marriott5e6ea922024-11-23 14:01:57 +0100801 && STRNCMP(match->cp_str.string, str, len) == 0
802 && ((int)match->cp_str.length <= len
803 || match->cp_str.string[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 return NOTDONE;
805 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000806 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 }
808
809 // Remove any popup menu before changing the list of matches.
810 ins_compl_del_pum();
811
812 // Allocate a new match structure.
813 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200814 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 if (match == NULL)
816 return FAIL;
817 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200818 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 match->cp_number = 0;
John Marriott5e6ea922024-11-23 14:01:57 +0100820 if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100821 {
822 vim_free(match);
823 return FAIL;
824 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100825
John Marriott5e6ea922024-11-23 14:01:57 +0100826 match->cp_str.length = len;
827
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100828 // match-fname is:
829 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200830 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100831 // - NULL otherwise. --Acevedo
832 if (fname != NULL
833 && compl_curr_match != NULL
834 && compl_curr_match->cp_fname != NULL
835 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
836 match->cp_fname = compl_curr_match->cp_fname;
837 else if (fname != NULL)
838 {
839 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200840 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100841 }
842 else
843 match->cp_fname = NULL;
844 match->cp_flags = flags;
glepnir80b66202024-11-27 21:53:53 +0100845 match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1;
846 match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100847
848 if (cptext != NULL)
849 {
850 int i;
851
852 for (i = 0; i < CPT_COUNT; ++i)
glepnir6e199932024-12-14 21:13:27 +0100853 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100854 if (cptext[i] != NULL && *cptext[i] != NUL)
855 match->cp_text[i] = vim_strsave(cptext[i]);
glepnir6e199932024-12-14 21:13:27 +0100856 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100857 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100858#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100859 if (user_data != NULL)
860 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100861#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000863 // Link the new match structure after (FORWARD) or before (BACKWARD) the
864 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100865 if (compl_first_match == NULL)
866 match->cp_next = match->cp_prev = NULL;
867 else if (dir == FORWARD)
868 {
869 match->cp_next = compl_curr_match->cp_next;
870 match->cp_prev = compl_curr_match;
871 }
872 else // BACKWARD
873 {
874 match->cp_next = compl_curr_match;
875 match->cp_prev = compl_curr_match->cp_prev;
876 }
877 if (match->cp_next)
878 match->cp_next->cp_prev = match;
879 if (match->cp_prev)
880 match->cp_prev->cp_next = match;
881 else // if there's nothing before, it is the first match
882 compl_first_match = match;
883 compl_curr_match = match;
884
885 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200886 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100887 ins_compl_longest_match(match);
888
889 return OK;
890}
891
892/*
893 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200894 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100895 */
896 static int
897ins_compl_equal(compl_T *match, char_u *str, int len)
898{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200899 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200900 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200901 if (match->cp_flags & CP_ICASE)
John Marriott5e6ea922024-11-23 14:01:57 +0100902 return STRNICMP(match->cp_str.string, str, (size_t)len) == 0;
903 return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100904}
905
906/*
glepnir6a38aff2024-12-16 21:56:16 +0100907 * when len is -1 mean use whole length of p otherwise part of p
908 */
909 static void
910ins_compl_insert_bytes(char_u *p, int len)
911{
912 if (len == -1)
913 len = (int)STRLEN(p);
914 ins_bytes_len(p, len);
915 compl_ins_end_col = curwin->w_cursor.col - 1;
916}
917
918/*
919 * Checks if the column is within the currently inserted completion text
920 * column range. If it is, it returns a special highlight attribute.
921 * -1 mean normal item.
922 */
923 int
924ins_compl_col_range_attr(int col)
925{
926 if (col >= compl_col && col < compl_ins_end_col)
927 return syn_name2attr((char_u *)"ComplMatchIns");
928
929 return -1;
930}
931
932/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 * Reduce the longest common string for match "match".
934 */
935 static void
936ins_compl_longest_match(compl_T *match)
937{
938 char_u *p, *s;
939 int c1, c2;
940 int had_match;
941
John Marriott5e6ea922024-11-23 14:01:57 +0100942 if (compl_leader.string == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100943 {
944 // First match, use it as a whole.
John Marriott5e6ea922024-11-23 14:01:57 +0100945 compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length);
946 if (compl_leader.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000947 return;
948
John Marriott5e6ea922024-11-23 14:01:57 +0100949 compl_leader.length = match->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000950 had_match = (curwin->w_cursor.col > compl_col);
951 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +0100952 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000953 ins_redraw(FALSE);
954
955 // When the match isn't there (to avoid matching itself) remove it
956 // again after redrawing.
957 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100958 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100959 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000960
961 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100962 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000963
964 // Reduce the text if this match differs from compl_leader.
John Marriott5e6ea922024-11-23 14:01:57 +0100965 p = compl_leader.string;
966 s = match->cp_str.string;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967 while (*p != NUL)
968 {
969 if (has_mbyte)
970 {
971 c1 = mb_ptr2char(p);
972 c2 = mb_ptr2char(s);
973 }
974 else
975 {
976 c1 = *p;
977 c2 = *s;
978 }
979 if ((match->cp_flags & CP_ICASE)
980 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
981 break;
982 if (has_mbyte)
983 {
984 MB_PTR_ADV(p);
985 MB_PTR_ADV(s);
986 }
987 else
988 {
989 ++p;
990 ++s;
991 }
992 }
993
994 if (*p != NUL)
995 {
996 // Leader was shortened, need to change the inserted text.
997 *p = NUL;
John Marriott5e6ea922024-11-23 14:01:57 +0100998 compl_leader.length = (size_t)(p - compl_leader.string);
999
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001000 had_match = (curwin->w_cursor.col > compl_col);
1001 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001002 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001003 ins_redraw(FALSE);
1004
1005 // When the match isn't there (to avoid matching itself) remove it
1006 // again after redrawing.
1007 if (!had_match)
1008 ins_compl_delete();
1009 }
1010
1011 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001012}
1013
1014/*
1015 * Add an array of matches to the list of matches.
1016 * Frees matches[].
1017 */
1018 static void
1019ins_compl_add_matches(
1020 int num_matches,
1021 char_u **matches,
1022 int icase)
1023{
1024 int i;
1025 int add_r = OK;
1026 int dir = compl_direction;
1027
1028 for (i = 0; i < num_matches && add_r != FAIL; i++)
glepnir6e199932024-12-14 21:13:27 +01001029 {
1030 add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1031 CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL);
1032 if (add_r == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033 // if dir was BACKWARD then honor it just once
1034 dir = FORWARD;
glepnir6e199932024-12-14 21:13:27 +01001035 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001036 FreeWild(num_matches, matches);
1037}
1038
1039/*
1040 * Make the completion list cyclic.
1041 * Return the number of matches (excluding the original).
1042 */
1043 static int
1044ins_compl_make_cyclic(void)
1045{
1046 compl_T *match;
1047 int count = 0;
1048
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001049 if (compl_first_match == NULL)
1050 return 0;
1051
1052 // Find the end of the list.
1053 match = compl_first_match;
1054 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001055 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001056 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001057 match = match->cp_next;
1058 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001059 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001060 match->cp_next = compl_first_match;
1061 compl_first_match->cp_prev = match;
1062
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001063 return count;
1064}
1065
1066/*
1067 * Return whether there currently is a shown match.
1068 */
1069 int
1070ins_compl_has_shown_match(void)
1071{
1072 return compl_shown_match == NULL
1073 || compl_shown_match != compl_shown_match->cp_next;
1074}
1075
1076/*
1077 * Return whether the shown match is long enough.
1078 */
1079 int
1080ins_compl_long_shown_match(void)
1081{
John Marriott5e6ea922024-11-23 14:01:57 +01001082 return (int)compl_shown_match->cp_str.length
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001083 > curwin->w_cursor.col - compl_col;
1084}
1085
1086/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001087 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001089 unsigned int
1090get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001091{
zeertzjq529b9ad2024-06-05 20:27:06 +02001092 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001093}
1094
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001095/*
1096 * Update the screen and when there is any scrolling remove the popup menu.
1097 */
1098 static void
1099ins_compl_upd_pum(void)
1100{
1101 int h;
1102
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001103 if (compl_match_array == NULL)
1104 return;
1105
1106 h = curwin->w_cline_height;
1107 // Update the screen later, before drawing the popup menu over it.
1108 pum_call_update_screen();
1109 if (h != curwin->w_cline_height)
1110 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001111}
1112
1113/*
1114 * Remove any popup menu.
1115 */
1116 static void
1117ins_compl_del_pum(void)
1118{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001119 if (compl_match_array == NULL)
1120 return;
1121
1122 pum_undisplay();
1123 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001124}
1125
1126/*
1127 * Return TRUE if the popup menu should be displayed.
1128 */
1129 int
1130pum_wanted(void)
1131{
1132 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001133 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001134 return FALSE;
1135
1136 // The display looks bad on a B&W display.
1137 if (t_colors < 8
1138#ifdef FEAT_GUI
1139 && !gui.in_use
1140#endif
1141 )
1142 return FALSE;
1143 return TRUE;
1144}
1145
1146/*
1147 * Return TRUE if there are two or more matches to be shown in the popup menu.
1148 * One if 'completopt' contains "menuone".
1149 */
1150 static int
1151pum_enough_matches(void)
1152{
1153 compl_T *compl;
glepnir6e199932024-12-14 21:13:27 +01001154 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001155
1156 // Don't display the popup menu if there are no matches or there is only
1157 // one (ignoring the original text).
1158 compl = compl_first_match;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159 do
1160 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001161 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001162 break;
1163 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001164 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001165
zeertzjq529b9ad2024-06-05 20:27:06 +02001166 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001167 return (i >= 1);
1168 return (i >= 2);
1169}
1170
Bram Moolenaar3075a452021-11-17 15:51:52 +00001171#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001172/*
1173 * Allocate Dict for the completed item.
1174 * { word, abbr, menu, kind, info }
1175 */
1176 static dict_T *
1177ins_compl_dict_alloc(compl_T *match)
1178{
1179 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1180
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001181 if (dict == NULL)
1182 return NULL;
1183
John Marriott5e6ea922024-11-23 14:01:57 +01001184 dict_add_string(dict, "word", match->cp_str.string);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001185 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1186 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1187 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1188 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1189 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1190 dict_add_string(dict, "user_data", (char_u *)"");
1191 else
1192 dict_add_tv(dict, "user_data", &match->cp_user_data);
1193
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001194 return dict;
1195}
1196
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001197/*
1198 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1199 * completion menu is changed.
1200 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 static void
1202trigger_complete_changed_event(int cur)
1203{
1204 dict_T *v_event;
1205 dict_T *item;
1206 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001207 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001208
1209 if (recursive)
1210 return;
1211
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212 if (cur < 0)
1213 item = dict_alloc();
1214 else
1215 item = ins_compl_dict_alloc(compl_curr_match);
1216 if (item == NULL)
1217 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001218 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001219 dict_add_dict(v_event, "completed_item", item);
1220 pum_set_event_info(v_event);
1221 dict_set_items_ro(v_event);
1222
1223 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001224 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001225 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001226 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001227 recursive = FALSE;
1228
Bram Moolenaar3075a452021-11-17 15:51:52 +00001229 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001230}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001231#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001232
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001233/*
glepnira218cc62024-06-03 19:32:39 +02001234 * pumitem qsort compare func
1235 */
1236 static int
zeertzjq8e567472024-06-14 20:04:42 +02001237ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001238{
1239 const int sa = (*(pumitem_T *)a).pum_score;
1240 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001241 const int ia = (*(pumitem_T *)a).pum_idx;
1242 const int ib = (*(pumitem_T *)b).pum_idx;
1243 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001244}
1245
1246/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001247 * Build a popup menu to show the completion matches.
1248 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1249 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001250 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001251 static int
1252ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001253{
1254 compl_T *compl;
1255 compl_T *shown_compl = NULL;
1256 int did_find_shown_match = FALSE;
1257 int shown_match_ok = FALSE;
glepnira49c0772024-11-30 10:56:30 +01001258 int i = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001259 int cur = -1;
glepnira218cc62024-06-03 19:32:39 +02001260 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001261 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001262 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1263 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
glepnir80b66202024-11-27 21:53:53 +01001264 compl_T *match_head = NULL;
1265 compl_T *match_tail = NULL;
1266 compl_T *match_next = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001267
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001268 // Need to build the popup menu list.
1269 compl_match_arraysize = 0;
1270 compl = compl_first_match;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001271
glepnira49c0772024-11-30 10:56:30 +01001272 // If the current match is the original text don't find the first
1273 // match after it, don't highlight anything.
1274 if (match_at_original_text(compl_shown_match))
1275 shown_match_ok = TRUE;
1276
1277 if (compl_leader.string != NULL
1278 && STRCMP(compl_leader.string, compl_orig_text.string) == 0
1279 && shown_match_ok == FALSE)
1280 compl_shown_match = compl_no_select ? compl_first_match
1281 : compl_first_match->cp_next;
1282
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001283 do
1284 {
zeertzjq551d8c32024-06-05 19:53:32 +02001285 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1286 // set the cp_score for later comparisons.
John Marriott5e6ea922024-11-23 14:01:57 +01001287 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1288 compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
glepnira218cc62024-06-03 19:32:39 +02001289
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001290 if (!match_at_original_text(compl)
John Marriott5e6ea922024-11-23 14:01:57 +01001291 && (compl_leader.string == NULL
1292 || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02001293 || (compl_fuzzy_match && compl->cp_score > 0)))
glepnir80b66202024-11-27 21:53:53 +01001294 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001295 ++compl_match_arraysize;
glepnir80b66202024-11-27 21:53:53 +01001296 if (match_head == NULL)
1297 match_head = compl;
1298 else
glepnira49c0772024-11-30 10:56:30 +01001299 match_tail->cp_match_next = compl;
glepnir80b66202024-11-27 21:53:53 +01001300 match_tail = compl;
glepnira49c0772024-11-30 10:56:30 +01001301
1302 if (!shown_match_ok && !compl_fuzzy_match)
1303 {
1304 if (compl == compl_shown_match || did_find_shown_match)
1305 {
1306 // This item is the shown match or this is the
1307 // first displayed item after the shown match.
1308 compl_shown_match = compl;
1309 did_find_shown_match = TRUE;
1310 shown_match_ok = TRUE;
1311 }
1312 else
1313 // Remember this displayed match for when the
1314 // shown match is just below it.
1315 shown_compl = compl;
1316 cur = i;
1317 }
1318 else if (compl_fuzzy_match)
1319 {
1320 if (i == 0)
1321 shown_compl = compl;
1322 // Update the maximum fuzzy score and the shown match
1323 // if the current item's score is higher
1324 if (compl->cp_score > max_fuzzy_score)
1325 {
1326 did_find_shown_match = TRUE;
1327 max_fuzzy_score = compl->cp_score;
1328 if (!compl_no_select)
1329 compl_shown_match = compl;
1330 }
1331
1332 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1333 {
1334 cur = i;
1335 shown_match_ok = TRUE;
1336 }
1337 }
1338 i++;
glepnir80b66202024-11-27 21:53:53 +01001339 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001340
glepnira218cc62024-06-03 19:32:39 +02001341 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001342 {
1343 did_find_shown_match = TRUE;
1344
1345 // When the original text is the shown match don't set
1346 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001347 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 shown_match_ok = TRUE;
1349
1350 if (!shown_match_ok && shown_compl != NULL)
1351 {
1352 // The shown match isn't displayed, set it to the
1353 // previously displayed match.
1354 compl_shown_match = shown_compl;
1355 shown_match_ok = TRUE;
1356 }
1357 }
glepnira49c0772024-11-30 10:56:30 +01001358 compl = compl->cp_next;
1359 } while (compl != NULL && !is_first_match(compl));
1360
1361 if (compl_match_arraysize == 0)
1362 return -1;
1363
1364 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1365 if (compl_match_array == NULL)
1366 return -1;
1367
1368 compl = match_head;
1369 i = 0;
1370 while (compl != NULL)
1371 {
glepnir6e199932024-12-14 21:13:27 +01001372 compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL
1373 ? compl->cp_text[CPT_ABBR] : compl->cp_str.string;
glepnira49c0772024-11-30 10:56:30 +01001374 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1375 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1376 compl_match_array[i].pum_score = compl->cp_score;
1377 compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr;
1378 compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
glepnir6e199932024-12-14 21:13:27 +01001379 compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL
1380 ? compl->cp_text[CPT_MENU] : compl->cp_fname;
glepnir80b66202024-11-27 21:53:53 +01001381 match_next = compl->cp_match_next;
1382 compl->cp_match_next = NULL;
1383 compl = match_next;
1384 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001385
John Marriott5e6ea922024-11-23 14:01:57 +01001386 if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001387 {
1388 for (i = 0; i < compl_match_arraysize; i++)
1389 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001390 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001391 qsort(compl_match_array, (size_t)compl_match_arraysize,
1392 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001393 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001394 }
glepnira218cc62024-06-03 19:32:39 +02001395
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001396 if (!shown_match_ok) // no displayed match at all
1397 cur = -1;
1398
1399 return cur;
1400}
1401
1402/*
1403 * Show the popup menu for the list of matches.
1404 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1405 */
1406 void
1407ins_compl_show_pum(void)
1408{
1409 int i;
1410 int cur = -1;
1411 colnr_T col;
1412
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001413 if (!pum_wanted() || !pum_enough_matches())
1414 return;
1415
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001416 // Update the screen later, before drawing the popup menu over it.
1417 pum_call_update_screen();
1418
1419 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001420 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001421 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 else
1423 {
1424 // popup menu already exists, only need to find the current item.
1425 for (i = 0; i < compl_match_arraysize; ++i)
John Marriott5e6ea922024-11-23 14:01:57 +01001426 if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001427 || compl_match_array[i].pum_text
1428 == compl_shown_match->cp_text[CPT_ABBR])
1429 {
1430 cur = i;
1431 break;
1432 }
1433 }
1434
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001435 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001436 {
1437#ifdef FEAT_EVAL
1438 if (compl_started && has_completechanged())
1439 trigger_complete_changed_event(cur);
1440#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001441 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001442 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001443
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001444 // In Replace mode when a $ is displayed at the end of the line only
1445 // part of the screen would be updated. We do need to redraw here.
1446 dollar_vcol = -1;
1447
1448 // Compute the screen column of the start of the completed text.
1449 // Use the cursor to get all wrapping and other settings right.
1450 col = curwin->w_cursor.col;
1451 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001452 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001453 pum_display(compl_match_array, compl_match_arraysize, cur);
1454 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001455
glepnircbb46b42024-02-03 18:11:13 +01001456 // After adding leader, set the current match to shown match.
1457 if (compl_started && compl_curr_match != compl_shown_match)
1458 compl_curr_match = compl_shown_match;
1459
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001460#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001461 if (has_completechanged())
1462 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001463#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001464}
1465
1466#define DICT_FIRST (1) // use just first element in "dict"
1467#define DICT_EXACT (2) // "dict" is the exact name of a file
1468
1469/*
glepnir40c1c332024-06-11 19:37:04 +02001470 * Get current completion leader
1471 */
1472 char_u *
1473ins_compl_leader(void)
1474{
John Marriott5e6ea922024-11-23 14:01:57 +01001475 return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string;
1476}
1477
1478/*
1479 * Get current completion leader length
1480 */
1481 size_t
1482ins_compl_leader_len(void)
1483{
1484 return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length;
glepnir40c1c332024-06-11 19:37:04 +02001485}
1486
1487/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001488 * Add any identifiers that match the given pattern "pat" in the list of
1489 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001490 */
1491 static void
1492ins_compl_dictionaries(
1493 char_u *dict_start,
1494 char_u *pat,
1495 int flags, // DICT_FIRST and/or DICT_EXACT
1496 int thesaurus) // Thesaurus completion
1497{
1498 char_u *dict = dict_start;
1499 char_u *ptr;
1500 char_u *buf;
1501 regmatch_T regmatch;
1502 char_u **files;
1503 int count;
1504 int save_p_scs;
1505 int dir = compl_direction;
1506
1507 if (*dict == NUL)
1508 {
1509#ifdef FEAT_SPELL
1510 // When 'dictionary' is empty and spell checking is enabled use
1511 // "spell".
1512 if (!thesaurus && curwin->w_p_spell)
1513 dict = (char_u *)"spell";
1514 else
1515#endif
1516 return;
1517 }
1518
1519 buf = alloc(LSIZE);
1520 if (buf == NULL)
1521 return;
1522 regmatch.regprog = NULL; // so that we can goto theend
1523
1524 // If 'infercase' is set, don't use 'smartcase' here
1525 save_p_scs = p_scs;
1526 if (curbuf->b_p_inf)
1527 p_scs = FALSE;
1528
1529 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1530 // to only match at the start of a line. Otherwise just match the
1531 // pattern. Also need to double backslashes.
1532 if (ctrl_x_mode_line_or_eval())
1533 {
1534 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1535 size_t len;
1536
1537 if (pat_esc == NULL)
1538 goto theend;
1539 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001540 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001541 if (ptr == NULL)
1542 {
1543 vim_free(pat_esc);
1544 goto theend;
1545 }
1546 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1547 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1548 vim_free(pat_esc);
1549 vim_free(ptr);
1550 }
1551 else
1552 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001553 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001554 if (regmatch.regprog == NULL)
1555 goto theend;
1556 }
1557
1558 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1559 regmatch.rm_ic = ignorecase(pat);
1560 while (*dict != NUL && !got_int && !compl_interrupted)
1561 {
1562 // copy one dictionary file name into buf
1563 if (flags == DICT_EXACT)
1564 {
1565 count = 1;
1566 files = &dict;
1567 }
1568 else
1569 {
1570 // Expand wildcards in the dictionary name, but do not allow
1571 // backticks (for security, the 'dict' option may have been set in
1572 // a modeline).
1573 copy_option_part(&dict, buf, LSIZE, ",");
1574# ifdef FEAT_SPELL
1575 if (!thesaurus && STRCMP(buf, "spell") == 0)
1576 count = -1;
1577 else
1578# endif
1579 if (vim_strchr(buf, '`') != NULL
1580 || expand_wildcards(1, &buf, &count, &files,
1581 EW_FILE|EW_SILENT) != OK)
1582 count = 0;
1583 }
1584
1585# ifdef FEAT_SPELL
1586 if (count == -1)
1587 {
1588 // Complete from active spelling. Skip "\<" in the pattern, we
1589 // don't use it as a RE.
1590 if (pat[0] == '\\' && pat[1] == '<')
1591 ptr = pat + 2;
1592 else
1593 ptr = pat;
1594 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1595 }
1596 else
1597# endif
1598 if (count > 0) // avoid warning for using "files" uninit
1599 {
1600 ins_compl_files(count, files, thesaurus, flags,
1601 &regmatch, buf, &dir);
1602 if (flags != DICT_EXACT)
1603 FreeWild(count, files);
1604 }
1605 if (flags != 0)
1606 break;
1607 }
1608
1609theend:
1610 p_scs = save_p_scs;
1611 vim_regfree(regmatch.regprog);
1612 vim_free(buf);
1613}
1614
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001615/*
1616 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1617 * skipping the word at 'skip_word'. Returns OK on success.
1618 */
1619 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001620thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001621 char_u *fname,
1622 char_u **buf_arg,
1623 int dir,
1624 char_u *skip_word)
1625{
1626 int status = OK;
1627 char_u *ptr;
1628 char_u *wstart;
1629
1630 // Add the other matches on the line
1631 ptr = *buf_arg;
1632 while (!got_int)
1633 {
1634 // Find start of the next word. Skip white
1635 // space and punctuation.
1636 ptr = find_word_start(ptr);
1637 if (*ptr == NUL || *ptr == NL)
1638 break;
1639 wstart = ptr;
1640
1641 // Find end of the word.
1642 if (has_mbyte)
1643 // Japanese words may have characters in
1644 // different classes, only separate words
1645 // with single-byte non-word characters.
1646 while (*ptr != NUL)
1647 {
1648 int l = (*mb_ptr2len)(ptr);
1649
1650 if (l < 2 && !vim_iswordc(*ptr))
1651 break;
1652 ptr += l;
1653 }
1654 else
1655 ptr = find_word_end(ptr);
1656
1657 // Add the word. Skip the regexp match.
1658 if (wstart != skip_word)
1659 {
1660 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1661 fname, dir, FALSE);
1662 if (status == FAIL)
1663 break;
1664 }
1665 }
1666
1667 *buf_arg = ptr;
1668 return status;
1669}
1670
1671/*
1672 * Process "count" dictionary/thesaurus "files" and add the text matching
1673 * "regmatch".
1674 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001675 static void
1676ins_compl_files(
1677 int count,
1678 char_u **files,
1679 int thesaurus,
1680 int flags,
1681 regmatch_T *regmatch,
1682 char_u *buf,
1683 int *dir)
1684{
1685 char_u *ptr;
1686 int i;
1687 FILE *fp;
1688 int add_r;
1689
1690 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1691 {
1692 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001693 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001694 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001695 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001696 vim_snprintf((char *)IObuff, IOSIZE,
1697 _("Scanning dictionary: %s"), (char *)files[i]);
1698 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1699 }
1700
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001701 if (fp == NULL)
1702 continue;
1703
1704 // Read dictionary file line by line.
1705 // Check each line for a match.
1706 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001708 ptr = buf;
1709 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001710 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001711 ptr = regmatch->startp[0];
glepnir6e199932024-12-14 21:13:27 +01001712 ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr)
1713 : find_word_end(ptr);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001714 add_r = ins_compl_add_infercase(regmatch->startp[0],
1715 (int)(ptr - regmatch->startp[0]),
1716 p_ic, files[i], *dir, FALSE);
1717 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001718 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001719 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001720 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001721 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001722 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001723 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001724 if (add_r == OK)
1725 // if dir was BACKWARD then honor it just once
1726 *dir = FORWARD;
1727 else if (add_r == FAIL)
1728 break;
1729 // avoid expensive call to vim_regexec() when at end
1730 // of line
1731 if (*ptr == '\n' || got_int)
1732 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001733 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001734 line_breakcheck();
1735 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001736 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001737 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001738 }
1739}
1740
1741/*
1742 * Find the start of the next word.
1743 * Returns a pointer to the first char of the word. Also stops at a NUL.
1744 */
1745 char_u *
1746find_word_start(char_u *ptr)
1747{
1748 if (has_mbyte)
1749 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1750 ptr += (*mb_ptr2len)(ptr);
1751 else
1752 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1753 ++ptr;
1754 return ptr;
1755}
1756
1757/*
1758 * Find the end of the word. Assumes it starts inside a word.
1759 * Returns a pointer to just after the word.
1760 */
1761 char_u *
1762find_word_end(char_u *ptr)
1763{
1764 int start_class;
1765
1766 if (has_mbyte)
1767 {
1768 start_class = mb_get_class(ptr);
1769 if (start_class > 1)
1770 while (*ptr != NUL)
1771 {
1772 ptr += (*mb_ptr2len)(ptr);
1773 if (mb_get_class(ptr) != start_class)
1774 break;
1775 }
1776 }
1777 else
1778 while (vim_iswordc(*ptr))
1779 ++ptr;
1780 return ptr;
1781}
1782
1783/*
1784 * Find the end of the line, omitting CR and NL at the end.
1785 * Returns a pointer to just after the line.
1786 */
1787 static char_u *
1788find_line_end(char_u *ptr)
1789{
1790 char_u *s;
1791
1792 s = ptr + STRLEN(ptr);
1793 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1794 --s;
1795 return s;
1796}
1797
1798/*
1799 * Free the list of completions
1800 */
1801 static void
1802ins_compl_free(void)
1803{
1804 compl_T *match;
1805 int i;
1806
John Marriott5e6ea922024-11-23 14:01:57 +01001807 VIM_CLEAR_STRING(compl_pattern);
1808 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001809
1810 if (compl_first_match == NULL)
1811 return;
1812
1813 ins_compl_del_pum();
1814 pum_clear();
1815
1816 compl_curr_match = compl_first_match;
1817 do
1818 {
1819 match = compl_curr_match;
1820 compl_curr_match = compl_curr_match->cp_next;
John Marriott5e6ea922024-11-23 14:01:57 +01001821 VIM_CLEAR_STRING(match->cp_str);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001822 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001823 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001824 vim_free(match->cp_fname);
1825 for (i = 0; i < CPT_COUNT; ++i)
1826 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001827#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001828 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001829#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001830 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001831 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001832 compl_first_match = compl_curr_match = NULL;
1833 compl_shown_match = NULL;
1834 compl_old_match = NULL;
1835}
1836
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001837/*
1838 * Reset/clear the completion state.
1839 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001840 void
1841ins_compl_clear(void)
1842{
1843 compl_cont_status = 0;
1844 compl_started = FALSE;
1845 compl_matches = 0;
glepnir6a38aff2024-12-16 21:56:16 +01001846 compl_ins_end_col = 0;
John Marriott5e6ea922024-11-23 14:01:57 +01001847 VIM_CLEAR_STRING(compl_pattern);
1848 VIM_CLEAR_STRING(compl_leader);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001849 edit_submode_extra = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01001850 VIM_CLEAR_STRING(compl_orig_text);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001851 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001852#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001853 // clear v:completed_item
1854 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856}
1857
1858/*
1859 * Return TRUE when Insert completion is active.
1860 */
1861 int
1862ins_compl_active(void)
1863{
1864 return compl_started;
1865}
1866
1867/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001868 * Selected one of the matches. When FALSE the match was edited or using the
1869 * longest common string.
1870 */
1871 int
1872ins_compl_used_match(void)
1873{
1874 return compl_used_match;
1875}
1876
1877/*
1878 * Initialize get longest common string.
1879 */
1880 void
1881ins_compl_init_get_longest(void)
1882{
1883 compl_get_longest = FALSE;
1884}
1885
1886/*
1887 * Returns TRUE when insert completion is interrupted.
1888 */
1889 int
1890ins_compl_interrupted(void)
1891{
1892 return compl_interrupted;
1893}
1894
1895/*
1896 * Returns TRUE if the <Enter> key selects a match in the completion popup
1897 * menu.
1898 */
1899 int
1900ins_compl_enter_selects(void)
1901{
1902 return compl_enter_selects;
1903}
1904
1905/*
1906 * Return the column where the text starts that is being completed
1907 */
1908 colnr_T
1909ins_compl_col(void)
1910{
1911 return compl_col;
1912}
1913
1914/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001915 * Return the length in bytes of the text being completed
1916 */
1917 int
1918ins_compl_len(void)
1919{
1920 return compl_length;
1921}
1922
1923/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001924 * Delete one character before the cursor and show the subset of the matches
1925 * that match the word that is now before the cursor.
1926 * Returns the character to be used, NUL if the work is done and another char
1927 * to be got from the user.
1928 */
1929 int
1930ins_compl_bs(void)
1931{
1932 char_u *line;
1933 char_u *p;
1934
1935 line = ml_get_curline();
1936 p = line + curwin->w_cursor.col;
1937 MB_PTR_BACK(line, p);
1938
1939 // Stop completion when the whole word was deleted. For Omni completion
1940 // allow the word to be deleted, we won't match everything.
1941 // Respect the 'backspace' option.
1942 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001943 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1944 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001945 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1946 - compl_length < 0))
1947 return K_BS;
1948
1949 // Deleted more than what was used to find matches or didn't finish
1950 // finding all matches: need to look for matches all over again.
1951 if (curwin->w_cursor.col <= compl_col + compl_length
1952 || ins_compl_need_restart())
1953 ins_compl_restart();
1954
John Marriott5e6ea922024-11-23 14:01:57 +01001955 VIM_CLEAR_STRING(compl_leader);
1956 compl_leader.length = (size_t)((p - line) - compl_col);
1957 compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length);
1958 if (compl_leader.string == NULL)
1959 {
1960 compl_leader.length = 0;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001961 return K_BS;
John Marriott5e6ea922024-11-23 14:01:57 +01001962 }
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001963
1964 ins_compl_new_leader();
1965 if (compl_shown_match != NULL)
1966 // Make sure current match is not a hidden item.
1967 compl_curr_match = compl_shown_match;
1968 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969}
1970
1971/*
1972 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1973 * be called.
1974 */
1975 static int
1976ins_compl_need_restart(void)
1977{
1978 // Return TRUE if we didn't complete finding matches or when the
1979 // 'completefunc' returned "always" in the "refresh" dictionary item.
1980 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001981 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001982 && compl_opt_refresh_always);
1983}
1984
1985/*
1986 * Called after changing "compl_leader".
1987 * Show the popup menu with a different set of matches.
1988 * May also search for matches again if the previous search was interrupted.
1989 */
1990 static void
1991ins_compl_new_leader(void)
1992{
1993 ins_compl_del_pum();
1994 ins_compl_delete();
glepnir6a38aff2024-12-16 21:56:16 +01001995 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 compl_used_match = FALSE;
1997
1998 if (compl_started)
John Marriott5e6ea922024-11-23 14:01:57 +01001999 ins_compl_set_original_text(compl_leader.string, compl_leader.length);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002000 else
2001 {
2002#ifdef FEAT_SPELL
2003 spell_bad_len = 0; // need to redetect bad word
2004#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002005 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002006 // the popup menu display the changed text before the cursor. Set
2007 // "compl_restarting" to avoid that the first match is inserted.
2008 pum_call_update_screen();
2009#ifdef FEAT_GUI
2010 if (gui.in_use)
2011 {
2012 // Show the cursor after the match, not after the redrawn text.
2013 setcursor();
2014 out_flush_cursor(FALSE, FALSE);
2015 }
2016#endif
2017 compl_restarting = TRUE;
2018 if (ins_complete(Ctrl_N, TRUE) == FAIL)
2019 compl_cont_status = 0;
2020 compl_restarting = FALSE;
2021 }
2022
2023 compl_enter_selects = !compl_used_match;
2024
2025 // Show the popup menu with a different set of matches.
2026 ins_compl_show_pum();
2027
2028 // Don't let Enter select the original text when there is no popup menu.
2029 if (compl_match_array == NULL)
2030 compl_enter_selects = FALSE;
2031}
2032
2033/*
2034 * Return the length of the completion, from the completion start column to
2035 * the cursor column. Making sure it never goes below zero.
2036 */
2037 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002038get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002039{
2040 int off = (int)curwin->w_cursor.col - (int)compl_col;
2041
2042 if (off < 0)
2043 return 0;
2044 return off;
2045}
2046
2047/*
2048 * Append one character to the match leader. May reduce the number of
2049 * matches.
2050 */
2051 void
2052ins_compl_addleader(int c)
2053{
2054 int cc;
2055
2056 if (stop_arrow() == FAIL)
2057 return;
2058 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2059 {
2060 char_u buf[MB_MAXBYTES + 1];
2061
2062 (*mb_char2bytes)(c, buf);
2063 buf[cc] = NUL;
2064 ins_char_bytes(buf, cc);
2065 if (compl_opt_refresh_always)
2066 AppendToRedobuff(buf);
2067 }
2068 else
2069 {
2070 ins_char(c);
2071 if (compl_opt_refresh_always)
2072 AppendCharToRedobuff(c);
2073 }
2074
2075 // If we didn't complete finding matches we must search again.
2076 if (ins_compl_need_restart())
2077 ins_compl_restart();
2078
2079 // When 'always' is set, don't reset compl_leader. While completing,
2080 // cursor doesn't point original position, changing compl_leader would
2081 // break redo.
2082 if (!compl_opt_refresh_always)
2083 {
John Marriott5e6ea922024-11-23 14:01:57 +01002084 VIM_CLEAR_STRING(compl_leader);
2085 compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col);
2086 compl_leader.string = vim_strnsave(ml_get_curline() + compl_col,
2087 compl_leader.length);
2088 if (compl_leader.string == NULL)
2089 {
2090 compl_leader.length = 0;
2091 return;
2092 }
2093
2094 ins_compl_new_leader();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002095 }
2096}
2097
2098/*
2099 * Setup for finding completions again without leaving CTRL-X mode. Used when
2100 * BS or a key was typed while still searching for matches.
2101 */
2102 static void
2103ins_compl_restart(void)
2104{
2105 ins_compl_free();
2106 compl_started = FALSE;
2107 compl_matches = 0;
2108 compl_cont_status = 0;
2109 compl_cont_mode = 0;
2110}
2111
2112/*
2113 * Set the first match, the original text.
2114 */
2115 static void
John Marriott5e6ea922024-11-23 14:01:57 +01002116ins_compl_set_original_text(char_u *str, size_t len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002117{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002118 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002119 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2120 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002121 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002122 {
John Marriott5e6ea922024-11-23 14:01:57 +01002123 char_u *p = vim_strnsave(str, len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002124 if (p != NULL)
2125 {
John Marriott5e6ea922024-11-23 14:01:57 +01002126 VIM_CLEAR_STRING(compl_first_match->cp_str);
2127 compl_first_match->cp_str.string = p;
2128 compl_first_match->cp_str.length = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002129 }
2130 }
2131 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002132 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002133 {
John Marriott5e6ea922024-11-23 14:01:57 +01002134 char_u *p = vim_strnsave(str, len);
2135 if (p != NULL)
2136 {
2137 VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str);
2138 compl_first_match->cp_prev->cp_str.string = p;
2139 compl_first_match->cp_prev->cp_str.length = len;
2140 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 }
2142}
2143
2144/*
2145 * Append one character to the match leader. May reduce the number of
2146 * matches.
2147 */
2148 void
2149ins_compl_addfrommatch(void)
2150{
2151 char_u *p;
2152 int len = (int)curwin->w_cursor.col - (int)compl_col;
2153 int c;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002154
John Marriott5e6ea922024-11-23 14:01:57 +01002155 p = compl_shown_match->cp_str.string;
2156 if ((int)compl_shown_match->cp_str.length <= len) // the match is too short
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002157 {
John Marriott5e6ea922024-11-23 14:01:57 +01002158 size_t plen;
2159 compl_T *cp;
2160
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002161 // When still at the original match use the first entry that matches
2162 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002163 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002164 return;
2165
2166 p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002167 plen = 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002168 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002169 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002170 {
John Marriott5e6ea922024-11-23 14:01:57 +01002171 if (compl_leader.string == NULL
2172 || ins_compl_equal(cp, compl_leader.string,
2173 (int)compl_leader.length))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002174 {
John Marriott5e6ea922024-11-23 14:01:57 +01002175 p = cp->cp_str.string;
2176 plen = cp->cp_str.length;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002177 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002178 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002179 }
John Marriott5e6ea922024-11-23 14:01:57 +01002180 if (p == NULL || (int)plen <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002181 return;
2182 }
2183 p += len;
2184 c = PTR2CHAR(p);
2185 ins_compl_addleader(c);
2186}
2187
2188/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002189 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002190 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2191 * compl_cont_mode and compl_cont_status.
2192 * Returns TRUE when the character is not to be inserted.
2193 */
2194 static int
2195set_ctrl_x_mode(int c)
2196{
2197 int retval = FALSE;
2198
2199 switch (c)
2200 {
2201 case Ctrl_E:
2202 case Ctrl_Y:
2203 // scroll the window one line up or down
2204 ctrl_x_mode = CTRL_X_SCROLL;
2205 if (!(State & REPLACE_FLAG))
2206 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2207 else
2208 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2209 edit_submode_pre = NULL;
2210 showmode();
2211 break;
2212 case Ctrl_L:
2213 // complete whole line
2214 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2215 break;
2216 case Ctrl_F:
2217 // complete filenames
2218 ctrl_x_mode = CTRL_X_FILES;
2219 break;
2220 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002221 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002222 ctrl_x_mode = CTRL_X_DICTIONARY;
2223 break;
2224 case Ctrl_R:
2225 // Register insertion without exiting CTRL-X mode
2226 // Simply allow ^R to happen without affecting ^X mode
2227 break;
2228 case Ctrl_T:
2229 // complete words from a thesaurus
2230 ctrl_x_mode = CTRL_X_THESAURUS;
2231 break;
2232#ifdef FEAT_COMPL_FUNC
2233 case Ctrl_U:
2234 // user defined completion
2235 ctrl_x_mode = CTRL_X_FUNCTION;
2236 break;
2237 case Ctrl_O:
2238 // omni completion
2239 ctrl_x_mode = CTRL_X_OMNI;
2240 break;
2241#endif
2242 case 's':
2243 case Ctrl_S:
2244 // complete spelling suggestions
2245 ctrl_x_mode = CTRL_X_SPELL;
2246#ifdef FEAT_SPELL
2247 ++emsg_off; // Avoid getting the E756 error twice.
2248 spell_back_to_badword();
2249 --emsg_off;
2250#endif
2251 break;
2252 case Ctrl_RSB:
2253 // complete tag names
2254 ctrl_x_mode = CTRL_X_TAGS;
2255 break;
2256#ifdef FEAT_FIND_ID
2257 case Ctrl_I:
2258 case K_S_TAB:
2259 // complete keywords from included files
2260 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2261 break;
2262 case Ctrl_D:
2263 // complete definitions from included files
2264 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2265 break;
2266#endif
2267 case Ctrl_V:
2268 case Ctrl_Q:
2269 // complete vim commands
2270 ctrl_x_mode = CTRL_X_CMDLINE;
2271 break;
2272 case Ctrl_Z:
2273 // stop completion
2274 ctrl_x_mode = CTRL_X_NORMAL;
2275 edit_submode = NULL;
2276 showmode();
2277 retval = TRUE;
2278 break;
2279 case Ctrl_P:
2280 case Ctrl_N:
2281 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2282 // just started ^X mode, or there were enough ^X's to cancel
2283 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2284 // do normal expansion when interrupting a different mode (say
2285 // ^X^F^X^P or ^P^X^X^P, see below)
2286 // nothing changes if interrupting mode 0, (eg, the flag
2287 // doesn't change when going to ADDING mode -- Acevedo
2288 if (!(compl_cont_status & CONT_INTRPT))
2289 compl_cont_status |= CONT_LOCAL;
2290 else if (compl_cont_mode != 0)
2291 compl_cont_status &= ~CONT_LOCAL;
2292 // FALLTHROUGH
2293 default:
2294 // If we have typed at least 2 ^X's... for modes != 0, we set
2295 // compl_cont_status = 0 (eg, as if we had just started ^X
2296 // mode).
2297 // For mode 0, we set "compl_cont_mode" to an impossible
2298 // value, in both cases ^X^X can be used to restart the same
2299 // mode (avoiding ADDING mode).
2300 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2301 // 'complete' and local ^P expansions respectively.
2302 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2303 // mode -- Acevedo
2304 if (c == Ctrl_X)
2305 {
2306 if (compl_cont_mode != 0)
2307 compl_cont_status = 0;
2308 else
2309 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2310 }
2311 ctrl_x_mode = CTRL_X_NORMAL;
2312 edit_submode = NULL;
2313 showmode();
2314 break;
2315 }
2316
2317 return retval;
2318}
2319
2320/*
glepnir1c5a1202024-12-04 20:27:34 +01002321 * Trigger CompleteDone event and adds relevant information to v:event
2322 */
2323 static void
2324trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED)
2325{
2326#if defined(FEAT_EVAL)
2327 save_v_event_T save_v_event;
2328 dict_T *v_event = get_v_event(&save_v_event);
2329 char_u *mode_str = NULL;
2330
2331 mode = mode & ~CTRL_X_WANT_IDENT;
2332 if (ctrl_x_mode_names[mode])
2333 mode_str = (char_u *)ctrl_x_mode_names[mode];
2334
2335 (void)dict_add_string(v_event, "complete_word",
2336 word == NULL ? (char_u *)"" : word);
2337 (void)dict_add_string(v_event, "complete_type",
2338 mode_str != NULL ? mode_str : (char_u *)"");
2339
2340 dict_set_items_ro(v_event);
2341#endif
2342 ins_apply_autocmds(EVENT_COMPLETEDONE);
2343
2344#if defined(FEAT_EVAL)
2345 restore_v_event(v_event, &save_v_event);
2346#endif
2347}
2348
2349/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002350 * Stop insert completion mode
2351 */
2352 static int
2353ins_compl_stop(int c, int prev_mode, int retval)
2354{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002355 int want_cindent;
glepnir1c5a1202024-12-04 20:27:34 +01002356 char_u *word = NULL;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002357
2358 // Get here when we have finished typing a sequence of ^N and
2359 // ^P or other completion characters in CTRL-X mode. Free up
2360 // memory that was used, and make sure we can redo the insert.
John Marriott5e6ea922024-11-23 14:01:57 +01002361 if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002362 {
John Marriott5e6ea922024-11-23 14:01:57 +01002363 char_u *ptr;
2364
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002365 // If any of the original typed text has been changed, eg when
2366 // ignorecase is set, we must add back-spaces to the redo
2367 // buffer. We add as few as necessary to delete just the part
2368 // of the original text that has changed.
2369 // When using the longest match, edited the match or used
2370 // CTRL-E then don't use the current match.
2371 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
John Marriott5e6ea922024-11-23 14:01:57 +01002372 ptr = compl_curr_match->cp_str.string;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002373 else
2374 ptr = NULL;
2375 ins_compl_fixRedoBufForLeader(ptr);
2376 }
2377
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002378 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002379
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002380 // When completing whole lines: fix indent for 'cindent'.
2381 // Otherwise, break line if it's too long.
2382 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2383 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002384 // re-indent the current line
2385 if (want_cindent)
2386 {
2387 do_c_expr_indent();
2388 want_cindent = FALSE; // don't do it again
2389 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002390 }
2391 else
2392 {
2393 int prev_col = curwin->w_cursor.col;
2394
2395 // put the cursor on the last char, for 'tw' formatting
2396 if (prev_col > 0)
2397 dec_cursor();
2398 // only format when something was inserted
2399 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2400 insertchar(NUL, 0, -1);
2401 if (prev_col > 0
2402 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2403 inc_cursor();
2404 }
2405
2406 // If the popup menu is displayed pressing CTRL-Y means accepting
2407 // the selection without inserting anything. When
2408 // compl_enter_selects is set the Enter key does the same.
2409 if ((c == Ctrl_Y || (compl_enter_selects
2410 && (c == CAR || c == K_KENTER || c == NL)))
2411 && pum_visible())
glepnir1c5a1202024-12-04 20:27:34 +01002412 {
2413 word = vim_strsave(compl_shown_match->cp_str.string);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002414 retval = TRUE;
glepnir1c5a1202024-12-04 20:27:34 +01002415 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002416
2417 // CTRL-E means completion is Ended, go back to the typed text.
2418 // but only do this, if the Popup is still visible
2419 if (c == Ctrl_E)
2420 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002421 char_u *p = NULL;
John Marriott5e6ea922024-11-23 14:01:57 +01002422 size_t plen = 0;
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002423
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002424 ins_compl_delete();
John Marriott5e6ea922024-11-23 14:01:57 +01002425 if (compl_leader.string != NULL)
2426 {
2427 p = compl_leader.string;
2428 plen = compl_leader.length;
2429 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002430 else if (compl_first_match != NULL)
John Marriott5e6ea922024-11-23 14:01:57 +01002431 {
2432 p = compl_orig_text.string;
2433 plen = compl_orig_text.length;
2434 }
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002435 if (p != NULL)
2436 {
2437 int compl_len = get_compl_len();
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002438
John Marriott5e6ea922024-11-23 14:01:57 +01002439 if ((int)plen > compl_len)
glepnir6a38aff2024-12-16 21:56:16 +01002440 ins_compl_insert_bytes(p + compl_len, (int)(plen - compl_len));
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002441 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002442 retval = TRUE;
2443 }
2444
2445 auto_format(FALSE, TRUE);
2446
2447 // Trigger the CompleteDonePre event to give scripts a chance to
2448 // act upon the completion before clearing the info, and restore
2449 // ctrl_x_mode, so that complete_info() can be used.
2450 ctrl_x_mode = prev_mode;
2451 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2452
2453 ins_compl_free();
2454 compl_started = FALSE;
2455 compl_matches = 0;
2456 if (!shortmess(SHM_COMPLETIONMENU))
2457 msg_clr_cmdline(); // necessary for "noshowmode"
2458 ctrl_x_mode = CTRL_X_NORMAL;
2459 compl_enter_selects = FALSE;
2460 if (edit_submode != NULL)
2461 {
2462 edit_submode = NULL;
2463 showmode();
2464 }
2465
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002466 if (c == Ctrl_C && cmdwin_type != 0)
2467 // Avoid the popup menu remains displayed when leaving the
2468 // command line window.
2469 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002470 // Indent now if a key was typed that is in 'cinkeys'.
2471 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2472 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002473 // Trigger the CompleteDone event to give scripts a chance to act
2474 // upon the end of completion.
glepnir1c5a1202024-12-04 20:27:34 +01002475 trigger_complete_done_event(prev_mode, word);
2476 vim_free(word);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002477
2478 return retval;
2479}
2480
2481/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002482 * Prepare for Insert mode completion, or stop it.
2483 * Called just after typing a character in Insert mode.
2484 * Returns TRUE when the character is not to be inserted;
2485 */
2486 int
2487ins_compl_prep(int c)
2488{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002489 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002490 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002491
2492 // Forget any previous 'special' messages if this is actually
2493 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2494 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2495 edit_submode_extra = NULL;
2496
zeertzjq440d4cb2023-03-02 17:51:32 +00002497 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002498 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002499 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002500 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002501 return retval;
2502
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002503#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002504 // Ignore mouse events in a popup window
2505 if (is_mouse_key(c))
2506 {
2507 // Ignore drag and release events, the position does not need to be in
2508 // the popup and it may have just closed.
2509 if (c == K_LEFTRELEASE
2510 || c == K_LEFTRELEASE_NM
2511 || c == K_MIDDLERELEASE
2512 || c == K_RIGHTRELEASE
2513 || c == K_X1RELEASE
2514 || c == K_X2RELEASE
2515 || c == K_LEFTDRAG
2516 || c == K_MIDDLEDRAG
2517 || c == K_RIGHTDRAG
2518 || c == K_X1DRAG
2519 || c == K_X2DRAG)
2520 return retval;
2521 if (popup_visible)
2522 {
2523 int row = mouse_row;
2524 int col = mouse_col;
2525 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2526
2527 if (wp != NULL && WIN_IS_POPUP(wp))
2528 return retval;
2529 }
2530 }
2531#endif
2532
zeertzjqdca29d92021-08-31 19:12:51 +02002533 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2534 {
2535 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2536 || !vim_is_ctrl_x_key(c))
2537 {
2538 // Not starting another completion mode.
2539 ctrl_x_mode = CTRL_X_CMDLINE;
2540
2541 // CTRL-X CTRL-Z should stop completion without inserting anything
2542 if (c == Ctrl_Z)
2543 retval = TRUE;
2544 }
2545 else
2546 {
2547 ctrl_x_mode = CTRL_X_CMDLINE;
2548
2549 // Other CTRL-X keys first stop completion, then start another
2550 // completion mode.
2551 ins_compl_prep(' ');
2552 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2553 }
2554 }
2555
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002556 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002557 if (ctrl_x_mode_not_defined_yet()
2558 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002559 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002560 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002561 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002562 }
2563
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002564 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002565 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2566 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002567 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002568 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002569 {
2570 // We're already in CTRL-X mode, do we stay in it?
2571 if (!vim_is_ctrl_x_key(c))
2572 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002573 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002574 ctrl_x_mode = CTRL_X_NORMAL;
2575 else
2576 ctrl_x_mode = CTRL_X_FINISHED;
2577 edit_submode = NULL;
2578 }
2579 showmode();
2580 }
2581
2582 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2583 {
2584 // Show error message from attempted keyword completion (probably
2585 // 'Pattern not found') until another key is hit, then go back to
2586 // showing what mode we are in.
2587 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002588 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002589 && c != Ctrl_R && !ins_compl_pum_key(c))
2590 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002591 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002592 }
2593 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2594 // Trigger the CompleteDone event to give scripts a chance to act
2595 // upon the (possibly failed) completion.
glepnir1c5a1202024-12-04 20:27:34 +01002596 trigger_complete_done_event(ctrl_x_mode, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002597
LemonBoy2bf52dd2022-04-09 18:17:34 +01002598 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002599
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002600 // reset continue_* if we left expansion-mode, if we stay they'll be
2601 // (re)set properly in ins_complete()
2602 if (!vim_is_ctrl_x_key(c))
2603 {
2604 compl_cont_status = 0;
2605 compl_cont_mode = 0;
2606 }
2607
2608 return retval;
2609}
2610
2611/*
2612 * Fix the redo buffer for the completion leader replacing some of the typed
2613 * text. This inserts backspaces and appends the changed text.
2614 * "ptr" is the known leader text or NUL.
2615 */
2616 static void
2617ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2618{
John Marriott5e6ea922024-11-23 14:01:57 +01002619 int len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620 char_u *p;
2621 char_u *ptr = ptr_arg;
2622
2623 if (ptr == NULL)
2624 {
John Marriott5e6ea922024-11-23 14:01:57 +01002625 if (compl_leader.string != NULL)
2626 ptr = compl_leader.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002627 else
2628 return; // nothing to do
2629 }
John Marriott5e6ea922024-11-23 14:01:57 +01002630 if (compl_orig_text.string != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002631 {
John Marriott5e6ea922024-11-23 14:01:57 +01002632 p = compl_orig_text.string;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002633 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2634 ;
2635 if (len > 0)
2636 len -= (*mb_head_off)(p, p + len);
2637 for (p += len; *p != NUL; MB_PTR_ADV(p))
2638 AppendCharToRedobuff(K_BS);
2639 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002640 if (ptr != NULL)
2641 AppendToRedobuffLit(ptr + len, -1);
2642}
2643
2644/*
2645 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2646 * (depending on flag) starting from buf and looking for a non-scanned
2647 * buffer (other than curbuf). curbuf is special, if it is called with
2648 * buf=curbuf then it has to be the first call for a given flag/expansion.
2649 *
2650 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2651 */
2652 static buf_T *
2653ins_compl_next_buf(buf_T *buf, int flag)
2654{
2655 static win_T *wp = NULL;
2656
2657 if (flag == 'w') // just windows
2658 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002659 if (buf == curbuf || !win_valid(wp))
2660 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002661 wp = curwin;
2662 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2663 && wp->w_buffer->b_scanned)
2664 ;
2665 buf = wp->w_buffer;
2666 }
2667 else
2668 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2669 // (unlisted buffers)
2670 // When completing whole lines skip unloaded buffers.
2671 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2672 && ((flag == 'U'
2673 ? buf->b_p_bl
2674 : (!buf->b_p_bl
2675 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2676 || buf->b_scanned))
2677 ;
2678 return buf;
2679}
2680
2681#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002682
2683# ifdef FEAT_EVAL
2684static callback_T cfu_cb; // 'completefunc' callback function
2685static callback_T ofu_cb; // 'omnifunc' callback function
2686static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2687# endif
2688
2689/*
2690 * Copy a global callback function to a buffer local callback.
2691 */
2692 static void
2693copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2694{
2695 free_callback(bufcb);
2696 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2697 copy_callback(bufcb, globcb);
2698}
2699
2700/*
2701 * Parse the 'completefunc' option value and set the callback function.
2702 * Invoked when the 'completefunc' option is set. The option value can be a
2703 * name of a function (string), or function(<name>) or funcref(<name>) or a
2704 * lambda expression.
2705 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002706 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002707did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002708{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002709 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2710 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002711
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002712 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002713
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002714 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002715}
2716
2717/*
2718 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002719 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002720 */
2721 void
2722set_buflocal_cfu_callback(buf_T *buf UNUSED)
2723{
2724# ifdef FEAT_EVAL
2725 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2726# endif
2727}
2728
2729/*
2730 * Parse the 'omnifunc' option value and set the callback function.
2731 * Invoked when the 'omnifunc' option is set. The option value can be a
2732 * name of a function (string), or function(<name>) or funcref(<name>) or a
2733 * lambda expression.
2734 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002736did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002737{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002738 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2739 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002740
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002741 set_buflocal_ofu_callback(curbuf);
2742 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002743}
2744
2745/*
2746 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002747 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002748 */
2749 void
2750set_buflocal_ofu_callback(buf_T *buf UNUSED)
2751{
2752# ifdef FEAT_EVAL
2753 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2754# endif
2755}
2756
2757/*
2758 * Parse the 'thesaurusfunc' option value and set the callback function.
2759 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2760 * name of a function (string), or function(<name>) or funcref(<name>) or a
2761 * lambda expression.
2762 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002763 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002764did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002765{
2766 int retval;
2767
zeertzjq6eda2692024-11-03 09:23:33 +01002768 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002769 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002770 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2771 &curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002772 else
2773 {
2774 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002775 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01002776 // when using :set, free the local callback
2777 if (!(args->os_flags & OPT_GLOBAL))
2778 free_callback(&curbuf->b_tsrfu_cb);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002779 }
2780
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002781 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002782}
2783
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002784/*
2785 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002786 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002787 */
2788 int
2789set_ref_in_insexpand_funcs(int copyID)
2790{
2791 int abort = FALSE;
2792
2793 abort = set_ref_in_callback(&cfu_cb, copyID);
2794 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2795 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2796
2797 return abort;
2798}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002799
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002800/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002801 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002802 */
2803 static char_u *
2804get_complete_funcname(int type)
2805{
2806 switch (type)
2807 {
2808 case CTRL_X_FUNCTION:
2809 return curbuf->b_p_cfu;
2810 case CTRL_X_OMNI:
2811 return curbuf->b_p_ofu;
2812 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002813 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002814 default:
2815 return (char_u *)"";
2816 }
2817}
2818
2819/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002820 * Get the callback to use for insert mode completion.
2821 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002822 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002823get_insert_callback(int type)
2824{
2825 if (type == CTRL_X_FUNCTION)
2826 return &curbuf->b_cfu_cb;
2827 if (type == CTRL_X_OMNI)
2828 return &curbuf->b_ofu_cb;
2829 // CTRL_X_THESAURUS
2830 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2831}
2832
2833/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002834 * Execute user defined complete function 'completefunc', 'omnifunc' or
2835 * 'thesaurusfunc', and get matches in "matches".
2836 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002837 */
2838 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002839expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002840{
2841 list_T *matchlist = NULL;
2842 dict_T *matchdict = NULL;
2843 typval_T args[3];
2844 char_u *funcname;
2845 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002846 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002847 typval_T rettv;
2848 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002849 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002850
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002851 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002852 if (*funcname == NUL)
2853 return;
2854
2855 // Call 'completefunc' to obtain the list of matches.
2856 args[0].v_type = VAR_NUMBER;
2857 args[0].vval.v_number = 0;
2858 args[1].v_type = VAR_STRING;
2859 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2860 args[2].v_type = VAR_UNKNOWN;
2861
2862 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002863 // Lock the text to avoid weird things from happening. Also disallow
2864 // switching to another window, it should not be needed and may end up in
2865 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002866 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002867
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002868 cb = get_insert_callback(type);
2869 retval = call_callback(cb, 0, &rettv, 2, args);
2870
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002871 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002872 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002873 {
2874 switch (rettv.v_type)
2875 {
2876 case VAR_LIST:
2877 matchlist = rettv.vval.v_list;
2878 break;
2879 case VAR_DICT:
2880 matchdict = rettv.vval.v_dict;
2881 break;
2882 case VAR_SPECIAL:
2883 if (rettv.vval.v_number == VVAL_NONE)
2884 compl_opt_suppress_empty = TRUE;
2885 // FALLTHROUGH
2886 default:
2887 // TODO: Give error message?
2888 clear_tv(&rettv);
2889 break;
2890 }
2891 }
zeertzjqcfe45652022-05-27 17:26:55 +01002892 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002894 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002895 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002896 validate_cursor();
2897 if (!EQUAL_POS(curwin->w_cursor, pos))
2898 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002899 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002900 goto theend;
2901 }
2902
2903 if (matchlist != NULL)
2904 ins_compl_add_list(matchlist);
2905 else if (matchdict != NULL)
2906 ins_compl_add_dict(matchdict);
2907
2908theend:
2909 // Restore State, it might have been changed.
2910 State = save_State;
2911
2912 if (matchdict != NULL)
2913 dict_unref(matchdict);
2914 if (matchlist != NULL)
2915 list_unref(matchlist);
2916}
2917#endif // FEAT_COMPL_FUNC
2918
2919#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
glepnir38f99a12024-08-23 18:31:06 +02002920
2921 static inline int
2922get_user_highlight_attr(char_u *hlname)
2923{
2924 if (hlname != NULL && *hlname != NUL)
2925 return syn_name2attr(hlname);
2926 return -1;
2927}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002928/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002929 * Add a match to the list of matches from a typeval_T.
2930 * If the given string is already in the list of completions, then return
2931 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2932 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002933 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002934 */
2935 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002936ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002937{
2938 char_u *word;
2939 int dup = FALSE;
2940 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002941 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002942 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002943 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002944 int status;
glepnir0fe17f82024-10-08 22:26:44 +02002945 char_u *user_abbr_hlname;
glepnir38f99a12024-08-23 18:31:06 +02002946 char_u *user_kind_hlname;
glepnir80b66202024-11-27 21:53:53 +01002947 int user_hl[2] = { -1, -1 };
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002948
Bram Moolenaar08928322020-01-04 14:32:48 +01002949 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002950 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2951 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002952 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2953 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2954 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2955 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2956 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
glepnir38f99a12024-08-23 18:31:06 +02002957
glepnir0fe17f82024-10-08 22:26:44 +02002958 user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002959 user_hl[0] = get_user_highlight_attr(user_abbr_hlname);
glepnir38f99a12024-08-23 18:31:06 +02002960
2961 user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
glepnir80b66202024-11-27 21:53:53 +01002962 user_hl[1] = get_user_highlight_attr(user_kind_hlname);
glepnir508e7852024-07-25 21:39:08 +02002963
Bram Moolenaard61efa52022-07-23 09:52:04 +01002964 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2965 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2966 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002967 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002968 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2969 dup = dict_get_number(tv->vval.v_dict, "dup");
2970 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2971 empty = dict_get_number(tv->vval.v_dict, "empty");
2972 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2973 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002974 flags |= CP_EQUAL;
2975 }
2976 else
2977 {
2978 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002979 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002980 }
2981 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002982 {
2983 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002984 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002985 }
glepnir508e7852024-07-25 21:39:08 +02002986 status = ins_compl_add(word, -1, NULL, cptext,
glepnir80b66202024-11-27 21:53:53 +01002987 &user_data, dir, flags, dup, user_hl);
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002988 if (status != OK)
2989 clear_tv(&user_data);
2990 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002991}
2992
2993/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002994 * Add completions from a list.
2995 */
2996 static void
2997ins_compl_add_list(list_T *list)
2998{
2999 listitem_T *li;
3000 int dir = compl_direction;
3001
3002 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003003 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003004 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003005 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02003006 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003007 // if dir was BACKWARD then honor it just once
3008 dir = FORWARD;
3009 else if (did_emsg)
3010 break;
3011 }
3012}
3013
3014/*
3015 * Add completions from a dict.
3016 */
3017 static void
3018ins_compl_add_dict(dict_T *dict)
3019{
3020 dictitem_T *di_refresh;
3021 dictitem_T *di_words;
3022
3023 // Check for optional "refresh" item.
3024 compl_opt_refresh_always = FALSE;
3025 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
3026 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
3027 {
3028 char_u *v = di_refresh->di_tv.vval.v_string;
3029
3030 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3031 compl_opt_refresh_always = TRUE;
3032 }
3033
3034 // Add completions from a "words" list.
3035 di_words = dict_find(dict, (char_u *)"words", 5);
3036 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
3037 ins_compl_add_list(di_words->di_tv.vval.v_list);
3038}
3039
3040/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003041 * Start completion for the complete() function.
3042 * "startcol" is where the matched text starts (1 is first column).
3043 * "list" is the list of matches.
3044 */
3045 static void
3046set_completion(colnr_T startcol, list_T *list)
3047{
3048 int save_w_wrow = curwin->w_wrow;
3049 int save_w_leftcol = curwin->w_leftcol;
3050 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02003051 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02003052 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
3053 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
3054 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003055
3056 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003057 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003058 ins_compl_prep(' ');
3059 ins_compl_clear();
3060 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01003061 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003062
3063 compl_direction = FORWARD;
3064 if (startcol > curwin->w_cursor.col)
3065 startcol = curwin->w_cursor.col;
3066 compl_col = startcol;
3067 compl_length = (int)curwin->w_cursor.col - (int)startcol;
3068 // compl_pattern doesn't need to be set
John Marriott5e6ea922024-11-23 14:01:57 +01003069 compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003070 if (p_ic)
3071 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01003072 if (compl_orig_text.string == NULL)
3073 {
3074 compl_orig_text.length = 0;
3075 return;
3076 }
3077 compl_orig_text.length = (size_t)compl_length;
3078 if (ins_compl_add(compl_orig_text.string,
3079 (int)compl_orig_text.length, NULL, NULL, NULL, 0,
3080 flags | CP_FAST, FALSE, NULL) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081 return;
3082
3083 ctrl_x_mode = CTRL_X_EVAL;
3084
3085 ins_compl_add_list(list);
3086 compl_matches = ins_compl_make_cyclic();
3087 compl_started = TRUE;
3088 compl_used_match = TRUE;
3089 compl_cont_status = 0;
3090
3091 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01003092 int no_select = compl_no_select || compl_longest;
3093 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003094 {
3095 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01003096 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003097 // Down/Up has no real effect.
3098 ins_complete(K_UP, FALSE);
3099 }
3100 else
3101 ins_complete(Ctrl_N, FALSE);
3102 compl_enter_selects = compl_no_insert;
3103
3104 // Lazily show the popup menu, unless we got interrupted.
3105 if (!compl_interrupted)
3106 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003107 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003108 out_flush();
3109}
3110
3111/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003112 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003113 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003114 void
3115f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003116{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003117 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003118
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003119 if (in_vim9script()
3120 && (check_for_number_arg(argvars, 0) == FAIL
3121 || check_for_list_arg(argvars, 1) == FAIL))
3122 return;
3123
Bram Moolenaar24959102022-05-07 20:01:16 +01003124 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003125 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003126 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003127 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003128 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003129
3130 // Check for undo allowed here, because if something was already inserted
3131 // the line was already saved for undo and this check isn't done.
3132 if (!undo_allowed())
3133 return;
3134
Bram Moolenaard83392a2022-09-01 12:22:46 +01003135 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003136 {
3137 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3138 if (startcol > 0)
3139 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003140 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003141}
3142
3143/*
3144 * "complete_add()" function
3145 */
3146 void
3147f_complete_add(typval_T *argvars, typval_T *rettv)
3148{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003149 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003150 return;
3151
Bram Moolenaar440cf092021-04-03 20:13:30 +02003152 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003153}
3154
3155/*
3156 * "complete_check()" function
3157 */
3158 void
3159f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3160{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003161 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003162 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003163
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003164 ins_compl_check_keys(0, TRUE);
3165 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003166
3167 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003168}
3169
3170/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003171 * Return Insert completion mode name string
3172 */
3173 static char_u *
3174ins_compl_mode(void)
3175{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003176 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003177 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3178
3179 return (char_u *)"";
3180}
3181
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003182/*
3183 * Assign the sequence number to all the completion matches which don't have
3184 * one assigned yet.
3185 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003186 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003187ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003188{
3189 int number = 0;
3190 compl_T *match;
3191
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003192 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003193 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003194 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003195 // This should normally succeed already at the first loop
3196 // cycle, so it's fast!
3197 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003198 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003199 if (match->cp_number != -1)
3200 {
3201 number = match->cp_number;
3202 break;
3203 }
3204 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003205 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003206 for (match = match->cp_next;
3207 match != NULL && match->cp_number == -1;
3208 match = match->cp_next)
3209 match->cp_number = ++number;
3210 }
3211 else // BACKWARD
3212 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003213 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003214 // number. This should normally succeed already at the
3215 // first loop cycle, so it's fast!
3216 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003217 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003218 if (match->cp_number != -1)
3219 {
3220 number = match->cp_number;
3221 break;
3222 }
3223 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003224 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003225 for (match = match->cp_prev; match
3226 && match->cp_number == -1;
3227 match = match->cp_prev)
3228 match->cp_number = ++number;
3229 }
3230}
3231
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003232/*
3233 * Get complete information
3234 */
3235 static void
3236get_complete_info(list_T *what_list, dict_T *retdict)
3237{
3238 int ret = OK;
3239 listitem_T *item;
3240#define CI_WHAT_MODE 0x01
3241#define CI_WHAT_PUM_VISIBLE 0x02
3242#define CI_WHAT_ITEMS 0x04
3243#define CI_WHAT_SELECTED 0x08
3244#define CI_WHAT_INSERTED 0x10
3245#define CI_WHAT_ALL 0xff
3246 int what_flag;
3247
3248 if (what_list == NULL)
3249 what_flag = CI_WHAT_ALL;
3250 else
3251 {
3252 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003253 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003254 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003255 {
3256 char_u *what = tv_get_string(&item->li_tv);
3257
3258 if (STRCMP(what, "mode") == 0)
3259 what_flag |= CI_WHAT_MODE;
3260 else if (STRCMP(what, "pum_visible") == 0)
3261 what_flag |= CI_WHAT_PUM_VISIBLE;
3262 else if (STRCMP(what, "items") == 0)
3263 what_flag |= CI_WHAT_ITEMS;
3264 else if (STRCMP(what, "selected") == 0)
3265 what_flag |= CI_WHAT_SELECTED;
3266 else if (STRCMP(what, "inserted") == 0)
3267 what_flag |= CI_WHAT_INSERTED;
3268 }
3269 }
3270
3271 if (ret == OK && (what_flag & CI_WHAT_MODE))
3272 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3273
3274 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3275 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3276
Girish Palya8950bf72024-03-20 20:07:29 +01003277 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003278 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003279 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003280 dict_T *di;
3281 compl_T *match;
3282 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003283
Girish Palya8950bf72024-03-20 20:07:29 +01003284 if (what_flag & CI_WHAT_ITEMS)
3285 {
3286 li = list_alloc();
3287 if (li == NULL)
3288 return;
3289 ret = dict_add_list(retdict, "items", li);
3290 }
3291 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3292 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3293 ins_compl_update_sequence_numbers();
3294 if (ret == OK && compl_first_match != NULL)
3295 {
3296 int list_idx = 0;
3297 match = compl_first_match;
3298 do
3299 {
3300 if (!match_at_original_text(match))
3301 {
3302 if (what_flag & CI_WHAT_ITEMS)
3303 {
3304 di = dict_alloc();
3305 if (di == NULL)
3306 return;
3307 ret = list_append_dict(li, di);
3308 if (ret != OK)
3309 return;
John Marriott5e6ea922024-11-23 14:01:57 +01003310 dict_add_string(di, "word", match->cp_str.string);
Girish Palya8950bf72024-03-20 20:07:29 +01003311 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3312 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3313 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3314 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3315 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3316 // Add an empty string for backwards compatibility
3317 dict_add_string(di, "user_data", (char_u *)"");
3318 else
3319 dict_add_tv(di, "user_data", &match->cp_user_data);
3320 }
3321 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3322 selected_idx = list_idx;
3323 list_idx += 1;
3324 }
3325 match = match->cp_next;
3326 }
3327 while (match != NULL && !is_first_match(match));
3328 }
3329 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3330 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003331 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003332
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003333 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3334 {
3335 // TODO
3336 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003337}
3338
3339/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003340 * "complete_info()" function
3341 */
3342 void
3343f_complete_info(typval_T *argvars, typval_T *rettv)
3344{
3345 list_T *what_list = NULL;
3346
Bram Moolenaar93a10962022-06-16 11:42:09 +01003347 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003348 return;
3349
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003350 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3351 return;
3352
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003353 if (argvars[0].v_type != VAR_UNKNOWN)
3354 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003355 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003356 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003357 what_list = argvars[0].vval.v_list;
3358 }
3359 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003360}
3361#endif
3362
3363/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003364 * Returns TRUE when using a user-defined function for thesaurus completion.
3365 */
3366 static int
3367thesaurus_func_complete(int type UNUSED)
3368{
3369#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003370 return type == CTRL_X_THESAURUS
3371 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003372#else
3373 return FALSE;
3374#endif
3375}
3376
3377/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003378 * Return value of process_next_cpt_value()
3379 */
3380enum
3381{
3382 INS_COMPL_CPT_OK = 1,
3383 INS_COMPL_CPT_CONT,
3384 INS_COMPL_CPT_END
3385};
3386
3387/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003388 * state information used for getting the next set of insert completion
3389 * matches.
3390 */
3391typedef struct
3392{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003393 char_u *e_cpt_copy; // copy of 'complete'
3394 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003395 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003396 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003397 pos_T prev_match_pos; // previous match position
3398 int set_match_pos; // save first_match_pos/last_match_pos
3399 pos_T first_match_pos; // first match position
3400 pos_T last_match_pos; // last match position
3401 int found_all; // found all matches of a certain type.
3402 char_u *dict; // dictionary file to search
3403 int dict_f; // "dict" is an exact file name or not
3404} ins_compl_next_state_T;
3405
3406/*
3407 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408 *
3409 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003410 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003411 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003412 * st->found_all - all matches of this type are found
3413 * st->ins_buf - search for completions in this buffer
3414 * st->first_match_pos - position of the first completion match
3415 * st->last_match_pos - position of the last completion match
3416 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003417 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003418 * st->dict - name of the dictionary or thesaurus file to search
3419 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003420 *
3421 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003422 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3423 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003424 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003425 */
3426 static int
3427process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003428 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003430 pos_T *start_match_pos,
3431 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003432{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003433 int compl_type = -1;
3434 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003435
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003436 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003437
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003438 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3439 st->e_cpt++;
3440
3441 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003443 st->ins_buf = curbuf;
3444 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003445 // Move the cursor back one character so that ^N can match the
3446 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003447 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003448 {
3449 // Move the cursor to after the last character in the
3450 // buffer, so that word at start of buffer is found
3451 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003452 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003453 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003454 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003455 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003456 compl_type = 0;
3457
3458 // Remember the first match so that the loop stops when we
3459 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003460 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003461 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003462 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003463 && (st->ins_buf = ins_compl_next_buf(
3464 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003465 {
3466 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003467 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003468 {
3469 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003470 st->first_match_pos.col = st->last_match_pos.col = 0;
3471 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3472 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003473 compl_type = 0;
3474 }
3475 else // unloaded buffer, scan like dictionary
3476 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003477 st->found_all = TRUE;
3478 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003479 {
3480 status = INS_COMPL_CPT_CONT;
3481 goto done;
3482 }
3483 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003484 st->dict = st->ins_buf->b_fname;
3485 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003486 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003487 if (!shortmess(SHM_COMPLETIONSCAN))
3488 {
3489 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3490 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3491 st->ins_buf->b_fname == NULL
3492 ? buf_spname(st->ins_buf)
3493 : st->ins_buf->b_sfname == NULL
3494 ? st->ins_buf->b_fname
3495 : st->ins_buf->b_sfname);
3496 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3497 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003498 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003499 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003500 status = INS_COMPL_CPT_END;
3501 else
3502 {
3503 if (ctrl_x_mode_line_or_eval())
3504 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003505 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003506 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003507 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003508 compl_type = CTRL_X_DICTIONARY;
3509 else
3510 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003511 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003512 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003513 st->dict = st->e_cpt;
3514 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003515 }
3516 }
3517#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003518 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003519 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003520 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003521 compl_type = CTRL_X_PATH_DEFINES;
3522#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003523 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003524 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003525 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003526 if (!shortmess(SHM_COMPLETIONSCAN))
3527 {
3528 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3529 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3530 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3531 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003532 }
3533 else
3534 compl_type = -1;
3535
3536 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003537 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003538
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003539 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003540 if (compl_type == -1)
3541 status = INS_COMPL_CPT_CONT;
3542 }
3543
3544done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003545 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003546 return status;
3547}
3548
3549#ifdef FEAT_FIND_ID
3550/*
3551 * Get the next set of identifiers or defines matching "compl_pattern" in
3552 * included files.
3553 */
3554 static void
3555get_next_include_file_completion(int compl_type)
3556{
John Marriott5e6ea922024-11-23 14:01:57 +01003557 find_pattern_in_path(compl_pattern.string, compl_direction,
3558 (int)compl_pattern.length, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559 (compl_type == CTRL_X_PATH_DEFINES
3560 && !(compl_cont_status & CONT_SOL))
3561 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003562 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563}
3564#endif
3565
3566/*
3567 * Get the next set of words matching "compl_pattern" in dictionary or
3568 * thesaurus files.
3569 */
3570 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003571get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572{
3573#ifdef FEAT_COMPL_FUNC
3574 if (thesaurus_func_complete(compl_type))
John Marriott5e6ea922024-11-23 14:01:57 +01003575 expand_by_function(compl_type, compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576 else
3577#endif
3578 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003579 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003580 : (compl_type == CTRL_X_THESAURUS
3581 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3582 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
John Marriott5e6ea922024-11-23 14:01:57 +01003583 compl_pattern.string,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003584 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003585 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003586}
3587
3588/*
3589 * Get the next set of tag names matching "compl_pattern".
3590 */
3591 static void
3592get_next_tag_completion(void)
3593{
3594 int save_p_ic;
3595 char_u **matches;
3596 int num_matches;
3597
3598 // set p_ic according to p_ic, p_scs and pat for find_tags().
3599 save_p_ic = p_ic;
John Marriott5e6ea922024-11-23 14:01:57 +01003600 p_ic = ignorecase(compl_pattern.string);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003601
3602 // Find up to TAG_MANY matches. Avoids that an enormous number
3603 // of matches is found when compl_pattern is empty
3604 g_tag_at_cursor = TRUE;
John Marriott5e6ea922024-11-23 14:01:57 +01003605 if (find_tags(compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003607 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003608 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3609 ins_compl_add_matches(num_matches, matches, p_ic);
3610 g_tag_at_cursor = FALSE;
3611 p_ic = save_p_ic;
3612}
3613
3614/*
glepnir8159fb12024-07-17 20:32:54 +02003615 * Compare function for qsort
3616 */
3617static int compare_scores(const void *a, const void *b)
3618{
3619 int idx_a = *(const int *)a;
3620 int idx_b = *(const int *)b;
3621 int score_a = compl_fuzzy_scores[idx_a];
3622 int score_b = compl_fuzzy_scores[idx_b];
zeertzjq58d70522024-08-31 17:05:39 +02003623 return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1))
3624 : (score_a > score_b ? -1 : 1);
glepnir8159fb12024-07-17 20:32:54 +02003625}
3626
3627/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003628 * Get the next set of filename matching "compl_pattern".
3629 */
3630 static void
3631get_next_filename_completion(void)
3632{
3633 char_u **matches;
3634 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003635 char_u *ptr;
3636 garray_T fuzzy_indices;
3637 int i;
3638 int score;
3639 char_u *leader = ins_compl_leader();
John Marriott5e6ea922024-11-23 14:01:57 +01003640 size_t leader_len = ins_compl_leader_len();;
glepnir8159fb12024-07-17 20:32:54 +02003641 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3642 char_u **sorted_matches;
3643 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003644 char_u *last_sep = NULL;
glepnir8159fb12024-07-17 20:32:54 +02003645
glepnirb9de1a02024-08-02 19:14:38 +02003646#ifdef BACKSLASH_IN_FILENAME
3647 char pathsep = (curbuf->b_p_csl[0] == 's') ?
3648 '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP;
3649#else
3650 char pathsep = PATHSEP;
3651#endif
3652
glepnir8159fb12024-07-17 20:32:54 +02003653 if (in_fuzzy)
3654 {
glepnirb9de1a02024-08-02 19:14:38 +02003655#ifdef BACKSLASH_IN_FILENAME
3656 if (curbuf->b_p_csl[0] == 's')
3657 {
John Marriott5e6ea922024-11-23 14:01:57 +01003658 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003659 {
3660 if (leader[i] == '\\')
3661 leader[i] = '/';
3662 }
3663 }
3664 else if (curbuf->b_p_csl[0] == 'b')
3665 {
John Marriott5e6ea922024-11-23 14:01:57 +01003666 for (i = 0; i < (int)leader_len; i++)
glepnirb9de1a02024-08-02 19:14:38 +02003667 {
3668 if (leader[i] == '/')
3669 leader[i] = '\\';
3670 }
3671 }
3672#endif
3673 last_sep = vim_strrchr(leader, pathsep);
glepnir0be03e12024-07-19 16:45:05 +02003674 if (last_sep == NULL)
3675 {
3676 // No path separator or separator is the last character,
3677 // fuzzy match the whole leader
John Marriott5e6ea922024-11-23 14:01:57 +01003678 VIM_CLEAR_STRING(compl_pattern);
3679 compl_pattern.string = vim_strnsave((char_u *)"*", 1);
3680 if (compl_pattern.string == NULL)
3681 return;
3682 compl_pattern.length = 1;
glepnir0be03e12024-07-19 16:45:05 +02003683 }
3684 else if (*(last_sep + 1) == '\0')
3685 in_fuzzy = FALSE;
3686 else
3687 {
3688 // Split leader into path and file parts
3689 int path_len = last_sep - leader + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003690 char_u *path_with_wildcard;
3691
3692 path_with_wildcard = alloc(path_len + 2);
glepnir0be03e12024-07-19 16:45:05 +02003693 if (path_with_wildcard != NULL)
3694 {
John Marriott5e6ea922024-11-23 14:01:57 +01003695 vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader);
3696 VIM_CLEAR_STRING(compl_pattern);
3697 compl_pattern.string = path_with_wildcard;
3698 compl_pattern.length = path_len + 1;
glepnir0be03e12024-07-19 16:45:05 +02003699
3700 // Move leader to the file part
3701 leader = last_sep + 1;
John Marriott5e6ea922024-11-23 14:01:57 +01003702 leader_len -= path_len;
glepnir0be03e12024-07-19 16:45:05 +02003703 }
3704 }
glepnir8159fb12024-07-17 20:32:54 +02003705 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003706
John Marriott5e6ea922024-11-23 14:01:57 +01003707 if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3709 return;
3710
3711 // May change home directory back to "~".
John Marriott5e6ea922024-11-23 14:01:57 +01003712 tilde_replace(compl_pattern.string, num_matches, matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003713#ifdef BACKSLASH_IN_FILENAME
3714 if (curbuf->b_p_csl[0] != NUL)
3715 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003716 for (i = 0; i < num_matches; ++i)
3717 {
glepnir8159fb12024-07-17 20:32:54 +02003718 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003719 while (*ptr != NUL)
3720 {
3721 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3722 *ptr = '/';
3723 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3724 *ptr = '\\';
3725 ptr += (*mb_ptr2len)(ptr);
3726 }
3727 }
3728 }
3729#endif
glepnir8159fb12024-07-17 20:32:54 +02003730
3731 if (in_fuzzy)
3732 {
3733 ga_init2(&fuzzy_indices, sizeof(int), 10);
3734 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3735
3736 for (i = 0; i < num_matches; i++)
3737 {
3738 ptr = matches[i];
3739 score = fuzzy_match_str(ptr, leader);
3740 if (score > 0)
3741 {
3742 if (ga_grow(&fuzzy_indices, 1) == OK)
3743 {
3744 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3745 compl_fuzzy_scores[i] = score;
3746 fuzzy_indices.ga_len++;
3747 }
3748 }
3749 }
3750
Christian Brabandt220474d2024-07-20 13:26:44 +02003751 // prevent qsort from deref NULL pointer
3752 if (fuzzy_indices.ga_len > 0)
3753 {
3754 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3755 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003756
Christian Brabandt220474d2024-07-20 13:26:44 +02003757 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3758 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3759 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003760
Christian Brabandt220474d2024-07-20 13:26:44 +02003761 FreeWild(num_matches, matches);
3762 matches = sorted_matches;
3763 num_matches = fuzzy_indices.ga_len;
3764 }
glepnir6b6280c2024-07-27 16:25:45 +02003765 else if (leader_len > 0)
3766 {
3767 FreeWild(num_matches, matches);
3768 num_matches = 0;
3769 }
Christian Brabandt220474d2024-07-20 13:26:44 +02003770
glepnir8159fb12024-07-17 20:32:54 +02003771 vim_free(compl_fuzzy_scores);
3772 ga_clear(&fuzzy_indices);
3773 }
3774
glepnir6b6280c2024-07-27 16:25:45 +02003775 if (num_matches > 0)
3776 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003777}
3778
3779/*
3780 * Get the next set of command-line completions matching "compl_pattern".
3781 */
3782 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003783get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003784{
3785 char_u **matches;
3786 int num_matches;
3787
John Marriott5e6ea922024-11-23 14:01:57 +01003788 if (expand_cmdline(&compl_xp, compl_pattern.string,
3789 (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003790 ins_compl_add_matches(num_matches, matches, FALSE);
3791}
3792
3793/*
3794 * Get the next set of spell suggestions matching "compl_pattern".
3795 */
3796 static void
3797get_next_spell_completion(linenr_T lnum UNUSED)
3798{
3799#ifdef FEAT_SPELL
3800 char_u **matches;
3801 int num_matches;
3802
John Marriott5e6ea922024-11-23 14:01:57 +01003803 num_matches = expand_spelling(lnum, compl_pattern.string, &matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003804 if (num_matches > 0)
3805 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003806 else
3807 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003808#endif
3809}
3810
3811/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003812 * Return the next word or line from buffer "ins_buf" at position
3813 * "cur_match_pos" for completion. The length of the match is set in "len".
3814 */
3815 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003816ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003817 buf_T *ins_buf, // buffer being scanned
3818 pos_T *cur_match_pos, // current match position
3819 int *match_len,
3820 int *cont_s_ipos) // next ^X<> will set initial_pos
3821{
3822 char_u *ptr;
3823 int len;
3824
3825 *match_len = 0;
3826 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3827 cur_match_pos->col;
John Marriott5e6ea922024-11-23 14:01:57 +01003828 len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003829 if (ctrl_x_mode_line_or_eval())
3830 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003831 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003832 {
3833 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3834 return NULL;
3835 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
John Marriott5e6ea922024-11-23 14:01:57 +01003836 len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003837 if (!p_paste)
John Marriott5e6ea922024-11-23 14:01:57 +01003838 {
3839 char_u *tmp_ptr = ptr;
3840
3841 ptr = skipwhite(tmp_ptr);
3842 len -= (int)(ptr - tmp_ptr);
3843 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003844 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003845 }
3846 else
3847 {
3848 char_u *tmp_ptr = ptr;
3849
John Marriott5e6ea922024-11-23 14:01:57 +01003850 if (compl_status_adding() && compl_length <= len)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003851 {
3852 tmp_ptr += compl_length;
3853 // Skip if already inside a word.
3854 if (vim_iswordp(tmp_ptr))
3855 return NULL;
3856 // Find start of next word.
3857 tmp_ptr = find_word_start(tmp_ptr);
3858 }
3859 // Find end of this word.
3860 tmp_ptr = find_word_end(tmp_ptr);
3861 len = (int)(tmp_ptr - ptr);
3862
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003863 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003864 {
3865 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3866 {
3867 // Try next line, if any. the new word will be
3868 // "join" as if the normal command "J" was used.
3869 // IOSIZE is always greater than
3870 // compl_length, so the next STRNCPY always
3871 // works -- Acevedo
3872 STRNCPY(IObuff, ptr, len);
3873 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3874 tmp_ptr = ptr = skipwhite(ptr);
3875 // Find start of next word.
3876 tmp_ptr = find_word_start(tmp_ptr);
3877 // Find end of next word.
3878 tmp_ptr = find_word_end(tmp_ptr);
3879 if (tmp_ptr > ptr)
3880 {
3881 if (*ptr != ')' && IObuff[len - 1] != TAB)
3882 {
3883 if (IObuff[len - 1] != ' ')
3884 IObuff[len++] = ' ';
3885 // IObuf =~ "\k.* ", thus len >= 2
3886 if (p_js
3887 && (IObuff[len - 2] == '.'
3888 || (vim_strchr(p_cpo, CPO_JOINSP)
3889 == NULL
3890 && (IObuff[len - 2] == '?'
3891 || IObuff[len - 2] == '!'))))
3892 IObuff[len++] = ' ';
3893 }
3894 // copy as much as possible of the new word
3895 if (tmp_ptr - ptr >= IOSIZE - len)
3896 tmp_ptr = ptr + IOSIZE - len - 1;
3897 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3898 len += (int)(tmp_ptr - ptr);
3899 *cont_s_ipos = TRUE;
3900 }
3901 IObuff[len] = NUL;
3902 ptr = IObuff;
3903 }
3904 if (len == compl_length)
3905 return NULL;
3906 }
3907 }
3908
3909 *match_len = len;
3910 return ptr;
3911}
3912
3913/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003914 * Get the next set of words matching "compl_pattern" for default completion(s)
3915 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003916 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3917 * position "st->start_pos" in the "compl_direction" direction. If
3918 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3919 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003920 * Returns OK if a new next match is found, otherwise returns FAIL.
3921 */
3922 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003923get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003924{
3925 int found_new_match = FAIL;
3926 int save_p_scs;
3927 int save_p_ws;
3928 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003929 char_u *ptr = NULL;
3930 int len = 0;
3931 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3932 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003933
3934 // If 'infercase' is set, don't use 'smartcase' here
3935 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003936 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003937 p_scs = FALSE;
3938
3939 // Buffers other than curbuf are scanned from the beginning or the
3940 // end but never from the middle, thus setting nowrapscan in this
3941 // buffer is a good idea, on the other hand, we always set
3942 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3943 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003944 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003945 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003946 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003947 p_ws = TRUE;
3948 looped_around = FALSE;
3949 for (;;)
3950 {
3951 int cont_s_ipos = FALSE;
3952
3953 ++msg_silent; // Don't want messages for wrapscan.
3954
3955 // ctrl_x_mode_line_or_eval() || word-wise search that
3956 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003957 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003958 found_new_match = search_for_exact_line(st->ins_buf,
John Marriott5e6ea922024-11-23 14:01:57 +01003959 st->cur_match_pos, compl_direction, compl_pattern.string);
glepnir8159fb12024-07-17 20:32:54 +02003960 else if (in_fuzzy)
3961 found_new_match = search_for_fuzzy_match(st->ins_buf,
3962 st->cur_match_pos, leader, compl_direction,
3963 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003964 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003965 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott5e6ea922024-11-23 14:01:57 +01003966 NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length,
John Marriott8c85a2a2024-05-20 19:18:26 +02003967 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003968 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003969 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003970 {
3971 // set "compl_started" even on fail
3972 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003973 st->first_match_pos = *st->cur_match_pos;
3974 st->last_match_pos = *st->cur_match_pos;
3975 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003976 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003977 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3978 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003979 {
3980 found_new_match = FAIL;
3981 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003982 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003983 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3984 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3985 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003986 {
3987 if (looped_around)
3988 found_new_match = FAIL;
3989 else
3990 looped_around = TRUE;
3991 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003992 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003993 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3994 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3995 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003996 {
3997 if (looped_around)
3998 found_new_match = FAIL;
3999 else
4000 looped_around = TRUE;
4001 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004002 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004003 if (found_new_match == FAIL)
4004 break;
4005
4006 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004007 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004008 && start_pos->lnum == st->cur_match_pos->lnum
4009 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004010 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004011
glepnir8159fb12024-07-17 20:32:54 +02004012 if (!in_fuzzy)
4013 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
4014 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004015 if (ptr == NULL)
4016 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004017
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004018 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004019 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004020 0, cont_s_ipos) != NOTDONE)
4021 {
4022 found_new_match = OK;
4023 break;
4024 }
4025 }
4026 p_scs = save_p_scs;
4027 p_ws = save_p_ws;
4028
4029 return found_new_match;
4030}
4031
4032/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004033 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004034 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004035 */
4036 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004037get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004038{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004039 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004040
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004041 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004043 case -1:
4044 break;
4045#ifdef FEAT_FIND_ID
4046 case CTRL_X_PATH_PATTERNS:
4047 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004048 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004049 break;
4050#endif
4051
4052 case CTRL_X_DICTIONARY:
4053 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004054 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
4055 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004056 break;
4057
4058 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004059 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060 break;
4061
4062 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004063 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004064 break;
4065
4066 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02004067 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00004068 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069 break;
4070
4071#ifdef FEAT_COMPL_FUNC
4072 case CTRL_X_FUNCTION:
4073 case CTRL_X_OMNI:
John Marriott5e6ea922024-11-23 14:01:57 +01004074 expand_by_function(type, compl_pattern.string);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004075 break;
4076#endif
4077
4078 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004079 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004080 break;
4081
4082 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004083 found_new_match = get_next_default_completion(st, ini);
4084 if (found_new_match == FAIL && st->ins_buf == curbuf)
4085 st->found_all = TRUE;
4086 }
4087
4088 // check if compl_curr_match has changed, (e.g. other type of
4089 // expansion added something)
4090 if (type != 0 && compl_curr_match != compl_old_match)
4091 found_new_match = OK;
4092
4093 return found_new_match;
4094}
4095
4096/*
4097 * Get the next expansion(s), using "compl_pattern".
4098 * The search starts at position "ini" in curbuf and in the direction
4099 * compl_direction.
4100 * When "compl_started" is FALSE start at that position, otherwise continue
4101 * where we stopped searching before.
4102 * This may return before finding all the matches.
4103 * Return the total number of matches or -1 if still unknown -- Acevedo
4104 */
4105 static int
4106ins_compl_get_exp(pos_T *ini)
4107{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004108 static ins_compl_next_state_T st;
4109 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004110 int i;
4111 int found_new_match;
4112 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02004113 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004114
4115 if (!compl_started)
4116 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004117 buf_T *buf;
4118
4119 FOR_ALL_BUFFERS(buf)
4120 buf->b_scanned = 0;
4121 if (!st_cleared)
4122 {
4123 CLEAR_FIELD(st);
4124 st_cleared = TRUE;
4125 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004126 st.found_all = FALSE;
4127 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004128 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004129 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004130 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
4131 ? (char_u *)"." : curbuf->b_p_cpt);
4132 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004133 st.last_match_pos = st.first_match_pos = *ini;
4134 }
4135 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
4136 st.ins_buf = curbuf; // In case the buffer was wiped out.
4137
4138 compl_old_match = compl_curr_match; // remember the last current match
Christian Brabandt13032a42024-07-28 21:16:48 +02004139 st.cur_match_pos = (compl_dir_forward())
glepnir8159fb12024-07-17 20:32:54 +02004140 ? &st.last_match_pos : &st.first_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004141
4142 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4143 for (;;)
4144 {
4145 found_new_match = FAIL;
4146 st.set_match_pos = FALSE;
4147
4148 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4149 // or if found_all says this entry is done. For ^X^L only use the
4150 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004151 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004152 && (!compl_started || st.found_all))
4153 {
glepnir8159fb12024-07-17 20:32:54 +02004154 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004155
4156 if (status == INS_COMPL_CPT_END)
4157 break;
4158 if (status == INS_COMPL_CPT_CONT)
4159 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004160 }
4161
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004162 // If complete() was called then compl_pattern has been reset. The
4163 // following won't work then, bail out.
John Marriott5e6ea922024-11-23 14:01:57 +01004164 if (compl_pattern.string == NULL)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004165 break;
4166
4167 // get the next set of completion matches
4168 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004169
4170 // break the loop for specialized modes (use 'complete' just for the
4171 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4172 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004173 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4174 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004175 {
4176 if (got_int)
4177 break;
4178 // Fill the popup menu as soon as possible.
4179 if (type != -1)
4180 ins_compl_check_keys(0, FALSE);
4181
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004182 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004183 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4184 break;
4185 compl_started = TRUE;
4186 }
4187 else
4188 {
4189 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004190 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004191 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004192
4193 compl_started = FALSE;
4194 }
4195 }
4196 compl_started = TRUE;
4197
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004198 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004199 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004200 found_new_match = FAIL;
4201
4202 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004203 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004204 && !ctrl_x_mode_line_or_eval()))
4205 i = ins_compl_make_cyclic();
4206
4207 if (compl_old_match != NULL)
4208 {
4209 // If several matches were added (FORWARD) or the search failed and has
4210 // just been made cyclic then we have to move compl_curr_match to the
4211 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004212 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004213 : compl_old_match->cp_prev;
4214 if (compl_curr_match == NULL)
4215 compl_curr_match = compl_old_match;
4216 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004217 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004218
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004219 return i;
4220}
4221
4222/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004223 * Update "compl_shown_match" to the actually shown match, it may differ when
4224 * "compl_leader" is used to omit some of the matches.
4225 */
4226 static void
4227ins_compl_update_shown_match(void)
4228{
4229 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004230 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004231 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004232 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004233 compl_shown_match = compl_shown_match->cp_next;
4234
4235 // If we didn't find it searching forward, and compl_shows_dir is
4236 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004237 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004238 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004239 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004240 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004241 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004242 {
4243 while (!ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004244 compl_leader.string, (int)compl_leader.length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004245 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004246 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004247 compl_shown_match = compl_shown_match->cp_prev;
4248 }
4249}
4250
4251/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004252 * Delete the old text being completed.
4253 */
4254 void
4255ins_compl_delete(void)
4256{
4257 int col;
4258
4259 // In insert mode: Delete the typed part.
4260 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004261 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004262 if ((int)curwin->w_cursor.col > col)
4263 {
4264 if (stop_arrow() == FAIL)
4265 return;
4266 backspace_until_column(col);
4267 }
4268
4269 // TODO: is this sufficient for redrawing? Redrawing everything causes
4270 // flicker, thus we can't do that.
4271 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004272#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004273 // clear v:completed_item
4274 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004275#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004276}
4277
4278/*
4279 * Insert the new text being completed.
4280 * "in_compl_func" is TRUE when called from complete_check().
4281 */
4282 void
4283ins_compl_insert(int in_compl_func)
4284{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004285 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004286
4287 // Make sure we don't go over the end of the string, this can happen with
4288 // illegal bytes.
John Marriott5e6ea922024-11-23 14:01:57 +01004289 if (compl_len < (int)compl_shown_match->cp_str.length)
glepnir6a38aff2024-12-16 21:56:16 +01004290 ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004291 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004292 compl_used_match = FALSE;
4293 else
4294 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004295#ifdef FEAT_EVAL
4296 {
4297 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4298
4299 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4300 }
4301#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004302 if (!in_compl_func)
4303 compl_curr_match = compl_shown_match;
4304}
4305
4306/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004307 * show the file name for the completion match (if any). Truncate the file
4308 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004309 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004310 static void
4311ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004312{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004313 char *lead = _("match in file");
4314 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4315 char_u *s;
4316 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004317
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004318 if (space <= 0)
4319 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004320
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004321 // We need the tail that fits. With double-byte encoding going
4322 // back from the end is very slow, thus go from the start and keep
4323 // the text that fits in "space" between "s" and "e".
4324 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004325 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004326 space -= ptr2cells(e);
4327 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004328 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004329 space += ptr2cells(s);
4330 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004331 }
4332 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004333 msg_hist_off = TRUE;
4334 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4335 s > compl_shown_match->cp_fname ? "<" : "", s);
4336 msg((char *)IObuff);
4337 msg_hist_off = FALSE;
4338 redraw_cmdline = FALSE; // don't overwrite!
4339}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004340
glepnirdca57fb2024-06-04 22:01:21 +02004341/*
zeertzjq551d8c32024-06-05 19:53:32 +02004342 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004343 */
glepnira218cc62024-06-03 19:32:39 +02004344 static compl_T *
4345find_comp_when_fuzzy(void)
4346{
4347 int score;
4348 char_u* str;
4349 int target_idx = -1;
4350 int is_forward = compl_shows_dir_forward();
4351 int is_backward = compl_shows_dir_backward();
4352 compl_T *comp = NULL;
4353
glepnir43eef882024-06-19 20:20:48 +02004354 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004355 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004356 return compl_first_match != compl_shown_match ? compl_first_match :
4357 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004358
4359 if (is_forward)
4360 target_idx = compl_selected_item + 1;
4361 else if (is_backward)
4362 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004363 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004364
4365 score = compl_match_array[target_idx].pum_score;
4366 str = compl_match_array[target_idx].pum_text;
4367
4368 comp = compl_first_match;
4369 do {
John Marriott5e6ea922024-11-23 14:01:57 +01004370 if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR]))
glepnira218cc62024-06-03 19:32:39 +02004371 return comp;
4372 comp = comp->cp_next;
4373 } while (comp != NULL && !is_first_match(comp));
4374
4375 return NULL;
4376}
4377
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004378/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004379 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004380 * times. The number of matches found is returned in 'num_matches'.
4381 *
4382 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4383 * get more completions. If it is FALSE, then do nothing when there are no more
4384 * completions in the given direction.
4385 *
4386 * If "advance" is TRUE, then completion will move to the first match.
4387 * Otherwise, the original text will be shown.
4388 *
4389 * Returns OK on success and -1 if the number of matches are unknown.
4390 */
4391 static int
4392find_next_completion_match(
4393 int allow_get_expansion,
4394 int todo, // repeat completion this many times
4395 int advance,
4396 int *num_matches)
4397{
4398 int found_end = FALSE;
4399 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004400 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004401 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4402 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004403
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004404 while (--todo >= 0)
4405 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004406 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004407 {
glepniraced8c22024-06-15 15:13:05 +02004408 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4409 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004410 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004411 && (is_first_match(compl_shown_match->cp_next)
4412 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004413 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004414 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004415 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004416 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004417 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004418 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4419 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004420 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004421 }
4422 else
4423 {
4424 if (!allow_get_expansion)
4425 {
4426 if (advance)
4427 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004428 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004429 compl_pending -= todo + 1;
4430 else
4431 compl_pending += todo + 1;
4432 }
4433 return -1;
4434 }
4435
4436 if (!compl_no_select && advance)
4437 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004438 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004439 --compl_pending;
4440 else
4441 ++compl_pending;
4442 }
4443
4444 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004445 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004446
4447 // handle any pending completions
4448 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004449 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004450 {
4451 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4452 {
4453 compl_shown_match = compl_shown_match->cp_next;
4454 --compl_pending;
4455 }
4456 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4457 {
4458 compl_shown_match = compl_shown_match->cp_prev;
4459 ++compl_pending;
4460 }
4461 else
4462 break;
4463 }
4464 found_end = FALSE;
4465 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004466 if (!match_at_original_text(compl_shown_match)
John Marriott5e6ea922024-11-23 14:01:57 +01004467 && compl_leader.string != NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004468 && !ins_compl_equal(compl_shown_match,
John Marriott5e6ea922024-11-23 14:01:57 +01004469 compl_leader.string, (int)compl_leader.length)
glepnira218cc62024-06-03 19:32:39 +02004470 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004471 ++todo;
4472 else
4473 // Remember a matching item.
4474 found_compl = compl_shown_match;
4475
4476 // Stop at the end of the list when we found a usable match.
4477 if (found_end)
4478 {
4479 if (found_compl != NULL)
4480 {
4481 compl_shown_match = found_compl;
4482 break;
4483 }
4484 todo = 1; // use first usable match after wrapping around
4485 }
4486 }
4487
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004488 return OK;
4489}
4490
4491/*
4492 * Fill in the next completion in the current direction.
4493 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4494 * get more completions. If it is FALSE, then we just do nothing when there
4495 * are no more completions in a given direction. The latter case is used when
4496 * we are still in the middle of finding completions, to allow browsing
4497 * through the ones found so far.
4498 * Return the total number of matches, or -1 if still unknown -- webb.
4499 *
4500 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4501 * compl_shown_match here.
4502 *
4503 * Note that this function may be called recursively once only. First with
4504 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4505 * calls this function with "allow_get_expansion" FALSE.
4506 */
4507 static int
4508ins_compl_next(
4509 int allow_get_expansion,
4510 int count, // repeat completion this many times; should
4511 // be at least 1
4512 int insert_match, // Insert the newly selected match
4513 int in_compl_func) // called from complete_check()
4514{
4515 int num_matches = -1;
4516 int todo = count;
4517 int advance;
4518 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004519 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004520 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004521 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4522 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004523
4524 // When user complete function return -1 for findstart which is next
4525 // time of 'always', compl_shown_match become NULL.
4526 if (compl_shown_match == NULL)
4527 return -1;
4528
John Marriott5e6ea922024-11-23 14:01:57 +01004529 if (compl_leader.string != NULL
glepnira218cc62024-06-03 19:32:39 +02004530 && !match_at_original_text(compl_shown_match)
4531 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004532 // Update "compl_shown_match" to the actually shown match
4533 ins_compl_update_shown_match();
4534
4535 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004536 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004537 // Delete old text to be replaced
4538 ins_compl_delete();
4539
4540 // When finding the longest common text we stick at the original text,
4541 // don't let CTRL-N or CTRL-P move to the first match.
4542 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4543
4544 // When restarting the search don't insert the first match either.
4545 if (compl_restarting)
4546 {
4547 advance = FALSE;
4548 compl_restarting = FALSE;
4549 }
4550
4551 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4552 // around.
4553 if (find_next_completion_match(allow_get_expansion, todo, advance,
4554 &num_matches) == -1)
4555 return -1;
4556
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004557 if (curbuf != orig_curbuf)
4558 {
4559 // In case some completion function switched buffer, don't want to
4560 // insert the completion elsewhere.
4561 return -1;
4562 }
4563
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004564 // Insert the text of the new completion, or the compl_leader.
4565 if (compl_no_insert && !started)
4566 {
glepnir6a38aff2024-12-16 21:56:16 +01004567 ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004568 compl_used_match = FALSE;
4569 }
4570 else if (insert_match)
4571 {
4572 if (!compl_get_longest || compl_used_match)
4573 ins_compl_insert(in_compl_func);
4574 else
glepnir6a38aff2024-12-16 21:56:16 +01004575 ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004576 }
4577 else
4578 compl_used_match = FALSE;
4579
4580 if (!allow_get_expansion)
4581 {
4582 // may undisplay the popup menu first
4583 ins_compl_upd_pum();
4584
4585 if (pum_enough_matches())
4586 // Will display the popup menu, don't redraw yet to avoid flicker.
4587 pum_call_update_screen();
4588 else
4589 // Not showing the popup menu yet, redraw to show the user what was
4590 // inserted.
4591 update_screen(0);
4592
4593 // display the updated popup menu
4594 ins_compl_show_pum();
4595#ifdef FEAT_GUI
4596 if (gui.in_use)
4597 {
4598 // Show the cursor after the match, not after the redrawn text.
4599 setcursor();
4600 out_flush_cursor(FALSE, FALSE);
4601 }
4602#endif
4603
4604 // Delete old text to be replaced, since we're still searching and
4605 // don't want to match ourselves!
4606 ins_compl_delete();
4607 }
4608
4609 // Enter will select a match when the match wasn't inserted and the popup
4610 // menu is visible.
4611 if (compl_no_insert && !started)
4612 compl_enter_selects = TRUE;
4613 else
4614 compl_enter_selects = !insert_match && compl_match_array != NULL;
4615
4616 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004617 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004618 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004619
4620 return num_matches;
4621}
4622
4623/*
4624 * Call this while finding completions, to check whether the user has hit a key
4625 * that should change the currently displayed completion, or exit completion
4626 * mode. Also, when compl_pending is not zero, show a completion as soon as
4627 * possible. -- webb
4628 * "frequency" specifies out of how many calls we actually check.
4629 * "in_compl_func" is TRUE when called from complete_check(), don't set
4630 * compl_curr_match.
4631 */
4632 void
4633ins_compl_check_keys(int frequency, int in_compl_func)
4634{
4635 static int count = 0;
4636 int c;
4637
4638 // Don't check when reading keys from a script, :normal or feedkeys().
4639 // That would break the test scripts. But do check for keys when called
4640 // from complete_check().
4641 if (!in_compl_func && (using_script() || ex_normal_busy))
4642 return;
4643
4644 // Only do this at regular intervals
4645 if (++count < frequency)
4646 return;
4647 count = 0;
4648
4649 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4650 // can't do its work correctly.
4651 c = vpeekc_any();
4652 if (c != NUL)
4653 {
4654 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4655 {
4656 c = safe_vgetc(); // Eat the character
4657 compl_shows_dir = ins_compl_key2dir(c);
4658 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4659 c != K_UP && c != K_DOWN, in_compl_func);
4660 }
4661 else
4662 {
4663 // Need to get the character to have KeyTyped set. We'll put it
4664 // back with vungetc() below. But skip K_IGNORE.
4665 c = safe_vgetc();
4666 if (c != K_IGNORE)
4667 {
4668 // Don't interrupt completion when the character wasn't typed,
4669 // e.g., when doing @q to replay keys.
4670 if (c != Ctrl_R && KeyTyped)
4671 compl_interrupted = TRUE;
4672
4673 vungetc(c);
4674 }
4675 }
4676 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004677 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004678 {
4679 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4680
4681 compl_pending = 0;
4682 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4683 }
4684}
4685
4686/*
4687 * Decide the direction of Insert mode complete from the key typed.
4688 * Returns BACKWARD or FORWARD.
4689 */
4690 static int
4691ins_compl_key2dir(int c)
4692{
4693 if (c == Ctrl_P || c == Ctrl_L
4694 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4695 return BACKWARD;
4696 return FORWARD;
4697}
4698
4699/*
4700 * Return TRUE for keys that are used for completion only when the popup menu
4701 * is visible.
4702 */
4703 static int
4704ins_compl_pum_key(int c)
4705{
4706 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4707 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4708 || c == K_UP || c == K_DOWN);
4709}
4710
4711/*
4712 * Decide the number of completions to move forward.
4713 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4714 */
4715 static int
4716ins_compl_key2count(int c)
4717{
4718 int h;
4719
4720 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4721 {
4722 h = pum_get_height();
4723 if (h > 3)
4724 h -= 2; // keep some context
4725 return h;
4726 }
4727 return 1;
4728}
4729
4730/*
4731 * Return TRUE if completion with "c" should insert the match, FALSE if only
4732 * to change the currently selected completion.
4733 */
4734 static int
4735ins_compl_use_match(int c)
4736{
4737 switch (c)
4738 {
4739 case K_UP:
4740 case K_DOWN:
4741 case K_PAGEDOWN:
4742 case K_KPAGEDOWN:
4743 case K_S_DOWN:
4744 case K_PAGEUP:
4745 case K_KPAGEUP:
4746 case K_S_UP:
4747 return FALSE;
4748 }
4749 return TRUE;
4750}
4751
4752/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004753 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4754 * completion)
John Marriott5e6ea922024-11-23 14:01:57 +01004755 * Sets the global variables: compl_col, compl_length and compl_pattern.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004756 * Uses the global variables: compl_cont_status and ctrl_x_mode
4757 */
4758 static int
4759get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4760{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004761 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004762 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004763 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004764 {
4765 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4766 ;
4767 compl_col += ++startcol;
4768 compl_length = curs_col - startcol;
4769 }
4770 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004771 {
John Marriott5e6ea922024-11-23 14:01:57 +01004772 compl_pattern.string = str_foldcase(line + compl_col,
4773 compl_length, NULL, 0);
4774 if (compl_pattern.string == NULL)
4775 {
4776 compl_pattern.length = 0;
4777 return FAIL;
4778 }
4779 compl_pattern.length = STRLEN(compl_pattern.string);
4780 }
4781 else
4782 {
4783 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4784 if (compl_pattern.string == NULL)
4785 {
4786 compl_pattern.length = 0;
4787 return FAIL;
4788 }
4789 compl_pattern.length = (size_t)compl_length;
John Marriott8c85a2a2024-05-20 19:18:26 +02004790 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004791 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004792 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004793 {
4794 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004795 size_t prefixlen = STRLEN_LITERAL("\\<");
John Marriott5e6ea922024-11-23 14:01:57 +01004796 size_t n;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004797
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004798 if (!vim_iswordp(line + compl_col)
4799 || (compl_col > 0
4800 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004801 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004802 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004803 prefixlen = 0;
4804 }
John Marriott5e6ea922024-11-23 14:01:57 +01004805
4806 // we need up to 2 extra chars for the prefix
4807 n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen;
4808 compl_pattern.string = alloc(n);
4809 if (compl_pattern.string == NULL)
4810 {
4811 compl_pattern.length = 0;
4812 return FAIL;
4813 }
4814 STRCPY((char *)compl_pattern.string, prefix);
4815 (void)quote_meta(compl_pattern.string + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004816 line + compl_col, compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004817 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004818 }
4819 else if (--startcol < 0
4820 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4821 {
John Marriott5e6ea922024-11-23 14:01:57 +01004822 size_t len = STRLEN_LITERAL("\\<\\k\\k");
4823
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004824 // Match any word of at least two chars
John Marriott5e6ea922024-11-23 14:01:57 +01004825 compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len);
4826 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004827 {
John Marriott5e6ea922024-11-23 14:01:57 +01004828 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004829 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004830 }
John Marriott5e6ea922024-11-23 14:01:57 +01004831 compl_pattern.length = len;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004832 compl_col += curs_col;
4833 compl_length = 0;
4834 }
4835 else
4836 {
4837 // Search the point of change class of multibyte character
4838 // or not a word single byte character backward.
4839 if (has_mbyte)
4840 {
4841 int base_class;
4842 int head_off;
4843
4844 startcol -= (*mb_head_off)(line, line + startcol);
4845 base_class = mb_get_class(line + startcol);
4846 while (--startcol >= 0)
4847 {
4848 head_off = (*mb_head_off)(line, line + startcol);
4849 if (base_class != mb_get_class(line + startcol
4850 - head_off))
4851 break;
4852 startcol -= head_off;
4853 }
4854 }
4855 else
4856 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4857 ;
4858 compl_col += ++startcol;
4859 compl_length = (int)curs_col - startcol;
4860 if (compl_length == 1)
4861 {
4862 // Only match word with at least two chars -- webb
4863 // there's no need to call quote_meta,
4864 // alloc(7) is enough -- Acevedo
John Marriott5e6ea922024-11-23 14:01:57 +01004865 compl_pattern.string = alloc(7);
4866 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004867 {
John Marriott5e6ea922024-11-23 14:01:57 +01004868 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004869 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004870 }
John Marriott5e6ea922024-11-23 14:01:57 +01004871 STRCPY((char *)compl_pattern.string, "\\<");
4872 (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1);
4873 STRCAT((char *)compl_pattern.string, "\\k");
4874 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004875 }
4876 else
4877 {
John Marriott5e6ea922024-11-23 14:01:57 +01004878 size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2;
4879
4880 compl_pattern.string = alloc(n);
4881 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004882 {
John Marriott5e6ea922024-11-23 14:01:57 +01004883 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004884 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004885 }
John Marriott5e6ea922024-11-23 14:01:57 +01004886 STRCPY((char *)compl_pattern.string, "\\<");
4887 (void)quote_meta(compl_pattern.string + 2, line + compl_col,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004888 compl_length);
John Marriott5e6ea922024-11-23 14:01:57 +01004889 compl_pattern.length = n - 1;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004890 }
4891 }
4892
4893 return OK;
4894}
4895
4896/*
4897 * Get the pattern, column and length for whole line completion or for the
4898 * complete() function.
4899 * Sets the global variables: compl_col, compl_length and compl_pattern.
4900 */
4901 static int
4902get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4903{
4904 compl_col = (colnr_T)getwhitecols(line);
4905 compl_length = (int)curs_col - (int)compl_col;
4906 if (compl_length < 0) // cursor in indent: empty pattern
4907 compl_length = 0;
4908 if (p_ic)
John Marriott8c85a2a2024-05-20 19:18:26 +02004909 {
John Marriott5e6ea922024-11-23 14:01:57 +01004910 compl_pattern.string = str_foldcase(line + compl_col, compl_length,
4911 NULL, 0);
4912 if (compl_pattern.string == NULL)
4913 {
4914 compl_pattern.length = 0;
4915 return FAIL;
4916 }
4917 compl_pattern.length = STRLEN(compl_pattern.string);
John Marriott8c85a2a2024-05-20 19:18:26 +02004918 }
John Marriott5e6ea922024-11-23 14:01:57 +01004919 else
4920 {
4921 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
4922 if (compl_pattern.string == NULL)
4923 {
4924 compl_pattern.length = 0;
4925 return FAIL;
4926 }
4927 compl_pattern.length = (size_t)compl_length;
4928 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004929
4930 return OK;
4931}
4932
4933/*
4934 * Get the pattern, column and length for filename completion.
4935 * Sets the global variables: compl_col, compl_length and compl_pattern.
4936 */
4937 static int
4938get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4939{
4940 // Go back to just before the first filename character.
4941 if (startcol > 0)
4942 {
4943 char_u *p = line + startcol;
4944
4945 MB_PTR_BACK(line, p);
4946 while (p > line && vim_isfilec(PTR2CHAR(p)))
4947 MB_PTR_BACK(line, p);
4948 if (p == line && vim_isfilec(PTR2CHAR(p)))
4949 startcol = 0;
4950 else
4951 startcol = (int)(p - line) + 1;
4952 }
4953
4954 compl_col += startcol;
4955 compl_length = (int)curs_col - startcol;
John Marriott5e6ea922024-11-23 14:01:57 +01004956 compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES);
4957 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004958 {
John Marriott5e6ea922024-11-23 14:01:57 +01004959 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004960 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004961 }
4962
John Marriott5e6ea922024-11-23 14:01:57 +01004963 compl_pattern.length = STRLEN(compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004964
4965 return OK;
4966}
4967
4968/*
4969 * Get the pattern, column and length for command-line completion.
4970 * Sets the global variables: compl_col, compl_length and compl_pattern.
4971 */
4972 static int
4973get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4974{
John Marriott5e6ea922024-11-23 14:01:57 +01004975 compl_pattern.string = vim_strnsave(line, curs_col);
4976 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004977 {
John Marriott5e6ea922024-11-23 14:01:57 +01004978 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004979 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004980 }
John Marriott5e6ea922024-11-23 14:01:57 +01004981 compl_pattern.length = curs_col;
4982 set_cmd_context(&compl_xp, compl_pattern.string,
4983 (int)compl_pattern.length, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004984 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4985 || compl_xp.xp_context == EXPAND_NOTHING)
4986 // No completion possible, use an empty pattern to get a
4987 // "pattern not found" message.
4988 compl_col = curs_col;
4989 else
John Marriott5e6ea922024-11-23 14:01:57 +01004990 compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004991 compl_length = curs_col - compl_col;
4992
4993 return OK;
4994}
4995
4996/*
4997 * Get the pattern, column and length for user defined completion ('omnifunc',
4998 * 'completefunc' and 'thesaurusfunc')
4999 * Sets the global variables: compl_col, compl_length and compl_pattern.
5000 * Uses the global variable: spell_bad_len
5001 */
5002 static int
5003get_userdefined_compl_info(colnr_T curs_col UNUSED)
5004{
5005 int ret = FAIL;
5006
5007#ifdef FEAT_COMPL_FUNC
5008 // Call user defined function 'completefunc' with "a:findstart"
5009 // set to 1 to obtain the length of text to use for completion.
5010 char_u *line;
5011 typval_T args[3];
5012 int col;
5013 char_u *funcname;
5014 pos_T pos;
5015 int save_State = State;
5016 callback_T *cb;
5017
5018 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
5019 // length as a string
5020 funcname = get_complete_funcname(ctrl_x_mode);
5021 if (*funcname == NUL)
5022 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005023 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005024 ? "completefunc" : "omnifunc");
5025 return FAIL;
5026 }
5027
5028 args[0].v_type = VAR_NUMBER;
5029 args[0].vval.v_number = 1;
5030 args[1].v_type = VAR_STRING;
5031 args[1].vval.v_string = (char_u *)"";
5032 args[2].v_type = VAR_UNKNOWN;
5033 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01005034 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005035 cb = get_insert_callback(ctrl_x_mode);
5036 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01005037 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005038
5039 State = save_State;
5040 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02005041 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005042 validate_cursor();
5043 if (!EQUAL_POS(curwin->w_cursor, pos))
5044 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005045 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005046 return FAIL;
5047 }
5048
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005049 // Return value -2 means the user complete function wants to cancel the
5050 // complete without an error, do the same if the function did not execute
5051 // successfully.
5052 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005053 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01005054 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005055 if (col == -3)
5056 {
5057 ctrl_x_mode = CTRL_X_NORMAL;
5058 edit_submode = NULL;
5059 if (!shortmess(SHM_COMPLETIONMENU))
5060 msg_clr_cmdline();
5061 return FAIL;
5062 }
5063
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00005064 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005065 // completion.
5066 compl_opt_refresh_always = FALSE;
5067 compl_opt_suppress_empty = FALSE;
5068
5069 if (col < 0)
5070 col = curs_col;
5071 compl_col = col;
5072 if (compl_col > curs_col)
5073 compl_col = curs_col;
5074
5075 // Setup variables for completion. Need to obtain "line" again,
5076 // it may have become invalid.
5077 line = ml_get(curwin->w_cursor.lnum);
5078 compl_length = curs_col - compl_col;
John Marriott5e6ea922024-11-23 14:01:57 +01005079 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5080 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005081 {
John Marriott5e6ea922024-11-23 14:01:57 +01005082 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005083 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005084 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005085
John Marriott5e6ea922024-11-23 14:01:57 +01005086 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005087 ret = OK;
5088#endif
5089
5090 return ret;
5091}
5092
5093/*
5094 * Get the pattern, column and length for spell completion.
5095 * Sets the global variables: compl_col, compl_length and compl_pattern.
5096 * Uses the global variable: spell_bad_len
5097 */
5098 static int
5099get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
5100{
5101 int ret = FAIL;
5102#ifdef FEAT_SPELL
5103 char_u *line;
5104
5105 if (spell_bad_len > 0)
5106 compl_col = curs_col - spell_bad_len;
5107 else
5108 compl_col = spell_word_start(startcol);
5109 if (compl_col >= (colnr_T)startcol)
5110 {
5111 compl_length = 0;
5112 compl_col = curs_col;
5113 }
5114 else
5115 {
5116 spell_expand_check_cap(compl_col);
5117 compl_length = (int)curs_col - compl_col;
5118 }
5119 // Need to obtain "line" again, it may have become invalid.
5120 line = ml_get(curwin->w_cursor.lnum);
John Marriott5e6ea922024-11-23 14:01:57 +01005121 compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length);
5122 if (compl_pattern.string == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005123 {
John Marriott5e6ea922024-11-23 14:01:57 +01005124 compl_pattern.length = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005125 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02005126 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005127
John Marriott5e6ea922024-11-23 14:01:57 +01005128 compl_pattern.length = (size_t)compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005129 ret = OK;
5130#endif
5131
5132 return ret;
5133}
5134
5135/*
5136 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005137 * "startcol" - start column number of the completion pattern/text
5138 * "cur_col" - current cursor column
5139 * On return, "line_invalid" is set to TRUE, if the current line may have
5140 * become invalid and needs to be fetched again.
5141 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005142 */
5143 static int
5144compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
5145{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005146 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005147 || (ctrl_x_mode & CTRL_X_WANT_IDENT
5148 && !thesaurus_func_complete(ctrl_x_mode)))
5149 {
5150 return get_normal_compl_info(line, startcol, curs_col);
5151 }
5152 else if (ctrl_x_mode_line_or_eval())
5153 {
5154 return get_wholeline_compl_info(line, curs_col);
5155 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005156 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005157 {
5158 return get_filename_compl_info(line, startcol, curs_col);
5159 }
5160 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5161 {
5162 return get_cmdline_compl_info(line, curs_col);
5163 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005164 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005165 || thesaurus_func_complete(ctrl_x_mode))
5166 {
5167 if (get_userdefined_compl_info(curs_col) == FAIL)
5168 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005169 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005170 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005171 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005172 {
5173 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5174 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005175 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005176 }
5177 else
5178 {
5179 internal_error("ins_complete()");
5180 return FAIL;
5181 }
5182
5183 return OK;
5184}
5185
5186/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005187 * Continue an interrupted completion mode search in "line".
5188 *
5189 * If this same ctrl_x_mode has been interrupted use the text from
5190 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5191 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5192 * the same line as the cursor then fix it (the line has been split because it
5193 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5194 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005195 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005196 static void
5197ins_compl_continue_search(char_u *line)
5198{
5199 // it is a continued search
5200 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005201 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5202 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005203 {
5204 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5205 {
5206 // line (probably) wrapped, set compl_startpos to the
5207 // first non_blank in the line, if it is not a wordchar
5208 // include it to get a better pattern, but then we don't
5209 // want the "\\<" prefix, check it below
5210 compl_col = (colnr_T)getwhitecols(line);
5211 compl_startpos.col = compl_col;
5212 compl_startpos.lnum = curwin->w_cursor.lnum;
5213 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5214 }
5215 else
5216 {
5217 // S_IPOS was set when we inserted a word that was at the
5218 // beginning of the line, which means that we'll go to SOL
5219 // mode but first we need to redefine compl_startpos
5220 if (compl_cont_status & CONT_S_IPOS)
5221 {
5222 compl_cont_status |= CONT_SOL;
5223 compl_startpos.col = (colnr_T)(skipwhite(
5224 line + compl_length
5225 + compl_startpos.col) - line);
5226 }
5227 compl_col = compl_startpos.col;
5228 }
5229 compl_length = curwin->w_cursor.col - (int)compl_col;
5230 // IObuff is used to add a "word from the next line" would we
5231 // have enough space? just being paranoid
5232#define MIN_SPACE 75
5233 if (compl_length > (IOSIZE - MIN_SPACE))
5234 {
5235 compl_cont_status &= ~CONT_SOL;
5236 compl_length = (IOSIZE - MIN_SPACE);
5237 compl_col = curwin->w_cursor.col - compl_length;
5238 }
5239 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5240 if (compl_length < 1)
5241 compl_cont_status &= CONT_LOCAL;
5242 }
5243 else if (ctrl_x_mode_line_or_eval())
5244 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5245 else
5246 compl_cont_status = 0;
5247}
5248
5249/*
5250 * start insert mode completion
5251 */
5252 static int
5253ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005254{
5255 char_u *line;
5256 int startcol = 0; // column where searched text starts
5257 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005258 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005259 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005260 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005261
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005262 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005263
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005264 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005265 did_si = FALSE;
5266 can_si = FALSE;
5267 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005268 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005269 return FAIL;
5270
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005271 line = ml_get(curwin->w_cursor.lnum);
5272 curs_col = curwin->w_cursor.col;
5273 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005274
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005275 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5276 && compl_cont_mode == ctrl_x_mode)
5277 // this same ctrl-x_mode was interrupted previously. Continue the
5278 // completion.
5279 ins_compl_continue_search(line);
5280 else
5281 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005282
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005283 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005284 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005285 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005286 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005287 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5288 compl_cont_status = 0;
5289 compl_cont_status |= CONT_N_ADDS;
5290 compl_startpos = curwin->w_cursor;
5291 startcol = (int)curs_col;
5292 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005293 }
5294
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005295 // Work out completion pattern and original text -- webb
5296 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5297 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005298 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5299 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005300 // restore did_ai, so that adding comment leader works
5301 did_ai = save_did_ai;
5302 return FAIL;
5303 }
5304 // If "line" was changed while getting completion info get it again.
5305 if (line_invalid)
5306 line = ml_get(curwin->w_cursor.lnum);
5307
glepnir7cfe6932024-09-15 20:06:28 +02005308 int in_fuzzy = get_cot_flags() & COT_FUZZY;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005309 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005310 {
5311 edit_submode_pre = (char_u *)_(" Adding");
5312 if (ctrl_x_mode_line_or_eval())
5313 {
5314 // Insert a new line, keep indentation but ignore 'comments'.
5315 char_u *old = curbuf->b_p_com;
5316
5317 curbuf->b_p_com = (char_u *)"";
5318 compl_startpos.lnum = curwin->w_cursor.lnum;
5319 compl_startpos.col = compl_col;
5320 ins_eol('\r');
5321 curbuf->b_p_com = old;
5322 compl_length = 0;
5323 compl_col = curwin->w_cursor.col;
5324 }
glepnir7cfe6932024-09-15 20:06:28 +02005325 else if (ctrl_x_mode_normal() && in_fuzzy)
5326 {
5327 compl_startpos = curwin->w_cursor;
5328 compl_cont_status &= CONT_S_IPOS;
5329 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005330 }
5331 else
5332 {
5333 edit_submode_pre = NULL;
5334 compl_startpos.col = compl_col;
5335 }
5336
5337 if (compl_cont_status & CONT_LOCAL)
5338 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5339 else
5340 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5341
5342 // If any of the original typed text has been changed we need to fix
5343 // the redo buffer.
5344 ins_compl_fixRedoBufForLeader(NULL);
5345
5346 // Always add completion for the original text.
John Marriott5e6ea922024-11-23 14:01:57 +01005347 VIM_CLEAR_STRING(compl_orig_text);
5348 compl_orig_text.length = (size_t)compl_length;
5349 compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005350 if (p_ic)
5351 flags |= CP_ICASE;
John Marriott5e6ea922024-11-23 14:01:57 +01005352 if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string,
5353 (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005354 {
John Marriott5e6ea922024-11-23 14:01:57 +01005355 VIM_CLEAR_STRING(compl_pattern);
5356 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005357 return FAIL;
5358 }
5359
5360 // showmode might reset the internal line pointers, so it must
5361 // be called before line = ml_get(), or when this address is no
5362 // longer needed. -- Acevedo.
5363 edit_submode_extra = (char_u *)_("-- Searching...");
5364 edit_submode_highl = HLF_COUNT;
5365 showmode();
5366 edit_submode_extra = NULL;
5367 out_flush();
5368
5369 return OK;
5370}
5371
5372/*
5373 * display the completion status message
5374 */
5375 static void
5376ins_compl_show_statusmsg(void)
5377{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005378 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005379 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005380 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005381 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005382 ? (char_u *)_("Hit end of paragraph")
5383 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005384 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005385 }
5386
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005387 if (edit_submode_extra == NULL)
5388 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005389 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005390 {
5391 edit_submode_extra = (char_u *)_("Back at original");
5392 edit_submode_highl = HLF_W;
5393 }
5394 else if (compl_cont_status & CONT_S_IPOS)
5395 {
5396 edit_submode_extra = (char_u *)_("Word from other line");
5397 edit_submode_highl = HLF_COUNT;
5398 }
5399 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5400 {
5401 edit_submode_extra = (char_u *)_("The only match");
5402 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005403 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005404 }
5405 else
5406 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005407#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005408 // Update completion sequence number when needed.
5409 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005410 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005411#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005412 // The match should always have a sequence number now, this is
5413 // just a safety check.
5414 if (compl_curr_match->cp_number != -1)
5415 {
5416 // Space for 10 text chars. + 2x10-digit no.s = 31.
5417 // Translations may need more than twice that.
5418 static char_u match_ref[81];
5419
5420 if (compl_matches > 0)
5421 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005422 _("match %d of %d"),
5423 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005424 else
5425 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005426 _("match %d"),
5427 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005428 edit_submode_extra = match_ref;
5429 edit_submode_highl = HLF_R;
5430 if (dollar_vcol >= 0)
5431 curs_columns(FALSE);
5432 }
5433 }
5434 }
5435
5436 // Show a message about what (completion) mode we're in.
5437 if (!compl_opt_suppress_empty)
5438 {
5439 showmode();
5440 if (!shortmess(SHM_COMPLETIONMENU))
5441 {
5442 if (edit_submode_extra != NULL)
5443 {
5444 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005445 {
5446 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005447 msg_attr((char *)edit_submode_extra,
5448 edit_submode_highl < HLF_COUNT
5449 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005450 msg_hist_off = FALSE;
5451 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005452 }
5453 else
5454 msg_clr_cmdline(); // necessary for "noshowmode"
5455 }
5456 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005457}
5458
5459/*
5460 * Do Insert mode completion.
5461 * Called when character "c" was typed, which has a meaning for completion.
5462 * Returns OK if completion was done, FAIL if something failed (out of mem).
5463 */
5464 int
5465ins_complete(int c, int enable_pum)
5466{
5467 int n;
5468 int save_w_wrow;
5469 int save_w_leftcol;
5470 int insert_match;
5471
5472 compl_direction = ins_compl_key2dir(c);
5473 insert_match = ins_compl_use_match(c);
5474
5475 if (!compl_started)
5476 {
5477 if (ins_compl_start() == FAIL)
5478 return FAIL;
5479 }
5480 else if (insert_match && stop_arrow() == FAIL)
5481 return FAIL;
5482
5483 compl_shown_match = compl_curr_match;
5484 compl_shows_dir = compl_direction;
5485
5486 // Find next match (and following matches).
5487 save_w_wrow = curwin->w_wrow;
5488 save_w_leftcol = curwin->w_leftcol;
5489 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5490
5491 // may undisplay the popup menu
5492 ins_compl_upd_pum();
5493
5494 if (n > 1) // all matches have been found
5495 compl_matches = n;
5496 compl_curr_match = compl_shown_match;
5497 compl_direction = compl_shows_dir;
5498
5499 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5500 // mode.
5501 if (got_int && !global_busy)
5502 {
5503 (void)vgetc();
5504 got_int = FALSE;
5505 }
5506
5507 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005508 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005509 {
5510 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5511 // because we couldn't expand anything at first place, but if we used
5512 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5513 // (such as M in M'exico) if not tried already. -- Acevedo
5514 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005515 || compl_status_adding()
5516 || (ctrl_x_mode_not_default()
5517 && !ctrl_x_mode_path_patterns()
5518 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005519 compl_cont_status &= ~CONT_N_ADDS;
5520 }
5521
5522 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5523 compl_cont_status |= CONT_S_IPOS;
5524 else
5525 compl_cont_status &= ~CONT_S_IPOS;
5526
5527 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005528
5529 // Show the popup menu, unless we got interrupted.
5530 if (enable_pum && !compl_interrupted)
5531 show_pum(save_w_wrow, save_w_leftcol);
5532
5533 compl_was_interrupted = compl_interrupted;
5534 compl_interrupted = FALSE;
5535
5536 return OK;
5537}
5538
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005539/*
5540 * Remove (if needed) and show the popup menu
5541 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005542 static void
5543show_pum(int prev_w_wrow, int prev_w_leftcol)
5544{
5545 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005546 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005547 RedrawingDisabled = 0;
5548
5549 // If the cursor moved or the display scrolled we need to remove the pum
5550 // first.
5551 setcursor();
5552 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5553 ins_compl_del_pum();
5554
5555 ins_compl_show_pum();
5556 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005557
5558 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005559}
5560
5561/*
5562 * Looks in the first "len" chars. of "src" for search-metachars.
5563 * If dest is not NULL the chars. are copied there quoting (with
5564 * a backslash) the metachars, and dest would be NUL terminated.
5565 * Returns the length (needed) of dest
5566 */
5567 static unsigned
5568quote_meta(char_u *dest, char_u *src, int len)
5569{
5570 unsigned m = (unsigned)len + 1; // one extra for the NUL
5571
5572 for ( ; --len >= 0; src++)
5573 {
5574 switch (*src)
5575 {
5576 case '.':
5577 case '*':
5578 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005579 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005580 break;
5581 // FALLTHROUGH
5582 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005583 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005584 break;
5585 // FALLTHROUGH
5586 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005587 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005588 break;
5589 // FALLTHROUGH
5590 case '^': // currently it's not needed.
5591 case '$':
5592 m++;
5593 if (dest != NULL)
5594 *dest++ = '\\';
5595 break;
5596 }
5597 if (dest != NULL)
5598 *dest++ = *src;
5599 // Copy remaining bytes of a multibyte character.
5600 if (has_mbyte)
5601 {
5602 int i, mb_len;
5603
5604 mb_len = (*mb_ptr2len)(src) - 1;
5605 if (mb_len > 0 && len >= mb_len)
5606 for (i = 0; i < mb_len; ++i)
5607 {
5608 --len;
5609 ++src;
5610 if (dest != NULL)
5611 *dest++ = *src;
5612 }
5613 }
5614 }
5615 if (dest != NULL)
5616 *dest = NUL;
5617
5618 return m;
5619}
5620
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005621#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005622 void
5623free_insexpand_stuff(void)
5624{
John Marriott5e6ea922024-11-23 14:01:57 +01005625 VIM_CLEAR_STRING(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005626# ifdef FEAT_EVAL
5627 free_callback(&cfu_cb);
5628 free_callback(&ofu_cb);
5629 free_callback(&tsrfu_cb);
5630# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005631}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005632#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005633
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005634#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005635/*
5636 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5637 * spelled word, if there is one.
5638 */
5639 static void
5640spell_back_to_badword(void)
5641{
5642 pos_T tpos = curwin->w_cursor;
5643
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005644 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005645 if (curwin->w_cursor.col != tpos.col)
5646 start_arrow(&tpos);
5647}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005648#endif