blob: 74be94cf4b9149543158336f0bd5df8ac1fd429e [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)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 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/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
glepnira218cc62024-06-03 19:32:39 +0200116 int cp_score; // fuzzy match score
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100117};
118
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200119// values for cp_flags
120# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
121# define CP_FREE_FNAME 2 // cp_fname is allocated
122# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
123# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
124# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200125# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127/*
128 * All the current matches are stored in a list.
129 * "compl_first_match" points to the start of the list.
130 * "compl_curr_match" points to the currently selected entry.
131 * "compl_shown_match" is different from compl_curr_match during
132 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000133 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100134 */
135static compl_T *compl_first_match = NULL;
136static compl_T *compl_curr_match = NULL;
137static compl_T *compl_shown_match = NULL;
138static compl_T *compl_old_match = NULL;
139
140// After using a cursor key <Enter> selects a match in the popup menu,
141// otherwise it inserts a line break.
142static int compl_enter_selects = FALSE;
143
144// When "compl_leader" is not NULL only matches that start with this string
145// are used.
146static char_u *compl_leader = NULL;
147
148static int compl_get_longest = FALSE; // put longest common string
149 // in compl_leader
150
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100151// Selected one of the matches. When FALSE the match was edited or using the
152// longest common string.
153static int compl_used_match;
154
155// didn't finish finding completions.
156static int compl_was_interrupted = FALSE;
157
158// Set when character typed while looking for matches and it means we should
159// stop looking for matches.
160static int compl_interrupted = FALSE;
161
162static int compl_restarting = FALSE; // don't insert match
163
164// When the first completion is done "compl_started" is set. When it's
165// FALSE the word to be completed must be located.
166static int compl_started = FALSE;
167
168// Which Ctrl-X mode are we in?
169static int ctrl_x_mode = CTRL_X_NORMAL;
170
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000171static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100172static char_u *compl_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200173static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100174static int compl_direction = FORWARD;
175static int compl_shows_dir = FORWARD;
176static int compl_pending = 0; // > 1 for postponed CTRL-N
177static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000178// Length in bytes of the text being completed (this is deleted to be replaced
179// by the match.)
180static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100181static colnr_T compl_col = 0; // column where the text starts
182 // that is being completed
183static char_u *compl_orig_text = NULL; // text as it was before
184 // completion started
185static int compl_cont_mode = 0;
186static expand_T compl_xp;
187
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000188// List of flags for method of completion.
189static int compl_cont_status = 0;
190# define CONT_ADDING 1 // "normal" or "adding" expansion
191# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
192 // it's set only iff N_ADDS is set
193# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
194# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
195 // if so, word-wise-expansion will set SOL
196# define CONT_SOL 16 // pattern includes start of line, just for
197 // word-wise expansion, not set for ^X^L
198# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
199 // expansion, (eg use complete=.)
200
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100201static int compl_opt_refresh_always = FALSE;
202static int compl_opt_suppress_empty = FALSE;
203
glepnira218cc62024-06-03 19:32:39 +0200204static int compl_selected_item = -1;
205
Bram Moolenaar08928322020-01-04 14:32:48 +0100206static 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);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100207static void ins_compl_longest_match(compl_T *match);
208static void ins_compl_del_pum(void);
209static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
210static char_u *find_line_end(char_u *ptr);
211static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100212static int ins_compl_need_restart(void);
213static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000214static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static void ins_compl_restart(void);
216static void ins_compl_set_original_text(char_u *str);
217static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
218# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
219static void ins_compl_add_list(list_T *list);
220static void ins_compl_add_dict(dict_T *dict);
221# endif
222static int ins_compl_key2dir(int c);
223static int ins_compl_pum_key(int c);
224static int ins_compl_key2count(int c);
225static void show_pum(int prev_w_wrow, int prev_w_leftcol);
226static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100227
228#ifdef FEAT_SPELL
229static void spell_back_to_badword(void);
230static int spell_bad_len = 0; // length of located bad word
231#endif
232
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100233/*
234 * CTRL-X pressed in Insert mode.
235 */
236 void
237ins_ctrl_x(void)
238{
zeertzjqdca29d92021-08-31 19:12:51 +0200239 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100240 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000241 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100242 if (compl_cont_status & CONT_N_ADDS)
243 compl_cont_status |= CONT_INTRPT;
244 else
245 compl_cont_status = 0;
246 // We're not sure which CTRL-X mode it will be yet
247 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
248 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
249 edit_submode_pre = NULL;
250 showmode();
251 }
zeertzjqdca29d92021-08-31 19:12:51 +0200252 else
253 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
254 // CTRL-V look like CTRL-N
255 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100256
LemonBoy2bf52dd2022-04-09 18:17:34 +0100257 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100258}
259
260/*
261 * Functions to check the current CTRL-X mode.
262 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000263int ctrl_x_mode_none(void)
264 { return ctrl_x_mode == 0; }
265int ctrl_x_mode_normal(void)
266 { return ctrl_x_mode == CTRL_X_NORMAL; }
267int ctrl_x_mode_scroll(void)
268 { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void)
270 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
271int ctrl_x_mode_files(void)
272 { return ctrl_x_mode == CTRL_X_FILES; }
273int ctrl_x_mode_tags(void)
274 { return ctrl_x_mode == CTRL_X_TAGS; }
275int ctrl_x_mode_path_patterns(void)
276 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
277int ctrl_x_mode_path_defines(void)
278 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
279int ctrl_x_mode_dictionary(void)
280 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
281int ctrl_x_mode_thesaurus(void)
282 { return ctrl_x_mode == CTRL_X_THESAURUS; }
283int ctrl_x_mode_cmdline(void)
284 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200285 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000286int ctrl_x_mode_function(void)
287 { return ctrl_x_mode == CTRL_X_FUNCTION; }
288int ctrl_x_mode_omni(void)
289 { return ctrl_x_mode == CTRL_X_OMNI; }
290int ctrl_x_mode_spell(void)
291 { return ctrl_x_mode == CTRL_X_SPELL; }
292static int ctrl_x_mode_eval(void)
293 { return ctrl_x_mode == CTRL_X_EVAL; }
294int ctrl_x_mode_line_or_eval(void)
295 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100296
297/*
298 * Whether other than default completion has been selected.
299 */
300 int
301ctrl_x_mode_not_default(void)
302{
303 return ctrl_x_mode != CTRL_X_NORMAL;
304}
305
306/*
zeertzjqdca29d92021-08-31 19:12:51 +0200307 * Whether CTRL-X was typed without a following character,
308 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100309 */
310 int
311ctrl_x_mode_not_defined_yet(void)
312{
313 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
314}
315
316/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000317 * Return TRUE if currently in "normal" or "adding" insert completion matches
318 * state
319 */
320 int
321compl_status_adding(void)
322{
323 return compl_cont_status & CONT_ADDING;
324}
325
326/*
327 * Return TRUE if the completion pattern includes start of line, just for
328 * word-wise expansion.
329 */
330 int
331compl_status_sol(void)
332{
333 return compl_cont_status & CONT_SOL;
334}
335
336/*
337 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
338 */
339 int
340compl_status_local(void)
341{
342 return compl_cont_status & CONT_LOCAL;
343}
344
345/*
346 * Clear the completion status flags
347 */
348 void
349compl_status_clear(void)
350{
351 compl_cont_status = 0;
352}
353
354/*
355 * Return TRUE if completion is using the forward direction matches
356 */
357 static int
358compl_dir_forward(void)
359{
360 return compl_direction == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing forward completion matches
365 */
366 static int
367compl_shows_dir_forward(void)
368{
369 return compl_shows_dir == FORWARD;
370}
371
372/*
373 * Return TRUE if currently showing backward completion matches
374 */
375 static int
376compl_shows_dir_backward(void)
377{
378 return compl_shows_dir == BACKWARD;
379}
380
381/*
382 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100383 */
384 int
385has_compl_option(int dict_opt)
386{
387 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200388#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100389 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200390#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100392 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
393#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100394 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100395#endif
396 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100397 {
398 ctrl_x_mode = CTRL_X_NORMAL;
399 edit_submode = NULL;
400 msg_attr(dict_opt ? _("'dictionary' option is empty")
401 : _("'thesaurus' option is empty"),
402 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100403 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100404 {
405 vim_beep(BO_COMPL);
406 setcursor();
407 out_flush();
408#ifdef FEAT_EVAL
409 if (!get_vim_var_nr(VV_TESTING))
410#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100411 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100412 }
413 return FALSE;
414 }
415 return TRUE;
416}
417
418/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000419 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100420 * This depends on the current mode.
421 */
422 int
423vim_is_ctrl_x_key(int c)
424{
425 // Always allow ^R - let its results then be checked
426 if (c == Ctrl_R)
427 return TRUE;
428
429 // Accept <PageUp> and <PageDown> if the popup menu is visible.
430 if (ins_compl_pum_key(c))
431 return TRUE;
432
433 switch (ctrl_x_mode)
434 {
435 case 0: // Not in any CTRL-X mode
436 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
437 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200438 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100439 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
440 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
441 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
442 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
443 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200444 || c == Ctrl_S || c == Ctrl_K || c == 's'
445 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100446 case CTRL_X_SCROLL:
447 return (c == Ctrl_Y || c == Ctrl_E);
448 case CTRL_X_WHOLE_LINE:
449 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
450 case CTRL_X_FILES:
451 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_DICTIONARY:
453 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_THESAURUS:
455 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_TAGS:
457 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
458#ifdef FEAT_FIND_ID
459 case CTRL_X_PATH_PATTERNS:
460 return (c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_PATH_DEFINES:
462 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_CMDLINE:
465 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
466 || c == Ctrl_X);
467#ifdef FEAT_COMPL_FUNC
468 case CTRL_X_FUNCTION:
469 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
470 case CTRL_X_OMNI:
471 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
472#endif
473 case CTRL_X_SPELL:
474 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
475 case CTRL_X_EVAL:
476 return (c == Ctrl_P || c == Ctrl_N);
477 }
478 internal_error("vim_is_ctrl_x_key()");
479 return FALSE;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000484 */
485 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000486match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000487{
488 return match->cp_flags & CP_ORIGINAL_TEXT;
489}
490
491/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000492 * Returns TRUE if "match" is the first match in the completion list.
493 */
494 static int
495is_first_match(compl_T *match)
496{
497 return match == compl_first_match;
498}
499
500/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100501 * Return TRUE when character "c" is part of the item currently being
502 * completed. Used to decide whether to abandon complete mode when the menu
503 * is visible.
504 */
505 int
506ins_compl_accept_char(int c)
507{
508 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
509 // When expanding an identifier only accept identifier chars.
510 return vim_isIDc(c);
511
512 switch (ctrl_x_mode)
513 {
514 case CTRL_X_FILES:
515 // When expanding file name only accept file name chars. But not
516 // path separators, so that "proto/<Tab>" expands files in
517 // "proto", not "proto/" as a whole
518 return vim_isfilec(c) && !vim_ispathsep(c);
519
520 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200521 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100522 case CTRL_X_OMNI:
523 // Command line and Omni completion can work with just about any
524 // printable character, but do stop at white space.
525 return vim_isprintc(c) && !VIM_ISWHITE(c);
526
527 case CTRL_X_WHOLE_LINE:
528 // For while line completion a space can be part of the line.
529 return vim_isprintc(c);
530 }
531 return vim_iswordc(c);
532}
533
534/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000535 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100536 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000537 */
538 static char_u *
539ins_compl_infercase_gettext(
540 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100541 int char_len,
542 int compl_char_len,
543 int min_len,
544 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000545{
546 int *wca; // Wide character array.
547 char_u *p;
548 int i, c;
549 int has_lower = FALSE;
550 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100551 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000552
553 IObuff[0] = NUL;
554
555 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100556 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000557 if (wca == NULL)
558 return IObuff;
559
560 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100561 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000562 if (has_mbyte)
563 wca[i] = mb_ptr2char_adv(&p);
564 else
565 wca[i] = *(p++);
566
567 // Rule 1: Were any chars converted to lower?
568 p = compl_orig_text;
569 for (i = 0; i < min_len; ++i)
570 {
571 if (has_mbyte)
572 c = mb_ptr2char_adv(&p);
573 else
574 c = *(p++);
575 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 {
592 p = compl_orig_text;
593 for (i = 0; i < min_len; ++i)
594 {
595 if (has_mbyte)
596 c = mb_ptr2char_adv(&p);
597 else
598 c = *(p++);
599 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
600 {
601 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100602 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000603 wca[i] = MB_TOUPPER(wca[i]);
604 break;
605 }
606 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
607 }
608 }
609
610 // Copy the original case of the part we typed.
611 p = compl_orig_text;
612 for (i = 0; i < min_len; ++i)
613 {
614 if (has_mbyte)
615 c = mb_ptr2char_adv(&p);
616 else
617 c = *(p++);
618 if (MB_ISLOWER(c))
619 wca[i] = MB_TOLOWER(wca[i]);
620 else if (MB_ISUPPER(c))
621 wca[i] = MB_TOUPPER(wca[i]);
622 }
623
624 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000625 p = IObuff;
626 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100627 ga_init2(&gap, 1, 500);
628 while (i < char_len)
629 {
630 if (gap.ga_data != NULL)
631 {
632 if (ga_grow(&gap, 10) == FAIL)
633 {
634 ga_clear(&gap);
635 return (char_u *)"[failed]";
636 }
637 p = (char_u *)gap.ga_data + gap.ga_len;
638 if (has_mbyte)
639 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
640 else
641 {
642 *p = wca[i++];
643 ++gap.ga_len;
644 }
645 }
646 else if ((p - IObuff) + 6 >= IOSIZE)
647 {
648 // Multi-byte characters can occupy up to five bytes more than
649 // ASCII characters, and we also need one byte for NUL, so when
650 // getting to six bytes from the edge of IObuff switch to using a
651 // growarray. Add the character in the next round.
652 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100653 {
654 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100655 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100656 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100657 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100658 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100659 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 }
661 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000662 p += (*mb_char2bytes)(wca[i++], p);
663 else
664 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100665 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000666 vim_free(wca);
667
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100668 if (gap.ga_data != NULL)
669 {
670 *tofree = gap.ga_data;
671 return gap.ga_data;
672 }
673
674 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000675 return IObuff;
676}
677
678/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
680 * case of the originally typed text is used, and the case of the completed
681 * text is inferred, ie this tries to work out what case you probably wanted
682 * the rest of the word to be in -- webb
683 */
684 int
685ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200686 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687 int len,
688 int icase,
689 char_u *fname,
690 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200691 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200693 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100694 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100695 int char_len; // count multi-byte characters
696 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100697 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200698 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100699 int res;
700 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701
702 if (p_ic && curbuf->b_p_inf && len > 0)
703 {
704 // Infer case of completed part.
705
706 // Find actual length of completion.
707 if (has_mbyte)
708 {
709 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100710 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100711 while (*p != NUL)
712 {
713 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 }
716 }
717 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719
720 // Find actual length of original text.
721 if (has_mbyte)
722 {
723 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 while (*p != NUL)
726 {
727 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100728 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729 }
730 }
731 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100734 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100738 str = ins_compl_infercase_gettext(str, char_len,
739 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100740 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200741 if (cont_s_ipos)
742 flags |= CP_CONT_S_IPOS;
743 if (icase)
744 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200745
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100746 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
747 vim_free(tofree);
748 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100749}
750
751/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000752 * Add a match to the list of matches. The arguments are:
753 * str - text of the match to add
754 * len - length of "str". If -1, then the length of "str" is
755 * computed.
756 * fname - file name to associate with this match.
757 * cptext - list of strings to use with this match (for abbr, menu, info
758 * and kind)
759 * user_data - user supplied data (any vim type) for this match
760 * cdir - match direction. If 0, use "compl_direction".
761 * flags_arg - match flags (cp_flags)
762 * adup - accept this match even if it is already present.
763 * If "cdir" is FORWARD, then the match is added after the current match.
764 * Otherwise, it is added before the current match.
765 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100766 * If the given string is already in the list of completions, then return
767 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
768 * maybe because alloc() returns NULL, then FAIL is returned.
769 */
770 static int
771ins_compl_add(
772 char_u *str,
773 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 char_u **cptext, // extra text for popup menu or NULL
776 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200778 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100779 int adup) // accept duplicate match
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)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100801 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100802 && ((int)STRLEN(match->cp_str) <= len
803 || match->cp_str[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;
820 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
821 {
822 vim_free(match);
823 return FAIL;
824 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100825
826 // match-fname is:
827 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200828 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 // - NULL otherwise. --Acevedo
830 if (fname != NULL
831 && compl_curr_match != NULL
832 && compl_curr_match->cp_fname != NULL
833 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
834 match->cp_fname = compl_curr_match->cp_fname;
835 else if (fname != NULL)
836 {
837 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200838 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100839 }
840 else
841 match->cp_fname = NULL;
842 match->cp_flags = flags;
843
844 if (cptext != NULL)
845 {
846 int i;
847
848 for (i = 0; i < CPT_COUNT; ++i)
849 if (cptext[i] != NULL && *cptext[i] != NUL)
850 match->cp_text[i] = vim_strsave(cptext[i]);
851 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100852#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100853 if (user_data != NULL)
854 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000857 // Link the new match structure after (FORWARD) or before (BACKWARD) the
858 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 if (compl_first_match == NULL)
860 match->cp_next = match->cp_prev = NULL;
861 else if (dir == FORWARD)
862 {
863 match->cp_next = compl_curr_match->cp_next;
864 match->cp_prev = compl_curr_match;
865 }
866 else // BACKWARD
867 {
868 match->cp_next = compl_curr_match;
869 match->cp_prev = compl_curr_match->cp_prev;
870 }
871 if (match->cp_next)
872 match->cp_next->cp_prev = match;
873 if (match->cp_prev)
874 match->cp_prev->cp_next = match;
875 else // if there's nothing before, it is the first match
876 compl_first_match = match;
877 compl_curr_match = match;
878
879 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200880 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100881 ins_compl_longest_match(match);
882
883 return OK;
884}
885
886/*
887 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200888 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 */
890 static int
891ins_compl_equal(compl_T *match, char_u *str, int len)
892{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200893 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200894 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100896 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
897 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
898}
899
900/*
901 * Reduce the longest common string for match "match".
902 */
903 static void
904ins_compl_longest_match(compl_T *match)
905{
906 char_u *p, *s;
907 int c1, c2;
908 int had_match;
909
910 if (compl_leader == NULL)
911 {
912 // First match, use it as a whole.
913 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000914 if (compl_leader == NULL)
915 return;
916
917 had_match = (curwin->w_cursor.col > compl_col);
918 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000919 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000920 ins_redraw(FALSE);
921
922 // When the match isn't there (to avoid matching itself) remove it
923 // again after redrawing.
924 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100926 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000927
928 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000930
931 // Reduce the text if this match differs from compl_leader.
932 p = compl_leader;
933 s = match->cp_str;
934 while (*p != NUL)
935 {
936 if (has_mbyte)
937 {
938 c1 = mb_ptr2char(p);
939 c2 = mb_ptr2char(s);
940 }
941 else
942 {
943 c1 = *p;
944 c2 = *s;
945 }
946 if ((match->cp_flags & CP_ICASE)
947 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
948 break;
949 if (has_mbyte)
950 {
951 MB_PTR_ADV(p);
952 MB_PTR_ADV(s);
953 }
954 else
955 {
956 ++p;
957 ++s;
958 }
959 }
960
961 if (*p != NUL)
962 {
963 // Leader was shortened, need to change the inserted text.
964 *p = NUL;
965 had_match = (curwin->w_cursor.col > compl_col);
966 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000967 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000968 ins_redraw(FALSE);
969
970 // When the match isn't there (to avoid matching itself) remove it
971 // again after redrawing.
972 if (!had_match)
973 ins_compl_delete();
974 }
975
976 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100977}
978
979/*
980 * Add an array of matches to the list of matches.
981 * Frees matches[].
982 */
983 static void
984ins_compl_add_matches(
985 int num_matches,
986 char_u **matches,
987 int icase)
988{
989 int i;
990 int add_r = OK;
991 int dir = compl_direction;
992
993 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100994 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200995 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100996 // if dir was BACKWARD then honor it just once
997 dir = FORWARD;
998 FreeWild(num_matches, matches);
999}
1000
1001/*
1002 * Make the completion list cyclic.
1003 * Return the number of matches (excluding the original).
1004 */
1005 static int
1006ins_compl_make_cyclic(void)
1007{
1008 compl_T *match;
1009 int count = 0;
1010
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001011 if (compl_first_match == NULL)
1012 return 0;
1013
1014 // Find the end of the list.
1015 match = compl_first_match;
1016 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001017 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001018 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001019 match = match->cp_next;
1020 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001021 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001022 match->cp_next = compl_first_match;
1023 compl_first_match->cp_prev = match;
1024
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001025 return count;
1026}
1027
1028/*
1029 * Return whether there currently is a shown match.
1030 */
1031 int
1032ins_compl_has_shown_match(void)
1033{
1034 return compl_shown_match == NULL
1035 || compl_shown_match != compl_shown_match->cp_next;
1036}
1037
1038/*
1039 * Return whether the shown match is long enough.
1040 */
1041 int
1042ins_compl_long_shown_match(void)
1043{
1044 return (int)STRLEN(compl_shown_match->cp_str)
1045 > curwin->w_cursor.col - compl_col;
1046}
1047
1048/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001049 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001050 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001051 unsigned int
1052get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001053{
zeertzjq529b9ad2024-06-05 20:27:06 +02001054 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001055}
1056
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057
1058// "compl_match_array" points the currently displayed list of entries in the
1059// popup menu. It is NULL when there is no popup menu.
1060static pumitem_T *compl_match_array = NULL;
1061static int compl_match_arraysize;
1062
1063/*
1064 * Update the screen and when there is any scrolling remove the popup menu.
1065 */
1066 static void
1067ins_compl_upd_pum(void)
1068{
1069 int h;
1070
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001071 if (compl_match_array == NULL)
1072 return;
1073
1074 h = curwin->w_cline_height;
1075 // Update the screen later, before drawing the popup menu over it.
1076 pum_call_update_screen();
1077 if (h != curwin->w_cline_height)
1078 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001079}
1080
1081/*
1082 * Remove any popup menu.
1083 */
1084 static void
1085ins_compl_del_pum(void)
1086{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001087 if (compl_match_array == NULL)
1088 return;
1089
1090 pum_undisplay();
1091 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092}
1093
1094/*
1095 * Return TRUE if the popup menu should be displayed.
1096 */
1097 int
1098pum_wanted(void)
1099{
1100 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001101 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001102 return FALSE;
1103
1104 // The display looks bad on a B&W display.
1105 if (t_colors < 8
1106#ifdef FEAT_GUI
1107 && !gui.in_use
1108#endif
1109 )
1110 return FALSE;
1111 return TRUE;
1112}
1113
1114/*
1115 * Return TRUE if there are two or more matches to be shown in the popup menu.
1116 * One if 'completopt' contains "menuone".
1117 */
1118 static int
1119pum_enough_matches(void)
1120{
1121 compl_T *compl;
1122 int i;
1123
1124 // Don't display the popup menu if there are no matches or there is only
1125 // one (ignoring the original text).
1126 compl = compl_first_match;
1127 i = 0;
1128 do
1129 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001130 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001131 break;
1132 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001133 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001134
zeertzjq529b9ad2024-06-05 20:27:06 +02001135 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001136 return (i >= 1);
1137 return (i >= 2);
1138}
1139
Bram Moolenaar3075a452021-11-17 15:51:52 +00001140#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001141/*
1142 * Allocate Dict for the completed item.
1143 * { word, abbr, menu, kind, info }
1144 */
1145 static dict_T *
1146ins_compl_dict_alloc(compl_T *match)
1147{
1148 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1149
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001150 if (dict == NULL)
1151 return NULL;
1152
1153 dict_add_string(dict, "word", match->cp_str);
1154 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1155 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1156 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1157 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1158 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1159 dict_add_string(dict, "user_data", (char_u *)"");
1160 else
1161 dict_add_tv(dict, "user_data", &match->cp_user_data);
1162
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001163 return dict;
1164}
1165
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001166/*
1167 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1168 * completion menu is changed.
1169 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001170 static void
1171trigger_complete_changed_event(int cur)
1172{
1173 dict_T *v_event;
1174 dict_T *item;
1175 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001176 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001177
1178 if (recursive)
1179 return;
1180
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001181 if (cur < 0)
1182 item = dict_alloc();
1183 else
1184 item = ins_compl_dict_alloc(compl_curr_match);
1185 if (item == NULL)
1186 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001187 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001188 dict_add_dict(v_event, "completed_item", item);
1189 pum_set_event_info(v_event);
1190 dict_set_items_ro(v_event);
1191
1192 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001193 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001194 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001195 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001196 recursive = FALSE;
1197
Bram Moolenaar3075a452021-11-17 15:51:52 +00001198 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001199}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001200#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001202/*
glepnira218cc62024-06-03 19:32:39 +02001203 * pumitem qsort compare func
1204 */
1205 static int
1206ins_compl_fuzzy_sort(const void *a, const void *b)
1207{
1208 const int sa = (*(pumitem_T *)a).pum_score;
1209 const int sb = (*(pumitem_T *)b).pum_score;
1210 return sa == sb ? 0 : sa < sb ? 1 : -1;
1211}
1212
1213/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001214 * Build a popup menu to show the completion matches.
1215 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1216 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001217 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001218 static int
1219ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001220{
1221 compl_T *compl;
1222 compl_T *shown_compl = NULL;
1223 int did_find_shown_match = FALSE;
1224 int shown_match_ok = FALSE;
1225 int i;
1226 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001227 int lead_len = 0;
glepnira218cc62024-06-03 19:32:39 +02001228 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001229 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001230 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1231 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001232
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001233 // Need to build the popup menu list.
1234 compl_match_arraysize = 0;
1235 compl = compl_first_match;
1236 if (compl_leader != NULL)
1237 lead_len = (int)STRLEN(compl_leader);
1238
1239 do
1240 {
zeertzjq551d8c32024-06-05 19:53:32 +02001241 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1242 // set the cp_score for later comparisons.
glepnira218cc62024-06-03 19:32:39 +02001243 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1244 compl->cp_score = fuzzy_match_str(compl->cp_str, compl_leader);
1245
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001246 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001247 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001248 || ins_compl_equal(compl, compl_leader, lead_len)
1249 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001250 ++compl_match_arraysize;
1251 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001252 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001253
1254 if (compl_match_arraysize == 0)
1255 return -1;
1256
1257 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1258 if (compl_match_array == NULL)
1259 return -1;
1260
1261 // If the current match is the original text don't find the first
1262 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001263 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001264 shown_match_ok = TRUE;
1265
glepnir53387c52024-05-27 15:11:01 +02001266 if (compl_leader != NULL
1267 && STRCMP(compl_leader, compl_orig_text) == 0
1268 && shown_match_ok == FALSE)
1269 compl_shown_match = compl_no_select ? compl_first_match
1270 : compl_first_match->cp_next;
1271
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001272 i = 0;
1273 compl = compl_first_match;
1274 do
1275 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001276 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001277 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001278 || ins_compl_equal(compl, compl_leader, lead_len)
1279 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001280 {
glepnira218cc62024-06-03 19:32:39 +02001281 if (!shown_match_ok && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001282 {
1283 if (compl == compl_shown_match || did_find_shown_match)
1284 {
1285 // This item is the shown match or this is the
1286 // first displayed item after the shown match.
1287 compl_shown_match = compl;
1288 did_find_shown_match = TRUE;
1289 shown_match_ok = TRUE;
1290 }
1291 else
1292 // Remember this displayed match for when the
1293 // shown match is just below it.
1294 shown_compl = compl;
1295 cur = i;
1296 }
glepnira218cc62024-06-03 19:32:39 +02001297 else if (compl_fuzzy_match)
1298 {
glepnirdca57fb2024-06-04 22:01:21 +02001299 // Update the maximum fuzzy score and the shown match
1300 // if the current item's score is higher
glepnira218cc62024-06-03 19:32:39 +02001301 if (compl->cp_score > max_fuzzy_score)
1302 {
1303 did_find_shown_match = TRUE;
1304 max_fuzzy_score = compl->cp_score;
1305 compl_shown_match = compl;
1306 shown_match_ok = TRUE;
1307 }
1308
glepnirdca57fb2024-06-04 22:01:21 +02001309 // If there is no "no select" condition and the max fuzzy
1310 // score is positive, or there is no completion leader or the
1311 // leader length is zero, mark the shown match as valid and
1312 // reset the current index.
glepnira218cc62024-06-03 19:32:39 +02001313 if (!compl_no_select
1314 && (max_fuzzy_score > 0
1315 || (compl_leader == NULL || lead_len == 0)))
1316 {
1317 shown_match_ok = TRUE;
1318 cur = 0;
1319 }
1320 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001321
1322 if (compl->cp_text[CPT_ABBR] != NULL)
1323 compl_match_array[i].pum_text =
1324 compl->cp_text[CPT_ABBR];
1325 else
1326 compl_match_array[i].pum_text = compl->cp_str;
1327 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1328 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
glepnira218cc62024-06-03 19:32:39 +02001329 compl_match_array[i].pum_score = compl->cp_score;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001330 if (compl->cp_text[CPT_MENU] != NULL)
1331 compl_match_array[i++].pum_extra =
1332 compl->cp_text[CPT_MENU];
1333 else
1334 compl_match_array[i++].pum_extra = compl->cp_fname;
1335 }
1336
glepnira218cc62024-06-03 19:32:39 +02001337 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001338 {
1339 did_find_shown_match = TRUE;
1340
1341 // When the original text is the shown match don't set
1342 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001343 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001344 shown_match_ok = TRUE;
1345
1346 if (!shown_match_ok && shown_compl != NULL)
1347 {
1348 // The shown match isn't displayed, set it to the
1349 // previously displayed match.
1350 compl_shown_match = shown_compl;
1351 shown_match_ok = TRUE;
1352 }
1353 }
1354 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001355 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001356
glepnira218cc62024-06-03 19:32:39 +02001357 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1358 // sort by the largest score of fuzzy match
1359 qsort(compl_match_array, (size_t)compl_match_arraysize, sizeof(pumitem_T), ins_compl_fuzzy_sort);
1360
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001361 if (!shown_match_ok) // no displayed match at all
1362 cur = -1;
1363
1364 return cur;
1365}
1366
1367/*
1368 * Show the popup menu for the list of matches.
1369 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1370 */
1371 void
1372ins_compl_show_pum(void)
1373{
1374 int i;
1375 int cur = -1;
1376 colnr_T col;
1377
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001378 if (!pum_wanted() || !pum_enough_matches())
1379 return;
1380
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001381 // Update the screen later, before drawing the popup menu over it.
1382 pum_call_update_screen();
1383
1384 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001385 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001386 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001387 else
1388 {
1389 // popup menu already exists, only need to find the current item.
1390 for (i = 0; i < compl_match_arraysize; ++i)
1391 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1392 || compl_match_array[i].pum_text
1393 == compl_shown_match->cp_text[CPT_ABBR])
1394 {
1395 cur = i;
1396 break;
1397 }
1398 }
1399
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001400 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001401 {
1402#ifdef FEAT_EVAL
1403 if (compl_started && has_completechanged())
1404 trigger_complete_changed_event(cur);
1405#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001406 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001407 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001408
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001409 // In Replace mode when a $ is displayed at the end of the line only
1410 // part of the screen would be updated. We do need to redraw here.
1411 dollar_vcol = -1;
1412
1413 // Compute the screen column of the start of the completed text.
1414 // Use the cursor to get all wrapping and other settings right.
1415 col = curwin->w_cursor.col;
1416 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001417 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001418 pum_display(compl_match_array, compl_match_arraysize, cur);
1419 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001420
glepnircbb46b42024-02-03 18:11:13 +01001421 // After adding leader, set the current match to shown match.
1422 if (compl_started && compl_curr_match != compl_shown_match)
1423 compl_curr_match = compl_shown_match;
1424
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001425#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001426 if (has_completechanged())
1427 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001428#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001429}
1430
1431#define DICT_FIRST (1) // use just first element in "dict"
1432#define DICT_EXACT (2) // "dict" is the exact name of a file
1433
1434/*
glepnir40c1c332024-06-11 19:37:04 +02001435 * Get current completion leader
1436 */
1437 char_u *
1438ins_compl_leader(void)
1439{
1440 return compl_leader;
1441}
1442
1443/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001444 * Add any identifiers that match the given pattern "pat" in the list of
1445 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001446 */
1447 static void
1448ins_compl_dictionaries(
1449 char_u *dict_start,
1450 char_u *pat,
1451 int flags, // DICT_FIRST and/or DICT_EXACT
1452 int thesaurus) // Thesaurus completion
1453{
1454 char_u *dict = dict_start;
1455 char_u *ptr;
1456 char_u *buf;
1457 regmatch_T regmatch;
1458 char_u **files;
1459 int count;
1460 int save_p_scs;
1461 int dir = compl_direction;
1462
1463 if (*dict == NUL)
1464 {
1465#ifdef FEAT_SPELL
1466 // When 'dictionary' is empty and spell checking is enabled use
1467 // "spell".
1468 if (!thesaurus && curwin->w_p_spell)
1469 dict = (char_u *)"spell";
1470 else
1471#endif
1472 return;
1473 }
1474
1475 buf = alloc(LSIZE);
1476 if (buf == NULL)
1477 return;
1478 regmatch.regprog = NULL; // so that we can goto theend
1479
1480 // If 'infercase' is set, don't use 'smartcase' here
1481 save_p_scs = p_scs;
1482 if (curbuf->b_p_inf)
1483 p_scs = FALSE;
1484
1485 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1486 // to only match at the start of a line. Otherwise just match the
1487 // pattern. Also need to double backslashes.
1488 if (ctrl_x_mode_line_or_eval())
1489 {
1490 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1491 size_t len;
1492
1493 if (pat_esc == NULL)
1494 goto theend;
1495 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001496 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001497 if (ptr == NULL)
1498 {
1499 vim_free(pat_esc);
1500 goto theend;
1501 }
1502 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1503 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1504 vim_free(pat_esc);
1505 vim_free(ptr);
1506 }
1507 else
1508 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001509 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001510 if (regmatch.regprog == NULL)
1511 goto theend;
1512 }
1513
1514 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1515 regmatch.rm_ic = ignorecase(pat);
1516 while (*dict != NUL && !got_int && !compl_interrupted)
1517 {
1518 // copy one dictionary file name into buf
1519 if (flags == DICT_EXACT)
1520 {
1521 count = 1;
1522 files = &dict;
1523 }
1524 else
1525 {
1526 // Expand wildcards in the dictionary name, but do not allow
1527 // backticks (for security, the 'dict' option may have been set in
1528 // a modeline).
1529 copy_option_part(&dict, buf, LSIZE, ",");
1530# ifdef FEAT_SPELL
1531 if (!thesaurus && STRCMP(buf, "spell") == 0)
1532 count = -1;
1533 else
1534# endif
1535 if (vim_strchr(buf, '`') != NULL
1536 || expand_wildcards(1, &buf, &count, &files,
1537 EW_FILE|EW_SILENT) != OK)
1538 count = 0;
1539 }
1540
1541# ifdef FEAT_SPELL
1542 if (count == -1)
1543 {
1544 // Complete from active spelling. Skip "\<" in the pattern, we
1545 // don't use it as a RE.
1546 if (pat[0] == '\\' && pat[1] == '<')
1547 ptr = pat + 2;
1548 else
1549 ptr = pat;
1550 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1551 }
1552 else
1553# endif
1554 if (count > 0) // avoid warning for using "files" uninit
1555 {
1556 ins_compl_files(count, files, thesaurus, flags,
1557 &regmatch, buf, &dir);
1558 if (flags != DICT_EXACT)
1559 FreeWild(count, files);
1560 }
1561 if (flags != 0)
1562 break;
1563 }
1564
1565theend:
1566 p_scs = save_p_scs;
1567 vim_regfree(regmatch.regprog);
1568 vim_free(buf);
1569}
1570
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001571/*
1572 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1573 * skipping the word at 'skip_word'. Returns OK on success.
1574 */
1575 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001576thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001577 char_u *fname,
1578 char_u **buf_arg,
1579 int dir,
1580 char_u *skip_word)
1581{
1582 int status = OK;
1583 char_u *ptr;
1584 char_u *wstart;
1585
1586 // Add the other matches on the line
1587 ptr = *buf_arg;
1588 while (!got_int)
1589 {
1590 // Find start of the next word. Skip white
1591 // space and punctuation.
1592 ptr = find_word_start(ptr);
1593 if (*ptr == NUL || *ptr == NL)
1594 break;
1595 wstart = ptr;
1596
1597 // Find end of the word.
1598 if (has_mbyte)
1599 // Japanese words may have characters in
1600 // different classes, only separate words
1601 // with single-byte non-word characters.
1602 while (*ptr != NUL)
1603 {
1604 int l = (*mb_ptr2len)(ptr);
1605
1606 if (l < 2 && !vim_iswordc(*ptr))
1607 break;
1608 ptr += l;
1609 }
1610 else
1611 ptr = find_word_end(ptr);
1612
1613 // Add the word. Skip the regexp match.
1614 if (wstart != skip_word)
1615 {
1616 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1617 fname, dir, FALSE);
1618 if (status == FAIL)
1619 break;
1620 }
1621 }
1622
1623 *buf_arg = ptr;
1624 return status;
1625}
1626
1627/*
1628 * Process "count" dictionary/thesaurus "files" and add the text matching
1629 * "regmatch".
1630 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001631 static void
1632ins_compl_files(
1633 int count,
1634 char_u **files,
1635 int thesaurus,
1636 int flags,
1637 regmatch_T *regmatch,
1638 char_u *buf,
1639 int *dir)
1640{
1641 char_u *ptr;
1642 int i;
1643 FILE *fp;
1644 int add_r;
1645
1646 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1647 {
1648 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001649 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001650 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001651 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001652 vim_snprintf((char *)IObuff, IOSIZE,
1653 _("Scanning dictionary: %s"), (char *)files[i]);
1654 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1655 }
1656
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001657 if (fp == NULL)
1658 continue;
1659
1660 // Read dictionary file line by line.
1661 // Check each line for a match.
1662 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001663 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001664 ptr = buf;
1665 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001666 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001667 ptr = regmatch->startp[0];
1668 if (ctrl_x_mode_line_or_eval())
1669 ptr = find_line_end(ptr);
1670 else
1671 ptr = find_word_end(ptr);
1672 add_r = ins_compl_add_infercase(regmatch->startp[0],
1673 (int)(ptr - regmatch->startp[0]),
1674 p_ic, files[i], *dir, FALSE);
1675 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001676 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001677 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001678 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001679 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001680 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001682 if (add_r == OK)
1683 // if dir was BACKWARD then honor it just once
1684 *dir = FORWARD;
1685 else if (add_r == FAIL)
1686 break;
1687 // avoid expensive call to vim_regexec() when at end
1688 // of line
1689 if (*ptr == '\n' || got_int)
1690 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001691 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001692 line_breakcheck();
1693 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001694 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001695 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001696 }
1697}
1698
1699/*
1700 * Find the start of the next word.
1701 * Returns a pointer to the first char of the word. Also stops at a NUL.
1702 */
1703 char_u *
1704find_word_start(char_u *ptr)
1705{
1706 if (has_mbyte)
1707 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1708 ptr += (*mb_ptr2len)(ptr);
1709 else
1710 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1711 ++ptr;
1712 return ptr;
1713}
1714
1715/*
1716 * Find the end of the word. Assumes it starts inside a word.
1717 * Returns a pointer to just after the word.
1718 */
1719 char_u *
1720find_word_end(char_u *ptr)
1721{
1722 int start_class;
1723
1724 if (has_mbyte)
1725 {
1726 start_class = mb_get_class(ptr);
1727 if (start_class > 1)
1728 while (*ptr != NUL)
1729 {
1730 ptr += (*mb_ptr2len)(ptr);
1731 if (mb_get_class(ptr) != start_class)
1732 break;
1733 }
1734 }
1735 else
1736 while (vim_iswordc(*ptr))
1737 ++ptr;
1738 return ptr;
1739}
1740
1741/*
1742 * Find the end of the line, omitting CR and NL at the end.
1743 * Returns a pointer to just after the line.
1744 */
1745 static char_u *
1746find_line_end(char_u *ptr)
1747{
1748 char_u *s;
1749
1750 s = ptr + STRLEN(ptr);
1751 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1752 --s;
1753 return s;
1754}
1755
1756/*
1757 * Free the list of completions
1758 */
1759 static void
1760ins_compl_free(void)
1761{
1762 compl_T *match;
1763 int i;
1764
1765 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001766 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001767 VIM_CLEAR(compl_leader);
1768
1769 if (compl_first_match == NULL)
1770 return;
1771
1772 ins_compl_del_pum();
1773 pum_clear();
1774
1775 compl_curr_match = compl_first_match;
1776 do
1777 {
1778 match = compl_curr_match;
1779 compl_curr_match = compl_curr_match->cp_next;
1780 vim_free(match->cp_str);
1781 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001782 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001783 vim_free(match->cp_fname);
1784 for (i = 0; i < CPT_COUNT; ++i)
1785 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001786#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001787 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001788#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001789 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001790 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001791 compl_first_match = compl_curr_match = NULL;
1792 compl_shown_match = NULL;
1793 compl_old_match = NULL;
1794}
1795
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001796/*
1797 * Reset/clear the completion state.
1798 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001799 void
1800ins_compl_clear(void)
1801{
1802 compl_cont_status = 0;
1803 compl_started = FALSE;
1804 compl_matches = 0;
1805 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001806 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001807 VIM_CLEAR(compl_leader);
1808 edit_submode_extra = NULL;
1809 VIM_CLEAR(compl_orig_text);
1810 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001811#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001812 // clear v:completed_item
1813 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001814#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001815}
1816
1817/*
1818 * Return TRUE when Insert completion is active.
1819 */
1820 int
1821ins_compl_active(void)
1822{
1823 return compl_started;
1824}
1825
1826/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001827 * Selected one of the matches. When FALSE the match was edited or using the
1828 * longest common string.
1829 */
1830 int
1831ins_compl_used_match(void)
1832{
1833 return compl_used_match;
1834}
1835
1836/*
1837 * Initialize get longest common string.
1838 */
1839 void
1840ins_compl_init_get_longest(void)
1841{
1842 compl_get_longest = FALSE;
1843}
1844
1845/*
1846 * Returns TRUE when insert completion is interrupted.
1847 */
1848 int
1849ins_compl_interrupted(void)
1850{
1851 return compl_interrupted;
1852}
1853
1854/*
1855 * Returns TRUE if the <Enter> key selects a match in the completion popup
1856 * menu.
1857 */
1858 int
1859ins_compl_enter_selects(void)
1860{
1861 return compl_enter_selects;
1862}
1863
1864/*
1865 * Return the column where the text starts that is being completed
1866 */
1867 colnr_T
1868ins_compl_col(void)
1869{
1870 return compl_col;
1871}
1872
1873/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001874 * Return the length in bytes of the text being completed
1875 */
1876 int
1877ins_compl_len(void)
1878{
1879 return compl_length;
1880}
1881
1882/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001883 * Delete one character before the cursor and show the subset of the matches
1884 * that match the word that is now before the cursor.
1885 * Returns the character to be used, NUL if the work is done and another char
1886 * to be got from the user.
1887 */
1888 int
1889ins_compl_bs(void)
1890{
1891 char_u *line;
1892 char_u *p;
1893
1894 line = ml_get_curline();
1895 p = line + curwin->w_cursor.col;
1896 MB_PTR_BACK(line, p);
1897
1898 // Stop completion when the whole word was deleted. For Omni completion
1899 // allow the word to be deleted, we won't match everything.
1900 // Respect the 'backspace' option.
1901 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001902 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1903 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001904 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1905 - compl_length < 0))
1906 return K_BS;
1907
1908 // Deleted more than what was used to find matches or didn't finish
1909 // finding all matches: need to look for matches all over again.
1910 if (curwin->w_cursor.col <= compl_col + compl_length
1911 || ins_compl_need_restart())
1912 ins_compl_restart();
1913
1914 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001915 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001916 if (compl_leader == NULL)
1917 return K_BS;
1918
1919 ins_compl_new_leader();
1920 if (compl_shown_match != NULL)
1921 // Make sure current match is not a hidden item.
1922 compl_curr_match = compl_shown_match;
1923 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001924}
1925
1926/*
1927 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1928 * be called.
1929 */
1930 static int
1931ins_compl_need_restart(void)
1932{
1933 // Return TRUE if we didn't complete finding matches or when the
1934 // 'completefunc' returned "always" in the "refresh" dictionary item.
1935 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001936 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001937 && compl_opt_refresh_always);
1938}
1939
1940/*
1941 * Called after changing "compl_leader".
1942 * Show the popup menu with a different set of matches.
1943 * May also search for matches again if the previous search was interrupted.
1944 */
1945 static void
1946ins_compl_new_leader(void)
1947{
1948 ins_compl_del_pum();
1949 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001950 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001951 compl_used_match = FALSE;
1952
1953 if (compl_started)
1954 ins_compl_set_original_text(compl_leader);
1955 else
1956 {
1957#ifdef FEAT_SPELL
1958 spell_bad_len = 0; // need to redetect bad word
1959#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001960 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001961 // the popup menu display the changed text before the cursor. Set
1962 // "compl_restarting" to avoid that the first match is inserted.
1963 pum_call_update_screen();
1964#ifdef FEAT_GUI
1965 if (gui.in_use)
1966 {
1967 // Show the cursor after the match, not after the redrawn text.
1968 setcursor();
1969 out_flush_cursor(FALSE, FALSE);
1970 }
1971#endif
1972 compl_restarting = TRUE;
1973 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1974 compl_cont_status = 0;
1975 compl_restarting = FALSE;
1976 }
1977
1978 compl_enter_selects = !compl_used_match;
1979
1980 // Show the popup menu with a different set of matches.
1981 ins_compl_show_pum();
1982
1983 // Don't let Enter select the original text when there is no popup menu.
1984 if (compl_match_array == NULL)
1985 compl_enter_selects = FALSE;
1986}
1987
1988/*
1989 * Return the length of the completion, from the completion start column to
1990 * the cursor column. Making sure it never goes below zero.
1991 */
1992 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001993get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001994{
1995 int off = (int)curwin->w_cursor.col - (int)compl_col;
1996
1997 if (off < 0)
1998 return 0;
1999 return off;
2000}
2001
2002/*
2003 * Append one character to the match leader. May reduce the number of
2004 * matches.
2005 */
2006 void
2007ins_compl_addleader(int c)
2008{
2009 int cc;
2010
2011 if (stop_arrow() == FAIL)
2012 return;
2013 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2014 {
2015 char_u buf[MB_MAXBYTES + 1];
2016
2017 (*mb_char2bytes)(c, buf);
2018 buf[cc] = NUL;
2019 ins_char_bytes(buf, cc);
2020 if (compl_opt_refresh_always)
2021 AppendToRedobuff(buf);
2022 }
2023 else
2024 {
2025 ins_char(c);
2026 if (compl_opt_refresh_always)
2027 AppendCharToRedobuff(c);
2028 }
2029
2030 // If we didn't complete finding matches we must search again.
2031 if (ins_compl_need_restart())
2032 ins_compl_restart();
2033
2034 // When 'always' is set, don't reset compl_leader. While completing,
2035 // cursor doesn't point original position, changing compl_leader would
2036 // break redo.
2037 if (!compl_opt_refresh_always)
2038 {
2039 vim_free(compl_leader);
2040 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002041 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002042 if (compl_leader != NULL)
2043 ins_compl_new_leader();
2044 }
2045}
2046
2047/*
2048 * Setup for finding completions again without leaving CTRL-X mode. Used when
2049 * BS or a key was typed while still searching for matches.
2050 */
2051 static void
2052ins_compl_restart(void)
2053{
2054 ins_compl_free();
2055 compl_started = FALSE;
2056 compl_matches = 0;
2057 compl_cont_status = 0;
2058 compl_cont_mode = 0;
2059}
2060
2061/*
2062 * Set the first match, the original text.
2063 */
2064 static void
2065ins_compl_set_original_text(char_u *str)
2066{
2067 char_u *p;
2068
2069 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002070 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2071 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002072 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002073 {
2074 p = vim_strsave(str);
2075 if (p != NULL)
2076 {
2077 vim_free(compl_first_match->cp_str);
2078 compl_first_match->cp_str = p;
2079 }
2080 }
2081 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002082 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002083 {
2084 p = vim_strsave(str);
2085 if (p != NULL)
2086 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002087 vim_free(compl_first_match->cp_prev->cp_str);
2088 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002089 }
2090 }
2091}
2092
2093/*
2094 * Append one character to the match leader. May reduce the number of
2095 * matches.
2096 */
2097 void
2098ins_compl_addfrommatch(void)
2099{
2100 char_u *p;
2101 int len = (int)curwin->w_cursor.col - (int)compl_col;
2102 int c;
2103 compl_T *cp;
2104
2105 p = compl_shown_match->cp_str;
2106 if ((int)STRLEN(p) <= len) // the match is too short
2107 {
2108 // When still at the original match use the first entry that matches
2109 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002110 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002111 return;
2112
2113 p = NULL;
2114 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002115 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002116 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002117 if (compl_leader == NULL
2118 || ins_compl_equal(cp, compl_leader,
2119 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002120 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002121 p = cp->cp_str;
2122 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002123 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002124 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002125 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002126 return;
2127 }
2128 p += len;
2129 c = PTR2CHAR(p);
2130 ins_compl_addleader(c);
2131}
2132
2133/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002134 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002135 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2136 * compl_cont_mode and compl_cont_status.
2137 * Returns TRUE when the character is not to be inserted.
2138 */
2139 static int
2140set_ctrl_x_mode(int c)
2141{
2142 int retval = FALSE;
2143
2144 switch (c)
2145 {
2146 case Ctrl_E:
2147 case Ctrl_Y:
2148 // scroll the window one line up or down
2149 ctrl_x_mode = CTRL_X_SCROLL;
2150 if (!(State & REPLACE_FLAG))
2151 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2152 else
2153 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2154 edit_submode_pre = NULL;
2155 showmode();
2156 break;
2157 case Ctrl_L:
2158 // complete whole line
2159 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2160 break;
2161 case Ctrl_F:
2162 // complete filenames
2163 ctrl_x_mode = CTRL_X_FILES;
2164 break;
2165 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002166 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002167 ctrl_x_mode = CTRL_X_DICTIONARY;
2168 break;
2169 case Ctrl_R:
2170 // Register insertion without exiting CTRL-X mode
2171 // Simply allow ^R to happen without affecting ^X mode
2172 break;
2173 case Ctrl_T:
2174 // complete words from a thesaurus
2175 ctrl_x_mode = CTRL_X_THESAURUS;
2176 break;
2177#ifdef FEAT_COMPL_FUNC
2178 case Ctrl_U:
2179 // user defined completion
2180 ctrl_x_mode = CTRL_X_FUNCTION;
2181 break;
2182 case Ctrl_O:
2183 // omni completion
2184 ctrl_x_mode = CTRL_X_OMNI;
2185 break;
2186#endif
2187 case 's':
2188 case Ctrl_S:
2189 // complete spelling suggestions
2190 ctrl_x_mode = CTRL_X_SPELL;
2191#ifdef FEAT_SPELL
2192 ++emsg_off; // Avoid getting the E756 error twice.
2193 spell_back_to_badword();
2194 --emsg_off;
2195#endif
2196 break;
2197 case Ctrl_RSB:
2198 // complete tag names
2199 ctrl_x_mode = CTRL_X_TAGS;
2200 break;
2201#ifdef FEAT_FIND_ID
2202 case Ctrl_I:
2203 case K_S_TAB:
2204 // complete keywords from included files
2205 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2206 break;
2207 case Ctrl_D:
2208 // complete definitions from included files
2209 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2210 break;
2211#endif
2212 case Ctrl_V:
2213 case Ctrl_Q:
2214 // complete vim commands
2215 ctrl_x_mode = CTRL_X_CMDLINE;
2216 break;
2217 case Ctrl_Z:
2218 // stop completion
2219 ctrl_x_mode = CTRL_X_NORMAL;
2220 edit_submode = NULL;
2221 showmode();
2222 retval = TRUE;
2223 break;
2224 case Ctrl_P:
2225 case Ctrl_N:
2226 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2227 // just started ^X mode, or there were enough ^X's to cancel
2228 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2229 // do normal expansion when interrupting a different mode (say
2230 // ^X^F^X^P or ^P^X^X^P, see below)
2231 // nothing changes if interrupting mode 0, (eg, the flag
2232 // doesn't change when going to ADDING mode -- Acevedo
2233 if (!(compl_cont_status & CONT_INTRPT))
2234 compl_cont_status |= CONT_LOCAL;
2235 else if (compl_cont_mode != 0)
2236 compl_cont_status &= ~CONT_LOCAL;
2237 // FALLTHROUGH
2238 default:
2239 // If we have typed at least 2 ^X's... for modes != 0, we set
2240 // compl_cont_status = 0 (eg, as if we had just started ^X
2241 // mode).
2242 // For mode 0, we set "compl_cont_mode" to an impossible
2243 // value, in both cases ^X^X can be used to restart the same
2244 // mode (avoiding ADDING mode).
2245 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2246 // 'complete' and local ^P expansions respectively.
2247 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2248 // mode -- Acevedo
2249 if (c == Ctrl_X)
2250 {
2251 if (compl_cont_mode != 0)
2252 compl_cont_status = 0;
2253 else
2254 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2255 }
2256 ctrl_x_mode = CTRL_X_NORMAL;
2257 edit_submode = NULL;
2258 showmode();
2259 break;
2260 }
2261
2262 return retval;
2263}
2264
2265/*
2266 * Stop insert completion mode
2267 */
2268 static int
2269ins_compl_stop(int c, int prev_mode, int retval)
2270{
2271 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002272 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002273
2274 // Get here when we have finished typing a sequence of ^N and
2275 // ^P or other completion characters in CTRL-X mode. Free up
2276 // memory that was used, and make sure we can redo the insert.
2277 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2278 {
2279 // If any of the original typed text has been changed, eg when
2280 // ignorecase is set, we must add back-spaces to the redo
2281 // buffer. We add as few as necessary to delete just the part
2282 // of the original text that has changed.
2283 // When using the longest match, edited the match or used
2284 // CTRL-E then don't use the current match.
2285 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2286 ptr = compl_curr_match->cp_str;
2287 else
2288 ptr = NULL;
2289 ins_compl_fixRedoBufForLeader(ptr);
2290 }
2291
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002292 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002293
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002294 // When completing whole lines: fix indent for 'cindent'.
2295 // Otherwise, break line if it's too long.
2296 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2297 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002298 // re-indent the current line
2299 if (want_cindent)
2300 {
2301 do_c_expr_indent();
2302 want_cindent = FALSE; // don't do it again
2303 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002304 }
2305 else
2306 {
2307 int prev_col = curwin->w_cursor.col;
2308
2309 // put the cursor on the last char, for 'tw' formatting
2310 if (prev_col > 0)
2311 dec_cursor();
2312 // only format when something was inserted
2313 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2314 insertchar(NUL, 0, -1);
2315 if (prev_col > 0
2316 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2317 inc_cursor();
2318 }
2319
2320 // If the popup menu is displayed pressing CTRL-Y means accepting
2321 // the selection without inserting anything. When
2322 // compl_enter_selects is set the Enter key does the same.
2323 if ((c == Ctrl_Y || (compl_enter_selects
2324 && (c == CAR || c == K_KENTER || c == NL)))
2325 && pum_visible())
2326 retval = TRUE;
2327
2328 // CTRL-E means completion is Ended, go back to the typed text.
2329 // but only do this, if the Popup is still visible
2330 if (c == Ctrl_E)
2331 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002332 char_u *p = NULL;
2333
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002334 ins_compl_delete();
2335 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002336 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002337 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002338 p = compl_orig_text;
2339 if (p != NULL)
2340 {
2341 int compl_len = get_compl_len();
2342 int len = (int)STRLEN(p);
2343
2344 if (len > compl_len)
2345 ins_bytes_len(p + compl_len, len - compl_len);
2346 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002347 retval = TRUE;
2348 }
2349
2350 auto_format(FALSE, TRUE);
2351
2352 // Trigger the CompleteDonePre event to give scripts a chance to
2353 // act upon the completion before clearing the info, and restore
2354 // ctrl_x_mode, so that complete_info() can be used.
2355 ctrl_x_mode = prev_mode;
2356 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2357
2358 ins_compl_free();
2359 compl_started = FALSE;
2360 compl_matches = 0;
2361 if (!shortmess(SHM_COMPLETIONMENU))
2362 msg_clr_cmdline(); // necessary for "noshowmode"
2363 ctrl_x_mode = CTRL_X_NORMAL;
2364 compl_enter_selects = FALSE;
2365 if (edit_submode != NULL)
2366 {
2367 edit_submode = NULL;
2368 showmode();
2369 }
2370
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002371 if (c == Ctrl_C && cmdwin_type != 0)
2372 // Avoid the popup menu remains displayed when leaving the
2373 // command line window.
2374 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002375 // Indent now if a key was typed that is in 'cinkeys'.
2376 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2377 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002378 // Trigger the CompleteDone event to give scripts a chance to act
2379 // upon the end of completion.
2380 ins_apply_autocmds(EVENT_COMPLETEDONE);
2381
2382 return retval;
2383}
2384
2385/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002386 * Prepare for Insert mode completion, or stop it.
2387 * Called just after typing a character in Insert mode.
2388 * Returns TRUE when the character is not to be inserted;
2389 */
2390 int
2391ins_compl_prep(int c)
2392{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002393 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002394 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002395
2396 // Forget any previous 'special' messages if this is actually
2397 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2398 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2399 edit_submode_extra = NULL;
2400
zeertzjq440d4cb2023-03-02 17:51:32 +00002401 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002402 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002403 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002404 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002405 return retval;
2406
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002407#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002408 // Ignore mouse events in a popup window
2409 if (is_mouse_key(c))
2410 {
2411 // Ignore drag and release events, the position does not need to be in
2412 // the popup and it may have just closed.
2413 if (c == K_LEFTRELEASE
2414 || c == K_LEFTRELEASE_NM
2415 || c == K_MIDDLERELEASE
2416 || c == K_RIGHTRELEASE
2417 || c == K_X1RELEASE
2418 || c == K_X2RELEASE
2419 || c == K_LEFTDRAG
2420 || c == K_MIDDLEDRAG
2421 || c == K_RIGHTDRAG
2422 || c == K_X1DRAG
2423 || c == K_X2DRAG)
2424 return retval;
2425 if (popup_visible)
2426 {
2427 int row = mouse_row;
2428 int col = mouse_col;
2429 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2430
2431 if (wp != NULL && WIN_IS_POPUP(wp))
2432 return retval;
2433 }
2434 }
2435#endif
2436
zeertzjqdca29d92021-08-31 19:12:51 +02002437 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2438 {
2439 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2440 || !vim_is_ctrl_x_key(c))
2441 {
2442 // Not starting another completion mode.
2443 ctrl_x_mode = CTRL_X_CMDLINE;
2444
2445 // CTRL-X CTRL-Z should stop completion without inserting anything
2446 if (c == Ctrl_Z)
2447 retval = TRUE;
2448 }
2449 else
2450 {
2451 ctrl_x_mode = CTRL_X_CMDLINE;
2452
2453 // Other CTRL-X keys first stop completion, then start another
2454 // completion mode.
2455 ins_compl_prep(' ');
2456 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2457 }
2458 }
2459
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002460 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002461 if (ctrl_x_mode_not_defined_yet()
2462 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002463 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002464 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002465 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002466 }
2467
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002468 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002469 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2470 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002471 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002472 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002473 {
2474 // We're already in CTRL-X mode, do we stay in it?
2475 if (!vim_is_ctrl_x_key(c))
2476 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002477 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002478 ctrl_x_mode = CTRL_X_NORMAL;
2479 else
2480 ctrl_x_mode = CTRL_X_FINISHED;
2481 edit_submode = NULL;
2482 }
2483 showmode();
2484 }
2485
2486 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2487 {
2488 // Show error message from attempted keyword completion (probably
2489 // 'Pattern not found') until another key is hit, then go back to
2490 // showing what mode we are in.
2491 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002492 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002493 && c != Ctrl_R && !ins_compl_pum_key(c))
2494 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002495 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002496 }
2497 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2498 // Trigger the CompleteDone event to give scripts a chance to act
2499 // upon the (possibly failed) completion.
2500 ins_apply_autocmds(EVENT_COMPLETEDONE);
2501
LemonBoy2bf52dd2022-04-09 18:17:34 +01002502 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002503
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002504 // reset continue_* if we left expansion-mode, if we stay they'll be
2505 // (re)set properly in ins_complete()
2506 if (!vim_is_ctrl_x_key(c))
2507 {
2508 compl_cont_status = 0;
2509 compl_cont_mode = 0;
2510 }
2511
2512 return retval;
2513}
2514
2515/*
2516 * Fix the redo buffer for the completion leader replacing some of the typed
2517 * text. This inserts backspaces and appends the changed text.
2518 * "ptr" is the known leader text or NUL.
2519 */
2520 static void
2521ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2522{
2523 int len;
2524 char_u *p;
2525 char_u *ptr = ptr_arg;
2526
2527 if (ptr == NULL)
2528 {
2529 if (compl_leader != NULL)
2530 ptr = compl_leader;
2531 else
2532 return; // nothing to do
2533 }
2534 if (compl_orig_text != NULL)
2535 {
2536 p = compl_orig_text;
2537 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2538 ;
2539 if (len > 0)
2540 len -= (*mb_head_off)(p, p + len);
2541 for (p += len; *p != NUL; MB_PTR_ADV(p))
2542 AppendCharToRedobuff(K_BS);
2543 }
2544 else
2545 len = 0;
2546 if (ptr != NULL)
2547 AppendToRedobuffLit(ptr + len, -1);
2548}
2549
2550/*
2551 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2552 * (depending on flag) starting from buf and looking for a non-scanned
2553 * buffer (other than curbuf). curbuf is special, if it is called with
2554 * buf=curbuf then it has to be the first call for a given flag/expansion.
2555 *
2556 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2557 */
2558 static buf_T *
2559ins_compl_next_buf(buf_T *buf, int flag)
2560{
2561 static win_T *wp = NULL;
2562
2563 if (flag == 'w') // just windows
2564 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002565 if (buf == curbuf || !win_valid(wp))
2566 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002567 wp = curwin;
2568 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2569 && wp->w_buffer->b_scanned)
2570 ;
2571 buf = wp->w_buffer;
2572 }
2573 else
2574 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2575 // (unlisted buffers)
2576 // When completing whole lines skip unloaded buffers.
2577 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2578 && ((flag == 'U'
2579 ? buf->b_p_bl
2580 : (!buf->b_p_bl
2581 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2582 || buf->b_scanned))
2583 ;
2584 return buf;
2585}
2586
2587#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002588
2589# ifdef FEAT_EVAL
2590static callback_T cfu_cb; // 'completefunc' callback function
2591static callback_T ofu_cb; // 'omnifunc' callback function
2592static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2593# endif
2594
2595/*
2596 * Copy a global callback function to a buffer local callback.
2597 */
2598 static void
2599copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2600{
2601 free_callback(bufcb);
2602 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2603 copy_callback(bufcb, globcb);
2604}
2605
2606/*
2607 * Parse the 'completefunc' option value and set the callback function.
2608 * Invoked when the 'completefunc' option is set. The option value can be a
2609 * name of a function (string), or function(<name>) or funcref(<name>) or a
2610 * lambda expression.
2611 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002612 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002613did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002614{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002615 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2616 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002617
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002618 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002619
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002620 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002621}
2622
2623/*
2624 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002625 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002626 */
2627 void
2628set_buflocal_cfu_callback(buf_T *buf UNUSED)
2629{
2630# ifdef FEAT_EVAL
2631 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2632# endif
2633}
2634
2635/*
2636 * Parse the 'omnifunc' option value and set the callback function.
2637 * Invoked when the 'omnifunc' option is set. The option value can be a
2638 * name of a function (string), or function(<name>) or funcref(<name>) or a
2639 * lambda expression.
2640 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002641 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002642did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002643{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002644 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2645 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002646
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002647 set_buflocal_ofu_callback(curbuf);
2648 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002649}
2650
2651/*
2652 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002653 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002654 */
2655 void
2656set_buflocal_ofu_callback(buf_T *buf UNUSED)
2657{
2658# ifdef FEAT_EVAL
2659 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2660# endif
2661}
2662
2663/*
2664 * Parse the 'thesaurusfunc' option value and set the callback function.
2665 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2666 * name of a function (string), or function(<name>) or funcref(<name>) or a
2667 * lambda expression.
2668 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002669 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002670did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002671{
2672 int retval;
2673
2674 if (*curbuf->b_p_tsrfu != NUL)
2675 {
2676 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002677 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2678 &curbuf->b_tsrfu_cb);
2679 }
2680 else
2681 {
2682 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002683 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2684 }
2685
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002686 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002687}
2688
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002689/*
2690 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002691 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002692 */
2693 int
2694set_ref_in_insexpand_funcs(int copyID)
2695{
2696 int abort = FALSE;
2697
2698 abort = set_ref_in_callback(&cfu_cb, copyID);
2699 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2700 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2701
2702 return abort;
2703}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002704
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002705/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002706 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002707 */
2708 static char_u *
2709get_complete_funcname(int type)
2710{
2711 switch (type)
2712 {
2713 case CTRL_X_FUNCTION:
2714 return curbuf->b_p_cfu;
2715 case CTRL_X_OMNI:
2716 return curbuf->b_p_ofu;
2717 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002718 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002719 default:
2720 return (char_u *)"";
2721 }
2722}
2723
2724/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002725 * Get the callback to use for insert mode completion.
2726 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002727 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002728get_insert_callback(int type)
2729{
2730 if (type == CTRL_X_FUNCTION)
2731 return &curbuf->b_cfu_cb;
2732 if (type == CTRL_X_OMNI)
2733 return &curbuf->b_ofu_cb;
2734 // CTRL_X_THESAURUS
2735 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2736}
2737
2738/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002739 * Execute user defined complete function 'completefunc', 'omnifunc' or
2740 * 'thesaurusfunc', and get matches in "matches".
2741 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002742 */
2743 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002744expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002745{
2746 list_T *matchlist = NULL;
2747 dict_T *matchdict = NULL;
2748 typval_T args[3];
2749 char_u *funcname;
2750 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002751 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 typval_T rettv;
2753 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002754 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002756 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002757 if (*funcname == NUL)
2758 return;
2759
2760 // Call 'completefunc' to obtain the list of matches.
2761 args[0].v_type = VAR_NUMBER;
2762 args[0].vval.v_number = 0;
2763 args[1].v_type = VAR_STRING;
2764 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2765 args[2].v_type = VAR_UNKNOWN;
2766
2767 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002768 // Lock the text to avoid weird things from happening. Also disallow
2769 // switching to another window, it should not be needed and may end up in
2770 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002771 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002772
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002773 cb = get_insert_callback(type);
2774 retval = call_callback(cb, 0, &rettv, 2, args);
2775
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002776 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002777 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002778 {
2779 switch (rettv.v_type)
2780 {
2781 case VAR_LIST:
2782 matchlist = rettv.vval.v_list;
2783 break;
2784 case VAR_DICT:
2785 matchdict = rettv.vval.v_dict;
2786 break;
2787 case VAR_SPECIAL:
2788 if (rettv.vval.v_number == VVAL_NONE)
2789 compl_opt_suppress_empty = TRUE;
2790 // FALLTHROUGH
2791 default:
2792 // TODO: Give error message?
2793 clear_tv(&rettv);
2794 break;
2795 }
2796 }
zeertzjqcfe45652022-05-27 17:26:55 +01002797 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002798
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002799 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002800 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002801 validate_cursor();
2802 if (!EQUAL_POS(curwin->w_cursor, pos))
2803 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002804 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002805 goto theend;
2806 }
2807
2808 if (matchlist != NULL)
2809 ins_compl_add_list(matchlist);
2810 else if (matchdict != NULL)
2811 ins_compl_add_dict(matchdict);
2812
2813theend:
2814 // Restore State, it might have been changed.
2815 State = save_State;
2816
2817 if (matchdict != NULL)
2818 dict_unref(matchdict);
2819 if (matchlist != NULL)
2820 list_unref(matchlist);
2821}
2822#endif // FEAT_COMPL_FUNC
2823
2824#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2825/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002826 * Add a match to the list of matches from a typeval_T.
2827 * If the given string is already in the list of completions, then return
2828 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2829 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002830 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002831 */
2832 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002833ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002834{
2835 char_u *word;
2836 int dup = FALSE;
2837 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002838 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002839 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002840 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002841 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002842
Bram Moolenaar08928322020-01-04 14:32:48 +01002843 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002844 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2845 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002846 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2847 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2848 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2849 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2850 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2851 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2852 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2853 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002854 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002855 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2856 dup = dict_get_number(tv->vval.v_dict, "dup");
2857 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2858 empty = dict_get_number(tv->vval.v_dict, "empty");
2859 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2860 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002861 flags |= CP_EQUAL;
2862 }
2863 else
2864 {
2865 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002866 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002867 }
2868 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002869 {
2870 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002871 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002872 }
2873 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2874 if (status != OK)
2875 clear_tv(&user_data);
2876 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002877}
2878
2879/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002880 * Add completions from a list.
2881 */
2882 static void
2883ins_compl_add_list(list_T *list)
2884{
2885 listitem_T *li;
2886 int dir = compl_direction;
2887
2888 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002889 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002890 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002891 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002892 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893 // if dir was BACKWARD then honor it just once
2894 dir = FORWARD;
2895 else if (did_emsg)
2896 break;
2897 }
2898}
2899
2900/*
2901 * Add completions from a dict.
2902 */
2903 static void
2904ins_compl_add_dict(dict_T *dict)
2905{
2906 dictitem_T *di_refresh;
2907 dictitem_T *di_words;
2908
2909 // Check for optional "refresh" item.
2910 compl_opt_refresh_always = FALSE;
2911 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2912 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2913 {
2914 char_u *v = di_refresh->di_tv.vval.v_string;
2915
2916 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2917 compl_opt_refresh_always = TRUE;
2918 }
2919
2920 // Add completions from a "words" list.
2921 di_words = dict_find(dict, (char_u *)"words", 5);
2922 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2923 ins_compl_add_list(di_words->di_tv.vval.v_list);
2924}
2925
2926/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002927 * Start completion for the complete() function.
2928 * "startcol" is where the matched text starts (1 is first column).
2929 * "list" is the list of matches.
2930 */
2931 static void
2932set_completion(colnr_T startcol, list_T *list)
2933{
2934 int save_w_wrow = curwin->w_wrow;
2935 int save_w_leftcol = curwin->w_leftcol;
2936 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02002937 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02002938 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
2939 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
2940 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002941
2942 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002943 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002944 ins_compl_prep(' ');
2945 ins_compl_clear();
2946 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002947 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002948
2949 compl_direction = FORWARD;
2950 if (startcol > curwin->w_cursor.col)
2951 startcol = curwin->w_cursor.col;
2952 compl_col = startcol;
2953 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2954 // compl_pattern doesn't need to be set
2955 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2956 if (p_ic)
2957 flags |= CP_ICASE;
2958 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002959 -1, NULL, NULL, NULL, 0,
2960 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002961 return;
2962
2963 ctrl_x_mode = CTRL_X_EVAL;
2964
2965 ins_compl_add_list(list);
2966 compl_matches = ins_compl_make_cyclic();
2967 compl_started = TRUE;
2968 compl_used_match = TRUE;
2969 compl_cont_status = 0;
2970
2971 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002972 int no_select = compl_no_select || compl_longest;
2973 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002974 {
2975 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002976 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002977 // Down/Up has no real effect.
2978 ins_complete(K_UP, FALSE);
2979 }
2980 else
2981 ins_complete(Ctrl_N, FALSE);
2982 compl_enter_selects = compl_no_insert;
2983
2984 // Lazily show the popup menu, unless we got interrupted.
2985 if (!compl_interrupted)
2986 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002987 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002988 out_flush();
2989}
2990
2991/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002992 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002993 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002994 void
2995f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002996{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002998
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002999 if (in_vim9script()
3000 && (check_for_number_arg(argvars, 0) == FAIL
3001 || check_for_list_arg(argvars, 1) == FAIL))
3002 return;
3003
Bram Moolenaar24959102022-05-07 20:01:16 +01003004 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003005 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003006 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003007 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003008 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003009
3010 // Check for undo allowed here, because if something was already inserted
3011 // the line was already saved for undo and this check isn't done.
3012 if (!undo_allowed())
3013 return;
3014
Bram Moolenaard83392a2022-09-01 12:22:46 +01003015 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003016 {
3017 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3018 if (startcol > 0)
3019 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003020 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003021}
3022
3023/*
3024 * "complete_add()" function
3025 */
3026 void
3027f_complete_add(typval_T *argvars, typval_T *rettv)
3028{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003029 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003030 return;
3031
Bram Moolenaar440cf092021-04-03 20:13:30 +02003032 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003033}
3034
3035/*
3036 * "complete_check()" function
3037 */
3038 void
3039f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3040{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003041 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003042 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003043
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003044 ins_compl_check_keys(0, TRUE);
3045 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003046
3047 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003048}
3049
3050/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003051 * Return Insert completion mode name string
3052 */
3053 static char_u *
3054ins_compl_mode(void)
3055{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003056 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003057 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3058
3059 return (char_u *)"";
3060}
3061
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003062/*
3063 * Assign the sequence number to all the completion matches which don't have
3064 * one assigned yet.
3065 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003066 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003067ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003068{
3069 int number = 0;
3070 compl_T *match;
3071
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003072 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003073 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003074 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003075 // This should normally succeed already at the first loop
3076 // cycle, so it's fast!
3077 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003078 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003079 if (match->cp_number != -1)
3080 {
3081 number = match->cp_number;
3082 break;
3083 }
3084 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003085 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003086 for (match = match->cp_next;
3087 match != NULL && match->cp_number == -1;
3088 match = match->cp_next)
3089 match->cp_number = ++number;
3090 }
3091 else // BACKWARD
3092 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003093 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003094 // number. This should normally succeed already at the
3095 // first loop cycle, so it's fast!
3096 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003097 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003098 if (match->cp_number != -1)
3099 {
3100 number = match->cp_number;
3101 break;
3102 }
3103 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003104 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003105 for (match = match->cp_prev; match
3106 && match->cp_number == -1;
3107 match = match->cp_prev)
3108 match->cp_number = ++number;
3109 }
3110}
3111
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003112/*
3113 * Get complete information
3114 */
3115 static void
3116get_complete_info(list_T *what_list, dict_T *retdict)
3117{
3118 int ret = OK;
3119 listitem_T *item;
3120#define CI_WHAT_MODE 0x01
3121#define CI_WHAT_PUM_VISIBLE 0x02
3122#define CI_WHAT_ITEMS 0x04
3123#define CI_WHAT_SELECTED 0x08
3124#define CI_WHAT_INSERTED 0x10
3125#define CI_WHAT_ALL 0xff
3126 int what_flag;
3127
3128 if (what_list == NULL)
3129 what_flag = CI_WHAT_ALL;
3130 else
3131 {
3132 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003133 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003134 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003135 {
3136 char_u *what = tv_get_string(&item->li_tv);
3137
3138 if (STRCMP(what, "mode") == 0)
3139 what_flag |= CI_WHAT_MODE;
3140 else if (STRCMP(what, "pum_visible") == 0)
3141 what_flag |= CI_WHAT_PUM_VISIBLE;
3142 else if (STRCMP(what, "items") == 0)
3143 what_flag |= CI_WHAT_ITEMS;
3144 else if (STRCMP(what, "selected") == 0)
3145 what_flag |= CI_WHAT_SELECTED;
3146 else if (STRCMP(what, "inserted") == 0)
3147 what_flag |= CI_WHAT_INSERTED;
3148 }
3149 }
3150
3151 if (ret == OK && (what_flag & CI_WHAT_MODE))
3152 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3153
3154 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3155 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3156
Girish Palya8950bf72024-03-20 20:07:29 +01003157 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003158 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003159 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003160 dict_T *di;
3161 compl_T *match;
3162 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003163
Girish Palya8950bf72024-03-20 20:07:29 +01003164 if (what_flag & CI_WHAT_ITEMS)
3165 {
3166 li = list_alloc();
3167 if (li == NULL)
3168 return;
3169 ret = dict_add_list(retdict, "items", li);
3170 }
3171 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3172 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3173 ins_compl_update_sequence_numbers();
3174 if (ret == OK && compl_first_match != NULL)
3175 {
3176 int list_idx = 0;
3177 match = compl_first_match;
3178 do
3179 {
3180 if (!match_at_original_text(match))
3181 {
3182 if (what_flag & CI_WHAT_ITEMS)
3183 {
3184 di = dict_alloc();
3185 if (di == NULL)
3186 return;
3187 ret = list_append_dict(li, di);
3188 if (ret != OK)
3189 return;
3190 dict_add_string(di, "word", match->cp_str);
3191 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3192 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3193 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3194 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3195 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3196 // Add an empty string for backwards compatibility
3197 dict_add_string(di, "user_data", (char_u *)"");
3198 else
3199 dict_add_tv(di, "user_data", &match->cp_user_data);
3200 }
3201 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3202 selected_idx = list_idx;
3203 list_idx += 1;
3204 }
3205 match = match->cp_next;
3206 }
3207 while (match != NULL && !is_first_match(match));
3208 }
3209 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3210 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003211 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003212
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003213 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3214 {
3215 // TODO
3216 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003217}
3218
3219/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003220 * "complete_info()" function
3221 */
3222 void
3223f_complete_info(typval_T *argvars, typval_T *rettv)
3224{
3225 list_T *what_list = NULL;
3226
Bram Moolenaar93a10962022-06-16 11:42:09 +01003227 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003228 return;
3229
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003230 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3231 return;
3232
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003233 if (argvars[0].v_type != VAR_UNKNOWN)
3234 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003235 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003236 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003237 what_list = argvars[0].vval.v_list;
3238 }
3239 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003240}
3241#endif
3242
3243/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003244 * Returns TRUE when using a user-defined function for thesaurus completion.
3245 */
3246 static int
3247thesaurus_func_complete(int type UNUSED)
3248{
3249#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003250 return type == CTRL_X_THESAURUS
3251 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003252#else
3253 return FALSE;
3254#endif
3255}
3256
3257/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003258 * Return value of process_next_cpt_value()
3259 */
3260enum
3261{
3262 INS_COMPL_CPT_OK = 1,
3263 INS_COMPL_CPT_CONT,
3264 INS_COMPL_CPT_END
3265};
3266
3267/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003268 * state information used for getting the next set of insert completion
3269 * matches.
3270 */
3271typedef struct
3272{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003273 char_u *e_cpt_copy; // copy of 'complete'
3274 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003275 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003276 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003277 pos_T prev_match_pos; // previous match position
3278 int set_match_pos; // save first_match_pos/last_match_pos
3279 pos_T first_match_pos; // first match position
3280 pos_T last_match_pos; // last match position
3281 int found_all; // found all matches of a certain type.
3282 char_u *dict; // dictionary file to search
3283 int dict_f; // "dict" is an exact file name or not
3284} ins_compl_next_state_T;
3285
3286/*
3287 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003288 *
3289 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003290 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003291 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003292 * st->found_all - all matches of this type are found
3293 * st->ins_buf - search for completions in this buffer
3294 * st->first_match_pos - position of the first completion match
3295 * st->last_match_pos - position of the last completion match
3296 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003297 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003298 * st->dict - name of the dictionary or thesaurus file to search
3299 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003300 *
3301 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003302 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3303 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003304 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003305 */
3306 static int
3307process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003308 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003309 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003310 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003311{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003312 int compl_type = -1;
3313 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003314
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003317 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3318 st->e_cpt++;
3319
3320 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003321 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003322 st->ins_buf = curbuf;
3323 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003324 // Move the cursor back one character so that ^N can match the
3325 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003326 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003327 {
3328 // Move the cursor to after the last character in the
3329 // buffer, so that word at start of buffer is found
3330 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003331 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003332 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003333 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003334 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003335 compl_type = 0;
3336
3337 // Remember the first match so that the loop stops when we
3338 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003339 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003341 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003342 && (st->ins_buf = ins_compl_next_buf(
3343 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003344 {
3345 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003346 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003347 {
3348 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003349 st->first_match_pos.col = st->last_match_pos.col = 0;
3350 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3351 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003352 compl_type = 0;
3353 }
3354 else // unloaded buffer, scan like dictionary
3355 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003356 st->found_all = TRUE;
3357 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003358 {
3359 status = INS_COMPL_CPT_CONT;
3360 goto done;
3361 }
3362 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003363 st->dict = st->ins_buf->b_fname;
3364 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003365 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003366 if (!shortmess(SHM_COMPLETIONSCAN))
3367 {
3368 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3369 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3370 st->ins_buf->b_fname == NULL
3371 ? buf_spname(st->ins_buf)
3372 : st->ins_buf->b_sfname == NULL
3373 ? st->ins_buf->b_fname
3374 : st->ins_buf->b_sfname);
3375 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3376 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003377 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003378 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003379 status = INS_COMPL_CPT_END;
3380 else
3381 {
3382 if (ctrl_x_mode_line_or_eval())
3383 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003384 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003385 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003386 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003387 compl_type = CTRL_X_DICTIONARY;
3388 else
3389 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003390 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003391 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003392 st->dict = st->e_cpt;
3393 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003394 }
3395 }
3396#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003397 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003398 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003399 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003400 compl_type = CTRL_X_PATH_DEFINES;
3401#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003402 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003404 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003405 if (!shortmess(SHM_COMPLETIONSCAN))
3406 {
3407 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3408 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3409 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3410 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003411 }
3412 else
3413 compl_type = -1;
3414
3415 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003416 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003417
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003418 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003419 if (compl_type == -1)
3420 status = INS_COMPL_CPT_CONT;
3421 }
3422
3423done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003424 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003425 return status;
3426}
3427
3428#ifdef FEAT_FIND_ID
3429/*
3430 * Get the next set of identifiers or defines matching "compl_pattern" in
3431 * included files.
3432 */
3433 static void
3434get_next_include_file_completion(int compl_type)
3435{
3436 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003437 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003438 (compl_type == CTRL_X_PATH_DEFINES
3439 && !(compl_cont_status & CONT_SOL))
3440 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003441 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442}
3443#endif
3444
3445/*
3446 * Get the next set of words matching "compl_pattern" in dictionary or
3447 * thesaurus files.
3448 */
3449 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003450get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003451{
3452#ifdef FEAT_COMPL_FUNC
3453 if (thesaurus_func_complete(compl_type))
3454 expand_by_function(compl_type, compl_pattern);
3455 else
3456#endif
3457 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003458 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003459 : (compl_type == CTRL_X_THESAURUS
3460 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3461 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3462 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003463 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003464 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003465}
3466
3467/*
3468 * Get the next set of tag names matching "compl_pattern".
3469 */
3470 static void
3471get_next_tag_completion(void)
3472{
3473 int save_p_ic;
3474 char_u **matches;
3475 int num_matches;
3476
3477 // set p_ic according to p_ic, p_scs and pat for find_tags().
3478 save_p_ic = p_ic;
3479 p_ic = ignorecase(compl_pattern);
3480
3481 // Find up to TAG_MANY matches. Avoids that an enormous number
3482 // of matches is found when compl_pattern is empty
3483 g_tag_at_cursor = TRUE;
3484 if (find_tags(compl_pattern, &num_matches, &matches,
3485 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003486 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003487 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3488 ins_compl_add_matches(num_matches, matches, p_ic);
3489 g_tag_at_cursor = FALSE;
3490 p_ic = save_p_ic;
3491}
3492
3493/*
3494 * Get the next set of filename matching "compl_pattern".
3495 */
3496 static void
3497get_next_filename_completion(void)
3498{
3499 char_u **matches;
3500 int num_matches;
3501
3502 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3503 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3504 return;
3505
3506 // May change home directory back to "~".
3507 tilde_replace(compl_pattern, num_matches, matches);
3508#ifdef BACKSLASH_IN_FILENAME
3509 if (curbuf->b_p_csl[0] != NUL)
3510 {
3511 int i;
3512
3513 for (i = 0; i < num_matches; ++i)
3514 {
3515 char_u *ptr = matches[i];
3516
3517 while (*ptr != NUL)
3518 {
3519 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3520 *ptr = '/';
3521 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3522 *ptr = '\\';
3523 ptr += (*mb_ptr2len)(ptr);
3524 }
3525 }
3526 }
3527#endif
3528 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3529}
3530
3531/*
3532 * Get the next set of command-line completions matching "compl_pattern".
3533 */
3534 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003535get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003536{
3537 char_u **matches;
3538 int num_matches;
3539
3540 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003541 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003542 ins_compl_add_matches(num_matches, matches, FALSE);
3543}
3544
3545/*
3546 * Get the next set of spell suggestions matching "compl_pattern".
3547 */
3548 static void
3549get_next_spell_completion(linenr_T lnum UNUSED)
3550{
3551#ifdef FEAT_SPELL
3552 char_u **matches;
3553 int num_matches;
3554
3555 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3556 if (num_matches > 0)
3557 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003558 else
3559 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003560#endif
3561}
3562
3563/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003564 * Return the next word or line from buffer "ins_buf" at position
3565 * "cur_match_pos" for completion. The length of the match is set in "len".
3566 */
3567 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003568ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003569 buf_T *ins_buf, // buffer being scanned
3570 pos_T *cur_match_pos, // current match position
3571 int *match_len,
3572 int *cont_s_ipos) // next ^X<> will set initial_pos
3573{
3574 char_u *ptr;
3575 int len;
3576
3577 *match_len = 0;
3578 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3579 cur_match_pos->col;
3580 if (ctrl_x_mode_line_or_eval())
3581 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003582 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003583 {
3584 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3585 return NULL;
3586 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3587 if (!p_paste)
3588 ptr = skipwhite(ptr);
3589 }
3590 len = (int)STRLEN(ptr);
3591 }
3592 else
3593 {
3594 char_u *tmp_ptr = ptr;
3595
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003596 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003597 {
3598 tmp_ptr += compl_length;
3599 // Skip if already inside a word.
3600 if (vim_iswordp(tmp_ptr))
3601 return NULL;
3602 // Find start of next word.
3603 tmp_ptr = find_word_start(tmp_ptr);
3604 }
3605 // Find end of this word.
3606 tmp_ptr = find_word_end(tmp_ptr);
3607 len = (int)(tmp_ptr - ptr);
3608
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003609 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003610 {
3611 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3612 {
3613 // Try next line, if any. the new word will be
3614 // "join" as if the normal command "J" was used.
3615 // IOSIZE is always greater than
3616 // compl_length, so the next STRNCPY always
3617 // works -- Acevedo
3618 STRNCPY(IObuff, ptr, len);
3619 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3620 tmp_ptr = ptr = skipwhite(ptr);
3621 // Find start of next word.
3622 tmp_ptr = find_word_start(tmp_ptr);
3623 // Find end of next word.
3624 tmp_ptr = find_word_end(tmp_ptr);
3625 if (tmp_ptr > ptr)
3626 {
3627 if (*ptr != ')' && IObuff[len - 1] != TAB)
3628 {
3629 if (IObuff[len - 1] != ' ')
3630 IObuff[len++] = ' ';
3631 // IObuf =~ "\k.* ", thus len >= 2
3632 if (p_js
3633 && (IObuff[len - 2] == '.'
3634 || (vim_strchr(p_cpo, CPO_JOINSP)
3635 == NULL
3636 && (IObuff[len - 2] == '?'
3637 || IObuff[len - 2] == '!'))))
3638 IObuff[len++] = ' ';
3639 }
3640 // copy as much as possible of the new word
3641 if (tmp_ptr - ptr >= IOSIZE - len)
3642 tmp_ptr = ptr + IOSIZE - len - 1;
3643 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3644 len += (int)(tmp_ptr - ptr);
3645 *cont_s_ipos = TRUE;
3646 }
3647 IObuff[len] = NUL;
3648 ptr = IObuff;
3649 }
3650 if (len == compl_length)
3651 return NULL;
3652 }
3653 }
3654
3655 *match_len = len;
3656 return ptr;
3657}
3658
3659/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 * Get the next set of words matching "compl_pattern" for default completion(s)
3661 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003662 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3663 * position "st->start_pos" in the "compl_direction" direction. If
3664 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3665 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666 * Returns OK if a new next match is found, otherwise returns FAIL.
3667 */
3668 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003669get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003670{
3671 int found_new_match = FAIL;
3672 int save_p_scs;
3673 int save_p_ws;
3674 int looped_around = FALSE;
3675 char_u *ptr;
3676 int len;
3677
3678 // If 'infercase' is set, don't use 'smartcase' here
3679 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003680 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003681 p_scs = FALSE;
3682
3683 // Buffers other than curbuf are scanned from the beginning or the
3684 // end but never from the middle, thus setting nowrapscan in this
3685 // buffer is a good idea, on the other hand, we always set
3686 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3687 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003688 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003689 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003690 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691 p_ws = TRUE;
3692 looped_around = FALSE;
3693 for (;;)
3694 {
3695 int cont_s_ipos = FALSE;
3696
3697 ++msg_silent; // Don't want messages for wrapscan.
3698
3699 // ctrl_x_mode_line_or_eval() || word-wise search that
3700 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003701 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003702 found_new_match = search_for_exact_line(st->ins_buf,
3703 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003704 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003705 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003706 NULL, compl_direction, compl_pattern, compl_patternlen,
3707 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003709 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003710 {
3711 // set "compl_started" even on fail
3712 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003713 st->first_match_pos = *st->cur_match_pos;
3714 st->last_match_pos = *st->cur_match_pos;
3715 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003716 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003717 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3718 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003719 {
3720 found_new_match = FAIL;
3721 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003722 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003723 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3724 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3725 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003726 {
3727 if (looped_around)
3728 found_new_match = FAIL;
3729 else
3730 looped_around = TRUE;
3731 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003732 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003733 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3734 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3735 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003736 {
3737 if (looped_around)
3738 found_new_match = FAIL;
3739 else
3740 looped_around = TRUE;
3741 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003742 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003743 if (found_new_match == FAIL)
3744 break;
3745
3746 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003747 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003748 && start_pos->lnum == st->cur_match_pos->lnum
3749 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003751
zeertzjq440d4cb2023-03-02 17:51:32 +00003752 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3753 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003754 if (ptr == NULL)
3755 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003756
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003757 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003758 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003759 0, cont_s_ipos) != NOTDONE)
3760 {
3761 found_new_match = OK;
3762 break;
3763 }
3764 }
3765 p_scs = save_p_scs;
3766 p_ws = save_p_ws;
3767
3768 return found_new_match;
3769}
3770
3771/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003772 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003773 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003774 */
3775 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003776get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003777{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003778 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003779
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003780 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003781 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003782 case -1:
3783 break;
3784#ifdef FEAT_FIND_ID
3785 case CTRL_X_PATH_PATTERNS:
3786 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003787 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003788 break;
3789#endif
3790
3791 case CTRL_X_DICTIONARY:
3792 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003793 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3794 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003795 break;
3796
3797 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003798 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003799 break;
3800
3801 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003802 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003803 break;
3804
3805 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003806 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003807 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003808 break;
3809
3810#ifdef FEAT_COMPL_FUNC
3811 case CTRL_X_FUNCTION:
3812 case CTRL_X_OMNI:
3813 expand_by_function(type, compl_pattern);
3814 break;
3815#endif
3816
3817 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003818 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003819 break;
3820
3821 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003822 found_new_match = get_next_default_completion(st, ini);
3823 if (found_new_match == FAIL && st->ins_buf == curbuf)
3824 st->found_all = TRUE;
3825 }
3826
3827 // check if compl_curr_match has changed, (e.g. other type of
3828 // expansion added something)
3829 if (type != 0 && compl_curr_match != compl_old_match)
3830 found_new_match = OK;
3831
3832 return found_new_match;
3833}
3834
3835/*
3836 * Get the next expansion(s), using "compl_pattern".
3837 * The search starts at position "ini" in curbuf and in the direction
3838 * compl_direction.
3839 * When "compl_started" is FALSE start at that position, otherwise continue
3840 * where we stopped searching before.
3841 * This may return before finding all the matches.
3842 * Return the total number of matches or -1 if still unknown -- Acevedo
3843 */
3844 static int
3845ins_compl_get_exp(pos_T *ini)
3846{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003847 static ins_compl_next_state_T st;
3848 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003849 int i;
3850 int found_new_match;
3851 int type = ctrl_x_mode;
3852
3853 if (!compl_started)
3854 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003855 buf_T *buf;
3856
3857 FOR_ALL_BUFFERS(buf)
3858 buf->b_scanned = 0;
3859 if (!st_cleared)
3860 {
3861 CLEAR_FIELD(st);
3862 st_cleared = TRUE;
3863 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003864 st.found_all = FALSE;
3865 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003866 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003867 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003868 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3869 ? (char_u *)"." : curbuf->b_p_cpt);
3870 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003871 st.last_match_pos = st.first_match_pos = *ini;
3872 }
3873 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3874 st.ins_buf = curbuf; // In case the buffer was wiped out.
3875
3876 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003877 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003878 ? &st.last_match_pos : &st.first_match_pos;
3879
3880 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3881 for (;;)
3882 {
3883 found_new_match = FAIL;
3884 st.set_match_pos = FALSE;
3885
3886 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3887 // or if found_all says this entry is done. For ^X^L only use the
3888 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003889 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003890 && (!compl_started || st.found_all))
3891 {
3892 int status = process_next_cpt_value(&st, &type, ini);
3893
3894 if (status == INS_COMPL_CPT_END)
3895 break;
3896 if (status == INS_COMPL_CPT_CONT)
3897 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003898 }
3899
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003900 // If complete() was called then compl_pattern has been reset. The
3901 // following won't work then, bail out.
3902 if (compl_pattern == NULL)
3903 break;
3904
3905 // get the next set of completion matches
3906 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003907
3908 // break the loop for specialized modes (use 'complete' just for the
3909 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3910 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003911 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3912 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003913 {
3914 if (got_int)
3915 break;
3916 // Fill the popup menu as soon as possible.
3917 if (type != -1)
3918 ins_compl_check_keys(0, FALSE);
3919
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003920 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003921 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3922 break;
3923 compl_started = TRUE;
3924 }
3925 else
3926 {
3927 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003928 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003929 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003930
3931 compl_started = FALSE;
3932 }
3933 }
3934 compl_started = TRUE;
3935
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003936 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003937 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003938 found_new_match = FAIL;
3939
3940 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003941 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003942 && !ctrl_x_mode_line_or_eval()))
3943 i = ins_compl_make_cyclic();
3944
3945 if (compl_old_match != NULL)
3946 {
3947 // If several matches were added (FORWARD) or the search failed and has
3948 // just been made cyclic then we have to move compl_curr_match to the
3949 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003950 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003951 : compl_old_match->cp_prev;
3952 if (compl_curr_match == NULL)
3953 compl_curr_match = compl_old_match;
3954 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003955 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003956
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003957 return i;
3958}
3959
3960/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003961 * Update "compl_shown_match" to the actually shown match, it may differ when
3962 * "compl_leader" is used to omit some of the matches.
3963 */
3964 static void
3965ins_compl_update_shown_match(void)
3966{
3967 while (!ins_compl_equal(compl_shown_match,
3968 compl_leader, (int)STRLEN(compl_leader))
3969 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003970 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003971 compl_shown_match = compl_shown_match->cp_next;
3972
3973 // If we didn't find it searching forward, and compl_shows_dir is
3974 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003975 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003976 && !ins_compl_equal(compl_shown_match,
3977 compl_leader, (int)STRLEN(compl_leader))
3978 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003979 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003980 {
3981 while (!ins_compl_equal(compl_shown_match,
3982 compl_leader, (int)STRLEN(compl_leader))
3983 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003984 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003985 compl_shown_match = compl_shown_match->cp_prev;
3986 }
3987}
3988
3989/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003990 * Delete the old text being completed.
3991 */
3992 void
3993ins_compl_delete(void)
3994{
3995 int col;
3996
3997 // In insert mode: Delete the typed part.
3998 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003999 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004000 if ((int)curwin->w_cursor.col > col)
4001 {
4002 if (stop_arrow() == FAIL)
4003 return;
4004 backspace_until_column(col);
4005 }
4006
4007 // TODO: is this sufficient for redrawing? Redrawing everything causes
4008 // flicker, thus we can't do that.
4009 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004010#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004011 // clear v:completed_item
4012 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004013#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004014}
4015
4016/*
4017 * Insert the new text being completed.
4018 * "in_compl_func" is TRUE when called from complete_check().
4019 */
4020 void
4021ins_compl_insert(int in_compl_func)
4022{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004023 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004024
4025 // Make sure we don't go over the end of the string, this can happen with
4026 // illegal bytes.
4027 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4028 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004029 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004030 compl_used_match = FALSE;
4031 else
4032 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004033#ifdef FEAT_EVAL
4034 {
4035 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4036
4037 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4038 }
4039#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004040 if (!in_compl_func)
4041 compl_curr_match = compl_shown_match;
4042}
4043
4044/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004045 * show the file name for the completion match (if any). Truncate the file
4046 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004047 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004048 static void
4049ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004050{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004051 char *lead = _("match in file");
4052 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4053 char_u *s;
4054 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004055
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004056 if (space <= 0)
4057 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004058
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004059 // We need the tail that fits. With double-byte encoding going
4060 // back from the end is very slow, thus go from the start and keep
4061 // the text that fits in "space" between "s" and "e".
4062 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004063 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004064 space -= ptr2cells(e);
4065 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004067 space += ptr2cells(s);
4068 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069 }
4070 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004071 msg_hist_off = TRUE;
4072 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4073 s > compl_shown_match->cp_fname ? "<" : "", s);
4074 msg((char *)IObuff);
4075 msg_hist_off = FALSE;
4076 redraw_cmdline = FALSE; // don't overwrite!
4077}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004078
glepnirdca57fb2024-06-04 22:01:21 +02004079/*
zeertzjq551d8c32024-06-05 19:53:32 +02004080 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004081 */
glepnira218cc62024-06-03 19:32:39 +02004082 static compl_T *
4083find_comp_when_fuzzy(void)
4084{
4085 int score;
4086 char_u* str;
4087 int target_idx = -1;
4088 int is_forward = compl_shows_dir_forward();
4089 int is_backward = compl_shows_dir_backward();
4090 compl_T *comp = NULL;
4091
4092 if (compl_match_array == NULL ||
4093 (is_forward && compl_selected_item == compl_match_arraysize - 1)
4094 || (is_backward && compl_selected_item == 0))
4095 return compl_first_match;
4096
4097 if (is_forward)
4098 target_idx = compl_selected_item + 1;
4099 else if (is_backward)
4100 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004101 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004102
4103 score = compl_match_array[target_idx].pum_score;
4104 str = compl_match_array[target_idx].pum_text;
4105
4106 comp = compl_first_match;
4107 do {
4108 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4109 return comp;
4110 comp = comp->cp_next;
4111 } while (comp != NULL && !is_first_match(comp));
4112
4113 return NULL;
4114}
4115
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004116/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004117 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004118 * times. The number of matches found is returned in 'num_matches'.
4119 *
4120 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4121 * get more completions. If it is FALSE, then do nothing when there are no more
4122 * completions in the given direction.
4123 *
4124 * If "advance" is TRUE, then completion will move to the first match.
4125 * Otherwise, the original text will be shown.
4126 *
4127 * Returns OK on success and -1 if the number of matches are unknown.
4128 */
4129 static int
4130find_next_completion_match(
4131 int allow_get_expansion,
4132 int todo, // repeat completion this many times
4133 int advance,
4134 int *num_matches)
4135{
4136 int found_end = FALSE;
4137 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004138 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004139 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4140 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004141
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004142 while (--todo >= 0)
4143 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004144 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004145 {
glepnira218cc62024-06-03 19:32:39 +02004146 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_next
4147 : find_comp_when_fuzzy();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004148 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004149 && (is_first_match(compl_shown_match->cp_next)
4150 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004151 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004152 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004153 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004154 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004155 found_end = is_first_match(compl_shown_match);
glepnira218cc62024-06-03 19:32:39 +02004156 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_prev
4157 : find_comp_when_fuzzy();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004158 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004159 }
4160 else
4161 {
4162 if (!allow_get_expansion)
4163 {
4164 if (advance)
4165 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004166 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004167 compl_pending -= todo + 1;
4168 else
4169 compl_pending += todo + 1;
4170 }
4171 return -1;
4172 }
4173
4174 if (!compl_no_select && advance)
4175 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004176 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004177 --compl_pending;
4178 else
4179 ++compl_pending;
4180 }
4181
4182 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004183 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004184
4185 // handle any pending completions
4186 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004187 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004188 {
4189 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4190 {
4191 compl_shown_match = compl_shown_match->cp_next;
4192 --compl_pending;
4193 }
4194 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4195 {
4196 compl_shown_match = compl_shown_match->cp_prev;
4197 ++compl_pending;
4198 }
4199 else
4200 break;
4201 }
4202 found_end = FALSE;
4203 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004204 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004205 && compl_leader != NULL
4206 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004207 compl_leader, (int)STRLEN(compl_leader))
4208 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209 ++todo;
4210 else
4211 // Remember a matching item.
4212 found_compl = compl_shown_match;
4213
4214 // Stop at the end of the list when we found a usable match.
4215 if (found_end)
4216 {
4217 if (found_compl != NULL)
4218 {
4219 compl_shown_match = found_compl;
4220 break;
4221 }
4222 todo = 1; // use first usable match after wrapping around
4223 }
4224 }
4225
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004226 return OK;
4227}
4228
4229/*
4230 * Fill in the next completion in the current direction.
4231 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4232 * get more completions. If it is FALSE, then we just do nothing when there
4233 * are no more completions in a given direction. The latter case is used when
4234 * we are still in the middle of finding completions, to allow browsing
4235 * through the ones found so far.
4236 * Return the total number of matches, or -1 if still unknown -- webb.
4237 *
4238 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4239 * compl_shown_match here.
4240 *
4241 * Note that this function may be called recursively once only. First with
4242 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4243 * calls this function with "allow_get_expansion" FALSE.
4244 */
4245 static int
4246ins_compl_next(
4247 int allow_get_expansion,
4248 int count, // repeat completion this many times; should
4249 // be at least 1
4250 int insert_match, // Insert the newly selected match
4251 int in_compl_func) // called from complete_check()
4252{
4253 int num_matches = -1;
4254 int todo = count;
4255 int advance;
4256 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004257 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004258 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004259 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4260 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004261
4262 // When user complete function return -1 for findstart which is next
4263 // time of 'always', compl_shown_match become NULL.
4264 if (compl_shown_match == NULL)
4265 return -1;
4266
glepnira218cc62024-06-03 19:32:39 +02004267 if (compl_leader != NULL
4268 && !match_at_original_text(compl_shown_match)
4269 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004270 // Update "compl_shown_match" to the actually shown match
4271 ins_compl_update_shown_match();
4272
4273 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004274 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004275 // Delete old text to be replaced
4276 ins_compl_delete();
4277
4278 // When finding the longest common text we stick at the original text,
4279 // don't let CTRL-N or CTRL-P move to the first match.
4280 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4281
4282 // When restarting the search don't insert the first match either.
4283 if (compl_restarting)
4284 {
4285 advance = FALSE;
4286 compl_restarting = FALSE;
4287 }
4288
4289 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4290 // around.
4291 if (find_next_completion_match(allow_get_expansion, todo, advance,
4292 &num_matches) == -1)
4293 return -1;
4294
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004295 if (curbuf != orig_curbuf)
4296 {
4297 // In case some completion function switched buffer, don't want to
4298 // insert the completion elsewhere.
4299 return -1;
4300 }
4301
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004302 // Insert the text of the new completion, or the compl_leader.
4303 if (compl_no_insert && !started)
4304 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004305 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004306 compl_used_match = FALSE;
4307 }
4308 else if (insert_match)
4309 {
4310 if (!compl_get_longest || compl_used_match)
4311 ins_compl_insert(in_compl_func);
4312 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004313 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004314 }
4315 else
4316 compl_used_match = FALSE;
4317
4318 if (!allow_get_expansion)
4319 {
4320 // may undisplay the popup menu first
4321 ins_compl_upd_pum();
4322
4323 if (pum_enough_matches())
4324 // Will display the popup menu, don't redraw yet to avoid flicker.
4325 pum_call_update_screen();
4326 else
4327 // Not showing the popup menu yet, redraw to show the user what was
4328 // inserted.
4329 update_screen(0);
4330
4331 // display the updated popup menu
4332 ins_compl_show_pum();
4333#ifdef FEAT_GUI
4334 if (gui.in_use)
4335 {
4336 // Show the cursor after the match, not after the redrawn text.
4337 setcursor();
4338 out_flush_cursor(FALSE, FALSE);
4339 }
4340#endif
4341
4342 // Delete old text to be replaced, since we're still searching and
4343 // don't want to match ourselves!
4344 ins_compl_delete();
4345 }
4346
4347 // Enter will select a match when the match wasn't inserted and the popup
4348 // menu is visible.
4349 if (compl_no_insert && !started)
4350 compl_enter_selects = TRUE;
4351 else
4352 compl_enter_selects = !insert_match && compl_match_array != NULL;
4353
4354 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004355 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004356 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004357
4358 return num_matches;
4359}
4360
4361/*
4362 * Call this while finding completions, to check whether the user has hit a key
4363 * that should change the currently displayed completion, or exit completion
4364 * mode. Also, when compl_pending is not zero, show a completion as soon as
4365 * possible. -- webb
4366 * "frequency" specifies out of how many calls we actually check.
4367 * "in_compl_func" is TRUE when called from complete_check(), don't set
4368 * compl_curr_match.
4369 */
4370 void
4371ins_compl_check_keys(int frequency, int in_compl_func)
4372{
4373 static int count = 0;
4374 int c;
4375
4376 // Don't check when reading keys from a script, :normal or feedkeys().
4377 // That would break the test scripts. But do check for keys when called
4378 // from complete_check().
4379 if (!in_compl_func && (using_script() || ex_normal_busy))
4380 return;
4381
4382 // Only do this at regular intervals
4383 if (++count < frequency)
4384 return;
4385 count = 0;
4386
4387 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4388 // can't do its work correctly.
4389 c = vpeekc_any();
4390 if (c != NUL)
4391 {
4392 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4393 {
4394 c = safe_vgetc(); // Eat the character
4395 compl_shows_dir = ins_compl_key2dir(c);
4396 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4397 c != K_UP && c != K_DOWN, in_compl_func);
4398 }
4399 else
4400 {
4401 // Need to get the character to have KeyTyped set. We'll put it
4402 // back with vungetc() below. But skip K_IGNORE.
4403 c = safe_vgetc();
4404 if (c != K_IGNORE)
4405 {
4406 // Don't interrupt completion when the character wasn't typed,
4407 // e.g., when doing @q to replay keys.
4408 if (c != Ctrl_R && KeyTyped)
4409 compl_interrupted = TRUE;
4410
4411 vungetc(c);
4412 }
4413 }
4414 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004415 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004416 {
4417 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4418
4419 compl_pending = 0;
4420 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4421 }
4422}
4423
4424/*
4425 * Decide the direction of Insert mode complete from the key typed.
4426 * Returns BACKWARD or FORWARD.
4427 */
4428 static int
4429ins_compl_key2dir(int c)
4430{
4431 if (c == Ctrl_P || c == Ctrl_L
4432 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4433 return BACKWARD;
4434 return FORWARD;
4435}
4436
4437/*
4438 * Return TRUE for keys that are used for completion only when the popup menu
4439 * is visible.
4440 */
4441 static int
4442ins_compl_pum_key(int c)
4443{
4444 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4445 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4446 || c == K_UP || c == K_DOWN);
4447}
4448
4449/*
4450 * Decide the number of completions to move forward.
4451 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4452 */
4453 static int
4454ins_compl_key2count(int c)
4455{
4456 int h;
4457
4458 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4459 {
4460 h = pum_get_height();
4461 if (h > 3)
4462 h -= 2; // keep some context
4463 return h;
4464 }
4465 return 1;
4466}
4467
4468/*
4469 * Return TRUE if completion with "c" should insert the match, FALSE if only
4470 * to change the currently selected completion.
4471 */
4472 static int
4473ins_compl_use_match(int c)
4474{
4475 switch (c)
4476 {
4477 case K_UP:
4478 case K_DOWN:
4479 case K_PAGEDOWN:
4480 case K_KPAGEDOWN:
4481 case K_S_DOWN:
4482 case K_PAGEUP:
4483 case K_KPAGEUP:
4484 case K_S_UP:
4485 return FALSE;
4486 }
4487 return TRUE;
4488}
4489
4490/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004491 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4492 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004493 * Sets the global variables: compl_col, compl_length, compl_pattern and
4494 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004495 * Uses the global variables: compl_cont_status and ctrl_x_mode
4496 */
4497 static int
4498get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4499{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004500 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004501 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004502 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004503 {
4504 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4505 ;
4506 compl_col += ++startcol;
4507 compl_length = curs_col - startcol;
4508 }
4509 if (p_ic)
4510 compl_pattern = str_foldcase(line + compl_col,
4511 compl_length, NULL, 0);
4512 else
4513 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4514 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004515 {
4516 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004517 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004518 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004519 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004520 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004521 {
4522 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004523 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004524
4525 // we need up to 2 extra chars for the prefix
4526 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004527 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004528 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004529 {
4530 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004531 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004532 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004533 if (!vim_iswordp(line + compl_col)
4534 || (compl_col > 0
4535 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004536 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004537 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004538 prefixlen = 0;
4539 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004540 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004541 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004542 line + compl_col, compl_length);
4543 }
4544 else if (--startcol < 0
4545 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4546 {
4547 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004548 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004549 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004550 {
4551 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004552 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004553 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004554 compl_col += curs_col;
4555 compl_length = 0;
4556 }
4557 else
4558 {
4559 // Search the point of change class of multibyte character
4560 // or not a word single byte character backward.
4561 if (has_mbyte)
4562 {
4563 int base_class;
4564 int head_off;
4565
4566 startcol -= (*mb_head_off)(line, line + startcol);
4567 base_class = mb_get_class(line + startcol);
4568 while (--startcol >= 0)
4569 {
4570 head_off = (*mb_head_off)(line, line + startcol);
4571 if (base_class != mb_get_class(line + startcol
4572 - head_off))
4573 break;
4574 startcol -= head_off;
4575 }
4576 }
4577 else
4578 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4579 ;
4580 compl_col += ++startcol;
4581 compl_length = (int)curs_col - startcol;
4582 if (compl_length == 1)
4583 {
4584 // Only match word with at least two chars -- webb
4585 // there's no need to call quote_meta,
4586 // alloc(7) is enough -- Acevedo
4587 compl_pattern = alloc(7);
4588 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004589 {
4590 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004591 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004592 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004593 STRCPY((char *)compl_pattern, "\\<");
4594 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4595 STRCAT((char *)compl_pattern, "\\k");
4596 }
4597 else
4598 {
4599 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4600 compl_length) + 2);
4601 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004602 {
4603 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004604 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004605 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004606 STRCPY((char *)compl_pattern, "\\<");
4607 (void)quote_meta(compl_pattern + 2, line + compl_col,
4608 compl_length);
4609 }
4610 }
4611
John Marriott8c85a2a2024-05-20 19:18:26 +02004612 compl_patternlen = STRLEN(compl_pattern);
4613
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004614 return OK;
4615}
4616
4617/*
4618 * Get the pattern, column and length for whole line completion or for the
4619 * complete() function.
4620 * Sets the global variables: compl_col, compl_length and compl_pattern.
4621 */
4622 static int
4623get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4624{
4625 compl_col = (colnr_T)getwhitecols(line);
4626 compl_length = (int)curs_col - (int)compl_col;
4627 if (compl_length < 0) // cursor in indent: empty pattern
4628 compl_length = 0;
4629 if (p_ic)
4630 compl_pattern = str_foldcase(line + compl_col, compl_length,
4631 NULL, 0);
4632 else
4633 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4634 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004635 {
4636 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004637 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004638 }
4639
4640 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004641
4642 return OK;
4643}
4644
4645/*
4646 * Get the pattern, column and length for filename completion.
4647 * Sets the global variables: compl_col, compl_length and compl_pattern.
4648 */
4649 static int
4650get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4651{
4652 // Go back to just before the first filename character.
4653 if (startcol > 0)
4654 {
4655 char_u *p = line + startcol;
4656
4657 MB_PTR_BACK(line, p);
4658 while (p > line && vim_isfilec(PTR2CHAR(p)))
4659 MB_PTR_BACK(line, p);
4660 if (p == line && vim_isfilec(PTR2CHAR(p)))
4661 startcol = 0;
4662 else
4663 startcol = (int)(p - line) + 1;
4664 }
4665
4666 compl_col += startcol;
4667 compl_length = (int)curs_col - startcol;
4668 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4669 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004670 {
4671 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004672 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004673 }
4674
4675 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004676
4677 return OK;
4678}
4679
4680/*
4681 * Get the pattern, column and length for command-line completion.
4682 * Sets the global variables: compl_col, compl_length and compl_pattern.
4683 */
4684 static int
4685get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4686{
4687 compl_pattern = vim_strnsave(line, curs_col);
4688 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004689 {
4690 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004691 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004692 }
4693 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004694 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004695 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004696 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4697 || compl_xp.xp_context == EXPAND_NOTHING)
4698 // No completion possible, use an empty pattern to get a
4699 // "pattern not found" message.
4700 compl_col = curs_col;
4701 else
4702 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4703 compl_length = curs_col - compl_col;
4704
4705 return OK;
4706}
4707
4708/*
4709 * Get the pattern, column and length for user defined completion ('omnifunc',
4710 * 'completefunc' and 'thesaurusfunc')
4711 * Sets the global variables: compl_col, compl_length and compl_pattern.
4712 * Uses the global variable: spell_bad_len
4713 */
4714 static int
4715get_userdefined_compl_info(colnr_T curs_col UNUSED)
4716{
4717 int ret = FAIL;
4718
4719#ifdef FEAT_COMPL_FUNC
4720 // Call user defined function 'completefunc' with "a:findstart"
4721 // set to 1 to obtain the length of text to use for completion.
4722 char_u *line;
4723 typval_T args[3];
4724 int col;
4725 char_u *funcname;
4726 pos_T pos;
4727 int save_State = State;
4728 callback_T *cb;
4729
4730 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4731 // length as a string
4732 funcname = get_complete_funcname(ctrl_x_mode);
4733 if (*funcname == NUL)
4734 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004735 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004736 ? "completefunc" : "omnifunc");
4737 return FAIL;
4738 }
4739
4740 args[0].v_type = VAR_NUMBER;
4741 args[0].vval.v_number = 1;
4742 args[1].v_type = VAR_STRING;
4743 args[1].vval.v_string = (char_u *)"";
4744 args[2].v_type = VAR_UNKNOWN;
4745 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004746 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004747 cb = get_insert_callback(ctrl_x_mode);
4748 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004749 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004750
4751 State = save_State;
4752 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004753 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004754 validate_cursor();
4755 if (!EQUAL_POS(curwin->w_cursor, pos))
4756 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004757 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004758 return FAIL;
4759 }
4760
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004761 // Return value -2 means the user complete function wants to cancel the
4762 // complete without an error, do the same if the function did not execute
4763 // successfully.
4764 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004765 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004766 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004767 if (col == -3)
4768 {
4769 ctrl_x_mode = CTRL_X_NORMAL;
4770 edit_submode = NULL;
4771 if (!shortmess(SHM_COMPLETIONMENU))
4772 msg_clr_cmdline();
4773 return FAIL;
4774 }
4775
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004776 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004777 // completion.
4778 compl_opt_refresh_always = FALSE;
4779 compl_opt_suppress_empty = FALSE;
4780
4781 if (col < 0)
4782 col = curs_col;
4783 compl_col = col;
4784 if (compl_col > curs_col)
4785 compl_col = curs_col;
4786
4787 // Setup variables for completion. Need to obtain "line" again,
4788 // it may have become invalid.
4789 line = ml_get(curwin->w_cursor.lnum);
4790 compl_length = curs_col - compl_col;
4791 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4792 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004793 {
4794 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004795 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004796 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004797
John Marriott8c85a2a2024-05-20 19:18:26 +02004798 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004799 ret = OK;
4800#endif
4801
4802 return ret;
4803}
4804
4805/*
4806 * Get the pattern, column and length for spell completion.
4807 * Sets the global variables: compl_col, compl_length and compl_pattern.
4808 * Uses the global variable: spell_bad_len
4809 */
4810 static int
4811get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4812{
4813 int ret = FAIL;
4814#ifdef FEAT_SPELL
4815 char_u *line;
4816
4817 if (spell_bad_len > 0)
4818 compl_col = curs_col - spell_bad_len;
4819 else
4820 compl_col = spell_word_start(startcol);
4821 if (compl_col >= (colnr_T)startcol)
4822 {
4823 compl_length = 0;
4824 compl_col = curs_col;
4825 }
4826 else
4827 {
4828 spell_expand_check_cap(compl_col);
4829 compl_length = (int)curs_col - compl_col;
4830 }
4831 // Need to obtain "line" again, it may have become invalid.
4832 line = ml_get(curwin->w_cursor.lnum);
4833 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4834 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004835 {
4836 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004837 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004838 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004839
John Marriott8c85a2a2024-05-20 19:18:26 +02004840 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004841 ret = OK;
4842#endif
4843
4844 return ret;
4845}
4846
4847/*
4848 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004849 * "startcol" - start column number of the completion pattern/text
4850 * "cur_col" - current cursor column
4851 * On return, "line_invalid" is set to TRUE, if the current line may have
4852 * become invalid and needs to be fetched again.
4853 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004854 */
4855 static int
4856compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4857{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004858 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004859 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4860 && !thesaurus_func_complete(ctrl_x_mode)))
4861 {
4862 return get_normal_compl_info(line, startcol, curs_col);
4863 }
4864 else if (ctrl_x_mode_line_or_eval())
4865 {
4866 return get_wholeline_compl_info(line, curs_col);
4867 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004868 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004869 {
4870 return get_filename_compl_info(line, startcol, curs_col);
4871 }
4872 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4873 {
4874 return get_cmdline_compl_info(line, curs_col);
4875 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004876 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004877 || thesaurus_func_complete(ctrl_x_mode))
4878 {
4879 if (get_userdefined_compl_info(curs_col) == FAIL)
4880 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004881 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004882 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004883 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004884 {
4885 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4886 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004887 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004888 }
4889 else
4890 {
4891 internal_error("ins_complete()");
4892 return FAIL;
4893 }
4894
4895 return OK;
4896}
4897
4898/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004899 * Continue an interrupted completion mode search in "line".
4900 *
4901 * If this same ctrl_x_mode has been interrupted use the text from
4902 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4903 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4904 * the same line as the cursor then fix it (the line has been split because it
4905 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4906 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004907 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004908 static void
4909ins_compl_continue_search(char_u *line)
4910{
4911 // it is a continued search
4912 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004913 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4914 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004915 {
4916 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4917 {
4918 // line (probably) wrapped, set compl_startpos to the
4919 // first non_blank in the line, if it is not a wordchar
4920 // include it to get a better pattern, but then we don't
4921 // want the "\\<" prefix, check it below
4922 compl_col = (colnr_T)getwhitecols(line);
4923 compl_startpos.col = compl_col;
4924 compl_startpos.lnum = curwin->w_cursor.lnum;
4925 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4926 }
4927 else
4928 {
4929 // S_IPOS was set when we inserted a word that was at the
4930 // beginning of the line, which means that we'll go to SOL
4931 // mode but first we need to redefine compl_startpos
4932 if (compl_cont_status & CONT_S_IPOS)
4933 {
4934 compl_cont_status |= CONT_SOL;
4935 compl_startpos.col = (colnr_T)(skipwhite(
4936 line + compl_length
4937 + compl_startpos.col) - line);
4938 }
4939 compl_col = compl_startpos.col;
4940 }
4941 compl_length = curwin->w_cursor.col - (int)compl_col;
4942 // IObuff is used to add a "word from the next line" would we
4943 // have enough space? just being paranoid
4944#define MIN_SPACE 75
4945 if (compl_length > (IOSIZE - MIN_SPACE))
4946 {
4947 compl_cont_status &= ~CONT_SOL;
4948 compl_length = (IOSIZE - MIN_SPACE);
4949 compl_col = curwin->w_cursor.col - compl_length;
4950 }
4951 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4952 if (compl_length < 1)
4953 compl_cont_status &= CONT_LOCAL;
4954 }
4955 else if (ctrl_x_mode_line_or_eval())
4956 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4957 else
4958 compl_cont_status = 0;
4959}
4960
4961/*
4962 * start insert mode completion
4963 */
4964 static int
4965ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004966{
4967 char_u *line;
4968 int startcol = 0; // column where searched text starts
4969 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004970 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004971 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004972 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004973
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004974 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004975
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004976 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004977 did_si = FALSE;
4978 can_si = FALSE;
4979 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004980 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004981 return FAIL;
4982
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004983 line = ml_get(curwin->w_cursor.lnum);
4984 curs_col = curwin->w_cursor.col;
4985 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004986
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004987 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4988 && compl_cont_mode == ctrl_x_mode)
4989 // this same ctrl-x_mode was interrupted previously. Continue the
4990 // completion.
4991 ins_compl_continue_search(line);
4992 else
4993 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004994
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004995 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004996 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004997 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004998 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004999 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5000 compl_cont_status = 0;
5001 compl_cont_status |= CONT_N_ADDS;
5002 compl_startpos = curwin->w_cursor;
5003 startcol = (int)curs_col;
5004 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005005 }
5006
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005007 // Work out completion pattern and original text -- webb
5008 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5009 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005010 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5011 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005012 // restore did_ai, so that adding comment leader works
5013 did_ai = save_did_ai;
5014 return FAIL;
5015 }
5016 // If "line" was changed while getting completion info get it again.
5017 if (line_invalid)
5018 line = ml_get(curwin->w_cursor.lnum);
5019
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005020 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005021 {
5022 edit_submode_pre = (char_u *)_(" Adding");
5023 if (ctrl_x_mode_line_or_eval())
5024 {
5025 // Insert a new line, keep indentation but ignore 'comments'.
5026 char_u *old = curbuf->b_p_com;
5027
5028 curbuf->b_p_com = (char_u *)"";
5029 compl_startpos.lnum = curwin->w_cursor.lnum;
5030 compl_startpos.col = compl_col;
5031 ins_eol('\r');
5032 curbuf->b_p_com = old;
5033 compl_length = 0;
5034 compl_col = curwin->w_cursor.col;
5035 }
5036 }
5037 else
5038 {
5039 edit_submode_pre = NULL;
5040 compl_startpos.col = compl_col;
5041 }
5042
5043 if (compl_cont_status & CONT_LOCAL)
5044 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5045 else
5046 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5047
5048 // If any of the original typed text has been changed we need to fix
5049 // the redo buffer.
5050 ins_compl_fixRedoBufForLeader(NULL);
5051
5052 // Always add completion for the original text.
5053 vim_free(compl_orig_text);
5054 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5055 if (p_ic)
5056 flags |= CP_ICASE;
5057 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5058 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
5059 {
5060 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005061 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005062 VIM_CLEAR(compl_orig_text);
5063 return FAIL;
5064 }
5065
5066 // showmode might reset the internal line pointers, so it must
5067 // be called before line = ml_get(), or when this address is no
5068 // longer needed. -- Acevedo.
5069 edit_submode_extra = (char_u *)_("-- Searching...");
5070 edit_submode_highl = HLF_COUNT;
5071 showmode();
5072 edit_submode_extra = NULL;
5073 out_flush();
5074
5075 return OK;
5076}
5077
5078/*
5079 * display the completion status message
5080 */
5081 static void
5082ins_compl_show_statusmsg(void)
5083{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005084 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005085 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005086 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005087 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005088 ? (char_u *)_("Hit end of paragraph")
5089 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005090 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005091 }
5092
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005093 if (edit_submode_extra == NULL)
5094 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005095 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005096 {
5097 edit_submode_extra = (char_u *)_("Back at original");
5098 edit_submode_highl = HLF_W;
5099 }
5100 else if (compl_cont_status & CONT_S_IPOS)
5101 {
5102 edit_submode_extra = (char_u *)_("Word from other line");
5103 edit_submode_highl = HLF_COUNT;
5104 }
5105 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5106 {
5107 edit_submode_extra = (char_u *)_("The only match");
5108 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005109 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005110 }
5111 else
5112 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005113#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005114 // Update completion sequence number when needed.
5115 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005116 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005117#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005118 // The match should always have a sequence number now, this is
5119 // just a safety check.
5120 if (compl_curr_match->cp_number != -1)
5121 {
5122 // Space for 10 text chars. + 2x10-digit no.s = 31.
5123 // Translations may need more than twice that.
5124 static char_u match_ref[81];
5125
5126 if (compl_matches > 0)
5127 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005128 _("match %d of %d"),
5129 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005130 else
5131 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005132 _("match %d"),
5133 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005134 edit_submode_extra = match_ref;
5135 edit_submode_highl = HLF_R;
5136 if (dollar_vcol >= 0)
5137 curs_columns(FALSE);
5138 }
5139 }
5140 }
5141
5142 // Show a message about what (completion) mode we're in.
5143 if (!compl_opt_suppress_empty)
5144 {
5145 showmode();
5146 if (!shortmess(SHM_COMPLETIONMENU))
5147 {
5148 if (edit_submode_extra != NULL)
5149 {
5150 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005151 {
5152 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005153 msg_attr((char *)edit_submode_extra,
5154 edit_submode_highl < HLF_COUNT
5155 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005156 msg_hist_off = FALSE;
5157 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005158 }
5159 else
5160 msg_clr_cmdline(); // necessary for "noshowmode"
5161 }
5162 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005163}
5164
5165/*
5166 * Do Insert mode completion.
5167 * Called when character "c" was typed, which has a meaning for completion.
5168 * Returns OK if completion was done, FAIL if something failed (out of mem).
5169 */
5170 int
5171ins_complete(int c, int enable_pum)
5172{
5173 int n;
5174 int save_w_wrow;
5175 int save_w_leftcol;
5176 int insert_match;
5177
5178 compl_direction = ins_compl_key2dir(c);
5179 insert_match = ins_compl_use_match(c);
5180
5181 if (!compl_started)
5182 {
5183 if (ins_compl_start() == FAIL)
5184 return FAIL;
5185 }
5186 else if (insert_match && stop_arrow() == FAIL)
5187 return FAIL;
5188
5189 compl_shown_match = compl_curr_match;
5190 compl_shows_dir = compl_direction;
5191
5192 // Find next match (and following matches).
5193 save_w_wrow = curwin->w_wrow;
5194 save_w_leftcol = curwin->w_leftcol;
5195 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5196
5197 // may undisplay the popup menu
5198 ins_compl_upd_pum();
5199
5200 if (n > 1) // all matches have been found
5201 compl_matches = n;
5202 compl_curr_match = compl_shown_match;
5203 compl_direction = compl_shows_dir;
5204
5205 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5206 // mode.
5207 if (got_int && !global_busy)
5208 {
5209 (void)vgetc();
5210 got_int = FALSE;
5211 }
5212
5213 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005214 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005215 {
5216 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5217 // because we couldn't expand anything at first place, but if we used
5218 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5219 // (such as M in M'exico) if not tried already. -- Acevedo
5220 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005221 || compl_status_adding()
5222 || (ctrl_x_mode_not_default()
5223 && !ctrl_x_mode_path_patterns()
5224 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005225 compl_cont_status &= ~CONT_N_ADDS;
5226 }
5227
5228 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5229 compl_cont_status |= CONT_S_IPOS;
5230 else
5231 compl_cont_status &= ~CONT_S_IPOS;
5232
5233 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005234
5235 // Show the popup menu, unless we got interrupted.
5236 if (enable_pum && !compl_interrupted)
5237 show_pum(save_w_wrow, save_w_leftcol);
5238
5239 compl_was_interrupted = compl_interrupted;
5240 compl_interrupted = FALSE;
5241
5242 return OK;
5243}
5244
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005245/*
5246 * Remove (if needed) and show the popup menu
5247 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005248 static void
5249show_pum(int prev_w_wrow, int prev_w_leftcol)
5250{
5251 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005252 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005253 RedrawingDisabled = 0;
5254
5255 // If the cursor moved or the display scrolled we need to remove the pum
5256 // first.
5257 setcursor();
5258 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5259 ins_compl_del_pum();
5260
5261 ins_compl_show_pum();
5262 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005263
5264 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005265}
5266
5267/*
5268 * Looks in the first "len" chars. of "src" for search-metachars.
5269 * If dest is not NULL the chars. are copied there quoting (with
5270 * a backslash) the metachars, and dest would be NUL terminated.
5271 * Returns the length (needed) of dest
5272 */
5273 static unsigned
5274quote_meta(char_u *dest, char_u *src, int len)
5275{
5276 unsigned m = (unsigned)len + 1; // one extra for the NUL
5277
5278 for ( ; --len >= 0; src++)
5279 {
5280 switch (*src)
5281 {
5282 case '.':
5283 case '*':
5284 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005285 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005286 break;
5287 // FALLTHROUGH
5288 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005289 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005290 break;
5291 // FALLTHROUGH
5292 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005293 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005294 break;
5295 // FALLTHROUGH
5296 case '^': // currently it's not needed.
5297 case '$':
5298 m++;
5299 if (dest != NULL)
5300 *dest++ = '\\';
5301 break;
5302 }
5303 if (dest != NULL)
5304 *dest++ = *src;
5305 // Copy remaining bytes of a multibyte character.
5306 if (has_mbyte)
5307 {
5308 int i, mb_len;
5309
5310 mb_len = (*mb_ptr2len)(src) - 1;
5311 if (mb_len > 0 && len >= mb_len)
5312 for (i = 0; i < mb_len; ++i)
5313 {
5314 --len;
5315 ++src;
5316 if (dest != NULL)
5317 *dest++ = *src;
5318 }
5319 }
5320 }
5321 if (dest != NULL)
5322 *dest = NUL;
5323
5324 return m;
5325}
5326
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005327#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005328 void
5329free_insexpand_stuff(void)
5330{
5331 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005332# ifdef FEAT_EVAL
5333 free_callback(&cfu_cb);
5334 free_callback(&ofu_cb);
5335 free_callback(&tsrfu_cb);
5336# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005337}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005338#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005339
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005340#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005341/*
5342 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5343 * spelled word, if there is one.
5344 */
5345 static void
5346spell_back_to_badword(void)
5347{
5348 pos_T tpos = curwin->w_cursor;
5349
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005350 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005351 if (curwin->w_cursor.col != tpos.col)
5352 start_arrow(&tpos);
5353}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005354#endif