blob: 8fcce7f40fa2f1bdafe0fc719af116ab84747e79 [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
260 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.
527 */
528 static char_u *
529ins_compl_infercase_gettext(
530 char_u *str,
531 int actual_len,
532 int actual_compl_length,
533 int min_len)
534{
535 int *wca; // Wide character array.
536 char_u *p;
537 int i, c;
538 int has_lower = FALSE;
539 int was_letter = FALSE;
540
541 IObuff[0] = NUL;
542
543 // Allocate wide character array for the completion and fill it.
544 wca = ALLOC_MULT(int, actual_len);
545 if (wca == NULL)
546 return IObuff;
547
548 p = str;
549 for (i = 0; i < actual_len; ++i)
550 if (has_mbyte)
551 wca[i] = mb_ptr2char_adv(&p);
552 else
553 wca[i] = *(p++);
554
555 // Rule 1: Were any chars converted to lower?
556 p = compl_orig_text;
557 for (i = 0; i < min_len; ++i)
558 {
559 if (has_mbyte)
560 c = mb_ptr2char_adv(&p);
561 else
562 c = *(p++);
563 if (MB_ISLOWER(c))
564 {
565 has_lower = TRUE;
566 if (MB_ISUPPER(wca[i]))
567 {
568 // Rule 1 is satisfied.
569 for (i = actual_compl_length; i < actual_len; ++i)
570 wca[i] = MB_TOLOWER(wca[i]);
571 break;
572 }
573 }
574 }
575
576 // Rule 2: No lower case, 2nd consecutive letter converted to
577 // upper case.
578 if (!has_lower)
579 {
580 p = compl_orig_text;
581 for (i = 0; i < min_len; ++i)
582 {
583 if (has_mbyte)
584 c = mb_ptr2char_adv(&p);
585 else
586 c = *(p++);
587 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
588 {
589 // Rule 2 is satisfied.
590 for (i = actual_compl_length; i < actual_len; ++i)
591 wca[i] = MB_TOUPPER(wca[i]);
592 break;
593 }
594 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
595 }
596 }
597
598 // Copy the original case of the part we typed.
599 p = compl_orig_text;
600 for (i = 0; i < min_len; ++i)
601 {
602 if (has_mbyte)
603 c = mb_ptr2char_adv(&p);
604 else
605 c = *(p++);
606 if (MB_ISLOWER(c))
607 wca[i] = MB_TOLOWER(wca[i]);
608 else if (MB_ISUPPER(c))
609 wca[i] = MB_TOUPPER(wca[i]);
610 }
611
612 // Generate encoding specific output from wide character array.
613 // Multi-byte characters can occupy up to five bytes more than
614 // ASCII characters, and we also need one byte for NUL, so stay
615 // six bytes away from the edge of IObuff.
616 p = IObuff;
617 i = 0;
618 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
619 if (has_mbyte)
620 p += (*mb_char2bytes)(wca[i++], p);
621 else
622 *(p++) = wca[i++];
623 *p = NUL;
624
625 vim_free(wca);
626
627 return IObuff;
628}
629
630/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
632 * case of the originally typed text is used, and the case of the completed
633 * text is inferred, ie this tries to work out what case you probably wanted
634 * the rest of the word to be in -- webb
635 */
636 int
637ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200638 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100639 int len,
640 int icase,
641 char_u *fname,
642 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200643 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100644{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200645 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100646 char_u *p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100647 int actual_len; // Take multi-byte characters
648 int actual_compl_length; // into account.
649 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200650 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100651
652 if (p_ic && curbuf->b_p_inf && len > 0)
653 {
654 // Infer case of completed part.
655
656 // Find actual length of completion.
657 if (has_mbyte)
658 {
659 p = str;
660 actual_len = 0;
661 while (*p != NUL)
662 {
663 MB_PTR_ADV(p);
664 ++actual_len;
665 }
666 }
667 else
668 actual_len = len;
669
670 // Find actual length of original text.
671 if (has_mbyte)
672 {
673 p = compl_orig_text;
674 actual_compl_length = 0;
675 while (*p != NUL)
676 {
677 MB_PTR_ADV(p);
678 ++actual_compl_length;
679 }
680 }
681 else
682 actual_compl_length = compl_length;
683
684 // "actual_len" may be smaller than "actual_compl_length" when using
685 // thesaurus, only use the minimum when comparing.
686 min_len = actual_len < actual_compl_length
687 ? actual_len : actual_compl_length;
688
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000689 str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
690 min_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200692 if (cont_s_ipos)
693 flags |= CP_CONT_S_IPOS;
694 if (icase)
695 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696
Bram Moolenaar08928322020-01-04 14:32:48 +0100697 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698}
699
700/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000701 * Add a match to the list of matches. The arguments are:
702 * str - text of the match to add
703 * len - length of "str". If -1, then the length of "str" is
704 * computed.
705 * fname - file name to associate with this match.
706 * cptext - list of strings to use with this match (for abbr, menu, info
707 * and kind)
708 * user_data - user supplied data (any vim type) for this match
709 * cdir - match direction. If 0, use "compl_direction".
710 * flags_arg - match flags (cp_flags)
711 * adup - accept this match even if it is already present.
712 * If "cdir" is FORWARD, then the match is added after the current match.
713 * Otherwise, it is added before the current match.
714 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 * If the given string is already in the list of completions, then return
716 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
717 * maybe because alloc() returns NULL, then FAIL is returned.
718 */
719 static int
720ins_compl_add(
721 char_u *str,
722 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 char_u **cptext, // extra text for popup menu or NULL
725 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100726 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200727 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100728 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729{
730 compl_T *match;
731 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200732 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733
Bram Moolenaarceb06192021-04-04 15:05:22 +0200734 if (flags & CP_FAST)
735 fast_breakcheck();
736 else
737 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 if (got_int)
739 return FAIL;
740 if (len < 0)
741 len = (int)STRLEN(str);
742
743 // If the same match is already present, don't add it.
744 if (compl_first_match != NULL && !adup)
745 {
746 match = compl_first_match;
747 do
748 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000749 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100750 && STRNCMP(match->cp_str, str, len) == 0
751 && match->cp_str[len] == NUL)
752 return NOTDONE;
753 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000754 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100755 }
756
757 // Remove any popup menu before changing the list of matches.
758 ins_compl_del_pum();
759
760 // Allocate a new match structure.
761 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200762 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100763 if (match == NULL)
764 return FAIL;
765 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200766 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100767 match->cp_number = 0;
768 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
769 {
770 vim_free(match);
771 return FAIL;
772 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100773
774 // match-fname is:
775 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200776 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 // - NULL otherwise. --Acevedo
778 if (fname != NULL
779 && compl_curr_match != NULL
780 && compl_curr_match->cp_fname != NULL
781 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
782 match->cp_fname = compl_curr_match->cp_fname;
783 else if (fname != NULL)
784 {
785 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200786 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100787 }
788 else
789 match->cp_fname = NULL;
790 match->cp_flags = flags;
791
792 if (cptext != NULL)
793 {
794 int i;
795
796 for (i = 0; i < CPT_COUNT; ++i)
797 if (cptext[i] != NULL && *cptext[i] != NUL)
798 match->cp_text[i] = vim_strsave(cptext[i]);
799 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100800#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100801 if (user_data != NULL)
802 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100803#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000805 // Link the new match structure after (FORWARD) or before (BACKWARD) the
806 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 if (compl_first_match == NULL)
808 match->cp_next = match->cp_prev = NULL;
809 else if (dir == FORWARD)
810 {
811 match->cp_next = compl_curr_match->cp_next;
812 match->cp_prev = compl_curr_match;
813 }
814 else // BACKWARD
815 {
816 match->cp_next = compl_curr_match;
817 match->cp_prev = compl_curr_match->cp_prev;
818 }
819 if (match->cp_next)
820 match->cp_next->cp_prev = match;
821 if (match->cp_prev)
822 match->cp_prev->cp_next = match;
823 else // if there's nothing before, it is the first match
824 compl_first_match = match;
825 compl_curr_match = match;
826
827 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200828 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 ins_compl_longest_match(match);
830
831 return OK;
832}
833
834/*
835 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200836 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100837 */
838 static int
839ins_compl_equal(compl_T *match, char_u *str, int len)
840{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200841 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200842 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200843 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100844 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
845 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
846}
847
848/*
849 * Reduce the longest common string for match "match".
850 */
851 static void
852ins_compl_longest_match(compl_T *match)
853{
854 char_u *p, *s;
855 int c1, c2;
856 int had_match;
857
858 if (compl_leader == NULL)
859 {
860 // First match, use it as a whole.
861 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000862 if (compl_leader == NULL)
863 return;
864
865 had_match = (curwin->w_cursor.col > compl_col);
866 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000867 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000868 ins_redraw(FALSE);
869
870 // When the match isn't there (to avoid matching itself) remove it
871 // again after redrawing.
872 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100873 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100874 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000875
876 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100877 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000878
879 // Reduce the text if this match differs from compl_leader.
880 p = compl_leader;
881 s = match->cp_str;
882 while (*p != NUL)
883 {
884 if (has_mbyte)
885 {
886 c1 = mb_ptr2char(p);
887 c2 = mb_ptr2char(s);
888 }
889 else
890 {
891 c1 = *p;
892 c2 = *s;
893 }
894 if ((match->cp_flags & CP_ICASE)
895 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
896 break;
897 if (has_mbyte)
898 {
899 MB_PTR_ADV(p);
900 MB_PTR_ADV(s);
901 }
902 else
903 {
904 ++p;
905 ++s;
906 }
907 }
908
909 if (*p != NUL)
910 {
911 // Leader was shortened, need to change the inserted text.
912 *p = NUL;
913 had_match = (curwin->w_cursor.col > compl_col);
914 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000915 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000916 ins_redraw(FALSE);
917
918 // When the match isn't there (to avoid matching itself) remove it
919 // again after redrawing.
920 if (!had_match)
921 ins_compl_delete();
922 }
923
924 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925}
926
927/*
928 * Add an array of matches to the list of matches.
929 * Frees matches[].
930 */
931 static void
932ins_compl_add_matches(
933 int num_matches,
934 char_u **matches,
935 int icase)
936{
937 int i;
938 int add_r = OK;
939 int dir = compl_direction;
940
941 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100942 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200943 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100944 // if dir was BACKWARD then honor it just once
945 dir = FORWARD;
946 FreeWild(num_matches, matches);
947}
948
949/*
950 * Make the completion list cyclic.
951 * Return the number of matches (excluding the original).
952 */
953 static int
954ins_compl_make_cyclic(void)
955{
956 compl_T *match;
957 int count = 0;
958
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000959 if (compl_first_match == NULL)
960 return 0;
961
962 // Find the end of the list.
963 match = compl_first_match;
964 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000965 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100966 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000967 match = match->cp_next;
968 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000970 match->cp_next = compl_first_match;
971 compl_first_match->cp_prev = match;
972
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100973 return count;
974}
975
976/*
977 * Return whether there currently is a shown match.
978 */
979 int
980ins_compl_has_shown_match(void)
981{
982 return compl_shown_match == NULL
983 || compl_shown_match != compl_shown_match->cp_next;
984}
985
986/*
987 * Return whether the shown match is long enough.
988 */
989 int
990ins_compl_long_shown_match(void)
991{
992 return (int)STRLEN(compl_shown_match->cp_str)
993 > curwin->w_cursor.col - compl_col;
994}
995
996/*
997 * Set variables that store noselect and noinsert behavior from the
998 * 'completeopt' value.
999 */
1000 void
1001completeopt_was_set(void)
1002{
1003 compl_no_insert = FALSE;
1004 compl_no_select = FALSE;
1005 if (strstr((char *)p_cot, "noselect") != NULL)
1006 compl_no_select = TRUE;
1007 if (strstr((char *)p_cot, "noinsert") != NULL)
1008 compl_no_insert = TRUE;
1009}
1010
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001011
1012// "compl_match_array" points the currently displayed list of entries in the
1013// popup menu. It is NULL when there is no popup menu.
1014static pumitem_T *compl_match_array = NULL;
1015static int compl_match_arraysize;
1016
1017/*
1018 * Update the screen and when there is any scrolling remove the popup menu.
1019 */
1020 static void
1021ins_compl_upd_pum(void)
1022{
1023 int h;
1024
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001025 if (compl_match_array == NULL)
1026 return;
1027
1028 h = curwin->w_cline_height;
1029 // Update the screen later, before drawing the popup menu over it.
1030 pum_call_update_screen();
1031 if (h != curwin->w_cline_height)
1032 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001033}
1034
1035/*
1036 * Remove any popup menu.
1037 */
1038 static void
1039ins_compl_del_pum(void)
1040{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001041 if (compl_match_array == NULL)
1042 return;
1043
1044 pum_undisplay();
1045 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001046}
1047
1048/*
1049 * Return TRUE if the popup menu should be displayed.
1050 */
1051 int
1052pum_wanted(void)
1053{
1054 // 'completeopt' must contain "menu" or "menuone"
1055 if (vim_strchr(p_cot, 'm') == NULL)
1056 return FALSE;
1057
1058 // The display looks bad on a B&W display.
1059 if (t_colors < 8
1060#ifdef FEAT_GUI
1061 && !gui.in_use
1062#endif
1063 )
1064 return FALSE;
1065 return TRUE;
1066}
1067
1068/*
1069 * Return TRUE if there are two or more matches to be shown in the popup menu.
1070 * One if 'completopt' contains "menuone".
1071 */
1072 static int
1073pum_enough_matches(void)
1074{
1075 compl_T *compl;
1076 int i;
1077
1078 // Don't display the popup menu if there are no matches or there is only
1079 // one (ignoring the original text).
1080 compl = compl_first_match;
1081 i = 0;
1082 do
1083 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001084 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001085 break;
1086 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001087 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088
1089 if (strstr((char *)p_cot, "menuone") != NULL)
1090 return (i >= 1);
1091 return (i >= 2);
1092}
1093
Bram Moolenaar3075a452021-11-17 15:51:52 +00001094#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001095/*
1096 * Allocate Dict for the completed item.
1097 * { word, abbr, menu, kind, info }
1098 */
1099 static dict_T *
1100ins_compl_dict_alloc(compl_T *match)
1101{
1102 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1103
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001104 if (dict == NULL)
1105 return NULL;
1106
1107 dict_add_string(dict, "word", match->cp_str);
1108 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1109 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1110 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1111 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1112 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1113 dict_add_string(dict, "user_data", (char_u *)"");
1114 else
1115 dict_add_tv(dict, "user_data", &match->cp_user_data);
1116
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001117 return dict;
1118}
1119
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001120/*
1121 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1122 * completion menu is changed.
1123 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001124 static void
1125trigger_complete_changed_event(int cur)
1126{
1127 dict_T *v_event;
1128 dict_T *item;
1129 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001130 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001131
1132 if (recursive)
1133 return;
1134
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001135 if (cur < 0)
1136 item = dict_alloc();
1137 else
1138 item = ins_compl_dict_alloc(compl_curr_match);
1139 if (item == NULL)
1140 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001141 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001142 dict_add_dict(v_event, "completed_item", item);
1143 pum_set_event_info(v_event);
1144 dict_set_items_ro(v_event);
1145
1146 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001147 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001148 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001149 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001150 recursive = FALSE;
1151
Bram Moolenaar3075a452021-11-17 15:51:52 +00001152 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001153}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001154#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001155
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001157 * Build a popup menu to show the completion matches.
1158 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1159 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001160 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001161 static int
1162ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001163{
1164 compl_T *compl;
1165 compl_T *shown_compl = NULL;
1166 int did_find_shown_match = FALSE;
1167 int shown_match_ok = FALSE;
1168 int i;
1169 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001170 int lead_len = 0;
1171
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001172 // Need to build the popup menu list.
1173 compl_match_arraysize = 0;
1174 compl = compl_first_match;
1175 if (compl_leader != NULL)
1176 lead_len = (int)STRLEN(compl_leader);
1177
1178 do
1179 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001180 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001181 && (compl_leader == NULL
1182 || ins_compl_equal(compl, compl_leader, lead_len)))
1183 ++compl_match_arraysize;
1184 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001185 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001186
1187 if (compl_match_arraysize == 0)
1188 return -1;
1189
1190 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1191 if (compl_match_array == NULL)
1192 return -1;
1193
1194 // If the current match is the original text don't find the first
1195 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001196 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001197 shown_match_ok = TRUE;
1198
1199 i = 0;
1200 compl = compl_first_match;
1201 do
1202 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001203 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001204 && (compl_leader == NULL
1205 || ins_compl_equal(compl, compl_leader, lead_len)))
1206 {
1207 if (!shown_match_ok)
1208 {
1209 if (compl == compl_shown_match || did_find_shown_match)
1210 {
1211 // This item is the shown match or this is the
1212 // first displayed item after the shown match.
1213 compl_shown_match = compl;
1214 did_find_shown_match = TRUE;
1215 shown_match_ok = TRUE;
1216 }
1217 else
1218 // Remember this displayed match for when the
1219 // shown match is just below it.
1220 shown_compl = compl;
1221 cur = i;
1222 }
1223
1224 if (compl->cp_text[CPT_ABBR] != NULL)
1225 compl_match_array[i].pum_text =
1226 compl->cp_text[CPT_ABBR];
1227 else
1228 compl_match_array[i].pum_text = compl->cp_str;
1229 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1230 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1231 if (compl->cp_text[CPT_MENU] != NULL)
1232 compl_match_array[i++].pum_extra =
1233 compl->cp_text[CPT_MENU];
1234 else
1235 compl_match_array[i++].pum_extra = compl->cp_fname;
1236 }
1237
1238 if (compl == compl_shown_match)
1239 {
1240 did_find_shown_match = TRUE;
1241
1242 // When the original text is the shown match don't set
1243 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001244 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001245 shown_match_ok = TRUE;
1246
1247 if (!shown_match_ok && shown_compl != NULL)
1248 {
1249 // The shown match isn't displayed, set it to the
1250 // previously displayed match.
1251 compl_shown_match = shown_compl;
1252 shown_match_ok = TRUE;
1253 }
1254 }
1255 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001256 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001257
1258 if (!shown_match_ok) // no displayed match at all
1259 cur = -1;
1260
1261 return cur;
1262}
1263
1264/*
1265 * Show the popup menu for the list of matches.
1266 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1267 */
1268 void
1269ins_compl_show_pum(void)
1270{
1271 int i;
1272 int cur = -1;
1273 colnr_T col;
1274
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001275 if (!pum_wanted() || !pum_enough_matches())
1276 return;
1277
1278#if defined(FEAT_EVAL)
1279 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001280 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001281#endif
1282
1283 // Update the screen later, before drawing the popup menu over it.
1284 pum_call_update_screen();
1285
1286 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001287 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001289 else
1290 {
1291 // popup menu already exists, only need to find the current item.
1292 for (i = 0; i < compl_match_arraysize; ++i)
1293 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1294 || compl_match_array[i].pum_text
1295 == compl_shown_match->cp_text[CPT_ABBR])
1296 {
1297 cur = i;
1298 break;
1299 }
1300 }
1301
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001302 if (compl_match_array == NULL)
1303 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001304
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001305 // In Replace mode when a $ is displayed at the end of the line only
1306 // part of the screen would be updated. We do need to redraw here.
1307 dollar_vcol = -1;
1308
1309 // Compute the screen column of the start of the completed text.
1310 // Use the cursor to get all wrapping and other settings right.
1311 col = curwin->w_cursor.col;
1312 curwin->w_cursor.col = compl_col;
1313 pum_display(compl_match_array, compl_match_arraysize, cur);
1314 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001315
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001316#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001317 if (has_completechanged())
1318 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001319#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001320}
1321
1322#define DICT_FIRST (1) // use just first element in "dict"
1323#define DICT_EXACT (2) // "dict" is the exact name of a file
1324
1325/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001326 * Add any identifiers that match the given pattern "pat" in the list of
1327 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001328 */
1329 static void
1330ins_compl_dictionaries(
1331 char_u *dict_start,
1332 char_u *pat,
1333 int flags, // DICT_FIRST and/or DICT_EXACT
1334 int thesaurus) // Thesaurus completion
1335{
1336 char_u *dict = dict_start;
1337 char_u *ptr;
1338 char_u *buf;
1339 regmatch_T regmatch;
1340 char_u **files;
1341 int count;
1342 int save_p_scs;
1343 int dir = compl_direction;
1344
1345 if (*dict == NUL)
1346 {
1347#ifdef FEAT_SPELL
1348 // When 'dictionary' is empty and spell checking is enabled use
1349 // "spell".
1350 if (!thesaurus && curwin->w_p_spell)
1351 dict = (char_u *)"spell";
1352 else
1353#endif
1354 return;
1355 }
1356
1357 buf = alloc(LSIZE);
1358 if (buf == NULL)
1359 return;
1360 regmatch.regprog = NULL; // so that we can goto theend
1361
1362 // If 'infercase' is set, don't use 'smartcase' here
1363 save_p_scs = p_scs;
1364 if (curbuf->b_p_inf)
1365 p_scs = FALSE;
1366
1367 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1368 // to only match at the start of a line. Otherwise just match the
1369 // pattern. Also need to double backslashes.
1370 if (ctrl_x_mode_line_or_eval())
1371 {
1372 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1373 size_t len;
1374
1375 if (pat_esc == NULL)
1376 goto theend;
1377 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001378 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001379 if (ptr == NULL)
1380 {
1381 vim_free(pat_esc);
1382 goto theend;
1383 }
1384 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1385 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1386 vim_free(pat_esc);
1387 vim_free(ptr);
1388 }
1389 else
1390 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001391 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 if (regmatch.regprog == NULL)
1393 goto theend;
1394 }
1395
1396 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1397 regmatch.rm_ic = ignorecase(pat);
1398 while (*dict != NUL && !got_int && !compl_interrupted)
1399 {
1400 // copy one dictionary file name into buf
1401 if (flags == DICT_EXACT)
1402 {
1403 count = 1;
1404 files = &dict;
1405 }
1406 else
1407 {
1408 // Expand wildcards in the dictionary name, but do not allow
1409 // backticks (for security, the 'dict' option may have been set in
1410 // a modeline).
1411 copy_option_part(&dict, buf, LSIZE, ",");
1412# ifdef FEAT_SPELL
1413 if (!thesaurus && STRCMP(buf, "spell") == 0)
1414 count = -1;
1415 else
1416# endif
1417 if (vim_strchr(buf, '`') != NULL
1418 || expand_wildcards(1, &buf, &count, &files,
1419 EW_FILE|EW_SILENT) != OK)
1420 count = 0;
1421 }
1422
1423# ifdef FEAT_SPELL
1424 if (count == -1)
1425 {
1426 // Complete from active spelling. Skip "\<" in the pattern, we
1427 // don't use it as a RE.
1428 if (pat[0] == '\\' && pat[1] == '<')
1429 ptr = pat + 2;
1430 else
1431 ptr = pat;
1432 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1433 }
1434 else
1435# endif
1436 if (count > 0) // avoid warning for using "files" uninit
1437 {
1438 ins_compl_files(count, files, thesaurus, flags,
1439 &regmatch, buf, &dir);
1440 if (flags != DICT_EXACT)
1441 FreeWild(count, files);
1442 }
1443 if (flags != 0)
1444 break;
1445 }
1446
1447theend:
1448 p_scs = save_p_scs;
1449 vim_regfree(regmatch.regprog);
1450 vim_free(buf);
1451}
1452
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001453/*
1454 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1455 * skipping the word at 'skip_word'. Returns OK on success.
1456 */
1457 static int
1458thesarurs_add_words_in_line(
1459 char_u *fname,
1460 char_u **buf_arg,
1461 int dir,
1462 char_u *skip_word)
1463{
1464 int status = OK;
1465 char_u *ptr;
1466 char_u *wstart;
1467
1468 // Add the other matches on the line
1469 ptr = *buf_arg;
1470 while (!got_int)
1471 {
1472 // Find start of the next word. Skip white
1473 // space and punctuation.
1474 ptr = find_word_start(ptr);
1475 if (*ptr == NUL || *ptr == NL)
1476 break;
1477 wstart = ptr;
1478
1479 // Find end of the word.
1480 if (has_mbyte)
1481 // Japanese words may have characters in
1482 // different classes, only separate words
1483 // with single-byte non-word characters.
1484 while (*ptr != NUL)
1485 {
1486 int l = (*mb_ptr2len)(ptr);
1487
1488 if (l < 2 && !vim_iswordc(*ptr))
1489 break;
1490 ptr += l;
1491 }
1492 else
1493 ptr = find_word_end(ptr);
1494
1495 // Add the word. Skip the regexp match.
1496 if (wstart != skip_word)
1497 {
1498 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1499 fname, dir, FALSE);
1500 if (status == FAIL)
1501 break;
1502 }
1503 }
1504
1505 *buf_arg = ptr;
1506 return status;
1507}
1508
1509/*
1510 * Process "count" dictionary/thesaurus "files" and add the text matching
1511 * "regmatch".
1512 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001513 static void
1514ins_compl_files(
1515 int count,
1516 char_u **files,
1517 int thesaurus,
1518 int flags,
1519 regmatch_T *regmatch,
1520 char_u *buf,
1521 int *dir)
1522{
1523 char_u *ptr;
1524 int i;
1525 FILE *fp;
1526 int add_r;
1527
1528 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1529 {
1530 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1531 if (flags != DICT_EXACT)
1532 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001533 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001534 vim_snprintf((char *)IObuff, IOSIZE,
1535 _("Scanning dictionary: %s"), (char *)files[i]);
1536 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1537 }
1538
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001539 if (fp == NULL)
1540 continue;
1541
1542 // Read dictionary file line by line.
1543 // Check each line for a match.
1544 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001545 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001546 ptr = buf;
1547 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001548 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001549 ptr = regmatch->startp[0];
1550 if (ctrl_x_mode_line_or_eval())
1551 ptr = find_line_end(ptr);
1552 else
1553 ptr = find_word_end(ptr);
1554 add_r = ins_compl_add_infercase(regmatch->startp[0],
1555 (int)(ptr - regmatch->startp[0]),
1556 p_ic, files[i], *dir, FALSE);
1557 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001558 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001559 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001560 ptr = buf;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001561 add_r = thesarurs_add_words_in_line(files[i], &ptr, *dir,
1562 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001563 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001564 if (add_r == OK)
1565 // if dir was BACKWARD then honor it just once
1566 *dir = FORWARD;
1567 else if (add_r == FAIL)
1568 break;
1569 // avoid expensive call to vim_regexec() when at end
1570 // of line
1571 if (*ptr == '\n' || got_int)
1572 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001573 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001574 line_breakcheck();
1575 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001576 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001577 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001578 }
1579}
1580
1581/*
1582 * Find the start of the next word.
1583 * Returns a pointer to the first char of the word. Also stops at a NUL.
1584 */
1585 char_u *
1586find_word_start(char_u *ptr)
1587{
1588 if (has_mbyte)
1589 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1590 ptr += (*mb_ptr2len)(ptr);
1591 else
1592 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1593 ++ptr;
1594 return ptr;
1595}
1596
1597/*
1598 * Find the end of the word. Assumes it starts inside a word.
1599 * Returns a pointer to just after the word.
1600 */
1601 char_u *
1602find_word_end(char_u *ptr)
1603{
1604 int start_class;
1605
1606 if (has_mbyte)
1607 {
1608 start_class = mb_get_class(ptr);
1609 if (start_class > 1)
1610 while (*ptr != NUL)
1611 {
1612 ptr += (*mb_ptr2len)(ptr);
1613 if (mb_get_class(ptr) != start_class)
1614 break;
1615 }
1616 }
1617 else
1618 while (vim_iswordc(*ptr))
1619 ++ptr;
1620 return ptr;
1621}
1622
1623/*
1624 * Find the end of the line, omitting CR and NL at the end.
1625 * Returns a pointer to just after the line.
1626 */
1627 static char_u *
1628find_line_end(char_u *ptr)
1629{
1630 char_u *s;
1631
1632 s = ptr + STRLEN(ptr);
1633 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1634 --s;
1635 return s;
1636}
1637
1638/*
1639 * Free the list of completions
1640 */
1641 static void
1642ins_compl_free(void)
1643{
1644 compl_T *match;
1645 int i;
1646
1647 VIM_CLEAR(compl_pattern);
1648 VIM_CLEAR(compl_leader);
1649
1650 if (compl_first_match == NULL)
1651 return;
1652
1653 ins_compl_del_pum();
1654 pum_clear();
1655
1656 compl_curr_match = compl_first_match;
1657 do
1658 {
1659 match = compl_curr_match;
1660 compl_curr_match = compl_curr_match->cp_next;
1661 vim_free(match->cp_str);
1662 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001663 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001664 vim_free(match->cp_fname);
1665 for (i = 0; i < CPT_COUNT; ++i)
1666 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001667#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001668 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001669#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001670 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001671 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001672 compl_first_match = compl_curr_match = NULL;
1673 compl_shown_match = NULL;
1674 compl_old_match = NULL;
1675}
1676
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001677/*
1678 * Reset/clear the completion state.
1679 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001680 void
1681ins_compl_clear(void)
1682{
1683 compl_cont_status = 0;
1684 compl_started = FALSE;
1685 compl_matches = 0;
1686 VIM_CLEAR(compl_pattern);
1687 VIM_CLEAR(compl_leader);
1688 edit_submode_extra = NULL;
1689 VIM_CLEAR(compl_orig_text);
1690 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001691#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001692 // clear v:completed_item
1693 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001694#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001695}
1696
1697/*
1698 * Return TRUE when Insert completion is active.
1699 */
1700 int
1701ins_compl_active(void)
1702{
1703 return compl_started;
1704}
1705
1706/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 * Selected one of the matches. When FALSE the match was edited or using the
1708 * longest common string.
1709 */
1710 int
1711ins_compl_used_match(void)
1712{
1713 return compl_used_match;
1714}
1715
1716/*
1717 * Initialize get longest common string.
1718 */
1719 void
1720ins_compl_init_get_longest(void)
1721{
1722 compl_get_longest = FALSE;
1723}
1724
1725/*
1726 * Returns TRUE when insert completion is interrupted.
1727 */
1728 int
1729ins_compl_interrupted(void)
1730{
1731 return compl_interrupted;
1732}
1733
1734/*
1735 * Returns TRUE if the <Enter> key selects a match in the completion popup
1736 * menu.
1737 */
1738 int
1739ins_compl_enter_selects(void)
1740{
1741 return compl_enter_selects;
1742}
1743
1744/*
1745 * Return the column where the text starts that is being completed
1746 */
1747 colnr_T
1748ins_compl_col(void)
1749{
1750 return compl_col;
1751}
1752
1753/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001754 * Return the length in bytes of the text being completed
1755 */
1756 int
1757ins_compl_len(void)
1758{
1759 return compl_length;
1760}
1761
1762/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001763 * Delete one character before the cursor and show the subset of the matches
1764 * that match the word that is now before the cursor.
1765 * Returns the character to be used, NUL if the work is done and another char
1766 * to be got from the user.
1767 */
1768 int
1769ins_compl_bs(void)
1770{
1771 char_u *line;
1772 char_u *p;
1773
1774 line = ml_get_curline();
1775 p = line + curwin->w_cursor.col;
1776 MB_PTR_BACK(line, p);
1777
1778 // Stop completion when the whole word was deleted. For Omni completion
1779 // allow the word to be deleted, we won't match everything.
1780 // Respect the 'backspace' option.
1781 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001782 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1783 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001784 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1785 - compl_length < 0))
1786 return K_BS;
1787
1788 // Deleted more than what was used to find matches or didn't finish
1789 // finding all matches: need to look for matches all over again.
1790 if (curwin->w_cursor.col <= compl_col + compl_length
1791 || ins_compl_need_restart())
1792 ins_compl_restart();
1793
1794 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001795 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001796 if (compl_leader == NULL)
1797 return K_BS;
1798
1799 ins_compl_new_leader();
1800 if (compl_shown_match != NULL)
1801 // Make sure current match is not a hidden item.
1802 compl_curr_match = compl_shown_match;
1803 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001804}
1805
1806/*
1807 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1808 * be called.
1809 */
1810 static int
1811ins_compl_need_restart(void)
1812{
1813 // Return TRUE if we didn't complete finding matches or when the
1814 // 'completefunc' returned "always" in the "refresh" dictionary item.
1815 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001816 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001817 && compl_opt_refresh_always);
1818}
1819
1820/*
1821 * Called after changing "compl_leader".
1822 * Show the popup menu with a different set of matches.
1823 * May also search for matches again if the previous search was interrupted.
1824 */
1825 static void
1826ins_compl_new_leader(void)
1827{
1828 ins_compl_del_pum();
1829 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001830 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 compl_used_match = FALSE;
1832
1833 if (compl_started)
1834 ins_compl_set_original_text(compl_leader);
1835 else
1836 {
1837#ifdef FEAT_SPELL
1838 spell_bad_len = 0; // need to redetect bad word
1839#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001840 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001841 // the popup menu display the changed text before the cursor. Set
1842 // "compl_restarting" to avoid that the first match is inserted.
1843 pum_call_update_screen();
1844#ifdef FEAT_GUI
1845 if (gui.in_use)
1846 {
1847 // Show the cursor after the match, not after the redrawn text.
1848 setcursor();
1849 out_flush_cursor(FALSE, FALSE);
1850 }
1851#endif
1852 compl_restarting = TRUE;
1853 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1854 compl_cont_status = 0;
1855 compl_restarting = FALSE;
1856 }
1857
1858 compl_enter_selects = !compl_used_match;
1859
1860 // Show the popup menu with a different set of matches.
1861 ins_compl_show_pum();
1862
1863 // Don't let Enter select the original text when there is no popup menu.
1864 if (compl_match_array == NULL)
1865 compl_enter_selects = FALSE;
1866}
1867
1868/*
1869 * Return the length of the completion, from the completion start column to
1870 * the cursor column. Making sure it never goes below zero.
1871 */
1872 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001873get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001874{
1875 int off = (int)curwin->w_cursor.col - (int)compl_col;
1876
1877 if (off < 0)
1878 return 0;
1879 return off;
1880}
1881
1882/*
1883 * Append one character to the match leader. May reduce the number of
1884 * matches.
1885 */
1886 void
1887ins_compl_addleader(int c)
1888{
1889 int cc;
1890
1891 if (stop_arrow() == FAIL)
1892 return;
1893 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1894 {
1895 char_u buf[MB_MAXBYTES + 1];
1896
1897 (*mb_char2bytes)(c, buf);
1898 buf[cc] = NUL;
1899 ins_char_bytes(buf, cc);
1900 if (compl_opt_refresh_always)
1901 AppendToRedobuff(buf);
1902 }
1903 else
1904 {
1905 ins_char(c);
1906 if (compl_opt_refresh_always)
1907 AppendCharToRedobuff(c);
1908 }
1909
1910 // If we didn't complete finding matches we must search again.
1911 if (ins_compl_need_restart())
1912 ins_compl_restart();
1913
1914 // When 'always' is set, don't reset compl_leader. While completing,
1915 // cursor doesn't point original position, changing compl_leader would
1916 // break redo.
1917 if (!compl_opt_refresh_always)
1918 {
1919 vim_free(compl_leader);
1920 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001921 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001922 if (compl_leader != NULL)
1923 ins_compl_new_leader();
1924 }
1925}
1926
1927/*
1928 * Setup for finding completions again without leaving CTRL-X mode. Used when
1929 * BS or a key was typed while still searching for matches.
1930 */
1931 static void
1932ins_compl_restart(void)
1933{
1934 ins_compl_free();
1935 compl_started = FALSE;
1936 compl_matches = 0;
1937 compl_cont_status = 0;
1938 compl_cont_mode = 0;
1939}
1940
1941/*
1942 * Set the first match, the original text.
1943 */
1944 static void
1945ins_compl_set_original_text(char_u *str)
1946{
1947 char_u *p;
1948
1949 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001950 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1951 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001952 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001953 {
1954 p = vim_strsave(str);
1955 if (p != NULL)
1956 {
1957 vim_free(compl_first_match->cp_str);
1958 compl_first_match->cp_str = p;
1959 }
1960 }
1961 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001962 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001963 {
1964 p = vim_strsave(str);
1965 if (p != NULL)
1966 {
1967 vim_free(compl_first_match->cp_prev->cp_str);
1968 compl_first_match->cp_prev->cp_str = p;
1969 }
1970 }
1971}
1972
1973/*
1974 * Append one character to the match leader. May reduce the number of
1975 * matches.
1976 */
1977 void
1978ins_compl_addfrommatch(void)
1979{
1980 char_u *p;
1981 int len = (int)curwin->w_cursor.col - (int)compl_col;
1982 int c;
1983 compl_T *cp;
1984
1985 p = compl_shown_match->cp_str;
1986 if ((int)STRLEN(p) <= len) // the match is too short
1987 {
1988 // When still at the original match use the first entry that matches
1989 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001990 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001991 return;
1992
1993 p = NULL;
1994 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001995 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001997 if (compl_leader == NULL
1998 || ins_compl_equal(cp, compl_leader,
1999 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002000 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002001 p = cp->cp_str;
2002 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002003 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002004 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002005 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002006 return;
2007 }
2008 p += len;
2009 c = PTR2CHAR(p);
2010 ins_compl_addleader(c);
2011}
2012
2013/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002014 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002015 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2016 * compl_cont_mode and compl_cont_status.
2017 * Returns TRUE when the character is not to be inserted.
2018 */
2019 static int
2020set_ctrl_x_mode(int c)
2021{
2022 int retval = FALSE;
2023
2024 switch (c)
2025 {
2026 case Ctrl_E:
2027 case Ctrl_Y:
2028 // scroll the window one line up or down
2029 ctrl_x_mode = CTRL_X_SCROLL;
2030 if (!(State & REPLACE_FLAG))
2031 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2032 else
2033 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2034 edit_submode_pre = NULL;
2035 showmode();
2036 break;
2037 case Ctrl_L:
2038 // complete whole line
2039 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2040 break;
2041 case Ctrl_F:
2042 // complete filenames
2043 ctrl_x_mode = CTRL_X_FILES;
2044 break;
2045 case Ctrl_K:
2046 // complete words from a dictinoary
2047 ctrl_x_mode = CTRL_X_DICTIONARY;
2048 break;
2049 case Ctrl_R:
2050 // Register insertion without exiting CTRL-X mode
2051 // Simply allow ^R to happen without affecting ^X mode
2052 break;
2053 case Ctrl_T:
2054 // complete words from a thesaurus
2055 ctrl_x_mode = CTRL_X_THESAURUS;
2056 break;
2057#ifdef FEAT_COMPL_FUNC
2058 case Ctrl_U:
2059 // user defined completion
2060 ctrl_x_mode = CTRL_X_FUNCTION;
2061 break;
2062 case Ctrl_O:
2063 // omni completion
2064 ctrl_x_mode = CTRL_X_OMNI;
2065 break;
2066#endif
2067 case 's':
2068 case Ctrl_S:
2069 // complete spelling suggestions
2070 ctrl_x_mode = CTRL_X_SPELL;
2071#ifdef FEAT_SPELL
2072 ++emsg_off; // Avoid getting the E756 error twice.
2073 spell_back_to_badword();
2074 --emsg_off;
2075#endif
2076 break;
2077 case Ctrl_RSB:
2078 // complete tag names
2079 ctrl_x_mode = CTRL_X_TAGS;
2080 break;
2081#ifdef FEAT_FIND_ID
2082 case Ctrl_I:
2083 case K_S_TAB:
2084 // complete keywords from included files
2085 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2086 break;
2087 case Ctrl_D:
2088 // complete definitions from included files
2089 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2090 break;
2091#endif
2092 case Ctrl_V:
2093 case Ctrl_Q:
2094 // complete vim commands
2095 ctrl_x_mode = CTRL_X_CMDLINE;
2096 break;
2097 case Ctrl_Z:
2098 // stop completion
2099 ctrl_x_mode = CTRL_X_NORMAL;
2100 edit_submode = NULL;
2101 showmode();
2102 retval = TRUE;
2103 break;
2104 case Ctrl_P:
2105 case Ctrl_N:
2106 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2107 // just started ^X mode, or there were enough ^X's to cancel
2108 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2109 // do normal expansion when interrupting a different mode (say
2110 // ^X^F^X^P or ^P^X^X^P, see below)
2111 // nothing changes if interrupting mode 0, (eg, the flag
2112 // doesn't change when going to ADDING mode -- Acevedo
2113 if (!(compl_cont_status & CONT_INTRPT))
2114 compl_cont_status |= CONT_LOCAL;
2115 else if (compl_cont_mode != 0)
2116 compl_cont_status &= ~CONT_LOCAL;
2117 // FALLTHROUGH
2118 default:
2119 // If we have typed at least 2 ^X's... for modes != 0, we set
2120 // compl_cont_status = 0 (eg, as if we had just started ^X
2121 // mode).
2122 // For mode 0, we set "compl_cont_mode" to an impossible
2123 // value, in both cases ^X^X can be used to restart the same
2124 // mode (avoiding ADDING mode).
2125 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2126 // 'complete' and local ^P expansions respectively.
2127 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2128 // mode -- Acevedo
2129 if (c == Ctrl_X)
2130 {
2131 if (compl_cont_mode != 0)
2132 compl_cont_status = 0;
2133 else
2134 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2135 }
2136 ctrl_x_mode = CTRL_X_NORMAL;
2137 edit_submode = NULL;
2138 showmode();
2139 break;
2140 }
2141
2142 return retval;
2143}
2144
2145/*
2146 * Stop insert completion mode
2147 */
2148 static int
2149ins_compl_stop(int c, int prev_mode, int retval)
2150{
2151 char_u *ptr;
2152#ifdef FEAT_CINDENT
2153 int want_cindent;
2154#endif
2155
2156 // Get here when we have finished typing a sequence of ^N and
2157 // ^P or other completion characters in CTRL-X mode. Free up
2158 // memory that was used, and make sure we can redo the insert.
2159 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2160 {
2161 // If any of the original typed text has been changed, eg when
2162 // ignorecase is set, we must add back-spaces to the redo
2163 // buffer. We add as few as necessary to delete just the part
2164 // of the original text that has changed.
2165 // When using the longest match, edited the match or used
2166 // CTRL-E then don't use the current match.
2167 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2168 ptr = compl_curr_match->cp_str;
2169 else
2170 ptr = NULL;
2171 ins_compl_fixRedoBufForLeader(ptr);
2172 }
2173
2174#ifdef FEAT_CINDENT
2175 want_cindent = (get_can_cindent() && cindent_on());
2176#endif
2177 // When completing whole lines: fix indent for 'cindent'.
2178 // Otherwise, break line if it's too long.
2179 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2180 {
2181#ifdef FEAT_CINDENT
2182 // re-indent the current line
2183 if (want_cindent)
2184 {
2185 do_c_expr_indent();
2186 want_cindent = FALSE; // don't do it again
2187 }
2188#endif
2189 }
2190 else
2191 {
2192 int prev_col = curwin->w_cursor.col;
2193
2194 // put the cursor on the last char, for 'tw' formatting
2195 if (prev_col > 0)
2196 dec_cursor();
2197 // only format when something was inserted
2198 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2199 insertchar(NUL, 0, -1);
2200 if (prev_col > 0
2201 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2202 inc_cursor();
2203 }
2204
2205 // If the popup menu is displayed pressing CTRL-Y means accepting
2206 // the selection without inserting anything. When
2207 // compl_enter_selects is set the Enter key does the same.
2208 if ((c == Ctrl_Y || (compl_enter_selects
2209 && (c == CAR || c == K_KENTER || c == NL)))
2210 && pum_visible())
2211 retval = TRUE;
2212
2213 // CTRL-E means completion is Ended, go back to the typed text.
2214 // but only do this, if the Popup is still visible
2215 if (c == Ctrl_E)
2216 {
2217 ins_compl_delete();
2218 if (compl_leader != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002219 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002220 else if (compl_first_match != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002221 ins_bytes(compl_orig_text + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002222 retval = TRUE;
2223 }
2224
2225 auto_format(FALSE, TRUE);
2226
2227 // Trigger the CompleteDonePre event to give scripts a chance to
2228 // act upon the completion before clearing the info, and restore
2229 // ctrl_x_mode, so that complete_info() can be used.
2230 ctrl_x_mode = prev_mode;
2231 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2232
2233 ins_compl_free();
2234 compl_started = FALSE;
2235 compl_matches = 0;
2236 if (!shortmess(SHM_COMPLETIONMENU))
2237 msg_clr_cmdline(); // necessary for "noshowmode"
2238 ctrl_x_mode = CTRL_X_NORMAL;
2239 compl_enter_selects = FALSE;
2240 if (edit_submode != NULL)
2241 {
2242 edit_submode = NULL;
2243 showmode();
2244 }
2245
2246#ifdef FEAT_CMDWIN
2247 if (c == Ctrl_C && cmdwin_type != 0)
2248 // Avoid the popup menu remains displayed when leaving the
2249 // command line window.
2250 update_screen(0);
2251#endif
2252#ifdef FEAT_CINDENT
2253 // Indent now if a key was typed that is in 'cinkeys'.
2254 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2255 do_c_expr_indent();
2256#endif
2257 // Trigger the CompleteDone event to give scripts a chance to act
2258 // upon the end of completion.
2259 ins_apply_autocmds(EVENT_COMPLETEDONE);
2260
2261 return retval;
2262}
2263
2264/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002265 * Prepare for Insert mode completion, or stop it.
2266 * Called just after typing a character in Insert mode.
2267 * Returns TRUE when the character is not to be inserted;
2268 */
2269 int
2270ins_compl_prep(int c)
2271{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002272 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002273 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002274
2275 // Forget any previous 'special' messages if this is actually
2276 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2277 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2278 edit_submode_extra = NULL;
2279
2280 // Ignore end of Select mode mapping and mouse scroll buttons.
2281 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01002282 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002283 return retval;
2284
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002285#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002286 // Ignore mouse events in a popup window
2287 if (is_mouse_key(c))
2288 {
2289 // Ignore drag and release events, the position does not need to be in
2290 // the popup and it may have just closed.
2291 if (c == K_LEFTRELEASE
2292 || c == K_LEFTRELEASE_NM
2293 || c == K_MIDDLERELEASE
2294 || c == K_RIGHTRELEASE
2295 || c == K_X1RELEASE
2296 || c == K_X2RELEASE
2297 || c == K_LEFTDRAG
2298 || c == K_MIDDLEDRAG
2299 || c == K_RIGHTDRAG
2300 || c == K_X1DRAG
2301 || c == K_X2DRAG)
2302 return retval;
2303 if (popup_visible)
2304 {
2305 int row = mouse_row;
2306 int col = mouse_col;
2307 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2308
2309 if (wp != NULL && WIN_IS_POPUP(wp))
2310 return retval;
2311 }
2312 }
2313#endif
2314
zeertzjqdca29d92021-08-31 19:12:51 +02002315 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2316 {
2317 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2318 || !vim_is_ctrl_x_key(c))
2319 {
2320 // Not starting another completion mode.
2321 ctrl_x_mode = CTRL_X_CMDLINE;
2322
2323 // CTRL-X CTRL-Z should stop completion without inserting anything
2324 if (c == Ctrl_Z)
2325 retval = TRUE;
2326 }
2327 else
2328 {
2329 ctrl_x_mode = CTRL_X_CMDLINE;
2330
2331 // Other CTRL-X keys first stop completion, then start another
2332 // completion mode.
2333 ins_compl_prep(' ');
2334 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2335 }
2336 }
2337
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002338 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002339 if (ctrl_x_mode_not_defined_yet()
2340 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002341 {
2342 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2343 compl_used_match = TRUE;
2344
2345 }
2346
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002347 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002348 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2349 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002350 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002351 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002352 {
2353 // We're already in CTRL-X mode, do we stay in it?
2354 if (!vim_is_ctrl_x_key(c))
2355 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002356 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002357 ctrl_x_mode = CTRL_X_NORMAL;
2358 else
2359 ctrl_x_mode = CTRL_X_FINISHED;
2360 edit_submode = NULL;
2361 }
2362 showmode();
2363 }
2364
2365 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2366 {
2367 // Show error message from attempted keyword completion (probably
2368 // 'Pattern not found') until another key is hit, then go back to
2369 // showing what mode we are in.
2370 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002371 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002372 && c != Ctrl_R && !ins_compl_pum_key(c))
2373 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002374 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002375 }
2376 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2377 // Trigger the CompleteDone event to give scripts a chance to act
2378 // upon the (possibly failed) completion.
2379 ins_apply_autocmds(EVENT_COMPLETEDONE);
2380
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002381 trigger_modechanged();
2382
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002383 // reset continue_* if we left expansion-mode, if we stay they'll be
2384 // (re)set properly in ins_complete()
2385 if (!vim_is_ctrl_x_key(c))
2386 {
2387 compl_cont_status = 0;
2388 compl_cont_mode = 0;
2389 }
2390
2391 return retval;
2392}
2393
2394/*
2395 * Fix the redo buffer for the completion leader replacing some of the typed
2396 * text. This inserts backspaces and appends the changed text.
2397 * "ptr" is the known leader text or NUL.
2398 */
2399 static void
2400ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2401{
2402 int len;
2403 char_u *p;
2404 char_u *ptr = ptr_arg;
2405
2406 if (ptr == NULL)
2407 {
2408 if (compl_leader != NULL)
2409 ptr = compl_leader;
2410 else
2411 return; // nothing to do
2412 }
2413 if (compl_orig_text != NULL)
2414 {
2415 p = compl_orig_text;
2416 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2417 ;
2418 if (len > 0)
2419 len -= (*mb_head_off)(p, p + len);
2420 for (p += len; *p != NUL; MB_PTR_ADV(p))
2421 AppendCharToRedobuff(K_BS);
2422 }
2423 else
2424 len = 0;
2425 if (ptr != NULL)
2426 AppendToRedobuffLit(ptr + len, -1);
2427}
2428
2429/*
2430 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2431 * (depending on flag) starting from buf and looking for a non-scanned
2432 * buffer (other than curbuf). curbuf is special, if it is called with
2433 * buf=curbuf then it has to be the first call for a given flag/expansion.
2434 *
2435 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2436 */
2437 static buf_T *
2438ins_compl_next_buf(buf_T *buf, int flag)
2439{
2440 static win_T *wp = NULL;
2441
2442 if (flag == 'w') // just windows
2443 {
2444 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2445 wp = curwin;
2446 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2447 && wp->w_buffer->b_scanned)
2448 ;
2449 buf = wp->w_buffer;
2450 }
2451 else
2452 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2453 // (unlisted buffers)
2454 // When completing whole lines skip unloaded buffers.
2455 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2456 && ((flag == 'U'
2457 ? buf->b_p_bl
2458 : (!buf->b_p_bl
2459 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2460 || buf->b_scanned))
2461 ;
2462 return buf;
2463}
2464
2465#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002466
2467# ifdef FEAT_EVAL
2468static callback_T cfu_cb; // 'completefunc' callback function
2469static callback_T ofu_cb; // 'omnifunc' callback function
2470static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2471# endif
2472
2473/*
2474 * Copy a global callback function to a buffer local callback.
2475 */
2476 static void
2477copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2478{
2479 free_callback(bufcb);
2480 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2481 copy_callback(bufcb, globcb);
2482}
2483
2484/*
2485 * Parse the 'completefunc' option value and set the callback function.
2486 * Invoked when the 'completefunc' option is set. The option value can be a
2487 * name of a function (string), or function(<name>) or funcref(<name>) or a
2488 * lambda expression.
2489 */
2490 int
2491set_completefunc_option(void)
2492{
2493 int retval;
2494
2495 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2496 if (retval == OK)
2497 set_buflocal_cfu_callback(curbuf);
2498
2499 return retval;
2500}
2501
2502/*
2503 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002504 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002505 */
2506 void
2507set_buflocal_cfu_callback(buf_T *buf UNUSED)
2508{
2509# ifdef FEAT_EVAL
2510 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2511# endif
2512}
2513
2514/*
2515 * Parse the 'omnifunc' option value and set the callback function.
2516 * Invoked when the 'omnifunc' option is set. The option value can be a
2517 * name of a function (string), or function(<name>) or funcref(<name>) or a
2518 * lambda expression.
2519 */
2520 int
2521set_omnifunc_option(void)
2522{
2523 int retval;
2524
2525 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2526 if (retval == OK)
2527 set_buflocal_ofu_callback(curbuf);
2528
2529 return retval;
2530}
2531
2532/*
2533 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002534 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002535 */
2536 void
2537set_buflocal_ofu_callback(buf_T *buf UNUSED)
2538{
2539# ifdef FEAT_EVAL
2540 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2541# endif
2542}
2543
2544/*
2545 * Parse the 'thesaurusfunc' option value and set the callback function.
2546 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2547 * name of a function (string), or function(<name>) or funcref(<name>) or a
2548 * lambda expression.
2549 */
2550 int
2551set_thesaurusfunc_option(void)
2552{
2553 int retval;
2554
2555 if (*curbuf->b_p_tsrfu != NUL)
2556 {
2557 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002558 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2559 &curbuf->b_tsrfu_cb);
2560 }
2561 else
2562 {
2563 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002564 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2565 }
2566
2567 return retval;
2568}
2569
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002570/*
2571 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002572 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002573 */
2574 int
2575set_ref_in_insexpand_funcs(int copyID)
2576{
2577 int abort = FALSE;
2578
2579 abort = set_ref_in_callback(&cfu_cb, copyID);
2580 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2581 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2582
2583 return abort;
2584}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002585
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002586/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002587 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002588 */
2589 static char_u *
2590get_complete_funcname(int type)
2591{
2592 switch (type)
2593 {
2594 case CTRL_X_FUNCTION:
2595 return curbuf->b_p_cfu;
2596 case CTRL_X_OMNI:
2597 return curbuf->b_p_ofu;
2598 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002599 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002600 default:
2601 return (char_u *)"";
2602 }
2603}
2604
2605/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002606 * Get the callback to use for insert mode completion.
2607 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002608 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002609get_insert_callback(int type)
2610{
2611 if (type == CTRL_X_FUNCTION)
2612 return &curbuf->b_cfu_cb;
2613 if (type == CTRL_X_OMNI)
2614 return &curbuf->b_ofu_cb;
2615 // CTRL_X_THESAURUS
2616 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2617}
2618
2619/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002620 * Execute user defined complete function 'completefunc', 'omnifunc' or
2621 * 'thesaurusfunc', and get matches in "matches".
2622 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002623 */
2624 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002625expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002626{
2627 list_T *matchlist = NULL;
2628 dict_T *matchdict = NULL;
2629 typval_T args[3];
2630 char_u *funcname;
2631 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002632 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002633 typval_T rettv;
2634 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002635 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002636
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002637 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002638 if (*funcname == NUL)
2639 return;
2640
2641 // Call 'completefunc' to obtain the list of matches.
2642 args[0].v_type = VAR_NUMBER;
2643 args[0].vval.v_number = 0;
2644 args[1].v_type = VAR_STRING;
2645 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2646 args[2].v_type = VAR_UNKNOWN;
2647
2648 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002649 // Lock the text to avoid weird things from happening. Also disallow
2650 // switching to another window, it should not be needed and may end up in
2651 // Insert mode in another buffer.
2652 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002654 cb = get_insert_callback(type);
2655 retval = call_callback(cb, 0, &rettv, 2, args);
2656
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002657 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002658 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002659 {
2660 switch (rettv.v_type)
2661 {
2662 case VAR_LIST:
2663 matchlist = rettv.vval.v_list;
2664 break;
2665 case VAR_DICT:
2666 matchdict = rettv.vval.v_dict;
2667 break;
2668 case VAR_SPECIAL:
2669 if (rettv.vval.v_number == VVAL_NONE)
2670 compl_opt_suppress_empty = TRUE;
2671 // FALLTHROUGH
2672 default:
2673 // TODO: Give error message?
2674 clear_tv(&rettv);
2675 break;
2676 }
2677 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002678 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002680 curwin->w_cursor = pos; // restore the cursor position
2681 validate_cursor();
2682 if (!EQUAL_POS(curwin->w_cursor, pos))
2683 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002684 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002685 goto theend;
2686 }
2687
2688 if (matchlist != NULL)
2689 ins_compl_add_list(matchlist);
2690 else if (matchdict != NULL)
2691 ins_compl_add_dict(matchdict);
2692
2693theend:
2694 // Restore State, it might have been changed.
2695 State = save_State;
2696
2697 if (matchdict != NULL)
2698 dict_unref(matchdict);
2699 if (matchlist != NULL)
2700 list_unref(matchlist);
2701}
2702#endif // FEAT_COMPL_FUNC
2703
2704#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2705/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002706 * Add a match to the list of matches from a typeval_T.
2707 * If the given string is already in the list of completions, then return
2708 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2709 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002710 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002711 */
2712 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002713ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002714{
2715 char_u *word;
2716 int dup = FALSE;
2717 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002718 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002719 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002720 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002721 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002722
Bram Moolenaar08928322020-01-04 14:32:48 +01002723 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002724 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2725 {
2726 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2727 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2728 (char_u *)"abbr", FALSE);
2729 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2730 (char_u *)"menu", FALSE);
2731 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2732 (char_u *)"kind", FALSE);
2733 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2734 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002735 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002736 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2737 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2738 flags |= CP_ICASE;
2739 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2740 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2741 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2742 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2743 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2744 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2745 flags |= CP_EQUAL;
2746 }
2747 else
2748 {
2749 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002750 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002751 }
2752 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002753 {
2754 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002755 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002756 }
2757 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2758 if (status != OK)
2759 clear_tv(&user_data);
2760 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002761}
2762
2763/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002764 * Add completions from a list.
2765 */
2766 static void
2767ins_compl_add_list(list_T *list)
2768{
2769 listitem_T *li;
2770 int dir = compl_direction;
2771
2772 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002773 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002774 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002775 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002776 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002777 // if dir was BACKWARD then honor it just once
2778 dir = FORWARD;
2779 else if (did_emsg)
2780 break;
2781 }
2782}
2783
2784/*
2785 * Add completions from a dict.
2786 */
2787 static void
2788ins_compl_add_dict(dict_T *dict)
2789{
2790 dictitem_T *di_refresh;
2791 dictitem_T *di_words;
2792
2793 // Check for optional "refresh" item.
2794 compl_opt_refresh_always = FALSE;
2795 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2796 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2797 {
2798 char_u *v = di_refresh->di_tv.vval.v_string;
2799
2800 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2801 compl_opt_refresh_always = TRUE;
2802 }
2803
2804 // Add completions from a "words" list.
2805 di_words = dict_find(dict, (char_u *)"words", 5);
2806 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2807 ins_compl_add_list(di_words->di_tv.vval.v_list);
2808}
2809
2810/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002811 * Start completion for the complete() function.
2812 * "startcol" is where the matched text starts (1 is first column).
2813 * "list" is the list of matches.
2814 */
2815 static void
2816set_completion(colnr_T startcol, list_T *list)
2817{
2818 int save_w_wrow = curwin->w_wrow;
2819 int save_w_leftcol = curwin->w_leftcol;
2820 int flags = CP_ORIGINAL_TEXT;
2821
2822 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002823 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002824 ins_compl_prep(' ');
2825 ins_compl_clear();
2826 ins_compl_free();
2827
2828 compl_direction = FORWARD;
2829 if (startcol > curwin->w_cursor.col)
2830 startcol = curwin->w_cursor.col;
2831 compl_col = startcol;
2832 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2833 // compl_pattern doesn't need to be set
2834 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2835 if (p_ic)
2836 flags |= CP_ICASE;
2837 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002838 -1, NULL, NULL, NULL, 0,
2839 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002840 return;
2841
2842 ctrl_x_mode = CTRL_X_EVAL;
2843
2844 ins_compl_add_list(list);
2845 compl_matches = ins_compl_make_cyclic();
2846 compl_started = TRUE;
2847 compl_used_match = TRUE;
2848 compl_cont_status = 0;
2849
2850 compl_curr_match = compl_first_match;
2851 if (compl_no_insert || compl_no_select)
2852 {
2853 ins_complete(K_DOWN, FALSE);
2854 if (compl_no_select)
2855 // Down/Up has no real effect.
2856 ins_complete(K_UP, FALSE);
2857 }
2858 else
2859 ins_complete(Ctrl_N, FALSE);
2860 compl_enter_selects = compl_no_insert;
2861
2862 // Lazily show the popup menu, unless we got interrupted.
2863 if (!compl_interrupted)
2864 show_pum(save_w_wrow, save_w_leftcol);
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002865 trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002866 out_flush();
2867}
2868
2869/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002870 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002871 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002872 void
2873f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002874{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002875 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002876 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002877
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002878 if (in_vim9script()
2879 && (check_for_number_arg(argvars, 0) == FAIL
2880 || check_for_list_arg(argvars, 1) == FAIL))
2881 return;
2882
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002883 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002884 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002885 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002886 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002887 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002888
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002889 // "textlock" is set when evaluating 'completefunc' but we can change
2890 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002891 textlock = 0;
2892
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002893 // Check for undo allowed here, because if something was already inserted
2894 // the line was already saved for undo and this check isn't done.
2895 if (!undo_allowed())
2896 return;
2897
2898 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002899 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002900 else
2901 {
2902 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2903 if (startcol > 0)
2904 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002905 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002906 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002907}
2908
2909/*
2910 * "complete_add()" function
2911 */
2912 void
2913f_complete_add(typval_T *argvars, typval_T *rettv)
2914{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002915 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002916 return;
2917
Bram Moolenaar440cf092021-04-03 20:13:30 +02002918 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002919}
2920
2921/*
2922 * "complete_check()" function
2923 */
2924 void
2925f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2926{
2927 int saved = RedrawingDisabled;
2928
2929 RedrawingDisabled = 0;
2930 ins_compl_check_keys(0, TRUE);
2931 rettv->vval.v_number = ins_compl_interrupted();
2932 RedrawingDisabled = saved;
2933}
2934
2935/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002936 * Return Insert completion mode name string
2937 */
2938 static char_u *
2939ins_compl_mode(void)
2940{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002941 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002942 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2943
2944 return (char_u *)"";
2945}
2946
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002947/*
2948 * Assign the sequence number to all the completion matches which don't have
2949 * one assigned yet.
2950 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002951 static void
2952ins_compl_update_sequence_numbers()
2953{
2954 int number = 0;
2955 compl_T *match;
2956
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002957 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002958 {
2959 // search backwards for the first valid (!= -1) number.
2960 // This should normally succeed already at the first loop
2961 // cycle, so it's fast!
2962 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002963 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002964 if (match->cp_number != -1)
2965 {
2966 number = match->cp_number;
2967 break;
2968 }
2969 if (match != NULL)
2970 // go up and assign all numbers which are not assigned
2971 // yet
2972 for (match = match->cp_next;
2973 match != NULL && match->cp_number == -1;
2974 match = match->cp_next)
2975 match->cp_number = ++number;
2976 }
2977 else // BACKWARD
2978 {
2979 // search forwards (upwards) for the first valid (!= -1)
2980 // number. This should normally succeed already at the
2981 // first loop cycle, so it's fast!
2982 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002983 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002984 if (match->cp_number != -1)
2985 {
2986 number = match->cp_number;
2987 break;
2988 }
2989 if (match != NULL)
2990 // go down and assign all numbers which are not
2991 // assigned yet
2992 for (match = match->cp_prev; match
2993 && match->cp_number == -1;
2994 match = match->cp_prev)
2995 match->cp_number = ++number;
2996 }
2997}
2998
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002999/*
3000 * Get complete information
3001 */
3002 static void
3003get_complete_info(list_T *what_list, dict_T *retdict)
3004{
3005 int ret = OK;
3006 listitem_T *item;
3007#define CI_WHAT_MODE 0x01
3008#define CI_WHAT_PUM_VISIBLE 0x02
3009#define CI_WHAT_ITEMS 0x04
3010#define CI_WHAT_SELECTED 0x08
3011#define CI_WHAT_INSERTED 0x10
3012#define CI_WHAT_ALL 0xff
3013 int what_flag;
3014
3015 if (what_list == NULL)
3016 what_flag = CI_WHAT_ALL;
3017 else
3018 {
3019 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003020 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003021 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003022 {
3023 char_u *what = tv_get_string(&item->li_tv);
3024
3025 if (STRCMP(what, "mode") == 0)
3026 what_flag |= CI_WHAT_MODE;
3027 else if (STRCMP(what, "pum_visible") == 0)
3028 what_flag |= CI_WHAT_PUM_VISIBLE;
3029 else if (STRCMP(what, "items") == 0)
3030 what_flag |= CI_WHAT_ITEMS;
3031 else if (STRCMP(what, "selected") == 0)
3032 what_flag |= CI_WHAT_SELECTED;
3033 else if (STRCMP(what, "inserted") == 0)
3034 what_flag |= CI_WHAT_INSERTED;
3035 }
3036 }
3037
3038 if (ret == OK && (what_flag & CI_WHAT_MODE))
3039 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3040
3041 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3042 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3043
3044 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3045 {
3046 list_T *li;
3047 dict_T *di;
3048 compl_T *match;
3049
3050 li = list_alloc();
3051 if (li == NULL)
3052 return;
3053 ret = dict_add_list(retdict, "items", li);
3054 if (ret == OK && compl_first_match != NULL)
3055 {
3056 match = compl_first_match;
3057 do
3058 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003059 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003060 {
3061 di = dict_alloc();
3062 if (di == NULL)
3063 return;
3064 ret = list_append_dict(li, di);
3065 if (ret != OK)
3066 return;
3067 dict_add_string(di, "word", match->cp_str);
3068 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3069 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3070 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3071 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003072 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3073 // Add an empty string for backwards compatibility
3074 dict_add_string(di, "user_data", (char_u *)"");
3075 else
3076 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003077 }
3078 match = match->cp_next;
3079 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003080 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003081 }
3082 }
3083
3084 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003085 {
3086 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3087 ins_compl_update_sequence_numbers();
3088 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3089 ? compl_curr_match->cp_number - 1 : -1);
3090 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003091
3092 // TODO
3093 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3094}
3095
3096/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003097 * "complete_info()" function
3098 */
3099 void
3100f_complete_info(typval_T *argvars, typval_T *rettv)
3101{
3102 list_T *what_list = NULL;
3103
3104 if (rettv_dict_alloc(rettv) != OK)
3105 return;
3106
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003107 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3108 return;
3109
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003110 if (argvars[0].v_type != VAR_UNKNOWN)
3111 {
3112 if (argvars[0].v_type != VAR_LIST)
3113 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003114 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003115 return;
3116 }
3117 what_list = argvars[0].vval.v_list;
3118 }
3119 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003120}
3121#endif
3122
3123/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003124 * Returns TRUE when using a user-defined function for thesaurus completion.
3125 */
3126 static int
3127thesaurus_func_complete(int type UNUSED)
3128{
3129#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003130 return type == CTRL_X_THESAURUS
3131 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003132#else
3133 return FALSE;
3134#endif
3135}
3136
3137/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003138 * Return value of process_next_cpt_value()
3139 */
3140enum
3141{
3142 INS_COMPL_CPT_OK = 1,
3143 INS_COMPL_CPT_CONT,
3144 INS_COMPL_CPT_END
3145};
3146
3147/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003148 * state information used for getting the next set of insert completion
3149 * matches.
3150 */
3151typedef struct
3152{
3153 char_u *e_cpt; // current entry in 'complete'
3154 buf_T *ins_buf; // buffer being scanned
3155 pos_T *cur_match_pos; // current match position
3156 pos_T prev_match_pos; // previous match position
3157 int set_match_pos; // save first_match_pos/last_match_pos
3158 pos_T first_match_pos; // first match position
3159 pos_T last_match_pos; // last match position
3160 int found_all; // found all matches of a certain type.
3161 char_u *dict; // dictionary file to search
3162 int dict_f; // "dict" is an exact file name or not
3163} ins_compl_next_state_T;
3164
3165/*
3166 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003167 *
3168 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003169 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003170 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003171 * st->found_all - all matches of this type are found
3172 * st->ins_buf - search for completions in this buffer
3173 * st->first_match_pos - position of the first completion match
3174 * st->last_match_pos - position of the last completion match
3175 * st->set_match_pos - TRUE if the first match position should be saved to
3176 * avoid loops after the search wraps around.
3177 * st->dict - name of the dictionary or thesaurus file to search
3178 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003179 *
3180 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003181 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3182 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003183 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003184 */
3185 static int
3186process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003187 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003188 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003189 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003190{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003191 int compl_type = -1;
3192 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003193
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003194 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003195
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003196 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3197 st->e_cpt++;
3198
3199 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003200 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003201 st->ins_buf = curbuf;
3202 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003203 // Move the cursor back one character so that ^N can match the
3204 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003205 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003206 {
3207 // Move the cursor to after the last character in the
3208 // buffer, so that word at start of buffer is found
3209 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003210 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3211 st->first_match_pos.col =
3212 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003213 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003214 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003215 compl_type = 0;
3216
3217 // Remember the first match so that the loop stops when we
3218 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003219 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003220 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003221 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3222 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003223 {
3224 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003225 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003226 {
3227 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003228 st->first_match_pos.col = st->last_match_pos.col = 0;
3229 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3230 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003231 compl_type = 0;
3232 }
3233 else // unloaded buffer, scan like dictionary
3234 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003235 st->found_all = TRUE;
3236 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003237 {
3238 status = INS_COMPL_CPT_CONT;
3239 goto done;
3240 }
3241 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003242 st->dict = st->ins_buf->b_fname;
3243 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003244 }
3245 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3246 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 st->ins_buf->b_fname == NULL
3248 ? buf_spname(st->ins_buf)
3249 : st->ins_buf->b_sfname == NULL
3250 ? st->ins_buf->b_fname
3251 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003252 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3253 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255 status = INS_COMPL_CPT_END;
3256 else
3257 {
3258 if (ctrl_x_mode_line_or_eval())
3259 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003260 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003261 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003262 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 compl_type = CTRL_X_DICTIONARY;
3264 else
3265 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003266 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003267 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003268 st->dict = st->e_cpt;
3269 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003270 }
3271 }
3272#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003273 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003275 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276 compl_type = CTRL_X_PATH_DEFINES;
3277#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003278 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003279 {
3280 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3281 compl_type = CTRL_X_TAGS;
3282 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3283 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3284 }
3285 else
3286 compl_type = -1;
3287
3288 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003289 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003290
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003291 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003292 if (compl_type == -1)
3293 status = INS_COMPL_CPT_CONT;
3294 }
3295
3296done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003297 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003298 return status;
3299}
3300
3301#ifdef FEAT_FIND_ID
3302/*
3303 * Get the next set of identifiers or defines matching "compl_pattern" in
3304 * included files.
3305 */
3306 static void
3307get_next_include_file_completion(int compl_type)
3308{
3309 find_pattern_in_path(compl_pattern, compl_direction,
3310 (int)STRLEN(compl_pattern), FALSE, FALSE,
3311 (compl_type == CTRL_X_PATH_DEFINES
3312 && !(compl_cont_status & CONT_SOL))
3313 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3314 (linenr_T)1, (linenr_T)MAXLNUM);
3315}
3316#endif
3317
3318/*
3319 * Get the next set of words matching "compl_pattern" in dictionary or
3320 * thesaurus files.
3321 */
3322 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003323get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003324{
3325#ifdef FEAT_COMPL_FUNC
3326 if (thesaurus_func_complete(compl_type))
3327 expand_by_function(compl_type, compl_pattern);
3328 else
3329#endif
3330 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003331 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003332 : (compl_type == CTRL_X_THESAURUS
3333 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3334 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3335 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003336 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003337 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003338}
3339
3340/*
3341 * Get the next set of tag names matching "compl_pattern".
3342 */
3343 static void
3344get_next_tag_completion(void)
3345{
3346 int save_p_ic;
3347 char_u **matches;
3348 int num_matches;
3349
3350 // set p_ic according to p_ic, p_scs and pat for find_tags().
3351 save_p_ic = p_ic;
3352 p_ic = ignorecase(compl_pattern);
3353
3354 // Find up to TAG_MANY matches. Avoids that an enormous number
3355 // of matches is found when compl_pattern is empty
3356 g_tag_at_cursor = TRUE;
3357 if (find_tags(compl_pattern, &num_matches, &matches,
3358 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003359 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003360 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3361 ins_compl_add_matches(num_matches, matches, p_ic);
3362 g_tag_at_cursor = FALSE;
3363 p_ic = save_p_ic;
3364}
3365
3366/*
3367 * Get the next set of filename matching "compl_pattern".
3368 */
3369 static void
3370get_next_filename_completion(void)
3371{
3372 char_u **matches;
3373 int num_matches;
3374
3375 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3376 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3377 return;
3378
3379 // May change home directory back to "~".
3380 tilde_replace(compl_pattern, num_matches, matches);
3381#ifdef BACKSLASH_IN_FILENAME
3382 if (curbuf->b_p_csl[0] != NUL)
3383 {
3384 int i;
3385
3386 for (i = 0; i < num_matches; ++i)
3387 {
3388 char_u *ptr = matches[i];
3389
3390 while (*ptr != NUL)
3391 {
3392 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3393 *ptr = '/';
3394 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3395 *ptr = '\\';
3396 ptr += (*mb_ptr2len)(ptr);
3397 }
3398 }
3399 }
3400#endif
3401 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3402}
3403
3404/*
3405 * Get the next set of command-line completions matching "compl_pattern".
3406 */
3407 static void
3408get_next_cmdline_completion()
3409{
3410 char_u **matches;
3411 int num_matches;
3412
3413 if (expand_cmdline(&compl_xp, compl_pattern,
3414 (int)STRLEN(compl_pattern),
3415 &num_matches, &matches) == EXPAND_OK)
3416 ins_compl_add_matches(num_matches, matches, FALSE);
3417}
3418
3419/*
3420 * Get the next set of spell suggestions matching "compl_pattern".
3421 */
3422 static void
3423get_next_spell_completion(linenr_T lnum UNUSED)
3424{
3425#ifdef FEAT_SPELL
3426 char_u **matches;
3427 int num_matches;
3428
3429 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3430 if (num_matches > 0)
3431 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003432 else
3433 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003434#endif
3435}
3436
3437/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003438 * Return the next word or line from buffer "ins_buf" at position
3439 * "cur_match_pos" for completion. The length of the match is set in "len".
3440 */
3441 static char_u *
3442ins_comp_get_next_word_or_line(
3443 buf_T *ins_buf, // buffer being scanned
3444 pos_T *cur_match_pos, // current match position
3445 int *match_len,
3446 int *cont_s_ipos) // next ^X<> will set initial_pos
3447{
3448 char_u *ptr;
3449 int len;
3450
3451 *match_len = 0;
3452 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3453 cur_match_pos->col;
3454 if (ctrl_x_mode_line_or_eval())
3455 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003456 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003457 {
3458 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3459 return NULL;
3460 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3461 if (!p_paste)
3462 ptr = skipwhite(ptr);
3463 }
3464 len = (int)STRLEN(ptr);
3465 }
3466 else
3467 {
3468 char_u *tmp_ptr = ptr;
3469
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003470 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003471 {
3472 tmp_ptr += compl_length;
3473 // Skip if already inside a word.
3474 if (vim_iswordp(tmp_ptr))
3475 return NULL;
3476 // Find start of next word.
3477 tmp_ptr = find_word_start(tmp_ptr);
3478 }
3479 // Find end of this word.
3480 tmp_ptr = find_word_end(tmp_ptr);
3481 len = (int)(tmp_ptr - ptr);
3482
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003483 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003484 {
3485 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3486 {
3487 // Try next line, if any. the new word will be
3488 // "join" as if the normal command "J" was used.
3489 // IOSIZE is always greater than
3490 // compl_length, so the next STRNCPY always
3491 // works -- Acevedo
3492 STRNCPY(IObuff, ptr, len);
3493 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3494 tmp_ptr = ptr = skipwhite(ptr);
3495 // Find start of next word.
3496 tmp_ptr = find_word_start(tmp_ptr);
3497 // Find end of next word.
3498 tmp_ptr = find_word_end(tmp_ptr);
3499 if (tmp_ptr > ptr)
3500 {
3501 if (*ptr != ')' && IObuff[len - 1] != TAB)
3502 {
3503 if (IObuff[len - 1] != ' ')
3504 IObuff[len++] = ' ';
3505 // IObuf =~ "\k.* ", thus len >= 2
3506 if (p_js
3507 && (IObuff[len - 2] == '.'
3508 || (vim_strchr(p_cpo, CPO_JOINSP)
3509 == NULL
3510 && (IObuff[len - 2] == '?'
3511 || IObuff[len - 2] == '!'))))
3512 IObuff[len++] = ' ';
3513 }
3514 // copy as much as possible of the new word
3515 if (tmp_ptr - ptr >= IOSIZE - len)
3516 tmp_ptr = ptr + IOSIZE - len - 1;
3517 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3518 len += (int)(tmp_ptr - ptr);
3519 *cont_s_ipos = TRUE;
3520 }
3521 IObuff[len] = NUL;
3522 ptr = IObuff;
3523 }
3524 if (len == compl_length)
3525 return NULL;
3526 }
3527 }
3528
3529 *match_len = len;
3530 return ptr;
3531}
3532
3533/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003534 * Get the next set of words matching "compl_pattern" for default completion(s)
3535 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003536 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3537 * position "st->start_pos" in the "compl_direction" direction. If
3538 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3539 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003540 * Returns OK if a new next match is found, otherwise returns FAIL.
3541 */
3542 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003543get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003544{
3545 int found_new_match = FAIL;
3546 int save_p_scs;
3547 int save_p_ws;
3548 int looped_around = FALSE;
3549 char_u *ptr;
3550 int len;
3551
3552 // If 'infercase' is set, don't use 'smartcase' here
3553 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003554 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003555 p_scs = FALSE;
3556
3557 // Buffers other than curbuf are scanned from the beginning or the
3558 // end but never from the middle, thus setting nowrapscan in this
3559 // buffer is a good idea, on the other hand, we always set
3560 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3561 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003562 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003564 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003565 p_ws = TRUE;
3566 looped_around = FALSE;
3567 for (;;)
3568 {
3569 int cont_s_ipos = FALSE;
3570
3571 ++msg_silent; // Don't want messages for wrapscan.
3572
3573 // ctrl_x_mode_line_or_eval() || word-wise search that
3574 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003575 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003576 found_new_match = search_for_exact_line(st->ins_buf,
3577 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003578 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003579 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3580 NULL, compl_direction, compl_pattern, 1L,
3581 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003582 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003583 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003584 {
3585 // set "compl_started" even on fail
3586 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003587 st->first_match_pos = *st->cur_match_pos;
3588 st->last_match_pos = *st->cur_match_pos;
3589 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003590 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003591 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3592 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003593 {
3594 found_new_match = FAIL;
3595 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003596 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003597 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3598 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3599 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003600 {
3601 if (looped_around)
3602 found_new_match = FAIL;
3603 else
3604 looped_around = TRUE;
3605 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003606 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003607 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3608 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3609 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 {
3611 if (looped_around)
3612 found_new_match = FAIL;
3613 else
3614 looped_around = TRUE;
3615 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003616 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003617 if (found_new_match == FAIL)
3618 break;
3619
3620 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003621 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003622 && start_pos->lnum == st->cur_match_pos->lnum
3623 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003625
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003626 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3627 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003628 if (ptr == NULL)
3629 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003631 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003632 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003633 0, cont_s_ipos) != NOTDONE)
3634 {
3635 found_new_match = OK;
3636 break;
3637 }
3638 }
3639 p_scs = save_p_scs;
3640 p_ws = save_p_ws;
3641
3642 return found_new_match;
3643}
3644
3645/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003646 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003647 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003648 */
3649 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003650get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003651{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003652 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003653
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003654 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003655 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003656 case -1:
3657 break;
3658#ifdef FEAT_FIND_ID
3659 case CTRL_X_PATH_PATTERNS:
3660 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003661 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003662 break;
3663#endif
3664
3665 case CTRL_X_DICTIONARY:
3666 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003667 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3668 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003669 break;
3670
3671 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003673 break;
3674
3675 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003677 break;
3678
3679 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003680 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003681 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003682 break;
3683
3684#ifdef FEAT_COMPL_FUNC
3685 case CTRL_X_FUNCTION:
3686 case CTRL_X_OMNI:
3687 expand_by_function(type, compl_pattern);
3688 break;
3689#endif
3690
3691 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003692 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003693 break;
3694
3695 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003696 found_new_match = get_next_default_completion(st, ini);
3697 if (found_new_match == FAIL && st->ins_buf == curbuf)
3698 st->found_all = TRUE;
3699 }
3700
3701 // check if compl_curr_match has changed, (e.g. other type of
3702 // expansion added something)
3703 if (type != 0 && compl_curr_match != compl_old_match)
3704 found_new_match = OK;
3705
3706 return found_new_match;
3707}
3708
3709/*
3710 * Get the next expansion(s), using "compl_pattern".
3711 * The search starts at position "ini" in curbuf and in the direction
3712 * compl_direction.
3713 * When "compl_started" is FALSE start at that position, otherwise continue
3714 * where we stopped searching before.
3715 * This may return before finding all the matches.
3716 * Return the total number of matches or -1 if still unknown -- Acevedo
3717 */
3718 static int
3719ins_compl_get_exp(pos_T *ini)
3720{
3721 static ins_compl_next_state_T st;
3722 int i;
3723 int found_new_match;
3724 int type = ctrl_x_mode;
3725
3726 if (!compl_started)
3727 {
3728 FOR_ALL_BUFFERS(st.ins_buf)
3729 st.ins_buf->b_scanned = 0;
3730 st.found_all = FALSE;
3731 st.ins_buf = curbuf;
3732 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3733 ? (char_u *)"." : curbuf->b_p_cpt;
3734 st.last_match_pos = st.first_match_pos = *ini;
3735 }
3736 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3737 st.ins_buf = curbuf; // In case the buffer was wiped out.
3738
3739 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003740 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003741 ? &st.last_match_pos : &st.first_match_pos;
3742
3743 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3744 for (;;)
3745 {
3746 found_new_match = FAIL;
3747 st.set_match_pos = FALSE;
3748
3749 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3750 // or if found_all says this entry is done. For ^X^L only use the
3751 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003752 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003753 && (!compl_started || st.found_all))
3754 {
3755 int status = process_next_cpt_value(&st, &type, ini);
3756
3757 if (status == INS_COMPL_CPT_END)
3758 break;
3759 if (status == INS_COMPL_CPT_CONT)
3760 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003761 }
3762
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003763 // If complete() was called then compl_pattern has been reset. The
3764 // following won't work then, bail out.
3765 if (compl_pattern == NULL)
3766 break;
3767
3768 // get the next set of completion matches
3769 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003770
3771 // break the loop for specialized modes (use 'complete' just for the
3772 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3773 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003774 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3775 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003776 {
3777 if (got_int)
3778 break;
3779 // Fill the popup menu as soon as possible.
3780 if (type != -1)
3781 ins_compl_check_keys(0, FALSE);
3782
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003783 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003784 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3785 break;
3786 compl_started = TRUE;
3787 }
3788 else
3789 {
3790 // Mark a buffer scanned when it has been scanned completely
3791 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003792 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003793
3794 compl_started = FALSE;
3795 }
3796 }
3797 compl_started = TRUE;
3798
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003799 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003800 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003801 found_new_match = FAIL;
3802
3803 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003804 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003805 && !ctrl_x_mode_line_or_eval()))
3806 i = ins_compl_make_cyclic();
3807
3808 if (compl_old_match != NULL)
3809 {
3810 // If several matches were added (FORWARD) or the search failed and has
3811 // just been made cyclic then we have to move compl_curr_match to the
3812 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003813 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003814 : compl_old_match->cp_prev;
3815 if (compl_curr_match == NULL)
3816 compl_curr_match = compl_old_match;
3817 }
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003818 trigger_modechanged();
3819
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003820 return i;
3821}
3822
3823/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003824 * Update "compl_shown_match" to the actually shown match, it may differ when
3825 * "compl_leader" is used to omit some of the matches.
3826 */
3827 static void
3828ins_compl_update_shown_match(void)
3829{
3830 while (!ins_compl_equal(compl_shown_match,
3831 compl_leader, (int)STRLEN(compl_leader))
3832 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003833 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003834 compl_shown_match = compl_shown_match->cp_next;
3835
3836 // If we didn't find it searching forward, and compl_shows_dir is
3837 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003838 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003839 && !ins_compl_equal(compl_shown_match,
3840 compl_leader, (int)STRLEN(compl_leader))
3841 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003842 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003843 {
3844 while (!ins_compl_equal(compl_shown_match,
3845 compl_leader, (int)STRLEN(compl_leader))
3846 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003847 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003848 compl_shown_match = compl_shown_match->cp_prev;
3849 }
3850}
3851
3852/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003853 * Delete the old text being completed.
3854 */
3855 void
3856ins_compl_delete(void)
3857{
3858 int col;
3859
3860 // In insert mode: Delete the typed part.
3861 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003862 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003863 if ((int)curwin->w_cursor.col > col)
3864 {
3865 if (stop_arrow() == FAIL)
3866 return;
3867 backspace_until_column(col);
3868 }
3869
3870 // TODO: is this sufficient for redrawing? Redrawing everything causes
3871 // flicker, thus we can't do that.
3872 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003873#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003874 // clear v:completed_item
3875 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003876#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003877}
3878
3879/*
3880 * Insert the new text being completed.
3881 * "in_compl_func" is TRUE when called from complete_check().
3882 */
3883 void
3884ins_compl_insert(int in_compl_func)
3885{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003886 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003887
3888 // Make sure we don't go over the end of the string, this can happen with
3889 // illegal bytes.
3890 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3891 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003892 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003893 compl_used_match = FALSE;
3894 else
3895 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003896#ifdef FEAT_EVAL
3897 {
3898 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3899
3900 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3901 }
3902#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003903 if (!in_compl_func)
3904 compl_curr_match = compl_shown_match;
3905}
3906
3907/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003908 * show the file name for the completion match (if any). Truncate the file
3909 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003910 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003911 static void
3912ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003913{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003914 char *lead = _("match in file");
3915 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3916 char_u *s;
3917 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003918
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003919 if (space <= 0)
3920 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003921
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003922 // We need the tail that fits. With double-byte encoding going
3923 // back from the end is very slow, thus go from the start and keep
3924 // the text that fits in "space" between "s" and "e".
3925 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003926 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003927 space -= ptr2cells(e);
3928 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003929 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003930 space += ptr2cells(s);
3931 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003932 }
3933 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003934 msg_hist_off = TRUE;
3935 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3936 s > compl_shown_match->cp_fname ? "<" : "", s);
3937 msg((char *)IObuff);
3938 msg_hist_off = FALSE;
3939 redraw_cmdline = FALSE; // don't overwrite!
3940}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003941
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003942/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003943 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003944 * times. The number of matches found is returned in 'num_matches'.
3945 *
3946 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3947 * get more completions. If it is FALSE, then do nothing when there are no more
3948 * completions in the given direction.
3949 *
3950 * If "advance" is TRUE, then completion will move to the first match.
3951 * Otherwise, the original text will be shown.
3952 *
3953 * Returns OK on success and -1 if the number of matches are unknown.
3954 */
3955 static int
3956find_next_completion_match(
3957 int allow_get_expansion,
3958 int todo, // repeat completion this many times
3959 int advance,
3960 int *num_matches)
3961{
3962 int found_end = FALSE;
3963 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003964
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003965 while (--todo >= 0)
3966 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003967 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003968 {
3969 compl_shown_match = compl_shown_match->cp_next;
3970 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003971 && (is_first_match(compl_shown_match->cp_next)
3972 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003973 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003974 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003975 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003976 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003977 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003978 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003979 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003980 }
3981 else
3982 {
3983 if (!allow_get_expansion)
3984 {
3985 if (advance)
3986 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003987 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003988 compl_pending -= todo + 1;
3989 else
3990 compl_pending += todo + 1;
3991 }
3992 return -1;
3993 }
3994
3995 if (!compl_no_select && advance)
3996 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003997 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003998 --compl_pending;
3999 else
4000 ++compl_pending;
4001 }
4002
4003 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004004 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004005
4006 // handle any pending completions
4007 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004008 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004009 {
4010 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4011 {
4012 compl_shown_match = compl_shown_match->cp_next;
4013 --compl_pending;
4014 }
4015 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4016 {
4017 compl_shown_match = compl_shown_match->cp_prev;
4018 ++compl_pending;
4019 }
4020 else
4021 break;
4022 }
4023 found_end = FALSE;
4024 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004025 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004026 && compl_leader != NULL
4027 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004028 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004029 ++todo;
4030 else
4031 // Remember a matching item.
4032 found_compl = compl_shown_match;
4033
4034 // Stop at the end of the list when we found a usable match.
4035 if (found_end)
4036 {
4037 if (found_compl != NULL)
4038 {
4039 compl_shown_match = found_compl;
4040 break;
4041 }
4042 todo = 1; // use first usable match after wrapping around
4043 }
4044 }
4045
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004046 return OK;
4047}
4048
4049/*
4050 * Fill in the next completion in the current direction.
4051 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4052 * get more completions. If it is FALSE, then we just do nothing when there
4053 * are no more completions in a given direction. The latter case is used when
4054 * we are still in the middle of finding completions, to allow browsing
4055 * through the ones found so far.
4056 * Return the total number of matches, or -1 if still unknown -- webb.
4057 *
4058 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4059 * compl_shown_match here.
4060 *
4061 * Note that this function may be called recursively once only. First with
4062 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4063 * calls this function with "allow_get_expansion" FALSE.
4064 */
4065 static int
4066ins_compl_next(
4067 int allow_get_expansion,
4068 int count, // repeat completion this many times; should
4069 // be at least 1
4070 int insert_match, // Insert the newly selected match
4071 int in_compl_func) // called from complete_check()
4072{
4073 int num_matches = -1;
4074 int todo = count;
4075 int advance;
4076 int started = compl_started;
4077
4078 // When user complete function return -1 for findstart which is next
4079 // time of 'always', compl_shown_match become NULL.
4080 if (compl_shown_match == NULL)
4081 return -1;
4082
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004083 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004084 // Update "compl_shown_match" to the actually shown match
4085 ins_compl_update_shown_match();
4086
4087 if (allow_get_expansion && insert_match
4088 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4089 // Delete old text to be replaced
4090 ins_compl_delete();
4091
4092 // When finding the longest common text we stick at the original text,
4093 // don't let CTRL-N or CTRL-P move to the first match.
4094 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4095
4096 // When restarting the search don't insert the first match either.
4097 if (compl_restarting)
4098 {
4099 advance = FALSE;
4100 compl_restarting = FALSE;
4101 }
4102
4103 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4104 // around.
4105 if (find_next_completion_match(allow_get_expansion, todo, advance,
4106 &num_matches) == -1)
4107 return -1;
4108
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004109 // Insert the text of the new completion, or the compl_leader.
4110 if (compl_no_insert && !started)
4111 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004112 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004113 compl_used_match = FALSE;
4114 }
4115 else if (insert_match)
4116 {
4117 if (!compl_get_longest || compl_used_match)
4118 ins_compl_insert(in_compl_func);
4119 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004120 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004121 }
4122 else
4123 compl_used_match = FALSE;
4124
4125 if (!allow_get_expansion)
4126 {
4127 // may undisplay the popup menu first
4128 ins_compl_upd_pum();
4129
4130 if (pum_enough_matches())
4131 // Will display the popup menu, don't redraw yet to avoid flicker.
4132 pum_call_update_screen();
4133 else
4134 // Not showing the popup menu yet, redraw to show the user what was
4135 // inserted.
4136 update_screen(0);
4137
4138 // display the updated popup menu
4139 ins_compl_show_pum();
4140#ifdef FEAT_GUI
4141 if (gui.in_use)
4142 {
4143 // Show the cursor after the match, not after the redrawn text.
4144 setcursor();
4145 out_flush_cursor(FALSE, FALSE);
4146 }
4147#endif
4148
4149 // Delete old text to be replaced, since we're still searching and
4150 // don't want to match ourselves!
4151 ins_compl_delete();
4152 }
4153
4154 // Enter will select a match when the match wasn't inserted and the popup
4155 // menu is visible.
4156 if (compl_no_insert && !started)
4157 compl_enter_selects = TRUE;
4158 else
4159 compl_enter_selects = !insert_match && compl_match_array != NULL;
4160
4161 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004162 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004163 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004164
4165 return num_matches;
4166}
4167
4168/*
4169 * Call this while finding completions, to check whether the user has hit a key
4170 * that should change the currently displayed completion, or exit completion
4171 * mode. Also, when compl_pending is not zero, show a completion as soon as
4172 * possible. -- webb
4173 * "frequency" specifies out of how many calls we actually check.
4174 * "in_compl_func" is TRUE when called from complete_check(), don't set
4175 * compl_curr_match.
4176 */
4177 void
4178ins_compl_check_keys(int frequency, int in_compl_func)
4179{
4180 static int count = 0;
4181 int c;
4182
4183 // Don't check when reading keys from a script, :normal or feedkeys().
4184 // That would break the test scripts. But do check for keys when called
4185 // from complete_check().
4186 if (!in_compl_func && (using_script() || ex_normal_busy))
4187 return;
4188
4189 // Only do this at regular intervals
4190 if (++count < frequency)
4191 return;
4192 count = 0;
4193
4194 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4195 // can't do its work correctly.
4196 c = vpeekc_any();
4197 if (c != NUL)
4198 {
4199 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4200 {
4201 c = safe_vgetc(); // Eat the character
4202 compl_shows_dir = ins_compl_key2dir(c);
4203 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4204 c != K_UP && c != K_DOWN, in_compl_func);
4205 }
4206 else
4207 {
4208 // Need to get the character to have KeyTyped set. We'll put it
4209 // back with vungetc() below. But skip K_IGNORE.
4210 c = safe_vgetc();
4211 if (c != K_IGNORE)
4212 {
4213 // Don't interrupt completion when the character wasn't typed,
4214 // e.g., when doing @q to replay keys.
4215 if (c != Ctrl_R && KeyTyped)
4216 compl_interrupted = TRUE;
4217
4218 vungetc(c);
4219 }
4220 }
4221 }
4222 if (compl_pending != 0 && !got_int && !compl_no_insert)
4223 {
4224 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4225
4226 compl_pending = 0;
4227 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4228 }
4229}
4230
4231/*
4232 * Decide the direction of Insert mode complete from the key typed.
4233 * Returns BACKWARD or FORWARD.
4234 */
4235 static int
4236ins_compl_key2dir(int c)
4237{
4238 if (c == Ctrl_P || c == Ctrl_L
4239 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4240 return BACKWARD;
4241 return FORWARD;
4242}
4243
4244/*
4245 * Return TRUE for keys that are used for completion only when the popup menu
4246 * is visible.
4247 */
4248 static int
4249ins_compl_pum_key(int c)
4250{
4251 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4252 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4253 || c == K_UP || c == K_DOWN);
4254}
4255
4256/*
4257 * Decide the number of completions to move forward.
4258 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4259 */
4260 static int
4261ins_compl_key2count(int c)
4262{
4263 int h;
4264
4265 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4266 {
4267 h = pum_get_height();
4268 if (h > 3)
4269 h -= 2; // keep some context
4270 return h;
4271 }
4272 return 1;
4273}
4274
4275/*
4276 * Return TRUE if completion with "c" should insert the match, FALSE if only
4277 * to change the currently selected completion.
4278 */
4279 static int
4280ins_compl_use_match(int c)
4281{
4282 switch (c)
4283 {
4284 case K_UP:
4285 case K_DOWN:
4286 case K_PAGEDOWN:
4287 case K_KPAGEDOWN:
4288 case K_S_DOWN:
4289 case K_PAGEUP:
4290 case K_KPAGEUP:
4291 case K_S_UP:
4292 return FALSE;
4293 }
4294 return TRUE;
4295}
4296
4297/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004298 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4299 * completion)
4300 * Sets the global variables: compl_col, compl_length and compl_pattern.
4301 * Uses the global variables: compl_cont_status and ctrl_x_mode
4302 */
4303 static int
4304get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4305{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004306 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004307 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004308 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004309 {
4310 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4311 ;
4312 compl_col += ++startcol;
4313 compl_length = curs_col - startcol;
4314 }
4315 if (p_ic)
4316 compl_pattern = str_foldcase(line + compl_col,
4317 compl_length, NULL, 0);
4318 else
4319 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4320 if (compl_pattern == NULL)
4321 return FAIL;
4322 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004323 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004324 {
4325 char_u *prefix = (char_u *)"\\<";
4326
4327 // we need up to 2 extra chars for the prefix
4328 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4329 compl_length) + 2);
4330 if (compl_pattern == NULL)
4331 return FAIL;
4332 if (!vim_iswordp(line + compl_col)
4333 || (compl_col > 0
4334 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4335 prefix = (char_u *)"";
4336 STRCPY((char *)compl_pattern, prefix);
4337 (void)quote_meta(compl_pattern + STRLEN(prefix),
4338 line + compl_col, compl_length);
4339 }
4340 else if (--startcol < 0
4341 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4342 {
4343 // Match any word of at least two chars
4344 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4345 if (compl_pattern == NULL)
4346 return FAIL;
4347 compl_col += curs_col;
4348 compl_length = 0;
4349 }
4350 else
4351 {
4352 // Search the point of change class of multibyte character
4353 // or not a word single byte character backward.
4354 if (has_mbyte)
4355 {
4356 int base_class;
4357 int head_off;
4358
4359 startcol -= (*mb_head_off)(line, line + startcol);
4360 base_class = mb_get_class(line + startcol);
4361 while (--startcol >= 0)
4362 {
4363 head_off = (*mb_head_off)(line, line + startcol);
4364 if (base_class != mb_get_class(line + startcol
4365 - head_off))
4366 break;
4367 startcol -= head_off;
4368 }
4369 }
4370 else
4371 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4372 ;
4373 compl_col += ++startcol;
4374 compl_length = (int)curs_col - startcol;
4375 if (compl_length == 1)
4376 {
4377 // Only match word with at least two chars -- webb
4378 // there's no need to call quote_meta,
4379 // alloc(7) is enough -- Acevedo
4380 compl_pattern = alloc(7);
4381 if (compl_pattern == NULL)
4382 return FAIL;
4383 STRCPY((char *)compl_pattern, "\\<");
4384 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4385 STRCAT((char *)compl_pattern, "\\k");
4386 }
4387 else
4388 {
4389 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4390 compl_length) + 2);
4391 if (compl_pattern == NULL)
4392 return FAIL;
4393 STRCPY((char *)compl_pattern, "\\<");
4394 (void)quote_meta(compl_pattern + 2, line + compl_col,
4395 compl_length);
4396 }
4397 }
4398
4399 return OK;
4400}
4401
4402/*
4403 * Get the pattern, column and length for whole line completion or for the
4404 * complete() function.
4405 * Sets the global variables: compl_col, compl_length and compl_pattern.
4406 */
4407 static int
4408get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4409{
4410 compl_col = (colnr_T)getwhitecols(line);
4411 compl_length = (int)curs_col - (int)compl_col;
4412 if (compl_length < 0) // cursor in indent: empty pattern
4413 compl_length = 0;
4414 if (p_ic)
4415 compl_pattern = str_foldcase(line + compl_col, compl_length,
4416 NULL, 0);
4417 else
4418 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4419 if (compl_pattern == NULL)
4420 return FAIL;
4421
4422 return OK;
4423}
4424
4425/*
4426 * Get the pattern, column and length for filename completion.
4427 * Sets the global variables: compl_col, compl_length and compl_pattern.
4428 */
4429 static int
4430get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4431{
4432 // Go back to just before the first filename character.
4433 if (startcol > 0)
4434 {
4435 char_u *p = line + startcol;
4436
4437 MB_PTR_BACK(line, p);
4438 while (p > line && vim_isfilec(PTR2CHAR(p)))
4439 MB_PTR_BACK(line, p);
4440 if (p == line && vim_isfilec(PTR2CHAR(p)))
4441 startcol = 0;
4442 else
4443 startcol = (int)(p - line) + 1;
4444 }
4445
4446 compl_col += startcol;
4447 compl_length = (int)curs_col - startcol;
4448 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4449 if (compl_pattern == NULL)
4450 return FAIL;
4451
4452 return OK;
4453}
4454
4455/*
4456 * Get the pattern, column and length for command-line completion.
4457 * Sets the global variables: compl_col, compl_length and compl_pattern.
4458 */
4459 static int
4460get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4461{
4462 compl_pattern = vim_strnsave(line, curs_col);
4463 if (compl_pattern == NULL)
4464 return FAIL;
4465 set_cmd_context(&compl_xp, compl_pattern,
4466 (int)STRLEN(compl_pattern), curs_col, FALSE);
4467 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4468 || compl_xp.xp_context == EXPAND_NOTHING)
4469 // No completion possible, use an empty pattern to get a
4470 // "pattern not found" message.
4471 compl_col = curs_col;
4472 else
4473 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4474 compl_length = curs_col - compl_col;
4475
4476 return OK;
4477}
4478
4479/*
4480 * Get the pattern, column and length for user defined completion ('omnifunc',
4481 * 'completefunc' and 'thesaurusfunc')
4482 * Sets the global variables: compl_col, compl_length and compl_pattern.
4483 * Uses the global variable: spell_bad_len
4484 */
4485 static int
4486get_userdefined_compl_info(colnr_T curs_col UNUSED)
4487{
4488 int ret = FAIL;
4489
4490#ifdef FEAT_COMPL_FUNC
4491 // Call user defined function 'completefunc' with "a:findstart"
4492 // set to 1 to obtain the length of text to use for completion.
4493 char_u *line;
4494 typval_T args[3];
4495 int col;
4496 char_u *funcname;
4497 pos_T pos;
4498 int save_State = State;
4499 callback_T *cb;
4500
4501 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4502 // length as a string
4503 funcname = get_complete_funcname(ctrl_x_mode);
4504 if (*funcname == NUL)
4505 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004506 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004507 ? "completefunc" : "omnifunc");
4508 return FAIL;
4509 }
4510
4511 args[0].v_type = VAR_NUMBER;
4512 args[0].vval.v_number = 1;
4513 args[1].v_type = VAR_STRING;
4514 args[1].vval.v_string = (char_u *)"";
4515 args[2].v_type = VAR_UNKNOWN;
4516 pos = curwin->w_cursor;
4517 ++textwinlock;
4518 cb = get_insert_callback(ctrl_x_mode);
4519 col = call_callback_retnr(cb, 2, args);
4520 --textwinlock;
4521
4522 State = save_State;
4523 curwin->w_cursor = pos; // restore the cursor position
4524 validate_cursor();
4525 if (!EQUAL_POS(curwin->w_cursor, pos))
4526 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004527 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004528 return FAIL;
4529 }
4530
4531 // Return value -2 means the user complete function wants to
4532 // cancel the complete without an error.
4533 // Return value -3 does the same as -2 and leaves CTRL-X mode.
4534 if (col == -2)
4535 return FAIL;
4536 if (col == -3)
4537 {
4538 ctrl_x_mode = CTRL_X_NORMAL;
4539 edit_submode = NULL;
4540 if (!shortmess(SHM_COMPLETIONMENU))
4541 msg_clr_cmdline();
4542 return FAIL;
4543 }
4544
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004545 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004546 // completion.
4547 compl_opt_refresh_always = FALSE;
4548 compl_opt_suppress_empty = FALSE;
4549
4550 if (col < 0)
4551 col = curs_col;
4552 compl_col = col;
4553 if (compl_col > curs_col)
4554 compl_col = curs_col;
4555
4556 // Setup variables for completion. Need to obtain "line" again,
4557 // it may have become invalid.
4558 line = ml_get(curwin->w_cursor.lnum);
4559 compl_length = curs_col - compl_col;
4560 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4561 if (compl_pattern == NULL)
4562 return FAIL;
4563
4564 ret = OK;
4565#endif
4566
4567 return ret;
4568}
4569
4570/*
4571 * Get the pattern, column and length for spell completion.
4572 * Sets the global variables: compl_col, compl_length and compl_pattern.
4573 * Uses the global variable: spell_bad_len
4574 */
4575 static int
4576get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4577{
4578 int ret = FAIL;
4579#ifdef FEAT_SPELL
4580 char_u *line;
4581
4582 if (spell_bad_len > 0)
4583 compl_col = curs_col - spell_bad_len;
4584 else
4585 compl_col = spell_word_start(startcol);
4586 if (compl_col >= (colnr_T)startcol)
4587 {
4588 compl_length = 0;
4589 compl_col = curs_col;
4590 }
4591 else
4592 {
4593 spell_expand_check_cap(compl_col);
4594 compl_length = (int)curs_col - compl_col;
4595 }
4596 // Need to obtain "line" again, it may have become invalid.
4597 line = ml_get(curwin->w_cursor.lnum);
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 completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004610 * "startcol" - start column number of the completion pattern/text
4611 * "cur_col" - current cursor column
4612 * On return, "line_invalid" is set to TRUE, if the current line may have
4613 * become invalid and needs to be fetched again.
4614 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004615 */
4616 static int
4617compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4618{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004619 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004620 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4621 && !thesaurus_func_complete(ctrl_x_mode)))
4622 {
4623 return get_normal_compl_info(line, startcol, curs_col);
4624 }
4625 else if (ctrl_x_mode_line_or_eval())
4626 {
4627 return get_wholeline_compl_info(line, curs_col);
4628 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004629 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004630 {
4631 return get_filename_compl_info(line, startcol, curs_col);
4632 }
4633 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4634 {
4635 return get_cmdline_compl_info(line, curs_col);
4636 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004637 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004638 || thesaurus_func_complete(ctrl_x_mode))
4639 {
4640 if (get_userdefined_compl_info(curs_col) == FAIL)
4641 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004642 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004643 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004644 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004645 {
4646 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4647 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004648 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004649 }
4650 else
4651 {
4652 internal_error("ins_complete()");
4653 return FAIL;
4654 }
4655
4656 return OK;
4657}
4658
4659/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004660 * Continue an interrupted completion mode search in "line".
4661 *
4662 * If this same ctrl_x_mode has been interrupted use the text from
4663 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4664 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4665 * the same line as the cursor then fix it (the line has been split because it
4666 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4667 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004668 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004669 static void
4670ins_compl_continue_search(char_u *line)
4671{
4672 // it is a continued search
4673 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004674 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4675 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004676 {
4677 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4678 {
4679 // line (probably) wrapped, set compl_startpos to the
4680 // first non_blank in the line, if it is not a wordchar
4681 // include it to get a better pattern, but then we don't
4682 // want the "\\<" prefix, check it below
4683 compl_col = (colnr_T)getwhitecols(line);
4684 compl_startpos.col = compl_col;
4685 compl_startpos.lnum = curwin->w_cursor.lnum;
4686 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4687 }
4688 else
4689 {
4690 // S_IPOS was set when we inserted a word that was at the
4691 // beginning of the line, which means that we'll go to SOL
4692 // mode but first we need to redefine compl_startpos
4693 if (compl_cont_status & CONT_S_IPOS)
4694 {
4695 compl_cont_status |= CONT_SOL;
4696 compl_startpos.col = (colnr_T)(skipwhite(
4697 line + compl_length
4698 + compl_startpos.col) - line);
4699 }
4700 compl_col = compl_startpos.col;
4701 }
4702 compl_length = curwin->w_cursor.col - (int)compl_col;
4703 // IObuff is used to add a "word from the next line" would we
4704 // have enough space? just being paranoid
4705#define MIN_SPACE 75
4706 if (compl_length > (IOSIZE - MIN_SPACE))
4707 {
4708 compl_cont_status &= ~CONT_SOL;
4709 compl_length = (IOSIZE - MIN_SPACE);
4710 compl_col = curwin->w_cursor.col - compl_length;
4711 }
4712 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4713 if (compl_length < 1)
4714 compl_cont_status &= CONT_LOCAL;
4715 }
4716 else if (ctrl_x_mode_line_or_eval())
4717 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4718 else
4719 compl_cont_status = 0;
4720}
4721
4722/*
4723 * start insert mode completion
4724 */
4725 static int
4726ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004727{
4728 char_u *line;
4729 int startcol = 0; // column where searched text starts
4730 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004731 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004732 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004733 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004734
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004735 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004736
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004737 did_ai = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004738#ifdef FEAT_SMARTINDENT
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004739 did_si = FALSE;
4740 can_si = FALSE;
4741 can_si_back = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004742#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004743 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004744 return FAIL;
4745
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004746 line = ml_get(curwin->w_cursor.lnum);
4747 curs_col = curwin->w_cursor.col;
4748 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004749
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004750 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4751 && compl_cont_mode == ctrl_x_mode)
4752 // this same ctrl-x_mode was interrupted previously. Continue the
4753 // completion.
4754 ins_compl_continue_search(line);
4755 else
4756 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004757
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004758 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004759 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004760 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004761 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004762 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4763 compl_cont_status = 0;
4764 compl_cont_status |= CONT_N_ADDS;
4765 compl_startpos = curwin->w_cursor;
4766 startcol = (int)curs_col;
4767 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004768 }
4769
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004770 // Work out completion pattern and original text -- webb
4771 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4772 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004773 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4774 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004775 // restore did_ai, so that adding comment leader works
4776 did_ai = save_did_ai;
4777 return FAIL;
4778 }
4779 // If "line" was changed while getting completion info get it again.
4780 if (line_invalid)
4781 line = ml_get(curwin->w_cursor.lnum);
4782
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004783 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004784 {
4785 edit_submode_pre = (char_u *)_(" Adding");
4786 if (ctrl_x_mode_line_or_eval())
4787 {
4788 // Insert a new line, keep indentation but ignore 'comments'.
4789 char_u *old = curbuf->b_p_com;
4790
4791 curbuf->b_p_com = (char_u *)"";
4792 compl_startpos.lnum = curwin->w_cursor.lnum;
4793 compl_startpos.col = compl_col;
4794 ins_eol('\r');
4795 curbuf->b_p_com = old;
4796 compl_length = 0;
4797 compl_col = curwin->w_cursor.col;
4798 }
4799 }
4800 else
4801 {
4802 edit_submode_pre = NULL;
4803 compl_startpos.col = compl_col;
4804 }
4805
4806 if (compl_cont_status & CONT_LOCAL)
4807 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4808 else
4809 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4810
4811 // If any of the original typed text has been changed we need to fix
4812 // the redo buffer.
4813 ins_compl_fixRedoBufForLeader(NULL);
4814
4815 // Always add completion for the original text.
4816 vim_free(compl_orig_text);
4817 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4818 if (p_ic)
4819 flags |= CP_ICASE;
4820 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4821 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4822 {
4823 VIM_CLEAR(compl_pattern);
4824 VIM_CLEAR(compl_orig_text);
4825 return FAIL;
4826 }
4827
4828 // showmode might reset the internal line pointers, so it must
4829 // be called before line = ml_get(), or when this address is no
4830 // longer needed. -- Acevedo.
4831 edit_submode_extra = (char_u *)_("-- Searching...");
4832 edit_submode_highl = HLF_COUNT;
4833 showmode();
4834 edit_submode_extra = NULL;
4835 out_flush();
4836
4837 return OK;
4838}
4839
4840/*
4841 * display the completion status message
4842 */
4843 static void
4844ins_compl_show_statusmsg(void)
4845{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004846 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004847 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004848 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004849 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004850 ? (char_u *)_(e_hitend)
4851 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004852 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004853 }
4854
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004855 if (edit_submode_extra == NULL)
4856 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004857 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004858 {
4859 edit_submode_extra = (char_u *)_("Back at original");
4860 edit_submode_highl = HLF_W;
4861 }
4862 else if (compl_cont_status & CONT_S_IPOS)
4863 {
4864 edit_submode_extra = (char_u *)_("Word from other line");
4865 edit_submode_highl = HLF_COUNT;
4866 }
4867 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4868 {
4869 edit_submode_extra = (char_u *)_("The only match");
4870 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004871 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004872 }
4873 else
4874 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004875#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876 // Update completion sequence number when needed.
4877 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004878 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004879#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004880 // The match should always have a sequence number now, this is
4881 // just a safety check.
4882 if (compl_curr_match->cp_number != -1)
4883 {
4884 // Space for 10 text chars. + 2x10-digit no.s = 31.
4885 // Translations may need more than twice that.
4886 static char_u match_ref[81];
4887
4888 if (compl_matches > 0)
4889 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004890 _("match %d of %d"),
4891 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004892 else
4893 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004894 _("match %d"),
4895 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004896 edit_submode_extra = match_ref;
4897 edit_submode_highl = HLF_R;
4898 if (dollar_vcol >= 0)
4899 curs_columns(FALSE);
4900 }
4901 }
4902 }
4903
4904 // Show a message about what (completion) mode we're in.
4905 if (!compl_opt_suppress_empty)
4906 {
4907 showmode();
4908 if (!shortmess(SHM_COMPLETIONMENU))
4909 {
4910 if (edit_submode_extra != NULL)
4911 {
4912 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004913 {
4914 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004915 msg_attr((char *)edit_submode_extra,
4916 edit_submode_highl < HLF_COUNT
4917 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004918 msg_hist_off = FALSE;
4919 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004920 }
4921 else
4922 msg_clr_cmdline(); // necessary for "noshowmode"
4923 }
4924 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004925}
4926
4927/*
4928 * Do Insert mode completion.
4929 * Called when character "c" was typed, which has a meaning for completion.
4930 * Returns OK if completion was done, FAIL if something failed (out of mem).
4931 */
4932 int
4933ins_complete(int c, int enable_pum)
4934{
4935 int n;
4936 int save_w_wrow;
4937 int save_w_leftcol;
4938 int insert_match;
4939
4940 compl_direction = ins_compl_key2dir(c);
4941 insert_match = ins_compl_use_match(c);
4942
4943 if (!compl_started)
4944 {
4945 if (ins_compl_start() == FAIL)
4946 return FAIL;
4947 }
4948 else if (insert_match && stop_arrow() == FAIL)
4949 return FAIL;
4950
4951 compl_shown_match = compl_curr_match;
4952 compl_shows_dir = compl_direction;
4953
4954 // Find next match (and following matches).
4955 save_w_wrow = curwin->w_wrow;
4956 save_w_leftcol = curwin->w_leftcol;
4957 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4958
4959 // may undisplay the popup menu
4960 ins_compl_upd_pum();
4961
4962 if (n > 1) // all matches have been found
4963 compl_matches = n;
4964 compl_curr_match = compl_shown_match;
4965 compl_direction = compl_shows_dir;
4966
4967 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4968 // mode.
4969 if (got_int && !global_busy)
4970 {
4971 (void)vgetc();
4972 got_int = FALSE;
4973 }
4974
4975 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004976 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004977 {
4978 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4979 // because we couldn't expand anything at first place, but if we used
4980 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4981 // (such as M in M'exico) if not tried already. -- Acevedo
4982 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004983 || compl_status_adding()
4984 || (ctrl_x_mode_not_default()
4985 && !ctrl_x_mode_path_patterns()
4986 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004987 compl_cont_status &= ~CONT_N_ADDS;
4988 }
4989
4990 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4991 compl_cont_status |= CONT_S_IPOS;
4992 else
4993 compl_cont_status &= ~CONT_S_IPOS;
4994
4995 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004996
4997 // Show the popup menu, unless we got interrupted.
4998 if (enable_pum && !compl_interrupted)
4999 show_pum(save_w_wrow, save_w_leftcol);
5000
5001 compl_was_interrupted = compl_interrupted;
5002 compl_interrupted = FALSE;
5003
5004 return OK;
5005}
5006
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005007/*
5008 * Remove (if needed) and show the popup menu
5009 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005010 static void
5011show_pum(int prev_w_wrow, int prev_w_leftcol)
5012{
5013 // RedrawingDisabled may be set when invoked through complete().
5014 int n = RedrawingDisabled;
5015
5016 RedrawingDisabled = 0;
5017
5018 // If the cursor moved or the display scrolled we need to remove the pum
5019 // first.
5020 setcursor();
5021 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5022 ins_compl_del_pum();
5023
5024 ins_compl_show_pum();
5025 setcursor();
5026 RedrawingDisabled = n;
5027}
5028
5029/*
5030 * Looks in the first "len" chars. of "src" for search-metachars.
5031 * If dest is not NULL the chars. are copied there quoting (with
5032 * a backslash) the metachars, and dest would be NUL terminated.
5033 * Returns the length (needed) of dest
5034 */
5035 static unsigned
5036quote_meta(char_u *dest, char_u *src, int len)
5037{
5038 unsigned m = (unsigned)len + 1; // one extra for the NUL
5039
5040 for ( ; --len >= 0; src++)
5041 {
5042 switch (*src)
5043 {
5044 case '.':
5045 case '*':
5046 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005047 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005048 break;
5049 // FALLTHROUGH
5050 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005051 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005052 break;
5053 // FALLTHROUGH
5054 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005055 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005056 break;
5057 // FALLTHROUGH
5058 case '^': // currently it's not needed.
5059 case '$':
5060 m++;
5061 if (dest != NULL)
5062 *dest++ = '\\';
5063 break;
5064 }
5065 if (dest != NULL)
5066 *dest++ = *src;
5067 // Copy remaining bytes of a multibyte character.
5068 if (has_mbyte)
5069 {
5070 int i, mb_len;
5071
5072 mb_len = (*mb_ptr2len)(src) - 1;
5073 if (mb_len > 0 && len >= mb_len)
5074 for (i = 0; i < mb_len; ++i)
5075 {
5076 --len;
5077 ++src;
5078 if (dest != NULL)
5079 *dest++ = *src;
5080 }
5081 }
5082 }
5083 if (dest != NULL)
5084 *dest = NUL;
5085
5086 return m;
5087}
5088
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005089#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005090 void
5091free_insexpand_stuff(void)
5092{
5093 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005094# ifdef FEAT_EVAL
5095 free_callback(&cfu_cb);
5096 free_callback(&ofu_cb);
5097 free_callback(&tsrfu_cb);
5098# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005099}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005100#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005101
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005102#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005103/*
5104 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5105 * spelled word, if there is one.
5106 */
5107 static void
5108spell_back_to_badword(void)
5109{
5110 pos_T tpos = curwin->w_cursor;
5111
5112 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5113 if (curwin->w_cursor.col != tpos.col)
5114 start_arrow(&tpos);
5115}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005116#endif