blob: b49a631a6b4f61171b719ec612454456dfb364ba [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127
128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000134 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100135 */
136static compl_T *compl_first_match = NULL;
137static compl_T *compl_curr_match = NULL;
138static compl_T *compl_shown_match = NULL;
139static compl_T *compl_old_match = NULL;
140
141// After using a cursor key <Enter> selects a match in the popup menu,
142// otherwise it inserts a line break.
143static int compl_enter_selects = FALSE;
144
145// When "compl_leader" is not NULL only matches that start with this string
146// are used.
147static char_u *compl_leader = NULL;
148
149static int compl_get_longest = FALSE; // put longest common string
150 // in compl_leader
151
152static int compl_no_insert = FALSE; // FALSE: select & insert
153 // TRUE: noinsert
154static int compl_no_select = FALSE; // FALSE: select & insert
155 // TRUE: noselect
156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static 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 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
266int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
270int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
271int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
272int ctrl_x_mode_path_patterns(void) {
273 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
274int ctrl_x_mode_path_defines(void) {
275 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
276int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
277int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200278int ctrl_x_mode_cmdline(void) {
279 return ctrl_x_mode == CTRL_X_CMDLINE
280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100281int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
282int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
283int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000284static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100285int ctrl_x_mode_line_or_eval(void) {
286 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
287
288/*
289 * Whether other than default completion has been selected.
290 */
291 int
292ctrl_x_mode_not_default(void)
293{
294 return ctrl_x_mode != CTRL_X_NORMAL;
295}
296
297/*
zeertzjqdca29d92021-08-31 19:12:51 +0200298 * Whether CTRL-X was typed without a following character,
299 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300 */
301 int
302ctrl_x_mode_not_defined_yet(void)
303{
304 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
305}
306
307/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000308 * Return TRUE if currently in "normal" or "adding" insert completion matches
309 * state
310 */
311 int
312compl_status_adding(void)
313{
314 return compl_cont_status & CONT_ADDING;
315}
316
317/*
318 * Return TRUE if the completion pattern includes start of line, just for
319 * word-wise expansion.
320 */
321 int
322compl_status_sol(void)
323{
324 return compl_cont_status & CONT_SOL;
325}
326
327/*
328 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
329 */
330 int
331compl_status_local(void)
332{
333 return compl_cont_status & CONT_LOCAL;
334}
335
336/*
337 * Clear the completion status flags
338 */
339 void
340compl_status_clear(void)
341{
342 compl_cont_status = 0;
343}
344
345/*
346 * Return TRUE if completion is using the forward direction matches
347 */
348 static int
349compl_dir_forward(void)
350{
351 return compl_direction == FORWARD;
352}
353
354/*
355 * Return TRUE if currently showing forward completion matches
356 */
357 static int
358compl_shows_dir_forward(void)
359{
360 return compl_shows_dir == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing backward completion matches
365 */
366 static int
367compl_shows_dir_backward(void)
368{
369 return compl_shows_dir == BACKWARD;
370}
371
372/*
373 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100374 */
375 int
376has_compl_option(int dict_opt)
377{
378 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200379#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100380 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200381#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100382 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100383 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
384#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100385 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386#endif
387 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 {
389 ctrl_x_mode = CTRL_X_NORMAL;
390 edit_submode = NULL;
391 msg_attr(dict_opt ? _("'dictionary' option is empty")
392 : _("'thesaurus' option is empty"),
393 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100394 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 {
396 vim_beep(BO_COMPL);
397 setcursor();
398 out_flush();
399#ifdef FEAT_EVAL
400 if (!get_vim_var_nr(VV_TESTING))
401#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100402 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 }
404 return FALSE;
405 }
406 return TRUE;
407}
408
409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000410 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100411 * This depends on the current mode.
412 */
413 int
414vim_is_ctrl_x_key(int c)
415{
416 // Always allow ^R - let its results then be checked
417 if (c == Ctrl_R)
418 return TRUE;
419
420 // Accept <PageUp> and <PageDown> if the popup menu is visible.
421 if (ins_compl_pum_key(c))
422 return TRUE;
423
424 switch (ctrl_x_mode)
425 {
426 case 0: // Not in any CTRL-X mode
427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
428 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200429 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100430 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
431 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
432 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
433 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
434 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200435 || c == Ctrl_S || c == Ctrl_K || c == 's'
436 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437 case CTRL_X_SCROLL:
438 return (c == Ctrl_Y || c == Ctrl_E);
439 case CTRL_X_WHOLE_LINE:
440 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
441 case CTRL_X_FILES:
442 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
443 case CTRL_X_DICTIONARY:
444 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_THESAURUS:
446 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_TAGS:
448 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
449#ifdef FEAT_FIND_ID
450 case CTRL_X_PATH_PATTERNS:
451 return (c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_PATH_DEFINES:
453 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
454#endif
455 case CTRL_X_CMDLINE:
456 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
457 || c == Ctrl_X);
458#ifdef FEAT_COMPL_FUNC
459 case CTRL_X_FUNCTION:
460 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_OMNI:
462 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_SPELL:
465 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_EVAL:
467 return (c == Ctrl_P || c == Ctrl_N);
468 }
469 internal_error("vim_is_ctrl_x_key()");
470 return FALSE;
471}
472
473/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000474 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000475 */
476 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478{
479 return match->cp_flags & CP_ORIGINAL_TEXT;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Returns TRUE if "match" is the first match in the completion list.
484 */
485 static int
486is_first_match(compl_T *match)
487{
488 return match == compl_first_match;
489}
490
491/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100492 * Return TRUE when character "c" is part of the item currently being
493 * completed. Used to decide whether to abandon complete mode when the menu
494 * is visible.
495 */
496 int
497ins_compl_accept_char(int c)
498{
499 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
500 // When expanding an identifier only accept identifier chars.
501 return vim_isIDc(c);
502
503 switch (ctrl_x_mode)
504 {
505 case CTRL_X_FILES:
506 // When expanding file name only accept file name chars. But not
507 // path separators, so that "proto/<Tab>" expands files in
508 // "proto", not "proto/" as a whole
509 return vim_isfilec(c) && !vim_ispathsep(c);
510
511 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200512 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 case CTRL_X_OMNI:
514 // Command line and Omni completion can work with just about any
515 // printable character, but do stop at white space.
516 return vim_isprintc(c) && !VIM_ISWHITE(c);
517
518 case CTRL_X_WHOLE_LINE:
519 // For while line completion a space can be part of the line.
520 return vim_isprintc(c);
521 }
522 return vim_iswordc(c);
523}
524
525/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000526 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100527 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000528 */
529 static char_u *
530ins_compl_infercase_gettext(
531 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100532 int char_len,
533 int compl_char_len,
534 int min_len,
535 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000536{
537 int *wca; // Wide character array.
538 char_u *p;
539 int i, c;
540 int has_lower = FALSE;
541 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100542 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000543
544 IObuff[0] = NUL;
545
546 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100547 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548 if (wca == NULL)
549 return IObuff;
550
551 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100552 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000553 if (has_mbyte)
554 wca[i] = mb_ptr2char_adv(&p);
555 else
556 wca[i] = *(p++);
557
558 // Rule 1: Were any chars converted to lower?
559 p = compl_orig_text;
560 for (i = 0; i < min_len; ++i)
561 {
562 if (has_mbyte)
563 c = mb_ptr2char_adv(&p);
564 else
565 c = *(p++);
566 if (MB_ISLOWER(c))
567 {
568 has_lower = TRUE;
569 if (MB_ISUPPER(wca[i]))
570 {
571 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100572 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000573 wca[i] = MB_TOLOWER(wca[i]);
574 break;
575 }
576 }
577 }
578
579 // Rule 2: No lower case, 2nd consecutive letter converted to
580 // upper case.
581 if (!has_lower)
582 {
583 p = compl_orig_text;
584 for (i = 0; i < min_len; ++i)
585 {
586 if (has_mbyte)
587 c = mb_ptr2char_adv(&p);
588 else
589 c = *(p++);
590 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
591 {
592 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100593 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 wca[i] = MB_TOUPPER(wca[i]);
595 break;
596 }
597 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
598 }
599 }
600
601 // Copy the original case of the part we typed.
602 p = compl_orig_text;
603 for (i = 0; i < min_len; ++i)
604 {
605 if (has_mbyte)
606 c = mb_ptr2char_adv(&p);
607 else
608 c = *(p++);
609 if (MB_ISLOWER(c))
610 wca[i] = MB_TOLOWER(wca[i]);
611 else if (MB_ISUPPER(c))
612 wca[i] = MB_TOUPPER(wca[i]);
613 }
614
615 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000616 p = IObuff;
617 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100618 ga_init2(&gap, 1, 500);
619 while (i < char_len)
620 {
621 if (gap.ga_data != NULL)
622 {
623 if (ga_grow(&gap, 10) == FAIL)
624 {
625 ga_clear(&gap);
626 return (char_u *)"[failed]";
627 }
628 p = (char_u *)gap.ga_data + gap.ga_len;
629 if (has_mbyte)
630 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
631 else
632 {
633 *p = wca[i++];
634 ++gap.ga_len;
635 }
636 }
637 else if ((p - IObuff) + 6 >= IOSIZE)
638 {
639 // Multi-byte characters can occupy up to five bytes more than
640 // ASCII characters, and we also need one byte for NUL, so when
641 // getting to six bytes from the edge of IObuff switch to using a
642 // growarray. Add the character in the next round.
643 if (ga_grow(&gap, IOSIZE) == FAIL)
644 return (char_u *)"[failed]";
645 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100646 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100647 }
648 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000649 p += (*mb_char2bytes)(wca[i++], p);
650 else
651 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100652 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000653 vim_free(wca);
654
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100655 if (gap.ga_data != NULL)
656 {
657 *tofree = gap.ga_data;
658 return gap.ga_data;
659 }
660
661 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000662 return IObuff;
663}
664
665/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100666 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
667 * case of the originally typed text is used, and the case of the completed
668 * text is inferred, ie this tries to work out what case you probably wanted
669 * the rest of the word to be in -- webb
670 */
671 int
672ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200673 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100674 int len,
675 int icase,
676 char_u *fname,
677 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200678 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200680 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100681 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100682 int char_len; // count multi-byte characters
683 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100684 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200685 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100686 int res;
687 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100688
689 if (p_ic && curbuf->b_p_inf && len > 0)
690 {
691 // Infer case of completed part.
692
693 // Find actual length of completion.
694 if (has_mbyte)
695 {
696 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100697 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698 while (*p != NUL)
699 {
700 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100701 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100702 }
703 }
704 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100705 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100706
707 // Find actual length of original text.
708 if (has_mbyte)
709 {
710 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100711 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100712 while (*p != NUL)
713 {
714 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100715 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716 }
717 }
718 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100719 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100720
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100723 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100724
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100725 str = ins_compl_infercase_gettext(str, char_len,
726 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100727 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200728 if (cont_s_ipos)
729 flags |= CP_CONT_S_IPOS;
730 if (icase)
731 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200732
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100733 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
734 vim_free(tofree);
735 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736}
737
738/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000739 * Add a match to the list of matches. The arguments are:
740 * str - text of the match to add
741 * len - length of "str". If -1, then the length of "str" is
742 * computed.
743 * fname - file name to associate with this match.
744 * cptext - list of strings to use with this match (for abbr, menu, info
745 * and kind)
746 * user_data - user supplied data (any vim type) for this match
747 * cdir - match direction. If 0, use "compl_direction".
748 * flags_arg - match flags (cp_flags)
749 * adup - accept this match even if it is already present.
750 * If "cdir" is FORWARD, then the match is added after the current match.
751 * Otherwise, it is added before the current match.
752 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100753 * If the given string is already in the list of completions, then return
754 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
755 * maybe because alloc() returns NULL, then FAIL is returned.
756 */
757 static int
758ins_compl_add(
759 char_u *str,
760 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100761 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 char_u **cptext, // extra text for popup menu or NULL
763 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100764 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200765 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100766 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100767{
768 compl_T *match;
769 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200770 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100771
Bram Moolenaarceb06192021-04-04 15:05:22 +0200772 if (flags & CP_FAST)
773 fast_breakcheck();
774 else
775 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100776 if (got_int)
777 return FAIL;
778 if (len < 0)
779 len = (int)STRLEN(str);
780
781 // If the same match is already present, don't add it.
782 if (compl_first_match != NULL && !adup)
783 {
784 match = compl_first_match;
785 do
786 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000787 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100789 && ((int)STRLEN(match->cp_str) <= len
790 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100791 return NOTDONE;
792 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000793 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100794 }
795
796 // Remove any popup menu before changing the list of matches.
797 ins_compl_del_pum();
798
799 // Allocate a new match structure.
800 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200801 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100802 if (match == NULL)
803 return FAIL;
804 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200805 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100806 match->cp_number = 0;
807 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
808 {
809 vim_free(match);
810 return FAIL;
811 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100812
813 // match-fname is:
814 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200815 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100816 // - NULL otherwise. --Acevedo
817 if (fname != NULL
818 && compl_curr_match != NULL
819 && compl_curr_match->cp_fname != NULL
820 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
821 match->cp_fname = compl_curr_match->cp_fname;
822 else if (fname != NULL)
823 {
824 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200825 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100826 }
827 else
828 match->cp_fname = NULL;
829 match->cp_flags = flags;
830
831 if (cptext != NULL)
832 {
833 int i;
834
835 for (i = 0; i < CPT_COUNT; ++i)
836 if (cptext[i] != NULL && *cptext[i] != NUL)
837 match->cp_text[i] = vim_strsave(cptext[i]);
838 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100839#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100840 if (user_data != NULL)
841 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100842#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000844 // Link the new match structure after (FORWARD) or before (BACKWARD) the
845 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100846 if (compl_first_match == NULL)
847 match->cp_next = match->cp_prev = NULL;
848 else if (dir == FORWARD)
849 {
850 match->cp_next = compl_curr_match->cp_next;
851 match->cp_prev = compl_curr_match;
852 }
853 else // BACKWARD
854 {
855 match->cp_next = compl_curr_match;
856 match->cp_prev = compl_curr_match->cp_prev;
857 }
858 if (match->cp_next)
859 match->cp_next->cp_prev = match;
860 if (match->cp_prev)
861 match->cp_prev->cp_next = match;
862 else // if there's nothing before, it is the first match
863 compl_first_match = match;
864 compl_curr_match = match;
865
866 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200867 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100868 ins_compl_longest_match(match);
869
870 return OK;
871}
872
873/*
874 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200875 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100876 */
877 static int
878ins_compl_equal(compl_T *match, char_u *str, int len)
879{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200880 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200881 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200882 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100883 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
884 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
885}
886
887/*
888 * Reduce the longest common string for match "match".
889 */
890 static void
891ins_compl_longest_match(compl_T *match)
892{
893 char_u *p, *s;
894 int c1, c2;
895 int had_match;
896
897 if (compl_leader == NULL)
898 {
899 // First match, use it as a whole.
900 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000901 if (compl_leader == NULL)
902 return;
903
904 had_match = (curwin->w_cursor.col > compl_col);
905 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000906 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000907 ins_redraw(FALSE);
908
909 // When the match isn't there (to avoid matching itself) remove it
910 // again after redrawing.
911 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100912 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100913 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000914
915 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100916 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000917
918 // Reduce the text if this match differs from compl_leader.
919 p = compl_leader;
920 s = match->cp_str;
921 while (*p != NUL)
922 {
923 if (has_mbyte)
924 {
925 c1 = mb_ptr2char(p);
926 c2 = mb_ptr2char(s);
927 }
928 else
929 {
930 c1 = *p;
931 c2 = *s;
932 }
933 if ((match->cp_flags & CP_ICASE)
934 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
935 break;
936 if (has_mbyte)
937 {
938 MB_PTR_ADV(p);
939 MB_PTR_ADV(s);
940 }
941 else
942 {
943 ++p;
944 ++s;
945 }
946 }
947
948 if (*p != NUL)
949 {
950 // Leader was shortened, need to change the inserted text.
951 *p = NUL;
952 had_match = (curwin->w_cursor.col > compl_col);
953 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000954 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000955 ins_redraw(FALSE);
956
957 // When the match isn't there (to avoid matching itself) remove it
958 // again after redrawing.
959 if (!had_match)
960 ins_compl_delete();
961 }
962
963 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100964}
965
966/*
967 * Add an array of matches to the list of matches.
968 * Frees matches[].
969 */
970 static void
971ins_compl_add_matches(
972 int num_matches,
973 char_u **matches,
974 int icase)
975{
976 int i;
977 int add_r = OK;
978 int dir = compl_direction;
979
980 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100981 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200982 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100983 // if dir was BACKWARD then honor it just once
984 dir = FORWARD;
985 FreeWild(num_matches, matches);
986}
987
988/*
989 * Make the completion list cyclic.
990 * Return the number of matches (excluding the original).
991 */
992 static int
993ins_compl_make_cyclic(void)
994{
995 compl_T *match;
996 int count = 0;
997
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000998 if (compl_first_match == NULL)
999 return 0;
1000
1001 // Find the end of the list.
1002 match = compl_first_match;
1003 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001004 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001005 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001006 match = match->cp_next;
1007 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001008 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001009 match->cp_next = compl_first_match;
1010 compl_first_match->cp_prev = match;
1011
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001012 return count;
1013}
1014
1015/*
1016 * Return whether there currently is a shown match.
1017 */
1018 int
1019ins_compl_has_shown_match(void)
1020{
1021 return compl_shown_match == NULL
1022 || compl_shown_match != compl_shown_match->cp_next;
1023}
1024
1025/*
1026 * Return whether the shown match is long enough.
1027 */
1028 int
1029ins_compl_long_shown_match(void)
1030{
1031 return (int)STRLEN(compl_shown_match->cp_str)
1032 > curwin->w_cursor.col - compl_col;
1033}
1034
1035/*
1036 * Set variables that store noselect and noinsert behavior from the
1037 * 'completeopt' value.
1038 */
1039 void
1040completeopt_was_set(void)
1041{
1042 compl_no_insert = FALSE;
1043 compl_no_select = FALSE;
1044 if (strstr((char *)p_cot, "noselect") != NULL)
1045 compl_no_select = TRUE;
1046 if (strstr((char *)p_cot, "noinsert") != NULL)
1047 compl_no_insert = TRUE;
1048}
1049
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001050
1051// "compl_match_array" points the currently displayed list of entries in the
1052// popup menu. It is NULL when there is no popup menu.
1053static pumitem_T *compl_match_array = NULL;
1054static int compl_match_arraysize;
1055
1056/*
1057 * Update the screen and when there is any scrolling remove the popup menu.
1058 */
1059 static void
1060ins_compl_upd_pum(void)
1061{
1062 int h;
1063
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001064 if (compl_match_array == NULL)
1065 return;
1066
1067 h = curwin->w_cline_height;
1068 // Update the screen later, before drawing the popup menu over it.
1069 pum_call_update_screen();
1070 if (h != curwin->w_cline_height)
1071 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001072}
1073
1074/*
1075 * Remove any popup menu.
1076 */
1077 static void
1078ins_compl_del_pum(void)
1079{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001080 if (compl_match_array == NULL)
1081 return;
1082
1083 pum_undisplay();
1084 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001085}
1086
1087/*
1088 * Return TRUE if the popup menu should be displayed.
1089 */
1090 int
1091pum_wanted(void)
1092{
1093 // 'completeopt' must contain "menu" or "menuone"
1094 if (vim_strchr(p_cot, 'm') == NULL)
1095 return FALSE;
1096
1097 // The display looks bad on a B&W display.
1098 if (t_colors < 8
1099#ifdef FEAT_GUI
1100 && !gui.in_use
1101#endif
1102 )
1103 return FALSE;
1104 return TRUE;
1105}
1106
1107/*
1108 * Return TRUE if there are two or more matches to be shown in the popup menu.
1109 * One if 'completopt' contains "menuone".
1110 */
1111 static int
1112pum_enough_matches(void)
1113{
1114 compl_T *compl;
1115 int i;
1116
1117 // Don't display the popup menu if there are no matches or there is only
1118 // one (ignoring the original text).
1119 compl = compl_first_match;
1120 i = 0;
1121 do
1122 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001123 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001124 break;
1125 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001126 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001127
1128 if (strstr((char *)p_cot, "menuone") != NULL)
1129 return (i >= 1);
1130 return (i >= 2);
1131}
1132
Bram Moolenaar3075a452021-11-17 15:51:52 +00001133#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001134/*
1135 * Allocate Dict for the completed item.
1136 * { word, abbr, menu, kind, info }
1137 */
1138 static dict_T *
1139ins_compl_dict_alloc(compl_T *match)
1140{
1141 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1142
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001143 if (dict == NULL)
1144 return NULL;
1145
1146 dict_add_string(dict, "word", match->cp_str);
1147 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1148 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1149 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1150 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1151 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1152 dict_add_string(dict, "user_data", (char_u *)"");
1153 else
1154 dict_add_tv(dict, "user_data", &match->cp_user_data);
1155
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001156 return dict;
1157}
1158
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001159/*
1160 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1161 * completion menu is changed.
1162 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001163 static void
1164trigger_complete_changed_event(int cur)
1165{
1166 dict_T *v_event;
1167 dict_T *item;
1168 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001169 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001170
1171 if (recursive)
1172 return;
1173
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001174 if (cur < 0)
1175 item = dict_alloc();
1176 else
1177 item = ins_compl_dict_alloc(compl_curr_match);
1178 if (item == NULL)
1179 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001180 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001181 dict_add_dict(v_event, "completed_item", item);
1182 pum_set_event_info(v_event);
1183 dict_set_items_ro(v_event);
1184
1185 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001186 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001187 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001188 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001189 recursive = FALSE;
1190
Bram Moolenaar3075a452021-11-17 15:51:52 +00001191 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001192}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001193#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001194
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001195/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001196 * Build a popup menu to show the completion matches.
1197 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1198 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001199 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001200 static int
1201ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001202{
1203 compl_T *compl;
1204 compl_T *shown_compl = NULL;
1205 int did_find_shown_match = FALSE;
1206 int shown_match_ok = FALSE;
1207 int i;
1208 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001209 int lead_len = 0;
1210
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001211 // Need to build the popup menu list.
1212 compl_match_arraysize = 0;
1213 compl = compl_first_match;
1214 if (compl_leader != NULL)
1215 lead_len = (int)STRLEN(compl_leader);
1216
1217 do
1218 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001219 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001220 && (compl_leader == NULL
1221 || ins_compl_equal(compl, compl_leader, lead_len)))
1222 ++compl_match_arraysize;
1223 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001224 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001225
1226 if (compl_match_arraysize == 0)
1227 return -1;
1228
1229 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1230 if (compl_match_array == NULL)
1231 return -1;
1232
1233 // If the current match is the original text don't find the first
1234 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001235 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001236 shown_match_ok = TRUE;
1237
1238 i = 0;
1239 compl = compl_first_match;
1240 do
1241 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001242 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001243 && (compl_leader == NULL
1244 || ins_compl_equal(compl, compl_leader, lead_len)))
1245 {
1246 if (!shown_match_ok)
1247 {
1248 if (compl == compl_shown_match || did_find_shown_match)
1249 {
1250 // This item is the shown match or this is the
1251 // first displayed item after the shown match.
1252 compl_shown_match = compl;
1253 did_find_shown_match = TRUE;
1254 shown_match_ok = TRUE;
1255 }
1256 else
1257 // Remember this displayed match for when the
1258 // shown match is just below it.
1259 shown_compl = compl;
1260 cur = i;
1261 }
1262
1263 if (compl->cp_text[CPT_ABBR] != NULL)
1264 compl_match_array[i].pum_text =
1265 compl->cp_text[CPT_ABBR];
1266 else
1267 compl_match_array[i].pum_text = compl->cp_str;
1268 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1269 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1270 if (compl->cp_text[CPT_MENU] != NULL)
1271 compl_match_array[i++].pum_extra =
1272 compl->cp_text[CPT_MENU];
1273 else
1274 compl_match_array[i++].pum_extra = compl->cp_fname;
1275 }
1276
1277 if (compl == compl_shown_match)
1278 {
1279 did_find_shown_match = TRUE;
1280
1281 // When the original text is the shown match don't set
1282 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001283 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001284 shown_match_ok = TRUE;
1285
1286 if (!shown_match_ok && shown_compl != NULL)
1287 {
1288 // The shown match isn't displayed, set it to the
1289 // previously displayed match.
1290 compl_shown_match = shown_compl;
1291 shown_match_ok = TRUE;
1292 }
1293 }
1294 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001295 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001296
1297 if (!shown_match_ok) // no displayed match at all
1298 cur = -1;
1299
1300 return cur;
1301}
1302
1303/*
1304 * Show the popup menu for the list of matches.
1305 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1306 */
1307 void
1308ins_compl_show_pum(void)
1309{
1310 int i;
1311 int cur = -1;
1312 colnr_T col;
1313
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001314 if (!pum_wanted() || !pum_enough_matches())
1315 return;
1316
1317#if defined(FEAT_EVAL)
1318 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001319 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001320#endif
1321
1322 // Update the screen later, before drawing the popup menu over it.
1323 pum_call_update_screen();
1324
1325 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001326 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001327 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001328 else
1329 {
1330 // popup menu already exists, only need to find the current item.
1331 for (i = 0; i < compl_match_arraysize; ++i)
1332 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1333 || compl_match_array[i].pum_text
1334 == compl_shown_match->cp_text[CPT_ABBR])
1335 {
1336 cur = i;
1337 break;
1338 }
1339 }
1340
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001341 if (compl_match_array == NULL)
1342 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001343
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001344 // In Replace mode when a $ is displayed at the end of the line only
1345 // part of the screen would be updated. We do need to redraw here.
1346 dollar_vcol = -1;
1347
1348 // Compute the screen column of the start of the completed text.
1349 // Use the cursor to get all wrapping and other settings right.
1350 col = curwin->w_cursor.col;
1351 curwin->w_cursor.col = compl_col;
1352 pum_display(compl_match_array, compl_match_arraysize, cur);
1353 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001354
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001355#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001356 if (has_completechanged())
1357 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001358#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001359}
1360
1361#define DICT_FIRST (1) // use just first element in "dict"
1362#define DICT_EXACT (2) // "dict" is the exact name of a file
1363
1364/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001365 * Add any identifiers that match the given pattern "pat" in the list of
1366 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001367 */
1368 static void
1369ins_compl_dictionaries(
1370 char_u *dict_start,
1371 char_u *pat,
1372 int flags, // DICT_FIRST and/or DICT_EXACT
1373 int thesaurus) // Thesaurus completion
1374{
1375 char_u *dict = dict_start;
1376 char_u *ptr;
1377 char_u *buf;
1378 regmatch_T regmatch;
1379 char_u **files;
1380 int count;
1381 int save_p_scs;
1382 int dir = compl_direction;
1383
1384 if (*dict == NUL)
1385 {
1386#ifdef FEAT_SPELL
1387 // When 'dictionary' is empty and spell checking is enabled use
1388 // "spell".
1389 if (!thesaurus && curwin->w_p_spell)
1390 dict = (char_u *)"spell";
1391 else
1392#endif
1393 return;
1394 }
1395
1396 buf = alloc(LSIZE);
1397 if (buf == NULL)
1398 return;
1399 regmatch.regprog = NULL; // so that we can goto theend
1400
1401 // If 'infercase' is set, don't use 'smartcase' here
1402 save_p_scs = p_scs;
1403 if (curbuf->b_p_inf)
1404 p_scs = FALSE;
1405
1406 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1407 // to only match at the start of a line. Otherwise just match the
1408 // pattern. Also need to double backslashes.
1409 if (ctrl_x_mode_line_or_eval())
1410 {
1411 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1412 size_t len;
1413
1414 if (pat_esc == NULL)
1415 goto theend;
1416 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001417 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001418 if (ptr == NULL)
1419 {
1420 vim_free(pat_esc);
1421 goto theend;
1422 }
1423 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1424 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1425 vim_free(pat_esc);
1426 vim_free(ptr);
1427 }
1428 else
1429 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001430 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001431 if (regmatch.regprog == NULL)
1432 goto theend;
1433 }
1434
1435 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1436 regmatch.rm_ic = ignorecase(pat);
1437 while (*dict != NUL && !got_int && !compl_interrupted)
1438 {
1439 // copy one dictionary file name into buf
1440 if (flags == DICT_EXACT)
1441 {
1442 count = 1;
1443 files = &dict;
1444 }
1445 else
1446 {
1447 // Expand wildcards in the dictionary name, but do not allow
1448 // backticks (for security, the 'dict' option may have been set in
1449 // a modeline).
1450 copy_option_part(&dict, buf, LSIZE, ",");
1451# ifdef FEAT_SPELL
1452 if (!thesaurus && STRCMP(buf, "spell") == 0)
1453 count = -1;
1454 else
1455# endif
1456 if (vim_strchr(buf, '`') != NULL
1457 || expand_wildcards(1, &buf, &count, &files,
1458 EW_FILE|EW_SILENT) != OK)
1459 count = 0;
1460 }
1461
1462# ifdef FEAT_SPELL
1463 if (count == -1)
1464 {
1465 // Complete from active spelling. Skip "\<" in the pattern, we
1466 // don't use it as a RE.
1467 if (pat[0] == '\\' && pat[1] == '<')
1468 ptr = pat + 2;
1469 else
1470 ptr = pat;
1471 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1472 }
1473 else
1474# endif
1475 if (count > 0) // avoid warning for using "files" uninit
1476 {
1477 ins_compl_files(count, files, thesaurus, flags,
1478 &regmatch, buf, &dir);
1479 if (flags != DICT_EXACT)
1480 FreeWild(count, files);
1481 }
1482 if (flags != 0)
1483 break;
1484 }
1485
1486theend:
1487 p_scs = save_p_scs;
1488 vim_regfree(regmatch.regprog);
1489 vim_free(buf);
1490}
1491
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001492/*
1493 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1494 * skipping the word at 'skip_word'. Returns OK on success.
1495 */
1496 static int
1497thesarurs_add_words_in_line(
1498 char_u *fname,
1499 char_u **buf_arg,
1500 int dir,
1501 char_u *skip_word)
1502{
1503 int status = OK;
1504 char_u *ptr;
1505 char_u *wstart;
1506
1507 // Add the other matches on the line
1508 ptr = *buf_arg;
1509 while (!got_int)
1510 {
1511 // Find start of the next word. Skip white
1512 // space and punctuation.
1513 ptr = find_word_start(ptr);
1514 if (*ptr == NUL || *ptr == NL)
1515 break;
1516 wstart = ptr;
1517
1518 // Find end of the word.
1519 if (has_mbyte)
1520 // Japanese words may have characters in
1521 // different classes, only separate words
1522 // with single-byte non-word characters.
1523 while (*ptr != NUL)
1524 {
1525 int l = (*mb_ptr2len)(ptr);
1526
1527 if (l < 2 && !vim_iswordc(*ptr))
1528 break;
1529 ptr += l;
1530 }
1531 else
1532 ptr = find_word_end(ptr);
1533
1534 // Add the word. Skip the regexp match.
1535 if (wstart != skip_word)
1536 {
1537 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1538 fname, dir, FALSE);
1539 if (status == FAIL)
1540 break;
1541 }
1542 }
1543
1544 *buf_arg = ptr;
1545 return status;
1546}
1547
1548/*
1549 * Process "count" dictionary/thesaurus "files" and add the text matching
1550 * "regmatch".
1551 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001552 static void
1553ins_compl_files(
1554 int count,
1555 char_u **files,
1556 int thesaurus,
1557 int flags,
1558 regmatch_T *regmatch,
1559 char_u *buf,
1560 int *dir)
1561{
1562 char_u *ptr;
1563 int i;
1564 FILE *fp;
1565 int add_r;
1566
1567 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1568 {
1569 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1570 if (flags != DICT_EXACT)
1571 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001572 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001573 vim_snprintf((char *)IObuff, IOSIZE,
1574 _("Scanning dictionary: %s"), (char *)files[i]);
1575 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1576 }
1577
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001578 if (fp == NULL)
1579 continue;
1580
1581 // Read dictionary file line by line.
1582 // Check each line for a match.
1583 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001584 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001585 ptr = buf;
1586 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001587 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001588 ptr = regmatch->startp[0];
1589 if (ctrl_x_mode_line_or_eval())
1590 ptr = find_line_end(ptr);
1591 else
1592 ptr = find_word_end(ptr);
1593 add_r = ins_compl_add_infercase(regmatch->startp[0],
1594 (int)(ptr - regmatch->startp[0]),
1595 p_ic, files[i], *dir, FALSE);
1596 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001597 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001598 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001599 ptr = buf;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001600 add_r = thesarurs_add_words_in_line(files[i], &ptr, *dir,
1601 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001602 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001603 if (add_r == OK)
1604 // if dir was BACKWARD then honor it just once
1605 *dir = FORWARD;
1606 else if (add_r == FAIL)
1607 break;
1608 // avoid expensive call to vim_regexec() when at end
1609 // of line
1610 if (*ptr == '\n' || got_int)
1611 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001612 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001613 line_breakcheck();
1614 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001615 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001616 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001617 }
1618}
1619
1620/*
1621 * Find the start of the next word.
1622 * Returns a pointer to the first char of the word. Also stops at a NUL.
1623 */
1624 char_u *
1625find_word_start(char_u *ptr)
1626{
1627 if (has_mbyte)
1628 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1629 ptr += (*mb_ptr2len)(ptr);
1630 else
1631 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1632 ++ptr;
1633 return ptr;
1634}
1635
1636/*
1637 * Find the end of the word. Assumes it starts inside a word.
1638 * Returns a pointer to just after the word.
1639 */
1640 char_u *
1641find_word_end(char_u *ptr)
1642{
1643 int start_class;
1644
1645 if (has_mbyte)
1646 {
1647 start_class = mb_get_class(ptr);
1648 if (start_class > 1)
1649 while (*ptr != NUL)
1650 {
1651 ptr += (*mb_ptr2len)(ptr);
1652 if (mb_get_class(ptr) != start_class)
1653 break;
1654 }
1655 }
1656 else
1657 while (vim_iswordc(*ptr))
1658 ++ptr;
1659 return ptr;
1660}
1661
1662/*
1663 * Find the end of the line, omitting CR and NL at the end.
1664 * Returns a pointer to just after the line.
1665 */
1666 static char_u *
1667find_line_end(char_u *ptr)
1668{
1669 char_u *s;
1670
1671 s = ptr + STRLEN(ptr);
1672 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1673 --s;
1674 return s;
1675}
1676
1677/*
1678 * Free the list of completions
1679 */
1680 static void
1681ins_compl_free(void)
1682{
1683 compl_T *match;
1684 int i;
1685
1686 VIM_CLEAR(compl_pattern);
1687 VIM_CLEAR(compl_leader);
1688
1689 if (compl_first_match == NULL)
1690 return;
1691
1692 ins_compl_del_pum();
1693 pum_clear();
1694
1695 compl_curr_match = compl_first_match;
1696 do
1697 {
1698 match = compl_curr_match;
1699 compl_curr_match = compl_curr_match->cp_next;
1700 vim_free(match->cp_str);
1701 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001702 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001703 vim_free(match->cp_fname);
1704 for (i = 0; i < CPT_COUNT; ++i)
1705 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001706#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001707 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001708#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001709 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001710 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001711 compl_first_match = compl_curr_match = NULL;
1712 compl_shown_match = NULL;
1713 compl_old_match = NULL;
1714}
1715
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001716/*
1717 * Reset/clear the completion state.
1718 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001719 void
1720ins_compl_clear(void)
1721{
1722 compl_cont_status = 0;
1723 compl_started = FALSE;
1724 compl_matches = 0;
1725 VIM_CLEAR(compl_pattern);
1726 VIM_CLEAR(compl_leader);
1727 edit_submode_extra = NULL;
1728 VIM_CLEAR(compl_orig_text);
1729 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001730#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001731 // clear v:completed_item
1732 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001733#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001734}
1735
1736/*
1737 * Return TRUE when Insert completion is active.
1738 */
1739 int
1740ins_compl_active(void)
1741{
1742 return compl_started;
1743}
1744
1745/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001746 * Selected one of the matches. When FALSE the match was edited or using the
1747 * longest common string.
1748 */
1749 int
1750ins_compl_used_match(void)
1751{
1752 return compl_used_match;
1753}
1754
1755/*
1756 * Initialize get longest common string.
1757 */
1758 void
1759ins_compl_init_get_longest(void)
1760{
1761 compl_get_longest = FALSE;
1762}
1763
1764/*
1765 * Returns TRUE when insert completion is interrupted.
1766 */
1767 int
1768ins_compl_interrupted(void)
1769{
1770 return compl_interrupted;
1771}
1772
1773/*
1774 * Returns TRUE if the <Enter> key selects a match in the completion popup
1775 * menu.
1776 */
1777 int
1778ins_compl_enter_selects(void)
1779{
1780 return compl_enter_selects;
1781}
1782
1783/*
1784 * Return the column where the text starts that is being completed
1785 */
1786 colnr_T
1787ins_compl_col(void)
1788{
1789 return compl_col;
1790}
1791
1792/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001793 * Return the length in bytes of the text being completed
1794 */
1795 int
1796ins_compl_len(void)
1797{
1798 return compl_length;
1799}
1800
1801/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001802 * Delete one character before the cursor and show the subset of the matches
1803 * that match the word that is now before the cursor.
1804 * Returns the character to be used, NUL if the work is done and another char
1805 * to be got from the user.
1806 */
1807 int
1808ins_compl_bs(void)
1809{
1810 char_u *line;
1811 char_u *p;
1812
1813 line = ml_get_curline();
1814 p = line + curwin->w_cursor.col;
1815 MB_PTR_BACK(line, p);
1816
1817 // Stop completion when the whole word was deleted. For Omni completion
1818 // allow the word to be deleted, we won't match everything.
1819 // Respect the 'backspace' option.
1820 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001821 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1822 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001823 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1824 - compl_length < 0))
1825 return K_BS;
1826
1827 // Deleted more than what was used to find matches or didn't finish
1828 // finding all matches: need to look for matches all over again.
1829 if (curwin->w_cursor.col <= compl_col + compl_length
1830 || ins_compl_need_restart())
1831 ins_compl_restart();
1832
1833 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001834 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001835 if (compl_leader == NULL)
1836 return K_BS;
1837
1838 ins_compl_new_leader();
1839 if (compl_shown_match != NULL)
1840 // Make sure current match is not a hidden item.
1841 compl_curr_match = compl_shown_match;
1842 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001843}
1844
1845/*
1846 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1847 * be called.
1848 */
1849 static int
1850ins_compl_need_restart(void)
1851{
1852 // Return TRUE if we didn't complete finding matches or when the
1853 // 'completefunc' returned "always" in the "refresh" dictionary item.
1854 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001855 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856 && compl_opt_refresh_always);
1857}
1858
1859/*
1860 * Called after changing "compl_leader".
1861 * Show the popup menu with a different set of matches.
1862 * May also search for matches again if the previous search was interrupted.
1863 */
1864 static void
1865ins_compl_new_leader(void)
1866{
1867 ins_compl_del_pum();
1868 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001869 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001870 compl_used_match = FALSE;
1871
1872 if (compl_started)
1873 ins_compl_set_original_text(compl_leader);
1874 else
1875 {
1876#ifdef FEAT_SPELL
1877 spell_bad_len = 0; // need to redetect bad word
1878#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001879 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001880 // the popup menu display the changed text before the cursor. Set
1881 // "compl_restarting" to avoid that the first match is inserted.
1882 pum_call_update_screen();
1883#ifdef FEAT_GUI
1884 if (gui.in_use)
1885 {
1886 // Show the cursor after the match, not after the redrawn text.
1887 setcursor();
1888 out_flush_cursor(FALSE, FALSE);
1889 }
1890#endif
1891 compl_restarting = TRUE;
1892 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1893 compl_cont_status = 0;
1894 compl_restarting = FALSE;
1895 }
1896
1897 compl_enter_selects = !compl_used_match;
1898
1899 // Show the popup menu with a different set of matches.
1900 ins_compl_show_pum();
1901
1902 // Don't let Enter select the original text when there is no popup menu.
1903 if (compl_match_array == NULL)
1904 compl_enter_selects = FALSE;
1905}
1906
1907/*
1908 * Return the length of the completion, from the completion start column to
1909 * the cursor column. Making sure it never goes below zero.
1910 */
1911 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001912get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001913{
1914 int off = (int)curwin->w_cursor.col - (int)compl_col;
1915
1916 if (off < 0)
1917 return 0;
1918 return off;
1919}
1920
1921/*
1922 * Append one character to the match leader. May reduce the number of
1923 * matches.
1924 */
1925 void
1926ins_compl_addleader(int c)
1927{
1928 int cc;
1929
1930 if (stop_arrow() == FAIL)
1931 return;
1932 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1933 {
1934 char_u buf[MB_MAXBYTES + 1];
1935
1936 (*mb_char2bytes)(c, buf);
1937 buf[cc] = NUL;
1938 ins_char_bytes(buf, cc);
1939 if (compl_opt_refresh_always)
1940 AppendToRedobuff(buf);
1941 }
1942 else
1943 {
1944 ins_char(c);
1945 if (compl_opt_refresh_always)
1946 AppendCharToRedobuff(c);
1947 }
1948
1949 // If we didn't complete finding matches we must search again.
1950 if (ins_compl_need_restart())
1951 ins_compl_restart();
1952
1953 // When 'always' is set, don't reset compl_leader. While completing,
1954 // cursor doesn't point original position, changing compl_leader would
1955 // break redo.
1956 if (!compl_opt_refresh_always)
1957 {
1958 vim_free(compl_leader);
1959 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001960 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001961 if (compl_leader != NULL)
1962 ins_compl_new_leader();
1963 }
1964}
1965
1966/*
1967 * Setup for finding completions again without leaving CTRL-X mode. Used when
1968 * BS or a key was typed while still searching for matches.
1969 */
1970 static void
1971ins_compl_restart(void)
1972{
1973 ins_compl_free();
1974 compl_started = FALSE;
1975 compl_matches = 0;
1976 compl_cont_status = 0;
1977 compl_cont_mode = 0;
1978}
1979
1980/*
1981 * Set the first match, the original text.
1982 */
1983 static void
1984ins_compl_set_original_text(char_u *str)
1985{
1986 char_u *p;
1987
1988 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001989 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1990 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001991 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992 {
1993 p = vim_strsave(str);
1994 if (p != NULL)
1995 {
1996 vim_free(compl_first_match->cp_str);
1997 compl_first_match->cp_str = p;
1998 }
1999 }
2000 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002001 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002002 {
2003 p = vim_strsave(str);
2004 if (p != NULL)
2005 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002006 vim_free(compl_first_match->cp_prev->cp_str);
2007 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002008 }
2009 }
2010}
2011
2012/*
2013 * Append one character to the match leader. May reduce the number of
2014 * matches.
2015 */
2016 void
2017ins_compl_addfrommatch(void)
2018{
2019 char_u *p;
2020 int len = (int)curwin->w_cursor.col - (int)compl_col;
2021 int c;
2022 compl_T *cp;
2023
2024 p = compl_shown_match->cp_str;
2025 if ((int)STRLEN(p) <= len) // the match is too short
2026 {
2027 // When still at the original match use the first entry that matches
2028 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002029 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002030 return;
2031
2032 p = NULL;
2033 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002034 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002035 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002036 if (compl_leader == NULL
2037 || ins_compl_equal(cp, compl_leader,
2038 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002039 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002040 p = cp->cp_str;
2041 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002042 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002043 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002044 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002045 return;
2046 }
2047 p += len;
2048 c = PTR2CHAR(p);
2049 ins_compl_addleader(c);
2050}
2051
2052/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002053 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002054 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2055 * compl_cont_mode and compl_cont_status.
2056 * Returns TRUE when the character is not to be inserted.
2057 */
2058 static int
2059set_ctrl_x_mode(int c)
2060{
2061 int retval = FALSE;
2062
2063 switch (c)
2064 {
2065 case Ctrl_E:
2066 case Ctrl_Y:
2067 // scroll the window one line up or down
2068 ctrl_x_mode = CTRL_X_SCROLL;
2069 if (!(State & REPLACE_FLAG))
2070 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2071 else
2072 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2073 edit_submode_pre = NULL;
2074 showmode();
2075 break;
2076 case Ctrl_L:
2077 // complete whole line
2078 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2079 break;
2080 case Ctrl_F:
2081 // complete filenames
2082 ctrl_x_mode = CTRL_X_FILES;
2083 break;
2084 case Ctrl_K:
2085 // complete words from a dictinoary
2086 ctrl_x_mode = CTRL_X_DICTIONARY;
2087 break;
2088 case Ctrl_R:
2089 // Register insertion without exiting CTRL-X mode
2090 // Simply allow ^R to happen without affecting ^X mode
2091 break;
2092 case Ctrl_T:
2093 // complete words from a thesaurus
2094 ctrl_x_mode = CTRL_X_THESAURUS;
2095 break;
2096#ifdef FEAT_COMPL_FUNC
2097 case Ctrl_U:
2098 // user defined completion
2099 ctrl_x_mode = CTRL_X_FUNCTION;
2100 break;
2101 case Ctrl_O:
2102 // omni completion
2103 ctrl_x_mode = CTRL_X_OMNI;
2104 break;
2105#endif
2106 case 's':
2107 case Ctrl_S:
2108 // complete spelling suggestions
2109 ctrl_x_mode = CTRL_X_SPELL;
2110#ifdef FEAT_SPELL
2111 ++emsg_off; // Avoid getting the E756 error twice.
2112 spell_back_to_badword();
2113 --emsg_off;
2114#endif
2115 break;
2116 case Ctrl_RSB:
2117 // complete tag names
2118 ctrl_x_mode = CTRL_X_TAGS;
2119 break;
2120#ifdef FEAT_FIND_ID
2121 case Ctrl_I:
2122 case K_S_TAB:
2123 // complete keywords from included files
2124 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2125 break;
2126 case Ctrl_D:
2127 // complete definitions from included files
2128 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2129 break;
2130#endif
2131 case Ctrl_V:
2132 case Ctrl_Q:
2133 // complete vim commands
2134 ctrl_x_mode = CTRL_X_CMDLINE;
2135 break;
2136 case Ctrl_Z:
2137 // stop completion
2138 ctrl_x_mode = CTRL_X_NORMAL;
2139 edit_submode = NULL;
2140 showmode();
2141 retval = TRUE;
2142 break;
2143 case Ctrl_P:
2144 case Ctrl_N:
2145 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2146 // just started ^X mode, or there were enough ^X's to cancel
2147 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2148 // do normal expansion when interrupting a different mode (say
2149 // ^X^F^X^P or ^P^X^X^P, see below)
2150 // nothing changes if interrupting mode 0, (eg, the flag
2151 // doesn't change when going to ADDING mode -- Acevedo
2152 if (!(compl_cont_status & CONT_INTRPT))
2153 compl_cont_status |= CONT_LOCAL;
2154 else if (compl_cont_mode != 0)
2155 compl_cont_status &= ~CONT_LOCAL;
2156 // FALLTHROUGH
2157 default:
2158 // If we have typed at least 2 ^X's... for modes != 0, we set
2159 // compl_cont_status = 0 (eg, as if we had just started ^X
2160 // mode).
2161 // For mode 0, we set "compl_cont_mode" to an impossible
2162 // value, in both cases ^X^X can be used to restart the same
2163 // mode (avoiding ADDING mode).
2164 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2165 // 'complete' and local ^P expansions respectively.
2166 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2167 // mode -- Acevedo
2168 if (c == Ctrl_X)
2169 {
2170 if (compl_cont_mode != 0)
2171 compl_cont_status = 0;
2172 else
2173 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2174 }
2175 ctrl_x_mode = CTRL_X_NORMAL;
2176 edit_submode = NULL;
2177 showmode();
2178 break;
2179 }
2180
2181 return retval;
2182}
2183
2184/*
2185 * Stop insert completion mode
2186 */
2187 static int
2188ins_compl_stop(int c, int prev_mode, int retval)
2189{
2190 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002191 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002192
2193 // Get here when we have finished typing a sequence of ^N and
2194 // ^P or other completion characters in CTRL-X mode. Free up
2195 // memory that was used, and make sure we can redo the insert.
2196 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2197 {
2198 // If any of the original typed text has been changed, eg when
2199 // ignorecase is set, we must add back-spaces to the redo
2200 // buffer. We add as few as necessary to delete just the part
2201 // of the original text that has changed.
2202 // When using the longest match, edited the match or used
2203 // CTRL-E then don't use the current match.
2204 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2205 ptr = compl_curr_match->cp_str;
2206 else
2207 ptr = NULL;
2208 ins_compl_fixRedoBufForLeader(ptr);
2209 }
2210
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002211 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002212
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002213 // When completing whole lines: fix indent for 'cindent'.
2214 // Otherwise, break line if it's too long.
2215 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2216 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002217 // re-indent the current line
2218 if (want_cindent)
2219 {
2220 do_c_expr_indent();
2221 want_cindent = FALSE; // don't do it again
2222 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002223 }
2224 else
2225 {
2226 int prev_col = curwin->w_cursor.col;
2227
2228 // put the cursor on the last char, for 'tw' formatting
2229 if (prev_col > 0)
2230 dec_cursor();
2231 // only format when something was inserted
2232 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2233 insertchar(NUL, 0, -1);
2234 if (prev_col > 0
2235 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2236 inc_cursor();
2237 }
2238
2239 // If the popup menu is displayed pressing CTRL-Y means accepting
2240 // the selection without inserting anything. When
2241 // compl_enter_selects is set the Enter key does the same.
2242 if ((c == Ctrl_Y || (compl_enter_selects
2243 && (c == CAR || c == K_KENTER || c == NL)))
2244 && pum_visible())
2245 retval = TRUE;
2246
2247 // CTRL-E means completion is Ended, go back to the typed text.
2248 // but only do this, if the Popup is still visible
2249 if (c == Ctrl_E)
2250 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002251 char_u *p = NULL;
2252
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002253 ins_compl_delete();
2254 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002255 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002256 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002257 p = compl_orig_text;
2258 if (p != NULL)
2259 {
2260 int compl_len = get_compl_len();
2261 int len = (int)STRLEN(p);
2262
2263 if (len > compl_len)
2264 ins_bytes_len(p + compl_len, len - compl_len);
2265 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002266 retval = TRUE;
2267 }
2268
2269 auto_format(FALSE, TRUE);
2270
2271 // Trigger the CompleteDonePre event to give scripts a chance to
2272 // act upon the completion before clearing the info, and restore
2273 // ctrl_x_mode, so that complete_info() can be used.
2274 ctrl_x_mode = prev_mode;
2275 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2276
2277 ins_compl_free();
2278 compl_started = FALSE;
2279 compl_matches = 0;
2280 if (!shortmess(SHM_COMPLETIONMENU))
2281 msg_clr_cmdline(); // necessary for "noshowmode"
2282 ctrl_x_mode = CTRL_X_NORMAL;
2283 compl_enter_selects = FALSE;
2284 if (edit_submode != NULL)
2285 {
2286 edit_submode = NULL;
2287 showmode();
2288 }
2289
2290#ifdef FEAT_CMDWIN
2291 if (c == Ctrl_C && cmdwin_type != 0)
2292 // Avoid the popup menu remains displayed when leaving the
2293 // command line window.
2294 update_screen(0);
2295#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002296 // Indent now if a key was typed that is in 'cinkeys'.
2297 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2298 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002299 // Trigger the CompleteDone event to give scripts a chance to act
2300 // upon the end of completion.
2301 ins_apply_autocmds(EVENT_COMPLETEDONE);
2302
2303 return retval;
2304}
2305
2306/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002307 * Prepare for Insert mode completion, or stop it.
2308 * Called just after typing a character in Insert mode.
2309 * Returns TRUE when the character is not to be inserted;
2310 */
2311 int
2312ins_compl_prep(int c)
2313{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002314 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002315 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002316
2317 // Forget any previous 'special' messages if this is actually
2318 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2319 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2320 edit_submode_extra = NULL;
2321
2322 // Ignore end of Select mode mapping and mouse scroll buttons.
2323 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002324 || c == K_MOUSELEFT || c == K_MOUSERIGHT
2325 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002326 return retval;
2327
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002328#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002329 // Ignore mouse events in a popup window
2330 if (is_mouse_key(c))
2331 {
2332 // Ignore drag and release events, the position does not need to be in
2333 // the popup and it may have just closed.
2334 if (c == K_LEFTRELEASE
2335 || c == K_LEFTRELEASE_NM
2336 || c == K_MIDDLERELEASE
2337 || c == K_RIGHTRELEASE
2338 || c == K_X1RELEASE
2339 || c == K_X2RELEASE
2340 || c == K_LEFTDRAG
2341 || c == K_MIDDLEDRAG
2342 || c == K_RIGHTDRAG
2343 || c == K_X1DRAG
2344 || c == K_X2DRAG)
2345 return retval;
2346 if (popup_visible)
2347 {
2348 int row = mouse_row;
2349 int col = mouse_col;
2350 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2351
2352 if (wp != NULL && WIN_IS_POPUP(wp))
2353 return retval;
2354 }
2355 }
2356#endif
2357
zeertzjqdca29d92021-08-31 19:12:51 +02002358 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2359 {
2360 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2361 || !vim_is_ctrl_x_key(c))
2362 {
2363 // Not starting another completion mode.
2364 ctrl_x_mode = CTRL_X_CMDLINE;
2365
2366 // CTRL-X CTRL-Z should stop completion without inserting anything
2367 if (c == Ctrl_Z)
2368 retval = TRUE;
2369 }
2370 else
2371 {
2372 ctrl_x_mode = CTRL_X_CMDLINE;
2373
2374 // Other CTRL-X keys first stop completion, then start another
2375 // completion mode.
2376 ins_compl_prep(' ');
2377 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2378 }
2379 }
2380
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002381 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002382 if (ctrl_x_mode_not_defined_yet()
2383 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002384 {
2385 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2386 compl_used_match = TRUE;
2387
2388 }
2389
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002390 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002391 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2392 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002393 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002394 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002395 {
2396 // We're already in CTRL-X mode, do we stay in it?
2397 if (!vim_is_ctrl_x_key(c))
2398 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002399 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002400 ctrl_x_mode = CTRL_X_NORMAL;
2401 else
2402 ctrl_x_mode = CTRL_X_FINISHED;
2403 edit_submode = NULL;
2404 }
2405 showmode();
2406 }
2407
2408 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2409 {
2410 // Show error message from attempted keyword completion (probably
2411 // 'Pattern not found') until another key is hit, then go back to
2412 // showing what mode we are in.
2413 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002414 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002415 && c != Ctrl_R && !ins_compl_pum_key(c))
2416 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002417 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002418 }
2419 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2420 // Trigger the CompleteDone event to give scripts a chance to act
2421 // upon the (possibly failed) completion.
2422 ins_apply_autocmds(EVENT_COMPLETEDONE);
2423
LemonBoy2bf52dd2022-04-09 18:17:34 +01002424 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002425
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002426 // reset continue_* if we left expansion-mode, if we stay they'll be
2427 // (re)set properly in ins_complete()
2428 if (!vim_is_ctrl_x_key(c))
2429 {
2430 compl_cont_status = 0;
2431 compl_cont_mode = 0;
2432 }
2433
2434 return retval;
2435}
2436
2437/*
2438 * Fix the redo buffer for the completion leader replacing some of the typed
2439 * text. This inserts backspaces and appends the changed text.
2440 * "ptr" is the known leader text or NUL.
2441 */
2442 static void
2443ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2444{
2445 int len;
2446 char_u *p;
2447 char_u *ptr = ptr_arg;
2448
2449 if (ptr == NULL)
2450 {
2451 if (compl_leader != NULL)
2452 ptr = compl_leader;
2453 else
2454 return; // nothing to do
2455 }
2456 if (compl_orig_text != NULL)
2457 {
2458 p = compl_orig_text;
2459 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2460 ;
2461 if (len > 0)
2462 len -= (*mb_head_off)(p, p + len);
2463 for (p += len; *p != NUL; MB_PTR_ADV(p))
2464 AppendCharToRedobuff(K_BS);
2465 }
2466 else
2467 len = 0;
2468 if (ptr != NULL)
2469 AppendToRedobuffLit(ptr + len, -1);
2470}
2471
2472/*
2473 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2474 * (depending on flag) starting from buf and looking for a non-scanned
2475 * buffer (other than curbuf). curbuf is special, if it is called with
2476 * buf=curbuf then it has to be the first call for a given flag/expansion.
2477 *
2478 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2479 */
2480 static buf_T *
2481ins_compl_next_buf(buf_T *buf, int flag)
2482{
2483 static win_T *wp = NULL;
2484
2485 if (flag == 'w') // just windows
2486 {
2487 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2488 wp = curwin;
2489 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2490 && wp->w_buffer->b_scanned)
2491 ;
2492 buf = wp->w_buffer;
2493 }
2494 else
2495 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2496 // (unlisted buffers)
2497 // When completing whole lines skip unloaded buffers.
2498 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2499 && ((flag == 'U'
2500 ? buf->b_p_bl
2501 : (!buf->b_p_bl
2502 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2503 || buf->b_scanned))
2504 ;
2505 return buf;
2506}
2507
2508#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002509
2510# ifdef FEAT_EVAL
2511static callback_T cfu_cb; // 'completefunc' callback function
2512static callback_T ofu_cb; // 'omnifunc' callback function
2513static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2514# endif
2515
2516/*
2517 * Copy a global callback function to a buffer local callback.
2518 */
2519 static void
2520copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2521{
2522 free_callback(bufcb);
2523 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2524 copy_callback(bufcb, globcb);
2525}
2526
2527/*
2528 * Parse the 'completefunc' option value and set the callback function.
2529 * Invoked when the 'completefunc' option is set. The option value can be a
2530 * name of a function (string), or function(<name>) or funcref(<name>) or a
2531 * lambda expression.
2532 */
2533 int
2534set_completefunc_option(void)
2535{
2536 int retval;
2537
2538 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2539 if (retval == OK)
2540 set_buflocal_cfu_callback(curbuf);
2541
2542 return retval;
2543}
2544
2545/*
2546 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002547 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002548 */
2549 void
2550set_buflocal_cfu_callback(buf_T *buf UNUSED)
2551{
2552# ifdef FEAT_EVAL
2553 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2554# endif
2555}
2556
2557/*
2558 * Parse the 'omnifunc' option value and set the callback function.
2559 * Invoked when the 'omnifunc' option is set. The option value can be a
2560 * name of a function (string), or function(<name>) or funcref(<name>) or a
2561 * lambda expression.
2562 */
2563 int
2564set_omnifunc_option(void)
2565{
2566 int retval;
2567
2568 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2569 if (retval == OK)
2570 set_buflocal_ofu_callback(curbuf);
2571
2572 return retval;
2573}
2574
2575/*
2576 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002577 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002578 */
2579 void
2580set_buflocal_ofu_callback(buf_T *buf UNUSED)
2581{
2582# ifdef FEAT_EVAL
2583 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2584# endif
2585}
2586
2587/*
2588 * Parse the 'thesaurusfunc' option value and set the callback function.
2589 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2590 * name of a function (string), or function(<name>) or funcref(<name>) or a
2591 * lambda expression.
2592 */
2593 int
2594set_thesaurusfunc_option(void)
2595{
2596 int retval;
2597
2598 if (*curbuf->b_p_tsrfu != NUL)
2599 {
2600 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002601 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2602 &curbuf->b_tsrfu_cb);
2603 }
2604 else
2605 {
2606 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002607 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2608 }
2609
2610 return retval;
2611}
2612
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002613/*
2614 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002615 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002616 */
2617 int
2618set_ref_in_insexpand_funcs(int copyID)
2619{
2620 int abort = FALSE;
2621
2622 abort = set_ref_in_callback(&cfu_cb, copyID);
2623 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2624 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2625
2626 return abort;
2627}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002628
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002629/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002630 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002631 */
2632 static char_u *
2633get_complete_funcname(int type)
2634{
2635 switch (type)
2636 {
2637 case CTRL_X_FUNCTION:
2638 return curbuf->b_p_cfu;
2639 case CTRL_X_OMNI:
2640 return curbuf->b_p_ofu;
2641 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002642 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002643 default:
2644 return (char_u *)"";
2645 }
2646}
2647
2648/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002649 * Get the callback to use for insert mode completion.
2650 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002651 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652get_insert_callback(int type)
2653{
2654 if (type == CTRL_X_FUNCTION)
2655 return &curbuf->b_cfu_cb;
2656 if (type == CTRL_X_OMNI)
2657 return &curbuf->b_ofu_cb;
2658 // CTRL_X_THESAURUS
2659 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2660}
2661
2662/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002663 * Execute user defined complete function 'completefunc', 'omnifunc' or
2664 * 'thesaurusfunc', and get matches in "matches".
2665 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002666 */
2667 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002668expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002669{
2670 list_T *matchlist = NULL;
2671 dict_T *matchdict = NULL;
2672 typval_T args[3];
2673 char_u *funcname;
2674 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002675 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002676 typval_T rettv;
2677 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002678 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002680 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002681 if (*funcname == NUL)
2682 return;
2683
2684 // Call 'completefunc' to obtain the list of matches.
2685 args[0].v_type = VAR_NUMBER;
2686 args[0].vval.v_number = 0;
2687 args[1].v_type = VAR_STRING;
2688 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2689 args[2].v_type = VAR_UNKNOWN;
2690
2691 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002692 // Lock the text to avoid weird things from happening. Also disallow
2693 // switching to another window, it should not be needed and may end up in
2694 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002695 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002696
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002697 cb = get_insert_callback(type);
2698 retval = call_callback(cb, 0, &rettv, 2, args);
2699
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002700 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002701 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002702 {
2703 switch (rettv.v_type)
2704 {
2705 case VAR_LIST:
2706 matchlist = rettv.vval.v_list;
2707 break;
2708 case VAR_DICT:
2709 matchdict = rettv.vval.v_dict;
2710 break;
2711 case VAR_SPECIAL:
2712 if (rettv.vval.v_number == VVAL_NONE)
2713 compl_opt_suppress_empty = TRUE;
2714 // FALLTHROUGH
2715 default:
2716 // TODO: Give error message?
2717 clear_tv(&rettv);
2718 break;
2719 }
2720 }
zeertzjqcfe45652022-05-27 17:26:55 +01002721 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002722
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002723 curwin->w_cursor = pos; // restore the cursor position
2724 validate_cursor();
2725 if (!EQUAL_POS(curwin->w_cursor, pos))
2726 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002727 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002728 goto theend;
2729 }
2730
2731 if (matchlist != NULL)
2732 ins_compl_add_list(matchlist);
2733 else if (matchdict != NULL)
2734 ins_compl_add_dict(matchdict);
2735
2736theend:
2737 // Restore State, it might have been changed.
2738 State = save_State;
2739
2740 if (matchdict != NULL)
2741 dict_unref(matchdict);
2742 if (matchlist != NULL)
2743 list_unref(matchlist);
2744}
2745#endif // FEAT_COMPL_FUNC
2746
2747#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2748/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002749 * Add a match to the list of matches from a typeval_T.
2750 * If the given string is already in the list of completions, then return
2751 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2752 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002753 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002754 */
2755 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002756ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002757{
2758 char_u *word;
2759 int dup = FALSE;
2760 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002761 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002762 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002763 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002764 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002765
Bram Moolenaar08928322020-01-04 14:32:48 +01002766 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002767 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2768 {
2769 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2770 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2771 (char_u *)"abbr", FALSE);
2772 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2773 (char_u *)"menu", FALSE);
2774 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2775 (char_u *)"kind", FALSE);
2776 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2777 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002778 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002779 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2780 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2781 flags |= CP_ICASE;
2782 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2783 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2784 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2785 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2786 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2787 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2788 flags |= CP_EQUAL;
2789 }
2790 else
2791 {
2792 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002793 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002794 }
2795 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002796 {
2797 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002798 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002799 }
2800 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2801 if (status != OK)
2802 clear_tv(&user_data);
2803 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002804}
2805
2806/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002807 * Add completions from a list.
2808 */
2809 static void
2810ins_compl_add_list(list_T *list)
2811{
2812 listitem_T *li;
2813 int dir = compl_direction;
2814
2815 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002816 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002817 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002818 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002819 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820 // if dir was BACKWARD then honor it just once
2821 dir = FORWARD;
2822 else if (did_emsg)
2823 break;
2824 }
2825}
2826
2827/*
2828 * Add completions from a dict.
2829 */
2830 static void
2831ins_compl_add_dict(dict_T *dict)
2832{
2833 dictitem_T *di_refresh;
2834 dictitem_T *di_words;
2835
2836 // Check for optional "refresh" item.
2837 compl_opt_refresh_always = FALSE;
2838 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2839 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2840 {
2841 char_u *v = di_refresh->di_tv.vval.v_string;
2842
2843 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2844 compl_opt_refresh_always = TRUE;
2845 }
2846
2847 // Add completions from a "words" list.
2848 di_words = dict_find(dict, (char_u *)"words", 5);
2849 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2850 ins_compl_add_list(di_words->di_tv.vval.v_list);
2851}
2852
2853/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002854 * Start completion for the complete() function.
2855 * "startcol" is where the matched text starts (1 is first column).
2856 * "list" is the list of matches.
2857 */
2858 static void
2859set_completion(colnr_T startcol, list_T *list)
2860{
2861 int save_w_wrow = curwin->w_wrow;
2862 int save_w_leftcol = curwin->w_leftcol;
2863 int flags = CP_ORIGINAL_TEXT;
2864
2865 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002866 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002867 ins_compl_prep(' ');
2868 ins_compl_clear();
2869 ins_compl_free();
2870
2871 compl_direction = FORWARD;
2872 if (startcol > curwin->w_cursor.col)
2873 startcol = curwin->w_cursor.col;
2874 compl_col = startcol;
2875 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2876 // compl_pattern doesn't need to be set
2877 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2878 if (p_ic)
2879 flags |= CP_ICASE;
2880 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002881 -1, NULL, NULL, NULL, 0,
2882 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002883 return;
2884
2885 ctrl_x_mode = CTRL_X_EVAL;
2886
2887 ins_compl_add_list(list);
2888 compl_matches = ins_compl_make_cyclic();
2889 compl_started = TRUE;
2890 compl_used_match = TRUE;
2891 compl_cont_status = 0;
2892
2893 compl_curr_match = compl_first_match;
2894 if (compl_no_insert || compl_no_select)
2895 {
2896 ins_complete(K_DOWN, FALSE);
2897 if (compl_no_select)
2898 // Down/Up has no real effect.
2899 ins_complete(K_UP, FALSE);
2900 }
2901 else
2902 ins_complete(Ctrl_N, FALSE);
2903 compl_enter_selects = compl_no_insert;
2904
2905 // Lazily show the popup menu, unless we got interrupted.
2906 if (!compl_interrupted)
2907 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002908 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002909 out_flush();
2910}
2911
2912/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002913 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002914 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002915 void
2916f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002917{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002918 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002919
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002920 if (in_vim9script()
2921 && (check_for_number_arg(argvars, 0) == FAIL
2922 || check_for_list_arg(argvars, 1) == FAIL))
2923 return;
2924
Bram Moolenaar24959102022-05-07 20:01:16 +01002925 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002926 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002927 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002928 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002929 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002930
2931 // Check for undo allowed here, because if something was already inserted
2932 // the line was already saved for undo and this check isn't done.
2933 if (!undo_allowed())
2934 return;
2935
2936 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002937 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002938 else
2939 {
2940 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2941 if (startcol > 0)
2942 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002943 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002944}
2945
2946/*
2947 * "complete_add()" function
2948 */
2949 void
2950f_complete_add(typval_T *argvars, typval_T *rettv)
2951{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002952 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002953 return;
2954
Bram Moolenaar440cf092021-04-03 20:13:30 +02002955 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002956}
2957
2958/*
2959 * "complete_check()" function
2960 */
2961 void
2962f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2963{
2964 int saved = RedrawingDisabled;
2965
2966 RedrawingDisabled = 0;
2967 ins_compl_check_keys(0, TRUE);
2968 rettv->vval.v_number = ins_compl_interrupted();
2969 RedrawingDisabled = saved;
2970}
2971
2972/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002973 * Return Insert completion mode name string
2974 */
2975 static char_u *
2976ins_compl_mode(void)
2977{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002978 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002979 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2980
2981 return (char_u *)"";
2982}
2983
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002984/*
2985 * Assign the sequence number to all the completion matches which don't have
2986 * one assigned yet.
2987 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002988 static void
2989ins_compl_update_sequence_numbers()
2990{
2991 int number = 0;
2992 compl_T *match;
2993
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002994 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002995 {
2996 // search backwards for the first valid (!= -1) number.
2997 // This should normally succeed already at the first loop
2998 // cycle, so it's fast!
2999 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003000 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003001 if (match->cp_number != -1)
3002 {
3003 number = match->cp_number;
3004 break;
3005 }
3006 if (match != NULL)
3007 // go up and assign all numbers which are not assigned
3008 // yet
3009 for (match = match->cp_next;
3010 match != NULL && match->cp_number == -1;
3011 match = match->cp_next)
3012 match->cp_number = ++number;
3013 }
3014 else // BACKWARD
3015 {
3016 // search forwards (upwards) for the first valid (!= -1)
3017 // number. This should normally succeed already at the
3018 // first loop cycle, so it's fast!
3019 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003020 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003021 if (match->cp_number != -1)
3022 {
3023 number = match->cp_number;
3024 break;
3025 }
3026 if (match != NULL)
3027 // go down and assign all numbers which are not
3028 // assigned yet
3029 for (match = match->cp_prev; match
3030 && match->cp_number == -1;
3031 match = match->cp_prev)
3032 match->cp_number = ++number;
3033 }
3034}
3035
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003036/*
3037 * Get complete information
3038 */
3039 static void
3040get_complete_info(list_T *what_list, dict_T *retdict)
3041{
3042 int ret = OK;
3043 listitem_T *item;
3044#define CI_WHAT_MODE 0x01
3045#define CI_WHAT_PUM_VISIBLE 0x02
3046#define CI_WHAT_ITEMS 0x04
3047#define CI_WHAT_SELECTED 0x08
3048#define CI_WHAT_INSERTED 0x10
3049#define CI_WHAT_ALL 0xff
3050 int what_flag;
3051
3052 if (what_list == NULL)
3053 what_flag = CI_WHAT_ALL;
3054 else
3055 {
3056 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003057 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003058 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003059 {
3060 char_u *what = tv_get_string(&item->li_tv);
3061
3062 if (STRCMP(what, "mode") == 0)
3063 what_flag |= CI_WHAT_MODE;
3064 else if (STRCMP(what, "pum_visible") == 0)
3065 what_flag |= CI_WHAT_PUM_VISIBLE;
3066 else if (STRCMP(what, "items") == 0)
3067 what_flag |= CI_WHAT_ITEMS;
3068 else if (STRCMP(what, "selected") == 0)
3069 what_flag |= CI_WHAT_SELECTED;
3070 else if (STRCMP(what, "inserted") == 0)
3071 what_flag |= CI_WHAT_INSERTED;
3072 }
3073 }
3074
3075 if (ret == OK && (what_flag & CI_WHAT_MODE))
3076 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3077
3078 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3079 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3080
3081 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3082 {
3083 list_T *li;
3084 dict_T *di;
3085 compl_T *match;
3086
3087 li = list_alloc();
3088 if (li == NULL)
3089 return;
3090 ret = dict_add_list(retdict, "items", li);
3091 if (ret == OK && compl_first_match != NULL)
3092 {
3093 match = compl_first_match;
3094 do
3095 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003096 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003097 {
3098 di = dict_alloc();
3099 if (di == NULL)
3100 return;
3101 ret = list_append_dict(li, di);
3102 if (ret != OK)
3103 return;
3104 dict_add_string(di, "word", match->cp_str);
3105 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3106 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3107 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3108 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003109 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3110 // Add an empty string for backwards compatibility
3111 dict_add_string(di, "user_data", (char_u *)"");
3112 else
3113 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003114 }
3115 match = match->cp_next;
3116 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003117 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003118 }
3119 }
3120
3121 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003122 {
3123 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3124 ins_compl_update_sequence_numbers();
3125 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3126 ? compl_curr_match->cp_number - 1 : -1);
3127 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003128
3129 // TODO
3130 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3131}
3132
3133/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003134 * "complete_info()" function
3135 */
3136 void
3137f_complete_info(typval_T *argvars, typval_T *rettv)
3138{
3139 list_T *what_list = NULL;
3140
Bram Moolenaar93a10962022-06-16 11:42:09 +01003141 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003142 return;
3143
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003144 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3145 return;
3146
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003147 if (argvars[0].v_type != VAR_UNKNOWN)
3148 {
3149 if (argvars[0].v_type != VAR_LIST)
3150 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003151 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003152 return;
3153 }
3154 what_list = argvars[0].vval.v_list;
3155 }
3156 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003157}
3158#endif
3159
3160/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003161 * Returns TRUE when using a user-defined function for thesaurus completion.
3162 */
3163 static int
3164thesaurus_func_complete(int type UNUSED)
3165{
3166#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003167 return type == CTRL_X_THESAURUS
3168 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003169#else
3170 return FALSE;
3171#endif
3172}
3173
3174/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003175 * Return value of process_next_cpt_value()
3176 */
3177enum
3178{
3179 INS_COMPL_CPT_OK = 1,
3180 INS_COMPL_CPT_CONT,
3181 INS_COMPL_CPT_END
3182};
3183
3184/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003185 * state information used for getting the next set of insert completion
3186 * matches.
3187 */
3188typedef struct
3189{
3190 char_u *e_cpt; // current entry in 'complete'
3191 buf_T *ins_buf; // buffer being scanned
3192 pos_T *cur_match_pos; // current match position
3193 pos_T prev_match_pos; // previous match position
3194 int set_match_pos; // save first_match_pos/last_match_pos
3195 pos_T first_match_pos; // first match position
3196 pos_T last_match_pos; // last match position
3197 int found_all; // found all matches of a certain type.
3198 char_u *dict; // dictionary file to search
3199 int dict_f; // "dict" is an exact file name or not
3200} ins_compl_next_state_T;
3201
3202/*
3203 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003204 *
3205 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003206 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003207 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003208 * st->found_all - all matches of this type are found
3209 * st->ins_buf - search for completions in this buffer
3210 * st->first_match_pos - position of the first completion match
3211 * st->last_match_pos - position of the last completion match
3212 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003213 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003214 * st->dict - name of the dictionary or thesaurus file to search
3215 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003216 *
3217 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003218 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3219 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003220 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003221 */
3222 static int
3223process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003224 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003225 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003226 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003227{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003228 int compl_type = -1;
3229 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003230
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003231 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003232
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003233 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3234 st->e_cpt++;
3235
3236 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003237 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003238 st->ins_buf = curbuf;
3239 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003240 // Move the cursor back one character so that ^N can match the
3241 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003242 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003243 {
3244 // Move the cursor to after the last character in the
3245 // buffer, so that word at start of buffer is found
3246 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3248 st->first_match_pos.col =
3249 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003250 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003251 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003252 compl_type = 0;
3253
3254 // Remember the first match so that the loop stops when we
3255 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003256 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003257 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003258 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3259 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003260 {
3261 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003262 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 {
3264 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003265 st->first_match_pos.col = st->last_match_pos.col = 0;
3266 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3267 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003268 compl_type = 0;
3269 }
3270 else // unloaded buffer, scan like dictionary
3271 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003272 st->found_all = TRUE;
3273 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 {
3275 status = INS_COMPL_CPT_CONT;
3276 goto done;
3277 }
3278 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003279 st->dict = st->ins_buf->b_fname;
3280 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003281 }
3282 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3283 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003284 st->ins_buf->b_fname == NULL
3285 ? buf_spname(st->ins_buf)
3286 : st->ins_buf->b_sfname == NULL
3287 ? st->ins_buf->b_fname
3288 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003289 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3290 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003291 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003292 status = INS_COMPL_CPT_END;
3293 else
3294 {
3295 if (ctrl_x_mode_line_or_eval())
3296 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003297 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003298 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003299 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003300 compl_type = CTRL_X_DICTIONARY;
3301 else
3302 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003303 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003304 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003305 st->dict = st->e_cpt;
3306 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003307 }
3308 }
3309#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003310 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003311 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003312 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003313 compl_type = CTRL_X_PATH_DEFINES;
3314#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316 {
3317 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3318 compl_type = CTRL_X_TAGS;
3319 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3320 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3321 }
3322 else
3323 compl_type = -1;
3324
3325 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003326 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003327
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003328 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003329 if (compl_type == -1)
3330 status = INS_COMPL_CPT_CONT;
3331 }
3332
3333done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003335 return status;
3336}
3337
3338#ifdef FEAT_FIND_ID
3339/*
3340 * Get the next set of identifiers or defines matching "compl_pattern" in
3341 * included files.
3342 */
3343 static void
3344get_next_include_file_completion(int compl_type)
3345{
3346 find_pattern_in_path(compl_pattern, compl_direction,
3347 (int)STRLEN(compl_pattern), FALSE, FALSE,
3348 (compl_type == CTRL_X_PATH_DEFINES
3349 && !(compl_cont_status & CONT_SOL))
3350 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3351 (linenr_T)1, (linenr_T)MAXLNUM);
3352}
3353#endif
3354
3355/*
3356 * Get the next set of words matching "compl_pattern" in dictionary or
3357 * thesaurus files.
3358 */
3359 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003360get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003361{
3362#ifdef FEAT_COMPL_FUNC
3363 if (thesaurus_func_complete(compl_type))
3364 expand_by_function(compl_type, compl_pattern);
3365 else
3366#endif
3367 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003368 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003369 : (compl_type == CTRL_X_THESAURUS
3370 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3371 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3372 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003373 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003374 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003375}
3376
3377/*
3378 * Get the next set of tag names matching "compl_pattern".
3379 */
3380 static void
3381get_next_tag_completion(void)
3382{
3383 int save_p_ic;
3384 char_u **matches;
3385 int num_matches;
3386
3387 // set p_ic according to p_ic, p_scs and pat for find_tags().
3388 save_p_ic = p_ic;
3389 p_ic = ignorecase(compl_pattern);
3390
3391 // Find up to TAG_MANY matches. Avoids that an enormous number
3392 // of matches is found when compl_pattern is empty
3393 g_tag_at_cursor = TRUE;
3394 if (find_tags(compl_pattern, &num_matches, &matches,
3395 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003396 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003397 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3398 ins_compl_add_matches(num_matches, matches, p_ic);
3399 g_tag_at_cursor = FALSE;
3400 p_ic = save_p_ic;
3401}
3402
3403/*
3404 * Get the next set of filename matching "compl_pattern".
3405 */
3406 static void
3407get_next_filename_completion(void)
3408{
3409 char_u **matches;
3410 int num_matches;
3411
3412 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3413 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3414 return;
3415
3416 // May change home directory back to "~".
3417 tilde_replace(compl_pattern, num_matches, matches);
3418#ifdef BACKSLASH_IN_FILENAME
3419 if (curbuf->b_p_csl[0] != NUL)
3420 {
3421 int i;
3422
3423 for (i = 0; i < num_matches; ++i)
3424 {
3425 char_u *ptr = matches[i];
3426
3427 while (*ptr != NUL)
3428 {
3429 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3430 *ptr = '/';
3431 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3432 *ptr = '\\';
3433 ptr += (*mb_ptr2len)(ptr);
3434 }
3435 }
3436 }
3437#endif
3438 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3439}
3440
3441/*
3442 * Get the next set of command-line completions matching "compl_pattern".
3443 */
3444 static void
3445get_next_cmdline_completion()
3446{
3447 char_u **matches;
3448 int num_matches;
3449
3450 if (expand_cmdline(&compl_xp, compl_pattern,
3451 (int)STRLEN(compl_pattern),
3452 &num_matches, &matches) == EXPAND_OK)
3453 ins_compl_add_matches(num_matches, matches, FALSE);
3454}
3455
3456/*
3457 * Get the next set of spell suggestions matching "compl_pattern".
3458 */
3459 static void
3460get_next_spell_completion(linenr_T lnum UNUSED)
3461{
3462#ifdef FEAT_SPELL
3463 char_u **matches;
3464 int num_matches;
3465
3466 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3467 if (num_matches > 0)
3468 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003469 else
3470 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003471#endif
3472}
3473
3474/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003475 * Return the next word or line from buffer "ins_buf" at position
3476 * "cur_match_pos" for completion. The length of the match is set in "len".
3477 */
3478 static char_u *
3479ins_comp_get_next_word_or_line(
3480 buf_T *ins_buf, // buffer being scanned
3481 pos_T *cur_match_pos, // current match position
3482 int *match_len,
3483 int *cont_s_ipos) // next ^X<> will set initial_pos
3484{
3485 char_u *ptr;
3486 int len;
3487
3488 *match_len = 0;
3489 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3490 cur_match_pos->col;
3491 if (ctrl_x_mode_line_or_eval())
3492 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003493 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003494 {
3495 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3496 return NULL;
3497 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3498 if (!p_paste)
3499 ptr = skipwhite(ptr);
3500 }
3501 len = (int)STRLEN(ptr);
3502 }
3503 else
3504 {
3505 char_u *tmp_ptr = ptr;
3506
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003507 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003508 {
3509 tmp_ptr += compl_length;
3510 // Skip if already inside a word.
3511 if (vim_iswordp(tmp_ptr))
3512 return NULL;
3513 // Find start of next word.
3514 tmp_ptr = find_word_start(tmp_ptr);
3515 }
3516 // Find end of this word.
3517 tmp_ptr = find_word_end(tmp_ptr);
3518 len = (int)(tmp_ptr - ptr);
3519
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003520 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003521 {
3522 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3523 {
3524 // Try next line, if any. the new word will be
3525 // "join" as if the normal command "J" was used.
3526 // IOSIZE is always greater than
3527 // compl_length, so the next STRNCPY always
3528 // works -- Acevedo
3529 STRNCPY(IObuff, ptr, len);
3530 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3531 tmp_ptr = ptr = skipwhite(ptr);
3532 // Find start of next word.
3533 tmp_ptr = find_word_start(tmp_ptr);
3534 // Find end of next word.
3535 tmp_ptr = find_word_end(tmp_ptr);
3536 if (tmp_ptr > ptr)
3537 {
3538 if (*ptr != ')' && IObuff[len - 1] != TAB)
3539 {
3540 if (IObuff[len - 1] != ' ')
3541 IObuff[len++] = ' ';
3542 // IObuf =~ "\k.* ", thus len >= 2
3543 if (p_js
3544 && (IObuff[len - 2] == '.'
3545 || (vim_strchr(p_cpo, CPO_JOINSP)
3546 == NULL
3547 && (IObuff[len - 2] == '?'
3548 || IObuff[len - 2] == '!'))))
3549 IObuff[len++] = ' ';
3550 }
3551 // copy as much as possible of the new word
3552 if (tmp_ptr - ptr >= IOSIZE - len)
3553 tmp_ptr = ptr + IOSIZE - len - 1;
3554 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3555 len += (int)(tmp_ptr - ptr);
3556 *cont_s_ipos = TRUE;
3557 }
3558 IObuff[len] = NUL;
3559 ptr = IObuff;
3560 }
3561 if (len == compl_length)
3562 return NULL;
3563 }
3564 }
3565
3566 *match_len = len;
3567 return ptr;
3568}
3569
3570/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003571 * Get the next set of words matching "compl_pattern" for default completion(s)
3572 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003573 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3574 * position "st->start_pos" in the "compl_direction" direction. If
3575 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3576 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003577 * Returns OK if a new next match is found, otherwise returns FAIL.
3578 */
3579 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003580get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003581{
3582 int found_new_match = FAIL;
3583 int save_p_scs;
3584 int save_p_ws;
3585 int looped_around = FALSE;
3586 char_u *ptr;
3587 int len;
3588
3589 // If 'infercase' is set, don't use 'smartcase' here
3590 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003591 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003592 p_scs = FALSE;
3593
3594 // Buffers other than curbuf are scanned from the beginning or the
3595 // end but never from the middle, thus setting nowrapscan in this
3596 // buffer is a good idea, on the other hand, we always set
3597 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3598 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003599 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003600 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003601 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003602 p_ws = TRUE;
3603 looped_around = FALSE;
3604 for (;;)
3605 {
3606 int cont_s_ipos = FALSE;
3607
3608 ++msg_silent; // Don't want messages for wrapscan.
3609
3610 // ctrl_x_mode_line_or_eval() || word-wise search that
3611 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003612 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003613 found_new_match = search_for_exact_line(st->ins_buf,
3614 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003615 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003616 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3617 NULL, compl_direction, compl_pattern, 1L,
3618 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003619 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003620 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003621 {
3622 // set "compl_started" even on fail
3623 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003624 st->first_match_pos = *st->cur_match_pos;
3625 st->last_match_pos = *st->cur_match_pos;
3626 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003627 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003628 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3629 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630 {
3631 found_new_match = FAIL;
3632 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003633 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003634 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3635 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3636 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003637 {
3638 if (looped_around)
3639 found_new_match = FAIL;
3640 else
3641 looped_around = TRUE;
3642 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003643 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003644 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3645 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3646 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003647 {
3648 if (looped_around)
3649 found_new_match = FAIL;
3650 else
3651 looped_around = TRUE;
3652 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003653 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 if (found_new_match == FAIL)
3655 break;
3656
3657 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003658 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003659 && start_pos->lnum == st->cur_match_pos->lnum
3660 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003661 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003662
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003663 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3664 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003665 if (ptr == NULL)
3666 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003667
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003669 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003670 0, cont_s_ipos) != NOTDONE)
3671 {
3672 found_new_match = OK;
3673 break;
3674 }
3675 }
3676 p_scs = save_p_scs;
3677 p_ws = save_p_ws;
3678
3679 return found_new_match;
3680}
3681
3682/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003683 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003684 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003685 */
3686 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003687get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003688{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003689 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003690
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003691 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003692 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003693 case -1:
3694 break;
3695#ifdef FEAT_FIND_ID
3696 case CTRL_X_PATH_PATTERNS:
3697 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003698 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003699 break;
3700#endif
3701
3702 case CTRL_X_DICTIONARY:
3703 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003704 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3705 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003706 break;
3707
3708 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003709 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003710 break;
3711
3712 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003713 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003714 break;
3715
3716 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003717 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003718 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003719 break;
3720
3721#ifdef FEAT_COMPL_FUNC
3722 case CTRL_X_FUNCTION:
3723 case CTRL_X_OMNI:
3724 expand_by_function(type, compl_pattern);
3725 break;
3726#endif
3727
3728 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003729 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003730 break;
3731
3732 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003733 found_new_match = get_next_default_completion(st, ini);
3734 if (found_new_match == FAIL && st->ins_buf == curbuf)
3735 st->found_all = TRUE;
3736 }
3737
3738 // check if compl_curr_match has changed, (e.g. other type of
3739 // expansion added something)
3740 if (type != 0 && compl_curr_match != compl_old_match)
3741 found_new_match = OK;
3742
3743 return found_new_match;
3744}
3745
3746/*
3747 * Get the next expansion(s), using "compl_pattern".
3748 * The search starts at position "ini" in curbuf and in the direction
3749 * compl_direction.
3750 * When "compl_started" is FALSE start at that position, otherwise continue
3751 * where we stopped searching before.
3752 * This may return before finding all the matches.
3753 * Return the total number of matches or -1 if still unknown -- Acevedo
3754 */
3755 static int
3756ins_compl_get_exp(pos_T *ini)
3757{
3758 static ins_compl_next_state_T st;
3759 int i;
3760 int found_new_match;
3761 int type = ctrl_x_mode;
3762
3763 if (!compl_started)
3764 {
3765 FOR_ALL_BUFFERS(st.ins_buf)
3766 st.ins_buf->b_scanned = 0;
3767 st.found_all = FALSE;
3768 st.ins_buf = curbuf;
3769 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3770 ? (char_u *)"." : curbuf->b_p_cpt;
3771 st.last_match_pos = st.first_match_pos = *ini;
3772 }
3773 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3774 st.ins_buf = curbuf; // In case the buffer was wiped out.
3775
3776 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003777 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003778 ? &st.last_match_pos : &st.first_match_pos;
3779
3780 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3781 for (;;)
3782 {
3783 found_new_match = FAIL;
3784 st.set_match_pos = FALSE;
3785
3786 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3787 // or if found_all says this entry is done. For ^X^L only use the
3788 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003789 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003790 && (!compl_started || st.found_all))
3791 {
3792 int status = process_next_cpt_value(&st, &type, ini);
3793
3794 if (status == INS_COMPL_CPT_END)
3795 break;
3796 if (status == INS_COMPL_CPT_CONT)
3797 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003798 }
3799
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003800 // If complete() was called then compl_pattern has been reset. The
3801 // following won't work then, bail out.
3802 if (compl_pattern == NULL)
3803 break;
3804
3805 // get the next set of completion matches
3806 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003807
3808 // break the loop for specialized modes (use 'complete' just for the
3809 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3810 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003811 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3812 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003813 {
3814 if (got_int)
3815 break;
3816 // Fill the popup menu as soon as possible.
3817 if (type != -1)
3818 ins_compl_check_keys(0, FALSE);
3819
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003820 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003821 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3822 break;
3823 compl_started = TRUE;
3824 }
3825 else
3826 {
3827 // Mark a buffer scanned when it has been scanned completely
3828 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003829 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003830
3831 compl_started = FALSE;
3832 }
3833 }
3834 compl_started = TRUE;
3835
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003836 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003837 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003838 found_new_match = FAIL;
3839
3840 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003841 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003842 && !ctrl_x_mode_line_or_eval()))
3843 i = ins_compl_make_cyclic();
3844
3845 if (compl_old_match != NULL)
3846 {
3847 // If several matches were added (FORWARD) or the search failed and has
3848 // just been made cyclic then we have to move compl_curr_match to the
3849 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003850 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003851 : compl_old_match->cp_prev;
3852 if (compl_curr_match == NULL)
3853 compl_curr_match = compl_old_match;
3854 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003855 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003856
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003857 return i;
3858}
3859
3860/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003861 * Update "compl_shown_match" to the actually shown match, it may differ when
3862 * "compl_leader" is used to omit some of the matches.
3863 */
3864 static void
3865ins_compl_update_shown_match(void)
3866{
3867 while (!ins_compl_equal(compl_shown_match,
3868 compl_leader, (int)STRLEN(compl_leader))
3869 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003870 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003871 compl_shown_match = compl_shown_match->cp_next;
3872
3873 // If we didn't find it searching forward, and compl_shows_dir is
3874 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003875 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003876 && !ins_compl_equal(compl_shown_match,
3877 compl_leader, (int)STRLEN(compl_leader))
3878 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003879 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003880 {
3881 while (!ins_compl_equal(compl_shown_match,
3882 compl_leader, (int)STRLEN(compl_leader))
3883 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003884 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003885 compl_shown_match = compl_shown_match->cp_prev;
3886 }
3887}
3888
3889/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003890 * Delete the old text being completed.
3891 */
3892 void
3893ins_compl_delete(void)
3894{
3895 int col;
3896
3897 // In insert mode: Delete the typed part.
3898 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003899 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900 if ((int)curwin->w_cursor.col > col)
3901 {
3902 if (stop_arrow() == FAIL)
3903 return;
3904 backspace_until_column(col);
3905 }
3906
3907 // TODO: is this sufficient for redrawing? Redrawing everything causes
3908 // flicker, thus we can't do that.
3909 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003910#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003911 // clear v:completed_item
3912 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003913#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003914}
3915
3916/*
3917 * Insert the new text being completed.
3918 * "in_compl_func" is TRUE when called from complete_check().
3919 */
3920 void
3921ins_compl_insert(int in_compl_func)
3922{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003923 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003924
3925 // Make sure we don't go over the end of the string, this can happen with
3926 // illegal bytes.
3927 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3928 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003929 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003930 compl_used_match = FALSE;
3931 else
3932 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003933#ifdef FEAT_EVAL
3934 {
3935 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3936
3937 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3938 }
3939#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003940 if (!in_compl_func)
3941 compl_curr_match = compl_shown_match;
3942}
3943
3944/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003945 * show the file name for the completion match (if any). Truncate the file
3946 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003948 static void
3949ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003950{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003951 char *lead = _("match in file");
3952 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3953 char_u *s;
3954 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003955
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003956 if (space <= 0)
3957 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003958
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003959 // We need the tail that fits. With double-byte encoding going
3960 // back from the end is very slow, thus go from the start and keep
3961 // the text that fits in "space" between "s" and "e".
3962 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003963 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003964 space -= ptr2cells(e);
3965 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003966 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003967 space += ptr2cells(s);
3968 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003969 }
3970 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003971 msg_hist_off = TRUE;
3972 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3973 s > compl_shown_match->cp_fname ? "<" : "", s);
3974 msg((char *)IObuff);
3975 msg_hist_off = FALSE;
3976 redraw_cmdline = FALSE; // don't overwrite!
3977}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003978
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003979/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003980 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003981 * times. The number of matches found is returned in 'num_matches'.
3982 *
3983 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3984 * get more completions. If it is FALSE, then do nothing when there are no more
3985 * completions in the given direction.
3986 *
3987 * If "advance" is TRUE, then completion will move to the first match.
3988 * Otherwise, the original text will be shown.
3989 *
3990 * Returns OK on success and -1 if the number of matches are unknown.
3991 */
3992 static int
3993find_next_completion_match(
3994 int allow_get_expansion,
3995 int todo, // repeat completion this many times
3996 int advance,
3997 int *num_matches)
3998{
3999 int found_end = FALSE;
4000 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004001
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004002 while (--todo >= 0)
4003 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004004 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004005 {
4006 compl_shown_match = compl_shown_match->cp_next;
4007 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004008 && (is_first_match(compl_shown_match->cp_next)
4009 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004010 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004011 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004012 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004013 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004014 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004015 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004016 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004017 }
4018 else
4019 {
4020 if (!allow_get_expansion)
4021 {
4022 if (advance)
4023 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004024 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004025 compl_pending -= todo + 1;
4026 else
4027 compl_pending += todo + 1;
4028 }
4029 return -1;
4030 }
4031
4032 if (!compl_no_select && advance)
4033 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004034 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004035 --compl_pending;
4036 else
4037 ++compl_pending;
4038 }
4039
4040 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004041 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042
4043 // handle any pending completions
4044 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004045 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004046 {
4047 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4048 {
4049 compl_shown_match = compl_shown_match->cp_next;
4050 --compl_pending;
4051 }
4052 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4053 {
4054 compl_shown_match = compl_shown_match->cp_prev;
4055 ++compl_pending;
4056 }
4057 else
4058 break;
4059 }
4060 found_end = FALSE;
4061 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004062 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004063 && compl_leader != NULL
4064 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004065 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 ++todo;
4067 else
4068 // Remember a matching item.
4069 found_compl = compl_shown_match;
4070
4071 // Stop at the end of the list when we found a usable match.
4072 if (found_end)
4073 {
4074 if (found_compl != NULL)
4075 {
4076 compl_shown_match = found_compl;
4077 break;
4078 }
4079 todo = 1; // use first usable match after wrapping around
4080 }
4081 }
4082
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004083 return OK;
4084}
4085
4086/*
4087 * Fill in the next completion in the current direction.
4088 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4089 * get more completions. If it is FALSE, then we just do nothing when there
4090 * are no more completions in a given direction. The latter case is used when
4091 * we are still in the middle of finding completions, to allow browsing
4092 * through the ones found so far.
4093 * Return the total number of matches, or -1 if still unknown -- webb.
4094 *
4095 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4096 * compl_shown_match here.
4097 *
4098 * Note that this function may be called recursively once only. First with
4099 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4100 * calls this function with "allow_get_expansion" FALSE.
4101 */
4102 static int
4103ins_compl_next(
4104 int allow_get_expansion,
4105 int count, // repeat completion this many times; should
4106 // be at least 1
4107 int insert_match, // Insert the newly selected match
4108 int in_compl_func) // called from complete_check()
4109{
4110 int num_matches = -1;
4111 int todo = count;
4112 int advance;
4113 int started = compl_started;
4114
4115 // When user complete function return -1 for findstart which is next
4116 // time of 'always', compl_shown_match become NULL.
4117 if (compl_shown_match == NULL)
4118 return -1;
4119
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004120 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004121 // Update "compl_shown_match" to the actually shown match
4122 ins_compl_update_shown_match();
4123
4124 if (allow_get_expansion && insert_match
4125 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4126 // Delete old text to be replaced
4127 ins_compl_delete();
4128
4129 // When finding the longest common text we stick at the original text,
4130 // don't let CTRL-N or CTRL-P move to the first match.
4131 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4132
4133 // When restarting the search don't insert the first match either.
4134 if (compl_restarting)
4135 {
4136 advance = FALSE;
4137 compl_restarting = FALSE;
4138 }
4139
4140 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4141 // around.
4142 if (find_next_completion_match(allow_get_expansion, todo, advance,
4143 &num_matches) == -1)
4144 return -1;
4145
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004146 // Insert the text of the new completion, or the compl_leader.
4147 if (compl_no_insert && !started)
4148 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004149 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004150 compl_used_match = FALSE;
4151 }
4152 else if (insert_match)
4153 {
4154 if (!compl_get_longest || compl_used_match)
4155 ins_compl_insert(in_compl_func);
4156 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004157 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004158 }
4159 else
4160 compl_used_match = FALSE;
4161
4162 if (!allow_get_expansion)
4163 {
4164 // may undisplay the popup menu first
4165 ins_compl_upd_pum();
4166
4167 if (pum_enough_matches())
4168 // Will display the popup menu, don't redraw yet to avoid flicker.
4169 pum_call_update_screen();
4170 else
4171 // Not showing the popup menu yet, redraw to show the user what was
4172 // inserted.
4173 update_screen(0);
4174
4175 // display the updated popup menu
4176 ins_compl_show_pum();
4177#ifdef FEAT_GUI
4178 if (gui.in_use)
4179 {
4180 // Show the cursor after the match, not after the redrawn text.
4181 setcursor();
4182 out_flush_cursor(FALSE, FALSE);
4183 }
4184#endif
4185
4186 // Delete old text to be replaced, since we're still searching and
4187 // don't want to match ourselves!
4188 ins_compl_delete();
4189 }
4190
4191 // Enter will select a match when the match wasn't inserted and the popup
4192 // menu is visible.
4193 if (compl_no_insert && !started)
4194 compl_enter_selects = TRUE;
4195 else
4196 compl_enter_selects = !insert_match && compl_match_array != NULL;
4197
4198 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004199 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004200 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004201
4202 return num_matches;
4203}
4204
4205/*
4206 * Call this while finding completions, to check whether the user has hit a key
4207 * that should change the currently displayed completion, or exit completion
4208 * mode. Also, when compl_pending is not zero, show a completion as soon as
4209 * possible. -- webb
4210 * "frequency" specifies out of how many calls we actually check.
4211 * "in_compl_func" is TRUE when called from complete_check(), don't set
4212 * compl_curr_match.
4213 */
4214 void
4215ins_compl_check_keys(int frequency, int in_compl_func)
4216{
4217 static int count = 0;
4218 int c;
4219
4220 // Don't check when reading keys from a script, :normal or feedkeys().
4221 // That would break the test scripts. But do check for keys when called
4222 // from complete_check().
4223 if (!in_compl_func && (using_script() || ex_normal_busy))
4224 return;
4225
4226 // Only do this at regular intervals
4227 if (++count < frequency)
4228 return;
4229 count = 0;
4230
4231 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4232 // can't do its work correctly.
4233 c = vpeekc_any();
4234 if (c != NUL)
4235 {
4236 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4237 {
4238 c = safe_vgetc(); // Eat the character
4239 compl_shows_dir = ins_compl_key2dir(c);
4240 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4241 c != K_UP && c != K_DOWN, in_compl_func);
4242 }
4243 else
4244 {
4245 // Need to get the character to have KeyTyped set. We'll put it
4246 // back with vungetc() below. But skip K_IGNORE.
4247 c = safe_vgetc();
4248 if (c != K_IGNORE)
4249 {
4250 // Don't interrupt completion when the character wasn't typed,
4251 // e.g., when doing @q to replay keys.
4252 if (c != Ctrl_R && KeyTyped)
4253 compl_interrupted = TRUE;
4254
4255 vungetc(c);
4256 }
4257 }
4258 }
4259 if (compl_pending != 0 && !got_int && !compl_no_insert)
4260 {
4261 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4262
4263 compl_pending = 0;
4264 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4265 }
4266}
4267
4268/*
4269 * Decide the direction of Insert mode complete from the key typed.
4270 * Returns BACKWARD or FORWARD.
4271 */
4272 static int
4273ins_compl_key2dir(int c)
4274{
4275 if (c == Ctrl_P || c == Ctrl_L
4276 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4277 return BACKWARD;
4278 return FORWARD;
4279}
4280
4281/*
4282 * Return TRUE for keys that are used for completion only when the popup menu
4283 * is visible.
4284 */
4285 static int
4286ins_compl_pum_key(int c)
4287{
4288 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4289 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4290 || c == K_UP || c == K_DOWN);
4291}
4292
4293/*
4294 * Decide the number of completions to move forward.
4295 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4296 */
4297 static int
4298ins_compl_key2count(int c)
4299{
4300 int h;
4301
4302 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4303 {
4304 h = pum_get_height();
4305 if (h > 3)
4306 h -= 2; // keep some context
4307 return h;
4308 }
4309 return 1;
4310}
4311
4312/*
4313 * Return TRUE if completion with "c" should insert the match, FALSE if only
4314 * to change the currently selected completion.
4315 */
4316 static int
4317ins_compl_use_match(int c)
4318{
4319 switch (c)
4320 {
4321 case K_UP:
4322 case K_DOWN:
4323 case K_PAGEDOWN:
4324 case K_KPAGEDOWN:
4325 case K_S_DOWN:
4326 case K_PAGEUP:
4327 case K_KPAGEUP:
4328 case K_S_UP:
4329 return FALSE;
4330 }
4331 return TRUE;
4332}
4333
4334/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004335 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4336 * completion)
4337 * Sets the global variables: compl_col, compl_length and compl_pattern.
4338 * Uses the global variables: compl_cont_status and ctrl_x_mode
4339 */
4340 static int
4341get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4342{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004343 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004344 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004345 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004346 {
4347 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4348 ;
4349 compl_col += ++startcol;
4350 compl_length = curs_col - startcol;
4351 }
4352 if (p_ic)
4353 compl_pattern = str_foldcase(line + compl_col,
4354 compl_length, NULL, 0);
4355 else
4356 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4357 if (compl_pattern == NULL)
4358 return FAIL;
4359 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004360 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004361 {
4362 char_u *prefix = (char_u *)"\\<";
4363
4364 // we need up to 2 extra chars for the prefix
4365 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4366 compl_length) + 2);
4367 if (compl_pattern == NULL)
4368 return FAIL;
4369 if (!vim_iswordp(line + compl_col)
4370 || (compl_col > 0
4371 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4372 prefix = (char_u *)"";
4373 STRCPY((char *)compl_pattern, prefix);
4374 (void)quote_meta(compl_pattern + STRLEN(prefix),
4375 line + compl_col, compl_length);
4376 }
4377 else if (--startcol < 0
4378 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4379 {
4380 // Match any word of at least two chars
4381 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4382 if (compl_pattern == NULL)
4383 return FAIL;
4384 compl_col += curs_col;
4385 compl_length = 0;
4386 }
4387 else
4388 {
4389 // Search the point of change class of multibyte character
4390 // or not a word single byte character backward.
4391 if (has_mbyte)
4392 {
4393 int base_class;
4394 int head_off;
4395
4396 startcol -= (*mb_head_off)(line, line + startcol);
4397 base_class = mb_get_class(line + startcol);
4398 while (--startcol >= 0)
4399 {
4400 head_off = (*mb_head_off)(line, line + startcol);
4401 if (base_class != mb_get_class(line + startcol
4402 - head_off))
4403 break;
4404 startcol -= head_off;
4405 }
4406 }
4407 else
4408 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4409 ;
4410 compl_col += ++startcol;
4411 compl_length = (int)curs_col - startcol;
4412 if (compl_length == 1)
4413 {
4414 // Only match word with at least two chars -- webb
4415 // there's no need to call quote_meta,
4416 // alloc(7) is enough -- Acevedo
4417 compl_pattern = alloc(7);
4418 if (compl_pattern == NULL)
4419 return FAIL;
4420 STRCPY((char *)compl_pattern, "\\<");
4421 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4422 STRCAT((char *)compl_pattern, "\\k");
4423 }
4424 else
4425 {
4426 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4427 compl_length) + 2);
4428 if (compl_pattern == NULL)
4429 return FAIL;
4430 STRCPY((char *)compl_pattern, "\\<");
4431 (void)quote_meta(compl_pattern + 2, line + compl_col,
4432 compl_length);
4433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Get the pattern, column and length for whole line completion or for the
4441 * complete() function.
4442 * Sets the global variables: compl_col, compl_length and compl_pattern.
4443 */
4444 static int
4445get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4446{
4447 compl_col = (colnr_T)getwhitecols(line);
4448 compl_length = (int)curs_col - (int)compl_col;
4449 if (compl_length < 0) // cursor in indent: empty pattern
4450 compl_length = 0;
4451 if (p_ic)
4452 compl_pattern = str_foldcase(line + compl_col, compl_length,
4453 NULL, 0);
4454 else
4455 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4456 if (compl_pattern == NULL)
4457 return FAIL;
4458
4459 return OK;
4460}
4461
4462/*
4463 * Get the pattern, column and length for filename completion.
4464 * Sets the global variables: compl_col, compl_length and compl_pattern.
4465 */
4466 static int
4467get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4468{
4469 // Go back to just before the first filename character.
4470 if (startcol > 0)
4471 {
4472 char_u *p = line + startcol;
4473
4474 MB_PTR_BACK(line, p);
4475 while (p > line && vim_isfilec(PTR2CHAR(p)))
4476 MB_PTR_BACK(line, p);
4477 if (p == line && vim_isfilec(PTR2CHAR(p)))
4478 startcol = 0;
4479 else
4480 startcol = (int)(p - line) + 1;
4481 }
4482
4483 compl_col += startcol;
4484 compl_length = (int)curs_col - startcol;
4485 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4486 if (compl_pattern == NULL)
4487 return FAIL;
4488
4489 return OK;
4490}
4491
4492/*
4493 * Get the pattern, column and length for command-line completion.
4494 * Sets the global variables: compl_col, compl_length and compl_pattern.
4495 */
4496 static int
4497get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4498{
4499 compl_pattern = vim_strnsave(line, curs_col);
4500 if (compl_pattern == NULL)
4501 return FAIL;
4502 set_cmd_context(&compl_xp, compl_pattern,
4503 (int)STRLEN(compl_pattern), curs_col, FALSE);
4504 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4505 || compl_xp.xp_context == EXPAND_NOTHING)
4506 // No completion possible, use an empty pattern to get a
4507 // "pattern not found" message.
4508 compl_col = curs_col;
4509 else
4510 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4511 compl_length = curs_col - compl_col;
4512
4513 return OK;
4514}
4515
4516/*
4517 * Get the pattern, column and length for user defined completion ('omnifunc',
4518 * 'completefunc' and 'thesaurusfunc')
4519 * Sets the global variables: compl_col, compl_length and compl_pattern.
4520 * Uses the global variable: spell_bad_len
4521 */
4522 static int
4523get_userdefined_compl_info(colnr_T curs_col UNUSED)
4524{
4525 int ret = FAIL;
4526
4527#ifdef FEAT_COMPL_FUNC
4528 // Call user defined function 'completefunc' with "a:findstart"
4529 // set to 1 to obtain the length of text to use for completion.
4530 char_u *line;
4531 typval_T args[3];
4532 int col;
4533 char_u *funcname;
4534 pos_T pos;
4535 int save_State = State;
4536 callback_T *cb;
4537
4538 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4539 // length as a string
4540 funcname = get_complete_funcname(ctrl_x_mode);
4541 if (*funcname == NUL)
4542 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004543 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004544 ? "completefunc" : "omnifunc");
4545 return FAIL;
4546 }
4547
4548 args[0].v_type = VAR_NUMBER;
4549 args[0].vval.v_number = 1;
4550 args[1].v_type = VAR_STRING;
4551 args[1].vval.v_string = (char_u *)"";
4552 args[2].v_type = VAR_UNKNOWN;
4553 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004554 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004555 cb = get_insert_callback(ctrl_x_mode);
4556 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004557 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004558
4559 State = save_State;
4560 curwin->w_cursor = pos; // restore the cursor position
4561 validate_cursor();
4562 if (!EQUAL_POS(curwin->w_cursor, pos))
4563 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004564 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004565 return FAIL;
4566 }
4567
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004568 // Return value -2 means the user complete function wants to cancel the
4569 // complete without an error, do the same if the function did not execute
4570 // successfully.
4571 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004572 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004573 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004574 if (col == -3)
4575 {
4576 ctrl_x_mode = CTRL_X_NORMAL;
4577 edit_submode = NULL;
4578 if (!shortmess(SHM_COMPLETIONMENU))
4579 msg_clr_cmdline();
4580 return FAIL;
4581 }
4582
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004583 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004584 // completion.
4585 compl_opt_refresh_always = FALSE;
4586 compl_opt_suppress_empty = FALSE;
4587
4588 if (col < 0)
4589 col = curs_col;
4590 compl_col = col;
4591 if (compl_col > curs_col)
4592 compl_col = curs_col;
4593
4594 // Setup variables for completion. Need to obtain "line" again,
4595 // it may have become invalid.
4596 line = ml_get(curwin->w_cursor.lnum);
4597 compl_length = curs_col - compl_col;
4598 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4599 if (compl_pattern == NULL)
4600 return FAIL;
4601
4602 ret = OK;
4603#endif
4604
4605 return ret;
4606}
4607
4608/*
4609 * Get the pattern, column and length for spell completion.
4610 * Sets the global variables: compl_col, compl_length and compl_pattern.
4611 * Uses the global variable: spell_bad_len
4612 */
4613 static int
4614get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4615{
4616 int ret = FAIL;
4617#ifdef FEAT_SPELL
4618 char_u *line;
4619
4620 if (spell_bad_len > 0)
4621 compl_col = curs_col - spell_bad_len;
4622 else
4623 compl_col = spell_word_start(startcol);
4624 if (compl_col >= (colnr_T)startcol)
4625 {
4626 compl_length = 0;
4627 compl_col = curs_col;
4628 }
4629 else
4630 {
4631 spell_expand_check_cap(compl_col);
4632 compl_length = (int)curs_col - compl_col;
4633 }
4634 // Need to obtain "line" again, it may have become invalid.
4635 line = ml_get(curwin->w_cursor.lnum);
4636 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4637 if (compl_pattern == NULL)
4638 return FAIL;
4639
4640 ret = OK;
4641#endif
4642
4643 return ret;
4644}
4645
4646/*
4647 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004648 * "startcol" - start column number of the completion pattern/text
4649 * "cur_col" - current cursor column
4650 * On return, "line_invalid" is set to TRUE, if the current line may have
4651 * become invalid and needs to be fetched again.
4652 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004653 */
4654 static int
4655compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4656{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004657 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004658 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4659 && !thesaurus_func_complete(ctrl_x_mode)))
4660 {
4661 return get_normal_compl_info(line, startcol, curs_col);
4662 }
4663 else if (ctrl_x_mode_line_or_eval())
4664 {
4665 return get_wholeline_compl_info(line, curs_col);
4666 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004667 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004668 {
4669 return get_filename_compl_info(line, startcol, curs_col);
4670 }
4671 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4672 {
4673 return get_cmdline_compl_info(line, curs_col);
4674 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004675 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004676 || thesaurus_func_complete(ctrl_x_mode))
4677 {
4678 if (get_userdefined_compl_info(curs_col) == FAIL)
4679 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004680 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004681 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004682 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004683 {
4684 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4685 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004686 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004687 }
4688 else
4689 {
4690 internal_error("ins_complete()");
4691 return FAIL;
4692 }
4693
4694 return OK;
4695}
4696
4697/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004698 * Continue an interrupted completion mode search in "line".
4699 *
4700 * If this same ctrl_x_mode has been interrupted use the text from
4701 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4702 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4703 * the same line as the cursor then fix it (the line has been split because it
4704 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4705 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004706 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004707 static void
4708ins_compl_continue_search(char_u *line)
4709{
4710 // it is a continued search
4711 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004712 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4713 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004714 {
4715 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4716 {
4717 // line (probably) wrapped, set compl_startpos to the
4718 // first non_blank in the line, if it is not a wordchar
4719 // include it to get a better pattern, but then we don't
4720 // want the "\\<" prefix, check it below
4721 compl_col = (colnr_T)getwhitecols(line);
4722 compl_startpos.col = compl_col;
4723 compl_startpos.lnum = curwin->w_cursor.lnum;
4724 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4725 }
4726 else
4727 {
4728 // S_IPOS was set when we inserted a word that was at the
4729 // beginning of the line, which means that we'll go to SOL
4730 // mode but first we need to redefine compl_startpos
4731 if (compl_cont_status & CONT_S_IPOS)
4732 {
4733 compl_cont_status |= CONT_SOL;
4734 compl_startpos.col = (colnr_T)(skipwhite(
4735 line + compl_length
4736 + compl_startpos.col) - line);
4737 }
4738 compl_col = compl_startpos.col;
4739 }
4740 compl_length = curwin->w_cursor.col - (int)compl_col;
4741 // IObuff is used to add a "word from the next line" would we
4742 // have enough space? just being paranoid
4743#define MIN_SPACE 75
4744 if (compl_length > (IOSIZE - MIN_SPACE))
4745 {
4746 compl_cont_status &= ~CONT_SOL;
4747 compl_length = (IOSIZE - MIN_SPACE);
4748 compl_col = curwin->w_cursor.col - compl_length;
4749 }
4750 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4751 if (compl_length < 1)
4752 compl_cont_status &= CONT_LOCAL;
4753 }
4754 else if (ctrl_x_mode_line_or_eval())
4755 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4756 else
4757 compl_cont_status = 0;
4758}
4759
4760/*
4761 * start insert mode completion
4762 */
4763 static int
4764ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004765{
4766 char_u *line;
4767 int startcol = 0; // column where searched text starts
4768 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004769 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004770 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004771 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004772
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004773 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004774
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004775 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004776 did_si = FALSE;
4777 can_si = FALSE;
4778 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004779 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004780 return FAIL;
4781
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004782 line = ml_get(curwin->w_cursor.lnum);
4783 curs_col = curwin->w_cursor.col;
4784 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004785
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004786 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4787 && compl_cont_mode == ctrl_x_mode)
4788 // this same ctrl-x_mode was interrupted previously. Continue the
4789 // completion.
4790 ins_compl_continue_search(line);
4791 else
4792 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004793
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004794 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004795 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004796 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004797 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004798 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4799 compl_cont_status = 0;
4800 compl_cont_status |= CONT_N_ADDS;
4801 compl_startpos = curwin->w_cursor;
4802 startcol = (int)curs_col;
4803 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004804 }
4805
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004806 // Work out completion pattern and original text -- webb
4807 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4808 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004809 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4810 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004811 // restore did_ai, so that adding comment leader works
4812 did_ai = save_did_ai;
4813 return FAIL;
4814 }
4815 // If "line" was changed while getting completion info get it again.
4816 if (line_invalid)
4817 line = ml_get(curwin->w_cursor.lnum);
4818
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004819 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004820 {
4821 edit_submode_pre = (char_u *)_(" Adding");
4822 if (ctrl_x_mode_line_or_eval())
4823 {
4824 // Insert a new line, keep indentation but ignore 'comments'.
4825 char_u *old = curbuf->b_p_com;
4826
4827 curbuf->b_p_com = (char_u *)"";
4828 compl_startpos.lnum = curwin->w_cursor.lnum;
4829 compl_startpos.col = compl_col;
4830 ins_eol('\r');
4831 curbuf->b_p_com = old;
4832 compl_length = 0;
4833 compl_col = curwin->w_cursor.col;
4834 }
4835 }
4836 else
4837 {
4838 edit_submode_pre = NULL;
4839 compl_startpos.col = compl_col;
4840 }
4841
4842 if (compl_cont_status & CONT_LOCAL)
4843 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4844 else
4845 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4846
4847 // If any of the original typed text has been changed we need to fix
4848 // the redo buffer.
4849 ins_compl_fixRedoBufForLeader(NULL);
4850
4851 // Always add completion for the original text.
4852 vim_free(compl_orig_text);
4853 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4854 if (p_ic)
4855 flags |= CP_ICASE;
4856 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4857 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4858 {
4859 VIM_CLEAR(compl_pattern);
4860 VIM_CLEAR(compl_orig_text);
4861 return FAIL;
4862 }
4863
4864 // showmode might reset the internal line pointers, so it must
4865 // be called before line = ml_get(), or when this address is no
4866 // longer needed. -- Acevedo.
4867 edit_submode_extra = (char_u *)_("-- Searching...");
4868 edit_submode_highl = HLF_COUNT;
4869 showmode();
4870 edit_submode_extra = NULL;
4871 out_flush();
4872
4873 return OK;
4874}
4875
4876/*
4877 * display the completion status message
4878 */
4879 static void
4880ins_compl_show_statusmsg(void)
4881{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004882 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004883 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004884 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004885 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004886 ? (char_u *)_(e_hitend)
4887 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004888 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004889 }
4890
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004891 if (edit_submode_extra == NULL)
4892 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004893 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004894 {
4895 edit_submode_extra = (char_u *)_("Back at original");
4896 edit_submode_highl = HLF_W;
4897 }
4898 else if (compl_cont_status & CONT_S_IPOS)
4899 {
4900 edit_submode_extra = (char_u *)_("Word from other line");
4901 edit_submode_highl = HLF_COUNT;
4902 }
4903 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4904 {
4905 edit_submode_extra = (char_u *)_("The only match");
4906 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004907 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004908 }
4909 else
4910 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004911#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004912 // Update completion sequence number when needed.
4913 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004914 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004915#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004916 // The match should always have a sequence number now, this is
4917 // just a safety check.
4918 if (compl_curr_match->cp_number != -1)
4919 {
4920 // Space for 10 text chars. + 2x10-digit no.s = 31.
4921 // Translations may need more than twice that.
4922 static char_u match_ref[81];
4923
4924 if (compl_matches > 0)
4925 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004926 _("match %d of %d"),
4927 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004928 else
4929 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004930 _("match %d"),
4931 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004932 edit_submode_extra = match_ref;
4933 edit_submode_highl = HLF_R;
4934 if (dollar_vcol >= 0)
4935 curs_columns(FALSE);
4936 }
4937 }
4938 }
4939
4940 // Show a message about what (completion) mode we're in.
4941 if (!compl_opt_suppress_empty)
4942 {
4943 showmode();
4944 if (!shortmess(SHM_COMPLETIONMENU))
4945 {
4946 if (edit_submode_extra != NULL)
4947 {
4948 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004949 {
4950 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004951 msg_attr((char *)edit_submode_extra,
4952 edit_submode_highl < HLF_COUNT
4953 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004954 msg_hist_off = FALSE;
4955 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004956 }
4957 else
4958 msg_clr_cmdline(); // necessary for "noshowmode"
4959 }
4960 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004961}
4962
4963/*
4964 * Do Insert mode completion.
4965 * Called when character "c" was typed, which has a meaning for completion.
4966 * Returns OK if completion was done, FAIL if something failed (out of mem).
4967 */
4968 int
4969ins_complete(int c, int enable_pum)
4970{
4971 int n;
4972 int save_w_wrow;
4973 int save_w_leftcol;
4974 int insert_match;
4975
4976 compl_direction = ins_compl_key2dir(c);
4977 insert_match = ins_compl_use_match(c);
4978
4979 if (!compl_started)
4980 {
4981 if (ins_compl_start() == FAIL)
4982 return FAIL;
4983 }
4984 else if (insert_match && stop_arrow() == FAIL)
4985 return FAIL;
4986
4987 compl_shown_match = compl_curr_match;
4988 compl_shows_dir = compl_direction;
4989
4990 // Find next match (and following matches).
4991 save_w_wrow = curwin->w_wrow;
4992 save_w_leftcol = curwin->w_leftcol;
4993 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4994
4995 // may undisplay the popup menu
4996 ins_compl_upd_pum();
4997
4998 if (n > 1) // all matches have been found
4999 compl_matches = n;
5000 compl_curr_match = compl_shown_match;
5001 compl_direction = compl_shows_dir;
5002
5003 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5004 // mode.
5005 if (got_int && !global_busy)
5006 {
5007 (void)vgetc();
5008 got_int = FALSE;
5009 }
5010
5011 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005012 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005013 {
5014 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5015 // because we couldn't expand anything at first place, but if we used
5016 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5017 // (such as M in M'exico) if not tried already. -- Acevedo
5018 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005019 || compl_status_adding()
5020 || (ctrl_x_mode_not_default()
5021 && !ctrl_x_mode_path_patterns()
5022 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005023 compl_cont_status &= ~CONT_N_ADDS;
5024 }
5025
5026 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5027 compl_cont_status |= CONT_S_IPOS;
5028 else
5029 compl_cont_status &= ~CONT_S_IPOS;
5030
5031 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005032
5033 // Show the popup menu, unless we got interrupted.
5034 if (enable_pum && !compl_interrupted)
5035 show_pum(save_w_wrow, save_w_leftcol);
5036
5037 compl_was_interrupted = compl_interrupted;
5038 compl_interrupted = FALSE;
5039
5040 return OK;
5041}
5042
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005043/*
5044 * Remove (if needed) and show the popup menu
5045 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005046 static void
5047show_pum(int prev_w_wrow, int prev_w_leftcol)
5048{
5049 // RedrawingDisabled may be set when invoked through complete().
5050 int n = RedrawingDisabled;
5051
5052 RedrawingDisabled = 0;
5053
5054 // If the cursor moved or the display scrolled we need to remove the pum
5055 // first.
5056 setcursor();
5057 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5058 ins_compl_del_pum();
5059
5060 ins_compl_show_pum();
5061 setcursor();
5062 RedrawingDisabled = n;
5063}
5064
5065/*
5066 * Looks in the first "len" chars. of "src" for search-metachars.
5067 * If dest is not NULL the chars. are copied there quoting (with
5068 * a backslash) the metachars, and dest would be NUL terminated.
5069 * Returns the length (needed) of dest
5070 */
5071 static unsigned
5072quote_meta(char_u *dest, char_u *src, int len)
5073{
5074 unsigned m = (unsigned)len + 1; // one extra for the NUL
5075
5076 for ( ; --len >= 0; src++)
5077 {
5078 switch (*src)
5079 {
5080 case '.':
5081 case '*':
5082 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005083 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005084 break;
5085 // FALLTHROUGH
5086 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005087 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005088 break;
5089 // FALLTHROUGH
5090 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005091 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005092 break;
5093 // FALLTHROUGH
5094 case '^': // currently it's not needed.
5095 case '$':
5096 m++;
5097 if (dest != NULL)
5098 *dest++ = '\\';
5099 break;
5100 }
5101 if (dest != NULL)
5102 *dest++ = *src;
5103 // Copy remaining bytes of a multibyte character.
5104 if (has_mbyte)
5105 {
5106 int i, mb_len;
5107
5108 mb_len = (*mb_ptr2len)(src) - 1;
5109 if (mb_len > 0 && len >= mb_len)
5110 for (i = 0; i < mb_len; ++i)
5111 {
5112 --len;
5113 ++src;
5114 if (dest != NULL)
5115 *dest++ = *src;
5116 }
5117 }
5118 }
5119 if (dest != NULL)
5120 *dest = NUL;
5121
5122 return m;
5123}
5124
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005125#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005126 void
5127free_insexpand_stuff(void)
5128{
5129 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005130# ifdef FEAT_EVAL
5131 free_callback(&cfu_cb);
5132 free_callback(&ofu_cb);
5133 free_callback(&tsrfu_cb);
5134# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005135}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005136#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005137
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005138#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005139/*
5140 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5141 * spelled word, if there is one.
5142 */
5143 static void
5144spell_back_to_badword(void)
5145{
5146 pos_T tpos = curwin->w_cursor;
5147
5148 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5149 if (curwin->w_cursor.col != tpos.col)
5150 start_arrow(&tpos);
5151}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005152#endif