blob: 2be63a58ef9cbcf9f6b9b802f2175ea709eb321d [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
Christian Brabandt220474d2024-07-20 13:26:44 +02003621 // prevent qsort from deref NULL pointer
3622 if (fuzzy_indices.ga_len > 0)
3623 {
3624 fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3625 qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
glepnir8159fb12024-07-17 20:32:54 +02003626
Christian Brabandt220474d2024-07-20 13:26:44 +02003627 sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3628 for (i = 0; i < fuzzy_indices.ga_len; ++i)
3629 sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
glepnir8159fb12024-07-17 20:32:54 +02003630
Christian Brabandt220474d2024-07-20 13:26:44 +02003631 FreeWild(num_matches, matches);
3632 matches = sorted_matches;
3633 num_matches = fuzzy_indices.ga_len;
3634 }
3635
glepnir8159fb12024-07-17 20:32:54 +02003636 vim_free(compl_fuzzy_scores);
3637 ga_clear(&fuzzy_indices);
3638 }
3639
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003640 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3641}
3642
3643/*
3644 * Get the next set of command-line completions matching "compl_pattern".
3645 */
3646 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003647get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003648{
3649 char_u **matches;
3650 int num_matches;
3651
3652 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003653 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 ins_compl_add_matches(num_matches, matches, FALSE);
3655}
3656
3657/*
3658 * Get the next set of spell suggestions matching "compl_pattern".
3659 */
3660 static void
3661get_next_spell_completion(linenr_T lnum UNUSED)
3662{
3663#ifdef FEAT_SPELL
3664 char_u **matches;
3665 int num_matches;
3666
3667 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3668 if (num_matches > 0)
3669 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003670 else
3671 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672#endif
3673}
3674
3675/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003676 * Return the next word or line from buffer "ins_buf" at position
3677 * "cur_match_pos" for completion. The length of the match is set in "len".
3678 */
3679 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003680ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003681 buf_T *ins_buf, // buffer being scanned
3682 pos_T *cur_match_pos, // current match position
3683 int *match_len,
3684 int *cont_s_ipos) // next ^X<> will set initial_pos
3685{
3686 char_u *ptr;
3687 int len;
3688
3689 *match_len = 0;
3690 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3691 cur_match_pos->col;
3692 if (ctrl_x_mode_line_or_eval())
3693 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003694 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003695 {
3696 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3697 return NULL;
3698 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3699 if (!p_paste)
3700 ptr = skipwhite(ptr);
3701 }
3702 len = (int)STRLEN(ptr);
3703 }
3704 else
3705 {
3706 char_u *tmp_ptr = ptr;
3707
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003708 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003709 {
3710 tmp_ptr += compl_length;
3711 // Skip if already inside a word.
3712 if (vim_iswordp(tmp_ptr))
3713 return NULL;
3714 // Find start of next word.
3715 tmp_ptr = find_word_start(tmp_ptr);
3716 }
3717 // Find end of this word.
3718 tmp_ptr = find_word_end(tmp_ptr);
3719 len = (int)(tmp_ptr - ptr);
3720
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003721 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003722 {
3723 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3724 {
3725 // Try next line, if any. the new word will be
3726 // "join" as if the normal command "J" was used.
3727 // IOSIZE is always greater than
3728 // compl_length, so the next STRNCPY always
3729 // works -- Acevedo
3730 STRNCPY(IObuff, ptr, len);
3731 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3732 tmp_ptr = ptr = skipwhite(ptr);
3733 // Find start of next word.
3734 tmp_ptr = find_word_start(tmp_ptr);
3735 // Find end of next word.
3736 tmp_ptr = find_word_end(tmp_ptr);
3737 if (tmp_ptr > ptr)
3738 {
3739 if (*ptr != ')' && IObuff[len - 1] != TAB)
3740 {
3741 if (IObuff[len - 1] != ' ')
3742 IObuff[len++] = ' ';
3743 // IObuf =~ "\k.* ", thus len >= 2
3744 if (p_js
3745 && (IObuff[len - 2] == '.'
3746 || (vim_strchr(p_cpo, CPO_JOINSP)
3747 == NULL
3748 && (IObuff[len - 2] == '?'
3749 || IObuff[len - 2] == '!'))))
3750 IObuff[len++] = ' ';
3751 }
3752 // copy as much as possible of the new word
3753 if (tmp_ptr - ptr >= IOSIZE - len)
3754 tmp_ptr = ptr + IOSIZE - len - 1;
3755 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3756 len += (int)(tmp_ptr - ptr);
3757 *cont_s_ipos = TRUE;
3758 }
3759 IObuff[len] = NUL;
3760 ptr = IObuff;
3761 }
3762 if (len == compl_length)
3763 return NULL;
3764 }
3765 }
3766
3767 *match_len = len;
3768 return ptr;
3769}
3770
3771/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003772 * Get the next set of words matching "compl_pattern" for default completion(s)
3773 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003774 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3775 * position "st->start_pos" in the "compl_direction" direction. If
3776 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3777 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003778 * Returns OK if a new next match is found, otherwise returns FAIL.
3779 */
3780 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003781get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003782{
3783 int found_new_match = FAIL;
3784 int save_p_scs;
3785 int save_p_ws;
3786 int looped_around = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003787 char_u *ptr = NULL;
3788 int len = 0;
3789 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0;
3790 char_u *leader = ins_compl_leader();
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003791
3792 // If 'infercase' is set, don't use 'smartcase' here
3793 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003794 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003795 p_scs = FALSE;
3796
3797 // Buffers other than curbuf are scanned from the beginning or the
3798 // end but never from the middle, thus setting nowrapscan in this
3799 // buffer is a good idea, on the other hand, we always set
3800 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3801 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003802 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003803 p_ws = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02003804 else if (*st->e_cpt == '.' && !in_fuzzy)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003805 p_ws = TRUE;
3806 looped_around = FALSE;
3807 for (;;)
3808 {
3809 int cont_s_ipos = FALSE;
3810
3811 ++msg_silent; // Don't want messages for wrapscan.
3812
3813 // ctrl_x_mode_line_or_eval() || word-wise search that
3814 // has added a word that was at the beginning of the line
glepnir8159fb12024-07-17 20:32:54 +02003815 if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003816 found_new_match = search_for_exact_line(st->ins_buf,
3817 st->cur_match_pos, compl_direction, compl_pattern);
glepnir8159fb12024-07-17 20:32:54 +02003818 else if (in_fuzzy)
3819 found_new_match = search_for_fuzzy_match(st->ins_buf,
3820 st->cur_match_pos, leader, compl_direction,
3821 start_pos, &len, &ptr, ctrl_x_mode_whole_line());
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003822 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003823 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003824 NULL, compl_direction, compl_pattern, compl_patternlen,
3825 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003826 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003827 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003828 {
3829 // set "compl_started" even on fail
3830 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003831 st->first_match_pos = *st->cur_match_pos;
3832 st->last_match_pos = *st->cur_match_pos;
3833 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003834 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003835 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3836 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003837 {
3838 found_new_match = FAIL;
3839 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003840 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003841 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3842 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3843 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003844 {
3845 if (looped_around)
3846 found_new_match = FAIL;
3847 else
3848 looped_around = TRUE;
3849 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003850 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003851 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3852 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3853 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003854 {
3855 if (looped_around)
3856 found_new_match = FAIL;
3857 else
3858 looped_around = TRUE;
3859 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003860 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003861 if (found_new_match == FAIL)
3862 break;
3863
3864 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003865 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003866 && start_pos->lnum == st->cur_match_pos->lnum
3867 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003868 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003869
glepnir8159fb12024-07-17 20:32:54 +02003870 if (!in_fuzzy)
3871 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3872 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003873 if (ptr == NULL)
3874 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003875
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003876 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003877 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003878 0, cont_s_ipos) != NOTDONE)
3879 {
3880 found_new_match = OK;
3881 break;
3882 }
3883 }
3884 p_scs = save_p_scs;
3885 p_ws = save_p_ws;
3886
3887 return found_new_match;
3888}
3889
3890/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003891 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003892 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003893 */
3894 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003895get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003896{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003897 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003898
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003899 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003901 case -1:
3902 break;
3903#ifdef FEAT_FIND_ID
3904 case CTRL_X_PATH_PATTERNS:
3905 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003906 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003907 break;
3908#endif
3909
3910 case CTRL_X_DICTIONARY:
3911 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003912 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3913 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003914 break;
3915
3916 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003917 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003918 break;
3919
3920 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003921 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003922 break;
3923
3924 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003925 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003926 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003927 break;
3928
3929#ifdef FEAT_COMPL_FUNC
3930 case CTRL_X_FUNCTION:
3931 case CTRL_X_OMNI:
3932 expand_by_function(type, compl_pattern);
3933 break;
3934#endif
3935
3936 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003937 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003938 break;
3939
3940 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003941 found_new_match = get_next_default_completion(st, ini);
3942 if (found_new_match == FAIL && st->ins_buf == curbuf)
3943 st->found_all = TRUE;
3944 }
3945
3946 // check if compl_curr_match has changed, (e.g. other type of
3947 // expansion added something)
3948 if (type != 0 && compl_curr_match != compl_old_match)
3949 found_new_match = OK;
3950
3951 return found_new_match;
3952}
3953
3954/*
3955 * Get the next expansion(s), using "compl_pattern".
3956 * The search starts at position "ini" in curbuf and in the direction
3957 * compl_direction.
3958 * When "compl_started" is FALSE start at that position, otherwise continue
3959 * where we stopped searching before.
3960 * This may return before finding all the matches.
3961 * Return the total number of matches or -1 if still unknown -- Acevedo
3962 */
3963 static int
3964ins_compl_get_exp(pos_T *ini)
3965{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003966 static ins_compl_next_state_T st;
3967 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003968 int i;
3969 int found_new_match;
3970 int type = ctrl_x_mode;
glepnir8159fb12024-07-17 20:32:54 +02003971 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003972
3973 if (!compl_started)
3974 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003975 buf_T *buf;
3976
3977 FOR_ALL_BUFFERS(buf)
3978 buf->b_scanned = 0;
3979 if (!st_cleared)
3980 {
3981 CLEAR_FIELD(st);
3982 st_cleared = TRUE;
3983 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003984 st.found_all = FALSE;
3985 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003986 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003987 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003988 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3989 ? (char_u *)"." : curbuf->b_p_cpt);
3990 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003991 st.last_match_pos = st.first_match_pos = *ini;
3992 }
3993 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3994 st.ins_buf = curbuf; // In case the buffer was wiped out.
3995
3996 compl_old_match = compl_curr_match; // remember the last current match
glepnir8159fb12024-07-17 20:32:54 +02003997 if (in_fuzzy)
3998 st.cur_match_pos = (compl_dir_forward())
3999 ? &st.last_match_pos : &st.first_match_pos;
4000 else
4001 st.cur_match_pos = &st.last_match_pos;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004002
4003 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4004 for (;;)
4005 {
4006 found_new_match = FAIL;
4007 st.set_match_pos = FALSE;
4008
4009 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
4010 // or if found_all says this entry is done. For ^X^L only use the
4011 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004012 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004013 && (!compl_started || st.found_all))
4014 {
glepnir8159fb12024-07-17 20:32:54 +02004015 int status = process_next_cpt_value(&st, &type, ini, in_fuzzy);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004016
4017 if (status == INS_COMPL_CPT_END)
4018 break;
4019 if (status == INS_COMPL_CPT_CONT)
4020 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004021 }
4022
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004023 // If complete() was called then compl_pattern has been reset. The
4024 // following won't work then, bail out.
4025 if (compl_pattern == NULL)
4026 break;
4027
4028 // get the next set of completion matches
4029 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004030
4031 // break the loop for specialized modes (use 'complete' just for the
4032 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4033 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004034 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
4035 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004036 {
4037 if (got_int)
4038 break;
4039 // Fill the popup menu as soon as possible.
4040 if (type != -1)
4041 ins_compl_check_keys(0, FALSE);
4042
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004043 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004044 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
4045 break;
4046 compl_started = TRUE;
4047 }
4048 else
4049 {
4050 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02004051 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004052 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053
4054 compl_started = FALSE;
4055 }
4056 }
4057 compl_started = TRUE;
4058
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004059 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00004060 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004061 found_new_match = FAIL;
4062
4063 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004064 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004065 && !ctrl_x_mode_line_or_eval()))
4066 i = ins_compl_make_cyclic();
4067
4068 if (compl_old_match != NULL)
4069 {
4070 // If several matches were added (FORWARD) or the search failed and has
4071 // just been made cyclic then we have to move compl_curr_match to the
4072 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004073 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 : compl_old_match->cp_prev;
4075 if (compl_curr_match == NULL)
4076 compl_curr_match = compl_old_match;
4077 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01004078 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01004079
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004080 return i;
4081}
4082
4083/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004084 * Update "compl_shown_match" to the actually shown match, it may differ when
4085 * "compl_leader" is used to omit some of the matches.
4086 */
4087 static void
4088ins_compl_update_shown_match(void)
4089{
4090 while (!ins_compl_equal(compl_shown_match,
4091 compl_leader, (int)STRLEN(compl_leader))
4092 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004093 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004094 compl_shown_match = compl_shown_match->cp_next;
4095
4096 // If we didn't find it searching forward, and compl_shows_dir is
4097 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004098 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004099 && !ins_compl_equal(compl_shown_match,
4100 compl_leader, (int)STRLEN(compl_leader))
4101 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004102 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004103 {
4104 while (!ins_compl_equal(compl_shown_match,
4105 compl_leader, (int)STRLEN(compl_leader))
4106 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004107 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004108 compl_shown_match = compl_shown_match->cp_prev;
4109 }
4110}
4111
4112/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004113 * Delete the old text being completed.
4114 */
4115 void
4116ins_compl_delete(void)
4117{
4118 int col;
4119
4120 // In insert mode: Delete the typed part.
4121 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004122 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004123 if ((int)curwin->w_cursor.col > col)
4124 {
4125 if (stop_arrow() == FAIL)
4126 return;
4127 backspace_until_column(col);
4128 }
4129
4130 // TODO: is this sufficient for redrawing? Redrawing everything causes
4131 // flicker, thus we can't do that.
4132 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004133#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004134 // clear v:completed_item
4135 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004136#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004137}
4138
4139/*
4140 * Insert the new text being completed.
4141 * "in_compl_func" is TRUE when called from complete_check().
4142 */
4143 void
4144ins_compl_insert(int in_compl_func)
4145{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004146 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004147
4148 // Make sure we don't go over the end of the string, this can happen with
4149 // illegal bytes.
4150 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4151 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004152 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004153 compl_used_match = FALSE;
4154 else
4155 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004156#ifdef FEAT_EVAL
4157 {
4158 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4159
4160 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4161 }
4162#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004163 if (!in_compl_func)
4164 compl_curr_match = compl_shown_match;
4165}
4166
4167/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004168 * show the file name for the completion match (if any). Truncate the file
4169 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004170 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004171 static void
4172ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004173{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004174 char *lead = _("match in file");
4175 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4176 char_u *s;
4177 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004178
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004179 if (space <= 0)
4180 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004181
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004182 // We need the tail that fits. With double-byte encoding going
4183 // back from the end is very slow, thus go from the start and keep
4184 // the text that fits in "space" between "s" and "e".
4185 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004186 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004187 space -= ptr2cells(e);
4188 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004189 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004190 space += ptr2cells(s);
4191 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004192 }
4193 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004194 msg_hist_off = TRUE;
4195 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4196 s > compl_shown_match->cp_fname ? "<" : "", s);
4197 msg((char *)IObuff);
4198 msg_hist_off = FALSE;
4199 redraw_cmdline = FALSE; // don't overwrite!
4200}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004201
glepnirdca57fb2024-06-04 22:01:21 +02004202/*
zeertzjq551d8c32024-06-05 19:53:32 +02004203 * Find a completion item when 'completeopt' contains "fuzzy".
glepnirdca57fb2024-06-04 22:01:21 +02004204 */
glepnira218cc62024-06-03 19:32:39 +02004205 static compl_T *
4206find_comp_when_fuzzy(void)
4207{
4208 int score;
4209 char_u* str;
4210 int target_idx = -1;
4211 int is_forward = compl_shows_dir_forward();
4212 int is_backward = compl_shows_dir_backward();
4213 compl_T *comp = NULL;
4214
glepnir43eef882024-06-19 20:20:48 +02004215 if ((is_forward && compl_selected_item == compl_match_arraysize - 1)
glepnira218cc62024-06-03 19:32:39 +02004216 || (is_backward && compl_selected_item == 0))
glepnir65407ce2024-07-06 16:09:19 +02004217 return compl_first_match != compl_shown_match ? compl_first_match :
4218 (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL);
glepnira218cc62024-06-03 19:32:39 +02004219
4220 if (is_forward)
4221 target_idx = compl_selected_item + 1;
4222 else if (is_backward)
4223 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004224 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004225
4226 score = compl_match_array[target_idx].pum_score;
4227 str = compl_match_array[target_idx].pum_text;
4228
4229 comp = compl_first_match;
4230 do {
4231 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4232 return comp;
4233 comp = comp->cp_next;
4234 } while (comp != NULL && !is_first_match(comp));
4235
4236 return NULL;
4237}
4238
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004239/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004240 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004241 * times. The number of matches found is returned in 'num_matches'.
4242 *
4243 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4244 * get more completions. If it is FALSE, then do nothing when there are no more
4245 * completions in the given direction.
4246 *
4247 * If "advance" is TRUE, then completion will move to the first match.
4248 * Otherwise, the original text will be shown.
4249 *
4250 * Returns OK on success and -1 if the number of matches are unknown.
4251 */
4252 static int
4253find_next_completion_match(
4254 int allow_get_expansion,
4255 int todo, // repeat completion this many times
4256 int advance,
4257 int *num_matches)
4258{
4259 int found_end = FALSE;
4260 compl_T *found_compl = NULL;
zeertzjqaa925ee2024-06-09 18:24:05 +02004261 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004262 int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
4263 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004264
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004265 while (--todo >= 0)
4266 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004267 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004268 {
glepniraced8c22024-06-15 15:13:05 +02004269 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4270 ? find_comp_when_fuzzy() : compl_shown_match->cp_next;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004271 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004272 && (is_first_match(compl_shown_match->cp_next)
4273 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004274 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004275 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004276 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004277 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004278 found_end = is_first_match(compl_shown_match);
glepniraced8c22024-06-15 15:13:05 +02004279 compl_shown_match = compl_fuzzy_match && compl_match_array != NULL
4280 ? find_comp_when_fuzzy() : compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004281 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004282 }
4283 else
4284 {
4285 if (!allow_get_expansion)
4286 {
4287 if (advance)
4288 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004289 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004290 compl_pending -= todo + 1;
4291 else
4292 compl_pending += todo + 1;
4293 }
4294 return -1;
4295 }
4296
4297 if (!compl_no_select && advance)
4298 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004299 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004300 --compl_pending;
4301 else
4302 ++compl_pending;
4303 }
4304
4305 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004306 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004307
4308 // handle any pending completions
4309 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004310 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004311 {
4312 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4313 {
4314 compl_shown_match = compl_shown_match->cp_next;
4315 --compl_pending;
4316 }
4317 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4318 {
4319 compl_shown_match = compl_shown_match->cp_prev;
4320 ++compl_pending;
4321 }
4322 else
4323 break;
4324 }
4325 found_end = FALSE;
4326 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004327 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004328 && compl_leader != NULL
4329 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004330 compl_leader, (int)STRLEN(compl_leader))
4331 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004332 ++todo;
4333 else
4334 // Remember a matching item.
4335 found_compl = compl_shown_match;
4336
4337 // Stop at the end of the list when we found a usable match.
4338 if (found_end)
4339 {
4340 if (found_compl != NULL)
4341 {
4342 compl_shown_match = found_compl;
4343 break;
4344 }
4345 todo = 1; // use first usable match after wrapping around
4346 }
4347 }
4348
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004349 return OK;
4350}
4351
4352/*
4353 * Fill in the next completion in the current direction.
4354 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4355 * get more completions. If it is FALSE, then we just do nothing when there
4356 * are no more completions in a given direction. The latter case is used when
4357 * we are still in the middle of finding completions, to allow browsing
4358 * through the ones found so far.
4359 * Return the total number of matches, or -1 if still unknown -- webb.
4360 *
4361 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4362 * compl_shown_match here.
4363 *
4364 * Note that this function may be called recursively once only. First with
4365 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4366 * calls this function with "allow_get_expansion" FALSE.
4367 */
4368 static int
4369ins_compl_next(
4370 int allow_get_expansion,
4371 int count, // repeat completion this many times; should
4372 // be at least 1
4373 int insert_match, // Insert the newly selected match
4374 int in_compl_func) // called from complete_check()
4375{
4376 int num_matches = -1;
4377 int todo = count;
4378 int advance;
4379 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004380 buf_T *orig_curbuf = curbuf;
zeertzjqaa925ee2024-06-09 18:24:05 +02004381 unsigned int cur_cot_flags = get_cot_flags();
zeertzjq529b9ad2024-06-05 20:27:06 +02004382 int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
4383 int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004384
4385 // When user complete function return -1 for findstart which is next
4386 // time of 'always', compl_shown_match become NULL.
4387 if (compl_shown_match == NULL)
4388 return -1;
4389
glepnira218cc62024-06-03 19:32:39 +02004390 if (compl_leader != NULL
4391 && !match_at_original_text(compl_shown_match)
4392 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004393 // Update "compl_shown_match" to the actually shown match
4394 ins_compl_update_shown_match();
4395
4396 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004397 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004398 // Delete old text to be replaced
4399 ins_compl_delete();
4400
4401 // When finding the longest common text we stick at the original text,
4402 // don't let CTRL-N or CTRL-P move to the first match.
4403 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4404
4405 // When restarting the search don't insert the first match either.
4406 if (compl_restarting)
4407 {
4408 advance = FALSE;
4409 compl_restarting = FALSE;
4410 }
4411
4412 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4413 // around.
4414 if (find_next_completion_match(allow_get_expansion, todo, advance,
4415 &num_matches) == -1)
4416 return -1;
4417
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004418 if (curbuf != orig_curbuf)
4419 {
4420 // In case some completion function switched buffer, don't want to
4421 // insert the completion elsewhere.
4422 return -1;
4423 }
4424
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004425 // Insert the text of the new completion, or the compl_leader.
4426 if (compl_no_insert && !started)
4427 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004428 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004429 compl_used_match = FALSE;
4430 }
4431 else if (insert_match)
4432 {
4433 if (!compl_get_longest || compl_used_match)
4434 ins_compl_insert(in_compl_func);
4435 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004436 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004437 }
4438 else
4439 compl_used_match = FALSE;
4440
4441 if (!allow_get_expansion)
4442 {
4443 // may undisplay the popup menu first
4444 ins_compl_upd_pum();
4445
4446 if (pum_enough_matches())
4447 // Will display the popup menu, don't redraw yet to avoid flicker.
4448 pum_call_update_screen();
4449 else
4450 // Not showing the popup menu yet, redraw to show the user what was
4451 // inserted.
4452 update_screen(0);
4453
4454 // display the updated popup menu
4455 ins_compl_show_pum();
4456#ifdef FEAT_GUI
4457 if (gui.in_use)
4458 {
4459 // Show the cursor after the match, not after the redrawn text.
4460 setcursor();
4461 out_flush_cursor(FALSE, FALSE);
4462 }
4463#endif
4464
4465 // Delete old text to be replaced, since we're still searching and
4466 // don't want to match ourselves!
4467 ins_compl_delete();
4468 }
4469
4470 // Enter will select a match when the match wasn't inserted and the popup
4471 // menu is visible.
4472 if (compl_no_insert && !started)
4473 compl_enter_selects = TRUE;
4474 else
4475 compl_enter_selects = !insert_match && compl_match_array != NULL;
4476
4477 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004478 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004479 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004480
4481 return num_matches;
4482}
4483
4484/*
4485 * Call this while finding completions, to check whether the user has hit a key
4486 * that should change the currently displayed completion, or exit completion
4487 * mode. Also, when compl_pending is not zero, show a completion as soon as
4488 * possible. -- webb
4489 * "frequency" specifies out of how many calls we actually check.
4490 * "in_compl_func" is TRUE when called from complete_check(), don't set
4491 * compl_curr_match.
4492 */
4493 void
4494ins_compl_check_keys(int frequency, int in_compl_func)
4495{
4496 static int count = 0;
4497 int c;
4498
4499 // Don't check when reading keys from a script, :normal or feedkeys().
4500 // That would break the test scripts. But do check for keys when called
4501 // from complete_check().
4502 if (!in_compl_func && (using_script() || ex_normal_busy))
4503 return;
4504
4505 // Only do this at regular intervals
4506 if (++count < frequency)
4507 return;
4508 count = 0;
4509
4510 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4511 // can't do its work correctly.
4512 c = vpeekc_any();
4513 if (c != NUL)
4514 {
4515 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4516 {
4517 c = safe_vgetc(); // Eat the character
4518 compl_shows_dir = ins_compl_key2dir(c);
4519 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4520 c != K_UP && c != K_DOWN, in_compl_func);
4521 }
4522 else
4523 {
4524 // Need to get the character to have KeyTyped set. We'll put it
4525 // back with vungetc() below. But skip K_IGNORE.
4526 c = safe_vgetc();
4527 if (c != K_IGNORE)
4528 {
4529 // Don't interrupt completion when the character wasn't typed,
4530 // e.g., when doing @q to replay keys.
4531 if (c != Ctrl_R && KeyTyped)
4532 compl_interrupted = TRUE;
4533
4534 vungetc(c);
4535 }
4536 }
4537 }
zeertzjq529b9ad2024-06-05 20:27:06 +02004538 if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004539 {
4540 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4541
4542 compl_pending = 0;
4543 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4544 }
4545}
4546
4547/*
4548 * Decide the direction of Insert mode complete from the key typed.
4549 * Returns BACKWARD or FORWARD.
4550 */
4551 static int
4552ins_compl_key2dir(int c)
4553{
4554 if (c == Ctrl_P || c == Ctrl_L
4555 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4556 return BACKWARD;
4557 return FORWARD;
4558}
4559
4560/*
4561 * Return TRUE for keys that are used for completion only when the popup menu
4562 * is visible.
4563 */
4564 static int
4565ins_compl_pum_key(int c)
4566{
4567 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4568 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4569 || c == K_UP || c == K_DOWN);
4570}
4571
4572/*
4573 * Decide the number of completions to move forward.
4574 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4575 */
4576 static int
4577ins_compl_key2count(int c)
4578{
4579 int h;
4580
4581 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4582 {
4583 h = pum_get_height();
4584 if (h > 3)
4585 h -= 2; // keep some context
4586 return h;
4587 }
4588 return 1;
4589}
4590
4591/*
4592 * Return TRUE if completion with "c" should insert the match, FALSE if only
4593 * to change the currently selected completion.
4594 */
4595 static int
4596ins_compl_use_match(int c)
4597{
4598 switch (c)
4599 {
4600 case K_UP:
4601 case K_DOWN:
4602 case K_PAGEDOWN:
4603 case K_KPAGEDOWN:
4604 case K_S_DOWN:
4605 case K_PAGEUP:
4606 case K_KPAGEUP:
4607 case K_S_UP:
4608 return FALSE;
4609 }
4610 return TRUE;
4611}
4612
4613/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004614 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4615 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004616 * Sets the global variables: compl_col, compl_length, compl_pattern and
4617 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004618 * Uses the global variables: compl_cont_status and ctrl_x_mode
4619 */
4620 static int
4621get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4622{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004623 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004624 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004625 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004626 {
4627 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4628 ;
4629 compl_col += ++startcol;
4630 compl_length = curs_col - startcol;
4631 }
4632 if (p_ic)
4633 compl_pattern = str_foldcase(line + compl_col,
4634 compl_length, NULL, 0);
4635 else
4636 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4637 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004638 {
4639 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004640 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004641 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004642 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004643 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004644 {
4645 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004646 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004647
4648 // we need up to 2 extra chars for the prefix
4649 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004650 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004651 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004652 {
4653 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004654 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004655 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004656 if (!vim_iswordp(line + compl_col)
4657 || (compl_col > 0
4658 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004659 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004660 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004661 prefixlen = 0;
4662 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004663 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004664 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004665 line + compl_col, compl_length);
4666 }
4667 else if (--startcol < 0
4668 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4669 {
4670 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004671 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004672 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004673 {
4674 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004675 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004676 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004677 compl_col += curs_col;
4678 compl_length = 0;
4679 }
4680 else
4681 {
4682 // Search the point of change class of multibyte character
4683 // or not a word single byte character backward.
4684 if (has_mbyte)
4685 {
4686 int base_class;
4687 int head_off;
4688
4689 startcol -= (*mb_head_off)(line, line + startcol);
4690 base_class = mb_get_class(line + startcol);
4691 while (--startcol >= 0)
4692 {
4693 head_off = (*mb_head_off)(line, line + startcol);
4694 if (base_class != mb_get_class(line + startcol
4695 - head_off))
4696 break;
4697 startcol -= head_off;
4698 }
4699 }
4700 else
4701 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4702 ;
4703 compl_col += ++startcol;
4704 compl_length = (int)curs_col - startcol;
4705 if (compl_length == 1)
4706 {
4707 // Only match word with at least two chars -- webb
4708 // there's no need to call quote_meta,
4709 // alloc(7) is enough -- Acevedo
4710 compl_pattern = alloc(7);
4711 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004712 {
4713 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004714 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004715 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004716 STRCPY((char *)compl_pattern, "\\<");
4717 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4718 STRCAT((char *)compl_pattern, "\\k");
4719 }
4720 else
4721 {
4722 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4723 compl_length) + 2);
4724 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004725 {
4726 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004727 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004728 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004729 STRCPY((char *)compl_pattern, "\\<");
4730 (void)quote_meta(compl_pattern + 2, line + compl_col,
4731 compl_length);
4732 }
4733 }
4734
John Marriott8c85a2a2024-05-20 19:18:26 +02004735 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004736 return OK;
4737}
4738
4739/*
4740 * Get the pattern, column and length for whole line completion or for the
4741 * complete() function.
4742 * Sets the global variables: compl_col, compl_length and compl_pattern.
4743 */
4744 static int
4745get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4746{
4747 compl_col = (colnr_T)getwhitecols(line);
4748 compl_length = (int)curs_col - (int)compl_col;
4749 if (compl_length < 0) // cursor in indent: empty pattern
4750 compl_length = 0;
4751 if (p_ic)
4752 compl_pattern = str_foldcase(line + compl_col, compl_length,
4753 NULL, 0);
4754 else
4755 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4756 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004757 {
4758 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004759 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004760 }
4761
4762 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004763
4764 return OK;
4765}
4766
4767/*
4768 * Get the pattern, column and length for filename completion.
4769 * Sets the global variables: compl_col, compl_length and compl_pattern.
4770 */
4771 static int
4772get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4773{
4774 // Go back to just before the first filename character.
4775 if (startcol > 0)
4776 {
4777 char_u *p = line + startcol;
4778
4779 MB_PTR_BACK(line, p);
4780 while (p > line && vim_isfilec(PTR2CHAR(p)))
4781 MB_PTR_BACK(line, p);
4782 if (p == line && vim_isfilec(PTR2CHAR(p)))
4783 startcol = 0;
4784 else
4785 startcol = (int)(p - line) + 1;
4786 }
4787
4788 compl_col += startcol;
4789 compl_length = (int)curs_col - startcol;
4790 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4791 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004792 {
4793 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004794 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004795 }
4796
4797 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004798
4799 return OK;
4800}
4801
4802/*
4803 * Get the pattern, column and length for command-line completion.
4804 * Sets the global variables: compl_col, compl_length and compl_pattern.
4805 */
4806 static int
4807get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4808{
4809 compl_pattern = vim_strnsave(line, curs_col);
4810 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004811 {
4812 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004813 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004814 }
4815 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004816 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004817 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004818 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4819 || compl_xp.xp_context == EXPAND_NOTHING)
4820 // No completion possible, use an empty pattern to get a
4821 // "pattern not found" message.
4822 compl_col = curs_col;
4823 else
4824 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4825 compl_length = curs_col - compl_col;
4826
4827 return OK;
4828}
4829
4830/*
4831 * Get the pattern, column and length for user defined completion ('omnifunc',
4832 * 'completefunc' and 'thesaurusfunc')
4833 * Sets the global variables: compl_col, compl_length and compl_pattern.
4834 * Uses the global variable: spell_bad_len
4835 */
4836 static int
4837get_userdefined_compl_info(colnr_T curs_col UNUSED)
4838{
4839 int ret = FAIL;
4840
4841#ifdef FEAT_COMPL_FUNC
4842 // Call user defined function 'completefunc' with "a:findstart"
4843 // set to 1 to obtain the length of text to use for completion.
4844 char_u *line;
4845 typval_T args[3];
4846 int col;
4847 char_u *funcname;
4848 pos_T pos;
4849 int save_State = State;
4850 callback_T *cb;
4851
4852 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4853 // length as a string
4854 funcname = get_complete_funcname(ctrl_x_mode);
4855 if (*funcname == NUL)
4856 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004857 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004858 ? "completefunc" : "omnifunc");
4859 return FAIL;
4860 }
4861
4862 args[0].v_type = VAR_NUMBER;
4863 args[0].vval.v_number = 1;
4864 args[1].v_type = VAR_STRING;
4865 args[1].vval.v_string = (char_u *)"";
4866 args[2].v_type = VAR_UNKNOWN;
4867 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004868 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004869 cb = get_insert_callback(ctrl_x_mode);
4870 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004871 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004872
4873 State = save_State;
4874 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004875 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004876 validate_cursor();
4877 if (!EQUAL_POS(curwin->w_cursor, pos))
4878 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004879 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004880 return FAIL;
4881 }
4882
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004883 // Return value -2 means the user complete function wants to cancel the
4884 // complete without an error, do the same if the function did not execute
4885 // successfully.
4886 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004887 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004888 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004889 if (col == -3)
4890 {
4891 ctrl_x_mode = CTRL_X_NORMAL;
4892 edit_submode = NULL;
4893 if (!shortmess(SHM_COMPLETIONMENU))
4894 msg_clr_cmdline();
4895 return FAIL;
4896 }
4897
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004898 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004899 // completion.
4900 compl_opt_refresh_always = FALSE;
4901 compl_opt_suppress_empty = FALSE;
4902
4903 if (col < 0)
4904 col = curs_col;
4905 compl_col = col;
4906 if (compl_col > curs_col)
4907 compl_col = curs_col;
4908
4909 // Setup variables for completion. Need to obtain "line" again,
4910 // it may have become invalid.
4911 line = ml_get(curwin->w_cursor.lnum);
4912 compl_length = curs_col - compl_col;
4913 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4914 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004915 {
4916 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004917 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004918 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004919
John Marriott8c85a2a2024-05-20 19:18:26 +02004920 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004921 ret = OK;
4922#endif
4923
4924 return ret;
4925}
4926
4927/*
4928 * Get the pattern, column and length for spell completion.
4929 * Sets the global variables: compl_col, compl_length and compl_pattern.
4930 * Uses the global variable: spell_bad_len
4931 */
4932 static int
4933get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4934{
4935 int ret = FAIL;
4936#ifdef FEAT_SPELL
4937 char_u *line;
4938
4939 if (spell_bad_len > 0)
4940 compl_col = curs_col - spell_bad_len;
4941 else
4942 compl_col = spell_word_start(startcol);
4943 if (compl_col >= (colnr_T)startcol)
4944 {
4945 compl_length = 0;
4946 compl_col = curs_col;
4947 }
4948 else
4949 {
4950 spell_expand_check_cap(compl_col);
4951 compl_length = (int)curs_col - compl_col;
4952 }
4953 // Need to obtain "line" again, it may have become invalid.
4954 line = ml_get(curwin->w_cursor.lnum);
4955 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4956 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004957 {
4958 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004959 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004960 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004961
John Marriott8c85a2a2024-05-20 19:18:26 +02004962 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004963 ret = OK;
4964#endif
4965
4966 return ret;
4967}
4968
4969/*
4970 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004971 * "startcol" - start column number of the completion pattern/text
4972 * "cur_col" - current cursor column
4973 * On return, "line_invalid" is set to TRUE, if the current line may have
4974 * become invalid and needs to be fetched again.
4975 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004976 */
4977 static int
4978compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4979{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004980 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004981 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4982 && !thesaurus_func_complete(ctrl_x_mode)))
4983 {
4984 return get_normal_compl_info(line, startcol, curs_col);
4985 }
4986 else if (ctrl_x_mode_line_or_eval())
4987 {
4988 return get_wholeline_compl_info(line, curs_col);
4989 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004990 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004991 {
4992 return get_filename_compl_info(line, startcol, curs_col);
4993 }
4994 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4995 {
4996 return get_cmdline_compl_info(line, curs_col);
4997 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004998 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004999 || thesaurus_func_complete(ctrl_x_mode))
5000 {
5001 if (get_userdefined_compl_info(curs_col) == FAIL)
5002 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005003 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005004 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005005 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005006 {
5007 if (get_spell_compl_info(startcol, curs_col) == FAIL)
5008 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005009 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00005010 }
5011 else
5012 {
5013 internal_error("ins_complete()");
5014 return FAIL;
5015 }
5016
5017 return OK;
5018}
5019
5020/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005021 * Continue an interrupted completion mode search in "line".
5022 *
5023 * If this same ctrl_x_mode has been interrupted use the text from
5024 * "compl_startpos" to the cursor as a pattern to add a new word instead of
5025 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
5026 * the same line as the cursor then fix it (the line has been split because it
5027 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
5028 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005029 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005030 static void
5031ins_compl_continue_search(char_u *line)
5032{
5033 // it is a continued search
5034 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005035 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
5036 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005037 {
5038 if (compl_startpos.lnum != curwin->w_cursor.lnum)
5039 {
5040 // line (probably) wrapped, set compl_startpos to the
5041 // first non_blank in the line, if it is not a wordchar
5042 // include it to get a better pattern, but then we don't
5043 // want the "\\<" prefix, check it below
5044 compl_col = (colnr_T)getwhitecols(line);
5045 compl_startpos.col = compl_col;
5046 compl_startpos.lnum = curwin->w_cursor.lnum;
5047 compl_cont_status &= ~CONT_SOL; // clear SOL if present
5048 }
5049 else
5050 {
5051 // S_IPOS was set when we inserted a word that was at the
5052 // beginning of the line, which means that we'll go to SOL
5053 // mode but first we need to redefine compl_startpos
5054 if (compl_cont_status & CONT_S_IPOS)
5055 {
5056 compl_cont_status |= CONT_SOL;
5057 compl_startpos.col = (colnr_T)(skipwhite(
5058 line + compl_length
5059 + compl_startpos.col) - line);
5060 }
5061 compl_col = compl_startpos.col;
5062 }
5063 compl_length = curwin->w_cursor.col - (int)compl_col;
5064 // IObuff is used to add a "word from the next line" would we
5065 // have enough space? just being paranoid
5066#define MIN_SPACE 75
5067 if (compl_length > (IOSIZE - MIN_SPACE))
5068 {
5069 compl_cont_status &= ~CONT_SOL;
5070 compl_length = (IOSIZE - MIN_SPACE);
5071 compl_col = curwin->w_cursor.col - compl_length;
5072 }
5073 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5074 if (compl_length < 1)
5075 compl_cont_status &= CONT_LOCAL;
5076 }
5077 else if (ctrl_x_mode_line_or_eval())
5078 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
5079 else
5080 compl_cont_status = 0;
5081}
5082
5083/*
5084 * start insert mode completion
5085 */
5086 static int
5087ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005088{
5089 char_u *line;
5090 int startcol = 0; // column where searched text starts
5091 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005092 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005093 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005094 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005095
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005096 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005097
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005098 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005099 did_si = FALSE;
5100 can_si = FALSE;
5101 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005102 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005103 return FAIL;
5104
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005105 line = ml_get(curwin->w_cursor.lnum);
5106 curs_col = curwin->w_cursor.col;
5107 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005108
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005109 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5110 && compl_cont_mode == ctrl_x_mode)
5111 // this same ctrl-x_mode was interrupted previously. Continue the
5112 // completion.
5113 ins_compl_continue_search(line);
5114 else
5115 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005116
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005117 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005118 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005119 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005120 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005121 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
5122 compl_cont_status = 0;
5123 compl_cont_status |= CONT_N_ADDS;
5124 compl_startpos = curwin->w_cursor;
5125 startcol = (int)curs_col;
5126 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005127 }
5128
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005129 // Work out completion pattern and original text -- webb
5130 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5131 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005132 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5133 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005134 // restore did_ai, so that adding comment leader works
5135 did_ai = save_did_ai;
5136 return FAIL;
5137 }
5138 // If "line" was changed while getting completion info get it again.
5139 if (line_invalid)
5140 line = ml_get(curwin->w_cursor.lnum);
5141
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005142 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005143 {
5144 edit_submode_pre = (char_u *)_(" Adding");
5145 if (ctrl_x_mode_line_or_eval())
5146 {
5147 // Insert a new line, keep indentation but ignore 'comments'.
5148 char_u *old = curbuf->b_p_com;
5149
5150 curbuf->b_p_com = (char_u *)"";
5151 compl_startpos.lnum = curwin->w_cursor.lnum;
5152 compl_startpos.col = compl_col;
5153 ins_eol('\r');
5154 curbuf->b_p_com = old;
5155 compl_length = 0;
5156 compl_col = curwin->w_cursor.col;
5157 }
5158 }
5159 else
5160 {
5161 edit_submode_pre = NULL;
5162 compl_startpos.col = compl_col;
5163 }
5164
5165 if (compl_cont_status & CONT_LOCAL)
5166 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5167 else
5168 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5169
5170 // If any of the original typed text has been changed we need to fix
5171 // the redo buffer.
5172 ins_compl_fixRedoBufForLeader(NULL);
5173
5174 // Always add completion for the original text.
5175 vim_free(compl_orig_text);
5176 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5177 if (p_ic)
5178 flags |= CP_ICASE;
5179 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5180 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
5181 {
5182 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005183 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005184 VIM_CLEAR(compl_orig_text);
5185 return FAIL;
5186 }
5187
5188 // showmode might reset the internal line pointers, so it must
5189 // be called before line = ml_get(), or when this address is no
5190 // longer needed. -- Acevedo.
5191 edit_submode_extra = (char_u *)_("-- Searching...");
5192 edit_submode_highl = HLF_COUNT;
5193 showmode();
5194 edit_submode_extra = NULL;
5195 out_flush();
5196
5197 return OK;
5198}
5199
5200/*
5201 * display the completion status message
5202 */
5203 static void
5204ins_compl_show_statusmsg(void)
5205{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005206 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005207 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005208 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005209 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005210 ? (char_u *)_("Hit end of paragraph")
5211 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005212 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005213 }
5214
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005215 if (edit_submode_extra == NULL)
5216 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005217 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005218 {
5219 edit_submode_extra = (char_u *)_("Back at original");
5220 edit_submode_highl = HLF_W;
5221 }
5222 else if (compl_cont_status & CONT_S_IPOS)
5223 {
5224 edit_submode_extra = (char_u *)_("Word from other line");
5225 edit_submode_highl = HLF_COUNT;
5226 }
5227 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5228 {
5229 edit_submode_extra = (char_u *)_("The only match");
5230 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005231 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005232 }
5233 else
5234 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005235#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005236 // Update completion sequence number when needed.
5237 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005238 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005239#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005240 // The match should always have a sequence number now, this is
5241 // just a safety check.
5242 if (compl_curr_match->cp_number != -1)
5243 {
5244 // Space for 10 text chars. + 2x10-digit no.s = 31.
5245 // Translations may need more than twice that.
5246 static char_u match_ref[81];
5247
5248 if (compl_matches > 0)
5249 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005250 _("match %d of %d"),
5251 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005252 else
5253 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005254 _("match %d"),
5255 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005256 edit_submode_extra = match_ref;
5257 edit_submode_highl = HLF_R;
5258 if (dollar_vcol >= 0)
5259 curs_columns(FALSE);
5260 }
5261 }
5262 }
5263
5264 // Show a message about what (completion) mode we're in.
5265 if (!compl_opt_suppress_empty)
5266 {
5267 showmode();
5268 if (!shortmess(SHM_COMPLETIONMENU))
5269 {
5270 if (edit_submode_extra != NULL)
5271 {
5272 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005273 {
5274 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005275 msg_attr((char *)edit_submode_extra,
5276 edit_submode_highl < HLF_COUNT
5277 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005278 msg_hist_off = FALSE;
5279 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005280 }
5281 else
5282 msg_clr_cmdline(); // necessary for "noshowmode"
5283 }
5284 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005285}
5286
5287/*
5288 * Do Insert mode completion.
5289 * Called when character "c" was typed, which has a meaning for completion.
5290 * Returns OK if completion was done, FAIL if something failed (out of mem).
5291 */
5292 int
5293ins_complete(int c, int enable_pum)
5294{
5295 int n;
5296 int save_w_wrow;
5297 int save_w_leftcol;
5298 int insert_match;
5299
5300 compl_direction = ins_compl_key2dir(c);
5301 insert_match = ins_compl_use_match(c);
5302
5303 if (!compl_started)
5304 {
5305 if (ins_compl_start() == FAIL)
5306 return FAIL;
5307 }
5308 else if (insert_match && stop_arrow() == FAIL)
5309 return FAIL;
5310
5311 compl_shown_match = compl_curr_match;
5312 compl_shows_dir = compl_direction;
5313
5314 // Find next match (and following matches).
5315 save_w_wrow = curwin->w_wrow;
5316 save_w_leftcol = curwin->w_leftcol;
5317 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5318
5319 // may undisplay the popup menu
5320 ins_compl_upd_pum();
5321
5322 if (n > 1) // all matches have been found
5323 compl_matches = n;
5324 compl_curr_match = compl_shown_match;
5325 compl_direction = compl_shows_dir;
5326
5327 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5328 // mode.
5329 if (got_int && !global_busy)
5330 {
5331 (void)vgetc();
5332 got_int = FALSE;
5333 }
5334
5335 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005336 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005337 {
5338 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5339 // because we couldn't expand anything at first place, but if we used
5340 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5341 // (such as M in M'exico) if not tried already. -- Acevedo
5342 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005343 || compl_status_adding()
5344 || (ctrl_x_mode_not_default()
5345 && !ctrl_x_mode_path_patterns()
5346 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005347 compl_cont_status &= ~CONT_N_ADDS;
5348 }
5349
5350 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5351 compl_cont_status |= CONT_S_IPOS;
5352 else
5353 compl_cont_status &= ~CONT_S_IPOS;
5354
5355 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005356
5357 // Show the popup menu, unless we got interrupted.
5358 if (enable_pum && !compl_interrupted)
5359 show_pum(save_w_wrow, save_w_leftcol);
5360
5361 compl_was_interrupted = compl_interrupted;
5362 compl_interrupted = FALSE;
5363
5364 return OK;
5365}
5366
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005367/*
5368 * Remove (if needed) and show the popup menu
5369 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005370 static void
5371show_pum(int prev_w_wrow, int prev_w_leftcol)
5372{
5373 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005374 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005375 RedrawingDisabled = 0;
5376
5377 // If the cursor moved or the display scrolled we need to remove the pum
5378 // first.
5379 setcursor();
5380 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5381 ins_compl_del_pum();
5382
5383 ins_compl_show_pum();
5384 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005385
5386 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005387}
5388
5389/*
5390 * Looks in the first "len" chars. of "src" for search-metachars.
5391 * If dest is not NULL the chars. are copied there quoting (with
5392 * a backslash) the metachars, and dest would be NUL terminated.
5393 * Returns the length (needed) of dest
5394 */
5395 static unsigned
5396quote_meta(char_u *dest, char_u *src, int len)
5397{
5398 unsigned m = (unsigned)len + 1; // one extra for the NUL
5399
5400 for ( ; --len >= 0; src++)
5401 {
5402 switch (*src)
5403 {
5404 case '.':
5405 case '*':
5406 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005407 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005408 break;
5409 // FALLTHROUGH
5410 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005411 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005412 break;
5413 // FALLTHROUGH
5414 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005415 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005416 break;
5417 // FALLTHROUGH
5418 case '^': // currently it's not needed.
5419 case '$':
5420 m++;
5421 if (dest != NULL)
5422 *dest++ = '\\';
5423 break;
5424 }
5425 if (dest != NULL)
5426 *dest++ = *src;
5427 // Copy remaining bytes of a multibyte character.
5428 if (has_mbyte)
5429 {
5430 int i, mb_len;
5431
5432 mb_len = (*mb_ptr2len)(src) - 1;
5433 if (mb_len > 0 && len >= mb_len)
5434 for (i = 0; i < mb_len; ++i)
5435 {
5436 --len;
5437 ++src;
5438 if (dest != NULL)
5439 *dest++ = *src;
5440 }
5441 }
5442 }
5443 if (dest != NULL)
5444 *dest = NUL;
5445
5446 return m;
5447}
5448
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005449#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005450 void
5451free_insexpand_stuff(void)
5452{
5453 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005454# ifdef FEAT_EVAL
5455 free_callback(&cfu_cb);
5456 free_callback(&ofu_cb);
5457 free_callback(&tsrfu_cb);
5458# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005459}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005460#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005461
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005462#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005463/*
5464 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5465 * spelled word, if there is one.
5466 */
5467 static void
5468spell_back_to_badword(void)
5469{
5470 pos_T tpos = curwin->w_cursor;
5471
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005472 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005473 if (curwin->w_cursor.col != tpos.col)
5474 start_arrow(&tpos);
5475}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005476#endif