blob: 897c3b587061a409444a3078df0cc3cbc85d76f3 [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;
John Marriott8c85a2a2024-05-20 19:18:26 +0200179static size_t compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100180static int compl_direction = FORWARD;
181static int compl_shows_dir = FORWARD;
182static int compl_pending = 0; // > 1 for postponed CTRL-N
183static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000184// Length in bytes of the text being completed (this is deleted to be replaced
185// by the match.)
186static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100187static colnr_T compl_col = 0; // column where the text starts
188 // that is being completed
189static char_u *compl_orig_text = NULL; // text as it was before
190 // completion started
191static int compl_cont_mode = 0;
192static expand_T compl_xp;
193
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000194// List of flags for method of completion.
195static int compl_cont_status = 0;
196# define CONT_ADDING 1 // "normal" or "adding" expansion
197# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
198 // it's set only iff N_ADDS is set
199# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
200# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
201 // if so, word-wise-expansion will set SOL
202# define CONT_SOL 16 // pattern includes start of line, just for
203 // word-wise expansion, not set for ^X^L
204# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
205 // expansion, (eg use complete=.)
206
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100207static int compl_opt_refresh_always = FALSE;
208static int compl_opt_suppress_empty = FALSE;
209
Bram Moolenaar08928322020-01-04 14:32:48 +0100210static 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 +0100211static void ins_compl_longest_match(compl_T *match);
212static void ins_compl_del_pum(void);
213static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
214static char_u *find_line_end(char_u *ptr);
215static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100216static int ins_compl_need_restart(void);
217static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000218static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100219static void ins_compl_restart(void);
220static void ins_compl_set_original_text(char_u *str);
221static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
222# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
223static void ins_compl_add_list(list_T *list);
224static void ins_compl_add_dict(dict_T *dict);
225# endif
226static int ins_compl_key2dir(int c);
227static int ins_compl_pum_key(int c);
228static int ins_compl_key2count(int c);
229static void show_pum(int prev_w_wrow, int prev_w_leftcol);
230static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100231
232#ifdef FEAT_SPELL
233static void spell_back_to_badword(void);
234static int spell_bad_len = 0; // length of located bad word
235#endif
236
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100237/*
238 * CTRL-X pressed in Insert mode.
239 */
240 void
241ins_ctrl_x(void)
242{
zeertzjqdca29d92021-08-31 19:12:51 +0200243 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100244 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000245 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100246 if (compl_cont_status & CONT_N_ADDS)
247 compl_cont_status |= CONT_INTRPT;
248 else
249 compl_cont_status = 0;
250 // We're not sure which CTRL-X mode it will be yet
251 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
252 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
253 edit_submode_pre = NULL;
254 showmode();
255 }
zeertzjqdca29d92021-08-31 19:12:51 +0200256 else
257 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
258 // CTRL-V look like CTRL-N
259 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100260
LemonBoy2bf52dd2022-04-09 18:17:34 +0100261 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100262}
263
264/*
265 * Functions to check the current CTRL-X mode.
266 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000267int ctrl_x_mode_none(void)
268 { return ctrl_x_mode == 0; }
269int ctrl_x_mode_normal(void)
270 { return ctrl_x_mode == CTRL_X_NORMAL; }
271int ctrl_x_mode_scroll(void)
272 { return ctrl_x_mode == CTRL_X_SCROLL; }
273int ctrl_x_mode_whole_line(void)
274 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
275int ctrl_x_mode_files(void)
276 { return ctrl_x_mode == CTRL_X_FILES; }
277int ctrl_x_mode_tags(void)
278 { return ctrl_x_mode == CTRL_X_TAGS; }
279int ctrl_x_mode_path_patterns(void)
280 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
281int ctrl_x_mode_path_defines(void)
282 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
283int ctrl_x_mode_dictionary(void)
284 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
285int ctrl_x_mode_thesaurus(void)
286 { return ctrl_x_mode == CTRL_X_THESAURUS; }
287int ctrl_x_mode_cmdline(void)
288 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200289 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000290int ctrl_x_mode_function(void)
291 { return ctrl_x_mode == CTRL_X_FUNCTION; }
292int ctrl_x_mode_omni(void)
293 { return ctrl_x_mode == CTRL_X_OMNI; }
294int ctrl_x_mode_spell(void)
295 { return ctrl_x_mode == CTRL_X_SPELL; }
296static int ctrl_x_mode_eval(void)
297 { return ctrl_x_mode == CTRL_X_EVAL; }
298int ctrl_x_mode_line_or_eval(void)
299 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100300
301/*
302 * Whether other than default completion has been selected.
303 */
304 int
305ctrl_x_mode_not_default(void)
306{
307 return ctrl_x_mode != CTRL_X_NORMAL;
308}
309
310/*
zeertzjqdca29d92021-08-31 19:12:51 +0200311 * Whether CTRL-X was typed without a following character,
312 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100313 */
314 int
315ctrl_x_mode_not_defined_yet(void)
316{
317 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
318}
319
320/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000321 * Return TRUE if currently in "normal" or "adding" insert completion matches
322 * state
323 */
324 int
325compl_status_adding(void)
326{
327 return compl_cont_status & CONT_ADDING;
328}
329
330/*
331 * Return TRUE if the completion pattern includes start of line, just for
332 * word-wise expansion.
333 */
334 int
335compl_status_sol(void)
336{
337 return compl_cont_status & CONT_SOL;
338}
339
340/*
341 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
342 */
343 int
344compl_status_local(void)
345{
346 return compl_cont_status & CONT_LOCAL;
347}
348
349/*
350 * Clear the completion status flags
351 */
352 void
353compl_status_clear(void)
354{
355 compl_cont_status = 0;
356}
357
358/*
359 * Return TRUE if completion is using the forward direction matches
360 */
361 static int
362compl_dir_forward(void)
363{
364 return compl_direction == FORWARD;
365}
366
367/*
368 * Return TRUE if currently showing forward completion matches
369 */
370 static int
371compl_shows_dir_forward(void)
372{
373 return compl_shows_dir == FORWARD;
374}
375
376/*
377 * Return TRUE if currently showing backward completion matches
378 */
379 static int
380compl_shows_dir_backward(void)
381{
382 return compl_shows_dir == BACKWARD;
383}
384
385/*
386 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100387 */
388 int
389has_compl_option(int dict_opt)
390{
391 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200392#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100393 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200394#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100395 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100396 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
397#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100398 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100399#endif
400 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100401 {
402 ctrl_x_mode = CTRL_X_NORMAL;
403 edit_submode = NULL;
404 msg_attr(dict_opt ? _("'dictionary' option is empty")
405 : _("'thesaurus' option is empty"),
406 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100407 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100408 {
409 vim_beep(BO_COMPL);
410 setcursor();
411 out_flush();
412#ifdef FEAT_EVAL
413 if (!get_vim_var_nr(VV_TESTING))
414#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100415 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100416 }
417 return FALSE;
418 }
419 return TRUE;
420}
421
422/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000423 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100424 * This depends on the current mode.
425 */
426 int
427vim_is_ctrl_x_key(int c)
428{
429 // Always allow ^R - let its results then be checked
430 if (c == Ctrl_R)
431 return TRUE;
432
433 // Accept <PageUp> and <PageDown> if the popup menu is visible.
434 if (ins_compl_pum_key(c))
435 return TRUE;
436
437 switch (ctrl_x_mode)
438 {
439 case 0: // Not in any CTRL-X mode
440 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
441 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200442 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100443 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
444 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
445 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
446 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
447 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200448 || c == Ctrl_S || c == Ctrl_K || c == 's'
449 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100450 case CTRL_X_SCROLL:
451 return (c == Ctrl_Y || c == Ctrl_E);
452 case CTRL_X_WHOLE_LINE:
453 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
454 case CTRL_X_FILES:
455 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
456 case CTRL_X_DICTIONARY:
457 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
458 case CTRL_X_THESAURUS:
459 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
460 case CTRL_X_TAGS:
461 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
462#ifdef FEAT_FIND_ID
463 case CTRL_X_PATH_PATTERNS:
464 return (c == Ctrl_P || c == Ctrl_N);
465 case CTRL_X_PATH_DEFINES:
466 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
467#endif
468 case CTRL_X_CMDLINE:
469 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
470 || c == Ctrl_X);
471#ifdef FEAT_COMPL_FUNC
472 case CTRL_X_FUNCTION:
473 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
474 case CTRL_X_OMNI:
475 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
476#endif
477 case CTRL_X_SPELL:
478 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
479 case CTRL_X_EVAL:
480 return (c == Ctrl_P || c == Ctrl_N);
481 }
482 internal_error("vim_is_ctrl_x_key()");
483 return FALSE;
484}
485
486/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000487 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000488 */
489 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000490match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000491{
492 return match->cp_flags & CP_ORIGINAL_TEXT;
493}
494
495/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000496 * Returns TRUE if "match" is the first match in the completion list.
497 */
498 static int
499is_first_match(compl_T *match)
500{
501 return match == compl_first_match;
502}
503
504/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100505 * Return TRUE when character "c" is part of the item currently being
506 * completed. Used to decide whether to abandon complete mode when the menu
507 * is visible.
508 */
509 int
510ins_compl_accept_char(int c)
511{
512 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
513 // When expanding an identifier only accept identifier chars.
514 return vim_isIDc(c);
515
516 switch (ctrl_x_mode)
517 {
518 case CTRL_X_FILES:
519 // When expanding file name only accept file name chars. But not
520 // path separators, so that "proto/<Tab>" expands files in
521 // "proto", not "proto/" as a whole
522 return vim_isfilec(c) && !vim_ispathsep(c);
523
524 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200525 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100526 case CTRL_X_OMNI:
527 // Command line and Omni completion can work with just about any
528 // printable character, but do stop at white space.
529 return vim_isprintc(c) && !VIM_ISWHITE(c);
530
531 case CTRL_X_WHOLE_LINE:
532 // For while line completion a space can be part of the line.
533 return vim_isprintc(c);
534 }
535 return vim_iswordc(c);
536}
537
538/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000539 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100540 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000541 */
542 static char_u *
543ins_compl_infercase_gettext(
544 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100545 int char_len,
546 int compl_char_len,
547 int min_len,
548 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000549{
550 int *wca; // Wide character array.
551 char_u *p;
552 int i, c;
553 int has_lower = FALSE;
554 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100555 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000556
557 IObuff[0] = NUL;
558
559 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100560 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000561 if (wca == NULL)
562 return IObuff;
563
564 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100565 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000566 if (has_mbyte)
567 wca[i] = mb_ptr2char_adv(&p);
568 else
569 wca[i] = *(p++);
570
571 // Rule 1: Were any chars converted to lower?
572 p = compl_orig_text;
573 for (i = 0; i < min_len; ++i)
574 {
575 if (has_mbyte)
576 c = mb_ptr2char_adv(&p);
577 else
578 c = *(p++);
579 if (MB_ISLOWER(c))
580 {
581 has_lower = TRUE;
582 if (MB_ISUPPER(wca[i]))
583 {
584 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100585 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000586 wca[i] = MB_TOLOWER(wca[i]);
587 break;
588 }
589 }
590 }
591
592 // Rule 2: No lower case, 2nd consecutive letter converted to
593 // upper case.
594 if (!has_lower)
595 {
596 p = compl_orig_text;
597 for (i = 0; i < min_len; ++i)
598 {
599 if (has_mbyte)
600 c = mb_ptr2char_adv(&p);
601 else
602 c = *(p++);
603 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
604 {
605 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100606 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000607 wca[i] = MB_TOUPPER(wca[i]);
608 break;
609 }
610 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
611 }
612 }
613
614 // Copy the original case of the part we typed.
615 p = compl_orig_text;
616 for (i = 0; i < min_len; ++i)
617 {
618 if (has_mbyte)
619 c = mb_ptr2char_adv(&p);
620 else
621 c = *(p++);
622 if (MB_ISLOWER(c))
623 wca[i] = MB_TOLOWER(wca[i]);
624 else if (MB_ISUPPER(c))
625 wca[i] = MB_TOUPPER(wca[i]);
626 }
627
628 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000629 p = IObuff;
630 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100631 ga_init2(&gap, 1, 500);
632 while (i < char_len)
633 {
634 if (gap.ga_data != NULL)
635 {
636 if (ga_grow(&gap, 10) == FAIL)
637 {
638 ga_clear(&gap);
639 return (char_u *)"[failed]";
640 }
641 p = (char_u *)gap.ga_data + gap.ga_len;
642 if (has_mbyte)
643 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
644 else
645 {
646 *p = wca[i++];
647 ++gap.ga_len;
648 }
649 }
650 else if ((p - IObuff) + 6 >= IOSIZE)
651 {
652 // Multi-byte characters can occupy up to five bytes more than
653 // ASCII characters, and we also need one byte for NUL, so when
654 // getting to six bytes from the edge of IObuff switch to using a
655 // growarray. Add the character in the next round.
656 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100657 {
658 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100659 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100660 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100661 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100662 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100663 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100664 }
665 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000666 p += (*mb_char2bytes)(wca[i++], p);
667 else
668 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100669 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000670 vim_free(wca);
671
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100672 if (gap.ga_data != NULL)
673 {
674 *tofree = gap.ga_data;
675 return gap.ga_data;
676 }
677
678 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000679 return IObuff;
680}
681
682/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100683 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
684 * case of the originally typed text is used, and the case of the completed
685 * text is inferred, ie this tries to work out what case you probably wanted
686 * the rest of the word to be in -- webb
687 */
688 int
689ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200690 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100691 int len,
692 int icase,
693 char_u *fname,
694 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200695 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100696{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200697 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100698 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100699 int char_len; // count multi-byte characters
700 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200702 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100703 int res;
704 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100705
706 if (p_ic && curbuf->b_p_inf && len > 0)
707 {
708 // Infer case of completed part.
709
710 // Find actual length of completion.
711 if (has_mbyte)
712 {
713 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100714 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 while (*p != NUL)
716 {
717 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100718 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719 }
720 }
721 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100722 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723
724 // Find actual length of original text.
725 if (has_mbyte)
726 {
727 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100728 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100729 while (*p != NUL)
730 {
731 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100732 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 }
734 }
735 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100736 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100737
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100738 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100739 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100740 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100741
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100742 str = ins_compl_infercase_gettext(str, char_len,
743 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100744 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200745 if (cont_s_ipos)
746 flags |= CP_CONT_S_IPOS;
747 if (icase)
748 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200749
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100750 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
751 vim_free(tofree);
752 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100753}
754
755/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000756 * Add a match to the list of matches. The arguments are:
757 * str - text of the match to add
758 * len - length of "str". If -1, then the length of "str" is
759 * computed.
760 * fname - file name to associate with this match.
761 * cptext - list of strings to use with this match (for abbr, menu, info
762 * and kind)
763 * user_data - user supplied data (any vim type) for this match
764 * cdir - match direction. If 0, use "compl_direction".
765 * flags_arg - match flags (cp_flags)
766 * adup - accept this match even if it is already present.
767 * If "cdir" is FORWARD, then the match is added after the current match.
768 * Otherwise, it is added before the current match.
769 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100770 * If the given string is already in the list of completions, then return
771 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
772 * maybe because alloc() returns NULL, then FAIL is returned.
773 */
774 static int
775ins_compl_add(
776 char_u *str,
777 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100778 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 char_u **cptext, // extra text for popup menu or NULL
780 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100781 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200782 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100783 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100784{
785 compl_T *match;
786 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200787 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100788
Bram Moolenaarceb06192021-04-04 15:05:22 +0200789 if (flags & CP_FAST)
790 fast_breakcheck();
791 else
792 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100793 if (got_int)
794 return FAIL;
795 if (len < 0)
796 len = (int)STRLEN(str);
797
798 // If the same match is already present, don't add it.
799 if (compl_first_match != NULL && !adup)
800 {
801 match = compl_first_match;
802 do
803 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000804 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100805 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100806 && ((int)STRLEN(match->cp_str) <= len
807 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100808 return NOTDONE;
809 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000810 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100811 }
812
813 // Remove any popup menu before changing the list of matches.
814 ins_compl_del_pum();
815
816 // Allocate a new match structure.
817 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200818 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100819 if (match == NULL)
820 return FAIL;
821 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200822 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100823 match->cp_number = 0;
824 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
825 {
826 vim_free(match);
827 return FAIL;
828 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100829
830 // match-fname is:
831 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200832 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833 // - NULL otherwise. --Acevedo
834 if (fname != NULL
835 && compl_curr_match != NULL
836 && compl_curr_match->cp_fname != NULL
837 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
838 match->cp_fname = compl_curr_match->cp_fname;
839 else if (fname != NULL)
840 {
841 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200842 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 }
844 else
845 match->cp_fname = NULL;
846 match->cp_flags = flags;
847
848 if (cptext != NULL)
849 {
850 int i;
851
852 for (i = 0; i < CPT_COUNT; ++i)
853 if (cptext[i] != NULL && *cptext[i] != NUL)
854 match->cp_text[i] = vim_strsave(cptext[i]);
855 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100856#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100857 if (user_data != NULL)
858 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100859#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100860
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000861 // Link the new match structure after (FORWARD) or before (BACKWARD) the
862 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863 if (compl_first_match == NULL)
864 match->cp_next = match->cp_prev = NULL;
865 else if (dir == FORWARD)
866 {
867 match->cp_next = compl_curr_match->cp_next;
868 match->cp_prev = compl_curr_match;
869 }
870 else // BACKWARD
871 {
872 match->cp_next = compl_curr_match;
873 match->cp_prev = compl_curr_match->cp_prev;
874 }
875 if (match->cp_next)
876 match->cp_next->cp_prev = match;
877 if (match->cp_prev)
878 match->cp_prev->cp_next = match;
879 else // if there's nothing before, it is the first match
880 compl_first_match = match;
881 compl_curr_match = match;
882
883 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200884 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100885 ins_compl_longest_match(match);
886
887 return OK;
888}
889
890/*
891 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200892 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100893 */
894 static int
895ins_compl_equal(compl_T *match, char_u *str, int len)
896{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200897 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200898 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200899 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100900 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
901 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
902}
903
904/*
905 * Reduce the longest common string for match "match".
906 */
907 static void
908ins_compl_longest_match(compl_T *match)
909{
910 char_u *p, *s;
911 int c1, c2;
912 int had_match;
913
914 if (compl_leader == NULL)
915 {
916 // First match, use it as a whole.
917 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000918 if (compl_leader == NULL)
919 return;
920
921 had_match = (curwin->w_cursor.col > compl_col);
922 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000923 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000924 ins_redraw(FALSE);
925
926 // When the match isn't there (to avoid matching itself) remove it
927 // again after redrawing.
928 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100930 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000931
932 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000934
935 // Reduce the text if this match differs from compl_leader.
936 p = compl_leader;
937 s = match->cp_str;
938 while (*p != NUL)
939 {
940 if (has_mbyte)
941 {
942 c1 = mb_ptr2char(p);
943 c2 = mb_ptr2char(s);
944 }
945 else
946 {
947 c1 = *p;
948 c2 = *s;
949 }
950 if ((match->cp_flags & CP_ICASE)
951 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
952 break;
953 if (has_mbyte)
954 {
955 MB_PTR_ADV(p);
956 MB_PTR_ADV(s);
957 }
958 else
959 {
960 ++p;
961 ++s;
962 }
963 }
964
965 if (*p != NUL)
966 {
967 // Leader was shortened, need to change the inserted text.
968 *p = NUL;
969 had_match = (curwin->w_cursor.col > compl_col);
970 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000971 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000972 ins_redraw(FALSE);
973
974 // When the match isn't there (to avoid matching itself) remove it
975 // again after redrawing.
976 if (!had_match)
977 ins_compl_delete();
978 }
979
980 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100981}
982
983/*
984 * Add an array of matches to the list of matches.
985 * Frees matches[].
986 */
987 static void
988ins_compl_add_matches(
989 int num_matches,
990 char_u **matches,
991 int icase)
992{
993 int i;
994 int add_r = OK;
995 int dir = compl_direction;
996
997 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100998 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200999 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001000 // if dir was BACKWARD then honor it just once
1001 dir = FORWARD;
1002 FreeWild(num_matches, matches);
1003}
1004
1005/*
1006 * Make the completion list cyclic.
1007 * Return the number of matches (excluding the original).
1008 */
1009 static int
1010ins_compl_make_cyclic(void)
1011{
1012 compl_T *match;
1013 int count = 0;
1014
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001015 if (compl_first_match == NULL)
1016 return 0;
1017
1018 // Find the end of the list.
1019 match = compl_first_match;
1020 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001021 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001022 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001023 match = match->cp_next;
1024 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001025 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001026 match->cp_next = compl_first_match;
1027 compl_first_match->cp_prev = match;
1028
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001029 return count;
1030}
1031
1032/*
1033 * Return whether there currently is a shown match.
1034 */
1035 int
1036ins_compl_has_shown_match(void)
1037{
1038 return compl_shown_match == NULL
1039 || compl_shown_match != compl_shown_match->cp_next;
1040}
1041
1042/*
1043 * Return whether the shown match is long enough.
1044 */
1045 int
1046ins_compl_long_shown_match(void)
1047{
1048 return (int)STRLEN(compl_shown_match->cp_str)
1049 > curwin->w_cursor.col - compl_col;
1050}
1051
1052/*
1053 * Set variables that store noselect and noinsert behavior from the
1054 * 'completeopt' value.
1055 */
1056 void
1057completeopt_was_set(void)
1058{
1059 compl_no_insert = FALSE;
1060 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001061 compl_longest = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001062 if (strstr((char *)p_cot, "noselect") != NULL)
1063 compl_no_select = TRUE;
1064 if (strstr((char *)p_cot, "noinsert") != NULL)
1065 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001066 if (strstr((char *)p_cot, "longest") != NULL)
1067 compl_longest = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001068}
1069
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001070
1071// "compl_match_array" points the currently displayed list of entries in the
1072// popup menu. It is NULL when there is no popup menu.
1073static pumitem_T *compl_match_array = NULL;
1074static int compl_match_arraysize;
1075
1076/*
1077 * Update the screen and when there is any scrolling remove the popup menu.
1078 */
1079 static void
1080ins_compl_upd_pum(void)
1081{
1082 int h;
1083
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001084 if (compl_match_array == NULL)
1085 return;
1086
1087 h = curwin->w_cline_height;
1088 // Update the screen later, before drawing the popup menu over it.
1089 pum_call_update_screen();
1090 if (h != curwin->w_cline_height)
1091 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001092}
1093
1094/*
1095 * Remove any popup menu.
1096 */
1097 static void
1098ins_compl_del_pum(void)
1099{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001100 if (compl_match_array == NULL)
1101 return;
1102
1103 pum_undisplay();
1104 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001105}
1106
1107/*
1108 * Return TRUE if the popup menu should be displayed.
1109 */
1110 int
1111pum_wanted(void)
1112{
1113 // 'completeopt' must contain "menu" or "menuone"
1114 if (vim_strchr(p_cot, 'm') == NULL)
1115 return FALSE;
1116
1117 // The display looks bad on a B&W display.
1118 if (t_colors < 8
1119#ifdef FEAT_GUI
1120 && !gui.in_use
1121#endif
1122 )
1123 return FALSE;
1124 return TRUE;
1125}
1126
1127/*
1128 * Return TRUE if there are two or more matches to be shown in the popup menu.
1129 * One if 'completopt' contains "menuone".
1130 */
1131 static int
1132pum_enough_matches(void)
1133{
1134 compl_T *compl;
1135 int i;
1136
1137 // Don't display the popup menu if there are no matches or there is only
1138 // one (ignoring the original text).
1139 compl = compl_first_match;
1140 i = 0;
1141 do
1142 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001143 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001144 break;
1145 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001146 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001147
1148 if (strstr((char *)p_cot, "menuone") != NULL)
1149 return (i >= 1);
1150 return (i >= 2);
1151}
1152
Bram Moolenaar3075a452021-11-17 15:51:52 +00001153#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001154/*
1155 * Allocate Dict for the completed item.
1156 * { word, abbr, menu, kind, info }
1157 */
1158 static dict_T *
1159ins_compl_dict_alloc(compl_T *match)
1160{
1161 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1162
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001163 if (dict == NULL)
1164 return NULL;
1165
1166 dict_add_string(dict, "word", match->cp_str);
1167 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1168 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1169 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1170 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1171 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1172 dict_add_string(dict, "user_data", (char_u *)"");
1173 else
1174 dict_add_tv(dict, "user_data", &match->cp_user_data);
1175
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001176 return dict;
1177}
1178
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001179/*
1180 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1181 * completion menu is changed.
1182 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001183 static void
1184trigger_complete_changed_event(int cur)
1185{
1186 dict_T *v_event;
1187 dict_T *item;
1188 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001189 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001190
1191 if (recursive)
1192 return;
1193
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001194 if (cur < 0)
1195 item = dict_alloc();
1196 else
1197 item = ins_compl_dict_alloc(compl_curr_match);
1198 if (item == NULL)
1199 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001200 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001201 dict_add_dict(v_event, "completed_item", item);
1202 pum_set_event_info(v_event);
1203 dict_set_items_ro(v_event);
1204
1205 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001206 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001207 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001208 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001209 recursive = FALSE;
1210
Bram Moolenaar3075a452021-11-17 15:51:52 +00001211 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001212}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001213#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001214
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001215/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001216 * Build a popup menu to show the completion matches.
1217 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1218 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001219 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001220 static int
1221ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001222{
1223 compl_T *compl;
1224 compl_T *shown_compl = NULL;
1225 int did_find_shown_match = FALSE;
1226 int shown_match_ok = FALSE;
1227 int i;
1228 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001229 int lead_len = 0;
1230
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001231 // Need to build the popup menu list.
1232 compl_match_arraysize = 0;
1233 compl = compl_first_match;
1234 if (compl_leader != NULL)
1235 lead_len = (int)STRLEN(compl_leader);
1236
1237 do
1238 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001239 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001240 && (compl_leader == NULL
1241 || ins_compl_equal(compl, compl_leader, lead_len)))
1242 ++compl_match_arraysize;
1243 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001244 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001245
1246 if (compl_match_arraysize == 0)
1247 return -1;
1248
1249 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1250 if (compl_match_array == NULL)
1251 return -1;
1252
1253 // If the current match is the original text don't find the first
1254 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001255 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001256 shown_match_ok = TRUE;
1257
glepnir53387c52024-05-27 15:11:01 +02001258 if (compl_leader != NULL
1259 && STRCMP(compl_leader, compl_orig_text) == 0
1260 && shown_match_ok == FALSE)
1261 compl_shown_match = compl_no_select ? compl_first_match
1262 : compl_first_match->cp_next;
1263
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001264 i = 0;
1265 compl = compl_first_match;
1266 do
1267 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001268 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001269 && (compl_leader == NULL
1270 || ins_compl_equal(compl, compl_leader, lead_len)))
1271 {
1272 if (!shown_match_ok)
1273 {
1274 if (compl == compl_shown_match || did_find_shown_match)
1275 {
1276 // This item is the shown match or this is the
1277 // first displayed item after the shown match.
1278 compl_shown_match = compl;
1279 did_find_shown_match = TRUE;
1280 shown_match_ok = TRUE;
1281 }
1282 else
1283 // Remember this displayed match for when the
1284 // shown match is just below it.
1285 shown_compl = compl;
1286 cur = i;
1287 }
1288
1289 if (compl->cp_text[CPT_ABBR] != NULL)
1290 compl_match_array[i].pum_text =
1291 compl->cp_text[CPT_ABBR];
1292 else
1293 compl_match_array[i].pum_text = compl->cp_str;
1294 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1295 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1296 if (compl->cp_text[CPT_MENU] != NULL)
1297 compl_match_array[i++].pum_extra =
1298 compl->cp_text[CPT_MENU];
1299 else
1300 compl_match_array[i++].pum_extra = compl->cp_fname;
1301 }
1302
1303 if (compl == compl_shown_match)
1304 {
1305 did_find_shown_match = TRUE;
1306
1307 // When the original text is the shown match don't set
1308 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001309 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001310 shown_match_ok = TRUE;
1311
1312 if (!shown_match_ok && shown_compl != NULL)
1313 {
1314 // The shown match isn't displayed, set it to the
1315 // previously displayed match.
1316 compl_shown_match = shown_compl;
1317 shown_match_ok = TRUE;
1318 }
1319 }
1320 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001321 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001322
1323 if (!shown_match_ok) // no displayed match at all
1324 cur = -1;
1325
1326 return cur;
1327}
1328
1329/*
1330 * Show the popup menu for the list of matches.
1331 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1332 */
1333 void
1334ins_compl_show_pum(void)
1335{
1336 int i;
1337 int cur = -1;
1338 colnr_T col;
1339
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001340 if (!pum_wanted() || !pum_enough_matches())
1341 return;
1342
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001343 // Update the screen later, before drawing the popup menu over it.
1344 pum_call_update_screen();
1345
1346 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001347 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001348 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001349 else
1350 {
1351 // popup menu already exists, only need to find the current item.
1352 for (i = 0; i < compl_match_arraysize; ++i)
1353 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1354 || compl_match_array[i].pum_text
1355 == compl_shown_match->cp_text[CPT_ABBR])
1356 {
1357 cur = i;
1358 break;
1359 }
1360 }
1361
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001362 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001363 {
1364#ifdef FEAT_EVAL
1365 if (compl_started && has_completechanged())
1366 trigger_complete_changed_event(cur);
1367#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001368 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001369 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001370
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001371 // In Replace mode when a $ is displayed at the end of the line only
1372 // part of the screen would be updated. We do need to redraw here.
1373 dollar_vcol = -1;
1374
1375 // Compute the screen column of the start of the completed text.
1376 // Use the cursor to get all wrapping and other settings right.
1377 col = curwin->w_cursor.col;
1378 curwin->w_cursor.col = compl_col;
1379 pum_display(compl_match_array, compl_match_arraysize, cur);
1380 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001381
glepnircbb46b42024-02-03 18:11:13 +01001382 // After adding leader, set the current match to shown match.
1383 if (compl_started && compl_curr_match != compl_shown_match)
1384 compl_curr_match = compl_shown_match;
1385
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001386#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001387 if (has_completechanged())
1388 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001389#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001390}
1391
1392#define DICT_FIRST (1) // use just first element in "dict"
1393#define DICT_EXACT (2) // "dict" is the exact name of a file
1394
1395/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001396 * Add any identifiers that match the given pattern "pat" in the list of
1397 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001398 */
1399 static void
1400ins_compl_dictionaries(
1401 char_u *dict_start,
1402 char_u *pat,
1403 int flags, // DICT_FIRST and/or DICT_EXACT
1404 int thesaurus) // Thesaurus completion
1405{
1406 char_u *dict = dict_start;
1407 char_u *ptr;
1408 char_u *buf;
1409 regmatch_T regmatch;
1410 char_u **files;
1411 int count;
1412 int save_p_scs;
1413 int dir = compl_direction;
1414
1415 if (*dict == NUL)
1416 {
1417#ifdef FEAT_SPELL
1418 // When 'dictionary' is empty and spell checking is enabled use
1419 // "spell".
1420 if (!thesaurus && curwin->w_p_spell)
1421 dict = (char_u *)"spell";
1422 else
1423#endif
1424 return;
1425 }
1426
1427 buf = alloc(LSIZE);
1428 if (buf == NULL)
1429 return;
1430 regmatch.regprog = NULL; // so that we can goto theend
1431
1432 // If 'infercase' is set, don't use 'smartcase' here
1433 save_p_scs = p_scs;
1434 if (curbuf->b_p_inf)
1435 p_scs = FALSE;
1436
1437 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1438 // to only match at the start of a line. Otherwise just match the
1439 // pattern. Also need to double backslashes.
1440 if (ctrl_x_mode_line_or_eval())
1441 {
1442 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1443 size_t len;
1444
1445 if (pat_esc == NULL)
1446 goto theend;
1447 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001448 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001449 if (ptr == NULL)
1450 {
1451 vim_free(pat_esc);
1452 goto theend;
1453 }
1454 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1455 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1456 vim_free(pat_esc);
1457 vim_free(ptr);
1458 }
1459 else
1460 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001461 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001462 if (regmatch.regprog == NULL)
1463 goto theend;
1464 }
1465
1466 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1467 regmatch.rm_ic = ignorecase(pat);
1468 while (*dict != NUL && !got_int && !compl_interrupted)
1469 {
1470 // copy one dictionary file name into buf
1471 if (flags == DICT_EXACT)
1472 {
1473 count = 1;
1474 files = &dict;
1475 }
1476 else
1477 {
1478 // Expand wildcards in the dictionary name, but do not allow
1479 // backticks (for security, the 'dict' option may have been set in
1480 // a modeline).
1481 copy_option_part(&dict, buf, LSIZE, ",");
1482# ifdef FEAT_SPELL
1483 if (!thesaurus && STRCMP(buf, "spell") == 0)
1484 count = -1;
1485 else
1486# endif
1487 if (vim_strchr(buf, '`') != NULL
1488 || expand_wildcards(1, &buf, &count, &files,
1489 EW_FILE|EW_SILENT) != OK)
1490 count = 0;
1491 }
1492
1493# ifdef FEAT_SPELL
1494 if (count == -1)
1495 {
1496 // Complete from active spelling. Skip "\<" in the pattern, we
1497 // don't use it as a RE.
1498 if (pat[0] == '\\' && pat[1] == '<')
1499 ptr = pat + 2;
1500 else
1501 ptr = pat;
1502 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1503 }
1504 else
1505# endif
1506 if (count > 0) // avoid warning for using "files" uninit
1507 {
1508 ins_compl_files(count, files, thesaurus, flags,
1509 &regmatch, buf, &dir);
1510 if (flags != DICT_EXACT)
1511 FreeWild(count, files);
1512 }
1513 if (flags != 0)
1514 break;
1515 }
1516
1517theend:
1518 p_scs = save_p_scs;
1519 vim_regfree(regmatch.regprog);
1520 vim_free(buf);
1521}
1522
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001523/*
1524 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1525 * skipping the word at 'skip_word'. Returns OK on success.
1526 */
1527 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001528thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001529 char_u *fname,
1530 char_u **buf_arg,
1531 int dir,
1532 char_u *skip_word)
1533{
1534 int status = OK;
1535 char_u *ptr;
1536 char_u *wstart;
1537
1538 // Add the other matches on the line
1539 ptr = *buf_arg;
1540 while (!got_int)
1541 {
1542 // Find start of the next word. Skip white
1543 // space and punctuation.
1544 ptr = find_word_start(ptr);
1545 if (*ptr == NUL || *ptr == NL)
1546 break;
1547 wstart = ptr;
1548
1549 // Find end of the word.
1550 if (has_mbyte)
1551 // Japanese words may have characters in
1552 // different classes, only separate words
1553 // with single-byte non-word characters.
1554 while (*ptr != NUL)
1555 {
1556 int l = (*mb_ptr2len)(ptr);
1557
1558 if (l < 2 && !vim_iswordc(*ptr))
1559 break;
1560 ptr += l;
1561 }
1562 else
1563 ptr = find_word_end(ptr);
1564
1565 // Add the word. Skip the regexp match.
1566 if (wstart != skip_word)
1567 {
1568 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1569 fname, dir, FALSE);
1570 if (status == FAIL)
1571 break;
1572 }
1573 }
1574
1575 *buf_arg = ptr;
1576 return status;
1577}
1578
1579/*
1580 * Process "count" dictionary/thesaurus "files" and add the text matching
1581 * "regmatch".
1582 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001583 static void
1584ins_compl_files(
1585 int count,
1586 char_u **files,
1587 int thesaurus,
1588 int flags,
1589 regmatch_T *regmatch,
1590 char_u *buf,
1591 int *dir)
1592{
1593 char_u *ptr;
1594 int i;
1595 FILE *fp;
1596 int add_r;
1597
1598 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1599 {
1600 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001601 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001602 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001603 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001604 vim_snprintf((char *)IObuff, IOSIZE,
1605 _("Scanning dictionary: %s"), (char *)files[i]);
1606 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1607 }
1608
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001609 if (fp == NULL)
1610 continue;
1611
1612 // Read dictionary file line by line.
1613 // Check each line for a match.
1614 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001615 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001616 ptr = buf;
1617 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001618 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001619 ptr = regmatch->startp[0];
1620 if (ctrl_x_mode_line_or_eval())
1621 ptr = find_line_end(ptr);
1622 else
1623 ptr = find_word_end(ptr);
1624 add_r = ins_compl_add_infercase(regmatch->startp[0],
1625 (int)(ptr - regmatch->startp[0]),
1626 p_ic, files[i], *dir, FALSE);
1627 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001628 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001629 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001630 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001631 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001632 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001633 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001634 if (add_r == OK)
1635 // if dir was BACKWARD then honor it just once
1636 *dir = FORWARD;
1637 else if (add_r == FAIL)
1638 break;
1639 // avoid expensive call to vim_regexec() when at end
1640 // of line
1641 if (*ptr == '\n' || got_int)
1642 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001643 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001644 line_breakcheck();
1645 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001646 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001647 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001648 }
1649}
1650
1651/*
1652 * Find the start of the next word.
1653 * Returns a pointer to the first char of the word. Also stops at a NUL.
1654 */
1655 char_u *
1656find_word_start(char_u *ptr)
1657{
1658 if (has_mbyte)
1659 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1660 ptr += (*mb_ptr2len)(ptr);
1661 else
1662 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1663 ++ptr;
1664 return ptr;
1665}
1666
1667/*
1668 * Find the end of the word. Assumes it starts inside a word.
1669 * Returns a pointer to just after the word.
1670 */
1671 char_u *
1672find_word_end(char_u *ptr)
1673{
1674 int start_class;
1675
1676 if (has_mbyte)
1677 {
1678 start_class = mb_get_class(ptr);
1679 if (start_class > 1)
1680 while (*ptr != NUL)
1681 {
1682 ptr += (*mb_ptr2len)(ptr);
1683 if (mb_get_class(ptr) != start_class)
1684 break;
1685 }
1686 }
1687 else
1688 while (vim_iswordc(*ptr))
1689 ++ptr;
1690 return ptr;
1691}
1692
1693/*
1694 * Find the end of the line, omitting CR and NL at the end.
1695 * Returns a pointer to just after the line.
1696 */
1697 static char_u *
1698find_line_end(char_u *ptr)
1699{
1700 char_u *s;
1701
1702 s = ptr + STRLEN(ptr);
1703 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1704 --s;
1705 return s;
1706}
1707
1708/*
1709 * Free the list of completions
1710 */
1711 static void
1712ins_compl_free(void)
1713{
1714 compl_T *match;
1715 int i;
1716
1717 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001718 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001719 VIM_CLEAR(compl_leader);
1720
1721 if (compl_first_match == NULL)
1722 return;
1723
1724 ins_compl_del_pum();
1725 pum_clear();
1726
1727 compl_curr_match = compl_first_match;
1728 do
1729 {
1730 match = compl_curr_match;
1731 compl_curr_match = compl_curr_match->cp_next;
1732 vim_free(match->cp_str);
1733 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001734 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001735 vim_free(match->cp_fname);
1736 for (i = 0; i < CPT_COUNT; ++i)
1737 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001738#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001739 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001740#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001741 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001742 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001743 compl_first_match = compl_curr_match = NULL;
1744 compl_shown_match = NULL;
1745 compl_old_match = NULL;
1746}
1747
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001748/*
1749 * Reset/clear the completion state.
1750 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001751 void
1752ins_compl_clear(void)
1753{
1754 compl_cont_status = 0;
1755 compl_started = FALSE;
1756 compl_matches = 0;
1757 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02001758 compl_patternlen = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001759 VIM_CLEAR(compl_leader);
1760 edit_submode_extra = NULL;
1761 VIM_CLEAR(compl_orig_text);
1762 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001763#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001764 // clear v:completed_item
1765 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001766#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001767}
1768
1769/*
1770 * Return TRUE when Insert completion is active.
1771 */
1772 int
1773ins_compl_active(void)
1774{
1775 return compl_started;
1776}
1777
1778/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001779 * Selected one of the matches. When FALSE the match was edited or using the
1780 * longest common string.
1781 */
1782 int
1783ins_compl_used_match(void)
1784{
1785 return compl_used_match;
1786}
1787
1788/*
1789 * Initialize get longest common string.
1790 */
1791 void
1792ins_compl_init_get_longest(void)
1793{
1794 compl_get_longest = FALSE;
1795}
1796
1797/*
1798 * Returns TRUE when insert completion is interrupted.
1799 */
1800 int
1801ins_compl_interrupted(void)
1802{
1803 return compl_interrupted;
1804}
1805
1806/*
1807 * Returns TRUE if the <Enter> key selects a match in the completion popup
1808 * menu.
1809 */
1810 int
1811ins_compl_enter_selects(void)
1812{
1813 return compl_enter_selects;
1814}
1815
1816/*
1817 * Return the column where the text starts that is being completed
1818 */
1819 colnr_T
1820ins_compl_col(void)
1821{
1822 return compl_col;
1823}
1824
1825/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001826 * Return the length in bytes of the text being completed
1827 */
1828 int
1829ins_compl_len(void)
1830{
1831 return compl_length;
1832}
1833
1834/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001835 * Delete one character before the cursor and show the subset of the matches
1836 * that match the word that is now before the cursor.
1837 * Returns the character to be used, NUL if the work is done and another char
1838 * to be got from the user.
1839 */
1840 int
1841ins_compl_bs(void)
1842{
1843 char_u *line;
1844 char_u *p;
1845
1846 line = ml_get_curline();
1847 p = line + curwin->w_cursor.col;
1848 MB_PTR_BACK(line, p);
1849
1850 // Stop completion when the whole word was deleted. For Omni completion
1851 // allow the word to be deleted, we won't match everything.
1852 // Respect the 'backspace' option.
1853 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001854 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1855 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001856 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1857 - compl_length < 0))
1858 return K_BS;
1859
1860 // Deleted more than what was used to find matches or didn't finish
1861 // finding all matches: need to look for matches all over again.
1862 if (curwin->w_cursor.col <= compl_col + compl_length
1863 || ins_compl_need_restart())
1864 ins_compl_restart();
1865
1866 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001867 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001868 if (compl_leader == NULL)
1869 return K_BS;
1870
1871 ins_compl_new_leader();
1872 if (compl_shown_match != NULL)
1873 // Make sure current match is not a hidden item.
1874 compl_curr_match = compl_shown_match;
1875 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001876}
1877
1878/*
1879 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1880 * be called.
1881 */
1882 static int
1883ins_compl_need_restart(void)
1884{
1885 // Return TRUE if we didn't complete finding matches or when the
1886 // 'completefunc' returned "always" in the "refresh" dictionary item.
1887 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001888 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001889 && compl_opt_refresh_always);
1890}
1891
1892/*
1893 * Called after changing "compl_leader".
1894 * Show the popup menu with a different set of matches.
1895 * May also search for matches again if the previous search was interrupted.
1896 */
1897 static void
1898ins_compl_new_leader(void)
1899{
1900 ins_compl_del_pum();
1901 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001902 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001903 compl_used_match = FALSE;
1904
1905 if (compl_started)
1906 ins_compl_set_original_text(compl_leader);
1907 else
1908 {
1909#ifdef FEAT_SPELL
1910 spell_bad_len = 0; // need to redetect bad word
1911#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001912 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001913 // the popup menu display the changed text before the cursor. Set
1914 // "compl_restarting" to avoid that the first match is inserted.
1915 pum_call_update_screen();
1916#ifdef FEAT_GUI
1917 if (gui.in_use)
1918 {
1919 // Show the cursor after the match, not after the redrawn text.
1920 setcursor();
1921 out_flush_cursor(FALSE, FALSE);
1922 }
1923#endif
1924 compl_restarting = TRUE;
1925 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1926 compl_cont_status = 0;
1927 compl_restarting = FALSE;
1928 }
1929
1930 compl_enter_selects = !compl_used_match;
1931
1932 // Show the popup menu with a different set of matches.
1933 ins_compl_show_pum();
1934
1935 // Don't let Enter select the original text when there is no popup menu.
1936 if (compl_match_array == NULL)
1937 compl_enter_selects = FALSE;
1938}
1939
1940/*
1941 * Return the length of the completion, from the completion start column to
1942 * the cursor column. Making sure it never goes below zero.
1943 */
1944 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001945get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001946{
1947 int off = (int)curwin->w_cursor.col - (int)compl_col;
1948
1949 if (off < 0)
1950 return 0;
1951 return off;
1952}
1953
1954/*
1955 * Append one character to the match leader. May reduce the number of
1956 * matches.
1957 */
1958 void
1959ins_compl_addleader(int c)
1960{
1961 int cc;
1962
1963 if (stop_arrow() == FAIL)
1964 return;
1965 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1966 {
1967 char_u buf[MB_MAXBYTES + 1];
1968
1969 (*mb_char2bytes)(c, buf);
1970 buf[cc] = NUL;
1971 ins_char_bytes(buf, cc);
1972 if (compl_opt_refresh_always)
1973 AppendToRedobuff(buf);
1974 }
1975 else
1976 {
1977 ins_char(c);
1978 if (compl_opt_refresh_always)
1979 AppendCharToRedobuff(c);
1980 }
1981
1982 // If we didn't complete finding matches we must search again.
1983 if (ins_compl_need_restart())
1984 ins_compl_restart();
1985
1986 // When 'always' is set, don't reset compl_leader. While completing,
1987 // cursor doesn't point original position, changing compl_leader would
1988 // break redo.
1989 if (!compl_opt_refresh_always)
1990 {
1991 vim_free(compl_leader);
1992 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001993 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001994 if (compl_leader != NULL)
1995 ins_compl_new_leader();
1996 }
1997}
1998
1999/*
2000 * Setup for finding completions again without leaving CTRL-X mode. Used when
2001 * BS or a key was typed while still searching for matches.
2002 */
2003 static void
2004ins_compl_restart(void)
2005{
2006 ins_compl_free();
2007 compl_started = FALSE;
2008 compl_matches = 0;
2009 compl_cont_status = 0;
2010 compl_cont_mode = 0;
2011}
2012
2013/*
2014 * Set the first match, the original text.
2015 */
2016 static void
2017ins_compl_set_original_text(char_u *str)
2018{
2019 char_u *p;
2020
2021 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002022 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2023 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002024 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002025 {
2026 p = vim_strsave(str);
2027 if (p != NULL)
2028 {
2029 vim_free(compl_first_match->cp_str);
2030 compl_first_match->cp_str = p;
2031 }
2032 }
2033 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002034 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002035 {
2036 p = vim_strsave(str);
2037 if (p != NULL)
2038 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002039 vim_free(compl_first_match->cp_prev->cp_str);
2040 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002041 }
2042 }
2043}
2044
2045/*
2046 * Append one character to the match leader. May reduce the number of
2047 * matches.
2048 */
2049 void
2050ins_compl_addfrommatch(void)
2051{
2052 char_u *p;
2053 int len = (int)curwin->w_cursor.col - (int)compl_col;
2054 int c;
2055 compl_T *cp;
2056
2057 p = compl_shown_match->cp_str;
2058 if ((int)STRLEN(p) <= len) // the match is too short
2059 {
2060 // When still at the original match use the first entry that matches
2061 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002062 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002063 return;
2064
2065 p = NULL;
2066 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002067 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002068 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002069 if (compl_leader == NULL
2070 || ins_compl_equal(cp, compl_leader,
2071 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002072 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002073 p = cp->cp_str;
2074 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002075 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002076 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002077 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002078 return;
2079 }
2080 p += len;
2081 c = PTR2CHAR(p);
2082 ins_compl_addleader(c);
2083}
2084
2085/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002086 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002087 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2088 * compl_cont_mode and compl_cont_status.
2089 * Returns TRUE when the character is not to be inserted.
2090 */
2091 static int
2092set_ctrl_x_mode(int c)
2093{
2094 int retval = FALSE;
2095
2096 switch (c)
2097 {
2098 case Ctrl_E:
2099 case Ctrl_Y:
2100 // scroll the window one line up or down
2101 ctrl_x_mode = CTRL_X_SCROLL;
2102 if (!(State & REPLACE_FLAG))
2103 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2104 else
2105 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2106 edit_submode_pre = NULL;
2107 showmode();
2108 break;
2109 case Ctrl_L:
2110 // complete whole line
2111 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2112 break;
2113 case Ctrl_F:
2114 // complete filenames
2115 ctrl_x_mode = CTRL_X_FILES;
2116 break;
2117 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002118 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002119 ctrl_x_mode = CTRL_X_DICTIONARY;
2120 break;
2121 case Ctrl_R:
2122 // Register insertion without exiting CTRL-X mode
2123 // Simply allow ^R to happen without affecting ^X mode
2124 break;
2125 case Ctrl_T:
2126 // complete words from a thesaurus
2127 ctrl_x_mode = CTRL_X_THESAURUS;
2128 break;
2129#ifdef FEAT_COMPL_FUNC
2130 case Ctrl_U:
2131 // user defined completion
2132 ctrl_x_mode = CTRL_X_FUNCTION;
2133 break;
2134 case Ctrl_O:
2135 // omni completion
2136 ctrl_x_mode = CTRL_X_OMNI;
2137 break;
2138#endif
2139 case 's':
2140 case Ctrl_S:
2141 // complete spelling suggestions
2142 ctrl_x_mode = CTRL_X_SPELL;
2143#ifdef FEAT_SPELL
2144 ++emsg_off; // Avoid getting the E756 error twice.
2145 spell_back_to_badword();
2146 --emsg_off;
2147#endif
2148 break;
2149 case Ctrl_RSB:
2150 // complete tag names
2151 ctrl_x_mode = CTRL_X_TAGS;
2152 break;
2153#ifdef FEAT_FIND_ID
2154 case Ctrl_I:
2155 case K_S_TAB:
2156 // complete keywords from included files
2157 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2158 break;
2159 case Ctrl_D:
2160 // complete definitions from included files
2161 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2162 break;
2163#endif
2164 case Ctrl_V:
2165 case Ctrl_Q:
2166 // complete vim commands
2167 ctrl_x_mode = CTRL_X_CMDLINE;
2168 break;
2169 case Ctrl_Z:
2170 // stop completion
2171 ctrl_x_mode = CTRL_X_NORMAL;
2172 edit_submode = NULL;
2173 showmode();
2174 retval = TRUE;
2175 break;
2176 case Ctrl_P:
2177 case Ctrl_N:
2178 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2179 // just started ^X mode, or there were enough ^X's to cancel
2180 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2181 // do normal expansion when interrupting a different mode (say
2182 // ^X^F^X^P or ^P^X^X^P, see below)
2183 // nothing changes if interrupting mode 0, (eg, the flag
2184 // doesn't change when going to ADDING mode -- Acevedo
2185 if (!(compl_cont_status & CONT_INTRPT))
2186 compl_cont_status |= CONT_LOCAL;
2187 else if (compl_cont_mode != 0)
2188 compl_cont_status &= ~CONT_LOCAL;
2189 // FALLTHROUGH
2190 default:
2191 // If we have typed at least 2 ^X's... for modes != 0, we set
2192 // compl_cont_status = 0 (eg, as if we had just started ^X
2193 // mode).
2194 // For mode 0, we set "compl_cont_mode" to an impossible
2195 // value, in both cases ^X^X can be used to restart the same
2196 // mode (avoiding ADDING mode).
2197 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2198 // 'complete' and local ^P expansions respectively.
2199 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2200 // mode -- Acevedo
2201 if (c == Ctrl_X)
2202 {
2203 if (compl_cont_mode != 0)
2204 compl_cont_status = 0;
2205 else
2206 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2207 }
2208 ctrl_x_mode = CTRL_X_NORMAL;
2209 edit_submode = NULL;
2210 showmode();
2211 break;
2212 }
2213
2214 return retval;
2215}
2216
2217/*
2218 * Stop insert completion mode
2219 */
2220 static int
2221ins_compl_stop(int c, int prev_mode, int retval)
2222{
2223 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002224 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002225
2226 // Get here when we have finished typing a sequence of ^N and
2227 // ^P or other completion characters in CTRL-X mode. Free up
2228 // memory that was used, and make sure we can redo the insert.
2229 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2230 {
2231 // If any of the original typed text has been changed, eg when
2232 // ignorecase is set, we must add back-spaces to the redo
2233 // buffer. We add as few as necessary to delete just the part
2234 // of the original text that has changed.
2235 // When using the longest match, edited the match or used
2236 // CTRL-E then don't use the current match.
2237 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2238 ptr = compl_curr_match->cp_str;
2239 else
2240 ptr = NULL;
2241 ins_compl_fixRedoBufForLeader(ptr);
2242 }
2243
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002244 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002245
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002246 // When completing whole lines: fix indent for 'cindent'.
2247 // Otherwise, break line if it's too long.
2248 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2249 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002250 // re-indent the current line
2251 if (want_cindent)
2252 {
2253 do_c_expr_indent();
2254 want_cindent = FALSE; // don't do it again
2255 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002256 }
2257 else
2258 {
2259 int prev_col = curwin->w_cursor.col;
2260
2261 // put the cursor on the last char, for 'tw' formatting
2262 if (prev_col > 0)
2263 dec_cursor();
2264 // only format when something was inserted
2265 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2266 insertchar(NUL, 0, -1);
2267 if (prev_col > 0
2268 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2269 inc_cursor();
2270 }
2271
2272 // If the popup menu is displayed pressing CTRL-Y means accepting
2273 // the selection without inserting anything. When
2274 // compl_enter_selects is set the Enter key does the same.
2275 if ((c == Ctrl_Y || (compl_enter_selects
2276 && (c == CAR || c == K_KENTER || c == NL)))
2277 && pum_visible())
2278 retval = TRUE;
2279
2280 // CTRL-E means completion is Ended, go back to the typed text.
2281 // but only do this, if the Popup is still visible
2282 if (c == Ctrl_E)
2283 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002284 char_u *p = NULL;
2285
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002286 ins_compl_delete();
2287 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002288 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002289 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002290 p = compl_orig_text;
2291 if (p != NULL)
2292 {
2293 int compl_len = get_compl_len();
2294 int len = (int)STRLEN(p);
2295
2296 if (len > compl_len)
2297 ins_bytes_len(p + compl_len, len - compl_len);
2298 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002299 retval = TRUE;
2300 }
2301
2302 auto_format(FALSE, TRUE);
2303
2304 // Trigger the CompleteDonePre event to give scripts a chance to
2305 // act upon the completion before clearing the info, and restore
2306 // ctrl_x_mode, so that complete_info() can be used.
2307 ctrl_x_mode = prev_mode;
2308 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2309
2310 ins_compl_free();
2311 compl_started = FALSE;
2312 compl_matches = 0;
2313 if (!shortmess(SHM_COMPLETIONMENU))
2314 msg_clr_cmdline(); // necessary for "noshowmode"
2315 ctrl_x_mode = CTRL_X_NORMAL;
2316 compl_enter_selects = FALSE;
2317 if (edit_submode != NULL)
2318 {
2319 edit_submode = NULL;
2320 showmode();
2321 }
2322
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002323 if (c == Ctrl_C && cmdwin_type != 0)
2324 // Avoid the popup menu remains displayed when leaving the
2325 // command line window.
2326 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002327 // Indent now if a key was typed that is in 'cinkeys'.
2328 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2329 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002330 // Trigger the CompleteDone event to give scripts a chance to act
2331 // upon the end of completion.
2332 ins_apply_autocmds(EVENT_COMPLETEDONE);
2333
2334 return retval;
2335}
2336
2337/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002338 * Prepare for Insert mode completion, or stop it.
2339 * Called just after typing a character in Insert mode.
2340 * Returns TRUE when the character is not to be inserted;
2341 */
2342 int
2343ins_compl_prep(int c)
2344{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002345 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002346 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002347
2348 // Forget any previous 'special' messages if this is actually
2349 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2350 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2351 edit_submode_extra = NULL;
2352
zeertzjq440d4cb2023-03-02 17:51:32 +00002353 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002354 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002355 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002356 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002357 return retval;
2358
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002359#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002360 // Ignore mouse events in a popup window
2361 if (is_mouse_key(c))
2362 {
2363 // Ignore drag and release events, the position does not need to be in
2364 // the popup and it may have just closed.
2365 if (c == K_LEFTRELEASE
2366 || c == K_LEFTRELEASE_NM
2367 || c == K_MIDDLERELEASE
2368 || c == K_RIGHTRELEASE
2369 || c == K_X1RELEASE
2370 || c == K_X2RELEASE
2371 || c == K_LEFTDRAG
2372 || c == K_MIDDLEDRAG
2373 || c == K_RIGHTDRAG
2374 || c == K_X1DRAG
2375 || c == K_X2DRAG)
2376 return retval;
2377 if (popup_visible)
2378 {
2379 int row = mouse_row;
2380 int col = mouse_col;
2381 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2382
2383 if (wp != NULL && WIN_IS_POPUP(wp))
2384 return retval;
2385 }
2386 }
2387#endif
2388
zeertzjqdca29d92021-08-31 19:12:51 +02002389 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2390 {
2391 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2392 || !vim_is_ctrl_x_key(c))
2393 {
2394 // Not starting another completion mode.
2395 ctrl_x_mode = CTRL_X_CMDLINE;
2396
2397 // CTRL-X CTRL-Z should stop completion without inserting anything
2398 if (c == Ctrl_Z)
2399 retval = TRUE;
2400 }
2401 else
2402 {
2403 ctrl_x_mode = CTRL_X_CMDLINE;
2404
2405 // Other CTRL-X keys first stop completion, then start another
2406 // completion mode.
2407 ins_compl_prep(' ');
2408 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2409 }
2410 }
2411
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002412 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002413 if (ctrl_x_mode_not_defined_yet()
2414 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002415 {
bfredl87af60c2022-09-24 11:17:51 +01002416 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002417 compl_used_match = TRUE;
2418
2419 }
2420
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002421 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002422 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2423 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002424 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002425 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002426 {
2427 // We're already in CTRL-X mode, do we stay in it?
2428 if (!vim_is_ctrl_x_key(c))
2429 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002430 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002431 ctrl_x_mode = CTRL_X_NORMAL;
2432 else
2433 ctrl_x_mode = CTRL_X_FINISHED;
2434 edit_submode = NULL;
2435 }
2436 showmode();
2437 }
2438
2439 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2440 {
2441 // Show error message from attempted keyword completion (probably
2442 // 'Pattern not found') until another key is hit, then go back to
2443 // showing what mode we are in.
2444 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002445 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002446 && c != Ctrl_R && !ins_compl_pum_key(c))
2447 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002448 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002449 }
2450 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2451 // Trigger the CompleteDone event to give scripts a chance to act
2452 // upon the (possibly failed) completion.
2453 ins_apply_autocmds(EVENT_COMPLETEDONE);
2454
LemonBoy2bf52dd2022-04-09 18:17:34 +01002455 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002456
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002457 // reset continue_* if we left expansion-mode, if we stay they'll be
2458 // (re)set properly in ins_complete()
2459 if (!vim_is_ctrl_x_key(c))
2460 {
2461 compl_cont_status = 0;
2462 compl_cont_mode = 0;
2463 }
2464
2465 return retval;
2466}
2467
2468/*
2469 * Fix the redo buffer for the completion leader replacing some of the typed
2470 * text. This inserts backspaces and appends the changed text.
2471 * "ptr" is the known leader text or NUL.
2472 */
2473 static void
2474ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2475{
2476 int len;
2477 char_u *p;
2478 char_u *ptr = ptr_arg;
2479
2480 if (ptr == NULL)
2481 {
2482 if (compl_leader != NULL)
2483 ptr = compl_leader;
2484 else
2485 return; // nothing to do
2486 }
2487 if (compl_orig_text != NULL)
2488 {
2489 p = compl_orig_text;
2490 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2491 ;
2492 if (len > 0)
2493 len -= (*mb_head_off)(p, p + len);
2494 for (p += len; *p != NUL; MB_PTR_ADV(p))
2495 AppendCharToRedobuff(K_BS);
2496 }
2497 else
2498 len = 0;
2499 if (ptr != NULL)
2500 AppendToRedobuffLit(ptr + len, -1);
2501}
2502
2503/*
2504 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2505 * (depending on flag) starting from buf and looking for a non-scanned
2506 * buffer (other than curbuf). curbuf is special, if it is called with
2507 * buf=curbuf then it has to be the first call for a given flag/expansion.
2508 *
2509 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2510 */
2511 static buf_T *
2512ins_compl_next_buf(buf_T *buf, int flag)
2513{
2514 static win_T *wp = NULL;
2515
2516 if (flag == 'w') // just windows
2517 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002518 if (buf == curbuf || !win_valid(wp))
2519 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002520 wp = curwin;
2521 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2522 && wp->w_buffer->b_scanned)
2523 ;
2524 buf = wp->w_buffer;
2525 }
2526 else
2527 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2528 // (unlisted buffers)
2529 // When completing whole lines skip unloaded buffers.
2530 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2531 && ((flag == 'U'
2532 ? buf->b_p_bl
2533 : (!buf->b_p_bl
2534 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2535 || buf->b_scanned))
2536 ;
2537 return buf;
2538}
2539
2540#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002541
2542# ifdef FEAT_EVAL
2543static callback_T cfu_cb; // 'completefunc' callback function
2544static callback_T ofu_cb; // 'omnifunc' callback function
2545static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2546# endif
2547
2548/*
2549 * Copy a global callback function to a buffer local callback.
2550 */
2551 static void
2552copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2553{
2554 free_callback(bufcb);
2555 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2556 copy_callback(bufcb, globcb);
2557}
2558
2559/*
2560 * Parse the 'completefunc' option value and set the callback function.
2561 * Invoked when the 'completefunc' option is set. The option value can be a
2562 * name of a function (string), or function(<name>) or funcref(<name>) or a
2563 * lambda expression.
2564 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002566did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002567{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002568 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2569 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002570
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002571 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002572
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002573 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002574}
2575
2576/*
2577 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002578 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002579 */
2580 void
2581set_buflocal_cfu_callback(buf_T *buf UNUSED)
2582{
2583# ifdef FEAT_EVAL
2584 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2585# endif
2586}
2587
2588/*
2589 * Parse the 'omnifunc' option value and set the callback function.
2590 * Invoked when the 'omnifunc' option is set. The option value can be a
2591 * name of a function (string), or function(<name>) or funcref(<name>) or a
2592 * lambda expression.
2593 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002594 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002595did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002596{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002597 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2598 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002599
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002600 set_buflocal_ofu_callback(curbuf);
2601 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002602}
2603
2604/*
2605 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002606 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002607 */
2608 void
2609set_buflocal_ofu_callback(buf_T *buf UNUSED)
2610{
2611# ifdef FEAT_EVAL
2612 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2613# endif
2614}
2615
2616/*
2617 * Parse the 'thesaurusfunc' option value and set the callback function.
2618 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2619 * name of a function (string), or function(<name>) or funcref(<name>) or a
2620 * lambda expression.
2621 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002622 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002623did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002624{
2625 int retval;
2626
2627 if (*curbuf->b_p_tsrfu != NUL)
2628 {
2629 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002630 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2631 &curbuf->b_tsrfu_cb);
2632 }
2633 else
2634 {
2635 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002636 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2637 }
2638
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002639 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002640}
2641
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002642/*
2643 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002644 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002645 */
2646 int
2647set_ref_in_insexpand_funcs(int copyID)
2648{
2649 int abort = FALSE;
2650
2651 abort = set_ref_in_callback(&cfu_cb, copyID);
2652 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2653 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2654
2655 return abort;
2656}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002657
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002658/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002659 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002660 */
2661 static char_u *
2662get_complete_funcname(int type)
2663{
2664 switch (type)
2665 {
2666 case CTRL_X_FUNCTION:
2667 return curbuf->b_p_cfu;
2668 case CTRL_X_OMNI:
2669 return curbuf->b_p_ofu;
2670 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002671 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002672 default:
2673 return (char_u *)"";
2674 }
2675}
2676
2677/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002678 * Get the callback to use for insert mode completion.
2679 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002680 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002681get_insert_callback(int type)
2682{
2683 if (type == CTRL_X_FUNCTION)
2684 return &curbuf->b_cfu_cb;
2685 if (type == CTRL_X_OMNI)
2686 return &curbuf->b_ofu_cb;
2687 // CTRL_X_THESAURUS
2688 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2689}
2690
2691/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002692 * Execute user defined complete function 'completefunc', 'omnifunc' or
2693 * 'thesaurusfunc', and get matches in "matches".
2694 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002695 */
2696 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002697expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002698{
2699 list_T *matchlist = NULL;
2700 dict_T *matchdict = NULL;
2701 typval_T args[3];
2702 char_u *funcname;
2703 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002704 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002705 typval_T rettv;
2706 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002707 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002708
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002709 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002710 if (*funcname == NUL)
2711 return;
2712
2713 // Call 'completefunc' to obtain the list of matches.
2714 args[0].v_type = VAR_NUMBER;
2715 args[0].vval.v_number = 0;
2716 args[1].v_type = VAR_STRING;
2717 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2718 args[2].v_type = VAR_UNKNOWN;
2719
2720 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002721 // Lock the text to avoid weird things from happening. Also disallow
2722 // switching to another window, it should not be needed and may end up in
2723 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002724 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002725
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002726 cb = get_insert_callback(type);
2727 retval = call_callback(cb, 0, &rettv, 2, args);
2728
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002729 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002730 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002731 {
2732 switch (rettv.v_type)
2733 {
2734 case VAR_LIST:
2735 matchlist = rettv.vval.v_list;
2736 break;
2737 case VAR_DICT:
2738 matchdict = rettv.vval.v_dict;
2739 break;
2740 case VAR_SPECIAL:
2741 if (rettv.vval.v_number == VVAL_NONE)
2742 compl_opt_suppress_empty = TRUE;
2743 // FALLTHROUGH
2744 default:
2745 // TODO: Give error message?
2746 clear_tv(&rettv);
2747 break;
2748 }
2749 }
zeertzjqcfe45652022-05-27 17:26:55 +01002750 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002751
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002752 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02002753 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002754 validate_cursor();
2755 if (!EQUAL_POS(curwin->w_cursor, pos))
2756 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002757 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758 goto theend;
2759 }
2760
2761 if (matchlist != NULL)
2762 ins_compl_add_list(matchlist);
2763 else if (matchdict != NULL)
2764 ins_compl_add_dict(matchdict);
2765
2766theend:
2767 // Restore State, it might have been changed.
2768 State = save_State;
2769
2770 if (matchdict != NULL)
2771 dict_unref(matchdict);
2772 if (matchlist != NULL)
2773 list_unref(matchlist);
2774}
2775#endif // FEAT_COMPL_FUNC
2776
2777#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2778/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002779 * Add a match to the list of matches from a typeval_T.
2780 * If the given string is already in the list of completions, then return
2781 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2782 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002783 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002784 */
2785 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002786ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002787{
2788 char_u *word;
2789 int dup = FALSE;
2790 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002791 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002792 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002793 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002794 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002795
Bram Moolenaar08928322020-01-04 14:32:48 +01002796 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002797 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2798 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002799 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2800 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2801 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2802 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2803 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2804 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2805 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2806 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002807 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002808 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2809 dup = dict_get_number(tv->vval.v_dict, "dup");
2810 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2811 empty = dict_get_number(tv->vval.v_dict, "empty");
2812 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2813 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002814 flags |= CP_EQUAL;
2815 }
2816 else
2817 {
2818 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002819 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002820 }
2821 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002822 {
2823 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002824 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002825 }
2826 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2827 if (status != OK)
2828 clear_tv(&user_data);
2829 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002830}
2831
2832/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002833 * Add completions from a list.
2834 */
2835 static void
2836ins_compl_add_list(list_T *list)
2837{
2838 listitem_T *li;
2839 int dir = compl_direction;
2840
2841 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002842 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002843 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002844 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002845 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002846 // if dir was BACKWARD then honor it just once
2847 dir = FORWARD;
2848 else if (did_emsg)
2849 break;
2850 }
2851}
2852
2853/*
2854 * Add completions from a dict.
2855 */
2856 static void
2857ins_compl_add_dict(dict_T *dict)
2858{
2859 dictitem_T *di_refresh;
2860 dictitem_T *di_words;
2861
2862 // Check for optional "refresh" item.
2863 compl_opt_refresh_always = FALSE;
2864 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2865 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2866 {
2867 char_u *v = di_refresh->di_tv.vval.v_string;
2868
2869 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2870 compl_opt_refresh_always = TRUE;
2871 }
2872
2873 // Add completions from a "words" list.
2874 di_words = dict_find(dict, (char_u *)"words", 5);
2875 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2876 ins_compl_add_list(di_words->di_tv.vval.v_list);
2877}
2878
2879/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002880 * Start completion for the complete() function.
2881 * "startcol" is where the matched text starts (1 is first column).
2882 * "list" is the list of matches.
2883 */
2884 static void
2885set_completion(colnr_T startcol, list_T *list)
2886{
2887 int save_w_wrow = curwin->w_wrow;
2888 int save_w_leftcol = curwin->w_leftcol;
2889 int flags = CP_ORIGINAL_TEXT;
2890
2891 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002892 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002893 ins_compl_prep(' ');
2894 ins_compl_clear();
2895 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002896 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002897
2898 compl_direction = FORWARD;
2899 if (startcol > curwin->w_cursor.col)
2900 startcol = curwin->w_cursor.col;
2901 compl_col = startcol;
2902 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2903 // compl_pattern doesn't need to be set
2904 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2905 if (p_ic)
2906 flags |= CP_ICASE;
2907 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002908 -1, NULL, NULL, NULL, 0,
2909 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002910 return;
2911
2912 ctrl_x_mode = CTRL_X_EVAL;
2913
2914 ins_compl_add_list(list);
2915 compl_matches = ins_compl_make_cyclic();
2916 compl_started = TRUE;
2917 compl_used_match = TRUE;
2918 compl_cont_status = 0;
2919
2920 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002921 int no_select = compl_no_select || compl_longest;
2922 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002923 {
2924 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002925 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002926 // Down/Up has no real effect.
2927 ins_complete(K_UP, FALSE);
2928 }
2929 else
2930 ins_complete(Ctrl_N, FALSE);
2931 compl_enter_selects = compl_no_insert;
2932
2933 // Lazily show the popup menu, unless we got interrupted.
2934 if (!compl_interrupted)
2935 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002936 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002937 out_flush();
2938}
2939
2940/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002941 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002942 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002943 void
2944f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002945{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002946 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002947
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002948 if (in_vim9script()
2949 && (check_for_number_arg(argvars, 0) == FAIL
2950 || check_for_list_arg(argvars, 1) == FAIL))
2951 return;
2952
Bram Moolenaar24959102022-05-07 20:01:16 +01002953 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002954 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002955 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002956 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002957 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002958
2959 // Check for undo allowed here, because if something was already inserted
2960 // the line was already saved for undo and this check isn't done.
2961 if (!undo_allowed())
2962 return;
2963
Bram Moolenaard83392a2022-09-01 12:22:46 +01002964 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002965 {
2966 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2967 if (startcol > 0)
2968 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002969 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002970}
2971
2972/*
2973 * "complete_add()" function
2974 */
2975 void
2976f_complete_add(typval_T *argvars, typval_T *rettv)
2977{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002978 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002979 return;
2980
Bram Moolenaar440cf092021-04-03 20:13:30 +02002981 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002982}
2983
2984/*
2985 * "complete_check()" function
2986 */
2987 void
2988f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2989{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002990 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002991 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002992
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002993 ins_compl_check_keys(0, TRUE);
2994 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002995
2996 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002997}
2998
2999/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003000 * Return Insert completion mode name string
3001 */
3002 static char_u *
3003ins_compl_mode(void)
3004{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003005 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003006 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3007
3008 return (char_u *)"";
3009}
3010
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003011/*
3012 * Assign the sequence number to all the completion matches which don't have
3013 * one assigned yet.
3014 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003015 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003016ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003017{
3018 int number = 0;
3019 compl_T *match;
3020
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003021 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003022 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003023 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003024 // This should normally succeed already at the first loop
3025 // cycle, so it's fast!
3026 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003027 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003028 if (match->cp_number != -1)
3029 {
3030 number = match->cp_number;
3031 break;
3032 }
3033 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003034 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003035 for (match = match->cp_next;
3036 match != NULL && match->cp_number == -1;
3037 match = match->cp_next)
3038 match->cp_number = ++number;
3039 }
3040 else // BACKWARD
3041 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003042 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003043 // number. This should normally succeed already at the
3044 // first loop cycle, so it's fast!
3045 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003046 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003047 if (match->cp_number != -1)
3048 {
3049 number = match->cp_number;
3050 break;
3051 }
3052 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003053 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003054 for (match = match->cp_prev; match
3055 && match->cp_number == -1;
3056 match = match->cp_prev)
3057 match->cp_number = ++number;
3058 }
3059}
3060
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003061/*
3062 * Get complete information
3063 */
3064 static void
3065get_complete_info(list_T *what_list, dict_T *retdict)
3066{
3067 int ret = OK;
3068 listitem_T *item;
3069#define CI_WHAT_MODE 0x01
3070#define CI_WHAT_PUM_VISIBLE 0x02
3071#define CI_WHAT_ITEMS 0x04
3072#define CI_WHAT_SELECTED 0x08
3073#define CI_WHAT_INSERTED 0x10
3074#define CI_WHAT_ALL 0xff
3075 int what_flag;
3076
3077 if (what_list == NULL)
3078 what_flag = CI_WHAT_ALL;
3079 else
3080 {
3081 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003082 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003083 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003084 {
3085 char_u *what = tv_get_string(&item->li_tv);
3086
3087 if (STRCMP(what, "mode") == 0)
3088 what_flag |= CI_WHAT_MODE;
3089 else if (STRCMP(what, "pum_visible") == 0)
3090 what_flag |= CI_WHAT_PUM_VISIBLE;
3091 else if (STRCMP(what, "items") == 0)
3092 what_flag |= CI_WHAT_ITEMS;
3093 else if (STRCMP(what, "selected") == 0)
3094 what_flag |= CI_WHAT_SELECTED;
3095 else if (STRCMP(what, "inserted") == 0)
3096 what_flag |= CI_WHAT_INSERTED;
3097 }
3098 }
3099
3100 if (ret == OK && (what_flag & CI_WHAT_MODE))
3101 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3102
3103 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3104 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3105
Girish Palya8950bf72024-03-20 20:07:29 +01003106 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003107 {
Christian Brabandt9eb236f2024-03-21 20:12:59 +01003108 list_T *li = NULL;
Girish Palya8950bf72024-03-20 20:07:29 +01003109 dict_T *di;
3110 compl_T *match;
3111 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003112
Girish Palya8950bf72024-03-20 20:07:29 +01003113 if (what_flag & CI_WHAT_ITEMS)
3114 {
3115 li = list_alloc();
3116 if (li == NULL)
3117 return;
3118 ret = dict_add_list(retdict, "items", li);
3119 }
3120 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3121 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3122 ins_compl_update_sequence_numbers();
3123 if (ret == OK && compl_first_match != NULL)
3124 {
3125 int list_idx = 0;
3126 match = compl_first_match;
3127 do
3128 {
3129 if (!match_at_original_text(match))
3130 {
3131 if (what_flag & CI_WHAT_ITEMS)
3132 {
3133 di = dict_alloc();
3134 if (di == NULL)
3135 return;
3136 ret = list_append_dict(li, di);
3137 if (ret != OK)
3138 return;
3139 dict_add_string(di, "word", match->cp_str);
3140 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3141 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3142 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3143 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3144 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3145 // Add an empty string for backwards compatibility
3146 dict_add_string(di, "user_data", (char_u *)"");
3147 else
3148 dict_add_tv(di, "user_data", &match->cp_user_data);
3149 }
3150 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3151 selected_idx = list_idx;
3152 list_idx += 1;
3153 }
3154 match = match->cp_next;
3155 }
3156 while (match != NULL && !is_first_match(match));
3157 }
3158 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3159 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003160 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003161
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003162 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3163 {
3164 // TODO
3165 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003166}
3167
3168/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003169 * "complete_info()" function
3170 */
3171 void
3172f_complete_info(typval_T *argvars, typval_T *rettv)
3173{
3174 list_T *what_list = NULL;
3175
Bram Moolenaar93a10962022-06-16 11:42:09 +01003176 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003177 return;
3178
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003179 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3180 return;
3181
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003182 if (argvars[0].v_type != VAR_UNKNOWN)
3183 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003184 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003185 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003186 what_list = argvars[0].vval.v_list;
3187 }
3188 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003189}
3190#endif
3191
3192/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003193 * Returns TRUE when using a user-defined function for thesaurus completion.
3194 */
3195 static int
3196thesaurus_func_complete(int type UNUSED)
3197{
3198#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003199 return type == CTRL_X_THESAURUS
3200 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003201#else
3202 return FALSE;
3203#endif
3204}
3205
3206/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003207 * Return value of process_next_cpt_value()
3208 */
3209enum
3210{
3211 INS_COMPL_CPT_OK = 1,
3212 INS_COMPL_CPT_CONT,
3213 INS_COMPL_CPT_END
3214};
3215
3216/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003217 * state information used for getting the next set of insert completion
3218 * matches.
3219 */
3220typedef struct
3221{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003222 char_u *e_cpt_copy; // copy of 'complete'
3223 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003224 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003225 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003226 pos_T prev_match_pos; // previous match position
3227 int set_match_pos; // save first_match_pos/last_match_pos
3228 pos_T first_match_pos; // first match position
3229 pos_T last_match_pos; // last match position
3230 int found_all; // found all matches of a certain type.
3231 char_u *dict; // dictionary file to search
3232 int dict_f; // "dict" is an exact file name or not
3233} ins_compl_next_state_T;
3234
3235/*
3236 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003237 *
3238 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003239 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003240 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003241 * st->found_all - all matches of this type are found
3242 * st->ins_buf - search for completions in this buffer
3243 * st->first_match_pos - position of the first completion match
3244 * st->last_match_pos - position of the last completion match
3245 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003246 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 * st->dict - name of the dictionary or thesaurus file to search
3248 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 *
3250 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003251 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3252 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003253 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003254 */
3255 static int
3256process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003257 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003258 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003259 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003260{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003261 int compl_type = -1;
3262 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003264 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003265
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003266 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3267 st->e_cpt++;
3268
3269 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003270 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003271 st->ins_buf = curbuf;
3272 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003273 // Move the cursor back one character so that ^N can match the
3274 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003275 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003276 {
3277 // Move the cursor to after the last character in the
3278 // buffer, so that word at start of buffer is found
3279 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003280 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003281 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003282 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003283 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003284 compl_type = 0;
3285
3286 // Remember the first match so that the loop stops when we
3287 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003288 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003289 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003290 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003291 && (st->ins_buf = ins_compl_next_buf(
3292 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003293 {
3294 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003295 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003296 {
3297 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003298 st->first_match_pos.col = st->last_match_pos.col = 0;
3299 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3300 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003301 compl_type = 0;
3302 }
3303 else // unloaded buffer, scan like dictionary
3304 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003305 st->found_all = TRUE;
3306 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003307 {
3308 status = INS_COMPL_CPT_CONT;
3309 goto done;
3310 }
3311 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003312 st->dict = st->ins_buf->b_fname;
3313 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003314 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003315 if (!shortmess(SHM_COMPLETIONSCAN))
3316 {
3317 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3318 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3319 st->ins_buf->b_fname == NULL
3320 ? buf_spname(st->ins_buf)
3321 : st->ins_buf->b_sfname == NULL
3322 ? st->ins_buf->b_fname
3323 : st->ins_buf->b_sfname);
3324 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3325 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003327 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003328 status = INS_COMPL_CPT_END;
3329 else
3330 {
3331 if (ctrl_x_mode_line_or_eval())
3332 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003333 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003334 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003335 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003336 compl_type = CTRL_X_DICTIONARY;
3337 else
3338 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003339 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003340 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003341 st->dict = st->e_cpt;
3342 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003343 }
3344 }
3345#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003346 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003347 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003348 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003349 compl_type = CTRL_X_PATH_DEFINES;
3350#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003351 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003352 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003353 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003354 if (!shortmess(SHM_COMPLETIONSCAN))
3355 {
3356 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3357 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3358 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3359 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003360 }
3361 else
3362 compl_type = -1;
3363
3364 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003365 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003366
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003367 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003368 if (compl_type == -1)
3369 status = INS_COMPL_CPT_CONT;
3370 }
3371
3372done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003373 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003374 return status;
3375}
3376
3377#ifdef FEAT_FIND_ID
3378/*
3379 * Get the next set of identifiers or defines matching "compl_pattern" in
3380 * included files.
3381 */
3382 static void
3383get_next_include_file_completion(int compl_type)
3384{
3385 find_pattern_in_path(compl_pattern, compl_direction,
Mike Williams16b63bd2024-06-01 11:33:40 +02003386 (int)compl_patternlen, FALSE, FALSE,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003387 (compl_type == CTRL_X_PATH_DEFINES
3388 && !(compl_cont_status & CONT_SOL))
3389 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003390 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003391}
3392#endif
3393
3394/*
3395 * Get the next set of words matching "compl_pattern" in dictionary or
3396 * thesaurus files.
3397 */
3398 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003399get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003400{
3401#ifdef FEAT_COMPL_FUNC
3402 if (thesaurus_func_complete(compl_type))
3403 expand_by_function(compl_type, compl_pattern);
3404 else
3405#endif
3406 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003407 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003408 : (compl_type == CTRL_X_THESAURUS
3409 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3410 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3411 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003412 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003413 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003414}
3415
3416/*
3417 * Get the next set of tag names matching "compl_pattern".
3418 */
3419 static void
3420get_next_tag_completion(void)
3421{
3422 int save_p_ic;
3423 char_u **matches;
3424 int num_matches;
3425
3426 // set p_ic according to p_ic, p_scs and pat for find_tags().
3427 save_p_ic = p_ic;
3428 p_ic = ignorecase(compl_pattern);
3429
3430 // Find up to TAG_MANY matches. Avoids that an enormous number
3431 // of matches is found when compl_pattern is empty
3432 g_tag_at_cursor = TRUE;
3433 if (find_tags(compl_pattern, &num_matches, &matches,
3434 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003435 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003436 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3437 ins_compl_add_matches(num_matches, matches, p_ic);
3438 g_tag_at_cursor = FALSE;
3439 p_ic = save_p_ic;
3440}
3441
3442/*
3443 * Get the next set of filename matching "compl_pattern".
3444 */
3445 static void
3446get_next_filename_completion(void)
3447{
3448 char_u **matches;
3449 int num_matches;
3450
3451 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3452 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3453 return;
3454
3455 // May change home directory back to "~".
3456 tilde_replace(compl_pattern, num_matches, matches);
3457#ifdef BACKSLASH_IN_FILENAME
3458 if (curbuf->b_p_csl[0] != NUL)
3459 {
3460 int i;
3461
3462 for (i = 0; i < num_matches; ++i)
3463 {
3464 char_u *ptr = matches[i];
3465
3466 while (*ptr != NUL)
3467 {
3468 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3469 *ptr = '/';
3470 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3471 *ptr = '\\';
3472 ptr += (*mb_ptr2len)(ptr);
3473 }
3474 }
3475 }
3476#endif
3477 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3478}
3479
3480/*
3481 * Get the next set of command-line completions matching "compl_pattern".
3482 */
3483 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003484get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003485{
3486 char_u **matches;
3487 int num_matches;
3488
3489 if (expand_cmdline(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02003490 (int)compl_patternlen, &num_matches, &matches) == EXPAND_OK)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003491 ins_compl_add_matches(num_matches, matches, FALSE);
3492}
3493
3494/*
3495 * Get the next set of spell suggestions matching "compl_pattern".
3496 */
3497 static void
3498get_next_spell_completion(linenr_T lnum UNUSED)
3499{
3500#ifdef FEAT_SPELL
3501 char_u **matches;
3502 int num_matches;
3503
3504 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3505 if (num_matches > 0)
3506 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003507 else
3508 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003509#endif
3510}
3511
3512/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003513 * Return the next word or line from buffer "ins_buf" at position
3514 * "cur_match_pos" for completion. The length of the match is set in "len".
3515 */
3516 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003517ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003518 buf_T *ins_buf, // buffer being scanned
3519 pos_T *cur_match_pos, // current match position
3520 int *match_len,
3521 int *cont_s_ipos) // next ^X<> will set initial_pos
3522{
3523 char_u *ptr;
3524 int len;
3525
3526 *match_len = 0;
3527 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3528 cur_match_pos->col;
3529 if (ctrl_x_mode_line_or_eval())
3530 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003531 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003532 {
3533 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3534 return NULL;
3535 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3536 if (!p_paste)
3537 ptr = skipwhite(ptr);
3538 }
3539 len = (int)STRLEN(ptr);
3540 }
3541 else
3542 {
3543 char_u *tmp_ptr = ptr;
3544
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003545 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003546 {
3547 tmp_ptr += compl_length;
3548 // Skip if already inside a word.
3549 if (vim_iswordp(tmp_ptr))
3550 return NULL;
3551 // Find start of next word.
3552 tmp_ptr = find_word_start(tmp_ptr);
3553 }
3554 // Find end of this word.
3555 tmp_ptr = find_word_end(tmp_ptr);
3556 len = (int)(tmp_ptr - ptr);
3557
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003558 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003559 {
3560 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3561 {
3562 // Try next line, if any. the new word will be
3563 // "join" as if the normal command "J" was used.
3564 // IOSIZE is always greater than
3565 // compl_length, so the next STRNCPY always
3566 // works -- Acevedo
3567 STRNCPY(IObuff, ptr, len);
3568 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3569 tmp_ptr = ptr = skipwhite(ptr);
3570 // Find start of next word.
3571 tmp_ptr = find_word_start(tmp_ptr);
3572 // Find end of next word.
3573 tmp_ptr = find_word_end(tmp_ptr);
3574 if (tmp_ptr > ptr)
3575 {
3576 if (*ptr != ')' && IObuff[len - 1] != TAB)
3577 {
3578 if (IObuff[len - 1] != ' ')
3579 IObuff[len++] = ' ';
3580 // IObuf =~ "\k.* ", thus len >= 2
3581 if (p_js
3582 && (IObuff[len - 2] == '.'
3583 || (vim_strchr(p_cpo, CPO_JOINSP)
3584 == NULL
3585 && (IObuff[len - 2] == '?'
3586 || IObuff[len - 2] == '!'))))
3587 IObuff[len++] = ' ';
3588 }
3589 // copy as much as possible of the new word
3590 if (tmp_ptr - ptr >= IOSIZE - len)
3591 tmp_ptr = ptr + IOSIZE - len - 1;
3592 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3593 len += (int)(tmp_ptr - ptr);
3594 *cont_s_ipos = TRUE;
3595 }
3596 IObuff[len] = NUL;
3597 ptr = IObuff;
3598 }
3599 if (len == compl_length)
3600 return NULL;
3601 }
3602 }
3603
3604 *match_len = len;
3605 return ptr;
3606}
3607
3608/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003609 * Get the next set of words matching "compl_pattern" for default completion(s)
3610 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003611 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3612 * position "st->start_pos" in the "compl_direction" direction. If
3613 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3614 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003615 * Returns OK if a new next match is found, otherwise returns FAIL.
3616 */
3617 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003618get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003619{
3620 int found_new_match = FAIL;
3621 int save_p_scs;
3622 int save_p_ws;
3623 int looped_around = FALSE;
3624 char_u *ptr;
3625 int len;
3626
3627 // If 'infercase' is set, don't use 'smartcase' here
3628 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003629 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003630 p_scs = FALSE;
3631
3632 // Buffers other than curbuf are scanned from the beginning or the
3633 // end but never from the middle, thus setting nowrapscan in this
3634 // buffer is a good idea, on the other hand, we always set
3635 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3636 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003637 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003638 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003639 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003640 p_ws = TRUE;
3641 looped_around = FALSE;
3642 for (;;)
3643 {
3644 int cont_s_ipos = FALSE;
3645
3646 ++msg_silent; // Don't want messages for wrapscan.
3647
3648 // ctrl_x_mode_line_or_eval() || word-wise search that
3649 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003650 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003651 found_new_match = search_for_exact_line(st->ins_buf,
3652 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003653 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003654 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003655 NULL, compl_direction, compl_pattern, compl_patternlen,
3656 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003657 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003658 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 {
3660 // set "compl_started" even on fail
3661 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003662 st->first_match_pos = *st->cur_match_pos;
3663 st->last_match_pos = *st->cur_match_pos;
3664 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003665 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003666 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3667 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003668 {
3669 found_new_match = FAIL;
3670 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003671 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003672 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3673 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3674 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003675 {
3676 if (looped_around)
3677 found_new_match = FAIL;
3678 else
3679 looped_around = TRUE;
3680 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003681 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003682 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3683 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3684 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003685 {
3686 if (looped_around)
3687 found_new_match = FAIL;
3688 else
3689 looped_around = TRUE;
3690 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003691 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003692 if (found_new_match == FAIL)
3693 break;
3694
3695 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003696 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003697 && start_pos->lnum == st->cur_match_pos->lnum
3698 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003699 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003700
zeertzjq440d4cb2023-03-02 17:51:32 +00003701 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3702 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003703 if (ptr == NULL)
3704 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003705
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003706 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003707 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003708 0, cont_s_ipos) != NOTDONE)
3709 {
3710 found_new_match = OK;
3711 break;
3712 }
3713 }
3714 p_scs = save_p_scs;
3715 p_ws = save_p_ws;
3716
3717 return found_new_match;
3718}
3719
3720/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003721 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003722 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003723 */
3724 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003725get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003726{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003727 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003728
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003729 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003730 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003731 case -1:
3732 break;
3733#ifdef FEAT_FIND_ID
3734 case CTRL_X_PATH_PATTERNS:
3735 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003736 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003737 break;
3738#endif
3739
3740 case CTRL_X_DICTIONARY:
3741 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003742 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3743 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003744 break;
3745
3746 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003747 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003748 break;
3749
3750 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003751 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003752 break;
3753
3754 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003755 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003756 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003757 break;
3758
3759#ifdef FEAT_COMPL_FUNC
3760 case CTRL_X_FUNCTION:
3761 case CTRL_X_OMNI:
3762 expand_by_function(type, compl_pattern);
3763 break;
3764#endif
3765
3766 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003767 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003768 break;
3769
3770 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003771 found_new_match = get_next_default_completion(st, ini);
3772 if (found_new_match == FAIL && st->ins_buf == curbuf)
3773 st->found_all = TRUE;
3774 }
3775
3776 // check if compl_curr_match has changed, (e.g. other type of
3777 // expansion added something)
3778 if (type != 0 && compl_curr_match != compl_old_match)
3779 found_new_match = OK;
3780
3781 return found_new_match;
3782}
3783
3784/*
3785 * Get the next expansion(s), using "compl_pattern".
3786 * The search starts at position "ini" in curbuf and in the direction
3787 * compl_direction.
3788 * When "compl_started" is FALSE start at that position, otherwise continue
3789 * where we stopped searching before.
3790 * This may return before finding all the matches.
3791 * Return the total number of matches or -1 if still unknown -- Acevedo
3792 */
3793 static int
3794ins_compl_get_exp(pos_T *ini)
3795{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003796 static ins_compl_next_state_T st;
3797 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003798 int i;
3799 int found_new_match;
3800 int type = ctrl_x_mode;
3801
3802 if (!compl_started)
3803 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003804 buf_T *buf;
3805
3806 FOR_ALL_BUFFERS(buf)
3807 buf->b_scanned = 0;
3808 if (!st_cleared)
3809 {
3810 CLEAR_FIELD(st);
3811 st_cleared = TRUE;
3812 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003813 st.found_all = FALSE;
3814 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003815 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003816 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003817 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3818 ? (char_u *)"." : curbuf->b_p_cpt);
3819 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003820 st.last_match_pos = st.first_match_pos = *ini;
3821 }
3822 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3823 st.ins_buf = curbuf; // In case the buffer was wiped out.
3824
3825 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003826 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003827 ? &st.last_match_pos : &st.first_match_pos;
3828
3829 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3830 for (;;)
3831 {
3832 found_new_match = FAIL;
3833 st.set_match_pos = FALSE;
3834
3835 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3836 // or if found_all says this entry is done. For ^X^L only use the
3837 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003838 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003839 && (!compl_started || st.found_all))
3840 {
3841 int status = process_next_cpt_value(&st, &type, ini);
3842
3843 if (status == INS_COMPL_CPT_END)
3844 break;
3845 if (status == INS_COMPL_CPT_CONT)
3846 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003847 }
3848
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003849 // If complete() was called then compl_pattern has been reset. The
3850 // following won't work then, bail out.
3851 if (compl_pattern == NULL)
3852 break;
3853
3854 // get the next set of completion matches
3855 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003856
3857 // break the loop for specialized modes (use 'complete' just for the
3858 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3859 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003860 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3861 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003862 {
3863 if (got_int)
3864 break;
3865 // Fill the popup menu as soon as possible.
3866 if (type != -1)
3867 ins_compl_check_keys(0, FALSE);
3868
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003869 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003870 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3871 break;
3872 compl_started = TRUE;
3873 }
3874 else
3875 {
3876 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003877 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003878 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003879
3880 compl_started = FALSE;
3881 }
3882 }
3883 compl_started = TRUE;
3884
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003885 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003886 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003887 found_new_match = FAIL;
3888
3889 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003890 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003891 && !ctrl_x_mode_line_or_eval()))
3892 i = ins_compl_make_cyclic();
3893
3894 if (compl_old_match != NULL)
3895 {
3896 // If several matches were added (FORWARD) or the search failed and has
3897 // just been made cyclic then we have to move compl_curr_match to the
3898 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003899 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900 : compl_old_match->cp_prev;
3901 if (compl_curr_match == NULL)
3902 compl_curr_match = compl_old_match;
3903 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003904 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003905
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003906 return i;
3907}
3908
3909/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003910 * Update "compl_shown_match" to the actually shown match, it may differ when
3911 * "compl_leader" is used to omit some of the matches.
3912 */
3913 static void
3914ins_compl_update_shown_match(void)
3915{
3916 while (!ins_compl_equal(compl_shown_match,
3917 compl_leader, (int)STRLEN(compl_leader))
3918 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003919 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003920 compl_shown_match = compl_shown_match->cp_next;
3921
3922 // If we didn't find it searching forward, and compl_shows_dir is
3923 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003924 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003925 && !ins_compl_equal(compl_shown_match,
3926 compl_leader, (int)STRLEN(compl_leader))
3927 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003928 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003929 {
3930 while (!ins_compl_equal(compl_shown_match,
3931 compl_leader, (int)STRLEN(compl_leader))
3932 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003933 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003934 compl_shown_match = compl_shown_match->cp_prev;
3935 }
3936}
3937
3938/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003939 * Delete the old text being completed.
3940 */
3941 void
3942ins_compl_delete(void)
3943{
3944 int col;
3945
3946 // In insert mode: Delete the typed part.
3947 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003948 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003949 if ((int)curwin->w_cursor.col > col)
3950 {
3951 if (stop_arrow() == FAIL)
3952 return;
3953 backspace_until_column(col);
3954 }
3955
3956 // TODO: is this sufficient for redrawing? Redrawing everything causes
3957 // flicker, thus we can't do that.
3958 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003959#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003960 // clear v:completed_item
3961 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003962#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003963}
3964
3965/*
3966 * Insert the new text being completed.
3967 * "in_compl_func" is TRUE when called from complete_check().
3968 */
3969 void
3970ins_compl_insert(int in_compl_func)
3971{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003972 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003973
3974 // Make sure we don't go over the end of the string, this can happen with
3975 // illegal bytes.
3976 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3977 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003978 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003979 compl_used_match = FALSE;
3980 else
3981 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003982#ifdef FEAT_EVAL
3983 {
3984 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3985
3986 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3987 }
3988#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003989 if (!in_compl_func)
3990 compl_curr_match = compl_shown_match;
3991}
3992
3993/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003994 * show the file name for the completion match (if any). Truncate the file
3995 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003996 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003997 static void
3998ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003999{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004000 char *lead = _("match in file");
4001 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4002 char_u *s;
4003 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004004
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004005 if (space <= 0)
4006 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004007
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004008 // We need the tail that fits. With double-byte encoding going
4009 // back from the end is very slow, thus go from the start and keep
4010 // the text that fits in "space" between "s" and "e".
4011 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004012 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004013 space -= ptr2cells(e);
4014 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004015 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004016 space += ptr2cells(s);
4017 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004018 }
4019 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004020 msg_hist_off = TRUE;
4021 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4022 s > compl_shown_match->cp_fname ? "<" : "", s);
4023 msg((char *)IObuff);
4024 msg_hist_off = FALSE;
4025 redraw_cmdline = FALSE; // don't overwrite!
4026}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004027
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004028/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004029 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004030 * times. The number of matches found is returned in 'num_matches'.
4031 *
4032 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4033 * get more completions. If it is FALSE, then do nothing when there are no more
4034 * completions in the given direction.
4035 *
4036 * If "advance" is TRUE, then completion will move to the first match.
4037 * Otherwise, the original text will be shown.
4038 *
4039 * Returns OK on success and -1 if the number of matches are unknown.
4040 */
4041 static int
4042find_next_completion_match(
4043 int allow_get_expansion,
4044 int todo, // repeat completion this many times
4045 int advance,
4046 int *num_matches)
4047{
4048 int found_end = FALSE;
4049 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004050
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004051 while (--todo >= 0)
4052 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004053 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004054 {
4055 compl_shown_match = compl_shown_match->cp_next;
4056 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004057 && (is_first_match(compl_shown_match->cp_next)
4058 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004059 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004060 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004061 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004062 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004063 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004064 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004065 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004066 }
4067 else
4068 {
4069 if (!allow_get_expansion)
4070 {
4071 if (advance)
4072 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004073 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004074 compl_pending -= todo + 1;
4075 else
4076 compl_pending += todo + 1;
4077 }
4078 return -1;
4079 }
4080
4081 if (!compl_no_select && advance)
4082 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004083 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004084 --compl_pending;
4085 else
4086 ++compl_pending;
4087 }
4088
4089 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004090 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004091
4092 // handle any pending completions
4093 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004094 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004095 {
4096 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4097 {
4098 compl_shown_match = compl_shown_match->cp_next;
4099 --compl_pending;
4100 }
4101 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4102 {
4103 compl_shown_match = compl_shown_match->cp_prev;
4104 ++compl_pending;
4105 }
4106 else
4107 break;
4108 }
4109 found_end = FALSE;
4110 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004111 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004112 && compl_leader != NULL
4113 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004114 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004115 ++todo;
4116 else
4117 // Remember a matching item.
4118 found_compl = compl_shown_match;
4119
4120 // Stop at the end of the list when we found a usable match.
4121 if (found_end)
4122 {
4123 if (found_compl != NULL)
4124 {
4125 compl_shown_match = found_compl;
4126 break;
4127 }
4128 todo = 1; // use first usable match after wrapping around
4129 }
4130 }
4131
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004132 return OK;
4133}
4134
4135/*
4136 * Fill in the next completion in the current direction.
4137 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4138 * get more completions. If it is FALSE, then we just do nothing when there
4139 * are no more completions in a given direction. The latter case is used when
4140 * we are still in the middle of finding completions, to allow browsing
4141 * through the ones found so far.
4142 * Return the total number of matches, or -1 if still unknown -- webb.
4143 *
4144 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4145 * compl_shown_match here.
4146 *
4147 * Note that this function may be called recursively once only. First with
4148 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4149 * calls this function with "allow_get_expansion" FALSE.
4150 */
4151 static int
4152ins_compl_next(
4153 int allow_get_expansion,
4154 int count, // repeat completion this many times; should
4155 // be at least 1
4156 int insert_match, // Insert the newly selected match
4157 int in_compl_func) // called from complete_check()
4158{
4159 int num_matches = -1;
4160 int todo = count;
4161 int advance;
4162 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004163 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004164
4165 // When user complete function return -1 for findstart which is next
4166 // time of 'always', compl_shown_match become NULL.
4167 if (compl_shown_match == NULL)
4168 return -1;
4169
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004170 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004171 // Update "compl_shown_match" to the actually shown match
4172 ins_compl_update_shown_match();
4173
4174 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004175 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004176 // Delete old text to be replaced
4177 ins_compl_delete();
4178
4179 // When finding the longest common text we stick at the original text,
4180 // don't let CTRL-N or CTRL-P move to the first match.
4181 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4182
4183 // When restarting the search don't insert the first match either.
4184 if (compl_restarting)
4185 {
4186 advance = FALSE;
4187 compl_restarting = FALSE;
4188 }
4189
4190 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4191 // around.
4192 if (find_next_completion_match(allow_get_expansion, todo, advance,
4193 &num_matches) == -1)
4194 return -1;
4195
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004196 if (curbuf != orig_curbuf)
4197 {
4198 // In case some completion function switched buffer, don't want to
4199 // insert the completion elsewhere.
4200 return -1;
4201 }
4202
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004203 // Insert the text of the new completion, or the compl_leader.
4204 if (compl_no_insert && !started)
4205 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004206 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004207 compl_used_match = FALSE;
4208 }
4209 else if (insert_match)
4210 {
4211 if (!compl_get_longest || compl_used_match)
4212 ins_compl_insert(in_compl_func);
4213 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004214 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004215 }
4216 else
4217 compl_used_match = FALSE;
4218
4219 if (!allow_get_expansion)
4220 {
4221 // may undisplay the popup menu first
4222 ins_compl_upd_pum();
4223
4224 if (pum_enough_matches())
4225 // Will display the popup menu, don't redraw yet to avoid flicker.
4226 pum_call_update_screen();
4227 else
4228 // Not showing the popup menu yet, redraw to show the user what was
4229 // inserted.
4230 update_screen(0);
4231
4232 // display the updated popup menu
4233 ins_compl_show_pum();
4234#ifdef FEAT_GUI
4235 if (gui.in_use)
4236 {
4237 // Show the cursor after the match, not after the redrawn text.
4238 setcursor();
4239 out_flush_cursor(FALSE, FALSE);
4240 }
4241#endif
4242
4243 // Delete old text to be replaced, since we're still searching and
4244 // don't want to match ourselves!
4245 ins_compl_delete();
4246 }
4247
4248 // Enter will select a match when the match wasn't inserted and the popup
4249 // menu is visible.
4250 if (compl_no_insert && !started)
4251 compl_enter_selects = TRUE;
4252 else
4253 compl_enter_selects = !insert_match && compl_match_array != NULL;
4254
4255 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004256 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004257 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004258
4259 return num_matches;
4260}
4261
4262/*
4263 * Call this while finding completions, to check whether the user has hit a key
4264 * that should change the currently displayed completion, or exit completion
4265 * mode. Also, when compl_pending is not zero, show a completion as soon as
4266 * possible. -- webb
4267 * "frequency" specifies out of how many calls we actually check.
4268 * "in_compl_func" is TRUE when called from complete_check(), don't set
4269 * compl_curr_match.
4270 */
4271 void
4272ins_compl_check_keys(int frequency, int in_compl_func)
4273{
4274 static int count = 0;
4275 int c;
4276
4277 // Don't check when reading keys from a script, :normal or feedkeys().
4278 // That would break the test scripts. But do check for keys when called
4279 // from complete_check().
4280 if (!in_compl_func && (using_script() || ex_normal_busy))
4281 return;
4282
4283 // Only do this at regular intervals
4284 if (++count < frequency)
4285 return;
4286 count = 0;
4287
4288 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4289 // can't do its work correctly.
4290 c = vpeekc_any();
4291 if (c != NUL)
4292 {
4293 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4294 {
4295 c = safe_vgetc(); // Eat the character
4296 compl_shows_dir = ins_compl_key2dir(c);
4297 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4298 c != K_UP && c != K_DOWN, in_compl_func);
4299 }
4300 else
4301 {
4302 // Need to get the character to have KeyTyped set. We'll put it
4303 // back with vungetc() below. But skip K_IGNORE.
4304 c = safe_vgetc();
4305 if (c != K_IGNORE)
4306 {
4307 // Don't interrupt completion when the character wasn't typed,
4308 // e.g., when doing @q to replay keys.
4309 if (c != Ctrl_R && KeyTyped)
4310 compl_interrupted = TRUE;
4311
4312 vungetc(c);
4313 }
4314 }
4315 }
4316 if (compl_pending != 0 && !got_int && !compl_no_insert)
4317 {
4318 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4319
4320 compl_pending = 0;
4321 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4322 }
4323}
4324
4325/*
4326 * Decide the direction of Insert mode complete from the key typed.
4327 * Returns BACKWARD or FORWARD.
4328 */
4329 static int
4330ins_compl_key2dir(int c)
4331{
4332 if (c == Ctrl_P || c == Ctrl_L
4333 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4334 return BACKWARD;
4335 return FORWARD;
4336}
4337
4338/*
4339 * Return TRUE for keys that are used for completion only when the popup menu
4340 * is visible.
4341 */
4342 static int
4343ins_compl_pum_key(int c)
4344{
4345 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4346 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4347 || c == K_UP || c == K_DOWN);
4348}
4349
4350/*
4351 * Decide the number of completions to move forward.
4352 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4353 */
4354 static int
4355ins_compl_key2count(int c)
4356{
4357 int h;
4358
4359 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4360 {
4361 h = pum_get_height();
4362 if (h > 3)
4363 h -= 2; // keep some context
4364 return h;
4365 }
4366 return 1;
4367}
4368
4369/*
4370 * Return TRUE if completion with "c" should insert the match, FALSE if only
4371 * to change the currently selected completion.
4372 */
4373 static int
4374ins_compl_use_match(int c)
4375{
4376 switch (c)
4377 {
4378 case K_UP:
4379 case K_DOWN:
4380 case K_PAGEDOWN:
4381 case K_KPAGEDOWN:
4382 case K_S_DOWN:
4383 case K_PAGEUP:
4384 case K_KPAGEUP:
4385 case K_S_UP:
4386 return FALSE;
4387 }
4388 return TRUE;
4389}
4390
4391/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004392 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4393 * completion)
John Marriott8c85a2a2024-05-20 19:18:26 +02004394 * Sets the global variables: compl_col, compl_length, compl_pattern and
4395 * compl_patternlen.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004396 * Uses the global variables: compl_cont_status and ctrl_x_mode
4397 */
4398 static int
4399get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4400{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004401 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004402 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004403 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004404 {
4405 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4406 ;
4407 compl_col += ++startcol;
4408 compl_length = curs_col - startcol;
4409 }
4410 if (p_ic)
4411 compl_pattern = str_foldcase(line + compl_col,
4412 compl_length, NULL, 0);
4413 else
4414 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4415 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004416 {
4417 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004418 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004419 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004420 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004421 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004422 {
4423 char_u *prefix = (char_u *)"\\<";
John Marriott8c85a2a2024-05-20 19:18:26 +02004424 size_t prefixlen = STRLEN_LITERAL("\\<");
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004425
4426 // we need up to 2 extra chars for the prefix
4427 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
John Marriott8c85a2a2024-05-20 19:18:26 +02004428 compl_length) + prefixlen);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004429 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004430 {
4431 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004432 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004433 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004434 if (!vim_iswordp(line + compl_col)
4435 || (compl_col > 0
4436 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
John Marriott8c85a2a2024-05-20 19:18:26 +02004437 {
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004438 prefix = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02004439 prefixlen = 0;
4440 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004441 STRCPY((char *)compl_pattern, prefix);
John Marriott8c85a2a2024-05-20 19:18:26 +02004442 (void)quote_meta(compl_pattern + prefixlen,
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004443 line + compl_col, compl_length);
4444 }
4445 else if (--startcol < 0
4446 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4447 {
4448 // Match any word of at least two chars
John Marriott8c85a2a2024-05-20 19:18:26 +02004449 compl_pattern = vim_strnsave((char_u *)"\\<\\k\\k", STRLEN_LITERAL("\\<\\k\\k"));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004450 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004451 {
4452 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004453 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004454 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004455 compl_col += curs_col;
4456 compl_length = 0;
4457 }
4458 else
4459 {
4460 // Search the point of change class of multibyte character
4461 // or not a word single byte character backward.
4462 if (has_mbyte)
4463 {
4464 int base_class;
4465 int head_off;
4466
4467 startcol -= (*mb_head_off)(line, line + startcol);
4468 base_class = mb_get_class(line + startcol);
4469 while (--startcol >= 0)
4470 {
4471 head_off = (*mb_head_off)(line, line + startcol);
4472 if (base_class != mb_get_class(line + startcol
4473 - head_off))
4474 break;
4475 startcol -= head_off;
4476 }
4477 }
4478 else
4479 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4480 ;
4481 compl_col += ++startcol;
4482 compl_length = (int)curs_col - startcol;
4483 if (compl_length == 1)
4484 {
4485 // Only match word with at least two chars -- webb
4486 // there's no need to call quote_meta,
4487 // alloc(7) is enough -- Acevedo
4488 compl_pattern = alloc(7);
4489 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004490 {
4491 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004492 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004493 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004494 STRCPY((char *)compl_pattern, "\\<");
4495 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4496 STRCAT((char *)compl_pattern, "\\k");
4497 }
4498 else
4499 {
4500 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4501 compl_length) + 2);
4502 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004503 {
4504 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004505 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004506 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004507 STRCPY((char *)compl_pattern, "\\<");
4508 (void)quote_meta(compl_pattern + 2, line + compl_col,
4509 compl_length);
4510 }
4511 }
4512
John Marriott8c85a2a2024-05-20 19:18:26 +02004513 compl_patternlen = STRLEN(compl_pattern);
4514
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004515 return OK;
4516}
4517
4518/*
4519 * Get the pattern, column and length for whole line completion or for the
4520 * complete() function.
4521 * Sets the global variables: compl_col, compl_length and compl_pattern.
4522 */
4523 static int
4524get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4525{
4526 compl_col = (colnr_T)getwhitecols(line);
4527 compl_length = (int)curs_col - (int)compl_col;
4528 if (compl_length < 0) // cursor in indent: empty pattern
4529 compl_length = 0;
4530 if (p_ic)
4531 compl_pattern = str_foldcase(line + compl_col, compl_length,
4532 NULL, 0);
4533 else
4534 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4535 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004536 {
4537 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004538 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004539 }
4540
4541 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004542
4543 return OK;
4544}
4545
4546/*
4547 * Get the pattern, column and length for filename completion.
4548 * Sets the global variables: compl_col, compl_length and compl_pattern.
4549 */
4550 static int
4551get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4552{
4553 // Go back to just before the first filename character.
4554 if (startcol > 0)
4555 {
4556 char_u *p = line + startcol;
4557
4558 MB_PTR_BACK(line, p);
4559 while (p > line && vim_isfilec(PTR2CHAR(p)))
4560 MB_PTR_BACK(line, p);
4561 if (p == line && vim_isfilec(PTR2CHAR(p)))
4562 startcol = 0;
4563 else
4564 startcol = (int)(p - line) + 1;
4565 }
4566
4567 compl_col += startcol;
4568 compl_length = (int)curs_col - startcol;
4569 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4570 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004571 {
4572 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004573 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004574 }
4575
4576 compl_patternlen = STRLEN(compl_pattern);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004577
4578 return OK;
4579}
4580
4581/*
4582 * Get the pattern, column and length for command-line completion.
4583 * Sets the global variables: compl_col, compl_length and compl_pattern.
4584 */
4585 static int
4586get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4587{
4588 compl_pattern = vim_strnsave(line, curs_col);
4589 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004590 {
4591 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004592 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004593 }
4594 compl_patternlen = curs_col;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004595 set_cmd_context(&compl_xp, compl_pattern,
Mike Williams16b63bd2024-06-01 11:33:40 +02004596 (int)compl_patternlen, curs_col, FALSE);
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004597 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4598 || compl_xp.xp_context == EXPAND_NOTHING)
4599 // No completion possible, use an empty pattern to get a
4600 // "pattern not found" message.
4601 compl_col = curs_col;
4602 else
4603 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4604 compl_length = curs_col - compl_col;
4605
4606 return OK;
4607}
4608
4609/*
4610 * Get the pattern, column and length for user defined completion ('omnifunc',
4611 * 'completefunc' and 'thesaurusfunc')
4612 * Sets the global variables: compl_col, compl_length and compl_pattern.
4613 * Uses the global variable: spell_bad_len
4614 */
4615 static int
4616get_userdefined_compl_info(colnr_T curs_col UNUSED)
4617{
4618 int ret = FAIL;
4619
4620#ifdef FEAT_COMPL_FUNC
4621 // Call user defined function 'completefunc' with "a:findstart"
4622 // set to 1 to obtain the length of text to use for completion.
4623 char_u *line;
4624 typval_T args[3];
4625 int col;
4626 char_u *funcname;
4627 pos_T pos;
4628 int save_State = State;
4629 callback_T *cb;
4630
4631 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4632 // length as a string
4633 funcname = get_complete_funcname(ctrl_x_mode);
4634 if (*funcname == NUL)
4635 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004636 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004637 ? "completefunc" : "omnifunc");
4638 return FAIL;
4639 }
4640
4641 args[0].v_type = VAR_NUMBER;
4642 args[0].vval.v_number = 1;
4643 args[1].v_type = VAR_STRING;
4644 args[1].vval.v_string = (char_u *)"";
4645 args[2].v_type = VAR_UNKNOWN;
4646 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004647 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004648 cb = get_insert_callback(ctrl_x_mode);
4649 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004650 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004651
4652 State = save_State;
4653 curwin->w_cursor = pos; // restore the cursor position
zeertzjq0a419e02024-04-02 19:01:14 +02004654 check_cursor(); // make sure cursor position is valid, just in case
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004655 validate_cursor();
4656 if (!EQUAL_POS(curwin->w_cursor, pos))
4657 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004658 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004659 return FAIL;
4660 }
4661
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004662 // Return value -2 means the user complete function wants to cancel the
4663 // complete without an error, do the same if the function did not execute
4664 // successfully.
4665 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004666 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004667 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004668 if (col == -3)
4669 {
4670 ctrl_x_mode = CTRL_X_NORMAL;
4671 edit_submode = NULL;
4672 if (!shortmess(SHM_COMPLETIONMENU))
4673 msg_clr_cmdline();
4674 return FAIL;
4675 }
4676
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004677 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004678 // completion.
4679 compl_opt_refresh_always = FALSE;
4680 compl_opt_suppress_empty = FALSE;
4681
4682 if (col < 0)
4683 col = curs_col;
4684 compl_col = col;
4685 if (compl_col > curs_col)
4686 compl_col = curs_col;
4687
4688 // Setup variables for completion. Need to obtain "line" again,
4689 // it may have become invalid.
4690 line = ml_get(curwin->w_cursor.lnum);
4691 compl_length = curs_col - compl_col;
4692 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4693 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004694 {
4695 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004696 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004697 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004698
John Marriott8c85a2a2024-05-20 19:18:26 +02004699 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004700 ret = OK;
4701#endif
4702
4703 return ret;
4704}
4705
4706/*
4707 * Get the pattern, column and length for spell completion.
4708 * Sets the global variables: compl_col, compl_length and compl_pattern.
4709 * Uses the global variable: spell_bad_len
4710 */
4711 static int
4712get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4713{
4714 int ret = FAIL;
4715#ifdef FEAT_SPELL
4716 char_u *line;
4717
4718 if (spell_bad_len > 0)
4719 compl_col = curs_col - spell_bad_len;
4720 else
4721 compl_col = spell_word_start(startcol);
4722 if (compl_col >= (colnr_T)startcol)
4723 {
4724 compl_length = 0;
4725 compl_col = curs_col;
4726 }
4727 else
4728 {
4729 spell_expand_check_cap(compl_col);
4730 compl_length = (int)curs_col - compl_col;
4731 }
4732 // Need to obtain "line" again, it may have become invalid.
4733 line = ml_get(curwin->w_cursor.lnum);
4734 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4735 if (compl_pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02004736 {
4737 compl_patternlen = 0;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004738 return FAIL;
John Marriott8c85a2a2024-05-20 19:18:26 +02004739 }
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004740
John Marriott8c85a2a2024-05-20 19:18:26 +02004741 compl_patternlen = compl_length;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004742 ret = OK;
4743#endif
4744
4745 return ret;
4746}
4747
4748/*
4749 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004750 * "startcol" - start column number of the completion pattern/text
4751 * "cur_col" - current cursor column
4752 * On return, "line_invalid" is set to TRUE, if the current line may have
4753 * become invalid and needs to be fetched again.
4754 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004755 */
4756 static int
4757compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4758{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004759 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004760 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4761 && !thesaurus_func_complete(ctrl_x_mode)))
4762 {
4763 return get_normal_compl_info(line, startcol, curs_col);
4764 }
4765 else if (ctrl_x_mode_line_or_eval())
4766 {
4767 return get_wholeline_compl_info(line, curs_col);
4768 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004769 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004770 {
4771 return get_filename_compl_info(line, startcol, curs_col);
4772 }
4773 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4774 {
4775 return get_cmdline_compl_info(line, curs_col);
4776 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004777 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004778 || thesaurus_func_complete(ctrl_x_mode))
4779 {
4780 if (get_userdefined_compl_info(curs_col) == FAIL)
4781 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004782 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004783 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004784 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004785 {
4786 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4787 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004788 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004789 }
4790 else
4791 {
4792 internal_error("ins_complete()");
4793 return FAIL;
4794 }
4795
4796 return OK;
4797}
4798
4799/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004800 * Continue an interrupted completion mode search in "line".
4801 *
4802 * If this same ctrl_x_mode has been interrupted use the text from
4803 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4804 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4805 * the same line as the cursor then fix it (the line has been split because it
4806 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4807 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004808 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004809 static void
4810ins_compl_continue_search(char_u *line)
4811{
4812 // it is a continued search
4813 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004814 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4815 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004816 {
4817 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4818 {
4819 // line (probably) wrapped, set compl_startpos to the
4820 // first non_blank in the line, if it is not a wordchar
4821 // include it to get a better pattern, but then we don't
4822 // want the "\\<" prefix, check it below
4823 compl_col = (colnr_T)getwhitecols(line);
4824 compl_startpos.col = compl_col;
4825 compl_startpos.lnum = curwin->w_cursor.lnum;
4826 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4827 }
4828 else
4829 {
4830 // S_IPOS was set when we inserted a word that was at the
4831 // beginning of the line, which means that we'll go to SOL
4832 // mode but first we need to redefine compl_startpos
4833 if (compl_cont_status & CONT_S_IPOS)
4834 {
4835 compl_cont_status |= CONT_SOL;
4836 compl_startpos.col = (colnr_T)(skipwhite(
4837 line + compl_length
4838 + compl_startpos.col) - line);
4839 }
4840 compl_col = compl_startpos.col;
4841 }
4842 compl_length = curwin->w_cursor.col - (int)compl_col;
4843 // IObuff is used to add a "word from the next line" would we
4844 // have enough space? just being paranoid
4845#define MIN_SPACE 75
4846 if (compl_length > (IOSIZE - MIN_SPACE))
4847 {
4848 compl_cont_status &= ~CONT_SOL;
4849 compl_length = (IOSIZE - MIN_SPACE);
4850 compl_col = curwin->w_cursor.col - compl_length;
4851 }
4852 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4853 if (compl_length < 1)
4854 compl_cont_status &= CONT_LOCAL;
4855 }
4856 else if (ctrl_x_mode_line_or_eval())
4857 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4858 else
4859 compl_cont_status = 0;
4860}
4861
4862/*
4863 * start insert mode completion
4864 */
4865 static int
4866ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004867{
4868 char_u *line;
4869 int startcol = 0; // column where searched text starts
4870 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004871 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004872 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004873 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004874
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004875 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004876
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004877 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004878 did_si = FALSE;
4879 can_si = FALSE;
4880 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004881 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004882 return FAIL;
4883
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004884 line = ml_get(curwin->w_cursor.lnum);
4885 curs_col = curwin->w_cursor.col;
4886 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004887
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004888 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4889 && compl_cont_mode == ctrl_x_mode)
4890 // this same ctrl-x_mode was interrupted previously. Continue the
4891 // completion.
4892 ins_compl_continue_search(line);
4893 else
4894 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004895
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004896 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004897 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004898 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004899 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004900 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4901 compl_cont_status = 0;
4902 compl_cont_status |= CONT_N_ADDS;
4903 compl_startpos = curwin->w_cursor;
4904 startcol = (int)curs_col;
4905 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004906 }
4907
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004908 // Work out completion pattern and original text -- webb
4909 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4910 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004911 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4912 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004913 // restore did_ai, so that adding comment leader works
4914 did_ai = save_did_ai;
4915 return FAIL;
4916 }
4917 // If "line" was changed while getting completion info get it again.
4918 if (line_invalid)
4919 line = ml_get(curwin->w_cursor.lnum);
4920
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004921 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004922 {
4923 edit_submode_pre = (char_u *)_(" Adding");
4924 if (ctrl_x_mode_line_or_eval())
4925 {
4926 // Insert a new line, keep indentation but ignore 'comments'.
4927 char_u *old = curbuf->b_p_com;
4928
4929 curbuf->b_p_com = (char_u *)"";
4930 compl_startpos.lnum = curwin->w_cursor.lnum;
4931 compl_startpos.col = compl_col;
4932 ins_eol('\r');
4933 curbuf->b_p_com = old;
4934 compl_length = 0;
4935 compl_col = curwin->w_cursor.col;
4936 }
4937 }
4938 else
4939 {
4940 edit_submode_pre = NULL;
4941 compl_startpos.col = compl_col;
4942 }
4943
4944 if (compl_cont_status & CONT_LOCAL)
4945 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4946 else
4947 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4948
4949 // If any of the original typed text has been changed we need to fix
4950 // the redo buffer.
4951 ins_compl_fixRedoBufForLeader(NULL);
4952
4953 // Always add completion for the original text.
4954 vim_free(compl_orig_text);
4955 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4956 if (p_ic)
4957 flags |= CP_ICASE;
4958 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4959 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4960 {
4961 VIM_CLEAR(compl_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +02004962 compl_patternlen = 0;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004963 VIM_CLEAR(compl_orig_text);
4964 return FAIL;
4965 }
4966
4967 // showmode might reset the internal line pointers, so it must
4968 // be called before line = ml_get(), or when this address is no
4969 // longer needed. -- Acevedo.
4970 edit_submode_extra = (char_u *)_("-- Searching...");
4971 edit_submode_highl = HLF_COUNT;
4972 showmode();
4973 edit_submode_extra = NULL;
4974 out_flush();
4975
4976 return OK;
4977}
4978
4979/*
4980 * display the completion status message
4981 */
4982 static void
4983ins_compl_show_statusmsg(void)
4984{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004985 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004986 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004987 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004988 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004989 ? (char_u *)_("Hit end of paragraph")
4990 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004991 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004992 }
4993
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004994 if (edit_submode_extra == NULL)
4995 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004996 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004997 {
4998 edit_submode_extra = (char_u *)_("Back at original");
4999 edit_submode_highl = HLF_W;
5000 }
5001 else if (compl_cont_status & CONT_S_IPOS)
5002 {
5003 edit_submode_extra = (char_u *)_("Word from other line");
5004 edit_submode_highl = HLF_COUNT;
5005 }
5006 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
5007 {
5008 edit_submode_extra = (char_u *)_("The only match");
5009 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005010 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005011 }
5012 else
5013 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005014#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005015 // Update completion sequence number when needed.
5016 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01005017 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01005018#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005019 // The match should always have a sequence number now, this is
5020 // just a safety check.
5021 if (compl_curr_match->cp_number != -1)
5022 {
5023 // Space for 10 text chars. + 2x10-digit no.s = 31.
5024 // Translations may need more than twice that.
5025 static char_u match_ref[81];
5026
5027 if (compl_matches > 0)
5028 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005029 _("match %d of %d"),
5030 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005031 else
5032 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005033 _("match %d"),
5034 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005035 edit_submode_extra = match_ref;
5036 edit_submode_highl = HLF_R;
5037 if (dollar_vcol >= 0)
5038 curs_columns(FALSE);
5039 }
5040 }
5041 }
5042
5043 // Show a message about what (completion) mode we're in.
5044 if (!compl_opt_suppress_empty)
5045 {
5046 showmode();
5047 if (!shortmess(SHM_COMPLETIONMENU))
5048 {
5049 if (edit_submode_extra != NULL)
5050 {
5051 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01005052 {
5053 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005054 msg_attr((char *)edit_submode_extra,
5055 edit_submode_highl < HLF_COUNT
5056 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005057 msg_hist_off = FALSE;
5058 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005059 }
5060 else
5061 msg_clr_cmdline(); // necessary for "noshowmode"
5062 }
5063 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005064}
5065
5066/*
5067 * Do Insert mode completion.
5068 * Called when character "c" was typed, which has a meaning for completion.
5069 * Returns OK if completion was done, FAIL if something failed (out of mem).
5070 */
5071 int
5072ins_complete(int c, int enable_pum)
5073{
5074 int n;
5075 int save_w_wrow;
5076 int save_w_leftcol;
5077 int insert_match;
5078
5079 compl_direction = ins_compl_key2dir(c);
5080 insert_match = ins_compl_use_match(c);
5081
5082 if (!compl_started)
5083 {
5084 if (ins_compl_start() == FAIL)
5085 return FAIL;
5086 }
5087 else if (insert_match && stop_arrow() == FAIL)
5088 return FAIL;
5089
5090 compl_shown_match = compl_curr_match;
5091 compl_shows_dir = compl_direction;
5092
5093 // Find next match (and following matches).
5094 save_w_wrow = curwin->w_wrow;
5095 save_w_leftcol = curwin->w_leftcol;
5096 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5097
5098 // may undisplay the popup menu
5099 ins_compl_upd_pum();
5100
5101 if (n > 1) // all matches have been found
5102 compl_matches = n;
5103 compl_curr_match = compl_shown_match;
5104 compl_direction = compl_shows_dir;
5105
5106 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5107 // mode.
5108 if (got_int && !global_busy)
5109 {
5110 (void)vgetc();
5111 got_int = FALSE;
5112 }
5113
5114 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005115 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005116 {
5117 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5118 // because we couldn't expand anything at first place, but if we used
5119 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5120 // (such as M in M'exico) if not tried already. -- Acevedo
5121 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005122 || compl_status_adding()
5123 || (ctrl_x_mode_not_default()
5124 && !ctrl_x_mode_path_patterns()
5125 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005126 compl_cont_status &= ~CONT_N_ADDS;
5127 }
5128
5129 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5130 compl_cont_status |= CONT_S_IPOS;
5131 else
5132 compl_cont_status &= ~CONT_S_IPOS;
5133
5134 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005135
5136 // Show the popup menu, unless we got interrupted.
5137 if (enable_pum && !compl_interrupted)
5138 show_pum(save_w_wrow, save_w_leftcol);
5139
5140 compl_was_interrupted = compl_interrupted;
5141 compl_interrupted = FALSE;
5142
5143 return OK;
5144}
5145
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005146/*
5147 * Remove (if needed) and show the popup menu
5148 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005149 static void
5150show_pum(int prev_w_wrow, int prev_w_leftcol)
5151{
5152 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005153 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005154 RedrawingDisabled = 0;
5155
5156 // If the cursor moved or the display scrolled we need to remove the pum
5157 // first.
5158 setcursor();
5159 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5160 ins_compl_del_pum();
5161
5162 ins_compl_show_pum();
5163 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005164
5165 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005166}
5167
5168/*
5169 * Looks in the first "len" chars. of "src" for search-metachars.
5170 * If dest is not NULL the chars. are copied there quoting (with
5171 * a backslash) the metachars, and dest would be NUL terminated.
5172 * Returns the length (needed) of dest
5173 */
5174 static unsigned
5175quote_meta(char_u *dest, char_u *src, int len)
5176{
5177 unsigned m = (unsigned)len + 1; // one extra for the NUL
5178
5179 for ( ; --len >= 0; src++)
5180 {
5181 switch (*src)
5182 {
5183 case '.':
5184 case '*':
5185 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005186 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005187 break;
5188 // FALLTHROUGH
5189 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005190 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005191 break;
5192 // FALLTHROUGH
5193 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005194 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005195 break;
5196 // FALLTHROUGH
5197 case '^': // currently it's not needed.
5198 case '$':
5199 m++;
5200 if (dest != NULL)
5201 *dest++ = '\\';
5202 break;
5203 }
5204 if (dest != NULL)
5205 *dest++ = *src;
5206 // Copy remaining bytes of a multibyte character.
5207 if (has_mbyte)
5208 {
5209 int i, mb_len;
5210
5211 mb_len = (*mb_ptr2len)(src) - 1;
5212 if (mb_len > 0 && len >= mb_len)
5213 for (i = 0; i < mb_len; ++i)
5214 {
5215 --len;
5216 ++src;
5217 if (dest != NULL)
5218 *dest++ = *src;
5219 }
5220 }
5221 }
5222 if (dest != NULL)
5223 *dest = NUL;
5224
5225 return m;
5226}
5227
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005228#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005229 void
5230free_insexpand_stuff(void)
5231{
5232 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005233# ifdef FEAT_EVAL
5234 free_callback(&cfu_cb);
5235 free_callback(&ofu_cb);
5236 free_callback(&tsrfu_cb);
5237# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005238}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005239#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005240
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005241#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005242/*
5243 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5244 * spelled word, if there is one.
5245 */
5246 static void
5247spell_back_to_badword(void)
5248{
5249 pos_T tpos = curwin->w_cursor;
5250
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02005251 spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005252 if (curwin->w_cursor.col != tpos.col)
5253 start_arrow(&tpos);
5254}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005255#endif