blob: d712181bcd478724528cdebede086dc69ba7b8f7 [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 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000266int ctrl_x_mode_none(void)
267 { return ctrl_x_mode == 0; }
268int ctrl_x_mode_normal(void)
269 { return ctrl_x_mode == CTRL_X_NORMAL; }
270int ctrl_x_mode_scroll(void)
271 { return ctrl_x_mode == CTRL_X_SCROLL; }
272int ctrl_x_mode_whole_line(void)
273 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
274int ctrl_x_mode_files(void)
275 { return ctrl_x_mode == CTRL_X_FILES; }
276int ctrl_x_mode_tags(void)
277 { return ctrl_x_mode == CTRL_X_TAGS; }
278int ctrl_x_mode_path_patterns(void)
279 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
280int ctrl_x_mode_path_defines(void)
281 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
282int ctrl_x_mode_dictionary(void)
283 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
284int ctrl_x_mode_thesaurus(void)
285 { return ctrl_x_mode == CTRL_X_THESAURUS; }
286int ctrl_x_mode_cmdline(void)
287 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200288 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000289int ctrl_x_mode_function(void)
290 { return ctrl_x_mode == CTRL_X_FUNCTION; }
291int ctrl_x_mode_omni(void)
292 { return ctrl_x_mode == CTRL_X_OMNI; }
293int ctrl_x_mode_spell(void)
294 { return ctrl_x_mode == CTRL_X_SPELL; }
295static int ctrl_x_mode_eval(void)
296 { return ctrl_x_mode == CTRL_X_EVAL; }
297int ctrl_x_mode_line_or_eval(void)
298 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100299
300/*
301 * Whether other than default completion has been selected.
302 */
303 int
304ctrl_x_mode_not_default(void)
305{
306 return ctrl_x_mode != CTRL_X_NORMAL;
307}
308
309/*
zeertzjqdca29d92021-08-31 19:12:51 +0200310 * Whether CTRL-X was typed without a following character,
311 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100312 */
313 int
314ctrl_x_mode_not_defined_yet(void)
315{
316 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
317}
318
319/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000320 * Return TRUE if currently in "normal" or "adding" insert completion matches
321 * state
322 */
323 int
324compl_status_adding(void)
325{
326 return compl_cont_status & CONT_ADDING;
327}
328
329/*
330 * Return TRUE if the completion pattern includes start of line, just for
331 * word-wise expansion.
332 */
333 int
334compl_status_sol(void)
335{
336 return compl_cont_status & CONT_SOL;
337}
338
339/*
340 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
341 */
342 int
343compl_status_local(void)
344{
345 return compl_cont_status & CONT_LOCAL;
346}
347
348/*
349 * Clear the completion status flags
350 */
351 void
352compl_status_clear(void)
353{
354 compl_cont_status = 0;
355}
356
357/*
358 * Return TRUE if completion is using the forward direction matches
359 */
360 static int
361compl_dir_forward(void)
362{
363 return compl_direction == FORWARD;
364}
365
366/*
367 * Return TRUE if currently showing forward completion matches
368 */
369 static int
370compl_shows_dir_forward(void)
371{
372 return compl_shows_dir == FORWARD;
373}
374
375/*
376 * Return TRUE if currently showing backward completion matches
377 */
378 static int
379compl_shows_dir_backward(void)
380{
381 return compl_shows_dir == BACKWARD;
382}
383
384/*
385 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100386 */
387 int
388has_compl_option(int dict_opt)
389{
390 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200391#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200393#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100394 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100395 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
396#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100397 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100398#endif
399 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100400 {
401 ctrl_x_mode = CTRL_X_NORMAL;
402 edit_submode = NULL;
403 msg_attr(dict_opt ? _("'dictionary' option is empty")
404 : _("'thesaurus' option is empty"),
405 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100406 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100407 {
408 vim_beep(BO_COMPL);
409 setcursor();
410 out_flush();
411#ifdef FEAT_EVAL
412 if (!get_vim_var_nr(VV_TESTING))
413#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100414 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100415 }
416 return FALSE;
417 }
418 return TRUE;
419}
420
421/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000422 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100423 * This depends on the current mode.
424 */
425 int
426vim_is_ctrl_x_key(int c)
427{
428 // Always allow ^R - let its results then be checked
429 if (c == Ctrl_R)
430 return TRUE;
431
432 // Accept <PageUp> and <PageDown> if the popup menu is visible.
433 if (ins_compl_pum_key(c))
434 return TRUE;
435
436 switch (ctrl_x_mode)
437 {
438 case 0: // Not in any CTRL-X mode
439 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
440 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200441 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100442 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
443 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
444 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
445 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
446 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200447 || c == Ctrl_S || c == Ctrl_K || c == 's'
448 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100449 case CTRL_X_SCROLL:
450 return (c == Ctrl_Y || c == Ctrl_E);
451 case CTRL_X_WHOLE_LINE:
452 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
453 case CTRL_X_FILES:
454 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_DICTIONARY:
456 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_THESAURUS:
458 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
459 case CTRL_X_TAGS:
460 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
461#ifdef FEAT_FIND_ID
462 case CTRL_X_PATH_PATTERNS:
463 return (c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_PATH_DEFINES:
465 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
466#endif
467 case CTRL_X_CMDLINE:
468 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
469 || c == Ctrl_X);
470#ifdef FEAT_COMPL_FUNC
471 case CTRL_X_FUNCTION:
472 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
473 case CTRL_X_OMNI:
474 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
475#endif
476 case CTRL_X_SPELL:
477 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
478 case CTRL_X_EVAL:
479 return (c == Ctrl_P || c == Ctrl_N);
480 }
481 internal_error("vim_is_ctrl_x_key()");
482 return FALSE;
483}
484
485/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000486 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000487 */
488 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000489match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000490{
491 return match->cp_flags & CP_ORIGINAL_TEXT;
492}
493
494/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000495 * Returns TRUE if "match" is the first match in the completion list.
496 */
497 static int
498is_first_match(compl_T *match)
499{
500 return match == compl_first_match;
501}
502
503/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100504 * Return TRUE when character "c" is part of the item currently being
505 * completed. Used to decide whether to abandon complete mode when the menu
506 * is visible.
507 */
508 int
509ins_compl_accept_char(int c)
510{
511 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
512 // When expanding an identifier only accept identifier chars.
513 return vim_isIDc(c);
514
515 switch (ctrl_x_mode)
516 {
517 case CTRL_X_FILES:
518 // When expanding file name only accept file name chars. But not
519 // path separators, so that "proto/<Tab>" expands files in
520 // "proto", not "proto/" as a whole
521 return vim_isfilec(c) && !vim_ispathsep(c);
522
523 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200524 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100525 case CTRL_X_OMNI:
526 // Command line and Omni completion can work with just about any
527 // printable character, but do stop at white space.
528 return vim_isprintc(c) && !VIM_ISWHITE(c);
529
530 case CTRL_X_WHOLE_LINE:
531 // For while line completion a space can be part of the line.
532 return vim_isprintc(c);
533 }
534 return vim_iswordc(c);
535}
536
537/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000538 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100539 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000540 */
541 static char_u *
542ins_compl_infercase_gettext(
543 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100544 int char_len,
545 int compl_char_len,
546 int min_len,
547 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548{
549 int *wca; // Wide character array.
550 char_u *p;
551 int i, c;
552 int has_lower = FALSE;
553 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100554 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000555
556 IObuff[0] = NUL;
557
558 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100559 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000560 if (wca == NULL)
561 return IObuff;
562
563 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100564 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (has_mbyte)
566 wca[i] = mb_ptr2char_adv(&p);
567 else
568 wca[i] = *(p++);
569
570 // Rule 1: Were any chars converted to lower?
571 p = compl_orig_text;
572 for (i = 0; i < min_len; ++i)
573 {
574 if (has_mbyte)
575 c = mb_ptr2char_adv(&p);
576 else
577 c = *(p++);
578 if (MB_ISLOWER(c))
579 {
580 has_lower = TRUE;
581 if (MB_ISUPPER(wca[i]))
582 {
583 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100584 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000585 wca[i] = MB_TOLOWER(wca[i]);
586 break;
587 }
588 }
589 }
590
591 // Rule 2: No lower case, 2nd consecutive letter converted to
592 // upper case.
593 if (!has_lower)
594 {
595 p = compl_orig_text;
596 for (i = 0; i < min_len; ++i)
597 {
598 if (has_mbyte)
599 c = mb_ptr2char_adv(&p);
600 else
601 c = *(p++);
602 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
603 {
604 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100605 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000606 wca[i] = MB_TOUPPER(wca[i]);
607 break;
608 }
609 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
610 }
611 }
612
613 // Copy the original case of the part we typed.
614 p = compl_orig_text;
615 for (i = 0; i < min_len; ++i)
616 {
617 if (has_mbyte)
618 c = mb_ptr2char_adv(&p);
619 else
620 c = *(p++);
621 if (MB_ISLOWER(c))
622 wca[i] = MB_TOLOWER(wca[i]);
623 else if (MB_ISUPPER(c))
624 wca[i] = MB_TOUPPER(wca[i]);
625 }
626
627 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000628 p = IObuff;
629 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100630 ga_init2(&gap, 1, 500);
631 while (i < char_len)
632 {
633 if (gap.ga_data != NULL)
634 {
635 if (ga_grow(&gap, 10) == FAIL)
636 {
637 ga_clear(&gap);
638 return (char_u *)"[failed]";
639 }
640 p = (char_u *)gap.ga_data + gap.ga_len;
641 if (has_mbyte)
642 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
643 else
644 {
645 *p = wca[i++];
646 ++gap.ga_len;
647 }
648 }
649 else if ((p - IObuff) + 6 >= IOSIZE)
650 {
651 // Multi-byte characters can occupy up to five bytes more than
652 // ASCII characters, and we also need one byte for NUL, so when
653 // getting to six bytes from the edge of IObuff switch to using a
654 // growarray. Add the character in the next round.
655 if (ga_grow(&gap, IOSIZE) == FAIL)
656 return (char_u *)"[failed]";
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100657 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100658 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100659 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100660 }
661 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000662 p += (*mb_char2bytes)(wca[i++], p);
663 else
664 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100665 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000666 vim_free(wca);
667
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100668 if (gap.ga_data != NULL)
669 {
670 *tofree = gap.ga_data;
671 return gap.ga_data;
672 }
673
674 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000675 return IObuff;
676}
677
678/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
680 * case of the originally typed text is used, and the case of the completed
681 * text is inferred, ie this tries to work out what case you probably wanted
682 * the rest of the word to be in -- webb
683 */
684 int
685ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200686 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100687 int len,
688 int icase,
689 char_u *fname,
690 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200691 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200693 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100694 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100695 int char_len; // count multi-byte characters
696 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100697 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200698 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100699 int res;
700 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701
702 if (p_ic && curbuf->b_p_inf && len > 0)
703 {
704 // Infer case of completed part.
705
706 // Find actual length of completion.
707 if (has_mbyte)
708 {
709 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100710 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100711 while (*p != NUL)
712 {
713 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 }
716 }
717 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719
720 // Find actual length of original text.
721 if (has_mbyte)
722 {
723 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100724 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100725 while (*p != NUL)
726 {
727 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100728 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729 }
730 }
731 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100734 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100735 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100738 str = ins_compl_infercase_gettext(str, char_len,
739 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100740 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200741 if (cont_s_ipos)
742 flags |= CP_CONT_S_IPOS;
743 if (icase)
744 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200745
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100746 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
747 vim_free(tofree);
748 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100749}
750
751/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000752 * Add a match to the list of matches. The arguments are:
753 * str - text of the match to add
754 * len - length of "str". If -1, then the length of "str" is
755 * computed.
756 * fname - file name to associate with this match.
757 * cptext - list of strings to use with this match (for abbr, menu, info
758 * and kind)
759 * user_data - user supplied data (any vim type) for this match
760 * cdir - match direction. If 0, use "compl_direction".
761 * flags_arg - match flags (cp_flags)
762 * adup - accept this match even if it is already present.
763 * If "cdir" is FORWARD, then the match is added after the current match.
764 * Otherwise, it is added before the current match.
765 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100766 * If the given string is already in the list of completions, then return
767 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
768 * maybe because alloc() returns NULL, then FAIL is returned.
769 */
770 static int
771ins_compl_add(
772 char_u *str,
773 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100774 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 char_u **cptext, // extra text for popup menu or NULL
776 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200778 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100779 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780{
781 compl_T *match;
782 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200783 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784
Bram Moolenaarceb06192021-04-04 15:05:22 +0200785 if (flags & CP_FAST)
786 fast_breakcheck();
787 else
788 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100789 if (got_int)
790 return FAIL;
791 if (len < 0)
792 len = (int)STRLEN(str);
793
794 // If the same match is already present, don't add it.
795 if (compl_first_match != NULL && !adup)
796 {
797 match = compl_first_match;
798 do
799 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000800 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100801 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100802 && ((int)STRLEN(match->cp_str) <= len
803 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 return NOTDONE;
805 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000806 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 }
808
809 // Remove any popup menu before changing the list of matches.
810 ins_compl_del_pum();
811
812 // Allocate a new match structure.
813 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200814 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100815 if (match == NULL)
816 return FAIL;
817 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200818 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 match->cp_number = 0;
820 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
821 {
822 vim_free(match);
823 return FAIL;
824 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100825
826 // match-fname is:
827 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200828 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829 // - NULL otherwise. --Acevedo
830 if (fname != NULL
831 && compl_curr_match != NULL
832 && compl_curr_match->cp_fname != NULL
833 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
834 match->cp_fname = compl_curr_match->cp_fname;
835 else if (fname != NULL)
836 {
837 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200838 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100839 }
840 else
841 match->cp_fname = NULL;
842 match->cp_flags = flags;
843
844 if (cptext != NULL)
845 {
846 int i;
847
848 for (i = 0; i < CPT_COUNT; ++i)
849 if (cptext[i] != NULL && *cptext[i] != NUL)
850 match->cp_text[i] = vim_strsave(cptext[i]);
851 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100852#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100853 if (user_data != NULL)
854 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100855#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100856
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000857 // Link the new match structure after (FORWARD) or before (BACKWARD) the
858 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859 if (compl_first_match == NULL)
860 match->cp_next = match->cp_prev = NULL;
861 else if (dir == FORWARD)
862 {
863 match->cp_next = compl_curr_match->cp_next;
864 match->cp_prev = compl_curr_match;
865 }
866 else // BACKWARD
867 {
868 match->cp_next = compl_curr_match;
869 match->cp_prev = compl_curr_match->cp_prev;
870 }
871 if (match->cp_next)
872 match->cp_next->cp_prev = match;
873 if (match->cp_prev)
874 match->cp_prev->cp_next = match;
875 else // if there's nothing before, it is the first match
876 compl_first_match = match;
877 compl_curr_match = match;
878
879 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200880 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100881 ins_compl_longest_match(match);
882
883 return OK;
884}
885
886/*
887 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200888 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100889 */
890 static int
891ins_compl_equal(compl_T *match, char_u *str, int len)
892{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200893 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200894 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200895 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100896 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
897 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
898}
899
900/*
901 * Reduce the longest common string for match "match".
902 */
903 static void
904ins_compl_longest_match(compl_T *match)
905{
906 char_u *p, *s;
907 int c1, c2;
908 int had_match;
909
910 if (compl_leader == NULL)
911 {
912 // First match, use it as a whole.
913 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000914 if (compl_leader == NULL)
915 return;
916
917 had_match = (curwin->w_cursor.col > compl_col);
918 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000919 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000920 ins_redraw(FALSE);
921
922 // When the match isn't there (to avoid matching itself) remove it
923 // again after redrawing.
924 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100926 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000927
928 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000930
931 // Reduce the text if this match differs from compl_leader.
932 p = compl_leader;
933 s = match->cp_str;
934 while (*p != NUL)
935 {
936 if (has_mbyte)
937 {
938 c1 = mb_ptr2char(p);
939 c2 = mb_ptr2char(s);
940 }
941 else
942 {
943 c1 = *p;
944 c2 = *s;
945 }
946 if ((match->cp_flags & CP_ICASE)
947 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
948 break;
949 if (has_mbyte)
950 {
951 MB_PTR_ADV(p);
952 MB_PTR_ADV(s);
953 }
954 else
955 {
956 ++p;
957 ++s;
958 }
959 }
960
961 if (*p != NUL)
962 {
963 // Leader was shortened, need to change the inserted text.
964 *p = NUL;
965 had_match = (curwin->w_cursor.col > compl_col);
966 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000967 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000968 ins_redraw(FALSE);
969
970 // When the match isn't there (to avoid matching itself) remove it
971 // again after redrawing.
972 if (!had_match)
973 ins_compl_delete();
974 }
975
976 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100977}
978
979/*
980 * Add an array of matches to the list of matches.
981 * Frees matches[].
982 */
983 static void
984ins_compl_add_matches(
985 int num_matches,
986 char_u **matches,
987 int icase)
988{
989 int i;
990 int add_r = OK;
991 int dir = compl_direction;
992
993 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100994 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200995 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100996 // if dir was BACKWARD then honor it just once
997 dir = FORWARD;
998 FreeWild(num_matches, matches);
999}
1000
1001/*
1002 * Make the completion list cyclic.
1003 * Return the number of matches (excluding the original).
1004 */
1005 static int
1006ins_compl_make_cyclic(void)
1007{
1008 compl_T *match;
1009 int count = 0;
1010
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001011 if (compl_first_match == NULL)
1012 return 0;
1013
1014 // Find the end of the list.
1015 match = compl_first_match;
1016 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001017 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001018 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001019 match = match->cp_next;
1020 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001021 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001022 match->cp_next = compl_first_match;
1023 compl_first_match->cp_prev = match;
1024
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001025 return count;
1026}
1027
1028/*
1029 * Return whether there currently is a shown match.
1030 */
1031 int
1032ins_compl_has_shown_match(void)
1033{
1034 return compl_shown_match == NULL
1035 || compl_shown_match != compl_shown_match->cp_next;
1036}
1037
1038/*
1039 * Return whether the shown match is long enough.
1040 */
1041 int
1042ins_compl_long_shown_match(void)
1043{
1044 return (int)STRLEN(compl_shown_match->cp_str)
1045 > curwin->w_cursor.col - compl_col;
1046}
1047
1048/*
1049 * Set variables that store noselect and noinsert behavior from the
1050 * 'completeopt' value.
1051 */
1052 void
1053completeopt_was_set(void)
1054{
1055 compl_no_insert = FALSE;
1056 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001057 compl_longest = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001058 if (strstr((char *)p_cot, "noselect") != NULL)
1059 compl_no_select = TRUE;
1060 if (strstr((char *)p_cot, "noinsert") != NULL)
1061 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001062 if (strstr((char *)p_cot, "longest") != NULL)
1063 compl_longest = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001064}
1065
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001066
1067// "compl_match_array" points the currently displayed list of entries in the
1068// popup menu. It is NULL when there is no popup menu.
1069static pumitem_T *compl_match_array = NULL;
1070static int compl_match_arraysize;
1071
1072/*
1073 * Update the screen and when there is any scrolling remove the popup menu.
1074 */
1075 static void
1076ins_compl_upd_pum(void)
1077{
1078 int h;
1079
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001080 if (compl_match_array == NULL)
1081 return;
1082
1083 h = curwin->w_cline_height;
1084 // Update the screen later, before drawing the popup menu over it.
1085 pum_call_update_screen();
1086 if (h != curwin->w_cline_height)
1087 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001088}
1089
1090/*
1091 * Remove any popup menu.
1092 */
1093 static void
1094ins_compl_del_pum(void)
1095{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001096 if (compl_match_array == NULL)
1097 return;
1098
1099 pum_undisplay();
1100 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001101}
1102
1103/*
1104 * Return TRUE if the popup menu should be displayed.
1105 */
1106 int
1107pum_wanted(void)
1108{
1109 // 'completeopt' must contain "menu" or "menuone"
1110 if (vim_strchr(p_cot, 'm') == NULL)
1111 return FALSE;
1112
1113 // The display looks bad on a B&W display.
1114 if (t_colors < 8
1115#ifdef FEAT_GUI
1116 && !gui.in_use
1117#endif
1118 )
1119 return FALSE;
1120 return TRUE;
1121}
1122
1123/*
1124 * Return TRUE if there are two or more matches to be shown in the popup menu.
1125 * One if 'completopt' contains "menuone".
1126 */
1127 static int
1128pum_enough_matches(void)
1129{
1130 compl_T *compl;
1131 int i;
1132
1133 // Don't display the popup menu if there are no matches or there is only
1134 // one (ignoring the original text).
1135 compl = compl_first_match;
1136 i = 0;
1137 do
1138 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001139 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001140 break;
1141 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001142 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001143
1144 if (strstr((char *)p_cot, "menuone") != NULL)
1145 return (i >= 1);
1146 return (i >= 2);
1147}
1148
Bram Moolenaar3075a452021-11-17 15:51:52 +00001149#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001150/*
1151 * Allocate Dict for the completed item.
1152 * { word, abbr, menu, kind, info }
1153 */
1154 static dict_T *
1155ins_compl_dict_alloc(compl_T *match)
1156{
1157 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1158
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001159 if (dict == NULL)
1160 return NULL;
1161
1162 dict_add_string(dict, "word", match->cp_str);
1163 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1164 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1165 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1166 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1167 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1168 dict_add_string(dict, "user_data", (char_u *)"");
1169 else
1170 dict_add_tv(dict, "user_data", &match->cp_user_data);
1171
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001172 return dict;
1173}
1174
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001175/*
1176 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1177 * completion menu is changed.
1178 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001179 static void
1180trigger_complete_changed_event(int cur)
1181{
1182 dict_T *v_event;
1183 dict_T *item;
1184 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001185 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001186
1187 if (recursive)
1188 return;
1189
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001190 if (cur < 0)
1191 item = dict_alloc();
1192 else
1193 item = ins_compl_dict_alloc(compl_curr_match);
1194 if (item == NULL)
1195 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001196 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001197 dict_add_dict(v_event, "completed_item", item);
1198 pum_set_event_info(v_event);
1199 dict_set_items_ro(v_event);
1200
1201 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001202 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001203 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001204 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001205 recursive = FALSE;
1206
Bram Moolenaar3075a452021-11-17 15:51:52 +00001207 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001208}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001209#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001210
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001211/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001212 * Build a popup menu to show the completion matches.
1213 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1214 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001215 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001216 static int
1217ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001218{
1219 compl_T *compl;
1220 compl_T *shown_compl = NULL;
1221 int did_find_shown_match = FALSE;
1222 int shown_match_ok = FALSE;
1223 int i;
1224 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001225 int lead_len = 0;
1226
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001227 // Need to build the popup menu list.
1228 compl_match_arraysize = 0;
1229 compl = compl_first_match;
1230 if (compl_leader != NULL)
1231 lead_len = (int)STRLEN(compl_leader);
1232
1233 do
1234 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001235 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001236 && (compl_leader == NULL
1237 || ins_compl_equal(compl, compl_leader, lead_len)))
1238 ++compl_match_arraysize;
1239 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001240 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001241
1242 if (compl_match_arraysize == 0)
1243 return -1;
1244
1245 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1246 if (compl_match_array == NULL)
1247 return -1;
1248
1249 // If the current match is the original text don't find the first
1250 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001251 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001252 shown_match_ok = TRUE;
1253
1254 i = 0;
1255 compl = compl_first_match;
1256 do
1257 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001258 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001259 && (compl_leader == NULL
1260 || ins_compl_equal(compl, compl_leader, lead_len)))
1261 {
1262 if (!shown_match_ok)
1263 {
1264 if (compl == compl_shown_match || did_find_shown_match)
1265 {
1266 // This item is the shown match or this is the
1267 // first displayed item after the shown match.
1268 compl_shown_match = compl;
1269 did_find_shown_match = TRUE;
1270 shown_match_ok = TRUE;
1271 }
1272 else
1273 // Remember this displayed match for when the
1274 // shown match is just below it.
1275 shown_compl = compl;
1276 cur = i;
1277 }
1278
1279 if (compl->cp_text[CPT_ABBR] != NULL)
1280 compl_match_array[i].pum_text =
1281 compl->cp_text[CPT_ABBR];
1282 else
1283 compl_match_array[i].pum_text = compl->cp_str;
1284 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1285 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1286 if (compl->cp_text[CPT_MENU] != NULL)
1287 compl_match_array[i++].pum_extra =
1288 compl->cp_text[CPT_MENU];
1289 else
1290 compl_match_array[i++].pum_extra = compl->cp_fname;
1291 }
1292
1293 if (compl == compl_shown_match)
1294 {
1295 did_find_shown_match = TRUE;
1296
1297 // When the original text is the shown match don't set
1298 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001299 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001300 shown_match_ok = TRUE;
1301
1302 if (!shown_match_ok && shown_compl != NULL)
1303 {
1304 // The shown match isn't displayed, set it to the
1305 // previously displayed match.
1306 compl_shown_match = shown_compl;
1307 shown_match_ok = TRUE;
1308 }
1309 }
1310 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001311 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001312
1313 if (!shown_match_ok) // no displayed match at all
1314 cur = -1;
1315
1316 return cur;
1317}
1318
1319/*
1320 * Show the popup menu for the list of matches.
1321 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1322 */
1323 void
1324ins_compl_show_pum(void)
1325{
1326 int i;
1327 int cur = -1;
1328 colnr_T col;
1329
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001330 if (!pum_wanted() || !pum_enough_matches())
1331 return;
1332
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001333 // Update the screen later, before drawing the popup menu over it.
1334 pum_call_update_screen();
1335
1336 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001337 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001338 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001339 else
1340 {
1341 // popup menu already exists, only need to find the current item.
1342 for (i = 0; i < compl_match_arraysize; ++i)
1343 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1344 || compl_match_array[i].pum_text
1345 == compl_shown_match->cp_text[CPT_ABBR])
1346 {
1347 cur = i;
1348 break;
1349 }
1350 }
1351
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001352 if (compl_match_array == NULL)
1353 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001354
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001355 // In Replace mode when a $ is displayed at the end of the line only
1356 // part of the screen would be updated. We do need to redraw here.
1357 dollar_vcol = -1;
1358
1359 // Compute the screen column of the start of the completed text.
1360 // Use the cursor to get all wrapping and other settings right.
1361 col = curwin->w_cursor.col;
1362 curwin->w_cursor.col = compl_col;
1363 pum_display(compl_match_array, compl_match_arraysize, cur);
1364 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001365
glepnircbb46b42024-02-03 18:11:13 +01001366 // After adding leader, set the current match to shown match.
1367 if (compl_started && compl_curr_match != compl_shown_match)
1368 compl_curr_match = compl_shown_match;
1369
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001370#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001371 if (has_completechanged())
1372 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001373#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001374}
1375
1376#define DICT_FIRST (1) // use just first element in "dict"
1377#define DICT_EXACT (2) // "dict" is the exact name of a file
1378
1379/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001380 * Add any identifiers that match the given pattern "pat" in the list of
1381 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001382 */
1383 static void
1384ins_compl_dictionaries(
1385 char_u *dict_start,
1386 char_u *pat,
1387 int flags, // DICT_FIRST and/or DICT_EXACT
1388 int thesaurus) // Thesaurus completion
1389{
1390 char_u *dict = dict_start;
1391 char_u *ptr;
1392 char_u *buf;
1393 regmatch_T regmatch;
1394 char_u **files;
1395 int count;
1396 int save_p_scs;
1397 int dir = compl_direction;
1398
1399 if (*dict == NUL)
1400 {
1401#ifdef FEAT_SPELL
1402 // When 'dictionary' is empty and spell checking is enabled use
1403 // "spell".
1404 if (!thesaurus && curwin->w_p_spell)
1405 dict = (char_u *)"spell";
1406 else
1407#endif
1408 return;
1409 }
1410
1411 buf = alloc(LSIZE);
1412 if (buf == NULL)
1413 return;
1414 regmatch.regprog = NULL; // so that we can goto theend
1415
1416 // If 'infercase' is set, don't use 'smartcase' here
1417 save_p_scs = p_scs;
1418 if (curbuf->b_p_inf)
1419 p_scs = FALSE;
1420
1421 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1422 // to only match at the start of a line. Otherwise just match the
1423 // pattern. Also need to double backslashes.
1424 if (ctrl_x_mode_line_or_eval())
1425 {
1426 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1427 size_t len;
1428
1429 if (pat_esc == NULL)
1430 goto theend;
1431 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001432 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001433 if (ptr == NULL)
1434 {
1435 vim_free(pat_esc);
1436 goto theend;
1437 }
1438 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1439 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1440 vim_free(pat_esc);
1441 vim_free(ptr);
1442 }
1443 else
1444 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001445 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001446 if (regmatch.regprog == NULL)
1447 goto theend;
1448 }
1449
1450 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1451 regmatch.rm_ic = ignorecase(pat);
1452 while (*dict != NUL && !got_int && !compl_interrupted)
1453 {
1454 // copy one dictionary file name into buf
1455 if (flags == DICT_EXACT)
1456 {
1457 count = 1;
1458 files = &dict;
1459 }
1460 else
1461 {
1462 // Expand wildcards in the dictionary name, but do not allow
1463 // backticks (for security, the 'dict' option may have been set in
1464 // a modeline).
1465 copy_option_part(&dict, buf, LSIZE, ",");
1466# ifdef FEAT_SPELL
1467 if (!thesaurus && STRCMP(buf, "spell") == 0)
1468 count = -1;
1469 else
1470# endif
1471 if (vim_strchr(buf, '`') != NULL
1472 || expand_wildcards(1, &buf, &count, &files,
1473 EW_FILE|EW_SILENT) != OK)
1474 count = 0;
1475 }
1476
1477# ifdef FEAT_SPELL
1478 if (count == -1)
1479 {
1480 // Complete from active spelling. Skip "\<" in the pattern, we
1481 // don't use it as a RE.
1482 if (pat[0] == '\\' && pat[1] == '<')
1483 ptr = pat + 2;
1484 else
1485 ptr = pat;
1486 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1487 }
1488 else
1489# endif
1490 if (count > 0) // avoid warning for using "files" uninit
1491 {
1492 ins_compl_files(count, files, thesaurus, flags,
1493 &regmatch, buf, &dir);
1494 if (flags != DICT_EXACT)
1495 FreeWild(count, files);
1496 }
1497 if (flags != 0)
1498 break;
1499 }
1500
1501theend:
1502 p_scs = save_p_scs;
1503 vim_regfree(regmatch.regprog);
1504 vim_free(buf);
1505}
1506
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001507/*
1508 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1509 * skipping the word at 'skip_word'. Returns OK on success.
1510 */
1511 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001512thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001513 char_u *fname,
1514 char_u **buf_arg,
1515 int dir,
1516 char_u *skip_word)
1517{
1518 int status = OK;
1519 char_u *ptr;
1520 char_u *wstart;
1521
1522 // Add the other matches on the line
1523 ptr = *buf_arg;
1524 while (!got_int)
1525 {
1526 // Find start of the next word. Skip white
1527 // space and punctuation.
1528 ptr = find_word_start(ptr);
1529 if (*ptr == NUL || *ptr == NL)
1530 break;
1531 wstart = ptr;
1532
1533 // Find end of the word.
1534 if (has_mbyte)
1535 // Japanese words may have characters in
1536 // different classes, only separate words
1537 // with single-byte non-word characters.
1538 while (*ptr != NUL)
1539 {
1540 int l = (*mb_ptr2len)(ptr);
1541
1542 if (l < 2 && !vim_iswordc(*ptr))
1543 break;
1544 ptr += l;
1545 }
1546 else
1547 ptr = find_word_end(ptr);
1548
1549 // Add the word. Skip the regexp match.
1550 if (wstart != skip_word)
1551 {
1552 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1553 fname, dir, FALSE);
1554 if (status == FAIL)
1555 break;
1556 }
1557 }
1558
1559 *buf_arg = ptr;
1560 return status;
1561}
1562
1563/*
1564 * Process "count" dictionary/thesaurus "files" and add the text matching
1565 * "regmatch".
1566 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001567 static void
1568ins_compl_files(
1569 int count,
1570 char_u **files,
1571 int thesaurus,
1572 int flags,
1573 regmatch_T *regmatch,
1574 char_u *buf,
1575 int *dir)
1576{
1577 char_u *ptr;
1578 int i;
1579 FILE *fp;
1580 int add_r;
1581
1582 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1583 {
1584 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001585 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001586 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001587 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001588 vim_snprintf((char *)IObuff, IOSIZE,
1589 _("Scanning dictionary: %s"), (char *)files[i]);
1590 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1591 }
1592
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001593 if (fp == NULL)
1594 continue;
1595
1596 // Read dictionary file line by line.
1597 // Check each line for a match.
1598 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001599 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001600 ptr = buf;
1601 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001602 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001603 ptr = regmatch->startp[0];
1604 if (ctrl_x_mode_line_or_eval())
1605 ptr = find_line_end(ptr);
1606 else
1607 ptr = find_word_end(ptr);
1608 add_r = ins_compl_add_infercase(regmatch->startp[0],
1609 (int)(ptr - regmatch->startp[0]),
1610 p_ic, files[i], *dir, FALSE);
1611 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001612 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001613 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001614 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001615 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001616 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001617 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001618 if (add_r == OK)
1619 // if dir was BACKWARD then honor it just once
1620 *dir = FORWARD;
1621 else if (add_r == FAIL)
1622 break;
1623 // avoid expensive call to vim_regexec() when at end
1624 // of line
1625 if (*ptr == '\n' || got_int)
1626 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001627 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001628 line_breakcheck();
1629 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001630 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001631 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001632 }
1633}
1634
1635/*
1636 * Find the start of the next word.
1637 * Returns a pointer to the first char of the word. Also stops at a NUL.
1638 */
1639 char_u *
1640find_word_start(char_u *ptr)
1641{
1642 if (has_mbyte)
1643 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1644 ptr += (*mb_ptr2len)(ptr);
1645 else
1646 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1647 ++ptr;
1648 return ptr;
1649}
1650
1651/*
1652 * Find the end of the word. Assumes it starts inside a word.
1653 * Returns a pointer to just after the word.
1654 */
1655 char_u *
1656find_word_end(char_u *ptr)
1657{
1658 int start_class;
1659
1660 if (has_mbyte)
1661 {
1662 start_class = mb_get_class(ptr);
1663 if (start_class > 1)
1664 while (*ptr != NUL)
1665 {
1666 ptr += (*mb_ptr2len)(ptr);
1667 if (mb_get_class(ptr) != start_class)
1668 break;
1669 }
1670 }
1671 else
1672 while (vim_iswordc(*ptr))
1673 ++ptr;
1674 return ptr;
1675}
1676
1677/*
1678 * Find the end of the line, omitting CR and NL at the end.
1679 * Returns a pointer to just after the line.
1680 */
1681 static char_u *
1682find_line_end(char_u *ptr)
1683{
1684 char_u *s;
1685
1686 s = ptr + STRLEN(ptr);
1687 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1688 --s;
1689 return s;
1690}
1691
1692/*
1693 * Free the list of completions
1694 */
1695 static void
1696ins_compl_free(void)
1697{
1698 compl_T *match;
1699 int i;
1700
1701 VIM_CLEAR(compl_pattern);
1702 VIM_CLEAR(compl_leader);
1703
1704 if (compl_first_match == NULL)
1705 return;
1706
1707 ins_compl_del_pum();
1708 pum_clear();
1709
1710 compl_curr_match = compl_first_match;
1711 do
1712 {
1713 match = compl_curr_match;
1714 compl_curr_match = compl_curr_match->cp_next;
1715 vim_free(match->cp_str);
1716 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001717 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001718 vim_free(match->cp_fname);
1719 for (i = 0; i < CPT_COUNT; ++i)
1720 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001721#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001722 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001723#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001724 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001725 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001726 compl_first_match = compl_curr_match = NULL;
1727 compl_shown_match = NULL;
1728 compl_old_match = NULL;
1729}
1730
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001731/*
1732 * Reset/clear the completion state.
1733 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001734 void
1735ins_compl_clear(void)
1736{
1737 compl_cont_status = 0;
1738 compl_started = FALSE;
1739 compl_matches = 0;
1740 VIM_CLEAR(compl_pattern);
1741 VIM_CLEAR(compl_leader);
1742 edit_submode_extra = NULL;
1743 VIM_CLEAR(compl_orig_text);
1744 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001745#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001746 // clear v:completed_item
1747 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001748#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001749}
1750
1751/*
1752 * Return TRUE when Insert completion is active.
1753 */
1754 int
1755ins_compl_active(void)
1756{
1757 return compl_started;
1758}
1759
1760/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001761 * Selected one of the matches. When FALSE the match was edited or using the
1762 * longest common string.
1763 */
1764 int
1765ins_compl_used_match(void)
1766{
1767 return compl_used_match;
1768}
1769
1770/*
1771 * Initialize get longest common string.
1772 */
1773 void
1774ins_compl_init_get_longest(void)
1775{
1776 compl_get_longest = FALSE;
1777}
1778
1779/*
1780 * Returns TRUE when insert completion is interrupted.
1781 */
1782 int
1783ins_compl_interrupted(void)
1784{
1785 return compl_interrupted;
1786}
1787
1788/*
1789 * Returns TRUE if the <Enter> key selects a match in the completion popup
1790 * menu.
1791 */
1792 int
1793ins_compl_enter_selects(void)
1794{
1795 return compl_enter_selects;
1796}
1797
1798/*
1799 * Return the column where the text starts that is being completed
1800 */
1801 colnr_T
1802ins_compl_col(void)
1803{
1804 return compl_col;
1805}
1806
1807/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001808 * Return the length in bytes of the text being completed
1809 */
1810 int
1811ins_compl_len(void)
1812{
1813 return compl_length;
1814}
1815
1816/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001817 * Delete one character before the cursor and show the subset of the matches
1818 * that match the word that is now before the cursor.
1819 * Returns the character to be used, NUL if the work is done and another char
1820 * to be got from the user.
1821 */
1822 int
1823ins_compl_bs(void)
1824{
1825 char_u *line;
1826 char_u *p;
1827
1828 line = ml_get_curline();
1829 p = line + curwin->w_cursor.col;
1830 MB_PTR_BACK(line, p);
1831
1832 // Stop completion when the whole word was deleted. For Omni completion
1833 // allow the word to be deleted, we won't match everything.
1834 // Respect the 'backspace' option.
1835 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001836 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1837 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001838 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1839 - compl_length < 0))
1840 return K_BS;
1841
1842 // Deleted more than what was used to find matches or didn't finish
1843 // finding all matches: need to look for matches all over again.
1844 if (curwin->w_cursor.col <= compl_col + compl_length
1845 || ins_compl_need_restart())
1846 ins_compl_restart();
1847
1848 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001849 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001850 if (compl_leader == NULL)
1851 return K_BS;
1852
1853 ins_compl_new_leader();
1854 if (compl_shown_match != NULL)
1855 // Make sure current match is not a hidden item.
1856 compl_curr_match = compl_shown_match;
1857 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001858}
1859
1860/*
1861 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1862 * be called.
1863 */
1864 static int
1865ins_compl_need_restart(void)
1866{
1867 // Return TRUE if we didn't complete finding matches or when the
1868 // 'completefunc' returned "always" in the "refresh" dictionary item.
1869 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001870 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001871 && compl_opt_refresh_always);
1872}
1873
1874/*
1875 * Called after changing "compl_leader".
1876 * Show the popup menu with a different set of matches.
1877 * May also search for matches again if the previous search was interrupted.
1878 */
1879 static void
1880ins_compl_new_leader(void)
1881{
1882 ins_compl_del_pum();
1883 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001884 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001885 compl_used_match = FALSE;
1886
1887 if (compl_started)
1888 ins_compl_set_original_text(compl_leader);
1889 else
1890 {
1891#ifdef FEAT_SPELL
1892 spell_bad_len = 0; // need to redetect bad word
1893#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001894 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001895 // the popup menu display the changed text before the cursor. Set
1896 // "compl_restarting" to avoid that the first match is inserted.
1897 pum_call_update_screen();
1898#ifdef FEAT_GUI
1899 if (gui.in_use)
1900 {
1901 // Show the cursor after the match, not after the redrawn text.
1902 setcursor();
1903 out_flush_cursor(FALSE, FALSE);
1904 }
1905#endif
1906 compl_restarting = TRUE;
1907 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1908 compl_cont_status = 0;
1909 compl_restarting = FALSE;
1910 }
1911
1912 compl_enter_selects = !compl_used_match;
1913
1914 // Show the popup menu with a different set of matches.
1915 ins_compl_show_pum();
1916
1917 // Don't let Enter select the original text when there is no popup menu.
1918 if (compl_match_array == NULL)
1919 compl_enter_selects = FALSE;
1920}
1921
1922/*
1923 * Return the length of the completion, from the completion start column to
1924 * the cursor column. Making sure it never goes below zero.
1925 */
1926 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001927get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001928{
1929 int off = (int)curwin->w_cursor.col - (int)compl_col;
1930
1931 if (off < 0)
1932 return 0;
1933 return off;
1934}
1935
1936/*
1937 * Append one character to the match leader. May reduce the number of
1938 * matches.
1939 */
1940 void
1941ins_compl_addleader(int c)
1942{
1943 int cc;
1944
1945 if (stop_arrow() == FAIL)
1946 return;
1947 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1948 {
1949 char_u buf[MB_MAXBYTES + 1];
1950
1951 (*mb_char2bytes)(c, buf);
1952 buf[cc] = NUL;
1953 ins_char_bytes(buf, cc);
1954 if (compl_opt_refresh_always)
1955 AppendToRedobuff(buf);
1956 }
1957 else
1958 {
1959 ins_char(c);
1960 if (compl_opt_refresh_always)
1961 AppendCharToRedobuff(c);
1962 }
1963
1964 // If we didn't complete finding matches we must search again.
1965 if (ins_compl_need_restart())
1966 ins_compl_restart();
1967
1968 // When 'always' is set, don't reset compl_leader. While completing,
1969 // cursor doesn't point original position, changing compl_leader would
1970 // break redo.
1971 if (!compl_opt_refresh_always)
1972 {
1973 vim_free(compl_leader);
1974 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001975 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001976 if (compl_leader != NULL)
1977 ins_compl_new_leader();
1978 }
1979}
1980
1981/*
1982 * Setup for finding completions again without leaving CTRL-X mode. Used when
1983 * BS or a key was typed while still searching for matches.
1984 */
1985 static void
1986ins_compl_restart(void)
1987{
1988 ins_compl_free();
1989 compl_started = FALSE;
1990 compl_matches = 0;
1991 compl_cont_status = 0;
1992 compl_cont_mode = 0;
1993}
1994
1995/*
1996 * Set the first match, the original text.
1997 */
1998 static void
1999ins_compl_set_original_text(char_u *str)
2000{
2001 char_u *p;
2002
2003 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002004 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2005 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002006 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002007 {
2008 p = vim_strsave(str);
2009 if (p != NULL)
2010 {
2011 vim_free(compl_first_match->cp_str);
2012 compl_first_match->cp_str = p;
2013 }
2014 }
2015 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002016 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002017 {
2018 p = vim_strsave(str);
2019 if (p != NULL)
2020 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002021 vim_free(compl_first_match->cp_prev->cp_str);
2022 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 }
2024 }
2025}
2026
2027/*
2028 * Append one character to the match leader. May reduce the number of
2029 * matches.
2030 */
2031 void
2032ins_compl_addfrommatch(void)
2033{
2034 char_u *p;
2035 int len = (int)curwin->w_cursor.col - (int)compl_col;
2036 int c;
2037 compl_T *cp;
2038
2039 p = compl_shown_match->cp_str;
2040 if ((int)STRLEN(p) <= len) // the match is too short
2041 {
2042 // When still at the original match use the first entry that matches
2043 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002044 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002045 return;
2046
2047 p = NULL;
2048 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002049 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002050 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002051 if (compl_leader == NULL
2052 || ins_compl_equal(cp, compl_leader,
2053 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002054 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002055 p = cp->cp_str;
2056 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002057 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002058 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002059 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002060 return;
2061 }
2062 p += len;
2063 c = PTR2CHAR(p);
2064 ins_compl_addleader(c);
2065}
2066
2067/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002068 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002069 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2070 * compl_cont_mode and compl_cont_status.
2071 * Returns TRUE when the character is not to be inserted.
2072 */
2073 static int
2074set_ctrl_x_mode(int c)
2075{
2076 int retval = FALSE;
2077
2078 switch (c)
2079 {
2080 case Ctrl_E:
2081 case Ctrl_Y:
2082 // scroll the window one line up or down
2083 ctrl_x_mode = CTRL_X_SCROLL;
2084 if (!(State & REPLACE_FLAG))
2085 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2086 else
2087 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2088 edit_submode_pre = NULL;
2089 showmode();
2090 break;
2091 case Ctrl_L:
2092 // complete whole line
2093 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2094 break;
2095 case Ctrl_F:
2096 // complete filenames
2097 ctrl_x_mode = CTRL_X_FILES;
2098 break;
2099 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002100 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002101 ctrl_x_mode = CTRL_X_DICTIONARY;
2102 break;
2103 case Ctrl_R:
2104 // Register insertion without exiting CTRL-X mode
2105 // Simply allow ^R to happen without affecting ^X mode
2106 break;
2107 case Ctrl_T:
2108 // complete words from a thesaurus
2109 ctrl_x_mode = CTRL_X_THESAURUS;
2110 break;
2111#ifdef FEAT_COMPL_FUNC
2112 case Ctrl_U:
2113 // user defined completion
2114 ctrl_x_mode = CTRL_X_FUNCTION;
2115 break;
2116 case Ctrl_O:
2117 // omni completion
2118 ctrl_x_mode = CTRL_X_OMNI;
2119 break;
2120#endif
2121 case 's':
2122 case Ctrl_S:
2123 // complete spelling suggestions
2124 ctrl_x_mode = CTRL_X_SPELL;
2125#ifdef FEAT_SPELL
2126 ++emsg_off; // Avoid getting the E756 error twice.
2127 spell_back_to_badword();
2128 --emsg_off;
2129#endif
2130 break;
2131 case Ctrl_RSB:
2132 // complete tag names
2133 ctrl_x_mode = CTRL_X_TAGS;
2134 break;
2135#ifdef FEAT_FIND_ID
2136 case Ctrl_I:
2137 case K_S_TAB:
2138 // complete keywords from included files
2139 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2140 break;
2141 case Ctrl_D:
2142 // complete definitions from included files
2143 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2144 break;
2145#endif
2146 case Ctrl_V:
2147 case Ctrl_Q:
2148 // complete vim commands
2149 ctrl_x_mode = CTRL_X_CMDLINE;
2150 break;
2151 case Ctrl_Z:
2152 // stop completion
2153 ctrl_x_mode = CTRL_X_NORMAL;
2154 edit_submode = NULL;
2155 showmode();
2156 retval = TRUE;
2157 break;
2158 case Ctrl_P:
2159 case Ctrl_N:
2160 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2161 // just started ^X mode, or there were enough ^X's to cancel
2162 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2163 // do normal expansion when interrupting a different mode (say
2164 // ^X^F^X^P or ^P^X^X^P, see below)
2165 // nothing changes if interrupting mode 0, (eg, the flag
2166 // doesn't change when going to ADDING mode -- Acevedo
2167 if (!(compl_cont_status & CONT_INTRPT))
2168 compl_cont_status |= CONT_LOCAL;
2169 else if (compl_cont_mode != 0)
2170 compl_cont_status &= ~CONT_LOCAL;
2171 // FALLTHROUGH
2172 default:
2173 // If we have typed at least 2 ^X's... for modes != 0, we set
2174 // compl_cont_status = 0 (eg, as if we had just started ^X
2175 // mode).
2176 // For mode 0, we set "compl_cont_mode" to an impossible
2177 // value, in both cases ^X^X can be used to restart the same
2178 // mode (avoiding ADDING mode).
2179 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2180 // 'complete' and local ^P expansions respectively.
2181 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2182 // mode -- Acevedo
2183 if (c == Ctrl_X)
2184 {
2185 if (compl_cont_mode != 0)
2186 compl_cont_status = 0;
2187 else
2188 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2189 }
2190 ctrl_x_mode = CTRL_X_NORMAL;
2191 edit_submode = NULL;
2192 showmode();
2193 break;
2194 }
2195
2196 return retval;
2197}
2198
2199/*
2200 * Stop insert completion mode
2201 */
2202 static int
2203ins_compl_stop(int c, int prev_mode, int retval)
2204{
2205 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002206 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002207
2208 // Get here when we have finished typing a sequence of ^N and
2209 // ^P or other completion characters in CTRL-X mode. Free up
2210 // memory that was used, and make sure we can redo the insert.
2211 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2212 {
2213 // If any of the original typed text has been changed, eg when
2214 // ignorecase is set, we must add back-spaces to the redo
2215 // buffer. We add as few as necessary to delete just the part
2216 // of the original text that has changed.
2217 // When using the longest match, edited the match or used
2218 // CTRL-E then don't use the current match.
2219 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2220 ptr = compl_curr_match->cp_str;
2221 else
2222 ptr = NULL;
2223 ins_compl_fixRedoBufForLeader(ptr);
2224 }
2225
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002226 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002227
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002228 // When completing whole lines: fix indent for 'cindent'.
2229 // Otherwise, break line if it's too long.
2230 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2231 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002232 // re-indent the current line
2233 if (want_cindent)
2234 {
2235 do_c_expr_indent();
2236 want_cindent = FALSE; // don't do it again
2237 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002238 }
2239 else
2240 {
2241 int prev_col = curwin->w_cursor.col;
2242
2243 // put the cursor on the last char, for 'tw' formatting
2244 if (prev_col > 0)
2245 dec_cursor();
2246 // only format when something was inserted
2247 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2248 insertchar(NUL, 0, -1);
2249 if (prev_col > 0
2250 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2251 inc_cursor();
2252 }
2253
2254 // If the popup menu is displayed pressing CTRL-Y means accepting
2255 // the selection without inserting anything. When
2256 // compl_enter_selects is set the Enter key does the same.
2257 if ((c == Ctrl_Y || (compl_enter_selects
2258 && (c == CAR || c == K_KENTER || c == NL)))
2259 && pum_visible())
2260 retval = TRUE;
2261
2262 // CTRL-E means completion is Ended, go back to the typed text.
2263 // but only do this, if the Popup is still visible
2264 if (c == Ctrl_E)
2265 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002266 char_u *p = NULL;
2267
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002268 ins_compl_delete();
2269 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002270 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002271 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002272 p = compl_orig_text;
2273 if (p != NULL)
2274 {
2275 int compl_len = get_compl_len();
2276 int len = (int)STRLEN(p);
2277
2278 if (len > compl_len)
2279 ins_bytes_len(p + compl_len, len - compl_len);
2280 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002281 retval = TRUE;
2282 }
2283
2284 auto_format(FALSE, TRUE);
2285
2286 // Trigger the CompleteDonePre event to give scripts a chance to
2287 // act upon the completion before clearing the info, and restore
2288 // ctrl_x_mode, so that complete_info() can be used.
2289 ctrl_x_mode = prev_mode;
2290 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2291
2292 ins_compl_free();
2293 compl_started = FALSE;
2294 compl_matches = 0;
2295 if (!shortmess(SHM_COMPLETIONMENU))
2296 msg_clr_cmdline(); // necessary for "noshowmode"
2297 ctrl_x_mode = CTRL_X_NORMAL;
2298 compl_enter_selects = FALSE;
2299 if (edit_submode != NULL)
2300 {
2301 edit_submode = NULL;
2302 showmode();
2303 }
2304
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002305 if (c == Ctrl_C && cmdwin_type != 0)
2306 // Avoid the popup menu remains displayed when leaving the
2307 // command line window.
2308 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002309 // Indent now if a key was typed that is in 'cinkeys'.
2310 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2311 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002312 // Trigger the CompleteDone event to give scripts a chance to act
2313 // upon the end of completion.
2314 ins_apply_autocmds(EVENT_COMPLETEDONE);
2315
2316 return retval;
2317}
2318
2319/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002320 * Prepare for Insert mode completion, or stop it.
2321 * Called just after typing a character in Insert mode.
2322 * Returns TRUE when the character is not to be inserted;
2323 */
2324 int
2325ins_compl_prep(int c)
2326{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002327 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002328 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002329
2330 // Forget any previous 'special' messages if this is actually
2331 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2332 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2333 edit_submode_extra = NULL;
2334
zeertzjq440d4cb2023-03-02 17:51:32 +00002335 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002336 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002337 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002338 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002339 return retval;
2340
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002341#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002342 // Ignore mouse events in a popup window
2343 if (is_mouse_key(c))
2344 {
2345 // Ignore drag and release events, the position does not need to be in
2346 // the popup and it may have just closed.
2347 if (c == K_LEFTRELEASE
2348 || c == K_LEFTRELEASE_NM
2349 || c == K_MIDDLERELEASE
2350 || c == K_RIGHTRELEASE
2351 || c == K_X1RELEASE
2352 || c == K_X2RELEASE
2353 || c == K_LEFTDRAG
2354 || c == K_MIDDLEDRAG
2355 || c == K_RIGHTDRAG
2356 || c == K_X1DRAG
2357 || c == K_X2DRAG)
2358 return retval;
2359 if (popup_visible)
2360 {
2361 int row = mouse_row;
2362 int col = mouse_col;
2363 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2364
2365 if (wp != NULL && WIN_IS_POPUP(wp))
2366 return retval;
2367 }
2368 }
2369#endif
2370
zeertzjqdca29d92021-08-31 19:12:51 +02002371 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2372 {
2373 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2374 || !vim_is_ctrl_x_key(c))
2375 {
2376 // Not starting another completion mode.
2377 ctrl_x_mode = CTRL_X_CMDLINE;
2378
2379 // CTRL-X CTRL-Z should stop completion without inserting anything
2380 if (c == Ctrl_Z)
2381 retval = TRUE;
2382 }
2383 else
2384 {
2385 ctrl_x_mode = CTRL_X_CMDLINE;
2386
2387 // Other CTRL-X keys first stop completion, then start another
2388 // completion mode.
2389 ins_compl_prep(' ');
2390 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2391 }
2392 }
2393
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002394 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002395 if (ctrl_x_mode_not_defined_yet()
2396 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002397 {
bfredl87af60c2022-09-24 11:17:51 +01002398 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002399 compl_used_match = TRUE;
2400
2401 }
2402
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002403 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002404 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2405 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002406 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002407 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002408 {
2409 // We're already in CTRL-X mode, do we stay in it?
2410 if (!vim_is_ctrl_x_key(c))
2411 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002412 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002413 ctrl_x_mode = CTRL_X_NORMAL;
2414 else
2415 ctrl_x_mode = CTRL_X_FINISHED;
2416 edit_submode = NULL;
2417 }
2418 showmode();
2419 }
2420
2421 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2422 {
2423 // Show error message from attempted keyword completion (probably
2424 // 'Pattern not found') until another key is hit, then go back to
2425 // showing what mode we are in.
2426 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002427 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002428 && c != Ctrl_R && !ins_compl_pum_key(c))
2429 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002430 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002431 }
2432 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2433 // Trigger the CompleteDone event to give scripts a chance to act
2434 // upon the (possibly failed) completion.
2435 ins_apply_autocmds(EVENT_COMPLETEDONE);
2436
LemonBoy2bf52dd2022-04-09 18:17:34 +01002437 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002438
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002439 // reset continue_* if we left expansion-mode, if we stay they'll be
2440 // (re)set properly in ins_complete()
2441 if (!vim_is_ctrl_x_key(c))
2442 {
2443 compl_cont_status = 0;
2444 compl_cont_mode = 0;
2445 }
2446
2447 return retval;
2448}
2449
2450/*
2451 * Fix the redo buffer for the completion leader replacing some of the typed
2452 * text. This inserts backspaces and appends the changed text.
2453 * "ptr" is the known leader text or NUL.
2454 */
2455 static void
2456ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2457{
2458 int len;
2459 char_u *p;
2460 char_u *ptr = ptr_arg;
2461
2462 if (ptr == NULL)
2463 {
2464 if (compl_leader != NULL)
2465 ptr = compl_leader;
2466 else
2467 return; // nothing to do
2468 }
2469 if (compl_orig_text != NULL)
2470 {
2471 p = compl_orig_text;
2472 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2473 ;
2474 if (len > 0)
2475 len -= (*mb_head_off)(p, p + len);
2476 for (p += len; *p != NUL; MB_PTR_ADV(p))
2477 AppendCharToRedobuff(K_BS);
2478 }
2479 else
2480 len = 0;
2481 if (ptr != NULL)
2482 AppendToRedobuffLit(ptr + len, -1);
2483}
2484
2485/*
2486 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2487 * (depending on flag) starting from buf and looking for a non-scanned
2488 * buffer (other than curbuf). curbuf is special, if it is called with
2489 * buf=curbuf then it has to be the first call for a given flag/expansion.
2490 *
2491 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2492 */
2493 static buf_T *
2494ins_compl_next_buf(buf_T *buf, int flag)
2495{
2496 static win_T *wp = NULL;
2497
2498 if (flag == 'w') // just windows
2499 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002500 if (buf == curbuf || !win_valid(wp))
2501 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002502 wp = curwin;
2503 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2504 && wp->w_buffer->b_scanned)
2505 ;
2506 buf = wp->w_buffer;
2507 }
2508 else
2509 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2510 // (unlisted buffers)
2511 // When completing whole lines skip unloaded buffers.
2512 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2513 && ((flag == 'U'
2514 ? buf->b_p_bl
2515 : (!buf->b_p_bl
2516 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2517 || buf->b_scanned))
2518 ;
2519 return buf;
2520}
2521
2522#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002523
2524# ifdef FEAT_EVAL
2525static callback_T cfu_cb; // 'completefunc' callback function
2526static callback_T ofu_cb; // 'omnifunc' callback function
2527static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2528# endif
2529
2530/*
2531 * Copy a global callback function to a buffer local callback.
2532 */
2533 static void
2534copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2535{
2536 free_callback(bufcb);
2537 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2538 copy_callback(bufcb, globcb);
2539}
2540
2541/*
2542 * Parse the 'completefunc' option value and set the callback function.
2543 * Invoked when the 'completefunc' option is set. The option value can be a
2544 * name of a function (string), or function(<name>) or funcref(<name>) or a
2545 * lambda expression.
2546 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002547 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002548did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002549{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002550 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2551 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002552
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002553 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002554
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002555 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002556}
2557
2558/*
2559 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002560 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002561 */
2562 void
2563set_buflocal_cfu_callback(buf_T *buf UNUSED)
2564{
2565# ifdef FEAT_EVAL
2566 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2567# endif
2568}
2569
2570/*
2571 * Parse the 'omnifunc' option value and set the callback function.
2572 * Invoked when the 'omnifunc' option is set. The option value can be a
2573 * name of a function (string), or function(<name>) or funcref(<name>) or a
2574 * lambda expression.
2575 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002576 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002577did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002578{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002579 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2580 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002581
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002582 set_buflocal_ofu_callback(curbuf);
2583 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002584}
2585
2586/*
2587 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002588 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002589 */
2590 void
2591set_buflocal_ofu_callback(buf_T *buf UNUSED)
2592{
2593# ifdef FEAT_EVAL
2594 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2595# endif
2596}
2597
2598/*
2599 * Parse the 'thesaurusfunc' option value and set the callback function.
2600 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2601 * name of a function (string), or function(<name>) or funcref(<name>) or a
2602 * lambda expression.
2603 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002604 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002605did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002606{
2607 int retval;
2608
2609 if (*curbuf->b_p_tsrfu != NUL)
2610 {
2611 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002612 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2613 &curbuf->b_tsrfu_cb);
2614 }
2615 else
2616 {
2617 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002618 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2619 }
2620
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002622}
2623
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002624/*
2625 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002626 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002627 */
2628 int
2629set_ref_in_insexpand_funcs(int copyID)
2630{
2631 int abort = FALSE;
2632
2633 abort = set_ref_in_callback(&cfu_cb, copyID);
2634 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2635 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2636
2637 return abort;
2638}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002639
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002640/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002641 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002642 */
2643 static char_u *
2644get_complete_funcname(int type)
2645{
2646 switch (type)
2647 {
2648 case CTRL_X_FUNCTION:
2649 return curbuf->b_p_cfu;
2650 case CTRL_X_OMNI:
2651 return curbuf->b_p_ofu;
2652 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002653 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002654 default:
2655 return (char_u *)"";
2656 }
2657}
2658
2659/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002660 * Get the callback to use for insert mode completion.
2661 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002662 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002663get_insert_callback(int type)
2664{
2665 if (type == CTRL_X_FUNCTION)
2666 return &curbuf->b_cfu_cb;
2667 if (type == CTRL_X_OMNI)
2668 return &curbuf->b_ofu_cb;
2669 // CTRL_X_THESAURUS
2670 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2671}
2672
2673/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002674 * Execute user defined complete function 'completefunc', 'omnifunc' or
2675 * 'thesaurusfunc', and get matches in "matches".
2676 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002677 */
2678 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002679expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002680{
2681 list_T *matchlist = NULL;
2682 dict_T *matchdict = NULL;
2683 typval_T args[3];
2684 char_u *funcname;
2685 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002686 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002687 typval_T rettv;
2688 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002689 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002690
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002691 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002692 if (*funcname == NUL)
2693 return;
2694
2695 // Call 'completefunc' to obtain the list of matches.
2696 args[0].v_type = VAR_NUMBER;
2697 args[0].vval.v_number = 0;
2698 args[1].v_type = VAR_STRING;
2699 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2700 args[2].v_type = VAR_UNKNOWN;
2701
2702 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002703 // Lock the text to avoid weird things from happening. Also disallow
2704 // switching to another window, it should not be needed and may end up in
2705 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002706 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002707
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002708 cb = get_insert_callback(type);
2709 retval = call_callback(cb, 0, &rettv, 2, args);
2710
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002711 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002712 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002713 {
2714 switch (rettv.v_type)
2715 {
2716 case VAR_LIST:
2717 matchlist = rettv.vval.v_list;
2718 break;
2719 case VAR_DICT:
2720 matchdict = rettv.vval.v_dict;
2721 break;
2722 case VAR_SPECIAL:
2723 if (rettv.vval.v_number == VVAL_NONE)
2724 compl_opt_suppress_empty = TRUE;
2725 // FALLTHROUGH
2726 default:
2727 // TODO: Give error message?
2728 clear_tv(&rettv);
2729 break;
2730 }
2731 }
zeertzjqcfe45652022-05-27 17:26:55 +01002732 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002733
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002734 curwin->w_cursor = pos; // restore the cursor position
2735 validate_cursor();
2736 if (!EQUAL_POS(curwin->w_cursor, pos))
2737 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002738 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002739 goto theend;
2740 }
2741
2742 if (matchlist != NULL)
2743 ins_compl_add_list(matchlist);
2744 else if (matchdict != NULL)
2745 ins_compl_add_dict(matchdict);
2746
2747theend:
2748 // Restore State, it might have been changed.
2749 State = save_State;
2750
2751 if (matchdict != NULL)
2752 dict_unref(matchdict);
2753 if (matchlist != NULL)
2754 list_unref(matchlist);
2755}
2756#endif // FEAT_COMPL_FUNC
2757
2758#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2759/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002760 * Add a match to the list of matches from a typeval_T.
2761 * If the given string is already in the list of completions, then return
2762 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2763 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002764 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002765 */
2766 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002767ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002768{
2769 char_u *word;
2770 int dup = FALSE;
2771 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002772 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002773 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002774 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002775 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002776
Bram Moolenaar08928322020-01-04 14:32:48 +01002777 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002778 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2779 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002780 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2781 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2782 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2783 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2784 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2785 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2786 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2787 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002788 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002789 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2790 dup = dict_get_number(tv->vval.v_dict, "dup");
2791 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2792 empty = dict_get_number(tv->vval.v_dict, "empty");
2793 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2794 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002795 flags |= CP_EQUAL;
2796 }
2797 else
2798 {
2799 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002800 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002801 }
2802 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002803 {
2804 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002805 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002806 }
2807 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2808 if (status != OK)
2809 clear_tv(&user_data);
2810 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002811}
2812
2813/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002814 * Add completions from a list.
2815 */
2816 static void
2817ins_compl_add_list(list_T *list)
2818{
2819 listitem_T *li;
2820 int dir = compl_direction;
2821
2822 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002823 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002824 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002825 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002826 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002827 // if dir was BACKWARD then honor it just once
2828 dir = FORWARD;
2829 else if (did_emsg)
2830 break;
2831 }
2832}
2833
2834/*
2835 * Add completions from a dict.
2836 */
2837 static void
2838ins_compl_add_dict(dict_T *dict)
2839{
2840 dictitem_T *di_refresh;
2841 dictitem_T *di_words;
2842
2843 // Check for optional "refresh" item.
2844 compl_opt_refresh_always = FALSE;
2845 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2846 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2847 {
2848 char_u *v = di_refresh->di_tv.vval.v_string;
2849
2850 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2851 compl_opt_refresh_always = TRUE;
2852 }
2853
2854 // Add completions from a "words" list.
2855 di_words = dict_find(dict, (char_u *)"words", 5);
2856 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2857 ins_compl_add_list(di_words->di_tv.vval.v_list);
2858}
2859
2860/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002861 * Start completion for the complete() function.
2862 * "startcol" is where the matched text starts (1 is first column).
2863 * "list" is the list of matches.
2864 */
2865 static void
2866set_completion(colnr_T startcol, list_T *list)
2867{
2868 int save_w_wrow = curwin->w_wrow;
2869 int save_w_leftcol = curwin->w_leftcol;
2870 int flags = CP_ORIGINAL_TEXT;
2871
2872 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002873 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002874 ins_compl_prep(' ');
2875 ins_compl_clear();
2876 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002877 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002878
2879 compl_direction = FORWARD;
2880 if (startcol > curwin->w_cursor.col)
2881 startcol = curwin->w_cursor.col;
2882 compl_col = startcol;
2883 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2884 // compl_pattern doesn't need to be set
2885 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2886 if (p_ic)
2887 flags |= CP_ICASE;
2888 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002889 -1, NULL, NULL, NULL, 0,
2890 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002891 return;
2892
2893 ctrl_x_mode = CTRL_X_EVAL;
2894
2895 ins_compl_add_list(list);
2896 compl_matches = ins_compl_make_cyclic();
2897 compl_started = TRUE;
2898 compl_used_match = TRUE;
2899 compl_cont_status = 0;
2900
2901 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002902 int no_select = compl_no_select || compl_longest;
2903 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002904 {
2905 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002906 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002907 // Down/Up has no real effect.
2908 ins_complete(K_UP, FALSE);
2909 }
2910 else
2911 ins_complete(Ctrl_N, FALSE);
2912 compl_enter_selects = compl_no_insert;
2913
2914 // Lazily show the popup menu, unless we got interrupted.
2915 if (!compl_interrupted)
2916 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002917 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002918 out_flush();
2919}
2920
2921/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002922 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002923 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002924 void
2925f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002926{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002927 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002928
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002929 if (in_vim9script()
2930 && (check_for_number_arg(argvars, 0) == FAIL
2931 || check_for_list_arg(argvars, 1) == FAIL))
2932 return;
2933
Bram Moolenaar24959102022-05-07 20:01:16 +01002934 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002935 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002936 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002937 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002938 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002939
2940 // Check for undo allowed here, because if something was already inserted
2941 // the line was already saved for undo and this check isn't done.
2942 if (!undo_allowed())
2943 return;
2944
Bram Moolenaard83392a2022-09-01 12:22:46 +01002945 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002946 {
2947 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2948 if (startcol > 0)
2949 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002950 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002951}
2952
2953/*
2954 * "complete_add()" function
2955 */
2956 void
2957f_complete_add(typval_T *argvars, typval_T *rettv)
2958{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002959 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002960 return;
2961
Bram Moolenaar440cf092021-04-03 20:13:30 +02002962 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002963}
2964
2965/*
2966 * "complete_check()" function
2967 */
2968 void
2969f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2970{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002971 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002972 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002973
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002974 ins_compl_check_keys(0, TRUE);
2975 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002976
2977 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002978}
2979
2980/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002981 * Return Insert completion mode name string
2982 */
2983 static char_u *
2984ins_compl_mode(void)
2985{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002986 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002987 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2988
2989 return (char_u *)"";
2990}
2991
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002992/*
2993 * Assign the sequence number to all the completion matches which don't have
2994 * one assigned yet.
2995 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002996 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002997ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002998{
2999 int number = 0;
3000 compl_T *match;
3001
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003002 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003003 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003004 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003005 // This should normally succeed already at the first loop
3006 // cycle, so it's fast!
3007 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003008 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003009 if (match->cp_number != -1)
3010 {
3011 number = match->cp_number;
3012 break;
3013 }
3014 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003015 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003016 for (match = match->cp_next;
3017 match != NULL && match->cp_number == -1;
3018 match = match->cp_next)
3019 match->cp_number = ++number;
3020 }
3021 else // BACKWARD
3022 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003023 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003024 // number. This should normally succeed already at the
3025 // first loop cycle, so it's fast!
3026 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003027 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003028 if (match->cp_number != -1)
3029 {
3030 number = match->cp_number;
3031 break;
3032 }
3033 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003034 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003035 for (match = match->cp_prev; match
3036 && match->cp_number == -1;
3037 match = match->cp_prev)
3038 match->cp_number = ++number;
3039 }
3040}
3041
LemonBoy69fb5af2023-10-11 21:55:56 +02003042 static int
3043info_add_completion_info(list_T *li)
3044{
3045 compl_T *match;
Christian Brabandtdaef8c72023-10-27 19:16:26 +02003046 int forward = compl_dir_forward();
LemonBoy69fb5af2023-10-11 21:55:56 +02003047
3048 if (compl_first_match == NULL)
3049 return OK;
3050
Christian Brabandtdaef8c72023-10-27 19:16:26 +02003051 match = compl_first_match;
3052 // There are four cases to consider here:
3053 // 1) when just going forward through the menu,
3054 // compl_first_match should point to the initial entry with
3055 // number zero and CP_ORIGINAL_TEXT flag set
3056 // 2) when just going backwards,
3057 // compl-first_match should point to the last entry before
3058 // the entry with the CP_ORIGINAL_TEXT flag set
3059 // 3) when first going forwards and then backwards, e.g.
3060 // pressing C-N, C-P, compl_first_match points to the
3061 // last entry before the entry with the CP_ORIGINAL_TEXT
3062 // flag set and next-entry moves opposite through the list
3063 // compared to case 2, so pretend the direction is forward again
3064 // 4) when first going backwards and then forwards, e.g.
3065 // pressing C-P, C-N, compl_first_match points to the
3066 // first entry with the CP_ORIGINAL_TEXT
3067 // flag set and next-entry moves in opposite direction through the list
3068 // compared to case 1, so pretend the direction is backwards again
3069 //
3070 // But only do this when the 'noselect' option is not active!
3071
3072 if (!compl_no_select)
3073 {
3074 if (forward && !match_at_original_text(match))
3075 forward = FALSE;
3076 else if (!forward && match_at_original_text(match))
3077 forward = TRUE;
3078 }
3079
LemonBoy69fb5af2023-10-11 21:55:56 +02003080 // Skip the element with the CP_ORIGINAL_TEXT flag at the beginning, in case of
3081 // forward completion, or at the end, in case of backward completion.
Christian Brabandtfef66302024-01-29 21:46:58 +01003082 match = forward || match->cp_prev == NULL ? match->cp_next :
3083 (compl_no_select && match_at_original_text(match) ? match->cp_prev : match->cp_prev->cp_prev);
Christian Brabandtdaef8c72023-10-27 19:16:26 +02003084
LemonBoy69fb5af2023-10-11 21:55:56 +02003085 while (match != NULL && !match_at_original_text(match))
3086 {
3087 dict_T *di = dict_alloc();
3088
3089 if (di == NULL)
3090 return FAIL;
3091 if (list_append_dict(li, di) == FAIL)
3092 return FAIL;
3093 dict_add_string(di, "word", match->cp_str);
3094 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3095 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3096 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3097 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3098 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3099 // Add an empty string for backwards compatibility
3100 dict_add_string(di, "user_data", (char_u *)"");
3101 else
3102 dict_add_tv(di, "user_data", &match->cp_user_data);
3103
Christian Brabandtdaef8c72023-10-27 19:16:26 +02003104 match = forward ? match->cp_next : match->cp_prev;
LemonBoy69fb5af2023-10-11 21:55:56 +02003105 }
3106
3107 return OK;
3108}
3109
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003110/*
3111 * Get complete information
3112 */
3113 static void
3114get_complete_info(list_T *what_list, dict_T *retdict)
3115{
3116 int ret = OK;
3117 listitem_T *item;
3118#define CI_WHAT_MODE 0x01
3119#define CI_WHAT_PUM_VISIBLE 0x02
3120#define CI_WHAT_ITEMS 0x04
3121#define CI_WHAT_SELECTED 0x08
3122#define CI_WHAT_INSERTED 0x10
3123#define CI_WHAT_ALL 0xff
3124 int what_flag;
3125
3126 if (what_list == NULL)
3127 what_flag = CI_WHAT_ALL;
3128 else
3129 {
3130 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003131 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003132 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003133 {
3134 char_u *what = tv_get_string(&item->li_tv);
3135
3136 if (STRCMP(what, "mode") == 0)
3137 what_flag |= CI_WHAT_MODE;
3138 else if (STRCMP(what, "pum_visible") == 0)
3139 what_flag |= CI_WHAT_PUM_VISIBLE;
3140 else if (STRCMP(what, "items") == 0)
3141 what_flag |= CI_WHAT_ITEMS;
3142 else if (STRCMP(what, "selected") == 0)
3143 what_flag |= CI_WHAT_SELECTED;
3144 else if (STRCMP(what, "inserted") == 0)
3145 what_flag |= CI_WHAT_INSERTED;
3146 }
3147 }
3148
3149 if (ret == OK && (what_flag & CI_WHAT_MODE))
3150 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3151
3152 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3153 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3154
3155 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3156 {
3157 list_T *li;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003158
3159 li = list_alloc();
3160 if (li == NULL)
3161 return;
3162 ret = dict_add_list(retdict, "items", li);
LemonBoy69fb5af2023-10-11 21:55:56 +02003163 if (ret == OK)
3164 ret = info_add_completion_info(li);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003165 }
3166
3167 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003168 {
3169 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3170 ins_compl_update_sequence_numbers();
3171 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3172 ? compl_curr_match->cp_number - 1 : -1);
3173 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003174
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003175 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3176 {
3177 // TODO
3178 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003179}
3180
3181/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003182 * "complete_info()" function
3183 */
3184 void
3185f_complete_info(typval_T *argvars, typval_T *rettv)
3186{
3187 list_T *what_list = NULL;
3188
Bram Moolenaar93a10962022-06-16 11:42:09 +01003189 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003190 return;
3191
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003192 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3193 return;
3194
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003195 if (argvars[0].v_type != VAR_UNKNOWN)
3196 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003197 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003198 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003199 what_list = argvars[0].vval.v_list;
3200 }
3201 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003202}
3203#endif
3204
3205/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003206 * Returns TRUE when using a user-defined function for thesaurus completion.
3207 */
3208 static int
3209thesaurus_func_complete(int type UNUSED)
3210{
3211#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003212 return type == CTRL_X_THESAURUS
3213 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003214#else
3215 return FALSE;
3216#endif
3217}
3218
3219/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003220 * Return value of process_next_cpt_value()
3221 */
3222enum
3223{
3224 INS_COMPL_CPT_OK = 1,
3225 INS_COMPL_CPT_CONT,
3226 INS_COMPL_CPT_END
3227};
3228
3229/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003230 * state information used for getting the next set of insert completion
3231 * matches.
3232 */
3233typedef struct
3234{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003235 char_u *e_cpt_copy; // copy of 'complete'
3236 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003237 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003238 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003239 pos_T prev_match_pos; // previous match position
3240 int set_match_pos; // save first_match_pos/last_match_pos
3241 pos_T first_match_pos; // first match position
3242 pos_T last_match_pos; // last match position
3243 int found_all; // found all matches of a certain type.
3244 char_u *dict; // dictionary file to search
3245 int dict_f; // "dict" is an exact file name or not
3246} ins_compl_next_state_T;
3247
3248/*
3249 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003250 *
3251 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003252 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003253 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 * st->found_all - all matches of this type are found
3255 * st->ins_buf - search for completions in this buffer
3256 * st->first_match_pos - position of the first completion match
3257 * st->last_match_pos - position of the last completion match
3258 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003259 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003260 * st->dict - name of the dictionary or thesaurus file to search
3261 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003262 *
3263 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003264 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3265 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003266 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003267 */
3268 static int
3269process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003270 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003271 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003272 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003273{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 int compl_type = -1;
3275 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003277 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003278
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003279 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3280 st->e_cpt++;
3281
3282 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003283 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003284 st->ins_buf = curbuf;
3285 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003286 // Move the cursor back one character so that ^N can match the
3287 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003288 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003289 {
3290 // Move the cursor to after the last character in the
3291 // buffer, so that word at start of buffer is found
3292 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003293 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3294 st->first_match_pos.col =
3295 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003296 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003297 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003298 compl_type = 0;
3299
3300 // Remember the first match so that the loop stops when we
3301 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003302 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003303 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003304 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003305 && (st->ins_buf = ins_compl_next_buf(
3306 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003307 {
3308 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003309 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003310 {
3311 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003312 st->first_match_pos.col = st->last_match_pos.col = 0;
3313 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3314 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003315 compl_type = 0;
3316 }
3317 else // unloaded buffer, scan like dictionary
3318 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003319 st->found_all = TRUE;
3320 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003321 {
3322 status = INS_COMPL_CPT_CONT;
3323 goto done;
3324 }
3325 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003326 st->dict = st->ins_buf->b_fname;
3327 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003328 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003329 if (!shortmess(SHM_COMPLETIONSCAN))
3330 {
3331 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3332 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3333 st->ins_buf->b_fname == NULL
3334 ? buf_spname(st->ins_buf)
3335 : st->ins_buf->b_sfname == NULL
3336 ? st->ins_buf->b_fname
3337 : st->ins_buf->b_sfname);
3338 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3339 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003341 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003342 status = INS_COMPL_CPT_END;
3343 else
3344 {
3345 if (ctrl_x_mode_line_or_eval())
3346 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003347 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003348 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003349 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003350 compl_type = CTRL_X_DICTIONARY;
3351 else
3352 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003353 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003354 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003355 st->dict = st->e_cpt;
3356 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003357 }
3358 }
3359#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003360 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003361 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003362 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003363 compl_type = CTRL_X_PATH_DEFINES;
3364#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003365 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003366 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003367 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003368 if (!shortmess(SHM_COMPLETIONSCAN))
3369 {
3370 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3371 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3372 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3373 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003374 }
3375 else
3376 compl_type = -1;
3377
3378 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003379 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003380
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003381 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003382 if (compl_type == -1)
3383 status = INS_COMPL_CPT_CONT;
3384 }
3385
3386done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003387 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003388 return status;
3389}
3390
3391#ifdef FEAT_FIND_ID
3392/*
3393 * Get the next set of identifiers or defines matching "compl_pattern" in
3394 * included files.
3395 */
3396 static void
3397get_next_include_file_completion(int compl_type)
3398{
3399 find_pattern_in_path(compl_pattern, compl_direction,
3400 (int)STRLEN(compl_pattern), FALSE, FALSE,
3401 (compl_type == CTRL_X_PATH_DEFINES
3402 && !(compl_cont_status & CONT_SOL))
3403 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3404 (linenr_T)1, (linenr_T)MAXLNUM);
3405}
3406#endif
3407
3408/*
3409 * Get the next set of words matching "compl_pattern" in dictionary or
3410 * thesaurus files.
3411 */
3412 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003413get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003414{
3415#ifdef FEAT_COMPL_FUNC
3416 if (thesaurus_func_complete(compl_type))
3417 expand_by_function(compl_type, compl_pattern);
3418 else
3419#endif
3420 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003421 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003422 : (compl_type == CTRL_X_THESAURUS
3423 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3424 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3425 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003426 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003427 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003428}
3429
3430/*
3431 * Get the next set of tag names matching "compl_pattern".
3432 */
3433 static void
3434get_next_tag_completion(void)
3435{
3436 int save_p_ic;
3437 char_u **matches;
3438 int num_matches;
3439
3440 // set p_ic according to p_ic, p_scs and pat for find_tags().
3441 save_p_ic = p_ic;
3442 p_ic = ignorecase(compl_pattern);
3443
3444 // Find up to TAG_MANY matches. Avoids that an enormous number
3445 // of matches is found when compl_pattern is empty
3446 g_tag_at_cursor = TRUE;
3447 if (find_tags(compl_pattern, &num_matches, &matches,
3448 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003449 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003450 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3451 ins_compl_add_matches(num_matches, matches, p_ic);
3452 g_tag_at_cursor = FALSE;
3453 p_ic = save_p_ic;
3454}
3455
3456/*
3457 * Get the next set of filename matching "compl_pattern".
3458 */
3459 static void
3460get_next_filename_completion(void)
3461{
3462 char_u **matches;
3463 int num_matches;
3464
3465 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3466 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3467 return;
3468
3469 // May change home directory back to "~".
3470 tilde_replace(compl_pattern, num_matches, matches);
3471#ifdef BACKSLASH_IN_FILENAME
3472 if (curbuf->b_p_csl[0] != NUL)
3473 {
3474 int i;
3475
3476 for (i = 0; i < num_matches; ++i)
3477 {
3478 char_u *ptr = matches[i];
3479
3480 while (*ptr != NUL)
3481 {
3482 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3483 *ptr = '/';
3484 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3485 *ptr = '\\';
3486 ptr += (*mb_ptr2len)(ptr);
3487 }
3488 }
3489 }
3490#endif
3491 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3492}
3493
3494/*
3495 * Get the next set of command-line completions matching "compl_pattern".
3496 */
3497 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003498get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003499{
3500 char_u **matches;
3501 int num_matches;
3502
3503 if (expand_cmdline(&compl_xp, compl_pattern,
3504 (int)STRLEN(compl_pattern),
3505 &num_matches, &matches) == EXPAND_OK)
3506 ins_compl_add_matches(num_matches, matches, FALSE);
3507}
3508
3509/*
3510 * Get the next set of spell suggestions matching "compl_pattern".
3511 */
3512 static void
3513get_next_spell_completion(linenr_T lnum UNUSED)
3514{
3515#ifdef FEAT_SPELL
3516 char_u **matches;
3517 int num_matches;
3518
3519 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3520 if (num_matches > 0)
3521 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003522 else
3523 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003524#endif
3525}
3526
3527/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003528 * Return the next word or line from buffer "ins_buf" at position
3529 * "cur_match_pos" for completion. The length of the match is set in "len".
3530 */
3531 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003532ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003533 buf_T *ins_buf, // buffer being scanned
3534 pos_T *cur_match_pos, // current match position
3535 int *match_len,
3536 int *cont_s_ipos) // next ^X<> will set initial_pos
3537{
3538 char_u *ptr;
3539 int len;
3540
3541 *match_len = 0;
3542 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3543 cur_match_pos->col;
3544 if (ctrl_x_mode_line_or_eval())
3545 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003546 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003547 {
3548 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3549 return NULL;
3550 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3551 if (!p_paste)
3552 ptr = skipwhite(ptr);
3553 }
3554 len = (int)STRLEN(ptr);
3555 }
3556 else
3557 {
3558 char_u *tmp_ptr = ptr;
3559
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003560 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003561 {
3562 tmp_ptr += compl_length;
3563 // Skip if already inside a word.
3564 if (vim_iswordp(tmp_ptr))
3565 return NULL;
3566 // Find start of next word.
3567 tmp_ptr = find_word_start(tmp_ptr);
3568 }
3569 // Find end of this word.
3570 tmp_ptr = find_word_end(tmp_ptr);
3571 len = (int)(tmp_ptr - ptr);
3572
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003573 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003574 {
3575 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3576 {
3577 // Try next line, if any. the new word will be
3578 // "join" as if the normal command "J" was used.
3579 // IOSIZE is always greater than
3580 // compl_length, so the next STRNCPY always
3581 // works -- Acevedo
3582 STRNCPY(IObuff, ptr, len);
3583 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3584 tmp_ptr = ptr = skipwhite(ptr);
3585 // Find start of next word.
3586 tmp_ptr = find_word_start(tmp_ptr);
3587 // Find end of next word.
3588 tmp_ptr = find_word_end(tmp_ptr);
3589 if (tmp_ptr > ptr)
3590 {
3591 if (*ptr != ')' && IObuff[len - 1] != TAB)
3592 {
3593 if (IObuff[len - 1] != ' ')
3594 IObuff[len++] = ' ';
3595 // IObuf =~ "\k.* ", thus len >= 2
3596 if (p_js
3597 && (IObuff[len - 2] == '.'
3598 || (vim_strchr(p_cpo, CPO_JOINSP)
3599 == NULL
3600 && (IObuff[len - 2] == '?'
3601 || IObuff[len - 2] == '!'))))
3602 IObuff[len++] = ' ';
3603 }
3604 // copy as much as possible of the new word
3605 if (tmp_ptr - ptr >= IOSIZE - len)
3606 tmp_ptr = ptr + IOSIZE - len - 1;
3607 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3608 len += (int)(tmp_ptr - ptr);
3609 *cont_s_ipos = TRUE;
3610 }
3611 IObuff[len] = NUL;
3612 ptr = IObuff;
3613 }
3614 if (len == compl_length)
3615 return NULL;
3616 }
3617 }
3618
3619 *match_len = len;
3620 return ptr;
3621}
3622
3623/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003624 * Get the next set of words matching "compl_pattern" for default completion(s)
3625 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003626 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3627 * position "st->start_pos" in the "compl_direction" direction. If
3628 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3629 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630 * Returns OK if a new next match is found, otherwise returns FAIL.
3631 */
3632 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003633get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003634{
3635 int found_new_match = FAIL;
3636 int save_p_scs;
3637 int save_p_ws;
3638 int looped_around = FALSE;
3639 char_u *ptr;
3640 int len;
3641
3642 // If 'infercase' is set, don't use 'smartcase' here
3643 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003644 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003645 p_scs = FALSE;
3646
3647 // Buffers other than curbuf are scanned from the beginning or the
3648 // end but never from the middle, thus setting nowrapscan in this
3649 // buffer is a good idea, on the other hand, we always set
3650 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3651 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003652 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003654 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003655 p_ws = TRUE;
3656 looped_around = FALSE;
3657 for (;;)
3658 {
3659 int cont_s_ipos = FALSE;
3660
3661 ++msg_silent; // Don't want messages for wrapscan.
3662
3663 // ctrl_x_mode_line_or_eval() || word-wise search that
3664 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003665 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003666 found_new_match = search_for_exact_line(st->ins_buf,
3667 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003669 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3670 NULL, compl_direction, compl_pattern, 1L,
3671 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003672 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003673 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003674 {
3675 // set "compl_started" even on fail
3676 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003677 st->first_match_pos = *st->cur_match_pos;
3678 st->last_match_pos = *st->cur_match_pos;
3679 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003680 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003681 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3682 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003683 {
3684 found_new_match = FAIL;
3685 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003686 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003687 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3688 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3689 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690 {
3691 if (looped_around)
3692 found_new_match = FAIL;
3693 else
3694 looped_around = TRUE;
3695 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003696 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003697 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3698 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3699 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003700 {
3701 if (looped_around)
3702 found_new_match = FAIL;
3703 else
3704 looped_around = TRUE;
3705 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003706 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003707 if (found_new_match == FAIL)
3708 break;
3709
3710 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003711 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003712 && start_pos->lnum == st->cur_match_pos->lnum
3713 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003714 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003715
zeertzjq440d4cb2023-03-02 17:51:32 +00003716 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3717 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003718 if (ptr == NULL)
3719 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003720
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003721 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003722 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003723 0, cont_s_ipos) != NOTDONE)
3724 {
3725 found_new_match = OK;
3726 break;
3727 }
3728 }
3729 p_scs = save_p_scs;
3730 p_ws = save_p_ws;
3731
3732 return found_new_match;
3733}
3734
3735/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003736 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003737 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003738 */
3739 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003740get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003741{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003742 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003743
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003744 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003745 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003746 case -1:
3747 break;
3748#ifdef FEAT_FIND_ID
3749 case CTRL_X_PATH_PATTERNS:
3750 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003751 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003752 break;
3753#endif
3754
3755 case CTRL_X_DICTIONARY:
3756 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003757 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3758 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003759 break;
3760
3761 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003762 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003763 break;
3764
3765 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003766 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003767 break;
3768
3769 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003770 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003771 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003772 break;
3773
3774#ifdef FEAT_COMPL_FUNC
3775 case CTRL_X_FUNCTION:
3776 case CTRL_X_OMNI:
3777 expand_by_function(type, compl_pattern);
3778 break;
3779#endif
3780
3781 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003782 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003783 break;
3784
3785 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003786 found_new_match = get_next_default_completion(st, ini);
3787 if (found_new_match == FAIL && st->ins_buf == curbuf)
3788 st->found_all = TRUE;
3789 }
3790
3791 // check if compl_curr_match has changed, (e.g. other type of
3792 // expansion added something)
3793 if (type != 0 && compl_curr_match != compl_old_match)
3794 found_new_match = OK;
3795
3796 return found_new_match;
3797}
3798
3799/*
3800 * Get the next expansion(s), using "compl_pattern".
3801 * The search starts at position "ini" in curbuf and in the direction
3802 * compl_direction.
3803 * When "compl_started" is FALSE start at that position, otherwise continue
3804 * where we stopped searching before.
3805 * This may return before finding all the matches.
3806 * Return the total number of matches or -1 if still unknown -- Acevedo
3807 */
3808 static int
3809ins_compl_get_exp(pos_T *ini)
3810{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003811 static ins_compl_next_state_T st;
3812 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003813 int i;
3814 int found_new_match;
3815 int type = ctrl_x_mode;
3816
3817 if (!compl_started)
3818 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003819 buf_T *buf;
3820
3821 FOR_ALL_BUFFERS(buf)
3822 buf->b_scanned = 0;
3823 if (!st_cleared)
3824 {
3825 CLEAR_FIELD(st);
3826 st_cleared = TRUE;
3827 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003828 st.found_all = FALSE;
3829 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003830 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003831 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003832 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3833 ? (char_u *)"." : curbuf->b_p_cpt);
3834 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003835 st.last_match_pos = st.first_match_pos = *ini;
3836 }
3837 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3838 st.ins_buf = curbuf; // In case the buffer was wiped out.
3839
3840 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003841 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003842 ? &st.last_match_pos : &st.first_match_pos;
3843
3844 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3845 for (;;)
3846 {
3847 found_new_match = FAIL;
3848 st.set_match_pos = FALSE;
3849
3850 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3851 // or if found_all says this entry is done. For ^X^L only use the
3852 // entries from 'complete' that look in loaded buffers.
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 && (!compl_started || st.found_all))
3855 {
3856 int status = process_next_cpt_value(&st, &type, ini);
3857
3858 if (status == INS_COMPL_CPT_END)
3859 break;
3860 if (status == INS_COMPL_CPT_CONT)
3861 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003862 }
3863
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003864 // If complete() was called then compl_pattern has been reset. The
3865 // following won't work then, bail out.
3866 if (compl_pattern == NULL)
3867 break;
3868
3869 // get the next set of completion matches
3870 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003871
3872 // break the loop for specialized modes (use 'complete' just for the
3873 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3874 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003875 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3876 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003877 {
3878 if (got_int)
3879 break;
3880 // Fill the popup menu as soon as possible.
3881 if (type != -1)
3882 ins_compl_check_keys(0, FALSE);
3883
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003884 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003885 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3886 break;
3887 compl_started = TRUE;
3888 }
3889 else
3890 {
3891 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003892 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003893 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003894
3895 compl_started = FALSE;
3896 }
3897 }
3898 compl_started = TRUE;
3899
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003900 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003901 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003902 found_new_match = FAIL;
3903
3904 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003905 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003906 && !ctrl_x_mode_line_or_eval()))
3907 i = ins_compl_make_cyclic();
3908
3909 if (compl_old_match != NULL)
3910 {
3911 // If several matches were added (FORWARD) or the search failed and has
3912 // just been made cyclic then we have to move compl_curr_match to the
3913 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003914 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003915 : compl_old_match->cp_prev;
3916 if (compl_curr_match == NULL)
3917 compl_curr_match = compl_old_match;
3918 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003919 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003920
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003921 return i;
3922}
3923
3924/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003925 * Update "compl_shown_match" to the actually shown match, it may differ when
3926 * "compl_leader" is used to omit some of the matches.
3927 */
3928 static void
3929ins_compl_update_shown_match(void)
3930{
3931 while (!ins_compl_equal(compl_shown_match,
3932 compl_leader, (int)STRLEN(compl_leader))
3933 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003934 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003935 compl_shown_match = compl_shown_match->cp_next;
3936
3937 // If we didn't find it searching forward, and compl_shows_dir is
3938 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003939 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003940 && !ins_compl_equal(compl_shown_match,
3941 compl_leader, (int)STRLEN(compl_leader))
3942 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003943 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003944 {
3945 while (!ins_compl_equal(compl_shown_match,
3946 compl_leader, (int)STRLEN(compl_leader))
3947 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003948 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003949 compl_shown_match = compl_shown_match->cp_prev;
3950 }
3951}
3952
3953/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003954 * Delete the old text being completed.
3955 */
3956 void
3957ins_compl_delete(void)
3958{
3959 int col;
3960
3961 // In insert mode: Delete the typed part.
3962 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003963 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003964 if ((int)curwin->w_cursor.col > col)
3965 {
3966 if (stop_arrow() == FAIL)
3967 return;
3968 backspace_until_column(col);
3969 }
3970
3971 // TODO: is this sufficient for redrawing? Redrawing everything causes
3972 // flicker, thus we can't do that.
3973 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003974#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003975 // clear v:completed_item
3976 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003977#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003978}
3979
3980/*
3981 * Insert the new text being completed.
3982 * "in_compl_func" is TRUE when called from complete_check().
3983 */
3984 void
3985ins_compl_insert(int in_compl_func)
3986{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003987 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003988
3989 // Make sure we don't go over the end of the string, this can happen with
3990 // illegal bytes.
3991 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3992 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003993 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003994 compl_used_match = FALSE;
3995 else
3996 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003997#ifdef FEAT_EVAL
3998 {
3999 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
4000
4001 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
4002 }
4003#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004004 if (!in_compl_func)
4005 compl_curr_match = compl_shown_match;
4006}
4007
4008/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004009 * show the file name for the completion match (if any). Truncate the file
4010 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004011 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004012 static void
4013ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004014{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004015 char *lead = _("match in file");
4016 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4017 char_u *s;
4018 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004019
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004020 if (space <= 0)
4021 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004022
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004023 // We need the tail that fits. With double-byte encoding going
4024 // back from the end is very slow, thus go from the start and keep
4025 // the text that fits in "space" between "s" and "e".
4026 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004027 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004028 space -= ptr2cells(e);
4029 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004030 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004031 space += ptr2cells(s);
4032 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004033 }
4034 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004035 msg_hist_off = TRUE;
4036 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4037 s > compl_shown_match->cp_fname ? "<" : "", s);
4038 msg((char *)IObuff);
4039 msg_hist_off = FALSE;
4040 redraw_cmdline = FALSE; // don't overwrite!
4041}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004043/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004044 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004045 * times. The number of matches found is returned in 'num_matches'.
4046 *
4047 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4048 * get more completions. If it is FALSE, then do nothing when there are no more
4049 * completions in the given direction.
4050 *
4051 * If "advance" is TRUE, then completion will move to the first match.
4052 * Otherwise, the original text will be shown.
4053 *
4054 * Returns OK on success and -1 if the number of matches are unknown.
4055 */
4056 static int
4057find_next_completion_match(
4058 int allow_get_expansion,
4059 int todo, // repeat completion this many times
4060 int advance,
4061 int *num_matches)
4062{
4063 int found_end = FALSE;
4064 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004065
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 while (--todo >= 0)
4067 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004068 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004069 {
4070 compl_shown_match = compl_shown_match->cp_next;
4071 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004072 && (is_first_match(compl_shown_match->cp_next)
4073 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004075 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004076 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004077 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004078 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004079 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004080 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004081 }
4082 else
4083 {
4084 if (!allow_get_expansion)
4085 {
4086 if (advance)
4087 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004088 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004089 compl_pending -= todo + 1;
4090 else
4091 compl_pending += todo + 1;
4092 }
4093 return -1;
4094 }
4095
4096 if (!compl_no_select && advance)
4097 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004098 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004099 --compl_pending;
4100 else
4101 ++compl_pending;
4102 }
4103
4104 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004105 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106
4107 // handle any pending completions
4108 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004109 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004110 {
4111 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4112 {
4113 compl_shown_match = compl_shown_match->cp_next;
4114 --compl_pending;
4115 }
4116 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4117 {
4118 compl_shown_match = compl_shown_match->cp_prev;
4119 ++compl_pending;
4120 }
4121 else
4122 break;
4123 }
4124 found_end = FALSE;
4125 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004126 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004127 && compl_leader != NULL
4128 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004129 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004130 ++todo;
4131 else
4132 // Remember a matching item.
4133 found_compl = compl_shown_match;
4134
4135 // Stop at the end of the list when we found a usable match.
4136 if (found_end)
4137 {
4138 if (found_compl != NULL)
4139 {
4140 compl_shown_match = found_compl;
4141 break;
4142 }
4143 todo = 1; // use first usable match after wrapping around
4144 }
4145 }
4146
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004147 return OK;
4148}
4149
4150/*
4151 * Fill in the next completion in the current direction.
4152 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4153 * get more completions. If it is FALSE, then we just do nothing when there
4154 * are no more completions in a given direction. The latter case is used when
4155 * we are still in the middle of finding completions, to allow browsing
4156 * through the ones found so far.
4157 * Return the total number of matches, or -1 if still unknown -- webb.
4158 *
4159 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4160 * compl_shown_match here.
4161 *
4162 * Note that this function may be called recursively once only. First with
4163 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4164 * calls this function with "allow_get_expansion" FALSE.
4165 */
4166 static int
4167ins_compl_next(
4168 int allow_get_expansion,
4169 int count, // repeat completion this many times; should
4170 // be at least 1
4171 int insert_match, // Insert the newly selected match
4172 int in_compl_func) // called from complete_check()
4173{
4174 int num_matches = -1;
4175 int todo = count;
4176 int advance;
4177 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004178 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004179
4180 // When user complete function return -1 for findstart which is next
4181 // time of 'always', compl_shown_match become NULL.
4182 if (compl_shown_match == NULL)
4183 return -1;
4184
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004185 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004186 // Update "compl_shown_match" to the actually shown match
4187 ins_compl_update_shown_match();
4188
4189 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004190 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004191 // Delete old text to be replaced
4192 ins_compl_delete();
4193
4194 // When finding the longest common text we stick at the original text,
4195 // don't let CTRL-N or CTRL-P move to the first match.
4196 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4197
4198 // When restarting the search don't insert the first match either.
4199 if (compl_restarting)
4200 {
4201 advance = FALSE;
4202 compl_restarting = FALSE;
4203 }
4204
4205 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4206 // around.
4207 if (find_next_completion_match(allow_get_expansion, todo, advance,
4208 &num_matches) == -1)
4209 return -1;
4210
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004211 if (curbuf != orig_curbuf)
4212 {
4213 // In case some completion function switched buffer, don't want to
4214 // insert the completion elsewhere.
4215 return -1;
4216 }
4217
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004218 // Insert the text of the new completion, or the compl_leader.
4219 if (compl_no_insert && !started)
4220 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004221 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004222 compl_used_match = FALSE;
4223 }
4224 else if (insert_match)
4225 {
4226 if (!compl_get_longest || compl_used_match)
4227 ins_compl_insert(in_compl_func);
4228 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004229 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004230 }
4231 else
4232 compl_used_match = FALSE;
4233
4234 if (!allow_get_expansion)
4235 {
4236 // may undisplay the popup menu first
4237 ins_compl_upd_pum();
4238
4239 if (pum_enough_matches())
4240 // Will display the popup menu, don't redraw yet to avoid flicker.
4241 pum_call_update_screen();
4242 else
4243 // Not showing the popup menu yet, redraw to show the user what was
4244 // inserted.
4245 update_screen(0);
4246
4247 // display the updated popup menu
4248 ins_compl_show_pum();
4249#ifdef FEAT_GUI
4250 if (gui.in_use)
4251 {
4252 // Show the cursor after the match, not after the redrawn text.
4253 setcursor();
4254 out_flush_cursor(FALSE, FALSE);
4255 }
4256#endif
4257
4258 // Delete old text to be replaced, since we're still searching and
4259 // don't want to match ourselves!
4260 ins_compl_delete();
4261 }
4262
4263 // Enter will select a match when the match wasn't inserted and the popup
4264 // menu is visible.
4265 if (compl_no_insert && !started)
4266 compl_enter_selects = TRUE;
4267 else
4268 compl_enter_selects = !insert_match && compl_match_array != NULL;
4269
4270 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004271 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004272 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004273
4274 return num_matches;
4275}
4276
4277/*
4278 * Call this while finding completions, to check whether the user has hit a key
4279 * that should change the currently displayed completion, or exit completion
4280 * mode. Also, when compl_pending is not zero, show a completion as soon as
4281 * possible. -- webb
4282 * "frequency" specifies out of how many calls we actually check.
4283 * "in_compl_func" is TRUE when called from complete_check(), don't set
4284 * compl_curr_match.
4285 */
4286 void
4287ins_compl_check_keys(int frequency, int in_compl_func)
4288{
4289 static int count = 0;
4290 int c;
4291
4292 // Don't check when reading keys from a script, :normal or feedkeys().
4293 // That would break the test scripts. But do check for keys when called
4294 // from complete_check().
4295 if (!in_compl_func && (using_script() || ex_normal_busy))
4296 return;
4297
4298 // Only do this at regular intervals
4299 if (++count < frequency)
4300 return;
4301 count = 0;
4302
4303 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4304 // can't do its work correctly.
4305 c = vpeekc_any();
4306 if (c != NUL)
4307 {
4308 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4309 {
4310 c = safe_vgetc(); // Eat the character
4311 compl_shows_dir = ins_compl_key2dir(c);
4312 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4313 c != K_UP && c != K_DOWN, in_compl_func);
4314 }
4315 else
4316 {
4317 // Need to get the character to have KeyTyped set. We'll put it
4318 // back with vungetc() below. But skip K_IGNORE.
4319 c = safe_vgetc();
4320 if (c != K_IGNORE)
4321 {
4322 // Don't interrupt completion when the character wasn't typed,
4323 // e.g., when doing @q to replay keys.
4324 if (c != Ctrl_R && KeyTyped)
4325 compl_interrupted = TRUE;
4326
4327 vungetc(c);
4328 }
4329 }
4330 }
4331 if (compl_pending != 0 && !got_int && !compl_no_insert)
4332 {
4333 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4334
4335 compl_pending = 0;
4336 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4337 }
4338}
4339
4340/*
4341 * Decide the direction of Insert mode complete from the key typed.
4342 * Returns BACKWARD or FORWARD.
4343 */
4344 static int
4345ins_compl_key2dir(int c)
4346{
4347 if (c == Ctrl_P || c == Ctrl_L
4348 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4349 return BACKWARD;
4350 return FORWARD;
4351}
4352
4353/*
4354 * Return TRUE for keys that are used for completion only when the popup menu
4355 * is visible.
4356 */
4357 static int
4358ins_compl_pum_key(int c)
4359{
4360 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4361 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4362 || c == K_UP || c == K_DOWN);
4363}
4364
4365/*
4366 * Decide the number of completions to move forward.
4367 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4368 */
4369 static int
4370ins_compl_key2count(int c)
4371{
4372 int h;
4373
4374 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4375 {
4376 h = pum_get_height();
4377 if (h > 3)
4378 h -= 2; // keep some context
4379 return h;
4380 }
4381 return 1;
4382}
4383
4384/*
4385 * Return TRUE if completion with "c" should insert the match, FALSE if only
4386 * to change the currently selected completion.
4387 */
4388 static int
4389ins_compl_use_match(int c)
4390{
4391 switch (c)
4392 {
4393 case K_UP:
4394 case K_DOWN:
4395 case K_PAGEDOWN:
4396 case K_KPAGEDOWN:
4397 case K_S_DOWN:
4398 case K_PAGEUP:
4399 case K_KPAGEUP:
4400 case K_S_UP:
4401 return FALSE;
4402 }
4403 return TRUE;
4404}
4405
4406/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004407 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4408 * completion)
4409 * Sets the global variables: compl_col, compl_length and compl_pattern.
4410 * Uses the global variables: compl_cont_status and ctrl_x_mode
4411 */
4412 static int
4413get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4414{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004415 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004416 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004417 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004418 {
4419 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4420 ;
4421 compl_col += ++startcol;
4422 compl_length = curs_col - startcol;
4423 }
4424 if (p_ic)
4425 compl_pattern = str_foldcase(line + compl_col,
4426 compl_length, NULL, 0);
4427 else
4428 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4429 if (compl_pattern == NULL)
4430 return FAIL;
4431 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004432 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004433 {
4434 char_u *prefix = (char_u *)"\\<";
4435
4436 // we need up to 2 extra chars for the prefix
4437 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4438 compl_length) + 2);
4439 if (compl_pattern == NULL)
4440 return FAIL;
4441 if (!vim_iswordp(line + compl_col)
4442 || (compl_col > 0
4443 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4444 prefix = (char_u *)"";
4445 STRCPY((char *)compl_pattern, prefix);
4446 (void)quote_meta(compl_pattern + STRLEN(prefix),
4447 line + compl_col, compl_length);
4448 }
4449 else if (--startcol < 0
4450 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4451 {
4452 // Match any word of at least two chars
4453 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4454 if (compl_pattern == NULL)
4455 return FAIL;
4456 compl_col += curs_col;
4457 compl_length = 0;
4458 }
4459 else
4460 {
4461 // Search the point of change class of multibyte character
4462 // or not a word single byte character backward.
4463 if (has_mbyte)
4464 {
4465 int base_class;
4466 int head_off;
4467
4468 startcol -= (*mb_head_off)(line, line + startcol);
4469 base_class = mb_get_class(line + startcol);
4470 while (--startcol >= 0)
4471 {
4472 head_off = (*mb_head_off)(line, line + startcol);
4473 if (base_class != mb_get_class(line + startcol
4474 - head_off))
4475 break;
4476 startcol -= head_off;
4477 }
4478 }
4479 else
4480 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4481 ;
4482 compl_col += ++startcol;
4483 compl_length = (int)curs_col - startcol;
4484 if (compl_length == 1)
4485 {
4486 // Only match word with at least two chars -- webb
4487 // there's no need to call quote_meta,
4488 // alloc(7) is enough -- Acevedo
4489 compl_pattern = alloc(7);
4490 if (compl_pattern == NULL)
4491 return FAIL;
4492 STRCPY((char *)compl_pattern, "\\<");
4493 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4494 STRCAT((char *)compl_pattern, "\\k");
4495 }
4496 else
4497 {
4498 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4499 compl_length) + 2);
4500 if (compl_pattern == NULL)
4501 return FAIL;
4502 STRCPY((char *)compl_pattern, "\\<");
4503 (void)quote_meta(compl_pattern + 2, line + compl_col,
4504 compl_length);
4505 }
4506 }
4507
4508 return OK;
4509}
4510
4511/*
4512 * Get the pattern, column and length for whole line completion or for the
4513 * complete() function.
4514 * Sets the global variables: compl_col, compl_length and compl_pattern.
4515 */
4516 static int
4517get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4518{
4519 compl_col = (colnr_T)getwhitecols(line);
4520 compl_length = (int)curs_col - (int)compl_col;
4521 if (compl_length < 0) // cursor in indent: empty pattern
4522 compl_length = 0;
4523 if (p_ic)
4524 compl_pattern = str_foldcase(line + compl_col, compl_length,
4525 NULL, 0);
4526 else
4527 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4528 if (compl_pattern == NULL)
4529 return FAIL;
4530
4531 return OK;
4532}
4533
4534/*
4535 * Get the pattern, column and length for filename completion.
4536 * Sets the global variables: compl_col, compl_length and compl_pattern.
4537 */
4538 static int
4539get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4540{
4541 // Go back to just before the first filename character.
4542 if (startcol > 0)
4543 {
4544 char_u *p = line + startcol;
4545
4546 MB_PTR_BACK(line, p);
4547 while (p > line && vim_isfilec(PTR2CHAR(p)))
4548 MB_PTR_BACK(line, p);
4549 if (p == line && vim_isfilec(PTR2CHAR(p)))
4550 startcol = 0;
4551 else
4552 startcol = (int)(p - line) + 1;
4553 }
4554
4555 compl_col += startcol;
4556 compl_length = (int)curs_col - startcol;
4557 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4558 if (compl_pattern == NULL)
4559 return FAIL;
4560
4561 return OK;
4562}
4563
4564/*
4565 * Get the pattern, column and length for command-line completion.
4566 * Sets the global variables: compl_col, compl_length and compl_pattern.
4567 */
4568 static int
4569get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4570{
4571 compl_pattern = vim_strnsave(line, curs_col);
4572 if (compl_pattern == NULL)
4573 return FAIL;
4574 set_cmd_context(&compl_xp, compl_pattern,
4575 (int)STRLEN(compl_pattern), curs_col, FALSE);
4576 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4577 || compl_xp.xp_context == EXPAND_NOTHING)
4578 // No completion possible, use an empty pattern to get a
4579 // "pattern not found" message.
4580 compl_col = curs_col;
4581 else
4582 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4583 compl_length = curs_col - compl_col;
4584
4585 return OK;
4586}
4587
4588/*
4589 * Get the pattern, column and length for user defined completion ('omnifunc',
4590 * 'completefunc' and 'thesaurusfunc')
4591 * Sets the global variables: compl_col, compl_length and compl_pattern.
4592 * Uses the global variable: spell_bad_len
4593 */
4594 static int
4595get_userdefined_compl_info(colnr_T curs_col UNUSED)
4596{
4597 int ret = FAIL;
4598
4599#ifdef FEAT_COMPL_FUNC
4600 // Call user defined function 'completefunc' with "a:findstart"
4601 // set to 1 to obtain the length of text to use for completion.
4602 char_u *line;
4603 typval_T args[3];
4604 int col;
4605 char_u *funcname;
4606 pos_T pos;
4607 int save_State = State;
4608 callback_T *cb;
4609
4610 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4611 // length as a string
4612 funcname = get_complete_funcname(ctrl_x_mode);
4613 if (*funcname == NUL)
4614 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004615 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004616 ? "completefunc" : "omnifunc");
4617 return FAIL;
4618 }
4619
4620 args[0].v_type = VAR_NUMBER;
4621 args[0].vval.v_number = 1;
4622 args[1].v_type = VAR_STRING;
4623 args[1].vval.v_string = (char_u *)"";
4624 args[2].v_type = VAR_UNKNOWN;
4625 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004626 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004627 cb = get_insert_callback(ctrl_x_mode);
4628 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004629 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004630
4631 State = save_State;
4632 curwin->w_cursor = pos; // restore the cursor position
4633 validate_cursor();
4634 if (!EQUAL_POS(curwin->w_cursor, pos))
4635 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004636 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004637 return FAIL;
4638 }
4639
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004640 // Return value -2 means the user complete function wants to cancel the
4641 // complete without an error, do the same if the function did not execute
4642 // successfully.
4643 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004644 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004645 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004646 if (col == -3)
4647 {
4648 ctrl_x_mode = CTRL_X_NORMAL;
4649 edit_submode = NULL;
4650 if (!shortmess(SHM_COMPLETIONMENU))
4651 msg_clr_cmdline();
4652 return FAIL;
4653 }
4654
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004655 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004656 // completion.
4657 compl_opt_refresh_always = FALSE;
4658 compl_opt_suppress_empty = FALSE;
4659
4660 if (col < 0)
4661 col = curs_col;
4662 compl_col = col;
4663 if (compl_col > curs_col)
4664 compl_col = curs_col;
4665
4666 // Setup variables for completion. Need to obtain "line" again,
4667 // it may have become invalid.
4668 line = ml_get(curwin->w_cursor.lnum);
4669 compl_length = curs_col - compl_col;
4670 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4671 if (compl_pattern == NULL)
4672 return FAIL;
4673
4674 ret = OK;
4675#endif
4676
4677 return ret;
4678}
4679
4680/*
4681 * Get the pattern, column and length for spell completion.
4682 * Sets the global variables: compl_col, compl_length and compl_pattern.
4683 * Uses the global variable: spell_bad_len
4684 */
4685 static int
4686get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4687{
4688 int ret = FAIL;
4689#ifdef FEAT_SPELL
4690 char_u *line;
4691
4692 if (spell_bad_len > 0)
4693 compl_col = curs_col - spell_bad_len;
4694 else
4695 compl_col = spell_word_start(startcol);
4696 if (compl_col >= (colnr_T)startcol)
4697 {
4698 compl_length = 0;
4699 compl_col = curs_col;
4700 }
4701 else
4702 {
4703 spell_expand_check_cap(compl_col);
4704 compl_length = (int)curs_col - compl_col;
4705 }
4706 // Need to obtain "line" again, it may have become invalid.
4707 line = ml_get(curwin->w_cursor.lnum);
4708 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4709 if (compl_pattern == NULL)
4710 return FAIL;
4711
4712 ret = OK;
4713#endif
4714
4715 return ret;
4716}
4717
4718/*
4719 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004720 * "startcol" - start column number of the completion pattern/text
4721 * "cur_col" - current cursor column
4722 * On return, "line_invalid" is set to TRUE, if the current line may have
4723 * become invalid and needs to be fetched again.
4724 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004725 */
4726 static int
4727compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4728{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004729 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004730 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4731 && !thesaurus_func_complete(ctrl_x_mode)))
4732 {
4733 return get_normal_compl_info(line, startcol, curs_col);
4734 }
4735 else if (ctrl_x_mode_line_or_eval())
4736 {
4737 return get_wholeline_compl_info(line, curs_col);
4738 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004739 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004740 {
4741 return get_filename_compl_info(line, startcol, curs_col);
4742 }
4743 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4744 {
4745 return get_cmdline_compl_info(line, curs_col);
4746 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004747 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004748 || thesaurus_func_complete(ctrl_x_mode))
4749 {
4750 if (get_userdefined_compl_info(curs_col) == FAIL)
4751 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004752 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004753 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004754 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004755 {
4756 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4757 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004758 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004759 }
4760 else
4761 {
4762 internal_error("ins_complete()");
4763 return FAIL;
4764 }
4765
4766 return OK;
4767}
4768
4769/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004770 * Continue an interrupted completion mode search in "line".
4771 *
4772 * If this same ctrl_x_mode has been interrupted use the text from
4773 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4774 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4775 * the same line as the cursor then fix it (the line has been split because it
4776 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4777 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004778 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004779 static void
4780ins_compl_continue_search(char_u *line)
4781{
4782 // it is a continued search
4783 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004784 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4785 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004786 {
4787 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4788 {
4789 // line (probably) wrapped, set compl_startpos to the
4790 // first non_blank in the line, if it is not a wordchar
4791 // include it to get a better pattern, but then we don't
4792 // want the "\\<" prefix, check it below
4793 compl_col = (colnr_T)getwhitecols(line);
4794 compl_startpos.col = compl_col;
4795 compl_startpos.lnum = curwin->w_cursor.lnum;
4796 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4797 }
4798 else
4799 {
4800 // S_IPOS was set when we inserted a word that was at the
4801 // beginning of the line, which means that we'll go to SOL
4802 // mode but first we need to redefine compl_startpos
4803 if (compl_cont_status & CONT_S_IPOS)
4804 {
4805 compl_cont_status |= CONT_SOL;
4806 compl_startpos.col = (colnr_T)(skipwhite(
4807 line + compl_length
4808 + compl_startpos.col) - line);
4809 }
4810 compl_col = compl_startpos.col;
4811 }
4812 compl_length = curwin->w_cursor.col - (int)compl_col;
4813 // IObuff is used to add a "word from the next line" would we
4814 // have enough space? just being paranoid
4815#define MIN_SPACE 75
4816 if (compl_length > (IOSIZE - MIN_SPACE))
4817 {
4818 compl_cont_status &= ~CONT_SOL;
4819 compl_length = (IOSIZE - MIN_SPACE);
4820 compl_col = curwin->w_cursor.col - compl_length;
4821 }
4822 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4823 if (compl_length < 1)
4824 compl_cont_status &= CONT_LOCAL;
4825 }
4826 else if (ctrl_x_mode_line_or_eval())
4827 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4828 else
4829 compl_cont_status = 0;
4830}
4831
4832/*
4833 * start insert mode completion
4834 */
4835 static int
4836ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004837{
4838 char_u *line;
4839 int startcol = 0; // column where searched text starts
4840 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004841 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004842 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004843 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004844
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004845 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004846
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004847 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004848 did_si = FALSE;
4849 can_si = FALSE;
4850 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004851 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004852 return FAIL;
4853
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004854 line = ml_get(curwin->w_cursor.lnum);
4855 curs_col = curwin->w_cursor.col;
4856 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004857
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004858 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4859 && compl_cont_mode == ctrl_x_mode)
4860 // this same ctrl-x_mode was interrupted previously. Continue the
4861 // completion.
4862 ins_compl_continue_search(line);
4863 else
4864 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004865
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004866 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004867 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004868 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004869 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004870 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4871 compl_cont_status = 0;
4872 compl_cont_status |= CONT_N_ADDS;
4873 compl_startpos = curwin->w_cursor;
4874 startcol = (int)curs_col;
4875 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876 }
4877
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004878 // Work out completion pattern and original text -- webb
4879 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4880 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004881 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4882 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004883 // restore did_ai, so that adding comment leader works
4884 did_ai = save_did_ai;
4885 return FAIL;
4886 }
4887 // If "line" was changed while getting completion info get it again.
4888 if (line_invalid)
4889 line = ml_get(curwin->w_cursor.lnum);
4890
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004891 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004892 {
4893 edit_submode_pre = (char_u *)_(" Adding");
4894 if (ctrl_x_mode_line_or_eval())
4895 {
4896 // Insert a new line, keep indentation but ignore 'comments'.
4897 char_u *old = curbuf->b_p_com;
4898
4899 curbuf->b_p_com = (char_u *)"";
4900 compl_startpos.lnum = curwin->w_cursor.lnum;
4901 compl_startpos.col = compl_col;
4902 ins_eol('\r');
4903 curbuf->b_p_com = old;
4904 compl_length = 0;
4905 compl_col = curwin->w_cursor.col;
4906 }
4907 }
4908 else
4909 {
4910 edit_submode_pre = NULL;
4911 compl_startpos.col = compl_col;
4912 }
4913
4914 if (compl_cont_status & CONT_LOCAL)
4915 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4916 else
4917 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4918
4919 // If any of the original typed text has been changed we need to fix
4920 // the redo buffer.
4921 ins_compl_fixRedoBufForLeader(NULL);
4922
4923 // Always add completion for the original text.
4924 vim_free(compl_orig_text);
4925 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4926 if (p_ic)
4927 flags |= CP_ICASE;
4928 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4929 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4930 {
4931 VIM_CLEAR(compl_pattern);
4932 VIM_CLEAR(compl_orig_text);
4933 return FAIL;
4934 }
4935
4936 // showmode might reset the internal line pointers, so it must
4937 // be called before line = ml_get(), or when this address is no
4938 // longer needed. -- Acevedo.
4939 edit_submode_extra = (char_u *)_("-- Searching...");
4940 edit_submode_highl = HLF_COUNT;
4941 showmode();
4942 edit_submode_extra = NULL;
4943 out_flush();
4944
4945 return OK;
4946}
4947
4948/*
4949 * display the completion status message
4950 */
4951 static void
4952ins_compl_show_statusmsg(void)
4953{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004954 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004955 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004956 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004957 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004958 ? (char_u *)_("Hit end of paragraph")
4959 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004960 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004961 }
4962
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004963 if (edit_submode_extra == NULL)
4964 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004965 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004966 {
4967 edit_submode_extra = (char_u *)_("Back at original");
4968 edit_submode_highl = HLF_W;
4969 }
4970 else if (compl_cont_status & CONT_S_IPOS)
4971 {
4972 edit_submode_extra = (char_u *)_("Word from other line");
4973 edit_submode_highl = HLF_COUNT;
4974 }
4975 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4976 {
4977 edit_submode_extra = (char_u *)_("The only match");
4978 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004979 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004980 }
4981 else
4982 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004983#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004984 // Update completion sequence number when needed.
4985 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004986 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004987#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004988 // The match should always have a sequence number now, this is
4989 // just a safety check.
4990 if (compl_curr_match->cp_number != -1)
4991 {
4992 // Space for 10 text chars. + 2x10-digit no.s = 31.
4993 // Translations may need more than twice that.
4994 static char_u match_ref[81];
4995
4996 if (compl_matches > 0)
4997 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004998 _("match %d of %d"),
4999 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005000 else
5001 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005002 _("match %d"),
5003 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005004 edit_submode_extra = match_ref;
5005 edit_submode_highl = HLF_R;
5006 if (dollar_vcol >= 0)
5007 curs_columns(FALSE);
5008 }
5009 }
5010 }
5011
5012 // Show a message about what (completion) mode we're in.
5013 if (!compl_opt_suppress_empty)
5014 {
5015 showmode();
5016 if (!shortmess(SHM_COMPLETIONMENU))
5017 {
5018 if (edit_submode_extra != NULL)
5019 {
5020 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005021 {
5022 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005023 msg_attr((char *)edit_submode_extra,
5024 edit_submode_highl < HLF_COUNT
5025 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005026 msg_hist_off = FALSE;
5027 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005028 }
5029 else
5030 msg_clr_cmdline(); // necessary for "noshowmode"
5031 }
5032 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005033}
5034
5035/*
5036 * Do Insert mode completion.
5037 * Called when character "c" was typed, which has a meaning for completion.
5038 * Returns OK if completion was done, FAIL if something failed (out of mem).
5039 */
5040 int
5041ins_complete(int c, int enable_pum)
5042{
5043 int n;
5044 int save_w_wrow;
5045 int save_w_leftcol;
5046 int insert_match;
5047
5048 compl_direction = ins_compl_key2dir(c);
5049 insert_match = ins_compl_use_match(c);
5050
5051 if (!compl_started)
5052 {
5053 if (ins_compl_start() == FAIL)
5054 return FAIL;
5055 }
5056 else if (insert_match && stop_arrow() == FAIL)
5057 return FAIL;
5058
5059 compl_shown_match = compl_curr_match;
5060 compl_shows_dir = compl_direction;
5061
5062 // Find next match (and following matches).
5063 save_w_wrow = curwin->w_wrow;
5064 save_w_leftcol = curwin->w_leftcol;
5065 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5066
5067 // may undisplay the popup menu
5068 ins_compl_upd_pum();
5069
5070 if (n > 1) // all matches have been found
5071 compl_matches = n;
5072 compl_curr_match = compl_shown_match;
5073 compl_direction = compl_shows_dir;
5074
5075 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5076 // mode.
5077 if (got_int && !global_busy)
5078 {
5079 (void)vgetc();
5080 got_int = FALSE;
5081 }
5082
5083 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005084 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005085 {
5086 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5087 // because we couldn't expand anything at first place, but if we used
5088 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5089 // (such as M in M'exico) if not tried already. -- Acevedo
5090 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005091 || compl_status_adding()
5092 || (ctrl_x_mode_not_default()
5093 && !ctrl_x_mode_path_patterns()
5094 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005095 compl_cont_status &= ~CONT_N_ADDS;
5096 }
5097
5098 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5099 compl_cont_status |= CONT_S_IPOS;
5100 else
5101 compl_cont_status &= ~CONT_S_IPOS;
5102
5103 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005104
5105 // Show the popup menu, unless we got interrupted.
5106 if (enable_pum && !compl_interrupted)
5107 show_pum(save_w_wrow, save_w_leftcol);
5108
5109 compl_was_interrupted = compl_interrupted;
5110 compl_interrupted = FALSE;
5111
5112 return OK;
5113}
5114
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005115/*
5116 * Remove (if needed) and show the popup menu
5117 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005118 static void
5119show_pum(int prev_w_wrow, int prev_w_leftcol)
5120{
5121 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005122 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005123 RedrawingDisabled = 0;
5124
5125 // If the cursor moved or the display scrolled we need to remove the pum
5126 // first.
5127 setcursor();
5128 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5129 ins_compl_del_pum();
5130
5131 ins_compl_show_pum();
5132 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005133
5134 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005135}
5136
5137/*
5138 * Looks in the first "len" chars. of "src" for search-metachars.
5139 * If dest is not NULL the chars. are copied there quoting (with
5140 * a backslash) the metachars, and dest would be NUL terminated.
5141 * Returns the length (needed) of dest
5142 */
5143 static unsigned
5144quote_meta(char_u *dest, char_u *src, int len)
5145{
5146 unsigned m = (unsigned)len + 1; // one extra for the NUL
5147
5148 for ( ; --len >= 0; src++)
5149 {
5150 switch (*src)
5151 {
5152 case '.':
5153 case '*':
5154 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005155 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005156 break;
5157 // FALLTHROUGH
5158 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005159 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005160 break;
5161 // FALLTHROUGH
5162 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005163 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005164 break;
5165 // FALLTHROUGH
5166 case '^': // currently it's not needed.
5167 case '$':
5168 m++;
5169 if (dest != NULL)
5170 *dest++ = '\\';
5171 break;
5172 }
5173 if (dest != NULL)
5174 *dest++ = *src;
5175 // Copy remaining bytes of a multibyte character.
5176 if (has_mbyte)
5177 {
5178 int i, mb_len;
5179
5180 mb_len = (*mb_ptr2len)(src) - 1;
5181 if (mb_len > 0 && len >= mb_len)
5182 for (i = 0; i < mb_len; ++i)
5183 {
5184 --len;
5185 ++src;
5186 if (dest != NULL)
5187 *dest++ = *src;
5188 }
5189 }
5190 }
5191 if (dest != NULL)
5192 *dest = NUL;
5193
5194 return m;
5195}
5196
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005197#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005198 void
5199free_insexpand_stuff(void)
5200{
5201 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005202# ifdef FEAT_EVAL
5203 free_callback(&cfu_cb);
5204 free_callback(&ofu_cb);
5205 free_callback(&tsrfu_cb);
5206# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005207}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005208#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005209
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005210#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005211/*
5212 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5213 * spelled word, if there is one.
5214 */
5215 static void
5216spell_back_to_badword(void)
5217{
5218 pos_T tpos = curwin->w_cursor;
5219
5220 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5221 if (curwin->w_cursor.col != tpos.col)
5222 start_arrow(&tpos);
5223}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005224#endif