blob: 4a5feac9dc1f71b4c11fc6b4fdee7d8bf0b8516a [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127
128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000134 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100135 */
136static compl_T *compl_first_match = NULL;
137static compl_T *compl_curr_match = NULL;
138static compl_T *compl_shown_match = NULL;
139static compl_T *compl_old_match = NULL;
140
141// After using a cursor key <Enter> selects a match in the popup menu,
142// otherwise it inserts a line break.
143static int compl_enter_selects = FALSE;
144
145// When "compl_leader" is not NULL only matches that start with this string
146// are used.
147static char_u *compl_leader = NULL;
148
149static int compl_get_longest = FALSE; // put longest common string
150 // in compl_leader
151
152static int compl_no_insert = FALSE; // FALSE: select & insert
153 // TRUE: noinsert
154static int compl_no_select = FALSE; // FALSE: select & insert
155 // TRUE: noselect
156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
266int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
270int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
271int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
272int ctrl_x_mode_path_patterns(void) {
273 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
274int ctrl_x_mode_path_defines(void) {
275 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
276int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
277int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200278int ctrl_x_mode_cmdline(void) {
279 return ctrl_x_mode == CTRL_X_CMDLINE
280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100281int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
282int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
283int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000284static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100285int ctrl_x_mode_line_or_eval(void) {
286 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
287
288/*
289 * Whether other than default completion has been selected.
290 */
291 int
292ctrl_x_mode_not_default(void)
293{
294 return ctrl_x_mode != CTRL_X_NORMAL;
295}
296
297/*
zeertzjqdca29d92021-08-31 19:12:51 +0200298 * Whether CTRL-X was typed without a following character,
299 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300 */
301 int
302ctrl_x_mode_not_defined_yet(void)
303{
304 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
305}
306
307/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000308 * Return TRUE if currently in "normal" or "adding" insert completion matches
309 * state
310 */
311 int
312compl_status_adding(void)
313{
314 return compl_cont_status & CONT_ADDING;
315}
316
317/*
318 * Return TRUE if the completion pattern includes start of line, just for
319 * word-wise expansion.
320 */
321 int
322compl_status_sol(void)
323{
324 return compl_cont_status & CONT_SOL;
325}
326
327/*
328 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
329 */
330 int
331compl_status_local(void)
332{
333 return compl_cont_status & CONT_LOCAL;
334}
335
336/*
337 * Clear the completion status flags
338 */
339 void
340compl_status_clear(void)
341{
342 compl_cont_status = 0;
343}
344
345/*
346 * Return TRUE if completion is using the forward direction matches
347 */
348 static int
349compl_dir_forward(void)
350{
351 return compl_direction == FORWARD;
352}
353
354/*
355 * Return TRUE if currently showing forward completion matches
356 */
357 static int
358compl_shows_dir_forward(void)
359{
360 return compl_shows_dir == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing backward completion matches
365 */
366 static int
367compl_shows_dir_backward(void)
368{
369 return compl_shows_dir == BACKWARD;
370}
371
372/*
373 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100374 */
375 int
376has_compl_option(int dict_opt)
377{
378 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200379#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100380 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200381#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100382 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100383 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
384#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100385 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386#endif
387 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 {
389 ctrl_x_mode = CTRL_X_NORMAL;
390 edit_submode = NULL;
391 msg_attr(dict_opt ? _("'dictionary' option is empty")
392 : _("'thesaurus' option is empty"),
393 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100394 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 {
396 vim_beep(BO_COMPL);
397 setcursor();
398 out_flush();
399#ifdef FEAT_EVAL
400 if (!get_vim_var_nr(VV_TESTING))
401#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100402 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 }
404 return FALSE;
405 }
406 return TRUE;
407}
408
409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000410 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100411 * This depends on the current mode.
412 */
413 int
414vim_is_ctrl_x_key(int c)
415{
416 // Always allow ^R - let its results then be checked
417 if (c == Ctrl_R)
418 return TRUE;
419
420 // Accept <PageUp> and <PageDown> if the popup menu is visible.
421 if (ins_compl_pum_key(c))
422 return TRUE;
423
424 switch (ctrl_x_mode)
425 {
426 case 0: // Not in any CTRL-X mode
427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
428 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200429 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100430 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
431 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
432 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
433 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
434 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200435 || c == Ctrl_S || c == Ctrl_K || c == 's'
436 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437 case CTRL_X_SCROLL:
438 return (c == Ctrl_Y || c == Ctrl_E);
439 case CTRL_X_WHOLE_LINE:
440 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
441 case CTRL_X_FILES:
442 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
443 case CTRL_X_DICTIONARY:
444 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_THESAURUS:
446 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_TAGS:
448 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
449#ifdef FEAT_FIND_ID
450 case CTRL_X_PATH_PATTERNS:
451 return (c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_PATH_DEFINES:
453 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
454#endif
455 case CTRL_X_CMDLINE:
456 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
457 || c == Ctrl_X);
458#ifdef FEAT_COMPL_FUNC
459 case CTRL_X_FUNCTION:
460 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_OMNI:
462 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_SPELL:
465 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_EVAL:
467 return (c == Ctrl_P || c == Ctrl_N);
468 }
469 internal_error("vim_is_ctrl_x_key()");
470 return FALSE;
471}
472
473/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000474 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000475 */
476 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478{
479 return match->cp_flags & CP_ORIGINAL_TEXT;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Returns TRUE if "match" is the first match in the completion list.
484 */
485 static int
486is_first_match(compl_T *match)
487{
488 return match == compl_first_match;
489}
490
491/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100492 * Return TRUE when character "c" is part of the item currently being
493 * completed. Used to decide whether to abandon complete mode when the menu
494 * is visible.
495 */
496 int
497ins_compl_accept_char(int c)
498{
499 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
500 // When expanding an identifier only accept identifier chars.
501 return vim_isIDc(c);
502
503 switch (ctrl_x_mode)
504 {
505 case CTRL_X_FILES:
506 // When expanding file name only accept file name chars. But not
507 // path separators, so that "proto/<Tab>" expands files in
508 // "proto", not "proto/" as a whole
509 return vim_isfilec(c) && !vim_ispathsep(c);
510
511 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200512 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 case CTRL_X_OMNI:
514 // Command line and Omni completion can work with just about any
515 // printable character, but do stop at white space.
516 return vim_isprintc(c) && !VIM_ISWHITE(c);
517
518 case CTRL_X_WHOLE_LINE:
519 // For while line completion a space can be part of the line.
520 return vim_isprintc(c);
521 }
522 return vim_iswordc(c);
523}
524
525/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000526 * Get the completed text by inferring the case of the originally typed text.
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;
zeertzjqcfe45652022-05-27 17:26:55 +01001147 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001148 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001149 textlock--;
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 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001967 vim_free(compl_first_match->cp_prev->cp_str);
1968 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001969 }
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;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002152 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002153
2154 // Get here when we have finished typing a sequence of ^N and
2155 // ^P or other completion characters in CTRL-X mode. Free up
2156 // memory that was used, and make sure we can redo the insert.
2157 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2158 {
2159 // If any of the original typed text has been changed, eg when
2160 // ignorecase is set, we must add back-spaces to the redo
2161 // buffer. We add as few as necessary to delete just the part
2162 // of the original text that has changed.
2163 // When using the longest match, edited the match or used
2164 // CTRL-E then don't use the current match.
2165 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2166 ptr = compl_curr_match->cp_str;
2167 else
2168 ptr = NULL;
2169 ins_compl_fixRedoBufForLeader(ptr);
2170 }
2171
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002172 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002173
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002174 // When completing whole lines: fix indent for 'cindent'.
2175 // Otherwise, break line if it's too long.
2176 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2177 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002178 // re-indent the current line
2179 if (want_cindent)
2180 {
2181 do_c_expr_indent();
2182 want_cindent = FALSE; // don't do it again
2183 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002184 }
2185 else
2186 {
2187 int prev_col = curwin->w_cursor.col;
2188
2189 // put the cursor on the last char, for 'tw' formatting
2190 if (prev_col > 0)
2191 dec_cursor();
2192 // only format when something was inserted
2193 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2194 insertchar(NUL, 0, -1);
2195 if (prev_col > 0
2196 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2197 inc_cursor();
2198 }
2199
2200 // If the popup menu is displayed pressing CTRL-Y means accepting
2201 // the selection without inserting anything. When
2202 // compl_enter_selects is set the Enter key does the same.
2203 if ((c == Ctrl_Y || (compl_enter_selects
2204 && (c == CAR || c == K_KENTER || c == NL)))
2205 && pum_visible())
2206 retval = TRUE;
2207
2208 // CTRL-E means completion is Ended, go back to the typed text.
2209 // but only do this, if the Popup is still visible
2210 if (c == Ctrl_E)
2211 {
2212 ins_compl_delete();
2213 if (compl_leader != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002214 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002215 else if (compl_first_match != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002216 ins_bytes(compl_orig_text + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002217 retval = TRUE;
2218 }
2219
2220 auto_format(FALSE, TRUE);
2221
2222 // Trigger the CompleteDonePre event to give scripts a chance to
2223 // act upon the completion before clearing the info, and restore
2224 // ctrl_x_mode, so that complete_info() can be used.
2225 ctrl_x_mode = prev_mode;
2226 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2227
2228 ins_compl_free();
2229 compl_started = FALSE;
2230 compl_matches = 0;
2231 if (!shortmess(SHM_COMPLETIONMENU))
2232 msg_clr_cmdline(); // necessary for "noshowmode"
2233 ctrl_x_mode = CTRL_X_NORMAL;
2234 compl_enter_selects = FALSE;
2235 if (edit_submode != NULL)
2236 {
2237 edit_submode = NULL;
2238 showmode();
2239 }
2240
2241#ifdef FEAT_CMDWIN
2242 if (c == Ctrl_C && cmdwin_type != 0)
2243 // Avoid the popup menu remains displayed when leaving the
2244 // command line window.
2245 update_screen(0);
2246#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002247 // Indent now if a key was typed that is in 'cinkeys'.
2248 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2249 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002250 // Trigger the CompleteDone event to give scripts a chance to act
2251 // upon the end of completion.
2252 ins_apply_autocmds(EVENT_COMPLETEDONE);
2253
2254 return retval;
2255}
2256
2257/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002258 * Prepare for Insert mode completion, or stop it.
2259 * Called just after typing a character in Insert mode.
2260 * Returns TRUE when the character is not to be inserted;
2261 */
2262 int
2263ins_compl_prep(int c)
2264{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002265 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002266 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002267
2268 // Forget any previous 'special' messages if this is actually
2269 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2270 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2271 edit_submode_extra = NULL;
2272
2273 // Ignore end of Select mode mapping and mouse scroll buttons.
2274 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002275 || c == K_MOUSELEFT || c == K_MOUSERIGHT
2276 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002277 return retval;
2278
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002279#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002280 // Ignore mouse events in a popup window
2281 if (is_mouse_key(c))
2282 {
2283 // Ignore drag and release events, the position does not need to be in
2284 // the popup and it may have just closed.
2285 if (c == K_LEFTRELEASE
2286 || c == K_LEFTRELEASE_NM
2287 || c == K_MIDDLERELEASE
2288 || c == K_RIGHTRELEASE
2289 || c == K_X1RELEASE
2290 || c == K_X2RELEASE
2291 || c == K_LEFTDRAG
2292 || c == K_MIDDLEDRAG
2293 || c == K_RIGHTDRAG
2294 || c == K_X1DRAG
2295 || c == K_X2DRAG)
2296 return retval;
2297 if (popup_visible)
2298 {
2299 int row = mouse_row;
2300 int col = mouse_col;
2301 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2302
2303 if (wp != NULL && WIN_IS_POPUP(wp))
2304 return retval;
2305 }
2306 }
2307#endif
2308
zeertzjqdca29d92021-08-31 19:12:51 +02002309 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2310 {
2311 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2312 || !vim_is_ctrl_x_key(c))
2313 {
2314 // Not starting another completion mode.
2315 ctrl_x_mode = CTRL_X_CMDLINE;
2316
2317 // CTRL-X CTRL-Z should stop completion without inserting anything
2318 if (c == Ctrl_Z)
2319 retval = TRUE;
2320 }
2321 else
2322 {
2323 ctrl_x_mode = CTRL_X_CMDLINE;
2324
2325 // Other CTRL-X keys first stop completion, then start another
2326 // completion mode.
2327 ins_compl_prep(' ');
2328 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2329 }
2330 }
2331
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002332 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002333 if (ctrl_x_mode_not_defined_yet()
2334 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002335 {
2336 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2337 compl_used_match = TRUE;
2338
2339 }
2340
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002341 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002342 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2343 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002344 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002345 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002346 {
2347 // We're already in CTRL-X mode, do we stay in it?
2348 if (!vim_is_ctrl_x_key(c))
2349 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002350 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002351 ctrl_x_mode = CTRL_X_NORMAL;
2352 else
2353 ctrl_x_mode = CTRL_X_FINISHED;
2354 edit_submode = NULL;
2355 }
2356 showmode();
2357 }
2358
2359 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2360 {
2361 // Show error message from attempted keyword completion (probably
2362 // 'Pattern not found') until another key is hit, then go back to
2363 // showing what mode we are in.
2364 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002365 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002366 && c != Ctrl_R && !ins_compl_pum_key(c))
2367 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002368 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002369 }
2370 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2371 // Trigger the CompleteDone event to give scripts a chance to act
2372 // upon the (possibly failed) completion.
2373 ins_apply_autocmds(EVENT_COMPLETEDONE);
2374
LemonBoy2bf52dd2022-04-09 18:17:34 +01002375 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002376
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002377 // reset continue_* if we left expansion-mode, if we stay they'll be
2378 // (re)set properly in ins_complete()
2379 if (!vim_is_ctrl_x_key(c))
2380 {
2381 compl_cont_status = 0;
2382 compl_cont_mode = 0;
2383 }
2384
2385 return retval;
2386}
2387
2388/*
2389 * Fix the redo buffer for the completion leader replacing some of the typed
2390 * text. This inserts backspaces and appends the changed text.
2391 * "ptr" is the known leader text or NUL.
2392 */
2393 static void
2394ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2395{
2396 int len;
2397 char_u *p;
2398 char_u *ptr = ptr_arg;
2399
2400 if (ptr == NULL)
2401 {
2402 if (compl_leader != NULL)
2403 ptr = compl_leader;
2404 else
2405 return; // nothing to do
2406 }
2407 if (compl_orig_text != NULL)
2408 {
2409 p = compl_orig_text;
2410 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2411 ;
2412 if (len > 0)
2413 len -= (*mb_head_off)(p, p + len);
2414 for (p += len; *p != NUL; MB_PTR_ADV(p))
2415 AppendCharToRedobuff(K_BS);
2416 }
2417 else
2418 len = 0;
2419 if (ptr != NULL)
2420 AppendToRedobuffLit(ptr + len, -1);
2421}
2422
2423/*
2424 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2425 * (depending on flag) starting from buf and looking for a non-scanned
2426 * buffer (other than curbuf). curbuf is special, if it is called with
2427 * buf=curbuf then it has to be the first call for a given flag/expansion.
2428 *
2429 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2430 */
2431 static buf_T *
2432ins_compl_next_buf(buf_T *buf, int flag)
2433{
2434 static win_T *wp = NULL;
2435
2436 if (flag == 'w') // just windows
2437 {
2438 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2439 wp = curwin;
2440 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2441 && wp->w_buffer->b_scanned)
2442 ;
2443 buf = wp->w_buffer;
2444 }
2445 else
2446 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2447 // (unlisted buffers)
2448 // When completing whole lines skip unloaded buffers.
2449 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2450 && ((flag == 'U'
2451 ? buf->b_p_bl
2452 : (!buf->b_p_bl
2453 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2454 || buf->b_scanned))
2455 ;
2456 return buf;
2457}
2458
2459#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002460
2461# ifdef FEAT_EVAL
2462static callback_T cfu_cb; // 'completefunc' callback function
2463static callback_T ofu_cb; // 'omnifunc' callback function
2464static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2465# endif
2466
2467/*
2468 * Copy a global callback function to a buffer local callback.
2469 */
2470 static void
2471copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2472{
2473 free_callback(bufcb);
2474 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2475 copy_callback(bufcb, globcb);
2476}
2477
2478/*
2479 * Parse the 'completefunc' option value and set the callback function.
2480 * Invoked when the 'completefunc' option is set. The option value can be a
2481 * name of a function (string), or function(<name>) or funcref(<name>) or a
2482 * lambda expression.
2483 */
2484 int
2485set_completefunc_option(void)
2486{
2487 int retval;
2488
2489 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2490 if (retval == OK)
2491 set_buflocal_cfu_callback(curbuf);
2492
2493 return retval;
2494}
2495
2496/*
2497 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002498 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002499 */
2500 void
2501set_buflocal_cfu_callback(buf_T *buf UNUSED)
2502{
2503# ifdef FEAT_EVAL
2504 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2505# endif
2506}
2507
2508/*
2509 * Parse the 'omnifunc' option value and set the callback function.
2510 * Invoked when the 'omnifunc' option is set. The option value can be a
2511 * name of a function (string), or function(<name>) or funcref(<name>) or a
2512 * lambda expression.
2513 */
2514 int
2515set_omnifunc_option(void)
2516{
2517 int retval;
2518
2519 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2520 if (retval == OK)
2521 set_buflocal_ofu_callback(curbuf);
2522
2523 return retval;
2524}
2525
2526/*
2527 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002528 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002529 */
2530 void
2531set_buflocal_ofu_callback(buf_T *buf UNUSED)
2532{
2533# ifdef FEAT_EVAL
2534 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2535# endif
2536}
2537
2538/*
2539 * Parse the 'thesaurusfunc' option value and set the callback function.
2540 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2541 * name of a function (string), or function(<name>) or funcref(<name>) or a
2542 * lambda expression.
2543 */
2544 int
2545set_thesaurusfunc_option(void)
2546{
2547 int retval;
2548
2549 if (*curbuf->b_p_tsrfu != NUL)
2550 {
2551 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002552 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2553 &curbuf->b_tsrfu_cb);
2554 }
2555 else
2556 {
2557 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002558 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2559 }
2560
2561 return retval;
2562}
2563
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002564/*
2565 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002566 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002567 */
2568 int
2569set_ref_in_insexpand_funcs(int copyID)
2570{
2571 int abort = FALSE;
2572
2573 abort = set_ref_in_callback(&cfu_cb, copyID);
2574 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2575 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2576
2577 return abort;
2578}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002579
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002580/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002581 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002582 */
2583 static char_u *
2584get_complete_funcname(int type)
2585{
2586 switch (type)
2587 {
2588 case CTRL_X_FUNCTION:
2589 return curbuf->b_p_cfu;
2590 case CTRL_X_OMNI:
2591 return curbuf->b_p_ofu;
2592 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002593 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002594 default:
2595 return (char_u *)"";
2596 }
2597}
2598
2599/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002600 * Get the callback to use for insert mode completion.
2601 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002602 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002603get_insert_callback(int type)
2604{
2605 if (type == CTRL_X_FUNCTION)
2606 return &curbuf->b_cfu_cb;
2607 if (type == CTRL_X_OMNI)
2608 return &curbuf->b_ofu_cb;
2609 // CTRL_X_THESAURUS
2610 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2611}
2612
2613/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002614 * Execute user defined complete function 'completefunc', 'omnifunc' or
2615 * 'thesaurusfunc', and get matches in "matches".
2616 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002617 */
2618 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002619expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002620{
2621 list_T *matchlist = NULL;
2622 dict_T *matchdict = NULL;
2623 typval_T args[3];
2624 char_u *funcname;
2625 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002626 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002627 typval_T rettv;
2628 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002629 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002630
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002631 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002632 if (*funcname == NUL)
2633 return;
2634
2635 // Call 'completefunc' to obtain the list of matches.
2636 args[0].v_type = VAR_NUMBER;
2637 args[0].vval.v_number = 0;
2638 args[1].v_type = VAR_STRING;
2639 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2640 args[2].v_type = VAR_UNKNOWN;
2641
2642 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002643 // Lock the text to avoid weird things from happening. Also disallow
2644 // switching to another window, it should not be needed and may end up in
2645 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002646 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002647
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002648 cb = get_insert_callback(type);
2649 retval = call_callback(cb, 0, &rettv, 2, args);
2650
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002651 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002653 {
2654 switch (rettv.v_type)
2655 {
2656 case VAR_LIST:
2657 matchlist = rettv.vval.v_list;
2658 break;
2659 case VAR_DICT:
2660 matchdict = rettv.vval.v_dict;
2661 break;
2662 case VAR_SPECIAL:
2663 if (rettv.vval.v_number == VVAL_NONE)
2664 compl_opt_suppress_empty = TRUE;
2665 // FALLTHROUGH
2666 default:
2667 // TODO: Give error message?
2668 clear_tv(&rettv);
2669 break;
2670 }
2671 }
zeertzjqcfe45652022-05-27 17:26:55 +01002672 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002673
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002674 curwin->w_cursor = pos; // restore the cursor position
2675 validate_cursor();
2676 if (!EQUAL_POS(curwin->w_cursor, pos))
2677 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002678 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679 goto theend;
2680 }
2681
2682 if (matchlist != NULL)
2683 ins_compl_add_list(matchlist);
2684 else if (matchdict != NULL)
2685 ins_compl_add_dict(matchdict);
2686
2687theend:
2688 // Restore State, it might have been changed.
2689 State = save_State;
2690
2691 if (matchdict != NULL)
2692 dict_unref(matchdict);
2693 if (matchlist != NULL)
2694 list_unref(matchlist);
2695}
2696#endif // FEAT_COMPL_FUNC
2697
2698#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2699/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002700 * Add a match to the list of matches from a typeval_T.
2701 * If the given string is already in the list of completions, then return
2702 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2703 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002704 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002705 */
2706 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002707ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002708{
2709 char_u *word;
2710 int dup = FALSE;
2711 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002712 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002713 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002714 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002715 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002716
Bram Moolenaar08928322020-01-04 14:32:48 +01002717 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002718 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2719 {
2720 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2721 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2722 (char_u *)"abbr", FALSE);
2723 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2724 (char_u *)"menu", FALSE);
2725 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2726 (char_u *)"kind", FALSE);
2727 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2728 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002729 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002730 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2731 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2732 flags |= CP_ICASE;
2733 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2734 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2735 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2736 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2737 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2738 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2739 flags |= CP_EQUAL;
2740 }
2741 else
2742 {
2743 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002744 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002745 }
2746 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002747 {
2748 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002749 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002750 }
2751 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2752 if (status != OK)
2753 clear_tv(&user_data);
2754 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002755}
2756
2757/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758 * Add completions from a list.
2759 */
2760 static void
2761ins_compl_add_list(list_T *list)
2762{
2763 listitem_T *li;
2764 int dir = compl_direction;
2765
2766 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002767 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002768 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002769 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002770 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002771 // if dir was BACKWARD then honor it just once
2772 dir = FORWARD;
2773 else if (did_emsg)
2774 break;
2775 }
2776}
2777
2778/*
2779 * Add completions from a dict.
2780 */
2781 static void
2782ins_compl_add_dict(dict_T *dict)
2783{
2784 dictitem_T *di_refresh;
2785 dictitem_T *di_words;
2786
2787 // Check for optional "refresh" item.
2788 compl_opt_refresh_always = FALSE;
2789 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2790 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2791 {
2792 char_u *v = di_refresh->di_tv.vval.v_string;
2793
2794 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2795 compl_opt_refresh_always = TRUE;
2796 }
2797
2798 // Add completions from a "words" list.
2799 di_words = dict_find(dict, (char_u *)"words", 5);
2800 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2801 ins_compl_add_list(di_words->di_tv.vval.v_list);
2802}
2803
2804/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002805 * Start completion for the complete() function.
2806 * "startcol" is where the matched text starts (1 is first column).
2807 * "list" is the list of matches.
2808 */
2809 static void
2810set_completion(colnr_T startcol, list_T *list)
2811{
2812 int save_w_wrow = curwin->w_wrow;
2813 int save_w_leftcol = curwin->w_leftcol;
2814 int flags = CP_ORIGINAL_TEXT;
2815
2816 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002817 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002818 ins_compl_prep(' ');
2819 ins_compl_clear();
2820 ins_compl_free();
2821
2822 compl_direction = FORWARD;
2823 if (startcol > curwin->w_cursor.col)
2824 startcol = curwin->w_cursor.col;
2825 compl_col = startcol;
2826 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2827 // compl_pattern doesn't need to be set
2828 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2829 if (p_ic)
2830 flags |= CP_ICASE;
2831 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002832 -1, NULL, NULL, NULL, 0,
2833 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002834 return;
2835
2836 ctrl_x_mode = CTRL_X_EVAL;
2837
2838 ins_compl_add_list(list);
2839 compl_matches = ins_compl_make_cyclic();
2840 compl_started = TRUE;
2841 compl_used_match = TRUE;
2842 compl_cont_status = 0;
2843
2844 compl_curr_match = compl_first_match;
2845 if (compl_no_insert || compl_no_select)
2846 {
2847 ins_complete(K_DOWN, FALSE);
2848 if (compl_no_select)
2849 // Down/Up has no real effect.
2850 ins_complete(K_UP, FALSE);
2851 }
2852 else
2853 ins_complete(Ctrl_N, FALSE);
2854 compl_enter_selects = compl_no_insert;
2855
2856 // Lazily show the popup menu, unless we got interrupted.
2857 if (!compl_interrupted)
2858 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002859 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002860 out_flush();
2861}
2862
2863/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002864 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002865 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002866 void
2867f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002868{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002869 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002871 if (in_vim9script()
2872 && (check_for_number_arg(argvars, 0) == FAIL
2873 || check_for_list_arg(argvars, 1) == FAIL))
2874 return;
2875
Bram Moolenaar24959102022-05-07 20:01:16 +01002876 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002877 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002878 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002879 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002880 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002881
2882 // Check for undo allowed here, because if something was already inserted
2883 // the line was already saved for undo and this check isn't done.
2884 if (!undo_allowed())
2885 return;
2886
2887 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002888 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002889 else
2890 {
2891 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2892 if (startcol > 0)
2893 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002894 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002895}
2896
2897/*
2898 * "complete_add()" function
2899 */
2900 void
2901f_complete_add(typval_T *argvars, typval_T *rettv)
2902{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002903 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002904 return;
2905
Bram Moolenaar440cf092021-04-03 20:13:30 +02002906 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002907}
2908
2909/*
2910 * "complete_check()" function
2911 */
2912 void
2913f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2914{
2915 int saved = RedrawingDisabled;
2916
2917 RedrawingDisabled = 0;
2918 ins_compl_check_keys(0, TRUE);
2919 rettv->vval.v_number = ins_compl_interrupted();
2920 RedrawingDisabled = saved;
2921}
2922
2923/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002924 * Return Insert completion mode name string
2925 */
2926 static char_u *
2927ins_compl_mode(void)
2928{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002929 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002930 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2931
2932 return (char_u *)"";
2933}
2934
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002935/*
2936 * Assign the sequence number to all the completion matches which don't have
2937 * one assigned yet.
2938 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002939 static void
2940ins_compl_update_sequence_numbers()
2941{
2942 int number = 0;
2943 compl_T *match;
2944
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002945 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002946 {
2947 // search backwards for the first valid (!= -1) number.
2948 // This should normally succeed already at the first loop
2949 // cycle, so it's fast!
2950 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002951 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002952 if (match->cp_number != -1)
2953 {
2954 number = match->cp_number;
2955 break;
2956 }
2957 if (match != NULL)
2958 // go up and assign all numbers which are not assigned
2959 // yet
2960 for (match = match->cp_next;
2961 match != NULL && match->cp_number == -1;
2962 match = match->cp_next)
2963 match->cp_number = ++number;
2964 }
2965 else // BACKWARD
2966 {
2967 // search forwards (upwards) for the first valid (!= -1)
2968 // number. This should normally succeed already at the
2969 // first loop cycle, so it's fast!
2970 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002971 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002972 if (match->cp_number != -1)
2973 {
2974 number = match->cp_number;
2975 break;
2976 }
2977 if (match != NULL)
2978 // go down and assign all numbers which are not
2979 // assigned yet
2980 for (match = match->cp_prev; match
2981 && match->cp_number == -1;
2982 match = match->cp_prev)
2983 match->cp_number = ++number;
2984 }
2985}
2986
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002987/*
2988 * Get complete information
2989 */
2990 static void
2991get_complete_info(list_T *what_list, dict_T *retdict)
2992{
2993 int ret = OK;
2994 listitem_T *item;
2995#define CI_WHAT_MODE 0x01
2996#define CI_WHAT_PUM_VISIBLE 0x02
2997#define CI_WHAT_ITEMS 0x04
2998#define CI_WHAT_SELECTED 0x08
2999#define CI_WHAT_INSERTED 0x10
3000#define CI_WHAT_ALL 0xff
3001 int what_flag;
3002
3003 if (what_list == NULL)
3004 what_flag = CI_WHAT_ALL;
3005 else
3006 {
3007 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003008 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003009 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003010 {
3011 char_u *what = tv_get_string(&item->li_tv);
3012
3013 if (STRCMP(what, "mode") == 0)
3014 what_flag |= CI_WHAT_MODE;
3015 else if (STRCMP(what, "pum_visible") == 0)
3016 what_flag |= CI_WHAT_PUM_VISIBLE;
3017 else if (STRCMP(what, "items") == 0)
3018 what_flag |= CI_WHAT_ITEMS;
3019 else if (STRCMP(what, "selected") == 0)
3020 what_flag |= CI_WHAT_SELECTED;
3021 else if (STRCMP(what, "inserted") == 0)
3022 what_flag |= CI_WHAT_INSERTED;
3023 }
3024 }
3025
3026 if (ret == OK && (what_flag & CI_WHAT_MODE))
3027 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3028
3029 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3030 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3031
3032 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3033 {
3034 list_T *li;
3035 dict_T *di;
3036 compl_T *match;
3037
3038 li = list_alloc();
3039 if (li == NULL)
3040 return;
3041 ret = dict_add_list(retdict, "items", li);
3042 if (ret == OK && compl_first_match != NULL)
3043 {
3044 match = compl_first_match;
3045 do
3046 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003047 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003048 {
3049 di = dict_alloc();
3050 if (di == NULL)
3051 return;
3052 ret = list_append_dict(li, di);
3053 if (ret != OK)
3054 return;
3055 dict_add_string(di, "word", match->cp_str);
3056 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3057 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3058 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3059 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003060 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3061 // Add an empty string for backwards compatibility
3062 dict_add_string(di, "user_data", (char_u *)"");
3063 else
3064 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003065 }
3066 match = match->cp_next;
3067 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003068 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003069 }
3070 }
3071
3072 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003073 {
3074 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3075 ins_compl_update_sequence_numbers();
3076 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3077 ? compl_curr_match->cp_number - 1 : -1);
3078 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003079
3080 // TODO
3081 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3082}
3083
3084/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003085 * "complete_info()" function
3086 */
3087 void
3088f_complete_info(typval_T *argvars, typval_T *rettv)
3089{
3090 list_T *what_list = NULL;
3091
Bram Moolenaar93a10962022-06-16 11:42:09 +01003092 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003093 return;
3094
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003095 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3096 return;
3097
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003098 if (argvars[0].v_type != VAR_UNKNOWN)
3099 {
3100 if (argvars[0].v_type != VAR_LIST)
3101 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003102 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003103 return;
3104 }
3105 what_list = argvars[0].vval.v_list;
3106 }
3107 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108}
3109#endif
3110
3111/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003112 * Returns TRUE when using a user-defined function for thesaurus completion.
3113 */
3114 static int
3115thesaurus_func_complete(int type UNUSED)
3116{
3117#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003118 return type == CTRL_X_THESAURUS
3119 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003120#else
3121 return FALSE;
3122#endif
3123}
3124
3125/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003126 * Return value of process_next_cpt_value()
3127 */
3128enum
3129{
3130 INS_COMPL_CPT_OK = 1,
3131 INS_COMPL_CPT_CONT,
3132 INS_COMPL_CPT_END
3133};
3134
3135/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003136 * state information used for getting the next set of insert completion
3137 * matches.
3138 */
3139typedef struct
3140{
3141 char_u *e_cpt; // current entry in 'complete'
3142 buf_T *ins_buf; // buffer being scanned
3143 pos_T *cur_match_pos; // current match position
3144 pos_T prev_match_pos; // previous match position
3145 int set_match_pos; // save first_match_pos/last_match_pos
3146 pos_T first_match_pos; // first match position
3147 pos_T last_match_pos; // last match position
3148 int found_all; // found all matches of a certain type.
3149 char_u *dict; // dictionary file to search
3150 int dict_f; // "dict" is an exact file name or not
3151} ins_compl_next_state_T;
3152
3153/*
3154 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003155 *
3156 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003157 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003158 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003159 * st->found_all - all matches of this type are found
3160 * st->ins_buf - search for completions in this buffer
3161 * st->first_match_pos - position of the first completion match
3162 * st->last_match_pos - position of the last completion match
3163 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003164 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003165 * st->dict - name of the dictionary or thesaurus file to search
3166 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003167 *
3168 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003169 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3170 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003171 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003172 */
3173 static int
3174process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003175 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003176 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003177 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003178{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003179 int compl_type = -1;
3180 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003181
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003182 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003183
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003184 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3185 st->e_cpt++;
3186
3187 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003188 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003189 st->ins_buf = curbuf;
3190 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003191 // Move the cursor back one character so that ^N can match the
3192 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003193 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003194 {
3195 // Move the cursor to after the last character in the
3196 // buffer, so that word at start of buffer is found
3197 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003198 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3199 st->first_match_pos.col =
3200 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003201 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003202 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003203 compl_type = 0;
3204
3205 // Remember the first match so that the loop stops when we
3206 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003207 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003208 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003209 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3210 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003211 {
3212 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003213 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003214 {
3215 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003216 st->first_match_pos.col = st->last_match_pos.col = 0;
3217 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3218 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003219 compl_type = 0;
3220 }
3221 else // unloaded buffer, scan like dictionary
3222 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003223 st->found_all = TRUE;
3224 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003225 {
3226 status = INS_COMPL_CPT_CONT;
3227 goto done;
3228 }
3229 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003230 st->dict = st->ins_buf->b_fname;
3231 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003232 }
3233 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3234 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003235 st->ins_buf->b_fname == NULL
3236 ? buf_spname(st->ins_buf)
3237 : st->ins_buf->b_sfname == NULL
3238 ? st->ins_buf->b_fname
3239 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003240 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3241 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003242 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003243 status = INS_COMPL_CPT_END;
3244 else
3245 {
3246 if (ctrl_x_mode_line_or_eval())
3247 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003248 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003250 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003251 compl_type = CTRL_X_DICTIONARY;
3252 else
3253 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003256 st->dict = st->e_cpt;
3257 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003258 }
3259 }
3260#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003261 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003262 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003263 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003264 compl_type = CTRL_X_PATH_DEFINES;
3265#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003266 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003267 {
3268 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3269 compl_type = CTRL_X_TAGS;
3270 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3271 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3272 }
3273 else
3274 compl_type = -1;
3275
3276 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003277 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003278
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003279 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003280 if (compl_type == -1)
3281 status = INS_COMPL_CPT_CONT;
3282 }
3283
3284done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003285 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003286 return status;
3287}
3288
3289#ifdef FEAT_FIND_ID
3290/*
3291 * Get the next set of identifiers or defines matching "compl_pattern" in
3292 * included files.
3293 */
3294 static void
3295get_next_include_file_completion(int compl_type)
3296{
3297 find_pattern_in_path(compl_pattern, compl_direction,
3298 (int)STRLEN(compl_pattern), FALSE, FALSE,
3299 (compl_type == CTRL_X_PATH_DEFINES
3300 && !(compl_cont_status & CONT_SOL))
3301 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3302 (linenr_T)1, (linenr_T)MAXLNUM);
3303}
3304#endif
3305
3306/*
3307 * Get the next set of words matching "compl_pattern" in dictionary or
3308 * thesaurus files.
3309 */
3310 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003311get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003312{
3313#ifdef FEAT_COMPL_FUNC
3314 if (thesaurus_func_complete(compl_type))
3315 expand_by_function(compl_type, compl_pattern);
3316 else
3317#endif
3318 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003319 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320 : (compl_type == CTRL_X_THESAURUS
3321 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3322 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3323 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003324 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003325 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326}
3327
3328/*
3329 * Get the next set of tag names matching "compl_pattern".
3330 */
3331 static void
3332get_next_tag_completion(void)
3333{
3334 int save_p_ic;
3335 char_u **matches;
3336 int num_matches;
3337
3338 // set p_ic according to p_ic, p_scs and pat for find_tags().
3339 save_p_ic = p_ic;
3340 p_ic = ignorecase(compl_pattern);
3341
3342 // Find up to TAG_MANY matches. Avoids that an enormous number
3343 // of matches is found when compl_pattern is empty
3344 g_tag_at_cursor = TRUE;
3345 if (find_tags(compl_pattern, &num_matches, &matches,
3346 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003347 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003348 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3349 ins_compl_add_matches(num_matches, matches, p_ic);
3350 g_tag_at_cursor = FALSE;
3351 p_ic = save_p_ic;
3352}
3353
3354/*
3355 * Get the next set of filename matching "compl_pattern".
3356 */
3357 static void
3358get_next_filename_completion(void)
3359{
3360 char_u **matches;
3361 int num_matches;
3362
3363 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3364 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3365 return;
3366
3367 // May change home directory back to "~".
3368 tilde_replace(compl_pattern, num_matches, matches);
3369#ifdef BACKSLASH_IN_FILENAME
3370 if (curbuf->b_p_csl[0] != NUL)
3371 {
3372 int i;
3373
3374 for (i = 0; i < num_matches; ++i)
3375 {
3376 char_u *ptr = matches[i];
3377
3378 while (*ptr != NUL)
3379 {
3380 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3381 *ptr = '/';
3382 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3383 *ptr = '\\';
3384 ptr += (*mb_ptr2len)(ptr);
3385 }
3386 }
3387 }
3388#endif
3389 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3390}
3391
3392/*
3393 * Get the next set of command-line completions matching "compl_pattern".
3394 */
3395 static void
3396get_next_cmdline_completion()
3397{
3398 char_u **matches;
3399 int num_matches;
3400
3401 if (expand_cmdline(&compl_xp, compl_pattern,
3402 (int)STRLEN(compl_pattern),
3403 &num_matches, &matches) == EXPAND_OK)
3404 ins_compl_add_matches(num_matches, matches, FALSE);
3405}
3406
3407/*
3408 * Get the next set of spell suggestions matching "compl_pattern".
3409 */
3410 static void
3411get_next_spell_completion(linenr_T lnum UNUSED)
3412{
3413#ifdef FEAT_SPELL
3414 char_u **matches;
3415 int num_matches;
3416
3417 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3418 if (num_matches > 0)
3419 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003420 else
3421 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003422#endif
3423}
3424
3425/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003426 * Return the next word or line from buffer "ins_buf" at position
3427 * "cur_match_pos" for completion. The length of the match is set in "len".
3428 */
3429 static char_u *
3430ins_comp_get_next_word_or_line(
3431 buf_T *ins_buf, // buffer being scanned
3432 pos_T *cur_match_pos, // current match position
3433 int *match_len,
3434 int *cont_s_ipos) // next ^X<> will set initial_pos
3435{
3436 char_u *ptr;
3437 int len;
3438
3439 *match_len = 0;
3440 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3441 cur_match_pos->col;
3442 if (ctrl_x_mode_line_or_eval())
3443 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003444 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003445 {
3446 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3447 return NULL;
3448 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3449 if (!p_paste)
3450 ptr = skipwhite(ptr);
3451 }
3452 len = (int)STRLEN(ptr);
3453 }
3454 else
3455 {
3456 char_u *tmp_ptr = ptr;
3457
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003458 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003459 {
3460 tmp_ptr += compl_length;
3461 // Skip if already inside a word.
3462 if (vim_iswordp(tmp_ptr))
3463 return NULL;
3464 // Find start of next word.
3465 tmp_ptr = find_word_start(tmp_ptr);
3466 }
3467 // Find end of this word.
3468 tmp_ptr = find_word_end(tmp_ptr);
3469 len = (int)(tmp_ptr - ptr);
3470
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003471 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003472 {
3473 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3474 {
3475 // Try next line, if any. the new word will be
3476 // "join" as if the normal command "J" was used.
3477 // IOSIZE is always greater than
3478 // compl_length, so the next STRNCPY always
3479 // works -- Acevedo
3480 STRNCPY(IObuff, ptr, len);
3481 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3482 tmp_ptr = ptr = skipwhite(ptr);
3483 // Find start of next word.
3484 tmp_ptr = find_word_start(tmp_ptr);
3485 // Find end of next word.
3486 tmp_ptr = find_word_end(tmp_ptr);
3487 if (tmp_ptr > ptr)
3488 {
3489 if (*ptr != ')' && IObuff[len - 1] != TAB)
3490 {
3491 if (IObuff[len - 1] != ' ')
3492 IObuff[len++] = ' ';
3493 // IObuf =~ "\k.* ", thus len >= 2
3494 if (p_js
3495 && (IObuff[len - 2] == '.'
3496 || (vim_strchr(p_cpo, CPO_JOINSP)
3497 == NULL
3498 && (IObuff[len - 2] == '?'
3499 || IObuff[len - 2] == '!'))))
3500 IObuff[len++] = ' ';
3501 }
3502 // copy as much as possible of the new word
3503 if (tmp_ptr - ptr >= IOSIZE - len)
3504 tmp_ptr = ptr + IOSIZE - len - 1;
3505 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3506 len += (int)(tmp_ptr - ptr);
3507 *cont_s_ipos = TRUE;
3508 }
3509 IObuff[len] = NUL;
3510 ptr = IObuff;
3511 }
3512 if (len == compl_length)
3513 return NULL;
3514 }
3515 }
3516
3517 *match_len = len;
3518 return ptr;
3519}
3520
3521/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003522 * Get the next set of words matching "compl_pattern" for default completion(s)
3523 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003524 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3525 * position "st->start_pos" in the "compl_direction" direction. If
3526 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3527 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003528 * Returns OK if a new next match is found, otherwise returns FAIL.
3529 */
3530 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003531get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003532{
3533 int found_new_match = FAIL;
3534 int save_p_scs;
3535 int save_p_ws;
3536 int looped_around = FALSE;
3537 char_u *ptr;
3538 int len;
3539
3540 // If 'infercase' is set, don't use 'smartcase' here
3541 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003542 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003543 p_scs = FALSE;
3544
3545 // Buffers other than curbuf are scanned from the beginning or the
3546 // end but never from the middle, thus setting nowrapscan in this
3547 // buffer is a good idea, on the other hand, we always set
3548 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3549 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003550 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003551 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003552 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003553 p_ws = TRUE;
3554 looped_around = FALSE;
3555 for (;;)
3556 {
3557 int cont_s_ipos = FALSE;
3558
3559 ++msg_silent; // Don't want messages for wrapscan.
3560
3561 // ctrl_x_mode_line_or_eval() || word-wise search that
3562 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003563 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003564 found_new_match = search_for_exact_line(st->ins_buf,
3565 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003566 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003567 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3568 NULL, compl_direction, compl_pattern, 1L,
3569 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003570 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003571 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 {
3573 // set "compl_started" even on fail
3574 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003575 st->first_match_pos = *st->cur_match_pos;
3576 st->last_match_pos = *st->cur_match_pos;
3577 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003578 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003579 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3580 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003581 {
3582 found_new_match = FAIL;
3583 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003584 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003585 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3586 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3587 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003588 {
3589 if (looped_around)
3590 found_new_match = FAIL;
3591 else
3592 looped_around = TRUE;
3593 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003594 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003595 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3596 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3597 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003598 {
3599 if (looped_around)
3600 found_new_match = FAIL;
3601 else
3602 looped_around = TRUE;
3603 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003604 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003605 if (found_new_match == FAIL)
3606 break;
3607
3608 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003609 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003610 && start_pos->lnum == st->cur_match_pos->lnum
3611 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003612 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003613
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003614 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3615 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003616 if (ptr == NULL)
3617 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003619 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003620 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003621 0, cont_s_ipos) != NOTDONE)
3622 {
3623 found_new_match = OK;
3624 break;
3625 }
3626 }
3627 p_scs = save_p_scs;
3628 p_ws = save_p_ws;
3629
3630 return found_new_match;
3631}
3632
3633/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003634 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003635 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003636 */
3637 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003638get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003639{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003640 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003641
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003642 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003643 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003644 case -1:
3645 break;
3646#ifdef FEAT_FIND_ID
3647 case CTRL_X_PATH_PATTERNS:
3648 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003649 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003650 break;
3651#endif
3652
3653 case CTRL_X_DICTIONARY:
3654 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003655 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3656 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003657 break;
3658
3659 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003661 break;
3662
3663 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003664 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003665 break;
3666
3667 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003668 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003669 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003670 break;
3671
3672#ifdef FEAT_COMPL_FUNC
3673 case CTRL_X_FUNCTION:
3674 case CTRL_X_OMNI:
3675 expand_by_function(type, compl_pattern);
3676 break;
3677#endif
3678
3679 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003680 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003681 break;
3682
3683 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003684 found_new_match = get_next_default_completion(st, ini);
3685 if (found_new_match == FAIL && st->ins_buf == curbuf)
3686 st->found_all = TRUE;
3687 }
3688
3689 // check if compl_curr_match has changed, (e.g. other type of
3690 // expansion added something)
3691 if (type != 0 && compl_curr_match != compl_old_match)
3692 found_new_match = OK;
3693
3694 return found_new_match;
3695}
3696
3697/*
3698 * Get the next expansion(s), using "compl_pattern".
3699 * The search starts at position "ini" in curbuf and in the direction
3700 * compl_direction.
3701 * When "compl_started" is FALSE start at that position, otherwise continue
3702 * where we stopped searching before.
3703 * This may return before finding all the matches.
3704 * Return the total number of matches or -1 if still unknown -- Acevedo
3705 */
3706 static int
3707ins_compl_get_exp(pos_T *ini)
3708{
3709 static ins_compl_next_state_T st;
3710 int i;
3711 int found_new_match;
3712 int type = ctrl_x_mode;
3713
3714 if (!compl_started)
3715 {
3716 FOR_ALL_BUFFERS(st.ins_buf)
3717 st.ins_buf->b_scanned = 0;
3718 st.found_all = FALSE;
3719 st.ins_buf = curbuf;
3720 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3721 ? (char_u *)"." : curbuf->b_p_cpt;
3722 st.last_match_pos = st.first_match_pos = *ini;
3723 }
3724 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3725 st.ins_buf = curbuf; // In case the buffer was wiped out.
3726
3727 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003728 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003729 ? &st.last_match_pos : &st.first_match_pos;
3730
3731 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3732 for (;;)
3733 {
3734 found_new_match = FAIL;
3735 st.set_match_pos = FALSE;
3736
3737 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3738 // or if found_all says this entry is done. For ^X^L only use the
3739 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003740 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003741 && (!compl_started || st.found_all))
3742 {
3743 int status = process_next_cpt_value(&st, &type, ini);
3744
3745 if (status == INS_COMPL_CPT_END)
3746 break;
3747 if (status == INS_COMPL_CPT_CONT)
3748 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003749 }
3750
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003751 // If complete() was called then compl_pattern has been reset. The
3752 // following won't work then, bail out.
3753 if (compl_pattern == NULL)
3754 break;
3755
3756 // get the next set of completion matches
3757 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003758
3759 // break the loop for specialized modes (use 'complete' just for the
3760 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3761 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003762 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3763 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003764 {
3765 if (got_int)
3766 break;
3767 // Fill the popup menu as soon as possible.
3768 if (type != -1)
3769 ins_compl_check_keys(0, FALSE);
3770
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003771 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003772 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3773 break;
3774 compl_started = TRUE;
3775 }
3776 else
3777 {
3778 // Mark a buffer scanned when it has been scanned completely
3779 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003780 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003781
3782 compl_started = FALSE;
3783 }
3784 }
3785 compl_started = TRUE;
3786
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003787 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003788 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003789 found_new_match = FAIL;
3790
3791 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003792 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003793 && !ctrl_x_mode_line_or_eval()))
3794 i = ins_compl_make_cyclic();
3795
3796 if (compl_old_match != NULL)
3797 {
3798 // If several matches were added (FORWARD) or the search failed and has
3799 // just been made cyclic then we have to move compl_curr_match to the
3800 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003801 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003802 : compl_old_match->cp_prev;
3803 if (compl_curr_match == NULL)
3804 compl_curr_match = compl_old_match;
3805 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003806 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003807
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003808 return i;
3809}
3810
3811/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003812 * Update "compl_shown_match" to the actually shown match, it may differ when
3813 * "compl_leader" is used to omit some of the matches.
3814 */
3815 static void
3816ins_compl_update_shown_match(void)
3817{
3818 while (!ins_compl_equal(compl_shown_match,
3819 compl_leader, (int)STRLEN(compl_leader))
3820 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003821 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003822 compl_shown_match = compl_shown_match->cp_next;
3823
3824 // If we didn't find it searching forward, and compl_shows_dir is
3825 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003826 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003827 && !ins_compl_equal(compl_shown_match,
3828 compl_leader, (int)STRLEN(compl_leader))
3829 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003830 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003831 {
3832 while (!ins_compl_equal(compl_shown_match,
3833 compl_leader, (int)STRLEN(compl_leader))
3834 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003835 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003836 compl_shown_match = compl_shown_match->cp_prev;
3837 }
3838}
3839
3840/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003841 * Delete the old text being completed.
3842 */
3843 void
3844ins_compl_delete(void)
3845{
3846 int col;
3847
3848 // In insert mode: Delete the typed part.
3849 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003850 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003851 if ((int)curwin->w_cursor.col > col)
3852 {
3853 if (stop_arrow() == FAIL)
3854 return;
3855 backspace_until_column(col);
3856 }
3857
3858 // TODO: is this sufficient for redrawing? Redrawing everything causes
3859 // flicker, thus we can't do that.
3860 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003861#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003862 // clear v:completed_item
3863 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003864#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003865}
3866
3867/*
3868 * Insert the new text being completed.
3869 * "in_compl_func" is TRUE when called from complete_check().
3870 */
3871 void
3872ins_compl_insert(int in_compl_func)
3873{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003874 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003875
3876 // Make sure we don't go over the end of the string, this can happen with
3877 // illegal bytes.
3878 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3879 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003880 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003881 compl_used_match = FALSE;
3882 else
3883 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003884#ifdef FEAT_EVAL
3885 {
3886 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3887
3888 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3889 }
3890#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003891 if (!in_compl_func)
3892 compl_curr_match = compl_shown_match;
3893}
3894
3895/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003896 * show the file name for the completion match (if any). Truncate the file
3897 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003898 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003899 static void
3900ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003901{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003902 char *lead = _("match in file");
3903 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3904 char_u *s;
3905 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003906
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003907 if (space <= 0)
3908 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003909
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003910 // We need the tail that fits. With double-byte encoding going
3911 // back from the end is very slow, thus go from the start and keep
3912 // the text that fits in "space" between "s" and "e".
3913 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003914 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003915 space -= ptr2cells(e);
3916 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003917 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003918 space += ptr2cells(s);
3919 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003920 }
3921 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003922 msg_hist_off = TRUE;
3923 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3924 s > compl_shown_match->cp_fname ? "<" : "", s);
3925 msg((char *)IObuff);
3926 msg_hist_off = FALSE;
3927 redraw_cmdline = FALSE; // don't overwrite!
3928}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003929
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003930/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003931 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003932 * times. The number of matches found is returned in 'num_matches'.
3933 *
3934 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3935 * get more completions. If it is FALSE, then do nothing when there are no more
3936 * completions in the given direction.
3937 *
3938 * If "advance" is TRUE, then completion will move to the first match.
3939 * Otherwise, the original text will be shown.
3940 *
3941 * Returns OK on success and -1 if the number of matches are unknown.
3942 */
3943 static int
3944find_next_completion_match(
3945 int allow_get_expansion,
3946 int todo, // repeat completion this many times
3947 int advance,
3948 int *num_matches)
3949{
3950 int found_end = FALSE;
3951 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003952
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003953 while (--todo >= 0)
3954 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003955 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003956 {
3957 compl_shown_match = compl_shown_match->cp_next;
3958 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003959 && (is_first_match(compl_shown_match->cp_next)
3960 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003961 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003962 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003963 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003964 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003965 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003966 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003967 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003968 }
3969 else
3970 {
3971 if (!allow_get_expansion)
3972 {
3973 if (advance)
3974 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003975 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003976 compl_pending -= todo + 1;
3977 else
3978 compl_pending += todo + 1;
3979 }
3980 return -1;
3981 }
3982
3983 if (!compl_no_select && advance)
3984 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003985 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003986 --compl_pending;
3987 else
3988 ++compl_pending;
3989 }
3990
3991 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003992 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003993
3994 // handle any pending completions
3995 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003996 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003997 {
3998 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3999 {
4000 compl_shown_match = compl_shown_match->cp_next;
4001 --compl_pending;
4002 }
4003 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4004 {
4005 compl_shown_match = compl_shown_match->cp_prev;
4006 ++compl_pending;
4007 }
4008 else
4009 break;
4010 }
4011 found_end = FALSE;
4012 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004013 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004014 && compl_leader != NULL
4015 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004016 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004017 ++todo;
4018 else
4019 // Remember a matching item.
4020 found_compl = compl_shown_match;
4021
4022 // Stop at the end of the list when we found a usable match.
4023 if (found_end)
4024 {
4025 if (found_compl != NULL)
4026 {
4027 compl_shown_match = found_compl;
4028 break;
4029 }
4030 todo = 1; // use first usable match after wrapping around
4031 }
4032 }
4033
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004034 return OK;
4035}
4036
4037/*
4038 * Fill in the next completion in the current direction.
4039 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4040 * get more completions. If it is FALSE, then we just do nothing when there
4041 * are no more completions in a given direction. The latter case is used when
4042 * we are still in the middle of finding completions, to allow browsing
4043 * through the ones found so far.
4044 * Return the total number of matches, or -1 if still unknown -- webb.
4045 *
4046 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4047 * compl_shown_match here.
4048 *
4049 * Note that this function may be called recursively once only. First with
4050 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4051 * calls this function with "allow_get_expansion" FALSE.
4052 */
4053 static int
4054ins_compl_next(
4055 int allow_get_expansion,
4056 int count, // repeat completion this many times; should
4057 // be at least 1
4058 int insert_match, // Insert the newly selected match
4059 int in_compl_func) // called from complete_check()
4060{
4061 int num_matches = -1;
4062 int todo = count;
4063 int advance;
4064 int started = compl_started;
4065
4066 // When user complete function return -1 for findstart which is next
4067 // time of 'always', compl_shown_match become NULL.
4068 if (compl_shown_match == NULL)
4069 return -1;
4070
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004071 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004072 // Update "compl_shown_match" to the actually shown match
4073 ins_compl_update_shown_match();
4074
4075 if (allow_get_expansion && insert_match
4076 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4077 // Delete old text to be replaced
4078 ins_compl_delete();
4079
4080 // When finding the longest common text we stick at the original text,
4081 // don't let CTRL-N or CTRL-P move to the first match.
4082 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4083
4084 // When restarting the search don't insert the first match either.
4085 if (compl_restarting)
4086 {
4087 advance = FALSE;
4088 compl_restarting = FALSE;
4089 }
4090
4091 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4092 // around.
4093 if (find_next_completion_match(allow_get_expansion, todo, advance,
4094 &num_matches) == -1)
4095 return -1;
4096
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004097 // Insert the text of the new completion, or the compl_leader.
4098 if (compl_no_insert && !started)
4099 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004100 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004101 compl_used_match = FALSE;
4102 }
4103 else if (insert_match)
4104 {
4105 if (!compl_get_longest || compl_used_match)
4106 ins_compl_insert(in_compl_func);
4107 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004108 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004109 }
4110 else
4111 compl_used_match = FALSE;
4112
4113 if (!allow_get_expansion)
4114 {
4115 // may undisplay the popup menu first
4116 ins_compl_upd_pum();
4117
4118 if (pum_enough_matches())
4119 // Will display the popup menu, don't redraw yet to avoid flicker.
4120 pum_call_update_screen();
4121 else
4122 // Not showing the popup menu yet, redraw to show the user what was
4123 // inserted.
4124 update_screen(0);
4125
4126 // display the updated popup menu
4127 ins_compl_show_pum();
4128#ifdef FEAT_GUI
4129 if (gui.in_use)
4130 {
4131 // Show the cursor after the match, not after the redrawn text.
4132 setcursor();
4133 out_flush_cursor(FALSE, FALSE);
4134 }
4135#endif
4136
4137 // Delete old text to be replaced, since we're still searching and
4138 // don't want to match ourselves!
4139 ins_compl_delete();
4140 }
4141
4142 // Enter will select a match when the match wasn't inserted and the popup
4143 // menu is visible.
4144 if (compl_no_insert && !started)
4145 compl_enter_selects = TRUE;
4146 else
4147 compl_enter_selects = !insert_match && compl_match_array != NULL;
4148
4149 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004150 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004151 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004152
4153 return num_matches;
4154}
4155
4156/*
4157 * Call this while finding completions, to check whether the user has hit a key
4158 * that should change the currently displayed completion, or exit completion
4159 * mode. Also, when compl_pending is not zero, show a completion as soon as
4160 * possible. -- webb
4161 * "frequency" specifies out of how many calls we actually check.
4162 * "in_compl_func" is TRUE when called from complete_check(), don't set
4163 * compl_curr_match.
4164 */
4165 void
4166ins_compl_check_keys(int frequency, int in_compl_func)
4167{
4168 static int count = 0;
4169 int c;
4170
4171 // Don't check when reading keys from a script, :normal or feedkeys().
4172 // That would break the test scripts. But do check for keys when called
4173 // from complete_check().
4174 if (!in_compl_func && (using_script() || ex_normal_busy))
4175 return;
4176
4177 // Only do this at regular intervals
4178 if (++count < frequency)
4179 return;
4180 count = 0;
4181
4182 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4183 // can't do its work correctly.
4184 c = vpeekc_any();
4185 if (c != NUL)
4186 {
4187 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4188 {
4189 c = safe_vgetc(); // Eat the character
4190 compl_shows_dir = ins_compl_key2dir(c);
4191 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4192 c != K_UP && c != K_DOWN, in_compl_func);
4193 }
4194 else
4195 {
4196 // Need to get the character to have KeyTyped set. We'll put it
4197 // back with vungetc() below. But skip K_IGNORE.
4198 c = safe_vgetc();
4199 if (c != K_IGNORE)
4200 {
4201 // Don't interrupt completion when the character wasn't typed,
4202 // e.g., when doing @q to replay keys.
4203 if (c != Ctrl_R && KeyTyped)
4204 compl_interrupted = TRUE;
4205
4206 vungetc(c);
4207 }
4208 }
4209 }
4210 if (compl_pending != 0 && !got_int && !compl_no_insert)
4211 {
4212 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4213
4214 compl_pending = 0;
4215 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4216 }
4217}
4218
4219/*
4220 * Decide the direction of Insert mode complete from the key typed.
4221 * Returns BACKWARD or FORWARD.
4222 */
4223 static int
4224ins_compl_key2dir(int c)
4225{
4226 if (c == Ctrl_P || c == Ctrl_L
4227 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4228 return BACKWARD;
4229 return FORWARD;
4230}
4231
4232/*
4233 * Return TRUE for keys that are used for completion only when the popup menu
4234 * is visible.
4235 */
4236 static int
4237ins_compl_pum_key(int c)
4238{
4239 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4240 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4241 || c == K_UP || c == K_DOWN);
4242}
4243
4244/*
4245 * Decide the number of completions to move forward.
4246 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4247 */
4248 static int
4249ins_compl_key2count(int c)
4250{
4251 int h;
4252
4253 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4254 {
4255 h = pum_get_height();
4256 if (h > 3)
4257 h -= 2; // keep some context
4258 return h;
4259 }
4260 return 1;
4261}
4262
4263/*
4264 * Return TRUE if completion with "c" should insert the match, FALSE if only
4265 * to change the currently selected completion.
4266 */
4267 static int
4268ins_compl_use_match(int c)
4269{
4270 switch (c)
4271 {
4272 case K_UP:
4273 case K_DOWN:
4274 case K_PAGEDOWN:
4275 case K_KPAGEDOWN:
4276 case K_S_DOWN:
4277 case K_PAGEUP:
4278 case K_KPAGEUP:
4279 case K_S_UP:
4280 return FALSE;
4281 }
4282 return TRUE;
4283}
4284
4285/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004286 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4287 * completion)
4288 * Sets the global variables: compl_col, compl_length and compl_pattern.
4289 * Uses the global variables: compl_cont_status and ctrl_x_mode
4290 */
4291 static int
4292get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4293{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004294 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004295 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004296 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004297 {
4298 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4299 ;
4300 compl_col += ++startcol;
4301 compl_length = curs_col - startcol;
4302 }
4303 if (p_ic)
4304 compl_pattern = str_foldcase(line + compl_col,
4305 compl_length, NULL, 0);
4306 else
4307 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4308 if (compl_pattern == NULL)
4309 return FAIL;
4310 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004311 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004312 {
4313 char_u *prefix = (char_u *)"\\<";
4314
4315 // we need up to 2 extra chars for the prefix
4316 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4317 compl_length) + 2);
4318 if (compl_pattern == NULL)
4319 return FAIL;
4320 if (!vim_iswordp(line + compl_col)
4321 || (compl_col > 0
4322 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4323 prefix = (char_u *)"";
4324 STRCPY((char *)compl_pattern, prefix);
4325 (void)quote_meta(compl_pattern + STRLEN(prefix),
4326 line + compl_col, compl_length);
4327 }
4328 else if (--startcol < 0
4329 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4330 {
4331 // Match any word of at least two chars
4332 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4333 if (compl_pattern == NULL)
4334 return FAIL;
4335 compl_col += curs_col;
4336 compl_length = 0;
4337 }
4338 else
4339 {
4340 // Search the point of change class of multibyte character
4341 // or not a word single byte character backward.
4342 if (has_mbyte)
4343 {
4344 int base_class;
4345 int head_off;
4346
4347 startcol -= (*mb_head_off)(line, line + startcol);
4348 base_class = mb_get_class(line + startcol);
4349 while (--startcol >= 0)
4350 {
4351 head_off = (*mb_head_off)(line, line + startcol);
4352 if (base_class != mb_get_class(line + startcol
4353 - head_off))
4354 break;
4355 startcol -= head_off;
4356 }
4357 }
4358 else
4359 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4360 ;
4361 compl_col += ++startcol;
4362 compl_length = (int)curs_col - startcol;
4363 if (compl_length == 1)
4364 {
4365 // Only match word with at least two chars -- webb
4366 // there's no need to call quote_meta,
4367 // alloc(7) is enough -- Acevedo
4368 compl_pattern = alloc(7);
4369 if (compl_pattern == NULL)
4370 return FAIL;
4371 STRCPY((char *)compl_pattern, "\\<");
4372 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4373 STRCAT((char *)compl_pattern, "\\k");
4374 }
4375 else
4376 {
4377 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4378 compl_length) + 2);
4379 if (compl_pattern == NULL)
4380 return FAIL;
4381 STRCPY((char *)compl_pattern, "\\<");
4382 (void)quote_meta(compl_pattern + 2, line + compl_col,
4383 compl_length);
4384 }
4385 }
4386
4387 return OK;
4388}
4389
4390/*
4391 * Get the pattern, column and length for whole line completion or for the
4392 * complete() function.
4393 * Sets the global variables: compl_col, compl_length and compl_pattern.
4394 */
4395 static int
4396get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4397{
4398 compl_col = (colnr_T)getwhitecols(line);
4399 compl_length = (int)curs_col - (int)compl_col;
4400 if (compl_length < 0) // cursor in indent: empty pattern
4401 compl_length = 0;
4402 if (p_ic)
4403 compl_pattern = str_foldcase(line + compl_col, compl_length,
4404 NULL, 0);
4405 else
4406 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4407 if (compl_pattern == NULL)
4408 return FAIL;
4409
4410 return OK;
4411}
4412
4413/*
4414 * Get the pattern, column and length for filename completion.
4415 * Sets the global variables: compl_col, compl_length and compl_pattern.
4416 */
4417 static int
4418get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4419{
4420 // Go back to just before the first filename character.
4421 if (startcol > 0)
4422 {
4423 char_u *p = line + startcol;
4424
4425 MB_PTR_BACK(line, p);
4426 while (p > line && vim_isfilec(PTR2CHAR(p)))
4427 MB_PTR_BACK(line, p);
4428 if (p == line && vim_isfilec(PTR2CHAR(p)))
4429 startcol = 0;
4430 else
4431 startcol = (int)(p - line) + 1;
4432 }
4433
4434 compl_col += startcol;
4435 compl_length = (int)curs_col - startcol;
4436 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4437 if (compl_pattern == NULL)
4438 return FAIL;
4439
4440 return OK;
4441}
4442
4443/*
4444 * Get the pattern, column and length for command-line completion.
4445 * Sets the global variables: compl_col, compl_length and compl_pattern.
4446 */
4447 static int
4448get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4449{
4450 compl_pattern = vim_strnsave(line, curs_col);
4451 if (compl_pattern == NULL)
4452 return FAIL;
4453 set_cmd_context(&compl_xp, compl_pattern,
4454 (int)STRLEN(compl_pattern), curs_col, FALSE);
4455 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4456 || compl_xp.xp_context == EXPAND_NOTHING)
4457 // No completion possible, use an empty pattern to get a
4458 // "pattern not found" message.
4459 compl_col = curs_col;
4460 else
4461 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4462 compl_length = curs_col - compl_col;
4463
4464 return OK;
4465}
4466
4467/*
4468 * Get the pattern, column and length for user defined completion ('omnifunc',
4469 * 'completefunc' and 'thesaurusfunc')
4470 * Sets the global variables: compl_col, compl_length and compl_pattern.
4471 * Uses the global variable: spell_bad_len
4472 */
4473 static int
4474get_userdefined_compl_info(colnr_T curs_col UNUSED)
4475{
4476 int ret = FAIL;
4477
4478#ifdef FEAT_COMPL_FUNC
4479 // Call user defined function 'completefunc' with "a:findstart"
4480 // set to 1 to obtain the length of text to use for completion.
4481 char_u *line;
4482 typval_T args[3];
4483 int col;
4484 char_u *funcname;
4485 pos_T pos;
4486 int save_State = State;
4487 callback_T *cb;
4488
4489 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4490 // length as a string
4491 funcname = get_complete_funcname(ctrl_x_mode);
4492 if (*funcname == NUL)
4493 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004494 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004495 ? "completefunc" : "omnifunc");
4496 return FAIL;
4497 }
4498
4499 args[0].v_type = VAR_NUMBER;
4500 args[0].vval.v_number = 1;
4501 args[1].v_type = VAR_STRING;
4502 args[1].vval.v_string = (char_u *)"";
4503 args[2].v_type = VAR_UNKNOWN;
4504 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004505 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004506 cb = get_insert_callback(ctrl_x_mode);
4507 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004508 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004509
4510 State = save_State;
4511 curwin->w_cursor = pos; // restore the cursor position
4512 validate_cursor();
4513 if (!EQUAL_POS(curwin->w_cursor, pos))
4514 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004515 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004516 return FAIL;
4517 }
4518
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004519 // Return value -2 means the user complete function wants to cancel the
4520 // complete without an error, do the same if the function did not execute
4521 // successfully.
4522 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004523 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004524 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004525 if (col == -3)
4526 {
4527 ctrl_x_mode = CTRL_X_NORMAL;
4528 edit_submode = NULL;
4529 if (!shortmess(SHM_COMPLETIONMENU))
4530 msg_clr_cmdline();
4531 return FAIL;
4532 }
4533
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004534 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004535 // completion.
4536 compl_opt_refresh_always = FALSE;
4537 compl_opt_suppress_empty = FALSE;
4538
4539 if (col < 0)
4540 col = curs_col;
4541 compl_col = col;
4542 if (compl_col > curs_col)
4543 compl_col = curs_col;
4544
4545 // Setup variables for completion. Need to obtain "line" again,
4546 // it may have become invalid.
4547 line = ml_get(curwin->w_cursor.lnum);
4548 compl_length = curs_col - compl_col;
4549 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4550 if (compl_pattern == NULL)
4551 return FAIL;
4552
4553 ret = OK;
4554#endif
4555
4556 return ret;
4557}
4558
4559/*
4560 * Get the pattern, column and length for spell completion.
4561 * Sets the global variables: compl_col, compl_length and compl_pattern.
4562 * Uses the global variable: spell_bad_len
4563 */
4564 static int
4565get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4566{
4567 int ret = FAIL;
4568#ifdef FEAT_SPELL
4569 char_u *line;
4570
4571 if (spell_bad_len > 0)
4572 compl_col = curs_col - spell_bad_len;
4573 else
4574 compl_col = spell_word_start(startcol);
4575 if (compl_col >= (colnr_T)startcol)
4576 {
4577 compl_length = 0;
4578 compl_col = curs_col;
4579 }
4580 else
4581 {
4582 spell_expand_check_cap(compl_col);
4583 compl_length = (int)curs_col - compl_col;
4584 }
4585 // Need to obtain "line" again, it may have become invalid.
4586 line = ml_get(curwin->w_cursor.lnum);
4587 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4588 if (compl_pattern == NULL)
4589 return FAIL;
4590
4591 ret = OK;
4592#endif
4593
4594 return ret;
4595}
4596
4597/*
4598 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004599 * "startcol" - start column number of the completion pattern/text
4600 * "cur_col" - current cursor column
4601 * On return, "line_invalid" is set to TRUE, if the current line may have
4602 * become invalid and needs to be fetched again.
4603 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004604 */
4605 static int
4606compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4607{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004608 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004609 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4610 && !thesaurus_func_complete(ctrl_x_mode)))
4611 {
4612 return get_normal_compl_info(line, startcol, curs_col);
4613 }
4614 else if (ctrl_x_mode_line_or_eval())
4615 {
4616 return get_wholeline_compl_info(line, curs_col);
4617 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004618 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004619 {
4620 return get_filename_compl_info(line, startcol, curs_col);
4621 }
4622 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4623 {
4624 return get_cmdline_compl_info(line, curs_col);
4625 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004626 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004627 || thesaurus_func_complete(ctrl_x_mode))
4628 {
4629 if (get_userdefined_compl_info(curs_col) == FAIL)
4630 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004631 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004632 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004633 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004634 {
4635 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4636 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004637 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004638 }
4639 else
4640 {
4641 internal_error("ins_complete()");
4642 return FAIL;
4643 }
4644
4645 return OK;
4646}
4647
4648/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004649 * Continue an interrupted completion mode search in "line".
4650 *
4651 * If this same ctrl_x_mode has been interrupted use the text from
4652 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4653 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4654 * the same line as the cursor then fix it (the line has been split because it
4655 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4656 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004657 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004658 static void
4659ins_compl_continue_search(char_u *line)
4660{
4661 // it is a continued search
4662 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004663 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4664 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004665 {
4666 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4667 {
4668 // line (probably) wrapped, set compl_startpos to the
4669 // first non_blank in the line, if it is not a wordchar
4670 // include it to get a better pattern, but then we don't
4671 // want the "\\<" prefix, check it below
4672 compl_col = (colnr_T)getwhitecols(line);
4673 compl_startpos.col = compl_col;
4674 compl_startpos.lnum = curwin->w_cursor.lnum;
4675 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4676 }
4677 else
4678 {
4679 // S_IPOS was set when we inserted a word that was at the
4680 // beginning of the line, which means that we'll go to SOL
4681 // mode but first we need to redefine compl_startpos
4682 if (compl_cont_status & CONT_S_IPOS)
4683 {
4684 compl_cont_status |= CONT_SOL;
4685 compl_startpos.col = (colnr_T)(skipwhite(
4686 line + compl_length
4687 + compl_startpos.col) - line);
4688 }
4689 compl_col = compl_startpos.col;
4690 }
4691 compl_length = curwin->w_cursor.col - (int)compl_col;
4692 // IObuff is used to add a "word from the next line" would we
4693 // have enough space? just being paranoid
4694#define MIN_SPACE 75
4695 if (compl_length > (IOSIZE - MIN_SPACE))
4696 {
4697 compl_cont_status &= ~CONT_SOL;
4698 compl_length = (IOSIZE - MIN_SPACE);
4699 compl_col = curwin->w_cursor.col - compl_length;
4700 }
4701 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4702 if (compl_length < 1)
4703 compl_cont_status &= CONT_LOCAL;
4704 }
4705 else if (ctrl_x_mode_line_or_eval())
4706 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4707 else
4708 compl_cont_status = 0;
4709}
4710
4711/*
4712 * start insert mode completion
4713 */
4714 static int
4715ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004716{
4717 char_u *line;
4718 int startcol = 0; // column where searched text starts
4719 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004720 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004721 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004722 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004723
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004724 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004725
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004726 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004727 did_si = FALSE;
4728 can_si = FALSE;
4729 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004730 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004731 return FAIL;
4732
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004733 line = ml_get(curwin->w_cursor.lnum);
4734 curs_col = curwin->w_cursor.col;
4735 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004736
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004737 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4738 && compl_cont_mode == ctrl_x_mode)
4739 // this same ctrl-x_mode was interrupted previously. Continue the
4740 // completion.
4741 ins_compl_continue_search(line);
4742 else
4743 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004744
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004745 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004746 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004747 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004748 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004749 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4750 compl_cont_status = 0;
4751 compl_cont_status |= CONT_N_ADDS;
4752 compl_startpos = curwin->w_cursor;
4753 startcol = (int)curs_col;
4754 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004755 }
4756
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004757 // Work out completion pattern and original text -- webb
4758 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4759 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004760 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4761 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004762 // restore did_ai, so that adding comment leader works
4763 did_ai = save_did_ai;
4764 return FAIL;
4765 }
4766 // If "line" was changed while getting completion info get it again.
4767 if (line_invalid)
4768 line = ml_get(curwin->w_cursor.lnum);
4769
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004770 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004771 {
4772 edit_submode_pre = (char_u *)_(" Adding");
4773 if (ctrl_x_mode_line_or_eval())
4774 {
4775 // Insert a new line, keep indentation but ignore 'comments'.
4776 char_u *old = curbuf->b_p_com;
4777
4778 curbuf->b_p_com = (char_u *)"";
4779 compl_startpos.lnum = curwin->w_cursor.lnum;
4780 compl_startpos.col = compl_col;
4781 ins_eol('\r');
4782 curbuf->b_p_com = old;
4783 compl_length = 0;
4784 compl_col = curwin->w_cursor.col;
4785 }
4786 }
4787 else
4788 {
4789 edit_submode_pre = NULL;
4790 compl_startpos.col = compl_col;
4791 }
4792
4793 if (compl_cont_status & CONT_LOCAL)
4794 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4795 else
4796 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4797
4798 // If any of the original typed text has been changed we need to fix
4799 // the redo buffer.
4800 ins_compl_fixRedoBufForLeader(NULL);
4801
4802 // Always add completion for the original text.
4803 vim_free(compl_orig_text);
4804 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4805 if (p_ic)
4806 flags |= CP_ICASE;
4807 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4808 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4809 {
4810 VIM_CLEAR(compl_pattern);
4811 VIM_CLEAR(compl_orig_text);
4812 return FAIL;
4813 }
4814
4815 // showmode might reset the internal line pointers, so it must
4816 // be called before line = ml_get(), or when this address is no
4817 // longer needed. -- Acevedo.
4818 edit_submode_extra = (char_u *)_("-- Searching...");
4819 edit_submode_highl = HLF_COUNT;
4820 showmode();
4821 edit_submode_extra = NULL;
4822 out_flush();
4823
4824 return OK;
4825}
4826
4827/*
4828 * display the completion status message
4829 */
4830 static void
4831ins_compl_show_statusmsg(void)
4832{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004833 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004834 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004835 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004836 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004837 ? (char_u *)_(e_hitend)
4838 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004839 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004840 }
4841
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004842 if (edit_submode_extra == NULL)
4843 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004844 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004845 {
4846 edit_submode_extra = (char_u *)_("Back at original");
4847 edit_submode_highl = HLF_W;
4848 }
4849 else if (compl_cont_status & CONT_S_IPOS)
4850 {
4851 edit_submode_extra = (char_u *)_("Word from other line");
4852 edit_submode_highl = HLF_COUNT;
4853 }
4854 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4855 {
4856 edit_submode_extra = (char_u *)_("The only match");
4857 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004858 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004859 }
4860 else
4861 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004862#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004863 // Update completion sequence number when needed.
4864 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004865 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004866#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004867 // The match should always have a sequence number now, this is
4868 // just a safety check.
4869 if (compl_curr_match->cp_number != -1)
4870 {
4871 // Space for 10 text chars. + 2x10-digit no.s = 31.
4872 // Translations may need more than twice that.
4873 static char_u match_ref[81];
4874
4875 if (compl_matches > 0)
4876 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004877 _("match %d of %d"),
4878 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004879 else
4880 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004881 _("match %d"),
4882 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004883 edit_submode_extra = match_ref;
4884 edit_submode_highl = HLF_R;
4885 if (dollar_vcol >= 0)
4886 curs_columns(FALSE);
4887 }
4888 }
4889 }
4890
4891 // Show a message about what (completion) mode we're in.
4892 if (!compl_opt_suppress_empty)
4893 {
4894 showmode();
4895 if (!shortmess(SHM_COMPLETIONMENU))
4896 {
4897 if (edit_submode_extra != NULL)
4898 {
4899 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004900 {
4901 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004902 msg_attr((char *)edit_submode_extra,
4903 edit_submode_highl < HLF_COUNT
4904 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004905 msg_hist_off = FALSE;
4906 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004907 }
4908 else
4909 msg_clr_cmdline(); // necessary for "noshowmode"
4910 }
4911 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004912}
4913
4914/*
4915 * Do Insert mode completion.
4916 * Called when character "c" was typed, which has a meaning for completion.
4917 * Returns OK if completion was done, FAIL if something failed (out of mem).
4918 */
4919 int
4920ins_complete(int c, int enable_pum)
4921{
4922 int n;
4923 int save_w_wrow;
4924 int save_w_leftcol;
4925 int insert_match;
4926
4927 compl_direction = ins_compl_key2dir(c);
4928 insert_match = ins_compl_use_match(c);
4929
4930 if (!compl_started)
4931 {
4932 if (ins_compl_start() == FAIL)
4933 return FAIL;
4934 }
4935 else if (insert_match && stop_arrow() == FAIL)
4936 return FAIL;
4937
4938 compl_shown_match = compl_curr_match;
4939 compl_shows_dir = compl_direction;
4940
4941 // Find next match (and following matches).
4942 save_w_wrow = curwin->w_wrow;
4943 save_w_leftcol = curwin->w_leftcol;
4944 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4945
4946 // may undisplay the popup menu
4947 ins_compl_upd_pum();
4948
4949 if (n > 1) // all matches have been found
4950 compl_matches = n;
4951 compl_curr_match = compl_shown_match;
4952 compl_direction = compl_shows_dir;
4953
4954 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4955 // mode.
4956 if (got_int && !global_busy)
4957 {
4958 (void)vgetc();
4959 got_int = FALSE;
4960 }
4961
4962 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004963 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004964 {
4965 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4966 // because we couldn't expand anything at first place, but if we used
4967 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4968 // (such as M in M'exico) if not tried already. -- Acevedo
4969 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004970 || compl_status_adding()
4971 || (ctrl_x_mode_not_default()
4972 && !ctrl_x_mode_path_patterns()
4973 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004974 compl_cont_status &= ~CONT_N_ADDS;
4975 }
4976
4977 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4978 compl_cont_status |= CONT_S_IPOS;
4979 else
4980 compl_cont_status &= ~CONT_S_IPOS;
4981
4982 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004983
4984 // Show the popup menu, unless we got interrupted.
4985 if (enable_pum && !compl_interrupted)
4986 show_pum(save_w_wrow, save_w_leftcol);
4987
4988 compl_was_interrupted = compl_interrupted;
4989 compl_interrupted = FALSE;
4990
4991 return OK;
4992}
4993
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00004994/*
4995 * Remove (if needed) and show the popup menu
4996 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004997 static void
4998show_pum(int prev_w_wrow, int prev_w_leftcol)
4999{
5000 // RedrawingDisabled may be set when invoked through complete().
5001 int n = RedrawingDisabled;
5002
5003 RedrawingDisabled = 0;
5004
5005 // If the cursor moved or the display scrolled we need to remove the pum
5006 // first.
5007 setcursor();
5008 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5009 ins_compl_del_pum();
5010
5011 ins_compl_show_pum();
5012 setcursor();
5013 RedrawingDisabled = n;
5014}
5015
5016/*
5017 * Looks in the first "len" chars. of "src" for search-metachars.
5018 * If dest is not NULL the chars. are copied there quoting (with
5019 * a backslash) the metachars, and dest would be NUL terminated.
5020 * Returns the length (needed) of dest
5021 */
5022 static unsigned
5023quote_meta(char_u *dest, char_u *src, int len)
5024{
5025 unsigned m = (unsigned)len + 1; // one extra for the NUL
5026
5027 for ( ; --len >= 0; src++)
5028 {
5029 switch (*src)
5030 {
5031 case '.':
5032 case '*':
5033 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005034 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005035 break;
5036 // FALLTHROUGH
5037 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005038 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005039 break;
5040 // FALLTHROUGH
5041 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005042 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005043 break;
5044 // FALLTHROUGH
5045 case '^': // currently it's not needed.
5046 case '$':
5047 m++;
5048 if (dest != NULL)
5049 *dest++ = '\\';
5050 break;
5051 }
5052 if (dest != NULL)
5053 *dest++ = *src;
5054 // Copy remaining bytes of a multibyte character.
5055 if (has_mbyte)
5056 {
5057 int i, mb_len;
5058
5059 mb_len = (*mb_ptr2len)(src) - 1;
5060 if (mb_len > 0 && len >= mb_len)
5061 for (i = 0; i < mb_len; ++i)
5062 {
5063 --len;
5064 ++src;
5065 if (dest != NULL)
5066 *dest++ = *src;
5067 }
5068 }
5069 }
5070 if (dest != NULL)
5071 *dest = NUL;
5072
5073 return m;
5074}
5075
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005076#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005077 void
5078free_insexpand_stuff(void)
5079{
5080 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005081# ifdef FEAT_EVAL
5082 free_callback(&cfu_cb);
5083 free_callback(&ofu_cb);
5084 free_callback(&tsrfu_cb);
5085# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005086}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005087#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005088
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005089#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005090/*
5091 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5092 * spelled word, if there is one.
5093 */
5094 static void
5095spell_back_to_badword(void)
5096{
5097 pos_T tpos = curwin->w_cursor;
5098
5099 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5100 if (curwin->w_cursor.col != tpos.col)
5101 start_arrow(&tpos);
5102}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005103#endif