blob: c5d0bf286b7844d4f89aafa9936fb0e22c835c18 [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
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126/*
127 * All the current matches are stored in a list.
128 * "compl_first_match" points to the start of the list.
129 * "compl_curr_match" points to the currently selected entry.
130 * "compl_shown_match" is different from compl_curr_match during
131 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000132 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100133 */
134static compl_T *compl_first_match = NULL;
135static compl_T *compl_curr_match = NULL;
136static compl_T *compl_shown_match = NULL;
137static compl_T *compl_old_match = NULL;
138
139// After using a cursor key <Enter> selects a match in the popup menu,
140// otherwise it inserts a line break.
141static int compl_enter_selects = FALSE;
142
143// When "compl_leader" is not NULL only matches that start with this string
144// are used.
145static char_u *compl_leader = NULL;
146
147static int compl_get_longest = FALSE; // put longest common string
148 // in compl_leader
149
150static int compl_no_insert = FALSE; // FALSE: select & insert
151 // TRUE: noinsert
152static int compl_no_select = FALSE; // FALSE: select & insert
153 // TRUE: noselect
bfredl87af60c2022-09-24 11:17:51 +0100154static int compl_longest = FALSE; // FALSE: insert full match
155 // TRUE: insert longest prefix
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
266int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
267int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
268int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
269int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
270int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
271int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
272int ctrl_x_mode_path_patterns(void) {
273 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
274int ctrl_x_mode_path_defines(void) {
275 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
276int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
277int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200278int ctrl_x_mode_cmdline(void) {
279 return ctrl_x_mode == CTRL_X_CMDLINE
280 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100281int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
282int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
283int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000284static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100285int ctrl_x_mode_line_or_eval(void) {
286 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
287
288/*
289 * Whether other than default completion has been selected.
290 */
291 int
292ctrl_x_mode_not_default(void)
293{
294 return ctrl_x_mode != CTRL_X_NORMAL;
295}
296
297/*
zeertzjqdca29d92021-08-31 19:12:51 +0200298 * Whether CTRL-X was typed without a following character,
299 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300 */
301 int
302ctrl_x_mode_not_defined_yet(void)
303{
304 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
305}
306
307/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000308 * Return TRUE if currently in "normal" or "adding" insert completion matches
309 * state
310 */
311 int
312compl_status_adding(void)
313{
314 return compl_cont_status & CONT_ADDING;
315}
316
317/*
318 * Return TRUE if the completion pattern includes start of line, just for
319 * word-wise expansion.
320 */
321 int
322compl_status_sol(void)
323{
324 return compl_cont_status & CONT_SOL;
325}
326
327/*
328 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
329 */
330 int
331compl_status_local(void)
332{
333 return compl_cont_status & CONT_LOCAL;
334}
335
336/*
337 * Clear the completion status flags
338 */
339 void
340compl_status_clear(void)
341{
342 compl_cont_status = 0;
343}
344
345/*
346 * Return TRUE if completion is using the forward direction matches
347 */
348 static int
349compl_dir_forward(void)
350{
351 return compl_direction == FORWARD;
352}
353
354/*
355 * Return TRUE if currently showing forward completion matches
356 */
357 static int
358compl_shows_dir_forward(void)
359{
360 return compl_shows_dir == FORWARD;
361}
362
363/*
364 * Return TRUE if currently showing backward completion matches
365 */
366 static int
367compl_shows_dir_backward(void)
368{
369 return compl_shows_dir == BACKWARD;
370}
371
372/*
373 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100374 */
375 int
376has_compl_option(int dict_opt)
377{
378 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200379#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100380 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200381#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100382 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100383 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
384#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100385 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386#endif
387 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100388 {
389 ctrl_x_mode = CTRL_X_NORMAL;
390 edit_submode = NULL;
391 msg_attr(dict_opt ? _("'dictionary' option is empty")
392 : _("'thesaurus' option is empty"),
393 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100394 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 {
396 vim_beep(BO_COMPL);
397 setcursor();
398 out_flush();
399#ifdef FEAT_EVAL
400 if (!get_vim_var_nr(VV_TESTING))
401#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100402 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100403 }
404 return FALSE;
405 }
406 return TRUE;
407}
408
409/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000410 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100411 * This depends on the current mode.
412 */
413 int
414vim_is_ctrl_x_key(int c)
415{
416 // Always allow ^R - let its results then be checked
417 if (c == Ctrl_R)
418 return TRUE;
419
420 // Accept <PageUp> and <PageDown> if the popup menu is visible.
421 if (ins_compl_pum_key(c))
422 return TRUE;
423
424 switch (ctrl_x_mode)
425 {
426 case 0: // Not in any CTRL-X mode
427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
428 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200429 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100430 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
431 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
432 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
433 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
434 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200435 || c == Ctrl_S || c == Ctrl_K || c == 's'
436 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100437 case CTRL_X_SCROLL:
438 return (c == Ctrl_Y || c == Ctrl_E);
439 case CTRL_X_WHOLE_LINE:
440 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
441 case CTRL_X_FILES:
442 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
443 case CTRL_X_DICTIONARY:
444 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
445 case CTRL_X_THESAURUS:
446 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
447 case CTRL_X_TAGS:
448 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
449#ifdef FEAT_FIND_ID
450 case CTRL_X_PATH_PATTERNS:
451 return (c == Ctrl_P || c == Ctrl_N);
452 case CTRL_X_PATH_DEFINES:
453 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
454#endif
455 case CTRL_X_CMDLINE:
456 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
457 || c == Ctrl_X);
458#ifdef FEAT_COMPL_FUNC
459 case CTRL_X_FUNCTION:
460 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
461 case CTRL_X_OMNI:
462 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
463#endif
464 case CTRL_X_SPELL:
465 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
466 case CTRL_X_EVAL:
467 return (c == Ctrl_P || c == Ctrl_N);
468 }
469 internal_error("vim_is_ctrl_x_key()");
470 return FALSE;
471}
472
473/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000474 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000475 */
476 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478{
479 return match->cp_flags & CP_ORIGINAL_TEXT;
480}
481
482/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000483 * Returns TRUE if "match" is the first match in the completion list.
484 */
485 static int
486is_first_match(compl_T *match)
487{
488 return match == compl_first_match;
489}
490
491/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100492 * Return TRUE when character "c" is part of the item currently being
493 * completed. Used to decide whether to abandon complete mode when the menu
494 * is visible.
495 */
496 int
497ins_compl_accept_char(int c)
498{
499 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
500 // When expanding an identifier only accept identifier chars.
501 return vim_isIDc(c);
502
503 switch (ctrl_x_mode)
504 {
505 case CTRL_X_FILES:
506 // When expanding file name only accept file name chars. But not
507 // path separators, so that "proto/<Tab>" expands files in
508 // "proto", not "proto/" as a whole
509 return vim_isfilec(c) && !vim_ispathsep(c);
510
511 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200512 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100513 case CTRL_X_OMNI:
514 // Command line and Omni completion can work with just about any
515 // printable character, but do stop at white space.
516 return vim_isprintc(c) && !VIM_ISWHITE(c);
517
518 case CTRL_X_WHOLE_LINE:
519 // For while line completion a space can be part of the line.
520 return vim_isprintc(c);
521 }
522 return vim_iswordc(c);
523}
524
525/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000526 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100527 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000528 */
529 static char_u *
530ins_compl_infercase_gettext(
531 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100532 int char_len,
533 int compl_char_len,
534 int min_len,
535 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000536{
537 int *wca; // Wide character array.
538 char_u *p;
539 int i, c;
540 int has_lower = FALSE;
541 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100542 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000543
544 IObuff[0] = NUL;
545
546 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100547 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548 if (wca == NULL)
549 return IObuff;
550
551 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100552 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000553 if (has_mbyte)
554 wca[i] = mb_ptr2char_adv(&p);
555 else
556 wca[i] = *(p++);
557
558 // Rule 1: Were any chars converted to lower?
559 p = compl_orig_text;
560 for (i = 0; i < min_len; ++i)
561 {
562 if (has_mbyte)
563 c = mb_ptr2char_adv(&p);
564 else
565 c = *(p++);
566 if (MB_ISLOWER(c))
567 {
568 has_lower = TRUE;
569 if (MB_ISUPPER(wca[i]))
570 {
571 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100572 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000573 wca[i] = MB_TOLOWER(wca[i]);
574 break;
575 }
576 }
577 }
578
579 // Rule 2: No lower case, 2nd consecutive letter converted to
580 // upper case.
581 if (!has_lower)
582 {
583 p = compl_orig_text;
584 for (i = 0; i < min_len; ++i)
585 {
586 if (has_mbyte)
587 c = mb_ptr2char_adv(&p);
588 else
589 c = *(p++);
590 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
591 {
592 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100593 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000594 wca[i] = MB_TOUPPER(wca[i]);
595 break;
596 }
597 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
598 }
599 }
600
601 // Copy the original case of the part we typed.
602 p = compl_orig_text;
603 for (i = 0; i < min_len; ++i)
604 {
605 if (has_mbyte)
606 c = mb_ptr2char_adv(&p);
607 else
608 c = *(p++);
609 if (MB_ISLOWER(c))
610 wca[i] = MB_TOLOWER(wca[i]);
611 else if (MB_ISUPPER(c))
612 wca[i] = MB_TOUPPER(wca[i]);
613 }
614
615 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000616 p = IObuff;
617 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100618 ga_init2(&gap, 1, 500);
619 while (i < char_len)
620 {
621 if (gap.ga_data != NULL)
622 {
623 if (ga_grow(&gap, 10) == FAIL)
624 {
625 ga_clear(&gap);
626 return (char_u *)"[failed]";
627 }
628 p = (char_u *)gap.ga_data + gap.ga_len;
629 if (has_mbyte)
630 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
631 else
632 {
633 *p = wca[i++];
634 ++gap.ga_len;
635 }
636 }
637 else if ((p - IObuff) + 6 >= IOSIZE)
638 {
639 // Multi-byte characters can occupy up to five bytes more than
640 // ASCII characters, and we also need one byte for NUL, so when
641 // getting to six bytes from the edge of IObuff switch to using a
642 // growarray. Add the character in the next round.
643 if (ga_grow(&gap, IOSIZE) == FAIL)
644 return (char_u *)"[failed]";
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100645 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100646 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100647 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100648 }
649 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000650 p += (*mb_char2bytes)(wca[i++], p);
651 else
652 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100653 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000654 vim_free(wca);
655
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100656 if (gap.ga_data != NULL)
657 {
658 *tofree = gap.ga_data;
659 return gap.ga_data;
660 }
661
662 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000663 return IObuff;
664}
665
666/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100667 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
668 * case of the originally typed text is used, and the case of the completed
669 * text is inferred, ie this tries to work out what case you probably wanted
670 * the rest of the word to be in -- webb
671 */
672 int
673ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200674 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100675 int len,
676 int icase,
677 char_u *fname,
678 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200679 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100680{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200681 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100683 int char_len; // count multi-byte characters
684 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100685 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200686 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100687 int res;
688 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100689
690 if (p_ic && curbuf->b_p_inf && len > 0)
691 {
692 // Infer case of completed part.
693
694 // Find actual length of completion.
695 if (has_mbyte)
696 {
697 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100698 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100699 while (*p != NUL)
700 {
701 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100703 }
704 }
705 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100706 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100707
708 // Find actual length of original text.
709 if (has_mbyte)
710 {
711 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100712 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100713 while (*p != NUL)
714 {
715 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100716 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100717 }
718 }
719 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100720 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100721
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100722 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100726 str = ins_compl_infercase_gettext(str, char_len,
727 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200729 if (cont_s_ipos)
730 flags |= CP_CONT_S_IPOS;
731 if (icase)
732 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200733
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100734 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
735 vim_free(tofree);
736 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737}
738
739/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000740 * Add a match to the list of matches. The arguments are:
741 * str - text of the match to add
742 * len - length of "str". If -1, then the length of "str" is
743 * computed.
744 * fname - file name to associate with this match.
745 * cptext - list of strings to use with this match (for abbr, menu, info
746 * and kind)
747 * user_data - user supplied data (any vim type) for this match
748 * cdir - match direction. If 0, use "compl_direction".
749 * flags_arg - match flags (cp_flags)
750 * adup - accept this match even if it is already present.
751 * If "cdir" is FORWARD, then the match is added after the current match.
752 * Otherwise, it is added before the current match.
753 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100754 * If the given string is already in the list of completions, then return
755 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
756 * maybe because alloc() returns NULL, then FAIL is returned.
757 */
758 static int
759ins_compl_add(
760 char_u *str,
761 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100762 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 char_u **cptext, // extra text for popup menu or NULL
764 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100765 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200766 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100767 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100768{
769 compl_T *match;
770 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200771 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100772
Bram Moolenaarceb06192021-04-04 15:05:22 +0200773 if (flags & CP_FAST)
774 fast_breakcheck();
775 else
776 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 if (got_int)
778 return FAIL;
779 if (len < 0)
780 len = (int)STRLEN(str);
781
782 // If the same match is already present, don't add it.
783 if (compl_first_match != NULL && !adup)
784 {
785 match = compl_first_match;
786 do
787 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000788 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100789 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100790 && ((int)STRLEN(match->cp_str) <= len
791 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100792 return NOTDONE;
793 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000794 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100795 }
796
797 // Remove any popup menu before changing the list of matches.
798 ins_compl_del_pum();
799
800 // Allocate a new match structure.
801 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200802 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100803 if (match == NULL)
804 return FAIL;
805 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200806 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 match->cp_number = 0;
808 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
809 {
810 vim_free(match);
811 return FAIL;
812 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100813
814 // match-fname is:
815 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200816 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100817 // - NULL otherwise. --Acevedo
818 if (fname != NULL
819 && compl_curr_match != NULL
820 && compl_curr_match->cp_fname != NULL
821 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
822 match->cp_fname = compl_curr_match->cp_fname;
823 else if (fname != NULL)
824 {
825 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200826 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100827 }
828 else
829 match->cp_fname = NULL;
830 match->cp_flags = flags;
831
832 if (cptext != NULL)
833 {
834 int i;
835
836 for (i = 0; i < CPT_COUNT; ++i)
837 if (cptext[i] != NULL && *cptext[i] != NUL)
838 match->cp_text[i] = vim_strsave(cptext[i]);
839 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100840#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100841 if (user_data != NULL)
842 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100843#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100844
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000845 // Link the new match structure after (FORWARD) or before (BACKWARD) the
846 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100847 if (compl_first_match == NULL)
848 match->cp_next = match->cp_prev = NULL;
849 else if (dir == FORWARD)
850 {
851 match->cp_next = compl_curr_match->cp_next;
852 match->cp_prev = compl_curr_match;
853 }
854 else // BACKWARD
855 {
856 match->cp_next = compl_curr_match;
857 match->cp_prev = compl_curr_match->cp_prev;
858 }
859 if (match->cp_next)
860 match->cp_next->cp_prev = match;
861 if (match->cp_prev)
862 match->cp_prev->cp_next = match;
863 else // if there's nothing before, it is the first match
864 compl_first_match = match;
865 compl_curr_match = match;
866
867 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200868 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100869 ins_compl_longest_match(match);
870
871 return OK;
872}
873
874/*
875 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200876 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100877 */
878 static int
879ins_compl_equal(compl_T *match, char_u *str, int len)
880{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200881 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200882 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200883 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100884 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
885 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
886}
887
888/*
889 * Reduce the longest common string for match "match".
890 */
891 static void
892ins_compl_longest_match(compl_T *match)
893{
894 char_u *p, *s;
895 int c1, c2;
896 int had_match;
897
898 if (compl_leader == NULL)
899 {
900 // First match, use it as a whole.
901 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000902 if (compl_leader == NULL)
903 return;
904
905 had_match = (curwin->w_cursor.col > compl_col);
906 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000907 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000908 ins_redraw(FALSE);
909
910 // When the match isn't there (to avoid matching itself) remove it
911 // again after redrawing.
912 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100913 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100914 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000915
916 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100917 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000918
919 // Reduce the text if this match differs from compl_leader.
920 p = compl_leader;
921 s = match->cp_str;
922 while (*p != NUL)
923 {
924 if (has_mbyte)
925 {
926 c1 = mb_ptr2char(p);
927 c2 = mb_ptr2char(s);
928 }
929 else
930 {
931 c1 = *p;
932 c2 = *s;
933 }
934 if ((match->cp_flags & CP_ICASE)
935 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
936 break;
937 if (has_mbyte)
938 {
939 MB_PTR_ADV(p);
940 MB_PTR_ADV(s);
941 }
942 else
943 {
944 ++p;
945 ++s;
946 }
947 }
948
949 if (*p != NUL)
950 {
951 // Leader was shortened, need to change the inserted text.
952 *p = NUL;
953 had_match = (curwin->w_cursor.col > compl_col);
954 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000955 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000956 ins_redraw(FALSE);
957
958 // When the match isn't there (to avoid matching itself) remove it
959 // again after redrawing.
960 if (!had_match)
961 ins_compl_delete();
962 }
963
964 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100965}
966
967/*
968 * Add an array of matches to the list of matches.
969 * Frees matches[].
970 */
971 static void
972ins_compl_add_matches(
973 int num_matches,
974 char_u **matches,
975 int icase)
976{
977 int i;
978 int add_r = OK;
979 int dir = compl_direction;
980
981 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100982 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200983 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100984 // if dir was BACKWARD then honor it just once
985 dir = FORWARD;
986 FreeWild(num_matches, matches);
987}
988
989/*
990 * Make the completion list cyclic.
991 * Return the number of matches (excluding the original).
992 */
993 static int
994ins_compl_make_cyclic(void)
995{
996 compl_T *match;
997 int count = 0;
998
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000999 if (compl_first_match == NULL)
1000 return 0;
1001
1002 // Find the end of the list.
1003 match = compl_first_match;
1004 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001005 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001006 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001007 match = match->cp_next;
1008 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001009 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001010 match->cp_next = compl_first_match;
1011 compl_first_match->cp_prev = match;
1012
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001013 return count;
1014}
1015
1016/*
1017 * Return whether there currently is a shown match.
1018 */
1019 int
1020ins_compl_has_shown_match(void)
1021{
1022 return compl_shown_match == NULL
1023 || compl_shown_match != compl_shown_match->cp_next;
1024}
1025
1026/*
1027 * Return whether the shown match is long enough.
1028 */
1029 int
1030ins_compl_long_shown_match(void)
1031{
1032 return (int)STRLEN(compl_shown_match->cp_str)
1033 > curwin->w_cursor.col - compl_col;
1034}
1035
1036/*
1037 * Set variables that store noselect and noinsert behavior from the
1038 * 'completeopt' value.
1039 */
1040 void
1041completeopt_was_set(void)
1042{
1043 compl_no_insert = FALSE;
1044 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001045 compl_longest = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001046 if (strstr((char *)p_cot, "noselect") != NULL)
1047 compl_no_select = TRUE;
1048 if (strstr((char *)p_cot, "noinsert") != NULL)
1049 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001050 if (strstr((char *)p_cot, "longest") != NULL)
1051 compl_longest = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001052}
1053
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001054
1055// "compl_match_array" points the currently displayed list of entries in the
1056// popup menu. It is NULL when there is no popup menu.
1057static pumitem_T *compl_match_array = NULL;
1058static int compl_match_arraysize;
1059
1060/*
1061 * Update the screen and when there is any scrolling remove the popup menu.
1062 */
1063 static void
1064ins_compl_upd_pum(void)
1065{
1066 int h;
1067
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001068 if (compl_match_array == NULL)
1069 return;
1070
1071 h = curwin->w_cline_height;
1072 // Update the screen later, before drawing the popup menu over it.
1073 pum_call_update_screen();
1074 if (h != curwin->w_cline_height)
1075 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001076}
1077
1078/*
1079 * Remove any popup menu.
1080 */
1081 static void
1082ins_compl_del_pum(void)
1083{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001084 if (compl_match_array == NULL)
1085 return;
1086
1087 pum_undisplay();
1088 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001089}
1090
1091/*
1092 * Return TRUE if the popup menu should be displayed.
1093 */
1094 int
1095pum_wanted(void)
1096{
1097 // 'completeopt' must contain "menu" or "menuone"
1098 if (vim_strchr(p_cot, 'm') == NULL)
1099 return FALSE;
1100
1101 // The display looks bad on a B&W display.
1102 if (t_colors < 8
1103#ifdef FEAT_GUI
1104 && !gui.in_use
1105#endif
1106 )
1107 return FALSE;
1108 return TRUE;
1109}
1110
1111/*
1112 * Return TRUE if there are two or more matches to be shown in the popup menu.
1113 * One if 'completopt' contains "menuone".
1114 */
1115 static int
1116pum_enough_matches(void)
1117{
1118 compl_T *compl;
1119 int i;
1120
1121 // Don't display the popup menu if there are no matches or there is only
1122 // one (ignoring the original text).
1123 compl = compl_first_match;
1124 i = 0;
1125 do
1126 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001127 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001128 break;
1129 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001130 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001131
1132 if (strstr((char *)p_cot, "menuone") != NULL)
1133 return (i >= 1);
1134 return (i >= 2);
1135}
1136
Bram Moolenaar3075a452021-11-17 15:51:52 +00001137#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001138/*
1139 * Allocate Dict for the completed item.
1140 * { word, abbr, menu, kind, info }
1141 */
1142 static dict_T *
1143ins_compl_dict_alloc(compl_T *match)
1144{
1145 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1146
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001147 if (dict == NULL)
1148 return NULL;
1149
1150 dict_add_string(dict, "word", match->cp_str);
1151 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1152 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1153 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1154 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1155 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1156 dict_add_string(dict, "user_data", (char_u *)"");
1157 else
1158 dict_add_tv(dict, "user_data", &match->cp_user_data);
1159
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001160 return dict;
1161}
1162
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001163/*
1164 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1165 * completion menu is changed.
1166 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001167 static void
1168trigger_complete_changed_event(int cur)
1169{
1170 dict_T *v_event;
1171 dict_T *item;
1172 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001173 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001174
1175 if (recursive)
1176 return;
1177
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001178 if (cur < 0)
1179 item = dict_alloc();
1180 else
1181 item = ins_compl_dict_alloc(compl_curr_match);
1182 if (item == NULL)
1183 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001184 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001185 dict_add_dict(v_event, "completed_item", item);
1186 pum_set_event_info(v_event);
1187 dict_set_items_ro(v_event);
1188
1189 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001190 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001191 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001192 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001193 recursive = FALSE;
1194
Bram Moolenaar3075a452021-11-17 15:51:52 +00001195 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001196}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001197#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001198
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001199/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001200 * Build a popup menu to show the completion matches.
1201 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1202 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001203 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001204 static int
1205ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001206{
1207 compl_T *compl;
1208 compl_T *shown_compl = NULL;
1209 int did_find_shown_match = FALSE;
1210 int shown_match_ok = FALSE;
1211 int i;
1212 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001213 int lead_len = 0;
1214
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001215 // Need to build the popup menu list.
1216 compl_match_arraysize = 0;
1217 compl = compl_first_match;
1218 if (compl_leader != NULL)
1219 lead_len = (int)STRLEN(compl_leader);
1220
1221 do
1222 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001223 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001224 && (compl_leader == NULL
1225 || ins_compl_equal(compl, compl_leader, lead_len)))
1226 ++compl_match_arraysize;
1227 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001228 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001229
1230 if (compl_match_arraysize == 0)
1231 return -1;
1232
1233 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1234 if (compl_match_array == NULL)
1235 return -1;
1236
1237 // If the current match is the original text don't find the first
1238 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001239 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001240 shown_match_ok = TRUE;
1241
1242 i = 0;
1243 compl = compl_first_match;
1244 do
1245 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001246 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001247 && (compl_leader == NULL
1248 || ins_compl_equal(compl, compl_leader, lead_len)))
1249 {
1250 if (!shown_match_ok)
1251 {
1252 if (compl == compl_shown_match || did_find_shown_match)
1253 {
1254 // This item is the shown match or this is the
1255 // first displayed item after the shown match.
1256 compl_shown_match = compl;
1257 did_find_shown_match = TRUE;
1258 shown_match_ok = TRUE;
1259 }
1260 else
1261 // Remember this displayed match for when the
1262 // shown match is just below it.
1263 shown_compl = compl;
1264 cur = i;
1265 }
1266
1267 if (compl->cp_text[CPT_ABBR] != NULL)
1268 compl_match_array[i].pum_text =
1269 compl->cp_text[CPT_ABBR];
1270 else
1271 compl_match_array[i].pum_text = compl->cp_str;
1272 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1273 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1274 if (compl->cp_text[CPT_MENU] != NULL)
1275 compl_match_array[i++].pum_extra =
1276 compl->cp_text[CPT_MENU];
1277 else
1278 compl_match_array[i++].pum_extra = compl->cp_fname;
1279 }
1280
1281 if (compl == compl_shown_match)
1282 {
1283 did_find_shown_match = TRUE;
1284
1285 // When the original text is the shown match don't set
1286 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001287 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001288 shown_match_ok = TRUE;
1289
1290 if (!shown_match_ok && shown_compl != NULL)
1291 {
1292 // The shown match isn't displayed, set it to the
1293 // previously displayed match.
1294 compl_shown_match = shown_compl;
1295 shown_match_ok = TRUE;
1296 }
1297 }
1298 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001299 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001300
1301 if (!shown_match_ok) // no displayed match at all
1302 cur = -1;
1303
1304 return cur;
1305}
1306
1307/*
1308 * Show the popup menu for the list of matches.
1309 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1310 */
1311 void
1312ins_compl_show_pum(void)
1313{
1314 int i;
1315 int cur = -1;
1316 colnr_T col;
1317
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001318 if (!pum_wanted() || !pum_enough_matches())
1319 return;
1320
1321#if defined(FEAT_EVAL)
1322 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001323 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001324#endif
1325
1326 // Update the screen later, before drawing the popup menu over it.
1327 pum_call_update_screen();
1328
1329 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001330 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001331 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001332 else
1333 {
1334 // popup menu already exists, only need to find the current item.
1335 for (i = 0; i < compl_match_arraysize; ++i)
1336 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1337 || compl_match_array[i].pum_text
1338 == compl_shown_match->cp_text[CPT_ABBR])
1339 {
1340 cur = i;
1341 break;
1342 }
1343 }
1344
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001345 if (compl_match_array == NULL)
1346 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001347
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 // In Replace mode when a $ is displayed at the end of the line only
1349 // part of the screen would be updated. We do need to redraw here.
1350 dollar_vcol = -1;
1351
1352 // Compute the screen column of the start of the completed text.
1353 // Use the cursor to get all wrapping and other settings right.
1354 col = curwin->w_cursor.col;
1355 curwin->w_cursor.col = compl_col;
1356 pum_display(compl_match_array, compl_match_arraysize, cur);
1357 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001358
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001359#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001360 if (has_completechanged())
1361 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001362#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001363}
1364
1365#define DICT_FIRST (1) // use just first element in "dict"
1366#define DICT_EXACT (2) // "dict" is the exact name of a file
1367
1368/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001369 * Add any identifiers that match the given pattern "pat" in the list of
1370 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001371 */
1372 static void
1373ins_compl_dictionaries(
1374 char_u *dict_start,
1375 char_u *pat,
1376 int flags, // DICT_FIRST and/or DICT_EXACT
1377 int thesaurus) // Thesaurus completion
1378{
1379 char_u *dict = dict_start;
1380 char_u *ptr;
1381 char_u *buf;
1382 regmatch_T regmatch;
1383 char_u **files;
1384 int count;
1385 int save_p_scs;
1386 int dir = compl_direction;
1387
1388 if (*dict == NUL)
1389 {
1390#ifdef FEAT_SPELL
1391 // When 'dictionary' is empty and spell checking is enabled use
1392 // "spell".
1393 if (!thesaurus && curwin->w_p_spell)
1394 dict = (char_u *)"spell";
1395 else
1396#endif
1397 return;
1398 }
1399
1400 buf = alloc(LSIZE);
1401 if (buf == NULL)
1402 return;
1403 regmatch.regprog = NULL; // so that we can goto theend
1404
1405 // If 'infercase' is set, don't use 'smartcase' here
1406 save_p_scs = p_scs;
1407 if (curbuf->b_p_inf)
1408 p_scs = FALSE;
1409
1410 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1411 // to only match at the start of a line. Otherwise just match the
1412 // pattern. Also need to double backslashes.
1413 if (ctrl_x_mode_line_or_eval())
1414 {
1415 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1416 size_t len;
1417
1418 if (pat_esc == NULL)
1419 goto theend;
1420 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001421 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001422 if (ptr == NULL)
1423 {
1424 vim_free(pat_esc);
1425 goto theend;
1426 }
1427 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1428 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1429 vim_free(pat_esc);
1430 vim_free(ptr);
1431 }
1432 else
1433 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001434 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001435 if (regmatch.regprog == NULL)
1436 goto theend;
1437 }
1438
1439 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1440 regmatch.rm_ic = ignorecase(pat);
1441 while (*dict != NUL && !got_int && !compl_interrupted)
1442 {
1443 // copy one dictionary file name into buf
1444 if (flags == DICT_EXACT)
1445 {
1446 count = 1;
1447 files = &dict;
1448 }
1449 else
1450 {
1451 // Expand wildcards in the dictionary name, but do not allow
1452 // backticks (for security, the 'dict' option may have been set in
1453 // a modeline).
1454 copy_option_part(&dict, buf, LSIZE, ",");
1455# ifdef FEAT_SPELL
1456 if (!thesaurus && STRCMP(buf, "spell") == 0)
1457 count = -1;
1458 else
1459# endif
1460 if (vim_strchr(buf, '`') != NULL
1461 || expand_wildcards(1, &buf, &count, &files,
1462 EW_FILE|EW_SILENT) != OK)
1463 count = 0;
1464 }
1465
1466# ifdef FEAT_SPELL
1467 if (count == -1)
1468 {
1469 // Complete from active spelling. Skip "\<" in the pattern, we
1470 // don't use it as a RE.
1471 if (pat[0] == '\\' && pat[1] == '<')
1472 ptr = pat + 2;
1473 else
1474 ptr = pat;
1475 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1476 }
1477 else
1478# endif
1479 if (count > 0) // avoid warning for using "files" uninit
1480 {
1481 ins_compl_files(count, files, thesaurus, flags,
1482 &regmatch, buf, &dir);
1483 if (flags != DICT_EXACT)
1484 FreeWild(count, files);
1485 }
1486 if (flags != 0)
1487 break;
1488 }
1489
1490theend:
1491 p_scs = save_p_scs;
1492 vim_regfree(regmatch.regprog);
1493 vim_free(buf);
1494}
1495
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001496/*
1497 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1498 * skipping the word at 'skip_word'. Returns OK on success.
1499 */
1500 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001501thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001502 char_u *fname,
1503 char_u **buf_arg,
1504 int dir,
1505 char_u *skip_word)
1506{
1507 int status = OK;
1508 char_u *ptr;
1509 char_u *wstart;
1510
1511 // Add the other matches on the line
1512 ptr = *buf_arg;
1513 while (!got_int)
1514 {
1515 // Find start of the next word. Skip white
1516 // space and punctuation.
1517 ptr = find_word_start(ptr);
1518 if (*ptr == NUL || *ptr == NL)
1519 break;
1520 wstart = ptr;
1521
1522 // Find end of the word.
1523 if (has_mbyte)
1524 // Japanese words may have characters in
1525 // different classes, only separate words
1526 // with single-byte non-word characters.
1527 while (*ptr != NUL)
1528 {
1529 int l = (*mb_ptr2len)(ptr);
1530
1531 if (l < 2 && !vim_iswordc(*ptr))
1532 break;
1533 ptr += l;
1534 }
1535 else
1536 ptr = find_word_end(ptr);
1537
1538 // Add the word. Skip the regexp match.
1539 if (wstart != skip_word)
1540 {
1541 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1542 fname, dir, FALSE);
1543 if (status == FAIL)
1544 break;
1545 }
1546 }
1547
1548 *buf_arg = ptr;
1549 return status;
1550}
1551
1552/*
1553 * Process "count" dictionary/thesaurus "files" and add the text matching
1554 * "regmatch".
1555 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001556 static void
1557ins_compl_files(
1558 int count,
1559 char_u **files,
1560 int thesaurus,
1561 int flags,
1562 regmatch_T *regmatch,
1563 char_u *buf,
1564 int *dir)
1565{
1566 char_u *ptr;
1567 int i;
1568 FILE *fp;
1569 int add_r;
1570
1571 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1572 {
1573 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001574 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001575 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001576 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001577 vim_snprintf((char *)IObuff, IOSIZE,
1578 _("Scanning dictionary: %s"), (char *)files[i]);
1579 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1580 }
1581
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001582 if (fp == NULL)
1583 continue;
1584
1585 // Read dictionary file line by line.
1586 // Check each line for a match.
1587 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001588 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001589 ptr = buf;
1590 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001591 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001592 ptr = regmatch->startp[0];
1593 if (ctrl_x_mode_line_or_eval())
1594 ptr = find_line_end(ptr);
1595 else
1596 ptr = find_word_end(ptr);
1597 add_r = ins_compl_add_infercase(regmatch->startp[0],
1598 (int)(ptr - regmatch->startp[0]),
1599 p_ic, files[i], *dir, FALSE);
1600 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001601 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001602 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001603 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001604 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001605 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001606 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001607 if (add_r == OK)
1608 // if dir was BACKWARD then honor it just once
1609 *dir = FORWARD;
1610 else if (add_r == FAIL)
1611 break;
1612 // avoid expensive call to vim_regexec() when at end
1613 // of line
1614 if (*ptr == '\n' || got_int)
1615 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001616 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001617 line_breakcheck();
1618 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001619 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001620 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001621 }
1622}
1623
1624/*
1625 * Find the start of the next word.
1626 * Returns a pointer to the first char of the word. Also stops at a NUL.
1627 */
1628 char_u *
1629find_word_start(char_u *ptr)
1630{
1631 if (has_mbyte)
1632 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1633 ptr += (*mb_ptr2len)(ptr);
1634 else
1635 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1636 ++ptr;
1637 return ptr;
1638}
1639
1640/*
1641 * Find the end of the word. Assumes it starts inside a word.
1642 * Returns a pointer to just after the word.
1643 */
1644 char_u *
1645find_word_end(char_u *ptr)
1646{
1647 int start_class;
1648
1649 if (has_mbyte)
1650 {
1651 start_class = mb_get_class(ptr);
1652 if (start_class > 1)
1653 while (*ptr != NUL)
1654 {
1655 ptr += (*mb_ptr2len)(ptr);
1656 if (mb_get_class(ptr) != start_class)
1657 break;
1658 }
1659 }
1660 else
1661 while (vim_iswordc(*ptr))
1662 ++ptr;
1663 return ptr;
1664}
1665
1666/*
1667 * Find the end of the line, omitting CR and NL at the end.
1668 * Returns a pointer to just after the line.
1669 */
1670 static char_u *
1671find_line_end(char_u *ptr)
1672{
1673 char_u *s;
1674
1675 s = ptr + STRLEN(ptr);
1676 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1677 --s;
1678 return s;
1679}
1680
1681/*
1682 * Free the list of completions
1683 */
1684 static void
1685ins_compl_free(void)
1686{
1687 compl_T *match;
1688 int i;
1689
1690 VIM_CLEAR(compl_pattern);
1691 VIM_CLEAR(compl_leader);
1692
1693 if (compl_first_match == NULL)
1694 return;
1695
1696 ins_compl_del_pum();
1697 pum_clear();
1698
1699 compl_curr_match = compl_first_match;
1700 do
1701 {
1702 match = compl_curr_match;
1703 compl_curr_match = compl_curr_match->cp_next;
1704 vim_free(match->cp_str);
1705 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001706 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001707 vim_free(match->cp_fname);
1708 for (i = 0; i < CPT_COUNT; ++i)
1709 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001710#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001711 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001712#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001713 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001714 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001715 compl_first_match = compl_curr_match = NULL;
1716 compl_shown_match = NULL;
1717 compl_old_match = NULL;
1718}
1719
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001720/*
1721 * Reset/clear the completion state.
1722 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001723 void
1724ins_compl_clear(void)
1725{
1726 compl_cont_status = 0;
1727 compl_started = FALSE;
1728 compl_matches = 0;
1729 VIM_CLEAR(compl_pattern);
1730 VIM_CLEAR(compl_leader);
1731 edit_submode_extra = NULL;
1732 VIM_CLEAR(compl_orig_text);
1733 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001734#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001735 // clear v:completed_item
1736 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001737#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001738}
1739
1740/*
1741 * Return TRUE when Insert completion is active.
1742 */
1743 int
1744ins_compl_active(void)
1745{
1746 return compl_started;
1747}
1748
1749/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001750 * Selected one of the matches. When FALSE the match was edited or using the
1751 * longest common string.
1752 */
1753 int
1754ins_compl_used_match(void)
1755{
1756 return compl_used_match;
1757}
1758
1759/*
1760 * Initialize get longest common string.
1761 */
1762 void
1763ins_compl_init_get_longest(void)
1764{
1765 compl_get_longest = FALSE;
1766}
1767
1768/*
1769 * Returns TRUE when insert completion is interrupted.
1770 */
1771 int
1772ins_compl_interrupted(void)
1773{
1774 return compl_interrupted;
1775}
1776
1777/*
1778 * Returns TRUE if the <Enter> key selects a match in the completion popup
1779 * menu.
1780 */
1781 int
1782ins_compl_enter_selects(void)
1783{
1784 return compl_enter_selects;
1785}
1786
1787/*
1788 * Return the column where the text starts that is being completed
1789 */
1790 colnr_T
1791ins_compl_col(void)
1792{
1793 return compl_col;
1794}
1795
1796/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001797 * Return the length in bytes of the text being completed
1798 */
1799 int
1800ins_compl_len(void)
1801{
1802 return compl_length;
1803}
1804
1805/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001806 * Delete one character before the cursor and show the subset of the matches
1807 * that match the word that is now before the cursor.
1808 * Returns the character to be used, NUL if the work is done and another char
1809 * to be got from the user.
1810 */
1811 int
1812ins_compl_bs(void)
1813{
1814 char_u *line;
1815 char_u *p;
1816
1817 line = ml_get_curline();
1818 p = line + curwin->w_cursor.col;
1819 MB_PTR_BACK(line, p);
1820
1821 // Stop completion when the whole word was deleted. For Omni completion
1822 // allow the word to be deleted, we won't match everything.
1823 // Respect the 'backspace' option.
1824 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001825 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1826 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001827 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1828 - compl_length < 0))
1829 return K_BS;
1830
1831 // Deleted more than what was used to find matches or didn't finish
1832 // finding all matches: need to look for matches all over again.
1833 if (curwin->w_cursor.col <= compl_col + compl_length
1834 || ins_compl_need_restart())
1835 ins_compl_restart();
1836
1837 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001838 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001839 if (compl_leader == NULL)
1840 return K_BS;
1841
1842 ins_compl_new_leader();
1843 if (compl_shown_match != NULL)
1844 // Make sure current match is not a hidden item.
1845 compl_curr_match = compl_shown_match;
1846 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001847}
1848
1849/*
1850 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1851 * be called.
1852 */
1853 static int
1854ins_compl_need_restart(void)
1855{
1856 // Return TRUE if we didn't complete finding matches or when the
1857 // 'completefunc' returned "always" in the "refresh" dictionary item.
1858 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001859 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001860 && compl_opt_refresh_always);
1861}
1862
1863/*
1864 * Called after changing "compl_leader".
1865 * Show the popup menu with a different set of matches.
1866 * May also search for matches again if the previous search was interrupted.
1867 */
1868 static void
1869ins_compl_new_leader(void)
1870{
1871 ins_compl_del_pum();
1872 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001873 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001874 compl_used_match = FALSE;
1875
1876 if (compl_started)
1877 ins_compl_set_original_text(compl_leader);
1878 else
1879 {
1880#ifdef FEAT_SPELL
1881 spell_bad_len = 0; // need to redetect bad word
1882#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001883 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001884 // the popup menu display the changed text before the cursor. Set
1885 // "compl_restarting" to avoid that the first match is inserted.
1886 pum_call_update_screen();
1887#ifdef FEAT_GUI
1888 if (gui.in_use)
1889 {
1890 // Show the cursor after the match, not after the redrawn text.
1891 setcursor();
1892 out_flush_cursor(FALSE, FALSE);
1893 }
1894#endif
1895 compl_restarting = TRUE;
1896 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1897 compl_cont_status = 0;
1898 compl_restarting = FALSE;
1899 }
1900
1901 compl_enter_selects = !compl_used_match;
1902
1903 // Show the popup menu with a different set of matches.
1904 ins_compl_show_pum();
1905
1906 // Don't let Enter select the original text when there is no popup menu.
1907 if (compl_match_array == NULL)
1908 compl_enter_selects = FALSE;
1909}
1910
1911/*
1912 * Return the length of the completion, from the completion start column to
1913 * the cursor column. Making sure it never goes below zero.
1914 */
1915 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001916get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001917{
1918 int off = (int)curwin->w_cursor.col - (int)compl_col;
1919
1920 if (off < 0)
1921 return 0;
1922 return off;
1923}
1924
1925/*
1926 * Append one character to the match leader. May reduce the number of
1927 * matches.
1928 */
1929 void
1930ins_compl_addleader(int c)
1931{
1932 int cc;
1933
1934 if (stop_arrow() == FAIL)
1935 return;
1936 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1937 {
1938 char_u buf[MB_MAXBYTES + 1];
1939
1940 (*mb_char2bytes)(c, buf);
1941 buf[cc] = NUL;
1942 ins_char_bytes(buf, cc);
1943 if (compl_opt_refresh_always)
1944 AppendToRedobuff(buf);
1945 }
1946 else
1947 {
1948 ins_char(c);
1949 if (compl_opt_refresh_always)
1950 AppendCharToRedobuff(c);
1951 }
1952
1953 // If we didn't complete finding matches we must search again.
1954 if (ins_compl_need_restart())
1955 ins_compl_restart();
1956
1957 // When 'always' is set, don't reset compl_leader. While completing,
1958 // cursor doesn't point original position, changing compl_leader would
1959 // break redo.
1960 if (!compl_opt_refresh_always)
1961 {
1962 vim_free(compl_leader);
1963 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001964 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001965 if (compl_leader != NULL)
1966 ins_compl_new_leader();
1967 }
1968}
1969
1970/*
1971 * Setup for finding completions again without leaving CTRL-X mode. Used when
1972 * BS or a key was typed while still searching for matches.
1973 */
1974 static void
1975ins_compl_restart(void)
1976{
1977 ins_compl_free();
1978 compl_started = FALSE;
1979 compl_matches = 0;
1980 compl_cont_status = 0;
1981 compl_cont_mode = 0;
1982}
1983
1984/*
1985 * Set the first match, the original text.
1986 */
1987 static void
1988ins_compl_set_original_text(char_u *str)
1989{
1990 char_u *p;
1991
1992 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001993 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1994 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001995 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001996 {
1997 p = vim_strsave(str);
1998 if (p != NULL)
1999 {
2000 vim_free(compl_first_match->cp_str);
2001 compl_first_match->cp_str = p;
2002 }
2003 }
2004 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002005 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002006 {
2007 p = vim_strsave(str);
2008 if (p != NULL)
2009 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002010 vim_free(compl_first_match->cp_prev->cp_str);
2011 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002012 }
2013 }
2014}
2015
2016/*
2017 * Append one character to the match leader. May reduce the number of
2018 * matches.
2019 */
2020 void
2021ins_compl_addfrommatch(void)
2022{
2023 char_u *p;
2024 int len = (int)curwin->w_cursor.col - (int)compl_col;
2025 int c;
2026 compl_T *cp;
2027
2028 p = compl_shown_match->cp_str;
2029 if ((int)STRLEN(p) <= len) // the match is too short
2030 {
2031 // When still at the original match use the first entry that matches
2032 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002033 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002034 return;
2035
2036 p = NULL;
2037 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002038 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002039 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002040 if (compl_leader == NULL
2041 || ins_compl_equal(cp, compl_leader,
2042 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002043 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002044 p = cp->cp_str;
2045 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002046 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002047 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002048 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002049 return;
2050 }
2051 p += len;
2052 c = PTR2CHAR(p);
2053 ins_compl_addleader(c);
2054}
2055
2056/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002057 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002058 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2059 * compl_cont_mode and compl_cont_status.
2060 * Returns TRUE when the character is not to be inserted.
2061 */
2062 static int
2063set_ctrl_x_mode(int c)
2064{
2065 int retval = FALSE;
2066
2067 switch (c)
2068 {
2069 case Ctrl_E:
2070 case Ctrl_Y:
2071 // scroll the window one line up or down
2072 ctrl_x_mode = CTRL_X_SCROLL;
2073 if (!(State & REPLACE_FLAG))
2074 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2075 else
2076 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2077 edit_submode_pre = NULL;
2078 showmode();
2079 break;
2080 case Ctrl_L:
2081 // complete whole line
2082 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2083 break;
2084 case Ctrl_F:
2085 // complete filenames
2086 ctrl_x_mode = CTRL_X_FILES;
2087 break;
2088 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002089 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002090 ctrl_x_mode = CTRL_X_DICTIONARY;
2091 break;
2092 case Ctrl_R:
2093 // Register insertion without exiting CTRL-X mode
2094 // Simply allow ^R to happen without affecting ^X mode
2095 break;
2096 case Ctrl_T:
2097 // complete words from a thesaurus
2098 ctrl_x_mode = CTRL_X_THESAURUS;
2099 break;
2100#ifdef FEAT_COMPL_FUNC
2101 case Ctrl_U:
2102 // user defined completion
2103 ctrl_x_mode = CTRL_X_FUNCTION;
2104 break;
2105 case Ctrl_O:
2106 // omni completion
2107 ctrl_x_mode = CTRL_X_OMNI;
2108 break;
2109#endif
2110 case 's':
2111 case Ctrl_S:
2112 // complete spelling suggestions
2113 ctrl_x_mode = CTRL_X_SPELL;
2114#ifdef FEAT_SPELL
2115 ++emsg_off; // Avoid getting the E756 error twice.
2116 spell_back_to_badword();
2117 --emsg_off;
2118#endif
2119 break;
2120 case Ctrl_RSB:
2121 // complete tag names
2122 ctrl_x_mode = CTRL_X_TAGS;
2123 break;
2124#ifdef FEAT_FIND_ID
2125 case Ctrl_I:
2126 case K_S_TAB:
2127 // complete keywords from included files
2128 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2129 break;
2130 case Ctrl_D:
2131 // complete definitions from included files
2132 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2133 break;
2134#endif
2135 case Ctrl_V:
2136 case Ctrl_Q:
2137 // complete vim commands
2138 ctrl_x_mode = CTRL_X_CMDLINE;
2139 break;
2140 case Ctrl_Z:
2141 // stop completion
2142 ctrl_x_mode = CTRL_X_NORMAL;
2143 edit_submode = NULL;
2144 showmode();
2145 retval = TRUE;
2146 break;
2147 case Ctrl_P:
2148 case Ctrl_N:
2149 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2150 // just started ^X mode, or there were enough ^X's to cancel
2151 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2152 // do normal expansion when interrupting a different mode (say
2153 // ^X^F^X^P or ^P^X^X^P, see below)
2154 // nothing changes if interrupting mode 0, (eg, the flag
2155 // doesn't change when going to ADDING mode -- Acevedo
2156 if (!(compl_cont_status & CONT_INTRPT))
2157 compl_cont_status |= CONT_LOCAL;
2158 else if (compl_cont_mode != 0)
2159 compl_cont_status &= ~CONT_LOCAL;
2160 // FALLTHROUGH
2161 default:
2162 // If we have typed at least 2 ^X's... for modes != 0, we set
2163 // compl_cont_status = 0 (eg, as if we had just started ^X
2164 // mode).
2165 // For mode 0, we set "compl_cont_mode" to an impossible
2166 // value, in both cases ^X^X can be used to restart the same
2167 // mode (avoiding ADDING mode).
2168 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2169 // 'complete' and local ^P expansions respectively.
2170 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2171 // mode -- Acevedo
2172 if (c == Ctrl_X)
2173 {
2174 if (compl_cont_mode != 0)
2175 compl_cont_status = 0;
2176 else
2177 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2178 }
2179 ctrl_x_mode = CTRL_X_NORMAL;
2180 edit_submode = NULL;
2181 showmode();
2182 break;
2183 }
2184
2185 return retval;
2186}
2187
2188/*
2189 * Stop insert completion mode
2190 */
2191 static int
2192ins_compl_stop(int c, int prev_mode, int retval)
2193{
2194 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002195 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002196
2197 // Get here when we have finished typing a sequence of ^N and
2198 // ^P or other completion characters in CTRL-X mode. Free up
2199 // memory that was used, and make sure we can redo the insert.
2200 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2201 {
2202 // If any of the original typed text has been changed, eg when
2203 // ignorecase is set, we must add back-spaces to the redo
2204 // buffer. We add as few as necessary to delete just the part
2205 // of the original text that has changed.
2206 // When using the longest match, edited the match or used
2207 // CTRL-E then don't use the current match.
2208 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2209 ptr = compl_curr_match->cp_str;
2210 else
2211 ptr = NULL;
2212 ins_compl_fixRedoBufForLeader(ptr);
2213 }
2214
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002215 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002216
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002217 // When completing whole lines: fix indent for 'cindent'.
2218 // Otherwise, break line if it's too long.
2219 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2220 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002221 // re-indent the current line
2222 if (want_cindent)
2223 {
2224 do_c_expr_indent();
2225 want_cindent = FALSE; // don't do it again
2226 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002227 }
2228 else
2229 {
2230 int prev_col = curwin->w_cursor.col;
2231
2232 // put the cursor on the last char, for 'tw' formatting
2233 if (prev_col > 0)
2234 dec_cursor();
2235 // only format when something was inserted
2236 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2237 insertchar(NUL, 0, -1);
2238 if (prev_col > 0
2239 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2240 inc_cursor();
2241 }
2242
2243 // If the popup menu is displayed pressing CTRL-Y means accepting
2244 // the selection without inserting anything. When
2245 // compl_enter_selects is set the Enter key does the same.
2246 if ((c == Ctrl_Y || (compl_enter_selects
2247 && (c == CAR || c == K_KENTER || c == NL)))
2248 && pum_visible())
2249 retval = TRUE;
2250
2251 // CTRL-E means completion is Ended, go back to the typed text.
2252 // but only do this, if the Popup is still visible
2253 if (c == Ctrl_E)
2254 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002255 char_u *p = NULL;
2256
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002257 ins_compl_delete();
2258 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002259 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002260 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002261 p = compl_orig_text;
2262 if (p != NULL)
2263 {
2264 int compl_len = get_compl_len();
2265 int len = (int)STRLEN(p);
2266
2267 if (len > compl_len)
2268 ins_bytes_len(p + compl_len, len - compl_len);
2269 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002270 retval = TRUE;
2271 }
2272
2273 auto_format(FALSE, TRUE);
2274
2275 // Trigger the CompleteDonePre event to give scripts a chance to
2276 // act upon the completion before clearing the info, and restore
2277 // ctrl_x_mode, so that complete_info() can be used.
2278 ctrl_x_mode = prev_mode;
2279 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2280
2281 ins_compl_free();
2282 compl_started = FALSE;
2283 compl_matches = 0;
2284 if (!shortmess(SHM_COMPLETIONMENU))
2285 msg_clr_cmdline(); // necessary for "noshowmode"
2286 ctrl_x_mode = CTRL_X_NORMAL;
2287 compl_enter_selects = FALSE;
2288 if (edit_submode != NULL)
2289 {
2290 edit_submode = NULL;
2291 showmode();
2292 }
2293
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002294 if (c == Ctrl_C && cmdwin_type != 0)
2295 // Avoid the popup menu remains displayed when leaving the
2296 // command line window.
2297 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002298 // Indent now if a key was typed that is in 'cinkeys'.
2299 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2300 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002301 // Trigger the CompleteDone event to give scripts a chance to act
2302 // upon the end of completion.
2303 ins_apply_autocmds(EVENT_COMPLETEDONE);
2304
2305 return retval;
2306}
2307
2308/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002309 * Prepare for Insert mode completion, or stop it.
2310 * Called just after typing a character in Insert mode.
2311 * Returns TRUE when the character is not to be inserted;
2312 */
2313 int
2314ins_compl_prep(int c)
2315{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002316 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002317 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002318
2319 // Forget any previous 'special' messages if this is actually
2320 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2321 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2322 edit_submode_extra = NULL;
2323
2324 // Ignore end of Select mode mapping and mouse scroll buttons.
2325 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002326 || c == K_MOUSELEFT || c == K_MOUSERIGHT
2327 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002328 return retval;
2329
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002330#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002331 // Ignore mouse events in a popup window
2332 if (is_mouse_key(c))
2333 {
2334 // Ignore drag and release events, the position does not need to be in
2335 // the popup and it may have just closed.
2336 if (c == K_LEFTRELEASE
2337 || c == K_LEFTRELEASE_NM
2338 || c == K_MIDDLERELEASE
2339 || c == K_RIGHTRELEASE
2340 || c == K_X1RELEASE
2341 || c == K_X2RELEASE
2342 || c == K_LEFTDRAG
2343 || c == K_MIDDLEDRAG
2344 || c == K_RIGHTDRAG
2345 || c == K_X1DRAG
2346 || c == K_X2DRAG)
2347 return retval;
2348 if (popup_visible)
2349 {
2350 int row = mouse_row;
2351 int col = mouse_col;
2352 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2353
2354 if (wp != NULL && WIN_IS_POPUP(wp))
2355 return retval;
2356 }
2357 }
2358#endif
2359
zeertzjqdca29d92021-08-31 19:12:51 +02002360 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2361 {
2362 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2363 || !vim_is_ctrl_x_key(c))
2364 {
2365 // Not starting another completion mode.
2366 ctrl_x_mode = CTRL_X_CMDLINE;
2367
2368 // CTRL-X CTRL-Z should stop completion without inserting anything
2369 if (c == Ctrl_Z)
2370 retval = TRUE;
2371 }
2372 else
2373 {
2374 ctrl_x_mode = CTRL_X_CMDLINE;
2375
2376 // Other CTRL-X keys first stop completion, then start another
2377 // completion mode.
2378 ins_compl_prep(' ');
2379 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2380 }
2381 }
2382
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002383 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002384 if (ctrl_x_mode_not_defined_yet()
2385 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002386 {
bfredl87af60c2022-09-24 11:17:51 +01002387 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002388 compl_used_match = TRUE;
2389
2390 }
2391
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002392 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002393 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2394 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002395 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002396 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002397 {
2398 // We're already in CTRL-X mode, do we stay in it?
2399 if (!vim_is_ctrl_x_key(c))
2400 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002401 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002402 ctrl_x_mode = CTRL_X_NORMAL;
2403 else
2404 ctrl_x_mode = CTRL_X_FINISHED;
2405 edit_submode = NULL;
2406 }
2407 showmode();
2408 }
2409
2410 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2411 {
2412 // Show error message from attempted keyword completion (probably
2413 // 'Pattern not found') until another key is hit, then go back to
2414 // showing what mode we are in.
2415 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002416 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002417 && c != Ctrl_R && !ins_compl_pum_key(c))
2418 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002419 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002420 }
2421 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2422 // Trigger the CompleteDone event to give scripts a chance to act
2423 // upon the (possibly failed) completion.
2424 ins_apply_autocmds(EVENT_COMPLETEDONE);
2425
LemonBoy2bf52dd2022-04-09 18:17:34 +01002426 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002427
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002428 // reset continue_* if we left expansion-mode, if we stay they'll be
2429 // (re)set properly in ins_complete()
2430 if (!vim_is_ctrl_x_key(c))
2431 {
2432 compl_cont_status = 0;
2433 compl_cont_mode = 0;
2434 }
2435
2436 return retval;
2437}
2438
2439/*
2440 * Fix the redo buffer for the completion leader replacing some of the typed
2441 * text. This inserts backspaces and appends the changed text.
2442 * "ptr" is the known leader text or NUL.
2443 */
2444 static void
2445ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2446{
2447 int len;
2448 char_u *p;
2449 char_u *ptr = ptr_arg;
2450
2451 if (ptr == NULL)
2452 {
2453 if (compl_leader != NULL)
2454 ptr = compl_leader;
2455 else
2456 return; // nothing to do
2457 }
2458 if (compl_orig_text != NULL)
2459 {
2460 p = compl_orig_text;
2461 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2462 ;
2463 if (len > 0)
2464 len -= (*mb_head_off)(p, p + len);
2465 for (p += len; *p != NUL; MB_PTR_ADV(p))
2466 AppendCharToRedobuff(K_BS);
2467 }
2468 else
2469 len = 0;
2470 if (ptr != NULL)
2471 AppendToRedobuffLit(ptr + len, -1);
2472}
2473
2474/*
2475 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2476 * (depending on flag) starting from buf and looking for a non-scanned
2477 * buffer (other than curbuf). curbuf is special, if it is called with
2478 * buf=curbuf then it has to be the first call for a given flag/expansion.
2479 *
2480 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2481 */
2482 static buf_T *
2483ins_compl_next_buf(buf_T *buf, int flag)
2484{
2485 static win_T *wp = NULL;
2486
2487 if (flag == 'w') // just windows
2488 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002489 if (buf == curbuf || !win_valid(wp))
2490 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002491 wp = curwin;
2492 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2493 && wp->w_buffer->b_scanned)
2494 ;
2495 buf = wp->w_buffer;
2496 }
2497 else
2498 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2499 // (unlisted buffers)
2500 // When completing whole lines skip unloaded buffers.
2501 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2502 && ((flag == 'U'
2503 ? buf->b_p_bl
2504 : (!buf->b_p_bl
2505 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2506 || buf->b_scanned))
2507 ;
2508 return buf;
2509}
2510
2511#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002512
2513# ifdef FEAT_EVAL
2514static callback_T cfu_cb; // 'completefunc' callback function
2515static callback_T ofu_cb; // 'omnifunc' callback function
2516static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2517# endif
2518
2519/*
2520 * Copy a global callback function to a buffer local callback.
2521 */
2522 static void
2523copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2524{
2525 free_callback(bufcb);
2526 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2527 copy_callback(bufcb, globcb);
2528}
2529
2530/*
2531 * Parse the 'completefunc' option value and set the callback function.
2532 * Invoked when the 'completefunc' 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_completefunc_option(void)
2538{
2539 int retval;
2540
2541 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2542 if (retval == OK)
2543 set_buflocal_cfu_callback(curbuf);
2544
2545 return retval;
2546}
2547
2548/*
2549 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002550 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002551 */
2552 void
2553set_buflocal_cfu_callback(buf_T *buf UNUSED)
2554{
2555# ifdef FEAT_EVAL
2556 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2557# endif
2558}
2559
2560/*
2561 * Parse the 'omnifunc' option value and set the callback function.
2562 * Invoked when the 'omnifunc' option is set. The option value can be a
2563 * name of a function (string), or function(<name>) or funcref(<name>) or a
2564 * lambda expression.
2565 */
2566 int
2567set_omnifunc_option(void)
2568{
2569 int retval;
2570
2571 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2572 if (retval == OK)
2573 set_buflocal_ofu_callback(curbuf);
2574
2575 return retval;
2576}
2577
2578/*
2579 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002580 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002581 */
2582 void
2583set_buflocal_ofu_callback(buf_T *buf UNUSED)
2584{
2585# ifdef FEAT_EVAL
2586 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2587# endif
2588}
2589
2590/*
2591 * Parse the 'thesaurusfunc' option value and set the callback function.
2592 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2593 * name of a function (string), or function(<name>) or funcref(<name>) or a
2594 * lambda expression.
2595 */
2596 int
2597set_thesaurusfunc_option(void)
2598{
2599 int retval;
2600
2601 if (*curbuf->b_p_tsrfu != NUL)
2602 {
2603 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002604 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2605 &curbuf->b_tsrfu_cb);
2606 }
2607 else
2608 {
2609 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002610 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2611 }
2612
2613 return retval;
2614}
2615
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002616/*
2617 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002618 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002619 */
2620 int
2621set_ref_in_insexpand_funcs(int copyID)
2622{
2623 int abort = FALSE;
2624
2625 abort = set_ref_in_callback(&cfu_cb, copyID);
2626 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2627 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2628
2629 return abort;
2630}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002631
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002632/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002633 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002634 */
2635 static char_u *
2636get_complete_funcname(int type)
2637{
2638 switch (type)
2639 {
2640 case CTRL_X_FUNCTION:
2641 return curbuf->b_p_cfu;
2642 case CTRL_X_OMNI:
2643 return curbuf->b_p_ofu;
2644 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002645 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002646 default:
2647 return (char_u *)"";
2648 }
2649}
2650
2651/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002652 * Get the callback to use for insert mode completion.
2653 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002654 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002655get_insert_callback(int type)
2656{
2657 if (type == CTRL_X_FUNCTION)
2658 return &curbuf->b_cfu_cb;
2659 if (type == CTRL_X_OMNI)
2660 return &curbuf->b_ofu_cb;
2661 // CTRL_X_THESAURUS
2662 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2663}
2664
2665/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002666 * Execute user defined complete function 'completefunc', 'omnifunc' or
2667 * 'thesaurusfunc', and get matches in "matches".
2668 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002669 */
2670 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002671expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002672{
2673 list_T *matchlist = NULL;
2674 dict_T *matchdict = NULL;
2675 typval_T args[3];
2676 char_u *funcname;
2677 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002678 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002679 typval_T rettv;
2680 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002681 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002682
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002683 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002684 if (*funcname == NUL)
2685 return;
2686
2687 // Call 'completefunc' to obtain the list of matches.
2688 args[0].v_type = VAR_NUMBER;
2689 args[0].vval.v_number = 0;
2690 args[1].v_type = VAR_STRING;
2691 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2692 args[2].v_type = VAR_UNKNOWN;
2693
2694 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002695 // Lock the text to avoid weird things from happening. Also disallow
2696 // switching to another window, it should not be needed and may end up in
2697 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002698 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002699
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002700 cb = get_insert_callback(type);
2701 retval = call_callback(cb, 0, &rettv, 2, args);
2702
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002703 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002704 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002705 {
2706 switch (rettv.v_type)
2707 {
2708 case VAR_LIST:
2709 matchlist = rettv.vval.v_list;
2710 break;
2711 case VAR_DICT:
2712 matchdict = rettv.vval.v_dict;
2713 break;
2714 case VAR_SPECIAL:
2715 if (rettv.vval.v_number == VVAL_NONE)
2716 compl_opt_suppress_empty = TRUE;
2717 // FALLTHROUGH
2718 default:
2719 // TODO: Give error message?
2720 clear_tv(&rettv);
2721 break;
2722 }
2723 }
zeertzjqcfe45652022-05-27 17:26:55 +01002724 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002725
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002726 curwin->w_cursor = pos; // restore the cursor position
2727 validate_cursor();
2728 if (!EQUAL_POS(curwin->w_cursor, pos))
2729 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002730 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002731 goto theend;
2732 }
2733
2734 if (matchlist != NULL)
2735 ins_compl_add_list(matchlist);
2736 else if (matchdict != NULL)
2737 ins_compl_add_dict(matchdict);
2738
2739theend:
2740 // Restore State, it might have been changed.
2741 State = save_State;
2742
2743 if (matchdict != NULL)
2744 dict_unref(matchdict);
2745 if (matchlist != NULL)
2746 list_unref(matchlist);
2747}
2748#endif // FEAT_COMPL_FUNC
2749
2750#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2751/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002752 * Add a match to the list of matches from a typeval_T.
2753 * If the given string is already in the list of completions, then return
2754 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2755 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002756 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002757 */
2758 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002759ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002760{
2761 char_u *word;
2762 int dup = FALSE;
2763 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002764 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002765 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002766 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002767 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002768
Bram Moolenaar08928322020-01-04 14:32:48 +01002769 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002770 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2771 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002772 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2773 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2774 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2775 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2776 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2777 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2778 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2779 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002780 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002781 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2782 dup = dict_get_number(tv->vval.v_dict, "dup");
2783 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2784 empty = dict_get_number(tv->vval.v_dict, "empty");
2785 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2786 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002787 flags |= CP_EQUAL;
2788 }
2789 else
2790 {
2791 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002792 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002793 }
2794 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002795 {
2796 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002797 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002798 }
2799 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2800 if (status != OK)
2801 clear_tv(&user_data);
2802 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002803}
2804
2805/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002806 * Add completions from a list.
2807 */
2808 static void
2809ins_compl_add_list(list_T *list)
2810{
2811 listitem_T *li;
2812 int dir = compl_direction;
2813
2814 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002815 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002816 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002817 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002818 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002819 // if dir was BACKWARD then honor it just once
2820 dir = FORWARD;
2821 else if (did_emsg)
2822 break;
2823 }
2824}
2825
2826/*
2827 * Add completions from a dict.
2828 */
2829 static void
2830ins_compl_add_dict(dict_T *dict)
2831{
2832 dictitem_T *di_refresh;
2833 dictitem_T *di_words;
2834
2835 // Check for optional "refresh" item.
2836 compl_opt_refresh_always = FALSE;
2837 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2838 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2839 {
2840 char_u *v = di_refresh->di_tv.vval.v_string;
2841
2842 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2843 compl_opt_refresh_always = TRUE;
2844 }
2845
2846 // Add completions from a "words" list.
2847 di_words = dict_find(dict, (char_u *)"words", 5);
2848 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2849 ins_compl_add_list(di_words->di_tv.vval.v_list);
2850}
2851
2852/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002853 * Start completion for the complete() function.
2854 * "startcol" is where the matched text starts (1 is first column).
2855 * "list" is the list of matches.
2856 */
2857 static void
2858set_completion(colnr_T startcol, list_T *list)
2859{
2860 int save_w_wrow = curwin->w_wrow;
2861 int save_w_leftcol = curwin->w_leftcol;
2862 int flags = CP_ORIGINAL_TEXT;
2863
2864 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002865 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002866 ins_compl_prep(' ');
2867 ins_compl_clear();
2868 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002869 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002870
2871 compl_direction = FORWARD;
2872 if (startcol > curwin->w_cursor.col)
2873 startcol = curwin->w_cursor.col;
2874 compl_col = startcol;
2875 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2876 // compl_pattern doesn't need to be set
2877 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2878 if (p_ic)
2879 flags |= CP_ICASE;
2880 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002881 -1, NULL, NULL, NULL, 0,
2882 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002883 return;
2884
2885 ctrl_x_mode = CTRL_X_EVAL;
2886
2887 ins_compl_add_list(list);
2888 compl_matches = ins_compl_make_cyclic();
2889 compl_started = TRUE;
2890 compl_used_match = TRUE;
2891 compl_cont_status = 0;
2892
2893 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002894 int no_select = compl_no_select || compl_longest;
2895 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002896 {
2897 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002898 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002899 // Down/Up has no real effect.
2900 ins_complete(K_UP, FALSE);
2901 }
2902 else
2903 ins_complete(Ctrl_N, FALSE);
2904 compl_enter_selects = compl_no_insert;
2905
2906 // Lazily show the popup menu, unless we got interrupted.
2907 if (!compl_interrupted)
2908 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002909 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002910 out_flush();
2911}
2912
2913/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002914 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002915 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002916 void
2917f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002918{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002919 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002920
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002921 if (in_vim9script()
2922 && (check_for_number_arg(argvars, 0) == FAIL
2923 || check_for_list_arg(argvars, 1) == FAIL))
2924 return;
2925
Bram Moolenaar24959102022-05-07 20:01:16 +01002926 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002927 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002928 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002929 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002930 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002931
2932 // Check for undo allowed here, because if something was already inserted
2933 // the line was already saved for undo and this check isn't done.
2934 if (!undo_allowed())
2935 return;
2936
Bram Moolenaard83392a2022-09-01 12:22:46 +01002937 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002938 {
2939 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2940 if (startcol > 0)
2941 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002942 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002943}
2944
2945/*
2946 * "complete_add()" function
2947 */
2948 void
2949f_complete_add(typval_T *argvars, typval_T *rettv)
2950{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002951 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002952 return;
2953
Bram Moolenaar440cf092021-04-03 20:13:30 +02002954 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002955}
2956
2957/*
2958 * "complete_check()" function
2959 */
2960 void
2961f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2962{
2963 int saved = RedrawingDisabled;
2964
2965 RedrawingDisabled = 0;
2966 ins_compl_check_keys(0, TRUE);
2967 rettv->vval.v_number = ins_compl_interrupted();
2968 RedrawingDisabled = saved;
2969}
2970
2971/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002972 * Return Insert completion mode name string
2973 */
2974 static char_u *
2975ins_compl_mode(void)
2976{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002977 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002978 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2979
2980 return (char_u *)"";
2981}
2982
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002983/*
2984 * Assign the sequence number to all the completion matches which don't have
2985 * one assigned yet.
2986 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002987 static void
2988ins_compl_update_sequence_numbers()
2989{
2990 int number = 0;
2991 compl_T *match;
2992
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002993 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002994 {
2995 // search backwards for the first valid (!= -1) number.
2996 // This should normally succeed already at the first loop
2997 // cycle, so it's fast!
2998 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002999 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003000 if (match->cp_number != -1)
3001 {
3002 number = match->cp_number;
3003 break;
3004 }
3005 if (match != NULL)
3006 // go up and assign all numbers which are not assigned
3007 // yet
3008 for (match = match->cp_next;
3009 match != NULL && match->cp_number == -1;
3010 match = match->cp_next)
3011 match->cp_number = ++number;
3012 }
3013 else // BACKWARD
3014 {
3015 // search forwards (upwards) for the first valid (!= -1)
3016 // number. This should normally succeed already at the
3017 // first loop cycle, so it's fast!
3018 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003019 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003020 if (match->cp_number != -1)
3021 {
3022 number = match->cp_number;
3023 break;
3024 }
3025 if (match != NULL)
3026 // go down and assign all numbers which are not
3027 // assigned yet
3028 for (match = match->cp_prev; match
3029 && match->cp_number == -1;
3030 match = match->cp_prev)
3031 match->cp_number = ++number;
3032 }
3033}
3034
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003035/*
3036 * Get complete information
3037 */
3038 static void
3039get_complete_info(list_T *what_list, dict_T *retdict)
3040{
3041 int ret = OK;
3042 listitem_T *item;
3043#define CI_WHAT_MODE 0x01
3044#define CI_WHAT_PUM_VISIBLE 0x02
3045#define CI_WHAT_ITEMS 0x04
3046#define CI_WHAT_SELECTED 0x08
3047#define CI_WHAT_INSERTED 0x10
3048#define CI_WHAT_ALL 0xff
3049 int what_flag;
3050
3051 if (what_list == NULL)
3052 what_flag = CI_WHAT_ALL;
3053 else
3054 {
3055 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003056 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003057 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003058 {
3059 char_u *what = tv_get_string(&item->li_tv);
3060
3061 if (STRCMP(what, "mode") == 0)
3062 what_flag |= CI_WHAT_MODE;
3063 else if (STRCMP(what, "pum_visible") == 0)
3064 what_flag |= CI_WHAT_PUM_VISIBLE;
3065 else if (STRCMP(what, "items") == 0)
3066 what_flag |= CI_WHAT_ITEMS;
3067 else if (STRCMP(what, "selected") == 0)
3068 what_flag |= CI_WHAT_SELECTED;
3069 else if (STRCMP(what, "inserted") == 0)
3070 what_flag |= CI_WHAT_INSERTED;
3071 }
3072 }
3073
3074 if (ret == OK && (what_flag & CI_WHAT_MODE))
3075 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3076
3077 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3078 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3079
3080 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3081 {
3082 list_T *li;
3083 dict_T *di;
3084 compl_T *match;
3085
3086 li = list_alloc();
3087 if (li == NULL)
3088 return;
3089 ret = dict_add_list(retdict, "items", li);
3090 if (ret == OK && compl_first_match != NULL)
3091 {
3092 match = compl_first_match;
3093 do
3094 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003095 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003096 {
3097 di = dict_alloc();
3098 if (di == NULL)
3099 return;
3100 ret = list_append_dict(li, di);
3101 if (ret != OK)
3102 return;
3103 dict_add_string(di, "word", match->cp_str);
3104 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3105 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3106 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3107 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003108 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3109 // Add an empty string for backwards compatibility
3110 dict_add_string(di, "user_data", (char_u *)"");
3111 else
3112 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003113 }
3114 match = match->cp_next;
3115 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003116 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003117 }
3118 }
3119
3120 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003121 {
3122 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3123 ins_compl_update_sequence_numbers();
3124 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3125 ? compl_curr_match->cp_number - 1 : -1);
3126 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003127
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003128 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3129 {
3130 // TODO
3131 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003132}
3133
3134/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003135 * "complete_info()" function
3136 */
3137 void
3138f_complete_info(typval_T *argvars, typval_T *rettv)
3139{
3140 list_T *what_list = NULL;
3141
Bram Moolenaar93a10962022-06-16 11:42:09 +01003142 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003143 return;
3144
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003145 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3146 return;
3147
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003148 if (argvars[0].v_type != VAR_UNKNOWN)
3149 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003150 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003151 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003152 what_list = argvars[0].vval.v_list;
3153 }
3154 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003155}
3156#endif
3157
3158/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003159 * Returns TRUE when using a user-defined function for thesaurus completion.
3160 */
3161 static int
3162thesaurus_func_complete(int type UNUSED)
3163{
3164#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003165 return type == CTRL_X_THESAURUS
3166 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003167#else
3168 return FALSE;
3169#endif
3170}
3171
3172/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003173 * Return value of process_next_cpt_value()
3174 */
3175enum
3176{
3177 INS_COMPL_CPT_OK = 1,
3178 INS_COMPL_CPT_CONT,
3179 INS_COMPL_CPT_END
3180};
3181
3182/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003183 * state information used for getting the next set of insert completion
3184 * matches.
3185 */
3186typedef struct
3187{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003188 char_u *e_cpt_copy; // copy of 'complete'
3189 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003190 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003191 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003192 pos_T prev_match_pos; // previous match position
3193 int set_match_pos; // save first_match_pos/last_match_pos
3194 pos_T first_match_pos; // first match position
3195 pos_T last_match_pos; // last match position
3196 int found_all; // found all matches of a certain type.
3197 char_u *dict; // dictionary file to search
3198 int dict_f; // "dict" is an exact file name or not
3199} ins_compl_next_state_T;
3200
3201/*
3202 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003203 *
3204 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003205 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003206 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003207 * st->found_all - all matches of this type are found
3208 * st->ins_buf - search for completions in this buffer
3209 * st->first_match_pos - position of the first completion match
3210 * st->last_match_pos - position of the last completion match
3211 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003212 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003213 * st->dict - name of the dictionary or thesaurus file to search
3214 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003215 *
3216 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003217 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3218 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003219 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003220 */
3221 static int
3222process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003223 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003224 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003225 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003226{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003227 int compl_type = -1;
3228 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003229
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003230 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003231
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003232 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3233 st->e_cpt++;
3234
3235 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003236 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003237 st->ins_buf = curbuf;
3238 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003239 // Move the cursor back one character so that ^N can match the
3240 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003241 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003242 {
3243 // Move the cursor to after the last character in the
3244 // buffer, so that word at start of buffer is found
3245 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003246 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3247 st->first_match_pos.col =
3248 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003250 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003251 compl_type = 0;
3252
3253 // Remember the first match so that the loop stops when we
3254 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003255 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003256 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003257 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003258 && (st->ins_buf = ins_compl_next_buf(
3259 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003260 {
3261 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003262 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 {
3264 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003265 st->first_match_pos.col = st->last_match_pos.col = 0;
3266 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3267 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003268 compl_type = 0;
3269 }
3270 else // unloaded buffer, scan like dictionary
3271 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003272 st->found_all = TRUE;
3273 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 {
3275 status = INS_COMPL_CPT_CONT;
3276 goto done;
3277 }
3278 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003279 st->dict = st->ins_buf->b_fname;
3280 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003281 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003282 if (!shortmess(SHM_COMPLETIONSCAN))
3283 {
3284 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3285 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3286 st->ins_buf->b_fname == NULL
3287 ? buf_spname(st->ins_buf)
3288 : st->ins_buf->b_sfname == NULL
3289 ? st->ins_buf->b_fname
3290 : st->ins_buf->b_sfname);
3291 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3292 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003293 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003294 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003295 status = INS_COMPL_CPT_END;
3296 else
3297 {
3298 if (ctrl_x_mode_line_or_eval())
3299 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003300 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003301 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003302 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003303 compl_type = CTRL_X_DICTIONARY;
3304 else
3305 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003306 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003307 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003308 st->dict = st->e_cpt;
3309 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003310 }
3311 }
3312#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003313 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003314 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003315 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316 compl_type = CTRL_X_PATH_DEFINES;
3317#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003318 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003319 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003321 if (!shortmess(SHM_COMPLETIONSCAN))
3322 {
3323 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3324 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3325 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3326 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003327 }
3328 else
3329 compl_type = -1;
3330
3331 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003332 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003333
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003334 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003335 if (compl_type == -1)
3336 status = INS_COMPL_CPT_CONT;
3337 }
3338
3339done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003341 return status;
3342}
3343
3344#ifdef FEAT_FIND_ID
3345/*
3346 * Get the next set of identifiers or defines matching "compl_pattern" in
3347 * included files.
3348 */
3349 static void
3350get_next_include_file_completion(int compl_type)
3351{
3352 find_pattern_in_path(compl_pattern, compl_direction,
3353 (int)STRLEN(compl_pattern), FALSE, FALSE,
3354 (compl_type == CTRL_X_PATH_DEFINES
3355 && !(compl_cont_status & CONT_SOL))
3356 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3357 (linenr_T)1, (linenr_T)MAXLNUM);
3358}
3359#endif
3360
3361/*
3362 * Get the next set of words matching "compl_pattern" in dictionary or
3363 * thesaurus files.
3364 */
3365 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003366get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003367{
3368#ifdef FEAT_COMPL_FUNC
3369 if (thesaurus_func_complete(compl_type))
3370 expand_by_function(compl_type, compl_pattern);
3371 else
3372#endif
3373 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003374 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003375 : (compl_type == CTRL_X_THESAURUS
3376 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3377 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3378 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003379 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003380 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003381}
3382
3383/*
3384 * Get the next set of tag names matching "compl_pattern".
3385 */
3386 static void
3387get_next_tag_completion(void)
3388{
3389 int save_p_ic;
3390 char_u **matches;
3391 int num_matches;
3392
3393 // set p_ic according to p_ic, p_scs and pat for find_tags().
3394 save_p_ic = p_ic;
3395 p_ic = ignorecase(compl_pattern);
3396
3397 // Find up to TAG_MANY matches. Avoids that an enormous number
3398 // of matches is found when compl_pattern is empty
3399 g_tag_at_cursor = TRUE;
3400 if (find_tags(compl_pattern, &num_matches, &matches,
3401 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003402 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3404 ins_compl_add_matches(num_matches, matches, p_ic);
3405 g_tag_at_cursor = FALSE;
3406 p_ic = save_p_ic;
3407}
3408
3409/*
3410 * Get the next set of filename matching "compl_pattern".
3411 */
3412 static void
3413get_next_filename_completion(void)
3414{
3415 char_u **matches;
3416 int num_matches;
3417
3418 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3419 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3420 return;
3421
3422 // May change home directory back to "~".
3423 tilde_replace(compl_pattern, num_matches, matches);
3424#ifdef BACKSLASH_IN_FILENAME
3425 if (curbuf->b_p_csl[0] != NUL)
3426 {
3427 int i;
3428
3429 for (i = 0; i < num_matches; ++i)
3430 {
3431 char_u *ptr = matches[i];
3432
3433 while (*ptr != NUL)
3434 {
3435 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3436 *ptr = '/';
3437 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3438 *ptr = '\\';
3439 ptr += (*mb_ptr2len)(ptr);
3440 }
3441 }
3442 }
3443#endif
3444 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3445}
3446
3447/*
3448 * Get the next set of command-line completions matching "compl_pattern".
3449 */
3450 static void
3451get_next_cmdline_completion()
3452{
3453 char_u **matches;
3454 int num_matches;
3455
3456 if (expand_cmdline(&compl_xp, compl_pattern,
3457 (int)STRLEN(compl_pattern),
3458 &num_matches, &matches) == EXPAND_OK)
3459 ins_compl_add_matches(num_matches, matches, FALSE);
3460}
3461
3462/*
3463 * Get the next set of spell suggestions matching "compl_pattern".
3464 */
3465 static void
3466get_next_spell_completion(linenr_T lnum UNUSED)
3467{
3468#ifdef FEAT_SPELL
3469 char_u **matches;
3470 int num_matches;
3471
3472 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3473 if (num_matches > 0)
3474 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003475 else
3476 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003477#endif
3478}
3479
3480/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003481 * Return the next word or line from buffer "ins_buf" at position
3482 * "cur_match_pos" for completion. The length of the match is set in "len".
3483 */
3484 static char_u *
3485ins_comp_get_next_word_or_line(
3486 buf_T *ins_buf, // buffer being scanned
3487 pos_T *cur_match_pos, // current match position
3488 int *match_len,
3489 int *cont_s_ipos) // next ^X<> will set initial_pos
3490{
3491 char_u *ptr;
3492 int len;
3493
3494 *match_len = 0;
3495 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3496 cur_match_pos->col;
3497 if (ctrl_x_mode_line_or_eval())
3498 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003499 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003500 {
3501 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3502 return NULL;
3503 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3504 if (!p_paste)
3505 ptr = skipwhite(ptr);
3506 }
3507 len = (int)STRLEN(ptr);
3508 }
3509 else
3510 {
3511 char_u *tmp_ptr = ptr;
3512
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003513 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003514 {
3515 tmp_ptr += compl_length;
3516 // Skip if already inside a word.
3517 if (vim_iswordp(tmp_ptr))
3518 return NULL;
3519 // Find start of next word.
3520 tmp_ptr = find_word_start(tmp_ptr);
3521 }
3522 // Find end of this word.
3523 tmp_ptr = find_word_end(tmp_ptr);
3524 len = (int)(tmp_ptr - ptr);
3525
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003526 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003527 {
3528 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3529 {
3530 // Try next line, if any. the new word will be
3531 // "join" as if the normal command "J" was used.
3532 // IOSIZE is always greater than
3533 // compl_length, so the next STRNCPY always
3534 // works -- Acevedo
3535 STRNCPY(IObuff, ptr, len);
3536 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3537 tmp_ptr = ptr = skipwhite(ptr);
3538 // Find start of next word.
3539 tmp_ptr = find_word_start(tmp_ptr);
3540 // Find end of next word.
3541 tmp_ptr = find_word_end(tmp_ptr);
3542 if (tmp_ptr > ptr)
3543 {
3544 if (*ptr != ')' && IObuff[len - 1] != TAB)
3545 {
3546 if (IObuff[len - 1] != ' ')
3547 IObuff[len++] = ' ';
3548 // IObuf =~ "\k.* ", thus len >= 2
3549 if (p_js
3550 && (IObuff[len - 2] == '.'
3551 || (vim_strchr(p_cpo, CPO_JOINSP)
3552 == NULL
3553 && (IObuff[len - 2] == '?'
3554 || IObuff[len - 2] == '!'))))
3555 IObuff[len++] = ' ';
3556 }
3557 // copy as much as possible of the new word
3558 if (tmp_ptr - ptr >= IOSIZE - len)
3559 tmp_ptr = ptr + IOSIZE - len - 1;
3560 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3561 len += (int)(tmp_ptr - ptr);
3562 *cont_s_ipos = TRUE;
3563 }
3564 IObuff[len] = NUL;
3565 ptr = IObuff;
3566 }
3567 if (len == compl_length)
3568 return NULL;
3569 }
3570 }
3571
3572 *match_len = len;
3573 return ptr;
3574}
3575
3576/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003577 * Get the next set of words matching "compl_pattern" for default completion(s)
3578 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003579 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3580 * position "st->start_pos" in the "compl_direction" direction. If
3581 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3582 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003583 * Returns OK if a new next match is found, otherwise returns FAIL.
3584 */
3585 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003586get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003587{
3588 int found_new_match = FAIL;
3589 int save_p_scs;
3590 int save_p_ws;
3591 int looped_around = FALSE;
3592 char_u *ptr;
3593 int len;
3594
3595 // If 'infercase' is set, don't use 'smartcase' here
3596 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003597 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003598 p_scs = FALSE;
3599
3600 // Buffers other than curbuf are scanned from the beginning or the
3601 // end but never from the middle, thus setting nowrapscan in this
3602 // buffer is a good idea, on the other hand, we always set
3603 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3604 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003605 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003607 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003608 p_ws = TRUE;
3609 looped_around = FALSE;
3610 for (;;)
3611 {
3612 int cont_s_ipos = FALSE;
3613
3614 ++msg_silent; // Don't want messages for wrapscan.
3615
3616 // ctrl_x_mode_line_or_eval() || word-wise search that
3617 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003618 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003619 found_new_match = search_for_exact_line(st->ins_buf,
3620 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003621 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003622 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3623 NULL, compl_direction, compl_pattern, 1L,
3624 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003625 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003626 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003627 {
3628 // set "compl_started" even on fail
3629 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003630 st->first_match_pos = *st->cur_match_pos;
3631 st->last_match_pos = *st->cur_match_pos;
3632 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003633 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003634 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3635 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003636 {
3637 found_new_match = FAIL;
3638 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003639 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003640 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3641 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3642 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003643 {
3644 if (looped_around)
3645 found_new_match = FAIL;
3646 else
3647 looped_around = TRUE;
3648 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003649 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003650 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3651 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3652 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 {
3654 if (looped_around)
3655 found_new_match = FAIL;
3656 else
3657 looped_around = TRUE;
3658 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003659 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003660 if (found_new_match == FAIL)
3661 break;
3662
3663 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003664 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003665 && start_pos->lnum == st->cur_match_pos->lnum
3666 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003667 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003669 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3670 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003671 if (ptr == NULL)
3672 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003673
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003674 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003675 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 0, cont_s_ipos) != NOTDONE)
3677 {
3678 found_new_match = OK;
3679 break;
3680 }
3681 }
3682 p_scs = save_p_scs;
3683 p_ws = save_p_ws;
3684
3685 return found_new_match;
3686}
3687
3688/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003689 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003690 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003691 */
3692 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003693get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003694{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003695 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003696
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003697 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003698 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003699 case -1:
3700 break;
3701#ifdef FEAT_FIND_ID
3702 case CTRL_X_PATH_PATTERNS:
3703 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003704 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003705 break;
3706#endif
3707
3708 case CTRL_X_DICTIONARY:
3709 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003710 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3711 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003712 break;
3713
3714 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003715 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003716 break;
3717
3718 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003719 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003720 break;
3721
3722 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003723 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003724 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003725 break;
3726
3727#ifdef FEAT_COMPL_FUNC
3728 case CTRL_X_FUNCTION:
3729 case CTRL_X_OMNI:
3730 expand_by_function(type, compl_pattern);
3731 break;
3732#endif
3733
3734 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003735 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003736 break;
3737
3738 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003739 found_new_match = get_next_default_completion(st, ini);
3740 if (found_new_match == FAIL && st->ins_buf == curbuf)
3741 st->found_all = TRUE;
3742 }
3743
3744 // check if compl_curr_match has changed, (e.g. other type of
3745 // expansion added something)
3746 if (type != 0 && compl_curr_match != compl_old_match)
3747 found_new_match = OK;
3748
3749 return found_new_match;
3750}
3751
3752/*
3753 * Get the next expansion(s), using "compl_pattern".
3754 * The search starts at position "ini" in curbuf and in the direction
3755 * compl_direction.
3756 * When "compl_started" is FALSE start at that position, otherwise continue
3757 * where we stopped searching before.
3758 * This may return before finding all the matches.
3759 * Return the total number of matches or -1 if still unknown -- Acevedo
3760 */
3761 static int
3762ins_compl_get_exp(pos_T *ini)
3763{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003764 static ins_compl_next_state_T st;
3765 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003766 int i;
3767 int found_new_match;
3768 int type = ctrl_x_mode;
3769
3770 if (!compl_started)
3771 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003772 buf_T *buf;
3773
3774 FOR_ALL_BUFFERS(buf)
3775 buf->b_scanned = 0;
3776 if (!st_cleared)
3777 {
3778 CLEAR_FIELD(st);
3779 st_cleared = TRUE;
3780 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003781 st.found_all = FALSE;
3782 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003783 vim_free(st.e_cpt_copy);
3784 // Make a copy of 'complete', if case the buffer is wiped out.
3785 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3786 ? (char_u *)"." : curbuf->b_p_cpt);
3787 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003788 st.last_match_pos = st.first_match_pos = *ini;
3789 }
3790 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3791 st.ins_buf = curbuf; // In case the buffer was wiped out.
3792
3793 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003794 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003795 ? &st.last_match_pos : &st.first_match_pos;
3796
3797 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3798 for (;;)
3799 {
3800 found_new_match = FAIL;
3801 st.set_match_pos = FALSE;
3802
3803 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3804 // or if found_all says this entry is done. For ^X^L only use the
3805 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003806 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003807 && (!compl_started || st.found_all))
3808 {
3809 int status = process_next_cpt_value(&st, &type, ini);
3810
3811 if (status == INS_COMPL_CPT_END)
3812 break;
3813 if (status == INS_COMPL_CPT_CONT)
3814 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003815 }
3816
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003817 // If complete() was called then compl_pattern has been reset. The
3818 // following won't work then, bail out.
3819 if (compl_pattern == NULL)
3820 break;
3821
3822 // get the next set of completion matches
3823 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003824
3825 // break the loop for specialized modes (use 'complete' just for the
3826 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3827 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003828 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3829 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003830 {
3831 if (got_int)
3832 break;
3833 // Fill the popup menu as soon as possible.
3834 if (type != -1)
3835 ins_compl_check_keys(0, FALSE);
3836
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003837 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003838 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3839 break;
3840 compl_started = TRUE;
3841 }
3842 else
3843 {
3844 // Mark a buffer scanned when it has been scanned completely
3845 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003846 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003847
3848 compl_started = FALSE;
3849 }
3850 }
3851 compl_started = TRUE;
3852
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003853 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003854 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003855 found_new_match = FAIL;
3856
3857 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003858 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003859 && !ctrl_x_mode_line_or_eval()))
3860 i = ins_compl_make_cyclic();
3861
3862 if (compl_old_match != NULL)
3863 {
3864 // If several matches were added (FORWARD) or the search failed and has
3865 // just been made cyclic then we have to move compl_curr_match to the
3866 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003867 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003868 : compl_old_match->cp_prev;
3869 if (compl_curr_match == NULL)
3870 compl_curr_match = compl_old_match;
3871 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003872 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003873
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003874 return i;
3875}
3876
3877/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003878 * Update "compl_shown_match" to the actually shown match, it may differ when
3879 * "compl_leader" is used to omit some of the matches.
3880 */
3881 static void
3882ins_compl_update_shown_match(void)
3883{
3884 while (!ins_compl_equal(compl_shown_match,
3885 compl_leader, (int)STRLEN(compl_leader))
3886 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003887 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003888 compl_shown_match = compl_shown_match->cp_next;
3889
3890 // If we didn't find it searching forward, and compl_shows_dir is
3891 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003892 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003893 && !ins_compl_equal(compl_shown_match,
3894 compl_leader, (int)STRLEN(compl_leader))
3895 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003896 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003897 {
3898 while (!ins_compl_equal(compl_shown_match,
3899 compl_leader, (int)STRLEN(compl_leader))
3900 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003901 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003902 compl_shown_match = compl_shown_match->cp_prev;
3903 }
3904}
3905
3906/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003907 * Delete the old text being completed.
3908 */
3909 void
3910ins_compl_delete(void)
3911{
3912 int col;
3913
3914 // In insert mode: Delete the typed part.
3915 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003916 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003917 if ((int)curwin->w_cursor.col > col)
3918 {
3919 if (stop_arrow() == FAIL)
3920 return;
3921 backspace_until_column(col);
3922 }
3923
3924 // TODO: is this sufficient for redrawing? Redrawing everything causes
3925 // flicker, thus we can't do that.
3926 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003927#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003928 // clear v:completed_item
3929 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003930#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003931}
3932
3933/*
3934 * Insert the new text being completed.
3935 * "in_compl_func" is TRUE when called from complete_check().
3936 */
3937 void
3938ins_compl_insert(int in_compl_func)
3939{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003940 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003941
3942 // Make sure we don't go over the end of the string, this can happen with
3943 // illegal bytes.
3944 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3945 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003946 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 compl_used_match = FALSE;
3948 else
3949 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003950#ifdef FEAT_EVAL
3951 {
3952 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3953
3954 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3955 }
3956#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003957 if (!in_compl_func)
3958 compl_curr_match = compl_shown_match;
3959}
3960
3961/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003962 * show the file name for the completion match (if any). Truncate the file
3963 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003964 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003965 static void
3966ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003967{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003968 char *lead = _("match in file");
3969 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3970 char_u *s;
3971 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003972
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003973 if (space <= 0)
3974 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003975
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003976 // We need the tail that fits. With double-byte encoding going
3977 // back from the end is very slow, thus go from the start and keep
3978 // the text that fits in "space" between "s" and "e".
3979 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003980 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003981 space -= ptr2cells(e);
3982 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003983 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003984 space += ptr2cells(s);
3985 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003986 }
3987 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 msg_hist_off = TRUE;
3989 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3990 s > compl_shown_match->cp_fname ? "<" : "", s);
3991 msg((char *)IObuff);
3992 msg_hist_off = FALSE;
3993 redraw_cmdline = FALSE; // don't overwrite!
3994}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003995
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003996/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003997 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003998 * times. The number of matches found is returned in 'num_matches'.
3999 *
4000 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4001 * get more completions. If it is FALSE, then do nothing when there are no more
4002 * completions in the given direction.
4003 *
4004 * If "advance" is TRUE, then completion will move to the first match.
4005 * Otherwise, the original text will be shown.
4006 *
4007 * Returns OK on success and -1 if the number of matches are unknown.
4008 */
4009 static int
4010find_next_completion_match(
4011 int allow_get_expansion,
4012 int todo, // repeat completion this many times
4013 int advance,
4014 int *num_matches)
4015{
4016 int found_end = FALSE;
4017 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004018
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004019 while (--todo >= 0)
4020 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004021 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004022 {
4023 compl_shown_match = compl_shown_match->cp_next;
4024 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004025 && (is_first_match(compl_shown_match->cp_next)
4026 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004027 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004028 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004029 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004030 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004031 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004032 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004033 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004034 }
4035 else
4036 {
4037 if (!allow_get_expansion)
4038 {
4039 if (advance)
4040 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004041 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 compl_pending -= todo + 1;
4043 else
4044 compl_pending += todo + 1;
4045 }
4046 return -1;
4047 }
4048
4049 if (!compl_no_select && advance)
4050 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004051 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004052 --compl_pending;
4053 else
4054 ++compl_pending;
4055 }
4056
4057 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004058 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004059
4060 // handle any pending completions
4061 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004062 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004063 {
4064 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4065 {
4066 compl_shown_match = compl_shown_match->cp_next;
4067 --compl_pending;
4068 }
4069 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4070 {
4071 compl_shown_match = compl_shown_match->cp_prev;
4072 ++compl_pending;
4073 }
4074 else
4075 break;
4076 }
4077 found_end = FALSE;
4078 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004079 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004080 && compl_leader != NULL
4081 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004082 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004083 ++todo;
4084 else
4085 // Remember a matching item.
4086 found_compl = compl_shown_match;
4087
4088 // Stop at the end of the list when we found a usable match.
4089 if (found_end)
4090 {
4091 if (found_compl != NULL)
4092 {
4093 compl_shown_match = found_compl;
4094 break;
4095 }
4096 todo = 1; // use first usable match after wrapping around
4097 }
4098 }
4099
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004100 return OK;
4101}
4102
4103/*
4104 * Fill in the next completion in the current direction.
4105 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4106 * get more completions. If it is FALSE, then we just do nothing when there
4107 * are no more completions in a given direction. The latter case is used when
4108 * we are still in the middle of finding completions, to allow browsing
4109 * through the ones found so far.
4110 * Return the total number of matches, or -1 if still unknown -- webb.
4111 *
4112 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4113 * compl_shown_match here.
4114 *
4115 * Note that this function may be called recursively once only. First with
4116 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4117 * calls this function with "allow_get_expansion" FALSE.
4118 */
4119 static int
4120ins_compl_next(
4121 int allow_get_expansion,
4122 int count, // repeat completion this many times; should
4123 // be at least 1
4124 int insert_match, // Insert the newly selected match
4125 int in_compl_func) // called from complete_check()
4126{
4127 int num_matches = -1;
4128 int todo = count;
4129 int advance;
4130 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004131 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004132
4133 // When user complete function return -1 for findstart which is next
4134 // time of 'always', compl_shown_match become NULL.
4135 if (compl_shown_match == NULL)
4136 return -1;
4137
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004138 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004139 // Update "compl_shown_match" to the actually shown match
4140 ins_compl_update_shown_match();
4141
4142 if (allow_get_expansion && insert_match
4143 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4144 // Delete old text to be replaced
4145 ins_compl_delete();
4146
4147 // When finding the longest common text we stick at the original text,
4148 // don't let CTRL-N or CTRL-P move to the first match.
4149 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4150
4151 // When restarting the search don't insert the first match either.
4152 if (compl_restarting)
4153 {
4154 advance = FALSE;
4155 compl_restarting = FALSE;
4156 }
4157
4158 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4159 // around.
4160 if (find_next_completion_match(allow_get_expansion, todo, advance,
4161 &num_matches) == -1)
4162 return -1;
4163
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004164 if (curbuf != orig_curbuf)
4165 {
4166 // In case some completion function switched buffer, don't want to
4167 // insert the completion elsewhere.
4168 return -1;
4169 }
4170
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004171 // Insert the text of the new completion, or the compl_leader.
4172 if (compl_no_insert && !started)
4173 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004174 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004175 compl_used_match = FALSE;
4176 }
4177 else if (insert_match)
4178 {
4179 if (!compl_get_longest || compl_used_match)
4180 ins_compl_insert(in_compl_func);
4181 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004182 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004183 }
4184 else
4185 compl_used_match = FALSE;
4186
4187 if (!allow_get_expansion)
4188 {
4189 // may undisplay the popup menu first
4190 ins_compl_upd_pum();
4191
4192 if (pum_enough_matches())
4193 // Will display the popup menu, don't redraw yet to avoid flicker.
4194 pum_call_update_screen();
4195 else
4196 // Not showing the popup menu yet, redraw to show the user what was
4197 // inserted.
4198 update_screen(0);
4199
4200 // display the updated popup menu
4201 ins_compl_show_pum();
4202#ifdef FEAT_GUI
4203 if (gui.in_use)
4204 {
4205 // Show the cursor after the match, not after the redrawn text.
4206 setcursor();
4207 out_flush_cursor(FALSE, FALSE);
4208 }
4209#endif
4210
4211 // Delete old text to be replaced, since we're still searching and
4212 // don't want to match ourselves!
4213 ins_compl_delete();
4214 }
4215
4216 // Enter will select a match when the match wasn't inserted and the popup
4217 // menu is visible.
4218 if (compl_no_insert && !started)
4219 compl_enter_selects = TRUE;
4220 else
4221 compl_enter_selects = !insert_match && compl_match_array != NULL;
4222
4223 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004224 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004225 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004226
4227 return num_matches;
4228}
4229
4230/*
4231 * Call this while finding completions, to check whether the user has hit a key
4232 * that should change the currently displayed completion, or exit completion
4233 * mode. Also, when compl_pending is not zero, show a completion as soon as
4234 * possible. -- webb
4235 * "frequency" specifies out of how many calls we actually check.
4236 * "in_compl_func" is TRUE when called from complete_check(), don't set
4237 * compl_curr_match.
4238 */
4239 void
4240ins_compl_check_keys(int frequency, int in_compl_func)
4241{
4242 static int count = 0;
4243 int c;
4244
4245 // Don't check when reading keys from a script, :normal or feedkeys().
4246 // That would break the test scripts. But do check for keys when called
4247 // from complete_check().
4248 if (!in_compl_func && (using_script() || ex_normal_busy))
4249 return;
4250
4251 // Only do this at regular intervals
4252 if (++count < frequency)
4253 return;
4254 count = 0;
4255
4256 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4257 // can't do its work correctly.
4258 c = vpeekc_any();
4259 if (c != NUL)
4260 {
4261 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4262 {
4263 c = safe_vgetc(); // Eat the character
4264 compl_shows_dir = ins_compl_key2dir(c);
4265 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4266 c != K_UP && c != K_DOWN, in_compl_func);
4267 }
4268 else
4269 {
4270 // Need to get the character to have KeyTyped set. We'll put it
4271 // back with vungetc() below. But skip K_IGNORE.
4272 c = safe_vgetc();
4273 if (c != K_IGNORE)
4274 {
4275 // Don't interrupt completion when the character wasn't typed,
4276 // e.g., when doing @q to replay keys.
4277 if (c != Ctrl_R && KeyTyped)
4278 compl_interrupted = TRUE;
4279
4280 vungetc(c);
4281 }
4282 }
4283 }
4284 if (compl_pending != 0 && !got_int && !compl_no_insert)
4285 {
4286 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4287
4288 compl_pending = 0;
4289 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4290 }
4291}
4292
4293/*
4294 * Decide the direction of Insert mode complete from the key typed.
4295 * Returns BACKWARD or FORWARD.
4296 */
4297 static int
4298ins_compl_key2dir(int c)
4299{
4300 if (c == Ctrl_P || c == Ctrl_L
4301 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4302 return BACKWARD;
4303 return FORWARD;
4304}
4305
4306/*
4307 * Return TRUE for keys that are used for completion only when the popup menu
4308 * is visible.
4309 */
4310 static int
4311ins_compl_pum_key(int c)
4312{
4313 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4314 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4315 || c == K_UP || c == K_DOWN);
4316}
4317
4318/*
4319 * Decide the number of completions to move forward.
4320 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4321 */
4322 static int
4323ins_compl_key2count(int c)
4324{
4325 int h;
4326
4327 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4328 {
4329 h = pum_get_height();
4330 if (h > 3)
4331 h -= 2; // keep some context
4332 return h;
4333 }
4334 return 1;
4335}
4336
4337/*
4338 * Return TRUE if completion with "c" should insert the match, FALSE if only
4339 * to change the currently selected completion.
4340 */
4341 static int
4342ins_compl_use_match(int c)
4343{
4344 switch (c)
4345 {
4346 case K_UP:
4347 case K_DOWN:
4348 case K_PAGEDOWN:
4349 case K_KPAGEDOWN:
4350 case K_S_DOWN:
4351 case K_PAGEUP:
4352 case K_KPAGEUP:
4353 case K_S_UP:
4354 return FALSE;
4355 }
4356 return TRUE;
4357}
4358
4359/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004360 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4361 * completion)
4362 * Sets the global variables: compl_col, compl_length and compl_pattern.
4363 * Uses the global variables: compl_cont_status and ctrl_x_mode
4364 */
4365 static int
4366get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4367{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004368 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004369 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004370 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004371 {
4372 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4373 ;
4374 compl_col += ++startcol;
4375 compl_length = curs_col - startcol;
4376 }
4377 if (p_ic)
4378 compl_pattern = str_foldcase(line + compl_col,
4379 compl_length, NULL, 0);
4380 else
4381 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4382 if (compl_pattern == NULL)
4383 return FAIL;
4384 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004385 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004386 {
4387 char_u *prefix = (char_u *)"\\<";
4388
4389 // we need up to 2 extra chars for the prefix
4390 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4391 compl_length) + 2);
4392 if (compl_pattern == NULL)
4393 return FAIL;
4394 if (!vim_iswordp(line + compl_col)
4395 || (compl_col > 0
4396 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4397 prefix = (char_u *)"";
4398 STRCPY((char *)compl_pattern, prefix);
4399 (void)quote_meta(compl_pattern + STRLEN(prefix),
4400 line + compl_col, compl_length);
4401 }
4402 else if (--startcol < 0
4403 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4404 {
4405 // Match any word of at least two chars
4406 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4407 if (compl_pattern == NULL)
4408 return FAIL;
4409 compl_col += curs_col;
4410 compl_length = 0;
4411 }
4412 else
4413 {
4414 // Search the point of change class of multibyte character
4415 // or not a word single byte character backward.
4416 if (has_mbyte)
4417 {
4418 int base_class;
4419 int head_off;
4420
4421 startcol -= (*mb_head_off)(line, line + startcol);
4422 base_class = mb_get_class(line + startcol);
4423 while (--startcol >= 0)
4424 {
4425 head_off = (*mb_head_off)(line, line + startcol);
4426 if (base_class != mb_get_class(line + startcol
4427 - head_off))
4428 break;
4429 startcol -= head_off;
4430 }
4431 }
4432 else
4433 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4434 ;
4435 compl_col += ++startcol;
4436 compl_length = (int)curs_col - startcol;
4437 if (compl_length == 1)
4438 {
4439 // Only match word with at least two chars -- webb
4440 // there's no need to call quote_meta,
4441 // alloc(7) is enough -- Acevedo
4442 compl_pattern = alloc(7);
4443 if (compl_pattern == NULL)
4444 return FAIL;
4445 STRCPY((char *)compl_pattern, "\\<");
4446 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4447 STRCAT((char *)compl_pattern, "\\k");
4448 }
4449 else
4450 {
4451 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4452 compl_length) + 2);
4453 if (compl_pattern == NULL)
4454 return FAIL;
4455 STRCPY((char *)compl_pattern, "\\<");
4456 (void)quote_meta(compl_pattern + 2, line + compl_col,
4457 compl_length);
4458 }
4459 }
4460
4461 return OK;
4462}
4463
4464/*
4465 * Get the pattern, column and length for whole line completion or for the
4466 * complete() function.
4467 * Sets the global variables: compl_col, compl_length and compl_pattern.
4468 */
4469 static int
4470get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4471{
4472 compl_col = (colnr_T)getwhitecols(line);
4473 compl_length = (int)curs_col - (int)compl_col;
4474 if (compl_length < 0) // cursor in indent: empty pattern
4475 compl_length = 0;
4476 if (p_ic)
4477 compl_pattern = str_foldcase(line + compl_col, compl_length,
4478 NULL, 0);
4479 else
4480 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4481 if (compl_pattern == NULL)
4482 return FAIL;
4483
4484 return OK;
4485}
4486
4487/*
4488 * Get the pattern, column and length for filename completion.
4489 * Sets the global variables: compl_col, compl_length and compl_pattern.
4490 */
4491 static int
4492get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4493{
4494 // Go back to just before the first filename character.
4495 if (startcol > 0)
4496 {
4497 char_u *p = line + startcol;
4498
4499 MB_PTR_BACK(line, p);
4500 while (p > line && vim_isfilec(PTR2CHAR(p)))
4501 MB_PTR_BACK(line, p);
4502 if (p == line && vim_isfilec(PTR2CHAR(p)))
4503 startcol = 0;
4504 else
4505 startcol = (int)(p - line) + 1;
4506 }
4507
4508 compl_col += startcol;
4509 compl_length = (int)curs_col - startcol;
4510 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4511 if (compl_pattern == NULL)
4512 return FAIL;
4513
4514 return OK;
4515}
4516
4517/*
4518 * Get the pattern, column and length for command-line completion.
4519 * Sets the global variables: compl_col, compl_length and compl_pattern.
4520 */
4521 static int
4522get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4523{
4524 compl_pattern = vim_strnsave(line, curs_col);
4525 if (compl_pattern == NULL)
4526 return FAIL;
4527 set_cmd_context(&compl_xp, compl_pattern,
4528 (int)STRLEN(compl_pattern), curs_col, FALSE);
4529 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4530 || compl_xp.xp_context == EXPAND_NOTHING)
4531 // No completion possible, use an empty pattern to get a
4532 // "pattern not found" message.
4533 compl_col = curs_col;
4534 else
4535 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4536 compl_length = curs_col - compl_col;
4537
4538 return OK;
4539}
4540
4541/*
4542 * Get the pattern, column and length for user defined completion ('omnifunc',
4543 * 'completefunc' and 'thesaurusfunc')
4544 * Sets the global variables: compl_col, compl_length and compl_pattern.
4545 * Uses the global variable: spell_bad_len
4546 */
4547 static int
4548get_userdefined_compl_info(colnr_T curs_col UNUSED)
4549{
4550 int ret = FAIL;
4551
4552#ifdef FEAT_COMPL_FUNC
4553 // Call user defined function 'completefunc' with "a:findstart"
4554 // set to 1 to obtain the length of text to use for completion.
4555 char_u *line;
4556 typval_T args[3];
4557 int col;
4558 char_u *funcname;
4559 pos_T pos;
4560 int save_State = State;
4561 callback_T *cb;
4562
4563 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4564 // length as a string
4565 funcname = get_complete_funcname(ctrl_x_mode);
4566 if (*funcname == NUL)
4567 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004568 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004569 ? "completefunc" : "omnifunc");
4570 return FAIL;
4571 }
4572
4573 args[0].v_type = VAR_NUMBER;
4574 args[0].vval.v_number = 1;
4575 args[1].v_type = VAR_STRING;
4576 args[1].vval.v_string = (char_u *)"";
4577 args[2].v_type = VAR_UNKNOWN;
4578 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004579 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004580 cb = get_insert_callback(ctrl_x_mode);
4581 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004582 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004583
4584 State = save_State;
4585 curwin->w_cursor = pos; // restore the cursor position
4586 validate_cursor();
4587 if (!EQUAL_POS(curwin->w_cursor, pos))
4588 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004589 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004590 return FAIL;
4591 }
4592
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004593 // Return value -2 means the user complete function wants to cancel the
4594 // complete without an error, do the same if the function did not execute
4595 // successfully.
4596 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004597 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004598 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004599 if (col == -3)
4600 {
4601 ctrl_x_mode = CTRL_X_NORMAL;
4602 edit_submode = NULL;
4603 if (!shortmess(SHM_COMPLETIONMENU))
4604 msg_clr_cmdline();
4605 return FAIL;
4606 }
4607
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004608 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004609 // completion.
4610 compl_opt_refresh_always = FALSE;
4611 compl_opt_suppress_empty = FALSE;
4612
4613 if (col < 0)
4614 col = curs_col;
4615 compl_col = col;
4616 if (compl_col > curs_col)
4617 compl_col = curs_col;
4618
4619 // Setup variables for completion. Need to obtain "line" again,
4620 // it may have become invalid.
4621 line = ml_get(curwin->w_cursor.lnum);
4622 compl_length = curs_col - compl_col;
4623 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4624 if (compl_pattern == NULL)
4625 return FAIL;
4626
4627 ret = OK;
4628#endif
4629
4630 return ret;
4631}
4632
4633/*
4634 * Get the pattern, column and length for spell completion.
4635 * Sets the global variables: compl_col, compl_length and compl_pattern.
4636 * Uses the global variable: spell_bad_len
4637 */
4638 static int
4639get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4640{
4641 int ret = FAIL;
4642#ifdef FEAT_SPELL
4643 char_u *line;
4644
4645 if (spell_bad_len > 0)
4646 compl_col = curs_col - spell_bad_len;
4647 else
4648 compl_col = spell_word_start(startcol);
4649 if (compl_col >= (colnr_T)startcol)
4650 {
4651 compl_length = 0;
4652 compl_col = curs_col;
4653 }
4654 else
4655 {
4656 spell_expand_check_cap(compl_col);
4657 compl_length = (int)curs_col - compl_col;
4658 }
4659 // Need to obtain "line" again, it may have become invalid.
4660 line = ml_get(curwin->w_cursor.lnum);
4661 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4662 if (compl_pattern == NULL)
4663 return FAIL;
4664
4665 ret = OK;
4666#endif
4667
4668 return ret;
4669}
4670
4671/*
4672 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004673 * "startcol" - start column number of the completion pattern/text
4674 * "cur_col" - current cursor column
4675 * On return, "line_invalid" is set to TRUE, if the current line may have
4676 * become invalid and needs to be fetched again.
4677 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004678 */
4679 static int
4680compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4681{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004682 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004683 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4684 && !thesaurus_func_complete(ctrl_x_mode)))
4685 {
4686 return get_normal_compl_info(line, startcol, curs_col);
4687 }
4688 else if (ctrl_x_mode_line_or_eval())
4689 {
4690 return get_wholeline_compl_info(line, curs_col);
4691 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004692 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004693 {
4694 return get_filename_compl_info(line, startcol, curs_col);
4695 }
4696 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4697 {
4698 return get_cmdline_compl_info(line, curs_col);
4699 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004700 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004701 || thesaurus_func_complete(ctrl_x_mode))
4702 {
4703 if (get_userdefined_compl_info(curs_col) == FAIL)
4704 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004705 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004706 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004707 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004708 {
4709 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4710 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004711 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004712 }
4713 else
4714 {
4715 internal_error("ins_complete()");
4716 return FAIL;
4717 }
4718
4719 return OK;
4720}
4721
4722/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004723 * Continue an interrupted completion mode search in "line".
4724 *
4725 * If this same ctrl_x_mode has been interrupted use the text from
4726 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4727 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4728 * the same line as the cursor then fix it (the line has been split because it
4729 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4730 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004731 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004732 static void
4733ins_compl_continue_search(char_u *line)
4734{
4735 // it is a continued search
4736 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004737 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4738 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004739 {
4740 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4741 {
4742 // line (probably) wrapped, set compl_startpos to the
4743 // first non_blank in the line, if it is not a wordchar
4744 // include it to get a better pattern, but then we don't
4745 // want the "\\<" prefix, check it below
4746 compl_col = (colnr_T)getwhitecols(line);
4747 compl_startpos.col = compl_col;
4748 compl_startpos.lnum = curwin->w_cursor.lnum;
4749 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4750 }
4751 else
4752 {
4753 // S_IPOS was set when we inserted a word that was at the
4754 // beginning of the line, which means that we'll go to SOL
4755 // mode but first we need to redefine compl_startpos
4756 if (compl_cont_status & CONT_S_IPOS)
4757 {
4758 compl_cont_status |= CONT_SOL;
4759 compl_startpos.col = (colnr_T)(skipwhite(
4760 line + compl_length
4761 + compl_startpos.col) - line);
4762 }
4763 compl_col = compl_startpos.col;
4764 }
4765 compl_length = curwin->w_cursor.col - (int)compl_col;
4766 // IObuff is used to add a "word from the next line" would we
4767 // have enough space? just being paranoid
4768#define MIN_SPACE 75
4769 if (compl_length > (IOSIZE - MIN_SPACE))
4770 {
4771 compl_cont_status &= ~CONT_SOL;
4772 compl_length = (IOSIZE - MIN_SPACE);
4773 compl_col = curwin->w_cursor.col - compl_length;
4774 }
4775 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4776 if (compl_length < 1)
4777 compl_cont_status &= CONT_LOCAL;
4778 }
4779 else if (ctrl_x_mode_line_or_eval())
4780 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4781 else
4782 compl_cont_status = 0;
4783}
4784
4785/*
4786 * start insert mode completion
4787 */
4788 static int
4789ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004790{
4791 char_u *line;
4792 int startcol = 0; // column where searched text starts
4793 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004794 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004795 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004796 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004797
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004798 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004799
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004800 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004801 did_si = FALSE;
4802 can_si = FALSE;
4803 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004804 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004805 return FAIL;
4806
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004807 line = ml_get(curwin->w_cursor.lnum);
4808 curs_col = curwin->w_cursor.col;
4809 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004810
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004811 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4812 && compl_cont_mode == ctrl_x_mode)
4813 // this same ctrl-x_mode was interrupted previously. Continue the
4814 // completion.
4815 ins_compl_continue_search(line);
4816 else
4817 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004818
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004819 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004820 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004821 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004822 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004823 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4824 compl_cont_status = 0;
4825 compl_cont_status |= CONT_N_ADDS;
4826 compl_startpos = curwin->w_cursor;
4827 startcol = (int)curs_col;
4828 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004829 }
4830
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004831 // Work out completion pattern and original text -- webb
4832 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4833 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004834 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4835 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004836 // restore did_ai, so that adding comment leader works
4837 did_ai = save_did_ai;
4838 return FAIL;
4839 }
4840 // If "line" was changed while getting completion info get it again.
4841 if (line_invalid)
4842 line = ml_get(curwin->w_cursor.lnum);
4843
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004844 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004845 {
4846 edit_submode_pre = (char_u *)_(" Adding");
4847 if (ctrl_x_mode_line_or_eval())
4848 {
4849 // Insert a new line, keep indentation but ignore 'comments'.
4850 char_u *old = curbuf->b_p_com;
4851
4852 curbuf->b_p_com = (char_u *)"";
4853 compl_startpos.lnum = curwin->w_cursor.lnum;
4854 compl_startpos.col = compl_col;
4855 ins_eol('\r');
4856 curbuf->b_p_com = old;
4857 compl_length = 0;
4858 compl_col = curwin->w_cursor.col;
4859 }
4860 }
4861 else
4862 {
4863 edit_submode_pre = NULL;
4864 compl_startpos.col = compl_col;
4865 }
4866
4867 if (compl_cont_status & CONT_LOCAL)
4868 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4869 else
4870 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4871
4872 // If any of the original typed text has been changed we need to fix
4873 // the redo buffer.
4874 ins_compl_fixRedoBufForLeader(NULL);
4875
4876 // Always add completion for the original text.
4877 vim_free(compl_orig_text);
4878 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4879 if (p_ic)
4880 flags |= CP_ICASE;
4881 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4882 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4883 {
4884 VIM_CLEAR(compl_pattern);
4885 VIM_CLEAR(compl_orig_text);
4886 return FAIL;
4887 }
4888
4889 // showmode might reset the internal line pointers, so it must
4890 // be called before line = ml_get(), or when this address is no
4891 // longer needed. -- Acevedo.
4892 edit_submode_extra = (char_u *)_("-- Searching...");
4893 edit_submode_highl = HLF_COUNT;
4894 showmode();
4895 edit_submode_extra = NULL;
4896 out_flush();
4897
4898 return OK;
4899}
4900
4901/*
4902 * display the completion status message
4903 */
4904 static void
4905ins_compl_show_statusmsg(void)
4906{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004907 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004908 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004909 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004910 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004911 ? (char_u *)_("Hit end of paragraph")
4912 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004913 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004914 }
4915
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004916 if (edit_submode_extra == NULL)
4917 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004918 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004919 {
4920 edit_submode_extra = (char_u *)_("Back at original");
4921 edit_submode_highl = HLF_W;
4922 }
4923 else if (compl_cont_status & CONT_S_IPOS)
4924 {
4925 edit_submode_extra = (char_u *)_("Word from other line");
4926 edit_submode_highl = HLF_COUNT;
4927 }
4928 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4929 {
4930 edit_submode_extra = (char_u *)_("The only match");
4931 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004932 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004933 }
4934 else
4935 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004936#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004937 // Update completion sequence number when needed.
4938 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004939 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004940#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004941 // The match should always have a sequence number now, this is
4942 // just a safety check.
4943 if (compl_curr_match->cp_number != -1)
4944 {
4945 // Space for 10 text chars. + 2x10-digit no.s = 31.
4946 // Translations may need more than twice that.
4947 static char_u match_ref[81];
4948
4949 if (compl_matches > 0)
4950 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004951 _("match %d of %d"),
4952 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004953 else
4954 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004955 _("match %d"),
4956 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004957 edit_submode_extra = match_ref;
4958 edit_submode_highl = HLF_R;
4959 if (dollar_vcol >= 0)
4960 curs_columns(FALSE);
4961 }
4962 }
4963 }
4964
4965 // Show a message about what (completion) mode we're in.
4966 if (!compl_opt_suppress_empty)
4967 {
4968 showmode();
4969 if (!shortmess(SHM_COMPLETIONMENU))
4970 {
4971 if (edit_submode_extra != NULL)
4972 {
4973 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004974 {
4975 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004976 msg_attr((char *)edit_submode_extra,
4977 edit_submode_highl < HLF_COUNT
4978 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004979 msg_hist_off = FALSE;
4980 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004981 }
4982 else
4983 msg_clr_cmdline(); // necessary for "noshowmode"
4984 }
4985 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004986}
4987
4988/*
4989 * Do Insert mode completion.
4990 * Called when character "c" was typed, which has a meaning for completion.
4991 * Returns OK if completion was done, FAIL if something failed (out of mem).
4992 */
4993 int
4994ins_complete(int c, int enable_pum)
4995{
4996 int n;
4997 int save_w_wrow;
4998 int save_w_leftcol;
4999 int insert_match;
5000
5001 compl_direction = ins_compl_key2dir(c);
5002 insert_match = ins_compl_use_match(c);
5003
5004 if (!compl_started)
5005 {
5006 if (ins_compl_start() == FAIL)
5007 return FAIL;
5008 }
5009 else if (insert_match && stop_arrow() == FAIL)
5010 return FAIL;
5011
5012 compl_shown_match = compl_curr_match;
5013 compl_shows_dir = compl_direction;
5014
5015 // Find next match (and following matches).
5016 save_w_wrow = curwin->w_wrow;
5017 save_w_leftcol = curwin->w_leftcol;
5018 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5019
5020 // may undisplay the popup menu
5021 ins_compl_upd_pum();
5022
5023 if (n > 1) // all matches have been found
5024 compl_matches = n;
5025 compl_curr_match = compl_shown_match;
5026 compl_direction = compl_shows_dir;
5027
5028 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5029 // mode.
5030 if (got_int && !global_busy)
5031 {
5032 (void)vgetc();
5033 got_int = FALSE;
5034 }
5035
5036 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005037 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005038 {
5039 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5040 // because we couldn't expand anything at first place, but if we used
5041 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5042 // (such as M in M'exico) if not tried already. -- Acevedo
5043 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005044 || compl_status_adding()
5045 || (ctrl_x_mode_not_default()
5046 && !ctrl_x_mode_path_patterns()
5047 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005048 compl_cont_status &= ~CONT_N_ADDS;
5049 }
5050
5051 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5052 compl_cont_status |= CONT_S_IPOS;
5053 else
5054 compl_cont_status &= ~CONT_S_IPOS;
5055
5056 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005057
5058 // Show the popup menu, unless we got interrupted.
5059 if (enable_pum && !compl_interrupted)
5060 show_pum(save_w_wrow, save_w_leftcol);
5061
5062 compl_was_interrupted = compl_interrupted;
5063 compl_interrupted = FALSE;
5064
5065 return OK;
5066}
5067
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005068/*
5069 * Remove (if needed) and show the popup menu
5070 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005071 static void
5072show_pum(int prev_w_wrow, int prev_w_leftcol)
5073{
5074 // RedrawingDisabled may be set when invoked through complete().
5075 int n = RedrawingDisabled;
5076
5077 RedrawingDisabled = 0;
5078
5079 // If the cursor moved or the display scrolled we need to remove the pum
5080 // first.
5081 setcursor();
5082 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5083 ins_compl_del_pum();
5084
5085 ins_compl_show_pum();
5086 setcursor();
5087 RedrawingDisabled = n;
5088}
5089
5090/*
5091 * Looks in the first "len" chars. of "src" for search-metachars.
5092 * If dest is not NULL the chars. are copied there quoting (with
5093 * a backslash) the metachars, and dest would be NUL terminated.
5094 * Returns the length (needed) of dest
5095 */
5096 static unsigned
5097quote_meta(char_u *dest, char_u *src, int len)
5098{
5099 unsigned m = (unsigned)len + 1; // one extra for the NUL
5100
5101 for ( ; --len >= 0; src++)
5102 {
5103 switch (*src)
5104 {
5105 case '.':
5106 case '*':
5107 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005108 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005109 break;
5110 // FALLTHROUGH
5111 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005112 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005113 break;
5114 // FALLTHROUGH
5115 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005116 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005117 break;
5118 // FALLTHROUGH
5119 case '^': // currently it's not needed.
5120 case '$':
5121 m++;
5122 if (dest != NULL)
5123 *dest++ = '\\';
5124 break;
5125 }
5126 if (dest != NULL)
5127 *dest++ = *src;
5128 // Copy remaining bytes of a multibyte character.
5129 if (has_mbyte)
5130 {
5131 int i, mb_len;
5132
5133 mb_len = (*mb_ptr2len)(src) - 1;
5134 if (mb_len > 0 && len >= mb_len)
5135 for (i = 0; i < mb_len; ++i)
5136 {
5137 --len;
5138 ++src;
5139 if (dest != NULL)
5140 *dest++ = *src;
5141 }
5142 }
5143 }
5144 if (dest != NULL)
5145 *dest = NUL;
5146
5147 return m;
5148}
5149
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005150#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005151 void
5152free_insexpand_stuff(void)
5153{
5154 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005155# ifdef FEAT_EVAL
5156 free_callback(&cfu_cb);
5157 free_callback(&ofu_cb);
5158 free_callback(&tsrfu_cb);
5159# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005160}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005161#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005162
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005163#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005164/*
5165 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5166 * spelled word, if there is one.
5167 */
5168 static void
5169spell_back_to_badword(void)
5170{
5171 pos_T tpos = curwin->w_cursor;
5172
5173 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5174 if (curwin->w_cursor.col != tpos.col)
5175 start_arrow(&tpos);
5176}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005177#endif