blob: c420c0ed9364ea74893ffe2b7bbf843b6666d75f [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
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126/*
127 * All the current matches are stored in a list.
128 * "compl_first_match" points to the start of the list.
129 * "compl_curr_match" points to the currently selected entry.
130 * "compl_shown_match" is different from compl_curr_match during
131 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000132 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100133 */
134static compl_T *compl_first_match = NULL;
135static compl_T *compl_curr_match = NULL;
136static compl_T *compl_shown_match = NULL;
137static compl_T *compl_old_match = NULL;
138
139// After using a cursor key <Enter> selects a match in the popup menu,
140// otherwise it inserts a line break.
141static int compl_enter_selects = FALSE;
142
143// When "compl_leader" is not NULL only matches that start with this string
144// are used.
145static char_u *compl_leader = NULL;
146
147static int compl_get_longest = FALSE; // put longest common string
148 // in compl_leader
149
150static int compl_no_insert = FALSE; // FALSE: select & insert
151 // TRUE: noinsert
152static int compl_no_select = FALSE; // FALSE: select & insert
153 // TRUE: noselect
bfredl87af60c2022-09-24 11:17:51 +0100154static int compl_longest = FALSE; // FALSE: insert full match
155 // TRUE: insert longest prefix
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200179static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100180static int compl_direction = FORWARD;
181static int compl_shows_dir = FORWARD;
182static int compl_pending = 0; // > 1 for postponed CTRL-N
183static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000184// Length in bytes of the text being completed (this is deleted to be replaced
185// by the match.)
186static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100187static colnr_T compl_col = 0; // column where the text starts
188 // that is being completed
189static char_u *compl_orig_text = NULL; // text as it was before
190 // completion started
191static int compl_cont_mode = 0;
192static expand_T compl_xp;
193
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000194// List of flags for method of completion.
195static int compl_cont_status = 0;
196# define CONT_ADDING 1 // "normal" or "adding" expansion
197# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
198 // it's set only iff N_ADDS is set
199# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
200# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
201 // if so, word-wise-expansion will set SOL
202# define CONT_SOL 16 // pattern includes start of line, just for
203 // word-wise expansion, not set for ^X^L
204# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
205 // expansion, (eg use complete=.)
206
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100207static int compl_opt_refresh_always = FALSE;
208static int compl_opt_suppress_empty = FALSE;
209
Bram Moolenaar08928322020-01-04 14:32:48 +0100210static 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 +0100211static void ins_compl_longest_match(compl_T *match);
212static void ins_compl_del_pum(void);
213static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
214static char_u *find_line_end(char_u *ptr);
215static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100216static int ins_compl_need_restart(void);
217static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000218static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100219static void ins_compl_restart(void);
220static void ins_compl_set_original_text(char_u *str);
221static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
222# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
223static void ins_compl_add_list(list_T *list);
224static void ins_compl_add_dict(dict_T *dict);
225# endif
226static int ins_compl_key2dir(int c);
227static int ins_compl_pum_key(int c);
228static int ins_compl_key2count(int c);
229static void show_pum(int prev_w_wrow, int prev_w_leftcol);
230static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100231
232#ifdef FEAT_SPELL
233static void spell_back_to_badword(void);
234static int spell_bad_len = 0; // length of located bad word
235#endif
236
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100237/*
238 * CTRL-X pressed in Insert mode.
239 */
240 void
241ins_ctrl_x(void)
242{
zeertzjqdca29d92021-08-31 19:12:51 +0200243 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100244 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000245 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100246 if (compl_cont_status & CONT_N_ADDS)
247 compl_cont_status |= CONT_INTRPT;
248 else
249 compl_cont_status = 0;
250 // We're not sure which CTRL-X mode it will be yet
251 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
252 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
253 edit_submode_pre = NULL;
254 showmode();
255 }
zeertzjqdca29d92021-08-31 19:12:51 +0200256 else
257 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
258 // CTRL-V look like CTRL-N
259 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100260
LemonBoy2bf52dd2022-04-09 18:17:34 +0100261 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100262}
263
264/*
265 * Functions to check the current CTRL-X mode.
266 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000267int ctrl_x_mode_none(void)
268 { return ctrl_x_mode == 0; }
269int ctrl_x_mode_normal(void)
270 { return ctrl_x_mode == CTRL_X_NORMAL; }
271int ctrl_x_mode_scroll(void)
272 { return ctrl_x_mode == CTRL_X_SCROLL; }
273int ctrl_x_mode_whole_line(void)
274 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
275int ctrl_x_mode_files(void)
276 { return ctrl_x_mode == CTRL_X_FILES; }
277int ctrl_x_mode_tags(void)
278 { return ctrl_x_mode == CTRL_X_TAGS; }
279int ctrl_x_mode_path_patterns(void)
280 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
281int ctrl_x_mode_path_defines(void)
282 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
283int ctrl_x_mode_dictionary(void)
284 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
285int ctrl_x_mode_thesaurus(void)
286 { return ctrl_x_mode == CTRL_X_THESAURUS; }
287int ctrl_x_mode_cmdline(void)
288 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200289 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000290int ctrl_x_mode_function(void)
291 { return ctrl_x_mode == CTRL_X_FUNCTION; }
292int ctrl_x_mode_omni(void)
293 { return ctrl_x_mode == CTRL_X_OMNI; }
294int ctrl_x_mode_spell(void)
295 { return ctrl_x_mode == CTRL_X_SPELL; }
296static int ctrl_x_mode_eval(void)
297 { return ctrl_x_mode == CTRL_X_EVAL; }
298int ctrl_x_mode_line_or_eval(void)
299 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300
301/*
302 * Whether other than default completion has been selected.
303 */
304 int
305ctrl_x_mode_not_default(void)
306{
307 return ctrl_x_mode != CTRL_X_NORMAL;
308}
309
310/*
zeertzjqdca29d92021-08-31 19:12:51 +0200311 * Whether CTRL-X was typed without a following character,
312 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100313 */
314 int
315ctrl_x_mode_not_defined_yet(void)
316{
317 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
318}
319
320/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000321 * Return TRUE if currently in "normal" or "adding" insert completion matches
322 * state
323 */
324 int
325compl_status_adding(void)
326{
327 return compl_cont_status & CONT_ADDING;
328}
329
330/*
331 * Return TRUE if the completion pattern includes start of line, just for
332 * word-wise expansion.
333 */
334 int
335compl_status_sol(void)
336{
337 return compl_cont_status & CONT_SOL;
338}
339
340/*
341 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
342 */
343 int
344compl_status_local(void)
345{
346 return compl_cont_status & CONT_LOCAL;
347}
348
349/*
350 * Clear the completion status flags
351 */
352 void
353compl_status_clear(void)
354{
355 compl_cont_status = 0;
356}
357
358/*
359 * Return TRUE if completion is using the forward direction matches
360 */
361 static int
362compl_dir_forward(void)
363{
364 return compl_direction == FORWARD;
365}
366
367/*
368 * Return TRUE if currently showing forward completion matches
369 */
370 static int
371compl_shows_dir_forward(void)
372{
373 return compl_shows_dir == FORWARD;
374}
375
376/*
377 * Return TRUE if currently showing backward completion matches
378 */
379 static int
380compl_shows_dir_backward(void)
381{
382 return compl_shows_dir == BACKWARD;
383}
384
385/*
386 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100387 */
388 int
389has_compl_option(int dict_opt)
390{
391 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200392#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100393 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200394#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100396 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
397#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100398 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100399#endif
400 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100401 {
402 ctrl_x_mode = CTRL_X_NORMAL;
403 edit_submode = NULL;
404 msg_attr(dict_opt ? _("'dictionary' option is empty")
405 : _("'thesaurus' option is empty"),
406 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100407 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100408 {
409 vim_beep(BO_COMPL);
410 setcursor();
411 out_flush();
412#ifdef FEAT_EVAL
413 if (!get_vim_var_nr(VV_TESTING))
414#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100415 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100416 }
417 return FALSE;
418 }
419 return TRUE;
420}
421
422/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000423 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100424 * This depends on the current mode.
425 */
426 int
427vim_is_ctrl_x_key(int c)
428{
429 // Always allow ^R - let its results then be checked
430 if (c == Ctrl_R)
431 return TRUE;
432
433 // Accept <PageUp> and <PageDown> if the popup menu is visible.
434 if (ins_compl_pum_key(c))
435 return TRUE;
436
437 switch (ctrl_x_mode)
438 {
439 case 0: // Not in any CTRL-X mode
440 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
441 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200442 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100443 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
444 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
445 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
446 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
447 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200448 || c == Ctrl_S || c == Ctrl_K || c == 's'
449 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100450 case CTRL_X_SCROLL:
451 return (c == Ctrl_Y || c == Ctrl_E);
452 case CTRL_X_WHOLE_LINE:
453 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_FILES:
455 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_DICTIONARY:
457 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_THESAURUS:
459 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
460 case CTRL_X_TAGS:
461 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
462#ifdef FEAT_FIND_ID
463 case CTRL_X_PATH_PATTERNS:
464 return (c == Ctrl_P || c == Ctrl_N);
465 case CTRL_X_PATH_DEFINES:
466 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
467#endif
468 case CTRL_X_CMDLINE:
469 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
470 || c == Ctrl_X);
471#ifdef FEAT_COMPL_FUNC
472 case CTRL_X_FUNCTION:
473 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
474 case CTRL_X_OMNI:
475 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
476#endif
477 case CTRL_X_SPELL:
478 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
479 case CTRL_X_EVAL:
480 return (c == Ctrl_P || c == Ctrl_N);
481 }
482 internal_error("vim_is_ctrl_x_key()");
483 return FALSE;
484}
485
486/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000487 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000488 */
489 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000490match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000491{
492 return match->cp_flags & CP_ORIGINAL_TEXT;
493}
494
495/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000496 * Returns TRUE if "match" is the first match in the completion list.
497 */
498 static int
499is_first_match(compl_T *match)
500{
501 return match == compl_first_match;
502}
503
504/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100505 * Return TRUE when character "c" is part of the item currently being
506 * completed. Used to decide whether to abandon complete mode when the menu
507 * is visible.
508 */
509 int
510ins_compl_accept_char(int c)
511{
512 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
513 // When expanding an identifier only accept identifier chars.
514 return vim_isIDc(c);
515
516 switch (ctrl_x_mode)
517 {
518 case CTRL_X_FILES:
519 // When expanding file name only accept file name chars. But not
520 // path separators, so that "proto/<Tab>" expands files in
521 // "proto", not "proto/" as a whole
522 return vim_isfilec(c) && !vim_ispathsep(c);
523
524 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200525 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100526 case CTRL_X_OMNI:
527 // Command line and Omni completion can work with just about any
528 // printable character, but do stop at white space.
529 return vim_isprintc(c) && !VIM_ISWHITE(c);
530
531 case CTRL_X_WHOLE_LINE:
532 // For while line completion a space can be part of the line.
533 return vim_isprintc(c);
534 }
535 return vim_iswordc(c);
536}
537
538/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000539 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100540 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000541 */
542 static char_u *
543ins_compl_infercase_gettext(
544 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100545 int char_len,
546 int compl_char_len,
547 int min_len,
548 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000549{
550 int *wca; // Wide character array.
551 char_u *p;
552 int i, c;
553 int has_lower = FALSE;
554 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100555 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000556
557 IObuff[0] = NUL;
558
559 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100560 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000561 if (wca == NULL)
562 return IObuff;
563
564 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100565 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000566 if (has_mbyte)
567 wca[i] = mb_ptr2char_adv(&p);
568 else
569 wca[i] = *(p++);
570
571 // Rule 1: Were any chars converted to lower?
572 p = compl_orig_text;
573 for (i = 0; i < min_len; ++i)
574 {
575 if (has_mbyte)
576 c = mb_ptr2char_adv(&p);
577 else
578 c = *(p++);
579 if (MB_ISLOWER(c))
580 {
581 has_lower = TRUE;
582 if (MB_ISUPPER(wca[i]))
583 {
584 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100585 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000586 wca[i] = MB_TOLOWER(wca[i]);
587 break;
588 }
589 }
590 }
591
592 // Rule 2: No lower case, 2nd consecutive letter converted to
593 // upper case.
594 if (!has_lower)
595 {
596 p = compl_orig_text;
597 for (i = 0; i < min_len; ++i)
598 {
599 if (has_mbyte)
600 c = mb_ptr2char_adv(&p);
601 else
602 c = *(p++);
603 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
604 {
605 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100606 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000607 wca[i] = MB_TOUPPER(wca[i]);
608 break;
609 }
610 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
611 }
612 }
613
614 // Copy the original case of the part we typed.
615 p = compl_orig_text;
616 for (i = 0; i < min_len; ++i)
617 {
618 if (has_mbyte)
619 c = mb_ptr2char_adv(&p);
620 else
621 c = *(p++);
622 if (MB_ISLOWER(c))
623 wca[i] = MB_TOLOWER(wca[i]);
624 else if (MB_ISUPPER(c))
625 wca[i] = MB_TOUPPER(wca[i]);
626 }
627
628 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000629 p = IObuff;
630 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100631 ga_init2(&gap, 1, 500);
632 while (i < char_len)
633 {
634 if (gap.ga_data != NULL)
635 {
636 if (ga_grow(&gap, 10) == FAIL)
637 {
638 ga_clear(&gap);
639 return (char_u *)"[failed]";
640 }
641 p = (char_u *)gap.ga_data + gap.ga_len;
642 if (has_mbyte)
643 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
644 else
645 {
646 *p = wca[i++];
647 ++gap.ga_len;
648 }
649 }
650 else if ((p - IObuff) + 6 >= IOSIZE)
651 {
652 // Multi-byte characters can occupy up to five bytes more than
653 // ASCII characters, and we also need one byte for NUL, so when
654 // getting to six bytes from the edge of IObuff switch to using a
655 // growarray. Add the character in the next round.
656 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100657 {
658 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100659 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100660 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100661 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100662 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100663 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100664 }
665 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000666 p += (*mb_char2bytes)(wca[i++], p);
667 else
668 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100669 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000670 vim_free(wca);
671
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100672 if (gap.ga_data != NULL)
673 {
674 *tofree = gap.ga_data;
675 return gap.ga_data;
676 }
677
678 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000679 return IObuff;
680}
681
682/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100683 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
684 * case of the originally typed text is used, and the case of the completed
685 * text is inferred, ie this tries to work out what case you probably wanted
686 * the rest of the word to be in -- webb
687 */
688 int
689ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200690 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 int len,
692 int icase,
693 char_u *fname,
694 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200695 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100696{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200697 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100699 int char_len; // count multi-byte characters
700 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200702 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100703 int res;
704 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100705
706 if (p_ic && curbuf->b_p_inf && len > 0)
707 {
708 // Infer case of completed part.
709
710 // Find actual length of completion.
711 if (has_mbyte)
712 {
713 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 while (*p != NUL)
716 {
717 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719 }
720 }
721 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100722 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723
724 // Find actual length of original text.
725 if (has_mbyte)
726 {
727 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100728 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729 while (*p != NUL)
730 {
731 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 }
734 }
735 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100738 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100740 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100741
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100742 str = ins_compl_infercase_gettext(str, char_len,
743 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100744 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200745 if (cont_s_ipos)
746 flags |= CP_CONT_S_IPOS;
747 if (icase)
748 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200749
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100750 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
751 vim_free(tofree);
752 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100753}
754
755/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000756 * Add a match to the list of matches. The arguments are:
757 * str - text of the match to add
758 * len - length of "str". If -1, then the length of "str" is
759 * computed.
760 * fname - file name to associate with this match.
761 * cptext - list of strings to use with this match (for abbr, menu, info
762 * and kind)
763 * user_data - user supplied data (any vim type) for this match
764 * cdir - match direction. If 0, use "compl_direction".
765 * flags_arg - match flags (cp_flags)
766 * adup - accept this match even if it is already present.
767 * If "cdir" is FORWARD, then the match is added after the current match.
768 * Otherwise, it is added before the current match.
769 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100770 * If the given string is already in the list of completions, then return
771 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
772 * maybe because alloc() returns NULL, then FAIL is returned.
773 */
774 static int
775ins_compl_add(
776 char_u *str,
777 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100778 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 char_u **cptext, // extra text for popup menu or NULL
780 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100781 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200782 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100783 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784{
785 compl_T *match;
786 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200787 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788
Bram Moolenaarceb06192021-04-04 15:05:22 +0200789 if (flags & CP_FAST)
790 fast_breakcheck();
791 else
792 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100793 if (got_int)
794 return FAIL;
795 if (len < 0)
796 len = (int)STRLEN(str);
797
798 // If the same match is already present, don't add it.
799 if (compl_first_match != NULL && !adup)
800 {
801 match = compl_first_match;
802 do
803 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000804 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100806 && ((int)STRLEN(match->cp_str) <= len
807 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 return NOTDONE;
809 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000810 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100811 }
812
813 // Remove any popup menu before changing the list of matches.
814 ins_compl_del_pum();
815
816 // Allocate a new match structure.
817 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200818 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 if (match == NULL)
820 return FAIL;
821 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200822 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 match->cp_number = 0;
824 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
825 {
826 vim_free(match);
827 return FAIL;
828 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829
830 // match-fname is:
831 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200832 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833 // - NULL otherwise. --Acevedo
834 if (fname != NULL
835 && compl_curr_match != NULL
836 && compl_curr_match->cp_fname != NULL
837 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
838 match->cp_fname = compl_curr_match->cp_fname;
839 else if (fname != NULL)
840 {
841 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200842 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 }
844 else
845 match->cp_fname = NULL;
846 match->cp_flags = flags;
847
848 if (cptext != NULL)
849 {
850 int i;
851
852 for (i = 0; i < CPT_COUNT; ++i)
853 if (cptext[i] != NULL && *cptext[i] != NUL)
854 match->cp_text[i] = vim_strsave(cptext[i]);
855 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100856#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100857 if (user_data != NULL)
858 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100859#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100860
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000861 // Link the new match structure after (FORWARD) or before (BACKWARD) the
862 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863 if (compl_first_match == NULL)
864 match->cp_next = match->cp_prev = NULL;
865 else if (dir == FORWARD)
866 {
867 match->cp_next = compl_curr_match->cp_next;
868 match->cp_prev = compl_curr_match;
869 }
870 else // BACKWARD
871 {
872 match->cp_next = compl_curr_match;
873 match->cp_prev = compl_curr_match->cp_prev;
874 }
875 if (match->cp_next)
876 match->cp_next->cp_prev = match;
877 if (match->cp_prev)
878 match->cp_prev->cp_next = match;
879 else // if there's nothing before, it is the first match
880 compl_first_match = match;
881 compl_curr_match = match;
882
883 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200884 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100885 ins_compl_longest_match(match);
886
887 return OK;
888}
889
890/*
891 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200892 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100893 */
894 static int
895ins_compl_equal(compl_T *match, char_u *str, int len)
896{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200897 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200898 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200899 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100900 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
901 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
902}
903
904/*
905 * Reduce the longest common string for match "match".
906 */
907 static void
908ins_compl_longest_match(compl_T *match)
909{
910 char_u *p, *s;
911 int c1, c2;
912 int had_match;
913
914 if (compl_leader == NULL)
915 {
916 // First match, use it as a whole.
917 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000918 if (compl_leader == NULL)
919 return;
920
921 had_match = (curwin->w_cursor.col > compl_col);
922 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000923 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000924 ins_redraw(FALSE);
925
926 // When the match isn't there (to avoid matching itself) remove it
927 // again after redrawing.
928 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000931
932 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000934
935 // Reduce the text if this match differs from compl_leader.
936 p = compl_leader;
937 s = match->cp_str;
938 while (*p != NUL)
939 {
940 if (has_mbyte)
941 {
942 c1 = mb_ptr2char(p);
943 c2 = mb_ptr2char(s);
944 }
945 else
946 {
947 c1 = *p;
948 c2 = *s;
949 }
950 if ((match->cp_flags & CP_ICASE)
951 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
952 break;
953 if (has_mbyte)
954 {
955 MB_PTR_ADV(p);
956 MB_PTR_ADV(s);
957 }
958 else
959 {
960 ++p;
961 ++s;
962 }
963 }
964
965 if (*p != NUL)
966 {
967 // Leader was shortened, need to change the inserted text.
968 *p = NUL;
969 had_match = (curwin->w_cursor.col > compl_col);
970 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000971 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000972 ins_redraw(FALSE);
973
974 // When the match isn't there (to avoid matching itself) remove it
975 // again after redrawing.
976 if (!had_match)
977 ins_compl_delete();
978 }
979
980 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100981}
982
983/*
984 * Add an array of matches to the list of matches.
985 * Frees matches[].
986 */
987 static void
988ins_compl_add_matches(
989 int num_matches,
990 char_u **matches,
991 int icase)
992{
993 int i;
994 int add_r = OK;
995 int dir = compl_direction;
996
997 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100998 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200999 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001000 // if dir was BACKWARD then honor it just once
1001 dir = FORWARD;
1002 FreeWild(num_matches, matches);
1003}
1004
1005/*
1006 * Make the completion list cyclic.
1007 * Return the number of matches (excluding the original).
1008 */
1009 static int
1010ins_compl_make_cyclic(void)
1011{
1012 compl_T *match;
1013 int count = 0;
1014
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001015 if (compl_first_match == NULL)
1016 return 0;
1017
1018 // Find the end of the list.
1019 match = compl_first_match;
1020 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001021 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001022 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001023 match = match->cp_next;
1024 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001025 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001026 match->cp_next = compl_first_match;
1027 compl_first_match->cp_prev = match;
1028
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001029 return count;
1030}
1031
1032/*
1033 * Return whether there currently is a shown match.
1034 */
1035 int
1036ins_compl_has_shown_match(void)
1037{
1038 return compl_shown_match == NULL
1039 || compl_shown_match != compl_shown_match->cp_next;
1040}
1041
1042/*
1043 * Return whether the shown match is long enough.
1044 */
1045 int
1046ins_compl_long_shown_match(void)
1047{
1048 return (int)STRLEN(compl_shown_match->cp_str)
1049 > curwin->w_cursor.col - compl_col;
1050}
1051
1052/*
1053 * Set variables that store noselect and noinsert behavior from the
1054 * 'completeopt' value.
1055 */
1056 void
1057completeopt_was_set(void)
1058{
1059 compl_no_insert = FALSE;
1060 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001061 compl_longest = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001062 if (strstr((char *)p_cot, "noselect") != NULL)
1063 compl_no_select = TRUE;
1064 if (strstr((char *)p_cot, "noinsert") != NULL)
1065 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001066 if (strstr((char *)p_cot, "longest") != NULL)
1067 compl_longest = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001068}
1069
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001070
1071// "compl_match_array" points the currently displayed list of entries in the
1072// popup menu. It is NULL when there is no popup menu.
1073static pumitem_T *compl_match_array = NULL;
1074static int compl_match_arraysize;
1075
1076/*
1077 * Update the screen and when there is any scrolling remove the popup menu.
1078 */
1079 static void
1080ins_compl_upd_pum(void)
1081{
1082 int h;
1083
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001084 if (compl_match_array == NULL)
1085 return;
1086
1087 h = curwin->w_cline_height;
1088 // Update the screen later, before drawing the popup menu over it.
1089 pum_call_update_screen();
1090 if (h != curwin->w_cline_height)
1091 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092}
1093
1094/*
1095 * Remove any popup menu.
1096 */
1097 static void
1098ins_compl_del_pum(void)
1099{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001100 if (compl_match_array == NULL)
1101 return;
1102
1103 pum_undisplay();
1104 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001105}
1106
1107/*
1108 * Return TRUE if the popup menu should be displayed.
1109 */
1110 int
1111pum_wanted(void)
1112{
1113 // 'completeopt' must contain "menu" or "menuone"
1114 if (vim_strchr(p_cot, 'm') == NULL)
1115 return FALSE;
1116
1117 // The display looks bad on a B&W display.
1118 if (t_colors < 8
1119#ifdef FEAT_GUI
1120 && !gui.in_use
1121#endif
1122 )
1123 return FALSE;
1124 return TRUE;
1125}
1126
1127/*
1128 * Return TRUE if there are two or more matches to be shown in the popup menu.
1129 * One if 'completopt' contains "menuone".
1130 */
1131 static int
1132pum_enough_matches(void)
1133{
1134 compl_T *compl;
1135 int i;
1136
1137 // Don't display the popup menu if there are no matches or there is only
1138 // one (ignoring the original text).
1139 compl = compl_first_match;
1140 i = 0;
1141 do
1142 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001143 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001144 break;
1145 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001146 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001147
1148 if (strstr((char *)p_cot, "menuone") != NULL)
1149 return (i >= 1);
1150 return (i >= 2);
1151}
1152
Bram Moolenaar3075a452021-11-17 15:51:52 +00001153#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001154/*
1155 * Allocate Dict for the completed item.
1156 * { word, abbr, menu, kind, info }
1157 */
1158 static dict_T *
1159ins_compl_dict_alloc(compl_T *match)
1160{
1161 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1162
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001163 if (dict == NULL)
1164 return NULL;
1165
1166 dict_add_string(dict, "word", match->cp_str);
1167 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1168 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1169 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1170 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1171 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1172 dict_add_string(dict, "user_data", (char_u *)"");
1173 else
1174 dict_add_tv(dict, "user_data", &match->cp_user_data);
1175
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001176 return dict;
1177}
1178
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001179/*
1180 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1181 * completion menu is changed.
1182 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001183 static void
1184trigger_complete_changed_event(int cur)
1185{
1186 dict_T *v_event;
1187 dict_T *item;
1188 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001189 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001190
1191 if (recursive)
1192 return;
1193
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001194 if (cur < 0)
1195 item = dict_alloc();
1196 else
1197 item = ins_compl_dict_alloc(compl_curr_match);
1198 if (item == NULL)
1199 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001200 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 dict_add_dict(v_event, "completed_item", item);
1202 pum_set_event_info(v_event);
1203 dict_set_items_ro(v_event);
1204
1205 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001206 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001207 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001208 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001209 recursive = FALSE;
1210
Bram Moolenaar3075a452021-11-17 15:51:52 +00001211 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001213#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001214
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001215/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001216 * Build a popup menu to show the completion matches.
1217 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1218 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001219 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001220 static int
1221ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001222{
1223 compl_T *compl;
1224 compl_T *shown_compl = NULL;
1225 int did_find_shown_match = FALSE;
1226 int shown_match_ok = FALSE;
1227 int i;
1228 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001229 int lead_len = 0;
1230
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001231 // Need to build the popup menu list.
1232 compl_match_arraysize = 0;
1233 compl = compl_first_match;
1234 if (compl_leader != NULL)
1235 lead_len = (int)STRLEN(compl_leader);
1236
1237 do
1238 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001239 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001240 && (compl_leader == NULL
1241 || ins_compl_equal(compl, compl_leader, lead_len)))
1242 ++compl_match_arraysize;
1243 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001244 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001245
1246 if (compl_match_arraysize == 0)
1247 return -1;
1248
1249 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1250 if (compl_match_array == NULL)
1251 return -1;
1252
1253 // If the current match is the original text don't find the first
1254 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001255 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001256 shown_match_ok = TRUE;
1257
1258 i = 0;
1259 compl = compl_first_match;
1260 do
1261 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001262 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001263 && (compl_leader == NULL
1264 || ins_compl_equal(compl, compl_leader, lead_len)))
1265 {
1266 if (!shown_match_ok)
1267 {
1268 if (compl == compl_shown_match || did_find_shown_match)
1269 {
1270 // This item is the shown match or this is the
1271 // first displayed item after the shown match.
1272 compl_shown_match = compl;
1273 did_find_shown_match = TRUE;
1274 shown_match_ok = TRUE;
1275 }
1276 else
1277 // Remember this displayed match for when the
1278 // shown match is just below it.
1279 shown_compl = compl;
1280 cur = i;
1281 }
1282
1283 if (compl->cp_text[CPT_ABBR] != NULL)
1284 compl_match_array[i].pum_text =
1285 compl->cp_text[CPT_ABBR];
1286 else
1287 compl_match_array[i].pum_text = compl->cp_str;
1288 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1289 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1290 if (compl->cp_text[CPT_MENU] != NULL)
1291 compl_match_array[i++].pum_extra =
1292 compl->cp_text[CPT_MENU];
1293 else
1294 compl_match_array[i++].pum_extra = compl->cp_fname;
1295 }
1296
1297 if (compl == compl_shown_match)
1298 {
1299 did_find_shown_match = TRUE;
1300
1301 // When the original text is the shown match don't set
1302 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001303 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001304 shown_match_ok = TRUE;
1305
1306 if (!shown_match_ok && shown_compl != NULL)
1307 {
1308 // The shown match isn't displayed, set it to the
1309 // previously displayed match.
1310 compl_shown_match = shown_compl;
1311 shown_match_ok = TRUE;
1312 }
1313 }
1314 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001315 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001316
1317 if (!shown_match_ok) // no displayed match at all
1318 cur = -1;
1319
1320 return cur;
1321}
1322
1323/*
1324 * Show the popup menu for the list of matches.
1325 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1326 */
1327 void
1328ins_compl_show_pum(void)
1329{
1330 int i;
1331 int cur = -1;
1332 colnr_T col;
1333
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001334 if (!pum_wanted() || !pum_enough_matches())
1335 return;
1336
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001337 // Update the screen later, before drawing the popup menu over it.
1338 pum_call_update_screen();
1339
1340 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001341 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001342 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001343 else
1344 {
1345 // popup menu already exists, only need to find the current item.
1346 for (i = 0; i < compl_match_arraysize; ++i)
1347 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1348 || compl_match_array[i].pum_text
1349 == compl_shown_match->cp_text[CPT_ABBR])
1350 {
1351 cur = i;
1352 break;
1353 }
1354 }
1355
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001356 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001357 {
1358#ifdef FEAT_EVAL
1359 if (compl_started && has_completechanged())
1360 trigger_complete_changed_event(cur);
1361#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001362 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001363 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001364
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001365 // In Replace mode when a $ is displayed at the end of the line only
1366 // part of the screen would be updated. We do need to redraw here.
1367 dollar_vcol = -1;
1368
1369 // Compute the screen column of the start of the completed text.
1370 // Use the cursor to get all wrapping and other settings right.
1371 col = curwin->w_cursor.col;
1372 curwin->w_cursor.col = compl_col;
1373 pum_display(compl_match_array, compl_match_arraysize, cur);
1374 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001375
glepnircbb46b42024-02-03 18:11:13 +01001376 // After adding leader, set the current match to shown match.
1377 if (compl_started && compl_curr_match != compl_shown_match)
1378 compl_curr_match = compl_shown_match;
1379
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001380#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001381 if (has_completechanged())
1382 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001383#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001384}
1385
1386#define DICT_FIRST (1) // use just first element in "dict"
1387#define DICT_EXACT (2) // "dict" is the exact name of a file
1388
1389/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001390 * Add any identifiers that match the given pattern "pat" in the list of
1391 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 */
1393 static void
1394ins_compl_dictionaries(
1395 char_u *dict_start,
1396 char_u *pat,
1397 int flags, // DICT_FIRST and/or DICT_EXACT
1398 int thesaurus) // Thesaurus completion
1399{
1400 char_u *dict = dict_start;
1401 char_u *ptr;
1402 char_u *buf;
1403 regmatch_T regmatch;
1404 char_u **files;
1405 int count;
1406 int save_p_scs;
1407 int dir = compl_direction;
1408
1409 if (*dict == NUL)
1410 {
1411#ifdef FEAT_SPELL
1412 // When 'dictionary' is empty and spell checking is enabled use
1413 // "spell".
1414 if (!thesaurus && curwin->w_p_spell)
1415 dict = (char_u *)"spell";
1416 else
1417#endif
1418 return;
1419 }
1420
1421 buf = alloc(LSIZE);
1422 if (buf == NULL)
1423 return;
1424 regmatch.regprog = NULL; // so that we can goto theend
1425
1426 // If 'infercase' is set, don't use 'smartcase' here
1427 save_p_scs = p_scs;
1428 if (curbuf->b_p_inf)
1429 p_scs = FALSE;
1430
1431 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1432 // to only match at the start of a line. Otherwise just match the
1433 // pattern. Also need to double backslashes.
1434 if (ctrl_x_mode_line_or_eval())
1435 {
1436 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1437 size_t len;
1438
1439 if (pat_esc == NULL)
1440 goto theend;
1441 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001442 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001443 if (ptr == NULL)
1444 {
1445 vim_free(pat_esc);
1446 goto theend;
1447 }
1448 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1449 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1450 vim_free(pat_esc);
1451 vim_free(ptr);
1452 }
1453 else
1454 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001455 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001456 if (regmatch.regprog == NULL)
1457 goto theend;
1458 }
1459
1460 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1461 regmatch.rm_ic = ignorecase(pat);
1462 while (*dict != NUL && !got_int && !compl_interrupted)
1463 {
1464 // copy one dictionary file name into buf
1465 if (flags == DICT_EXACT)
1466 {
1467 count = 1;
1468 files = &dict;
1469 }
1470 else
1471 {
1472 // Expand wildcards in the dictionary name, but do not allow
1473 // backticks (for security, the 'dict' option may have been set in
1474 // a modeline).
1475 copy_option_part(&dict, buf, LSIZE, ",");
1476# ifdef FEAT_SPELL
1477 if (!thesaurus && STRCMP(buf, "spell") == 0)
1478 count = -1;
1479 else
1480# endif
1481 if (vim_strchr(buf, '`') != NULL
1482 || expand_wildcards(1, &buf, &count, &files,
1483 EW_FILE|EW_SILENT) != OK)
1484 count = 0;
1485 }
1486
1487# ifdef FEAT_SPELL
1488 if (count == -1)
1489 {
1490 // Complete from active spelling. Skip "\<" in the pattern, we
1491 // don't use it as a RE.
1492 if (pat[0] == '\\' && pat[1] == '<')
1493 ptr = pat + 2;
1494 else
1495 ptr = pat;
1496 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1497 }
1498 else
1499# endif
1500 if (count > 0) // avoid warning for using "files" uninit
1501 {
1502 ins_compl_files(count, files, thesaurus, flags,
1503 &regmatch, buf, &dir);
1504 if (flags != DICT_EXACT)
1505 FreeWild(count, files);
1506 }
1507 if (flags != 0)
1508 break;
1509 }
1510
1511theend:
1512 p_scs = save_p_scs;
1513 vim_regfree(regmatch.regprog);
1514 vim_free(buf);
1515}
1516
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001517/*
1518 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1519 * skipping the word at 'skip_word'. Returns OK on success.
1520 */
1521 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001522thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001523 char_u *fname,
1524 char_u **buf_arg,
1525 int dir,
1526 char_u *skip_word)
1527{
1528 int status = OK;
1529 char_u *ptr;
1530 char_u *wstart;
1531
1532 // Add the other matches on the line
1533 ptr = *buf_arg;
1534 while (!got_int)
1535 {
1536 // Find start of the next word. Skip white
1537 // space and punctuation.
1538 ptr = find_word_start(ptr);
1539 if (*ptr == NUL || *ptr == NL)
1540 break;
1541 wstart = ptr;
1542
1543 // Find end of the word.
1544 if (has_mbyte)
1545 // Japanese words may have characters in
1546 // different classes, only separate words
1547 // with single-byte non-word characters.
1548 while (*ptr != NUL)
1549 {
1550 int l = (*mb_ptr2len)(ptr);
1551
1552 if (l < 2 && !vim_iswordc(*ptr))
1553 break;
1554 ptr += l;
1555 }
1556 else
1557 ptr = find_word_end(ptr);
1558
1559 // Add the word. Skip the regexp match.
1560 if (wstart != skip_word)
1561 {
1562 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1563 fname, dir, FALSE);
1564 if (status == FAIL)
1565 break;
1566 }
1567 }
1568
1569 *buf_arg = ptr;
1570 return status;
1571}
1572
1573/*
1574 * Process "count" dictionary/thesaurus "files" and add the text matching
1575 * "regmatch".
1576 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001577 static void
1578ins_compl_files(
1579 int count,
1580 char_u **files,
1581 int thesaurus,
1582 int flags,
1583 regmatch_T *regmatch,
1584 char_u *buf,
1585 int *dir)
1586{
1587 char_u *ptr;
1588 int i;
1589 FILE *fp;
1590 int add_r;
1591
1592 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1593 {
1594 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001595 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001596 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001597 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001598 vim_snprintf((char *)IObuff, IOSIZE,
1599 _("Scanning dictionary: %s"), (char *)files[i]);
1600 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1601 }
1602
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001603 if (fp == NULL)
1604 continue;
1605
1606 // Read dictionary file line by line.
1607 // Check each line for a match.
1608 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001609 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001610 ptr = buf;
1611 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001612 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001613 ptr = regmatch->startp[0];
1614 if (ctrl_x_mode_line_or_eval())
1615 ptr = find_line_end(ptr);
1616 else
1617 ptr = find_word_end(ptr);
1618 add_r = ins_compl_add_infercase(regmatch->startp[0],
1619 (int)(ptr - regmatch->startp[0]),
1620 p_ic, files[i], *dir, FALSE);
1621 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001622 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001623 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001624 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001625 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001626 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001627 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001628 if (add_r == OK)
1629 // if dir was BACKWARD then honor it just once
1630 *dir = FORWARD;
1631 else if (add_r == FAIL)
1632 break;
1633 // avoid expensive call to vim_regexec() when at end
1634 // of line
1635 if (*ptr == '\n' || got_int)
1636 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001637 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001638 line_breakcheck();
1639 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001640 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001641 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001642 }
1643}
1644
1645/*
1646 * Find the start of the next word.
1647 * Returns a pointer to the first char of the word. Also stops at a NUL.
1648 */
1649 char_u *
1650find_word_start(char_u *ptr)
1651{
1652 if (has_mbyte)
1653 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1654 ptr += (*mb_ptr2len)(ptr);
1655 else
1656 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1657 ++ptr;
1658 return ptr;
1659}
1660
1661/*
1662 * Find the end of the word. Assumes it starts inside a word.
1663 * Returns a pointer to just after the word.
1664 */
1665 char_u *
1666find_word_end(char_u *ptr)
1667{
1668 int start_class;
1669
1670 if (has_mbyte)
1671 {
1672 start_class = mb_get_class(ptr);
1673 if (start_class > 1)
1674 while (*ptr != NUL)
1675 {
1676 ptr += (*mb_ptr2len)(ptr);
1677 if (mb_get_class(ptr) != start_class)
1678 break;
1679 }
1680 }
1681 else
1682 while (vim_iswordc(*ptr))
1683 ++ptr;
1684 return ptr;
1685}
1686
1687/*
1688 * Find the end of the line, omitting CR and NL at the end.
1689 * Returns a pointer to just after the line.
1690 */
1691 static char_u *
1692find_line_end(char_u *ptr)
1693{
1694 char_u *s;
1695
1696 s = ptr + STRLEN(ptr);
1697 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1698 --s;
1699 return s;
1700}
1701
1702/*
1703 * Free the list of completions
1704 */
1705 static void
1706ins_compl_free(void)
1707{
1708 compl_T *match;
1709 int i;
1710
1711 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001712 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 VIM_CLEAR(compl_leader);
1714
1715 if (compl_first_match == NULL)
1716 return;
1717
1718 ins_compl_del_pum();
1719 pum_clear();
1720
1721 compl_curr_match = compl_first_match;
1722 do
1723 {
1724 match = compl_curr_match;
1725 compl_curr_match = compl_curr_match->cp_next;
1726 vim_free(match->cp_str);
1727 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001728 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001729 vim_free(match->cp_fname);
1730 for (i = 0; i < CPT_COUNT; ++i)
1731 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001732#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001733 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001734#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001735 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001736 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001737 compl_first_match = compl_curr_match = NULL;
1738 compl_shown_match = NULL;
1739 compl_old_match = NULL;
1740}
1741
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001742/*
1743 * Reset/clear the completion state.
1744 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001745 void
1746ins_compl_clear(void)
1747{
1748 compl_cont_status = 0;
1749 compl_started = FALSE;
1750 compl_matches = 0;
1751 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001752 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001753 VIM_CLEAR(compl_leader);
1754 edit_submode_extra = NULL;
1755 VIM_CLEAR(compl_orig_text);
1756 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001757#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001758 // clear v:completed_item
1759 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001760#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001761}
1762
1763/*
1764 * Return TRUE when Insert completion is active.
1765 */
1766 int
1767ins_compl_active(void)
1768{
1769 return compl_started;
1770}
1771
1772/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001773 * Selected one of the matches. When FALSE the match was edited or using the
1774 * longest common string.
1775 */
1776 int
1777ins_compl_used_match(void)
1778{
1779 return compl_used_match;
1780}
1781
1782/*
1783 * Initialize get longest common string.
1784 */
1785 void
1786ins_compl_init_get_longest(void)
1787{
1788 compl_get_longest = FALSE;
1789}
1790
1791/*
1792 * Returns TRUE when insert completion is interrupted.
1793 */
1794 int
1795ins_compl_interrupted(void)
1796{
1797 return compl_interrupted;
1798}
1799
1800/*
1801 * Returns TRUE if the <Enter> key selects a match in the completion popup
1802 * menu.
1803 */
1804 int
1805ins_compl_enter_selects(void)
1806{
1807 return compl_enter_selects;
1808}
1809
1810/*
1811 * Return the column where the text starts that is being completed
1812 */
1813 colnr_T
1814ins_compl_col(void)
1815{
1816 return compl_col;
1817}
1818
1819/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001820 * Return the length in bytes of the text being completed
1821 */
1822 int
1823ins_compl_len(void)
1824{
1825 return compl_length;
1826}
1827
1828/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 * Delete one character before the cursor and show the subset of the matches
1830 * that match the word that is now before the cursor.
1831 * Returns the character to be used, NUL if the work is done and another char
1832 * to be got from the user.
1833 */
1834 int
1835ins_compl_bs(void)
1836{
1837 char_u *line;
1838 char_u *p;
1839
1840 line = ml_get_curline();
1841 p = line + curwin->w_cursor.col;
1842 MB_PTR_BACK(line, p);
1843
1844 // Stop completion when the whole word was deleted. For Omni completion
1845 // allow the word to be deleted, we won't match everything.
1846 // Respect the 'backspace' option.
1847 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001848 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1849 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001850 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1851 - compl_length < 0))
1852 return K_BS;
1853
1854 // Deleted more than what was used to find matches or didn't finish
1855 // finding all matches: need to look for matches all over again.
1856 if (curwin->w_cursor.col <= compl_col + compl_length
1857 || ins_compl_need_restart())
1858 ins_compl_restart();
1859
1860 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001861 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001862 if (compl_leader == NULL)
1863 return K_BS;
1864
1865 ins_compl_new_leader();
1866 if (compl_shown_match != NULL)
1867 // Make sure current match is not a hidden item.
1868 compl_curr_match = compl_shown_match;
1869 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001870}
1871
1872/*
1873 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1874 * be called.
1875 */
1876 static int
1877ins_compl_need_restart(void)
1878{
1879 // Return TRUE if we didn't complete finding matches or when the
1880 // 'completefunc' returned "always" in the "refresh" dictionary item.
1881 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001882 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001883 && compl_opt_refresh_always);
1884}
1885
1886/*
1887 * Called after changing "compl_leader".
1888 * Show the popup menu with a different set of matches.
1889 * May also search for matches again if the previous search was interrupted.
1890 */
1891 static void
1892ins_compl_new_leader(void)
1893{
1894 ins_compl_del_pum();
1895 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001896 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001897 compl_used_match = FALSE;
1898
1899 if (compl_started)
1900 ins_compl_set_original_text(compl_leader);
1901 else
1902 {
1903#ifdef FEAT_SPELL
1904 spell_bad_len = 0; // need to redetect bad word
1905#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001906 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001907 // the popup menu display the changed text before the cursor. Set
1908 // "compl_restarting" to avoid that the first match is inserted.
1909 pum_call_update_screen();
1910#ifdef FEAT_GUI
1911 if (gui.in_use)
1912 {
1913 // Show the cursor after the match, not after the redrawn text.
1914 setcursor();
1915 out_flush_cursor(FALSE, FALSE);
1916 }
1917#endif
1918 compl_restarting = TRUE;
1919 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1920 compl_cont_status = 0;
1921 compl_restarting = FALSE;
1922 }
1923
1924 compl_enter_selects = !compl_used_match;
1925
1926 // Show the popup menu with a different set of matches.
1927 ins_compl_show_pum();
1928
1929 // Don't let Enter select the original text when there is no popup menu.
1930 if (compl_match_array == NULL)
1931 compl_enter_selects = FALSE;
1932}
1933
1934/*
1935 * Return the length of the completion, from the completion start column to
1936 * the cursor column. Making sure it never goes below zero.
1937 */
1938 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001939get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001940{
1941 int off = (int)curwin->w_cursor.col - (int)compl_col;
1942
1943 if (off < 0)
1944 return 0;
1945 return off;
1946}
1947
1948/*
1949 * Append one character to the match leader. May reduce the number of
1950 * matches.
1951 */
1952 void
1953ins_compl_addleader(int c)
1954{
1955 int cc;
1956
1957 if (stop_arrow() == FAIL)
1958 return;
1959 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1960 {
1961 char_u buf[MB_MAXBYTES + 1];
1962
1963 (*mb_char2bytes)(c, buf);
1964 buf[cc] = NUL;
1965 ins_char_bytes(buf, cc);
1966 if (compl_opt_refresh_always)
1967 AppendToRedobuff(buf);
1968 }
1969 else
1970 {
1971 ins_char(c);
1972 if (compl_opt_refresh_always)
1973 AppendCharToRedobuff(c);
1974 }
1975
1976 // If we didn't complete finding matches we must search again.
1977 if (ins_compl_need_restart())
1978 ins_compl_restart();
1979
1980 // When 'always' is set, don't reset compl_leader. While completing,
1981 // cursor doesn't point original position, changing compl_leader would
1982 // break redo.
1983 if (!compl_opt_refresh_always)
1984 {
1985 vim_free(compl_leader);
1986 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001987 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001988 if (compl_leader != NULL)
1989 ins_compl_new_leader();
1990 }
1991}
1992
1993/*
1994 * Setup for finding completions again without leaving CTRL-X mode. Used when
1995 * BS or a key was typed while still searching for matches.
1996 */
1997 static void
1998ins_compl_restart(void)
1999{
2000 ins_compl_free();
2001 compl_started = FALSE;
2002 compl_matches = 0;
2003 compl_cont_status = 0;
2004 compl_cont_mode = 0;
2005}
2006
2007/*
2008 * Set the first match, the original text.
2009 */
2010 static void
2011ins_compl_set_original_text(char_u *str)
2012{
2013 char_u *p;
2014
2015 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002016 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2017 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002018 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002019 {
2020 p = vim_strsave(str);
2021 if (p != NULL)
2022 {
2023 vim_free(compl_first_match->cp_str);
2024 compl_first_match->cp_str = p;
2025 }
2026 }
2027 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002028 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002029 {
2030 p = vim_strsave(str);
2031 if (p != NULL)
2032 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002033 vim_free(compl_first_match->cp_prev->cp_str);
2034 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002035 }
2036 }
2037}
2038
2039/*
2040 * Append one character to the match leader. May reduce the number of
2041 * matches.
2042 */
2043 void
2044ins_compl_addfrommatch(void)
2045{
2046 char_u *p;
2047 int len = (int)curwin->w_cursor.col - (int)compl_col;
2048 int c;
2049 compl_T *cp;
2050
2051 p = compl_shown_match->cp_str;
2052 if ((int)STRLEN(p) <= len) // the match is too short
2053 {
2054 // When still at the original match use the first entry that matches
2055 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002056 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002057 return;
2058
2059 p = NULL;
2060 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002061 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002062 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002063 if (compl_leader == NULL
2064 || ins_compl_equal(cp, compl_leader,
2065 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002066 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002067 p = cp->cp_str;
2068 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002069 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002070 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002071 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002072 return;
2073 }
2074 p += len;
2075 c = PTR2CHAR(p);
2076 ins_compl_addleader(c);
2077}
2078
2079/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002080 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002081 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2082 * compl_cont_mode and compl_cont_status.
2083 * Returns TRUE when the character is not to be inserted.
2084 */
2085 static int
2086set_ctrl_x_mode(int c)
2087{
2088 int retval = FALSE;
2089
2090 switch (c)
2091 {
2092 case Ctrl_E:
2093 case Ctrl_Y:
2094 // scroll the window one line up or down
2095 ctrl_x_mode = CTRL_X_SCROLL;
2096 if (!(State & REPLACE_FLAG))
2097 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2098 else
2099 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2100 edit_submode_pre = NULL;
2101 showmode();
2102 break;
2103 case Ctrl_L:
2104 // complete whole line
2105 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2106 break;
2107 case Ctrl_F:
2108 // complete filenames
2109 ctrl_x_mode = CTRL_X_FILES;
2110 break;
2111 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002112 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002113 ctrl_x_mode = CTRL_X_DICTIONARY;
2114 break;
2115 case Ctrl_R:
2116 // Register insertion without exiting CTRL-X mode
2117 // Simply allow ^R to happen without affecting ^X mode
2118 break;
2119 case Ctrl_T:
2120 // complete words from a thesaurus
2121 ctrl_x_mode = CTRL_X_THESAURUS;
2122 break;
2123#ifdef FEAT_COMPL_FUNC
2124 case Ctrl_U:
2125 // user defined completion
2126 ctrl_x_mode = CTRL_X_FUNCTION;
2127 break;
2128 case Ctrl_O:
2129 // omni completion
2130 ctrl_x_mode = CTRL_X_OMNI;
2131 break;
2132#endif
2133 case 's':
2134 case Ctrl_S:
2135 // complete spelling suggestions
2136 ctrl_x_mode = CTRL_X_SPELL;
2137#ifdef FEAT_SPELL
2138 ++emsg_off; // Avoid getting the E756 error twice.
2139 spell_back_to_badword();
2140 --emsg_off;
2141#endif
2142 break;
2143 case Ctrl_RSB:
2144 // complete tag names
2145 ctrl_x_mode = CTRL_X_TAGS;
2146 break;
2147#ifdef FEAT_FIND_ID
2148 case Ctrl_I:
2149 case K_S_TAB:
2150 // complete keywords from included files
2151 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2152 break;
2153 case Ctrl_D:
2154 // complete definitions from included files
2155 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2156 break;
2157#endif
2158 case Ctrl_V:
2159 case Ctrl_Q:
2160 // complete vim commands
2161 ctrl_x_mode = CTRL_X_CMDLINE;
2162 break;
2163 case Ctrl_Z:
2164 // stop completion
2165 ctrl_x_mode = CTRL_X_NORMAL;
2166 edit_submode = NULL;
2167 showmode();
2168 retval = TRUE;
2169 break;
2170 case Ctrl_P:
2171 case Ctrl_N:
2172 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2173 // just started ^X mode, or there were enough ^X's to cancel
2174 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2175 // do normal expansion when interrupting a different mode (say
2176 // ^X^F^X^P or ^P^X^X^P, see below)
2177 // nothing changes if interrupting mode 0, (eg, the flag
2178 // doesn't change when going to ADDING mode -- Acevedo
2179 if (!(compl_cont_status & CONT_INTRPT))
2180 compl_cont_status |= CONT_LOCAL;
2181 else if (compl_cont_mode != 0)
2182 compl_cont_status &= ~CONT_LOCAL;
2183 // FALLTHROUGH
2184 default:
2185 // If we have typed at least 2 ^X's... for modes != 0, we set
2186 // compl_cont_status = 0 (eg, as if we had just started ^X
2187 // mode).
2188 // For mode 0, we set "compl_cont_mode" to an impossible
2189 // value, in both cases ^X^X can be used to restart the same
2190 // mode (avoiding ADDING mode).
2191 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2192 // 'complete' and local ^P expansions respectively.
2193 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2194 // mode -- Acevedo
2195 if (c == Ctrl_X)
2196 {
2197 if (compl_cont_mode != 0)
2198 compl_cont_status = 0;
2199 else
2200 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2201 }
2202 ctrl_x_mode = CTRL_X_NORMAL;
2203 edit_submode = NULL;
2204 showmode();
2205 break;
2206 }
2207
2208 return retval;
2209}
2210
2211/*
2212 * Stop insert completion mode
2213 */
2214 static int
2215ins_compl_stop(int c, int prev_mode, int retval)
2216{
2217 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002218 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002219
2220 // Get here when we have finished typing a sequence of ^N and
2221 // ^P or other completion characters in CTRL-X mode. Free up
2222 // memory that was used, and make sure we can redo the insert.
2223 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2224 {
2225 // If any of the original typed text has been changed, eg when
2226 // ignorecase is set, we must add back-spaces to the redo
2227 // buffer. We add as few as necessary to delete just the part
2228 // of the original text that has changed.
2229 // When using the longest match, edited the match or used
2230 // CTRL-E then don't use the current match.
2231 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2232 ptr = compl_curr_match->cp_str;
2233 else
2234 ptr = NULL;
2235 ins_compl_fixRedoBufForLeader(ptr);
2236 }
2237
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002238 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002239
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002240 // When completing whole lines: fix indent for 'cindent'.
2241 // Otherwise, break line if it's too long.
2242 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2243 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002244 // re-indent the current line
2245 if (want_cindent)
2246 {
2247 do_c_expr_indent();
2248 want_cindent = FALSE; // don't do it again
2249 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002250 }
2251 else
2252 {
2253 int prev_col = curwin->w_cursor.col;
2254
2255 // put the cursor on the last char, for 'tw' formatting
2256 if (prev_col > 0)
2257 dec_cursor();
2258 // only format when something was inserted
2259 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2260 insertchar(NUL, 0, -1);
2261 if (prev_col > 0
2262 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2263 inc_cursor();
2264 }
2265
2266 // If the popup menu is displayed pressing CTRL-Y means accepting
2267 // the selection without inserting anything. When
2268 // compl_enter_selects is set the Enter key does the same.
2269 if ((c == Ctrl_Y || (compl_enter_selects
2270 && (c == CAR || c == K_KENTER || c == NL)))
2271 && pum_visible())
2272 retval = TRUE;
2273
2274 // CTRL-E means completion is Ended, go back to the typed text.
2275 // but only do this, if the Popup is still visible
2276 if (c == Ctrl_E)
2277 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002278 char_u *p = NULL;
2279
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002280 ins_compl_delete();
2281 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002282 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002283 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002284 p = compl_orig_text;
2285 if (p != NULL)
2286 {
2287 int compl_len = get_compl_len();
2288 int len = (int)STRLEN(p);
2289
2290 if (len > compl_len)
2291 ins_bytes_len(p + compl_len, len - compl_len);
2292 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002293 retval = TRUE;
2294 }
2295
2296 auto_format(FALSE, TRUE);
2297
2298 // Trigger the CompleteDonePre event to give scripts a chance to
2299 // act upon the completion before clearing the info, and restore
2300 // ctrl_x_mode, so that complete_info() can be used.
2301 ctrl_x_mode = prev_mode;
2302 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2303
2304 ins_compl_free();
2305 compl_started = FALSE;
2306 compl_matches = 0;
2307 if (!shortmess(SHM_COMPLETIONMENU))
2308 msg_clr_cmdline(); // necessary for "noshowmode"
2309 ctrl_x_mode = CTRL_X_NORMAL;
2310 compl_enter_selects = FALSE;
2311 if (edit_submode != NULL)
2312 {
2313 edit_submode = NULL;
2314 showmode();
2315 }
2316
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002317 if (c == Ctrl_C && cmdwin_type != 0)
2318 // Avoid the popup menu remains displayed when leaving the
2319 // command line window.
2320 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002321 // Indent now if a key was typed that is in 'cinkeys'.
2322 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2323 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002324 // Trigger the CompleteDone event to give scripts a chance to act
2325 // upon the end of completion.
2326 ins_apply_autocmds(EVENT_COMPLETEDONE);
2327
2328 return retval;
2329}
2330
2331/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002332 * Prepare for Insert mode completion, or stop it.
2333 * Called just after typing a character in Insert mode.
2334 * Returns TRUE when the character is not to be inserted;
2335 */
2336 int
2337ins_compl_prep(int c)
2338{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002339 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002340 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002341
2342 // Forget any previous 'special' messages if this is actually
2343 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2344 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2345 edit_submode_extra = NULL;
2346
zeertzjq440d4cb2023-03-02 17:51:32 +00002347 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002348 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002349 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002350 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002351 return retval;
2352
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002353#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002354 // Ignore mouse events in a popup window
2355 if (is_mouse_key(c))
2356 {
2357 // Ignore drag and release events, the position does not need to be in
2358 // the popup and it may have just closed.
2359 if (c == K_LEFTRELEASE
2360 || c == K_LEFTRELEASE_NM
2361 || c == K_MIDDLERELEASE
2362 || c == K_RIGHTRELEASE
2363 || c == K_X1RELEASE
2364 || c == K_X2RELEASE
2365 || c == K_LEFTDRAG
2366 || c == K_MIDDLEDRAG
2367 || c == K_RIGHTDRAG
2368 || c == K_X1DRAG
2369 || c == K_X2DRAG)
2370 return retval;
2371 if (popup_visible)
2372 {
2373 int row = mouse_row;
2374 int col = mouse_col;
2375 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2376
2377 if (wp != NULL && WIN_IS_POPUP(wp))
2378 return retval;
2379 }
2380 }
2381#endif
2382
zeertzjqdca29d92021-08-31 19:12:51 +02002383 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2384 {
2385 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2386 || !vim_is_ctrl_x_key(c))
2387 {
2388 // Not starting another completion mode.
2389 ctrl_x_mode = CTRL_X_CMDLINE;
2390
2391 // CTRL-X CTRL-Z should stop completion without inserting anything
2392 if (c == Ctrl_Z)
2393 retval = TRUE;
2394 }
2395 else
2396 {
2397 ctrl_x_mode = CTRL_X_CMDLINE;
2398
2399 // Other CTRL-X keys first stop completion, then start another
2400 // completion mode.
2401 ins_compl_prep(' ');
2402 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2403 }
2404 }
2405
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002406 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002407 if (ctrl_x_mode_not_defined_yet()
2408 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002409 {
bfredl87af60c2022-09-24 11:17:51 +01002410 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002411 compl_used_match = TRUE;
2412
2413 }
2414
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002415 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002416 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2417 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002418 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002419 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002420 {
2421 // We're already in CTRL-X mode, do we stay in it?
2422 if (!vim_is_ctrl_x_key(c))
2423 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002424 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002425 ctrl_x_mode = CTRL_X_NORMAL;
2426 else
2427 ctrl_x_mode = CTRL_X_FINISHED;
2428 edit_submode = NULL;
2429 }
2430 showmode();
2431 }
2432
2433 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2434 {
2435 // Show error message from attempted keyword completion (probably
2436 // 'Pattern not found') until another key is hit, then go back to
2437 // showing what mode we are in.
2438 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002439 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002440 && c != Ctrl_R && !ins_compl_pum_key(c))
2441 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002442 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002443 }
2444 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2445 // Trigger the CompleteDone event to give scripts a chance to act
2446 // upon the (possibly failed) completion.
2447 ins_apply_autocmds(EVENT_COMPLETEDONE);
2448
LemonBoy2bf52dd2022-04-09 18:17:34 +01002449 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002450
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002451 // reset continue_* if we left expansion-mode, if we stay they'll be
2452 // (re)set properly in ins_complete()
2453 if (!vim_is_ctrl_x_key(c))
2454 {
2455 compl_cont_status = 0;
2456 compl_cont_mode = 0;
2457 }
2458
2459 return retval;
2460}
2461
2462/*
2463 * Fix the redo buffer for the completion leader replacing some of the typed
2464 * text. This inserts backspaces and appends the changed text.
2465 * "ptr" is the known leader text or NUL.
2466 */
2467 static void
2468ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2469{
2470 int len;
2471 char_u *p;
2472 char_u *ptr = ptr_arg;
2473
2474 if (ptr == NULL)
2475 {
2476 if (compl_leader != NULL)
2477 ptr = compl_leader;
2478 else
2479 return; // nothing to do
2480 }
2481 if (compl_orig_text != NULL)
2482 {
2483 p = compl_orig_text;
2484 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2485 ;
2486 if (len > 0)
2487 len -= (*mb_head_off)(p, p + len);
2488 for (p += len; *p != NUL; MB_PTR_ADV(p))
2489 AppendCharToRedobuff(K_BS);
2490 }
2491 else
2492 len = 0;
2493 if (ptr != NULL)
2494 AppendToRedobuffLit(ptr + len, -1);
2495}
2496
2497/*
2498 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2499 * (depending on flag) starting from buf and looking for a non-scanned
2500 * buffer (other than curbuf). curbuf is special, if it is called with
2501 * buf=curbuf then it has to be the first call for a given flag/expansion.
2502 *
2503 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2504 */
2505 static buf_T *
2506ins_compl_next_buf(buf_T *buf, int flag)
2507{
2508 static win_T *wp = NULL;
2509
2510 if (flag == 'w') // just windows
2511 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002512 if (buf == curbuf || !win_valid(wp))
2513 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002514 wp = curwin;
2515 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2516 && wp->w_buffer->b_scanned)
2517 ;
2518 buf = wp->w_buffer;
2519 }
2520 else
2521 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2522 // (unlisted buffers)
2523 // When completing whole lines skip unloaded buffers.
2524 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2525 && ((flag == 'U'
2526 ? buf->b_p_bl
2527 : (!buf->b_p_bl
2528 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2529 || buf->b_scanned))
2530 ;
2531 return buf;
2532}
2533
2534#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002535
2536# ifdef FEAT_EVAL
2537static callback_T cfu_cb; // 'completefunc' callback function
2538static callback_T ofu_cb; // 'omnifunc' callback function
2539static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2540# endif
2541
2542/*
2543 * Copy a global callback function to a buffer local callback.
2544 */
2545 static void
2546copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2547{
2548 free_callback(bufcb);
2549 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2550 copy_callback(bufcb, globcb);
2551}
2552
2553/*
2554 * Parse the 'completefunc' option value and set the callback function.
2555 * Invoked when the 'completefunc' option is set. The option value can be a
2556 * name of a function (string), or function(<name>) or funcref(<name>) or a
2557 * lambda expression.
2558 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002560did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002561{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002562 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2563 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002564
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002566
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002567 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002568}
2569
2570/*
2571 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002572 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002573 */
2574 void
2575set_buflocal_cfu_callback(buf_T *buf UNUSED)
2576{
2577# ifdef FEAT_EVAL
2578 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2579# endif
2580}
2581
2582/*
2583 * Parse the 'omnifunc' option value and set the callback function.
2584 * Invoked when the 'omnifunc' option is set. The option value can be a
2585 * name of a function (string), or function(<name>) or funcref(<name>) or a
2586 * lambda expression.
2587 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002588 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002589did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002590{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002591 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2592 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002593
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002594 set_buflocal_ofu_callback(curbuf);
2595 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002596}
2597
2598/*
2599 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002600 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002601 */
2602 void
2603set_buflocal_ofu_callback(buf_T *buf UNUSED)
2604{
2605# ifdef FEAT_EVAL
2606 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2607# endif
2608}
2609
2610/*
2611 * Parse the 'thesaurusfunc' option value and set the callback function.
2612 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2613 * name of a function (string), or function(<name>) or funcref(<name>) or a
2614 * lambda expression.
2615 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002617did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002618{
2619 int retval;
2620
2621 if (*curbuf->b_p_tsrfu != NUL)
2622 {
2623 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002624 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2625 &curbuf->b_tsrfu_cb);
2626 }
2627 else
2628 {
2629 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002630 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2631 }
2632
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002633 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002634}
2635
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002636/*
2637 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002638 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002639 */
2640 int
2641set_ref_in_insexpand_funcs(int copyID)
2642{
2643 int abort = FALSE;
2644
2645 abort = set_ref_in_callback(&cfu_cb, copyID);
2646 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2647 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2648
2649 return abort;
2650}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002651
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002652/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002653 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002654 */
2655 static char_u *
2656get_complete_funcname(int type)
2657{
2658 switch (type)
2659 {
2660 case CTRL_X_FUNCTION:
2661 return curbuf->b_p_cfu;
2662 case CTRL_X_OMNI:
2663 return curbuf->b_p_ofu;
2664 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002665 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002666 default:
2667 return (char_u *)"";
2668 }
2669}
2670
2671/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002672 * Get the callback to use for insert mode completion.
2673 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002674 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002675get_insert_callback(int type)
2676{
2677 if (type == CTRL_X_FUNCTION)
2678 return &curbuf->b_cfu_cb;
2679 if (type == CTRL_X_OMNI)
2680 return &curbuf->b_ofu_cb;
2681 // CTRL_X_THESAURUS
2682 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2683}
2684
2685/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002686 * Execute user defined complete function 'completefunc', 'omnifunc' or
2687 * 'thesaurusfunc', and get matches in "matches".
2688 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002689 */
2690 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002691expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002692{
2693 list_T *matchlist = NULL;
2694 dict_T *matchdict = NULL;
2695 typval_T args[3];
2696 char_u *funcname;
2697 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002698 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002699 typval_T rettv;
2700 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002701 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002702
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002703 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002704 if (*funcname == NUL)
2705 return;
2706
2707 // Call 'completefunc' to obtain the list of matches.
2708 args[0].v_type = VAR_NUMBER;
2709 args[0].vval.v_number = 0;
2710 args[1].v_type = VAR_STRING;
2711 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2712 args[2].v_type = VAR_UNKNOWN;
2713
2714 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002715 // Lock the text to avoid weird things from happening. Also disallow
2716 // switching to another window, it should not be needed and may end up in
2717 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002718 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002719
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002720 cb = get_insert_callback(type);
2721 retval = call_callback(cb, 0, &rettv, 2, args);
2722
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002723 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002724 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002725 {
2726 switch (rettv.v_type)
2727 {
2728 case VAR_LIST:
2729 matchlist = rettv.vval.v_list;
2730 break;
2731 case VAR_DICT:
2732 matchdict = rettv.vval.v_dict;
2733 break;
2734 case VAR_SPECIAL:
2735 if (rettv.vval.v_number == VVAL_NONE)
2736 compl_opt_suppress_empty = TRUE;
2737 // FALLTHROUGH
2738 default:
2739 // TODO: Give error message?
2740 clear_tv(&rettv);
2741 break;
2742 }
2743 }
zeertzjqcfe45652022-05-27 17:26:55 +01002744 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002745
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002746 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002747 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002748 validate_cursor();
2749 if (!EQUAL_POS(curwin->w_cursor, pos))
2750 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002751 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 goto theend;
2753 }
2754
2755 if (matchlist != NULL)
2756 ins_compl_add_list(matchlist);
2757 else if (matchdict != NULL)
2758 ins_compl_add_dict(matchdict);
2759
2760theend:
2761 // Restore State, it might have been changed.
2762 State = save_State;
2763
2764 if (matchdict != NULL)
2765 dict_unref(matchdict);
2766 if (matchlist != NULL)
2767 list_unref(matchlist);
2768}
2769#endif // FEAT_COMPL_FUNC
2770
2771#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2772/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002773 * Add a match to the list of matches from a typeval_T.
2774 * If the given string is already in the list of completions, then return
2775 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2776 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002777 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002778 */
2779 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002780ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002781{
2782 char_u *word;
2783 int dup = FALSE;
2784 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002785 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002786 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002787 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002788 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002789
Bram Moolenaar08928322020-01-04 14:32:48 +01002790 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002791 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2792 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002793 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2794 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2795 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2796 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2797 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2798 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2799 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2800 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002801 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002802 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2803 dup = dict_get_number(tv->vval.v_dict, "dup");
2804 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2805 empty = dict_get_number(tv->vval.v_dict, "empty");
2806 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2807 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002808 flags |= CP_EQUAL;
2809 }
2810 else
2811 {
2812 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002813 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002814 }
2815 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002816 {
2817 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002818 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002819 }
2820 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2821 if (status != OK)
2822 clear_tv(&user_data);
2823 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002824}
2825
2826/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002827 * Add completions from a list.
2828 */
2829 static void
2830ins_compl_add_list(list_T *list)
2831{
2832 listitem_T *li;
2833 int dir = compl_direction;
2834
2835 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002836 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002837 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002838 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002839 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002840 // if dir was BACKWARD then honor it just once
2841 dir = FORWARD;
2842 else if (did_emsg)
2843 break;
2844 }
2845}
2846
2847/*
2848 * Add completions from a dict.
2849 */
2850 static void
2851ins_compl_add_dict(dict_T *dict)
2852{
2853 dictitem_T *di_refresh;
2854 dictitem_T *di_words;
2855
2856 // Check for optional "refresh" item.
2857 compl_opt_refresh_always = FALSE;
2858 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2859 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2860 {
2861 char_u *v = di_refresh->di_tv.vval.v_string;
2862
2863 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2864 compl_opt_refresh_always = TRUE;
2865 }
2866
2867 // Add completions from a "words" list.
2868 di_words = dict_find(dict, (char_u *)"words", 5);
2869 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2870 ins_compl_add_list(di_words->di_tv.vval.v_list);
2871}
2872
2873/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002874 * Start completion for the complete() function.
2875 * "startcol" is where the matched text starts (1 is first column).
2876 * "list" is the list of matches.
2877 */
2878 static void
2879set_completion(colnr_T startcol, list_T *list)
2880{
2881 int save_w_wrow = curwin->w_wrow;
2882 int save_w_leftcol = curwin->w_leftcol;
2883 int flags = CP_ORIGINAL_TEXT;
2884
2885 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002886 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002887 ins_compl_prep(' ');
2888 ins_compl_clear();
2889 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002890 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002891
2892 compl_direction = FORWARD;
2893 if (startcol > curwin->w_cursor.col)
2894 startcol = curwin->w_cursor.col;
2895 compl_col = startcol;
2896 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2897 // compl_pattern doesn't need to be set
2898 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2899 if (p_ic)
2900 flags |= CP_ICASE;
2901 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002902 -1, NULL, NULL, NULL, 0,
2903 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002904 return;
2905
2906 ctrl_x_mode = CTRL_X_EVAL;
2907
2908 ins_compl_add_list(list);
2909 compl_matches = ins_compl_make_cyclic();
2910 compl_started = TRUE;
2911 compl_used_match = TRUE;
2912 compl_cont_status = 0;
2913
2914 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002915 int no_select = compl_no_select || compl_longest;
2916 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002917 {
2918 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002919 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002920 // Down/Up has no real effect.
2921 ins_complete(K_UP, FALSE);
2922 }
2923 else
2924 ins_complete(Ctrl_N, FALSE);
2925 compl_enter_selects = compl_no_insert;
2926
2927 // Lazily show the popup menu, unless we got interrupted.
2928 if (!compl_interrupted)
2929 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002930 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002931 out_flush();
2932}
2933
2934/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002935 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002936 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002937 void
2938f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002939{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002940 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002941
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002942 if (in_vim9script()
2943 && (check_for_number_arg(argvars, 0) == FAIL
2944 || check_for_list_arg(argvars, 1) == FAIL))
2945 return;
2946
Bram Moolenaar24959102022-05-07 20:01:16 +01002947 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002948 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002949 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002950 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002951 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002952
2953 // Check for undo allowed here, because if something was already inserted
2954 // the line was already saved for undo and this check isn't done.
2955 if (!undo_allowed())
2956 return;
2957
Bram Moolenaard83392a2022-09-01 12:22:46 +01002958 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002959 {
2960 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2961 if (startcol > 0)
2962 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002963 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002964}
2965
2966/*
2967 * "complete_add()" function
2968 */
2969 void
2970f_complete_add(typval_T *argvars, typval_T *rettv)
2971{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002972 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002973 return;
2974
Bram Moolenaar440cf092021-04-03 20:13:30 +02002975 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002976}
2977
2978/*
2979 * "complete_check()" function
2980 */
2981 void
2982f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2983{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002984 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002985 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002986
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002987 ins_compl_check_keys(0, TRUE);
2988 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002989
2990 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002991}
2992
2993/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002994 * Return Insert completion mode name string
2995 */
2996 static char_u *
2997ins_compl_mode(void)
2998{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002999 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003000 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3001
3002 return (char_u *)"";
3003}
3004
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003005/*
3006 * Assign the sequence number to all the completion matches which don't have
3007 * one assigned yet.
3008 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003009 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003010ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003011{
3012 int number = 0;
3013 compl_T *match;
3014
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003015 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003016 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003017 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003018 // This should normally succeed already at the first loop
3019 // cycle, so it's fast!
3020 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003021 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003022 if (match->cp_number != -1)
3023 {
3024 number = match->cp_number;
3025 break;
3026 }
3027 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003028 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003029 for (match = match->cp_next;
3030 match != NULL && match->cp_number == -1;
3031 match = match->cp_next)
3032 match->cp_number = ++number;
3033 }
3034 else // BACKWARD
3035 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003036 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003037 // number. This should normally succeed already at the
3038 // first loop cycle, so it's fast!
3039 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003040 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003041 if (match->cp_number != -1)
3042 {
3043 number = match->cp_number;
3044 break;
3045 }
3046 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003047 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003048 for (match = match->cp_prev; match
3049 && match->cp_number == -1;
3050 match = match->cp_prev)
3051 match->cp_number = ++number;
3052 }
3053}
3054
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003055/*
3056 * Get complete information
3057 */
3058 static void
3059get_complete_info(list_T *what_list, dict_T *retdict)
3060{
3061 int ret = OK;
3062 listitem_T *item;
3063#define CI_WHAT_MODE 0x01
3064#define CI_WHAT_PUM_VISIBLE 0x02
3065#define CI_WHAT_ITEMS 0x04
3066#define CI_WHAT_SELECTED 0x08
3067#define CI_WHAT_INSERTED 0x10
3068#define CI_WHAT_ALL 0xff
3069 int what_flag;
3070
3071 if (what_list == NULL)
3072 what_flag = CI_WHAT_ALL;
3073 else
3074 {
3075 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003076 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003077 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003078 {
3079 char_u *what = tv_get_string(&item->li_tv);
3080
3081 if (STRCMP(what, "mode") == 0)
3082 what_flag |= CI_WHAT_MODE;
3083 else if (STRCMP(what, "pum_visible") == 0)
3084 what_flag |= CI_WHAT_PUM_VISIBLE;
3085 else if (STRCMP(what, "items") == 0)
3086 what_flag |= CI_WHAT_ITEMS;
3087 else if (STRCMP(what, "selected") == 0)
3088 what_flag |= CI_WHAT_SELECTED;
3089 else if (STRCMP(what, "inserted") == 0)
3090 what_flag |= CI_WHAT_INSERTED;
3091 }
3092 }
3093
3094 if (ret == OK && (what_flag & CI_WHAT_MODE))
3095 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3096
3097 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3098 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3099
Girish Palya8950bf72024-03-20 20:07:29 +01003100 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003101 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003102 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003103 dict_T *di;
3104 compl_T *match;
3105 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003106
Girish Palya8950bf72024-03-20 20:07:29 +01003107 if (what_flag & CI_WHAT_ITEMS)
3108 {
3109 li = list_alloc();
3110 if (li == NULL)
3111 return;
3112 ret = dict_add_list(retdict, "items", li);
3113 }
3114 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3115 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3116 ins_compl_update_sequence_numbers();
3117 if (ret == OK && compl_first_match != NULL)
3118 {
3119 int list_idx = 0;
3120 match = compl_first_match;
3121 do
3122 {
3123 if (!match_at_original_text(match))
3124 {
3125 if (what_flag & CI_WHAT_ITEMS)
3126 {
3127 di = dict_alloc();
3128 if (di == NULL)
3129 return;
3130 ret = list_append_dict(li, di);
3131 if (ret != OK)
3132 return;
3133 dict_add_string(di, "word", match->cp_str);
3134 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3135 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3136 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3137 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3138 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3139 // Add an empty string for backwards compatibility
3140 dict_add_string(di, "user_data", (char_u *)"");
3141 else
3142 dict_add_tv(di, "user_data", &match->cp_user_data);
3143 }
3144 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3145 selected_idx = list_idx;
3146 list_idx += 1;
3147 }
3148 match = match->cp_next;
3149 }
3150 while (match != NULL && !is_first_match(match));
3151 }
3152 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3153 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003154 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003155
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003156 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3157 {
3158 // TODO
3159 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003160}
3161
3162/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003163 * "complete_info()" function
3164 */
3165 void
3166f_complete_info(typval_T *argvars, typval_T *rettv)
3167{
3168 list_T *what_list = NULL;
3169
Bram Moolenaar93a10962022-06-16 11:42:09 +01003170 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003171 return;
3172
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003173 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3174 return;
3175
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003176 if (argvars[0].v_type != VAR_UNKNOWN)
3177 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003178 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003179 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003180 what_list = argvars[0].vval.v_list;
3181 }
3182 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003183}
3184#endif
3185
3186/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003187 * Returns TRUE when using a user-defined function for thesaurus completion.
3188 */
3189 static int
3190thesaurus_func_complete(int type UNUSED)
3191{
3192#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003193 return type == CTRL_X_THESAURUS
3194 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003195#else
3196 return FALSE;
3197#endif
3198}
3199
3200/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003201 * Return value of process_next_cpt_value()
3202 */
3203enum
3204{
3205 INS_COMPL_CPT_OK = 1,
3206 INS_COMPL_CPT_CONT,
3207 INS_COMPL_CPT_END
3208};
3209
3210/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003211 * state information used for getting the next set of insert completion
3212 * matches.
3213 */
3214typedef struct
3215{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003216 char_u *e_cpt_copy; // copy of 'complete'
3217 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003218 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003219 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003220 pos_T prev_match_pos; // previous match position
3221 int set_match_pos; // save first_match_pos/last_match_pos
3222 pos_T first_match_pos; // first match position
3223 pos_T last_match_pos; // last match position
3224 int found_all; // found all matches of a certain type.
3225 char_u *dict; // dictionary file to search
3226 int dict_f; // "dict" is an exact file name or not
3227} ins_compl_next_state_T;
3228
3229/*
3230 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003231 *
3232 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003233 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003234 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003235 * st->found_all - all matches of this type are found
3236 * st->ins_buf - search for completions in this buffer
3237 * st->first_match_pos - position of the first completion match
3238 * st->last_match_pos - position of the last completion match
3239 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003240 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003241 * st->dict - name of the dictionary or thesaurus file to search
3242 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003243 *
3244 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003245 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3246 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003248 */
3249 static int
3250process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003251 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003252 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003253 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003254{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255 int compl_type = -1;
3256 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003257
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003258 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003259
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003260 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3261 st->e_cpt++;
3262
3263 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003264 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003265 st->ins_buf = curbuf;
3266 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003267 // Move the cursor back one character so that ^N can match the
3268 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003269 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003270 {
3271 // Move the cursor to after the last character in the
3272 // buffer, so that word at start of buffer is found
3273 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003274 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003275 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003277 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003278 compl_type = 0;
3279
3280 // Remember the first match so that the loop stops when we
3281 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003282 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003283 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003284 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003285 && (st->ins_buf = ins_compl_next_buf(
3286 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003287 {
3288 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003289 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003290 {
3291 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003292 st->first_match_pos.col = st->last_match_pos.col = 0;
3293 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3294 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003295 compl_type = 0;
3296 }
3297 else // unloaded buffer, scan like dictionary
3298 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003299 st->found_all = TRUE;
3300 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003301 {
3302 status = INS_COMPL_CPT_CONT;
3303 goto done;
3304 }
3305 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003306 st->dict = st->ins_buf->b_fname;
3307 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003308 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003309 if (!shortmess(SHM_COMPLETIONSCAN))
3310 {
3311 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3312 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3313 st->ins_buf->b_fname == NULL
3314 ? buf_spname(st->ins_buf)
3315 : st->ins_buf->b_sfname == NULL
3316 ? st->ins_buf->b_fname
3317 : st->ins_buf->b_sfname);
3318 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3319 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003321 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003322 status = INS_COMPL_CPT_END;
3323 else
3324 {
3325 if (ctrl_x_mode_line_or_eval())
3326 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003327 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003328 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003329 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003330 compl_type = CTRL_X_DICTIONARY;
3331 else
3332 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003335 st->dict = st->e_cpt;
3336 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003337 }
3338 }
3339#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003340 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003341 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003342 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003343 compl_type = CTRL_X_PATH_DEFINES;
3344#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003345 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003346 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003347 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003348 if (!shortmess(SHM_COMPLETIONSCAN))
3349 {
3350 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3351 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3352 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3353 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003354 }
3355 else
3356 compl_type = -1;
3357
3358 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003359 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003360
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003361 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003362 if (compl_type == -1)
3363 status = INS_COMPL_CPT_CONT;
3364 }
3365
3366done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003367 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003368 return status;
3369}
3370
3371#ifdef FEAT_FIND_ID
3372/*
3373 * Get the next set of identifiers or defines matching "compl_pattern" in
3374 * included files.
3375 */
3376 static void
3377get_next_include_file_completion(int compl_type)
3378{
3379 find_pattern_in_path(compl_pattern, compl_direction,
John Marriott8c85a2a2024-05-20 19:18:26 +02003380 compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003381 (compl_type == CTRL_X_PATH_DEFINES
3382 && !(compl_cont_status & CONT_SOL))
3383 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003384 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003385}
3386#endif
3387
3388/*
3389 * Get the next set of words matching "compl_pattern" in dictionary or
3390 * thesaurus files.
3391 */
3392 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003393get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003394{
3395#ifdef FEAT_COMPL_FUNC
3396 if (thesaurus_func_complete(compl_type))
3397 expand_by_function(compl_type, compl_pattern);
3398 else
3399#endif
3400 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003401 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003402 : (compl_type == CTRL_X_THESAURUS
3403 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3404 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3405 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003406 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003407 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408}
3409
3410/*
3411 * Get the next set of tag names matching "compl_pattern".
3412 */
3413 static void
3414get_next_tag_completion(void)
3415{
3416 int save_p_ic;
3417 char_u **matches;
3418 int num_matches;
3419
3420 // set p_ic according to p_ic, p_scs and pat for find_tags().
3421 save_p_ic = p_ic;
3422 p_ic = ignorecase(compl_pattern);
3423
3424 // Find up to TAG_MANY matches. Avoids that an enormous number
3425 // of matches is found when compl_pattern is empty
3426 g_tag_at_cursor = TRUE;
3427 if (find_tags(compl_pattern, &num_matches, &matches,
3428 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003429 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003430 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3431 ins_compl_add_matches(num_matches, matches, p_ic);
3432 g_tag_at_cursor = FALSE;
3433 p_ic = save_p_ic;
3434}
3435
3436/*
3437 * Get the next set of filename matching "compl_pattern".
3438 */
3439 static void
3440get_next_filename_completion(void)
3441{
3442 char_u **matches;
3443 int num_matches;
3444
3445 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3446 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3447 return;
3448
3449 // May change home directory back to "~".
3450 tilde_replace(compl_pattern, num_matches, matches);
3451#ifdef BACKSLASH_IN_FILENAME
3452 if (curbuf->b_p_csl[0] != NUL)
3453 {
3454 int i;
3455
3456 for (i = 0; i < num_matches; ++i)
3457 {
3458 char_u *ptr = matches[i];
3459
3460 while (*ptr != NUL)
3461 {
3462 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3463 *ptr = '/';
3464 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3465 *ptr = '\\';
3466 ptr += (*mb_ptr2len)(ptr);
3467 }
3468 }
3469 }
3470#endif
3471 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3472}
3473
3474/*
3475 * Get the next set of command-line completions matching "compl_pattern".
3476 */
3477 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003478get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003479{
3480 char_u **matches;
3481 int num_matches;
3482
3483 if (expand_cmdline(&compl_xp, compl_pattern,
John Marriott8c85a2a2024-05-20 19:18:26 +02003484 compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485 ins_compl_add_matches(num_matches, matches, FALSE);
3486}
3487
3488/*
3489 * Get the next set of spell suggestions matching "compl_pattern".
3490 */
3491 static void
3492get_next_spell_completion(linenr_T lnum UNUSED)
3493{
3494#ifdef FEAT_SPELL
3495 char_u **matches;
3496 int num_matches;
3497
3498 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3499 if (num_matches > 0)
3500 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003501 else
3502 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003503#endif
3504}
3505
3506/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003507 * Return the next word or line from buffer "ins_buf" at position
3508 * "cur_match_pos" for completion. The length of the match is set in "len".
3509 */
3510 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003511ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003512 buf_T *ins_buf, // buffer being scanned
3513 pos_T *cur_match_pos, // current match position
3514 int *match_len,
3515 int *cont_s_ipos) // next ^X<> will set initial_pos
3516{
3517 char_u *ptr;
3518 int len;
3519
3520 *match_len = 0;
3521 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3522 cur_match_pos->col;
3523 if (ctrl_x_mode_line_or_eval())
3524 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003525 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003526 {
3527 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3528 return NULL;
3529 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3530 if (!p_paste)
3531 ptr = skipwhite(ptr);
3532 }
3533 len = (int)STRLEN(ptr);
3534 }
3535 else
3536 {
3537 char_u *tmp_ptr = ptr;
3538
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003539 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003540 {
3541 tmp_ptr += compl_length;
3542 // Skip if already inside a word.
3543 if (vim_iswordp(tmp_ptr))
3544 return NULL;
3545 // Find start of next word.
3546 tmp_ptr = find_word_start(tmp_ptr);
3547 }
3548 // Find end of this word.
3549 tmp_ptr = find_word_end(tmp_ptr);
3550 len = (int)(tmp_ptr - ptr);
3551
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003552 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003553 {
3554 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3555 {
3556 // Try next line, if any. the new word will be
3557 // "join" as if the normal command "J" was used.
3558 // IOSIZE is always greater than
3559 // compl_length, so the next STRNCPY always
3560 // works -- Acevedo
3561 STRNCPY(IObuff, ptr, len);
3562 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3563 tmp_ptr = ptr = skipwhite(ptr);
3564 // Find start of next word.
3565 tmp_ptr = find_word_start(tmp_ptr);
3566 // Find end of next word.
3567 tmp_ptr = find_word_end(tmp_ptr);
3568 if (tmp_ptr > ptr)
3569 {
3570 if (*ptr != ')' && IObuff[len - 1] != TAB)
3571 {
3572 if (IObuff[len - 1] != ' ')
3573 IObuff[len++] = ' ';
3574 // IObuf =~ "\k.* ", thus len >= 2
3575 if (p_js
3576 && (IObuff[len - 2] == '.'
3577 || (vim_strchr(p_cpo, CPO_JOINSP)
3578 == NULL
3579 && (IObuff[len - 2] == '?'
3580 || IObuff[len - 2] == '!'))))
3581 IObuff[len++] = ' ';
3582 }
3583 // copy as much as possible of the new word
3584 if (tmp_ptr - ptr >= IOSIZE - len)
3585 tmp_ptr = ptr + IOSIZE - len - 1;
3586 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3587 len += (int)(tmp_ptr - ptr);
3588 *cont_s_ipos = TRUE;
3589 }
3590 IObuff[len] = NUL;
3591 ptr = IObuff;
3592 }
3593 if (len == compl_length)
3594 return NULL;
3595 }
3596 }
3597
3598 *match_len = len;
3599 return ptr;
3600}
3601
3602/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003603 * Get the next set of words matching "compl_pattern" for default completion(s)
3604 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3606 * position "st->start_pos" in the "compl_direction" direction. If
3607 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3608 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003609 * Returns OK if a new next match is found, otherwise returns FAIL.
3610 */
3611 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003612get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003613{
3614 int found_new_match = FAIL;
3615 int save_p_scs;
3616 int save_p_ws;
3617 int looped_around = FALSE;
3618 char_u *ptr;
3619 int len;
3620
3621 // If 'infercase' is set, don't use 'smartcase' here
3622 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003623 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 p_scs = FALSE;
3625
3626 // Buffers other than curbuf are scanned from the beginning or the
3627 // end but never from the middle, thus setting nowrapscan in this
3628 // buffer is a good idea, on the other hand, we always set
3629 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3630 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003631 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003632 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003633 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003634 p_ws = TRUE;
3635 looped_around = FALSE;
3636 for (;;)
3637 {
3638 int cont_s_ipos = FALSE;
3639
3640 ++msg_silent; // Don't want messages for wrapscan.
3641
3642 // ctrl_x_mode_line_or_eval() || word-wise search that
3643 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003644 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003645 found_new_match = search_for_exact_line(st->ins_buf,
3646 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003647 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003648 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003649 NULL, compl_direction, compl_pattern, compl_patternlen,
3650 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003651 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003652 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 {
3654 // set "compl_started" even on fail
3655 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003656 st->first_match_pos = *st->cur_match_pos;
3657 st->last_match_pos = *st->cur_match_pos;
3658 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003660 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3661 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003662 {
3663 found_new_match = FAIL;
3664 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003665 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003666 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3667 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3668 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669 {
3670 if (looped_around)
3671 found_new_match = FAIL;
3672 else
3673 looped_around = TRUE;
3674 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003675 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003676 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3677 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3678 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003679 {
3680 if (looped_around)
3681 found_new_match = FAIL;
3682 else
3683 looped_around = TRUE;
3684 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003685 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003686 if (found_new_match == FAIL)
3687 break;
3688
3689 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003690 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003691 && start_pos->lnum == st->cur_match_pos->lnum
3692 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003693 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003694
zeertzjq440d4cb2023-03-02 17:51:32 +00003695 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3696 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003697 if (ptr == NULL)
3698 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003699
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003700 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003701 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003702 0, cont_s_ipos) != NOTDONE)
3703 {
3704 found_new_match = OK;
3705 break;
3706 }
3707 }
3708 p_scs = save_p_scs;
3709 p_ws = save_p_ws;
3710
3711 return found_new_match;
3712}
3713
3714/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003715 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003716 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003717 */
3718 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003719get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003720{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003721 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003722
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003723 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003724 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003725 case -1:
3726 break;
3727#ifdef FEAT_FIND_ID
3728 case CTRL_X_PATH_PATTERNS:
3729 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003730 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003731 break;
3732#endif
3733
3734 case CTRL_X_DICTIONARY:
3735 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003736 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3737 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003738 break;
3739
3740 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003741 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003742 break;
3743
3744 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003745 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003746 break;
3747
3748 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003749 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003751 break;
3752
3753#ifdef FEAT_COMPL_FUNC
3754 case CTRL_X_FUNCTION:
3755 case CTRL_X_OMNI:
3756 expand_by_function(type, compl_pattern);
3757 break;
3758#endif
3759
3760 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003761 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003762 break;
3763
3764 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003765 found_new_match = get_next_default_completion(st, ini);
3766 if (found_new_match == FAIL && st->ins_buf == curbuf)
3767 st->found_all = TRUE;
3768 }
3769
3770 // check if compl_curr_match has changed, (e.g. other type of
3771 // expansion added something)
3772 if (type != 0 && compl_curr_match != compl_old_match)
3773 found_new_match = OK;
3774
3775 return found_new_match;
3776}
3777
3778/*
3779 * Get the next expansion(s), using "compl_pattern".
3780 * The search starts at position "ini" in curbuf and in the direction
3781 * compl_direction.
3782 * When "compl_started" is FALSE start at that position, otherwise continue
3783 * where we stopped searching before.
3784 * This may return before finding all the matches.
3785 * Return the total number of matches or -1 if still unknown -- Acevedo
3786 */
3787 static int
3788ins_compl_get_exp(pos_T *ini)
3789{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003790 static ins_compl_next_state_T st;
3791 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003792 int i;
3793 int found_new_match;
3794 int type = ctrl_x_mode;
3795
3796 if (!compl_started)
3797 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003798 buf_T *buf;
3799
3800 FOR_ALL_BUFFERS(buf)
3801 buf->b_scanned = 0;
3802 if (!st_cleared)
3803 {
3804 CLEAR_FIELD(st);
3805 st_cleared = TRUE;
3806 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003807 st.found_all = FALSE;
3808 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003809 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003810 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003811 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3812 ? (char_u *)"." : curbuf->b_p_cpt);
3813 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003814 st.last_match_pos = st.first_match_pos = *ini;
3815 }
3816 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3817 st.ins_buf = curbuf; // In case the buffer was wiped out.
3818
3819 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003820 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003821 ? &st.last_match_pos : &st.first_match_pos;
3822
3823 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3824 for (;;)
3825 {
3826 found_new_match = FAIL;
3827 st.set_match_pos = FALSE;
3828
3829 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3830 // or if found_all says this entry is done. For ^X^L only use the
3831 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003832 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003833 && (!compl_started || st.found_all))
3834 {
3835 int status = process_next_cpt_value(&st, &type, ini);
3836
3837 if (status == INS_COMPL_CPT_END)
3838 break;
3839 if (status == INS_COMPL_CPT_CONT)
3840 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003841 }
3842
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003843 // If complete() was called then compl_pattern has been reset. The
3844 // following won't work then, bail out.
3845 if (compl_pattern == NULL)
3846 break;
3847
3848 // get the next set of completion matches
3849 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003850
3851 // break the loop for specialized modes (use 'complete' just for the
3852 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3853 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003854 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3855 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003856 {
3857 if (got_int)
3858 break;
3859 // Fill the popup menu as soon as possible.
3860 if (type != -1)
3861 ins_compl_check_keys(0, FALSE);
3862
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003863 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003864 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3865 break;
3866 compl_started = TRUE;
3867 }
3868 else
3869 {
3870 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003871 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003872 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003873
3874 compl_started = FALSE;
3875 }
3876 }
3877 compl_started = TRUE;
3878
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003879 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003880 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003881 found_new_match = FAIL;
3882
3883 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003884 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003885 && !ctrl_x_mode_line_or_eval()))
3886 i = ins_compl_make_cyclic();
3887
3888 if (compl_old_match != NULL)
3889 {
3890 // If several matches were added (FORWARD) or the search failed and has
3891 // just been made cyclic then we have to move compl_curr_match to the
3892 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003893 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003894 : compl_old_match->cp_prev;
3895 if (compl_curr_match == NULL)
3896 compl_curr_match = compl_old_match;
3897 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003898 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003899
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900 return i;
3901}
3902
3903/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003904 * Update "compl_shown_match" to the actually shown match, it may differ when
3905 * "compl_leader" is used to omit some of the matches.
3906 */
3907 static void
3908ins_compl_update_shown_match(void)
3909{
3910 while (!ins_compl_equal(compl_shown_match,
3911 compl_leader, (int)STRLEN(compl_leader))
3912 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003913 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003914 compl_shown_match = compl_shown_match->cp_next;
3915
3916 // If we didn't find it searching forward, and compl_shows_dir is
3917 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003918 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003919 && !ins_compl_equal(compl_shown_match,
3920 compl_leader, (int)STRLEN(compl_leader))
3921 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003922 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003923 {
3924 while (!ins_compl_equal(compl_shown_match,
3925 compl_leader, (int)STRLEN(compl_leader))
3926 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003927 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003928 compl_shown_match = compl_shown_match->cp_prev;
3929 }
3930}
3931
3932/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003933 * Delete the old text being completed.
3934 */
3935 void
3936ins_compl_delete(void)
3937{
3938 int col;
3939
3940 // In insert mode: Delete the typed part.
3941 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003942 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003943 if ((int)curwin->w_cursor.col > col)
3944 {
3945 if (stop_arrow() == FAIL)
3946 return;
3947 backspace_until_column(col);
3948 }
3949
3950 // TODO: is this sufficient for redrawing? Redrawing everything causes
3951 // flicker, thus we can't do that.
3952 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003953#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003954 // clear v:completed_item
3955 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003956#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003957}
3958
3959/*
3960 * Insert the new text being completed.
3961 * "in_compl_func" is TRUE when called from complete_check().
3962 */
3963 void
3964ins_compl_insert(int in_compl_func)
3965{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003966 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003967
3968 // Make sure we don't go over the end of the string, this can happen with
3969 // illegal bytes.
3970 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3971 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003972 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003973 compl_used_match = FALSE;
3974 else
3975 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003976#ifdef FEAT_EVAL
3977 {
3978 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3979
3980 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3981 }
3982#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003983 if (!in_compl_func)
3984 compl_curr_match = compl_shown_match;
3985}
3986
3987/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 * show the file name for the completion match (if any). Truncate the file
3989 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003990 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003991 static void
3992ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003993{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003994 char *lead = _("match in file");
3995 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3996 char_u *s;
3997 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003998
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003999 if (space <= 0)
4000 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004001
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004002 // We need the tail that fits. With double-byte encoding going
4003 // back from the end is very slow, thus go from the start and keep
4004 // the text that fits in "space" between "s" and "e".
4005 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004006 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004007 space -= ptr2cells(e);
4008 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004009 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004010 space += ptr2cells(s);
4011 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004012 }
4013 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004014 msg_hist_off = TRUE;
4015 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4016 s > compl_shown_match->cp_fname ? "<" : "", s);
4017 msg((char *)IObuff);
4018 msg_hist_off = FALSE;
4019 redraw_cmdline = FALSE; // don't overwrite!
4020}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004021
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004022/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004023 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004024 * times. The number of matches found is returned in 'num_matches'.
4025 *
4026 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4027 * get more completions. If it is FALSE, then do nothing when there are no more
4028 * completions in the given direction.
4029 *
4030 * If "advance" is TRUE, then completion will move to the first match.
4031 * Otherwise, the original text will be shown.
4032 *
4033 * Returns OK on success and -1 if the number of matches are unknown.
4034 */
4035 static int
4036find_next_completion_match(
4037 int allow_get_expansion,
4038 int todo, // repeat completion this many times
4039 int advance,
4040 int *num_matches)
4041{
4042 int found_end = FALSE;
4043 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004044
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004045 while (--todo >= 0)
4046 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004047 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004048 {
4049 compl_shown_match = compl_shown_match->cp_next;
4050 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004051 && (is_first_match(compl_shown_match->cp_next)
4052 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004054 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004055 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004056 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004057 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004058 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004059 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060 }
4061 else
4062 {
4063 if (!allow_get_expansion)
4064 {
4065 if (advance)
4066 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004067 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004068 compl_pending -= todo + 1;
4069 else
4070 compl_pending += todo + 1;
4071 }
4072 return -1;
4073 }
4074
4075 if (!compl_no_select && advance)
4076 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004077 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004078 --compl_pending;
4079 else
4080 ++compl_pending;
4081 }
4082
4083 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004084 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004085
4086 // handle any pending completions
4087 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004088 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004089 {
4090 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4091 {
4092 compl_shown_match = compl_shown_match->cp_next;
4093 --compl_pending;
4094 }
4095 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4096 {
4097 compl_shown_match = compl_shown_match->cp_prev;
4098 ++compl_pending;
4099 }
4100 else
4101 break;
4102 }
4103 found_end = FALSE;
4104 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004105 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106 && compl_leader != NULL
4107 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004108 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004109 ++todo;
4110 else
4111 // Remember a matching item.
4112 found_compl = compl_shown_match;
4113
4114 // Stop at the end of the list when we found a usable match.
4115 if (found_end)
4116 {
4117 if (found_compl != NULL)
4118 {
4119 compl_shown_match = found_compl;
4120 break;
4121 }
4122 todo = 1; // use first usable match after wrapping around
4123 }
4124 }
4125
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004126 return OK;
4127}
4128
4129/*
4130 * Fill in the next completion in the current direction.
4131 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4132 * get more completions. If it is FALSE, then we just do nothing when there
4133 * are no more completions in a given direction. The latter case is used when
4134 * we are still in the middle of finding completions, to allow browsing
4135 * through the ones found so far.
4136 * Return the total number of matches, or -1 if still unknown -- webb.
4137 *
4138 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4139 * compl_shown_match here.
4140 *
4141 * Note that this function may be called recursively once only. First with
4142 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4143 * calls this function with "allow_get_expansion" FALSE.
4144 */
4145 static int
4146ins_compl_next(
4147 int allow_get_expansion,
4148 int count, // repeat completion this many times; should
4149 // be at least 1
4150 int insert_match, // Insert the newly selected match
4151 int in_compl_func) // called from complete_check()
4152{
4153 int num_matches = -1;
4154 int todo = count;
4155 int advance;
4156 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004157 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004158
4159 // When user complete function return -1 for findstart which is next
4160 // time of 'always', compl_shown_match become NULL.
4161 if (compl_shown_match == NULL)
4162 return -1;
4163
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004164 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004165 // Update "compl_shown_match" to the actually shown match
4166 ins_compl_update_shown_match();
4167
4168 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004169 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004170 // Delete old text to be replaced
4171 ins_compl_delete();
4172
4173 // When finding the longest common text we stick at the original text,
4174 // don't let CTRL-N or CTRL-P move to the first match.
4175 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4176
4177 // When restarting the search don't insert the first match either.
4178 if (compl_restarting)
4179 {
4180 advance = FALSE;
4181 compl_restarting = FALSE;
4182 }
4183
4184 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4185 // around.
4186 if (find_next_completion_match(allow_get_expansion, todo, advance,
4187 &num_matches) == -1)
4188 return -1;
4189
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004190 if (curbuf != orig_curbuf)
4191 {
4192 // In case some completion function switched buffer, don't want to
4193 // insert the completion elsewhere.
4194 return -1;
4195 }
4196
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004197 // Insert the text of the new completion, or the compl_leader.
4198 if (compl_no_insert && !started)
4199 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004200 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004201 compl_used_match = FALSE;
4202 }
4203 else if (insert_match)
4204 {
4205 if (!compl_get_longest || compl_used_match)
4206 ins_compl_insert(in_compl_func);
4207 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004208 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004209 }
4210 else
4211 compl_used_match = FALSE;
4212
4213 if (!allow_get_expansion)
4214 {
4215 // may undisplay the popup menu first
4216 ins_compl_upd_pum();
4217
4218 if (pum_enough_matches())
4219 // Will display the popup menu, don't redraw yet to avoid flicker.
4220 pum_call_update_screen();
4221 else
4222 // Not showing the popup menu yet, redraw to show the user what was
4223 // inserted.
4224 update_screen(0);
4225
4226 // display the updated popup menu
4227 ins_compl_show_pum();
4228#ifdef FEAT_GUI
4229 if (gui.in_use)
4230 {
4231 // Show the cursor after the match, not after the redrawn text.
4232 setcursor();
4233 out_flush_cursor(FALSE, FALSE);
4234 }
4235#endif
4236
4237 // Delete old text to be replaced, since we're still searching and
4238 // don't want to match ourselves!
4239 ins_compl_delete();
4240 }
4241
4242 // Enter will select a match when the match wasn't inserted and the popup
4243 // menu is visible.
4244 if (compl_no_insert && !started)
4245 compl_enter_selects = TRUE;
4246 else
4247 compl_enter_selects = !insert_match && compl_match_array != NULL;
4248
4249 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004250 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004251 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004252
4253 return num_matches;
4254}
4255
4256/*
4257 * Call this while finding completions, to check whether the user has hit a key
4258 * that should change the currently displayed completion, or exit completion
4259 * mode. Also, when compl_pending is not zero, show a completion as soon as
4260 * possible. -- webb
4261 * "frequency" specifies out of how many calls we actually check.
4262 * "in_compl_func" is TRUE when called from complete_check(), don't set
4263 * compl_curr_match.
4264 */
4265 void
4266ins_compl_check_keys(int frequency, int in_compl_func)
4267{
4268 static int count = 0;
4269 int c;
4270
4271 // Don't check when reading keys from a script, :normal or feedkeys().
4272 // That would break the test scripts. But do check for keys when called
4273 // from complete_check().
4274 if (!in_compl_func && (using_script() || ex_normal_busy))
4275 return;
4276
4277 // Only do this at regular intervals
4278 if (++count < frequency)
4279 return;
4280 count = 0;
4281
4282 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4283 // can't do its work correctly.
4284 c = vpeekc_any();
4285 if (c != NUL)
4286 {
4287 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4288 {
4289 c = safe_vgetc(); // Eat the character
4290 compl_shows_dir = ins_compl_key2dir(c);
4291 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4292 c != K_UP && c != K_DOWN, in_compl_func);
4293 }
4294 else
4295 {
4296 // Need to get the character to have KeyTyped set. We'll put it
4297 // back with vungetc() below. But skip K_IGNORE.
4298 c = safe_vgetc();
4299 if (c != K_IGNORE)
4300 {
4301 // Don't interrupt completion when the character wasn't typed,
4302 // e.g., when doing @q to replay keys.
4303 if (c != Ctrl_R && KeyTyped)
4304 compl_interrupted = TRUE;
4305
4306 vungetc(c);
4307 }
4308 }
4309 }
4310 if (compl_pending != 0 && !got_int && !compl_no_insert)
4311 {
4312 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4313
4314 compl_pending = 0;
4315 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4316 }
4317}
4318
4319/*
4320 * Decide the direction of Insert mode complete from the key typed.
4321 * Returns BACKWARD or FORWARD.
4322 */
4323 static int
4324ins_compl_key2dir(int c)
4325{
4326 if (c == Ctrl_P || c == Ctrl_L
4327 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4328 return BACKWARD;
4329 return FORWARD;
4330}
4331
4332/*
4333 * Return TRUE for keys that are used for completion only when the popup menu
4334 * is visible.
4335 */
4336 static int
4337ins_compl_pum_key(int c)
4338{
4339 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4340 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4341 || c == K_UP || c == K_DOWN);
4342}
4343
4344/*
4345 * Decide the number of completions to move forward.
4346 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4347 */
4348 static int
4349ins_compl_key2count(int c)
4350{
4351 int h;
4352
4353 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4354 {
4355 h = pum_get_height();
4356 if (h > 3)
4357 h -= 2; // keep some context
4358 return h;
4359 }
4360 return 1;
4361}
4362
4363/*
4364 * Return TRUE if completion with "c" should insert the match, FALSE if only
4365 * to change the currently selected completion.
4366 */
4367 static int
4368ins_compl_use_match(int c)
4369{
4370 switch (c)
4371 {
4372 case K_UP:
4373 case K_DOWN:
4374 case K_PAGEDOWN:
4375 case K_KPAGEDOWN:
4376 case K_S_DOWN:
4377 case K_PAGEUP:
4378 case K_KPAGEUP:
4379 case K_S_UP:
4380 return FALSE;
4381 }
4382 return TRUE;
4383}
4384
4385/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004386 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4387 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004388 * Sets the global variables: compl_col, compl_length, compl_pattern and
4389 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004390 * Uses the global variables: compl_cont_status and ctrl_x_mode
4391 */
4392 static int
4393get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4394{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004395 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004396 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004397 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004398 {
4399 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4400 ;
4401 compl_col += ++startcol;
4402 compl_length = curs_col - startcol;
4403 }
4404 if (p_ic)
4405 compl_pattern = str_foldcase(line + compl_col,
4406 compl_length, NULL, 0);
4407 else
4408 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4409 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004410 {
4411 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004412 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004413 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004414 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004415 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004416 {
4417 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004418 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004419
4420 // we need up to 2 extra chars for the prefix
4421 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004422 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004423 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004424 {
4425 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004426 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004427 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004428 if (!vim_iswordp(line + compl_col)
4429 || (compl_col > 0
4430 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004431 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004432 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004433 prefixlen = 0;
4434 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004435 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004436 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004437 line + compl_col, compl_length);
4438 }
4439 else if (--startcol < 0
4440 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4441 {
4442 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004443 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004444 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004445 {
4446 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004447 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004448 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004449 compl_col += curs_col;
4450 compl_length = 0;
4451 }
4452 else
4453 {
4454 // Search the point of change class of multibyte character
4455 // or not a word single byte character backward.
4456 if (has_mbyte)
4457 {
4458 int base_class;
4459 int head_off;
4460
4461 startcol -= (*mb_head_off)(line, line + startcol);
4462 base_class = mb_get_class(line + startcol);
4463 while (--startcol >= 0)
4464 {
4465 head_off = (*mb_head_off)(line, line + startcol);
4466 if (base_class != mb_get_class(line + startcol
4467 - head_off))
4468 break;
4469 startcol -= head_off;
4470 }
4471 }
4472 else
4473 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4474 ;
4475 compl_col += ++startcol;
4476 compl_length = (int)curs_col - startcol;
4477 if (compl_length == 1)
4478 {
4479 // Only match word with at least two chars -- webb
4480 // there's no need to call quote_meta,
4481 // alloc(7) is enough -- Acevedo
4482 compl_pattern = alloc(7);
4483 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004484 {
4485 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004486 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004487 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004488 STRCPY((char *)compl_pattern, "\\<");
4489 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4490 STRCAT((char *)compl_pattern, "\\k");
4491 }
4492 else
4493 {
4494 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4495 compl_length) + 2);
4496 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004497 {
4498 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004499 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004500 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004501 STRCPY((char *)compl_pattern, "\\<");
4502 (void)quote_meta(compl_pattern + 2, line + compl_col,
4503 compl_length);
4504 }
4505 }
4506
John Marriott8c85a2a2024-05-20 19:18:26 +02004507 compl_patternlen = STRLEN(compl_pattern);
4508
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004509 return OK;
4510}
4511
4512/*
4513 * Get the pattern, column and length for whole line completion or for the
4514 * complete() function.
4515 * Sets the global variables: compl_col, compl_length and compl_pattern.
4516 */
4517 static int
4518get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4519{
4520 compl_col = (colnr_T)getwhitecols(line);
4521 compl_length = (int)curs_col - (int)compl_col;
4522 if (compl_length < 0) // cursor in indent: empty pattern
4523 compl_length = 0;
4524 if (p_ic)
4525 compl_pattern = str_foldcase(line + compl_col, compl_length,
4526 NULL, 0);
4527 else
4528 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4529 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004530 {
4531 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004532 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004533 }
4534
4535 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004536
4537 return OK;
4538}
4539
4540/*
4541 * Get the pattern, column and length for filename completion.
4542 * Sets the global variables: compl_col, compl_length and compl_pattern.
4543 */
4544 static int
4545get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4546{
4547 // Go back to just before the first filename character.
4548 if (startcol > 0)
4549 {
4550 char_u *p = line + startcol;
4551
4552 MB_PTR_BACK(line, p);
4553 while (p > line && vim_isfilec(PTR2CHAR(p)))
4554 MB_PTR_BACK(line, p);
4555 if (p == line && vim_isfilec(PTR2CHAR(p)))
4556 startcol = 0;
4557 else
4558 startcol = (int)(p - line) + 1;
4559 }
4560
4561 compl_col += startcol;
4562 compl_length = (int)curs_col - startcol;
4563 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4564 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004565 {
4566 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004567 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004568 }
4569
4570 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004571
4572 return OK;
4573}
4574
4575/*
4576 * Get the pattern, column and length for command-line completion.
4577 * Sets the global variables: compl_col, compl_length and compl_pattern.
4578 */
4579 static int
4580get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4581{
4582 compl_pattern = vim_strnsave(line, curs_col);
4583 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004584 {
4585 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004586 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004587 }
4588 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004589 set_cmd_context(&compl_xp, compl_pattern,
John Marriott8c85a2a2024-05-20 19:18:26 +02004590 compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004591 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4592 || compl_xp.xp_context == EXPAND_NOTHING)
4593 // No completion possible, use an empty pattern to get a
4594 // "pattern not found" message.
4595 compl_col = curs_col;
4596 else
4597 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4598 compl_length = curs_col - compl_col;
4599
4600 return OK;
4601}
4602
4603/*
4604 * Get the pattern, column and length for user defined completion ('omnifunc',
4605 * 'completefunc' and 'thesaurusfunc')
4606 * Sets the global variables: compl_col, compl_length and compl_pattern.
4607 * Uses the global variable: spell_bad_len
4608 */
4609 static int
4610get_userdefined_compl_info(colnr_T curs_col UNUSED)
4611{
4612 int ret = FAIL;
4613
4614#ifdef FEAT_COMPL_FUNC
4615 // Call user defined function 'completefunc' with "a:findstart"
4616 // set to 1 to obtain the length of text to use for completion.
4617 char_u *line;
4618 typval_T args[3];
4619 int col;
4620 char_u *funcname;
4621 pos_T pos;
4622 int save_State = State;
4623 callback_T *cb;
4624
4625 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4626 // length as a string
4627 funcname = get_complete_funcname(ctrl_x_mode);
4628 if (*funcname == NUL)
4629 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004630 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004631 ? "completefunc" : "omnifunc");
4632 return FAIL;
4633 }
4634
4635 args[0].v_type = VAR_NUMBER;
4636 args[0].vval.v_number = 1;
4637 args[1].v_type = VAR_STRING;
4638 args[1].vval.v_string = (char_u *)"";
4639 args[2].v_type = VAR_UNKNOWN;
4640 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004641 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004642 cb = get_insert_callback(ctrl_x_mode);
4643 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004644 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004645
4646 State = save_State;
4647 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004648 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004649 validate_cursor();
4650 if (!EQUAL_POS(curwin->w_cursor, pos))
4651 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004652 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004653 return FAIL;
4654 }
4655
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004656 // Return value -2 means the user complete function wants to cancel the
4657 // complete without an error, do the same if the function did not execute
4658 // successfully.
4659 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004660 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004661 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004662 if (col == -3)
4663 {
4664 ctrl_x_mode = CTRL_X_NORMAL;
4665 edit_submode = NULL;
4666 if (!shortmess(SHM_COMPLETIONMENU))
4667 msg_clr_cmdline();
4668 return FAIL;
4669 }
4670
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004671 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004672 // completion.
4673 compl_opt_refresh_always = FALSE;
4674 compl_opt_suppress_empty = FALSE;
4675
4676 if (col < 0)
4677 col = curs_col;
4678 compl_col = col;
4679 if (compl_col > curs_col)
4680 compl_col = curs_col;
4681
4682 // Setup variables for completion. Need to obtain "line" again,
4683 // it may have become invalid.
4684 line = ml_get(curwin->w_cursor.lnum);
4685 compl_length = curs_col - compl_col;
4686 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4687 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004688 {
4689 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004690 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004691 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004692
John Marriott8c85a2a2024-05-20 19:18:26 +02004693 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004694 ret = OK;
4695#endif
4696
4697 return ret;
4698}
4699
4700/*
4701 * Get the pattern, column and length for spell completion.
4702 * Sets the global variables: compl_col, compl_length and compl_pattern.
4703 * Uses the global variable: spell_bad_len
4704 */
4705 static int
4706get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4707{
4708 int ret = FAIL;
4709#ifdef FEAT_SPELL
4710 char_u *line;
4711
4712 if (spell_bad_len > 0)
4713 compl_col = curs_col - spell_bad_len;
4714 else
4715 compl_col = spell_word_start(startcol);
4716 if (compl_col >= (colnr_T)startcol)
4717 {
4718 compl_length = 0;
4719 compl_col = curs_col;
4720 }
4721 else
4722 {
4723 spell_expand_check_cap(compl_col);
4724 compl_length = (int)curs_col - compl_col;
4725 }
4726 // Need to obtain "line" again, it may have become invalid.
4727 line = ml_get(curwin->w_cursor.lnum);
4728 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4729 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004730 {
4731 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004732 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004733 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004734
John Marriott8c85a2a2024-05-20 19:18:26 +02004735 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004736 ret = OK;
4737#endif
4738
4739 return ret;
4740}
4741
4742/*
4743 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004744 * "startcol" - start column number of the completion pattern/text
4745 * "cur_col" - current cursor column
4746 * On return, "line_invalid" is set to TRUE, if the current line may have
4747 * become invalid and needs to be fetched again.
4748 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004749 */
4750 static int
4751compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4752{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004753 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004754 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4755 && !thesaurus_func_complete(ctrl_x_mode)))
4756 {
4757 return get_normal_compl_info(line, startcol, curs_col);
4758 }
4759 else if (ctrl_x_mode_line_or_eval())
4760 {
4761 return get_wholeline_compl_info(line, curs_col);
4762 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004763 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004764 {
4765 return get_filename_compl_info(line, startcol, curs_col);
4766 }
4767 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4768 {
4769 return get_cmdline_compl_info(line, curs_col);
4770 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004771 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004772 || thesaurus_func_complete(ctrl_x_mode))
4773 {
4774 if (get_userdefined_compl_info(curs_col) == FAIL)
4775 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004776 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004777 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004778 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004779 {
4780 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4781 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004782 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004783 }
4784 else
4785 {
4786 internal_error("ins_complete()");
4787 return FAIL;
4788 }
4789
4790 return OK;
4791}
4792
4793/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004794 * Continue an interrupted completion mode search in "line".
4795 *
4796 * If this same ctrl_x_mode has been interrupted use the text from
4797 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4798 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4799 * the same line as the cursor then fix it (the line has been split because it
4800 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4801 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004802 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004803 static void
4804ins_compl_continue_search(char_u *line)
4805{
4806 // it is a continued search
4807 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004808 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4809 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004810 {
4811 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4812 {
4813 // line (probably) wrapped, set compl_startpos to the
4814 // first non_blank in the line, if it is not a wordchar
4815 // include it to get a better pattern, but then we don't
4816 // want the "\\<" prefix, check it below
4817 compl_col = (colnr_T)getwhitecols(line);
4818 compl_startpos.col = compl_col;
4819 compl_startpos.lnum = curwin->w_cursor.lnum;
4820 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4821 }
4822 else
4823 {
4824 // S_IPOS was set when we inserted a word that was at the
4825 // beginning of the line, which means that we'll go to SOL
4826 // mode but first we need to redefine compl_startpos
4827 if (compl_cont_status & CONT_S_IPOS)
4828 {
4829 compl_cont_status |= CONT_SOL;
4830 compl_startpos.col = (colnr_T)(skipwhite(
4831 line + compl_length
4832 + compl_startpos.col) - line);
4833 }
4834 compl_col = compl_startpos.col;
4835 }
4836 compl_length = curwin->w_cursor.col - (int)compl_col;
4837 // IObuff is used to add a "word from the next line" would we
4838 // have enough space? just being paranoid
4839#define MIN_SPACE 75
4840 if (compl_length > (IOSIZE - MIN_SPACE))
4841 {
4842 compl_cont_status &= ~CONT_SOL;
4843 compl_length = (IOSIZE - MIN_SPACE);
4844 compl_col = curwin->w_cursor.col - compl_length;
4845 }
4846 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4847 if (compl_length < 1)
4848 compl_cont_status &= CONT_LOCAL;
4849 }
4850 else if (ctrl_x_mode_line_or_eval())
4851 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4852 else
4853 compl_cont_status = 0;
4854}
4855
4856/*
4857 * start insert mode completion
4858 */
4859 static int
4860ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004861{
4862 char_u *line;
4863 int startcol = 0; // column where searched text starts
4864 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004865 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004866 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004867 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004868
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004869 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004870
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004871 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004872 did_si = FALSE;
4873 can_si = FALSE;
4874 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004875 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876 return FAIL;
4877
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004878 line = ml_get(curwin->w_cursor.lnum);
4879 curs_col = curwin->w_cursor.col;
4880 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004881
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004882 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4883 && compl_cont_mode == ctrl_x_mode)
4884 // this same ctrl-x_mode was interrupted previously. Continue the
4885 // completion.
4886 ins_compl_continue_search(line);
4887 else
4888 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004889
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004890 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004891 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004892 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004893 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004894 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4895 compl_cont_status = 0;
4896 compl_cont_status |= CONT_N_ADDS;
4897 compl_startpos = curwin->w_cursor;
4898 startcol = (int)curs_col;
4899 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004900 }
4901
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004902 // Work out completion pattern and original text -- webb
4903 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4904 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004905 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4906 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004907 // restore did_ai, so that adding comment leader works
4908 did_ai = save_did_ai;
4909 return FAIL;
4910 }
4911 // If "line" was changed while getting completion info get it again.
4912 if (line_invalid)
4913 line = ml_get(curwin->w_cursor.lnum);
4914
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004915 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004916 {
4917 edit_submode_pre = (char_u *)_(" Adding");
4918 if (ctrl_x_mode_line_or_eval())
4919 {
4920 // Insert a new line, keep indentation but ignore 'comments'.
4921 char_u *old = curbuf->b_p_com;
4922
4923 curbuf->b_p_com = (char_u *)"";
4924 compl_startpos.lnum = curwin->w_cursor.lnum;
4925 compl_startpos.col = compl_col;
4926 ins_eol('\r');
4927 curbuf->b_p_com = old;
4928 compl_length = 0;
4929 compl_col = curwin->w_cursor.col;
4930 }
4931 }
4932 else
4933 {
4934 edit_submode_pre = NULL;
4935 compl_startpos.col = compl_col;
4936 }
4937
4938 if (compl_cont_status & CONT_LOCAL)
4939 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4940 else
4941 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4942
4943 // If any of the original typed text has been changed we need to fix
4944 // the redo buffer.
4945 ins_compl_fixRedoBufForLeader(NULL);
4946
4947 // Always add completion for the original text.
4948 vim_free(compl_orig_text);
4949 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4950 if (p_ic)
4951 flags |= CP_ICASE;
4952 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4953 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4954 {
4955 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02004956 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004957 VIM_CLEAR(compl_orig_text);
4958 return FAIL;
4959 }
4960
4961 // showmode might reset the internal line pointers, so it must
4962 // be called before line = ml_get(), or when this address is no
4963 // longer needed. -- Acevedo.
4964 edit_submode_extra = (char_u *)_("-- Searching...");
4965 edit_submode_highl = HLF_COUNT;
4966 showmode();
4967 edit_submode_extra = NULL;
4968 out_flush();
4969
4970 return OK;
4971}
4972
4973/*
4974 * display the completion status message
4975 */
4976 static void
4977ins_compl_show_statusmsg(void)
4978{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004979 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004980 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004981 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004982 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004983 ? (char_u *)_("Hit end of paragraph")
4984 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004985 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004986 }
4987
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004988 if (edit_submode_extra == NULL)
4989 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004990 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004991 {
4992 edit_submode_extra = (char_u *)_("Back at original");
4993 edit_submode_highl = HLF_W;
4994 }
4995 else if (compl_cont_status & CONT_S_IPOS)
4996 {
4997 edit_submode_extra = (char_u *)_("Word from other line");
4998 edit_submode_highl = HLF_COUNT;
4999 }
5000 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5001 {
5002 edit_submode_extra = (char_u *)_("The only match");
5003 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005004 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005005 }
5006 else
5007 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005008#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005009 // Update completion sequence number when needed.
5010 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005011 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005012#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005013 // The match should always have a sequence number now, this is
5014 // just a safety check.
5015 if (compl_curr_match->cp_number != -1)
5016 {
5017 // Space for 10 text chars. + 2x10-digit no.s = 31.
5018 // Translations may need more than twice that.
5019 static char_u match_ref[81];
5020
5021 if (compl_matches > 0)
5022 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005023 _("match %d of %d"),
5024 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005025 else
5026 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005027 _("match %d"),
5028 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005029 edit_submode_extra = match_ref;
5030 edit_submode_highl = HLF_R;
5031 if (dollar_vcol >= 0)
5032 curs_columns(FALSE);
5033 }
5034 }
5035 }
5036
5037 // Show a message about what (completion) mode we're in.
5038 if (!compl_opt_suppress_empty)
5039 {
5040 showmode();
5041 if (!shortmess(SHM_COMPLETIONMENU))
5042 {
5043 if (edit_submode_extra != NULL)
5044 {
5045 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005046 {
5047 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005048 msg_attr((char *)edit_submode_extra,
5049 edit_submode_highl < HLF_COUNT
5050 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005051 msg_hist_off = FALSE;
5052 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005053 }
5054 else
5055 msg_clr_cmdline(); // necessary for "noshowmode"
5056 }
5057 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005058}
5059
5060/*
5061 * Do Insert mode completion.
5062 * Called when character "c" was typed, which has a meaning for completion.
5063 * Returns OK if completion was done, FAIL if something failed (out of mem).
5064 */
5065 int
5066ins_complete(int c, int enable_pum)
5067{
5068 int n;
5069 int save_w_wrow;
5070 int save_w_leftcol;
5071 int insert_match;
5072
5073 compl_direction = ins_compl_key2dir(c);
5074 insert_match = ins_compl_use_match(c);
5075
5076 if (!compl_started)
5077 {
5078 if (ins_compl_start() == FAIL)
5079 return FAIL;
5080 }
5081 else if (insert_match && stop_arrow() == FAIL)
5082 return FAIL;
5083
5084 compl_shown_match = compl_curr_match;
5085 compl_shows_dir = compl_direction;
5086
5087 // Find next match (and following matches).
5088 save_w_wrow = curwin->w_wrow;
5089 save_w_leftcol = curwin->w_leftcol;
5090 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5091
5092 // may undisplay the popup menu
5093 ins_compl_upd_pum();
5094
5095 if (n > 1) // all matches have been found
5096 compl_matches = n;
5097 compl_curr_match = compl_shown_match;
5098 compl_direction = compl_shows_dir;
5099
5100 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5101 // mode.
5102 if (got_int && !global_busy)
5103 {
5104 (void)vgetc();
5105 got_int = FALSE;
5106 }
5107
5108 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005109 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005110 {
5111 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5112 // because we couldn't expand anything at first place, but if we used
5113 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5114 // (such as M in M'exico) if not tried already. -- Acevedo
5115 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005116 || compl_status_adding()
5117 || (ctrl_x_mode_not_default()
5118 && !ctrl_x_mode_path_patterns()
5119 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005120 compl_cont_status &= ~CONT_N_ADDS;
5121 }
5122
5123 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5124 compl_cont_status |= CONT_S_IPOS;
5125 else
5126 compl_cont_status &= ~CONT_S_IPOS;
5127
5128 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005129
5130 // Show the popup menu, unless we got interrupted.
5131 if (enable_pum && !compl_interrupted)
5132 show_pum(save_w_wrow, save_w_leftcol);
5133
5134 compl_was_interrupted = compl_interrupted;
5135 compl_interrupted = FALSE;
5136
5137 return OK;
5138}
5139
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005140/*
5141 * Remove (if needed) and show the popup menu
5142 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005143 static void
5144show_pum(int prev_w_wrow, int prev_w_leftcol)
5145{
5146 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005147 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005148 RedrawingDisabled = 0;
5149
5150 // If the cursor moved or the display scrolled we need to remove the pum
5151 // first.
5152 setcursor();
5153 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5154 ins_compl_del_pum();
5155
5156 ins_compl_show_pum();
5157 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005158
5159 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005160}
5161
5162/*
5163 * Looks in the first "len" chars. of "src" for search-metachars.
5164 * If dest is not NULL the chars. are copied there quoting (with
5165 * a backslash) the metachars, and dest would be NUL terminated.
5166 * Returns the length (needed) of dest
5167 */
5168 static unsigned
5169quote_meta(char_u *dest, char_u *src, int len)
5170{
5171 unsigned m = (unsigned)len + 1; // one extra for the NUL
5172
5173 for ( ; --len >= 0; src++)
5174 {
5175 switch (*src)
5176 {
5177 case '.':
5178 case '*':
5179 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005180 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005181 break;
5182 // FALLTHROUGH
5183 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005184 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005185 break;
5186 // FALLTHROUGH
5187 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005188 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005189 break;
5190 // FALLTHROUGH
5191 case '^': // currently it's not needed.
5192 case '$':
5193 m++;
5194 if (dest != NULL)
5195 *dest++ = '\\';
5196 break;
5197 }
5198 if (dest != NULL)
5199 *dest++ = *src;
5200 // Copy remaining bytes of a multibyte character.
5201 if (has_mbyte)
5202 {
5203 int i, mb_len;
5204
5205 mb_len = (*mb_ptr2len)(src) - 1;
5206 if (mb_len > 0 && len >= mb_len)
5207 for (i = 0; i < mb_len; ++i)
5208 {
5209 --len;
5210 ++src;
5211 if (dest != NULL)
5212 *dest++ = *src;
5213 }
5214 }
5215 }
5216 if (dest != NULL)
5217 *dest = NUL;
5218
5219 return m;
5220}
5221
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005222#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005223 void
5224free_insexpand_stuff(void)
5225{
5226 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005227# ifdef FEAT_EVAL
5228 free_callback(&cfu_cb);
5229 free_callback(&ofu_cb);
5230 free_callback(&tsrfu_cb);
5231# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005232}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005233#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005234
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005235#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005236/*
5237 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5238 * spelled word, if there is one.
5239 */
5240 static void
5241spell_back_to_badword(void)
5242{
5243 pos_T tpos = curwin->w_cursor;
5244
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005245 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005246 if (curwin->w_cursor.col != tpos.col)
5247 start_arrow(&tpos);
5248}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005249#endif