blob: f43c12665fd1b0340f9e34269db63b816d3640d0 [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 {
glepnirdca57fb2024-06-04 22:01:21 +02001316 // Update the maximum fuzzy score and the shown match
1317 // if the current item's score is higher
glepnira218cc62024-06-03 19:32:39 +02001318 if (compl->cp_score > max_fuzzy_score)
1319 {
1320 did_find_shown_match = TRUE;
1321 max_fuzzy_score = compl->cp_score;
1322 compl_shown_match = compl;
1323 shown_match_ok = TRUE;
1324 }
1325
glepnirdca57fb2024-06-04 22:01:21 +02001326 // If there is no "no select" condition and the max fuzzy
1327 // score is positive, or there is no completion leader or the
1328 // leader length is zero, mark the shown match as valid and
1329 // reset the current index.
glepnira218cc62024-06-03 19:32:39 +02001330 if (!compl_no_select
1331 && (max_fuzzy_score > 0
1332 || (compl_leader == NULL || lead_len == 0)))
1333 {
1334 shown_match_ok = TRUE;
1335 cur = 0;
1336 }
1337 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001338
1339 if (compl->cp_text[CPT_ABBR] != NULL)
1340 compl_match_array[i].pum_text =
1341 compl->cp_text[CPT_ABBR];
1342 else
1343 compl_match_array[i].pum_text = compl->cp_str;
1344 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1345 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
glepnira218cc62024-06-03 19:32:39 +02001346 compl_match_array[i].pum_score = compl->cp_score;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001347 if (compl->cp_text[CPT_MENU] != NULL)
1348 compl_match_array[i++].pum_extra =
1349 compl->cp_text[CPT_MENU];
1350 else
1351 compl_match_array[i++].pum_extra = compl->cp_fname;
1352 }
1353
glepnira218cc62024-06-03 19:32:39 +02001354 if (compl == compl_shown_match && !compl_fuzzy_match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001355 {
1356 did_find_shown_match = TRUE;
1357
1358 // When the original text is the shown match don't set
1359 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001360 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001361 shown_match_ok = TRUE;
1362
1363 if (!shown_match_ok && shown_compl != NULL)
1364 {
1365 // The shown match isn't displayed, set it to the
1366 // previously displayed match.
1367 compl_shown_match = shown_compl;
1368 shown_match_ok = TRUE;
1369 }
1370 }
1371 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001372 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001373
glepnira218cc62024-06-03 19:32:39 +02001374 if (compl_fuzzy_match && compl_leader != NULL && lead_len > 0)
1375 // sort by the largest score of fuzzy match
1376 qsort(compl_match_array, (size_t)compl_match_arraysize, sizeof(pumitem_T), ins_compl_fuzzy_sort);
1377
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001378 if (!shown_match_ok) // no displayed match at all
1379 cur = -1;
1380
1381 return cur;
1382}
1383
1384/*
1385 * Show the popup menu for the list of matches.
1386 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1387 */
1388 void
1389ins_compl_show_pum(void)
1390{
1391 int i;
1392 int cur = -1;
1393 colnr_T col;
1394
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001395 if (!pum_wanted() || !pum_enough_matches())
1396 return;
1397
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001398 // Update the screen later, before drawing the popup menu over it.
1399 pum_call_update_screen();
1400
1401 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001402 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001403 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001404 else
1405 {
1406 // popup menu already exists, only need to find the current item.
1407 for (i = 0; i < compl_match_arraysize; ++i)
1408 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1409 || compl_match_array[i].pum_text
1410 == compl_shown_match->cp_text[CPT_ABBR])
1411 {
1412 cur = i;
1413 break;
1414 }
1415 }
1416
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001417 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001418 {
1419#ifdef FEAT_EVAL
1420 if (compl_started && has_completechanged())
1421 trigger_complete_changed_event(cur);
1422#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001423 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001424 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001425
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001426 // In Replace mode when a $ is displayed at the end of the line only
1427 // part of the screen would be updated. We do need to redraw here.
1428 dollar_vcol = -1;
1429
1430 // Compute the screen column of the start of the completed text.
1431 // Use the cursor to get all wrapping and other settings right.
1432 col = curwin->w_cursor.col;
1433 curwin->w_cursor.col = compl_col;
glepnira218cc62024-06-03 19:32:39 +02001434 compl_selected_item = cur;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001435 pum_display(compl_match_array, compl_match_arraysize, cur);
1436 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001437
glepnircbb46b42024-02-03 18:11:13 +01001438 // After adding leader, set the current match to shown match.
1439 if (compl_started && compl_curr_match != compl_shown_match)
1440 compl_curr_match = compl_shown_match;
1441
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001442#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001443 if (has_completechanged())
1444 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001445#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001446}
1447
1448#define DICT_FIRST (1) // use just first element in "dict"
1449#define DICT_EXACT (2) // "dict" is the exact name of a file
1450
1451/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001452 * Add any identifiers that match the given pattern "pat" in the list of
1453 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001454 */
1455 static void
1456ins_compl_dictionaries(
1457 char_u *dict_start,
1458 char_u *pat,
1459 int flags, // DICT_FIRST and/or DICT_EXACT
1460 int thesaurus) // Thesaurus completion
1461{
1462 char_u *dict = dict_start;
1463 char_u *ptr;
1464 char_u *buf;
1465 regmatch_T regmatch;
1466 char_u **files;
1467 int count;
1468 int save_p_scs;
1469 int dir = compl_direction;
1470
1471 if (*dict == NUL)
1472 {
1473#ifdef FEAT_SPELL
1474 // When 'dictionary' is empty and spell checking is enabled use
1475 // "spell".
1476 if (!thesaurus && curwin->w_p_spell)
1477 dict = (char_u *)"spell";
1478 else
1479#endif
1480 return;
1481 }
1482
1483 buf = alloc(LSIZE);
1484 if (buf == NULL)
1485 return;
1486 regmatch.regprog = NULL; // so that we can goto theend
1487
1488 // If 'infercase' is set, don't use 'smartcase' here
1489 save_p_scs = p_scs;
1490 if (curbuf->b_p_inf)
1491 p_scs = FALSE;
1492
1493 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1494 // to only match at the start of a line. Otherwise just match the
1495 // pattern. Also need to double backslashes.
1496 if (ctrl_x_mode_line_or_eval())
1497 {
1498 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1499 size_t len;
1500
1501 if (pat_esc == NULL)
1502 goto theend;
1503 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001504 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001505 if (ptr == NULL)
1506 {
1507 vim_free(pat_esc);
1508 goto theend;
1509 }
1510 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1511 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1512 vim_free(pat_esc);
1513 vim_free(ptr);
1514 }
1515 else
1516 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001517 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001518 if (regmatch.regprog == NULL)
1519 goto theend;
1520 }
1521
1522 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1523 regmatch.rm_ic = ignorecase(pat);
1524 while (*dict != NUL && !got_int && !compl_interrupted)
1525 {
1526 // copy one dictionary file name into buf
1527 if (flags == DICT_EXACT)
1528 {
1529 count = 1;
1530 files = &dict;
1531 }
1532 else
1533 {
1534 // Expand wildcards in the dictionary name, but do not allow
1535 // backticks (for security, the 'dict' option may have been set in
1536 // a modeline).
1537 copy_option_part(&dict, buf, LSIZE, ",");
1538# ifdef FEAT_SPELL
1539 if (!thesaurus && STRCMP(buf, "spell") == 0)
1540 count = -1;
1541 else
1542# endif
1543 if (vim_strchr(buf, '`') != NULL
1544 || expand_wildcards(1, &buf, &count, &files,
1545 EW_FILE|EW_SILENT) != OK)
1546 count = 0;
1547 }
1548
1549# ifdef FEAT_SPELL
1550 if (count == -1)
1551 {
1552 // Complete from active spelling. Skip "\<" in the pattern, we
1553 // don't use it as a RE.
1554 if (pat[0] == '\\' && pat[1] == '<')
1555 ptr = pat + 2;
1556 else
1557 ptr = pat;
1558 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1559 }
1560 else
1561# endif
1562 if (count > 0) // avoid warning for using "files" uninit
1563 {
1564 ins_compl_files(count, files, thesaurus, flags,
1565 &regmatch, buf, &dir);
1566 if (flags != DICT_EXACT)
1567 FreeWild(count, files);
1568 }
1569 if (flags != 0)
1570 break;
1571 }
1572
1573theend:
1574 p_scs = save_p_scs;
1575 vim_regfree(regmatch.regprog);
1576 vim_free(buf);
1577}
1578
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001579/*
1580 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1581 * skipping the word at 'skip_word'. Returns OK on success.
1582 */
1583 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001584thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001585 char_u *fname,
1586 char_u **buf_arg,
1587 int dir,
1588 char_u *skip_word)
1589{
1590 int status = OK;
1591 char_u *ptr;
1592 char_u *wstart;
1593
1594 // Add the other matches on the line
1595 ptr = *buf_arg;
1596 while (!got_int)
1597 {
1598 // Find start of the next word. Skip white
1599 // space and punctuation.
1600 ptr = find_word_start(ptr);
1601 if (*ptr == NUL || *ptr == NL)
1602 break;
1603 wstart = ptr;
1604
1605 // Find end of the word.
1606 if (has_mbyte)
1607 // Japanese words may have characters in
1608 // different classes, only separate words
1609 // with single-byte non-word characters.
1610 while (*ptr != NUL)
1611 {
1612 int l = (*mb_ptr2len)(ptr);
1613
1614 if (l < 2 && !vim_iswordc(*ptr))
1615 break;
1616 ptr += l;
1617 }
1618 else
1619 ptr = find_word_end(ptr);
1620
1621 // Add the word. Skip the regexp match.
1622 if (wstart != skip_word)
1623 {
1624 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1625 fname, dir, FALSE);
1626 if (status == FAIL)
1627 break;
1628 }
1629 }
1630
1631 *buf_arg = ptr;
1632 return status;
1633}
1634
1635/*
1636 * Process "count" dictionary/thesaurus "files" and add the text matching
1637 * "regmatch".
1638 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001639 static void
1640ins_compl_files(
1641 int count,
1642 char_u **files,
1643 int thesaurus,
1644 int flags,
1645 regmatch_T *regmatch,
1646 char_u *buf,
1647 int *dir)
1648{
1649 char_u *ptr;
1650 int i;
1651 FILE *fp;
1652 int add_r;
1653
1654 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1655 {
1656 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001657 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001658 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001659 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001660 vim_snprintf((char *)IObuff, IOSIZE,
1661 _("Scanning dictionary: %s"), (char *)files[i]);
1662 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1663 }
1664
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001665 if (fp == NULL)
1666 continue;
1667
1668 // Read dictionary file line by line.
1669 // Check each line for a match.
1670 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001671 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001672 ptr = buf;
1673 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001674 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001675 ptr = regmatch->startp[0];
1676 if (ctrl_x_mode_line_or_eval())
1677 ptr = find_line_end(ptr);
1678 else
1679 ptr = find_word_end(ptr);
1680 add_r = ins_compl_add_infercase(regmatch->startp[0],
1681 (int)(ptr - regmatch->startp[0]),
1682 p_ic, files[i], *dir, FALSE);
1683 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001685 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001686 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001687 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001688 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001689 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001690 if (add_r == OK)
1691 // if dir was BACKWARD then honor it just once
1692 *dir = FORWARD;
1693 else if (add_r == FAIL)
1694 break;
1695 // avoid expensive call to vim_regexec() when at end
1696 // of line
1697 if (*ptr == '\n' || got_int)
1698 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001699 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001700 line_breakcheck();
1701 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001702 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001703 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001704 }
1705}
1706
1707/*
1708 * Find the start of the next word.
1709 * Returns a pointer to the first char of the word. Also stops at a NUL.
1710 */
1711 char_u *
1712find_word_start(char_u *ptr)
1713{
1714 if (has_mbyte)
1715 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1716 ptr += (*mb_ptr2len)(ptr);
1717 else
1718 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1719 ++ptr;
1720 return ptr;
1721}
1722
1723/*
1724 * Find the end of the word. Assumes it starts inside a word.
1725 * Returns a pointer to just after the word.
1726 */
1727 char_u *
1728find_word_end(char_u *ptr)
1729{
1730 int start_class;
1731
1732 if (has_mbyte)
1733 {
1734 start_class = mb_get_class(ptr);
1735 if (start_class > 1)
1736 while (*ptr != NUL)
1737 {
1738 ptr += (*mb_ptr2len)(ptr);
1739 if (mb_get_class(ptr) != start_class)
1740 break;
1741 }
1742 }
1743 else
1744 while (vim_iswordc(*ptr))
1745 ++ptr;
1746 return ptr;
1747}
1748
1749/*
1750 * Find the end of the line, omitting CR and NL at the end.
1751 * Returns a pointer to just after the line.
1752 */
1753 static char_u *
1754find_line_end(char_u *ptr)
1755{
1756 char_u *s;
1757
1758 s = ptr + STRLEN(ptr);
1759 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1760 --s;
1761 return s;
1762}
1763
1764/*
1765 * Free the list of completions
1766 */
1767 static void
1768ins_compl_free(void)
1769{
1770 compl_T *match;
1771 int i;
1772
1773 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001774 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001775 VIM_CLEAR(compl_leader);
1776
1777 if (compl_first_match == NULL)
1778 return;
1779
1780 ins_compl_del_pum();
1781 pum_clear();
1782
1783 compl_curr_match = compl_first_match;
1784 do
1785 {
1786 match = compl_curr_match;
1787 compl_curr_match = compl_curr_match->cp_next;
1788 vim_free(match->cp_str);
1789 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001790 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001791 vim_free(match->cp_fname);
1792 for (i = 0; i < CPT_COUNT; ++i)
1793 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001794#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001795 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001796#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001797 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001798 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001799 compl_first_match = compl_curr_match = NULL;
1800 compl_shown_match = NULL;
1801 compl_old_match = NULL;
1802}
1803
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001804/*
1805 * Reset/clear the completion state.
1806 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001807 void
1808ins_compl_clear(void)
1809{
1810 compl_cont_status = 0;
1811 compl_started = FALSE;
1812 compl_matches = 0;
1813 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001814 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001815 VIM_CLEAR(compl_leader);
1816 edit_submode_extra = NULL;
1817 VIM_CLEAR(compl_orig_text);
1818 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001819#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 // clear v:completed_item
1821 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001822#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001823}
1824
1825/*
1826 * Return TRUE when Insert completion is active.
1827 */
1828 int
1829ins_compl_active(void)
1830{
1831 return compl_started;
1832}
1833
1834/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001835 * Selected one of the matches. When FALSE the match was edited or using the
1836 * longest common string.
1837 */
1838 int
1839ins_compl_used_match(void)
1840{
1841 return compl_used_match;
1842}
1843
1844/*
1845 * Initialize get longest common string.
1846 */
1847 void
1848ins_compl_init_get_longest(void)
1849{
1850 compl_get_longest = FALSE;
1851}
1852
1853/*
1854 * Returns TRUE when insert completion is interrupted.
1855 */
1856 int
1857ins_compl_interrupted(void)
1858{
1859 return compl_interrupted;
1860}
1861
1862/*
1863 * Returns TRUE if the <Enter> key selects a match in the completion popup
1864 * menu.
1865 */
1866 int
1867ins_compl_enter_selects(void)
1868{
1869 return compl_enter_selects;
1870}
1871
1872/*
1873 * Return the column where the text starts that is being completed
1874 */
1875 colnr_T
1876ins_compl_col(void)
1877{
1878 return compl_col;
1879}
1880
1881/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001882 * Return the length in bytes of the text being completed
1883 */
1884 int
1885ins_compl_len(void)
1886{
1887 return compl_length;
1888}
1889
1890/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001891 * Delete one character before the cursor and show the subset of the matches
1892 * that match the word that is now before the cursor.
1893 * Returns the character to be used, NUL if the work is done and another char
1894 * to be got from the user.
1895 */
1896 int
1897ins_compl_bs(void)
1898{
1899 char_u *line;
1900 char_u *p;
1901
1902 line = ml_get_curline();
1903 p = line + curwin->w_cursor.col;
1904 MB_PTR_BACK(line, p);
1905
1906 // Stop completion when the whole word was deleted. For Omni completion
1907 // allow the word to be deleted, we won't match everything.
1908 // Respect the 'backspace' option.
1909 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001910 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1911 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001912 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1913 - compl_length < 0))
1914 return K_BS;
1915
1916 // Deleted more than what was used to find matches or didn't finish
1917 // finding all matches: need to look for matches all over again.
1918 if (curwin->w_cursor.col <= compl_col + compl_length
1919 || ins_compl_need_restart())
1920 ins_compl_restart();
1921
1922 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001923 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001924 if (compl_leader == NULL)
1925 return K_BS;
1926
1927 ins_compl_new_leader();
1928 if (compl_shown_match != NULL)
1929 // Make sure current match is not a hidden item.
1930 compl_curr_match = compl_shown_match;
1931 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001932}
1933
1934/*
1935 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1936 * be called.
1937 */
1938 static int
1939ins_compl_need_restart(void)
1940{
1941 // Return TRUE if we didn't complete finding matches or when the
1942 // 'completefunc' returned "always" in the "refresh" dictionary item.
1943 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001944 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001945 && compl_opt_refresh_always);
1946}
1947
1948/*
1949 * Called after changing "compl_leader".
1950 * Show the popup menu with a different set of matches.
1951 * May also search for matches again if the previous search was interrupted.
1952 */
1953 static void
1954ins_compl_new_leader(void)
1955{
1956 ins_compl_del_pum();
1957 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001958 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001959 compl_used_match = FALSE;
1960
1961 if (compl_started)
1962 ins_compl_set_original_text(compl_leader);
1963 else
1964 {
1965#ifdef FEAT_SPELL
1966 spell_bad_len = 0; // need to redetect bad word
1967#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001968 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969 // the popup menu display the changed text before the cursor. Set
1970 // "compl_restarting" to avoid that the first match is inserted.
1971 pum_call_update_screen();
1972#ifdef FEAT_GUI
1973 if (gui.in_use)
1974 {
1975 // Show the cursor after the match, not after the redrawn text.
1976 setcursor();
1977 out_flush_cursor(FALSE, FALSE);
1978 }
1979#endif
1980 compl_restarting = TRUE;
1981 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1982 compl_cont_status = 0;
1983 compl_restarting = FALSE;
1984 }
1985
1986 compl_enter_selects = !compl_used_match;
1987
1988 // Show the popup menu with a different set of matches.
1989 ins_compl_show_pum();
1990
1991 // Don't let Enter select the original text when there is no popup menu.
1992 if (compl_match_array == NULL)
1993 compl_enter_selects = FALSE;
1994}
1995
1996/*
1997 * Return the length of the completion, from the completion start column to
1998 * the cursor column. Making sure it never goes below zero.
1999 */
2000 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002001get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002002{
2003 int off = (int)curwin->w_cursor.col - (int)compl_col;
2004
2005 if (off < 0)
2006 return 0;
2007 return off;
2008}
2009
2010/*
2011 * Append one character to the match leader. May reduce the number of
2012 * matches.
2013 */
2014 void
2015ins_compl_addleader(int c)
2016{
2017 int cc;
2018
2019 if (stop_arrow() == FAIL)
2020 return;
2021 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2022 {
2023 char_u buf[MB_MAXBYTES + 1];
2024
2025 (*mb_char2bytes)(c, buf);
2026 buf[cc] = NUL;
2027 ins_char_bytes(buf, cc);
2028 if (compl_opt_refresh_always)
2029 AppendToRedobuff(buf);
2030 }
2031 else
2032 {
2033 ins_char(c);
2034 if (compl_opt_refresh_always)
2035 AppendCharToRedobuff(c);
2036 }
2037
2038 // If we didn't complete finding matches we must search again.
2039 if (ins_compl_need_restart())
2040 ins_compl_restart();
2041
2042 // When 'always' is set, don't reset compl_leader. While completing,
2043 // cursor doesn't point original position, changing compl_leader would
2044 // break redo.
2045 if (!compl_opt_refresh_always)
2046 {
2047 vim_free(compl_leader);
2048 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002049 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002050 if (compl_leader != NULL)
2051 ins_compl_new_leader();
2052 }
2053}
2054
2055/*
2056 * Setup for finding completions again without leaving CTRL-X mode. Used when
2057 * BS or a key was typed while still searching for matches.
2058 */
2059 static void
2060ins_compl_restart(void)
2061{
2062 ins_compl_free();
2063 compl_started = FALSE;
2064 compl_matches = 0;
2065 compl_cont_status = 0;
2066 compl_cont_mode = 0;
2067}
2068
2069/*
2070 * Set the first match, the original text.
2071 */
2072 static void
2073ins_compl_set_original_text(char_u *str)
2074{
2075 char_u *p;
2076
2077 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002078 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2079 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002080 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002081 {
2082 p = vim_strsave(str);
2083 if (p != NULL)
2084 {
2085 vim_free(compl_first_match->cp_str);
2086 compl_first_match->cp_str = p;
2087 }
2088 }
2089 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002090 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002091 {
2092 p = vim_strsave(str);
2093 if (p != NULL)
2094 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002095 vim_free(compl_first_match->cp_prev->cp_str);
2096 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002097 }
2098 }
2099}
2100
2101/*
2102 * Append one character to the match leader. May reduce the number of
2103 * matches.
2104 */
2105 void
2106ins_compl_addfrommatch(void)
2107{
2108 char_u *p;
2109 int len = (int)curwin->w_cursor.col - (int)compl_col;
2110 int c;
2111 compl_T *cp;
2112
2113 p = compl_shown_match->cp_str;
2114 if ((int)STRLEN(p) <= len) // the match is too short
2115 {
2116 // When still at the original match use the first entry that matches
2117 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002118 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002119 return;
2120
2121 p = NULL;
2122 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002123 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002124 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002125 if (compl_leader == NULL
2126 || ins_compl_equal(cp, compl_leader,
2127 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002128 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002129 p = cp->cp_str;
2130 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002131 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002132 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002133 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002134 return;
2135 }
2136 p += len;
2137 c = PTR2CHAR(p);
2138 ins_compl_addleader(c);
2139}
2140
2141/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002142 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002143 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2144 * compl_cont_mode and compl_cont_status.
2145 * Returns TRUE when the character is not to be inserted.
2146 */
2147 static int
2148set_ctrl_x_mode(int c)
2149{
2150 int retval = FALSE;
2151
2152 switch (c)
2153 {
2154 case Ctrl_E:
2155 case Ctrl_Y:
2156 // scroll the window one line up or down
2157 ctrl_x_mode = CTRL_X_SCROLL;
2158 if (!(State & REPLACE_FLAG))
2159 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2160 else
2161 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2162 edit_submode_pre = NULL;
2163 showmode();
2164 break;
2165 case Ctrl_L:
2166 // complete whole line
2167 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2168 break;
2169 case Ctrl_F:
2170 // complete filenames
2171 ctrl_x_mode = CTRL_X_FILES;
2172 break;
2173 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002174 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002175 ctrl_x_mode = CTRL_X_DICTIONARY;
2176 break;
2177 case Ctrl_R:
2178 // Register insertion without exiting CTRL-X mode
2179 // Simply allow ^R to happen without affecting ^X mode
2180 break;
2181 case Ctrl_T:
2182 // complete words from a thesaurus
2183 ctrl_x_mode = CTRL_X_THESAURUS;
2184 break;
2185#ifdef FEAT_COMPL_FUNC
2186 case Ctrl_U:
2187 // user defined completion
2188 ctrl_x_mode = CTRL_X_FUNCTION;
2189 break;
2190 case Ctrl_O:
2191 // omni completion
2192 ctrl_x_mode = CTRL_X_OMNI;
2193 break;
2194#endif
2195 case 's':
2196 case Ctrl_S:
2197 // complete spelling suggestions
2198 ctrl_x_mode = CTRL_X_SPELL;
2199#ifdef FEAT_SPELL
2200 ++emsg_off; // Avoid getting the E756 error twice.
2201 spell_back_to_badword();
2202 --emsg_off;
2203#endif
2204 break;
2205 case Ctrl_RSB:
2206 // complete tag names
2207 ctrl_x_mode = CTRL_X_TAGS;
2208 break;
2209#ifdef FEAT_FIND_ID
2210 case Ctrl_I:
2211 case K_S_TAB:
2212 // complete keywords from included files
2213 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2214 break;
2215 case Ctrl_D:
2216 // complete definitions from included files
2217 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2218 break;
2219#endif
2220 case Ctrl_V:
2221 case Ctrl_Q:
2222 // complete vim commands
2223 ctrl_x_mode = CTRL_X_CMDLINE;
2224 break;
2225 case Ctrl_Z:
2226 // stop completion
2227 ctrl_x_mode = CTRL_X_NORMAL;
2228 edit_submode = NULL;
2229 showmode();
2230 retval = TRUE;
2231 break;
2232 case Ctrl_P:
2233 case Ctrl_N:
2234 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2235 // just started ^X mode, or there were enough ^X's to cancel
2236 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2237 // do normal expansion when interrupting a different mode (say
2238 // ^X^F^X^P or ^P^X^X^P, see below)
2239 // nothing changes if interrupting mode 0, (eg, the flag
2240 // doesn't change when going to ADDING mode -- Acevedo
2241 if (!(compl_cont_status & CONT_INTRPT))
2242 compl_cont_status |= CONT_LOCAL;
2243 else if (compl_cont_mode != 0)
2244 compl_cont_status &= ~CONT_LOCAL;
2245 // FALLTHROUGH
2246 default:
2247 // If we have typed at least 2 ^X's... for modes != 0, we set
2248 // compl_cont_status = 0 (eg, as if we had just started ^X
2249 // mode).
2250 // For mode 0, we set "compl_cont_mode" to an impossible
2251 // value, in both cases ^X^X can be used to restart the same
2252 // mode (avoiding ADDING mode).
2253 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2254 // 'complete' and local ^P expansions respectively.
2255 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2256 // mode -- Acevedo
2257 if (c == Ctrl_X)
2258 {
2259 if (compl_cont_mode != 0)
2260 compl_cont_status = 0;
2261 else
2262 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2263 }
2264 ctrl_x_mode = CTRL_X_NORMAL;
2265 edit_submode = NULL;
2266 showmode();
2267 break;
2268 }
2269
2270 return retval;
2271}
2272
2273/*
2274 * Stop insert completion mode
2275 */
2276 static int
2277ins_compl_stop(int c, int prev_mode, int retval)
2278{
2279 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002280 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002281
2282 // Get here when we have finished typing a sequence of ^N and
2283 // ^P or other completion characters in CTRL-X mode. Free up
2284 // memory that was used, and make sure we can redo the insert.
2285 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2286 {
2287 // If any of the original typed text has been changed, eg when
2288 // ignorecase is set, we must add back-spaces to the redo
2289 // buffer. We add as few as necessary to delete just the part
2290 // of the original text that has changed.
2291 // When using the longest match, edited the match or used
2292 // CTRL-E then don't use the current match.
2293 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2294 ptr = compl_curr_match->cp_str;
2295 else
2296 ptr = NULL;
2297 ins_compl_fixRedoBufForLeader(ptr);
2298 }
2299
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002300 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002301
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002302 // When completing whole lines: fix indent for 'cindent'.
2303 // Otherwise, break line if it's too long.
2304 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2305 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002306 // re-indent the current line
2307 if (want_cindent)
2308 {
2309 do_c_expr_indent();
2310 want_cindent = FALSE; // don't do it again
2311 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002312 }
2313 else
2314 {
2315 int prev_col = curwin->w_cursor.col;
2316
2317 // put the cursor on the last char, for 'tw' formatting
2318 if (prev_col > 0)
2319 dec_cursor();
2320 // only format when something was inserted
2321 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2322 insertchar(NUL, 0, -1);
2323 if (prev_col > 0
2324 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2325 inc_cursor();
2326 }
2327
2328 // If the popup menu is displayed pressing CTRL-Y means accepting
2329 // the selection without inserting anything. When
2330 // compl_enter_selects is set the Enter key does the same.
2331 if ((c == Ctrl_Y || (compl_enter_selects
2332 && (c == CAR || c == K_KENTER || c == NL)))
2333 && pum_visible())
2334 retval = TRUE;
2335
2336 // CTRL-E means completion is Ended, go back to the typed text.
2337 // but only do this, if the Popup is still visible
2338 if (c == Ctrl_E)
2339 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002340 char_u *p = NULL;
2341
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002342 ins_compl_delete();
2343 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002344 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002345 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002346 p = compl_orig_text;
2347 if (p != NULL)
2348 {
2349 int compl_len = get_compl_len();
2350 int len = (int)STRLEN(p);
2351
2352 if (len > compl_len)
2353 ins_bytes_len(p + compl_len, len - compl_len);
2354 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002355 retval = TRUE;
2356 }
2357
2358 auto_format(FALSE, TRUE);
2359
2360 // Trigger the CompleteDonePre event to give scripts a chance to
2361 // act upon the completion before clearing the info, and restore
2362 // ctrl_x_mode, so that complete_info() can be used.
2363 ctrl_x_mode = prev_mode;
2364 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2365
2366 ins_compl_free();
2367 compl_started = FALSE;
2368 compl_matches = 0;
2369 if (!shortmess(SHM_COMPLETIONMENU))
2370 msg_clr_cmdline(); // necessary for "noshowmode"
2371 ctrl_x_mode = CTRL_X_NORMAL;
2372 compl_enter_selects = FALSE;
2373 if (edit_submode != NULL)
2374 {
2375 edit_submode = NULL;
2376 showmode();
2377 }
2378
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002379 if (c == Ctrl_C && cmdwin_type != 0)
2380 // Avoid the popup menu remains displayed when leaving the
2381 // command line window.
2382 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002383 // Indent now if a key was typed that is in 'cinkeys'.
2384 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2385 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002386 // Trigger the CompleteDone event to give scripts a chance to act
2387 // upon the end of completion.
2388 ins_apply_autocmds(EVENT_COMPLETEDONE);
2389
2390 return retval;
2391}
2392
2393/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002394 * Prepare for Insert mode completion, or stop it.
2395 * Called just after typing a character in Insert mode.
2396 * Returns TRUE when the character is not to be inserted;
2397 */
2398 int
2399ins_compl_prep(int c)
2400{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002401 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002402 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002403
2404 // Forget any previous 'special' messages if this is actually
2405 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2406 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2407 edit_submode_extra = NULL;
2408
zeertzjq440d4cb2023-03-02 17:51:32 +00002409 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002410 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002411 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002412 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002413 return retval;
2414
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002415#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002416 // Ignore mouse events in a popup window
2417 if (is_mouse_key(c))
2418 {
2419 // Ignore drag and release events, the position does not need to be in
2420 // the popup and it may have just closed.
2421 if (c == K_LEFTRELEASE
2422 || c == K_LEFTRELEASE_NM
2423 || c == K_MIDDLERELEASE
2424 || c == K_RIGHTRELEASE
2425 || c == K_X1RELEASE
2426 || c == K_X2RELEASE
2427 || c == K_LEFTDRAG
2428 || c == K_MIDDLEDRAG
2429 || c == K_RIGHTDRAG
2430 || c == K_X1DRAG
2431 || c == K_X2DRAG)
2432 return retval;
2433 if (popup_visible)
2434 {
2435 int row = mouse_row;
2436 int col = mouse_col;
2437 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2438
2439 if (wp != NULL && WIN_IS_POPUP(wp))
2440 return retval;
2441 }
2442 }
2443#endif
2444
zeertzjqdca29d92021-08-31 19:12:51 +02002445 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2446 {
2447 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2448 || !vim_is_ctrl_x_key(c))
2449 {
2450 // Not starting another completion mode.
2451 ctrl_x_mode = CTRL_X_CMDLINE;
2452
2453 // CTRL-X CTRL-Z should stop completion without inserting anything
2454 if (c == Ctrl_Z)
2455 retval = TRUE;
2456 }
2457 else
2458 {
2459 ctrl_x_mode = CTRL_X_CMDLINE;
2460
2461 // Other CTRL-X keys first stop completion, then start another
2462 // completion mode.
2463 ins_compl_prep(' ');
2464 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2465 }
2466 }
2467
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002468 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002469 if (ctrl_x_mode_not_defined_yet()
2470 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002471 {
bfredl87af60c2022-09-24 11:17:51 +01002472 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002473 compl_used_match = TRUE;
2474
2475 }
2476
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002477 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002478 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2479 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002480 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002481 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002482 {
2483 // We're already in CTRL-X mode, do we stay in it?
2484 if (!vim_is_ctrl_x_key(c))
2485 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002486 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002487 ctrl_x_mode = CTRL_X_NORMAL;
2488 else
2489 ctrl_x_mode = CTRL_X_FINISHED;
2490 edit_submode = NULL;
2491 }
2492 showmode();
2493 }
2494
2495 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2496 {
2497 // Show error message from attempted keyword completion (probably
2498 // 'Pattern not found') until another key is hit, then go back to
2499 // showing what mode we are in.
2500 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002501 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002502 && c != Ctrl_R && !ins_compl_pum_key(c))
2503 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002504 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002505 }
2506 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2507 // Trigger the CompleteDone event to give scripts a chance to act
2508 // upon the (possibly failed) completion.
2509 ins_apply_autocmds(EVENT_COMPLETEDONE);
2510
LemonBoy2bf52dd2022-04-09 18:17:34 +01002511 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002512
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002513 // reset continue_* if we left expansion-mode, if we stay they'll be
2514 // (re)set properly in ins_complete()
2515 if (!vim_is_ctrl_x_key(c))
2516 {
2517 compl_cont_status = 0;
2518 compl_cont_mode = 0;
2519 }
2520
2521 return retval;
2522}
2523
2524/*
2525 * Fix the redo buffer for the completion leader replacing some of the typed
2526 * text. This inserts backspaces and appends the changed text.
2527 * "ptr" is the known leader text or NUL.
2528 */
2529 static void
2530ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2531{
2532 int len;
2533 char_u *p;
2534 char_u *ptr = ptr_arg;
2535
2536 if (ptr == NULL)
2537 {
2538 if (compl_leader != NULL)
2539 ptr = compl_leader;
2540 else
2541 return; // nothing to do
2542 }
2543 if (compl_orig_text != NULL)
2544 {
2545 p = compl_orig_text;
2546 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2547 ;
2548 if (len > 0)
2549 len -= (*mb_head_off)(p, p + len);
2550 for (p += len; *p != NUL; MB_PTR_ADV(p))
2551 AppendCharToRedobuff(K_BS);
2552 }
2553 else
2554 len = 0;
2555 if (ptr != NULL)
2556 AppendToRedobuffLit(ptr + len, -1);
2557}
2558
2559/*
2560 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2561 * (depending on flag) starting from buf and looking for a non-scanned
2562 * buffer (other than curbuf). curbuf is special, if it is called with
2563 * buf=curbuf then it has to be the first call for a given flag/expansion.
2564 *
2565 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2566 */
2567 static buf_T *
2568ins_compl_next_buf(buf_T *buf, int flag)
2569{
2570 static win_T *wp = NULL;
2571
2572 if (flag == 'w') // just windows
2573 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002574 if (buf == curbuf || !win_valid(wp))
2575 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002576 wp = curwin;
2577 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2578 && wp->w_buffer->b_scanned)
2579 ;
2580 buf = wp->w_buffer;
2581 }
2582 else
2583 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2584 // (unlisted buffers)
2585 // When completing whole lines skip unloaded buffers.
2586 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2587 && ((flag == 'U'
2588 ? buf->b_p_bl
2589 : (!buf->b_p_bl
2590 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2591 || buf->b_scanned))
2592 ;
2593 return buf;
2594}
2595
2596#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002597
2598# ifdef FEAT_EVAL
2599static callback_T cfu_cb; // 'completefunc' callback function
2600static callback_T ofu_cb; // 'omnifunc' callback function
2601static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2602# endif
2603
2604/*
2605 * Copy a global callback function to a buffer local callback.
2606 */
2607 static void
2608copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2609{
2610 free_callback(bufcb);
2611 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2612 copy_callback(bufcb, globcb);
2613}
2614
2615/*
2616 * Parse the 'completefunc' option value and set the callback function.
2617 * Invoked when the 'completefunc' option is set. The option value can be a
2618 * name of a function (string), or function(<name>) or funcref(<name>) or a
2619 * lambda expression.
2620 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002622did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002623{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002624 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2625 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002626
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002627 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002628
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002629 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002630}
2631
2632/*
2633 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002634 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002635 */
2636 void
2637set_buflocal_cfu_callback(buf_T *buf UNUSED)
2638{
2639# ifdef FEAT_EVAL
2640 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2641# endif
2642}
2643
2644/*
2645 * Parse the 'omnifunc' option value and set the callback function.
2646 * Invoked when the 'omnifunc' option is set. The option value can be a
2647 * name of a function (string), or function(<name>) or funcref(<name>) or a
2648 * lambda expression.
2649 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002650 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002651did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002653 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2654 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002655
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002656 set_buflocal_ofu_callback(curbuf);
2657 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002658}
2659
2660/*
2661 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002662 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002663 */
2664 void
2665set_buflocal_ofu_callback(buf_T *buf UNUSED)
2666{
2667# ifdef FEAT_EVAL
2668 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2669# endif
2670}
2671
2672/*
2673 * Parse the 'thesaurusfunc' option value and set the callback function.
2674 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2675 * name of a function (string), or function(<name>) or funcref(<name>) or a
2676 * lambda expression.
2677 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002678 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002679did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002680{
2681 int retval;
2682
2683 if (*curbuf->b_p_tsrfu != NUL)
2684 {
2685 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002686 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2687 &curbuf->b_tsrfu_cb);
2688 }
2689 else
2690 {
2691 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002692 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2693 }
2694
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002695 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002696}
2697
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002698/*
2699 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002700 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002701 */
2702 int
2703set_ref_in_insexpand_funcs(int copyID)
2704{
2705 int abort = FALSE;
2706
2707 abort = set_ref_in_callback(&cfu_cb, copyID);
2708 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2709 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2710
2711 return abort;
2712}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002713
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002714/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002715 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002716 */
2717 static char_u *
2718get_complete_funcname(int type)
2719{
2720 switch (type)
2721 {
2722 case CTRL_X_FUNCTION:
2723 return curbuf->b_p_cfu;
2724 case CTRL_X_OMNI:
2725 return curbuf->b_p_ofu;
2726 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002727 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002728 default:
2729 return (char_u *)"";
2730 }
2731}
2732
2733/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002734 * Get the callback to use for insert mode completion.
2735 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002736 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002737get_insert_callback(int type)
2738{
2739 if (type == CTRL_X_FUNCTION)
2740 return &curbuf->b_cfu_cb;
2741 if (type == CTRL_X_OMNI)
2742 return &curbuf->b_ofu_cb;
2743 // CTRL_X_THESAURUS
2744 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2745}
2746
2747/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002748 * Execute user defined complete function 'completefunc', 'omnifunc' or
2749 * 'thesaurusfunc', and get matches in "matches".
2750 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002751 */
2752 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002753expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002754{
2755 list_T *matchlist = NULL;
2756 dict_T *matchdict = NULL;
2757 typval_T args[3];
2758 char_u *funcname;
2759 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002760 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002761 typval_T rettv;
2762 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002763 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002764
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002765 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002766 if (*funcname == NUL)
2767 return;
2768
2769 // Call 'completefunc' to obtain the list of matches.
2770 args[0].v_type = VAR_NUMBER;
2771 args[0].vval.v_number = 0;
2772 args[1].v_type = VAR_STRING;
2773 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2774 args[2].v_type = VAR_UNKNOWN;
2775
2776 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002777 // Lock the text to avoid weird things from happening. Also disallow
2778 // switching to another window, it should not be needed and may end up in
2779 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002780 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002781
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002782 cb = get_insert_callback(type);
2783 retval = call_callback(cb, 0, &rettv, 2, args);
2784
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002785 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002786 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002787 {
2788 switch (rettv.v_type)
2789 {
2790 case VAR_LIST:
2791 matchlist = rettv.vval.v_list;
2792 break;
2793 case VAR_DICT:
2794 matchdict = rettv.vval.v_dict;
2795 break;
2796 case VAR_SPECIAL:
2797 if (rettv.vval.v_number == VVAL_NONE)
2798 compl_opt_suppress_empty = TRUE;
2799 // FALLTHROUGH
2800 default:
2801 // TODO: Give error message?
2802 clear_tv(&rettv);
2803 break;
2804 }
2805 }
zeertzjqcfe45652022-05-27 17:26:55 +01002806 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002807
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002808 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002809 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002810 validate_cursor();
2811 if (!EQUAL_POS(curwin->w_cursor, pos))
2812 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002813 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002814 goto theend;
2815 }
2816
2817 if (matchlist != NULL)
2818 ins_compl_add_list(matchlist);
2819 else if (matchdict != NULL)
2820 ins_compl_add_dict(matchdict);
2821
2822theend:
2823 // Restore State, it might have been changed.
2824 State = save_State;
2825
2826 if (matchdict != NULL)
2827 dict_unref(matchdict);
2828 if (matchlist != NULL)
2829 list_unref(matchlist);
2830}
2831#endif // FEAT_COMPL_FUNC
2832
2833#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2834/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002835 * Add a match to the list of matches from a typeval_T.
2836 * If the given string is already in the list of completions, then return
2837 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2838 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002839 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002840 */
2841 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002842ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002843{
2844 char_u *word;
2845 int dup = FALSE;
2846 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002847 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002848 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002849 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002850 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002851
Bram Moolenaar08928322020-01-04 14:32:48 +01002852 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002853 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2854 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002855 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2856 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2857 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2858 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2859 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2860 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2861 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2862 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002863 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002864 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2865 dup = dict_get_number(tv->vval.v_dict, "dup");
2866 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2867 empty = dict_get_number(tv->vval.v_dict, "empty");
2868 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2869 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002870 flags |= CP_EQUAL;
2871 }
2872 else
2873 {
2874 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002875 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002876 }
2877 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002878 {
2879 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002880 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002881 }
2882 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2883 if (status != OK)
2884 clear_tv(&user_data);
2885 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002886}
2887
2888/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002889 * Add completions from a list.
2890 */
2891 static void
2892ins_compl_add_list(list_T *list)
2893{
2894 listitem_T *li;
2895 int dir = compl_direction;
2896
2897 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002898 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002899 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002900 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002901 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002902 // if dir was BACKWARD then honor it just once
2903 dir = FORWARD;
2904 else if (did_emsg)
2905 break;
2906 }
2907}
2908
2909/*
2910 * Add completions from a dict.
2911 */
2912 static void
2913ins_compl_add_dict(dict_T *dict)
2914{
2915 dictitem_T *di_refresh;
2916 dictitem_T *di_words;
2917
2918 // Check for optional "refresh" item.
2919 compl_opt_refresh_always = FALSE;
2920 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2921 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2922 {
2923 char_u *v = di_refresh->di_tv.vval.v_string;
2924
2925 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2926 compl_opt_refresh_always = TRUE;
2927 }
2928
2929 // Add completions from a "words" list.
2930 di_words = dict_find(dict, (char_u *)"words", 5);
2931 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2932 ins_compl_add_list(di_words->di_tv.vval.v_list);
2933}
2934
2935/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002936 * Start completion for the complete() function.
2937 * "startcol" is where the matched text starts (1 is first column).
2938 * "list" is the list of matches.
2939 */
2940 static void
2941set_completion(colnr_T startcol, list_T *list)
2942{
2943 int save_w_wrow = curwin->w_wrow;
2944 int save_w_leftcol = curwin->w_leftcol;
2945 int flags = CP_ORIGINAL_TEXT;
2946
2947 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002948 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002949 ins_compl_prep(' ');
2950 ins_compl_clear();
2951 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002952 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002953
2954 compl_direction = FORWARD;
2955 if (startcol > curwin->w_cursor.col)
2956 startcol = curwin->w_cursor.col;
2957 compl_col = startcol;
2958 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2959 // compl_pattern doesn't need to be set
2960 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2961 if (p_ic)
2962 flags |= CP_ICASE;
2963 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002964 -1, NULL, NULL, NULL, 0,
2965 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002966 return;
2967
2968 ctrl_x_mode = CTRL_X_EVAL;
2969
2970 ins_compl_add_list(list);
2971 compl_matches = ins_compl_make_cyclic();
2972 compl_started = TRUE;
2973 compl_used_match = TRUE;
2974 compl_cont_status = 0;
2975
2976 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002977 int no_select = compl_no_select || compl_longest;
2978 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002979 {
2980 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002981 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002982 // Down/Up has no real effect.
2983 ins_complete(K_UP, FALSE);
2984 }
2985 else
2986 ins_complete(Ctrl_N, FALSE);
2987 compl_enter_selects = compl_no_insert;
2988
2989 // Lazily show the popup menu, unless we got interrupted.
2990 if (!compl_interrupted)
2991 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002992 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002993 out_flush();
2994}
2995
2996/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002998 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002999 void
3000f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003001{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003002 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003003
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003004 if (in_vim9script()
3005 && (check_for_number_arg(argvars, 0) == FAIL
3006 || check_for_list_arg(argvars, 1) == FAIL))
3007 return;
3008
Bram Moolenaar24959102022-05-07 20:01:16 +01003009 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003010 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003011 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003012 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003013 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003014
3015 // Check for undo allowed here, because if something was already inserted
3016 // the line was already saved for undo and this check isn't done.
3017 if (!undo_allowed())
3018 return;
3019
Bram Moolenaard83392a2022-09-01 12:22:46 +01003020 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02003021 {
3022 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
3023 if (startcol > 0)
3024 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003025 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003026}
3027
3028/*
3029 * "complete_add()" function
3030 */
3031 void
3032f_complete_add(typval_T *argvars, typval_T *rettv)
3033{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003034 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003035 return;
3036
Bram Moolenaar440cf092021-04-03 20:13:30 +02003037 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003038}
3039
3040/*
3041 * "complete_check()" function
3042 */
3043 void
3044f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
3045{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003046 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003047 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003048
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003049 ins_compl_check_keys(0, TRUE);
3050 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003051
3052 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003053}
3054
3055/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003056 * Return Insert completion mode name string
3057 */
3058 static char_u *
3059ins_compl_mode(void)
3060{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003061 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003062 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3063
3064 return (char_u *)"";
3065}
3066
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003067/*
3068 * Assign the sequence number to all the completion matches which don't have
3069 * one assigned yet.
3070 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003071 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003072ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003073{
3074 int number = 0;
3075 compl_T *match;
3076
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003077 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003078 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003079 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003080 // This should normally succeed already at the first loop
3081 // cycle, so it's fast!
3082 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003083 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003084 if (match->cp_number != -1)
3085 {
3086 number = match->cp_number;
3087 break;
3088 }
3089 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003090 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003091 for (match = match->cp_next;
3092 match != NULL && match->cp_number == -1;
3093 match = match->cp_next)
3094 match->cp_number = ++number;
3095 }
3096 else // BACKWARD
3097 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003098 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003099 // number. This should normally succeed already at the
3100 // first loop cycle, so it's fast!
3101 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003102 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003103 if (match->cp_number != -1)
3104 {
3105 number = match->cp_number;
3106 break;
3107 }
3108 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003109 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003110 for (match = match->cp_prev; match
3111 && match->cp_number == -1;
3112 match = match->cp_prev)
3113 match->cp_number = ++number;
3114 }
3115}
3116
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117/*
3118 * Get complete information
3119 */
3120 static void
3121get_complete_info(list_T *what_list, dict_T *retdict)
3122{
3123 int ret = OK;
3124 listitem_T *item;
3125#define CI_WHAT_MODE 0x01
3126#define CI_WHAT_PUM_VISIBLE 0x02
3127#define CI_WHAT_ITEMS 0x04
3128#define CI_WHAT_SELECTED 0x08
3129#define CI_WHAT_INSERTED 0x10
3130#define CI_WHAT_ALL 0xff
3131 int what_flag;
3132
3133 if (what_list == NULL)
3134 what_flag = CI_WHAT_ALL;
3135 else
3136 {
3137 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003138 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003139 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003140 {
3141 char_u *what = tv_get_string(&item->li_tv);
3142
3143 if (STRCMP(what, "mode") == 0)
3144 what_flag |= CI_WHAT_MODE;
3145 else if (STRCMP(what, "pum_visible") == 0)
3146 what_flag |= CI_WHAT_PUM_VISIBLE;
3147 else if (STRCMP(what, "items") == 0)
3148 what_flag |= CI_WHAT_ITEMS;
3149 else if (STRCMP(what, "selected") == 0)
3150 what_flag |= CI_WHAT_SELECTED;
3151 else if (STRCMP(what, "inserted") == 0)
3152 what_flag |= CI_WHAT_INSERTED;
3153 }
3154 }
3155
3156 if (ret == OK && (what_flag & CI_WHAT_MODE))
3157 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3158
3159 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3160 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3161
Girish Palya8950bf72024-03-20 20:07:29 +01003162 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003163 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003164 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003165 dict_T *di;
3166 compl_T *match;
3167 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003168
Girish Palya8950bf72024-03-20 20:07:29 +01003169 if (what_flag & CI_WHAT_ITEMS)
3170 {
3171 li = list_alloc();
3172 if (li == NULL)
3173 return;
3174 ret = dict_add_list(retdict, "items", li);
3175 }
3176 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3177 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3178 ins_compl_update_sequence_numbers();
3179 if (ret == OK && compl_first_match != NULL)
3180 {
3181 int list_idx = 0;
3182 match = compl_first_match;
3183 do
3184 {
3185 if (!match_at_original_text(match))
3186 {
3187 if (what_flag & CI_WHAT_ITEMS)
3188 {
3189 di = dict_alloc();
3190 if (di == NULL)
3191 return;
3192 ret = list_append_dict(li, di);
3193 if (ret != OK)
3194 return;
3195 dict_add_string(di, "word", match->cp_str);
3196 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3197 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3198 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3199 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3200 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3201 // Add an empty string for backwards compatibility
3202 dict_add_string(di, "user_data", (char_u *)"");
3203 else
3204 dict_add_tv(di, "user_data", &match->cp_user_data);
3205 }
3206 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3207 selected_idx = list_idx;
3208 list_idx += 1;
3209 }
3210 match = match->cp_next;
3211 }
3212 while (match != NULL && !is_first_match(match));
3213 }
3214 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3215 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003216 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003217
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003218 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3219 {
3220 // TODO
3221 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003222}
3223
3224/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003225 * "complete_info()" function
3226 */
3227 void
3228f_complete_info(typval_T *argvars, typval_T *rettv)
3229{
3230 list_T *what_list = NULL;
3231
Bram Moolenaar93a10962022-06-16 11:42:09 +01003232 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003233 return;
3234
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003235 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3236 return;
3237
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003238 if (argvars[0].v_type != VAR_UNKNOWN)
3239 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003240 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003241 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003242 what_list = argvars[0].vval.v_list;
3243 }
3244 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003245}
3246#endif
3247
3248/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003249 * Returns TRUE when using a user-defined function for thesaurus completion.
3250 */
3251 static int
3252thesaurus_func_complete(int type UNUSED)
3253{
3254#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003255 return type == CTRL_X_THESAURUS
3256 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003257#else
3258 return FALSE;
3259#endif
3260}
3261
3262/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 * Return value of process_next_cpt_value()
3264 */
3265enum
3266{
3267 INS_COMPL_CPT_OK = 1,
3268 INS_COMPL_CPT_CONT,
3269 INS_COMPL_CPT_END
3270};
3271
3272/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003273 * state information used for getting the next set of insert completion
3274 * matches.
3275 */
3276typedef struct
3277{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003278 char_u *e_cpt_copy; // copy of 'complete'
3279 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003280 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003281 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003282 pos_T prev_match_pos; // previous match position
3283 int set_match_pos; // save first_match_pos/last_match_pos
3284 pos_T first_match_pos; // first match position
3285 pos_T last_match_pos; // last match position
3286 int found_all; // found all matches of a certain type.
3287 char_u *dict; // dictionary file to search
3288 int dict_f; // "dict" is an exact file name or not
3289} ins_compl_next_state_T;
3290
3291/*
3292 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003293 *
3294 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003295 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003296 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003297 * st->found_all - all matches of this type are found
3298 * st->ins_buf - search for completions in this buffer
3299 * st->first_match_pos - position of the first completion match
3300 * st->last_match_pos - position of the last completion match
3301 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003302 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003303 * st->dict - name of the dictionary or thesaurus file to search
3304 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003305 *
3306 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003307 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3308 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003309 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003310 */
3311 static int
3312process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003313 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003314 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003317 int compl_type = -1;
3318 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003319
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003320 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003321
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003322 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3323 st->e_cpt++;
3324
3325 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003327 st->ins_buf = curbuf;
3328 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003329 // Move the cursor back one character so that ^N can match the
3330 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003331 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003332 {
3333 // Move the cursor to after the last character in the
3334 // buffer, so that word at start of buffer is found
3335 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003336 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003337 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003338 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003339 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 compl_type = 0;
3341
3342 // Remember the first match so that the loop stops when we
3343 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003344 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003345 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003346 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003347 && (st->ins_buf = ins_compl_next_buf(
3348 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003349 {
3350 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003351 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003352 {
3353 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003354 st->first_match_pos.col = st->last_match_pos.col = 0;
3355 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3356 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003357 compl_type = 0;
3358 }
3359 else // unloaded buffer, scan like dictionary
3360 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003361 st->found_all = TRUE;
3362 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003363 {
3364 status = INS_COMPL_CPT_CONT;
3365 goto done;
3366 }
3367 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003368 st->dict = st->ins_buf->b_fname;
3369 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003370 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003371 if (!shortmess(SHM_COMPLETIONSCAN))
3372 {
3373 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3374 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3375 st->ins_buf->b_fname == NULL
3376 ? buf_spname(st->ins_buf)
3377 : st->ins_buf->b_sfname == NULL
3378 ? st->ins_buf->b_fname
3379 : st->ins_buf->b_sfname);
3380 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3381 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003382 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003383 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003384 status = INS_COMPL_CPT_END;
3385 else
3386 {
3387 if (ctrl_x_mode_line_or_eval())
3388 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003389 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003390 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003391 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003392 compl_type = CTRL_X_DICTIONARY;
3393 else
3394 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003395 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003396 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003397 st->dict = st->e_cpt;
3398 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003399 }
3400 }
3401#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003402 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003404 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003405 compl_type = CTRL_X_PATH_DEFINES;
3406#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003407 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003409 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003410 if (!shortmess(SHM_COMPLETIONSCAN))
3411 {
3412 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3413 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3414 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3415 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003416 }
3417 else
3418 compl_type = -1;
3419
3420 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003421 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003422
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003423 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003424 if (compl_type == -1)
3425 status = INS_COMPL_CPT_CONT;
3426 }
3427
3428done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003429 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003430 return status;
3431}
3432
3433#ifdef FEAT_FIND_ID
3434/*
3435 * Get the next set of identifiers or defines matching "compl_pattern" in
3436 * included files.
3437 */
3438 static void
3439get_next_include_file_completion(int compl_type)
3440{
3441 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003442 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003443 (compl_type == CTRL_X_PATH_DEFINES
3444 && !(compl_cont_status & CONT_SOL))
3445 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003446 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003447}
3448#endif
3449
3450/*
3451 * Get the next set of words matching "compl_pattern" in dictionary or
3452 * thesaurus files.
3453 */
3454 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003455get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003456{
3457#ifdef FEAT_COMPL_FUNC
3458 if (thesaurus_func_complete(compl_type))
3459 expand_by_function(compl_type, compl_pattern);
3460 else
3461#endif
3462 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003463 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003464 : (compl_type == CTRL_X_THESAURUS
3465 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3466 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3467 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003468 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003469 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003470}
3471
3472/*
3473 * Get the next set of tag names matching "compl_pattern".
3474 */
3475 static void
3476get_next_tag_completion(void)
3477{
3478 int save_p_ic;
3479 char_u **matches;
3480 int num_matches;
3481
3482 // set p_ic according to p_ic, p_scs and pat for find_tags().
3483 save_p_ic = p_ic;
3484 p_ic = ignorecase(compl_pattern);
3485
3486 // Find up to TAG_MANY matches. Avoids that an enormous number
3487 // of matches is found when compl_pattern is empty
3488 g_tag_at_cursor = TRUE;
3489 if (find_tags(compl_pattern, &num_matches, &matches,
3490 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003491 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003492 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3493 ins_compl_add_matches(num_matches, matches, p_ic);
3494 g_tag_at_cursor = FALSE;
3495 p_ic = save_p_ic;
3496}
3497
3498/*
3499 * Get the next set of filename matching "compl_pattern".
3500 */
3501 static void
3502get_next_filename_completion(void)
3503{
3504 char_u **matches;
3505 int num_matches;
3506
3507 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3508 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3509 return;
3510
3511 // May change home directory back to "~".
3512 tilde_replace(compl_pattern, num_matches, matches);
3513#ifdef BACKSLASH_IN_FILENAME
3514 if (curbuf->b_p_csl[0] != NUL)
3515 {
3516 int i;
3517
3518 for (i = 0; i < num_matches; ++i)
3519 {
3520 char_u *ptr = matches[i];
3521
3522 while (*ptr != NUL)
3523 {
3524 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3525 *ptr = '/';
3526 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3527 *ptr = '\\';
3528 ptr += (*mb_ptr2len)(ptr);
3529 }
3530 }
3531 }
3532#endif
3533 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3534}
3535
3536/*
3537 * Get the next set of command-line completions matching "compl_pattern".
3538 */
3539 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003540get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003541{
3542 char_u **matches;
3543 int num_matches;
3544
3545 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003546 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003547 ins_compl_add_matches(num_matches, matches, FALSE);
3548}
3549
3550/*
3551 * Get the next set of spell suggestions matching "compl_pattern".
3552 */
3553 static void
3554get_next_spell_completion(linenr_T lnum UNUSED)
3555{
3556#ifdef FEAT_SPELL
3557 char_u **matches;
3558 int num_matches;
3559
3560 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3561 if (num_matches > 0)
3562 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003563 else
3564 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003565#endif
3566}
3567
3568/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003569 * Return the next word or line from buffer "ins_buf" at position
3570 * "cur_match_pos" for completion. The length of the match is set in "len".
3571 */
3572 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003573ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003574 buf_T *ins_buf, // buffer being scanned
3575 pos_T *cur_match_pos, // current match position
3576 int *match_len,
3577 int *cont_s_ipos) // next ^X<> will set initial_pos
3578{
3579 char_u *ptr;
3580 int len;
3581
3582 *match_len = 0;
3583 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3584 cur_match_pos->col;
3585 if (ctrl_x_mode_line_or_eval())
3586 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003587 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003588 {
3589 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3590 return NULL;
3591 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3592 if (!p_paste)
3593 ptr = skipwhite(ptr);
3594 }
3595 len = (int)STRLEN(ptr);
3596 }
3597 else
3598 {
3599 char_u *tmp_ptr = ptr;
3600
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003601 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003602 {
3603 tmp_ptr += compl_length;
3604 // Skip if already inside a word.
3605 if (vim_iswordp(tmp_ptr))
3606 return NULL;
3607 // Find start of next word.
3608 tmp_ptr = find_word_start(tmp_ptr);
3609 }
3610 // Find end of this word.
3611 tmp_ptr = find_word_end(tmp_ptr);
3612 len = (int)(tmp_ptr - ptr);
3613
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003614 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003615 {
3616 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3617 {
3618 // Try next line, if any. the new word will be
3619 // "join" as if the normal command "J" was used.
3620 // IOSIZE is always greater than
3621 // compl_length, so the next STRNCPY always
3622 // works -- Acevedo
3623 STRNCPY(IObuff, ptr, len);
3624 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3625 tmp_ptr = ptr = skipwhite(ptr);
3626 // Find start of next word.
3627 tmp_ptr = find_word_start(tmp_ptr);
3628 // Find end of next word.
3629 tmp_ptr = find_word_end(tmp_ptr);
3630 if (tmp_ptr > ptr)
3631 {
3632 if (*ptr != ')' && IObuff[len - 1] != TAB)
3633 {
3634 if (IObuff[len - 1] != ' ')
3635 IObuff[len++] = ' ';
3636 // IObuf =~ "\k.* ", thus len >= 2
3637 if (p_js
3638 && (IObuff[len - 2] == '.'
3639 || (vim_strchr(p_cpo, CPO_JOINSP)
3640 == NULL
3641 && (IObuff[len - 2] == '?'
3642 || IObuff[len - 2] == '!'))))
3643 IObuff[len++] = ' ';
3644 }
3645 // copy as much as possible of the new word
3646 if (tmp_ptr - ptr >= IOSIZE - len)
3647 tmp_ptr = ptr + IOSIZE - len - 1;
3648 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3649 len += (int)(tmp_ptr - ptr);
3650 *cont_s_ipos = TRUE;
3651 }
3652 IObuff[len] = NUL;
3653 ptr = IObuff;
3654 }
3655 if (len == compl_length)
3656 return NULL;
3657 }
3658 }
3659
3660 *match_len = len;
3661 return ptr;
3662}
3663
3664/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 * Get the next set of words matching "compl_pattern" for default completion(s)
3666 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003667 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3668 * position "st->start_pos" in the "compl_direction" direction. If
3669 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3670 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003671 * Returns OK if a new next match is found, otherwise returns FAIL.
3672 */
3673 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003674get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675{
3676 int found_new_match = FAIL;
3677 int save_p_scs;
3678 int save_p_ws;
3679 int looped_around = FALSE;
3680 char_u *ptr;
3681 int len;
3682
3683 // If 'infercase' is set, don't use 'smartcase' here
3684 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003685 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003686 p_scs = FALSE;
3687
3688 // Buffers other than curbuf are scanned from the beginning or the
3689 // end but never from the middle, thus setting nowrapscan in this
3690 // buffer is a good idea, on the other hand, we always set
3691 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3692 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003693 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003694 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003695 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003696 p_ws = TRUE;
3697 looped_around = FALSE;
3698 for (;;)
3699 {
3700 int cont_s_ipos = FALSE;
3701
3702 ++msg_silent; // Don't want messages for wrapscan.
3703
3704 // ctrl_x_mode_line_or_eval() || word-wise search that
3705 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003706 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003707 found_new_match = search_for_exact_line(st->ins_buf,
3708 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003709 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003710 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003711 NULL, compl_direction, compl_pattern, compl_patternlen,
3712 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003713 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003714 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003715 {
3716 // set "compl_started" even on fail
3717 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003718 st->first_match_pos = *st->cur_match_pos;
3719 st->last_match_pos = *st->cur_match_pos;
3720 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003721 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003722 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3723 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003724 {
3725 found_new_match = FAIL;
3726 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003727 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003728 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3729 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3730 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003731 {
3732 if (looped_around)
3733 found_new_match = FAIL;
3734 else
3735 looped_around = TRUE;
3736 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003737 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003738 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3739 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3740 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003741 {
3742 if (looped_around)
3743 found_new_match = FAIL;
3744 else
3745 looped_around = TRUE;
3746 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003747 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003748 if (found_new_match == FAIL)
3749 break;
3750
3751 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003752 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003753 && start_pos->lnum == st->cur_match_pos->lnum
3754 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003755 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003756
zeertzjq440d4cb2023-03-02 17:51:32 +00003757 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3758 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003759 if (ptr == NULL)
3760 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003761
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003762 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003763 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003764 0, cont_s_ipos) != NOTDONE)
3765 {
3766 found_new_match = OK;
3767 break;
3768 }
3769 }
3770 p_scs = save_p_scs;
3771 p_ws = save_p_ws;
3772
3773 return found_new_match;
3774}
3775
3776/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003777 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003778 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003779 */
3780 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003781get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003782{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003783 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003784
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003785 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003786 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003787 case -1:
3788 break;
3789#ifdef FEAT_FIND_ID
3790 case CTRL_X_PATH_PATTERNS:
3791 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003792 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003793 break;
3794#endif
3795
3796 case CTRL_X_DICTIONARY:
3797 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003798 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3799 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003800 break;
3801
3802 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003803 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003804 break;
3805
3806 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003807 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003808 break;
3809
3810 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003811 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003812 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003813 break;
3814
3815#ifdef FEAT_COMPL_FUNC
3816 case CTRL_X_FUNCTION:
3817 case CTRL_X_OMNI:
3818 expand_by_function(type, compl_pattern);
3819 break;
3820#endif
3821
3822 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003823 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003824 break;
3825
3826 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003827 found_new_match = get_next_default_completion(st, ini);
3828 if (found_new_match == FAIL && st->ins_buf == curbuf)
3829 st->found_all = TRUE;
3830 }
3831
3832 // check if compl_curr_match has changed, (e.g. other type of
3833 // expansion added something)
3834 if (type != 0 && compl_curr_match != compl_old_match)
3835 found_new_match = OK;
3836
3837 return found_new_match;
3838}
3839
3840/*
3841 * Get the next expansion(s), using "compl_pattern".
3842 * The search starts at position "ini" in curbuf and in the direction
3843 * compl_direction.
3844 * When "compl_started" is FALSE start at that position, otherwise continue
3845 * where we stopped searching before.
3846 * This may return before finding all the matches.
3847 * Return the total number of matches or -1 if still unknown -- Acevedo
3848 */
3849 static int
3850ins_compl_get_exp(pos_T *ini)
3851{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003852 static ins_compl_next_state_T st;
3853 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003854 int i;
3855 int found_new_match;
3856 int type = ctrl_x_mode;
3857
3858 if (!compl_started)
3859 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003860 buf_T *buf;
3861
3862 FOR_ALL_BUFFERS(buf)
3863 buf->b_scanned = 0;
3864 if (!st_cleared)
3865 {
3866 CLEAR_FIELD(st);
3867 st_cleared = TRUE;
3868 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003869 st.found_all = FALSE;
3870 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003871 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003872 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003873 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3874 ? (char_u *)"." : curbuf->b_p_cpt);
3875 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003876 st.last_match_pos = st.first_match_pos = *ini;
3877 }
3878 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3879 st.ins_buf = curbuf; // In case the buffer was wiped out.
3880
3881 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003882 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003883 ? &st.last_match_pos : &st.first_match_pos;
3884
3885 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3886 for (;;)
3887 {
3888 found_new_match = FAIL;
3889 st.set_match_pos = FALSE;
3890
3891 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3892 // or if found_all says this entry is done. For ^X^L only use the
3893 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003894 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003895 && (!compl_started || st.found_all))
3896 {
3897 int status = process_next_cpt_value(&st, &type, ini);
3898
3899 if (status == INS_COMPL_CPT_END)
3900 break;
3901 if (status == INS_COMPL_CPT_CONT)
3902 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003903 }
3904
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003905 // If complete() was called then compl_pattern has been reset. The
3906 // following won't work then, bail out.
3907 if (compl_pattern == NULL)
3908 break;
3909
3910 // get the next set of completion matches
3911 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003912
3913 // break the loop for specialized modes (use 'complete' just for the
3914 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3915 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003916 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3917 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003918 {
3919 if (got_int)
3920 break;
3921 // Fill the popup menu as soon as possible.
3922 if (type != -1)
3923 ins_compl_check_keys(0, FALSE);
3924
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003925 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003926 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3927 break;
3928 compl_started = TRUE;
3929 }
3930 else
3931 {
3932 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003933 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003934 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003935
3936 compl_started = FALSE;
3937 }
3938 }
3939 compl_started = TRUE;
3940
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003941 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003942 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003943 found_new_match = FAIL;
3944
3945 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003946 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 && !ctrl_x_mode_line_or_eval()))
3948 i = ins_compl_make_cyclic();
3949
3950 if (compl_old_match != NULL)
3951 {
3952 // If several matches were added (FORWARD) or the search failed and has
3953 // just been made cyclic then we have to move compl_curr_match to the
3954 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003955 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003956 : compl_old_match->cp_prev;
3957 if (compl_curr_match == NULL)
3958 compl_curr_match = compl_old_match;
3959 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003960 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003961
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003962 return i;
3963}
3964
3965/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003966 * Update "compl_shown_match" to the actually shown match, it may differ when
3967 * "compl_leader" is used to omit some of the matches.
3968 */
3969 static void
3970ins_compl_update_shown_match(void)
3971{
3972 while (!ins_compl_equal(compl_shown_match,
3973 compl_leader, (int)STRLEN(compl_leader))
3974 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003975 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003976 compl_shown_match = compl_shown_match->cp_next;
3977
3978 // If we didn't find it searching forward, and compl_shows_dir is
3979 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003980 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003981 && !ins_compl_equal(compl_shown_match,
3982 compl_leader, (int)STRLEN(compl_leader))
3983 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003984 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003985 {
3986 while (!ins_compl_equal(compl_shown_match,
3987 compl_leader, (int)STRLEN(compl_leader))
3988 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003989 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003990 compl_shown_match = compl_shown_match->cp_prev;
3991 }
3992}
3993
3994/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003995 * Delete the old text being completed.
3996 */
3997 void
3998ins_compl_delete(void)
3999{
4000 int col;
4001
4002 // In insert mode: Delete the typed part.
4003 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004004 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004005 if ((int)curwin->w_cursor.col > col)
4006 {
4007 if (stop_arrow() == FAIL)
4008 return;
4009 backspace_until_column(col);
4010 }
4011
4012 // TODO: is this sufficient for redrawing? Redrawing everything causes
4013 // flicker, thus we can't do that.
4014 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004015#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004016 // clear v:completed_item
4017 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004018#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004019}
4020
4021/*
4022 * Insert the new text being completed.
4023 * "in_compl_func" is TRUE when called from complete_check().
4024 */
4025 void
4026ins_compl_insert(int in_compl_func)
4027{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004028 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00004029
4030 // Make sure we don't go over the end of the string, this can happen with
4031 // illegal bytes.
4032 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
4033 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004034 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004035 compl_used_match = FALSE;
4036 else
4037 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02004038#ifdef FEAT_EVAL
4039 {
4040 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4041
4042 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4043 }
4044#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004045 if (!in_compl_func)
4046 compl_curr_match = compl_shown_match;
4047}
4048
4049/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004050 * show the file name for the completion match (if any). Truncate the file
4051 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004052 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004053 static void
4054ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004055{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004056 char *lead = _("match in file");
4057 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4058 char_u *s;
4059 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004060
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004061 if (space <= 0)
4062 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004063
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004064 // We need the tail that fits. With double-byte encoding going
4065 // back from the end is very slow, thus go from the start and keep
4066 // the text that fits in "space" between "s" and "e".
4067 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004068 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004069 space -= ptr2cells(e);
4070 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004071 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004072 space += ptr2cells(s);
4073 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 }
4075 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004076 msg_hist_off = TRUE;
4077 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4078 s > compl_shown_match->cp_fname ? "<" : "", s);
4079 msg((char *)IObuff);
4080 msg_hist_off = FALSE;
4081 redraw_cmdline = FALSE; // don't overwrite!
4082}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004083
glepnirdca57fb2024-06-04 22:01:21 +02004084/*
4085 * find a completion item in when completeopt include fuzzy option
4086 */
glepnira218cc62024-06-03 19:32:39 +02004087 static compl_T *
4088find_comp_when_fuzzy(void)
4089{
4090 int score;
4091 char_u* str;
4092 int target_idx = -1;
4093 int is_forward = compl_shows_dir_forward();
4094 int is_backward = compl_shows_dir_backward();
4095 compl_T *comp = NULL;
4096
4097 if (compl_match_array == NULL ||
4098 (is_forward && compl_selected_item == compl_match_arraysize - 1)
4099 || (is_backward && compl_selected_item == 0))
4100 return compl_first_match;
4101
4102 if (is_forward)
4103 target_idx = compl_selected_item + 1;
4104 else if (is_backward)
4105 target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1
glepnirdca57fb2024-06-04 22:01:21 +02004106 : compl_selected_item - 1;
glepnira218cc62024-06-03 19:32:39 +02004107
4108 score = compl_match_array[target_idx].pum_score;
4109 str = compl_match_array[target_idx].pum_text;
4110
4111 comp = compl_first_match;
4112 do {
4113 if (comp->cp_score == score && (str == comp->cp_str || str == comp->cp_text[CPT_ABBR]))
4114 return comp;
4115 comp = comp->cp_next;
4116 } while (comp != NULL && !is_first_match(comp));
4117
4118 return NULL;
4119}
4120
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004121/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004122 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004123 * times. The number of matches found is returned in 'num_matches'.
4124 *
4125 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4126 * get more completions. If it is FALSE, then do nothing when there are no more
4127 * completions in the given direction.
4128 *
4129 * If "advance" is TRUE, then completion will move to the first match.
4130 * Otherwise, the original text will be shown.
4131 *
4132 * Returns OK on success and -1 if the number of matches are unknown.
4133 */
4134 static int
4135find_next_completion_match(
4136 int allow_get_expansion,
4137 int todo, // repeat completion this many times
4138 int advance,
4139 int *num_matches)
4140{
4141 int found_end = FALSE;
4142 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004143
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004144 while (--todo >= 0)
4145 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004146 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004147 {
glepnira218cc62024-06-03 19:32:39 +02004148 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_next
4149 : find_comp_when_fuzzy();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004150 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004151 && (is_first_match(compl_shown_match->cp_next)
4152 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004153 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004154 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004155 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004156 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004157 found_end = is_first_match(compl_shown_match);
glepnira218cc62024-06-03 19:32:39 +02004158 compl_shown_match = !compl_fuzzy_match ? compl_shown_match->cp_prev
4159 : find_comp_when_fuzzy();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004160 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004161 }
4162 else
4163 {
4164 if (!allow_get_expansion)
4165 {
4166 if (advance)
4167 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004168 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004169 compl_pending -= todo + 1;
4170 else
4171 compl_pending += todo + 1;
4172 }
4173 return -1;
4174 }
4175
4176 if (!compl_no_select && advance)
4177 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004178 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004179 --compl_pending;
4180 else
4181 ++compl_pending;
4182 }
4183
4184 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004185 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004186
4187 // handle any pending completions
4188 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004189 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004190 {
4191 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4192 {
4193 compl_shown_match = compl_shown_match->cp_next;
4194 --compl_pending;
4195 }
4196 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4197 {
4198 compl_shown_match = compl_shown_match->cp_prev;
4199 ++compl_pending;
4200 }
4201 else
4202 break;
4203 }
4204 found_end = FALSE;
4205 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004206 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004207 && compl_leader != NULL
4208 && !ins_compl_equal(compl_shown_match,
glepnira218cc62024-06-03 19:32:39 +02004209 compl_leader, (int)STRLEN(compl_leader))
4210 && !(compl_fuzzy_match && compl_shown_match->cp_score > 0))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004211 ++todo;
4212 else
4213 // Remember a matching item.
4214 found_compl = compl_shown_match;
4215
4216 // Stop at the end of the list when we found a usable match.
4217 if (found_end)
4218 {
4219 if (found_compl != NULL)
4220 {
4221 compl_shown_match = found_compl;
4222 break;
4223 }
4224 todo = 1; // use first usable match after wrapping around
4225 }
4226 }
4227
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004228 return OK;
4229}
4230
4231/*
4232 * Fill in the next completion in the current direction.
4233 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4234 * get more completions. If it is FALSE, then we just do nothing when there
4235 * are no more completions in a given direction. The latter case is used when
4236 * we are still in the middle of finding completions, to allow browsing
4237 * through the ones found so far.
4238 * Return the total number of matches, or -1 if still unknown -- webb.
4239 *
4240 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4241 * compl_shown_match here.
4242 *
4243 * Note that this function may be called recursively once only. First with
4244 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4245 * calls this function with "allow_get_expansion" FALSE.
4246 */
4247 static int
4248ins_compl_next(
4249 int allow_get_expansion,
4250 int count, // repeat completion this many times; should
4251 // be at least 1
4252 int insert_match, // Insert the newly selected match
4253 int in_compl_func) // called from complete_check()
4254{
4255 int num_matches = -1;
4256 int todo = count;
4257 int advance;
4258 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004259 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004260
4261 // When user complete function return -1 for findstart which is next
4262 // time of 'always', compl_shown_match become NULL.
4263 if (compl_shown_match == NULL)
4264 return -1;
4265
glepnira218cc62024-06-03 19:32:39 +02004266 if (compl_leader != NULL
4267 && !match_at_original_text(compl_shown_match)
4268 && !compl_fuzzy_match)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004269 // Update "compl_shown_match" to the actually shown match
4270 ins_compl_update_shown_match();
4271
4272 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004273 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004274 // Delete old text to be replaced
4275 ins_compl_delete();
4276
4277 // When finding the longest common text we stick at the original text,
4278 // don't let CTRL-N or CTRL-P move to the first match.
4279 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4280
4281 // When restarting the search don't insert the first match either.
4282 if (compl_restarting)
4283 {
4284 advance = FALSE;
4285 compl_restarting = FALSE;
4286 }
4287
4288 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4289 // around.
4290 if (find_next_completion_match(allow_get_expansion, todo, advance,
4291 &num_matches) == -1)
4292 return -1;
4293
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004294 if (curbuf != orig_curbuf)
4295 {
4296 // In case some completion function switched buffer, don't want to
4297 // insert the completion elsewhere.
4298 return -1;
4299 }
4300
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004301 // Insert the text of the new completion, or the compl_leader.
4302 if (compl_no_insert && !started)
4303 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004304 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004305 compl_used_match = FALSE;
4306 }
4307 else if (insert_match)
4308 {
4309 if (!compl_get_longest || compl_used_match)
4310 ins_compl_insert(in_compl_func);
4311 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004312 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004313 }
4314 else
4315 compl_used_match = FALSE;
4316
4317 if (!allow_get_expansion)
4318 {
4319 // may undisplay the popup menu first
4320 ins_compl_upd_pum();
4321
4322 if (pum_enough_matches())
4323 // Will display the popup menu, don't redraw yet to avoid flicker.
4324 pum_call_update_screen();
4325 else
4326 // Not showing the popup menu yet, redraw to show the user what was
4327 // inserted.
4328 update_screen(0);
4329
4330 // display the updated popup menu
4331 ins_compl_show_pum();
4332#ifdef FEAT_GUI
4333 if (gui.in_use)
4334 {
4335 // Show the cursor after the match, not after the redrawn text.
4336 setcursor();
4337 out_flush_cursor(FALSE, FALSE);
4338 }
4339#endif
4340
4341 // Delete old text to be replaced, since we're still searching and
4342 // don't want to match ourselves!
4343 ins_compl_delete();
4344 }
4345
4346 // Enter will select a match when the match wasn't inserted and the popup
4347 // menu is visible.
4348 if (compl_no_insert && !started)
4349 compl_enter_selects = TRUE;
4350 else
4351 compl_enter_selects = !insert_match && compl_match_array != NULL;
4352
4353 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004354 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004355 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004356
4357 return num_matches;
4358}
4359
4360/*
4361 * Call this while finding completions, to check whether the user has hit a key
4362 * that should change the currently displayed completion, or exit completion
4363 * mode. Also, when compl_pending is not zero, show a completion as soon as
4364 * possible. -- webb
4365 * "frequency" specifies out of how many calls we actually check.
4366 * "in_compl_func" is TRUE when called from complete_check(), don't set
4367 * compl_curr_match.
4368 */
4369 void
4370ins_compl_check_keys(int frequency, int in_compl_func)
4371{
4372 static int count = 0;
4373 int c;
4374
4375 // Don't check when reading keys from a script, :normal or feedkeys().
4376 // That would break the test scripts. But do check for keys when called
4377 // from complete_check().
4378 if (!in_compl_func && (using_script() || ex_normal_busy))
4379 return;
4380
4381 // Only do this at regular intervals
4382 if (++count < frequency)
4383 return;
4384 count = 0;
4385
4386 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4387 // can't do its work correctly.
4388 c = vpeekc_any();
4389 if (c != NUL)
4390 {
4391 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4392 {
4393 c = safe_vgetc(); // Eat the character
4394 compl_shows_dir = ins_compl_key2dir(c);
4395 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4396 c != K_UP && c != K_DOWN, in_compl_func);
4397 }
4398 else
4399 {
4400 // Need to get the character to have KeyTyped set. We'll put it
4401 // back with vungetc() below. But skip K_IGNORE.
4402 c = safe_vgetc();
4403 if (c != K_IGNORE)
4404 {
4405 // Don't interrupt completion when the character wasn't typed,
4406 // e.g., when doing @q to replay keys.
4407 if (c != Ctrl_R && KeyTyped)
4408 compl_interrupted = TRUE;
4409
4410 vungetc(c);
4411 }
4412 }
4413 }
4414 if (compl_pending != 0 && !got_int && !compl_no_insert)
4415 {
4416 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4417
4418 compl_pending = 0;
4419 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4420 }
4421}
4422
4423/*
4424 * Decide the direction of Insert mode complete from the key typed.
4425 * Returns BACKWARD or FORWARD.
4426 */
4427 static int
4428ins_compl_key2dir(int c)
4429{
4430 if (c == Ctrl_P || c == Ctrl_L
4431 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4432 return BACKWARD;
4433 return FORWARD;
4434}
4435
4436/*
4437 * Return TRUE for keys that are used for completion only when the popup menu
4438 * is visible.
4439 */
4440 static int
4441ins_compl_pum_key(int c)
4442{
4443 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4444 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4445 || c == K_UP || c == K_DOWN);
4446}
4447
4448/*
4449 * Decide the number of completions to move forward.
4450 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4451 */
4452 static int
4453ins_compl_key2count(int c)
4454{
4455 int h;
4456
4457 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4458 {
4459 h = pum_get_height();
4460 if (h > 3)
4461 h -= 2; // keep some context
4462 return h;
4463 }
4464 return 1;
4465}
4466
4467/*
4468 * Return TRUE if completion with "c" should insert the match, FALSE if only
4469 * to change the currently selected completion.
4470 */
4471 static int
4472ins_compl_use_match(int c)
4473{
4474 switch (c)
4475 {
4476 case K_UP:
4477 case K_DOWN:
4478 case K_PAGEDOWN:
4479 case K_KPAGEDOWN:
4480 case K_S_DOWN:
4481 case K_PAGEUP:
4482 case K_KPAGEUP:
4483 case K_S_UP:
4484 return FALSE;
4485 }
4486 return TRUE;
4487}
4488
4489/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004490 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4491 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004492 * Sets the global variables: compl_col, compl_length, compl_pattern and
4493 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004494 * Uses the global variables: compl_cont_status and ctrl_x_mode
4495 */
4496 static int
4497get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4498{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004499 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004500 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004501 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004502 {
4503 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4504 ;
4505 compl_col += ++startcol;
4506 compl_length = curs_col - startcol;
4507 }
4508 if (p_ic)
4509 compl_pattern = str_foldcase(line + compl_col,
4510 compl_length, NULL, 0);
4511 else
4512 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4513 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004514 {
4515 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004516 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004517 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004518 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004519 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004520 {
4521 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004522 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004523
4524 // we need up to 2 extra chars for the prefix
4525 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004526 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004527 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004528 {
4529 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004530 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004531 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004532 if (!vim_iswordp(line + compl_col)
4533 || (compl_col > 0
4534 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004535 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004536 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004537 prefixlen = 0;
4538 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004539 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004540 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004541 line + compl_col, compl_length);
4542 }
4543 else if (--startcol < 0
4544 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4545 {
4546 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004547 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004548 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004549 {
4550 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004551 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004552 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004553 compl_col += curs_col;
4554 compl_length = 0;
4555 }
4556 else
4557 {
4558 // Search the point of change class of multibyte character
4559 // or not a word single byte character backward.
4560 if (has_mbyte)
4561 {
4562 int base_class;
4563 int head_off;
4564
4565 startcol -= (*mb_head_off)(line, line + startcol);
4566 base_class = mb_get_class(line + startcol);
4567 while (--startcol >= 0)
4568 {
4569 head_off = (*mb_head_off)(line, line + startcol);
4570 if (base_class != mb_get_class(line + startcol
4571 - head_off))
4572 break;
4573 startcol -= head_off;
4574 }
4575 }
4576 else
4577 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4578 ;
4579 compl_col += ++startcol;
4580 compl_length = (int)curs_col - startcol;
4581 if (compl_length == 1)
4582 {
4583 // Only match word with at least two chars -- webb
4584 // there's no need to call quote_meta,
4585 // alloc(7) is enough -- Acevedo
4586 compl_pattern = alloc(7);
4587 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004588 {
4589 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004590 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004591 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004592 STRCPY((char *)compl_pattern, "\\<");
4593 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4594 STRCAT((char *)compl_pattern, "\\k");
4595 }
4596 else
4597 {
4598 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4599 compl_length) + 2);
4600 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004601 {
4602 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004603 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004604 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004605 STRCPY((char *)compl_pattern, "\\<");
4606 (void)quote_meta(compl_pattern + 2, line + compl_col,
4607 compl_length);
4608 }
4609 }
4610
John Marriott8c85a2a2024-05-20 19:18:26 +02004611 compl_patternlen = STRLEN(compl_pattern);
4612
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004613 return OK;
4614}
4615
4616/*
4617 * Get the pattern, column and length for whole line completion or for the
4618 * complete() function.
4619 * Sets the global variables: compl_col, compl_length and compl_pattern.
4620 */
4621 static int
4622get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4623{
4624 compl_col = (colnr_T)getwhitecols(line);
4625 compl_length = (int)curs_col - (int)compl_col;
4626 if (compl_length < 0) // cursor in indent: empty pattern
4627 compl_length = 0;
4628 if (p_ic)
4629 compl_pattern = str_foldcase(line + compl_col, compl_length,
4630 NULL, 0);
4631 else
4632 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4633 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004634 {
4635 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004636 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004637 }
4638
4639 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004640
4641 return OK;
4642}
4643
4644/*
4645 * Get the pattern, column and length for filename completion.
4646 * Sets the global variables: compl_col, compl_length and compl_pattern.
4647 */
4648 static int
4649get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4650{
4651 // Go back to just before the first filename character.
4652 if (startcol > 0)
4653 {
4654 char_u *p = line + startcol;
4655
4656 MB_PTR_BACK(line, p);
4657 while (p > line && vim_isfilec(PTR2CHAR(p)))
4658 MB_PTR_BACK(line, p);
4659 if (p == line && vim_isfilec(PTR2CHAR(p)))
4660 startcol = 0;
4661 else
4662 startcol = (int)(p - line) + 1;
4663 }
4664
4665 compl_col += startcol;
4666 compl_length = (int)curs_col - startcol;
4667 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4668 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004669 {
4670 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004671 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004672 }
4673
4674 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004675
4676 return OK;
4677}
4678
4679/*
4680 * Get the pattern, column and length for command-line completion.
4681 * Sets the global variables: compl_col, compl_length and compl_pattern.
4682 */
4683 static int
4684get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4685{
4686 compl_pattern = vim_strnsave(line, curs_col);
4687 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004688 {
4689 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004690 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004691 }
4692 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004693 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004694 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004695 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4696 || compl_xp.xp_context == EXPAND_NOTHING)
4697 // No completion possible, use an empty pattern to get a
4698 // "pattern not found" message.
4699 compl_col = curs_col;
4700 else
4701 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4702 compl_length = curs_col - compl_col;
4703
4704 return OK;
4705}
4706
4707/*
4708 * Get the pattern, column and length for user defined completion ('omnifunc',
4709 * 'completefunc' and 'thesaurusfunc')
4710 * Sets the global variables: compl_col, compl_length and compl_pattern.
4711 * Uses the global variable: spell_bad_len
4712 */
4713 static int
4714get_userdefined_compl_info(colnr_T curs_col UNUSED)
4715{
4716 int ret = FAIL;
4717
4718#ifdef FEAT_COMPL_FUNC
4719 // Call user defined function 'completefunc' with "a:findstart"
4720 // set to 1 to obtain the length of text to use for completion.
4721 char_u *line;
4722 typval_T args[3];
4723 int col;
4724 char_u *funcname;
4725 pos_T pos;
4726 int save_State = State;
4727 callback_T *cb;
4728
4729 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4730 // length as a string
4731 funcname = get_complete_funcname(ctrl_x_mode);
4732 if (*funcname == NUL)
4733 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004734 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004735 ? "completefunc" : "omnifunc");
4736 return FAIL;
4737 }
4738
4739 args[0].v_type = VAR_NUMBER;
4740 args[0].vval.v_number = 1;
4741 args[1].v_type = VAR_STRING;
4742 args[1].vval.v_string = (char_u *)"";
4743 args[2].v_type = VAR_UNKNOWN;
4744 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004745 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004746 cb = get_insert_callback(ctrl_x_mode);
4747 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004748 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004749
4750 State = save_State;
4751 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004752 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004753 validate_cursor();
4754 if (!EQUAL_POS(curwin->w_cursor, pos))
4755 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004756 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004757 return FAIL;
4758 }
4759
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004760 // Return value -2 means the user complete function wants to cancel the
4761 // complete without an error, do the same if the function did not execute
4762 // successfully.
4763 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004764 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004765 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004766 if (col == -3)
4767 {
4768 ctrl_x_mode = CTRL_X_NORMAL;
4769 edit_submode = NULL;
4770 if (!shortmess(SHM_COMPLETIONMENU))
4771 msg_clr_cmdline();
4772 return FAIL;
4773 }
4774
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004775 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004776 // completion.
4777 compl_opt_refresh_always = FALSE;
4778 compl_opt_suppress_empty = FALSE;
4779
4780 if (col < 0)
4781 col = curs_col;
4782 compl_col = col;
4783 if (compl_col > curs_col)
4784 compl_col = curs_col;
4785
4786 // Setup variables for completion. Need to obtain "line" again,
4787 // it may have become invalid.
4788 line = ml_get(curwin->w_cursor.lnum);
4789 compl_length = curs_col - compl_col;
4790 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4791 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004792 {
4793 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004794 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004795 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004796
John Marriott8c85a2a2024-05-20 19:18:26 +02004797 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004798 ret = OK;
4799#endif
4800
4801 return ret;
4802}
4803
4804/*
4805 * Get the pattern, column and length for spell completion.
4806 * Sets the global variables: compl_col, compl_length and compl_pattern.
4807 * Uses the global variable: spell_bad_len
4808 */
4809 static int
4810get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4811{
4812 int ret = FAIL;
4813#ifdef FEAT_SPELL
4814 char_u *line;
4815
4816 if (spell_bad_len > 0)
4817 compl_col = curs_col - spell_bad_len;
4818 else
4819 compl_col = spell_word_start(startcol);
4820 if (compl_col >= (colnr_T)startcol)
4821 {
4822 compl_length = 0;
4823 compl_col = curs_col;
4824 }
4825 else
4826 {
4827 spell_expand_check_cap(compl_col);
4828 compl_length = (int)curs_col - compl_col;
4829 }
4830 // Need to obtain "line" again, it may have become invalid.
4831 line = ml_get(curwin->w_cursor.lnum);
4832 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4833 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004834 {
4835 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004836 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004837 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004838
John Marriott8c85a2a2024-05-20 19:18:26 +02004839 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004840 ret = OK;
4841#endif
4842
4843 return ret;
4844}
4845
4846/*
4847 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004848 * "startcol" - start column number of the completion pattern/text
4849 * "cur_col" - current cursor column
4850 * On return, "line_invalid" is set to TRUE, if the current line may have
4851 * become invalid and needs to be fetched again.
4852 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004853 */
4854 static int
4855compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4856{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004857 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004858 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4859 && !thesaurus_func_complete(ctrl_x_mode)))
4860 {
4861 return get_normal_compl_info(line, startcol, curs_col);
4862 }
4863 else if (ctrl_x_mode_line_or_eval())
4864 {
4865 return get_wholeline_compl_info(line, curs_col);
4866 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004867 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004868 {
4869 return get_filename_compl_info(line, startcol, curs_col);
4870 }
4871 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4872 {
4873 return get_cmdline_compl_info(line, curs_col);
4874 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004875 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004876 || thesaurus_func_complete(ctrl_x_mode))
4877 {
4878 if (get_userdefined_compl_info(curs_col) == FAIL)
4879 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004880 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004881 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004882 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004883 {
4884 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4885 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004886 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004887 }
4888 else
4889 {
4890 internal_error("ins_complete()");
4891 return FAIL;
4892 }
4893
4894 return OK;
4895}
4896
4897/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004898 * Continue an interrupted completion mode search in "line".
4899 *
4900 * If this same ctrl_x_mode has been interrupted use the text from
4901 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4902 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4903 * the same line as the cursor then fix it (the line has been split because it
4904 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4905 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004906 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004907 static void
4908ins_compl_continue_search(char_u *line)
4909{
4910 // it is a continued search
4911 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004912 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4913 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004914 {
4915 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4916 {
4917 // line (probably) wrapped, set compl_startpos to the
4918 // first non_blank in the line, if it is not a wordchar
4919 // include it to get a better pattern, but then we don't
4920 // want the "\\<" prefix, check it below
4921 compl_col = (colnr_T)getwhitecols(line);
4922 compl_startpos.col = compl_col;
4923 compl_startpos.lnum = curwin->w_cursor.lnum;
4924 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4925 }
4926 else
4927 {
4928 // S_IPOS was set when we inserted a word that was at the
4929 // beginning of the line, which means that we'll go to SOL
4930 // mode but first we need to redefine compl_startpos
4931 if (compl_cont_status & CONT_S_IPOS)
4932 {
4933 compl_cont_status |= CONT_SOL;
4934 compl_startpos.col = (colnr_T)(skipwhite(
4935 line + compl_length
4936 + compl_startpos.col) - line);
4937 }
4938 compl_col = compl_startpos.col;
4939 }
4940 compl_length = curwin->w_cursor.col - (int)compl_col;
4941 // IObuff is used to add a "word from the next line" would we
4942 // have enough space? just being paranoid
4943#define MIN_SPACE 75
4944 if (compl_length > (IOSIZE - MIN_SPACE))
4945 {
4946 compl_cont_status &= ~CONT_SOL;
4947 compl_length = (IOSIZE - MIN_SPACE);
4948 compl_col = curwin->w_cursor.col - compl_length;
4949 }
4950 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4951 if (compl_length < 1)
4952 compl_cont_status &= CONT_LOCAL;
4953 }
4954 else if (ctrl_x_mode_line_or_eval())
4955 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4956 else
4957 compl_cont_status = 0;
4958}
4959
4960/*
4961 * start insert mode completion
4962 */
4963 static int
4964ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004965{
4966 char_u *line;
4967 int startcol = 0; // column where searched text starts
4968 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004969 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004970 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004971 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004972
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004973 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004974
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004975 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004976 did_si = FALSE;
4977 can_si = FALSE;
4978 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004979 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004980 return FAIL;
4981
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004982 line = ml_get(curwin->w_cursor.lnum);
4983 curs_col = curwin->w_cursor.col;
4984 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004985
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004986 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4987 && compl_cont_mode == ctrl_x_mode)
4988 // this same ctrl-x_mode was interrupted previously. Continue the
4989 // completion.
4990 ins_compl_continue_search(line);
4991 else
4992 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004993
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004994 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004995 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004996 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004997 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004998 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4999 compl_cont_status = 0;
5000 compl_cont_status |= CONT_N_ADDS;
5001 compl_startpos = curwin->w_cursor;
5002 startcol = (int)curs_col;
5003 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005004 }
5005
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005006 // Work out completion pattern and original text -- webb
5007 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
5008 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005009 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
5010 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005011 // restore did_ai, so that adding comment leader works
5012 did_ai = save_did_ai;
5013 return FAIL;
5014 }
5015 // If "line" was changed while getting completion info get it again.
5016 if (line_invalid)
5017 line = ml_get(curwin->w_cursor.lnum);
5018
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005019 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005020 {
5021 edit_submode_pre = (char_u *)_(" Adding");
5022 if (ctrl_x_mode_line_or_eval())
5023 {
5024 // Insert a new line, keep indentation but ignore 'comments'.
5025 char_u *old = curbuf->b_p_com;
5026
5027 curbuf->b_p_com = (char_u *)"";
5028 compl_startpos.lnum = curwin->w_cursor.lnum;
5029 compl_startpos.col = compl_col;
5030 ins_eol('\r');
5031 curbuf->b_p_com = old;
5032 compl_length = 0;
5033 compl_col = curwin->w_cursor.col;
5034 }
5035 }
5036 else
5037 {
5038 edit_submode_pre = NULL;
5039 compl_startpos.col = compl_col;
5040 }
5041
5042 if (compl_cont_status & CONT_LOCAL)
5043 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
5044 else
5045 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5046
5047 // If any of the original typed text has been changed we need to fix
5048 // the redo buffer.
5049 ins_compl_fixRedoBufForLeader(NULL);
5050
5051 // Always add completion for the original text.
5052 vim_free(compl_orig_text);
5053 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5054 if (p_ic)
5055 flags |= CP_ICASE;
5056 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5057 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
5058 {
5059 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02005060 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005061 VIM_CLEAR(compl_orig_text);
5062 return FAIL;
5063 }
5064
5065 // showmode might reset the internal line pointers, so it must
5066 // be called before line = ml_get(), or when this address is no
5067 // longer needed. -- Acevedo.
5068 edit_submode_extra = (char_u *)_("-- Searching...");
5069 edit_submode_highl = HLF_COUNT;
5070 showmode();
5071 edit_submode_extra = NULL;
5072 out_flush();
5073
5074 return OK;
5075}
5076
5077/*
5078 * display the completion status message
5079 */
5080 static void
5081ins_compl_show_statusmsg(void)
5082{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005083 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005084 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005085 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005086 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00005087 ? (char_u *)_("Hit end of paragraph")
5088 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005089 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005090 }
5091
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005092 if (edit_submode_extra == NULL)
5093 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005094 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005095 {
5096 edit_submode_extra = (char_u *)_("Back at original");
5097 edit_submode_highl = HLF_W;
5098 }
5099 else if (compl_cont_status & CONT_S_IPOS)
5100 {
5101 edit_submode_extra = (char_u *)_("Word from other line");
5102 edit_submode_highl = HLF_COUNT;
5103 }
5104 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5105 {
5106 edit_submode_extra = (char_u *)_("The only match");
5107 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005108 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005109 }
5110 else
5111 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005112#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005113 // Update completion sequence number when needed.
5114 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005115 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005116#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005117 // The match should always have a sequence number now, this is
5118 // just a safety check.
5119 if (compl_curr_match->cp_number != -1)
5120 {
5121 // Space for 10 text chars. + 2x10-digit no.s = 31.
5122 // Translations may need more than twice that.
5123 static char_u match_ref[81];
5124
5125 if (compl_matches > 0)
5126 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005127 _("match %d of %d"),
5128 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005129 else
5130 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005131 _("match %d"),
5132 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005133 edit_submode_extra = match_ref;
5134 edit_submode_highl = HLF_R;
5135 if (dollar_vcol >= 0)
5136 curs_columns(FALSE);
5137 }
5138 }
5139 }
5140
5141 // Show a message about what (completion) mode we're in.
5142 if (!compl_opt_suppress_empty)
5143 {
5144 showmode();
5145 if (!shortmess(SHM_COMPLETIONMENU))
5146 {
5147 if (edit_submode_extra != NULL)
5148 {
5149 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005150 {
5151 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005152 msg_attr((char *)edit_submode_extra,
5153 edit_submode_highl < HLF_COUNT
5154 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005155 msg_hist_off = FALSE;
5156 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005157 }
5158 else
5159 msg_clr_cmdline(); // necessary for "noshowmode"
5160 }
5161 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005162}
5163
5164/*
5165 * Do Insert mode completion.
5166 * Called when character "c" was typed, which has a meaning for completion.
5167 * Returns OK if completion was done, FAIL if something failed (out of mem).
5168 */
5169 int
5170ins_complete(int c, int enable_pum)
5171{
5172 int n;
5173 int save_w_wrow;
5174 int save_w_leftcol;
5175 int insert_match;
5176
5177 compl_direction = ins_compl_key2dir(c);
5178 insert_match = ins_compl_use_match(c);
5179
5180 if (!compl_started)
5181 {
5182 if (ins_compl_start() == FAIL)
5183 return FAIL;
5184 }
5185 else if (insert_match && stop_arrow() == FAIL)
5186 return FAIL;
5187
5188 compl_shown_match = compl_curr_match;
5189 compl_shows_dir = compl_direction;
5190
5191 // Find next match (and following matches).
5192 save_w_wrow = curwin->w_wrow;
5193 save_w_leftcol = curwin->w_leftcol;
5194 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5195
5196 // may undisplay the popup menu
5197 ins_compl_upd_pum();
5198
5199 if (n > 1) // all matches have been found
5200 compl_matches = n;
5201 compl_curr_match = compl_shown_match;
5202 compl_direction = compl_shows_dir;
5203
5204 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5205 // mode.
5206 if (got_int && !global_busy)
5207 {
5208 (void)vgetc();
5209 got_int = FALSE;
5210 }
5211
5212 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005213 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005214 {
5215 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5216 // because we couldn't expand anything at first place, but if we used
5217 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5218 // (such as M in M'exico) if not tried already. -- Acevedo
5219 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005220 || compl_status_adding()
5221 || (ctrl_x_mode_not_default()
5222 && !ctrl_x_mode_path_patterns()
5223 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005224 compl_cont_status &= ~CONT_N_ADDS;
5225 }
5226
5227 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5228 compl_cont_status |= CONT_S_IPOS;
5229 else
5230 compl_cont_status &= ~CONT_S_IPOS;
5231
5232 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005233
5234 // Show the popup menu, unless we got interrupted.
5235 if (enable_pum && !compl_interrupted)
5236 show_pum(save_w_wrow, save_w_leftcol);
5237
5238 compl_was_interrupted = compl_interrupted;
5239 compl_interrupted = FALSE;
5240
5241 return OK;
5242}
5243
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005244/*
5245 * Remove (if needed) and show the popup menu
5246 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005247 static void
5248show_pum(int prev_w_wrow, int prev_w_leftcol)
5249{
5250 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005251 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005252 RedrawingDisabled = 0;
5253
5254 // If the cursor moved or the display scrolled we need to remove the pum
5255 // first.
5256 setcursor();
5257 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5258 ins_compl_del_pum();
5259
5260 ins_compl_show_pum();
5261 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005262
5263 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005264}
5265
5266/*
5267 * Looks in the first "len" chars. of "src" for search-metachars.
5268 * If dest is not NULL the chars. are copied there quoting (with
5269 * a backslash) the metachars, and dest would be NUL terminated.
5270 * Returns the length (needed) of dest
5271 */
5272 static unsigned
5273quote_meta(char_u *dest, char_u *src, int len)
5274{
5275 unsigned m = (unsigned)len + 1; // one extra for the NUL
5276
5277 for ( ; --len >= 0; src++)
5278 {
5279 switch (*src)
5280 {
5281 case '.':
5282 case '*':
5283 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005284 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005285 break;
5286 // FALLTHROUGH
5287 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005288 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005289 break;
5290 // FALLTHROUGH
5291 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005292 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005293 break;
5294 // FALLTHROUGH
5295 case '^': // currently it's not needed.
5296 case '$':
5297 m++;
5298 if (dest != NULL)
5299 *dest++ = '\\';
5300 break;
5301 }
5302 if (dest != NULL)
5303 *dest++ = *src;
5304 // Copy remaining bytes of a multibyte character.
5305 if (has_mbyte)
5306 {
5307 int i, mb_len;
5308
5309 mb_len = (*mb_ptr2len)(src) - 1;
5310 if (mb_len > 0 && len >= mb_len)
5311 for (i = 0; i < mb_len; ++i)
5312 {
5313 --len;
5314 ++src;
5315 if (dest != NULL)
5316 *dest++ = *src;
5317 }
5318 }
5319 }
5320 if (dest != NULL)
5321 *dest = NUL;
5322
5323 return m;
5324}
5325
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005326#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005327 void
5328free_insexpand_stuff(void)
5329{
5330 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005331# ifdef FEAT_EVAL
5332 free_callback(&cfu_cb);
5333 free_callback(&ofu_cb);
5334 free_callback(&tsrfu_cb);
5335# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005336}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005337#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005338
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005339#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005340/*
5341 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5342 * spelled word, if there is one.
5343 */
5344 static void
5345spell_back_to_badword(void)
5346{
5347 pos_T tpos = curwin->w_cursor;
5348
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005349 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005350 if (curwin->w_cursor.col != tpos.col)
5351 start_arrow(&tpos);
5352}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005353#endif