blob: e6d05bfe980de7bf4cad72e46d829119d52436db [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100127
128/*
129 * All the current matches are stored in a list.
130 * "compl_first_match" points to the start of the list.
131 * "compl_curr_match" points to the currently selected entry.
132 * "compl_shown_match" is different from compl_curr_match during
133 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000134 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100135 */
136static compl_T *compl_first_match = NULL;
137static compl_T *compl_curr_match = NULL;
138static compl_T *compl_shown_match = NULL;
139static compl_T *compl_old_match = NULL;
140
141// After using a cursor key <Enter> selects a match in the popup menu,
142// otherwise it inserts a line break.
143static int compl_enter_selects = FALSE;
144
145// When "compl_leader" is not NULL only matches that start with this string
146// are used.
147static char_u *compl_leader = NULL;
148
149static int compl_get_longest = FALSE; // put longest common string
150 // in compl_leader
151
152static int compl_no_insert = FALSE; // FALSE: select & insert
153 // TRUE: noinsert
154static int compl_no_select = FALSE; // FALSE: select & insert
155 // TRUE: noselect
156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
260 trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
266int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
270int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
271int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
272int ctrl_x_mode_path_patterns(void) {
273 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
274int ctrl_x_mode_path_defines(void) {
275 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
276int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
277int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200278int ctrl_x_mode_cmdline(void) {
279 return ctrl_x_mode == CTRL_X_CMDLINE
280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100281int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
282int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
283int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000284static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100285int ctrl_x_mode_line_or_eval(void) {
286 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
287
288/*
289 * Whether other than default completion has been selected.
290 */
291 int
292ctrl_x_mode_not_default(void)
293{
294 return ctrl_x_mode != CTRL_X_NORMAL;
295}
296
297/*
zeertzjqdca29d92021-08-31 19:12:51 +0200298 * Whether CTRL-X was typed without a following character,
299 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300 */
301 int
302ctrl_x_mode_not_defined_yet(void)
303{
304 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
305}
306
307/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000308 * Return TRUE if currently in "normal" or "adding" insert completion matches
309 * state
310 */
311 int
312compl_status_adding(void)
313{
314 return compl_cont_status & CONT_ADDING;
315}
316
317/*
318 * Return TRUE if the completion pattern includes start of line, just for
319 * word-wise expansion.
320 */
321 int
322compl_status_sol(void)
323{
324 return compl_cont_status & CONT_SOL;
325}
326
327/*
328 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
329 */
330 int
331compl_status_local(void)
332{
333 return compl_cont_status & CONT_LOCAL;
334}
335
336/*
337 * Clear the completion status flags
338 */
339 void
340compl_status_clear(void)
341{
342 compl_cont_status = 0;
343}
344
345/*
346 * Return TRUE if completion is using the forward direction matches
347 */
348 static int
349compl_dir_forward(void)
350{
351 return compl_direction == FORWARD;
352}
353
354/*
355 * Return TRUE if currently showing forward completion matches
356 */
357 static int
358compl_shows_dir_forward(void)
359{
360 return compl_shows_dir == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing backward completion matches
365 */
366 static int
367compl_shows_dir_backward(void)
368{
369 return compl_shows_dir == BACKWARD;
370}
371
372/*
373 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100374 */
375 int
376has_compl_option(int dict_opt)
377{
378 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200379#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100380 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200381#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100382 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100383 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
384#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100385 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386#endif
387 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 {
389 ctrl_x_mode = CTRL_X_NORMAL;
390 edit_submode = NULL;
391 msg_attr(dict_opt ? _("'dictionary' option is empty")
392 : _("'thesaurus' option is empty"),
393 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100394 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 {
396 vim_beep(BO_COMPL);
397 setcursor();
398 out_flush();
399#ifdef FEAT_EVAL
400 if (!get_vim_var_nr(VV_TESTING))
401#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100402 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 }
404 return FALSE;
405 }
406 return TRUE;
407}
408
409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000410 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100411 * This depends on the current mode.
412 */
413 int
414vim_is_ctrl_x_key(int c)
415{
416 // Always allow ^R - let its results then be checked
417 if (c == Ctrl_R)
418 return TRUE;
419
420 // Accept <PageUp> and <PageDown> if the popup menu is visible.
421 if (ins_compl_pum_key(c))
422 return TRUE;
423
424 switch (ctrl_x_mode)
425 {
426 case 0: // Not in any CTRL-X mode
427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
428 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200429 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100430 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
431 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
432 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
433 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
434 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200435 || c == Ctrl_S || c == Ctrl_K || c == 's'
436 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437 case CTRL_X_SCROLL:
438 return (c == Ctrl_Y || c == Ctrl_E);
439 case CTRL_X_WHOLE_LINE:
440 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
441 case CTRL_X_FILES:
442 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
443 case CTRL_X_DICTIONARY:
444 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_THESAURUS:
446 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_TAGS:
448 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
449#ifdef FEAT_FIND_ID
450 case CTRL_X_PATH_PATTERNS:
451 return (c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_PATH_DEFINES:
453 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
454#endif
455 case CTRL_X_CMDLINE:
456 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
457 || c == Ctrl_X);
458#ifdef FEAT_COMPL_FUNC
459 case CTRL_X_FUNCTION:
460 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_OMNI:
462 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_SPELL:
465 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_EVAL:
467 return (c == Ctrl_P || c == Ctrl_N);
468 }
469 internal_error("vim_is_ctrl_x_key()");
470 return FALSE;
471}
472
473/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000474 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000475 */
476 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478{
479 return match->cp_flags & CP_ORIGINAL_TEXT;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Returns TRUE if "match" is the first match in the completion list.
484 */
485 static int
486is_first_match(compl_T *match)
487{
488 return match == compl_first_match;
489}
490
491/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100492 * Return TRUE when character "c" is part of the item currently being
493 * completed. Used to decide whether to abandon complete mode when the menu
494 * is visible.
495 */
496 int
497ins_compl_accept_char(int c)
498{
499 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
500 // When expanding an identifier only accept identifier chars.
501 return vim_isIDc(c);
502
503 switch (ctrl_x_mode)
504 {
505 case CTRL_X_FILES:
506 // When expanding file name only accept file name chars. But not
507 // path separators, so that "proto/<Tab>" expands files in
508 // "proto", not "proto/" as a whole
509 return vim_isfilec(c) && !vim_ispathsep(c);
510
511 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200512 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 case CTRL_X_OMNI:
514 // Command line and Omni completion can work with just about any
515 // printable character, but do stop at white space.
516 return vim_isprintc(c) && !VIM_ISWHITE(c);
517
518 case CTRL_X_WHOLE_LINE:
519 // For while line completion a space can be part of the line.
520 return vim_isprintc(c);
521 }
522 return vim_iswordc(c);
523}
524
525/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000526 * Get the completed text by inferring the case of the originally typed text.
527 */
528 static char_u *
529ins_compl_infercase_gettext(
530 char_u *str,
531 int actual_len,
532 int actual_compl_length,
533 int min_len)
534{
535 int *wca; // Wide character array.
536 char_u *p;
537 int i, c;
538 int has_lower = FALSE;
539 int was_letter = FALSE;
540
541 IObuff[0] = NUL;
542
543 // Allocate wide character array for the completion and fill it.
544 wca = ALLOC_MULT(int, actual_len);
545 if (wca == NULL)
546 return IObuff;
547
548 p = str;
549 for (i = 0; i < actual_len; ++i)
550 if (has_mbyte)
551 wca[i] = mb_ptr2char_adv(&p);
552 else
553 wca[i] = *(p++);
554
555 // Rule 1: Were any chars converted to lower?
556 p = compl_orig_text;
557 for (i = 0; i < min_len; ++i)
558 {
559 if (has_mbyte)
560 c = mb_ptr2char_adv(&p);
561 else
562 c = *(p++);
563 if (MB_ISLOWER(c))
564 {
565 has_lower = TRUE;
566 if (MB_ISUPPER(wca[i]))
567 {
568 // Rule 1 is satisfied.
569 for (i = actual_compl_length; i < actual_len; ++i)
570 wca[i] = MB_TOLOWER(wca[i]);
571 break;
572 }
573 }
574 }
575
576 // Rule 2: No lower case, 2nd consecutive letter converted to
577 // upper case.
578 if (!has_lower)
579 {
580 p = compl_orig_text;
581 for (i = 0; i < min_len; ++i)
582 {
583 if (has_mbyte)
584 c = mb_ptr2char_adv(&p);
585 else
586 c = *(p++);
587 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
588 {
589 // Rule 2 is satisfied.
590 for (i = actual_compl_length; i < actual_len; ++i)
591 wca[i] = MB_TOUPPER(wca[i]);
592 break;
593 }
594 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
595 }
596 }
597
598 // Copy the original case of the part we typed.
599 p = compl_orig_text;
600 for (i = 0; i < min_len; ++i)
601 {
602 if (has_mbyte)
603 c = mb_ptr2char_adv(&p);
604 else
605 c = *(p++);
606 if (MB_ISLOWER(c))
607 wca[i] = MB_TOLOWER(wca[i]);
608 else if (MB_ISUPPER(c))
609 wca[i] = MB_TOUPPER(wca[i]);
610 }
611
612 // Generate encoding specific output from wide character array.
613 // Multi-byte characters can occupy up to five bytes more than
614 // ASCII characters, and we also need one byte for NUL, so stay
615 // six bytes away from the edge of IObuff.
616 p = IObuff;
617 i = 0;
618 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
619 if (has_mbyte)
620 p += (*mb_char2bytes)(wca[i++], p);
621 else
622 *(p++) = wca[i++];
623 *p = NUL;
624
625 vim_free(wca);
626
627 return IObuff;
628}
629
630/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
632 * case of the originally typed text is used, and the case of the completed
633 * text is inferred, ie this tries to work out what case you probably wanted
634 * the rest of the word to be in -- webb
635 */
636 int
637ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200638 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100639 int len,
640 int icase,
641 char_u *fname,
642 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200643 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100644{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200645 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100646 char_u *p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100647 int actual_len; // Take multi-byte characters
648 int actual_compl_length; // into account.
649 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200650 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100651
652 if (p_ic && curbuf->b_p_inf && len > 0)
653 {
654 // Infer case of completed part.
655
656 // Find actual length of completion.
657 if (has_mbyte)
658 {
659 p = str;
660 actual_len = 0;
661 while (*p != NUL)
662 {
663 MB_PTR_ADV(p);
664 ++actual_len;
665 }
666 }
667 else
668 actual_len = len;
669
670 // Find actual length of original text.
671 if (has_mbyte)
672 {
673 p = compl_orig_text;
674 actual_compl_length = 0;
675 while (*p != NUL)
676 {
677 MB_PTR_ADV(p);
678 ++actual_compl_length;
679 }
680 }
681 else
682 actual_compl_length = compl_length;
683
684 // "actual_len" may be smaller than "actual_compl_length" when using
685 // thesaurus, only use the minimum when comparing.
686 min_len = actual_len < actual_compl_length
687 ? actual_len : actual_compl_length;
688
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000689 str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
690 min_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200692 if (cont_s_ipos)
693 flags |= CP_CONT_S_IPOS;
694 if (icase)
695 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696
Bram Moolenaar08928322020-01-04 14:32:48 +0100697 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698}
699
700/*
701 * Add a match to the list of matches.
702 * If the given string is already in the list of completions, then return
703 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
704 * maybe because alloc() returns NULL, then FAIL is returned.
705 */
706 static int
707ins_compl_add(
708 char_u *str,
709 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100710 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 char_u **cptext, // extra text for popup menu or NULL
712 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100713 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200714 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100715 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716{
717 compl_T *match;
718 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200719 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100720
Bram Moolenaarceb06192021-04-04 15:05:22 +0200721 if (flags & CP_FAST)
722 fast_breakcheck();
723 else
724 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 if (got_int)
726 return FAIL;
727 if (len < 0)
728 len = (int)STRLEN(str);
729
730 // If the same match is already present, don't add it.
731 if (compl_first_match != NULL && !adup)
732 {
733 match = compl_first_match;
734 do
735 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000736 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737 && STRNCMP(match->cp_str, str, len) == 0
738 && match->cp_str[len] == NUL)
739 return NOTDONE;
740 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000741 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100742 }
743
744 // Remove any popup menu before changing the list of matches.
745 ins_compl_del_pum();
746
747 // Allocate a new match structure.
748 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200749 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100750 if (match == NULL)
751 return FAIL;
752 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200753 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100754 match->cp_number = 0;
755 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
756 {
757 vim_free(match);
758 return FAIL;
759 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100760
761 // match-fname is:
762 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200763 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100764 // - NULL otherwise. --Acevedo
765 if (fname != NULL
766 && compl_curr_match != NULL
767 && compl_curr_match->cp_fname != NULL
768 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
769 match->cp_fname = compl_curr_match->cp_fname;
770 else if (fname != NULL)
771 {
772 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200773 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 }
775 else
776 match->cp_fname = NULL;
777 match->cp_flags = flags;
778
779 if (cptext != NULL)
780 {
781 int i;
782
783 for (i = 0; i < CPT_COUNT; ++i)
784 if (cptext[i] != NULL && *cptext[i] != NUL)
785 match->cp_text[i] = vim_strsave(cptext[i]);
786 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100787#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100788 if (user_data != NULL)
789 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100790#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100791
792 // Link the new match structure in the list of matches.
793 if (compl_first_match == NULL)
794 match->cp_next = match->cp_prev = NULL;
795 else if (dir == FORWARD)
796 {
797 match->cp_next = compl_curr_match->cp_next;
798 match->cp_prev = compl_curr_match;
799 }
800 else // BACKWARD
801 {
802 match->cp_next = compl_curr_match;
803 match->cp_prev = compl_curr_match->cp_prev;
804 }
805 if (match->cp_next)
806 match->cp_next->cp_prev = match;
807 if (match->cp_prev)
808 match->cp_prev->cp_next = match;
809 else // if there's nothing before, it is the first match
810 compl_first_match = match;
811 compl_curr_match = match;
812
813 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200814 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 ins_compl_longest_match(match);
816
817 return OK;
818}
819
820/*
821 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200822 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 */
824 static int
825ins_compl_equal(compl_T *match, char_u *str, int len)
826{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200827 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200828 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200829 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100830 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
831 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
832}
833
834/*
835 * Reduce the longest common string for match "match".
836 */
837 static void
838ins_compl_longest_match(compl_T *match)
839{
840 char_u *p, *s;
841 int c1, c2;
842 int had_match;
843
844 if (compl_leader == NULL)
845 {
846 // First match, use it as a whole.
847 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000848 if (compl_leader == NULL)
849 return;
850
851 had_match = (curwin->w_cursor.col > compl_col);
852 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000853 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000854 ins_redraw(FALSE);
855
856 // When the match isn't there (to avoid matching itself) remove it
857 // again after redrawing.
858 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100860 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000861
862 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000864
865 // Reduce the text if this match differs from compl_leader.
866 p = compl_leader;
867 s = match->cp_str;
868 while (*p != NUL)
869 {
870 if (has_mbyte)
871 {
872 c1 = mb_ptr2char(p);
873 c2 = mb_ptr2char(s);
874 }
875 else
876 {
877 c1 = *p;
878 c2 = *s;
879 }
880 if ((match->cp_flags & CP_ICASE)
881 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
882 break;
883 if (has_mbyte)
884 {
885 MB_PTR_ADV(p);
886 MB_PTR_ADV(s);
887 }
888 else
889 {
890 ++p;
891 ++s;
892 }
893 }
894
895 if (*p != NUL)
896 {
897 // Leader was shortened, need to change the inserted text.
898 *p = NUL;
899 had_match = (curwin->w_cursor.col > compl_col);
900 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000901 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000902 ins_redraw(FALSE);
903
904 // When the match isn't there (to avoid matching itself) remove it
905 // again after redrawing.
906 if (!had_match)
907 ins_compl_delete();
908 }
909
910 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100911}
912
913/*
914 * Add an array of matches to the list of matches.
915 * Frees matches[].
916 */
917 static void
918ins_compl_add_matches(
919 int num_matches,
920 char_u **matches,
921 int icase)
922{
923 int i;
924 int add_r = OK;
925 int dir = compl_direction;
926
927 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100928 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200929 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 // if dir was BACKWARD then honor it just once
931 dir = FORWARD;
932 FreeWild(num_matches, matches);
933}
934
935/*
936 * Make the completion list cyclic.
937 * Return the number of matches (excluding the original).
938 */
939 static int
940ins_compl_make_cyclic(void)
941{
942 compl_T *match;
943 int count = 0;
944
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000945 if (compl_first_match == NULL)
946 return 0;
947
948 // Find the end of the list.
949 match = compl_first_match;
950 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000951 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100952 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000953 match = match->cp_next;
954 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100955 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000956 match->cp_next = compl_first_match;
957 compl_first_match->cp_prev = match;
958
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100959 return count;
960}
961
962/*
963 * Return whether there currently is a shown match.
964 */
965 int
966ins_compl_has_shown_match(void)
967{
968 return compl_shown_match == NULL
969 || compl_shown_match != compl_shown_match->cp_next;
970}
971
972/*
973 * Return whether the shown match is long enough.
974 */
975 int
976ins_compl_long_shown_match(void)
977{
978 return (int)STRLEN(compl_shown_match->cp_str)
979 > curwin->w_cursor.col - compl_col;
980}
981
982/*
983 * Set variables that store noselect and noinsert behavior from the
984 * 'completeopt' value.
985 */
986 void
987completeopt_was_set(void)
988{
989 compl_no_insert = FALSE;
990 compl_no_select = FALSE;
991 if (strstr((char *)p_cot, "noselect") != NULL)
992 compl_no_select = TRUE;
993 if (strstr((char *)p_cot, "noinsert") != NULL)
994 compl_no_insert = TRUE;
995}
996
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100997
998// "compl_match_array" points the currently displayed list of entries in the
999// popup menu. It is NULL when there is no popup menu.
1000static pumitem_T *compl_match_array = NULL;
1001static int compl_match_arraysize;
1002
1003/*
1004 * Update the screen and when there is any scrolling remove the popup menu.
1005 */
1006 static void
1007ins_compl_upd_pum(void)
1008{
1009 int h;
1010
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001011 if (compl_match_array == NULL)
1012 return;
1013
1014 h = curwin->w_cline_height;
1015 // Update the screen later, before drawing the popup menu over it.
1016 pum_call_update_screen();
1017 if (h != curwin->w_cline_height)
1018 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001019}
1020
1021/*
1022 * Remove any popup menu.
1023 */
1024 static void
1025ins_compl_del_pum(void)
1026{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001027 if (compl_match_array == NULL)
1028 return;
1029
1030 pum_undisplay();
1031 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001032}
1033
1034/*
1035 * Return TRUE if the popup menu should be displayed.
1036 */
1037 int
1038pum_wanted(void)
1039{
1040 // 'completeopt' must contain "menu" or "menuone"
1041 if (vim_strchr(p_cot, 'm') == NULL)
1042 return FALSE;
1043
1044 // The display looks bad on a B&W display.
1045 if (t_colors < 8
1046#ifdef FEAT_GUI
1047 && !gui.in_use
1048#endif
1049 )
1050 return FALSE;
1051 return TRUE;
1052}
1053
1054/*
1055 * Return TRUE if there are two or more matches to be shown in the popup menu.
1056 * One if 'completopt' contains "menuone".
1057 */
1058 static int
1059pum_enough_matches(void)
1060{
1061 compl_T *compl;
1062 int i;
1063
1064 // Don't display the popup menu if there are no matches or there is only
1065 // one (ignoring the original text).
1066 compl = compl_first_match;
1067 i = 0;
1068 do
1069 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001070 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001071 break;
1072 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001073 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001074
1075 if (strstr((char *)p_cot, "menuone") != NULL)
1076 return (i >= 1);
1077 return (i >= 2);
1078}
1079
Bram Moolenaar3075a452021-11-17 15:51:52 +00001080#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001081/*
1082 * Allocate Dict for the completed item.
1083 * { word, abbr, menu, kind, info }
1084 */
1085 static dict_T *
1086ins_compl_dict_alloc(compl_T *match)
1087{
1088 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1089
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001090 if (dict == NULL)
1091 return NULL;
1092
1093 dict_add_string(dict, "word", match->cp_str);
1094 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1095 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1096 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1097 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1098 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1099 dict_add_string(dict, "user_data", (char_u *)"");
1100 else
1101 dict_add_tv(dict, "user_data", &match->cp_user_data);
1102
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001103 return dict;
1104}
1105
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001106/*
1107 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1108 * completion menu is changed.
1109 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001110 static void
1111trigger_complete_changed_event(int cur)
1112{
1113 dict_T *v_event;
1114 dict_T *item;
1115 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001116 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001117
1118 if (recursive)
1119 return;
1120
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001121 if (cur < 0)
1122 item = dict_alloc();
1123 else
1124 item = ins_compl_dict_alloc(compl_curr_match);
1125 if (item == NULL)
1126 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001127 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001128 dict_add_dict(v_event, "completed_item", item);
1129 pum_set_event_info(v_event);
1130 dict_set_items_ro(v_event);
1131
1132 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001133 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001134 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001135 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001136 recursive = FALSE;
1137
Bram Moolenaar3075a452021-11-17 15:51:52 +00001138 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001139}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001140#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001141
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001142/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001143 * Build a popup menu to show the completion matches.
1144 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1145 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001146 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001147 static int
1148ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001149{
1150 compl_T *compl;
1151 compl_T *shown_compl = NULL;
1152 int did_find_shown_match = FALSE;
1153 int shown_match_ok = FALSE;
1154 int i;
1155 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001156 int lead_len = 0;
1157
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001158 // Need to build the popup menu list.
1159 compl_match_arraysize = 0;
1160 compl = compl_first_match;
1161 if (compl_leader != NULL)
1162 lead_len = (int)STRLEN(compl_leader);
1163
1164 do
1165 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001166 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001167 && (compl_leader == NULL
1168 || ins_compl_equal(compl, compl_leader, lead_len)))
1169 ++compl_match_arraysize;
1170 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001171 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001172
1173 if (compl_match_arraysize == 0)
1174 return -1;
1175
1176 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1177 if (compl_match_array == NULL)
1178 return -1;
1179
1180 // If the current match is the original text don't find the first
1181 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001182 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001183 shown_match_ok = TRUE;
1184
1185 i = 0;
1186 compl = compl_first_match;
1187 do
1188 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001189 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001190 && (compl_leader == NULL
1191 || ins_compl_equal(compl, compl_leader, lead_len)))
1192 {
1193 if (!shown_match_ok)
1194 {
1195 if (compl == compl_shown_match || did_find_shown_match)
1196 {
1197 // This item is the shown match or this is the
1198 // first displayed item after the shown match.
1199 compl_shown_match = compl;
1200 did_find_shown_match = TRUE;
1201 shown_match_ok = TRUE;
1202 }
1203 else
1204 // Remember this displayed match for when the
1205 // shown match is just below it.
1206 shown_compl = compl;
1207 cur = i;
1208 }
1209
1210 if (compl->cp_text[CPT_ABBR] != NULL)
1211 compl_match_array[i].pum_text =
1212 compl->cp_text[CPT_ABBR];
1213 else
1214 compl_match_array[i].pum_text = compl->cp_str;
1215 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1216 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1217 if (compl->cp_text[CPT_MENU] != NULL)
1218 compl_match_array[i++].pum_extra =
1219 compl->cp_text[CPT_MENU];
1220 else
1221 compl_match_array[i++].pum_extra = compl->cp_fname;
1222 }
1223
1224 if (compl == compl_shown_match)
1225 {
1226 did_find_shown_match = TRUE;
1227
1228 // When the original text is the shown match don't set
1229 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001230 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001231 shown_match_ok = TRUE;
1232
1233 if (!shown_match_ok && shown_compl != NULL)
1234 {
1235 // The shown match isn't displayed, set it to the
1236 // previously displayed match.
1237 compl_shown_match = shown_compl;
1238 shown_match_ok = TRUE;
1239 }
1240 }
1241 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001242 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001243
1244 if (!shown_match_ok) // no displayed match at all
1245 cur = -1;
1246
1247 return cur;
1248}
1249
1250/*
1251 * Show the popup menu for the list of matches.
1252 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1253 */
1254 void
1255ins_compl_show_pum(void)
1256{
1257 int i;
1258 int cur = -1;
1259 colnr_T col;
1260
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001261 if (!pum_wanted() || !pum_enough_matches())
1262 return;
1263
1264#if defined(FEAT_EVAL)
1265 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001266 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001267#endif
1268
1269 // Update the screen later, before drawing the popup menu over it.
1270 pum_call_update_screen();
1271
1272 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001273 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001274 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001275 else
1276 {
1277 // popup menu already exists, only need to find the current item.
1278 for (i = 0; i < compl_match_arraysize; ++i)
1279 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1280 || compl_match_array[i].pum_text
1281 == compl_shown_match->cp_text[CPT_ABBR])
1282 {
1283 cur = i;
1284 break;
1285 }
1286 }
1287
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 if (compl_match_array == NULL)
1289 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001290
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001291 // In Replace mode when a $ is displayed at the end of the line only
1292 // part of the screen would be updated. We do need to redraw here.
1293 dollar_vcol = -1;
1294
1295 // Compute the screen column of the start of the completed text.
1296 // Use the cursor to get all wrapping and other settings right.
1297 col = curwin->w_cursor.col;
1298 curwin->w_cursor.col = compl_col;
1299 pum_display(compl_match_array, compl_match_arraysize, cur);
1300 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001301
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001302#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001303 if (has_completechanged())
1304 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001305#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001306}
1307
1308#define DICT_FIRST (1) // use just first element in "dict"
1309#define DICT_EXACT (2) // "dict" is the exact name of a file
1310
1311/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001312 * Add any identifiers that match the given pattern "pat" in the list of
1313 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001314 */
1315 static void
1316ins_compl_dictionaries(
1317 char_u *dict_start,
1318 char_u *pat,
1319 int flags, // DICT_FIRST and/or DICT_EXACT
1320 int thesaurus) // Thesaurus completion
1321{
1322 char_u *dict = dict_start;
1323 char_u *ptr;
1324 char_u *buf;
1325 regmatch_T regmatch;
1326 char_u **files;
1327 int count;
1328 int save_p_scs;
1329 int dir = compl_direction;
1330
1331 if (*dict == NUL)
1332 {
1333#ifdef FEAT_SPELL
1334 // When 'dictionary' is empty and spell checking is enabled use
1335 // "spell".
1336 if (!thesaurus && curwin->w_p_spell)
1337 dict = (char_u *)"spell";
1338 else
1339#endif
1340 return;
1341 }
1342
1343 buf = alloc(LSIZE);
1344 if (buf == NULL)
1345 return;
1346 regmatch.regprog = NULL; // so that we can goto theend
1347
1348 // If 'infercase' is set, don't use 'smartcase' here
1349 save_p_scs = p_scs;
1350 if (curbuf->b_p_inf)
1351 p_scs = FALSE;
1352
1353 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1354 // to only match at the start of a line. Otherwise just match the
1355 // pattern. Also need to double backslashes.
1356 if (ctrl_x_mode_line_or_eval())
1357 {
1358 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1359 size_t len;
1360
1361 if (pat_esc == NULL)
1362 goto theend;
1363 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001364 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001365 if (ptr == NULL)
1366 {
1367 vim_free(pat_esc);
1368 goto theend;
1369 }
1370 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1371 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1372 vim_free(pat_esc);
1373 vim_free(ptr);
1374 }
1375 else
1376 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001377 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001378 if (regmatch.regprog == NULL)
1379 goto theend;
1380 }
1381
1382 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1383 regmatch.rm_ic = ignorecase(pat);
1384 while (*dict != NUL && !got_int && !compl_interrupted)
1385 {
1386 // copy one dictionary file name into buf
1387 if (flags == DICT_EXACT)
1388 {
1389 count = 1;
1390 files = &dict;
1391 }
1392 else
1393 {
1394 // Expand wildcards in the dictionary name, but do not allow
1395 // backticks (for security, the 'dict' option may have been set in
1396 // a modeline).
1397 copy_option_part(&dict, buf, LSIZE, ",");
1398# ifdef FEAT_SPELL
1399 if (!thesaurus && STRCMP(buf, "spell") == 0)
1400 count = -1;
1401 else
1402# endif
1403 if (vim_strchr(buf, '`') != NULL
1404 || expand_wildcards(1, &buf, &count, &files,
1405 EW_FILE|EW_SILENT) != OK)
1406 count = 0;
1407 }
1408
1409# ifdef FEAT_SPELL
1410 if (count == -1)
1411 {
1412 // Complete from active spelling. Skip "\<" in the pattern, we
1413 // don't use it as a RE.
1414 if (pat[0] == '\\' && pat[1] == '<')
1415 ptr = pat + 2;
1416 else
1417 ptr = pat;
1418 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1419 }
1420 else
1421# endif
1422 if (count > 0) // avoid warning for using "files" uninit
1423 {
1424 ins_compl_files(count, files, thesaurus, flags,
1425 &regmatch, buf, &dir);
1426 if (flags != DICT_EXACT)
1427 FreeWild(count, files);
1428 }
1429 if (flags != 0)
1430 break;
1431 }
1432
1433theend:
1434 p_scs = save_p_scs;
1435 vim_regfree(regmatch.regprog);
1436 vim_free(buf);
1437}
1438
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001439/*
1440 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1441 * skipping the word at 'skip_word'. Returns OK on success.
1442 */
1443 static int
1444thesarurs_add_words_in_line(
1445 char_u *fname,
1446 char_u **buf_arg,
1447 int dir,
1448 char_u *skip_word)
1449{
1450 int status = OK;
1451 char_u *ptr;
1452 char_u *wstart;
1453
1454 // Add the other matches on the line
1455 ptr = *buf_arg;
1456 while (!got_int)
1457 {
1458 // Find start of the next word. Skip white
1459 // space and punctuation.
1460 ptr = find_word_start(ptr);
1461 if (*ptr == NUL || *ptr == NL)
1462 break;
1463 wstart = ptr;
1464
1465 // Find end of the word.
1466 if (has_mbyte)
1467 // Japanese words may have characters in
1468 // different classes, only separate words
1469 // with single-byte non-word characters.
1470 while (*ptr != NUL)
1471 {
1472 int l = (*mb_ptr2len)(ptr);
1473
1474 if (l < 2 && !vim_iswordc(*ptr))
1475 break;
1476 ptr += l;
1477 }
1478 else
1479 ptr = find_word_end(ptr);
1480
1481 // Add the word. Skip the regexp match.
1482 if (wstart != skip_word)
1483 {
1484 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1485 fname, dir, FALSE);
1486 if (status == FAIL)
1487 break;
1488 }
1489 }
1490
1491 *buf_arg = ptr;
1492 return status;
1493}
1494
1495/*
1496 * Process "count" dictionary/thesaurus "files" and add the text matching
1497 * "regmatch".
1498 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001499 static void
1500ins_compl_files(
1501 int count,
1502 char_u **files,
1503 int thesaurus,
1504 int flags,
1505 regmatch_T *regmatch,
1506 char_u *buf,
1507 int *dir)
1508{
1509 char_u *ptr;
1510 int i;
1511 FILE *fp;
1512 int add_r;
1513
1514 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1515 {
1516 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1517 if (flags != DICT_EXACT)
1518 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001519 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001520 vim_snprintf((char *)IObuff, IOSIZE,
1521 _("Scanning dictionary: %s"), (char *)files[i]);
1522 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1523 }
1524
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001525 if (fp == NULL)
1526 continue;
1527
1528 // Read dictionary file line by line.
1529 // Check each line for a match.
1530 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001531 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001532 ptr = buf;
1533 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001534 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001535 ptr = regmatch->startp[0];
1536 if (ctrl_x_mode_line_or_eval())
1537 ptr = find_line_end(ptr);
1538 else
1539 ptr = find_word_end(ptr);
1540 add_r = ins_compl_add_infercase(regmatch->startp[0],
1541 (int)(ptr - regmatch->startp[0]),
1542 p_ic, files[i], *dir, FALSE);
1543 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001544 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001545 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001546 ptr = buf;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001547 add_r = thesarurs_add_words_in_line(files[i], &ptr, *dir,
1548 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001549 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001550 if (add_r == OK)
1551 // if dir was BACKWARD then honor it just once
1552 *dir = FORWARD;
1553 else if (add_r == FAIL)
1554 break;
1555 // avoid expensive call to vim_regexec() when at end
1556 // of line
1557 if (*ptr == '\n' || got_int)
1558 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001559 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001560 line_breakcheck();
1561 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001562 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001563 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001564 }
1565}
1566
1567/*
1568 * Find the start of the next word.
1569 * Returns a pointer to the first char of the word. Also stops at a NUL.
1570 */
1571 char_u *
1572find_word_start(char_u *ptr)
1573{
1574 if (has_mbyte)
1575 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1576 ptr += (*mb_ptr2len)(ptr);
1577 else
1578 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1579 ++ptr;
1580 return ptr;
1581}
1582
1583/*
1584 * Find the end of the word. Assumes it starts inside a word.
1585 * Returns a pointer to just after the word.
1586 */
1587 char_u *
1588find_word_end(char_u *ptr)
1589{
1590 int start_class;
1591
1592 if (has_mbyte)
1593 {
1594 start_class = mb_get_class(ptr);
1595 if (start_class > 1)
1596 while (*ptr != NUL)
1597 {
1598 ptr += (*mb_ptr2len)(ptr);
1599 if (mb_get_class(ptr) != start_class)
1600 break;
1601 }
1602 }
1603 else
1604 while (vim_iswordc(*ptr))
1605 ++ptr;
1606 return ptr;
1607}
1608
1609/*
1610 * Find the end of the line, omitting CR and NL at the end.
1611 * Returns a pointer to just after the line.
1612 */
1613 static char_u *
1614find_line_end(char_u *ptr)
1615{
1616 char_u *s;
1617
1618 s = ptr + STRLEN(ptr);
1619 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1620 --s;
1621 return s;
1622}
1623
1624/*
1625 * Free the list of completions
1626 */
1627 static void
1628ins_compl_free(void)
1629{
1630 compl_T *match;
1631 int i;
1632
1633 VIM_CLEAR(compl_pattern);
1634 VIM_CLEAR(compl_leader);
1635
1636 if (compl_first_match == NULL)
1637 return;
1638
1639 ins_compl_del_pum();
1640 pum_clear();
1641
1642 compl_curr_match = compl_first_match;
1643 do
1644 {
1645 match = compl_curr_match;
1646 compl_curr_match = compl_curr_match->cp_next;
1647 vim_free(match->cp_str);
1648 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001649 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001650 vim_free(match->cp_fname);
1651 for (i = 0; i < CPT_COUNT; ++i)
1652 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001653#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001654 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001655#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001656 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001657 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001658 compl_first_match = compl_curr_match = NULL;
1659 compl_shown_match = NULL;
1660 compl_old_match = NULL;
1661}
1662
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001663/*
1664 * Reset/clear the completion state.
1665 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001666 void
1667ins_compl_clear(void)
1668{
1669 compl_cont_status = 0;
1670 compl_started = FALSE;
1671 compl_matches = 0;
1672 VIM_CLEAR(compl_pattern);
1673 VIM_CLEAR(compl_leader);
1674 edit_submode_extra = NULL;
1675 VIM_CLEAR(compl_orig_text);
1676 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001677#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001678 // clear v:completed_item
1679 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001680#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681}
1682
1683/*
1684 * Return TRUE when Insert completion is active.
1685 */
1686 int
1687ins_compl_active(void)
1688{
1689 return compl_started;
1690}
1691
1692/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001693 * Selected one of the matches. When FALSE the match was edited or using the
1694 * longest common string.
1695 */
1696 int
1697ins_compl_used_match(void)
1698{
1699 return compl_used_match;
1700}
1701
1702/*
1703 * Initialize get longest common string.
1704 */
1705 void
1706ins_compl_init_get_longest(void)
1707{
1708 compl_get_longest = FALSE;
1709}
1710
1711/*
1712 * Returns TRUE when insert completion is interrupted.
1713 */
1714 int
1715ins_compl_interrupted(void)
1716{
1717 return compl_interrupted;
1718}
1719
1720/*
1721 * Returns TRUE if the <Enter> key selects a match in the completion popup
1722 * menu.
1723 */
1724 int
1725ins_compl_enter_selects(void)
1726{
1727 return compl_enter_selects;
1728}
1729
1730/*
1731 * Return the column where the text starts that is being completed
1732 */
1733 colnr_T
1734ins_compl_col(void)
1735{
1736 return compl_col;
1737}
1738
1739/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001740 * Return the length in bytes of the text being completed
1741 */
1742 int
1743ins_compl_len(void)
1744{
1745 return compl_length;
1746}
1747
1748/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001749 * Delete one character before the cursor and show the subset of the matches
1750 * that match the word that is now before the cursor.
1751 * Returns the character to be used, NUL if the work is done and another char
1752 * to be got from the user.
1753 */
1754 int
1755ins_compl_bs(void)
1756{
1757 char_u *line;
1758 char_u *p;
1759
1760 line = ml_get_curline();
1761 p = line + curwin->w_cursor.col;
1762 MB_PTR_BACK(line, p);
1763
1764 // Stop completion when the whole word was deleted. For Omni completion
1765 // allow the word to be deleted, we won't match everything.
1766 // Respect the 'backspace' option.
1767 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001768 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1769 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001770 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1771 - compl_length < 0))
1772 return K_BS;
1773
1774 // Deleted more than what was used to find matches or didn't finish
1775 // finding all matches: need to look for matches all over again.
1776 if (curwin->w_cursor.col <= compl_col + compl_length
1777 || ins_compl_need_restart())
1778 ins_compl_restart();
1779
1780 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001781 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001782 if (compl_leader == NULL)
1783 return K_BS;
1784
1785 ins_compl_new_leader();
1786 if (compl_shown_match != NULL)
1787 // Make sure current match is not a hidden item.
1788 compl_curr_match = compl_shown_match;
1789 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001790}
1791
1792/*
1793 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1794 * be called.
1795 */
1796 static int
1797ins_compl_need_restart(void)
1798{
1799 // Return TRUE if we didn't complete finding matches or when the
1800 // 'completefunc' returned "always" in the "refresh" dictionary item.
1801 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001802 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001803 && compl_opt_refresh_always);
1804}
1805
1806/*
1807 * Called after changing "compl_leader".
1808 * Show the popup menu with a different set of matches.
1809 * May also search for matches again if the previous search was interrupted.
1810 */
1811 static void
1812ins_compl_new_leader(void)
1813{
1814 ins_compl_del_pum();
1815 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001816 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001817 compl_used_match = FALSE;
1818
1819 if (compl_started)
1820 ins_compl_set_original_text(compl_leader);
1821 else
1822 {
1823#ifdef FEAT_SPELL
1824 spell_bad_len = 0; // need to redetect bad word
1825#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001826 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001827 // the popup menu display the changed text before the cursor. Set
1828 // "compl_restarting" to avoid that the first match is inserted.
1829 pum_call_update_screen();
1830#ifdef FEAT_GUI
1831 if (gui.in_use)
1832 {
1833 // Show the cursor after the match, not after the redrawn text.
1834 setcursor();
1835 out_flush_cursor(FALSE, FALSE);
1836 }
1837#endif
1838 compl_restarting = TRUE;
1839 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1840 compl_cont_status = 0;
1841 compl_restarting = FALSE;
1842 }
1843
1844 compl_enter_selects = !compl_used_match;
1845
1846 // Show the popup menu with a different set of matches.
1847 ins_compl_show_pum();
1848
1849 // Don't let Enter select the original text when there is no popup menu.
1850 if (compl_match_array == NULL)
1851 compl_enter_selects = FALSE;
1852}
1853
1854/*
1855 * Return the length of the completion, from the completion start column to
1856 * the cursor column. Making sure it never goes below zero.
1857 */
1858 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001859get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001860{
1861 int off = (int)curwin->w_cursor.col - (int)compl_col;
1862
1863 if (off < 0)
1864 return 0;
1865 return off;
1866}
1867
1868/*
1869 * Append one character to the match leader. May reduce the number of
1870 * matches.
1871 */
1872 void
1873ins_compl_addleader(int c)
1874{
1875 int cc;
1876
1877 if (stop_arrow() == FAIL)
1878 return;
1879 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1880 {
1881 char_u buf[MB_MAXBYTES + 1];
1882
1883 (*mb_char2bytes)(c, buf);
1884 buf[cc] = NUL;
1885 ins_char_bytes(buf, cc);
1886 if (compl_opt_refresh_always)
1887 AppendToRedobuff(buf);
1888 }
1889 else
1890 {
1891 ins_char(c);
1892 if (compl_opt_refresh_always)
1893 AppendCharToRedobuff(c);
1894 }
1895
1896 // If we didn't complete finding matches we must search again.
1897 if (ins_compl_need_restart())
1898 ins_compl_restart();
1899
1900 // When 'always' is set, don't reset compl_leader. While completing,
1901 // cursor doesn't point original position, changing compl_leader would
1902 // break redo.
1903 if (!compl_opt_refresh_always)
1904 {
1905 vim_free(compl_leader);
1906 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001907 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001908 if (compl_leader != NULL)
1909 ins_compl_new_leader();
1910 }
1911}
1912
1913/*
1914 * Setup for finding completions again without leaving CTRL-X mode. Used when
1915 * BS or a key was typed while still searching for matches.
1916 */
1917 static void
1918ins_compl_restart(void)
1919{
1920 ins_compl_free();
1921 compl_started = FALSE;
1922 compl_matches = 0;
1923 compl_cont_status = 0;
1924 compl_cont_mode = 0;
1925}
1926
1927/*
1928 * Set the first match, the original text.
1929 */
1930 static void
1931ins_compl_set_original_text(char_u *str)
1932{
1933 char_u *p;
1934
1935 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001936 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1937 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001938 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001939 {
1940 p = vim_strsave(str);
1941 if (p != NULL)
1942 {
1943 vim_free(compl_first_match->cp_str);
1944 compl_first_match->cp_str = p;
1945 }
1946 }
1947 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001948 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001949 {
1950 p = vim_strsave(str);
1951 if (p != NULL)
1952 {
1953 vim_free(compl_first_match->cp_prev->cp_str);
1954 compl_first_match->cp_prev->cp_str = p;
1955 }
1956 }
1957}
1958
1959/*
1960 * Append one character to the match leader. May reduce the number of
1961 * matches.
1962 */
1963 void
1964ins_compl_addfrommatch(void)
1965{
1966 char_u *p;
1967 int len = (int)curwin->w_cursor.col - (int)compl_col;
1968 int c;
1969 compl_T *cp;
1970
1971 p = compl_shown_match->cp_str;
1972 if ((int)STRLEN(p) <= len) // the match is too short
1973 {
1974 // When still at the original match use the first entry that matches
1975 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001976 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001977 return;
1978
1979 p = NULL;
1980 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001981 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001982 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001983 if (compl_leader == NULL
1984 || ins_compl_equal(cp, compl_leader,
1985 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001986 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001987 p = cp->cp_str;
1988 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001989 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001990 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001991 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992 return;
1993 }
1994 p += len;
1995 c = PTR2CHAR(p);
1996 ins_compl_addleader(c);
1997}
1998
1999/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002000 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002001 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2002 * compl_cont_mode and compl_cont_status.
2003 * Returns TRUE when the character is not to be inserted.
2004 */
2005 static int
2006set_ctrl_x_mode(int c)
2007{
2008 int retval = FALSE;
2009
2010 switch (c)
2011 {
2012 case Ctrl_E:
2013 case Ctrl_Y:
2014 // scroll the window one line up or down
2015 ctrl_x_mode = CTRL_X_SCROLL;
2016 if (!(State & REPLACE_FLAG))
2017 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2018 else
2019 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2020 edit_submode_pre = NULL;
2021 showmode();
2022 break;
2023 case Ctrl_L:
2024 // complete whole line
2025 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2026 break;
2027 case Ctrl_F:
2028 // complete filenames
2029 ctrl_x_mode = CTRL_X_FILES;
2030 break;
2031 case Ctrl_K:
2032 // complete words from a dictinoary
2033 ctrl_x_mode = CTRL_X_DICTIONARY;
2034 break;
2035 case Ctrl_R:
2036 // Register insertion without exiting CTRL-X mode
2037 // Simply allow ^R to happen without affecting ^X mode
2038 break;
2039 case Ctrl_T:
2040 // complete words from a thesaurus
2041 ctrl_x_mode = CTRL_X_THESAURUS;
2042 break;
2043#ifdef FEAT_COMPL_FUNC
2044 case Ctrl_U:
2045 // user defined completion
2046 ctrl_x_mode = CTRL_X_FUNCTION;
2047 break;
2048 case Ctrl_O:
2049 // omni completion
2050 ctrl_x_mode = CTRL_X_OMNI;
2051 break;
2052#endif
2053 case 's':
2054 case Ctrl_S:
2055 // complete spelling suggestions
2056 ctrl_x_mode = CTRL_X_SPELL;
2057#ifdef FEAT_SPELL
2058 ++emsg_off; // Avoid getting the E756 error twice.
2059 spell_back_to_badword();
2060 --emsg_off;
2061#endif
2062 break;
2063 case Ctrl_RSB:
2064 // complete tag names
2065 ctrl_x_mode = CTRL_X_TAGS;
2066 break;
2067#ifdef FEAT_FIND_ID
2068 case Ctrl_I:
2069 case K_S_TAB:
2070 // complete keywords from included files
2071 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2072 break;
2073 case Ctrl_D:
2074 // complete definitions from included files
2075 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2076 break;
2077#endif
2078 case Ctrl_V:
2079 case Ctrl_Q:
2080 // complete vim commands
2081 ctrl_x_mode = CTRL_X_CMDLINE;
2082 break;
2083 case Ctrl_Z:
2084 // stop completion
2085 ctrl_x_mode = CTRL_X_NORMAL;
2086 edit_submode = NULL;
2087 showmode();
2088 retval = TRUE;
2089 break;
2090 case Ctrl_P:
2091 case Ctrl_N:
2092 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2093 // just started ^X mode, or there were enough ^X's to cancel
2094 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2095 // do normal expansion when interrupting a different mode (say
2096 // ^X^F^X^P or ^P^X^X^P, see below)
2097 // nothing changes if interrupting mode 0, (eg, the flag
2098 // doesn't change when going to ADDING mode -- Acevedo
2099 if (!(compl_cont_status & CONT_INTRPT))
2100 compl_cont_status |= CONT_LOCAL;
2101 else if (compl_cont_mode != 0)
2102 compl_cont_status &= ~CONT_LOCAL;
2103 // FALLTHROUGH
2104 default:
2105 // If we have typed at least 2 ^X's... for modes != 0, we set
2106 // compl_cont_status = 0 (eg, as if we had just started ^X
2107 // mode).
2108 // For mode 0, we set "compl_cont_mode" to an impossible
2109 // value, in both cases ^X^X can be used to restart the same
2110 // mode (avoiding ADDING mode).
2111 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2112 // 'complete' and local ^P expansions respectively.
2113 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2114 // mode -- Acevedo
2115 if (c == Ctrl_X)
2116 {
2117 if (compl_cont_mode != 0)
2118 compl_cont_status = 0;
2119 else
2120 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2121 }
2122 ctrl_x_mode = CTRL_X_NORMAL;
2123 edit_submode = NULL;
2124 showmode();
2125 break;
2126 }
2127
2128 return retval;
2129}
2130
2131/*
2132 * Stop insert completion mode
2133 */
2134 static int
2135ins_compl_stop(int c, int prev_mode, int retval)
2136{
2137 char_u *ptr;
2138#ifdef FEAT_CINDENT
2139 int want_cindent;
2140#endif
2141
2142 // Get here when we have finished typing a sequence of ^N and
2143 // ^P or other completion characters in CTRL-X mode. Free up
2144 // memory that was used, and make sure we can redo the insert.
2145 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2146 {
2147 // If any of the original typed text has been changed, eg when
2148 // ignorecase is set, we must add back-spaces to the redo
2149 // buffer. We add as few as necessary to delete just the part
2150 // of the original text that has changed.
2151 // When using the longest match, edited the match or used
2152 // CTRL-E then don't use the current match.
2153 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2154 ptr = compl_curr_match->cp_str;
2155 else
2156 ptr = NULL;
2157 ins_compl_fixRedoBufForLeader(ptr);
2158 }
2159
2160#ifdef FEAT_CINDENT
2161 want_cindent = (get_can_cindent() && cindent_on());
2162#endif
2163 // When completing whole lines: fix indent for 'cindent'.
2164 // Otherwise, break line if it's too long.
2165 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2166 {
2167#ifdef FEAT_CINDENT
2168 // re-indent the current line
2169 if (want_cindent)
2170 {
2171 do_c_expr_indent();
2172 want_cindent = FALSE; // don't do it again
2173 }
2174#endif
2175 }
2176 else
2177 {
2178 int prev_col = curwin->w_cursor.col;
2179
2180 // put the cursor on the last char, for 'tw' formatting
2181 if (prev_col > 0)
2182 dec_cursor();
2183 // only format when something was inserted
2184 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2185 insertchar(NUL, 0, -1);
2186 if (prev_col > 0
2187 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2188 inc_cursor();
2189 }
2190
2191 // If the popup menu is displayed pressing CTRL-Y means accepting
2192 // the selection without inserting anything. When
2193 // compl_enter_selects is set the Enter key does the same.
2194 if ((c == Ctrl_Y || (compl_enter_selects
2195 && (c == CAR || c == K_KENTER || c == NL)))
2196 && pum_visible())
2197 retval = TRUE;
2198
2199 // CTRL-E means completion is Ended, go back to the typed text.
2200 // but only do this, if the Popup is still visible
2201 if (c == Ctrl_E)
2202 {
2203 ins_compl_delete();
2204 if (compl_leader != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002205 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002206 else if (compl_first_match != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002207 ins_bytes(compl_orig_text + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002208 retval = TRUE;
2209 }
2210
2211 auto_format(FALSE, TRUE);
2212
2213 // Trigger the CompleteDonePre event to give scripts a chance to
2214 // act upon the completion before clearing the info, and restore
2215 // ctrl_x_mode, so that complete_info() can be used.
2216 ctrl_x_mode = prev_mode;
2217 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2218
2219 ins_compl_free();
2220 compl_started = FALSE;
2221 compl_matches = 0;
2222 if (!shortmess(SHM_COMPLETIONMENU))
2223 msg_clr_cmdline(); // necessary for "noshowmode"
2224 ctrl_x_mode = CTRL_X_NORMAL;
2225 compl_enter_selects = FALSE;
2226 if (edit_submode != NULL)
2227 {
2228 edit_submode = NULL;
2229 showmode();
2230 }
2231
2232#ifdef FEAT_CMDWIN
2233 if (c == Ctrl_C && cmdwin_type != 0)
2234 // Avoid the popup menu remains displayed when leaving the
2235 // command line window.
2236 update_screen(0);
2237#endif
2238#ifdef FEAT_CINDENT
2239 // Indent now if a key was typed that is in 'cinkeys'.
2240 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2241 do_c_expr_indent();
2242#endif
2243 // Trigger the CompleteDone event to give scripts a chance to act
2244 // upon the end of completion.
2245 ins_apply_autocmds(EVENT_COMPLETEDONE);
2246
2247 return retval;
2248}
2249
2250/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002251 * Prepare for Insert mode completion, or stop it.
2252 * Called just after typing a character in Insert mode.
2253 * Returns TRUE when the character is not to be inserted;
2254 */
2255 int
2256ins_compl_prep(int c)
2257{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002258 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002259 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002260
2261 // Forget any previous 'special' messages if this is actually
2262 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2263 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2264 edit_submode_extra = NULL;
2265
2266 // Ignore end of Select mode mapping and mouse scroll buttons.
2267 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01002268 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002269 return retval;
2270
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002271#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002272 // Ignore mouse events in a popup window
2273 if (is_mouse_key(c))
2274 {
2275 // Ignore drag and release events, the position does not need to be in
2276 // the popup and it may have just closed.
2277 if (c == K_LEFTRELEASE
2278 || c == K_LEFTRELEASE_NM
2279 || c == K_MIDDLERELEASE
2280 || c == K_RIGHTRELEASE
2281 || c == K_X1RELEASE
2282 || c == K_X2RELEASE
2283 || c == K_LEFTDRAG
2284 || c == K_MIDDLEDRAG
2285 || c == K_RIGHTDRAG
2286 || c == K_X1DRAG
2287 || c == K_X2DRAG)
2288 return retval;
2289 if (popup_visible)
2290 {
2291 int row = mouse_row;
2292 int col = mouse_col;
2293 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2294
2295 if (wp != NULL && WIN_IS_POPUP(wp))
2296 return retval;
2297 }
2298 }
2299#endif
2300
zeertzjqdca29d92021-08-31 19:12:51 +02002301 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2302 {
2303 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2304 || !vim_is_ctrl_x_key(c))
2305 {
2306 // Not starting another completion mode.
2307 ctrl_x_mode = CTRL_X_CMDLINE;
2308
2309 // CTRL-X CTRL-Z should stop completion without inserting anything
2310 if (c == Ctrl_Z)
2311 retval = TRUE;
2312 }
2313 else
2314 {
2315 ctrl_x_mode = CTRL_X_CMDLINE;
2316
2317 // Other CTRL-X keys first stop completion, then start another
2318 // completion mode.
2319 ins_compl_prep(' ');
2320 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2321 }
2322 }
2323
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002324 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002325 if (ctrl_x_mode_not_defined_yet()
2326 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002327 {
2328 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2329 compl_used_match = TRUE;
2330
2331 }
2332
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002333 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002334 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2335 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002336 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002337 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002338 {
2339 // We're already in CTRL-X mode, do we stay in it?
2340 if (!vim_is_ctrl_x_key(c))
2341 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002342 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002343 ctrl_x_mode = CTRL_X_NORMAL;
2344 else
2345 ctrl_x_mode = CTRL_X_FINISHED;
2346 edit_submode = NULL;
2347 }
2348 showmode();
2349 }
2350
2351 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2352 {
2353 // Show error message from attempted keyword completion (probably
2354 // 'Pattern not found') until another key is hit, then go back to
2355 // showing what mode we are in.
2356 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002357 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002358 && c != Ctrl_R && !ins_compl_pum_key(c))
2359 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002360 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002361 }
2362 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2363 // Trigger the CompleteDone event to give scripts a chance to act
2364 // upon the (possibly failed) completion.
2365 ins_apply_autocmds(EVENT_COMPLETEDONE);
2366
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002367 trigger_modechanged();
2368
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002369 // reset continue_* if we left expansion-mode, if we stay they'll be
2370 // (re)set properly in ins_complete()
2371 if (!vim_is_ctrl_x_key(c))
2372 {
2373 compl_cont_status = 0;
2374 compl_cont_mode = 0;
2375 }
2376
2377 return retval;
2378}
2379
2380/*
2381 * Fix the redo buffer for the completion leader replacing some of the typed
2382 * text. This inserts backspaces and appends the changed text.
2383 * "ptr" is the known leader text or NUL.
2384 */
2385 static void
2386ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2387{
2388 int len;
2389 char_u *p;
2390 char_u *ptr = ptr_arg;
2391
2392 if (ptr == NULL)
2393 {
2394 if (compl_leader != NULL)
2395 ptr = compl_leader;
2396 else
2397 return; // nothing to do
2398 }
2399 if (compl_orig_text != NULL)
2400 {
2401 p = compl_orig_text;
2402 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2403 ;
2404 if (len > 0)
2405 len -= (*mb_head_off)(p, p + len);
2406 for (p += len; *p != NUL; MB_PTR_ADV(p))
2407 AppendCharToRedobuff(K_BS);
2408 }
2409 else
2410 len = 0;
2411 if (ptr != NULL)
2412 AppendToRedobuffLit(ptr + len, -1);
2413}
2414
2415/*
2416 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2417 * (depending on flag) starting from buf and looking for a non-scanned
2418 * buffer (other than curbuf). curbuf is special, if it is called with
2419 * buf=curbuf then it has to be the first call for a given flag/expansion.
2420 *
2421 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2422 */
2423 static buf_T *
2424ins_compl_next_buf(buf_T *buf, int flag)
2425{
2426 static win_T *wp = NULL;
2427
2428 if (flag == 'w') // just windows
2429 {
2430 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2431 wp = curwin;
2432 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2433 && wp->w_buffer->b_scanned)
2434 ;
2435 buf = wp->w_buffer;
2436 }
2437 else
2438 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2439 // (unlisted buffers)
2440 // When completing whole lines skip unloaded buffers.
2441 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2442 && ((flag == 'U'
2443 ? buf->b_p_bl
2444 : (!buf->b_p_bl
2445 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2446 || buf->b_scanned))
2447 ;
2448 return buf;
2449}
2450
2451#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002452
2453# ifdef FEAT_EVAL
2454static callback_T cfu_cb; // 'completefunc' callback function
2455static callback_T ofu_cb; // 'omnifunc' callback function
2456static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2457# endif
2458
2459/*
2460 * Copy a global callback function to a buffer local callback.
2461 */
2462 static void
2463copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2464{
2465 free_callback(bufcb);
2466 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2467 copy_callback(bufcb, globcb);
2468}
2469
2470/*
2471 * Parse the 'completefunc' option value and set the callback function.
2472 * Invoked when the 'completefunc' option is set. The option value can be a
2473 * name of a function (string), or function(<name>) or funcref(<name>) or a
2474 * lambda expression.
2475 */
2476 int
2477set_completefunc_option(void)
2478{
2479 int retval;
2480
2481 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2482 if (retval == OK)
2483 set_buflocal_cfu_callback(curbuf);
2484
2485 return retval;
2486}
2487
2488/*
2489 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002490 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002491 */
2492 void
2493set_buflocal_cfu_callback(buf_T *buf UNUSED)
2494{
2495# ifdef FEAT_EVAL
2496 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2497# endif
2498}
2499
2500/*
2501 * Parse the 'omnifunc' option value and set the callback function.
2502 * Invoked when the 'omnifunc' option is set. The option value can be a
2503 * name of a function (string), or function(<name>) or funcref(<name>) or a
2504 * lambda expression.
2505 */
2506 int
2507set_omnifunc_option(void)
2508{
2509 int retval;
2510
2511 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2512 if (retval == OK)
2513 set_buflocal_ofu_callback(curbuf);
2514
2515 return retval;
2516}
2517
2518/*
2519 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002520 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002521 */
2522 void
2523set_buflocal_ofu_callback(buf_T *buf UNUSED)
2524{
2525# ifdef FEAT_EVAL
2526 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2527# endif
2528}
2529
2530/*
2531 * Parse the 'thesaurusfunc' option value and set the callback function.
2532 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2533 * name of a function (string), or function(<name>) or funcref(<name>) or a
2534 * lambda expression.
2535 */
2536 int
2537set_thesaurusfunc_option(void)
2538{
2539 int retval;
2540
2541 if (*curbuf->b_p_tsrfu != NUL)
2542 {
2543 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002544 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2545 &curbuf->b_tsrfu_cb);
2546 }
2547 else
2548 {
2549 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002550 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2551 }
2552
2553 return retval;
2554}
2555
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002556/*
2557 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002558 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002559 */
2560 int
2561set_ref_in_insexpand_funcs(int copyID)
2562{
2563 int abort = FALSE;
2564
2565 abort = set_ref_in_callback(&cfu_cb, copyID);
2566 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2567 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2568
2569 return abort;
2570}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002571
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002572/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002573 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002574 */
2575 static char_u *
2576get_complete_funcname(int type)
2577{
2578 switch (type)
2579 {
2580 case CTRL_X_FUNCTION:
2581 return curbuf->b_p_cfu;
2582 case CTRL_X_OMNI:
2583 return curbuf->b_p_ofu;
2584 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002585 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002586 default:
2587 return (char_u *)"";
2588 }
2589}
2590
2591/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002592 * Get the callback to use for insert mode completion.
2593 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002594 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002595get_insert_callback(int type)
2596{
2597 if (type == CTRL_X_FUNCTION)
2598 return &curbuf->b_cfu_cb;
2599 if (type == CTRL_X_OMNI)
2600 return &curbuf->b_ofu_cb;
2601 // CTRL_X_THESAURUS
2602 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2603}
2604
2605/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002606 * Execute user defined complete function 'completefunc', 'omnifunc' or
2607 * 'thesaurusfunc', and get matches in "matches".
2608 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002609 */
2610 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002611expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002612{
2613 list_T *matchlist = NULL;
2614 dict_T *matchdict = NULL;
2615 typval_T args[3];
2616 char_u *funcname;
2617 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002618 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002619 typval_T rettv;
2620 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002621 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002622
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002623 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002624 if (*funcname == NUL)
2625 return;
2626
2627 // Call 'completefunc' to obtain the list of matches.
2628 args[0].v_type = VAR_NUMBER;
2629 args[0].vval.v_number = 0;
2630 args[1].v_type = VAR_STRING;
2631 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2632 args[2].v_type = VAR_UNKNOWN;
2633
2634 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002635 // Lock the text to avoid weird things from happening. Also disallow
2636 // switching to another window, it should not be needed and may end up in
2637 // Insert mode in another buffer.
2638 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002639
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002640 cb = get_insert_callback(type);
2641 retval = call_callback(cb, 0, &rettv, 2, args);
2642
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002643 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002644 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002645 {
2646 switch (rettv.v_type)
2647 {
2648 case VAR_LIST:
2649 matchlist = rettv.vval.v_list;
2650 break;
2651 case VAR_DICT:
2652 matchdict = rettv.vval.v_dict;
2653 break;
2654 case VAR_SPECIAL:
2655 if (rettv.vval.v_number == VVAL_NONE)
2656 compl_opt_suppress_empty = TRUE;
2657 // FALLTHROUGH
2658 default:
2659 // TODO: Give error message?
2660 clear_tv(&rettv);
2661 break;
2662 }
2663 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002664 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002665
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002666 curwin->w_cursor = pos; // restore the cursor position
2667 validate_cursor();
2668 if (!EQUAL_POS(curwin->w_cursor, pos))
2669 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002670 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002671 goto theend;
2672 }
2673
2674 if (matchlist != NULL)
2675 ins_compl_add_list(matchlist);
2676 else if (matchdict != NULL)
2677 ins_compl_add_dict(matchdict);
2678
2679theend:
2680 // Restore State, it might have been changed.
2681 State = save_State;
2682
2683 if (matchdict != NULL)
2684 dict_unref(matchdict);
2685 if (matchlist != NULL)
2686 list_unref(matchlist);
2687}
2688#endif // FEAT_COMPL_FUNC
2689
2690#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2691/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002692 * Add a match to the list of matches from a typeval_T.
2693 * If the given string is already in the list of completions, then return
2694 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2695 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002696 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002697 */
2698 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002699ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002700{
2701 char_u *word;
2702 int dup = FALSE;
2703 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002704 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002705 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002706 typval_T user_data;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002707
Bram Moolenaar08928322020-01-04 14:32:48 +01002708 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002709 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2710 {
2711 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2712 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2713 (char_u *)"abbr", FALSE);
2714 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2715 (char_u *)"menu", FALSE);
2716 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2717 (char_u *)"kind", FALSE);
2718 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2719 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002720 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002721 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2722 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2723 flags |= CP_ICASE;
2724 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2725 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2726 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2727 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2728 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2729 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2730 flags |= CP_EQUAL;
2731 }
2732 else
2733 {
2734 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002735 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002736 }
2737 if (word == NULL || (!empty && *word == NUL))
2738 return FAIL;
Bram Moolenaar08928322020-01-04 14:32:48 +01002739 return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002740}
2741
2742/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002743 * Add completions from a list.
2744 */
2745 static void
2746ins_compl_add_list(list_T *list)
2747{
2748 listitem_T *li;
2749 int dir = compl_direction;
2750
2751 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002752 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002753 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002754 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002755 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002756 // if dir was BACKWARD then honor it just once
2757 dir = FORWARD;
2758 else if (did_emsg)
2759 break;
2760 }
2761}
2762
2763/*
2764 * Add completions from a dict.
2765 */
2766 static void
2767ins_compl_add_dict(dict_T *dict)
2768{
2769 dictitem_T *di_refresh;
2770 dictitem_T *di_words;
2771
2772 // Check for optional "refresh" item.
2773 compl_opt_refresh_always = FALSE;
2774 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2775 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2776 {
2777 char_u *v = di_refresh->di_tv.vval.v_string;
2778
2779 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2780 compl_opt_refresh_always = TRUE;
2781 }
2782
2783 // Add completions from a "words" list.
2784 di_words = dict_find(dict, (char_u *)"words", 5);
2785 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2786 ins_compl_add_list(di_words->di_tv.vval.v_list);
2787}
2788
2789/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002790 * Start completion for the complete() function.
2791 * "startcol" is where the matched text starts (1 is first column).
2792 * "list" is the list of matches.
2793 */
2794 static void
2795set_completion(colnr_T startcol, list_T *list)
2796{
2797 int save_w_wrow = curwin->w_wrow;
2798 int save_w_leftcol = curwin->w_leftcol;
2799 int flags = CP_ORIGINAL_TEXT;
2800
2801 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002802 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002803 ins_compl_prep(' ');
2804 ins_compl_clear();
2805 ins_compl_free();
2806
2807 compl_direction = FORWARD;
2808 if (startcol > curwin->w_cursor.col)
2809 startcol = curwin->w_cursor.col;
2810 compl_col = startcol;
2811 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2812 // compl_pattern doesn't need to be set
2813 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2814 if (p_ic)
2815 flags |= CP_ICASE;
2816 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002817 -1, NULL, NULL, NULL, 0,
2818 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002819 return;
2820
2821 ctrl_x_mode = CTRL_X_EVAL;
2822
2823 ins_compl_add_list(list);
2824 compl_matches = ins_compl_make_cyclic();
2825 compl_started = TRUE;
2826 compl_used_match = TRUE;
2827 compl_cont_status = 0;
2828
2829 compl_curr_match = compl_first_match;
2830 if (compl_no_insert || compl_no_select)
2831 {
2832 ins_complete(K_DOWN, FALSE);
2833 if (compl_no_select)
2834 // Down/Up has no real effect.
2835 ins_complete(K_UP, FALSE);
2836 }
2837 else
2838 ins_complete(Ctrl_N, FALSE);
2839 compl_enter_selects = compl_no_insert;
2840
2841 // Lazily show the popup menu, unless we got interrupted.
2842 if (!compl_interrupted)
2843 show_pum(save_w_wrow, save_w_leftcol);
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002844 trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002845 out_flush();
2846}
2847
2848/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002849 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002850 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002851 void
2852f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002853{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002854 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002855 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002856
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002857 if (in_vim9script()
2858 && (check_for_number_arg(argvars, 0) == FAIL
2859 || check_for_list_arg(argvars, 1) == FAIL))
2860 return;
2861
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002862 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002863 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002864 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002865 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002866 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002867
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002868 // "textlock" is set when evaluating 'completefunc' but we can change
2869 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002870 textlock = 0;
2871
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002872 // Check for undo allowed here, because if something was already inserted
2873 // the line was already saved for undo and this check isn't done.
2874 if (!undo_allowed())
2875 return;
2876
2877 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002878 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002879 else
2880 {
2881 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2882 if (startcol > 0)
2883 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002884 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002885 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002886}
2887
2888/*
2889 * "complete_add()" function
2890 */
2891 void
2892f_complete_add(typval_T *argvars, typval_T *rettv)
2893{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002894 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002895 return;
2896
Bram Moolenaar440cf092021-04-03 20:13:30 +02002897 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002898}
2899
2900/*
2901 * "complete_check()" function
2902 */
2903 void
2904f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2905{
2906 int saved = RedrawingDisabled;
2907
2908 RedrawingDisabled = 0;
2909 ins_compl_check_keys(0, TRUE);
2910 rettv->vval.v_number = ins_compl_interrupted();
2911 RedrawingDisabled = saved;
2912}
2913
2914/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002915 * Return Insert completion mode name string
2916 */
2917 static char_u *
2918ins_compl_mode(void)
2919{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002920 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002921 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2922
2923 return (char_u *)"";
2924}
2925
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002926/*
2927 * Assign the sequence number to all the completion matches which don't have
2928 * one assigned yet.
2929 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002930 static void
2931ins_compl_update_sequence_numbers()
2932{
2933 int number = 0;
2934 compl_T *match;
2935
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002936 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002937 {
2938 // search backwards for the first valid (!= -1) number.
2939 // This should normally succeed already at the first loop
2940 // cycle, so it's fast!
2941 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002942 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002943 if (match->cp_number != -1)
2944 {
2945 number = match->cp_number;
2946 break;
2947 }
2948 if (match != NULL)
2949 // go up and assign all numbers which are not assigned
2950 // yet
2951 for (match = match->cp_next;
2952 match != NULL && match->cp_number == -1;
2953 match = match->cp_next)
2954 match->cp_number = ++number;
2955 }
2956 else // BACKWARD
2957 {
2958 // search forwards (upwards) for the first valid (!= -1)
2959 // number. This should normally succeed already at the
2960 // first loop cycle, so it's fast!
2961 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002962 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002963 if (match->cp_number != -1)
2964 {
2965 number = match->cp_number;
2966 break;
2967 }
2968 if (match != NULL)
2969 // go down and assign all numbers which are not
2970 // assigned yet
2971 for (match = match->cp_prev; match
2972 && match->cp_number == -1;
2973 match = match->cp_prev)
2974 match->cp_number = ++number;
2975 }
2976}
2977
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002978/*
2979 * Get complete information
2980 */
2981 static void
2982get_complete_info(list_T *what_list, dict_T *retdict)
2983{
2984 int ret = OK;
2985 listitem_T *item;
2986#define CI_WHAT_MODE 0x01
2987#define CI_WHAT_PUM_VISIBLE 0x02
2988#define CI_WHAT_ITEMS 0x04
2989#define CI_WHAT_SELECTED 0x08
2990#define CI_WHAT_INSERTED 0x10
2991#define CI_WHAT_ALL 0xff
2992 int what_flag;
2993
2994 if (what_list == NULL)
2995 what_flag = CI_WHAT_ALL;
2996 else
2997 {
2998 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002999 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003000 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003001 {
3002 char_u *what = tv_get_string(&item->li_tv);
3003
3004 if (STRCMP(what, "mode") == 0)
3005 what_flag |= CI_WHAT_MODE;
3006 else if (STRCMP(what, "pum_visible") == 0)
3007 what_flag |= CI_WHAT_PUM_VISIBLE;
3008 else if (STRCMP(what, "items") == 0)
3009 what_flag |= CI_WHAT_ITEMS;
3010 else if (STRCMP(what, "selected") == 0)
3011 what_flag |= CI_WHAT_SELECTED;
3012 else if (STRCMP(what, "inserted") == 0)
3013 what_flag |= CI_WHAT_INSERTED;
3014 }
3015 }
3016
3017 if (ret == OK && (what_flag & CI_WHAT_MODE))
3018 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3019
3020 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3021 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3022
3023 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3024 {
3025 list_T *li;
3026 dict_T *di;
3027 compl_T *match;
3028
3029 li = list_alloc();
3030 if (li == NULL)
3031 return;
3032 ret = dict_add_list(retdict, "items", li);
3033 if (ret == OK && compl_first_match != NULL)
3034 {
3035 match = compl_first_match;
3036 do
3037 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003038 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003039 {
3040 di = dict_alloc();
3041 if (di == NULL)
3042 return;
3043 ret = list_append_dict(li, di);
3044 if (ret != OK)
3045 return;
3046 dict_add_string(di, "word", match->cp_str);
3047 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3048 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3049 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3050 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003051 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3052 // Add an empty string for backwards compatibility
3053 dict_add_string(di, "user_data", (char_u *)"");
3054 else
3055 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003056 }
3057 match = match->cp_next;
3058 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003059 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003060 }
3061 }
3062
3063 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003064 {
3065 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3066 ins_compl_update_sequence_numbers();
3067 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3068 ? compl_curr_match->cp_number - 1 : -1);
3069 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003070
3071 // TODO
3072 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3073}
3074
3075/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003076 * "complete_info()" function
3077 */
3078 void
3079f_complete_info(typval_T *argvars, typval_T *rettv)
3080{
3081 list_T *what_list = NULL;
3082
3083 if (rettv_dict_alloc(rettv) != OK)
3084 return;
3085
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003086 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3087 return;
3088
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003089 if (argvars[0].v_type != VAR_UNKNOWN)
3090 {
3091 if (argvars[0].v_type != VAR_LIST)
3092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003093 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003094 return;
3095 }
3096 what_list = argvars[0].vval.v_list;
3097 }
3098 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003099}
3100#endif
3101
3102/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003103 * Returns TRUE when using a user-defined function for thesaurus completion.
3104 */
3105 static int
3106thesaurus_func_complete(int type UNUSED)
3107{
3108#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003109 return type == CTRL_X_THESAURUS
3110 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003111#else
3112 return FALSE;
3113#endif
3114}
3115
3116/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003117 * Return value of process_next_cpt_value()
3118 */
3119enum
3120{
3121 INS_COMPL_CPT_OK = 1,
3122 INS_COMPL_CPT_CONT,
3123 INS_COMPL_CPT_END
3124};
3125
3126/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003127 * state information used for getting the next set of insert completion
3128 * matches.
3129 */
3130typedef struct
3131{
3132 char_u *e_cpt; // current entry in 'complete'
3133 buf_T *ins_buf; // buffer being scanned
3134 pos_T *cur_match_pos; // current match position
3135 pos_T prev_match_pos; // previous match position
3136 int set_match_pos; // save first_match_pos/last_match_pos
3137 pos_T first_match_pos; // first match position
3138 pos_T last_match_pos; // last match position
3139 int found_all; // found all matches of a certain type.
3140 char_u *dict; // dictionary file to search
3141 int dict_f; // "dict" is an exact file name or not
3142} ins_compl_next_state_T;
3143
3144/*
3145 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003146 *
3147 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003148 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003149 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003150 * st->found_all - all matches of this type are found
3151 * st->ins_buf - search for completions in this buffer
3152 * st->first_match_pos - position of the first completion match
3153 * st->last_match_pos - position of the last completion match
3154 * st->set_match_pos - TRUE if the first match position should be saved to
3155 * avoid loops after the search wraps around.
3156 * st->dict - name of the dictionary or thesaurus file to search
3157 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003158 *
3159 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
3160 * Returns INS_COMPL_CPT_CONT to skip the current value and process the next
3161 * option value.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003162 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003163 */
3164 static int
3165process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003166 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003167 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003168 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003169{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003170 int compl_type = -1;
3171 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003172
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003173 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003174
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003175 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3176 st->e_cpt++;
3177
3178 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003179 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003180 st->ins_buf = curbuf;
3181 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003182 // Move the cursor back one character so that ^N can match the
3183 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003184 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003185 {
3186 // Move the cursor to after the last character in the
3187 // buffer, so that word at start of buffer is found
3188 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003189 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3190 st->first_match_pos.col =
3191 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003192 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003193 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003194 compl_type = 0;
3195
3196 // Remember the first match so that the loop stops when we
3197 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003198 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003199 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003200 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3201 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003202 {
3203 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003204 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003205 {
3206 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003207 st->first_match_pos.col = st->last_match_pos.col = 0;
3208 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3209 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003210 compl_type = 0;
3211 }
3212 else // unloaded buffer, scan like dictionary
3213 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003214 st->found_all = TRUE;
3215 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003216 {
3217 status = INS_COMPL_CPT_CONT;
3218 goto done;
3219 }
3220 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003221 st->dict = st->ins_buf->b_fname;
3222 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003223 }
3224 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3225 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003226 st->ins_buf->b_fname == NULL
3227 ? buf_spname(st->ins_buf)
3228 : st->ins_buf->b_sfname == NULL
3229 ? st->ins_buf->b_fname
3230 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003231 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3232 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003233 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003234 status = INS_COMPL_CPT_END;
3235 else
3236 {
3237 if (ctrl_x_mode_line_or_eval())
3238 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003239 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003240 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003241 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003242 compl_type = CTRL_X_DICTIONARY;
3243 else
3244 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003245 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003246 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 st->dict = st->e_cpt;
3248 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 }
3250 }
3251#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003252 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003253 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255 compl_type = CTRL_X_PATH_DEFINES;
3256#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003257 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003258 {
3259 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3260 compl_type = CTRL_X_TAGS;
3261 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3262 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3263 }
3264 else
3265 compl_type = -1;
3266
3267 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003268 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003269
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003270 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003271 if (compl_type == -1)
3272 status = INS_COMPL_CPT_CONT;
3273 }
3274
3275done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003277 return status;
3278}
3279
3280#ifdef FEAT_FIND_ID
3281/*
3282 * Get the next set of identifiers or defines matching "compl_pattern" in
3283 * included files.
3284 */
3285 static void
3286get_next_include_file_completion(int compl_type)
3287{
3288 find_pattern_in_path(compl_pattern, compl_direction,
3289 (int)STRLEN(compl_pattern), FALSE, FALSE,
3290 (compl_type == CTRL_X_PATH_DEFINES
3291 && !(compl_cont_status & CONT_SOL))
3292 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3293 (linenr_T)1, (linenr_T)MAXLNUM);
3294}
3295#endif
3296
3297/*
3298 * Get the next set of words matching "compl_pattern" in dictionary or
3299 * thesaurus files.
3300 */
3301 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003302get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003303{
3304#ifdef FEAT_COMPL_FUNC
3305 if (thesaurus_func_complete(compl_type))
3306 expand_by_function(compl_type, compl_pattern);
3307 else
3308#endif
3309 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003310 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003311 : (compl_type == CTRL_X_THESAURUS
3312 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3313 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3314 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003317}
3318
3319/*
3320 * Get the next set of tag names matching "compl_pattern".
3321 */
3322 static void
3323get_next_tag_completion(void)
3324{
3325 int save_p_ic;
3326 char_u **matches;
3327 int num_matches;
3328
3329 // set p_ic according to p_ic, p_scs and pat for find_tags().
3330 save_p_ic = p_ic;
3331 p_ic = ignorecase(compl_pattern);
3332
3333 // Find up to TAG_MANY matches. Avoids that an enormous number
3334 // of matches is found when compl_pattern is empty
3335 g_tag_at_cursor = TRUE;
3336 if (find_tags(compl_pattern, &num_matches, &matches,
3337 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003338 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003339 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3340 ins_compl_add_matches(num_matches, matches, p_ic);
3341 g_tag_at_cursor = FALSE;
3342 p_ic = save_p_ic;
3343}
3344
3345/*
3346 * Get the next set of filename matching "compl_pattern".
3347 */
3348 static void
3349get_next_filename_completion(void)
3350{
3351 char_u **matches;
3352 int num_matches;
3353
3354 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3355 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3356 return;
3357
3358 // May change home directory back to "~".
3359 tilde_replace(compl_pattern, num_matches, matches);
3360#ifdef BACKSLASH_IN_FILENAME
3361 if (curbuf->b_p_csl[0] != NUL)
3362 {
3363 int i;
3364
3365 for (i = 0; i < num_matches; ++i)
3366 {
3367 char_u *ptr = matches[i];
3368
3369 while (*ptr != NUL)
3370 {
3371 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3372 *ptr = '/';
3373 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3374 *ptr = '\\';
3375 ptr += (*mb_ptr2len)(ptr);
3376 }
3377 }
3378 }
3379#endif
3380 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3381}
3382
3383/*
3384 * Get the next set of command-line completions matching "compl_pattern".
3385 */
3386 static void
3387get_next_cmdline_completion()
3388{
3389 char_u **matches;
3390 int num_matches;
3391
3392 if (expand_cmdline(&compl_xp, compl_pattern,
3393 (int)STRLEN(compl_pattern),
3394 &num_matches, &matches) == EXPAND_OK)
3395 ins_compl_add_matches(num_matches, matches, FALSE);
3396}
3397
3398/*
3399 * Get the next set of spell suggestions matching "compl_pattern".
3400 */
3401 static void
3402get_next_spell_completion(linenr_T lnum UNUSED)
3403{
3404#ifdef FEAT_SPELL
3405 char_u **matches;
3406 int num_matches;
3407
3408 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3409 if (num_matches > 0)
3410 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003411 else
3412 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003413#endif
3414}
3415
3416/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003417 * Return the next word or line from buffer "ins_buf" at position
3418 * "cur_match_pos" for completion. The length of the match is set in "len".
3419 */
3420 static char_u *
3421ins_comp_get_next_word_or_line(
3422 buf_T *ins_buf, // buffer being scanned
3423 pos_T *cur_match_pos, // current match position
3424 int *match_len,
3425 int *cont_s_ipos) // next ^X<> will set initial_pos
3426{
3427 char_u *ptr;
3428 int len;
3429
3430 *match_len = 0;
3431 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3432 cur_match_pos->col;
3433 if (ctrl_x_mode_line_or_eval())
3434 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003435 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003436 {
3437 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3438 return NULL;
3439 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3440 if (!p_paste)
3441 ptr = skipwhite(ptr);
3442 }
3443 len = (int)STRLEN(ptr);
3444 }
3445 else
3446 {
3447 char_u *tmp_ptr = ptr;
3448
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003449 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003450 {
3451 tmp_ptr += compl_length;
3452 // Skip if already inside a word.
3453 if (vim_iswordp(tmp_ptr))
3454 return NULL;
3455 // Find start of next word.
3456 tmp_ptr = find_word_start(tmp_ptr);
3457 }
3458 // Find end of this word.
3459 tmp_ptr = find_word_end(tmp_ptr);
3460 len = (int)(tmp_ptr - ptr);
3461
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003462 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003463 {
3464 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3465 {
3466 // Try next line, if any. the new word will be
3467 // "join" as if the normal command "J" was used.
3468 // IOSIZE is always greater than
3469 // compl_length, so the next STRNCPY always
3470 // works -- Acevedo
3471 STRNCPY(IObuff, ptr, len);
3472 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3473 tmp_ptr = ptr = skipwhite(ptr);
3474 // Find start of next word.
3475 tmp_ptr = find_word_start(tmp_ptr);
3476 // Find end of next word.
3477 tmp_ptr = find_word_end(tmp_ptr);
3478 if (tmp_ptr > ptr)
3479 {
3480 if (*ptr != ')' && IObuff[len - 1] != TAB)
3481 {
3482 if (IObuff[len - 1] != ' ')
3483 IObuff[len++] = ' ';
3484 // IObuf =~ "\k.* ", thus len >= 2
3485 if (p_js
3486 && (IObuff[len - 2] == '.'
3487 || (vim_strchr(p_cpo, CPO_JOINSP)
3488 == NULL
3489 && (IObuff[len - 2] == '?'
3490 || IObuff[len - 2] == '!'))))
3491 IObuff[len++] = ' ';
3492 }
3493 // copy as much as possible of the new word
3494 if (tmp_ptr - ptr >= IOSIZE - len)
3495 tmp_ptr = ptr + IOSIZE - len - 1;
3496 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3497 len += (int)(tmp_ptr - ptr);
3498 *cont_s_ipos = TRUE;
3499 }
3500 IObuff[len] = NUL;
3501 ptr = IObuff;
3502 }
3503 if (len == compl_length)
3504 return NULL;
3505 }
3506 }
3507
3508 *match_len = len;
3509 return ptr;
3510}
3511
3512/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003513 * Get the next set of words matching "compl_pattern" for default completion(s)
3514 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003515 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3516 * position "st->start_pos" in the "compl_direction" direction. If
3517 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3518 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003519 * Returns OK if a new next match is found, otherwise returns FAIL.
3520 */
3521 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003522get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003523{
3524 int found_new_match = FAIL;
3525 int save_p_scs;
3526 int save_p_ws;
3527 int looped_around = FALSE;
3528 char_u *ptr;
3529 int len;
3530
3531 // If 'infercase' is set, don't use 'smartcase' here
3532 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003533 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003534 p_scs = FALSE;
3535
3536 // Buffers other than curbuf are scanned from the beginning or the
3537 // end but never from the middle, thus setting nowrapscan in this
3538 // buffer is a good idea, on the other hand, we always set
3539 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3540 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003541 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003542 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003543 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003544 p_ws = TRUE;
3545 looped_around = FALSE;
3546 for (;;)
3547 {
3548 int cont_s_ipos = FALSE;
3549
3550 ++msg_silent; // Don't want messages for wrapscan.
3551
3552 // ctrl_x_mode_line_or_eval() || word-wise search that
3553 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003554 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003555 found_new_match = search_for_exact_line(st->ins_buf,
3556 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003557 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003558 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3559 NULL, compl_direction, compl_pattern, 1L,
3560 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003561 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003562 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003563 {
3564 // set "compl_started" even on fail
3565 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003566 st->first_match_pos = *st->cur_match_pos;
3567 st->last_match_pos = *st->cur_match_pos;
3568 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003569 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003570 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3571 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 {
3573 found_new_match = FAIL;
3574 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003575 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003576 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3577 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3578 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003579 {
3580 if (looped_around)
3581 found_new_match = FAIL;
3582 else
3583 looped_around = TRUE;
3584 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003585 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003586 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3587 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3588 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003589 {
3590 if (looped_around)
3591 found_new_match = FAIL;
3592 else
3593 looped_around = TRUE;
3594 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003595 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003596 if (found_new_match == FAIL)
3597 break;
3598
3599 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003600 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003601 && start_pos->lnum == st->cur_match_pos->lnum
3602 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003603 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003604
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3606 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003607 if (ptr == NULL)
3608 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003609
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003611 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003612 0, cont_s_ipos) != NOTDONE)
3613 {
3614 found_new_match = OK;
3615 break;
3616 }
3617 }
3618 p_scs = save_p_scs;
3619 p_ws = save_p_ws;
3620
3621 return found_new_match;
3622}
3623
3624/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003625 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003626 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003627 */
3628 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003629get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003630{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003631 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003632
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003633 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003634 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003635 case -1:
3636 break;
3637#ifdef FEAT_FIND_ID
3638 case CTRL_X_PATH_PATTERNS:
3639 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003640 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003641 break;
3642#endif
3643
3644 case CTRL_X_DICTIONARY:
3645 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003646 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3647 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003648 break;
3649
3650 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003651 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003652 break;
3653
3654 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003655 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003656 break;
3657
3658 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003659 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003661 break;
3662
3663#ifdef FEAT_COMPL_FUNC
3664 case CTRL_X_FUNCTION:
3665 case CTRL_X_OMNI:
3666 expand_by_function(type, compl_pattern);
3667 break;
3668#endif
3669
3670 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003671 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003672 break;
3673
3674 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003675 found_new_match = get_next_default_completion(st, ini);
3676 if (found_new_match == FAIL && st->ins_buf == curbuf)
3677 st->found_all = TRUE;
3678 }
3679
3680 // check if compl_curr_match has changed, (e.g. other type of
3681 // expansion added something)
3682 if (type != 0 && compl_curr_match != compl_old_match)
3683 found_new_match = OK;
3684
3685 return found_new_match;
3686}
3687
3688/*
3689 * Get the next expansion(s), using "compl_pattern".
3690 * The search starts at position "ini" in curbuf and in the direction
3691 * compl_direction.
3692 * When "compl_started" is FALSE start at that position, otherwise continue
3693 * where we stopped searching before.
3694 * This may return before finding all the matches.
3695 * Return the total number of matches or -1 if still unknown -- Acevedo
3696 */
3697 static int
3698ins_compl_get_exp(pos_T *ini)
3699{
3700 static ins_compl_next_state_T st;
3701 int i;
3702 int found_new_match;
3703 int type = ctrl_x_mode;
3704
3705 if (!compl_started)
3706 {
3707 FOR_ALL_BUFFERS(st.ins_buf)
3708 st.ins_buf->b_scanned = 0;
3709 st.found_all = FALSE;
3710 st.ins_buf = curbuf;
3711 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3712 ? (char_u *)"." : curbuf->b_p_cpt;
3713 st.last_match_pos = st.first_match_pos = *ini;
3714 }
3715 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3716 st.ins_buf = curbuf; // In case the buffer was wiped out.
3717
3718 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003719 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003720 ? &st.last_match_pos : &st.first_match_pos;
3721
3722 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3723 for (;;)
3724 {
3725 found_new_match = FAIL;
3726 st.set_match_pos = FALSE;
3727
3728 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3729 // or if found_all says this entry is done. For ^X^L only use the
3730 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003731 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003732 && (!compl_started || st.found_all))
3733 {
3734 int status = process_next_cpt_value(&st, &type, ini);
3735
3736 if (status == INS_COMPL_CPT_END)
3737 break;
3738 if (status == INS_COMPL_CPT_CONT)
3739 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003740 }
3741
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003742 // If complete() was called then compl_pattern has been reset. The
3743 // following won't work then, bail out.
3744 if (compl_pattern == NULL)
3745 break;
3746
3747 // get the next set of completion matches
3748 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003749
3750 // break the loop for specialized modes (use 'complete' just for the
3751 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3752 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003753 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3754 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003755 {
3756 if (got_int)
3757 break;
3758 // Fill the popup menu as soon as possible.
3759 if (type != -1)
3760 ins_compl_check_keys(0, FALSE);
3761
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003762 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003763 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3764 break;
3765 compl_started = TRUE;
3766 }
3767 else
3768 {
3769 // Mark a buffer scanned when it has been scanned completely
3770 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003771 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003772
3773 compl_started = FALSE;
3774 }
3775 }
3776 compl_started = TRUE;
3777
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003778 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003779 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003780 found_new_match = FAIL;
3781
3782 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003783 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003784 && !ctrl_x_mode_line_or_eval()))
3785 i = ins_compl_make_cyclic();
3786
3787 if (compl_old_match != NULL)
3788 {
3789 // If several matches were added (FORWARD) or the search failed and has
3790 // just been made cyclic then we have to move compl_curr_match to the
3791 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003792 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003793 : compl_old_match->cp_prev;
3794 if (compl_curr_match == NULL)
3795 compl_curr_match = compl_old_match;
3796 }
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003797 trigger_modechanged();
3798
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003799 return i;
3800}
3801
3802/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003803 * Update "compl_shown_match" to the actually shown match, it may differ when
3804 * "compl_leader" is used to omit some of the matches.
3805 */
3806 static void
3807ins_compl_update_shown_match(void)
3808{
3809 while (!ins_compl_equal(compl_shown_match,
3810 compl_leader, (int)STRLEN(compl_leader))
3811 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003812 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003813 compl_shown_match = compl_shown_match->cp_next;
3814
3815 // If we didn't find it searching forward, and compl_shows_dir is
3816 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003817 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003818 && !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 {
3823 while (!ins_compl_equal(compl_shown_match,
3824 compl_leader, (int)STRLEN(compl_leader))
3825 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003826 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003827 compl_shown_match = compl_shown_match->cp_prev;
3828 }
3829}
3830
3831/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003832 * Delete the old text being completed.
3833 */
3834 void
3835ins_compl_delete(void)
3836{
3837 int col;
3838
3839 // In insert mode: Delete the typed part.
3840 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003841 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003842 if ((int)curwin->w_cursor.col > col)
3843 {
3844 if (stop_arrow() == FAIL)
3845 return;
3846 backspace_until_column(col);
3847 }
3848
3849 // TODO: is this sufficient for redrawing? Redrawing everything causes
3850 // flicker, thus we can't do that.
3851 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003852#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003853 // clear v:completed_item
3854 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003856}
3857
3858/*
3859 * Insert the new text being completed.
3860 * "in_compl_func" is TRUE when called from complete_check().
3861 */
3862 void
3863ins_compl_insert(int in_compl_func)
3864{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003865 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003866
3867 // Make sure we don't go over the end of the string, this can happen with
3868 // illegal bytes.
3869 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3870 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003871 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003872 compl_used_match = FALSE;
3873 else
3874 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003875#ifdef FEAT_EVAL
3876 {
3877 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3878
3879 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3880 }
3881#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003882 if (!in_compl_func)
3883 compl_curr_match = compl_shown_match;
3884}
3885
3886/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003887 * show the file name for the completion match (if any). Truncate the file
3888 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003889 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003890 static void
3891ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003892{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003893 char *lead = _("match in file");
3894 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3895 char_u *s;
3896 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003897
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003898 if (space <= 0)
3899 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003901 // We need the tail that fits. With double-byte encoding going
3902 // back from the end is very slow, thus go from the start and keep
3903 // the text that fits in "space" between "s" and "e".
3904 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003905 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003906 space -= ptr2cells(e);
3907 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003908 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003909 space += ptr2cells(s);
3910 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003911 }
3912 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003913 msg_hist_off = TRUE;
3914 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3915 s > compl_shown_match->cp_fname ? "<" : "", s);
3916 msg((char *)IObuff);
3917 msg_hist_off = FALSE;
3918 redraw_cmdline = FALSE; // don't overwrite!
3919}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003920
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003921/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003922 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003923 * times. The number of matches found is returned in 'num_matches'.
3924 *
3925 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3926 * get more completions. If it is FALSE, then do nothing when there are no more
3927 * completions in the given direction.
3928 *
3929 * If "advance" is TRUE, then completion will move to the first match.
3930 * Otherwise, the original text will be shown.
3931 *
3932 * Returns OK on success and -1 if the number of matches are unknown.
3933 */
3934 static int
3935find_next_completion_match(
3936 int allow_get_expansion,
3937 int todo, // repeat completion this many times
3938 int advance,
3939 int *num_matches)
3940{
3941 int found_end = FALSE;
3942 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003943
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003944 while (--todo >= 0)
3945 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003946 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 {
3948 compl_shown_match = compl_shown_match->cp_next;
3949 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003950 && (is_first_match(compl_shown_match->cp_next)
3951 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003952 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003953 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003954 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003955 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003956 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003957 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003958 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003959 }
3960 else
3961 {
3962 if (!allow_get_expansion)
3963 {
3964 if (advance)
3965 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003966 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003967 compl_pending -= todo + 1;
3968 else
3969 compl_pending += todo + 1;
3970 }
3971 return -1;
3972 }
3973
3974 if (!compl_no_select && advance)
3975 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003976 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003977 --compl_pending;
3978 else
3979 ++compl_pending;
3980 }
3981
3982 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003983 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003984
3985 // handle any pending completions
3986 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003987 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003988 {
3989 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3990 {
3991 compl_shown_match = compl_shown_match->cp_next;
3992 --compl_pending;
3993 }
3994 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3995 {
3996 compl_shown_match = compl_shown_match->cp_prev;
3997 ++compl_pending;
3998 }
3999 else
4000 break;
4001 }
4002 found_end = FALSE;
4003 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004004 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004005 && compl_leader != NULL
4006 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004007 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004008 ++todo;
4009 else
4010 // Remember a matching item.
4011 found_compl = compl_shown_match;
4012
4013 // Stop at the end of the list when we found a usable match.
4014 if (found_end)
4015 {
4016 if (found_compl != NULL)
4017 {
4018 compl_shown_match = found_compl;
4019 break;
4020 }
4021 todo = 1; // use first usable match after wrapping around
4022 }
4023 }
4024
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004025 return OK;
4026}
4027
4028/*
4029 * Fill in the next completion in the current direction.
4030 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4031 * get more completions. If it is FALSE, then we just do nothing when there
4032 * are no more completions in a given direction. The latter case is used when
4033 * we are still in the middle of finding completions, to allow browsing
4034 * through the ones found so far.
4035 * Return the total number of matches, or -1 if still unknown -- webb.
4036 *
4037 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4038 * compl_shown_match here.
4039 *
4040 * Note that this function may be called recursively once only. First with
4041 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4042 * calls this function with "allow_get_expansion" FALSE.
4043 */
4044 static int
4045ins_compl_next(
4046 int allow_get_expansion,
4047 int count, // repeat completion this many times; should
4048 // be at least 1
4049 int insert_match, // Insert the newly selected match
4050 int in_compl_func) // called from complete_check()
4051{
4052 int num_matches = -1;
4053 int todo = count;
4054 int advance;
4055 int started = compl_started;
4056
4057 // When user complete function return -1 for findstart which is next
4058 // time of 'always', compl_shown_match become NULL.
4059 if (compl_shown_match == NULL)
4060 return -1;
4061
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004062 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004063 // Update "compl_shown_match" to the actually shown match
4064 ins_compl_update_shown_match();
4065
4066 if (allow_get_expansion && insert_match
4067 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4068 // Delete old text to be replaced
4069 ins_compl_delete();
4070
4071 // When finding the longest common text we stick at the original text,
4072 // don't let CTRL-N or CTRL-P move to the first match.
4073 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4074
4075 // When restarting the search don't insert the first match either.
4076 if (compl_restarting)
4077 {
4078 advance = FALSE;
4079 compl_restarting = FALSE;
4080 }
4081
4082 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4083 // around.
4084 if (find_next_completion_match(allow_get_expansion, todo, advance,
4085 &num_matches) == -1)
4086 return -1;
4087
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004088 // Insert the text of the new completion, or the compl_leader.
4089 if (compl_no_insert && !started)
4090 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004091 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004092 compl_used_match = FALSE;
4093 }
4094 else if (insert_match)
4095 {
4096 if (!compl_get_longest || compl_used_match)
4097 ins_compl_insert(in_compl_func);
4098 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004099 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004100 }
4101 else
4102 compl_used_match = FALSE;
4103
4104 if (!allow_get_expansion)
4105 {
4106 // may undisplay the popup menu first
4107 ins_compl_upd_pum();
4108
4109 if (pum_enough_matches())
4110 // Will display the popup menu, don't redraw yet to avoid flicker.
4111 pum_call_update_screen();
4112 else
4113 // Not showing the popup menu yet, redraw to show the user what was
4114 // inserted.
4115 update_screen(0);
4116
4117 // display the updated popup menu
4118 ins_compl_show_pum();
4119#ifdef FEAT_GUI
4120 if (gui.in_use)
4121 {
4122 // Show the cursor after the match, not after the redrawn text.
4123 setcursor();
4124 out_flush_cursor(FALSE, FALSE);
4125 }
4126#endif
4127
4128 // Delete old text to be replaced, since we're still searching and
4129 // don't want to match ourselves!
4130 ins_compl_delete();
4131 }
4132
4133 // Enter will select a match when the match wasn't inserted and the popup
4134 // menu is visible.
4135 if (compl_no_insert && !started)
4136 compl_enter_selects = TRUE;
4137 else
4138 compl_enter_selects = !insert_match && compl_match_array != NULL;
4139
4140 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004141 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004142 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004143
4144 return num_matches;
4145}
4146
4147/*
4148 * Call this while finding completions, to check whether the user has hit a key
4149 * that should change the currently displayed completion, or exit completion
4150 * mode. Also, when compl_pending is not zero, show a completion as soon as
4151 * possible. -- webb
4152 * "frequency" specifies out of how many calls we actually check.
4153 * "in_compl_func" is TRUE when called from complete_check(), don't set
4154 * compl_curr_match.
4155 */
4156 void
4157ins_compl_check_keys(int frequency, int in_compl_func)
4158{
4159 static int count = 0;
4160 int c;
4161
4162 // Don't check when reading keys from a script, :normal or feedkeys().
4163 // That would break the test scripts. But do check for keys when called
4164 // from complete_check().
4165 if (!in_compl_func && (using_script() || ex_normal_busy))
4166 return;
4167
4168 // Only do this at regular intervals
4169 if (++count < frequency)
4170 return;
4171 count = 0;
4172
4173 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4174 // can't do its work correctly.
4175 c = vpeekc_any();
4176 if (c != NUL)
4177 {
4178 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4179 {
4180 c = safe_vgetc(); // Eat the character
4181 compl_shows_dir = ins_compl_key2dir(c);
4182 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4183 c != K_UP && c != K_DOWN, in_compl_func);
4184 }
4185 else
4186 {
4187 // Need to get the character to have KeyTyped set. We'll put it
4188 // back with vungetc() below. But skip K_IGNORE.
4189 c = safe_vgetc();
4190 if (c != K_IGNORE)
4191 {
4192 // Don't interrupt completion when the character wasn't typed,
4193 // e.g., when doing @q to replay keys.
4194 if (c != Ctrl_R && KeyTyped)
4195 compl_interrupted = TRUE;
4196
4197 vungetc(c);
4198 }
4199 }
4200 }
4201 if (compl_pending != 0 && !got_int && !compl_no_insert)
4202 {
4203 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4204
4205 compl_pending = 0;
4206 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4207 }
4208}
4209
4210/*
4211 * Decide the direction of Insert mode complete from the key typed.
4212 * Returns BACKWARD or FORWARD.
4213 */
4214 static int
4215ins_compl_key2dir(int c)
4216{
4217 if (c == Ctrl_P || c == Ctrl_L
4218 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4219 return BACKWARD;
4220 return FORWARD;
4221}
4222
4223/*
4224 * Return TRUE for keys that are used for completion only when the popup menu
4225 * is visible.
4226 */
4227 static int
4228ins_compl_pum_key(int c)
4229{
4230 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4231 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4232 || c == K_UP || c == K_DOWN);
4233}
4234
4235/*
4236 * Decide the number of completions to move forward.
4237 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4238 */
4239 static int
4240ins_compl_key2count(int c)
4241{
4242 int h;
4243
4244 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4245 {
4246 h = pum_get_height();
4247 if (h > 3)
4248 h -= 2; // keep some context
4249 return h;
4250 }
4251 return 1;
4252}
4253
4254/*
4255 * Return TRUE if completion with "c" should insert the match, FALSE if only
4256 * to change the currently selected completion.
4257 */
4258 static int
4259ins_compl_use_match(int c)
4260{
4261 switch (c)
4262 {
4263 case K_UP:
4264 case K_DOWN:
4265 case K_PAGEDOWN:
4266 case K_KPAGEDOWN:
4267 case K_S_DOWN:
4268 case K_PAGEUP:
4269 case K_KPAGEUP:
4270 case K_S_UP:
4271 return FALSE;
4272 }
4273 return TRUE;
4274}
4275
4276/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004277 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4278 * completion)
4279 * Sets the global variables: compl_col, compl_length and compl_pattern.
4280 * Uses the global variables: compl_cont_status and ctrl_x_mode
4281 */
4282 static int
4283get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4284{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004285 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004286 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004287 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004288 {
4289 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4290 ;
4291 compl_col += ++startcol;
4292 compl_length = curs_col - startcol;
4293 }
4294 if (p_ic)
4295 compl_pattern = str_foldcase(line + compl_col,
4296 compl_length, NULL, 0);
4297 else
4298 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4299 if (compl_pattern == NULL)
4300 return FAIL;
4301 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004302 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004303 {
4304 char_u *prefix = (char_u *)"\\<";
4305
4306 // we need up to 2 extra chars for the prefix
4307 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4308 compl_length) + 2);
4309 if (compl_pattern == NULL)
4310 return FAIL;
4311 if (!vim_iswordp(line + compl_col)
4312 || (compl_col > 0
4313 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4314 prefix = (char_u *)"";
4315 STRCPY((char *)compl_pattern, prefix);
4316 (void)quote_meta(compl_pattern + STRLEN(prefix),
4317 line + compl_col, compl_length);
4318 }
4319 else if (--startcol < 0
4320 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4321 {
4322 // Match any word of at least two chars
4323 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4324 if (compl_pattern == NULL)
4325 return FAIL;
4326 compl_col += curs_col;
4327 compl_length = 0;
4328 }
4329 else
4330 {
4331 // Search the point of change class of multibyte character
4332 // or not a word single byte character backward.
4333 if (has_mbyte)
4334 {
4335 int base_class;
4336 int head_off;
4337
4338 startcol -= (*mb_head_off)(line, line + startcol);
4339 base_class = mb_get_class(line + startcol);
4340 while (--startcol >= 0)
4341 {
4342 head_off = (*mb_head_off)(line, line + startcol);
4343 if (base_class != mb_get_class(line + startcol
4344 - head_off))
4345 break;
4346 startcol -= head_off;
4347 }
4348 }
4349 else
4350 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4351 ;
4352 compl_col += ++startcol;
4353 compl_length = (int)curs_col - startcol;
4354 if (compl_length == 1)
4355 {
4356 // Only match word with at least two chars -- webb
4357 // there's no need to call quote_meta,
4358 // alloc(7) is enough -- Acevedo
4359 compl_pattern = alloc(7);
4360 if (compl_pattern == NULL)
4361 return FAIL;
4362 STRCPY((char *)compl_pattern, "\\<");
4363 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4364 STRCAT((char *)compl_pattern, "\\k");
4365 }
4366 else
4367 {
4368 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4369 compl_length) + 2);
4370 if (compl_pattern == NULL)
4371 return FAIL;
4372 STRCPY((char *)compl_pattern, "\\<");
4373 (void)quote_meta(compl_pattern + 2, line + compl_col,
4374 compl_length);
4375 }
4376 }
4377
4378 return OK;
4379}
4380
4381/*
4382 * Get the pattern, column and length for whole line completion or for the
4383 * complete() function.
4384 * Sets the global variables: compl_col, compl_length and compl_pattern.
4385 */
4386 static int
4387get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4388{
4389 compl_col = (colnr_T)getwhitecols(line);
4390 compl_length = (int)curs_col - (int)compl_col;
4391 if (compl_length < 0) // cursor in indent: empty pattern
4392 compl_length = 0;
4393 if (p_ic)
4394 compl_pattern = str_foldcase(line + compl_col, compl_length,
4395 NULL, 0);
4396 else
4397 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4398 if (compl_pattern == NULL)
4399 return FAIL;
4400
4401 return OK;
4402}
4403
4404/*
4405 * Get the pattern, column and length for filename completion.
4406 * Sets the global variables: compl_col, compl_length and compl_pattern.
4407 */
4408 static int
4409get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4410{
4411 // Go back to just before the first filename character.
4412 if (startcol > 0)
4413 {
4414 char_u *p = line + startcol;
4415
4416 MB_PTR_BACK(line, p);
4417 while (p > line && vim_isfilec(PTR2CHAR(p)))
4418 MB_PTR_BACK(line, p);
4419 if (p == line && vim_isfilec(PTR2CHAR(p)))
4420 startcol = 0;
4421 else
4422 startcol = (int)(p - line) + 1;
4423 }
4424
4425 compl_col += startcol;
4426 compl_length = (int)curs_col - startcol;
4427 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4428 if (compl_pattern == NULL)
4429 return FAIL;
4430
4431 return OK;
4432}
4433
4434/*
4435 * Get the pattern, column and length for command-line completion.
4436 * Sets the global variables: compl_col, compl_length and compl_pattern.
4437 */
4438 static int
4439get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4440{
4441 compl_pattern = vim_strnsave(line, curs_col);
4442 if (compl_pattern == NULL)
4443 return FAIL;
4444 set_cmd_context(&compl_xp, compl_pattern,
4445 (int)STRLEN(compl_pattern), curs_col, FALSE);
4446 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4447 || compl_xp.xp_context == EXPAND_NOTHING)
4448 // No completion possible, use an empty pattern to get a
4449 // "pattern not found" message.
4450 compl_col = curs_col;
4451 else
4452 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4453 compl_length = curs_col - compl_col;
4454
4455 return OK;
4456}
4457
4458/*
4459 * Get the pattern, column and length for user defined completion ('omnifunc',
4460 * 'completefunc' and 'thesaurusfunc')
4461 * Sets the global variables: compl_col, compl_length and compl_pattern.
4462 * Uses the global variable: spell_bad_len
4463 */
4464 static int
4465get_userdefined_compl_info(colnr_T curs_col UNUSED)
4466{
4467 int ret = FAIL;
4468
4469#ifdef FEAT_COMPL_FUNC
4470 // Call user defined function 'completefunc' with "a:findstart"
4471 // set to 1 to obtain the length of text to use for completion.
4472 char_u *line;
4473 typval_T args[3];
4474 int col;
4475 char_u *funcname;
4476 pos_T pos;
4477 int save_State = State;
4478 callback_T *cb;
4479
4480 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4481 // length as a string
4482 funcname = get_complete_funcname(ctrl_x_mode);
4483 if (*funcname == NUL)
4484 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004485 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004486 ? "completefunc" : "omnifunc");
4487 return FAIL;
4488 }
4489
4490 args[0].v_type = VAR_NUMBER;
4491 args[0].vval.v_number = 1;
4492 args[1].v_type = VAR_STRING;
4493 args[1].vval.v_string = (char_u *)"";
4494 args[2].v_type = VAR_UNKNOWN;
4495 pos = curwin->w_cursor;
4496 ++textwinlock;
4497 cb = get_insert_callback(ctrl_x_mode);
4498 col = call_callback_retnr(cb, 2, args);
4499 --textwinlock;
4500
4501 State = save_State;
4502 curwin->w_cursor = pos; // restore the cursor position
4503 validate_cursor();
4504 if (!EQUAL_POS(curwin->w_cursor, pos))
4505 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004506 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004507 return FAIL;
4508 }
4509
4510 // Return value -2 means the user complete function wants to
4511 // cancel the complete without an error.
4512 // Return value -3 does the same as -2 and leaves CTRL-X mode.
4513 if (col == -2)
4514 return FAIL;
4515 if (col == -3)
4516 {
4517 ctrl_x_mode = CTRL_X_NORMAL;
4518 edit_submode = NULL;
4519 if (!shortmess(SHM_COMPLETIONMENU))
4520 msg_clr_cmdline();
4521 return FAIL;
4522 }
4523
4524 // Reset extended parameters of completion, when start new
4525 // completion.
4526 compl_opt_refresh_always = FALSE;
4527 compl_opt_suppress_empty = FALSE;
4528
4529 if (col < 0)
4530 col = curs_col;
4531 compl_col = col;
4532 if (compl_col > curs_col)
4533 compl_col = curs_col;
4534
4535 // Setup variables for completion. Need to obtain "line" again,
4536 // it may have become invalid.
4537 line = ml_get(curwin->w_cursor.lnum);
4538 compl_length = curs_col - compl_col;
4539 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4540 if (compl_pattern == NULL)
4541 return FAIL;
4542
4543 ret = OK;
4544#endif
4545
4546 return ret;
4547}
4548
4549/*
4550 * Get the pattern, column and length for spell completion.
4551 * Sets the global variables: compl_col, compl_length and compl_pattern.
4552 * Uses the global variable: spell_bad_len
4553 */
4554 static int
4555get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4556{
4557 int ret = FAIL;
4558#ifdef FEAT_SPELL
4559 char_u *line;
4560
4561 if (spell_bad_len > 0)
4562 compl_col = curs_col - spell_bad_len;
4563 else
4564 compl_col = spell_word_start(startcol);
4565 if (compl_col >= (colnr_T)startcol)
4566 {
4567 compl_length = 0;
4568 compl_col = curs_col;
4569 }
4570 else
4571 {
4572 spell_expand_check_cap(compl_col);
4573 compl_length = (int)curs_col - compl_col;
4574 }
4575 // Need to obtain "line" again, it may have become invalid.
4576 line = ml_get(curwin->w_cursor.lnum);
4577 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4578 if (compl_pattern == NULL)
4579 return FAIL;
4580
4581 ret = OK;
4582#endif
4583
4584 return ret;
4585}
4586
4587/*
4588 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004589 * "startcol" - start column number of the completion pattern/text
4590 * "cur_col" - current cursor column
4591 * On return, "line_invalid" is set to TRUE, if the current line may have
4592 * become invalid and needs to be fetched again.
4593 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004594 */
4595 static int
4596compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4597{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004598 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004599 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4600 && !thesaurus_func_complete(ctrl_x_mode)))
4601 {
4602 return get_normal_compl_info(line, startcol, curs_col);
4603 }
4604 else if (ctrl_x_mode_line_or_eval())
4605 {
4606 return get_wholeline_compl_info(line, curs_col);
4607 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004608 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004609 {
4610 return get_filename_compl_info(line, startcol, curs_col);
4611 }
4612 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4613 {
4614 return get_cmdline_compl_info(line, curs_col);
4615 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004616 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004617 || thesaurus_func_complete(ctrl_x_mode))
4618 {
4619 if (get_userdefined_compl_info(curs_col) == FAIL)
4620 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004621 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004622 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004623 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004624 {
4625 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4626 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004627 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004628 }
4629 else
4630 {
4631 internal_error("ins_complete()");
4632 return FAIL;
4633 }
4634
4635 return OK;
4636}
4637
4638/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004639 * Continue an interrupted completion mode search in "line".
4640 *
4641 * If this same ctrl_x_mode has been interrupted use the text from
4642 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4643 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4644 * the same line as the cursor then fix it (the line has been split because it
4645 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4646 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004647 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004648 static void
4649ins_compl_continue_search(char_u *line)
4650{
4651 // it is a continued search
4652 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004653 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4654 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004655 {
4656 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4657 {
4658 // line (probably) wrapped, set compl_startpos to the
4659 // first non_blank in the line, if it is not a wordchar
4660 // include it to get a better pattern, but then we don't
4661 // want the "\\<" prefix, check it below
4662 compl_col = (colnr_T)getwhitecols(line);
4663 compl_startpos.col = compl_col;
4664 compl_startpos.lnum = curwin->w_cursor.lnum;
4665 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4666 }
4667 else
4668 {
4669 // S_IPOS was set when we inserted a word that was at the
4670 // beginning of the line, which means that we'll go to SOL
4671 // mode but first we need to redefine compl_startpos
4672 if (compl_cont_status & CONT_S_IPOS)
4673 {
4674 compl_cont_status |= CONT_SOL;
4675 compl_startpos.col = (colnr_T)(skipwhite(
4676 line + compl_length
4677 + compl_startpos.col) - line);
4678 }
4679 compl_col = compl_startpos.col;
4680 }
4681 compl_length = curwin->w_cursor.col - (int)compl_col;
4682 // IObuff is used to add a "word from the next line" would we
4683 // have enough space? just being paranoid
4684#define MIN_SPACE 75
4685 if (compl_length > (IOSIZE - MIN_SPACE))
4686 {
4687 compl_cont_status &= ~CONT_SOL;
4688 compl_length = (IOSIZE - MIN_SPACE);
4689 compl_col = curwin->w_cursor.col - compl_length;
4690 }
4691 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4692 if (compl_length < 1)
4693 compl_cont_status &= CONT_LOCAL;
4694 }
4695 else if (ctrl_x_mode_line_or_eval())
4696 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4697 else
4698 compl_cont_status = 0;
4699}
4700
4701/*
4702 * start insert mode completion
4703 */
4704 static int
4705ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004706{
4707 char_u *line;
4708 int startcol = 0; // column where searched text starts
4709 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004710 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004711 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004712 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004713
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004714 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004715
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004716 did_ai = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004717#ifdef FEAT_SMARTINDENT
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004718 did_si = FALSE;
4719 can_si = FALSE;
4720 can_si_back = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004721#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004722 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004723 return FAIL;
4724
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004725 line = ml_get(curwin->w_cursor.lnum);
4726 curs_col = curwin->w_cursor.col;
4727 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004728
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004729 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4730 && compl_cont_mode == ctrl_x_mode)
4731 // this same ctrl-x_mode was interrupted previously. Continue the
4732 // completion.
4733 ins_compl_continue_search(line);
4734 else
4735 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004736
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004737 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004738 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004739 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004740 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004741 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4742 compl_cont_status = 0;
4743 compl_cont_status |= CONT_N_ADDS;
4744 compl_startpos = curwin->w_cursor;
4745 startcol = (int)curs_col;
4746 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004747 }
4748
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004749 // Work out completion pattern and original text -- webb
4750 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4751 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004752 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4753 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004754 // restore did_ai, so that adding comment leader works
4755 did_ai = save_did_ai;
4756 return FAIL;
4757 }
4758 // If "line" was changed while getting completion info get it again.
4759 if (line_invalid)
4760 line = ml_get(curwin->w_cursor.lnum);
4761
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004762 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004763 {
4764 edit_submode_pre = (char_u *)_(" Adding");
4765 if (ctrl_x_mode_line_or_eval())
4766 {
4767 // Insert a new line, keep indentation but ignore 'comments'.
4768 char_u *old = curbuf->b_p_com;
4769
4770 curbuf->b_p_com = (char_u *)"";
4771 compl_startpos.lnum = curwin->w_cursor.lnum;
4772 compl_startpos.col = compl_col;
4773 ins_eol('\r');
4774 curbuf->b_p_com = old;
4775 compl_length = 0;
4776 compl_col = curwin->w_cursor.col;
4777 }
4778 }
4779 else
4780 {
4781 edit_submode_pre = NULL;
4782 compl_startpos.col = compl_col;
4783 }
4784
4785 if (compl_cont_status & CONT_LOCAL)
4786 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4787 else
4788 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4789
4790 // If any of the original typed text has been changed we need to fix
4791 // the redo buffer.
4792 ins_compl_fixRedoBufForLeader(NULL);
4793
4794 // Always add completion for the original text.
4795 vim_free(compl_orig_text);
4796 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4797 if (p_ic)
4798 flags |= CP_ICASE;
4799 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4800 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4801 {
4802 VIM_CLEAR(compl_pattern);
4803 VIM_CLEAR(compl_orig_text);
4804 return FAIL;
4805 }
4806
4807 // showmode might reset the internal line pointers, so it must
4808 // be called before line = ml_get(), or when this address is no
4809 // longer needed. -- Acevedo.
4810 edit_submode_extra = (char_u *)_("-- Searching...");
4811 edit_submode_highl = HLF_COUNT;
4812 showmode();
4813 edit_submode_extra = NULL;
4814 out_flush();
4815
4816 return OK;
4817}
4818
4819/*
4820 * display the completion status message
4821 */
4822 static void
4823ins_compl_show_statusmsg(void)
4824{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004825 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004826 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004827 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004828 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004829 ? (char_u *)_(e_hitend)
4830 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004831 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004832 }
4833
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004834 if (edit_submode_extra == NULL)
4835 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004836 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004837 {
4838 edit_submode_extra = (char_u *)_("Back at original");
4839 edit_submode_highl = HLF_W;
4840 }
4841 else if (compl_cont_status & CONT_S_IPOS)
4842 {
4843 edit_submode_extra = (char_u *)_("Word from other line");
4844 edit_submode_highl = HLF_COUNT;
4845 }
4846 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4847 {
4848 edit_submode_extra = (char_u *)_("The only match");
4849 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004850 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004851 }
4852 else
4853 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004854#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004855 // Update completion sequence number when needed.
4856 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004857 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004858#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004859 // The match should always have a sequence number now, this is
4860 // just a safety check.
4861 if (compl_curr_match->cp_number != -1)
4862 {
4863 // Space for 10 text chars. + 2x10-digit no.s = 31.
4864 // Translations may need more than twice that.
4865 static char_u match_ref[81];
4866
4867 if (compl_matches > 0)
4868 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004869 _("match %d of %d"),
4870 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004871 else
4872 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004873 _("match %d"),
4874 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004875 edit_submode_extra = match_ref;
4876 edit_submode_highl = HLF_R;
4877 if (dollar_vcol >= 0)
4878 curs_columns(FALSE);
4879 }
4880 }
4881 }
4882
4883 // Show a message about what (completion) mode we're in.
4884 if (!compl_opt_suppress_empty)
4885 {
4886 showmode();
4887 if (!shortmess(SHM_COMPLETIONMENU))
4888 {
4889 if (edit_submode_extra != NULL)
4890 {
4891 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004892 {
4893 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004894 msg_attr((char *)edit_submode_extra,
4895 edit_submode_highl < HLF_COUNT
4896 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004897 msg_hist_off = FALSE;
4898 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004899 }
4900 else
4901 msg_clr_cmdline(); // necessary for "noshowmode"
4902 }
4903 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004904}
4905
4906/*
4907 * Do Insert mode completion.
4908 * Called when character "c" was typed, which has a meaning for completion.
4909 * Returns OK if completion was done, FAIL if something failed (out of mem).
4910 */
4911 int
4912ins_complete(int c, int enable_pum)
4913{
4914 int n;
4915 int save_w_wrow;
4916 int save_w_leftcol;
4917 int insert_match;
4918
4919 compl_direction = ins_compl_key2dir(c);
4920 insert_match = ins_compl_use_match(c);
4921
4922 if (!compl_started)
4923 {
4924 if (ins_compl_start() == FAIL)
4925 return FAIL;
4926 }
4927 else if (insert_match && stop_arrow() == FAIL)
4928 return FAIL;
4929
4930 compl_shown_match = compl_curr_match;
4931 compl_shows_dir = compl_direction;
4932
4933 // Find next match (and following matches).
4934 save_w_wrow = curwin->w_wrow;
4935 save_w_leftcol = curwin->w_leftcol;
4936 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4937
4938 // may undisplay the popup menu
4939 ins_compl_upd_pum();
4940
4941 if (n > 1) // all matches have been found
4942 compl_matches = n;
4943 compl_curr_match = compl_shown_match;
4944 compl_direction = compl_shows_dir;
4945
4946 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4947 // mode.
4948 if (got_int && !global_busy)
4949 {
4950 (void)vgetc();
4951 got_int = FALSE;
4952 }
4953
4954 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004955 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004956 {
4957 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4958 // because we couldn't expand anything at first place, but if we used
4959 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4960 // (such as M in M'exico) if not tried already. -- Acevedo
4961 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004962 || compl_status_adding()
4963 || (ctrl_x_mode_not_default()
4964 && !ctrl_x_mode_path_patterns()
4965 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004966 compl_cont_status &= ~CONT_N_ADDS;
4967 }
4968
4969 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4970 compl_cont_status |= CONT_S_IPOS;
4971 else
4972 compl_cont_status &= ~CONT_S_IPOS;
4973
4974 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004975
4976 // Show the popup menu, unless we got interrupted.
4977 if (enable_pum && !compl_interrupted)
4978 show_pum(save_w_wrow, save_w_leftcol);
4979
4980 compl_was_interrupted = compl_interrupted;
4981 compl_interrupted = FALSE;
4982
4983 return OK;
4984}
4985
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00004986/*
4987 * Remove (if needed) and show the popup menu
4988 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004989 static void
4990show_pum(int prev_w_wrow, int prev_w_leftcol)
4991{
4992 // RedrawingDisabled may be set when invoked through complete().
4993 int n = RedrawingDisabled;
4994
4995 RedrawingDisabled = 0;
4996
4997 // If the cursor moved or the display scrolled we need to remove the pum
4998 // first.
4999 setcursor();
5000 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5001 ins_compl_del_pum();
5002
5003 ins_compl_show_pum();
5004 setcursor();
5005 RedrawingDisabled = n;
5006}
5007
5008/*
5009 * Looks in the first "len" chars. of "src" for search-metachars.
5010 * If dest is not NULL the chars. are copied there quoting (with
5011 * a backslash) the metachars, and dest would be NUL terminated.
5012 * Returns the length (needed) of dest
5013 */
5014 static unsigned
5015quote_meta(char_u *dest, char_u *src, int len)
5016{
5017 unsigned m = (unsigned)len + 1; // one extra for the NUL
5018
5019 for ( ; --len >= 0; src++)
5020 {
5021 switch (*src)
5022 {
5023 case '.':
5024 case '*':
5025 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005026 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005027 break;
5028 // FALLTHROUGH
5029 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005030 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005031 break;
5032 // FALLTHROUGH
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 '^': // currently it's not needed.
5038 case '$':
5039 m++;
5040 if (dest != NULL)
5041 *dest++ = '\\';
5042 break;
5043 }
5044 if (dest != NULL)
5045 *dest++ = *src;
5046 // Copy remaining bytes of a multibyte character.
5047 if (has_mbyte)
5048 {
5049 int i, mb_len;
5050
5051 mb_len = (*mb_ptr2len)(src) - 1;
5052 if (mb_len > 0 && len >= mb_len)
5053 for (i = 0; i < mb_len; ++i)
5054 {
5055 --len;
5056 ++src;
5057 if (dest != NULL)
5058 *dest++ = *src;
5059 }
5060 }
5061 }
5062 if (dest != NULL)
5063 *dest = NUL;
5064
5065 return m;
5066}
5067
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005068#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005069 void
5070free_insexpand_stuff(void)
5071{
5072 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005073# ifdef FEAT_EVAL
5074 free_callback(&cfu_cb);
5075 free_callback(&ofu_cb);
5076 free_callback(&tsrfu_cb);
5077# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005078}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005079#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005080
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005081#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005082/*
5083 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5084 * spelled word, if there is one.
5085 */
5086 static void
5087spell_back_to_badword(void)
5088{
5089 pos_T tpos = curwin->w_cursor;
5090
5091 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5092 if (curwin->w_cursor.col != tpos.col)
5093 start_arrow(&tpos);
5094}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005095#endif