blob: c6bf681677248b56f7561e58d0628b2e86c4d4ad [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
glepnira218cc62024-06-03 19:32:39 +0200116 int cp_score; // fuzzy match score
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100117};
118
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200119// values for cp_flags
120# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
121# define CP_FREE_FNAME 2 // cp_fname is allocated
122# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
123# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
124# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200125# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127/*
128 * All the current matches are stored in a list.
129 * "compl_first_match" points to the start of the list.
130 * "compl_curr_match" points to the currently selected entry.
131 * "compl_shown_match" is different from compl_curr_match during
132 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000133 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100134 */
135static compl_T *compl_first_match = NULL;
136static compl_T *compl_curr_match = NULL;
137static compl_T *compl_shown_match = NULL;
138static compl_T *compl_old_match = NULL;
139
140// After using a cursor key <Enter> selects a match in the popup menu,
141// otherwise it inserts a line break.
142static int compl_enter_selects = FALSE;
143
144// When "compl_leader" is not NULL only matches that start with this string
145// are used.
146static char_u *compl_leader = NULL;
147
148static int compl_get_longest = FALSE; // put longest common string
149 // in compl_leader
150
151static int compl_no_insert = FALSE; // FALSE: select & insert
152 // TRUE: noinsert
153static int compl_no_select = FALSE; // FALSE: select & insert
154 // TRUE: noselect
bfredl87af60c2022-09-24 11:17:51 +0100155static int compl_longest = FALSE; // FALSE: insert full match
156 // TRUE: insert longest prefix
glepnira218cc62024-06-03 19:32:39 +0200157static int compl_fuzzy_match = FALSE; // True: fuzzy match enabled
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100158
159// Selected one of the matches. When FALSE the match was edited or using the
160// longest common string.
161static int compl_used_match;
162
163// didn't finish finding completions.
164static int compl_was_interrupted = FALSE;
165
166// Set when character typed while looking for matches and it means we should
167// stop looking for matches.
168static int compl_interrupted = FALSE;
169
170static int compl_restarting = FALSE; // don't insert match
171
172// When the first completion is done "compl_started" is set. When it's
173// FALSE the word to be completed must be located.
174static int compl_started = FALSE;
175
176// Which Ctrl-X mode are we in?
177static int ctrl_x_mode = CTRL_X_NORMAL;
178
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000179static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100180static char_u *compl_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200181static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100182static int compl_direction = FORWARD;
183static int compl_shows_dir = FORWARD;
184static int compl_pending = 0; // > 1 for postponed CTRL-N
185static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000186// Length in bytes of the text being completed (this is deleted to be replaced
187// by the match.)
188static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100189static colnr_T compl_col = 0; // column where the text starts
190 // that is being completed
191static char_u *compl_orig_text = NULL; // text as it was before
192 // completion started
193static int compl_cont_mode = 0;
194static expand_T compl_xp;
195
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000196// List of flags for method of completion.
197static int compl_cont_status = 0;
198# define CONT_ADDING 1 // "normal" or "adding" expansion
199# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
200 // it's set only iff N_ADDS is set
201# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
202# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
203 // if so, word-wise-expansion will set SOL
204# define CONT_SOL 16 // pattern includes start of line, just for
205 // word-wise expansion, not set for ^X^L
206# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
207 // expansion, (eg use complete=.)
208
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100209static int compl_opt_refresh_always = FALSE;
210static int compl_opt_suppress_empty = FALSE;
211
glepnira218cc62024-06-03 19:32:39 +0200212static int compl_selected_item = -1;
213
Bram Moolenaar08928322020-01-04 14:32:48 +0100214static 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 +0100215static void ins_compl_longest_match(compl_T *match);
216static void ins_compl_del_pum(void);
217static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
218static char_u *find_line_end(char_u *ptr);
219static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100220static int ins_compl_need_restart(void);
221static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000222static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100223static void ins_compl_restart(void);
224static void ins_compl_set_original_text(char_u *str);
225static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
226# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
227static void ins_compl_add_list(list_T *list);
228static void ins_compl_add_dict(dict_T *dict);
229# endif
230static int ins_compl_key2dir(int c);
231static int ins_compl_pum_key(int c);
232static int ins_compl_key2count(int c);
233static void show_pum(int prev_w_wrow, int prev_w_leftcol);
234static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100235
236#ifdef FEAT_SPELL
237static void spell_back_to_badword(void);
238static int spell_bad_len = 0; // length of located bad word
239#endif
240
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100241/*
242 * CTRL-X pressed in Insert mode.
243 */
244 void
245ins_ctrl_x(void)
246{
zeertzjqdca29d92021-08-31 19:12:51 +0200247 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100248 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000249 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100250 if (compl_cont_status & CONT_N_ADDS)
251 compl_cont_status |= CONT_INTRPT;
252 else
253 compl_cont_status = 0;
254 // We're not sure which CTRL-X mode it will be yet
255 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
256 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
257 edit_submode_pre = NULL;
258 showmode();
259 }
zeertzjqdca29d92021-08-31 19:12:51 +0200260 else
261 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
262 // CTRL-V look like CTRL-N
263 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100264
LemonBoy2bf52dd2022-04-09 18:17:34 +0100265 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100266}
267
268/*
269 * Functions to check the current CTRL-X mode.
270 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000271int ctrl_x_mode_none(void)
272 { return ctrl_x_mode == 0; }
273int ctrl_x_mode_normal(void)
274 { return ctrl_x_mode == CTRL_X_NORMAL; }
275int ctrl_x_mode_scroll(void)
276 { return ctrl_x_mode == CTRL_X_SCROLL; }
277int ctrl_x_mode_whole_line(void)
278 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
279int ctrl_x_mode_files(void)
280 { return ctrl_x_mode == CTRL_X_FILES; }
281int ctrl_x_mode_tags(void)
282 { return ctrl_x_mode == CTRL_X_TAGS; }
283int ctrl_x_mode_path_patterns(void)
284 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
285int ctrl_x_mode_path_defines(void)
286 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
287int ctrl_x_mode_dictionary(void)
288 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
289int ctrl_x_mode_thesaurus(void)
290 { return ctrl_x_mode == CTRL_X_THESAURUS; }
291int ctrl_x_mode_cmdline(void)
292 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200293 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000294int ctrl_x_mode_function(void)
295 { return ctrl_x_mode == CTRL_X_FUNCTION; }
296int ctrl_x_mode_omni(void)
297 { return ctrl_x_mode == CTRL_X_OMNI; }
298int ctrl_x_mode_spell(void)
299 { return ctrl_x_mode == CTRL_X_SPELL; }
300static int ctrl_x_mode_eval(void)
301 { return ctrl_x_mode == CTRL_X_EVAL; }
302int ctrl_x_mode_line_or_eval(void)
303 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100304
305/*
306 * Whether other than default completion has been selected.
307 */
308 int
309ctrl_x_mode_not_default(void)
310{
311 return ctrl_x_mode != CTRL_X_NORMAL;
312}
313
314/*
zeertzjqdca29d92021-08-31 19:12:51 +0200315 * Whether CTRL-X was typed without a following character,
316 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100317 */
318 int
319ctrl_x_mode_not_defined_yet(void)
320{
321 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
322}
323
324/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000325 * Return TRUE if currently in "normal" or "adding" insert completion matches
326 * state
327 */
328 int
329compl_status_adding(void)
330{
331 return compl_cont_status & CONT_ADDING;
332}
333
334/*
335 * Return TRUE if the completion pattern includes start of line, just for
336 * word-wise expansion.
337 */
338 int
339compl_status_sol(void)
340{
341 return compl_cont_status & CONT_SOL;
342}
343
344/*
345 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
346 */
347 int
348compl_status_local(void)
349{
350 return compl_cont_status & CONT_LOCAL;
351}
352
353/*
354 * Clear the completion status flags
355 */
356 void
357compl_status_clear(void)
358{
359 compl_cont_status = 0;
360}
361
362/*
363 * Return TRUE if completion is using the forward direction matches
364 */
365 static int
366compl_dir_forward(void)
367{
368 return compl_direction == FORWARD;
369}
370
371/*
372 * Return TRUE if currently showing forward completion matches
373 */
374 static int
375compl_shows_dir_forward(void)
376{
377 return compl_shows_dir == FORWARD;
378}
379
380/*
381 * Return TRUE if currently showing backward completion matches
382 */
383 static int
384compl_shows_dir_backward(void)
385{
386 return compl_shows_dir == BACKWARD;
387}
388
389/*
390 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 */
392 int
393has_compl_option(int dict_opt)
394{
395 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200396#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100397 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200398#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100399 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100400 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
401#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100402 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100403#endif
404 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100405 {
406 ctrl_x_mode = CTRL_X_NORMAL;
407 edit_submode = NULL;
408 msg_attr(dict_opt ? _("'dictionary' option is empty")
409 : _("'thesaurus' option is empty"),
410 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100411 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100412 {
413 vim_beep(BO_COMPL);
414 setcursor();
415 out_flush();
416#ifdef FEAT_EVAL
417 if (!get_vim_var_nr(VV_TESTING))
418#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100419 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100420 }
421 return FALSE;
422 }
423 return TRUE;
424}
425
426/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000427 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100428 * This depends on the current mode.
429 */
430 int
431vim_is_ctrl_x_key(int c)
432{
433 // Always allow ^R - let its results then be checked
434 if (c == Ctrl_R)
435 return TRUE;
436
437 // Accept <PageUp> and <PageDown> if the popup menu is visible.
438 if (ins_compl_pum_key(c))
439 return TRUE;
440
441 switch (ctrl_x_mode)
442 {
443 case 0: // Not in any CTRL-X mode
444 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
445 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200446 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100447 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
448 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
449 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
450 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
451 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200452 || c == Ctrl_S || c == Ctrl_K || c == 's'
453 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100454 case CTRL_X_SCROLL:
455 return (c == Ctrl_Y || c == Ctrl_E);
456 case CTRL_X_WHOLE_LINE:
457 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_FILES:
459 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
460 case CTRL_X_DICTIONARY:
461 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
462 case CTRL_X_THESAURUS:
463 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_TAGS:
465 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
466#ifdef FEAT_FIND_ID
467 case CTRL_X_PATH_PATTERNS:
468 return (c == Ctrl_P || c == Ctrl_N);
469 case CTRL_X_PATH_DEFINES:
470 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
471#endif
472 case CTRL_X_CMDLINE:
473 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
474 || c == Ctrl_X);
475#ifdef FEAT_COMPL_FUNC
476 case CTRL_X_FUNCTION:
477 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
478 case CTRL_X_OMNI:
479 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
480#endif
481 case CTRL_X_SPELL:
482 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
483 case CTRL_X_EVAL:
484 return (c == Ctrl_P || c == Ctrl_N);
485 }
486 internal_error("vim_is_ctrl_x_key()");
487 return FALSE;
488}
489
490/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000491 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000492 */
493 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000494match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000495{
496 return match->cp_flags & CP_ORIGINAL_TEXT;
497}
498
499/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000500 * Returns TRUE if "match" is the first match in the completion list.
501 */
502 static int
503is_first_match(compl_T *match)
504{
505 return match == compl_first_match;
506}
507
508/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100509 * Return TRUE when character "c" is part of the item currently being
510 * completed. Used to decide whether to abandon complete mode when the menu
511 * is visible.
512 */
513 int
514ins_compl_accept_char(int c)
515{
516 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
517 // When expanding an identifier only accept identifier chars.
518 return vim_isIDc(c);
519
520 switch (ctrl_x_mode)
521 {
522 case CTRL_X_FILES:
523 // When expanding file name only accept file name chars. But not
524 // path separators, so that "proto/<Tab>" expands files in
525 // "proto", not "proto/" as a whole
526 return vim_isfilec(c) && !vim_ispathsep(c);
527
528 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200529 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100530 case CTRL_X_OMNI:
531 // Command line and Omni completion can work with just about any
532 // printable character, but do stop at white space.
533 return vim_isprintc(c) && !VIM_ISWHITE(c);
534
535 case CTRL_X_WHOLE_LINE:
536 // For while line completion a space can be part of the line.
537 return vim_isprintc(c);
538 }
539 return vim_iswordc(c);
540}
541
542/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000543 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100544 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000545 */
546 static char_u *
547ins_compl_infercase_gettext(
548 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100549 int char_len,
550 int compl_char_len,
551 int min_len,
552 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000553{
554 int *wca; // Wide character array.
555 char_u *p;
556 int i, c;
557 int has_lower = FALSE;
558 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100559 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000560
561 IObuff[0] = NUL;
562
563 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100564 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (wca == NULL)
566 return IObuff;
567
568 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100569 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000570 if (has_mbyte)
571 wca[i] = mb_ptr2char_adv(&p);
572 else
573 wca[i] = *(p++);
574
575 // Rule 1: Were any chars converted to lower?
576 p = compl_orig_text;
577 for (i = 0; i < min_len; ++i)
578 {
579 if (has_mbyte)
580 c = mb_ptr2char_adv(&p);
581 else
582 c = *(p++);
583 if (MB_ISLOWER(c))
584 {
585 has_lower = TRUE;
586 if (MB_ISUPPER(wca[i]))
587 {
588 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100589 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000590 wca[i] = MB_TOLOWER(wca[i]);
591 break;
592 }
593 }
594 }
595
596 // Rule 2: No lower case, 2nd consecutive letter converted to
597 // upper case.
598 if (!has_lower)
599 {
600 p = compl_orig_text;
601 for (i = 0; i < min_len; ++i)
602 {
603 if (has_mbyte)
604 c = mb_ptr2char_adv(&p);
605 else
606 c = *(p++);
607 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
608 {
609 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100610 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000611 wca[i] = MB_TOUPPER(wca[i]);
612 break;
613 }
614 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
615 }
616 }
617
618 // Copy the original case of the part we typed.
619 p = compl_orig_text;
620 for (i = 0; i < min_len; ++i)
621 {
622 if (has_mbyte)
623 c = mb_ptr2char_adv(&p);
624 else
625 c = *(p++);
626 if (MB_ISLOWER(c))
627 wca[i] = MB_TOLOWER(wca[i]);
628 else if (MB_ISUPPER(c))
629 wca[i] = MB_TOUPPER(wca[i]);
630 }
631
632 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000633 p = IObuff;
634 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100635 ga_init2(&gap, 1, 500);
636 while (i < char_len)
637 {
638 if (gap.ga_data != NULL)
639 {
640 if (ga_grow(&gap, 10) == FAIL)
641 {
642 ga_clear(&gap);
643 return (char_u *)"[failed]";
644 }
645 p = (char_u *)gap.ga_data + gap.ga_len;
646 if (has_mbyte)
647 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
648 else
649 {
650 *p = wca[i++];
651 ++gap.ga_len;
652 }
653 }
654 else if ((p - IObuff) + 6 >= IOSIZE)
655 {
656 // Multi-byte characters can occupy up to five bytes more than
657 // ASCII characters, and we also need one byte for NUL, so when
658 // getting to six bytes from the edge of IObuff switch to using a
659 // growarray. Add the character in the next round.
660 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100661 {
662 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100664 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100665 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100666 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100667 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100668 }
669 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000670 p += (*mb_char2bytes)(wca[i++], p);
671 else
672 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100673 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000674 vim_free(wca);
675
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100676 if (gap.ga_data != NULL)
677 {
678 *tofree = gap.ga_data;
679 return gap.ga_data;
680 }
681
682 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000683 return IObuff;
684}
685
686/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
688 * case of the originally typed text is used, and the case of the completed
689 * text is inferred, ie this tries to work out what case you probably wanted
690 * the rest of the word to be in -- webb
691 */
692 int
693ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200694 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695 int len,
696 int icase,
697 char_u *fname,
698 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200699 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100700{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200701 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100702 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100703 int char_len; // count multi-byte characters
704 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100705 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200706 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100707 int res;
708 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709
710 if (p_ic && curbuf->b_p_inf && len > 0)
711 {
712 // Infer case of completed part.
713
714 // Find actual length of completion.
715 if (has_mbyte)
716 {
717 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719 while (*p != NUL)
720 {
721 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100722 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723 }
724 }
725 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100726 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100727
728 // Find actual length of original text.
729 if (has_mbyte)
730 {
731 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 while (*p != NUL)
734 {
735 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737 }
738 }
739 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100740 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100741
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100742 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100743 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100744 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100745
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100746 str = ins_compl_infercase_gettext(str, char_len,
747 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100748 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200749 if (cont_s_ipos)
750 flags |= CP_CONT_S_IPOS;
751 if (icase)
752 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200753
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100754 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
755 vim_free(tofree);
756 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100757}
758
759/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000760 * Add a match to the list of matches. The arguments are:
761 * str - text of the match to add
762 * len - length of "str". If -1, then the length of "str" is
763 * computed.
764 * fname - file name to associate with this match.
765 * cptext - list of strings to use with this match (for abbr, menu, info
766 * and kind)
767 * user_data - user supplied data (any vim type) for this match
768 * cdir - match direction. If 0, use "compl_direction".
769 * flags_arg - match flags (cp_flags)
770 * adup - accept this match even if it is already present.
771 * If "cdir" is FORWARD, then the match is added after the current match.
772 * Otherwise, it is added before the current match.
773 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 * If the given string is already in the list of completions, then return
775 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
776 * maybe because alloc() returns NULL, then FAIL is returned.
777 */
778 static int
779ins_compl_add(
780 char_u *str,
781 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100782 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 char_u **cptext, // extra text for popup menu or NULL
784 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100785 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200786 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100787 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788{
789 compl_T *match;
790 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200791 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100792
Bram Moolenaarceb06192021-04-04 15:05:22 +0200793 if (flags & CP_FAST)
794 fast_breakcheck();
795 else
796 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100797 if (got_int)
798 return FAIL;
799 if (len < 0)
800 len = (int)STRLEN(str);
801
802 // If the same match is already present, don't add it.
803 if (compl_first_match != NULL && !adup)
804 {
805 match = compl_first_match;
806 do
807 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000808 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100809 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100810 && ((int)STRLEN(match->cp_str) <= len
811 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100812 return NOTDONE;
813 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000814 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 }
816
817 // Remove any popup menu before changing the list of matches.
818 ins_compl_del_pum();
819
820 // Allocate a new match structure.
821 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200822 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 if (match == NULL)
824 return FAIL;
825 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200826 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100827 match->cp_number = 0;
828 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
829 {
830 vim_free(match);
831 return FAIL;
832 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833
834 // match-fname is:
835 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200836 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100837 // - NULL otherwise. --Acevedo
838 if (fname != NULL
839 && compl_curr_match != NULL
840 && compl_curr_match->cp_fname != NULL
841 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
842 match->cp_fname = compl_curr_match->cp_fname;
843 else if (fname != NULL)
844 {
845 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200846 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100847 }
848 else
849 match->cp_fname = NULL;
850 match->cp_flags = flags;
851
852 if (cptext != NULL)
853 {
854 int i;
855
856 for (i = 0; i < CPT_COUNT; ++i)
857 if (cptext[i] != NULL && *cptext[i] != NUL)
858 match->cp_text[i] = vim_strsave(cptext[i]);
859 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100860#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100861 if (user_data != NULL)
862 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100863#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100864
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000865 // Link the new match structure after (FORWARD) or before (BACKWARD) the
866 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100867 if (compl_first_match == NULL)
868 match->cp_next = match->cp_prev = NULL;
869 else if (dir == FORWARD)
870 {
871 match->cp_next = compl_curr_match->cp_next;
872 match->cp_prev = compl_curr_match;
873 }
874 else // BACKWARD
875 {
876 match->cp_next = compl_curr_match;
877 match->cp_prev = compl_curr_match->cp_prev;
878 }
879 if (match->cp_next)
880 match->cp_next->cp_prev = match;
881 if (match->cp_prev)
882 match->cp_prev->cp_next = match;
883 else // if there's nothing before, it is the first match
884 compl_first_match = match;
885 compl_curr_match = match;
886
887 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200888 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 ins_compl_longest_match(match);
890
891 return OK;
892}
893
894/*
895 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200896 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100897 */
898 static int
899ins_compl_equal(compl_T *match, char_u *str, int len)
900{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200901 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200902 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200903 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100904 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
905 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
906}
907
908/*
909 * Reduce the longest common string for match "match".
910 */
911 static void
912ins_compl_longest_match(compl_T *match)
913{
914 char_u *p, *s;
915 int c1, c2;
916 int had_match;
917
918 if (compl_leader == NULL)
919 {
920 // First match, use it as a whole.
921 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000922 if (compl_leader == NULL)
923 return;
924
925 had_match = (curwin->w_cursor.col > compl_col);
926 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000927 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000928 ins_redraw(FALSE);
929
930 // When the match isn't there (to avoid matching itself) remove it
931 // again after redrawing.
932 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100934 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000935
936 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100937 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000938
939 // Reduce the text if this match differs from compl_leader.
940 p = compl_leader;
941 s = match->cp_str;
942 while (*p != NUL)
943 {
944 if (has_mbyte)
945 {
946 c1 = mb_ptr2char(p);
947 c2 = mb_ptr2char(s);
948 }
949 else
950 {
951 c1 = *p;
952 c2 = *s;
953 }
954 if ((match->cp_flags & CP_ICASE)
955 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
956 break;
957 if (has_mbyte)
958 {
959 MB_PTR_ADV(p);
960 MB_PTR_ADV(s);
961 }
962 else
963 {
964 ++p;
965 ++s;
966 }
967 }
968
969 if (*p != NUL)
970 {
971 // Leader was shortened, need to change the inserted text.
972 *p = NUL;
973 had_match = (curwin->w_cursor.col > compl_col);
974 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000975 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000976 ins_redraw(FALSE);
977
978 // When the match isn't there (to avoid matching itself) remove it
979 // again after redrawing.
980 if (!had_match)
981 ins_compl_delete();
982 }
983
984 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100985}
986
987/*
988 * Add an array of matches to the list of matches.
989 * Frees matches[].
990 */
991 static void
992ins_compl_add_matches(
993 int num_matches,
994 char_u **matches,
995 int icase)
996{
997 int i;
998 int add_r = OK;
999 int dir = compl_direction;
1000
1001 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +01001002 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +02001003 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001004 // if dir was BACKWARD then honor it just once
1005 dir = FORWARD;
1006 FreeWild(num_matches, matches);
1007}
1008
1009/*
1010 * Make the completion list cyclic.
1011 * Return the number of matches (excluding the original).
1012 */
1013 static int
1014ins_compl_make_cyclic(void)
1015{
1016 compl_T *match;
1017 int count = 0;
1018
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001019 if (compl_first_match == NULL)
1020 return 0;
1021
1022 // Find the end of the list.
1023 match = compl_first_match;
1024 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001025 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001026 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001027 match = match->cp_next;
1028 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001029 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001030 match->cp_next = compl_first_match;
1031 compl_first_match->cp_prev = match;
1032
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033 return count;
1034}
1035
1036/*
1037 * Return whether there currently is a shown match.
1038 */
1039 int
1040ins_compl_has_shown_match(void)
1041{
1042 return compl_shown_match == NULL
1043 || compl_shown_match != compl_shown_match->cp_next;
1044}
1045
1046/*
1047 * Return whether the shown match is long enough.
1048 */
1049 int
1050ins_compl_long_shown_match(void)
1051{
1052 return (int)STRLEN(compl_shown_match->cp_str)
1053 > curwin->w_cursor.col - compl_col;
1054}
1055
1056/*
1057 * Set variables that store noselect and noinsert behavior from the
1058 * 'completeopt' value.
1059 */
1060 void
1061completeopt_was_set(void)
1062{
1063 compl_no_insert = FALSE;
1064 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001065 compl_longest = FALSE;
glepnira218cc62024-06-03 19:32:39 +02001066 compl_fuzzy_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001067 if (strstr((char *)p_cot, "noselect") != NULL)
1068 compl_no_select = TRUE;
1069 if (strstr((char *)p_cot, "noinsert") != NULL)
1070 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001071 if (strstr((char *)p_cot, "longest") != NULL)
1072 compl_longest = TRUE;
glepnira218cc62024-06-03 19:32:39 +02001073 if (strstr((char *)p_cot, "fuzzy") != NULL)
1074 compl_fuzzy_match = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001075}
1076
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001077
1078// "compl_match_array" points the currently displayed list of entries in the
1079// popup menu. It is NULL when there is no popup menu.
1080static pumitem_T *compl_match_array = NULL;
1081static int compl_match_arraysize;
1082
1083/*
1084 * Update the screen and when there is any scrolling remove the popup menu.
1085 */
1086 static void
1087ins_compl_upd_pum(void)
1088{
1089 int h;
1090
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001091 if (compl_match_array == NULL)
1092 return;
1093
1094 h = curwin->w_cline_height;
1095 // Update the screen later, before drawing the popup menu over it.
1096 pum_call_update_screen();
1097 if (h != curwin->w_cline_height)
1098 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001099}
1100
1101/*
1102 * Remove any popup menu.
1103 */
1104 static void
1105ins_compl_del_pum(void)
1106{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001107 if (compl_match_array == NULL)
1108 return;
1109
1110 pum_undisplay();
1111 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001112}
1113
1114/*
1115 * Return TRUE if the popup menu should be displayed.
1116 */
1117 int
1118pum_wanted(void)
1119{
1120 // 'completeopt' must contain "menu" or "menuone"
1121 if (vim_strchr(p_cot, 'm') == NULL)
1122 return FALSE;
1123
1124 // The display looks bad on a B&W display.
1125 if (t_colors < 8
1126#ifdef FEAT_GUI
1127 && !gui.in_use
1128#endif
1129 )
1130 return FALSE;
1131 return TRUE;
1132}
1133
1134/*
1135 * Return TRUE if there are two or more matches to be shown in the popup menu.
1136 * One if 'completopt' contains "menuone".
1137 */
1138 static int
1139pum_enough_matches(void)
1140{
1141 compl_T *compl;
1142 int i;
1143
1144 // Don't display the popup menu if there are no matches or there is only
1145 // one (ignoring the original text).
1146 compl = compl_first_match;
1147 i = 0;
1148 do
1149 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001150 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001151 break;
1152 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001153 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001154
1155 if (strstr((char *)p_cot, "menuone") != NULL)
1156 return (i >= 1);
1157 return (i >= 2);
1158}
1159
Bram Moolenaar3075a452021-11-17 15:51:52 +00001160#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001161/*
1162 * Allocate Dict for the completed item.
1163 * { word, abbr, menu, kind, info }
1164 */
1165 static dict_T *
1166ins_compl_dict_alloc(compl_T *match)
1167{
1168 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1169
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001170 if (dict == NULL)
1171 return NULL;
1172
1173 dict_add_string(dict, "word", match->cp_str);
1174 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1175 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1176 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1177 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1178 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1179 dict_add_string(dict, "user_data", (char_u *)"");
1180 else
1181 dict_add_tv(dict, "user_data", &match->cp_user_data);
1182
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001183 return dict;
1184}
1185
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001186/*
1187 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1188 * completion menu is changed.
1189 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001190 static void
1191trigger_complete_changed_event(int cur)
1192{
1193 dict_T *v_event;
1194 dict_T *item;
1195 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001196 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001197
1198 if (recursive)
1199 return;
1200
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 if (cur < 0)
1202 item = dict_alloc();
1203 else
1204 item = ins_compl_dict_alloc(compl_curr_match);
1205 if (item == NULL)
1206 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001207 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001208 dict_add_dict(v_event, "completed_item", item);
1209 pum_set_event_info(v_event);
1210 dict_set_items_ro(v_event);
1211
1212 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001213 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001214 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001215 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001216 recursive = FALSE;
1217
Bram Moolenaar3075a452021-11-17 15:51:52 +00001218 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001219}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001220#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001221
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001222/*
glepnira218cc62024-06-03 19:32:39 +02001223 * pumitem qsort compare func
1224 */
1225 static int
1226ins_compl_fuzzy_sort(const void *a, const void *b)
1227{
1228 const int sa = (*(pumitem_T *)a).pum_score;
1229 const int sb = (*(pumitem_T *)b).pum_score;
1230 return sa == sb ? 0 : sa < sb ? 1 : -1;
1231}
1232
1233/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001234 * Build a popup menu to show the completion matches.
1235 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1236 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001237 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001238 static int
1239ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001240{
1241 compl_T *compl;
1242 compl_T *shown_compl = NULL;
1243 int did_find_shown_match = FALSE;
1244 int shown_match_ok = FALSE;
1245 int i;
1246 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001247 int lead_len = 0;
glepnira218cc62024-06-03 19:32:39 +02001248 int max_fuzzy_score = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001249
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001250 // Need to build the popup menu list.
1251 compl_match_arraysize = 0;
1252 compl = compl_first_match;
1253 if (compl_leader != NULL)
1254 lead_len = (int)STRLEN(compl_leader);
1255
1256 do
1257 {
glepnira218cc62024-06-03 19:32:39 +02001258 // when completeopt include fuzzy option and leader is not null or empty
1259 // set the cp_score for after compare.
1260 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1261 compl->cp_score = fuzzy_match_str(compl->cp_str, compl_leader);
1262
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001263 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001264 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001265 || ins_compl_equal(compl, compl_leader, lead_len)
1266 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001267 ++compl_match_arraysize;
1268 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001269 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001270
1271 if (compl_match_arraysize == 0)
1272 return -1;
1273
1274 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1275 if (compl_match_array == NULL)
1276 return -1;
1277
1278 // If the current match is the original text don't find the first
1279 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001280 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001281 shown_match_ok = TRUE;
1282
glepnir53387c52024-05-27 15:11:01 +02001283 if (compl_leader != NULL
1284 && STRCMP(compl_leader, compl_orig_text) == 0
1285 && shown_match_ok == FALSE)
1286 compl_shown_match = compl_no_select ? compl_first_match
1287 : compl_first_match->cp_next;
1288
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001289 i = 0;
1290 compl = compl_first_match;
1291 do
1292 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001293 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001294 && (compl_leader == NULL
glepnira218cc62024-06-03 19:32:39 +02001295 || ins_compl_equal(compl, compl_leader, lead_len)
1296 || (compl_fuzzy_match && compl->cp_score > 0)))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001297 {
glepnira218cc62024-06-03 19:32:39 +02001298 if (!shown_match_ok && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001299 {
1300 if (compl == compl_shown_match || did_find_shown_match)
1301 {
1302 // This item is the shown match or this is the
1303 // first displayed item after the shown match.
1304 compl_shown_match = compl;
1305 did_find_shown_match = TRUE;
1306 shown_match_ok = TRUE;
1307 }
1308 else
1309 // Remember this displayed match for when the
1310 // shown match is just below it.
1311 shown_compl = compl;
1312 cur = i;
1313 }
glepnira218cc62024-06-03 19:32:39 +02001314 else if (compl_fuzzy_match)
1315 {
1316 if (compl->cp_score > max_fuzzy_score)
1317 {
1318 did_find_shown_match = TRUE;
1319 max_fuzzy_score = compl->cp_score;
1320 compl_shown_match = compl;
1321 shown_match_ok = TRUE;
1322 }
1323
1324 if (!compl_no_select
1325 && (max_fuzzy_score > 0
1326 || (compl_leader == NULL || lead_len == 0)))
1327 {
1328 shown_match_ok = TRUE;
1329 cur = 0;
1330 }
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)
1369 // sort by the largest score of fuzzy match
1370 qsort(compl_match_array, (size_t)compl_match_arraysize, sizeof(pumitem_T), ins_compl_fuzzy_sort);
1371
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001372 if (!shown_match_ok) // no displayed match at all
1373 cur = -1;
1374
1375 return cur;
1376}
1377
1378/*
1379 * Show the popup menu for the list of matches.
1380 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1381 */
1382 void
1383ins_compl_show_pum(void)
1384{
1385 int i;
1386 int cur = -1;
1387 colnr_T col;
1388
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001389 if (!pum_wanted() || !pum_enough_matches())
1390 return;
1391
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 // Update the screen later, before drawing the popup menu over it.
1393 pum_call_update_screen();
1394
1395 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001396 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001397 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001398 else
1399 {
1400 // popup menu already exists, only need to find the current item.
1401 for (i = 0; i < compl_match_arraysize; ++i)
1402 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1403 || compl_match_array[i].pum_text
1404 == compl_shown_match->cp_text[CPT_ABBR])
1405 {
1406 cur = i;
1407 break;
1408 }
1409 }
1410
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001411 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001412 {
1413#ifdef FEAT_EVAL
1414 if (compl_started && has_completechanged())
1415 trigger_complete_changed_event(cur);
1416#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001417 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001418 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001419
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001420 // In Replace mode when a $ is displayed at the end of the line only
1421 // part of the screen would be updated. We do need to redraw here.
1422 dollar_vcol = -1;
1423
1424 // Compute the screen column of the start of the completed text.
1425 // Use the cursor to get all wrapping and other settings right.
1426 col = curwin->w_cursor.col;
1427 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001428 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001429 pum_display(compl_match_array, compl_match_arraysize, cur);
1430 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001431
glepnircbb46b42024-02-03 18:11:13 +01001432 // After adding leader, set the current match to shown match.
1433 if (compl_started && compl_curr_match != compl_shown_match)
1434 compl_curr_match = compl_shown_match;
1435
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001436#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001437 if (has_completechanged())
1438 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001439#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001440}
1441
1442#define DICT_FIRST (1) // use just first element in "dict"
1443#define DICT_EXACT (2) // "dict" is the exact name of a file
1444
1445/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001446 * Add any identifiers that match the given pattern "pat" in the list of
1447 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001448 */
1449 static void
1450ins_compl_dictionaries(
1451 char_u *dict_start,
1452 char_u *pat,
1453 int flags, // DICT_FIRST and/or DICT_EXACT
1454 int thesaurus) // Thesaurus completion
1455{
1456 char_u *dict = dict_start;
1457 char_u *ptr;
1458 char_u *buf;
1459 regmatch_T regmatch;
1460 char_u **files;
1461 int count;
1462 int save_p_scs;
1463 int dir = compl_direction;
1464
1465 if (*dict == NUL)
1466 {
1467#ifdef FEAT_SPELL
1468 // When 'dictionary' is empty and spell checking is enabled use
1469 // "spell".
1470 if (!thesaurus && curwin->w_p_spell)
1471 dict = (char_u *)"spell";
1472 else
1473#endif
1474 return;
1475 }
1476
1477 buf = alloc(LSIZE);
1478 if (buf == NULL)
1479 return;
1480 regmatch.regprog = NULL; // so that we can goto theend
1481
1482 // If 'infercase' is set, don't use 'smartcase' here
1483 save_p_scs = p_scs;
1484 if (curbuf->b_p_inf)
1485 p_scs = FALSE;
1486
1487 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1488 // to only match at the start of a line. Otherwise just match the
1489 // pattern. Also need to double backslashes.
1490 if (ctrl_x_mode_line_or_eval())
1491 {
1492 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1493 size_t len;
1494
1495 if (pat_esc == NULL)
1496 goto theend;
1497 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001498 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001499 if (ptr == NULL)
1500 {
1501 vim_free(pat_esc);
1502 goto theend;
1503 }
1504 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1505 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1506 vim_free(pat_esc);
1507 vim_free(ptr);
1508 }
1509 else
1510 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001511 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001512 if (regmatch.regprog == NULL)
1513 goto theend;
1514 }
1515
1516 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1517 regmatch.rm_ic = ignorecase(pat);
1518 while (*dict != NUL && !got_int && !compl_interrupted)
1519 {
1520 // copy one dictionary file name into buf
1521 if (flags == DICT_EXACT)
1522 {
1523 count = 1;
1524 files = &dict;
1525 }
1526 else
1527 {
1528 // Expand wildcards in the dictionary name, but do not allow
1529 // backticks (for security, the 'dict' option may have been set in
1530 // a modeline).
1531 copy_option_part(&dict, buf, LSIZE, ",");
1532# ifdef FEAT_SPELL
1533 if (!thesaurus && STRCMP(buf, "spell") == 0)
1534 count = -1;
1535 else
1536# endif
1537 if (vim_strchr(buf, '`') != NULL
1538 || expand_wildcards(1, &buf, &count, &files,
1539 EW_FILE|EW_SILENT) != OK)
1540 count = 0;
1541 }
1542
1543# ifdef FEAT_SPELL
1544 if (count == -1)
1545 {
1546 // Complete from active spelling. Skip "\<" in the pattern, we
1547 // don't use it as a RE.
1548 if (pat[0] == '\\' && pat[1] == '<')
1549 ptr = pat + 2;
1550 else
1551 ptr = pat;
1552 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1553 }
1554 else
1555# endif
1556 if (count > 0) // avoid warning for using "files" uninit
1557 {
1558 ins_compl_files(count, files, thesaurus, flags,
1559 &regmatch, buf, &dir);
1560 if (flags != DICT_EXACT)
1561 FreeWild(count, files);
1562 }
1563 if (flags != 0)
1564 break;
1565 }
1566
1567theend:
1568 p_scs = save_p_scs;
1569 vim_regfree(regmatch.regprog);
1570 vim_free(buf);
1571}
1572
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001573/*
1574 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1575 * skipping the word at 'skip_word'. Returns OK on success.
1576 */
1577 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001578thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001579 char_u *fname,
1580 char_u **buf_arg,
1581 int dir,
1582 char_u *skip_word)
1583{
1584 int status = OK;
1585 char_u *ptr;
1586 char_u *wstart;
1587
1588 // Add the other matches on the line
1589 ptr = *buf_arg;
1590 while (!got_int)
1591 {
1592 // Find start of the next word. Skip white
1593 // space and punctuation.
1594 ptr = find_word_start(ptr);
1595 if (*ptr == NUL || *ptr == NL)
1596 break;
1597 wstart = ptr;
1598
1599 // Find end of the word.
1600 if (has_mbyte)
1601 // Japanese words may have characters in
1602 // different classes, only separate words
1603 // with single-byte non-word characters.
1604 while (*ptr != NUL)
1605 {
1606 int l = (*mb_ptr2len)(ptr);
1607
1608 if (l < 2 && !vim_iswordc(*ptr))
1609 break;
1610 ptr += l;
1611 }
1612 else
1613 ptr = find_word_end(ptr);
1614
1615 // Add the word. Skip the regexp match.
1616 if (wstart != skip_word)
1617 {
1618 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1619 fname, dir, FALSE);
1620 if (status == FAIL)
1621 break;
1622 }
1623 }
1624
1625 *buf_arg = ptr;
1626 return status;
1627}
1628
1629/*
1630 * Process "count" dictionary/thesaurus "files" and add the text matching
1631 * "regmatch".
1632 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001633 static void
1634ins_compl_files(
1635 int count,
1636 char_u **files,
1637 int thesaurus,
1638 int flags,
1639 regmatch_T *regmatch,
1640 char_u *buf,
1641 int *dir)
1642{
1643 char_u *ptr;
1644 int i;
1645 FILE *fp;
1646 int add_r;
1647
1648 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1649 {
1650 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001651 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001652 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001653 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001654 vim_snprintf((char *)IObuff, IOSIZE,
1655 _("Scanning dictionary: %s"), (char *)files[i]);
1656 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1657 }
1658
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001659 if (fp == NULL)
1660 continue;
1661
1662 // Read dictionary file line by line.
1663 // Check each line for a match.
1664 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001665 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001666 ptr = buf;
1667 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001668 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001669 ptr = regmatch->startp[0];
1670 if (ctrl_x_mode_line_or_eval())
1671 ptr = find_line_end(ptr);
1672 else
1673 ptr = find_word_end(ptr);
1674 add_r = ins_compl_add_infercase(regmatch->startp[0],
1675 (int)(ptr - regmatch->startp[0]),
1676 p_ic, files[i], *dir, FALSE);
1677 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001678 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001679 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001680 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001681 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001682 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001683 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001684 if (add_r == OK)
1685 // if dir was BACKWARD then honor it just once
1686 *dir = FORWARD;
1687 else if (add_r == FAIL)
1688 break;
1689 // avoid expensive call to vim_regexec() when at end
1690 // of line
1691 if (*ptr == '\n' || got_int)
1692 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001693 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001694 line_breakcheck();
1695 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001696 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001697 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001698 }
1699}
1700
1701/*
1702 * Find the start of the next word.
1703 * Returns a pointer to the first char of the word. Also stops at a NUL.
1704 */
1705 char_u *
1706find_word_start(char_u *ptr)
1707{
1708 if (has_mbyte)
1709 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1710 ptr += (*mb_ptr2len)(ptr);
1711 else
1712 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1713 ++ptr;
1714 return ptr;
1715}
1716
1717/*
1718 * Find the end of the word. Assumes it starts inside a word.
1719 * Returns a pointer to just after the word.
1720 */
1721 char_u *
1722find_word_end(char_u *ptr)
1723{
1724 int start_class;
1725
1726 if (has_mbyte)
1727 {
1728 start_class = mb_get_class(ptr);
1729 if (start_class > 1)
1730 while (*ptr != NUL)
1731 {
1732 ptr += (*mb_ptr2len)(ptr);
1733 if (mb_get_class(ptr) != start_class)
1734 break;
1735 }
1736 }
1737 else
1738 while (vim_iswordc(*ptr))
1739 ++ptr;
1740 return ptr;
1741}
1742
1743/*
1744 * Find the end of the line, omitting CR and NL at the end.
1745 * Returns a pointer to just after the line.
1746 */
1747 static char_u *
1748find_line_end(char_u *ptr)
1749{
1750 char_u *s;
1751
1752 s = ptr + STRLEN(ptr);
1753 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1754 --s;
1755 return s;
1756}
1757
1758/*
1759 * Free the list of completions
1760 */
1761 static void
1762ins_compl_free(void)
1763{
1764 compl_T *match;
1765 int i;
1766
1767 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001768 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001769 VIM_CLEAR(compl_leader);
1770
1771 if (compl_first_match == NULL)
1772 return;
1773
1774 ins_compl_del_pum();
1775 pum_clear();
1776
1777 compl_curr_match = compl_first_match;
1778 do
1779 {
1780 match = compl_curr_match;
1781 compl_curr_match = compl_curr_match->cp_next;
1782 vim_free(match->cp_str);
1783 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001784 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001785 vim_free(match->cp_fname);
1786 for (i = 0; i < CPT_COUNT; ++i)
1787 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001788#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001789 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001790#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001791 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001792 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001793 compl_first_match = compl_curr_match = NULL;
1794 compl_shown_match = NULL;
1795 compl_old_match = NULL;
1796}
1797
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001798/*
1799 * Reset/clear the completion state.
1800 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001801 void
1802ins_compl_clear(void)
1803{
1804 compl_cont_status = 0;
1805 compl_started = FALSE;
1806 compl_matches = 0;
1807 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001808 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001809 VIM_CLEAR(compl_leader);
1810 edit_submode_extra = NULL;
1811 VIM_CLEAR(compl_orig_text);
1812 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001813#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001814 // clear v:completed_item
1815 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001816#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001817}
1818
1819/*
1820 * Return TRUE when Insert completion is active.
1821 */
1822 int
1823ins_compl_active(void)
1824{
1825 return compl_started;
1826}
1827
1828/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 * Selected one of the matches. When FALSE the match was edited or using the
1830 * longest common string.
1831 */
1832 int
1833ins_compl_used_match(void)
1834{
1835 return compl_used_match;
1836}
1837
1838/*
1839 * Initialize get longest common string.
1840 */
1841 void
1842ins_compl_init_get_longest(void)
1843{
1844 compl_get_longest = FALSE;
1845}
1846
1847/*
1848 * Returns TRUE when insert completion is interrupted.
1849 */
1850 int
1851ins_compl_interrupted(void)
1852{
1853 return compl_interrupted;
1854}
1855
1856/*
1857 * Returns TRUE if the <Enter> key selects a match in the completion popup
1858 * menu.
1859 */
1860 int
1861ins_compl_enter_selects(void)
1862{
1863 return compl_enter_selects;
1864}
1865
1866/*
1867 * Return the column where the text starts that is being completed
1868 */
1869 colnr_T
1870ins_compl_col(void)
1871{
1872 return compl_col;
1873}
1874
1875/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001876 * Return the length in bytes of the text being completed
1877 */
1878 int
1879ins_compl_len(void)
1880{
1881 return compl_length;
1882}
1883
1884/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001885 * Delete one character before the cursor and show the subset of the matches
1886 * that match the word that is now before the cursor.
1887 * Returns the character to be used, NUL if the work is done and another char
1888 * to be got from the user.
1889 */
1890 int
1891ins_compl_bs(void)
1892{
1893 char_u *line;
1894 char_u *p;
1895
1896 line = ml_get_curline();
1897 p = line + curwin->w_cursor.col;
1898 MB_PTR_BACK(line, p);
1899
1900 // Stop completion when the whole word was deleted. For Omni completion
1901 // allow the word to be deleted, we won't match everything.
1902 // Respect the 'backspace' option.
1903 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001904 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1905 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001906 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1907 - compl_length < 0))
1908 return K_BS;
1909
1910 // Deleted more than what was used to find matches or didn't finish
1911 // finding all matches: need to look for matches all over again.
1912 if (curwin->w_cursor.col <= compl_col + compl_length
1913 || ins_compl_need_restart())
1914 ins_compl_restart();
1915
1916 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001917 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001918 if (compl_leader == NULL)
1919 return K_BS;
1920
1921 ins_compl_new_leader();
1922 if (compl_shown_match != NULL)
1923 // Make sure current match is not a hidden item.
1924 compl_curr_match = compl_shown_match;
1925 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001926}
1927
1928/*
1929 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1930 * be called.
1931 */
1932 static int
1933ins_compl_need_restart(void)
1934{
1935 // Return TRUE if we didn't complete finding matches or when the
1936 // 'completefunc' returned "always" in the "refresh" dictionary item.
1937 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001938 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001939 && compl_opt_refresh_always);
1940}
1941
1942/*
1943 * Called after changing "compl_leader".
1944 * Show the popup menu with a different set of matches.
1945 * May also search for matches again if the previous search was interrupted.
1946 */
1947 static void
1948ins_compl_new_leader(void)
1949{
1950 ins_compl_del_pum();
1951 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001952 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001953 compl_used_match = FALSE;
1954
1955 if (compl_started)
1956 ins_compl_set_original_text(compl_leader);
1957 else
1958 {
1959#ifdef FEAT_SPELL
1960 spell_bad_len = 0; // need to redetect bad word
1961#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001962 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001963 // the popup menu display the changed text before the cursor. Set
1964 // "compl_restarting" to avoid that the first match is inserted.
1965 pum_call_update_screen();
1966#ifdef FEAT_GUI
1967 if (gui.in_use)
1968 {
1969 // Show the cursor after the match, not after the redrawn text.
1970 setcursor();
1971 out_flush_cursor(FALSE, FALSE);
1972 }
1973#endif
1974 compl_restarting = TRUE;
1975 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1976 compl_cont_status = 0;
1977 compl_restarting = FALSE;
1978 }
1979
1980 compl_enter_selects = !compl_used_match;
1981
1982 // Show the popup menu with a different set of matches.
1983 ins_compl_show_pum();
1984
1985 // Don't let Enter select the original text when there is no popup menu.
1986 if (compl_match_array == NULL)
1987 compl_enter_selects = FALSE;
1988}
1989
1990/*
1991 * Return the length of the completion, from the completion start column to
1992 * the cursor column. Making sure it never goes below zero.
1993 */
1994 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001995get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996{
1997 int off = (int)curwin->w_cursor.col - (int)compl_col;
1998
1999 if (off < 0)
2000 return 0;
2001 return off;
2002}
2003
2004/*
2005 * Append one character to the match leader. May reduce the number of
2006 * matches.
2007 */
2008 void
2009ins_compl_addleader(int c)
2010{
2011 int cc;
2012
2013 if (stop_arrow() == FAIL)
2014 return;
2015 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2016 {
2017 char_u buf[MB_MAXBYTES + 1];
2018
2019 (*mb_char2bytes)(c, buf);
2020 buf[cc] = NUL;
2021 ins_char_bytes(buf, cc);
2022 if (compl_opt_refresh_always)
2023 AppendToRedobuff(buf);
2024 }
2025 else
2026 {
2027 ins_char(c);
2028 if (compl_opt_refresh_always)
2029 AppendCharToRedobuff(c);
2030 }
2031
2032 // If we didn't complete finding matches we must search again.
2033 if (ins_compl_need_restart())
2034 ins_compl_restart();
2035
2036 // When 'always' is set, don't reset compl_leader. While completing,
2037 // cursor doesn't point original position, changing compl_leader would
2038 // break redo.
2039 if (!compl_opt_refresh_always)
2040 {
2041 vim_free(compl_leader);
2042 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002043 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002044 if (compl_leader != NULL)
2045 ins_compl_new_leader();
2046 }
2047}
2048
2049/*
2050 * Setup for finding completions again without leaving CTRL-X mode. Used when
2051 * BS or a key was typed while still searching for matches.
2052 */
2053 static void
2054ins_compl_restart(void)
2055{
2056 ins_compl_free();
2057 compl_started = FALSE;
2058 compl_matches = 0;
2059 compl_cont_status = 0;
2060 compl_cont_mode = 0;
2061}
2062
2063/*
2064 * Set the first match, the original text.
2065 */
2066 static void
2067ins_compl_set_original_text(char_u *str)
2068{
2069 char_u *p;
2070
2071 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002072 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2073 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002074 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002075 {
2076 p = vim_strsave(str);
2077 if (p != NULL)
2078 {
2079 vim_free(compl_first_match->cp_str);
2080 compl_first_match->cp_str = p;
2081 }
2082 }
2083 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002084 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002085 {
2086 p = vim_strsave(str);
2087 if (p != NULL)
2088 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002089 vim_free(compl_first_match->cp_prev->cp_str);
2090 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002091 }
2092 }
2093}
2094
2095/*
2096 * Append one character to the match leader. May reduce the number of
2097 * matches.
2098 */
2099 void
2100ins_compl_addfrommatch(void)
2101{
2102 char_u *p;
2103 int len = (int)curwin->w_cursor.col - (int)compl_col;
2104 int c;
2105 compl_T *cp;
2106
2107 p = compl_shown_match->cp_str;
2108 if ((int)STRLEN(p) <= len) // the match is too short
2109 {
2110 // When still at the original match use the first entry that matches
2111 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002112 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002113 return;
2114
2115 p = NULL;
2116 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002117 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002118 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002119 if (compl_leader == NULL
2120 || ins_compl_equal(cp, compl_leader,
2121 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002122 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002123 p = cp->cp_str;
2124 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002125 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002126 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002127 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002128 return;
2129 }
2130 p += len;
2131 c = PTR2CHAR(p);
2132 ins_compl_addleader(c);
2133}
2134
2135/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002136 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002137 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2138 * compl_cont_mode and compl_cont_status.
2139 * Returns TRUE when the character is not to be inserted.
2140 */
2141 static int
2142set_ctrl_x_mode(int c)
2143{
2144 int retval = FALSE;
2145
2146 switch (c)
2147 {
2148 case Ctrl_E:
2149 case Ctrl_Y:
2150 // scroll the window one line up or down
2151 ctrl_x_mode = CTRL_X_SCROLL;
2152 if (!(State & REPLACE_FLAG))
2153 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2154 else
2155 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2156 edit_submode_pre = NULL;
2157 showmode();
2158 break;
2159 case Ctrl_L:
2160 // complete whole line
2161 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2162 break;
2163 case Ctrl_F:
2164 // complete filenames
2165 ctrl_x_mode = CTRL_X_FILES;
2166 break;
2167 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002168 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002169 ctrl_x_mode = CTRL_X_DICTIONARY;
2170 break;
2171 case Ctrl_R:
2172 // Register insertion without exiting CTRL-X mode
2173 // Simply allow ^R to happen without affecting ^X mode
2174 break;
2175 case Ctrl_T:
2176 // complete words from a thesaurus
2177 ctrl_x_mode = CTRL_X_THESAURUS;
2178 break;
2179#ifdef FEAT_COMPL_FUNC
2180 case Ctrl_U:
2181 // user defined completion
2182 ctrl_x_mode = CTRL_X_FUNCTION;
2183 break;
2184 case Ctrl_O:
2185 // omni completion
2186 ctrl_x_mode = CTRL_X_OMNI;
2187 break;
2188#endif
2189 case 's':
2190 case Ctrl_S:
2191 // complete spelling suggestions
2192 ctrl_x_mode = CTRL_X_SPELL;
2193#ifdef FEAT_SPELL
2194 ++emsg_off; // Avoid getting the E756 error twice.
2195 spell_back_to_badword();
2196 --emsg_off;
2197#endif
2198 break;
2199 case Ctrl_RSB:
2200 // complete tag names
2201 ctrl_x_mode = CTRL_X_TAGS;
2202 break;
2203#ifdef FEAT_FIND_ID
2204 case Ctrl_I:
2205 case K_S_TAB:
2206 // complete keywords from included files
2207 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2208 break;
2209 case Ctrl_D:
2210 // complete definitions from included files
2211 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2212 break;
2213#endif
2214 case Ctrl_V:
2215 case Ctrl_Q:
2216 // complete vim commands
2217 ctrl_x_mode = CTRL_X_CMDLINE;
2218 break;
2219 case Ctrl_Z:
2220 // stop completion
2221 ctrl_x_mode = CTRL_X_NORMAL;
2222 edit_submode = NULL;
2223 showmode();
2224 retval = TRUE;
2225 break;
2226 case Ctrl_P:
2227 case Ctrl_N:
2228 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2229 // just started ^X mode, or there were enough ^X's to cancel
2230 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2231 // do normal expansion when interrupting a different mode (say
2232 // ^X^F^X^P or ^P^X^X^P, see below)
2233 // nothing changes if interrupting mode 0, (eg, the flag
2234 // doesn't change when going to ADDING mode -- Acevedo
2235 if (!(compl_cont_status & CONT_INTRPT))
2236 compl_cont_status |= CONT_LOCAL;
2237 else if (compl_cont_mode != 0)
2238 compl_cont_status &= ~CONT_LOCAL;
2239 // FALLTHROUGH
2240 default:
2241 // If we have typed at least 2 ^X's... for modes != 0, we set
2242 // compl_cont_status = 0 (eg, as if we had just started ^X
2243 // mode).
2244 // For mode 0, we set "compl_cont_mode" to an impossible
2245 // value, in both cases ^X^X can be used to restart the same
2246 // mode (avoiding ADDING mode).
2247 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2248 // 'complete' and local ^P expansions respectively.
2249 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2250 // mode -- Acevedo
2251 if (c == Ctrl_X)
2252 {
2253 if (compl_cont_mode != 0)
2254 compl_cont_status = 0;
2255 else
2256 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2257 }
2258 ctrl_x_mode = CTRL_X_NORMAL;
2259 edit_submode = NULL;
2260 showmode();
2261 break;
2262 }
2263
2264 return retval;
2265}
2266
2267/*
2268 * Stop insert completion mode
2269 */
2270 static int
2271ins_compl_stop(int c, int prev_mode, int retval)
2272{
2273 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002274 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002275
2276 // Get here when we have finished typing a sequence of ^N and
2277 // ^P or other completion characters in CTRL-X mode. Free up
2278 // memory that was used, and make sure we can redo the insert.
2279 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2280 {
2281 // If any of the original typed text has been changed, eg when
2282 // ignorecase is set, we must add back-spaces to the redo
2283 // buffer. We add as few as necessary to delete just the part
2284 // of the original text that has changed.
2285 // When using the longest match, edited the match or used
2286 // CTRL-E then don't use the current match.
2287 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2288 ptr = compl_curr_match->cp_str;
2289 else
2290 ptr = NULL;
2291 ins_compl_fixRedoBufForLeader(ptr);
2292 }
2293
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002294 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002295
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002296 // When completing whole lines: fix indent for 'cindent'.
2297 // Otherwise, break line if it's too long.
2298 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2299 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002300 // re-indent the current line
2301 if (want_cindent)
2302 {
2303 do_c_expr_indent();
2304 want_cindent = FALSE; // don't do it again
2305 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002306 }
2307 else
2308 {
2309 int prev_col = curwin->w_cursor.col;
2310
2311 // put the cursor on the last char, for 'tw' formatting
2312 if (prev_col > 0)
2313 dec_cursor();
2314 // only format when something was inserted
2315 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2316 insertchar(NUL, 0, -1);
2317 if (prev_col > 0
2318 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2319 inc_cursor();
2320 }
2321
2322 // If the popup menu is displayed pressing CTRL-Y means accepting
2323 // the selection without inserting anything. When
2324 // compl_enter_selects is set the Enter key does the same.
2325 if ((c == Ctrl_Y || (compl_enter_selects
2326 && (c == CAR || c == K_KENTER || c == NL)))
2327 && pum_visible())
2328 retval = TRUE;
2329
2330 // CTRL-E means completion is Ended, go back to the typed text.
2331 // but only do this, if the Popup is still visible
2332 if (c == Ctrl_E)
2333 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002334 char_u *p = NULL;
2335
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002336 ins_compl_delete();
2337 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002338 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002339 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002340 p = compl_orig_text;
2341 if (p != NULL)
2342 {
2343 int compl_len = get_compl_len();
2344 int len = (int)STRLEN(p);
2345
2346 if (len > compl_len)
2347 ins_bytes_len(p + compl_len, len - compl_len);
2348 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002349 retval = TRUE;
2350 }
2351
2352 auto_format(FALSE, TRUE);
2353
2354 // Trigger the CompleteDonePre event to give scripts a chance to
2355 // act upon the completion before clearing the info, and restore
2356 // ctrl_x_mode, so that complete_info() can be used.
2357 ctrl_x_mode = prev_mode;
2358 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2359
2360 ins_compl_free();
2361 compl_started = FALSE;
2362 compl_matches = 0;
2363 if (!shortmess(SHM_COMPLETIONMENU))
2364 msg_clr_cmdline(); // necessary for "noshowmode"
2365 ctrl_x_mode = CTRL_X_NORMAL;
2366 compl_enter_selects = FALSE;
2367 if (edit_submode != NULL)
2368 {
2369 edit_submode = NULL;
2370 showmode();
2371 }
2372
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002373 if (c == Ctrl_C && cmdwin_type != 0)
2374 // Avoid the popup menu remains displayed when leaving the
2375 // command line window.
2376 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002377 // Indent now if a key was typed that is in 'cinkeys'.
2378 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2379 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002380 // Trigger the CompleteDone event to give scripts a chance to act
2381 // upon the end of completion.
2382 ins_apply_autocmds(EVENT_COMPLETEDONE);
2383
2384 return retval;
2385}
2386
2387/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002388 * Prepare for Insert mode completion, or stop it.
2389 * Called just after typing a character in Insert mode.
2390 * Returns TRUE when the character is not to be inserted;
2391 */
2392 int
2393ins_compl_prep(int c)
2394{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002395 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002396 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002397
2398 // Forget any previous 'special' messages if this is actually
2399 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2400 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2401 edit_submode_extra = NULL;
2402
zeertzjq440d4cb2023-03-02 17:51:32 +00002403 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002404 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002405 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002406 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002407 return retval;
2408
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002409#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002410 // Ignore mouse events in a popup window
2411 if (is_mouse_key(c))
2412 {
2413 // Ignore drag and release events, the position does not need to be in
2414 // the popup and it may have just closed.
2415 if (c == K_LEFTRELEASE
2416 || c == K_LEFTRELEASE_NM
2417 || c == K_MIDDLERELEASE
2418 || c == K_RIGHTRELEASE
2419 || c == K_X1RELEASE
2420 || c == K_X2RELEASE
2421 || c == K_LEFTDRAG
2422 || c == K_MIDDLEDRAG
2423 || c == K_RIGHTDRAG
2424 || c == K_X1DRAG
2425 || c == K_X2DRAG)
2426 return retval;
2427 if (popup_visible)
2428 {
2429 int row = mouse_row;
2430 int col = mouse_col;
2431 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2432
2433 if (wp != NULL && WIN_IS_POPUP(wp))
2434 return retval;
2435 }
2436 }
2437#endif
2438
zeertzjqdca29d92021-08-31 19:12:51 +02002439 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2440 {
2441 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2442 || !vim_is_ctrl_x_key(c))
2443 {
2444 // Not starting another completion mode.
2445 ctrl_x_mode = CTRL_X_CMDLINE;
2446
2447 // CTRL-X CTRL-Z should stop completion without inserting anything
2448 if (c == Ctrl_Z)
2449 retval = TRUE;
2450 }
2451 else
2452 {
2453 ctrl_x_mode = CTRL_X_CMDLINE;
2454
2455 // Other CTRL-X keys first stop completion, then start another
2456 // completion mode.
2457 ins_compl_prep(' ');
2458 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2459 }
2460 }
2461
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002462 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002463 if (ctrl_x_mode_not_defined_yet()
2464 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002465 {
bfredl87af60c2022-09-24 11:17:51 +01002466 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002467 compl_used_match = TRUE;
2468
2469 }
2470
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002471 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002472 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2473 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002474 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002475 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002476 {
2477 // We're already in CTRL-X mode, do we stay in it?
2478 if (!vim_is_ctrl_x_key(c))
2479 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002480 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002481 ctrl_x_mode = CTRL_X_NORMAL;
2482 else
2483 ctrl_x_mode = CTRL_X_FINISHED;
2484 edit_submode = NULL;
2485 }
2486 showmode();
2487 }
2488
2489 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2490 {
2491 // Show error message from attempted keyword completion (probably
2492 // 'Pattern not found') until another key is hit, then go back to
2493 // showing what mode we are in.
2494 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002495 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002496 && c != Ctrl_R && !ins_compl_pum_key(c))
2497 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002498 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002499 }
2500 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2501 // Trigger the CompleteDone event to give scripts a chance to act
2502 // upon the (possibly failed) completion.
2503 ins_apply_autocmds(EVENT_COMPLETEDONE);
2504
LemonBoy2bf52dd2022-04-09 18:17:34 +01002505 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002506
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002507 // reset continue_* if we left expansion-mode, if we stay they'll be
2508 // (re)set properly in ins_complete()
2509 if (!vim_is_ctrl_x_key(c))
2510 {
2511 compl_cont_status = 0;
2512 compl_cont_mode = 0;
2513 }
2514
2515 return retval;
2516}
2517
2518/*
2519 * Fix the redo buffer for the completion leader replacing some of the typed
2520 * text. This inserts backspaces and appends the changed text.
2521 * "ptr" is the known leader text or NUL.
2522 */
2523 static void
2524ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2525{
2526 int len;
2527 char_u *p;
2528 char_u *ptr = ptr_arg;
2529
2530 if (ptr == NULL)
2531 {
2532 if (compl_leader != NULL)
2533 ptr = compl_leader;
2534 else
2535 return; // nothing to do
2536 }
2537 if (compl_orig_text != NULL)
2538 {
2539 p = compl_orig_text;
2540 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2541 ;
2542 if (len > 0)
2543 len -= (*mb_head_off)(p, p + len);
2544 for (p += len; *p != NUL; MB_PTR_ADV(p))
2545 AppendCharToRedobuff(K_BS);
2546 }
2547 else
2548 len = 0;
2549 if (ptr != NULL)
2550 AppendToRedobuffLit(ptr + len, -1);
2551}
2552
2553/*
2554 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2555 * (depending on flag) starting from buf and looking for a non-scanned
2556 * buffer (other than curbuf). curbuf is special, if it is called with
2557 * buf=curbuf then it has to be the first call for a given flag/expansion.
2558 *
2559 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2560 */
2561 static buf_T *
2562ins_compl_next_buf(buf_T *buf, int flag)
2563{
2564 static win_T *wp = NULL;
2565
2566 if (flag == 'w') // just windows
2567 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002568 if (buf == curbuf || !win_valid(wp))
2569 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002570 wp = curwin;
2571 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2572 && wp->w_buffer->b_scanned)
2573 ;
2574 buf = wp->w_buffer;
2575 }
2576 else
2577 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2578 // (unlisted buffers)
2579 // When completing whole lines skip unloaded buffers.
2580 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2581 && ((flag == 'U'
2582 ? buf->b_p_bl
2583 : (!buf->b_p_bl
2584 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2585 || buf->b_scanned))
2586 ;
2587 return buf;
2588}
2589
2590#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002591
2592# ifdef FEAT_EVAL
2593static callback_T cfu_cb; // 'completefunc' callback function
2594static callback_T ofu_cb; // 'omnifunc' callback function
2595static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2596# endif
2597
2598/*
2599 * Copy a global callback function to a buffer local callback.
2600 */
2601 static void
2602copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2603{
2604 free_callback(bufcb);
2605 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2606 copy_callback(bufcb, globcb);
2607}
2608
2609/*
2610 * Parse the 'completefunc' option value and set the callback function.
2611 * Invoked when the 'completefunc' option is set. The option value can be a
2612 * name of a function (string), or function(<name>) or funcref(<name>) or a
2613 * lambda expression.
2614 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002615 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002616did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002617{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002618 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2619 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002620
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002622
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002623 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002624}
2625
2626/*
2627 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002628 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002629 */
2630 void
2631set_buflocal_cfu_callback(buf_T *buf UNUSED)
2632{
2633# ifdef FEAT_EVAL
2634 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2635# endif
2636}
2637
2638/*
2639 * Parse the 'omnifunc' option value and set the callback function.
2640 * Invoked when the 'omnifunc' option is set. The option value can be a
2641 * name of a function (string), or function(<name>) or funcref(<name>) or a
2642 * lambda expression.
2643 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002644 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002645did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002646{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002647 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2648 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002649
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002650 set_buflocal_ofu_callback(curbuf);
2651 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652}
2653
2654/*
2655 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002656 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002657 */
2658 void
2659set_buflocal_ofu_callback(buf_T *buf UNUSED)
2660{
2661# ifdef FEAT_EVAL
2662 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2663# endif
2664}
2665
2666/*
2667 * Parse the 'thesaurusfunc' option value and set the callback function.
2668 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2669 * name of a function (string), or function(<name>) or funcref(<name>) or a
2670 * lambda expression.
2671 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002672 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002673did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002674{
2675 int retval;
2676
2677 if (*curbuf->b_p_tsrfu != NUL)
2678 {
2679 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002680 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2681 &curbuf->b_tsrfu_cb);
2682 }
2683 else
2684 {
2685 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002686 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2687 }
2688
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002689 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002690}
2691
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002692/*
2693 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002694 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002695 */
2696 int
2697set_ref_in_insexpand_funcs(int copyID)
2698{
2699 int abort = FALSE;
2700
2701 abort = set_ref_in_callback(&cfu_cb, copyID);
2702 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2703 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2704
2705 return abort;
2706}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002707
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002708/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002709 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002710 */
2711 static char_u *
2712get_complete_funcname(int type)
2713{
2714 switch (type)
2715 {
2716 case CTRL_X_FUNCTION:
2717 return curbuf->b_p_cfu;
2718 case CTRL_X_OMNI:
2719 return curbuf->b_p_ofu;
2720 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002721 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002722 default:
2723 return (char_u *)"";
2724 }
2725}
2726
2727/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002728 * Get the callback to use for insert mode completion.
2729 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002730 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002731get_insert_callback(int type)
2732{
2733 if (type == CTRL_X_FUNCTION)
2734 return &curbuf->b_cfu_cb;
2735 if (type == CTRL_X_OMNI)
2736 return &curbuf->b_ofu_cb;
2737 // CTRL_X_THESAURUS
2738 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2739}
2740
2741/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002742 * Execute user defined complete function 'completefunc', 'omnifunc' or
2743 * 'thesaurusfunc', and get matches in "matches".
2744 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002745 */
2746 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002747expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002748{
2749 list_T *matchlist = NULL;
2750 dict_T *matchdict = NULL;
2751 typval_T args[3];
2752 char_u *funcname;
2753 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002754 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002755 typval_T rettv;
2756 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002757 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002759 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002760 if (*funcname == NUL)
2761 return;
2762
2763 // Call 'completefunc' to obtain the list of matches.
2764 args[0].v_type = VAR_NUMBER;
2765 args[0].vval.v_number = 0;
2766 args[1].v_type = VAR_STRING;
2767 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2768 args[2].v_type = VAR_UNKNOWN;
2769
2770 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002771 // Lock the text to avoid weird things from happening. Also disallow
2772 // switching to another window, it should not be needed and may end up in
2773 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002774 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002775
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002776 cb = get_insert_callback(type);
2777 retval = call_callback(cb, 0, &rettv, 2, args);
2778
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002779 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002780 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002781 {
2782 switch (rettv.v_type)
2783 {
2784 case VAR_LIST:
2785 matchlist = rettv.vval.v_list;
2786 break;
2787 case VAR_DICT:
2788 matchdict = rettv.vval.v_dict;
2789 break;
2790 case VAR_SPECIAL:
2791 if (rettv.vval.v_number == VVAL_NONE)
2792 compl_opt_suppress_empty = TRUE;
2793 // FALLTHROUGH
2794 default:
2795 // TODO: Give error message?
2796 clear_tv(&rettv);
2797 break;
2798 }
2799 }
zeertzjqcfe45652022-05-27 17:26:55 +01002800 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002801
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002802 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002803 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002804 validate_cursor();
2805 if (!EQUAL_POS(curwin->w_cursor, pos))
2806 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002807 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002808 goto theend;
2809 }
2810
2811 if (matchlist != NULL)
2812 ins_compl_add_list(matchlist);
2813 else if (matchdict != NULL)
2814 ins_compl_add_dict(matchdict);
2815
2816theend:
2817 // Restore State, it might have been changed.
2818 State = save_State;
2819
2820 if (matchdict != NULL)
2821 dict_unref(matchdict);
2822 if (matchlist != NULL)
2823 list_unref(matchlist);
2824}
2825#endif // FEAT_COMPL_FUNC
2826
2827#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2828/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002829 * Add a match to the list of matches from a typeval_T.
2830 * If the given string is already in the list of completions, then return
2831 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2832 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002833 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002834 */
2835 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002836ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002837{
2838 char_u *word;
2839 int dup = FALSE;
2840 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002841 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002842 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002843 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002844 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002845
Bram Moolenaar08928322020-01-04 14:32:48 +01002846 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002847 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2848 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002849 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2850 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2851 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2852 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2853 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2854 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2855 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2856 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002857 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002858 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2859 dup = dict_get_number(tv->vval.v_dict, "dup");
2860 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2861 empty = dict_get_number(tv->vval.v_dict, "empty");
2862 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2863 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002864 flags |= CP_EQUAL;
2865 }
2866 else
2867 {
2868 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002869 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002870 }
2871 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002872 {
2873 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002874 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002875 }
2876 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2877 if (status != OK)
2878 clear_tv(&user_data);
2879 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002880}
2881
2882/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002883 * Add completions from a list.
2884 */
2885 static void
2886ins_compl_add_list(list_T *list)
2887{
2888 listitem_T *li;
2889 int dir = compl_direction;
2890
2891 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002892 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002893 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002894 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002895 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002896 // if dir was BACKWARD then honor it just once
2897 dir = FORWARD;
2898 else if (did_emsg)
2899 break;
2900 }
2901}
2902
2903/*
2904 * Add completions from a dict.
2905 */
2906 static void
2907ins_compl_add_dict(dict_T *dict)
2908{
2909 dictitem_T *di_refresh;
2910 dictitem_T *di_words;
2911
2912 // Check for optional "refresh" item.
2913 compl_opt_refresh_always = FALSE;
2914 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2915 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2916 {
2917 char_u *v = di_refresh->di_tv.vval.v_string;
2918
2919 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2920 compl_opt_refresh_always = TRUE;
2921 }
2922
2923 // Add completions from a "words" list.
2924 di_words = dict_find(dict, (char_u *)"words", 5);
2925 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2926 ins_compl_add_list(di_words->di_tv.vval.v_list);
2927}
2928
2929/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002930 * Start completion for the complete() function.
2931 * "startcol" is where the matched text starts (1 is first column).
2932 * "list" is the list of matches.
2933 */
2934 static void
2935set_completion(colnr_T startcol, list_T *list)
2936{
2937 int save_w_wrow = curwin->w_wrow;
2938 int save_w_leftcol = curwin->w_leftcol;
2939 int flags = CP_ORIGINAL_TEXT;
2940
2941 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002942 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002943 ins_compl_prep(' ');
2944 ins_compl_clear();
2945 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002946 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002947
2948 compl_direction = FORWARD;
2949 if (startcol > curwin->w_cursor.col)
2950 startcol = curwin->w_cursor.col;
2951 compl_col = startcol;
2952 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2953 // compl_pattern doesn't need to be set
2954 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2955 if (p_ic)
2956 flags |= CP_ICASE;
2957 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002958 -1, NULL, NULL, NULL, 0,
2959 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002960 return;
2961
2962 ctrl_x_mode = CTRL_X_EVAL;
2963
2964 ins_compl_add_list(list);
2965 compl_matches = ins_compl_make_cyclic();
2966 compl_started = TRUE;
2967 compl_used_match = TRUE;
2968 compl_cont_status = 0;
2969
2970 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002971 int no_select = compl_no_select || compl_longest;
2972 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002973 {
2974 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002975 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002976 // Down/Up has no real effect.
2977 ins_complete(K_UP, FALSE);
2978 }
2979 else
2980 ins_complete(Ctrl_N, FALSE);
2981 compl_enter_selects = compl_no_insert;
2982
2983 // Lazily show the popup menu, unless we got interrupted.
2984 if (!compl_interrupted)
2985 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002986 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002987 out_flush();
2988}
2989
2990/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002991 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002992 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002993 void
2994f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002995{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002996 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002997
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002998 if (in_vim9script()
2999 && (check_for_number_arg(argvars, 0) == FAIL
3000 || check_for_list_arg(argvars, 1) == FAIL))
3001 return;
3002
Bram Moolenaar24959102022-05-07 20:01:16 +01003003 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003004 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003005 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003006 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003007 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003008
3009 // Check for undo allowed here, because if something was already inserted
3010 // the line was already saved for undo and this check isn't done.
3011 if (!undo_allowed())
3012 return;
3013
Bram Moolenaard83392a2022-09-01 12:22:46 +01003014 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003015 {
3016 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3017 if (startcol > 0)
3018 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003019 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003020}
3021
3022/*
3023 * "complete_add()" function
3024 */
3025 void
3026f_complete_add(typval_T *argvars, typval_T *rettv)
3027{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003028 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003029 return;
3030
Bram Moolenaar440cf092021-04-03 20:13:30 +02003031 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003032}
3033
3034/*
3035 * "complete_check()" function
3036 */
3037 void
3038f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3039{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003040 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003041 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003042
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003043 ins_compl_check_keys(0, TRUE);
3044 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003045
3046 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003047}
3048
3049/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003050 * Return Insert completion mode name string
3051 */
3052 static char_u *
3053ins_compl_mode(void)
3054{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003055 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003056 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3057
3058 return (char_u *)"";
3059}
3060
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003061/*
3062 * Assign the sequence number to all the completion matches which don't have
3063 * one assigned yet.
3064 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003065 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003066ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003067{
3068 int number = 0;
3069 compl_T *match;
3070
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003071 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003072 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003073 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003074 // This should normally succeed already at the first loop
3075 // cycle, so it's fast!
3076 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003077 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003078 if (match->cp_number != -1)
3079 {
3080 number = match->cp_number;
3081 break;
3082 }
3083 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003084 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003085 for (match = match->cp_next;
3086 match != NULL && match->cp_number == -1;
3087 match = match->cp_next)
3088 match->cp_number = ++number;
3089 }
3090 else // BACKWARD
3091 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003092 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003093 // number. This should normally succeed already at the
3094 // first loop cycle, so it's fast!
3095 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003096 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003097 if (match->cp_number != -1)
3098 {
3099 number = match->cp_number;
3100 break;
3101 }
3102 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003103 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003104 for (match = match->cp_prev; match
3105 && match->cp_number == -1;
3106 match = match->cp_prev)
3107 match->cp_number = ++number;
3108 }
3109}
3110
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003111/*
3112 * Get complete information
3113 */
3114 static void
3115get_complete_info(list_T *what_list, dict_T *retdict)
3116{
3117 int ret = OK;
3118 listitem_T *item;
3119#define CI_WHAT_MODE 0x01
3120#define CI_WHAT_PUM_VISIBLE 0x02
3121#define CI_WHAT_ITEMS 0x04
3122#define CI_WHAT_SELECTED 0x08
3123#define CI_WHAT_INSERTED 0x10
3124#define CI_WHAT_ALL 0xff
3125 int what_flag;
3126
3127 if (what_list == NULL)
3128 what_flag = CI_WHAT_ALL;
3129 else
3130 {
3131 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003132 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003133 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003134 {
3135 char_u *what = tv_get_string(&item->li_tv);
3136
3137 if (STRCMP(what, "mode") == 0)
3138 what_flag |= CI_WHAT_MODE;
3139 else if (STRCMP(what, "pum_visible") == 0)
3140 what_flag |= CI_WHAT_PUM_VISIBLE;
3141 else if (STRCMP(what, "items") == 0)
3142 what_flag |= CI_WHAT_ITEMS;
3143 else if (STRCMP(what, "selected") == 0)
3144 what_flag |= CI_WHAT_SELECTED;
3145 else if (STRCMP(what, "inserted") == 0)
3146 what_flag |= CI_WHAT_INSERTED;
3147 }
3148 }
3149
3150 if (ret == OK && (what_flag & CI_WHAT_MODE))
3151 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3152
3153 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3154 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3155
Girish Palya8950bf72024-03-20 20:07:29 +01003156 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003157 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003158 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003159 dict_T *di;
3160 compl_T *match;
3161 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003162
Girish Palya8950bf72024-03-20 20:07:29 +01003163 if (what_flag & CI_WHAT_ITEMS)
3164 {
3165 li = list_alloc();
3166 if (li == NULL)
3167 return;
3168 ret = dict_add_list(retdict, "items", li);
3169 }
3170 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3171 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3172 ins_compl_update_sequence_numbers();
3173 if (ret == OK && compl_first_match != NULL)
3174 {
3175 int list_idx = 0;
3176 match = compl_first_match;
3177 do
3178 {
3179 if (!match_at_original_text(match))
3180 {
3181 if (what_flag & CI_WHAT_ITEMS)
3182 {
3183 di = dict_alloc();
3184 if (di == NULL)
3185 return;
3186 ret = list_append_dict(li, di);
3187 if (ret != OK)
3188 return;
3189 dict_add_string(di, "word", match->cp_str);
3190 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3191 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3192 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3193 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3194 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3195 // Add an empty string for backwards compatibility
3196 dict_add_string(di, "user_data", (char_u *)"");
3197 else
3198 dict_add_tv(di, "user_data", &match->cp_user_data);
3199 }
3200 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3201 selected_idx = list_idx;
3202 list_idx += 1;
3203 }
3204 match = match->cp_next;
3205 }
3206 while (match != NULL && !is_first_match(match));
3207 }
3208 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3209 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003210 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003211
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003212 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3213 {
3214 // TODO
3215 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003216}
3217
3218/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003219 * "complete_info()" function
3220 */
3221 void
3222f_complete_info(typval_T *argvars, typval_T *rettv)
3223{
3224 list_T *what_list = NULL;
3225
Bram Moolenaar93a10962022-06-16 11:42:09 +01003226 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003227 return;
3228
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003229 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3230 return;
3231
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003232 if (argvars[0].v_type != VAR_UNKNOWN)
3233 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003234 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003235 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003236 what_list = argvars[0].vval.v_list;
3237 }
3238 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003239}
3240#endif
3241
3242/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003243 * Returns TRUE when using a user-defined function for thesaurus completion.
3244 */
3245 static int
3246thesaurus_func_complete(int type UNUSED)
3247{
3248#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003249 return type == CTRL_X_THESAURUS
3250 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003251#else
3252 return FALSE;
3253#endif
3254}
3255
3256/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003257 * Return value of process_next_cpt_value()
3258 */
3259enum
3260{
3261 INS_COMPL_CPT_OK = 1,
3262 INS_COMPL_CPT_CONT,
3263 INS_COMPL_CPT_END
3264};
3265
3266/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003267 * state information used for getting the next set of insert completion
3268 * matches.
3269 */
3270typedef struct
3271{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003272 char_u *e_cpt_copy; // copy of 'complete'
3273 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003274 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003275 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003276 pos_T prev_match_pos; // previous match position
3277 int set_match_pos; // save first_match_pos/last_match_pos
3278 pos_T first_match_pos; // first match position
3279 pos_T last_match_pos; // last match position
3280 int found_all; // found all matches of a certain type.
3281 char_u *dict; // dictionary file to search
3282 int dict_f; // "dict" is an exact file name or not
3283} ins_compl_next_state_T;
3284
3285/*
3286 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003287 *
3288 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003289 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003290 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003291 * st->found_all - all matches of this type are found
3292 * st->ins_buf - search for completions in this buffer
3293 * st->first_match_pos - position of the first completion match
3294 * st->last_match_pos - position of the last completion match
3295 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003296 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003297 * st->dict - name of the dictionary or thesaurus file to search
3298 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003299 *
3300 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003301 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3302 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003303 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003304 */
3305 static int
3306process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003307 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003308 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003309 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003310{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003311 int compl_type = -1;
3312 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003313
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003314 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003315
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003316 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3317 st->e_cpt++;
3318
3319 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003321 st->ins_buf = curbuf;
3322 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003323 // Move the cursor back one character so that ^N can match the
3324 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003325 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 {
3327 // Move the cursor to after the last character in the
3328 // buffer, so that word at start of buffer is found
3329 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003330 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003331 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003332 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 compl_type = 0;
3335
3336 // Remember the first match so that the loop stops when we
3337 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003338 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003339 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003340 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003341 && (st->ins_buf = ins_compl_next_buf(
3342 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003343 {
3344 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003345 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003346 {
3347 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003348 st->first_match_pos.col = st->last_match_pos.col = 0;
3349 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3350 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003351 compl_type = 0;
3352 }
3353 else // unloaded buffer, scan like dictionary
3354 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003355 st->found_all = TRUE;
3356 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003357 {
3358 status = INS_COMPL_CPT_CONT;
3359 goto done;
3360 }
3361 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003362 st->dict = st->ins_buf->b_fname;
3363 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003364 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003365 if (!shortmess(SHM_COMPLETIONSCAN))
3366 {
3367 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3368 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3369 st->ins_buf->b_fname == NULL
3370 ? buf_spname(st->ins_buf)
3371 : st->ins_buf->b_sfname == NULL
3372 ? st->ins_buf->b_fname
3373 : st->ins_buf->b_sfname);
3374 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3375 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003376 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003377 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003378 status = INS_COMPL_CPT_END;
3379 else
3380 {
3381 if (ctrl_x_mode_line_or_eval())
3382 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003383 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003384 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003385 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003386 compl_type = CTRL_X_DICTIONARY;
3387 else
3388 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003389 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003390 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003391 st->dict = st->e_cpt;
3392 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003393 }
3394 }
3395#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003396 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003397 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003398 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003399 compl_type = CTRL_X_PATH_DEFINES;
3400#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003401 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003402 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003404 if (!shortmess(SHM_COMPLETIONSCAN))
3405 {
3406 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3407 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3408 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3409 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003410 }
3411 else
3412 compl_type = -1;
3413
3414 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003415 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003416
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003417 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003418 if (compl_type == -1)
3419 status = INS_COMPL_CPT_CONT;
3420 }
3421
3422done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003423 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003424 return status;
3425}
3426
3427#ifdef FEAT_FIND_ID
3428/*
3429 * Get the next set of identifiers or defines matching "compl_pattern" in
3430 * included files.
3431 */
3432 static void
3433get_next_include_file_completion(int compl_type)
3434{
3435 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003436 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003437 (compl_type == CTRL_X_PATH_DEFINES
3438 && !(compl_cont_status & CONT_SOL))
3439 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003440 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003441}
3442#endif
3443
3444/*
3445 * Get the next set of words matching "compl_pattern" in dictionary or
3446 * thesaurus files.
3447 */
3448 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003449get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003450{
3451#ifdef FEAT_COMPL_FUNC
3452 if (thesaurus_func_complete(compl_type))
3453 expand_by_function(compl_type, compl_pattern);
3454 else
3455#endif
3456 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003457 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003458 : (compl_type == CTRL_X_THESAURUS
3459 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3460 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3461 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003462 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003463 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003464}
3465
3466/*
3467 * Get the next set of tag names matching "compl_pattern".
3468 */
3469 static void
3470get_next_tag_completion(void)
3471{
3472 int save_p_ic;
3473 char_u **matches;
3474 int num_matches;
3475
3476 // set p_ic according to p_ic, p_scs and pat for find_tags().
3477 save_p_ic = p_ic;
3478 p_ic = ignorecase(compl_pattern);
3479
3480 // Find up to TAG_MANY matches. Avoids that an enormous number
3481 // of matches is found when compl_pattern is empty
3482 g_tag_at_cursor = TRUE;
3483 if (find_tags(compl_pattern, &num_matches, &matches,
3484 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003485 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003486 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3487 ins_compl_add_matches(num_matches, matches, p_ic);
3488 g_tag_at_cursor = FALSE;
3489 p_ic = save_p_ic;
3490}
3491
3492/*
3493 * Get the next set of filename matching "compl_pattern".
3494 */
3495 static void
3496get_next_filename_completion(void)
3497{
3498 char_u **matches;
3499 int num_matches;
3500
3501 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3502 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3503 return;
3504
3505 // May change home directory back to "~".
3506 tilde_replace(compl_pattern, num_matches, matches);
3507#ifdef BACKSLASH_IN_FILENAME
3508 if (curbuf->b_p_csl[0] != NUL)
3509 {
3510 int i;
3511
3512 for (i = 0; i < num_matches; ++i)
3513 {
3514 char_u *ptr = matches[i];
3515
3516 while (*ptr != NUL)
3517 {
3518 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3519 *ptr = '/';
3520 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3521 *ptr = '\\';
3522 ptr += (*mb_ptr2len)(ptr);
3523 }
3524 }
3525 }
3526#endif
3527 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3528}
3529
3530/*
3531 * Get the next set of command-line completions matching "compl_pattern".
3532 */
3533 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003534get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003535{
3536 char_u **matches;
3537 int num_matches;
3538
3539 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003540 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003541 ins_compl_add_matches(num_matches, matches, FALSE);
3542}
3543
3544/*
3545 * Get the next set of spell suggestions matching "compl_pattern".
3546 */
3547 static void
3548get_next_spell_completion(linenr_T lnum UNUSED)
3549{
3550#ifdef FEAT_SPELL
3551 char_u **matches;
3552 int num_matches;
3553
3554 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3555 if (num_matches > 0)
3556 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003557 else
3558 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003559#endif
3560}
3561
3562/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003563 * Return the next word or line from buffer "ins_buf" at position
3564 * "cur_match_pos" for completion. The length of the match is set in "len".
3565 */
3566 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003567ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003568 buf_T *ins_buf, // buffer being scanned
3569 pos_T *cur_match_pos, // current match position
3570 int *match_len,
3571 int *cont_s_ipos) // next ^X<> will set initial_pos
3572{
3573 char_u *ptr;
3574 int len;
3575
3576 *match_len = 0;
3577 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3578 cur_match_pos->col;
3579 if (ctrl_x_mode_line_or_eval())
3580 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003581 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003582 {
3583 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3584 return NULL;
3585 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3586 if (!p_paste)
3587 ptr = skipwhite(ptr);
3588 }
3589 len = (int)STRLEN(ptr);
3590 }
3591 else
3592 {
3593 char_u *tmp_ptr = ptr;
3594
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003595 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003596 {
3597 tmp_ptr += compl_length;
3598 // Skip if already inside a word.
3599 if (vim_iswordp(tmp_ptr))
3600 return NULL;
3601 // Find start of next word.
3602 tmp_ptr = find_word_start(tmp_ptr);
3603 }
3604 // Find end of this word.
3605 tmp_ptr = find_word_end(tmp_ptr);
3606 len = (int)(tmp_ptr - ptr);
3607
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003608 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003609 {
3610 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3611 {
3612 // Try next line, if any. the new word will be
3613 // "join" as if the normal command "J" was used.
3614 // IOSIZE is always greater than
3615 // compl_length, so the next STRNCPY always
3616 // works -- Acevedo
3617 STRNCPY(IObuff, ptr, len);
3618 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3619 tmp_ptr = ptr = skipwhite(ptr);
3620 // Find start of next word.
3621 tmp_ptr = find_word_start(tmp_ptr);
3622 // Find end of next word.
3623 tmp_ptr = find_word_end(tmp_ptr);
3624 if (tmp_ptr > ptr)
3625 {
3626 if (*ptr != ')' && IObuff[len - 1] != TAB)
3627 {
3628 if (IObuff[len - 1] != ' ')
3629 IObuff[len++] = ' ';
3630 // IObuf =~ "\k.* ", thus len >= 2
3631 if (p_js
3632 && (IObuff[len - 2] == '.'
3633 || (vim_strchr(p_cpo, CPO_JOINSP)
3634 == NULL
3635 && (IObuff[len - 2] == '?'
3636 || IObuff[len - 2] == '!'))))
3637 IObuff[len++] = ' ';
3638 }
3639 // copy as much as possible of the new word
3640 if (tmp_ptr - ptr >= IOSIZE - len)
3641 tmp_ptr = ptr + IOSIZE - len - 1;
3642 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3643 len += (int)(tmp_ptr - ptr);
3644 *cont_s_ipos = TRUE;
3645 }
3646 IObuff[len] = NUL;
3647 ptr = IObuff;
3648 }
3649 if (len == compl_length)
3650 return NULL;
3651 }
3652 }
3653
3654 *match_len = len;
3655 return ptr;
3656}
3657
3658/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 * Get the next set of words matching "compl_pattern" for default completion(s)
3660 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003661 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3662 * position "st->start_pos" in the "compl_direction" direction. If
3663 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3664 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 * Returns OK if a new next match is found, otherwise returns FAIL.
3666 */
3667 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003668get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669{
3670 int found_new_match = FAIL;
3671 int save_p_scs;
3672 int save_p_ws;
3673 int looped_around = FALSE;
3674 char_u *ptr;
3675 int len;
3676
3677 // If 'infercase' is set, don't use 'smartcase' here
3678 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003679 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003680 p_scs = FALSE;
3681
3682 // Buffers other than curbuf are scanned from the beginning or the
3683 // end but never from the middle, thus setting nowrapscan in this
3684 // buffer is a good idea, on the other hand, we always set
3685 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3686 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003687 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003688 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003689 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690 p_ws = TRUE;
3691 looped_around = FALSE;
3692 for (;;)
3693 {
3694 int cont_s_ipos = FALSE;
3695
3696 ++msg_silent; // Don't want messages for wrapscan.
3697
3698 // ctrl_x_mode_line_or_eval() || word-wise search that
3699 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003700 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003701 found_new_match = search_for_exact_line(st->ins_buf,
3702 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003703 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003704 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003705 NULL, compl_direction, compl_pattern, compl_patternlen,
3706 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003707 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003708 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003709 {
3710 // set "compl_started" even on fail
3711 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003712 st->first_match_pos = *st->cur_match_pos;
3713 st->last_match_pos = *st->cur_match_pos;
3714 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003715 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003716 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3717 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003718 {
3719 found_new_match = FAIL;
3720 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003721 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003722 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3723 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3724 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003725 {
3726 if (looped_around)
3727 found_new_match = FAIL;
3728 else
3729 looped_around = TRUE;
3730 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003731 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003732 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3733 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3734 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003735 {
3736 if (looped_around)
3737 found_new_match = FAIL;
3738 else
3739 looped_around = TRUE;
3740 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003741 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003742 if (found_new_match == FAIL)
3743 break;
3744
3745 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003746 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003747 && start_pos->lnum == st->cur_match_pos->lnum
3748 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003749 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003750
zeertzjq440d4cb2023-03-02 17:51:32 +00003751 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3752 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003753 if (ptr == NULL)
3754 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003755
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003756 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003757 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003758 0, cont_s_ipos) != NOTDONE)
3759 {
3760 found_new_match = OK;
3761 break;
3762 }
3763 }
3764 p_scs = save_p_scs;
3765 p_ws = save_p_ws;
3766
3767 return found_new_match;
3768}
3769
3770/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003771 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003772 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003773 */
3774 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003775get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003776{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003777 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003778
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003779 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003780 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003781 case -1:
3782 break;
3783#ifdef FEAT_FIND_ID
3784 case CTRL_X_PATH_PATTERNS:
3785 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003786 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003787 break;
3788#endif
3789
3790 case CTRL_X_DICTIONARY:
3791 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003792 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3793 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003794 break;
3795
3796 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003797 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003798 break;
3799
3800 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003801 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003802 break;
3803
3804 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003805 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003806 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003807 break;
3808
3809#ifdef FEAT_COMPL_FUNC
3810 case CTRL_X_FUNCTION:
3811 case CTRL_X_OMNI:
3812 expand_by_function(type, compl_pattern);
3813 break;
3814#endif
3815
3816 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003817 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003818 break;
3819
3820 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003821 found_new_match = get_next_default_completion(st, ini);
3822 if (found_new_match == FAIL && st->ins_buf == curbuf)
3823 st->found_all = TRUE;
3824 }
3825
3826 // check if compl_curr_match has changed, (e.g. other type of
3827 // expansion added something)
3828 if (type != 0 && compl_curr_match != compl_old_match)
3829 found_new_match = OK;
3830
3831 return found_new_match;
3832}
3833
3834/*
3835 * Get the next expansion(s), using "compl_pattern".
3836 * The search starts at position "ini" in curbuf and in the direction
3837 * compl_direction.
3838 * When "compl_started" is FALSE start at that position, otherwise continue
3839 * where we stopped searching before.
3840 * This may return before finding all the matches.
3841 * Return the total number of matches or -1 if still unknown -- Acevedo
3842 */
3843 static int
3844ins_compl_get_exp(pos_T *ini)
3845{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003846 static ins_compl_next_state_T st;
3847 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003848 int i;
3849 int found_new_match;
3850 int type = ctrl_x_mode;
3851
3852 if (!compl_started)
3853 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003854 buf_T *buf;
3855
3856 FOR_ALL_BUFFERS(buf)
3857 buf->b_scanned = 0;
3858 if (!st_cleared)
3859 {
3860 CLEAR_FIELD(st);
3861 st_cleared = TRUE;
3862 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003863 st.found_all = FALSE;
3864 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003865 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003866 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003867 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3868 ? (char_u *)"." : curbuf->b_p_cpt);
3869 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003870 st.last_match_pos = st.first_match_pos = *ini;
3871 }
3872 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3873 st.ins_buf = curbuf; // In case the buffer was wiped out.
3874
3875 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003876 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003877 ? &st.last_match_pos : &st.first_match_pos;
3878
3879 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3880 for (;;)
3881 {
3882 found_new_match = FAIL;
3883 st.set_match_pos = FALSE;
3884
3885 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3886 // or if found_all says this entry is done. For ^X^L only use the
3887 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003888 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003889 && (!compl_started || st.found_all))
3890 {
3891 int status = process_next_cpt_value(&st, &type, ini);
3892
3893 if (status == INS_COMPL_CPT_END)
3894 break;
3895 if (status == INS_COMPL_CPT_CONT)
3896 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003897 }
3898
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003899 // If complete() was called then compl_pattern has been reset. The
3900 // following won't work then, bail out.
3901 if (compl_pattern == NULL)
3902 break;
3903
3904 // get the next set of completion matches
3905 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003906
3907 // break the loop for specialized modes (use 'complete' just for the
3908 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3909 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003910 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3911 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003912 {
3913 if (got_int)
3914 break;
3915 // Fill the popup menu as soon as possible.
3916 if (type != -1)
3917 ins_compl_check_keys(0, FALSE);
3918
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003919 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003920 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3921 break;
3922 compl_started = TRUE;
3923 }
3924 else
3925 {
3926 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003927 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003928 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003929
3930 compl_started = FALSE;
3931 }
3932 }
3933 compl_started = TRUE;
3934
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003935 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003936 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003937 found_new_match = FAIL;
3938
3939 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003940 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003941 && !ctrl_x_mode_line_or_eval()))
3942 i = ins_compl_make_cyclic();
3943
3944 if (compl_old_match != NULL)
3945 {
3946 // If several matches were added (FORWARD) or the search failed and has
3947 // just been made cyclic then we have to move compl_curr_match to the
3948 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003949 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003950 : compl_old_match->cp_prev;
3951 if (compl_curr_match == NULL)
3952 compl_curr_match = compl_old_match;
3953 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003954 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003955
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003956 return i;
3957}
3958
3959/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003960 * Update "compl_shown_match" to the actually shown match, it may differ when
3961 * "compl_leader" is used to omit some of the matches.
3962 */
3963 static void
3964ins_compl_update_shown_match(void)
3965{
3966 while (!ins_compl_equal(compl_shown_match,
3967 compl_leader, (int)STRLEN(compl_leader))
3968 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003969 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003970 compl_shown_match = compl_shown_match->cp_next;
3971
3972 // If we didn't find it searching forward, and compl_shows_dir is
3973 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003974 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003975 && !ins_compl_equal(compl_shown_match,
3976 compl_leader, (int)STRLEN(compl_leader))
3977 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003978 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003979 {
3980 while (!ins_compl_equal(compl_shown_match,
3981 compl_leader, (int)STRLEN(compl_leader))
3982 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003983 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003984 compl_shown_match = compl_shown_match->cp_prev;
3985 }
3986}
3987
3988/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003989 * Delete the old text being completed.
3990 */
3991 void
3992ins_compl_delete(void)
3993{
3994 int col;
3995
3996 // In insert mode: Delete the typed part.
3997 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003998 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003999 if ((int)curwin->w_cursor.col > col)
4000 {
4001 if (stop_arrow() == FAIL)
4002 return;
4003 backspace_until_column(col);
4004 }
4005
4006 // TODO: is this sufficient for redrawing? Redrawing everything causes
4007 // flicker, thus we can't do that.
4008 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004009#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004010 // clear v:completed_item
4011 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004012#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004013}
4014
4015/*
4016 * Insert the new text being completed.
4017 * "in_compl_func" is TRUE when called from complete_check().
4018 */
4019 void
4020ins_compl_insert(int in_compl_func)
4021{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004022 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004023
4024 // Make sure we don't go over the end of the string, this can happen with
4025 // illegal bytes.
4026 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4027 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004028 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004029 compl_used_match = FALSE;
4030 else
4031 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004032#ifdef FEAT_EVAL
4033 {
4034 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4035
4036 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4037 }
4038#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004039 if (!in_compl_func)
4040 compl_curr_match = compl_shown_match;
4041}
4042
4043/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004044 * show the file name for the completion match (if any). Truncate the file
4045 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004046 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004047 static void
4048ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004049{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004050 char *lead = _("match in file");
4051 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4052 char_u *s;
4053 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004054
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004055 if (space <= 0)
4056 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004057
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004058 // We need the tail that fits. With double-byte encoding going
4059 // back from the end is very slow, thus go from the start and keep
4060 // the text that fits in "space" between "s" and "e".
4061 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004062 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004063 space -= ptr2cells(e);
4064 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004065 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004066 space += ptr2cells(s);
4067 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004068 }
4069 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004070 msg_hist_off = TRUE;
4071 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4072 s > compl_shown_match->cp_fname ? "<" : "", s);
4073 msg((char *)IObuff);
4074 msg_hist_off = FALSE;
4075 redraw_cmdline = FALSE; // don't overwrite!
4076}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077
glepnira218cc62024-06-03 19:32:39 +02004078 static compl_T *
4079find_comp_when_fuzzy(void)
4080{
4081 int score;
4082 char_u* str;
4083 int target_idx = -1;
4084 int is_forward = compl_shows_dir_forward();
4085 int is_backward = compl_shows_dir_backward();
4086 compl_T *comp = NULL;
4087
4088 if (compl_match_array == NULL ||
4089 (is_forward && compl_selected_item == compl_match_arraysize - 1)
4090 || (is_backward && compl_selected_item == 0))
4091 return compl_first_match;
4092
4093 if (is_forward)
4094 target_idx = compl_selected_item + 1;
4095 else if (is_backward)
4096 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
4097 : compl_selected_item - 1;
4098
4099 score = compl_match_array[target_idx].pum_score;
4100 str = compl_match_array[target_idx].pum_text;
4101
4102 comp = compl_first_match;
4103 do {
4104 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4105 return comp;
4106 comp = comp->cp_next;
4107 } while (comp != NULL && !is_first_match(comp));
4108
4109 return NULL;
4110}
4111
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004112/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004113 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004114 * times. The number of matches found is returned in 'num_matches'.
4115 *
4116 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4117 * get more completions. If it is FALSE, then do nothing when there are no more
4118 * completions in the given direction.
4119 *
4120 * If "advance" is TRUE, then completion will move to the first match.
4121 * Otherwise, the original text will be shown.
4122 *
4123 * Returns OK on success and -1 if the number of matches are unknown.
4124 */
4125 static int
4126find_next_completion_match(
4127 int allow_get_expansion,
4128 int todo, // repeat completion this many times
4129 int advance,
4130 int *num_matches)
4131{
4132 int found_end = FALSE;
4133 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004134
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004135 while (--todo >= 0)
4136 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004137 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004138 {
glepnira218cc62024-06-03 19:32:39 +02004139 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_next
4140 : find_comp_when_fuzzy();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004141 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004142 && (is_first_match(compl_shown_match->cp_next)
4143 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004144 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004145 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004146 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004147 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004148 found_end = is_first_match(compl_shown_match);
glepnira218cc62024-06-03 19:32:39 +02004149 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_prev
4150 : find_comp_when_fuzzy();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004151 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004152 }
4153 else
4154 {
4155 if (!allow_get_expansion)
4156 {
4157 if (advance)
4158 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004159 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004160 compl_pending -= todo + 1;
4161 else
4162 compl_pending += todo + 1;
4163 }
4164 return -1;
4165 }
4166
4167 if (!compl_no_select && advance)
4168 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004169 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004170 --compl_pending;
4171 else
4172 ++compl_pending;
4173 }
4174
4175 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004176 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004177
4178 // handle any pending completions
4179 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004180 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004181 {
4182 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4183 {
4184 compl_shown_match = compl_shown_match->cp_next;
4185 --compl_pending;
4186 }
4187 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4188 {
4189 compl_shown_match = compl_shown_match->cp_prev;
4190 ++compl_pending;
4191 }
4192 else
4193 break;
4194 }
4195 found_end = FALSE;
4196 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004197 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004198 && compl_leader != NULL
4199 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004200 compl_leader, (int)STRLEN(compl_leader))
4201 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004202 ++todo;
4203 else
4204 // Remember a matching item.
4205 found_compl = compl_shown_match;
4206
4207 // Stop at the end of the list when we found a usable match.
4208 if (found_end)
4209 {
4210 if (found_compl != NULL)
4211 {
4212 compl_shown_match = found_compl;
4213 break;
4214 }
4215 todo = 1; // use first usable match after wrapping around
4216 }
4217 }
4218
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004219 return OK;
4220}
4221
4222/*
4223 * Fill in the next completion in the current direction.
4224 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4225 * get more completions. If it is FALSE, then we just do nothing when there
4226 * are no more completions in a given direction. The latter case is used when
4227 * we are still in the middle of finding completions, to allow browsing
4228 * through the ones found so far.
4229 * Return the total number of matches, or -1 if still unknown -- webb.
4230 *
4231 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4232 * compl_shown_match here.
4233 *
4234 * Note that this function may be called recursively once only. First with
4235 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4236 * calls this function with "allow_get_expansion" FALSE.
4237 */
4238 static int
4239ins_compl_next(
4240 int allow_get_expansion,
4241 int count, // repeat completion this many times; should
4242 // be at least 1
4243 int insert_match, // Insert the newly selected match
4244 int in_compl_func) // called from complete_check()
4245{
4246 int num_matches = -1;
4247 int todo = count;
4248 int advance;
4249 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004250 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004251
4252 // When user complete function return -1 for findstart which is next
4253 // time of 'always', compl_shown_match become NULL.
4254 if (compl_shown_match == NULL)
4255 return -1;
4256
glepnira218cc62024-06-03 19:32:39 +02004257 if (compl_leader != NULL
4258 && !match_at_original_text(compl_shown_match)
4259 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004260 // Update "compl_shown_match" to the actually shown match
4261 ins_compl_update_shown_match();
4262
4263 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004264 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004265 // Delete old text to be replaced
4266 ins_compl_delete();
4267
4268 // When finding the longest common text we stick at the original text,
4269 // don't let CTRL-N or CTRL-P move to the first match.
4270 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4271
4272 // When restarting the search don't insert the first match either.
4273 if (compl_restarting)
4274 {
4275 advance = FALSE;
4276 compl_restarting = FALSE;
4277 }
4278
4279 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4280 // around.
4281 if (find_next_completion_match(allow_get_expansion, todo, advance,
4282 &num_matches) == -1)
4283 return -1;
4284
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004285 if (curbuf != orig_curbuf)
4286 {
4287 // In case some completion function switched buffer, don't want to
4288 // insert the completion elsewhere.
4289 return -1;
4290 }
4291
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004292 // Insert the text of the new completion, or the compl_leader.
4293 if (compl_no_insert && !started)
4294 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004295 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004296 compl_used_match = FALSE;
4297 }
4298 else if (insert_match)
4299 {
4300 if (!compl_get_longest || compl_used_match)
4301 ins_compl_insert(in_compl_func);
4302 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004303 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004304 }
4305 else
4306 compl_used_match = FALSE;
4307
4308 if (!allow_get_expansion)
4309 {
4310 // may undisplay the popup menu first
4311 ins_compl_upd_pum();
4312
4313 if (pum_enough_matches())
4314 // Will display the popup menu, don't redraw yet to avoid flicker.
4315 pum_call_update_screen();
4316 else
4317 // Not showing the popup menu yet, redraw to show the user what was
4318 // inserted.
4319 update_screen(0);
4320
4321 // display the updated popup menu
4322 ins_compl_show_pum();
4323#ifdef FEAT_GUI
4324 if (gui.in_use)
4325 {
4326 // Show the cursor after the match, not after the redrawn text.
4327 setcursor();
4328 out_flush_cursor(FALSE, FALSE);
4329 }
4330#endif
4331
4332 // Delete old text to be replaced, since we're still searching and
4333 // don't want to match ourselves!
4334 ins_compl_delete();
4335 }
4336
4337 // Enter will select a match when the match wasn't inserted and the popup
4338 // menu is visible.
4339 if (compl_no_insert && !started)
4340 compl_enter_selects = TRUE;
4341 else
4342 compl_enter_selects = !insert_match && compl_match_array != NULL;
4343
4344 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004345 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004346 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004347
4348 return num_matches;
4349}
4350
4351/*
4352 * Call this while finding completions, to check whether the user has hit a key
4353 * that should change the currently displayed completion, or exit completion
4354 * mode. Also, when compl_pending is not zero, show a completion as soon as
4355 * possible. -- webb
4356 * "frequency" specifies out of how many calls we actually check.
4357 * "in_compl_func" is TRUE when called from complete_check(), don't set
4358 * compl_curr_match.
4359 */
4360 void
4361ins_compl_check_keys(int frequency, int in_compl_func)
4362{
4363 static int count = 0;
4364 int c;
4365
4366 // Don't check when reading keys from a script, :normal or feedkeys().
4367 // That would break the test scripts. But do check for keys when called
4368 // from complete_check().
4369 if (!in_compl_func && (using_script() || ex_normal_busy))
4370 return;
4371
4372 // Only do this at regular intervals
4373 if (++count < frequency)
4374 return;
4375 count = 0;
4376
4377 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4378 // can't do its work correctly.
4379 c = vpeekc_any();
4380 if (c != NUL)
4381 {
4382 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4383 {
4384 c = safe_vgetc(); // Eat the character
4385 compl_shows_dir = ins_compl_key2dir(c);
4386 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4387 c != K_UP && c != K_DOWN, in_compl_func);
4388 }
4389 else
4390 {
4391 // Need to get the character to have KeyTyped set. We'll put it
4392 // back with vungetc() below. But skip K_IGNORE.
4393 c = safe_vgetc();
4394 if (c != K_IGNORE)
4395 {
4396 // Don't interrupt completion when the character wasn't typed,
4397 // e.g., when doing @q to replay keys.
4398 if (c != Ctrl_R && KeyTyped)
4399 compl_interrupted = TRUE;
4400
4401 vungetc(c);
4402 }
4403 }
4404 }
4405 if (compl_pending != 0 && !got_int && !compl_no_insert)
4406 {
4407 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4408
4409 compl_pending = 0;
4410 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4411 }
4412}
4413
4414/*
4415 * Decide the direction of Insert mode complete from the key typed.
4416 * Returns BACKWARD or FORWARD.
4417 */
4418 static int
4419ins_compl_key2dir(int c)
4420{
4421 if (c == Ctrl_P || c == Ctrl_L
4422 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4423 return BACKWARD;
4424 return FORWARD;
4425}
4426
4427/*
4428 * Return TRUE for keys that are used for completion only when the popup menu
4429 * is visible.
4430 */
4431 static int
4432ins_compl_pum_key(int c)
4433{
4434 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4435 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4436 || c == K_UP || c == K_DOWN);
4437}
4438
4439/*
4440 * Decide the number of completions to move forward.
4441 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4442 */
4443 static int
4444ins_compl_key2count(int c)
4445{
4446 int h;
4447
4448 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4449 {
4450 h = pum_get_height();
4451 if (h > 3)
4452 h -= 2; // keep some context
4453 return h;
4454 }
4455 return 1;
4456}
4457
4458/*
4459 * Return TRUE if completion with "c" should insert the match, FALSE if only
4460 * to change the currently selected completion.
4461 */
4462 static int
4463ins_compl_use_match(int c)
4464{
4465 switch (c)
4466 {
4467 case K_UP:
4468 case K_DOWN:
4469 case K_PAGEDOWN:
4470 case K_KPAGEDOWN:
4471 case K_S_DOWN:
4472 case K_PAGEUP:
4473 case K_KPAGEUP:
4474 case K_S_UP:
4475 return FALSE;
4476 }
4477 return TRUE;
4478}
4479
4480/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004481 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4482 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004483 * Sets the global variables: compl_col, compl_length, compl_pattern and
4484 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004485 * Uses the global variables: compl_cont_status and ctrl_x_mode
4486 */
4487 static int
4488get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4489{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004490 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004491 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004492 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004493 {
4494 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4495 ;
4496 compl_col += ++startcol;
4497 compl_length = curs_col - startcol;
4498 }
4499 if (p_ic)
4500 compl_pattern = str_foldcase(line + compl_col,
4501 compl_length, NULL, 0);
4502 else
4503 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4504 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004505 {
4506 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004507 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004508 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004509 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004510 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004511 {
4512 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004513 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004514
4515 // we need up to 2 extra chars for the prefix
4516 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004517 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004518 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004519 {
4520 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004521 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004522 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004523 if (!vim_iswordp(line + compl_col)
4524 || (compl_col > 0
4525 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004526 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004527 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004528 prefixlen = 0;
4529 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004530 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004531 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004532 line + compl_col, compl_length);
4533 }
4534 else if (--startcol < 0
4535 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4536 {
4537 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004538 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004539 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004540 {
4541 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004542 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004543 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004544 compl_col += curs_col;
4545 compl_length = 0;
4546 }
4547 else
4548 {
4549 // Search the point of change class of multibyte character
4550 // or not a word single byte character backward.
4551 if (has_mbyte)
4552 {
4553 int base_class;
4554 int head_off;
4555
4556 startcol -= (*mb_head_off)(line, line + startcol);
4557 base_class = mb_get_class(line + startcol);
4558 while (--startcol >= 0)
4559 {
4560 head_off = (*mb_head_off)(line, line + startcol);
4561 if (base_class != mb_get_class(line + startcol
4562 - head_off))
4563 break;
4564 startcol -= head_off;
4565 }
4566 }
4567 else
4568 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4569 ;
4570 compl_col += ++startcol;
4571 compl_length = (int)curs_col - startcol;
4572 if (compl_length == 1)
4573 {
4574 // Only match word with at least two chars -- webb
4575 // there's no need to call quote_meta,
4576 // alloc(7) is enough -- Acevedo
4577 compl_pattern = alloc(7);
4578 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004579 {
4580 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004581 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004582 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004583 STRCPY((char *)compl_pattern, "\\<");
4584 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4585 STRCAT((char *)compl_pattern, "\\k");
4586 }
4587 else
4588 {
4589 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4590 compl_length) + 2);
4591 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004592 {
4593 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004594 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004595 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004596 STRCPY((char *)compl_pattern, "\\<");
4597 (void)quote_meta(compl_pattern + 2, line + compl_col,
4598 compl_length);
4599 }
4600 }
4601
John Marriott8c85a2a2024-05-20 19:18:26 +02004602 compl_patternlen = STRLEN(compl_pattern);
4603
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004604 return OK;
4605}
4606
4607/*
4608 * Get the pattern, column and length for whole line completion or for the
4609 * complete() function.
4610 * Sets the global variables: compl_col, compl_length and compl_pattern.
4611 */
4612 static int
4613get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4614{
4615 compl_col = (colnr_T)getwhitecols(line);
4616 compl_length = (int)curs_col - (int)compl_col;
4617 if (compl_length < 0) // cursor in indent: empty pattern
4618 compl_length = 0;
4619 if (p_ic)
4620 compl_pattern = str_foldcase(line + compl_col, compl_length,
4621 NULL, 0);
4622 else
4623 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4624 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004625 {
4626 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004627 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004628 }
4629
4630 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004631
4632 return OK;
4633}
4634
4635/*
4636 * Get the pattern, column and length for filename completion.
4637 * Sets the global variables: compl_col, compl_length and compl_pattern.
4638 */
4639 static int
4640get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4641{
4642 // Go back to just before the first filename character.
4643 if (startcol > 0)
4644 {
4645 char_u *p = line + startcol;
4646
4647 MB_PTR_BACK(line, p);
4648 while (p > line && vim_isfilec(PTR2CHAR(p)))
4649 MB_PTR_BACK(line, p);
4650 if (p == line && vim_isfilec(PTR2CHAR(p)))
4651 startcol = 0;
4652 else
4653 startcol = (int)(p - line) + 1;
4654 }
4655
4656 compl_col += startcol;
4657 compl_length = (int)curs_col - startcol;
4658 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4659 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004660 {
4661 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004662 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004663 }
4664
4665 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004666
4667 return OK;
4668}
4669
4670/*
4671 * Get the pattern, column and length for command-line completion.
4672 * Sets the global variables: compl_col, compl_length and compl_pattern.
4673 */
4674 static int
4675get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4676{
4677 compl_pattern = vim_strnsave(line, curs_col);
4678 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004679 {
4680 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004681 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004682 }
4683 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004684 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004685 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004686 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4687 || compl_xp.xp_context == EXPAND_NOTHING)
4688 // No completion possible, use an empty pattern to get a
4689 // "pattern not found" message.
4690 compl_col = curs_col;
4691 else
4692 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4693 compl_length = curs_col - compl_col;
4694
4695 return OK;
4696}
4697
4698/*
4699 * Get the pattern, column and length for user defined completion ('omnifunc',
4700 * 'completefunc' and 'thesaurusfunc')
4701 * Sets the global variables: compl_col, compl_length and compl_pattern.
4702 * Uses the global variable: spell_bad_len
4703 */
4704 static int
4705get_userdefined_compl_info(colnr_T curs_col UNUSED)
4706{
4707 int ret = FAIL;
4708
4709#ifdef FEAT_COMPL_FUNC
4710 // Call user defined function 'completefunc' with "a:findstart"
4711 // set to 1 to obtain the length of text to use for completion.
4712 char_u *line;
4713 typval_T args[3];
4714 int col;
4715 char_u *funcname;
4716 pos_T pos;
4717 int save_State = State;
4718 callback_T *cb;
4719
4720 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4721 // length as a string
4722 funcname = get_complete_funcname(ctrl_x_mode);
4723 if (*funcname == NUL)
4724 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004725 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004726 ? "completefunc" : "omnifunc");
4727 return FAIL;
4728 }
4729
4730 args[0].v_type = VAR_NUMBER;
4731 args[0].vval.v_number = 1;
4732 args[1].v_type = VAR_STRING;
4733 args[1].vval.v_string = (char_u *)"";
4734 args[2].v_type = VAR_UNKNOWN;
4735 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004736 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004737 cb = get_insert_callback(ctrl_x_mode);
4738 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004739 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004740
4741 State = save_State;
4742 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004743 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004744 validate_cursor();
4745 if (!EQUAL_POS(curwin->w_cursor, pos))
4746 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004747 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004748 return FAIL;
4749 }
4750
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004751 // Return value -2 means the user complete function wants to cancel the
4752 // complete without an error, do the same if the function did not execute
4753 // successfully.
4754 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004755 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004756 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004757 if (col == -3)
4758 {
4759 ctrl_x_mode = CTRL_X_NORMAL;
4760 edit_submode = NULL;
4761 if (!shortmess(SHM_COMPLETIONMENU))
4762 msg_clr_cmdline();
4763 return FAIL;
4764 }
4765
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004766 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004767 // completion.
4768 compl_opt_refresh_always = FALSE;
4769 compl_opt_suppress_empty = FALSE;
4770
4771 if (col < 0)
4772 col = curs_col;
4773 compl_col = col;
4774 if (compl_col > curs_col)
4775 compl_col = curs_col;
4776
4777 // Setup variables for completion. Need to obtain "line" again,
4778 // it may have become invalid.
4779 line = ml_get(curwin->w_cursor.lnum);
4780 compl_length = curs_col - compl_col;
4781 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4782 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004783 {
4784 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004785 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004786 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004787
John Marriott8c85a2a2024-05-20 19:18:26 +02004788 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004789 ret = OK;
4790#endif
4791
4792 return ret;
4793}
4794
4795/*
4796 * Get the pattern, column and length for spell completion.
4797 * Sets the global variables: compl_col, compl_length and compl_pattern.
4798 * Uses the global variable: spell_bad_len
4799 */
4800 static int
4801get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4802{
4803 int ret = FAIL;
4804#ifdef FEAT_SPELL
4805 char_u *line;
4806
4807 if (spell_bad_len > 0)
4808 compl_col = curs_col - spell_bad_len;
4809 else
4810 compl_col = spell_word_start(startcol);
4811 if (compl_col >= (colnr_T)startcol)
4812 {
4813 compl_length = 0;
4814 compl_col = curs_col;
4815 }
4816 else
4817 {
4818 spell_expand_check_cap(compl_col);
4819 compl_length = (int)curs_col - compl_col;
4820 }
4821 // Need to obtain "line" again, it may have become invalid.
4822 line = ml_get(curwin->w_cursor.lnum);
4823 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4824 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004825 {
4826 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004827 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004828 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004829
John Marriott8c85a2a2024-05-20 19:18:26 +02004830 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004831 ret = OK;
4832#endif
4833
4834 return ret;
4835}
4836
4837/*
4838 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004839 * "startcol" - start column number of the completion pattern/text
4840 * "cur_col" - current cursor column
4841 * On return, "line_invalid" is set to TRUE, if the current line may have
4842 * become invalid and needs to be fetched again.
4843 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004844 */
4845 static int
4846compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4847{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004848 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004849 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4850 && !thesaurus_func_complete(ctrl_x_mode)))
4851 {
4852 return get_normal_compl_info(line, startcol, curs_col);
4853 }
4854 else if (ctrl_x_mode_line_or_eval())
4855 {
4856 return get_wholeline_compl_info(line, curs_col);
4857 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004858 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004859 {
4860 return get_filename_compl_info(line, startcol, curs_col);
4861 }
4862 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4863 {
4864 return get_cmdline_compl_info(line, curs_col);
4865 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004866 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004867 || thesaurus_func_complete(ctrl_x_mode))
4868 {
4869 if (get_userdefined_compl_info(curs_col) == FAIL)
4870 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004871 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004872 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004873 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004874 {
4875 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4876 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004877 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004878 }
4879 else
4880 {
4881 internal_error("ins_complete()");
4882 return FAIL;
4883 }
4884
4885 return OK;
4886}
4887
4888/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004889 * Continue an interrupted completion mode search in "line".
4890 *
4891 * If this same ctrl_x_mode has been interrupted use the text from
4892 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4893 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4894 * the same line as the cursor then fix it (the line has been split because it
4895 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4896 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004897 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004898 static void
4899ins_compl_continue_search(char_u *line)
4900{
4901 // it is a continued search
4902 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004903 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4904 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004905 {
4906 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4907 {
4908 // line (probably) wrapped, set compl_startpos to the
4909 // first non_blank in the line, if it is not a wordchar
4910 // include it to get a better pattern, but then we don't
4911 // want the "\\<" prefix, check it below
4912 compl_col = (colnr_T)getwhitecols(line);
4913 compl_startpos.col = compl_col;
4914 compl_startpos.lnum = curwin->w_cursor.lnum;
4915 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4916 }
4917 else
4918 {
4919 // S_IPOS was set when we inserted a word that was at the
4920 // beginning of the line, which means that we'll go to SOL
4921 // mode but first we need to redefine compl_startpos
4922 if (compl_cont_status & CONT_S_IPOS)
4923 {
4924 compl_cont_status |= CONT_SOL;
4925 compl_startpos.col = (colnr_T)(skipwhite(
4926 line + compl_length
4927 + compl_startpos.col) - line);
4928 }
4929 compl_col = compl_startpos.col;
4930 }
4931 compl_length = curwin->w_cursor.col - (int)compl_col;
4932 // IObuff is used to add a "word from the next line" would we
4933 // have enough space? just being paranoid
4934#define MIN_SPACE 75
4935 if (compl_length > (IOSIZE - MIN_SPACE))
4936 {
4937 compl_cont_status &= ~CONT_SOL;
4938 compl_length = (IOSIZE - MIN_SPACE);
4939 compl_col = curwin->w_cursor.col - compl_length;
4940 }
4941 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4942 if (compl_length < 1)
4943 compl_cont_status &= CONT_LOCAL;
4944 }
4945 else if (ctrl_x_mode_line_or_eval())
4946 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4947 else
4948 compl_cont_status = 0;
4949}
4950
4951/*
4952 * start insert mode completion
4953 */
4954 static int
4955ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004956{
4957 char_u *line;
4958 int startcol = 0; // column where searched text starts
4959 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004960 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004961 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004962 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004963
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004964 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004965
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004966 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004967 did_si = FALSE;
4968 can_si = FALSE;
4969 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004970 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004971 return FAIL;
4972
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004973 line = ml_get(curwin->w_cursor.lnum);
4974 curs_col = curwin->w_cursor.col;
4975 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004976
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004977 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4978 && compl_cont_mode == ctrl_x_mode)
4979 // this same ctrl-x_mode was interrupted previously. Continue the
4980 // completion.
4981 ins_compl_continue_search(line);
4982 else
4983 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004984
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004985 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004986 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004987 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004988 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004989 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4990 compl_cont_status = 0;
4991 compl_cont_status |= CONT_N_ADDS;
4992 compl_startpos = curwin->w_cursor;
4993 startcol = (int)curs_col;
4994 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004995 }
4996
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004997 // Work out completion pattern and original text -- webb
4998 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4999 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005000 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5001 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005002 // restore did_ai, so that adding comment leader works
5003 did_ai = save_did_ai;
5004 return FAIL;
5005 }
5006 // If "line" was changed while getting completion info get it again.
5007 if (line_invalid)
5008 line = ml_get(curwin->w_cursor.lnum);
5009
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005010 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005011 {
5012 edit_submode_pre = (char_u *)_(" Adding");
5013 if (ctrl_x_mode_line_or_eval())
5014 {
5015 // Insert a new line, keep indentation but ignore 'comments'.
5016 char_u *old = curbuf->b_p_com;
5017
5018 curbuf->b_p_com = (char_u *)"";
5019 compl_startpos.lnum = curwin->w_cursor.lnum;
5020 compl_startpos.col = compl_col;
5021 ins_eol('\r');
5022 curbuf->b_p_com = old;
5023 compl_length = 0;
5024 compl_col = curwin->w_cursor.col;
5025 }
5026 }
5027 else
5028 {
5029 edit_submode_pre = NULL;
5030 compl_startpos.col = compl_col;
5031 }
5032
5033 if (compl_cont_status & CONT_LOCAL)
5034 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5035 else
5036 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5037
5038 // If any of the original typed text has been changed we need to fix
5039 // the redo buffer.
5040 ins_compl_fixRedoBufForLeader(NULL);
5041
5042 // Always add completion for the original text.
5043 vim_free(compl_orig_text);
5044 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5045 if (p_ic)
5046 flags |= CP_ICASE;
5047 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5048 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
5049 {
5050 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005051 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005052 VIM_CLEAR(compl_orig_text);
5053 return FAIL;
5054 }
5055
5056 // showmode might reset the internal line pointers, so it must
5057 // be called before line = ml_get(), or when this address is no
5058 // longer needed. -- Acevedo.
5059 edit_submode_extra = (char_u *)_("-- Searching...");
5060 edit_submode_highl = HLF_COUNT;
5061 showmode();
5062 edit_submode_extra = NULL;
5063 out_flush();
5064
5065 return OK;
5066}
5067
5068/*
5069 * display the completion status message
5070 */
5071 static void
5072ins_compl_show_statusmsg(void)
5073{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005074 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005075 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005076 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005077 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005078 ? (char_u *)_("Hit end of paragraph")
5079 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005080 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005081 }
5082
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005083 if (edit_submode_extra == NULL)
5084 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005085 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005086 {
5087 edit_submode_extra = (char_u *)_("Back at original");
5088 edit_submode_highl = HLF_W;
5089 }
5090 else if (compl_cont_status & CONT_S_IPOS)
5091 {
5092 edit_submode_extra = (char_u *)_("Word from other line");
5093 edit_submode_highl = HLF_COUNT;
5094 }
5095 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5096 {
5097 edit_submode_extra = (char_u *)_("The only match");
5098 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005099 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005100 }
5101 else
5102 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005103#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005104 // Update completion sequence number when needed.
5105 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005106 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005107#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005108 // The match should always have a sequence number now, this is
5109 // just a safety check.
5110 if (compl_curr_match->cp_number != -1)
5111 {
5112 // Space for 10 text chars. + 2x10-digit no.s = 31.
5113 // Translations may need more than twice that.
5114 static char_u match_ref[81];
5115
5116 if (compl_matches > 0)
5117 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005118 _("match %d of %d"),
5119 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005120 else
5121 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005122 _("match %d"),
5123 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005124 edit_submode_extra = match_ref;
5125 edit_submode_highl = HLF_R;
5126 if (dollar_vcol >= 0)
5127 curs_columns(FALSE);
5128 }
5129 }
5130 }
5131
5132 // Show a message about what (completion) mode we're in.
5133 if (!compl_opt_suppress_empty)
5134 {
5135 showmode();
5136 if (!shortmess(SHM_COMPLETIONMENU))
5137 {
5138 if (edit_submode_extra != NULL)
5139 {
5140 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005141 {
5142 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005143 msg_attr((char *)edit_submode_extra,
5144 edit_submode_highl < HLF_COUNT
5145 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005146 msg_hist_off = FALSE;
5147 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005148 }
5149 else
5150 msg_clr_cmdline(); // necessary for "noshowmode"
5151 }
5152 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005153}
5154
5155/*
5156 * Do Insert mode completion.
5157 * Called when character "c" was typed, which has a meaning for completion.
5158 * Returns OK if completion was done, FAIL if something failed (out of mem).
5159 */
5160 int
5161ins_complete(int c, int enable_pum)
5162{
5163 int n;
5164 int save_w_wrow;
5165 int save_w_leftcol;
5166 int insert_match;
5167
5168 compl_direction = ins_compl_key2dir(c);
5169 insert_match = ins_compl_use_match(c);
5170
5171 if (!compl_started)
5172 {
5173 if (ins_compl_start() == FAIL)
5174 return FAIL;
5175 }
5176 else if (insert_match && stop_arrow() == FAIL)
5177 return FAIL;
5178
5179 compl_shown_match = compl_curr_match;
5180 compl_shows_dir = compl_direction;
5181
5182 // Find next match (and following matches).
5183 save_w_wrow = curwin->w_wrow;
5184 save_w_leftcol = curwin->w_leftcol;
5185 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5186
5187 // may undisplay the popup menu
5188 ins_compl_upd_pum();
5189
5190 if (n > 1) // all matches have been found
5191 compl_matches = n;
5192 compl_curr_match = compl_shown_match;
5193 compl_direction = compl_shows_dir;
5194
5195 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5196 // mode.
5197 if (got_int && !global_busy)
5198 {
5199 (void)vgetc();
5200 got_int = FALSE;
5201 }
5202
5203 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005204 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005205 {
5206 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5207 // because we couldn't expand anything at first place, but if we used
5208 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5209 // (such as M in M'exico) if not tried already. -- Acevedo
5210 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005211 || compl_status_adding()
5212 || (ctrl_x_mode_not_default()
5213 && !ctrl_x_mode_path_patterns()
5214 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005215 compl_cont_status &= ~CONT_N_ADDS;
5216 }
5217
5218 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5219 compl_cont_status |= CONT_S_IPOS;
5220 else
5221 compl_cont_status &= ~CONT_S_IPOS;
5222
5223 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005224
5225 // Show the popup menu, unless we got interrupted.
5226 if (enable_pum && !compl_interrupted)
5227 show_pum(save_w_wrow, save_w_leftcol);
5228
5229 compl_was_interrupted = compl_interrupted;
5230 compl_interrupted = FALSE;
5231
5232 return OK;
5233}
5234
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005235/*
5236 * Remove (if needed) and show the popup menu
5237 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005238 static void
5239show_pum(int prev_w_wrow, int prev_w_leftcol)
5240{
5241 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005242 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005243 RedrawingDisabled = 0;
5244
5245 // If the cursor moved or the display scrolled we need to remove the pum
5246 // first.
5247 setcursor();
5248 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5249 ins_compl_del_pum();
5250
5251 ins_compl_show_pum();
5252 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005253
5254 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005255}
5256
5257/*
5258 * Looks in the first "len" chars. of "src" for search-metachars.
5259 * If dest is not NULL the chars. are copied there quoting (with
5260 * a backslash) the metachars, and dest would be NUL terminated.
5261 * Returns the length (needed) of dest
5262 */
5263 static unsigned
5264quote_meta(char_u *dest, char_u *src, int len)
5265{
5266 unsigned m = (unsigned)len + 1; // one extra for the NUL
5267
5268 for ( ; --len >= 0; src++)
5269 {
5270 switch (*src)
5271 {
5272 case '.':
5273 case '*':
5274 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005275 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005276 break;
5277 // FALLTHROUGH
5278 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005279 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005280 break;
5281 // FALLTHROUGH
5282 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005283 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005284 break;
5285 // FALLTHROUGH
5286 case '^': // currently it's not needed.
5287 case '$':
5288 m++;
5289 if (dest != NULL)
5290 *dest++ = '\\';
5291 break;
5292 }
5293 if (dest != NULL)
5294 *dest++ = *src;
5295 // Copy remaining bytes of a multibyte character.
5296 if (has_mbyte)
5297 {
5298 int i, mb_len;
5299
5300 mb_len = (*mb_ptr2len)(src) - 1;
5301 if (mb_len > 0 && len >= mb_len)
5302 for (i = 0; i < mb_len; ++i)
5303 {
5304 --len;
5305 ++src;
5306 if (dest != NULL)
5307 *dest++ = *src;
5308 }
5309 }
5310 }
5311 if (dest != NULL)
5312 *dest = NUL;
5313
5314 return m;
5315}
5316
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005317#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005318 void
5319free_insexpand_stuff(void)
5320{
5321 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005322# ifdef FEAT_EVAL
5323 free_callback(&cfu_cb);
5324 free_callback(&ofu_cb);
5325 free_callback(&tsrfu_cb);
5326# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005327}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005328#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005329
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005330#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005331/*
5332 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5333 * spelled word, if there is one.
5334 */
5335 static void
5336spell_back_to_badword(void)
5337{
5338 pos_T tpos = curwin->w_cursor;
5339
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005340 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005341 if (curwin->w_cursor.col != tpos.col)
5342 start_arrow(&tpos);
5343}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005344#endif