blob: 4ad1f41a647372f93979edb2beb699bdffcf7753 [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
zeertzjq7002c052024-06-21 07:55:07 +020061 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010062 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
glepnira218cc62024-06-03 19:32:39 +0200116 int cp_score; // fuzzy match score
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100117};
118
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200119// values for cp_flags
120# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
121# define CP_FREE_FNAME 2 // cp_fname is allocated
122# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
123# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
124# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200125# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127/*
128 * All the current matches are stored in a list.
129 * "compl_first_match" points to the start of the list.
130 * "compl_curr_match" points to the currently selected entry.
131 * "compl_shown_match" is different from compl_curr_match during
132 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000133 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100134 */
135static compl_T *compl_first_match = NULL;
136static compl_T *compl_curr_match = NULL;
137static compl_T *compl_shown_match = NULL;
138static compl_T *compl_old_match = NULL;
139
140// After using a cursor key <Enter> selects a match in the popup menu,
141// otherwise it inserts a line break.
142static int compl_enter_selects = FALSE;
143
144// When "compl_leader" is not NULL only matches that start with this string
145// are used.
146static char_u *compl_leader = NULL;
147
148static int compl_get_longest = FALSE; // put longest common string
149 // in compl_leader
150
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100151// Selected one of the matches. When FALSE the match was edited or using the
152// longest common string.
153static int compl_used_match;
154
155// didn't finish finding completions.
156static int compl_was_interrupted = FALSE;
157
158// Set when character typed while looking for matches and it means we should
159// stop looking for matches.
160static int compl_interrupted = FALSE;
161
162static int compl_restarting = FALSE; // don't insert match
163
164// When the first completion is done "compl_started" is set. When it's
165// FALSE the word to be completed must be located.
166static int compl_started = FALSE;
167
168// Which Ctrl-X mode are we in?
169static int ctrl_x_mode = CTRL_X_NORMAL;
170
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000171static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100172static char_u *compl_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200173static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100174static int compl_direction = FORWARD;
175static int compl_shows_dir = FORWARD;
176static int compl_pending = 0; // > 1 for postponed CTRL-N
177static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000178// Length in bytes of the text being completed (this is deleted to be replaced
179// by the match.)
180static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100181static colnr_T compl_col = 0; // column where the text starts
182 // that is being completed
183static char_u *compl_orig_text = NULL; // text as it was before
184 // completion started
185static int compl_cont_mode = 0;
186static expand_T compl_xp;
187
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000188// List of flags for method of completion.
189static int compl_cont_status = 0;
190# define CONT_ADDING 1 // "normal" or "adding" expansion
191# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
192 // it's set only iff N_ADDS is set
193# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
194# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
195 // if so, word-wise-expansion will set SOL
196# define CONT_SOL 16 // pattern includes start of line, just for
197 // word-wise expansion, not set for ^X^L
198# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
199 // expansion, (eg use complete=.)
200
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100201static int compl_opt_refresh_always = FALSE;
202static int compl_opt_suppress_empty = FALSE;
203
glepnira218cc62024-06-03 19:32:39 +0200204static int compl_selected_item = -1;
205
glepnir8159fb12024-07-17 20:32:54 +0200206static int *compl_fuzzy_scores;
207
Bram Moolenaar08928322020-01-04 14:32:48 +0100208static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100209static void ins_compl_longest_match(compl_T *match);
210static void ins_compl_del_pum(void);
211static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
212static char_u *find_line_end(char_u *ptr);
213static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100214static int ins_compl_need_restart(void);
215static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000216static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100217static void ins_compl_restart(void);
218static void ins_compl_set_original_text(char_u *str);
219static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
220# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
221static void ins_compl_add_list(list_T *list);
222static void ins_compl_add_dict(dict_T *dict);
223# endif
224static int ins_compl_key2dir(int c);
225static int ins_compl_pum_key(int c);
226static int ins_compl_key2count(int c);
227static void show_pum(int prev_w_wrow, int prev_w_leftcol);
228static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100229
230#ifdef FEAT_SPELL
231static void spell_back_to_badword(void);
232static int spell_bad_len = 0; // length of located bad word
233#endif
234
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100235/*
236 * CTRL-X pressed in Insert mode.
237 */
238 void
239ins_ctrl_x(void)
240{
zeertzjqdca29d92021-08-31 19:12:51 +0200241 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100242 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000243 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100244 if (compl_cont_status & CONT_N_ADDS)
245 compl_cont_status |= CONT_INTRPT;
246 else
247 compl_cont_status = 0;
248 // We're not sure which CTRL-X mode it will be yet
249 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
250 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
251 edit_submode_pre = NULL;
252 showmode();
253 }
zeertzjqdca29d92021-08-31 19:12:51 +0200254 else
255 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
256 // CTRL-V look like CTRL-N
257 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100258
LemonBoy2bf52dd2022-04-09 18:17:34 +0100259 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100260}
261
262/*
263 * Functions to check the current CTRL-X mode.
264 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000265int ctrl_x_mode_none(void)
266 { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void)
268 { return ctrl_x_mode == CTRL_X_NORMAL; }
269int ctrl_x_mode_scroll(void)
270 { return ctrl_x_mode == CTRL_X_SCROLL; }
271int ctrl_x_mode_whole_line(void)
272 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
273int ctrl_x_mode_files(void)
274 { return ctrl_x_mode == CTRL_X_FILES; }
275int ctrl_x_mode_tags(void)
276 { return ctrl_x_mode == CTRL_X_TAGS; }
277int ctrl_x_mode_path_patterns(void)
278 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
279int ctrl_x_mode_path_defines(void)
280 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
281int ctrl_x_mode_dictionary(void)
282 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
283int ctrl_x_mode_thesaurus(void)
284 { return ctrl_x_mode == CTRL_X_THESAURUS; }
285int ctrl_x_mode_cmdline(void)
286 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200287 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000288int ctrl_x_mode_function(void)
289 { return ctrl_x_mode == CTRL_X_FUNCTION; }
290int ctrl_x_mode_omni(void)
291 { return ctrl_x_mode == CTRL_X_OMNI; }
292int ctrl_x_mode_spell(void)
293 { return ctrl_x_mode == CTRL_X_SPELL; }
294static int ctrl_x_mode_eval(void)
295 { return ctrl_x_mode == CTRL_X_EVAL; }
296int ctrl_x_mode_line_or_eval(void)
297 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100298
299/*
300 * Whether other than default completion has been selected.
301 */
302 int
303ctrl_x_mode_not_default(void)
304{
305 return ctrl_x_mode != CTRL_X_NORMAL;
306}
307
308/*
zeertzjqdca29d92021-08-31 19:12:51 +0200309 * Whether CTRL-X was typed without a following character,
310 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100311 */
312 int
313ctrl_x_mode_not_defined_yet(void)
314{
315 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
316}
317
318/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000319 * Return TRUE if currently in "normal" or "adding" insert completion matches
320 * state
321 */
322 int
323compl_status_adding(void)
324{
325 return compl_cont_status & CONT_ADDING;
326}
327
328/*
329 * Return TRUE if the completion pattern includes start of line, just for
330 * word-wise expansion.
331 */
332 int
333compl_status_sol(void)
334{
335 return compl_cont_status & CONT_SOL;
336}
337
338/*
339 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
340 */
341 int
342compl_status_local(void)
343{
344 return compl_cont_status & CONT_LOCAL;
345}
346
347/*
348 * Clear the completion status flags
349 */
350 void
351compl_status_clear(void)
352{
353 compl_cont_status = 0;
354}
355
356/*
357 * Return TRUE if completion is using the forward direction matches
358 */
359 static int
360compl_dir_forward(void)
361{
362 return compl_direction == FORWARD;
363}
364
365/*
366 * Return TRUE if currently showing forward completion matches
367 */
368 static int
369compl_shows_dir_forward(void)
370{
371 return compl_shows_dir == FORWARD;
372}
373
374/*
375 * Return TRUE if currently showing backward completion matches
376 */
377 static int
378compl_shows_dir_backward(void)
379{
380 return compl_shows_dir == BACKWARD;
381}
382
383/*
384 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100385 */
386 int
387has_compl_option(int dict_opt)
388{
389 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200390#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200392#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100393 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100394 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
395#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100396 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100397#endif
398 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100399 {
400 ctrl_x_mode = CTRL_X_NORMAL;
401 edit_submode = NULL;
402 msg_attr(dict_opt ? _("'dictionary' option is empty")
403 : _("'thesaurus' option is empty"),
404 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100405 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100406 {
407 vim_beep(BO_COMPL);
408 setcursor();
409 out_flush();
410#ifdef FEAT_EVAL
411 if (!get_vim_var_nr(VV_TESTING))
412#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100413 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100414 }
415 return FALSE;
416 }
417 return TRUE;
418}
419
420/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000421 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100422 * This depends on the current mode.
423 */
424 int
425vim_is_ctrl_x_key(int c)
426{
427 // Always allow ^R - let its results then be checked
428 if (c == Ctrl_R)
429 return TRUE;
430
431 // Accept <PageUp> and <PageDown> if the popup menu is visible.
432 if (ins_compl_pum_key(c))
433 return TRUE;
434
435 switch (ctrl_x_mode)
436 {
437 case 0: // Not in any CTRL-X mode
438 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
439 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200440 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100441 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
442 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
443 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
444 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
445 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200446 || c == Ctrl_S || c == Ctrl_K || c == 's'
447 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100448 case CTRL_X_SCROLL:
449 return (c == Ctrl_Y || c == Ctrl_E);
450 case CTRL_X_WHOLE_LINE:
451 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_FILES:
453 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_DICTIONARY:
455 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_THESAURUS:
457 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_TAGS:
459 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
460#ifdef FEAT_FIND_ID
461 case CTRL_X_PATH_PATTERNS:
462 return (c == Ctrl_P || c == Ctrl_N);
463 case CTRL_X_PATH_DEFINES:
464 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
465#endif
466 case CTRL_X_CMDLINE:
467 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
468 || c == Ctrl_X);
469#ifdef FEAT_COMPL_FUNC
470 case CTRL_X_FUNCTION:
471 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
472 case CTRL_X_OMNI:
473 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
474#endif
475 case CTRL_X_SPELL:
476 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
477 case CTRL_X_EVAL:
478 return (c == Ctrl_P || c == Ctrl_N);
479 }
480 internal_error("vim_is_ctrl_x_key()");
481 return FALSE;
482}
483
484/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000485 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000486 */
487 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000488match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000489{
490 return match->cp_flags & CP_ORIGINAL_TEXT;
491}
492
493/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000494 * Returns TRUE if "match" is the first match in the completion list.
495 */
496 static int
497is_first_match(compl_T *match)
498{
499 return match == compl_first_match;
500}
501
502/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100503 * Return TRUE when character "c" is part of the item currently being
504 * completed. Used to decide whether to abandon complete mode when the menu
505 * is visible.
506 */
507 int
508ins_compl_accept_char(int c)
509{
510 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
511 // When expanding an identifier only accept identifier chars.
512 return vim_isIDc(c);
513
514 switch (ctrl_x_mode)
515 {
516 case CTRL_X_FILES:
517 // When expanding file name only accept file name chars. But not
518 // path separators, so that "proto/<Tab>" expands files in
519 // "proto", not "proto/" as a whole
520 return vim_isfilec(c) && !vim_ispathsep(c);
521
522 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200523 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100524 case CTRL_X_OMNI:
525 // Command line and Omni completion can work with just about any
526 // printable character, but do stop at white space.
527 return vim_isprintc(c) && !VIM_ISWHITE(c);
528
529 case CTRL_X_WHOLE_LINE:
530 // For while line completion a space can be part of the line.
531 return vim_isprintc(c);
532 }
533 return vim_iswordc(c);
534}
535
536/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000537 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100538 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000539 */
540 static char_u *
541ins_compl_infercase_gettext(
542 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100543 int char_len,
544 int compl_char_len,
545 int min_len,
546 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000547{
548 int *wca; // Wide character array.
549 char_u *p;
550 int i, c;
551 int has_lower = FALSE;
552 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100553 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000554
555 IObuff[0] = NUL;
556
557 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100558 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000559 if (wca == NULL)
560 return IObuff;
561
562 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100563 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000564 if (has_mbyte)
565 wca[i] = mb_ptr2char_adv(&p);
566 else
567 wca[i] = *(p++);
568
569 // Rule 1: Were any chars converted to lower?
570 p = compl_orig_text;
571 for (i = 0; i < min_len; ++i)
572 {
573 if (has_mbyte)
574 c = mb_ptr2char_adv(&p);
575 else
576 c = *(p++);
577 if (MB_ISLOWER(c))
578 {
579 has_lower = TRUE;
580 if (MB_ISUPPER(wca[i]))
581 {
582 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100583 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000584 wca[i] = MB_TOLOWER(wca[i]);
585 break;
586 }
587 }
588 }
589
590 // Rule 2: No lower case, 2nd consecutive letter converted to
591 // upper case.
592 if (!has_lower)
593 {
594 p = compl_orig_text;
595 for (i = 0; i < min_len; ++i)
596 {
597 if (has_mbyte)
598 c = mb_ptr2char_adv(&p);
599 else
600 c = *(p++);
601 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
602 {
603 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100604 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000605 wca[i] = MB_TOUPPER(wca[i]);
606 break;
607 }
608 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
609 }
610 }
611
612 // Copy the original case of the part we typed.
613 p = compl_orig_text;
614 for (i = 0; i < min_len; ++i)
615 {
616 if (has_mbyte)
617 c = mb_ptr2char_adv(&p);
618 else
619 c = *(p++);
620 if (MB_ISLOWER(c))
621 wca[i] = MB_TOLOWER(wca[i]);
622 else if (MB_ISUPPER(c))
623 wca[i] = MB_TOUPPER(wca[i]);
624 }
625
626 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000627 p = IObuff;
628 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100629 ga_init2(&gap, 1, 500);
630 while (i < char_len)
631 {
632 if (gap.ga_data != NULL)
633 {
634 if (ga_grow(&gap, 10) == FAIL)
635 {
636 ga_clear(&gap);
637 return (char_u *)"[failed]";
638 }
639 p = (char_u *)gap.ga_data + gap.ga_len;
640 if (has_mbyte)
641 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
642 else
643 {
644 *p = wca[i++];
645 ++gap.ga_len;
646 }
647 }
648 else if ((p - IObuff) + 6 >= IOSIZE)
649 {
650 // Multi-byte characters can occupy up to five bytes more than
651 // ASCII characters, and we also need one byte for NUL, so when
652 // getting to six bytes from the edge of IObuff switch to using a
653 // growarray. Add the character in the next round.
654 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100655 {
656 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100657 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100658 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100659 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100661 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100662 }
663 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000664 p += (*mb_char2bytes)(wca[i++], p);
665 else
666 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100667 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000668 vim_free(wca);
669
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100670 if (gap.ga_data != NULL)
671 {
672 *tofree = gap.ga_data;
673 return gap.ga_data;
674 }
675
676 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000677 return IObuff;
678}
679
680/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100681 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
682 * case of the originally typed text is used, and the case of the completed
683 * text is inferred, ie this tries to work out what case you probably wanted
684 * the rest of the word to be in -- webb
685 */
686 int
687ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200688 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100689 int len,
690 int icase,
691 char_u *fname,
692 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200693 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100694{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200695 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100696 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100697 int char_len; // count multi-byte characters
698 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100699 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200700 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100701 int res;
702 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100703
704 if (p_ic && curbuf->b_p_inf && len > 0)
705 {
706 // Infer case of completed part.
707
708 // Find actual length of completion.
709 if (has_mbyte)
710 {
711 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100712 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100713 while (*p != NUL)
714 {
715 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100716 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100717 }
718 }
719 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100720 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100721
722 // Find actual length of original text.
723 if (has_mbyte)
724 {
725 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100726 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100727 while (*p != NUL)
728 {
729 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100730 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100731 }
732 }
733 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100734 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100738 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100740 str = ins_compl_infercase_gettext(str, char_len,
741 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100742 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200743 if (cont_s_ipos)
744 flags |= CP_CONT_S_IPOS;
745 if (icase)
746 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200747
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100748 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
749 vim_free(tofree);
750 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100751}
752
753/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000754 * Add a match to the list of matches. The arguments are:
755 * str - text of the match to add
756 * len - length of "str". If -1, then the length of "str" is
757 * computed.
758 * fname - file name to associate with this match.
759 * cptext - list of strings to use with this match (for abbr, menu, info
760 * and kind)
761 * user_data - user supplied data (any vim type) for this match
762 * cdir - match direction. If 0, use "compl_direction".
763 * flags_arg - match flags (cp_flags)
764 * adup - accept this match even if it is already present.
765 * If "cdir" is FORWARD, then the match is added after the current match.
766 * Otherwise, it is added before the current match.
767 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100768 * If the given string is already in the list of completions, then return
769 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
770 * maybe because alloc() returns NULL, then FAIL is returned.
771 */
772 static int
773ins_compl_add(
774 char_u *str,
775 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100776 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 char_u **cptext, // extra text for popup menu or NULL
778 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100779 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200780 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100781 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100782{
783 compl_T *match;
784 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200785 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100786
Bram Moolenaarceb06192021-04-04 15:05:22 +0200787 if (flags & CP_FAST)
788 fast_breakcheck();
789 else
790 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100791 if (got_int)
792 return FAIL;
793 if (len < 0)
794 len = (int)STRLEN(str);
795
796 // If the same match is already present, don't add it.
797 if (compl_first_match != NULL && !adup)
798 {
799 match = compl_first_match;
800 do
801 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000802 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100803 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100804 && ((int)STRLEN(match->cp_str) <= len
805 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100806 return NOTDONE;
807 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000808 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100809 }
810
811 // Remove any popup menu before changing the list of matches.
812 ins_compl_del_pum();
813
814 // Allocate a new match structure.
815 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200816 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100817 if (match == NULL)
818 return FAIL;
819 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200820 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100821 match->cp_number = 0;
822 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
823 {
824 vim_free(match);
825 return FAIL;
826 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100827
828 // match-fname is:
829 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200830 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100831 // - NULL otherwise. --Acevedo
832 if (fname != NULL
833 && compl_curr_match != NULL
834 && compl_curr_match->cp_fname != NULL
835 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
836 match->cp_fname = compl_curr_match->cp_fname;
837 else if (fname != NULL)
838 {
839 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200840 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100841 }
842 else
843 match->cp_fname = NULL;
844 match->cp_flags = flags;
845
846 if (cptext != NULL)
847 {
848 int i;
849
850 for (i = 0; i < CPT_COUNT; ++i)
851 if (cptext[i] != NULL && *cptext[i] != NUL)
852 match->cp_text[i] = vim_strsave(cptext[i]);
853 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100854#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100855 if (user_data != NULL)
856 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100857#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100858
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000859 // Link the new match structure after (FORWARD) or before (BACKWARD) the
860 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100861 if (compl_first_match == NULL)
862 match->cp_next = match->cp_prev = NULL;
863 else if (dir == FORWARD)
864 {
865 match->cp_next = compl_curr_match->cp_next;
866 match->cp_prev = compl_curr_match;
867 }
868 else // BACKWARD
869 {
870 match->cp_next = compl_curr_match;
871 match->cp_prev = compl_curr_match->cp_prev;
872 }
873 if (match->cp_next)
874 match->cp_next->cp_prev = match;
875 if (match->cp_prev)
876 match->cp_prev->cp_next = match;
877 else // if there's nothing before, it is the first match
878 compl_first_match = match;
879 compl_curr_match = match;
880
881 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200882 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100883 ins_compl_longest_match(match);
884
885 return OK;
886}
887
888/*
889 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200890 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100891 */
892 static int
893ins_compl_equal(compl_T *match, char_u *str, int len)
894{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200896 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200897 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100898 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
899 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
900}
901
902/*
903 * Reduce the longest common string for match "match".
904 */
905 static void
906ins_compl_longest_match(compl_T *match)
907{
908 char_u *p, *s;
909 int c1, c2;
910 int had_match;
911
912 if (compl_leader == NULL)
913 {
914 // First match, use it as a whole.
915 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000916 if (compl_leader == NULL)
917 return;
918
919 had_match = (curwin->w_cursor.col > compl_col);
920 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000921 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000922 ins_redraw(FALSE);
923
924 // When the match isn't there (to avoid matching itself) remove it
925 // again after redrawing.
926 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100927 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100928 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000929
930 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000932
933 // Reduce the text if this match differs from compl_leader.
934 p = compl_leader;
935 s = match->cp_str;
936 while (*p != NUL)
937 {
938 if (has_mbyte)
939 {
940 c1 = mb_ptr2char(p);
941 c2 = mb_ptr2char(s);
942 }
943 else
944 {
945 c1 = *p;
946 c2 = *s;
947 }
948 if ((match->cp_flags & CP_ICASE)
949 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
950 break;
951 if (has_mbyte)
952 {
953 MB_PTR_ADV(p);
954 MB_PTR_ADV(s);
955 }
956 else
957 {
958 ++p;
959 ++s;
960 }
961 }
962
963 if (*p != NUL)
964 {
965 // Leader was shortened, need to change the inserted text.
966 *p = NUL;
967 had_match = (curwin->w_cursor.col > compl_col);
968 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000969 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000970 ins_redraw(FALSE);
971
972 // When the match isn't there (to avoid matching itself) remove it
973 // again after redrawing.
974 if (!had_match)
975 ins_compl_delete();
976 }
977
978 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100979}
980
981/*
982 * Add an array of matches to the list of matches.
983 * Frees matches[].
984 */
985 static void
986ins_compl_add_matches(
987 int num_matches,
988 char_u **matches,
989 int icase)
990{
991 int i;
992 int add_r = OK;
993 int dir = compl_direction;
994
995 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100996 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200997 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100998 // if dir was BACKWARD then honor it just once
999 dir = FORWARD;
1000 FreeWild(num_matches, matches);
1001}
1002
1003/*
1004 * Make the completion list cyclic.
1005 * Return the number of matches (excluding the original).
1006 */
1007 static int
1008ins_compl_make_cyclic(void)
1009{
1010 compl_T *match;
1011 int count = 0;
1012
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001013 if (compl_first_match == NULL)
1014 return 0;
1015
1016 // Find the end of the list.
1017 match = compl_first_match;
1018 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001019 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001020 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001021 match = match->cp_next;
1022 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001023 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001024 match->cp_next = compl_first_match;
1025 compl_first_match->cp_prev = match;
1026
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001027 return count;
1028}
1029
1030/*
1031 * Return whether there currently is a shown match.
1032 */
1033 int
1034ins_compl_has_shown_match(void)
1035{
1036 return compl_shown_match == NULL
1037 || compl_shown_match != compl_shown_match->cp_next;
1038}
1039
1040/*
1041 * Return whether the shown match is long enough.
1042 */
1043 int
1044ins_compl_long_shown_match(void)
1045{
1046 return (int)STRLEN(compl_shown_match->cp_str)
1047 > curwin->w_cursor.col - compl_col;
1048}
1049
1050/*
zeertzjq529b9ad2024-06-05 20:27:06 +02001051 * Get the local or global value of 'completeopt' flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001052 */
zeertzjq529b9ad2024-06-05 20:27:06 +02001053 unsigned int
1054get_cot_flags(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001055{
zeertzjq529b9ad2024-06-05 20:27:06 +02001056 return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001057}
1058
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001059
1060// "compl_match_array" points the currently displayed list of entries in the
1061// popup menu. It is NULL when there is no popup menu.
1062static pumitem_T *compl_match_array = NULL;
1063static int compl_match_arraysize;
1064
1065/*
1066 * Update the screen and when there is any scrolling remove the popup menu.
1067 */
1068 static void
1069ins_compl_upd_pum(void)
1070{
1071 int h;
1072
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001073 if (compl_match_array == NULL)
1074 return;
1075
1076 h = curwin->w_cline_height;
1077 // Update the screen later, before drawing the popup menu over it.
1078 pum_call_update_screen();
1079 if (h != curwin->w_cline_height)
1080 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001081}
1082
1083/*
1084 * Remove any popup menu.
1085 */
1086 static void
1087ins_compl_del_pum(void)
1088{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001089 if (compl_match_array == NULL)
1090 return;
1091
1092 pum_undisplay();
1093 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001094}
1095
1096/*
1097 * Return TRUE if the popup menu should be displayed.
1098 */
1099 int
1100pum_wanted(void)
1101{
1102 // 'completeopt' must contain "menu" or "menuone"
zeertzjq529b9ad2024-06-05 20:27:06 +02001103 if ((get_cot_flags() & COT_ANY_MENU) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001104 return FALSE;
1105
1106 // The display looks bad on a B&W display.
1107 if (t_colors < 8
1108#ifdef FEAT_GUI
1109 && !gui.in_use
1110#endif
1111 )
1112 return FALSE;
1113 return TRUE;
1114}
1115
1116/*
1117 * Return TRUE if there are two or more matches to be shown in the popup menu.
1118 * One if 'completopt' contains "menuone".
1119 */
1120 static int
1121pum_enough_matches(void)
1122{
1123 compl_T *compl;
1124 int i;
1125
1126 // Don't display the popup menu if there are no matches or there is only
1127 // one (ignoring the original text).
1128 compl = compl_first_match;
1129 i = 0;
1130 do
1131 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001132 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001133 break;
1134 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001135 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001136
zeertzjq529b9ad2024-06-05 20:27:06 +02001137 if (get_cot_flags() & COT_MENUONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001138 return (i >= 1);
1139 return (i >= 2);
1140}
1141
Bram Moolenaar3075a452021-11-17 15:51:52 +00001142#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001143/*
1144 * Allocate Dict for the completed item.
1145 * { word, abbr, menu, kind, info }
1146 */
1147 static dict_T *
1148ins_compl_dict_alloc(compl_T *match)
1149{
1150 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1151
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001152 if (dict == NULL)
1153 return NULL;
1154
1155 dict_add_string(dict, "word", match->cp_str);
1156 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1157 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1158 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1159 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1160 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1161 dict_add_string(dict, "user_data", (char_u *)"");
1162 else
1163 dict_add_tv(dict, "user_data", &match->cp_user_data);
1164
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001165 return dict;
1166}
1167
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001168/*
1169 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1170 * completion menu is changed.
1171 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001172 static void
1173trigger_complete_changed_event(int cur)
1174{
1175 dict_T *v_event;
1176 dict_T *item;
1177 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001178 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001179
1180 if (recursive)
1181 return;
1182
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001183 if (cur < 0)
1184 item = dict_alloc();
1185 else
1186 item = ins_compl_dict_alloc(compl_curr_match);
1187 if (item == NULL)
1188 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001189 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001190 dict_add_dict(v_event, "completed_item", item);
1191 pum_set_event_info(v_event);
1192 dict_set_items_ro(v_event);
1193
1194 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001195 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001196 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001197 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001198 recursive = FALSE;
1199
Bram Moolenaar3075a452021-11-17 15:51:52 +00001200 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001202#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001203
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001204/*
glepnira218cc62024-06-03 19:32:39 +02001205 * pumitem qsort compare func
1206 */
1207 static int
zeertzjq8e567472024-06-14 20:04:42 +02001208ins_compl_fuzzy_cmp(const void *a, const void *b)
glepnira218cc62024-06-03 19:32:39 +02001209{
1210 const int sa = (*(pumitem_T *)a).pum_score;
1211 const int sb = (*(pumitem_T *)b).pum_score;
zeertzjq8e567472024-06-14 20:04:42 +02001212 const int ia = (*(pumitem_T *)a).pum_idx;
1213 const int ib = (*(pumitem_T *)b).pum_idx;
1214 return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1);
glepnira218cc62024-06-03 19:32:39 +02001215}
1216
1217/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001218 * Build a popup menu to show the completion matches.
1219 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1220 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001221 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001222 static int
1223ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001224{
1225 compl_T *compl;
1226 compl_T *shown_compl = NULL;
1227 int did_find_shown_match = FALSE;
1228 int shown_match_ok = FALSE;
1229 int i;
1230 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001231 int lead_len = 0;
glepnira218cc62024-06-03 19:32:39 +02001232 int max_fuzzy_score = 0;
zeertzjqaa925ee2024-06-09 18:24:05 +02001233 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02001234 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1235 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001236
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001237 // Need to build the popup menu list.
1238 compl_match_arraysize = 0;
1239 compl = compl_first_match;
1240 if (compl_leader != NULL)
1241 lead_len = (int)STRLEN(compl_leader);
1242
1243 do
1244 {
zeertzjq551d8c32024-06-05 19:53:32 +02001245 // When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
1246 // set the cp_score for later comparisons.
glepnira218cc62024-06-03 19:32:39 +02001247 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1248 compl->cp_score = fuzzy_match_str(compl->cp_str, compl_leader);
1249
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001250 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001251 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001252 || ins_compl_equal(compl, compl_leader, lead_len)
1253 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001254 ++compl_match_arraysize;
1255 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001256 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001257
1258 if (compl_match_arraysize == 0)
1259 return -1;
1260
1261 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1262 if (compl_match_array == NULL)
1263 return -1;
1264
1265 // If the current match is the original text don't find the first
1266 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001267 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001268 shown_match_ok = TRUE;
1269
glepnir53387c52024-05-27 15:11:01 +02001270 if (compl_leader != NULL
1271 && STRCMP(compl_leader, compl_orig_text) == 0
1272 && shown_match_ok == FALSE)
1273 compl_shown_match = compl_no_select ? compl_first_match
1274 : compl_first_match->cp_next;
1275
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001276 i = 0;
1277 compl = compl_first_match;
1278 do
1279 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001280 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001281 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001282 || ins_compl_equal(compl, compl_leader, lead_len)
1283 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001284 {
glepnira218cc62024-06-03 19:32:39 +02001285 if (!shown_match_ok && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001286 {
1287 if (compl == compl_shown_match || did_find_shown_match)
1288 {
1289 // This item is the shown match or this is the
1290 // first displayed item after the shown match.
1291 compl_shown_match = compl;
1292 did_find_shown_match = TRUE;
1293 shown_match_ok = TRUE;
1294 }
1295 else
1296 // Remember this displayed match for when the
1297 // shown match is just below it.
1298 shown_compl = compl;
1299 cur = i;
1300 }
glepnira218cc62024-06-03 19:32:39 +02001301 else if (compl_fuzzy_match)
1302 {
glepnirf94c9c42024-06-14 21:11:56 +02001303 if (i == 0)
glepnir105f7412024-06-15 15:32:22 +02001304 shown_compl = compl;
glepnirdca57fb2024-06-04 22:01:21 +02001305 // Update the maximum fuzzy score and the shown match
1306 // if the current item's score is higher
glepnira218cc62024-06-03 19:32:39 +02001307 if (compl->cp_score > max_fuzzy_score)
1308 {
1309 did_find_shown_match = TRUE;
1310 max_fuzzy_score = compl->cp_score;
1311 compl_shown_match = compl;
glepnir65407ce2024-07-06 16:09:19 +02001312 }
1313
1314 if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
1315 {
1316 cur = i;
glepnira218cc62024-06-03 19:32:39 +02001317 shown_match_ok = TRUE;
1318 }
1319
glepnirdca57fb2024-06-04 22:01:21 +02001320 // If there is no "no select" condition and the max fuzzy
1321 // score is positive, or there is no completion leader or the
1322 // leader length is zero, mark the shown match as valid and
1323 // reset the current index.
glepnira218cc62024-06-03 19:32:39 +02001324 if (!compl_no_select
1325 && (max_fuzzy_score > 0
1326 || (compl_leader == NULL || lead_len == 0)))
1327 {
glepnirf94c9c42024-06-14 21:11:56 +02001328 if (match_at_original_text(compl_shown_match))
glepnir105f7412024-06-15 15:32:22 +02001329 compl_shown_match = shown_compl;
glepnira218cc62024-06-03 19:32:39 +02001330 }
1331 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001332
1333 if (compl->cp_text[CPT_ABBR] != NULL)
1334 compl_match_array[i].pum_text =
1335 compl->cp_text[CPT_ABBR];
1336 else
1337 compl_match_array[i].pum_text = compl->cp_str;
1338 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1339 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
glepnira218cc62024-06-03 19:32:39 +02001340 compl_match_array[i].pum_score = compl->cp_score;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001341 if (compl->cp_text[CPT_MENU] != NULL)
1342 compl_match_array[i++].pum_extra =
1343 compl->cp_text[CPT_MENU];
1344 else
1345 compl_match_array[i++].pum_extra = compl->cp_fname;
1346 }
1347
glepnira218cc62024-06-03 19:32:39 +02001348 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001349 {
1350 did_find_shown_match = TRUE;
1351
1352 // When the original text is the shown match don't set
1353 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001354 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001355 shown_match_ok = TRUE;
1356
1357 if (!shown_match_ok && shown_compl != NULL)
1358 {
1359 // The shown match isn't displayed, set it to the
1360 // previously displayed match.
1361 compl_shown_match = shown_compl;
1362 shown_match_ok = TRUE;
1363 }
1364 }
1365 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001366 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001367
glepnira218cc62024-06-03 19:32:39 +02001368 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
zeertzjq8e567472024-06-14 20:04:42 +02001369 {
1370 for (i = 0; i < compl_match_arraysize; i++)
1371 compl_match_array[i].pum_idx = i;
glepnira218cc62024-06-03 19:32:39 +02001372 // sort by the largest score of fuzzy match
zeertzjq8e567472024-06-14 20:04:42 +02001373 qsort(compl_match_array, (size_t)compl_match_arraysize,
1374 sizeof(pumitem_T), ins_compl_fuzzy_cmp);
glepnir65407ce2024-07-06 16:09:19 +02001375 shown_match_ok = TRUE;
zeertzjq8e567472024-06-14 20:04:42 +02001376 }
glepnira218cc62024-06-03 19:32:39 +02001377
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001378 if (!shown_match_ok) // no displayed match at all
1379 cur = -1;
1380
1381 return cur;
1382}
1383
1384/*
1385 * Show the popup menu for the list of matches.
1386 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1387 */
1388 void
1389ins_compl_show_pum(void)
1390{
1391 int i;
1392 int cur = -1;
1393 colnr_T col;
1394
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001395 if (!pum_wanted() || !pum_enough_matches())
1396 return;
1397
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001398 // Update the screen later, before drawing the popup menu over it.
1399 pum_call_update_screen();
1400
1401 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001402 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001403 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001404 else
1405 {
1406 // popup menu already exists, only need to find the current item.
1407 for (i = 0; i < compl_match_arraysize; ++i)
1408 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1409 || compl_match_array[i].pum_text
1410 == compl_shown_match->cp_text[CPT_ABBR])
1411 {
1412 cur = i;
1413 break;
1414 }
1415 }
1416
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001417 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001418 {
1419#ifdef FEAT_EVAL
1420 if (compl_started && has_completechanged())
1421 trigger_complete_changed_event(cur);
1422#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001423 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001424 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001425
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001426 // In Replace mode when a $ is displayed at the end of the line only
1427 // part of the screen would be updated. We do need to redraw here.
1428 dollar_vcol = -1;
1429
1430 // Compute the screen column of the start of the completed text.
1431 // Use the cursor to get all wrapping and other settings right.
1432 col = curwin->w_cursor.col;
1433 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001434 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001435 pum_display(compl_match_array, compl_match_arraysize, cur);
1436 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001437
glepnircbb46b42024-02-03 18:11:13 +01001438 // After adding leader, set the current match to shown match.
1439 if (compl_started && compl_curr_match != compl_shown_match)
1440 compl_curr_match = compl_shown_match;
1441
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001442#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001443 if (has_completechanged())
1444 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001445#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001446}
1447
1448#define DICT_FIRST (1) // use just first element in "dict"
1449#define DICT_EXACT (2) // "dict" is the exact name of a file
1450
1451/*
glepnir40c1c332024-06-11 19:37:04 +02001452 * Get current completion leader
1453 */
1454 char_u *
1455ins_compl_leader(void)
1456{
glepnirf1891382024-06-17 18:35:25 +02001457 return compl_leader != NULL ? compl_leader : compl_orig_text;
glepnir40c1c332024-06-11 19:37:04 +02001458}
1459
1460/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001461 * Add any identifiers that match the given pattern "pat" in the list of
1462 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001463 */
1464 static void
1465ins_compl_dictionaries(
1466 char_u *dict_start,
1467 char_u *pat,
1468 int flags, // DICT_FIRST and/or DICT_EXACT
1469 int thesaurus) // Thesaurus completion
1470{
1471 char_u *dict = dict_start;
1472 char_u *ptr;
1473 char_u *buf;
1474 regmatch_T regmatch;
1475 char_u **files;
1476 int count;
1477 int save_p_scs;
1478 int dir = compl_direction;
1479
1480 if (*dict == NUL)
1481 {
1482#ifdef FEAT_SPELL
1483 // When 'dictionary' is empty and spell checking is enabled use
1484 // "spell".
1485 if (!thesaurus && curwin->w_p_spell)
1486 dict = (char_u *)"spell";
1487 else
1488#endif
1489 return;
1490 }
1491
1492 buf = alloc(LSIZE);
1493 if (buf == NULL)
1494 return;
1495 regmatch.regprog = NULL; // so that we can goto theend
1496
1497 // If 'infercase' is set, don't use 'smartcase' here
1498 save_p_scs = p_scs;
1499 if (curbuf->b_p_inf)
1500 p_scs = FALSE;
1501
1502 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1503 // to only match at the start of a line. Otherwise just match the
1504 // pattern. Also need to double backslashes.
1505 if (ctrl_x_mode_line_or_eval())
1506 {
1507 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1508 size_t len;
1509
1510 if (pat_esc == NULL)
1511 goto theend;
1512 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001513 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001514 if (ptr == NULL)
1515 {
1516 vim_free(pat_esc);
1517 goto theend;
1518 }
1519 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1520 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1521 vim_free(pat_esc);
1522 vim_free(ptr);
1523 }
1524 else
1525 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001526 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001527 if (regmatch.regprog == NULL)
1528 goto theend;
1529 }
1530
1531 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1532 regmatch.rm_ic = ignorecase(pat);
1533 while (*dict != NUL && !got_int && !compl_interrupted)
1534 {
1535 // copy one dictionary file name into buf
1536 if (flags == DICT_EXACT)
1537 {
1538 count = 1;
1539 files = &dict;
1540 }
1541 else
1542 {
1543 // Expand wildcards in the dictionary name, but do not allow
1544 // backticks (for security, the 'dict' option may have been set in
1545 // a modeline).
1546 copy_option_part(&dict, buf, LSIZE, ",");
1547# ifdef FEAT_SPELL
1548 if (!thesaurus && STRCMP(buf, "spell") == 0)
1549 count = -1;
1550 else
1551# endif
1552 if (vim_strchr(buf, '`') != NULL
1553 || expand_wildcards(1, &buf, &count, &files,
1554 EW_FILE|EW_SILENT) != OK)
1555 count = 0;
1556 }
1557
1558# ifdef FEAT_SPELL
1559 if (count == -1)
1560 {
1561 // Complete from active spelling. Skip "\<" in the pattern, we
1562 // don't use it as a RE.
1563 if (pat[0] == '\\' && pat[1] == '<')
1564 ptr = pat + 2;
1565 else
1566 ptr = pat;
1567 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1568 }
1569 else
1570# endif
1571 if (count > 0) // avoid warning for using "files" uninit
1572 {
1573 ins_compl_files(count, files, thesaurus, flags,
1574 &regmatch, buf, &dir);
1575 if (flags != DICT_EXACT)
1576 FreeWild(count, files);
1577 }
1578 if (flags != 0)
1579 break;
1580 }
1581
1582theend:
1583 p_scs = save_p_scs;
1584 vim_regfree(regmatch.regprog);
1585 vim_free(buf);
1586}
1587
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001588/*
1589 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1590 * skipping the word at 'skip_word'. Returns OK on success.
1591 */
1592 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001593thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001594 char_u *fname,
1595 char_u **buf_arg,
1596 int dir,
1597 char_u *skip_word)
1598{
1599 int status = OK;
1600 char_u *ptr;
1601 char_u *wstart;
1602
1603 // Add the other matches on the line
1604 ptr = *buf_arg;
1605 while (!got_int)
1606 {
1607 // Find start of the next word. Skip white
1608 // space and punctuation.
1609 ptr = find_word_start(ptr);
1610 if (*ptr == NUL || *ptr == NL)
1611 break;
1612 wstart = ptr;
1613
1614 // Find end of the word.
1615 if (has_mbyte)
1616 // Japanese words may have characters in
1617 // different classes, only separate words
1618 // with single-byte non-word characters.
1619 while (*ptr != NUL)
1620 {
1621 int l = (*mb_ptr2len)(ptr);
1622
1623 if (l < 2 && !vim_iswordc(*ptr))
1624 break;
1625 ptr += l;
1626 }
1627 else
1628 ptr = find_word_end(ptr);
1629
1630 // Add the word. Skip the regexp match.
1631 if (wstart != skip_word)
1632 {
1633 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1634 fname, dir, FALSE);
1635 if (status == FAIL)
1636 break;
1637 }
1638 }
1639
1640 *buf_arg = ptr;
1641 return status;
1642}
1643
1644/*
1645 * Process "count" dictionary/thesaurus "files" and add the text matching
1646 * "regmatch".
1647 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001648 static void
1649ins_compl_files(
1650 int count,
1651 char_u **files,
1652 int thesaurus,
1653 int flags,
1654 regmatch_T *regmatch,
1655 char_u *buf,
1656 int *dir)
1657{
1658 char_u *ptr;
1659 int i;
1660 FILE *fp;
1661 int add_r;
1662
1663 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1664 {
1665 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001666 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001667 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001668 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001669 vim_snprintf((char *)IObuff, IOSIZE,
1670 _("Scanning dictionary: %s"), (char *)files[i]);
1671 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1672 }
1673
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001674 if (fp == NULL)
1675 continue;
1676
1677 // Read dictionary file line by line.
1678 // Check each line for a match.
1679 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001680 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001681 ptr = buf;
1682 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001683 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001684 ptr = regmatch->startp[0];
1685 if (ctrl_x_mode_line_or_eval())
1686 ptr = find_line_end(ptr);
1687 else
1688 ptr = find_word_end(ptr);
1689 add_r = ins_compl_add_infercase(regmatch->startp[0],
1690 (int)(ptr - regmatch->startp[0]),
1691 p_ic, files[i], *dir, FALSE);
1692 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001693 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001694 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001695 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001696 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001697 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001698 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001699 if (add_r == OK)
1700 // if dir was BACKWARD then honor it just once
1701 *dir = FORWARD;
1702 else if (add_r == FAIL)
1703 break;
1704 // avoid expensive call to vim_regexec() when at end
1705 // of line
1706 if (*ptr == '\n' || got_int)
1707 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001708 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001709 line_breakcheck();
1710 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001711 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001712 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 }
1714}
1715
1716/*
1717 * Find the start of the next word.
1718 * Returns a pointer to the first char of the word. Also stops at a NUL.
1719 */
1720 char_u *
1721find_word_start(char_u *ptr)
1722{
1723 if (has_mbyte)
1724 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1725 ptr += (*mb_ptr2len)(ptr);
1726 else
1727 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1728 ++ptr;
1729 return ptr;
1730}
1731
1732/*
1733 * Find the end of the word. Assumes it starts inside a word.
1734 * Returns a pointer to just after the word.
1735 */
1736 char_u *
1737find_word_end(char_u *ptr)
1738{
1739 int start_class;
1740
1741 if (has_mbyte)
1742 {
1743 start_class = mb_get_class(ptr);
1744 if (start_class > 1)
1745 while (*ptr != NUL)
1746 {
1747 ptr += (*mb_ptr2len)(ptr);
1748 if (mb_get_class(ptr) != start_class)
1749 break;
1750 }
1751 }
1752 else
1753 while (vim_iswordc(*ptr))
1754 ++ptr;
1755 return ptr;
1756}
1757
1758/*
1759 * Find the end of the line, omitting CR and NL at the end.
1760 * Returns a pointer to just after the line.
1761 */
1762 static char_u *
1763find_line_end(char_u *ptr)
1764{
1765 char_u *s;
1766
1767 s = ptr + STRLEN(ptr);
1768 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1769 --s;
1770 return s;
1771}
1772
1773/*
1774 * Free the list of completions
1775 */
1776 static void
1777ins_compl_free(void)
1778{
1779 compl_T *match;
1780 int i;
1781
1782 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001783 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001784 VIM_CLEAR(compl_leader);
1785
1786 if (compl_first_match == NULL)
1787 return;
1788
1789 ins_compl_del_pum();
1790 pum_clear();
1791
1792 compl_curr_match = compl_first_match;
1793 do
1794 {
1795 match = compl_curr_match;
1796 compl_curr_match = compl_curr_match->cp_next;
1797 vim_free(match->cp_str);
1798 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001799 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001800 vim_free(match->cp_fname);
1801 for (i = 0; i < CPT_COUNT; ++i)
1802 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001803#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001804 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001805#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001806 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001807 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001808 compl_first_match = compl_curr_match = NULL;
1809 compl_shown_match = NULL;
1810 compl_old_match = NULL;
1811}
1812
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001813/*
1814 * Reset/clear the completion state.
1815 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001816 void
1817ins_compl_clear(void)
1818{
1819 compl_cont_status = 0;
1820 compl_started = FALSE;
1821 compl_matches = 0;
1822 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001823 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001824 VIM_CLEAR(compl_leader);
1825 edit_submode_extra = NULL;
1826 VIM_CLEAR(compl_orig_text);
1827 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001828#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 // clear v:completed_item
1830 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001831#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001832}
1833
1834/*
1835 * Return TRUE when Insert completion is active.
1836 */
1837 int
1838ins_compl_active(void)
1839{
1840 return compl_started;
1841}
1842
1843/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001844 * Selected one of the matches. When FALSE the match was edited or using the
1845 * longest common string.
1846 */
1847 int
1848ins_compl_used_match(void)
1849{
1850 return compl_used_match;
1851}
1852
1853/*
1854 * Initialize get longest common string.
1855 */
1856 void
1857ins_compl_init_get_longest(void)
1858{
1859 compl_get_longest = FALSE;
1860}
1861
1862/*
1863 * Returns TRUE when insert completion is interrupted.
1864 */
1865 int
1866ins_compl_interrupted(void)
1867{
1868 return compl_interrupted;
1869}
1870
1871/*
1872 * Returns TRUE if the <Enter> key selects a match in the completion popup
1873 * menu.
1874 */
1875 int
1876ins_compl_enter_selects(void)
1877{
1878 return compl_enter_selects;
1879}
1880
1881/*
1882 * Return the column where the text starts that is being completed
1883 */
1884 colnr_T
1885ins_compl_col(void)
1886{
1887 return compl_col;
1888}
1889
1890/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001891 * Return the length in bytes of the text being completed
1892 */
1893 int
1894ins_compl_len(void)
1895{
1896 return compl_length;
1897}
1898
1899/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001900 * Delete one character before the cursor and show the subset of the matches
1901 * that match the word that is now before the cursor.
1902 * Returns the character to be used, NUL if the work is done and another char
1903 * to be got from the user.
1904 */
1905 int
1906ins_compl_bs(void)
1907{
1908 char_u *line;
1909 char_u *p;
1910
1911 line = ml_get_curline();
1912 p = line + curwin->w_cursor.col;
1913 MB_PTR_BACK(line, p);
1914
1915 // Stop completion when the whole word was deleted. For Omni completion
1916 // allow the word to be deleted, we won't match everything.
1917 // Respect the 'backspace' option.
1918 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001919 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1920 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001921 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1922 - compl_length < 0))
1923 return K_BS;
1924
1925 // Deleted more than what was used to find matches or didn't finish
1926 // finding all matches: need to look for matches all over again.
1927 if (curwin->w_cursor.col <= compl_col + compl_length
1928 || ins_compl_need_restart())
1929 ins_compl_restart();
1930
1931 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001932 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001933 if (compl_leader == NULL)
1934 return K_BS;
1935
1936 ins_compl_new_leader();
1937 if (compl_shown_match != NULL)
1938 // Make sure current match is not a hidden item.
1939 compl_curr_match = compl_shown_match;
1940 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001941}
1942
1943/*
1944 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1945 * be called.
1946 */
1947 static int
1948ins_compl_need_restart(void)
1949{
1950 // Return TRUE if we didn't complete finding matches or when the
1951 // 'completefunc' returned "always" in the "refresh" dictionary item.
1952 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001953 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001954 && compl_opt_refresh_always);
1955}
1956
1957/*
1958 * Called after changing "compl_leader".
1959 * Show the popup menu with a different set of matches.
1960 * May also search for matches again if the previous search was interrupted.
1961 */
1962 static void
1963ins_compl_new_leader(void)
1964{
1965 ins_compl_del_pum();
1966 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001967 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001968 compl_used_match = FALSE;
1969
1970 if (compl_started)
1971 ins_compl_set_original_text(compl_leader);
1972 else
1973 {
1974#ifdef FEAT_SPELL
1975 spell_bad_len = 0; // need to redetect bad word
1976#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001977 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001978 // the popup menu display the changed text before the cursor. Set
1979 // "compl_restarting" to avoid that the first match is inserted.
1980 pum_call_update_screen();
1981#ifdef FEAT_GUI
1982 if (gui.in_use)
1983 {
1984 // Show the cursor after the match, not after the redrawn text.
1985 setcursor();
1986 out_flush_cursor(FALSE, FALSE);
1987 }
1988#endif
1989 compl_restarting = TRUE;
1990 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1991 compl_cont_status = 0;
1992 compl_restarting = FALSE;
1993 }
1994
1995 compl_enter_selects = !compl_used_match;
1996
1997 // Show the popup menu with a different set of matches.
1998 ins_compl_show_pum();
1999
2000 // Don't let Enter select the original text when there is no popup menu.
2001 if (compl_match_array == NULL)
2002 compl_enter_selects = FALSE;
2003}
2004
2005/*
2006 * Return the length of the completion, from the completion start column to
2007 * the cursor column. Making sure it never goes below zero.
2008 */
2009 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002010get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002011{
2012 int off = (int)curwin->w_cursor.col - (int)compl_col;
2013
2014 if (off < 0)
2015 return 0;
2016 return off;
2017}
2018
2019/*
2020 * Append one character to the match leader. May reduce the number of
2021 * matches.
2022 */
2023 void
2024ins_compl_addleader(int c)
2025{
2026 int cc;
2027
2028 if (stop_arrow() == FAIL)
2029 return;
2030 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2031 {
2032 char_u buf[MB_MAXBYTES + 1];
2033
2034 (*mb_char2bytes)(c, buf);
2035 buf[cc] = NUL;
2036 ins_char_bytes(buf, cc);
2037 if (compl_opt_refresh_always)
2038 AppendToRedobuff(buf);
2039 }
2040 else
2041 {
2042 ins_char(c);
2043 if (compl_opt_refresh_always)
2044 AppendCharToRedobuff(c);
2045 }
2046
2047 // If we didn't complete finding matches we must search again.
2048 if (ins_compl_need_restart())
2049 ins_compl_restart();
2050
2051 // When 'always' is set, don't reset compl_leader. While completing,
2052 // cursor doesn't point original position, changing compl_leader would
2053 // break redo.
2054 if (!compl_opt_refresh_always)
2055 {
2056 vim_free(compl_leader);
2057 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002058 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002059 if (compl_leader != NULL)
2060 ins_compl_new_leader();
2061 }
2062}
2063
2064/*
2065 * Setup for finding completions again without leaving CTRL-X mode. Used when
2066 * BS or a key was typed while still searching for matches.
2067 */
2068 static void
2069ins_compl_restart(void)
2070{
2071 ins_compl_free();
2072 compl_started = FALSE;
2073 compl_matches = 0;
2074 compl_cont_status = 0;
2075 compl_cont_mode = 0;
2076}
2077
2078/*
2079 * Set the first match, the original text.
2080 */
2081 static void
2082ins_compl_set_original_text(char_u *str)
2083{
2084 char_u *p;
2085
2086 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002087 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2088 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002089 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002090 {
2091 p = vim_strsave(str);
2092 if (p != NULL)
2093 {
2094 vim_free(compl_first_match->cp_str);
2095 compl_first_match->cp_str = p;
2096 }
2097 }
2098 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002099 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002100 {
2101 p = vim_strsave(str);
2102 if (p != NULL)
2103 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002104 vim_free(compl_first_match->cp_prev->cp_str);
2105 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002106 }
2107 }
2108}
2109
2110/*
2111 * Append one character to the match leader. May reduce the number of
2112 * matches.
2113 */
2114 void
2115ins_compl_addfrommatch(void)
2116{
2117 char_u *p;
2118 int len = (int)curwin->w_cursor.col - (int)compl_col;
2119 int c;
2120 compl_T *cp;
2121
2122 p = compl_shown_match->cp_str;
2123 if ((int)STRLEN(p) <= len) // the match is too short
2124 {
2125 // When still at the original match use the first entry that matches
2126 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002127 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002128 return;
2129
2130 p = NULL;
2131 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002132 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002133 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002134 if (compl_leader == NULL
2135 || ins_compl_equal(cp, compl_leader,
2136 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002137 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002138 p = cp->cp_str;
2139 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002140 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002141 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002142 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002143 return;
2144 }
2145 p += len;
2146 c = PTR2CHAR(p);
2147 ins_compl_addleader(c);
2148}
2149
2150/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002151 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002152 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2153 * compl_cont_mode and compl_cont_status.
2154 * Returns TRUE when the character is not to be inserted.
2155 */
2156 static int
2157set_ctrl_x_mode(int c)
2158{
2159 int retval = FALSE;
2160
2161 switch (c)
2162 {
2163 case Ctrl_E:
2164 case Ctrl_Y:
2165 // scroll the window one line up or down
2166 ctrl_x_mode = CTRL_X_SCROLL;
2167 if (!(State & REPLACE_FLAG))
2168 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2169 else
2170 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2171 edit_submode_pre = NULL;
2172 showmode();
2173 break;
2174 case Ctrl_L:
2175 // complete whole line
2176 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2177 break;
2178 case Ctrl_F:
2179 // complete filenames
2180 ctrl_x_mode = CTRL_X_FILES;
2181 break;
2182 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002183 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002184 ctrl_x_mode = CTRL_X_DICTIONARY;
2185 break;
2186 case Ctrl_R:
2187 // Register insertion without exiting CTRL-X mode
2188 // Simply allow ^R to happen without affecting ^X mode
2189 break;
2190 case Ctrl_T:
2191 // complete words from a thesaurus
2192 ctrl_x_mode = CTRL_X_THESAURUS;
2193 break;
2194#ifdef FEAT_COMPL_FUNC
2195 case Ctrl_U:
2196 // user defined completion
2197 ctrl_x_mode = CTRL_X_FUNCTION;
2198 break;
2199 case Ctrl_O:
2200 // omni completion
2201 ctrl_x_mode = CTRL_X_OMNI;
2202 break;
2203#endif
2204 case 's':
2205 case Ctrl_S:
2206 // complete spelling suggestions
2207 ctrl_x_mode = CTRL_X_SPELL;
2208#ifdef FEAT_SPELL
2209 ++emsg_off; // Avoid getting the E756 error twice.
2210 spell_back_to_badword();
2211 --emsg_off;
2212#endif
2213 break;
2214 case Ctrl_RSB:
2215 // complete tag names
2216 ctrl_x_mode = CTRL_X_TAGS;
2217 break;
2218#ifdef FEAT_FIND_ID
2219 case Ctrl_I:
2220 case K_S_TAB:
2221 // complete keywords from included files
2222 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2223 break;
2224 case Ctrl_D:
2225 // complete definitions from included files
2226 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2227 break;
2228#endif
2229 case Ctrl_V:
2230 case Ctrl_Q:
2231 // complete vim commands
2232 ctrl_x_mode = CTRL_X_CMDLINE;
2233 break;
2234 case Ctrl_Z:
2235 // stop completion
2236 ctrl_x_mode = CTRL_X_NORMAL;
2237 edit_submode = NULL;
2238 showmode();
2239 retval = TRUE;
2240 break;
2241 case Ctrl_P:
2242 case Ctrl_N:
2243 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2244 // just started ^X mode, or there were enough ^X's to cancel
2245 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2246 // do normal expansion when interrupting a different mode (say
2247 // ^X^F^X^P or ^P^X^X^P, see below)
2248 // nothing changes if interrupting mode 0, (eg, the flag
2249 // doesn't change when going to ADDING mode -- Acevedo
2250 if (!(compl_cont_status & CONT_INTRPT))
2251 compl_cont_status |= CONT_LOCAL;
2252 else if (compl_cont_mode != 0)
2253 compl_cont_status &= ~CONT_LOCAL;
2254 // FALLTHROUGH
2255 default:
2256 // If we have typed at least 2 ^X's... for modes != 0, we set
2257 // compl_cont_status = 0 (eg, as if we had just started ^X
2258 // mode).
2259 // For mode 0, we set "compl_cont_mode" to an impossible
2260 // value, in both cases ^X^X can be used to restart the same
2261 // mode (avoiding ADDING mode).
2262 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2263 // 'complete' and local ^P expansions respectively.
2264 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2265 // mode -- Acevedo
2266 if (c == Ctrl_X)
2267 {
2268 if (compl_cont_mode != 0)
2269 compl_cont_status = 0;
2270 else
2271 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2272 }
2273 ctrl_x_mode = CTRL_X_NORMAL;
2274 edit_submode = NULL;
2275 showmode();
2276 break;
2277 }
2278
2279 return retval;
2280}
2281
2282/*
2283 * Stop insert completion mode
2284 */
2285 static int
2286ins_compl_stop(int c, int prev_mode, int retval)
2287{
2288 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002289 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002290
2291 // Get here when we have finished typing a sequence of ^N and
2292 // ^P or other completion characters in CTRL-X mode. Free up
2293 // memory that was used, and make sure we can redo the insert.
2294 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2295 {
2296 // If any of the original typed text has been changed, eg when
2297 // ignorecase is set, we must add back-spaces to the redo
2298 // buffer. We add as few as necessary to delete just the part
2299 // of the original text that has changed.
2300 // When using the longest match, edited the match or used
2301 // CTRL-E then don't use the current match.
2302 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2303 ptr = compl_curr_match->cp_str;
2304 else
2305 ptr = NULL;
2306 ins_compl_fixRedoBufForLeader(ptr);
2307 }
2308
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002309 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002310
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002311 // When completing whole lines: fix indent for 'cindent'.
2312 // Otherwise, break line if it's too long.
2313 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2314 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002315 // re-indent the current line
2316 if (want_cindent)
2317 {
2318 do_c_expr_indent();
2319 want_cindent = FALSE; // don't do it again
2320 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002321 }
2322 else
2323 {
2324 int prev_col = curwin->w_cursor.col;
2325
2326 // put the cursor on the last char, for 'tw' formatting
2327 if (prev_col > 0)
2328 dec_cursor();
2329 // only format when something was inserted
2330 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2331 insertchar(NUL, 0, -1);
2332 if (prev_col > 0
2333 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2334 inc_cursor();
2335 }
2336
2337 // If the popup menu is displayed pressing CTRL-Y means accepting
2338 // the selection without inserting anything. When
2339 // compl_enter_selects is set the Enter key does the same.
2340 if ((c == Ctrl_Y || (compl_enter_selects
2341 && (c == CAR || c == K_KENTER || c == NL)))
2342 && pum_visible())
2343 retval = TRUE;
2344
2345 // CTRL-E means completion is Ended, go back to the typed text.
2346 // but only do this, if the Popup is still visible
2347 if (c == Ctrl_E)
2348 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002349 char_u *p = NULL;
2350
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002351 ins_compl_delete();
2352 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002353 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002354 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002355 p = compl_orig_text;
2356 if (p != NULL)
2357 {
2358 int compl_len = get_compl_len();
2359 int len = (int)STRLEN(p);
2360
2361 if (len > compl_len)
2362 ins_bytes_len(p + compl_len, len - compl_len);
2363 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002364 retval = TRUE;
2365 }
2366
2367 auto_format(FALSE, TRUE);
2368
2369 // Trigger the CompleteDonePre event to give scripts a chance to
2370 // act upon the completion before clearing the info, and restore
2371 // ctrl_x_mode, so that complete_info() can be used.
2372 ctrl_x_mode = prev_mode;
2373 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2374
2375 ins_compl_free();
2376 compl_started = FALSE;
2377 compl_matches = 0;
2378 if (!shortmess(SHM_COMPLETIONMENU))
2379 msg_clr_cmdline(); // necessary for "noshowmode"
2380 ctrl_x_mode = CTRL_X_NORMAL;
2381 compl_enter_selects = FALSE;
2382 if (edit_submode != NULL)
2383 {
2384 edit_submode = NULL;
2385 showmode();
2386 }
2387
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002388 if (c == Ctrl_C && cmdwin_type != 0)
2389 // Avoid the popup menu remains displayed when leaving the
2390 // command line window.
2391 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002392 // Indent now if a key was typed that is in 'cinkeys'.
2393 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2394 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002395 // Trigger the CompleteDone event to give scripts a chance to act
2396 // upon the end of completion.
2397 ins_apply_autocmds(EVENT_COMPLETEDONE);
2398
2399 return retval;
2400}
2401
2402/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002403 * Prepare for Insert mode completion, or stop it.
2404 * Called just after typing a character in Insert mode.
2405 * Returns TRUE when the character is not to be inserted;
2406 */
2407 int
2408ins_compl_prep(int c)
2409{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002410 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002411 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002412
2413 // Forget any previous 'special' messages if this is actually
2414 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2415 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2416 edit_submode_extra = NULL;
2417
zeertzjq440d4cb2023-03-02 17:51:32 +00002418 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002419 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002420 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002421 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002422 return retval;
2423
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002424#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002425 // Ignore mouse events in a popup window
2426 if (is_mouse_key(c))
2427 {
2428 // Ignore drag and release events, the position does not need to be in
2429 // the popup and it may have just closed.
2430 if (c == K_LEFTRELEASE
2431 || c == K_LEFTRELEASE_NM
2432 || c == K_MIDDLERELEASE
2433 || c == K_RIGHTRELEASE
2434 || c == K_X1RELEASE
2435 || c == K_X2RELEASE
2436 || c == K_LEFTDRAG
2437 || c == K_MIDDLEDRAG
2438 || c == K_RIGHTDRAG
2439 || c == K_X1DRAG
2440 || c == K_X2DRAG)
2441 return retval;
2442 if (popup_visible)
2443 {
2444 int row = mouse_row;
2445 int col = mouse_col;
2446 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2447
2448 if (wp != NULL && WIN_IS_POPUP(wp))
2449 return retval;
2450 }
2451 }
2452#endif
2453
zeertzjqdca29d92021-08-31 19:12:51 +02002454 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2455 {
2456 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2457 || !vim_is_ctrl_x_key(c))
2458 {
2459 // Not starting another completion mode.
2460 ctrl_x_mode = CTRL_X_CMDLINE;
2461
2462 // CTRL-X CTRL-Z should stop completion without inserting anything
2463 if (c == Ctrl_Z)
2464 retval = TRUE;
2465 }
2466 else
2467 {
2468 ctrl_x_mode = CTRL_X_CMDLINE;
2469
2470 // Other CTRL-X keys first stop completion, then start another
2471 // completion mode.
2472 ins_compl_prep(' ');
2473 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2474 }
2475 }
2476
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002477 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002478 if (ctrl_x_mode_not_defined_yet()
2479 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002480 {
zeertzjq529b9ad2024-06-05 20:27:06 +02002481 compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002482 compl_used_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002483 }
2484
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002485 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002486 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2487 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002488 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002489 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002490 {
2491 // We're already in CTRL-X mode, do we stay in it?
2492 if (!vim_is_ctrl_x_key(c))
2493 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002494 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002495 ctrl_x_mode = CTRL_X_NORMAL;
2496 else
2497 ctrl_x_mode = CTRL_X_FINISHED;
2498 edit_submode = NULL;
2499 }
2500 showmode();
2501 }
2502
2503 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2504 {
2505 // Show error message from attempted keyword completion (probably
2506 // 'Pattern not found') until another key is hit, then go back to
2507 // showing what mode we are in.
2508 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002509 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002510 && c != Ctrl_R && !ins_compl_pum_key(c))
2511 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002512 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002513 }
2514 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2515 // Trigger the CompleteDone event to give scripts a chance to act
2516 // upon the (possibly failed) completion.
2517 ins_apply_autocmds(EVENT_COMPLETEDONE);
2518
LemonBoy2bf52dd2022-04-09 18:17:34 +01002519 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002520
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002521 // reset continue_* if we left expansion-mode, if we stay they'll be
2522 // (re)set properly in ins_complete()
2523 if (!vim_is_ctrl_x_key(c))
2524 {
2525 compl_cont_status = 0;
2526 compl_cont_mode = 0;
2527 }
2528
2529 return retval;
2530}
2531
2532/*
2533 * Fix the redo buffer for the completion leader replacing some of the typed
2534 * text. This inserts backspaces and appends the changed text.
2535 * "ptr" is the known leader text or NUL.
2536 */
2537 static void
2538ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2539{
2540 int len;
2541 char_u *p;
2542 char_u *ptr = ptr_arg;
2543
2544 if (ptr == NULL)
2545 {
2546 if (compl_leader != NULL)
2547 ptr = compl_leader;
2548 else
2549 return; // nothing to do
2550 }
2551 if (compl_orig_text != NULL)
2552 {
2553 p = compl_orig_text;
2554 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2555 ;
2556 if (len > 0)
2557 len -= (*mb_head_off)(p, p + len);
2558 for (p += len; *p != NUL; MB_PTR_ADV(p))
2559 AppendCharToRedobuff(K_BS);
2560 }
2561 else
2562 len = 0;
2563 if (ptr != NULL)
2564 AppendToRedobuffLit(ptr + len, -1);
2565}
2566
2567/*
2568 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2569 * (depending on flag) starting from buf and looking for a non-scanned
2570 * buffer (other than curbuf). curbuf is special, if it is called with
2571 * buf=curbuf then it has to be the first call for a given flag/expansion.
2572 *
2573 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2574 */
2575 static buf_T *
2576ins_compl_next_buf(buf_T *buf, int flag)
2577{
2578 static win_T *wp = NULL;
2579
2580 if (flag == 'w') // just windows
2581 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002582 if (buf == curbuf || !win_valid(wp))
2583 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002584 wp = curwin;
2585 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2586 && wp->w_buffer->b_scanned)
2587 ;
2588 buf = wp->w_buffer;
2589 }
2590 else
2591 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2592 // (unlisted buffers)
2593 // When completing whole lines skip unloaded buffers.
2594 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2595 && ((flag == 'U'
2596 ? buf->b_p_bl
2597 : (!buf->b_p_bl
2598 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2599 || buf->b_scanned))
2600 ;
2601 return buf;
2602}
2603
2604#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002605
2606# ifdef FEAT_EVAL
2607static callback_T cfu_cb; // 'completefunc' callback function
2608static callback_T ofu_cb; // 'omnifunc' callback function
2609static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2610# endif
2611
2612/*
2613 * Copy a global callback function to a buffer local callback.
2614 */
2615 static void
2616copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2617{
2618 free_callback(bufcb);
2619 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2620 copy_callback(bufcb, globcb);
2621}
2622
2623/*
2624 * Parse the 'completefunc' option value and set the callback function.
2625 * Invoked when the 'completefunc' option is set. The option value can be a
2626 * name of a function (string), or function(<name>) or funcref(<name>) or a
2627 * lambda expression.
2628 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002629 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002630did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002631{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002632 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2633 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002634
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002635 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002636
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002637 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002638}
2639
2640/*
2641 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002642 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002643 */
2644 void
2645set_buflocal_cfu_callback(buf_T *buf UNUSED)
2646{
2647# ifdef FEAT_EVAL
2648 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2649# endif
2650}
2651
2652/*
2653 * Parse the 'omnifunc' option value and set the callback function.
2654 * Invoked when the 'omnifunc' option is set. The option value can be a
2655 * name of a function (string), or function(<name>) or funcref(<name>) or a
2656 * lambda expression.
2657 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002658 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002659did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002660{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002661 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2662 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002663
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002664 set_buflocal_ofu_callback(curbuf);
2665 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002666}
2667
2668/*
2669 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002670 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002671 */
2672 void
2673set_buflocal_ofu_callback(buf_T *buf UNUSED)
2674{
2675# ifdef FEAT_EVAL
2676 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2677# endif
2678}
2679
2680/*
2681 * Parse the 'thesaurusfunc' option value and set the callback function.
2682 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2683 * name of a function (string), or function(<name>) or funcref(<name>) or a
2684 * lambda expression.
2685 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002686 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002687did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002688{
2689 int retval;
2690
2691 if (*curbuf->b_p_tsrfu != NUL)
2692 {
2693 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002694 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2695 &curbuf->b_tsrfu_cb);
2696 }
2697 else
2698 {
2699 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002700 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2701 }
2702
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002703 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002704}
2705
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002706/*
2707 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002708 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002709 */
2710 int
2711set_ref_in_insexpand_funcs(int copyID)
2712{
2713 int abort = FALSE;
2714
2715 abort = set_ref_in_callback(&cfu_cb, copyID);
2716 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2717 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2718
2719 return abort;
2720}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002721
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002722/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002723 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002724 */
2725 static char_u *
2726get_complete_funcname(int type)
2727{
2728 switch (type)
2729 {
2730 case CTRL_X_FUNCTION:
2731 return curbuf->b_p_cfu;
2732 case CTRL_X_OMNI:
2733 return curbuf->b_p_ofu;
2734 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002735 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002736 default:
2737 return (char_u *)"";
2738 }
2739}
2740
2741/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002742 * Get the callback to use for insert mode completion.
2743 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002744 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002745get_insert_callback(int type)
2746{
2747 if (type == CTRL_X_FUNCTION)
2748 return &curbuf->b_cfu_cb;
2749 if (type == CTRL_X_OMNI)
2750 return &curbuf->b_ofu_cb;
2751 // CTRL_X_THESAURUS
2752 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2753}
2754
2755/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002756 * Execute user defined complete function 'completefunc', 'omnifunc' or
2757 * 'thesaurusfunc', and get matches in "matches".
2758 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002759 */
2760 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002761expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002762{
2763 list_T *matchlist = NULL;
2764 dict_T *matchdict = NULL;
2765 typval_T args[3];
2766 char_u *funcname;
2767 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002768 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002769 typval_T rettv;
2770 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002771 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002772
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002773 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002774 if (*funcname == NUL)
2775 return;
2776
2777 // Call 'completefunc' to obtain the list of matches.
2778 args[0].v_type = VAR_NUMBER;
2779 args[0].vval.v_number = 0;
2780 args[1].v_type = VAR_STRING;
2781 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2782 args[2].v_type = VAR_UNKNOWN;
2783
2784 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002785 // Lock the text to avoid weird things from happening. Also disallow
2786 // switching to another window, it should not be needed and may end up in
2787 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002788 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002789
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002790 cb = get_insert_callback(type);
2791 retval = call_callback(cb, 0, &rettv, 2, args);
2792
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002793 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002794 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002795 {
2796 switch (rettv.v_type)
2797 {
2798 case VAR_LIST:
2799 matchlist = rettv.vval.v_list;
2800 break;
2801 case VAR_DICT:
2802 matchdict = rettv.vval.v_dict;
2803 break;
2804 case VAR_SPECIAL:
2805 if (rettv.vval.v_number == VVAL_NONE)
2806 compl_opt_suppress_empty = TRUE;
2807 // FALLTHROUGH
2808 default:
2809 // TODO: Give error message?
2810 clear_tv(&rettv);
2811 break;
2812 }
2813 }
zeertzjqcfe45652022-05-27 17:26:55 +01002814 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002815
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002816 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002817 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002818 validate_cursor();
2819 if (!EQUAL_POS(curwin->w_cursor, pos))
2820 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002821 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002822 goto theend;
2823 }
2824
2825 if (matchlist != NULL)
2826 ins_compl_add_list(matchlist);
2827 else if (matchdict != NULL)
2828 ins_compl_add_dict(matchdict);
2829
2830theend:
2831 // Restore State, it might have been changed.
2832 State = save_State;
2833
2834 if (matchdict != NULL)
2835 dict_unref(matchdict);
2836 if (matchlist != NULL)
2837 list_unref(matchlist);
2838}
2839#endif // FEAT_COMPL_FUNC
2840
2841#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2842/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002843 * Add a match to the list of matches from a typeval_T.
2844 * If the given string is already in the list of completions, then return
2845 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2846 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002847 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002848 */
2849 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002850ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002851{
2852 char_u *word;
2853 int dup = FALSE;
2854 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002855 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002856 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002857 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002858 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002859
Bram Moolenaar08928322020-01-04 14:32:48 +01002860 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002861 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2862 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002863 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2864 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2865 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2866 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2867 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2868 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2869 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2870 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002871 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002872 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2873 dup = dict_get_number(tv->vval.v_dict, "dup");
2874 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2875 empty = dict_get_number(tv->vval.v_dict, "empty");
2876 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2877 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002878 flags |= CP_EQUAL;
2879 }
2880 else
2881 {
2882 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002883 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002884 }
2885 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002886 {
2887 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002888 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002889 }
2890 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2891 if (status != OK)
2892 clear_tv(&user_data);
2893 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002894}
2895
2896/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002897 * Add completions from a list.
2898 */
2899 static void
2900ins_compl_add_list(list_T *list)
2901{
2902 listitem_T *li;
2903 int dir = compl_direction;
2904
2905 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002906 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002907 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002908 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002909 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002910 // if dir was BACKWARD then honor it just once
2911 dir = FORWARD;
2912 else if (did_emsg)
2913 break;
2914 }
2915}
2916
2917/*
2918 * Add completions from a dict.
2919 */
2920 static void
2921ins_compl_add_dict(dict_T *dict)
2922{
2923 dictitem_T *di_refresh;
2924 dictitem_T *di_words;
2925
2926 // Check for optional "refresh" item.
2927 compl_opt_refresh_always = FALSE;
2928 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2929 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2930 {
2931 char_u *v = di_refresh->di_tv.vval.v_string;
2932
2933 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2934 compl_opt_refresh_always = TRUE;
2935 }
2936
2937 // Add completions from a "words" list.
2938 di_words = dict_find(dict, (char_u *)"words", 5);
2939 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2940 ins_compl_add_list(di_words->di_tv.vval.v_list);
2941}
2942
2943/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002944 * Start completion for the complete() function.
2945 * "startcol" is where the matched text starts (1 is first column).
2946 * "list" is the list of matches.
2947 */
2948 static void
2949set_completion(colnr_T startcol, list_T *list)
2950{
2951 int save_w_wrow = curwin->w_wrow;
2952 int save_w_leftcol = curwin->w_leftcol;
2953 int flags = CP_ORIGINAL_TEXT;
zeertzjqaa925ee2024-06-09 18:24:05 +02002954 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02002955 int compl_longest = (cur_cot_flags & COT_LONGEST) != 0;
2956 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
2957 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002958
2959 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002960 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002961 ins_compl_prep(' ');
2962 ins_compl_clear();
2963 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002964 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002965
2966 compl_direction = FORWARD;
2967 if (startcol > curwin->w_cursor.col)
2968 startcol = curwin->w_cursor.col;
2969 compl_col = startcol;
2970 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2971 // compl_pattern doesn't need to be set
2972 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2973 if (p_ic)
2974 flags |= CP_ICASE;
2975 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002976 -1, NULL, NULL, NULL, 0,
2977 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002978 return;
2979
2980 ctrl_x_mode = CTRL_X_EVAL;
2981
2982 ins_compl_add_list(list);
2983 compl_matches = ins_compl_make_cyclic();
2984 compl_started = TRUE;
2985 compl_used_match = TRUE;
2986 compl_cont_status = 0;
2987
2988 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002989 int no_select = compl_no_select || compl_longest;
2990 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002991 {
2992 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002993 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002994 // Down/Up has no real effect.
2995 ins_complete(K_UP, FALSE);
2996 }
2997 else
2998 ins_complete(Ctrl_N, FALSE);
2999 compl_enter_selects = compl_no_insert;
3000
3001 // Lazily show the popup menu, unless we got interrupted.
3002 if (!compl_interrupted)
3003 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01003004 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003005 out_flush();
3006}
3007
3008/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003009 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003010 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003011 void
3012f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003013{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003014 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003015
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003016 if (in_vim9script()
3017 && (check_for_number_arg(argvars, 0) == FAIL
3018 || check_for_list_arg(argvars, 1) == FAIL))
3019 return;
3020
Bram Moolenaar24959102022-05-07 20:01:16 +01003021 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003022 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003023 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003024 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003025 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003026
3027 // Check for undo allowed here, because if something was already inserted
3028 // the line was already saved for undo and this check isn't done.
3029 if (!undo_allowed())
3030 return;
3031
Bram Moolenaard83392a2022-09-01 12:22:46 +01003032 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003033 {
3034 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3035 if (startcol > 0)
3036 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003037 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003038}
3039
3040/*
3041 * "complete_add()" function
3042 */
3043 void
3044f_complete_add(typval_T *argvars, typval_T *rettv)
3045{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003046 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003047 return;
3048
Bram Moolenaar440cf092021-04-03 20:13:30 +02003049 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003050}
3051
3052/*
3053 * "complete_check()" function
3054 */
3055 void
3056f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3057{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003058 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003059 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003060
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003061 ins_compl_check_keys(0, TRUE);
3062 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003063
3064 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003065}
3066
3067/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003068 * Return Insert completion mode name string
3069 */
3070 static char_u *
3071ins_compl_mode(void)
3072{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003073 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003074 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3075
3076 return (char_u *)"";
3077}
3078
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003079/*
3080 * Assign the sequence number to all the completion matches which don't have
3081 * one assigned yet.
3082 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003083 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003084ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003085{
3086 int number = 0;
3087 compl_T *match;
3088
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003089 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003090 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003091 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003092 // This should normally succeed already at the first loop
3093 // cycle, so it's fast!
3094 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003095 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003096 if (match->cp_number != -1)
3097 {
3098 number = match->cp_number;
3099 break;
3100 }
3101 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003102 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003103 for (match = match->cp_next;
3104 match != NULL && match->cp_number == -1;
3105 match = match->cp_next)
3106 match->cp_number = ++number;
3107 }
3108 else // BACKWARD
3109 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003110 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003111 // number. This should normally succeed already at the
3112 // first loop cycle, so it's fast!
3113 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003114 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003115 if (match->cp_number != -1)
3116 {
3117 number = match->cp_number;
3118 break;
3119 }
3120 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003121 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003122 for (match = match->cp_prev; match
3123 && match->cp_number == -1;
3124 match = match->cp_prev)
3125 match->cp_number = ++number;
3126 }
3127}
3128
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003129/*
3130 * Get complete information
3131 */
3132 static void
3133get_complete_info(list_T *what_list, dict_T *retdict)
3134{
3135 int ret = OK;
3136 listitem_T *item;
3137#define CI_WHAT_MODE 0x01
3138#define CI_WHAT_PUM_VISIBLE 0x02
3139#define CI_WHAT_ITEMS 0x04
3140#define CI_WHAT_SELECTED 0x08
3141#define CI_WHAT_INSERTED 0x10
3142#define CI_WHAT_ALL 0xff
3143 int what_flag;
3144
3145 if (what_list == NULL)
3146 what_flag = CI_WHAT_ALL;
3147 else
3148 {
3149 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003150 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003151 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003152 {
3153 char_u *what = tv_get_string(&item->li_tv);
3154
3155 if (STRCMP(what, "mode") == 0)
3156 what_flag |= CI_WHAT_MODE;
3157 else if (STRCMP(what, "pum_visible") == 0)
3158 what_flag |= CI_WHAT_PUM_VISIBLE;
3159 else if (STRCMP(what, "items") == 0)
3160 what_flag |= CI_WHAT_ITEMS;
3161 else if (STRCMP(what, "selected") == 0)
3162 what_flag |= CI_WHAT_SELECTED;
3163 else if (STRCMP(what, "inserted") == 0)
3164 what_flag |= CI_WHAT_INSERTED;
3165 }
3166 }
3167
3168 if (ret == OK && (what_flag & CI_WHAT_MODE))
3169 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3170
3171 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3172 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3173
Girish Palya8950bf72024-03-20 20:07:29 +01003174 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003175 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003176 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003177 dict_T *di;
3178 compl_T *match;
3179 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003180
Girish Palya8950bf72024-03-20 20:07:29 +01003181 if (what_flag & CI_WHAT_ITEMS)
3182 {
3183 li = list_alloc();
3184 if (li == NULL)
3185 return;
3186 ret = dict_add_list(retdict, "items", li);
3187 }
3188 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3189 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3190 ins_compl_update_sequence_numbers();
3191 if (ret == OK && compl_first_match != NULL)
3192 {
3193 int list_idx = 0;
3194 match = compl_first_match;
3195 do
3196 {
3197 if (!match_at_original_text(match))
3198 {
3199 if (what_flag & CI_WHAT_ITEMS)
3200 {
3201 di = dict_alloc();
3202 if (di == NULL)
3203 return;
3204 ret = list_append_dict(li, di);
3205 if (ret != OK)
3206 return;
3207 dict_add_string(di, "word", match->cp_str);
3208 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3209 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3210 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3211 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3212 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3213 // Add an empty string for backwards compatibility
3214 dict_add_string(di, "user_data", (char_u *)"");
3215 else
3216 dict_add_tv(di, "user_data", &match->cp_user_data);
3217 }
3218 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3219 selected_idx = list_idx;
3220 list_idx += 1;
3221 }
3222 match = match->cp_next;
3223 }
3224 while (match != NULL && !is_first_match(match));
3225 }
3226 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3227 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003228 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003229
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003230 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3231 {
3232 // TODO
3233 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003234}
3235
3236/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003237 * "complete_info()" function
3238 */
3239 void
3240f_complete_info(typval_T *argvars, typval_T *rettv)
3241{
3242 list_T *what_list = NULL;
3243
Bram Moolenaar93a10962022-06-16 11:42:09 +01003244 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003245 return;
3246
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003247 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3248 return;
3249
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003250 if (argvars[0].v_type != VAR_UNKNOWN)
3251 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003252 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003253 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003254 what_list = argvars[0].vval.v_list;
3255 }
3256 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003257}
3258#endif
3259
3260/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003261 * Returns TRUE when using a user-defined function for thesaurus completion.
3262 */
3263 static int
3264thesaurus_func_complete(int type UNUSED)
3265{
3266#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003267 return type == CTRL_X_THESAURUS
3268 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003269#else
3270 return FALSE;
3271#endif
3272}
3273
3274/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003275 * Return value of process_next_cpt_value()
3276 */
3277enum
3278{
3279 INS_COMPL_CPT_OK = 1,
3280 INS_COMPL_CPT_CONT,
3281 INS_COMPL_CPT_END
3282};
3283
3284/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003285 * state information used for getting the next set of insert completion
3286 * matches.
3287 */
3288typedef struct
3289{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003290 char_u *e_cpt_copy; // copy of 'complete'
3291 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003292 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003293 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003294 pos_T prev_match_pos; // previous match position
3295 int set_match_pos; // save first_match_pos/last_match_pos
3296 pos_T first_match_pos; // first match position
3297 pos_T last_match_pos; // last match position
3298 int found_all; // found all matches of a certain type.
3299 char_u *dict; // dictionary file to search
3300 int dict_f; // "dict" is an exact file name or not
3301} ins_compl_next_state_T;
3302
3303/*
3304 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003305 *
3306 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003307 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003308 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003309 * st->found_all - all matches of this type are found
3310 * st->ins_buf - search for completions in this buffer
3311 * st->first_match_pos - position of the first completion match
3312 * st->last_match_pos - position of the last completion match
3313 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003314 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 * st->dict - name of the dictionary or thesaurus file to search
3316 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003317 *
3318 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003319 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3320 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003321 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003322 */
3323 static int
3324process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003325 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 int *compl_type_arg,
glepnir8159fb12024-07-17 20:32:54 +02003327 pos_T *start_match_pos,
3328 int in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003329{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003330 int compl_type = -1;
3331 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003332
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003335 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3336 st->e_cpt++;
3337
3338 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003339 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003340 st->ins_buf = curbuf;
3341 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003342 // Move the cursor back one character so that ^N can match the
3343 // word immediately after the cursor.
glepnir8159fb12024-07-17 20:32:54 +02003344 if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003345 {
3346 // Move the cursor to after the last character in the
3347 // buffer, so that word at start of buffer is found
3348 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003349 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003350 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003351 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003352 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003353 compl_type = 0;
3354
3355 // Remember the first match so that the loop stops when we
3356 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003357 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003358 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003359 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003360 && (st->ins_buf = ins_compl_next_buf(
3361 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003362 {
3363 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003364 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003365 {
3366 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003367 st->first_match_pos.col = st->last_match_pos.col = 0;
3368 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3369 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003370 compl_type = 0;
3371 }
3372 else // unloaded buffer, scan like dictionary
3373 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003374 st->found_all = TRUE;
3375 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003376 {
3377 status = INS_COMPL_CPT_CONT;
3378 goto done;
3379 }
3380 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003381 st->dict = st->ins_buf->b_fname;
3382 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003383 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003384 if (!shortmess(SHM_COMPLETIONSCAN))
3385 {
3386 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3387 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3388 st->ins_buf->b_fname == NULL
3389 ? buf_spname(st->ins_buf)
3390 : st->ins_buf->b_sfname == NULL
3391 ? st->ins_buf->b_fname
3392 : st->ins_buf->b_sfname);
3393 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3394 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003395 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003396 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003397 status = INS_COMPL_CPT_END;
3398 else
3399 {
3400 if (ctrl_x_mode_line_or_eval())
3401 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003402 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003404 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003405 compl_type = CTRL_X_DICTIONARY;
3406 else
3407 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003408 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003409 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003410 st->dict = st->e_cpt;
3411 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003412 }
3413 }
3414#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003415 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003416 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003417 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003418 compl_type = CTRL_X_PATH_DEFINES;
3419#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003420 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003421 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003422 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003423 if (!shortmess(SHM_COMPLETIONSCAN))
3424 {
3425 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3426 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3427 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3428 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 }
3430 else
3431 compl_type = -1;
3432
3433 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003434 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003435
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003436 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003437 if (compl_type == -1)
3438 status = INS_COMPL_CPT_CONT;
3439 }
3440
3441done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003442 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003443 return status;
3444}
3445
3446#ifdef FEAT_FIND_ID
3447/*
3448 * Get the next set of identifiers or defines matching "compl_pattern" in
3449 * included files.
3450 */
3451 static void
3452get_next_include_file_completion(int compl_type)
3453{
3454 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003455 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003456 (compl_type == CTRL_X_PATH_DEFINES
3457 && !(compl_cont_status & CONT_SOL))
3458 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003459 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003460}
3461#endif
3462
3463/*
3464 * Get the next set of words matching "compl_pattern" in dictionary or
3465 * thesaurus files.
3466 */
3467 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003468get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003469{
3470#ifdef FEAT_COMPL_FUNC
3471 if (thesaurus_func_complete(compl_type))
3472 expand_by_function(compl_type, compl_pattern);
3473 else
3474#endif
3475 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003476 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003477 : (compl_type == CTRL_X_THESAURUS
3478 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3479 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3480 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003481 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003482 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003483}
3484
3485/*
3486 * Get the next set of tag names matching "compl_pattern".
3487 */
3488 static void
3489get_next_tag_completion(void)
3490{
3491 int save_p_ic;
3492 char_u **matches;
3493 int num_matches;
3494
3495 // set p_ic according to p_ic, p_scs and pat for find_tags().
3496 save_p_ic = p_ic;
3497 p_ic = ignorecase(compl_pattern);
3498
3499 // Find up to TAG_MANY matches. Avoids that an enormous number
3500 // of matches is found when compl_pattern is empty
3501 g_tag_at_cursor = TRUE;
3502 if (find_tags(compl_pattern, &num_matches, &matches,
3503 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003504 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003505 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3506 ins_compl_add_matches(num_matches, matches, p_ic);
3507 g_tag_at_cursor = FALSE;
3508 p_ic = save_p_ic;
3509}
3510
3511/*
glepnir8159fb12024-07-17 20:32:54 +02003512 * Compare function for qsort
3513 */
3514static int compare_scores(const void *a, const void *b)
3515{
3516 int idx_a = *(const int *)a;
3517 int idx_b = *(const int *)b;
3518 int score_a = compl_fuzzy_scores[idx_a];
3519 int score_b = compl_fuzzy_scores[idx_b];
3520 return (score_a > score_b) ? -1 : (score_a < score_b) ? 1 : 0;
3521}
3522
3523/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003524 * Get the next set of filename matching "compl_pattern".
3525 */
3526 static void
3527get_next_filename_completion(void)
3528{
3529 char_u **matches;
3530 int num_matches;
glepnir8159fb12024-07-17 20:32:54 +02003531 char_u *ptr;
3532 garray_T fuzzy_indices;
3533 int i;
3534 int score;
3535 char_u *leader = ins_compl_leader();
3536 int leader_len = STRLEN(leader);
3537 int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0);
3538 char_u **sorted_matches;
3539 int *fuzzy_indices_data;
glepnir0be03e12024-07-19 16:45:05 +02003540 char_u *last_sep = NULL;
3541 size_t path_with_wildcard_len;
3542 char_u *path_with_wildcard;
glepnir8159fb12024-07-17 20:32:54 +02003543
3544 if (in_fuzzy)
3545 {
glepnir0be03e12024-07-19 16:45:05 +02003546 last_sep = vim_strrchr(leader, PATHSEP);
3547 if (last_sep == NULL)
3548 {
3549 // No path separator or separator is the last character,
3550 // fuzzy match the whole leader
3551 vim_free(compl_pattern);
3552 compl_pattern = vim_strsave((char_u *)"*");
3553 compl_patternlen = STRLEN(compl_pattern);
3554 }
3555 else if (*(last_sep + 1) == '\0')
3556 in_fuzzy = FALSE;
3557 else
3558 {
3559 // Split leader into path and file parts
3560 int path_len = last_sep - leader + 1;
3561 path_with_wildcard_len = path_len + 2;
3562 path_with_wildcard = alloc(path_with_wildcard_len);
3563 if (path_with_wildcard != NULL)
3564 {
3565 vim_strncpy(path_with_wildcard, leader, path_len);
3566 vim_strcat(path_with_wildcard, (char_u *)"*", path_with_wildcard_len);
3567 vim_free(compl_pattern);
3568 compl_pattern = path_with_wildcard;
3569 compl_patternlen = STRLEN(compl_pattern);
3570
3571 // Move leader to the file part
3572 leader = last_sep + 1;
3573 }
3574 }
glepnir8159fb12024-07-17 20:32:54 +02003575 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003576
3577 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3578 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3579 return;
3580
3581 // May change home directory back to "~".
3582 tilde_replace(compl_pattern, num_matches, matches);
3583#ifdef BACKSLASH_IN_FILENAME
3584 if (curbuf->b_p_csl[0] != NUL)
3585 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003586 for (i = 0; i < num_matches; ++i)
3587 {
glepnir8159fb12024-07-17 20:32:54 +02003588 ptr = matches[i];
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003589 while (*ptr != NUL)
3590 {
3591 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3592 *ptr = '/';
3593 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3594 *ptr = '\\';
3595 ptr += (*mb_ptr2len)(ptr);
3596 }
3597 }
3598 }
3599#endif
glepnir8159fb12024-07-17 20:32:54 +02003600
3601 if (in_fuzzy)
3602 {
3603 ga_init2(&fuzzy_indices, sizeof(int), 10);
3604 compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches);
3605
3606 for (i = 0; i < num_matches; i++)
3607 {
3608 ptr = matches[i];
3609 score = fuzzy_match_str(ptr, leader);
3610 if (score > 0)
3611 {
3612 if (ga_grow(&fuzzy_indices, 1) == OK)
3613 {
3614 ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i;
3615 compl_fuzzy_scores[i] = score;
3616 fuzzy_indices.ga_len++;
3617 }
3618 }
3619 }
3620
3621 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3622 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
3623
3624 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3625 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3626 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
3627
3628 FreeWild(num_matches, matches);
3629 matches = sorted_matches;
3630 num_matches = fuzzy_indices.ga_len;
3631 vim_free(compl_fuzzy_scores);
3632 ga_clear(&fuzzy_indices);
3633 }
3634
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003635 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3636}
3637
3638/*
3639 * Get the next set of command-line completions matching "compl_pattern".
3640 */
3641 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003642get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003643{
3644 char_u **matches;
3645 int num_matches;
3646
3647 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003648 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003649 ins_compl_add_matches(num_matches, matches, FALSE);
3650}
3651
3652/*
3653 * Get the next set of spell suggestions matching "compl_pattern".
3654 */
3655 static void
3656get_next_spell_completion(linenr_T lnum UNUSED)
3657{
3658#ifdef FEAT_SPELL
3659 char_u **matches;
3660 int num_matches;
3661
3662 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3663 if (num_matches > 0)
3664 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003665 else
3666 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003667#endif
3668}
3669
3670/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003671 * Return the next word or line from buffer "ins_buf" at position
3672 * "cur_match_pos" for completion. The length of the match is set in "len".
3673 */
3674 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003675ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003676 buf_T *ins_buf, // buffer being scanned
3677 pos_T *cur_match_pos, // current match position
3678 int *match_len,
3679 int *cont_s_ipos) // next ^X<> will set initial_pos
3680{
3681 char_u *ptr;
3682 int len;
3683
3684 *match_len = 0;
3685 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3686 cur_match_pos->col;
3687 if (ctrl_x_mode_line_or_eval())
3688 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003689 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003690 {
3691 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3692 return NULL;
3693 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3694 if (!p_paste)
3695 ptr = skipwhite(ptr);
3696 }
3697 len = (int)STRLEN(ptr);
3698 }
3699 else
3700 {
3701 char_u *tmp_ptr = ptr;
3702
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003703 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003704 {
3705 tmp_ptr += compl_length;
3706 // Skip if already inside a word.
3707 if (vim_iswordp(tmp_ptr))
3708 return NULL;
3709 // Find start of next word.
3710 tmp_ptr = find_word_start(tmp_ptr);
3711 }
3712 // Find end of this word.
3713 tmp_ptr = find_word_end(tmp_ptr);
3714 len = (int)(tmp_ptr - ptr);
3715
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003716 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003717 {
3718 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3719 {
3720 // Try next line, if any. the new word will be
3721 // "join" as if the normal command "J" was used.
3722 // IOSIZE is always greater than
3723 // compl_length, so the next STRNCPY always
3724 // works -- Acevedo
3725 STRNCPY(IObuff, ptr, len);
3726 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3727 tmp_ptr = ptr = skipwhite(ptr);
3728 // Find start of next word.
3729 tmp_ptr = find_word_start(tmp_ptr);
3730 // Find end of next word.
3731 tmp_ptr = find_word_end(tmp_ptr);
3732 if (tmp_ptr > ptr)
3733 {
3734 if (*ptr != ')' && IObuff[len - 1] != TAB)
3735 {
3736 if (IObuff[len - 1] != ' ')
3737 IObuff[len++] = ' ';
3738 // IObuf =~ "\k.* ", thus len >= 2
3739 if (p_js
3740 && (IObuff[len - 2] == '.'
3741 || (vim_strchr(p_cpo, CPO_JOINSP)
3742 == NULL
3743 && (IObuff[len - 2] == '?'
3744 || IObuff[len - 2] == '!'))))
3745 IObuff[len++] = ' ';
3746 }
3747 // copy as much as possible of the new word
3748 if (tmp_ptr - ptr >= IOSIZE - len)
3749 tmp_ptr = ptr + IOSIZE - len - 1;
3750 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3751 len += (int)(tmp_ptr - ptr);
3752 *cont_s_ipos = TRUE;
3753 }
3754 IObuff[len] = NUL;
3755 ptr = IObuff;
3756 }
3757 if (len == compl_length)
3758 return NULL;
3759 }
3760 }
3761
3762 *match_len = len;
3763 return ptr;
3764}
3765
3766/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003767 * Get the next set of words matching "compl_pattern" for default completion(s)
3768 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003769 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3770 * position "st->start_pos" in the "compl_direction" direction. If
3771 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3772 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003773 * Returns OK if a new next match is found, otherwise returns FAIL.
3774 */
3775 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003776get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003777{
3778 int found_new_match = FAIL;
3779 int save_p_scs;
3780 int save_p_ws;
3781 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003782 char_u *ptr = NULL;
3783 int len = 0;
3784 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3785 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003786
3787 // If 'infercase' is set, don't use 'smartcase' here
3788 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003789 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003790 p_scs = FALSE;
3791
3792 // Buffers other than curbuf are scanned from the beginning or the
3793 // end but never from the middle, thus setting nowrapscan in this
3794 // buffer is a good idea, on the other hand, we always set
3795 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3796 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003797 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003798 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003799 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003800 p_ws = TRUE;
3801 looped_around = FALSE;
3802 for (;;)
3803 {
3804 int cont_s_ipos = FALSE;
3805
3806 ++msg_silent; // Don't want messages for wrapscan.
3807
3808 // ctrl_x_mode_line_or_eval() || word-wise search that
3809 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003810 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003811 found_new_match = search_for_exact_line(st->ins_buf,
3812 st->cur_match_pos, compl_direction, compl_pattern);
glepnir8159fb12024-07-17 20:32:54 +02003813 else if (in_fuzzy)
3814 found_new_match = search_for_fuzzy_match(st->ins_buf,
3815 st->cur_match_pos, leader, compl_direction,
3816 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003817 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003818 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003819 NULL, compl_direction, compl_pattern, compl_patternlen,
3820 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003821 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003822 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003823 {
3824 // set "compl_started" even on fail
3825 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003826 st->first_match_pos = *st->cur_match_pos;
3827 st->last_match_pos = *st->cur_match_pos;
3828 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003829 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003830 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3831 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003832 {
3833 found_new_match = FAIL;
3834 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003835 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003836 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3837 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3838 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003839 {
3840 if (looped_around)
3841 found_new_match = FAIL;
3842 else
3843 looped_around = TRUE;
3844 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003845 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003846 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3847 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3848 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003849 {
3850 if (looped_around)
3851 found_new_match = FAIL;
3852 else
3853 looped_around = TRUE;
3854 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003855 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003856 if (found_new_match == FAIL)
3857 break;
3858
3859 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003860 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003861 && start_pos->lnum == st->cur_match_pos->lnum
3862 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003863 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003864
glepnir8159fb12024-07-17 20:32:54 +02003865 if (!in_fuzzy)
3866 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3867 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003868 if (ptr == NULL)
3869 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003870
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003871 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003872 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003873 0, cont_s_ipos) != NOTDONE)
3874 {
3875 found_new_match = OK;
3876 break;
3877 }
3878 }
3879 p_scs = save_p_scs;
3880 p_ws = save_p_ws;
3881
3882 return found_new_match;
3883}
3884
3885/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003886 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003887 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003888 */
3889 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003890get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003891{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003892 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003893
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003894 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003895 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003896 case -1:
3897 break;
3898#ifdef FEAT_FIND_ID
3899 case CTRL_X_PATH_PATTERNS:
3900 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003901 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003902 break;
3903#endif
3904
3905 case CTRL_X_DICTIONARY:
3906 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003907 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3908 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003909 break;
3910
3911 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003912 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003913 break;
3914
3915 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003916 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003917 break;
3918
3919 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003920 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003921 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003922 break;
3923
3924#ifdef FEAT_COMPL_FUNC
3925 case CTRL_X_FUNCTION:
3926 case CTRL_X_OMNI:
3927 expand_by_function(type, compl_pattern);
3928 break;
3929#endif
3930
3931 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003932 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003933 break;
3934
3935 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003936 found_new_match = get_next_default_completion(st, ini);
3937 if (found_new_match == FAIL && st->ins_buf == curbuf)
3938 st->found_all = TRUE;
3939 }
3940
3941 // check if compl_curr_match has changed, (e.g. other type of
3942 // expansion added something)
3943 if (type != 0 && compl_curr_match != compl_old_match)
3944 found_new_match = OK;
3945
3946 return found_new_match;
3947}
3948
3949/*
3950 * Get the next expansion(s), using "compl_pattern".
3951 * The search starts at position "ini" in curbuf and in the direction
3952 * compl_direction.
3953 * When "compl_started" is FALSE start at that position, otherwise continue
3954 * where we stopped searching before.
3955 * This may return before finding all the matches.
3956 * Return the total number of matches or -1 if still unknown -- Acevedo
3957 */
3958 static int
3959ins_compl_get_exp(pos_T *ini)
3960{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003961 static ins_compl_next_state_T st;
3962 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003963 int i;
3964 int found_new_match;
3965 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02003966 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003967
3968 if (!compl_started)
3969 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003970 buf_T *buf;
3971
3972 FOR_ALL_BUFFERS(buf)
3973 buf->b_scanned = 0;
3974 if (!st_cleared)
3975 {
3976 CLEAR_FIELD(st);
3977 st_cleared = TRUE;
3978 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003979 st.found_all = FALSE;
3980 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003981 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003982 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003983 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3984 ? (char_u *)"." : curbuf->b_p_cpt);
3985 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003986 st.last_match_pos = st.first_match_pos = *ini;
3987 }
3988 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3989 st.ins_buf = curbuf; // In case the buffer was wiped out.
3990
3991 compl_old_match = compl_curr_match; // remember the last current match
glepnir8159fb12024-07-17 20:32:54 +02003992 if (in_fuzzy)
3993 st.cur_match_pos = (compl_dir_forward())
3994 ? &st.last_match_pos : &st.first_match_pos;
3995 else
3996 st.cur_match_pos = &st.last_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003997
3998 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3999 for (;;)
4000 {
4001 found_new_match = FAIL;
4002 st.set_match_pos = FALSE;
4003
4004 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4005 // or if found_all says this entry is done. For ^X^L only use the
4006 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004007 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004008 && (!compl_started || st.found_all))
4009 {
glepnir8159fb12024-07-17 20:32:54 +02004010 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004011
4012 if (status == INS_COMPL_CPT_END)
4013 break;
4014 if (status == INS_COMPL_CPT_CONT)
4015 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004016 }
4017
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004018 // If complete() was called then compl_pattern has been reset. The
4019 // following won't work then, bail out.
4020 if (compl_pattern == NULL)
4021 break;
4022
4023 // get the next set of completion matches
4024 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004025
4026 // break the loop for specialized modes (use 'complete' just for the
4027 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4028 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004029 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4030 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004031 {
4032 if (got_int)
4033 break;
4034 // Fill the popup menu as soon as possible.
4035 if (type != -1)
4036 ins_compl_check_keys(0, FALSE);
4037
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004038 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004039 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4040 break;
4041 compl_started = TRUE;
4042 }
4043 else
4044 {
4045 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004046 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004047 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004048
4049 compl_started = FALSE;
4050 }
4051 }
4052 compl_started = TRUE;
4053
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004054 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004055 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004056 found_new_match = FAIL;
4057
4058 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004059 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060 && !ctrl_x_mode_line_or_eval()))
4061 i = ins_compl_make_cyclic();
4062
4063 if (compl_old_match != NULL)
4064 {
4065 // If several matches were added (FORWARD) or the search failed and has
4066 // just been made cyclic then we have to move compl_curr_match to the
4067 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004068 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069 : compl_old_match->cp_prev;
4070 if (compl_curr_match == NULL)
4071 compl_curr_match = compl_old_match;
4072 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004073 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004074
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004075 return i;
4076}
4077
4078/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004079 * Update "compl_shown_match" to the actually shown match, it may differ when
4080 * "compl_leader" is used to omit some of the matches.
4081 */
4082 static void
4083ins_compl_update_shown_match(void)
4084{
4085 while (!ins_compl_equal(compl_shown_match,
4086 compl_leader, (int)STRLEN(compl_leader))
4087 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004088 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004089 compl_shown_match = compl_shown_match->cp_next;
4090
4091 // If we didn't find it searching forward, and compl_shows_dir is
4092 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004093 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004094 && !ins_compl_equal(compl_shown_match,
4095 compl_leader, (int)STRLEN(compl_leader))
4096 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004097 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004098 {
4099 while (!ins_compl_equal(compl_shown_match,
4100 compl_leader, (int)STRLEN(compl_leader))
4101 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004102 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004103 compl_shown_match = compl_shown_match->cp_prev;
4104 }
4105}
4106
4107/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004108 * Delete the old text being completed.
4109 */
4110 void
4111ins_compl_delete(void)
4112{
4113 int col;
4114
4115 // In insert mode: Delete the typed part.
4116 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004117 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004118 if ((int)curwin->w_cursor.col > col)
4119 {
4120 if (stop_arrow() == FAIL)
4121 return;
4122 backspace_until_column(col);
4123 }
4124
4125 // TODO: is this sufficient for redrawing? Redrawing everything causes
4126 // flicker, thus we can't do that.
4127 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004128#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004129 // clear v:completed_item
4130 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004131#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004132}
4133
4134/*
4135 * Insert the new text being completed.
4136 * "in_compl_func" is TRUE when called from complete_check().
4137 */
4138 void
4139ins_compl_insert(int in_compl_func)
4140{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004141 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004142
4143 // Make sure we don't go over the end of the string, this can happen with
4144 // illegal bytes.
4145 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4146 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004147 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004148 compl_used_match = FALSE;
4149 else
4150 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004151#ifdef FEAT_EVAL
4152 {
4153 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4154
4155 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4156 }
4157#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004158 if (!in_compl_func)
4159 compl_curr_match = compl_shown_match;
4160}
4161
4162/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004163 * show the file name for the completion match (if any). Truncate the file
4164 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004165 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004166 static void
4167ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004168{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004169 char *lead = _("match in file");
4170 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4171 char_u *s;
4172 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004173
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004174 if (space <= 0)
4175 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004176
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004177 // We need the tail that fits. With double-byte encoding going
4178 // back from the end is very slow, thus go from the start and keep
4179 // the text that fits in "space" between "s" and "e".
4180 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004181 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004182 space -= ptr2cells(e);
4183 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004184 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004185 space += ptr2cells(s);
4186 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004187 }
4188 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004189 msg_hist_off = TRUE;
4190 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4191 s > compl_shown_match->cp_fname ? "<" : "", s);
4192 msg((char *)IObuff);
4193 msg_hist_off = FALSE;
4194 redraw_cmdline = FALSE; // don't overwrite!
4195}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004196
glepnirdca57fb2024-06-04 22:01:21 +02004197/*
zeertzjq551d8c32024-06-05 19:53:32 +02004198 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004199 */
glepnira218cc62024-06-03 19:32:39 +02004200 static compl_T *
4201find_comp_when_fuzzy(void)
4202{
4203 int score;
4204 char_u* str;
4205 int target_idx = -1;
4206 int is_forward = compl_shows_dir_forward();
4207 int is_backward = compl_shows_dir_backward();
4208 compl_T *comp = NULL;
4209
glepnir43eef882024-06-19 20:20:48 +02004210 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004211 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004212 return compl_first_match != compl_shown_match ? compl_first_match :
4213 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004214
4215 if (is_forward)
4216 target_idx = compl_selected_item + 1;
4217 else if (is_backward)
4218 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004219 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004220
4221 score = compl_match_array[target_idx].pum_score;
4222 str = compl_match_array[target_idx].pum_text;
4223
4224 comp = compl_first_match;
4225 do {
4226 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4227 return comp;
4228 comp = comp->cp_next;
4229 } while (comp != NULL && !is_first_match(comp));
4230
4231 return NULL;
4232}
4233
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004234/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004235 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004236 * times. The number of matches found is returned in 'num_matches'.
4237 *
4238 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4239 * get more completions. If it is FALSE, then do nothing when there are no more
4240 * completions in the given direction.
4241 *
4242 * If "advance" is TRUE, then completion will move to the first match.
4243 * Otherwise, the original text will be shown.
4244 *
4245 * Returns OK on success and -1 if the number of matches are unknown.
4246 */
4247 static int
4248find_next_completion_match(
4249 int allow_get_expansion,
4250 int todo, // repeat completion this many times
4251 int advance,
4252 int *num_matches)
4253{
4254 int found_end = FALSE;
4255 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004256 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004257 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4258 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004259
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004260 while (--todo >= 0)
4261 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004262 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004263 {
glepniraced8c22024-06-15 15:13:05 +02004264 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4265 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004266 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004267 && (is_first_match(compl_shown_match->cp_next)
4268 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004269 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004270 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004271 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004272 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004273 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004274 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4275 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004276 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004277 }
4278 else
4279 {
4280 if (!allow_get_expansion)
4281 {
4282 if (advance)
4283 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004284 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004285 compl_pending -= todo + 1;
4286 else
4287 compl_pending += todo + 1;
4288 }
4289 return -1;
4290 }
4291
4292 if (!compl_no_select && advance)
4293 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004294 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004295 --compl_pending;
4296 else
4297 ++compl_pending;
4298 }
4299
4300 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004301 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004302
4303 // handle any pending completions
4304 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004305 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004306 {
4307 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4308 {
4309 compl_shown_match = compl_shown_match->cp_next;
4310 --compl_pending;
4311 }
4312 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4313 {
4314 compl_shown_match = compl_shown_match->cp_prev;
4315 ++compl_pending;
4316 }
4317 else
4318 break;
4319 }
4320 found_end = FALSE;
4321 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004322 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004323 && compl_leader != NULL
4324 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004325 compl_leader, (int)STRLEN(compl_leader))
4326 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004327 ++todo;
4328 else
4329 // Remember a matching item.
4330 found_compl = compl_shown_match;
4331
4332 // Stop at the end of the list when we found a usable match.
4333 if (found_end)
4334 {
4335 if (found_compl != NULL)
4336 {
4337 compl_shown_match = found_compl;
4338 break;
4339 }
4340 todo = 1; // use first usable match after wrapping around
4341 }
4342 }
4343
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004344 return OK;
4345}
4346
4347/*
4348 * Fill in the next completion in the current direction.
4349 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4350 * get more completions. If it is FALSE, then we just do nothing when there
4351 * are no more completions in a given direction. The latter case is used when
4352 * we are still in the middle of finding completions, to allow browsing
4353 * through the ones found so far.
4354 * Return the total number of matches, or -1 if still unknown -- webb.
4355 *
4356 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4357 * compl_shown_match here.
4358 *
4359 * Note that this function may be called recursively once only. First with
4360 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4361 * calls this function with "allow_get_expansion" FALSE.
4362 */
4363 static int
4364ins_compl_next(
4365 int allow_get_expansion,
4366 int count, // repeat completion this many times; should
4367 // be at least 1
4368 int insert_match, // Insert the newly selected match
4369 int in_compl_func) // called from complete_check()
4370{
4371 int num_matches = -1;
4372 int todo = count;
4373 int advance;
4374 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004375 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004376 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004377 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4378 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004379
4380 // When user complete function return -1 for findstart which is next
4381 // time of 'always', compl_shown_match become NULL.
4382 if (compl_shown_match == NULL)
4383 return -1;
4384
glepnira218cc62024-06-03 19:32:39 +02004385 if (compl_leader != NULL
4386 && !match_at_original_text(compl_shown_match)
4387 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004388 // Update "compl_shown_match" to the actually shown match
4389 ins_compl_update_shown_match();
4390
4391 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004392 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004393 // Delete old text to be replaced
4394 ins_compl_delete();
4395
4396 // When finding the longest common text we stick at the original text,
4397 // don't let CTRL-N or CTRL-P move to the first match.
4398 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4399
4400 // When restarting the search don't insert the first match either.
4401 if (compl_restarting)
4402 {
4403 advance = FALSE;
4404 compl_restarting = FALSE;
4405 }
4406
4407 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4408 // around.
4409 if (find_next_completion_match(allow_get_expansion, todo, advance,
4410 &num_matches) == -1)
4411 return -1;
4412
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004413 if (curbuf != orig_curbuf)
4414 {
4415 // In case some completion function switched buffer, don't want to
4416 // insert the completion elsewhere.
4417 return -1;
4418 }
4419
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004420 // Insert the text of the new completion, or the compl_leader.
4421 if (compl_no_insert && !started)
4422 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004423 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004424 compl_used_match = FALSE;
4425 }
4426 else if (insert_match)
4427 {
4428 if (!compl_get_longest || compl_used_match)
4429 ins_compl_insert(in_compl_func);
4430 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004431 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004432 }
4433 else
4434 compl_used_match = FALSE;
4435
4436 if (!allow_get_expansion)
4437 {
4438 // may undisplay the popup menu first
4439 ins_compl_upd_pum();
4440
4441 if (pum_enough_matches())
4442 // Will display the popup menu, don't redraw yet to avoid flicker.
4443 pum_call_update_screen();
4444 else
4445 // Not showing the popup menu yet, redraw to show the user what was
4446 // inserted.
4447 update_screen(0);
4448
4449 // display the updated popup menu
4450 ins_compl_show_pum();
4451#ifdef FEAT_GUI
4452 if (gui.in_use)
4453 {
4454 // Show the cursor after the match, not after the redrawn text.
4455 setcursor();
4456 out_flush_cursor(FALSE, FALSE);
4457 }
4458#endif
4459
4460 // Delete old text to be replaced, since we're still searching and
4461 // don't want to match ourselves!
4462 ins_compl_delete();
4463 }
4464
4465 // Enter will select a match when the match wasn't inserted and the popup
4466 // menu is visible.
4467 if (compl_no_insert && !started)
4468 compl_enter_selects = TRUE;
4469 else
4470 compl_enter_selects = !insert_match && compl_match_array != NULL;
4471
4472 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004473 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004474 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004475
4476 return num_matches;
4477}
4478
4479/*
4480 * Call this while finding completions, to check whether the user has hit a key
4481 * that should change the currently displayed completion, or exit completion
4482 * mode. Also, when compl_pending is not zero, show a completion as soon as
4483 * possible. -- webb
4484 * "frequency" specifies out of how many calls we actually check.
4485 * "in_compl_func" is TRUE when called from complete_check(), don't set
4486 * compl_curr_match.
4487 */
4488 void
4489ins_compl_check_keys(int frequency, int in_compl_func)
4490{
4491 static int count = 0;
4492 int c;
4493
4494 // Don't check when reading keys from a script, :normal or feedkeys().
4495 // That would break the test scripts. But do check for keys when called
4496 // from complete_check().
4497 if (!in_compl_func && (using_script() || ex_normal_busy))
4498 return;
4499
4500 // Only do this at regular intervals
4501 if (++count < frequency)
4502 return;
4503 count = 0;
4504
4505 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4506 // can't do its work correctly.
4507 c = vpeekc_any();
4508 if (c != NUL)
4509 {
4510 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4511 {
4512 c = safe_vgetc(); // Eat the character
4513 compl_shows_dir = ins_compl_key2dir(c);
4514 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4515 c != K_UP && c != K_DOWN, in_compl_func);
4516 }
4517 else
4518 {
4519 // Need to get the character to have KeyTyped set. We'll put it
4520 // back with vungetc() below. But skip K_IGNORE.
4521 c = safe_vgetc();
4522 if (c != K_IGNORE)
4523 {
4524 // Don't interrupt completion when the character wasn't typed,
4525 // e.g., when doing @q to replay keys.
4526 if (c != Ctrl_R && KeyTyped)
4527 compl_interrupted = TRUE;
4528
4529 vungetc(c);
4530 }
4531 }
4532 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004533 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004534 {
4535 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4536
4537 compl_pending = 0;
4538 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4539 }
4540}
4541
4542/*
4543 * Decide the direction of Insert mode complete from the key typed.
4544 * Returns BACKWARD or FORWARD.
4545 */
4546 static int
4547ins_compl_key2dir(int c)
4548{
4549 if (c == Ctrl_P || c == Ctrl_L
4550 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4551 return BACKWARD;
4552 return FORWARD;
4553}
4554
4555/*
4556 * Return TRUE for keys that are used for completion only when the popup menu
4557 * is visible.
4558 */
4559 static int
4560ins_compl_pum_key(int c)
4561{
4562 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4563 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4564 || c == K_UP || c == K_DOWN);
4565}
4566
4567/*
4568 * Decide the number of completions to move forward.
4569 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4570 */
4571 static int
4572ins_compl_key2count(int c)
4573{
4574 int h;
4575
4576 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4577 {
4578 h = pum_get_height();
4579 if (h > 3)
4580 h -= 2; // keep some context
4581 return h;
4582 }
4583 return 1;
4584}
4585
4586/*
4587 * Return TRUE if completion with "c" should insert the match, FALSE if only
4588 * to change the currently selected completion.
4589 */
4590 static int
4591ins_compl_use_match(int c)
4592{
4593 switch (c)
4594 {
4595 case K_UP:
4596 case K_DOWN:
4597 case K_PAGEDOWN:
4598 case K_KPAGEDOWN:
4599 case K_S_DOWN:
4600 case K_PAGEUP:
4601 case K_KPAGEUP:
4602 case K_S_UP:
4603 return FALSE;
4604 }
4605 return TRUE;
4606}
4607
4608/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004609 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4610 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004611 * Sets the global variables: compl_col, compl_length, compl_pattern and
4612 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004613 * Uses the global variables: compl_cont_status and ctrl_x_mode
4614 */
4615 static int
4616get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4617{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004618 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004619 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004620 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004621 {
4622 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4623 ;
4624 compl_col += ++startcol;
4625 compl_length = curs_col - startcol;
4626 }
4627 if (p_ic)
4628 compl_pattern = str_foldcase(line + compl_col,
4629 compl_length, NULL, 0);
4630 else
4631 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4632 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004633 {
4634 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004635 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004636 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004637 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004638 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004639 {
4640 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004641 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004642
4643 // we need up to 2 extra chars for the prefix
4644 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004645 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004646 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004647 {
4648 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004649 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004650 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004651 if (!vim_iswordp(line + compl_col)
4652 || (compl_col > 0
4653 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004654 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004655 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004656 prefixlen = 0;
4657 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004658 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004659 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004660 line + compl_col, compl_length);
4661 }
4662 else if (--startcol < 0
4663 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4664 {
4665 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004666 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004667 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004668 {
4669 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004670 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004671 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004672 compl_col += curs_col;
4673 compl_length = 0;
4674 }
4675 else
4676 {
4677 // Search the point of change class of multibyte character
4678 // or not a word single byte character backward.
4679 if (has_mbyte)
4680 {
4681 int base_class;
4682 int head_off;
4683
4684 startcol -= (*mb_head_off)(line, line + startcol);
4685 base_class = mb_get_class(line + startcol);
4686 while (--startcol >= 0)
4687 {
4688 head_off = (*mb_head_off)(line, line + startcol);
4689 if (base_class != mb_get_class(line + startcol
4690 - head_off))
4691 break;
4692 startcol -= head_off;
4693 }
4694 }
4695 else
4696 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4697 ;
4698 compl_col += ++startcol;
4699 compl_length = (int)curs_col - startcol;
4700 if (compl_length == 1)
4701 {
4702 // Only match word with at least two chars -- webb
4703 // there's no need to call quote_meta,
4704 // alloc(7) is enough -- Acevedo
4705 compl_pattern = alloc(7);
4706 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004707 {
4708 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004709 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004710 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004711 STRCPY((char *)compl_pattern, "\\<");
4712 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4713 STRCAT((char *)compl_pattern, "\\k");
4714 }
4715 else
4716 {
4717 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4718 compl_length) + 2);
4719 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004720 {
4721 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004722 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004723 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004724 STRCPY((char *)compl_pattern, "\\<");
4725 (void)quote_meta(compl_pattern + 2, line + compl_col,
4726 compl_length);
4727 }
4728 }
4729
John Marriott8c85a2a2024-05-20 19:18:26 +02004730 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004731 return OK;
4732}
4733
4734/*
4735 * Get the pattern, column and length for whole line completion or for the
4736 * complete() function.
4737 * Sets the global variables: compl_col, compl_length and compl_pattern.
4738 */
4739 static int
4740get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4741{
4742 compl_col = (colnr_T)getwhitecols(line);
4743 compl_length = (int)curs_col - (int)compl_col;
4744 if (compl_length < 0) // cursor in indent: empty pattern
4745 compl_length = 0;
4746 if (p_ic)
4747 compl_pattern = str_foldcase(line + compl_col, compl_length,
4748 NULL, 0);
4749 else
4750 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4751 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004752 {
4753 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004754 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004755 }
4756
4757 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004758
4759 return OK;
4760}
4761
4762/*
4763 * Get the pattern, column and length for filename completion.
4764 * Sets the global variables: compl_col, compl_length and compl_pattern.
4765 */
4766 static int
4767get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4768{
4769 // Go back to just before the first filename character.
4770 if (startcol > 0)
4771 {
4772 char_u *p = line + startcol;
4773
4774 MB_PTR_BACK(line, p);
4775 while (p > line && vim_isfilec(PTR2CHAR(p)))
4776 MB_PTR_BACK(line, p);
4777 if (p == line && vim_isfilec(PTR2CHAR(p)))
4778 startcol = 0;
4779 else
4780 startcol = (int)(p - line) + 1;
4781 }
4782
4783 compl_col += startcol;
4784 compl_length = (int)curs_col - startcol;
4785 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4786 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004787 {
4788 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004789 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004790 }
4791
4792 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004793
4794 return OK;
4795}
4796
4797/*
4798 * Get the pattern, column and length for command-line completion.
4799 * Sets the global variables: compl_col, compl_length and compl_pattern.
4800 */
4801 static int
4802get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4803{
4804 compl_pattern = vim_strnsave(line, curs_col);
4805 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004806 {
4807 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004808 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004809 }
4810 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004811 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004812 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004813 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4814 || compl_xp.xp_context == EXPAND_NOTHING)
4815 // No completion possible, use an empty pattern to get a
4816 // "pattern not found" message.
4817 compl_col = curs_col;
4818 else
4819 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4820 compl_length = curs_col - compl_col;
4821
4822 return OK;
4823}
4824
4825/*
4826 * Get the pattern, column and length for user defined completion ('omnifunc',
4827 * 'completefunc' and 'thesaurusfunc')
4828 * Sets the global variables: compl_col, compl_length and compl_pattern.
4829 * Uses the global variable: spell_bad_len
4830 */
4831 static int
4832get_userdefined_compl_info(colnr_T curs_col UNUSED)
4833{
4834 int ret = FAIL;
4835
4836#ifdef FEAT_COMPL_FUNC
4837 // Call user defined function 'completefunc' with "a:findstart"
4838 // set to 1 to obtain the length of text to use for completion.
4839 char_u *line;
4840 typval_T args[3];
4841 int col;
4842 char_u *funcname;
4843 pos_T pos;
4844 int save_State = State;
4845 callback_T *cb;
4846
4847 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4848 // length as a string
4849 funcname = get_complete_funcname(ctrl_x_mode);
4850 if (*funcname == NUL)
4851 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004852 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004853 ? "completefunc" : "omnifunc");
4854 return FAIL;
4855 }
4856
4857 args[0].v_type = VAR_NUMBER;
4858 args[0].vval.v_number = 1;
4859 args[1].v_type = VAR_STRING;
4860 args[1].vval.v_string = (char_u *)"";
4861 args[2].v_type = VAR_UNKNOWN;
4862 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004863 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004864 cb = get_insert_callback(ctrl_x_mode);
4865 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004866 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004867
4868 State = save_State;
4869 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004870 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004871 validate_cursor();
4872 if (!EQUAL_POS(curwin->w_cursor, pos))
4873 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004874 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004875 return FAIL;
4876 }
4877
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004878 // Return value -2 means the user complete function wants to cancel the
4879 // complete without an error, do the same if the function did not execute
4880 // successfully.
4881 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004882 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004883 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004884 if (col == -3)
4885 {
4886 ctrl_x_mode = CTRL_X_NORMAL;
4887 edit_submode = NULL;
4888 if (!shortmess(SHM_COMPLETIONMENU))
4889 msg_clr_cmdline();
4890 return FAIL;
4891 }
4892
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004893 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004894 // completion.
4895 compl_opt_refresh_always = FALSE;
4896 compl_opt_suppress_empty = FALSE;
4897
4898 if (col < 0)
4899 col = curs_col;
4900 compl_col = col;
4901 if (compl_col > curs_col)
4902 compl_col = curs_col;
4903
4904 // Setup variables for completion. Need to obtain "line" again,
4905 // it may have become invalid.
4906 line = ml_get(curwin->w_cursor.lnum);
4907 compl_length = curs_col - compl_col;
4908 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4909 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004910 {
4911 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004912 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004913 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004914
John Marriott8c85a2a2024-05-20 19:18:26 +02004915 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004916 ret = OK;
4917#endif
4918
4919 return ret;
4920}
4921
4922/*
4923 * Get the pattern, column and length for spell completion.
4924 * Sets the global variables: compl_col, compl_length and compl_pattern.
4925 * Uses the global variable: spell_bad_len
4926 */
4927 static int
4928get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4929{
4930 int ret = FAIL;
4931#ifdef FEAT_SPELL
4932 char_u *line;
4933
4934 if (spell_bad_len > 0)
4935 compl_col = curs_col - spell_bad_len;
4936 else
4937 compl_col = spell_word_start(startcol);
4938 if (compl_col >= (colnr_T)startcol)
4939 {
4940 compl_length = 0;
4941 compl_col = curs_col;
4942 }
4943 else
4944 {
4945 spell_expand_check_cap(compl_col);
4946 compl_length = (int)curs_col - compl_col;
4947 }
4948 // Need to obtain "line" again, it may have become invalid.
4949 line = ml_get(curwin->w_cursor.lnum);
4950 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4951 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004952 {
4953 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004954 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004955 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004956
John Marriott8c85a2a2024-05-20 19:18:26 +02004957 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004958 ret = OK;
4959#endif
4960
4961 return ret;
4962}
4963
4964/*
4965 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004966 * "startcol" - start column number of the completion pattern/text
4967 * "cur_col" - current cursor column
4968 * On return, "line_invalid" is set to TRUE, if the current line may have
4969 * become invalid and needs to be fetched again.
4970 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004971 */
4972 static int
4973compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4974{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004975 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004976 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4977 && !thesaurus_func_complete(ctrl_x_mode)))
4978 {
4979 return get_normal_compl_info(line, startcol, curs_col);
4980 }
4981 else if (ctrl_x_mode_line_or_eval())
4982 {
4983 return get_wholeline_compl_info(line, curs_col);
4984 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004985 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004986 {
4987 return get_filename_compl_info(line, startcol, curs_col);
4988 }
4989 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4990 {
4991 return get_cmdline_compl_info(line, curs_col);
4992 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004993 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004994 || thesaurus_func_complete(ctrl_x_mode))
4995 {
4996 if (get_userdefined_compl_info(curs_col) == FAIL)
4997 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004998 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004999 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005000 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005001 {
5002 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5003 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005004 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005005 }
5006 else
5007 {
5008 internal_error("ins_complete()");
5009 return FAIL;
5010 }
5011
5012 return OK;
5013}
5014
5015/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005016 * Continue an interrupted completion mode search in "line".
5017 *
5018 * If this same ctrl_x_mode has been interrupted use the text from
5019 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5020 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5021 * the same line as the cursor then fix it (the line has been split because it
5022 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5023 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005024 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005025 static void
5026ins_compl_continue_search(char_u *line)
5027{
5028 // it is a continued search
5029 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005030 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5031 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005032 {
5033 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5034 {
5035 // line (probably) wrapped, set compl_startpos to the
5036 // first non_blank in the line, if it is not a wordchar
5037 // include it to get a better pattern, but then we don't
5038 // want the "\\<" prefix, check it below
5039 compl_col = (colnr_T)getwhitecols(line);
5040 compl_startpos.col = compl_col;
5041 compl_startpos.lnum = curwin->w_cursor.lnum;
5042 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5043 }
5044 else
5045 {
5046 // S_IPOS was set when we inserted a word that was at the
5047 // beginning of the line, which means that we'll go to SOL
5048 // mode but first we need to redefine compl_startpos
5049 if (compl_cont_status & CONT_S_IPOS)
5050 {
5051 compl_cont_status |= CONT_SOL;
5052 compl_startpos.col = (colnr_T)(skipwhite(
5053 line + compl_length
5054 + compl_startpos.col) - line);
5055 }
5056 compl_col = compl_startpos.col;
5057 }
5058 compl_length = curwin->w_cursor.col - (int)compl_col;
5059 // IObuff is used to add a "word from the next line" would we
5060 // have enough space? just being paranoid
5061#define MIN_SPACE 75
5062 if (compl_length > (IOSIZE - MIN_SPACE))
5063 {
5064 compl_cont_status &= ~CONT_SOL;
5065 compl_length = (IOSIZE - MIN_SPACE);
5066 compl_col = curwin->w_cursor.col - compl_length;
5067 }
5068 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5069 if (compl_length < 1)
5070 compl_cont_status &= CONT_LOCAL;
5071 }
5072 else if (ctrl_x_mode_line_or_eval())
5073 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5074 else
5075 compl_cont_status = 0;
5076}
5077
5078/*
5079 * start insert mode completion
5080 */
5081 static int
5082ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005083{
5084 char_u *line;
5085 int startcol = 0; // column where searched text starts
5086 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005087 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005088 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005089 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005090
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005091 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005092
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005093 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005094 did_si = FALSE;
5095 can_si = FALSE;
5096 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005097 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005098 return FAIL;
5099
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005100 line = ml_get(curwin->w_cursor.lnum);
5101 curs_col = curwin->w_cursor.col;
5102 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005103
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005104 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5105 && compl_cont_mode == ctrl_x_mode)
5106 // this same ctrl-x_mode was interrupted previously. Continue the
5107 // completion.
5108 ins_compl_continue_search(line);
5109 else
5110 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005111
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005112 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005113 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005114 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005115 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005116 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5117 compl_cont_status = 0;
5118 compl_cont_status |= CONT_N_ADDS;
5119 compl_startpos = curwin->w_cursor;
5120 startcol = (int)curs_col;
5121 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005122 }
5123
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005124 // Work out completion pattern and original text -- webb
5125 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5126 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005127 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5128 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005129 // restore did_ai, so that adding comment leader works
5130 did_ai = save_did_ai;
5131 return FAIL;
5132 }
5133 // If "line" was changed while getting completion info get it again.
5134 if (line_invalid)
5135 line = ml_get(curwin->w_cursor.lnum);
5136
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005137 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005138 {
5139 edit_submode_pre = (char_u *)_(" Adding");
5140 if (ctrl_x_mode_line_or_eval())
5141 {
5142 // Insert a new line, keep indentation but ignore 'comments'.
5143 char_u *old = curbuf->b_p_com;
5144
5145 curbuf->b_p_com = (char_u *)"";
5146 compl_startpos.lnum = curwin->w_cursor.lnum;
5147 compl_startpos.col = compl_col;
5148 ins_eol('\r');
5149 curbuf->b_p_com = old;
5150 compl_length = 0;
5151 compl_col = curwin->w_cursor.col;
5152 }
5153 }
5154 else
5155 {
5156 edit_submode_pre = NULL;
5157 compl_startpos.col = compl_col;
5158 }
5159
5160 if (compl_cont_status & CONT_LOCAL)
5161 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5162 else
5163 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5164
5165 // If any of the original typed text has been changed we need to fix
5166 // the redo buffer.
5167 ins_compl_fixRedoBufForLeader(NULL);
5168
5169 // Always add completion for the original text.
5170 vim_free(compl_orig_text);
5171 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5172 if (p_ic)
5173 flags |= CP_ICASE;
5174 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5175 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
5176 {
5177 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005178 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005179 VIM_CLEAR(compl_orig_text);
5180 return FAIL;
5181 }
5182
5183 // showmode might reset the internal line pointers, so it must
5184 // be called before line = ml_get(), or when this address is no
5185 // longer needed. -- Acevedo.
5186 edit_submode_extra = (char_u *)_("-- Searching...");
5187 edit_submode_highl = HLF_COUNT;
5188 showmode();
5189 edit_submode_extra = NULL;
5190 out_flush();
5191
5192 return OK;
5193}
5194
5195/*
5196 * display the completion status message
5197 */
5198 static void
5199ins_compl_show_statusmsg(void)
5200{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005201 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005202 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005203 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005204 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005205 ? (char_u *)_("Hit end of paragraph")
5206 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005207 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005208 }
5209
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005210 if (edit_submode_extra == NULL)
5211 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005212 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005213 {
5214 edit_submode_extra = (char_u *)_("Back at original");
5215 edit_submode_highl = HLF_W;
5216 }
5217 else if (compl_cont_status & CONT_S_IPOS)
5218 {
5219 edit_submode_extra = (char_u *)_("Word from other line");
5220 edit_submode_highl = HLF_COUNT;
5221 }
5222 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5223 {
5224 edit_submode_extra = (char_u *)_("The only match");
5225 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005226 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005227 }
5228 else
5229 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005230#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005231 // Update completion sequence number when needed.
5232 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005233 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005234#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005235 // The match should always have a sequence number now, this is
5236 // just a safety check.
5237 if (compl_curr_match->cp_number != -1)
5238 {
5239 // Space for 10 text chars. + 2x10-digit no.s = 31.
5240 // Translations may need more than twice that.
5241 static char_u match_ref[81];
5242
5243 if (compl_matches > 0)
5244 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005245 _("match %d of %d"),
5246 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 else
5248 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005249 _("match %d"),
5250 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005251 edit_submode_extra = match_ref;
5252 edit_submode_highl = HLF_R;
5253 if (dollar_vcol >= 0)
5254 curs_columns(FALSE);
5255 }
5256 }
5257 }
5258
5259 // Show a message about what (completion) mode we're in.
5260 if (!compl_opt_suppress_empty)
5261 {
5262 showmode();
5263 if (!shortmess(SHM_COMPLETIONMENU))
5264 {
5265 if (edit_submode_extra != NULL)
5266 {
5267 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005268 {
5269 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005270 msg_attr((char *)edit_submode_extra,
5271 edit_submode_highl < HLF_COUNT
5272 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005273 msg_hist_off = FALSE;
5274 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005275 }
5276 else
5277 msg_clr_cmdline(); // necessary for "noshowmode"
5278 }
5279 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005280}
5281
5282/*
5283 * Do Insert mode completion.
5284 * Called when character "c" was typed, which has a meaning for completion.
5285 * Returns OK if completion was done, FAIL if something failed (out of mem).
5286 */
5287 int
5288ins_complete(int c, int enable_pum)
5289{
5290 int n;
5291 int save_w_wrow;
5292 int save_w_leftcol;
5293 int insert_match;
5294
5295 compl_direction = ins_compl_key2dir(c);
5296 insert_match = ins_compl_use_match(c);
5297
5298 if (!compl_started)
5299 {
5300 if (ins_compl_start() == FAIL)
5301 return FAIL;
5302 }
5303 else if (insert_match && stop_arrow() == FAIL)
5304 return FAIL;
5305
5306 compl_shown_match = compl_curr_match;
5307 compl_shows_dir = compl_direction;
5308
5309 // Find next match (and following matches).
5310 save_w_wrow = curwin->w_wrow;
5311 save_w_leftcol = curwin->w_leftcol;
5312 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5313
5314 // may undisplay the popup menu
5315 ins_compl_upd_pum();
5316
5317 if (n > 1) // all matches have been found
5318 compl_matches = n;
5319 compl_curr_match = compl_shown_match;
5320 compl_direction = compl_shows_dir;
5321
5322 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5323 // mode.
5324 if (got_int && !global_busy)
5325 {
5326 (void)vgetc();
5327 got_int = FALSE;
5328 }
5329
5330 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005331 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005332 {
5333 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5334 // because we couldn't expand anything at first place, but if we used
5335 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5336 // (such as M in M'exico) if not tried already. -- Acevedo
5337 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005338 || compl_status_adding()
5339 || (ctrl_x_mode_not_default()
5340 && !ctrl_x_mode_path_patterns()
5341 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005342 compl_cont_status &= ~CONT_N_ADDS;
5343 }
5344
5345 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5346 compl_cont_status |= CONT_S_IPOS;
5347 else
5348 compl_cont_status &= ~CONT_S_IPOS;
5349
5350 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005351
5352 // Show the popup menu, unless we got interrupted.
5353 if (enable_pum && !compl_interrupted)
5354 show_pum(save_w_wrow, save_w_leftcol);
5355
5356 compl_was_interrupted = compl_interrupted;
5357 compl_interrupted = FALSE;
5358
5359 return OK;
5360}
5361
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005362/*
5363 * Remove (if needed) and show the popup menu
5364 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005365 static void
5366show_pum(int prev_w_wrow, int prev_w_leftcol)
5367{
5368 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005369 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005370 RedrawingDisabled = 0;
5371
5372 // If the cursor moved or the display scrolled we need to remove the pum
5373 // first.
5374 setcursor();
5375 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5376 ins_compl_del_pum();
5377
5378 ins_compl_show_pum();
5379 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005380
5381 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005382}
5383
5384/*
5385 * Looks in the first "len" chars. of "src" for search-metachars.
5386 * If dest is not NULL the chars. are copied there quoting (with
5387 * a backslash) the metachars, and dest would be NUL terminated.
5388 * Returns the length (needed) of dest
5389 */
5390 static unsigned
5391quote_meta(char_u *dest, char_u *src, int len)
5392{
5393 unsigned m = (unsigned)len + 1; // one extra for the NUL
5394
5395 for ( ; --len >= 0; src++)
5396 {
5397 switch (*src)
5398 {
5399 case '.':
5400 case '*':
5401 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005402 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005403 break;
5404 // FALLTHROUGH
5405 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005406 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005407 break;
5408 // FALLTHROUGH
5409 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005410 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005411 break;
5412 // FALLTHROUGH
5413 case '^': // currently it's not needed.
5414 case '$':
5415 m++;
5416 if (dest != NULL)
5417 *dest++ = '\\';
5418 break;
5419 }
5420 if (dest != NULL)
5421 *dest++ = *src;
5422 // Copy remaining bytes of a multibyte character.
5423 if (has_mbyte)
5424 {
5425 int i, mb_len;
5426
5427 mb_len = (*mb_ptr2len)(src) - 1;
5428 if (mb_len > 0 && len >= mb_len)
5429 for (i = 0; i < mb_len; ++i)
5430 {
5431 --len;
5432 ++src;
5433 if (dest != NULL)
5434 *dest++ = *src;
5435 }
5436 }
5437 }
5438 if (dest != NULL)
5439 *dest = NUL;
5440
5441 return m;
5442}
5443
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005444#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005445 void
5446free_insexpand_stuff(void)
5447{
5448 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005449# ifdef FEAT_EVAL
5450 free_callback(&cfu_cb);
5451 free_callback(&ofu_cb);
5452 free_callback(&tsrfu_cb);
5453# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005454}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005455#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005456
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005457#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005458/*
5459 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5460 * spelled word, if there is one.
5461 */
5462 static void
5463spell_back_to_badword(void)
5464{
5465 pos_T tpos = curwin->w_cursor;
5466
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005467 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005468 if (curwin->w_cursor.col != tpos.col)
5469 start_arrow(&tpos);
5470}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005471#endif