blob: 63f2e3814d055e005079bb56f454742db8ad7cb5 [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)
glepnir0d3c0a62024-02-11 17:52:40 +01001353 {
1354#ifdef FEAT_EVAL
1355 if (compl_started && has_completechanged())
1356 trigger_complete_changed_event(cur);
1357#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001358 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001359 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001360
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001361 // In Replace mode when a $ is displayed at the end of the line only
1362 // part of the screen would be updated. We do need to redraw here.
1363 dollar_vcol = -1;
1364
1365 // Compute the screen column of the start of the completed text.
1366 // Use the cursor to get all wrapping and other settings right.
1367 col = curwin->w_cursor.col;
1368 curwin->w_cursor.col = compl_col;
1369 pum_display(compl_match_array, compl_match_arraysize, cur);
1370 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001371
glepnircbb46b42024-02-03 18:11:13 +01001372 // After adding leader, set the current match to shown match.
1373 if (compl_started && compl_curr_match != compl_shown_match)
1374 compl_curr_match = compl_shown_match;
1375
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001376#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001377 if (has_completechanged())
1378 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001379#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001380}
1381
1382#define DICT_FIRST (1) // use just first element in "dict"
1383#define DICT_EXACT (2) // "dict" is the exact name of a file
1384
1385/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001386 * Add any identifiers that match the given pattern "pat" in the list of
1387 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001388 */
1389 static void
1390ins_compl_dictionaries(
1391 char_u *dict_start,
1392 char_u *pat,
1393 int flags, // DICT_FIRST and/or DICT_EXACT
1394 int thesaurus) // Thesaurus completion
1395{
1396 char_u *dict = dict_start;
1397 char_u *ptr;
1398 char_u *buf;
1399 regmatch_T regmatch;
1400 char_u **files;
1401 int count;
1402 int save_p_scs;
1403 int dir = compl_direction;
1404
1405 if (*dict == NUL)
1406 {
1407#ifdef FEAT_SPELL
1408 // When 'dictionary' is empty and spell checking is enabled use
1409 // "spell".
1410 if (!thesaurus && curwin->w_p_spell)
1411 dict = (char_u *)"spell";
1412 else
1413#endif
1414 return;
1415 }
1416
1417 buf = alloc(LSIZE);
1418 if (buf == NULL)
1419 return;
1420 regmatch.regprog = NULL; // so that we can goto theend
1421
1422 // If 'infercase' is set, don't use 'smartcase' here
1423 save_p_scs = p_scs;
1424 if (curbuf->b_p_inf)
1425 p_scs = FALSE;
1426
1427 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1428 // to only match at the start of a line. Otherwise just match the
1429 // pattern. Also need to double backslashes.
1430 if (ctrl_x_mode_line_or_eval())
1431 {
1432 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1433 size_t len;
1434
1435 if (pat_esc == NULL)
1436 goto theend;
1437 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001438 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001439 if (ptr == NULL)
1440 {
1441 vim_free(pat_esc);
1442 goto theend;
1443 }
1444 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1445 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1446 vim_free(pat_esc);
1447 vim_free(ptr);
1448 }
1449 else
1450 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001451 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001452 if (regmatch.regprog == NULL)
1453 goto theend;
1454 }
1455
1456 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1457 regmatch.rm_ic = ignorecase(pat);
1458 while (*dict != NUL && !got_int && !compl_interrupted)
1459 {
1460 // copy one dictionary file name into buf
1461 if (flags == DICT_EXACT)
1462 {
1463 count = 1;
1464 files = &dict;
1465 }
1466 else
1467 {
1468 // Expand wildcards in the dictionary name, but do not allow
1469 // backticks (for security, the 'dict' option may have been set in
1470 // a modeline).
1471 copy_option_part(&dict, buf, LSIZE, ",");
1472# ifdef FEAT_SPELL
1473 if (!thesaurus && STRCMP(buf, "spell") == 0)
1474 count = -1;
1475 else
1476# endif
1477 if (vim_strchr(buf, '`') != NULL
1478 || expand_wildcards(1, &buf, &count, &files,
1479 EW_FILE|EW_SILENT) != OK)
1480 count = 0;
1481 }
1482
1483# ifdef FEAT_SPELL
1484 if (count == -1)
1485 {
1486 // Complete from active spelling. Skip "\<" in the pattern, we
1487 // don't use it as a RE.
1488 if (pat[0] == '\\' && pat[1] == '<')
1489 ptr = pat + 2;
1490 else
1491 ptr = pat;
1492 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1493 }
1494 else
1495# endif
1496 if (count > 0) // avoid warning for using "files" uninit
1497 {
1498 ins_compl_files(count, files, thesaurus, flags,
1499 &regmatch, buf, &dir);
1500 if (flags != DICT_EXACT)
1501 FreeWild(count, files);
1502 }
1503 if (flags != 0)
1504 break;
1505 }
1506
1507theend:
1508 p_scs = save_p_scs;
1509 vim_regfree(regmatch.regprog);
1510 vim_free(buf);
1511}
1512
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001513/*
1514 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1515 * skipping the word at 'skip_word'. Returns OK on success.
1516 */
1517 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001518thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001519 char_u *fname,
1520 char_u **buf_arg,
1521 int dir,
1522 char_u *skip_word)
1523{
1524 int status = OK;
1525 char_u *ptr;
1526 char_u *wstart;
1527
1528 // Add the other matches on the line
1529 ptr = *buf_arg;
1530 while (!got_int)
1531 {
1532 // Find start of the next word. Skip white
1533 // space and punctuation.
1534 ptr = find_word_start(ptr);
1535 if (*ptr == NUL || *ptr == NL)
1536 break;
1537 wstart = ptr;
1538
1539 // Find end of the word.
1540 if (has_mbyte)
1541 // Japanese words may have characters in
1542 // different classes, only separate words
1543 // with single-byte non-word characters.
1544 while (*ptr != NUL)
1545 {
1546 int l = (*mb_ptr2len)(ptr);
1547
1548 if (l < 2 && !vim_iswordc(*ptr))
1549 break;
1550 ptr += l;
1551 }
1552 else
1553 ptr = find_word_end(ptr);
1554
1555 // Add the word. Skip the regexp match.
1556 if (wstart != skip_word)
1557 {
1558 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1559 fname, dir, FALSE);
1560 if (status == FAIL)
1561 break;
1562 }
1563 }
1564
1565 *buf_arg = ptr;
1566 return status;
1567}
1568
1569/*
1570 * Process "count" dictionary/thesaurus "files" and add the text matching
1571 * "regmatch".
1572 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001573 static void
1574ins_compl_files(
1575 int count,
1576 char_u **files,
1577 int thesaurus,
1578 int flags,
1579 regmatch_T *regmatch,
1580 char_u *buf,
1581 int *dir)
1582{
1583 char_u *ptr;
1584 int i;
1585 FILE *fp;
1586 int add_r;
1587
1588 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1589 {
1590 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001591 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001592 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001593 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001594 vim_snprintf((char *)IObuff, IOSIZE,
1595 _("Scanning dictionary: %s"), (char *)files[i]);
1596 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1597 }
1598
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001599 if (fp == NULL)
1600 continue;
1601
1602 // Read dictionary file line by line.
1603 // Check each line for a match.
1604 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001605 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001606 ptr = buf;
1607 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001608 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001609 ptr = regmatch->startp[0];
1610 if (ctrl_x_mode_line_or_eval())
1611 ptr = find_line_end(ptr);
1612 else
1613 ptr = find_word_end(ptr);
1614 add_r = ins_compl_add_infercase(regmatch->startp[0],
1615 (int)(ptr - regmatch->startp[0]),
1616 p_ic, files[i], *dir, FALSE);
1617 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001618 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001619 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001620 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001621 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001622 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001623 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001624 if (add_r == OK)
1625 // if dir was BACKWARD then honor it just once
1626 *dir = FORWARD;
1627 else if (add_r == FAIL)
1628 break;
1629 // avoid expensive call to vim_regexec() when at end
1630 // of line
1631 if (*ptr == '\n' || got_int)
1632 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001633 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001634 line_breakcheck();
1635 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001636 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001637 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001638 }
1639}
1640
1641/*
1642 * Find the start of the next word.
1643 * Returns a pointer to the first char of the word. Also stops at a NUL.
1644 */
1645 char_u *
1646find_word_start(char_u *ptr)
1647{
1648 if (has_mbyte)
1649 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1650 ptr += (*mb_ptr2len)(ptr);
1651 else
1652 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1653 ++ptr;
1654 return ptr;
1655}
1656
1657/*
1658 * Find the end of the word. Assumes it starts inside a word.
1659 * Returns a pointer to just after the word.
1660 */
1661 char_u *
1662find_word_end(char_u *ptr)
1663{
1664 int start_class;
1665
1666 if (has_mbyte)
1667 {
1668 start_class = mb_get_class(ptr);
1669 if (start_class > 1)
1670 while (*ptr != NUL)
1671 {
1672 ptr += (*mb_ptr2len)(ptr);
1673 if (mb_get_class(ptr) != start_class)
1674 break;
1675 }
1676 }
1677 else
1678 while (vim_iswordc(*ptr))
1679 ++ptr;
1680 return ptr;
1681}
1682
1683/*
1684 * Find the end of the line, omitting CR and NL at the end.
1685 * Returns a pointer to just after the line.
1686 */
1687 static char_u *
1688find_line_end(char_u *ptr)
1689{
1690 char_u *s;
1691
1692 s = ptr + STRLEN(ptr);
1693 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1694 --s;
1695 return s;
1696}
1697
1698/*
1699 * Free the list of completions
1700 */
1701 static void
1702ins_compl_free(void)
1703{
1704 compl_T *match;
1705 int i;
1706
1707 VIM_CLEAR(compl_pattern);
1708 VIM_CLEAR(compl_leader);
1709
1710 if (compl_first_match == NULL)
1711 return;
1712
1713 ins_compl_del_pum();
1714 pum_clear();
1715
1716 compl_curr_match = compl_first_match;
1717 do
1718 {
1719 match = compl_curr_match;
1720 compl_curr_match = compl_curr_match->cp_next;
1721 vim_free(match->cp_str);
1722 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001723 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001724 vim_free(match->cp_fname);
1725 for (i = 0; i < CPT_COUNT; ++i)
1726 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001727#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001728 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001729#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001730 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001731 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001732 compl_first_match = compl_curr_match = NULL;
1733 compl_shown_match = NULL;
1734 compl_old_match = NULL;
1735}
1736
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001737/*
1738 * Reset/clear the completion state.
1739 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001740 void
1741ins_compl_clear(void)
1742{
1743 compl_cont_status = 0;
1744 compl_started = FALSE;
1745 compl_matches = 0;
1746 VIM_CLEAR(compl_pattern);
1747 VIM_CLEAR(compl_leader);
1748 edit_submode_extra = NULL;
1749 VIM_CLEAR(compl_orig_text);
1750 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001751#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001752 // clear v:completed_item
1753 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001754#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001755}
1756
1757/*
1758 * Return TRUE when Insert completion is active.
1759 */
1760 int
1761ins_compl_active(void)
1762{
1763 return compl_started;
1764}
1765
1766/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001767 * Selected one of the matches. When FALSE the match was edited or using the
1768 * longest common string.
1769 */
1770 int
1771ins_compl_used_match(void)
1772{
1773 return compl_used_match;
1774}
1775
1776/*
1777 * Initialize get longest common string.
1778 */
1779 void
1780ins_compl_init_get_longest(void)
1781{
1782 compl_get_longest = FALSE;
1783}
1784
1785/*
1786 * Returns TRUE when insert completion is interrupted.
1787 */
1788 int
1789ins_compl_interrupted(void)
1790{
1791 return compl_interrupted;
1792}
1793
1794/*
1795 * Returns TRUE if the <Enter> key selects a match in the completion popup
1796 * menu.
1797 */
1798 int
1799ins_compl_enter_selects(void)
1800{
1801 return compl_enter_selects;
1802}
1803
1804/*
1805 * Return the column where the text starts that is being completed
1806 */
1807 colnr_T
1808ins_compl_col(void)
1809{
1810 return compl_col;
1811}
1812
1813/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001814 * Return the length in bytes of the text being completed
1815 */
1816 int
1817ins_compl_len(void)
1818{
1819 return compl_length;
1820}
1821
1822/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001823 * Delete one character before the cursor and show the subset of the matches
1824 * that match the word that is now before the cursor.
1825 * Returns the character to be used, NUL if the work is done and another char
1826 * to be got from the user.
1827 */
1828 int
1829ins_compl_bs(void)
1830{
1831 char_u *line;
1832 char_u *p;
1833
1834 line = ml_get_curline();
1835 p = line + curwin->w_cursor.col;
1836 MB_PTR_BACK(line, p);
1837
1838 // Stop completion when the whole word was deleted. For Omni completion
1839 // allow the word to be deleted, we won't match everything.
1840 // Respect the 'backspace' option.
1841 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001842 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1843 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001844 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1845 - compl_length < 0))
1846 return K_BS;
1847
1848 // Deleted more than what was used to find matches or didn't finish
1849 // finding all matches: need to look for matches all over again.
1850 if (curwin->w_cursor.col <= compl_col + compl_length
1851 || ins_compl_need_restart())
1852 ins_compl_restart();
1853
1854 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001855 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001856 if (compl_leader == NULL)
1857 return K_BS;
1858
1859 ins_compl_new_leader();
1860 if (compl_shown_match != NULL)
1861 // Make sure current match is not a hidden item.
1862 compl_curr_match = compl_shown_match;
1863 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001864}
1865
1866/*
1867 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1868 * be called.
1869 */
1870 static int
1871ins_compl_need_restart(void)
1872{
1873 // Return TRUE if we didn't complete finding matches or when the
1874 // 'completefunc' returned "always" in the "refresh" dictionary item.
1875 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001876 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001877 && compl_opt_refresh_always);
1878}
1879
1880/*
1881 * Called after changing "compl_leader".
1882 * Show the popup menu with a different set of matches.
1883 * May also search for matches again if the previous search was interrupted.
1884 */
1885 static void
1886ins_compl_new_leader(void)
1887{
1888 ins_compl_del_pum();
1889 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001890 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001891 compl_used_match = FALSE;
1892
1893 if (compl_started)
1894 ins_compl_set_original_text(compl_leader);
1895 else
1896 {
1897#ifdef FEAT_SPELL
1898 spell_bad_len = 0; // need to redetect bad word
1899#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001900 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001901 // the popup menu display the changed text before the cursor. Set
1902 // "compl_restarting" to avoid that the first match is inserted.
1903 pum_call_update_screen();
1904#ifdef FEAT_GUI
1905 if (gui.in_use)
1906 {
1907 // Show the cursor after the match, not after the redrawn text.
1908 setcursor();
1909 out_flush_cursor(FALSE, FALSE);
1910 }
1911#endif
1912 compl_restarting = TRUE;
1913 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1914 compl_cont_status = 0;
1915 compl_restarting = FALSE;
1916 }
1917
1918 compl_enter_selects = !compl_used_match;
1919
1920 // Show the popup menu with a different set of matches.
1921 ins_compl_show_pum();
1922
1923 // Don't let Enter select the original text when there is no popup menu.
1924 if (compl_match_array == NULL)
1925 compl_enter_selects = FALSE;
1926}
1927
1928/*
1929 * Return the length of the completion, from the completion start column to
1930 * the cursor column. Making sure it never goes below zero.
1931 */
1932 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001933get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001934{
1935 int off = (int)curwin->w_cursor.col - (int)compl_col;
1936
1937 if (off < 0)
1938 return 0;
1939 return off;
1940}
1941
1942/*
1943 * Append one character to the match leader. May reduce the number of
1944 * matches.
1945 */
1946 void
1947ins_compl_addleader(int c)
1948{
1949 int cc;
1950
1951 if (stop_arrow() == FAIL)
1952 return;
1953 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1954 {
1955 char_u buf[MB_MAXBYTES + 1];
1956
1957 (*mb_char2bytes)(c, buf);
1958 buf[cc] = NUL;
1959 ins_char_bytes(buf, cc);
1960 if (compl_opt_refresh_always)
1961 AppendToRedobuff(buf);
1962 }
1963 else
1964 {
1965 ins_char(c);
1966 if (compl_opt_refresh_always)
1967 AppendCharToRedobuff(c);
1968 }
1969
1970 // If we didn't complete finding matches we must search again.
1971 if (ins_compl_need_restart())
1972 ins_compl_restart();
1973
1974 // When 'always' is set, don't reset compl_leader. While completing,
1975 // cursor doesn't point original position, changing compl_leader would
1976 // break redo.
1977 if (!compl_opt_refresh_always)
1978 {
1979 vim_free(compl_leader);
1980 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001981 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001982 if (compl_leader != NULL)
1983 ins_compl_new_leader();
1984 }
1985}
1986
1987/*
1988 * Setup for finding completions again without leaving CTRL-X mode. Used when
1989 * BS or a key was typed while still searching for matches.
1990 */
1991 static void
1992ins_compl_restart(void)
1993{
1994 ins_compl_free();
1995 compl_started = FALSE;
1996 compl_matches = 0;
1997 compl_cont_status = 0;
1998 compl_cont_mode = 0;
1999}
2000
2001/*
2002 * Set the first match, the original text.
2003 */
2004 static void
2005ins_compl_set_original_text(char_u *str)
2006{
2007 char_u *p;
2008
2009 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002010 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2011 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002012 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002013 {
2014 p = vim_strsave(str);
2015 if (p != NULL)
2016 {
2017 vim_free(compl_first_match->cp_str);
2018 compl_first_match->cp_str = p;
2019 }
2020 }
2021 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002022 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002023 {
2024 p = vim_strsave(str);
2025 if (p != NULL)
2026 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002027 vim_free(compl_first_match->cp_prev->cp_str);
2028 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002029 }
2030 }
2031}
2032
2033/*
2034 * Append one character to the match leader. May reduce the number of
2035 * matches.
2036 */
2037 void
2038ins_compl_addfrommatch(void)
2039{
2040 char_u *p;
2041 int len = (int)curwin->w_cursor.col - (int)compl_col;
2042 int c;
2043 compl_T *cp;
2044
2045 p = compl_shown_match->cp_str;
2046 if ((int)STRLEN(p) <= len) // the match is too short
2047 {
2048 // When still at the original match use the first entry that matches
2049 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002050 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002051 return;
2052
2053 p = NULL;
2054 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002055 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002056 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002057 if (compl_leader == NULL
2058 || ins_compl_equal(cp, compl_leader,
2059 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002060 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002061 p = cp->cp_str;
2062 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002063 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002064 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002065 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002066 return;
2067 }
2068 p += len;
2069 c = PTR2CHAR(p);
2070 ins_compl_addleader(c);
2071}
2072
2073/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002074 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002075 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2076 * compl_cont_mode and compl_cont_status.
2077 * Returns TRUE when the character is not to be inserted.
2078 */
2079 static int
2080set_ctrl_x_mode(int c)
2081{
2082 int retval = FALSE;
2083
2084 switch (c)
2085 {
2086 case Ctrl_E:
2087 case Ctrl_Y:
2088 // scroll the window one line up or down
2089 ctrl_x_mode = CTRL_X_SCROLL;
2090 if (!(State & REPLACE_FLAG))
2091 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2092 else
2093 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2094 edit_submode_pre = NULL;
2095 showmode();
2096 break;
2097 case Ctrl_L:
2098 // complete whole line
2099 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2100 break;
2101 case Ctrl_F:
2102 // complete filenames
2103 ctrl_x_mode = CTRL_X_FILES;
2104 break;
2105 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002106 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002107 ctrl_x_mode = CTRL_X_DICTIONARY;
2108 break;
2109 case Ctrl_R:
2110 // Register insertion without exiting CTRL-X mode
2111 // Simply allow ^R to happen without affecting ^X mode
2112 break;
2113 case Ctrl_T:
2114 // complete words from a thesaurus
2115 ctrl_x_mode = CTRL_X_THESAURUS;
2116 break;
2117#ifdef FEAT_COMPL_FUNC
2118 case Ctrl_U:
2119 // user defined completion
2120 ctrl_x_mode = CTRL_X_FUNCTION;
2121 break;
2122 case Ctrl_O:
2123 // omni completion
2124 ctrl_x_mode = CTRL_X_OMNI;
2125 break;
2126#endif
2127 case 's':
2128 case Ctrl_S:
2129 // complete spelling suggestions
2130 ctrl_x_mode = CTRL_X_SPELL;
2131#ifdef FEAT_SPELL
2132 ++emsg_off; // Avoid getting the E756 error twice.
2133 spell_back_to_badword();
2134 --emsg_off;
2135#endif
2136 break;
2137 case Ctrl_RSB:
2138 // complete tag names
2139 ctrl_x_mode = CTRL_X_TAGS;
2140 break;
2141#ifdef FEAT_FIND_ID
2142 case Ctrl_I:
2143 case K_S_TAB:
2144 // complete keywords from included files
2145 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2146 break;
2147 case Ctrl_D:
2148 // complete definitions from included files
2149 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2150 break;
2151#endif
2152 case Ctrl_V:
2153 case Ctrl_Q:
2154 // complete vim commands
2155 ctrl_x_mode = CTRL_X_CMDLINE;
2156 break;
2157 case Ctrl_Z:
2158 // stop completion
2159 ctrl_x_mode = CTRL_X_NORMAL;
2160 edit_submode = NULL;
2161 showmode();
2162 retval = TRUE;
2163 break;
2164 case Ctrl_P:
2165 case Ctrl_N:
2166 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2167 // just started ^X mode, or there were enough ^X's to cancel
2168 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2169 // do normal expansion when interrupting a different mode (say
2170 // ^X^F^X^P or ^P^X^X^P, see below)
2171 // nothing changes if interrupting mode 0, (eg, the flag
2172 // doesn't change when going to ADDING mode -- Acevedo
2173 if (!(compl_cont_status & CONT_INTRPT))
2174 compl_cont_status |= CONT_LOCAL;
2175 else if (compl_cont_mode != 0)
2176 compl_cont_status &= ~CONT_LOCAL;
2177 // FALLTHROUGH
2178 default:
2179 // If we have typed at least 2 ^X's... for modes != 0, we set
2180 // compl_cont_status = 0 (eg, as if we had just started ^X
2181 // mode).
2182 // For mode 0, we set "compl_cont_mode" to an impossible
2183 // value, in both cases ^X^X can be used to restart the same
2184 // mode (avoiding ADDING mode).
2185 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2186 // 'complete' and local ^P expansions respectively.
2187 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2188 // mode -- Acevedo
2189 if (c == Ctrl_X)
2190 {
2191 if (compl_cont_mode != 0)
2192 compl_cont_status = 0;
2193 else
2194 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2195 }
2196 ctrl_x_mode = CTRL_X_NORMAL;
2197 edit_submode = NULL;
2198 showmode();
2199 break;
2200 }
2201
2202 return retval;
2203}
2204
2205/*
2206 * Stop insert completion mode
2207 */
2208 static int
2209ins_compl_stop(int c, int prev_mode, int retval)
2210{
2211 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002212 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002213
2214 // Get here when we have finished typing a sequence of ^N and
2215 // ^P or other completion characters in CTRL-X mode. Free up
2216 // memory that was used, and make sure we can redo the insert.
2217 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2218 {
2219 // If any of the original typed text has been changed, eg when
2220 // ignorecase is set, we must add back-spaces to the redo
2221 // buffer. We add as few as necessary to delete just the part
2222 // of the original text that has changed.
2223 // When using the longest match, edited the match or used
2224 // CTRL-E then don't use the current match.
2225 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2226 ptr = compl_curr_match->cp_str;
2227 else
2228 ptr = NULL;
2229 ins_compl_fixRedoBufForLeader(ptr);
2230 }
2231
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002232 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002233
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002234 // When completing whole lines: fix indent for 'cindent'.
2235 // Otherwise, break line if it's too long.
2236 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2237 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002238 // re-indent the current line
2239 if (want_cindent)
2240 {
2241 do_c_expr_indent();
2242 want_cindent = FALSE; // don't do it again
2243 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002244 }
2245 else
2246 {
2247 int prev_col = curwin->w_cursor.col;
2248
2249 // put the cursor on the last char, for 'tw' formatting
2250 if (prev_col > 0)
2251 dec_cursor();
2252 // only format when something was inserted
2253 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2254 insertchar(NUL, 0, -1);
2255 if (prev_col > 0
2256 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2257 inc_cursor();
2258 }
2259
2260 // If the popup menu is displayed pressing CTRL-Y means accepting
2261 // the selection without inserting anything. When
2262 // compl_enter_selects is set the Enter key does the same.
2263 if ((c == Ctrl_Y || (compl_enter_selects
2264 && (c == CAR || c == K_KENTER || c == NL)))
2265 && pum_visible())
2266 retval = TRUE;
2267
2268 // CTRL-E means completion is Ended, go back to the typed text.
2269 // but only do this, if the Popup is still visible
2270 if (c == Ctrl_E)
2271 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002272 char_u *p = NULL;
2273
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002274 ins_compl_delete();
2275 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002276 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002277 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002278 p = compl_orig_text;
2279 if (p != NULL)
2280 {
2281 int compl_len = get_compl_len();
2282 int len = (int)STRLEN(p);
2283
2284 if (len > compl_len)
2285 ins_bytes_len(p + compl_len, len - compl_len);
2286 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002287 retval = TRUE;
2288 }
2289
2290 auto_format(FALSE, TRUE);
2291
2292 // Trigger the CompleteDonePre event to give scripts a chance to
2293 // act upon the completion before clearing the info, and restore
2294 // ctrl_x_mode, so that complete_info() can be used.
2295 ctrl_x_mode = prev_mode;
2296 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2297
2298 ins_compl_free();
2299 compl_started = FALSE;
2300 compl_matches = 0;
2301 if (!shortmess(SHM_COMPLETIONMENU))
2302 msg_clr_cmdline(); // necessary for "noshowmode"
2303 ctrl_x_mode = CTRL_X_NORMAL;
2304 compl_enter_selects = FALSE;
2305 if (edit_submode != NULL)
2306 {
2307 edit_submode = NULL;
2308 showmode();
2309 }
2310
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002311 if (c == Ctrl_C && cmdwin_type != 0)
2312 // Avoid the popup menu remains displayed when leaving the
2313 // command line window.
2314 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002315 // Indent now if a key was typed that is in 'cinkeys'.
2316 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2317 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002318 // Trigger the CompleteDone event to give scripts a chance to act
2319 // upon the end of completion.
2320 ins_apply_autocmds(EVENT_COMPLETEDONE);
2321
2322 return retval;
2323}
2324
2325/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002326 * Prepare for Insert mode completion, or stop it.
2327 * Called just after typing a character in Insert mode.
2328 * Returns TRUE when the character is not to be inserted;
2329 */
2330 int
2331ins_compl_prep(int c)
2332{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002333 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002334 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002335
2336 // Forget any previous 'special' messages if this is actually
2337 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2338 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2339 edit_submode_extra = NULL;
2340
zeertzjq440d4cb2023-03-02 17:51:32 +00002341 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002342 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002343 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002344 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002345 return retval;
2346
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002347#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002348 // Ignore mouse events in a popup window
2349 if (is_mouse_key(c))
2350 {
2351 // Ignore drag and release events, the position does not need to be in
2352 // the popup and it may have just closed.
2353 if (c == K_LEFTRELEASE
2354 || c == K_LEFTRELEASE_NM
2355 || c == K_MIDDLERELEASE
2356 || c == K_RIGHTRELEASE
2357 || c == K_X1RELEASE
2358 || c == K_X2RELEASE
2359 || c == K_LEFTDRAG
2360 || c == K_MIDDLEDRAG
2361 || c == K_RIGHTDRAG
2362 || c == K_X1DRAG
2363 || c == K_X2DRAG)
2364 return retval;
2365 if (popup_visible)
2366 {
2367 int row = mouse_row;
2368 int col = mouse_col;
2369 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2370
2371 if (wp != NULL && WIN_IS_POPUP(wp))
2372 return retval;
2373 }
2374 }
2375#endif
2376
zeertzjqdca29d92021-08-31 19:12:51 +02002377 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2378 {
2379 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2380 || !vim_is_ctrl_x_key(c))
2381 {
2382 // Not starting another completion mode.
2383 ctrl_x_mode = CTRL_X_CMDLINE;
2384
2385 // CTRL-X CTRL-Z should stop completion without inserting anything
2386 if (c == Ctrl_Z)
2387 retval = TRUE;
2388 }
2389 else
2390 {
2391 ctrl_x_mode = CTRL_X_CMDLINE;
2392
2393 // Other CTRL-X keys first stop completion, then start another
2394 // completion mode.
2395 ins_compl_prep(' ');
2396 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2397 }
2398 }
2399
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002400 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002401 if (ctrl_x_mode_not_defined_yet()
2402 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002403 {
bfredl87af60c2022-09-24 11:17:51 +01002404 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002405 compl_used_match = TRUE;
2406
2407 }
2408
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002409 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002410 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2411 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002412 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002413 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002414 {
2415 // We're already in CTRL-X mode, do we stay in it?
2416 if (!vim_is_ctrl_x_key(c))
2417 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002418 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002419 ctrl_x_mode = CTRL_X_NORMAL;
2420 else
2421 ctrl_x_mode = CTRL_X_FINISHED;
2422 edit_submode = NULL;
2423 }
2424 showmode();
2425 }
2426
2427 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2428 {
2429 // Show error message from attempted keyword completion (probably
2430 // 'Pattern not found') until another key is hit, then go back to
2431 // showing what mode we are in.
2432 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002433 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002434 && c != Ctrl_R && !ins_compl_pum_key(c))
2435 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002436 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002437 }
2438 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2439 // Trigger the CompleteDone event to give scripts a chance to act
2440 // upon the (possibly failed) completion.
2441 ins_apply_autocmds(EVENT_COMPLETEDONE);
2442
LemonBoy2bf52dd2022-04-09 18:17:34 +01002443 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002444
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002445 // reset continue_* if we left expansion-mode, if we stay they'll be
2446 // (re)set properly in ins_complete()
2447 if (!vim_is_ctrl_x_key(c))
2448 {
2449 compl_cont_status = 0;
2450 compl_cont_mode = 0;
2451 }
2452
2453 return retval;
2454}
2455
2456/*
2457 * Fix the redo buffer for the completion leader replacing some of the typed
2458 * text. This inserts backspaces and appends the changed text.
2459 * "ptr" is the known leader text or NUL.
2460 */
2461 static void
2462ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2463{
2464 int len;
2465 char_u *p;
2466 char_u *ptr = ptr_arg;
2467
2468 if (ptr == NULL)
2469 {
2470 if (compl_leader != NULL)
2471 ptr = compl_leader;
2472 else
2473 return; // nothing to do
2474 }
2475 if (compl_orig_text != NULL)
2476 {
2477 p = compl_orig_text;
2478 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2479 ;
2480 if (len > 0)
2481 len -= (*mb_head_off)(p, p + len);
2482 for (p += len; *p != NUL; MB_PTR_ADV(p))
2483 AppendCharToRedobuff(K_BS);
2484 }
2485 else
2486 len = 0;
2487 if (ptr != NULL)
2488 AppendToRedobuffLit(ptr + len, -1);
2489}
2490
2491/*
2492 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2493 * (depending on flag) starting from buf and looking for a non-scanned
2494 * buffer (other than curbuf). curbuf is special, if it is called with
2495 * buf=curbuf then it has to be the first call for a given flag/expansion.
2496 *
2497 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2498 */
2499 static buf_T *
2500ins_compl_next_buf(buf_T *buf, int flag)
2501{
2502 static win_T *wp = NULL;
2503
2504 if (flag == 'w') // just windows
2505 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002506 if (buf == curbuf || !win_valid(wp))
2507 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002508 wp = curwin;
2509 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2510 && wp->w_buffer->b_scanned)
2511 ;
2512 buf = wp->w_buffer;
2513 }
2514 else
2515 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2516 // (unlisted buffers)
2517 // When completing whole lines skip unloaded buffers.
2518 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2519 && ((flag == 'U'
2520 ? buf->b_p_bl
2521 : (!buf->b_p_bl
2522 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2523 || buf->b_scanned))
2524 ;
2525 return buf;
2526}
2527
2528#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002529
2530# ifdef FEAT_EVAL
2531static callback_T cfu_cb; // 'completefunc' callback function
2532static callback_T ofu_cb; // 'omnifunc' callback function
2533static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2534# endif
2535
2536/*
2537 * Copy a global callback function to a buffer local callback.
2538 */
2539 static void
2540copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2541{
2542 free_callback(bufcb);
2543 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2544 copy_callback(bufcb, globcb);
2545}
2546
2547/*
2548 * Parse the 'completefunc' option value and set the callback function.
2549 * Invoked when the 'completefunc' option is set. The option value can be a
2550 * name of a function (string), or function(<name>) or funcref(<name>) or a
2551 * lambda expression.
2552 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002553 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002554did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002555{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002556 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2557 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002558
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002560
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002561 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002562}
2563
2564/*
2565 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002566 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002567 */
2568 void
2569set_buflocal_cfu_callback(buf_T *buf UNUSED)
2570{
2571# ifdef FEAT_EVAL
2572 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2573# endif
2574}
2575
2576/*
2577 * Parse the 'omnifunc' option value and set the callback function.
2578 * Invoked when the 'omnifunc' option is set. The option value can be a
2579 * name of a function (string), or function(<name>) or funcref(<name>) or a
2580 * lambda expression.
2581 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002582 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002583did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002584{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002585 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2586 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002587
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002588 set_buflocal_ofu_callback(curbuf);
2589 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002590}
2591
2592/*
2593 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002594 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002595 */
2596 void
2597set_buflocal_ofu_callback(buf_T *buf UNUSED)
2598{
2599# ifdef FEAT_EVAL
2600 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2601# endif
2602}
2603
2604/*
2605 * Parse the 'thesaurusfunc' option value and set the callback function.
2606 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2607 * name of a function (string), or function(<name>) or funcref(<name>) or a
2608 * lambda expression.
2609 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002610 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002611did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002612{
2613 int retval;
2614
2615 if (*curbuf->b_p_tsrfu != NUL)
2616 {
2617 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002618 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2619 &curbuf->b_tsrfu_cb);
2620 }
2621 else
2622 {
2623 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002624 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2625 }
2626
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002627 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002628}
2629
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002630/*
2631 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002632 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002633 */
2634 int
2635set_ref_in_insexpand_funcs(int copyID)
2636{
2637 int abort = FALSE;
2638
2639 abort = set_ref_in_callback(&cfu_cb, copyID);
2640 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2641 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2642
2643 return abort;
2644}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002645
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002646/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002647 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002648 */
2649 static char_u *
2650get_complete_funcname(int type)
2651{
2652 switch (type)
2653 {
2654 case CTRL_X_FUNCTION:
2655 return curbuf->b_p_cfu;
2656 case CTRL_X_OMNI:
2657 return curbuf->b_p_ofu;
2658 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002659 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002660 default:
2661 return (char_u *)"";
2662 }
2663}
2664
2665/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002666 * Get the callback to use for insert mode completion.
2667 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002668 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002669get_insert_callback(int type)
2670{
2671 if (type == CTRL_X_FUNCTION)
2672 return &curbuf->b_cfu_cb;
2673 if (type == CTRL_X_OMNI)
2674 return &curbuf->b_ofu_cb;
2675 // CTRL_X_THESAURUS
2676 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2677}
2678
2679/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002680 * Execute user defined complete function 'completefunc', 'omnifunc' or
2681 * 'thesaurusfunc', and get matches in "matches".
2682 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002683 */
2684 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002685expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002686{
2687 list_T *matchlist = NULL;
2688 dict_T *matchdict = NULL;
2689 typval_T args[3];
2690 char_u *funcname;
2691 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002692 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002693 typval_T rettv;
2694 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002695 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002696
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002697 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002698 if (*funcname == NUL)
2699 return;
2700
2701 // Call 'completefunc' to obtain the list of matches.
2702 args[0].v_type = VAR_NUMBER;
2703 args[0].vval.v_number = 0;
2704 args[1].v_type = VAR_STRING;
2705 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2706 args[2].v_type = VAR_UNKNOWN;
2707
2708 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002709 // Lock the text to avoid weird things from happening. Also disallow
2710 // switching to another window, it should not be needed and may end up in
2711 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002712 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002713
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002714 cb = get_insert_callback(type);
2715 retval = call_callback(cb, 0, &rettv, 2, args);
2716
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002717 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002718 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002719 {
2720 switch (rettv.v_type)
2721 {
2722 case VAR_LIST:
2723 matchlist = rettv.vval.v_list;
2724 break;
2725 case VAR_DICT:
2726 matchdict = rettv.vval.v_dict;
2727 break;
2728 case VAR_SPECIAL:
2729 if (rettv.vval.v_number == VVAL_NONE)
2730 compl_opt_suppress_empty = TRUE;
2731 // FALLTHROUGH
2732 default:
2733 // TODO: Give error message?
2734 clear_tv(&rettv);
2735 break;
2736 }
2737 }
zeertzjqcfe45652022-05-27 17:26:55 +01002738 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002739
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002740 curwin->w_cursor = pos; // restore the cursor position
2741 validate_cursor();
2742 if (!EQUAL_POS(curwin->w_cursor, pos))
2743 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002744 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002745 goto theend;
2746 }
2747
2748 if (matchlist != NULL)
2749 ins_compl_add_list(matchlist);
2750 else if (matchdict != NULL)
2751 ins_compl_add_dict(matchdict);
2752
2753theend:
2754 // Restore State, it might have been changed.
2755 State = save_State;
2756
2757 if (matchdict != NULL)
2758 dict_unref(matchdict);
2759 if (matchlist != NULL)
2760 list_unref(matchlist);
2761}
2762#endif // FEAT_COMPL_FUNC
2763
2764#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2765/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002766 * Add a match to the list of matches from a typeval_T.
2767 * If the given string is already in the list of completions, then return
2768 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2769 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002770 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002771 */
2772 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002773ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002774{
2775 char_u *word;
2776 int dup = FALSE;
2777 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002778 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002779 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002780 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002781 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002782
Bram Moolenaar08928322020-01-04 14:32:48 +01002783 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002784 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2785 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002786 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2787 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2788 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2789 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2790 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2791 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2792 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2793 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002794 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002795 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2796 dup = dict_get_number(tv->vval.v_dict, "dup");
2797 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2798 empty = dict_get_number(tv->vval.v_dict, "empty");
2799 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2800 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002801 flags |= CP_EQUAL;
2802 }
2803 else
2804 {
2805 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002806 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002807 }
2808 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002809 {
2810 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002811 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002812 }
2813 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2814 if (status != OK)
2815 clear_tv(&user_data);
2816 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002817}
2818
2819/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002820 * Add completions from a list.
2821 */
2822 static void
2823ins_compl_add_list(list_T *list)
2824{
2825 listitem_T *li;
2826 int dir = compl_direction;
2827
2828 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002829 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002830 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002831 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002832 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002833 // if dir was BACKWARD then honor it just once
2834 dir = FORWARD;
2835 else if (did_emsg)
2836 break;
2837 }
2838}
2839
2840/*
2841 * Add completions from a dict.
2842 */
2843 static void
2844ins_compl_add_dict(dict_T *dict)
2845{
2846 dictitem_T *di_refresh;
2847 dictitem_T *di_words;
2848
2849 // Check for optional "refresh" item.
2850 compl_opt_refresh_always = FALSE;
2851 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2852 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2853 {
2854 char_u *v = di_refresh->di_tv.vval.v_string;
2855
2856 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2857 compl_opt_refresh_always = TRUE;
2858 }
2859
2860 // Add completions from a "words" list.
2861 di_words = dict_find(dict, (char_u *)"words", 5);
2862 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2863 ins_compl_add_list(di_words->di_tv.vval.v_list);
2864}
2865
2866/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002867 * Start completion for the complete() function.
2868 * "startcol" is where the matched text starts (1 is first column).
2869 * "list" is the list of matches.
2870 */
2871 static void
2872set_completion(colnr_T startcol, list_T *list)
2873{
2874 int save_w_wrow = curwin->w_wrow;
2875 int save_w_leftcol = curwin->w_leftcol;
2876 int flags = CP_ORIGINAL_TEXT;
2877
2878 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002879 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002880 ins_compl_prep(' ');
2881 ins_compl_clear();
2882 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002883 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002884
2885 compl_direction = FORWARD;
2886 if (startcol > curwin->w_cursor.col)
2887 startcol = curwin->w_cursor.col;
2888 compl_col = startcol;
2889 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2890 // compl_pattern doesn't need to be set
2891 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2892 if (p_ic)
2893 flags |= CP_ICASE;
2894 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002895 -1, NULL, NULL, NULL, 0,
2896 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002897 return;
2898
2899 ctrl_x_mode = CTRL_X_EVAL;
2900
2901 ins_compl_add_list(list);
2902 compl_matches = ins_compl_make_cyclic();
2903 compl_started = TRUE;
2904 compl_used_match = TRUE;
2905 compl_cont_status = 0;
2906
2907 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002908 int no_select = compl_no_select || compl_longest;
2909 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002910 {
2911 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002912 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002913 // Down/Up has no real effect.
2914 ins_complete(K_UP, FALSE);
2915 }
2916 else
2917 ins_complete(Ctrl_N, FALSE);
2918 compl_enter_selects = compl_no_insert;
2919
2920 // Lazily show the popup menu, unless we got interrupted.
2921 if (!compl_interrupted)
2922 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002923 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002924 out_flush();
2925}
2926
2927/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002928 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002929 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002930 void
2931f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002932{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002933 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002934
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002935 if (in_vim9script()
2936 && (check_for_number_arg(argvars, 0) == FAIL
2937 || check_for_list_arg(argvars, 1) == FAIL))
2938 return;
2939
Bram Moolenaar24959102022-05-07 20:01:16 +01002940 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002941 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002942 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002943 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002944 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002945
2946 // Check for undo allowed here, because if something was already inserted
2947 // the line was already saved for undo and this check isn't done.
2948 if (!undo_allowed())
2949 return;
2950
Bram Moolenaard83392a2022-09-01 12:22:46 +01002951 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002952 {
2953 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2954 if (startcol > 0)
2955 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002956 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002957}
2958
2959/*
2960 * "complete_add()" function
2961 */
2962 void
2963f_complete_add(typval_T *argvars, typval_T *rettv)
2964{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002965 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002966 return;
2967
Bram Moolenaar440cf092021-04-03 20:13:30 +02002968 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002969}
2970
2971/*
2972 * "complete_check()" function
2973 */
2974 void
2975f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2976{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002977 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002978 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002979
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002980 ins_compl_check_keys(0, TRUE);
2981 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002982
2983 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002984}
2985
2986/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002987 * Return Insert completion mode name string
2988 */
2989 static char_u *
2990ins_compl_mode(void)
2991{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002992 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002993 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2994
2995 return (char_u *)"";
2996}
2997
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002998/*
2999 * Assign the sequence number to all the completion matches which don't have
3000 * one assigned yet.
3001 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003002 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003003ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003004{
3005 int number = 0;
3006 compl_T *match;
3007
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003008 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003009 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003010 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003011 // This should normally succeed already at the first loop
3012 // cycle, so it's fast!
3013 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003014 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003015 if (match->cp_number != -1)
3016 {
3017 number = match->cp_number;
3018 break;
3019 }
3020 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003021 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003022 for (match = match->cp_next;
3023 match != NULL && match->cp_number == -1;
3024 match = match->cp_next)
3025 match->cp_number = ++number;
3026 }
3027 else // BACKWARD
3028 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003029 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003030 // number. This should normally succeed already at the
3031 // first loop cycle, so it's fast!
3032 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003033 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003034 if (match->cp_number != -1)
3035 {
3036 number = match->cp_number;
3037 break;
3038 }
3039 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003040 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003041 for (match = match->cp_prev; match
3042 && match->cp_number == -1;
3043 match = match->cp_prev)
3044 match->cp_number = ++number;
3045 }
3046}
3047
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003048/*
3049 * Get complete information
3050 */
3051 static void
3052get_complete_info(list_T *what_list, dict_T *retdict)
3053{
3054 int ret = OK;
3055 listitem_T *item;
3056#define CI_WHAT_MODE 0x01
3057#define CI_WHAT_PUM_VISIBLE 0x02
3058#define CI_WHAT_ITEMS 0x04
3059#define CI_WHAT_SELECTED 0x08
3060#define CI_WHAT_INSERTED 0x10
3061#define CI_WHAT_ALL 0xff
3062 int what_flag;
3063
3064 if (what_list == NULL)
3065 what_flag = CI_WHAT_ALL;
3066 else
3067 {
3068 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003069 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003070 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003071 {
3072 char_u *what = tv_get_string(&item->li_tv);
3073
3074 if (STRCMP(what, "mode") == 0)
3075 what_flag |= CI_WHAT_MODE;
3076 else if (STRCMP(what, "pum_visible") == 0)
3077 what_flag |= CI_WHAT_PUM_VISIBLE;
3078 else if (STRCMP(what, "items") == 0)
3079 what_flag |= CI_WHAT_ITEMS;
3080 else if (STRCMP(what, "selected") == 0)
3081 what_flag |= CI_WHAT_SELECTED;
3082 else if (STRCMP(what, "inserted") == 0)
3083 what_flag |= CI_WHAT_INSERTED;
3084 }
3085 }
3086
3087 if (ret == OK && (what_flag & CI_WHAT_MODE))
3088 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3089
3090 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3091 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3092
Girish Palya8950bf72024-03-20 20:07:29 +01003093 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003094 {
3095 list_T *li;
Girish Palya8950bf72024-03-20 20:07:29 +01003096 dict_T *di;
3097 compl_T *match;
3098 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003099
Girish Palya8950bf72024-03-20 20:07:29 +01003100 if (what_flag & CI_WHAT_ITEMS)
3101 {
3102 li = list_alloc();
3103 if (li == NULL)
3104 return;
3105 ret = dict_add_list(retdict, "items", li);
3106 }
3107 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3108 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3109 ins_compl_update_sequence_numbers();
3110 if (ret == OK && compl_first_match != NULL)
3111 {
3112 int list_idx = 0;
3113 match = compl_first_match;
3114 do
3115 {
3116 if (!match_at_original_text(match))
3117 {
3118 if (what_flag & CI_WHAT_ITEMS)
3119 {
3120 di = dict_alloc();
3121 if (di == NULL)
3122 return;
3123 ret = list_append_dict(li, di);
3124 if (ret != OK)
3125 return;
3126 dict_add_string(di, "word", match->cp_str);
3127 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3128 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3129 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3130 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3131 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3132 // Add an empty string for backwards compatibility
3133 dict_add_string(di, "user_data", (char_u *)"");
3134 else
3135 dict_add_tv(di, "user_data", &match->cp_user_data);
3136 }
3137 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3138 selected_idx = list_idx;
3139 list_idx += 1;
3140 }
3141 match = match->cp_next;
3142 }
3143 while (match != NULL && !is_first_match(match));
3144 }
3145 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3146 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003147 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003148
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003149 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3150 {
3151 // TODO
3152 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003153}
3154
3155/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003156 * "complete_info()" function
3157 */
3158 void
3159f_complete_info(typval_T *argvars, typval_T *rettv)
3160{
3161 list_T *what_list = NULL;
3162
Bram Moolenaar93a10962022-06-16 11:42:09 +01003163 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003164 return;
3165
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003166 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3167 return;
3168
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003169 if (argvars[0].v_type != VAR_UNKNOWN)
3170 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003171 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003172 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003173 what_list = argvars[0].vval.v_list;
3174 }
3175 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003176}
3177#endif
3178
3179/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003180 * Returns TRUE when using a user-defined function for thesaurus completion.
3181 */
3182 static int
3183thesaurus_func_complete(int type UNUSED)
3184{
3185#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003186 return type == CTRL_X_THESAURUS
3187 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003188#else
3189 return FALSE;
3190#endif
3191}
3192
3193/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003194 * Return value of process_next_cpt_value()
3195 */
3196enum
3197{
3198 INS_COMPL_CPT_OK = 1,
3199 INS_COMPL_CPT_CONT,
3200 INS_COMPL_CPT_END
3201};
3202
3203/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003204 * state information used for getting the next set of insert completion
3205 * matches.
3206 */
3207typedef struct
3208{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003209 char_u *e_cpt_copy; // copy of 'complete'
3210 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003211 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003212 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003213 pos_T prev_match_pos; // previous match position
3214 int set_match_pos; // save first_match_pos/last_match_pos
3215 pos_T first_match_pos; // first match position
3216 pos_T last_match_pos; // last match position
3217 int found_all; // found all matches of a certain type.
3218 char_u *dict; // dictionary file to search
3219 int dict_f; // "dict" is an exact file name or not
3220} ins_compl_next_state_T;
3221
3222/*
3223 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003224 *
3225 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003226 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003227 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003228 * st->found_all - all matches of this type are found
3229 * st->ins_buf - search for completions in this buffer
3230 * st->first_match_pos - position of the first completion match
3231 * st->last_match_pos - position of the last completion match
3232 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003233 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003234 * st->dict - name of the dictionary or thesaurus file to search
3235 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003236 *
3237 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003238 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3239 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003240 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003241 */
3242 static int
3243process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003244 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003245 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003246 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003247{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003248 int compl_type = -1;
3249 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003250
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003251 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003252
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003253 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3254 st->e_cpt++;
3255
3256 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003257 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003258 st->ins_buf = curbuf;
3259 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003260 // Move the cursor back one character so that ^N can match the
3261 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003262 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 {
3264 // Move the cursor to after the last character in the
3265 // buffer, so that word at start of buffer is found
3266 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003267 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003268 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003269 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003270 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003271 compl_type = 0;
3272
3273 // Remember the first match so that the loop stops when we
3274 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003275 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003277 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003278 && (st->ins_buf = ins_compl_next_buf(
3279 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003280 {
3281 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003282 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003283 {
3284 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003285 st->first_match_pos.col = st->last_match_pos.col = 0;
3286 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3287 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003288 compl_type = 0;
3289 }
3290 else // unloaded buffer, scan like dictionary
3291 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003292 st->found_all = TRUE;
3293 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003294 {
3295 status = INS_COMPL_CPT_CONT;
3296 goto done;
3297 }
3298 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003299 st->dict = st->ins_buf->b_fname;
3300 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003301 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003302 if (!shortmess(SHM_COMPLETIONSCAN))
3303 {
3304 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3305 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3306 st->ins_buf->b_fname == NULL
3307 ? buf_spname(st->ins_buf)
3308 : st->ins_buf->b_sfname == NULL
3309 ? st->ins_buf->b_fname
3310 : st->ins_buf->b_sfname);
3311 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3312 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003313 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003314 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003315 status = INS_COMPL_CPT_END;
3316 else
3317 {
3318 if (ctrl_x_mode_line_or_eval())
3319 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003320 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003321 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003322 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003323 compl_type = CTRL_X_DICTIONARY;
3324 else
3325 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003326 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003327 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003328 st->dict = st->e_cpt;
3329 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003330 }
3331 }
3332#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003335 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003336 compl_type = CTRL_X_PATH_DEFINES;
3337#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003338 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003339 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003341 if (!shortmess(SHM_COMPLETIONSCAN))
3342 {
3343 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3344 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3345 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3346 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003347 }
3348 else
3349 compl_type = -1;
3350
3351 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003352 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003353
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003354 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003355 if (compl_type == -1)
3356 status = INS_COMPL_CPT_CONT;
3357 }
3358
3359done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003360 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003361 return status;
3362}
3363
3364#ifdef FEAT_FIND_ID
3365/*
3366 * Get the next set of identifiers or defines matching "compl_pattern" in
3367 * included files.
3368 */
3369 static void
3370get_next_include_file_completion(int compl_type)
3371{
3372 find_pattern_in_path(compl_pattern, compl_direction,
3373 (int)STRLEN(compl_pattern), FALSE, FALSE,
3374 (compl_type == CTRL_X_PATH_DEFINES
3375 && !(compl_cont_status & CONT_SOL))
3376 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003377 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003378}
3379#endif
3380
3381/*
3382 * Get the next set of words matching "compl_pattern" in dictionary or
3383 * thesaurus files.
3384 */
3385 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003386get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003387{
3388#ifdef FEAT_COMPL_FUNC
3389 if (thesaurus_func_complete(compl_type))
3390 expand_by_function(compl_type, compl_pattern);
3391 else
3392#endif
3393 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003394 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003395 : (compl_type == CTRL_X_THESAURUS
3396 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3397 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3398 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003399 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003400 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003401}
3402
3403/*
3404 * Get the next set of tag names matching "compl_pattern".
3405 */
3406 static void
3407get_next_tag_completion(void)
3408{
3409 int save_p_ic;
3410 char_u **matches;
3411 int num_matches;
3412
3413 // set p_ic according to p_ic, p_scs and pat for find_tags().
3414 save_p_ic = p_ic;
3415 p_ic = ignorecase(compl_pattern);
3416
3417 // Find up to TAG_MANY matches. Avoids that an enormous number
3418 // of matches is found when compl_pattern is empty
3419 g_tag_at_cursor = TRUE;
3420 if (find_tags(compl_pattern, &num_matches, &matches,
3421 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003422 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003423 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3424 ins_compl_add_matches(num_matches, matches, p_ic);
3425 g_tag_at_cursor = FALSE;
3426 p_ic = save_p_ic;
3427}
3428
3429/*
3430 * Get the next set of filename matching "compl_pattern".
3431 */
3432 static void
3433get_next_filename_completion(void)
3434{
3435 char_u **matches;
3436 int num_matches;
3437
3438 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3439 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3440 return;
3441
3442 // May change home directory back to "~".
3443 tilde_replace(compl_pattern, num_matches, matches);
3444#ifdef BACKSLASH_IN_FILENAME
3445 if (curbuf->b_p_csl[0] != NUL)
3446 {
3447 int i;
3448
3449 for (i = 0; i < num_matches; ++i)
3450 {
3451 char_u *ptr = matches[i];
3452
3453 while (*ptr != NUL)
3454 {
3455 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3456 *ptr = '/';
3457 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3458 *ptr = '\\';
3459 ptr += (*mb_ptr2len)(ptr);
3460 }
3461 }
3462 }
3463#endif
3464 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3465}
3466
3467/*
3468 * Get the next set of command-line completions matching "compl_pattern".
3469 */
3470 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003471get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003472{
3473 char_u **matches;
3474 int num_matches;
3475
3476 if (expand_cmdline(&compl_xp, compl_pattern,
3477 (int)STRLEN(compl_pattern),
3478 &num_matches, &matches) == EXPAND_OK)
3479 ins_compl_add_matches(num_matches, matches, FALSE);
3480}
3481
3482/*
3483 * Get the next set of spell suggestions matching "compl_pattern".
3484 */
3485 static void
3486get_next_spell_completion(linenr_T lnum UNUSED)
3487{
3488#ifdef FEAT_SPELL
3489 char_u **matches;
3490 int num_matches;
3491
3492 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3493 if (num_matches > 0)
3494 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003495 else
3496 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003497#endif
3498}
3499
3500/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003501 * Return the next word or line from buffer "ins_buf" at position
3502 * "cur_match_pos" for completion. The length of the match is set in "len".
3503 */
3504 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003505ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003506 buf_T *ins_buf, // buffer being scanned
3507 pos_T *cur_match_pos, // current match position
3508 int *match_len,
3509 int *cont_s_ipos) // next ^X<> will set initial_pos
3510{
3511 char_u *ptr;
3512 int len;
3513
3514 *match_len = 0;
3515 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3516 cur_match_pos->col;
3517 if (ctrl_x_mode_line_or_eval())
3518 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003519 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003520 {
3521 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3522 return NULL;
3523 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3524 if (!p_paste)
3525 ptr = skipwhite(ptr);
3526 }
3527 len = (int)STRLEN(ptr);
3528 }
3529 else
3530 {
3531 char_u *tmp_ptr = ptr;
3532
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003533 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003534 {
3535 tmp_ptr += compl_length;
3536 // Skip if already inside a word.
3537 if (vim_iswordp(tmp_ptr))
3538 return NULL;
3539 // Find start of next word.
3540 tmp_ptr = find_word_start(tmp_ptr);
3541 }
3542 // Find end of this word.
3543 tmp_ptr = find_word_end(tmp_ptr);
3544 len = (int)(tmp_ptr - ptr);
3545
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003546 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003547 {
3548 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3549 {
3550 // Try next line, if any. the new word will be
3551 // "join" as if the normal command "J" was used.
3552 // IOSIZE is always greater than
3553 // compl_length, so the next STRNCPY always
3554 // works -- Acevedo
3555 STRNCPY(IObuff, ptr, len);
3556 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3557 tmp_ptr = ptr = skipwhite(ptr);
3558 // Find start of next word.
3559 tmp_ptr = find_word_start(tmp_ptr);
3560 // Find end of next word.
3561 tmp_ptr = find_word_end(tmp_ptr);
3562 if (tmp_ptr > ptr)
3563 {
3564 if (*ptr != ')' && IObuff[len - 1] != TAB)
3565 {
3566 if (IObuff[len - 1] != ' ')
3567 IObuff[len++] = ' ';
3568 // IObuf =~ "\k.* ", thus len >= 2
3569 if (p_js
3570 && (IObuff[len - 2] == '.'
3571 || (vim_strchr(p_cpo, CPO_JOINSP)
3572 == NULL
3573 && (IObuff[len - 2] == '?'
3574 || IObuff[len - 2] == '!'))))
3575 IObuff[len++] = ' ';
3576 }
3577 // copy as much as possible of the new word
3578 if (tmp_ptr - ptr >= IOSIZE - len)
3579 tmp_ptr = ptr + IOSIZE - len - 1;
3580 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3581 len += (int)(tmp_ptr - ptr);
3582 *cont_s_ipos = TRUE;
3583 }
3584 IObuff[len] = NUL;
3585 ptr = IObuff;
3586 }
3587 if (len == compl_length)
3588 return NULL;
3589 }
3590 }
3591
3592 *match_len = len;
3593 return ptr;
3594}
3595
3596/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003597 * Get the next set of words matching "compl_pattern" for default completion(s)
3598 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003599 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3600 * position "st->start_pos" in the "compl_direction" direction. If
3601 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3602 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003603 * Returns OK if a new next match is found, otherwise returns FAIL.
3604 */
3605 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003606get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003607{
3608 int found_new_match = FAIL;
3609 int save_p_scs;
3610 int save_p_ws;
3611 int looped_around = FALSE;
3612 char_u *ptr;
3613 int len;
3614
3615 // If 'infercase' is set, don't use 'smartcase' here
3616 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003617 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003618 p_scs = FALSE;
3619
3620 // Buffers other than curbuf are scanned from the beginning or the
3621 // end but never from the middle, thus setting nowrapscan in this
3622 // buffer is a good idea, on the other hand, we always set
3623 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3624 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003625 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003626 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003627 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003628 p_ws = TRUE;
3629 looped_around = FALSE;
3630 for (;;)
3631 {
3632 int cont_s_ipos = FALSE;
3633
3634 ++msg_silent; // Don't want messages for wrapscan.
3635
3636 // ctrl_x_mode_line_or_eval() || word-wise search that
3637 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003638 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003639 found_new_match = search_for_exact_line(st->ins_buf,
3640 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003641 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003642 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3643 NULL, compl_direction, compl_pattern, 1L,
3644 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003645 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003646 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003647 {
3648 // set "compl_started" even on fail
3649 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003650 st->first_match_pos = *st->cur_match_pos;
3651 st->last_match_pos = *st->cur_match_pos;
3652 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003654 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3655 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003656 {
3657 found_new_match = FAIL;
3658 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003659 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003660 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3661 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3662 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003663 {
3664 if (looped_around)
3665 found_new_match = FAIL;
3666 else
3667 looped_around = TRUE;
3668 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003669 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003670 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3671 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3672 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003673 {
3674 if (looped_around)
3675 found_new_match = FAIL;
3676 else
3677 looped_around = TRUE;
3678 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003679 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003680 if (found_new_match == FAIL)
3681 break;
3682
3683 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003684 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003685 && start_pos->lnum == st->cur_match_pos->lnum
3686 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003687 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003688
zeertzjq440d4cb2023-03-02 17:51:32 +00003689 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3690 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003691 if (ptr == NULL)
3692 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003693
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003694 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003695 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003696 0, cont_s_ipos) != NOTDONE)
3697 {
3698 found_new_match = OK;
3699 break;
3700 }
3701 }
3702 p_scs = save_p_scs;
3703 p_ws = save_p_ws;
3704
3705 return found_new_match;
3706}
3707
3708/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003709 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003710 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003711 */
3712 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003713get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003714{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003715 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003716
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003717 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003718 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003719 case -1:
3720 break;
3721#ifdef FEAT_FIND_ID
3722 case CTRL_X_PATH_PATTERNS:
3723 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003724 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003725 break;
3726#endif
3727
3728 case CTRL_X_DICTIONARY:
3729 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003730 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3731 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003732 break;
3733
3734 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003735 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003736 break;
3737
3738 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003739 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003740 break;
3741
3742 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003743 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003744 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003745 break;
3746
3747#ifdef FEAT_COMPL_FUNC
3748 case CTRL_X_FUNCTION:
3749 case CTRL_X_OMNI:
3750 expand_by_function(type, compl_pattern);
3751 break;
3752#endif
3753
3754 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003755 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003756 break;
3757
3758 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003759 found_new_match = get_next_default_completion(st, ini);
3760 if (found_new_match == FAIL && st->ins_buf == curbuf)
3761 st->found_all = TRUE;
3762 }
3763
3764 // check if compl_curr_match has changed, (e.g. other type of
3765 // expansion added something)
3766 if (type != 0 && compl_curr_match != compl_old_match)
3767 found_new_match = OK;
3768
3769 return found_new_match;
3770}
3771
3772/*
3773 * Get the next expansion(s), using "compl_pattern".
3774 * The search starts at position "ini" in curbuf and in the direction
3775 * compl_direction.
3776 * When "compl_started" is FALSE start at that position, otherwise continue
3777 * where we stopped searching before.
3778 * This may return before finding all the matches.
3779 * Return the total number of matches or -1 if still unknown -- Acevedo
3780 */
3781 static int
3782ins_compl_get_exp(pos_T *ini)
3783{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003784 static ins_compl_next_state_T st;
3785 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003786 int i;
3787 int found_new_match;
3788 int type = ctrl_x_mode;
3789
3790 if (!compl_started)
3791 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003792 buf_T *buf;
3793
3794 FOR_ALL_BUFFERS(buf)
3795 buf->b_scanned = 0;
3796 if (!st_cleared)
3797 {
3798 CLEAR_FIELD(st);
3799 st_cleared = TRUE;
3800 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003801 st.found_all = FALSE;
3802 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003803 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003804 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003805 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3806 ? (char_u *)"." : curbuf->b_p_cpt);
3807 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003808 st.last_match_pos = st.first_match_pos = *ini;
3809 }
3810 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3811 st.ins_buf = curbuf; // In case the buffer was wiped out.
3812
3813 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003814 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003815 ? &st.last_match_pos : &st.first_match_pos;
3816
3817 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3818 for (;;)
3819 {
3820 found_new_match = FAIL;
3821 st.set_match_pos = FALSE;
3822
3823 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3824 // or if found_all says this entry is done. For ^X^L only use the
3825 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003826 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003827 && (!compl_started || st.found_all))
3828 {
3829 int status = process_next_cpt_value(&st, &type, ini);
3830
3831 if (status == INS_COMPL_CPT_END)
3832 break;
3833 if (status == INS_COMPL_CPT_CONT)
3834 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003835 }
3836
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003837 // If complete() was called then compl_pattern has been reset. The
3838 // following won't work then, bail out.
3839 if (compl_pattern == NULL)
3840 break;
3841
3842 // get the next set of completion matches
3843 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003844
3845 // break the loop for specialized modes (use 'complete' just for the
3846 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3847 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003848 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3849 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003850 {
3851 if (got_int)
3852 break;
3853 // Fill the popup menu as soon as possible.
3854 if (type != -1)
3855 ins_compl_check_keys(0, FALSE);
3856
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003857 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003858 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3859 break;
3860 compl_started = TRUE;
3861 }
3862 else
3863 {
3864 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003865 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003866 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003867
3868 compl_started = FALSE;
3869 }
3870 }
3871 compl_started = TRUE;
3872
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003873 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003874 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003875 found_new_match = FAIL;
3876
3877 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003878 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003879 && !ctrl_x_mode_line_or_eval()))
3880 i = ins_compl_make_cyclic();
3881
3882 if (compl_old_match != NULL)
3883 {
3884 // If several matches were added (FORWARD) or the search failed and has
3885 // just been made cyclic then we have to move compl_curr_match to the
3886 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003887 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003888 : compl_old_match->cp_prev;
3889 if (compl_curr_match == NULL)
3890 compl_curr_match = compl_old_match;
3891 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003892 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003893
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003894 return i;
3895}
3896
3897/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003898 * Update "compl_shown_match" to the actually shown match, it may differ when
3899 * "compl_leader" is used to omit some of the matches.
3900 */
3901 static void
3902ins_compl_update_shown_match(void)
3903{
3904 while (!ins_compl_equal(compl_shown_match,
3905 compl_leader, (int)STRLEN(compl_leader))
3906 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003907 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003908 compl_shown_match = compl_shown_match->cp_next;
3909
3910 // If we didn't find it searching forward, and compl_shows_dir is
3911 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003912 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003913 && !ins_compl_equal(compl_shown_match,
3914 compl_leader, (int)STRLEN(compl_leader))
3915 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003916 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003917 {
3918 while (!ins_compl_equal(compl_shown_match,
3919 compl_leader, (int)STRLEN(compl_leader))
3920 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003921 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003922 compl_shown_match = compl_shown_match->cp_prev;
3923 }
3924}
3925
3926/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003927 * Delete the old text being completed.
3928 */
3929 void
3930ins_compl_delete(void)
3931{
3932 int col;
3933
3934 // In insert mode: Delete the typed part.
3935 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003936 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003937 if ((int)curwin->w_cursor.col > col)
3938 {
3939 if (stop_arrow() == FAIL)
3940 return;
3941 backspace_until_column(col);
3942 }
3943
3944 // TODO: is this sufficient for redrawing? Redrawing everything causes
3945 // flicker, thus we can't do that.
3946 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003947#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003948 // clear v:completed_item
3949 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003950#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003951}
3952
3953/*
3954 * Insert the new text being completed.
3955 * "in_compl_func" is TRUE when called from complete_check().
3956 */
3957 void
3958ins_compl_insert(int in_compl_func)
3959{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003960 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003961
3962 // Make sure we don't go over the end of the string, this can happen with
3963 // illegal bytes.
3964 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3965 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003966 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003967 compl_used_match = FALSE;
3968 else
3969 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003970#ifdef FEAT_EVAL
3971 {
3972 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3973
3974 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3975 }
3976#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003977 if (!in_compl_func)
3978 compl_curr_match = compl_shown_match;
3979}
3980
3981/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003982 * show the file name for the completion match (if any). Truncate the file
3983 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003984 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003985 static void
3986ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003987{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 char *lead = _("match in file");
3989 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3990 char_u *s;
3991 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003992
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003993 if (space <= 0)
3994 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003995
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003996 // We need the tail that fits. With double-byte encoding going
3997 // back from the end is very slow, thus go from the start and keep
3998 // the text that fits in "space" between "s" and "e".
3999 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004000 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004001 space -= ptr2cells(e);
4002 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004003 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004004 space += ptr2cells(s);
4005 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004006 }
4007 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004008 msg_hist_off = TRUE;
4009 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4010 s > compl_shown_match->cp_fname ? "<" : "", s);
4011 msg((char *)IObuff);
4012 msg_hist_off = FALSE;
4013 redraw_cmdline = FALSE; // don't overwrite!
4014}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004015
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004016/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004017 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004018 * times. The number of matches found is returned in 'num_matches'.
4019 *
4020 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4021 * get more completions. If it is FALSE, then do nothing when there are no more
4022 * completions in the given direction.
4023 *
4024 * If "advance" is TRUE, then completion will move to the first match.
4025 * Otherwise, the original text will be shown.
4026 *
4027 * Returns OK on success and -1 if the number of matches are unknown.
4028 */
4029 static int
4030find_next_completion_match(
4031 int allow_get_expansion,
4032 int todo, // repeat completion this many times
4033 int advance,
4034 int *num_matches)
4035{
4036 int found_end = FALSE;
4037 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004038
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004039 while (--todo >= 0)
4040 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004041 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 {
4043 compl_shown_match = compl_shown_match->cp_next;
4044 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004045 && (is_first_match(compl_shown_match->cp_next)
4046 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004047 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004048 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004049 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004050 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004051 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004052 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004053 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004054 }
4055 else
4056 {
4057 if (!allow_get_expansion)
4058 {
4059 if (advance)
4060 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004061 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004062 compl_pending -= todo + 1;
4063 else
4064 compl_pending += todo + 1;
4065 }
4066 return -1;
4067 }
4068
4069 if (!compl_no_select && advance)
4070 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004071 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004072 --compl_pending;
4073 else
4074 ++compl_pending;
4075 }
4076
4077 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004078 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004079
4080 // handle any pending completions
4081 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004082 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004083 {
4084 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4085 {
4086 compl_shown_match = compl_shown_match->cp_next;
4087 --compl_pending;
4088 }
4089 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4090 {
4091 compl_shown_match = compl_shown_match->cp_prev;
4092 ++compl_pending;
4093 }
4094 else
4095 break;
4096 }
4097 found_end = FALSE;
4098 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004099 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004100 && compl_leader != NULL
4101 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004102 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004103 ++todo;
4104 else
4105 // Remember a matching item.
4106 found_compl = compl_shown_match;
4107
4108 // Stop at the end of the list when we found a usable match.
4109 if (found_end)
4110 {
4111 if (found_compl != NULL)
4112 {
4113 compl_shown_match = found_compl;
4114 break;
4115 }
4116 todo = 1; // use first usable match after wrapping around
4117 }
4118 }
4119
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004120 return OK;
4121}
4122
4123/*
4124 * Fill in the next completion in the current direction.
4125 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4126 * get more completions. If it is FALSE, then we just do nothing when there
4127 * are no more completions in a given direction. The latter case is used when
4128 * we are still in the middle of finding completions, to allow browsing
4129 * through the ones found so far.
4130 * Return the total number of matches, or -1 if still unknown -- webb.
4131 *
4132 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4133 * compl_shown_match here.
4134 *
4135 * Note that this function may be called recursively once only. First with
4136 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4137 * calls this function with "allow_get_expansion" FALSE.
4138 */
4139 static int
4140ins_compl_next(
4141 int allow_get_expansion,
4142 int count, // repeat completion this many times; should
4143 // be at least 1
4144 int insert_match, // Insert the newly selected match
4145 int in_compl_func) // called from complete_check()
4146{
4147 int num_matches = -1;
4148 int todo = count;
4149 int advance;
4150 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004151 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004152
4153 // When user complete function return -1 for findstart which is next
4154 // time of 'always', compl_shown_match become NULL.
4155 if (compl_shown_match == NULL)
4156 return -1;
4157
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004158 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004159 // Update "compl_shown_match" to the actually shown match
4160 ins_compl_update_shown_match();
4161
4162 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004163 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004164 // Delete old text to be replaced
4165 ins_compl_delete();
4166
4167 // When finding the longest common text we stick at the original text,
4168 // don't let CTRL-N or CTRL-P move to the first match.
4169 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4170
4171 // When restarting the search don't insert the first match either.
4172 if (compl_restarting)
4173 {
4174 advance = FALSE;
4175 compl_restarting = FALSE;
4176 }
4177
4178 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4179 // around.
4180 if (find_next_completion_match(allow_get_expansion, todo, advance,
4181 &num_matches) == -1)
4182 return -1;
4183
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004184 if (curbuf != orig_curbuf)
4185 {
4186 // In case some completion function switched buffer, don't want to
4187 // insert the completion elsewhere.
4188 return -1;
4189 }
4190
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004191 // Insert the text of the new completion, or the compl_leader.
4192 if (compl_no_insert && !started)
4193 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004194 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004195 compl_used_match = FALSE;
4196 }
4197 else if (insert_match)
4198 {
4199 if (!compl_get_longest || compl_used_match)
4200 ins_compl_insert(in_compl_func);
4201 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004202 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004203 }
4204 else
4205 compl_used_match = FALSE;
4206
4207 if (!allow_get_expansion)
4208 {
4209 // may undisplay the popup menu first
4210 ins_compl_upd_pum();
4211
4212 if (pum_enough_matches())
4213 // Will display the popup menu, don't redraw yet to avoid flicker.
4214 pum_call_update_screen();
4215 else
4216 // Not showing the popup menu yet, redraw to show the user what was
4217 // inserted.
4218 update_screen(0);
4219
4220 // display the updated popup menu
4221 ins_compl_show_pum();
4222#ifdef FEAT_GUI
4223 if (gui.in_use)
4224 {
4225 // Show the cursor after the match, not after the redrawn text.
4226 setcursor();
4227 out_flush_cursor(FALSE, FALSE);
4228 }
4229#endif
4230
4231 // Delete old text to be replaced, since we're still searching and
4232 // don't want to match ourselves!
4233 ins_compl_delete();
4234 }
4235
4236 // Enter will select a match when the match wasn't inserted and the popup
4237 // menu is visible.
4238 if (compl_no_insert && !started)
4239 compl_enter_selects = TRUE;
4240 else
4241 compl_enter_selects = !insert_match && compl_match_array != NULL;
4242
4243 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004244 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004245 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004246
4247 return num_matches;
4248}
4249
4250/*
4251 * Call this while finding completions, to check whether the user has hit a key
4252 * that should change the currently displayed completion, or exit completion
4253 * mode. Also, when compl_pending is not zero, show a completion as soon as
4254 * possible. -- webb
4255 * "frequency" specifies out of how many calls we actually check.
4256 * "in_compl_func" is TRUE when called from complete_check(), don't set
4257 * compl_curr_match.
4258 */
4259 void
4260ins_compl_check_keys(int frequency, int in_compl_func)
4261{
4262 static int count = 0;
4263 int c;
4264
4265 // Don't check when reading keys from a script, :normal or feedkeys().
4266 // That would break the test scripts. But do check for keys when called
4267 // from complete_check().
4268 if (!in_compl_func && (using_script() || ex_normal_busy))
4269 return;
4270
4271 // Only do this at regular intervals
4272 if (++count < frequency)
4273 return;
4274 count = 0;
4275
4276 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4277 // can't do its work correctly.
4278 c = vpeekc_any();
4279 if (c != NUL)
4280 {
4281 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4282 {
4283 c = safe_vgetc(); // Eat the character
4284 compl_shows_dir = ins_compl_key2dir(c);
4285 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4286 c != K_UP && c != K_DOWN, in_compl_func);
4287 }
4288 else
4289 {
4290 // Need to get the character to have KeyTyped set. We'll put it
4291 // back with vungetc() below. But skip K_IGNORE.
4292 c = safe_vgetc();
4293 if (c != K_IGNORE)
4294 {
4295 // Don't interrupt completion when the character wasn't typed,
4296 // e.g., when doing @q to replay keys.
4297 if (c != Ctrl_R && KeyTyped)
4298 compl_interrupted = TRUE;
4299
4300 vungetc(c);
4301 }
4302 }
4303 }
4304 if (compl_pending != 0 && !got_int && !compl_no_insert)
4305 {
4306 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4307
4308 compl_pending = 0;
4309 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4310 }
4311}
4312
4313/*
4314 * Decide the direction of Insert mode complete from the key typed.
4315 * Returns BACKWARD or FORWARD.
4316 */
4317 static int
4318ins_compl_key2dir(int c)
4319{
4320 if (c == Ctrl_P || c == Ctrl_L
4321 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4322 return BACKWARD;
4323 return FORWARD;
4324}
4325
4326/*
4327 * Return TRUE for keys that are used for completion only when the popup menu
4328 * is visible.
4329 */
4330 static int
4331ins_compl_pum_key(int c)
4332{
4333 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4334 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4335 || c == K_UP || c == K_DOWN);
4336}
4337
4338/*
4339 * Decide the number of completions to move forward.
4340 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4341 */
4342 static int
4343ins_compl_key2count(int c)
4344{
4345 int h;
4346
4347 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4348 {
4349 h = pum_get_height();
4350 if (h > 3)
4351 h -= 2; // keep some context
4352 return h;
4353 }
4354 return 1;
4355}
4356
4357/*
4358 * Return TRUE if completion with "c" should insert the match, FALSE if only
4359 * to change the currently selected completion.
4360 */
4361 static int
4362ins_compl_use_match(int c)
4363{
4364 switch (c)
4365 {
4366 case K_UP:
4367 case K_DOWN:
4368 case K_PAGEDOWN:
4369 case K_KPAGEDOWN:
4370 case K_S_DOWN:
4371 case K_PAGEUP:
4372 case K_KPAGEUP:
4373 case K_S_UP:
4374 return FALSE;
4375 }
4376 return TRUE;
4377}
4378
4379/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004380 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4381 * completion)
4382 * Sets the global variables: compl_col, compl_length and compl_pattern.
4383 * Uses the global variables: compl_cont_status and ctrl_x_mode
4384 */
4385 static int
4386get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4387{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004388 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004389 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004390 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004391 {
4392 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4393 ;
4394 compl_col += ++startcol;
4395 compl_length = curs_col - startcol;
4396 }
4397 if (p_ic)
4398 compl_pattern = str_foldcase(line + compl_col,
4399 compl_length, NULL, 0);
4400 else
4401 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4402 if (compl_pattern == NULL)
4403 return FAIL;
4404 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004405 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004406 {
4407 char_u *prefix = (char_u *)"\\<";
4408
4409 // we need up to 2 extra chars for the prefix
4410 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4411 compl_length) + 2);
4412 if (compl_pattern == NULL)
4413 return FAIL;
4414 if (!vim_iswordp(line + compl_col)
4415 || (compl_col > 0
4416 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4417 prefix = (char_u *)"";
4418 STRCPY((char *)compl_pattern, prefix);
4419 (void)quote_meta(compl_pattern + STRLEN(prefix),
4420 line + compl_col, compl_length);
4421 }
4422 else if (--startcol < 0
4423 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4424 {
4425 // Match any word of at least two chars
4426 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4427 if (compl_pattern == NULL)
4428 return FAIL;
4429 compl_col += curs_col;
4430 compl_length = 0;
4431 }
4432 else
4433 {
4434 // Search the point of change class of multibyte character
4435 // or not a word single byte character backward.
4436 if (has_mbyte)
4437 {
4438 int base_class;
4439 int head_off;
4440
4441 startcol -= (*mb_head_off)(line, line + startcol);
4442 base_class = mb_get_class(line + startcol);
4443 while (--startcol >= 0)
4444 {
4445 head_off = (*mb_head_off)(line, line + startcol);
4446 if (base_class != mb_get_class(line + startcol
4447 - head_off))
4448 break;
4449 startcol -= head_off;
4450 }
4451 }
4452 else
4453 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4454 ;
4455 compl_col += ++startcol;
4456 compl_length = (int)curs_col - startcol;
4457 if (compl_length == 1)
4458 {
4459 // Only match word with at least two chars -- webb
4460 // there's no need to call quote_meta,
4461 // alloc(7) is enough -- Acevedo
4462 compl_pattern = alloc(7);
4463 if (compl_pattern == NULL)
4464 return FAIL;
4465 STRCPY((char *)compl_pattern, "\\<");
4466 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4467 STRCAT((char *)compl_pattern, "\\k");
4468 }
4469 else
4470 {
4471 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4472 compl_length) + 2);
4473 if (compl_pattern == NULL)
4474 return FAIL;
4475 STRCPY((char *)compl_pattern, "\\<");
4476 (void)quote_meta(compl_pattern + 2, line + compl_col,
4477 compl_length);
4478 }
4479 }
4480
4481 return OK;
4482}
4483
4484/*
4485 * Get the pattern, column and length for whole line completion or for the
4486 * complete() function.
4487 * Sets the global variables: compl_col, compl_length and compl_pattern.
4488 */
4489 static int
4490get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4491{
4492 compl_col = (colnr_T)getwhitecols(line);
4493 compl_length = (int)curs_col - (int)compl_col;
4494 if (compl_length < 0) // cursor in indent: empty pattern
4495 compl_length = 0;
4496 if (p_ic)
4497 compl_pattern = str_foldcase(line + compl_col, compl_length,
4498 NULL, 0);
4499 else
4500 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4501 if (compl_pattern == NULL)
4502 return FAIL;
4503
4504 return OK;
4505}
4506
4507/*
4508 * Get the pattern, column and length for filename completion.
4509 * Sets the global variables: compl_col, compl_length and compl_pattern.
4510 */
4511 static int
4512get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4513{
4514 // Go back to just before the first filename character.
4515 if (startcol > 0)
4516 {
4517 char_u *p = line + startcol;
4518
4519 MB_PTR_BACK(line, p);
4520 while (p > line && vim_isfilec(PTR2CHAR(p)))
4521 MB_PTR_BACK(line, p);
4522 if (p == line && vim_isfilec(PTR2CHAR(p)))
4523 startcol = 0;
4524 else
4525 startcol = (int)(p - line) + 1;
4526 }
4527
4528 compl_col += startcol;
4529 compl_length = (int)curs_col - startcol;
4530 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4531 if (compl_pattern == NULL)
4532 return FAIL;
4533
4534 return OK;
4535}
4536
4537/*
4538 * Get the pattern, column and length for command-line completion.
4539 * Sets the global variables: compl_col, compl_length and compl_pattern.
4540 */
4541 static int
4542get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4543{
4544 compl_pattern = vim_strnsave(line, curs_col);
4545 if (compl_pattern == NULL)
4546 return FAIL;
4547 set_cmd_context(&compl_xp, compl_pattern,
4548 (int)STRLEN(compl_pattern), curs_col, FALSE);
4549 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4550 || compl_xp.xp_context == EXPAND_NOTHING)
4551 // No completion possible, use an empty pattern to get a
4552 // "pattern not found" message.
4553 compl_col = curs_col;
4554 else
4555 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4556 compl_length = curs_col - compl_col;
4557
4558 return OK;
4559}
4560
4561/*
4562 * Get the pattern, column and length for user defined completion ('omnifunc',
4563 * 'completefunc' and 'thesaurusfunc')
4564 * Sets the global variables: compl_col, compl_length and compl_pattern.
4565 * Uses the global variable: spell_bad_len
4566 */
4567 static int
4568get_userdefined_compl_info(colnr_T curs_col UNUSED)
4569{
4570 int ret = FAIL;
4571
4572#ifdef FEAT_COMPL_FUNC
4573 // Call user defined function 'completefunc' with "a:findstart"
4574 // set to 1 to obtain the length of text to use for completion.
4575 char_u *line;
4576 typval_T args[3];
4577 int col;
4578 char_u *funcname;
4579 pos_T pos;
4580 int save_State = State;
4581 callback_T *cb;
4582
4583 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4584 // length as a string
4585 funcname = get_complete_funcname(ctrl_x_mode);
4586 if (*funcname == NUL)
4587 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004588 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004589 ? "completefunc" : "omnifunc");
4590 return FAIL;
4591 }
4592
4593 args[0].v_type = VAR_NUMBER;
4594 args[0].vval.v_number = 1;
4595 args[1].v_type = VAR_STRING;
4596 args[1].vval.v_string = (char_u *)"";
4597 args[2].v_type = VAR_UNKNOWN;
4598 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004599 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004600 cb = get_insert_callback(ctrl_x_mode);
4601 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004602 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004603
4604 State = save_State;
4605 curwin->w_cursor = pos; // restore the cursor position
4606 validate_cursor();
4607 if (!EQUAL_POS(curwin->w_cursor, pos))
4608 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004609 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004610 return FAIL;
4611 }
4612
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004613 // Return value -2 means the user complete function wants to cancel the
4614 // complete without an error, do the same if the function did not execute
4615 // successfully.
4616 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004617 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004618 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004619 if (col == -3)
4620 {
4621 ctrl_x_mode = CTRL_X_NORMAL;
4622 edit_submode = NULL;
4623 if (!shortmess(SHM_COMPLETIONMENU))
4624 msg_clr_cmdline();
4625 return FAIL;
4626 }
4627
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004628 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004629 // completion.
4630 compl_opt_refresh_always = FALSE;
4631 compl_opt_suppress_empty = FALSE;
4632
4633 if (col < 0)
4634 col = curs_col;
4635 compl_col = col;
4636 if (compl_col > curs_col)
4637 compl_col = curs_col;
4638
4639 // Setup variables for completion. Need to obtain "line" again,
4640 // it may have become invalid.
4641 line = ml_get(curwin->w_cursor.lnum);
4642 compl_length = curs_col - compl_col;
4643 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4644 if (compl_pattern == NULL)
4645 return FAIL;
4646
4647 ret = OK;
4648#endif
4649
4650 return ret;
4651}
4652
4653/*
4654 * Get the pattern, column and length for spell completion.
4655 * Sets the global variables: compl_col, compl_length and compl_pattern.
4656 * Uses the global variable: spell_bad_len
4657 */
4658 static int
4659get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4660{
4661 int ret = FAIL;
4662#ifdef FEAT_SPELL
4663 char_u *line;
4664
4665 if (spell_bad_len > 0)
4666 compl_col = curs_col - spell_bad_len;
4667 else
4668 compl_col = spell_word_start(startcol);
4669 if (compl_col >= (colnr_T)startcol)
4670 {
4671 compl_length = 0;
4672 compl_col = curs_col;
4673 }
4674 else
4675 {
4676 spell_expand_check_cap(compl_col);
4677 compl_length = (int)curs_col - compl_col;
4678 }
4679 // Need to obtain "line" again, it may have become invalid.
4680 line = ml_get(curwin->w_cursor.lnum);
4681 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4682 if (compl_pattern == NULL)
4683 return FAIL;
4684
4685 ret = OK;
4686#endif
4687
4688 return ret;
4689}
4690
4691/*
4692 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004693 * "startcol" - start column number of the completion pattern/text
4694 * "cur_col" - current cursor column
4695 * On return, "line_invalid" is set to TRUE, if the current line may have
4696 * become invalid and needs to be fetched again.
4697 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004698 */
4699 static int
4700compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4701{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004702 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004703 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4704 && !thesaurus_func_complete(ctrl_x_mode)))
4705 {
4706 return get_normal_compl_info(line, startcol, curs_col);
4707 }
4708 else if (ctrl_x_mode_line_or_eval())
4709 {
4710 return get_wholeline_compl_info(line, curs_col);
4711 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004712 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004713 {
4714 return get_filename_compl_info(line, startcol, curs_col);
4715 }
4716 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4717 {
4718 return get_cmdline_compl_info(line, curs_col);
4719 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004720 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004721 || thesaurus_func_complete(ctrl_x_mode))
4722 {
4723 if (get_userdefined_compl_info(curs_col) == FAIL)
4724 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004725 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004726 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004727 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004728 {
4729 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4730 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004731 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004732 }
4733 else
4734 {
4735 internal_error("ins_complete()");
4736 return FAIL;
4737 }
4738
4739 return OK;
4740}
4741
4742/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004743 * Continue an interrupted completion mode search in "line".
4744 *
4745 * If this same ctrl_x_mode has been interrupted use the text from
4746 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4747 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4748 * the same line as the cursor then fix it (the line has been split because it
4749 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4750 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004751 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004752 static void
4753ins_compl_continue_search(char_u *line)
4754{
4755 // it is a continued search
4756 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004757 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4758 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004759 {
4760 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4761 {
4762 // line (probably) wrapped, set compl_startpos to the
4763 // first non_blank in the line, if it is not a wordchar
4764 // include it to get a better pattern, but then we don't
4765 // want the "\\<" prefix, check it below
4766 compl_col = (colnr_T)getwhitecols(line);
4767 compl_startpos.col = compl_col;
4768 compl_startpos.lnum = curwin->w_cursor.lnum;
4769 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4770 }
4771 else
4772 {
4773 // S_IPOS was set when we inserted a word that was at the
4774 // beginning of the line, which means that we'll go to SOL
4775 // mode but first we need to redefine compl_startpos
4776 if (compl_cont_status & CONT_S_IPOS)
4777 {
4778 compl_cont_status |= CONT_SOL;
4779 compl_startpos.col = (colnr_T)(skipwhite(
4780 line + compl_length
4781 + compl_startpos.col) - line);
4782 }
4783 compl_col = compl_startpos.col;
4784 }
4785 compl_length = curwin->w_cursor.col - (int)compl_col;
4786 // IObuff is used to add a "word from the next line" would we
4787 // have enough space? just being paranoid
4788#define MIN_SPACE 75
4789 if (compl_length > (IOSIZE - MIN_SPACE))
4790 {
4791 compl_cont_status &= ~CONT_SOL;
4792 compl_length = (IOSIZE - MIN_SPACE);
4793 compl_col = curwin->w_cursor.col - compl_length;
4794 }
4795 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4796 if (compl_length < 1)
4797 compl_cont_status &= CONT_LOCAL;
4798 }
4799 else if (ctrl_x_mode_line_or_eval())
4800 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4801 else
4802 compl_cont_status = 0;
4803}
4804
4805/*
4806 * start insert mode completion
4807 */
4808 static int
4809ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004810{
4811 char_u *line;
4812 int startcol = 0; // column where searched text starts
4813 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004814 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004815 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004816 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004817
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004818 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004819
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004820 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004821 did_si = FALSE;
4822 can_si = FALSE;
4823 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004824 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004825 return FAIL;
4826
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004827 line = ml_get(curwin->w_cursor.lnum);
4828 curs_col = curwin->w_cursor.col;
4829 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004830
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004831 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4832 && compl_cont_mode == ctrl_x_mode)
4833 // this same ctrl-x_mode was interrupted previously. Continue the
4834 // completion.
4835 ins_compl_continue_search(line);
4836 else
4837 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004838
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004839 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004840 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004841 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004842 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004843 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4844 compl_cont_status = 0;
4845 compl_cont_status |= CONT_N_ADDS;
4846 compl_startpos = curwin->w_cursor;
4847 startcol = (int)curs_col;
4848 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004849 }
4850
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004851 // Work out completion pattern and original text -- webb
4852 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4853 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004854 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4855 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004856 // restore did_ai, so that adding comment leader works
4857 did_ai = save_did_ai;
4858 return FAIL;
4859 }
4860 // If "line" was changed while getting completion info get it again.
4861 if (line_invalid)
4862 line = ml_get(curwin->w_cursor.lnum);
4863
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004864 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004865 {
4866 edit_submode_pre = (char_u *)_(" Adding");
4867 if (ctrl_x_mode_line_or_eval())
4868 {
4869 // Insert a new line, keep indentation but ignore 'comments'.
4870 char_u *old = curbuf->b_p_com;
4871
4872 curbuf->b_p_com = (char_u *)"";
4873 compl_startpos.lnum = curwin->w_cursor.lnum;
4874 compl_startpos.col = compl_col;
4875 ins_eol('\r');
4876 curbuf->b_p_com = old;
4877 compl_length = 0;
4878 compl_col = curwin->w_cursor.col;
4879 }
4880 }
4881 else
4882 {
4883 edit_submode_pre = NULL;
4884 compl_startpos.col = compl_col;
4885 }
4886
4887 if (compl_cont_status & CONT_LOCAL)
4888 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4889 else
4890 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4891
4892 // If any of the original typed text has been changed we need to fix
4893 // the redo buffer.
4894 ins_compl_fixRedoBufForLeader(NULL);
4895
4896 // Always add completion for the original text.
4897 vim_free(compl_orig_text);
4898 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4899 if (p_ic)
4900 flags |= CP_ICASE;
4901 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4902 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4903 {
4904 VIM_CLEAR(compl_pattern);
4905 VIM_CLEAR(compl_orig_text);
4906 return FAIL;
4907 }
4908
4909 // showmode might reset the internal line pointers, so it must
4910 // be called before line = ml_get(), or when this address is no
4911 // longer needed. -- Acevedo.
4912 edit_submode_extra = (char_u *)_("-- Searching...");
4913 edit_submode_highl = HLF_COUNT;
4914 showmode();
4915 edit_submode_extra = NULL;
4916 out_flush();
4917
4918 return OK;
4919}
4920
4921/*
4922 * display the completion status message
4923 */
4924 static void
4925ins_compl_show_statusmsg(void)
4926{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004927 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004928 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004929 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004930 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004931 ? (char_u *)_("Hit end of paragraph")
4932 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004933 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004934 }
4935
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004936 if (edit_submode_extra == NULL)
4937 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004938 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004939 {
4940 edit_submode_extra = (char_u *)_("Back at original");
4941 edit_submode_highl = HLF_W;
4942 }
4943 else if (compl_cont_status & CONT_S_IPOS)
4944 {
4945 edit_submode_extra = (char_u *)_("Word from other line");
4946 edit_submode_highl = HLF_COUNT;
4947 }
4948 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4949 {
4950 edit_submode_extra = (char_u *)_("The only match");
4951 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004952 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004953 }
4954 else
4955 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004956#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004957 // Update completion sequence number when needed.
4958 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004959 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004960#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004961 // The match should always have a sequence number now, this is
4962 // just a safety check.
4963 if (compl_curr_match->cp_number != -1)
4964 {
4965 // Space for 10 text chars. + 2x10-digit no.s = 31.
4966 // Translations may need more than twice that.
4967 static char_u match_ref[81];
4968
4969 if (compl_matches > 0)
4970 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004971 _("match %d of %d"),
4972 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004973 else
4974 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004975 _("match %d"),
4976 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004977 edit_submode_extra = match_ref;
4978 edit_submode_highl = HLF_R;
4979 if (dollar_vcol >= 0)
4980 curs_columns(FALSE);
4981 }
4982 }
4983 }
4984
4985 // Show a message about what (completion) mode we're in.
4986 if (!compl_opt_suppress_empty)
4987 {
4988 showmode();
4989 if (!shortmess(SHM_COMPLETIONMENU))
4990 {
4991 if (edit_submode_extra != NULL)
4992 {
4993 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004994 {
4995 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004996 msg_attr((char *)edit_submode_extra,
4997 edit_submode_highl < HLF_COUNT
4998 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004999 msg_hist_off = FALSE;
5000 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005001 }
5002 else
5003 msg_clr_cmdline(); // necessary for "noshowmode"
5004 }
5005 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005006}
5007
5008/*
5009 * Do Insert mode completion.
5010 * Called when character "c" was typed, which has a meaning for completion.
5011 * Returns OK if completion was done, FAIL if something failed (out of mem).
5012 */
5013 int
5014ins_complete(int c, int enable_pum)
5015{
5016 int n;
5017 int save_w_wrow;
5018 int save_w_leftcol;
5019 int insert_match;
5020
5021 compl_direction = ins_compl_key2dir(c);
5022 insert_match = ins_compl_use_match(c);
5023
5024 if (!compl_started)
5025 {
5026 if (ins_compl_start() == FAIL)
5027 return FAIL;
5028 }
5029 else if (insert_match && stop_arrow() == FAIL)
5030 return FAIL;
5031
5032 compl_shown_match = compl_curr_match;
5033 compl_shows_dir = compl_direction;
5034
5035 // Find next match (and following matches).
5036 save_w_wrow = curwin->w_wrow;
5037 save_w_leftcol = curwin->w_leftcol;
5038 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5039
5040 // may undisplay the popup menu
5041 ins_compl_upd_pum();
5042
5043 if (n > 1) // all matches have been found
5044 compl_matches = n;
5045 compl_curr_match = compl_shown_match;
5046 compl_direction = compl_shows_dir;
5047
5048 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5049 // mode.
5050 if (got_int && !global_busy)
5051 {
5052 (void)vgetc();
5053 got_int = FALSE;
5054 }
5055
5056 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005057 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005058 {
5059 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5060 // because we couldn't expand anything at first place, but if we used
5061 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5062 // (such as M in M'exico) if not tried already. -- Acevedo
5063 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005064 || compl_status_adding()
5065 || (ctrl_x_mode_not_default()
5066 && !ctrl_x_mode_path_patterns()
5067 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005068 compl_cont_status &= ~CONT_N_ADDS;
5069 }
5070
5071 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5072 compl_cont_status |= CONT_S_IPOS;
5073 else
5074 compl_cont_status &= ~CONT_S_IPOS;
5075
5076 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005077
5078 // Show the popup menu, unless we got interrupted.
5079 if (enable_pum && !compl_interrupted)
5080 show_pum(save_w_wrow, save_w_leftcol);
5081
5082 compl_was_interrupted = compl_interrupted;
5083 compl_interrupted = FALSE;
5084
5085 return OK;
5086}
5087
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005088/*
5089 * Remove (if needed) and show the popup menu
5090 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005091 static void
5092show_pum(int prev_w_wrow, int prev_w_leftcol)
5093{
5094 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005095 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005096 RedrawingDisabled = 0;
5097
5098 // If the cursor moved or the display scrolled we need to remove the pum
5099 // first.
5100 setcursor();
5101 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5102 ins_compl_del_pum();
5103
5104 ins_compl_show_pum();
5105 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005106
5107 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005108}
5109
5110/*
5111 * Looks in the first "len" chars. of "src" for search-metachars.
5112 * If dest is not NULL the chars. are copied there quoting (with
5113 * a backslash) the metachars, and dest would be NUL terminated.
5114 * Returns the length (needed) of dest
5115 */
5116 static unsigned
5117quote_meta(char_u *dest, char_u *src, int len)
5118{
5119 unsigned m = (unsigned)len + 1; // one extra for the NUL
5120
5121 for ( ; --len >= 0; src++)
5122 {
5123 switch (*src)
5124 {
5125 case '.':
5126 case '*':
5127 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005128 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005129 break;
5130 // FALLTHROUGH
5131 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005132 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005133 break;
5134 // FALLTHROUGH
5135 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005136 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005137 break;
5138 // FALLTHROUGH
5139 case '^': // currently it's not needed.
5140 case '$':
5141 m++;
5142 if (dest != NULL)
5143 *dest++ = '\\';
5144 break;
5145 }
5146 if (dest != NULL)
5147 *dest++ = *src;
5148 // Copy remaining bytes of a multibyte character.
5149 if (has_mbyte)
5150 {
5151 int i, mb_len;
5152
5153 mb_len = (*mb_ptr2len)(src) - 1;
5154 if (mb_len > 0 && len >= mb_len)
5155 for (i = 0; i < mb_len; ++i)
5156 {
5157 --len;
5158 ++src;
5159 if (dest != NULL)
5160 *dest++ = *src;
5161 }
5162 }
5163 }
5164 if (dest != NULL)
5165 *dest = NUL;
5166
5167 return m;
5168}
5169
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005170#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005171 void
5172free_insexpand_stuff(void)
5173{
5174 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005175# ifdef FEAT_EVAL
5176 free_callback(&cfu_cb);
5177 free_callback(&ofu_cb);
5178 free_callback(&tsrfu_cb);
5179# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005180}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005181#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005182
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005183#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005184/*
5185 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5186 * spelled word, if there is one.
5187 */
5188 static void
5189spell_back_to_badword(void)
5190{
5191 pos_T tpos = curwin->w_cursor;
5192
5193 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5194 if (curwin->w_cursor.col != tpos.col)
5195 start_arrow(&tpos);
5196}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005197#endif