blob: 09786091f836a922b1cd67270978679ef4b8019a [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
127# ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128static char e_compldel[] = N_("E840: Completion function deleted text");
129# endif
130
131/*
132 * All the current matches are stored in a list.
133 * "compl_first_match" points to the start of the list.
134 * "compl_curr_match" points to the currently selected entry.
135 * "compl_shown_match" is different from compl_curr_match during
136 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000137 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100138 */
139static compl_T *compl_first_match = NULL;
140static compl_T *compl_curr_match = NULL;
141static compl_T *compl_shown_match = NULL;
142static compl_T *compl_old_match = NULL;
143
144// After using a cursor key <Enter> selects a match in the popup menu,
145// otherwise it inserts a line break.
146static int compl_enter_selects = FALSE;
147
148// When "compl_leader" is not NULL only matches that start with this string
149// are used.
150static char_u *compl_leader = NULL;
151
152static int compl_get_longest = FALSE; // put longest common string
153 // in compl_leader
154
155static int compl_no_insert = FALSE; // FALSE: select & insert
156 // TRUE: noinsert
157static int compl_no_select = FALSE; // FALSE: select & insert
158 // TRUE: noselect
159
160// Selected one of the matches. When FALSE the match was edited or using the
161// longest common string.
162static int compl_used_match;
163
164// didn't finish finding completions.
165static int compl_was_interrupted = FALSE;
166
167// Set when character typed while looking for matches and it means we should
168// stop looking for matches.
169static int compl_interrupted = FALSE;
170
171static int compl_restarting = FALSE; // don't insert match
172
173// When the first completion is done "compl_started" is set. When it's
174// FALSE the word to be completed must be located.
175static int compl_started = FALSE;
176
177// Which Ctrl-X mode are we in?
178static int ctrl_x_mode = CTRL_X_NORMAL;
179
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000180static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100181static char_u *compl_pattern = NULL;
182static int compl_direction = FORWARD;
183static int compl_shows_dir = FORWARD;
184static int compl_pending = 0; // > 1 for postponed CTRL-N
185static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000186// Length in bytes of the text being completed (this is deleted to be replaced
187// by the match.)
188static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100189static colnr_T compl_col = 0; // column where the text starts
190 // that is being completed
191static char_u *compl_orig_text = NULL; // text as it was before
192 // completion started
193static int compl_cont_mode = 0;
194static expand_T compl_xp;
195
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000196// List of flags for method of completion.
197static int compl_cont_status = 0;
198# define CONT_ADDING 1 // "normal" or "adding" expansion
199# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
200 // it's set only iff N_ADDS is set
201# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
202# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
203 // if so, word-wise-expansion will set SOL
204# define CONT_SOL 16 // pattern includes start of line, just for
205 // word-wise expansion, not set for ^X^L
206# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
207 // expansion, (eg use complete=.)
208
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100209static int compl_opt_refresh_always = FALSE;
210static int compl_opt_suppress_empty = FALSE;
211
Bram Moolenaar08928322020-01-04 14:32:48 +0100212static 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 +0100213static void ins_compl_longest_match(compl_T *match);
214static void ins_compl_del_pum(void);
215static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
216static char_u *find_line_end(char_u *ptr);
217static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static int ins_compl_need_restart(void);
219static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000220static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100221static void ins_compl_restart(void);
222static void ins_compl_set_original_text(char_u *str);
223static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
224# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
225static void ins_compl_add_list(list_T *list);
226static void ins_compl_add_dict(dict_T *dict);
227# endif
228static int ins_compl_key2dir(int c);
229static int ins_compl_pum_key(int c);
230static int ins_compl_key2count(int c);
231static void show_pum(int prev_w_wrow, int prev_w_leftcol);
232static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100233
234#ifdef FEAT_SPELL
235static void spell_back_to_badword(void);
236static int spell_bad_len = 0; // length of located bad word
237#endif
238
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100239/*
240 * CTRL-X pressed in Insert mode.
241 */
242 void
243ins_ctrl_x(void)
244{
zeertzjqdca29d92021-08-31 19:12:51 +0200245 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100246 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000247 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100248 if (compl_cont_status & CONT_N_ADDS)
249 compl_cont_status |= CONT_INTRPT;
250 else
251 compl_cont_status = 0;
252 // We're not sure which CTRL-X mode it will be yet
253 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
254 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
255 edit_submode_pre = NULL;
256 showmode();
257 }
zeertzjqdca29d92021-08-31 19:12:51 +0200258 else
259 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
260 // CTRL-V look like CTRL-N
261 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100262
263 trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100264}
265
266/*
267 * Functions to check the current CTRL-X mode.
268 */
269int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
270int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
271int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
272int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
273int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
274int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
275int ctrl_x_mode_path_patterns(void) {
276 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
277int ctrl_x_mode_path_defines(void) {
278 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
279int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
280int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200281int ctrl_x_mode_cmdline(void) {
282 return ctrl_x_mode == CTRL_X_CMDLINE
283 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100284int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
285int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
286int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000287static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100288int ctrl_x_mode_line_or_eval(void) {
289 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
290
291/*
292 * Whether other than default completion has been selected.
293 */
294 int
295ctrl_x_mode_not_default(void)
296{
297 return ctrl_x_mode != CTRL_X_NORMAL;
298}
299
300/*
zeertzjqdca29d92021-08-31 19:12:51 +0200301 * Whether CTRL-X was typed without a following character,
302 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100303 */
304 int
305ctrl_x_mode_not_defined_yet(void)
306{
307 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
308}
309
310/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000311 * Return TRUE if currently in "normal" or "adding" insert completion matches
312 * state
313 */
314 int
315compl_status_adding(void)
316{
317 return compl_cont_status & CONT_ADDING;
318}
319
320/*
321 * Return TRUE if the completion pattern includes start of line, just for
322 * word-wise expansion.
323 */
324 int
325compl_status_sol(void)
326{
327 return compl_cont_status & CONT_SOL;
328}
329
330/*
331 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
332 */
333 int
334compl_status_local(void)
335{
336 return compl_cont_status & CONT_LOCAL;
337}
338
339/*
340 * Clear the completion status flags
341 */
342 void
343compl_status_clear(void)
344{
345 compl_cont_status = 0;
346}
347
348/*
349 * Return TRUE if completion is using the forward direction matches
350 */
351 static int
352compl_dir_forward(void)
353{
354 return compl_direction == FORWARD;
355}
356
357/*
358 * Return TRUE if currently showing forward completion matches
359 */
360 static int
361compl_shows_dir_forward(void)
362{
363 return compl_shows_dir == FORWARD;
364}
365
366/*
367 * Return TRUE if currently showing backward completion matches
368 */
369 static int
370compl_shows_dir_backward(void)
371{
372 return compl_shows_dir == BACKWARD;
373}
374
375/*
376 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100377 */
378 int
379has_compl_option(int dict_opt)
380{
381 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200382#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100383 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200384#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100385 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100386 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
387#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100388 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100389#endif
390 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100391 {
392 ctrl_x_mode = CTRL_X_NORMAL;
393 edit_submode = NULL;
394 msg_attr(dict_opt ? _("'dictionary' option is empty")
395 : _("'thesaurus' option is empty"),
396 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100397 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100398 {
399 vim_beep(BO_COMPL);
400 setcursor();
401 out_flush();
402#ifdef FEAT_EVAL
403 if (!get_vim_var_nr(VV_TESTING))
404#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100405 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100406 }
407 return FALSE;
408 }
409 return TRUE;
410}
411
412/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000413 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100414 * This depends on the current mode.
415 */
416 int
417vim_is_ctrl_x_key(int c)
418{
419 // Always allow ^R - let its results then be checked
420 if (c == Ctrl_R)
421 return TRUE;
422
423 // Accept <PageUp> and <PageDown> if the popup menu is visible.
424 if (ins_compl_pum_key(c))
425 return TRUE;
426
427 switch (ctrl_x_mode)
428 {
429 case 0: // Not in any CTRL-X mode
430 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
431 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200432 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100433 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
434 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
435 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
436 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
437 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200438 || c == Ctrl_S || c == Ctrl_K || c == 's'
439 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100440 case CTRL_X_SCROLL:
441 return (c == Ctrl_Y || c == Ctrl_E);
442 case CTRL_X_WHOLE_LINE:
443 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
444 case CTRL_X_FILES:
445 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
446 case CTRL_X_DICTIONARY:
447 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
448 case CTRL_X_THESAURUS:
449 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
450 case CTRL_X_TAGS:
451 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
452#ifdef FEAT_FIND_ID
453 case CTRL_X_PATH_PATTERNS:
454 return (c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_PATH_DEFINES:
456 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
457#endif
458 case CTRL_X_CMDLINE:
459 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
460 || c == Ctrl_X);
461#ifdef FEAT_COMPL_FUNC
462 case CTRL_X_FUNCTION:
463 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_OMNI:
465 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
466#endif
467 case CTRL_X_SPELL:
468 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
469 case CTRL_X_EVAL:
470 return (c == Ctrl_P || c == Ctrl_N);
471 }
472 internal_error("vim_is_ctrl_x_key()");
473 return FALSE;
474}
475
476/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000477 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000478 */
479 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000480match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000481{
482 return match->cp_flags & CP_ORIGINAL_TEXT;
483}
484
485/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000486 * Returns TRUE if "match" is the first match in the completion list.
487 */
488 static int
489is_first_match(compl_T *match)
490{
491 return match == compl_first_match;
492}
493
494/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100495 * Return TRUE when character "c" is part of the item currently being
496 * completed. Used to decide whether to abandon complete mode when the menu
497 * is visible.
498 */
499 int
500ins_compl_accept_char(int c)
501{
502 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
503 // When expanding an identifier only accept identifier chars.
504 return vim_isIDc(c);
505
506 switch (ctrl_x_mode)
507 {
508 case CTRL_X_FILES:
509 // When expanding file name only accept file name chars. But not
510 // path separators, so that "proto/<Tab>" expands files in
511 // "proto", not "proto/" as a whole
512 return vim_isfilec(c) && !vim_ispathsep(c);
513
514 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200515 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100516 case CTRL_X_OMNI:
517 // Command line and Omni completion can work with just about any
518 // printable character, but do stop at white space.
519 return vim_isprintc(c) && !VIM_ISWHITE(c);
520
521 case CTRL_X_WHOLE_LINE:
522 // For while line completion a space can be part of the line.
523 return vim_isprintc(c);
524 }
525 return vim_iswordc(c);
526}
527
528/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000529 * Get the completed text by inferring the case of the originally typed text.
530 */
531 static char_u *
532ins_compl_infercase_gettext(
533 char_u *str,
534 int actual_len,
535 int actual_compl_length,
536 int min_len)
537{
538 int *wca; // Wide character array.
539 char_u *p;
540 int i, c;
541 int has_lower = FALSE;
542 int was_letter = FALSE;
543
544 IObuff[0] = NUL;
545
546 // Allocate wide character array for the completion and fill it.
547 wca = ALLOC_MULT(int, actual_len);
548 if (wca == NULL)
549 return IObuff;
550
551 p = str;
552 for (i = 0; i < actual_len; ++i)
553 if (has_mbyte)
554 wca[i] = mb_ptr2char_adv(&p);
555 else
556 wca[i] = *(p++);
557
558 // Rule 1: Were any chars converted to lower?
559 p = compl_orig_text;
560 for (i = 0; i < min_len; ++i)
561 {
562 if (has_mbyte)
563 c = mb_ptr2char_adv(&p);
564 else
565 c = *(p++);
566 if (MB_ISLOWER(c))
567 {
568 has_lower = TRUE;
569 if (MB_ISUPPER(wca[i]))
570 {
571 // Rule 1 is satisfied.
572 for (i = actual_compl_length; i < actual_len; ++i)
573 wca[i] = MB_TOLOWER(wca[i]);
574 break;
575 }
576 }
577 }
578
579 // Rule 2: No lower case, 2nd consecutive letter converted to
580 // upper case.
581 if (!has_lower)
582 {
583 p = compl_orig_text;
584 for (i = 0; i < min_len; ++i)
585 {
586 if (has_mbyte)
587 c = mb_ptr2char_adv(&p);
588 else
589 c = *(p++);
590 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
591 {
592 // Rule 2 is satisfied.
593 for (i = actual_compl_length; i < actual_len; ++i)
594 wca[i] = MB_TOUPPER(wca[i]);
595 break;
596 }
597 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
598 }
599 }
600
601 // Copy the original case of the part we typed.
602 p = compl_orig_text;
603 for (i = 0; i < min_len; ++i)
604 {
605 if (has_mbyte)
606 c = mb_ptr2char_adv(&p);
607 else
608 c = *(p++);
609 if (MB_ISLOWER(c))
610 wca[i] = MB_TOLOWER(wca[i]);
611 else if (MB_ISUPPER(c))
612 wca[i] = MB_TOUPPER(wca[i]);
613 }
614
615 // Generate encoding specific output from wide character array.
616 // Multi-byte characters can occupy up to five bytes more than
617 // ASCII characters, and we also need one byte for NUL, so stay
618 // six bytes away from the edge of IObuff.
619 p = IObuff;
620 i = 0;
621 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
622 if (has_mbyte)
623 p += (*mb_char2bytes)(wca[i++], p);
624 else
625 *(p++) = wca[i++];
626 *p = NUL;
627
628 vim_free(wca);
629
630 return IObuff;
631}
632
633/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100634 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
635 * case of the originally typed text is used, and the case of the completed
636 * text is inferred, ie this tries to work out what case you probably wanted
637 * the rest of the word to be in -- webb
638 */
639 int
640ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200641 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100642 int len,
643 int icase,
644 char_u *fname,
645 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200646 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100647{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200648 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100649 char_u *p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100650 int actual_len; // Take multi-byte characters
651 int actual_compl_length; // into account.
652 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200653 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100654
655 if (p_ic && curbuf->b_p_inf && len > 0)
656 {
657 // Infer case of completed part.
658
659 // Find actual length of completion.
660 if (has_mbyte)
661 {
662 p = str;
663 actual_len = 0;
664 while (*p != NUL)
665 {
666 MB_PTR_ADV(p);
667 ++actual_len;
668 }
669 }
670 else
671 actual_len = len;
672
673 // Find actual length of original text.
674 if (has_mbyte)
675 {
676 p = compl_orig_text;
677 actual_compl_length = 0;
678 while (*p != NUL)
679 {
680 MB_PTR_ADV(p);
681 ++actual_compl_length;
682 }
683 }
684 else
685 actual_compl_length = compl_length;
686
687 // "actual_len" may be smaller than "actual_compl_length" when using
688 // thesaurus, only use the minimum when comparing.
689 min_len = actual_len < actual_compl_length
690 ? actual_len : actual_compl_length;
691
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000692 str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
693 min_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100694 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200695 if (cont_s_ipos)
696 flags |= CP_CONT_S_IPOS;
697 if (icase)
698 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200699
Bram Moolenaar08928322020-01-04 14:32:48 +0100700 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100701}
702
703/*
704 * Add a match to the list of matches.
705 * If the given string is already in the list of completions, then return
706 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
707 * maybe because alloc() returns NULL, then FAIL is returned.
708 */
709 static int
710ins_compl_add(
711 char_u *str,
712 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100713 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 char_u **cptext, // extra text for popup menu or NULL
715 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100716 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200717 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100718 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100719{
720 compl_T *match;
721 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200722 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100723
Bram Moolenaarceb06192021-04-04 15:05:22 +0200724 if (flags & CP_FAST)
725 fast_breakcheck();
726 else
727 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728 if (got_int)
729 return FAIL;
730 if (len < 0)
731 len = (int)STRLEN(str);
732
733 // If the same match is already present, don't add it.
734 if (compl_first_match != NULL && !adup)
735 {
736 match = compl_first_match;
737 do
738 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000739 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100740 && STRNCMP(match->cp_str, str, len) == 0
741 && match->cp_str[len] == NUL)
742 return NOTDONE;
743 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000744 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100745 }
746
747 // Remove any popup menu before changing the list of matches.
748 ins_compl_del_pum();
749
750 // Allocate a new match structure.
751 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200752 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100753 if (match == NULL)
754 return FAIL;
755 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200756 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100757 match->cp_number = 0;
758 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
759 {
760 vim_free(match);
761 return FAIL;
762 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100763
764 // match-fname is:
765 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200766 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100767 // - NULL otherwise. --Acevedo
768 if (fname != NULL
769 && compl_curr_match != NULL
770 && compl_curr_match->cp_fname != NULL
771 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
772 match->cp_fname = compl_curr_match->cp_fname;
773 else if (fname != NULL)
774 {
775 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200776 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 }
778 else
779 match->cp_fname = NULL;
780 match->cp_flags = flags;
781
782 if (cptext != NULL)
783 {
784 int i;
785
786 for (i = 0; i < CPT_COUNT; ++i)
787 if (cptext[i] != NULL && *cptext[i] != NUL)
788 match->cp_text[i] = vim_strsave(cptext[i]);
789 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100790#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100791 if (user_data != NULL)
792 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100793#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100794
795 // Link the new match structure in the list of matches.
796 if (compl_first_match == NULL)
797 match->cp_next = match->cp_prev = NULL;
798 else if (dir == FORWARD)
799 {
800 match->cp_next = compl_curr_match->cp_next;
801 match->cp_prev = compl_curr_match;
802 }
803 else // BACKWARD
804 {
805 match->cp_next = compl_curr_match;
806 match->cp_prev = compl_curr_match->cp_prev;
807 }
808 if (match->cp_next)
809 match->cp_next->cp_prev = match;
810 if (match->cp_prev)
811 match->cp_prev->cp_next = match;
812 else // if there's nothing before, it is the first match
813 compl_first_match = match;
814 compl_curr_match = match;
815
816 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200817 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100818 ins_compl_longest_match(match);
819
820 return OK;
821}
822
823/*
824 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200825 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100826 */
827 static int
828ins_compl_equal(compl_T *match, char_u *str, int len)
829{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200830 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200831 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200832 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100833 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
834 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
835}
836
837/*
838 * Reduce the longest common string for match "match".
839 */
840 static void
841ins_compl_longest_match(compl_T *match)
842{
843 char_u *p, *s;
844 int c1, c2;
845 int had_match;
846
847 if (compl_leader == NULL)
848 {
849 // First match, use it as a whole.
850 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000851 if (compl_leader == NULL)
852 return;
853
854 had_match = (curwin->w_cursor.col > compl_col);
855 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000856 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000857 ins_redraw(FALSE);
858
859 // When the match isn't there (to avoid matching itself) remove it
860 // again after redrawing.
861 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100863 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000864
865 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100866 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000867
868 // Reduce the text if this match differs from compl_leader.
869 p = compl_leader;
870 s = match->cp_str;
871 while (*p != NUL)
872 {
873 if (has_mbyte)
874 {
875 c1 = mb_ptr2char(p);
876 c2 = mb_ptr2char(s);
877 }
878 else
879 {
880 c1 = *p;
881 c2 = *s;
882 }
883 if ((match->cp_flags & CP_ICASE)
884 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
885 break;
886 if (has_mbyte)
887 {
888 MB_PTR_ADV(p);
889 MB_PTR_ADV(s);
890 }
891 else
892 {
893 ++p;
894 ++s;
895 }
896 }
897
898 if (*p != NUL)
899 {
900 // Leader was shortened, need to change the inserted text.
901 *p = NUL;
902 had_match = (curwin->w_cursor.col > compl_col);
903 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000904 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000905 ins_redraw(FALSE);
906
907 // When the match isn't there (to avoid matching itself) remove it
908 // again after redrawing.
909 if (!had_match)
910 ins_compl_delete();
911 }
912
913 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100914}
915
916/*
917 * Add an array of matches to the list of matches.
918 * Frees matches[].
919 */
920 static void
921ins_compl_add_matches(
922 int num_matches,
923 char_u **matches,
924 int icase)
925{
926 int i;
927 int add_r = OK;
928 int dir = compl_direction;
929
930 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100931 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200932 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100933 // if dir was BACKWARD then honor it just once
934 dir = FORWARD;
935 FreeWild(num_matches, matches);
936}
937
938/*
939 * Make the completion list cyclic.
940 * Return the number of matches (excluding the original).
941 */
942 static int
943ins_compl_make_cyclic(void)
944{
945 compl_T *match;
946 int count = 0;
947
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000948 if (compl_first_match == NULL)
949 return 0;
950
951 // Find the end of the list.
952 match = compl_first_match;
953 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000954 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100955 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000956 match = match->cp_next;
957 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100958 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000959 match->cp_next = compl_first_match;
960 compl_first_match->cp_prev = match;
961
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100962 return count;
963}
964
965/*
966 * Return whether there currently is a shown match.
967 */
968 int
969ins_compl_has_shown_match(void)
970{
971 return compl_shown_match == NULL
972 || compl_shown_match != compl_shown_match->cp_next;
973}
974
975/*
976 * Return whether the shown match is long enough.
977 */
978 int
979ins_compl_long_shown_match(void)
980{
981 return (int)STRLEN(compl_shown_match->cp_str)
982 > curwin->w_cursor.col - compl_col;
983}
984
985/*
986 * Set variables that store noselect and noinsert behavior from the
987 * 'completeopt' value.
988 */
989 void
990completeopt_was_set(void)
991{
992 compl_no_insert = FALSE;
993 compl_no_select = FALSE;
994 if (strstr((char *)p_cot, "noselect") != NULL)
995 compl_no_select = TRUE;
996 if (strstr((char *)p_cot, "noinsert") != NULL)
997 compl_no_insert = TRUE;
998}
999
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001000
1001// "compl_match_array" points the currently displayed list of entries in the
1002// popup menu. It is NULL when there is no popup menu.
1003static pumitem_T *compl_match_array = NULL;
1004static int compl_match_arraysize;
1005
1006/*
1007 * Update the screen and when there is any scrolling remove the popup menu.
1008 */
1009 static void
1010ins_compl_upd_pum(void)
1011{
1012 int h;
1013
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001014 if (compl_match_array == NULL)
1015 return;
1016
1017 h = curwin->w_cline_height;
1018 // Update the screen later, before drawing the popup menu over it.
1019 pum_call_update_screen();
1020 if (h != curwin->w_cline_height)
1021 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001022}
1023
1024/*
1025 * Remove any popup menu.
1026 */
1027 static void
1028ins_compl_del_pum(void)
1029{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001030 if (compl_match_array == NULL)
1031 return;
1032
1033 pum_undisplay();
1034 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001035}
1036
1037/*
1038 * Return TRUE if the popup menu should be displayed.
1039 */
1040 int
1041pum_wanted(void)
1042{
1043 // 'completeopt' must contain "menu" or "menuone"
1044 if (vim_strchr(p_cot, 'm') == NULL)
1045 return FALSE;
1046
1047 // The display looks bad on a B&W display.
1048 if (t_colors < 8
1049#ifdef FEAT_GUI
1050 && !gui.in_use
1051#endif
1052 )
1053 return FALSE;
1054 return TRUE;
1055}
1056
1057/*
1058 * Return TRUE if there are two or more matches to be shown in the popup menu.
1059 * One if 'completopt' contains "menuone".
1060 */
1061 static int
1062pum_enough_matches(void)
1063{
1064 compl_T *compl;
1065 int i;
1066
1067 // Don't display the popup menu if there are no matches or there is only
1068 // one (ignoring the original text).
1069 compl = compl_first_match;
1070 i = 0;
1071 do
1072 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001073 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001074 break;
1075 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001076 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001077
1078 if (strstr((char *)p_cot, "menuone") != NULL)
1079 return (i >= 1);
1080 return (i >= 2);
1081}
1082
Bram Moolenaar3075a452021-11-17 15:51:52 +00001083#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001084/*
1085 * Allocate Dict for the completed item.
1086 * { word, abbr, menu, kind, info }
1087 */
1088 static dict_T *
1089ins_compl_dict_alloc(compl_T *match)
1090{
1091 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1092
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001093 if (dict == NULL)
1094 return NULL;
1095
1096 dict_add_string(dict, "word", match->cp_str);
1097 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1098 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1099 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1100 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1101 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1102 dict_add_string(dict, "user_data", (char_u *)"");
1103 else
1104 dict_add_tv(dict, "user_data", &match->cp_user_data);
1105
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001106 return dict;
1107}
1108
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001109/*
1110 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1111 * completion menu is changed.
1112 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001113 static void
1114trigger_complete_changed_event(int cur)
1115{
1116 dict_T *v_event;
1117 dict_T *item;
1118 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001119 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001120
1121 if (recursive)
1122 return;
1123
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001124 if (cur < 0)
1125 item = dict_alloc();
1126 else
1127 item = ins_compl_dict_alloc(compl_curr_match);
1128 if (item == NULL)
1129 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001130 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001131 dict_add_dict(v_event, "completed_item", item);
1132 pum_set_event_info(v_event);
1133 dict_set_items_ro(v_event);
1134
1135 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001136 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001137 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001138 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001139 recursive = FALSE;
1140
Bram Moolenaar3075a452021-11-17 15:51:52 +00001141 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001142}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001143#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001144
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001145/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001146 * Build a popup menu to show the completion matches.
1147 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1148 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001149 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001150 static int
1151ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001152{
1153 compl_T *compl;
1154 compl_T *shown_compl = NULL;
1155 int did_find_shown_match = FALSE;
1156 int shown_match_ok = FALSE;
1157 int i;
1158 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001159 int lead_len = 0;
1160
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001161 // Need to build the popup menu list.
1162 compl_match_arraysize = 0;
1163 compl = compl_first_match;
1164 if (compl_leader != NULL)
1165 lead_len = (int)STRLEN(compl_leader);
1166
1167 do
1168 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001169 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001170 && (compl_leader == NULL
1171 || ins_compl_equal(compl, compl_leader, lead_len)))
1172 ++compl_match_arraysize;
1173 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001174 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001175
1176 if (compl_match_arraysize == 0)
1177 return -1;
1178
1179 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1180 if (compl_match_array == NULL)
1181 return -1;
1182
1183 // If the current match is the original text don't find the first
1184 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001185 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001186 shown_match_ok = TRUE;
1187
1188 i = 0;
1189 compl = compl_first_match;
1190 do
1191 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001192 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001193 && (compl_leader == NULL
1194 || ins_compl_equal(compl, compl_leader, lead_len)))
1195 {
1196 if (!shown_match_ok)
1197 {
1198 if (compl == compl_shown_match || did_find_shown_match)
1199 {
1200 // This item is the shown match or this is the
1201 // first displayed item after the shown match.
1202 compl_shown_match = compl;
1203 did_find_shown_match = TRUE;
1204 shown_match_ok = TRUE;
1205 }
1206 else
1207 // Remember this displayed match for when the
1208 // shown match is just below it.
1209 shown_compl = compl;
1210 cur = i;
1211 }
1212
1213 if (compl->cp_text[CPT_ABBR] != NULL)
1214 compl_match_array[i].pum_text =
1215 compl->cp_text[CPT_ABBR];
1216 else
1217 compl_match_array[i].pum_text = compl->cp_str;
1218 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1219 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1220 if (compl->cp_text[CPT_MENU] != NULL)
1221 compl_match_array[i++].pum_extra =
1222 compl->cp_text[CPT_MENU];
1223 else
1224 compl_match_array[i++].pum_extra = compl->cp_fname;
1225 }
1226
1227 if (compl == compl_shown_match)
1228 {
1229 did_find_shown_match = TRUE;
1230
1231 // When the original text is the shown match don't set
1232 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001233 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001234 shown_match_ok = TRUE;
1235
1236 if (!shown_match_ok && shown_compl != NULL)
1237 {
1238 // The shown match isn't displayed, set it to the
1239 // previously displayed match.
1240 compl_shown_match = shown_compl;
1241 shown_match_ok = TRUE;
1242 }
1243 }
1244 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001245 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001246
1247 if (!shown_match_ok) // no displayed match at all
1248 cur = -1;
1249
1250 return cur;
1251}
1252
1253/*
1254 * Show the popup menu for the list of matches.
1255 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1256 */
1257 void
1258ins_compl_show_pum(void)
1259{
1260 int i;
1261 int cur = -1;
1262 colnr_T col;
1263
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001264 if (!pum_wanted() || !pum_enough_matches())
1265 return;
1266
1267#if defined(FEAT_EVAL)
1268 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001269 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001270#endif
1271
1272 // Update the screen later, before drawing the popup menu over it.
1273 pum_call_update_screen();
1274
1275 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001276 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001277 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001278 else
1279 {
1280 // popup menu already exists, only need to find the current item.
1281 for (i = 0; i < compl_match_arraysize; ++i)
1282 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1283 || compl_match_array[i].pum_text
1284 == compl_shown_match->cp_text[CPT_ABBR])
1285 {
1286 cur = i;
1287 break;
1288 }
1289 }
1290
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001291 if (compl_match_array == NULL)
1292 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001293
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001294 // In Replace mode when a $ is displayed at the end of the line only
1295 // part of the screen would be updated. We do need to redraw here.
1296 dollar_vcol = -1;
1297
1298 // Compute the screen column of the start of the completed text.
1299 // Use the cursor to get all wrapping and other settings right.
1300 col = curwin->w_cursor.col;
1301 curwin->w_cursor.col = compl_col;
1302 pum_display(compl_match_array, compl_match_arraysize, cur);
1303 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001304
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001305#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001306 if (has_completechanged())
1307 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001308#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001309}
1310
1311#define DICT_FIRST (1) // use just first element in "dict"
1312#define DICT_EXACT (2) // "dict" is the exact name of a file
1313
1314/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001315 * Add any identifiers that match the given pattern "pat" in the list of
1316 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001317 */
1318 static void
1319ins_compl_dictionaries(
1320 char_u *dict_start,
1321 char_u *pat,
1322 int flags, // DICT_FIRST and/or DICT_EXACT
1323 int thesaurus) // Thesaurus completion
1324{
1325 char_u *dict = dict_start;
1326 char_u *ptr;
1327 char_u *buf;
1328 regmatch_T regmatch;
1329 char_u **files;
1330 int count;
1331 int save_p_scs;
1332 int dir = compl_direction;
1333
1334 if (*dict == NUL)
1335 {
1336#ifdef FEAT_SPELL
1337 // When 'dictionary' is empty and spell checking is enabled use
1338 // "spell".
1339 if (!thesaurus && curwin->w_p_spell)
1340 dict = (char_u *)"spell";
1341 else
1342#endif
1343 return;
1344 }
1345
1346 buf = alloc(LSIZE);
1347 if (buf == NULL)
1348 return;
1349 regmatch.regprog = NULL; // so that we can goto theend
1350
1351 // If 'infercase' is set, don't use 'smartcase' here
1352 save_p_scs = p_scs;
1353 if (curbuf->b_p_inf)
1354 p_scs = FALSE;
1355
1356 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1357 // to only match at the start of a line. Otherwise just match the
1358 // pattern. Also need to double backslashes.
1359 if (ctrl_x_mode_line_or_eval())
1360 {
1361 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1362 size_t len;
1363
1364 if (pat_esc == NULL)
1365 goto theend;
1366 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001367 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001368 if (ptr == NULL)
1369 {
1370 vim_free(pat_esc);
1371 goto theend;
1372 }
1373 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1374 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1375 vim_free(pat_esc);
1376 vim_free(ptr);
1377 }
1378 else
1379 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001380 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001381 if (regmatch.regprog == NULL)
1382 goto theend;
1383 }
1384
1385 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1386 regmatch.rm_ic = ignorecase(pat);
1387 while (*dict != NUL && !got_int && !compl_interrupted)
1388 {
1389 // copy one dictionary file name into buf
1390 if (flags == DICT_EXACT)
1391 {
1392 count = 1;
1393 files = &dict;
1394 }
1395 else
1396 {
1397 // Expand wildcards in the dictionary name, but do not allow
1398 // backticks (for security, the 'dict' option may have been set in
1399 // a modeline).
1400 copy_option_part(&dict, buf, LSIZE, ",");
1401# ifdef FEAT_SPELL
1402 if (!thesaurus && STRCMP(buf, "spell") == 0)
1403 count = -1;
1404 else
1405# endif
1406 if (vim_strchr(buf, '`') != NULL
1407 || expand_wildcards(1, &buf, &count, &files,
1408 EW_FILE|EW_SILENT) != OK)
1409 count = 0;
1410 }
1411
1412# ifdef FEAT_SPELL
1413 if (count == -1)
1414 {
1415 // Complete from active spelling. Skip "\<" in the pattern, we
1416 // don't use it as a RE.
1417 if (pat[0] == '\\' && pat[1] == '<')
1418 ptr = pat + 2;
1419 else
1420 ptr = pat;
1421 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1422 }
1423 else
1424# endif
1425 if (count > 0) // avoid warning for using "files" uninit
1426 {
1427 ins_compl_files(count, files, thesaurus, flags,
1428 &regmatch, buf, &dir);
1429 if (flags != DICT_EXACT)
1430 FreeWild(count, files);
1431 }
1432 if (flags != 0)
1433 break;
1434 }
1435
1436theend:
1437 p_scs = save_p_scs;
1438 vim_regfree(regmatch.regprog);
1439 vim_free(buf);
1440}
1441
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001442/*
1443 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1444 * skipping the word at 'skip_word'. Returns OK on success.
1445 */
1446 static int
1447thesarurs_add_words_in_line(
1448 char_u *fname,
1449 char_u **buf_arg,
1450 int dir,
1451 char_u *skip_word)
1452{
1453 int status = OK;
1454 char_u *ptr;
1455 char_u *wstart;
1456
1457 // Add the other matches on the line
1458 ptr = *buf_arg;
1459 while (!got_int)
1460 {
1461 // Find start of the next word. Skip white
1462 // space and punctuation.
1463 ptr = find_word_start(ptr);
1464 if (*ptr == NUL || *ptr == NL)
1465 break;
1466 wstart = ptr;
1467
1468 // Find end of the word.
1469 if (has_mbyte)
1470 // Japanese words may have characters in
1471 // different classes, only separate words
1472 // with single-byte non-word characters.
1473 while (*ptr != NUL)
1474 {
1475 int l = (*mb_ptr2len)(ptr);
1476
1477 if (l < 2 && !vim_iswordc(*ptr))
1478 break;
1479 ptr += l;
1480 }
1481 else
1482 ptr = find_word_end(ptr);
1483
1484 // Add the word. Skip the regexp match.
1485 if (wstart != skip_word)
1486 {
1487 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1488 fname, dir, FALSE);
1489 if (status == FAIL)
1490 break;
1491 }
1492 }
1493
1494 *buf_arg = ptr;
1495 return status;
1496}
1497
1498/*
1499 * Process "count" dictionary/thesaurus "files" and add the text matching
1500 * "regmatch".
1501 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001502 static void
1503ins_compl_files(
1504 int count,
1505 char_u **files,
1506 int thesaurus,
1507 int flags,
1508 regmatch_T *regmatch,
1509 char_u *buf,
1510 int *dir)
1511{
1512 char_u *ptr;
1513 int i;
1514 FILE *fp;
1515 int add_r;
1516
1517 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1518 {
1519 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1520 if (flags != DICT_EXACT)
1521 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001522 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001523 vim_snprintf((char *)IObuff, IOSIZE,
1524 _("Scanning dictionary: %s"), (char *)files[i]);
1525 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1526 }
1527
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001528 if (fp == NULL)
1529 continue;
1530
1531 // Read dictionary file line by line.
1532 // Check each line for a match.
1533 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001534 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001535 ptr = buf;
1536 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001537 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001538 ptr = regmatch->startp[0];
1539 if (ctrl_x_mode_line_or_eval())
1540 ptr = find_line_end(ptr);
1541 else
1542 ptr = find_word_end(ptr);
1543 add_r = ins_compl_add_infercase(regmatch->startp[0],
1544 (int)(ptr - regmatch->startp[0]),
1545 p_ic, files[i], *dir, FALSE);
1546 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001547 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001548 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001549 ptr = buf;
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001550 add_r = thesarurs_add_words_in_line(files[i], &ptr, *dir,
1551 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001552 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001553 if (add_r == OK)
1554 // if dir was BACKWARD then honor it just once
1555 *dir = FORWARD;
1556 else if (add_r == FAIL)
1557 break;
1558 // avoid expensive call to vim_regexec() when at end
1559 // of line
1560 if (*ptr == '\n' || got_int)
1561 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001562 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001563 line_breakcheck();
1564 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001565 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001566 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001567 }
1568}
1569
1570/*
1571 * Find the start of the next word.
1572 * Returns a pointer to the first char of the word. Also stops at a NUL.
1573 */
1574 char_u *
1575find_word_start(char_u *ptr)
1576{
1577 if (has_mbyte)
1578 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1579 ptr += (*mb_ptr2len)(ptr);
1580 else
1581 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1582 ++ptr;
1583 return ptr;
1584}
1585
1586/*
1587 * Find the end of the word. Assumes it starts inside a word.
1588 * Returns a pointer to just after the word.
1589 */
1590 char_u *
1591find_word_end(char_u *ptr)
1592{
1593 int start_class;
1594
1595 if (has_mbyte)
1596 {
1597 start_class = mb_get_class(ptr);
1598 if (start_class > 1)
1599 while (*ptr != NUL)
1600 {
1601 ptr += (*mb_ptr2len)(ptr);
1602 if (mb_get_class(ptr) != start_class)
1603 break;
1604 }
1605 }
1606 else
1607 while (vim_iswordc(*ptr))
1608 ++ptr;
1609 return ptr;
1610}
1611
1612/*
1613 * Find the end of the line, omitting CR and NL at the end.
1614 * Returns a pointer to just after the line.
1615 */
1616 static char_u *
1617find_line_end(char_u *ptr)
1618{
1619 char_u *s;
1620
1621 s = ptr + STRLEN(ptr);
1622 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1623 --s;
1624 return s;
1625}
1626
1627/*
1628 * Free the list of completions
1629 */
1630 static void
1631ins_compl_free(void)
1632{
1633 compl_T *match;
1634 int i;
1635
1636 VIM_CLEAR(compl_pattern);
1637 VIM_CLEAR(compl_leader);
1638
1639 if (compl_first_match == NULL)
1640 return;
1641
1642 ins_compl_del_pum();
1643 pum_clear();
1644
1645 compl_curr_match = compl_first_match;
1646 do
1647 {
1648 match = compl_curr_match;
1649 compl_curr_match = compl_curr_match->cp_next;
1650 vim_free(match->cp_str);
1651 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001652 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001653 vim_free(match->cp_fname);
1654 for (i = 0; i < CPT_COUNT; ++i)
1655 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001656#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001657 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001658#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001659 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001660 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001661 compl_first_match = compl_curr_match = NULL;
1662 compl_shown_match = NULL;
1663 compl_old_match = NULL;
1664}
1665
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001666/*
1667 * Reset/clear the completion state.
1668 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001669 void
1670ins_compl_clear(void)
1671{
1672 compl_cont_status = 0;
1673 compl_started = FALSE;
1674 compl_matches = 0;
1675 VIM_CLEAR(compl_pattern);
1676 VIM_CLEAR(compl_leader);
1677 edit_submode_extra = NULL;
1678 VIM_CLEAR(compl_orig_text);
1679 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001680#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001681 // clear v:completed_item
1682 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001683#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001684}
1685
1686/*
1687 * Return TRUE when Insert completion is active.
1688 */
1689 int
1690ins_compl_active(void)
1691{
1692 return compl_started;
1693}
1694
1695/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001696 * Selected one of the matches. When FALSE the match was edited or using the
1697 * longest common string.
1698 */
1699 int
1700ins_compl_used_match(void)
1701{
1702 return compl_used_match;
1703}
1704
1705/*
1706 * Initialize get longest common string.
1707 */
1708 void
1709ins_compl_init_get_longest(void)
1710{
1711 compl_get_longest = FALSE;
1712}
1713
1714/*
1715 * Returns TRUE when insert completion is interrupted.
1716 */
1717 int
1718ins_compl_interrupted(void)
1719{
1720 return compl_interrupted;
1721}
1722
1723/*
1724 * Returns TRUE if the <Enter> key selects a match in the completion popup
1725 * menu.
1726 */
1727 int
1728ins_compl_enter_selects(void)
1729{
1730 return compl_enter_selects;
1731}
1732
1733/*
1734 * Return the column where the text starts that is being completed
1735 */
1736 colnr_T
1737ins_compl_col(void)
1738{
1739 return compl_col;
1740}
1741
1742/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001743 * Return the length in bytes of the text being completed
1744 */
1745 int
1746ins_compl_len(void)
1747{
1748 return compl_length;
1749}
1750
1751/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001752 * Delete one character before the cursor and show the subset of the matches
1753 * that match the word that is now before the cursor.
1754 * Returns the character to be used, NUL if the work is done and another char
1755 * to be got from the user.
1756 */
1757 int
1758ins_compl_bs(void)
1759{
1760 char_u *line;
1761 char_u *p;
1762
1763 line = ml_get_curline();
1764 p = line + curwin->w_cursor.col;
1765 MB_PTR_BACK(line, p);
1766
1767 // Stop completion when the whole word was deleted. For Omni completion
1768 // allow the word to be deleted, we won't match everything.
1769 // Respect the 'backspace' option.
1770 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001771 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1772 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001773 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1774 - compl_length < 0))
1775 return K_BS;
1776
1777 // Deleted more than what was used to find matches or didn't finish
1778 // finding all matches: need to look for matches all over again.
1779 if (curwin->w_cursor.col <= compl_col + compl_length
1780 || ins_compl_need_restart())
1781 ins_compl_restart();
1782
1783 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001784 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001785 if (compl_leader == NULL)
1786 return K_BS;
1787
1788 ins_compl_new_leader();
1789 if (compl_shown_match != NULL)
1790 // Make sure current match is not a hidden item.
1791 compl_curr_match = compl_shown_match;
1792 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001793}
1794
1795/*
1796 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1797 * be called.
1798 */
1799 static int
1800ins_compl_need_restart(void)
1801{
1802 // Return TRUE if we didn't complete finding matches or when the
1803 // 'completefunc' returned "always" in the "refresh" dictionary item.
1804 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001805 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001806 && compl_opt_refresh_always);
1807}
1808
1809/*
1810 * Called after changing "compl_leader".
1811 * Show the popup menu with a different set of matches.
1812 * May also search for matches again if the previous search was interrupted.
1813 */
1814 static void
1815ins_compl_new_leader(void)
1816{
1817 ins_compl_del_pum();
1818 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001819 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001820 compl_used_match = FALSE;
1821
1822 if (compl_started)
1823 ins_compl_set_original_text(compl_leader);
1824 else
1825 {
1826#ifdef FEAT_SPELL
1827 spell_bad_len = 0; // need to redetect bad word
1828#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001829 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001830 // the popup menu display the changed text before the cursor. Set
1831 // "compl_restarting" to avoid that the first match is inserted.
1832 pum_call_update_screen();
1833#ifdef FEAT_GUI
1834 if (gui.in_use)
1835 {
1836 // Show the cursor after the match, not after the redrawn text.
1837 setcursor();
1838 out_flush_cursor(FALSE, FALSE);
1839 }
1840#endif
1841 compl_restarting = TRUE;
1842 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1843 compl_cont_status = 0;
1844 compl_restarting = FALSE;
1845 }
1846
1847 compl_enter_selects = !compl_used_match;
1848
1849 // Show the popup menu with a different set of matches.
1850 ins_compl_show_pum();
1851
1852 // Don't let Enter select the original text when there is no popup menu.
1853 if (compl_match_array == NULL)
1854 compl_enter_selects = FALSE;
1855}
1856
1857/*
1858 * Return the length of the completion, from the completion start column to
1859 * the cursor column. Making sure it never goes below zero.
1860 */
1861 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001862get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001863{
1864 int off = (int)curwin->w_cursor.col - (int)compl_col;
1865
1866 if (off < 0)
1867 return 0;
1868 return off;
1869}
1870
1871/*
1872 * Append one character to the match leader. May reduce the number of
1873 * matches.
1874 */
1875 void
1876ins_compl_addleader(int c)
1877{
1878 int cc;
1879
1880 if (stop_arrow() == FAIL)
1881 return;
1882 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1883 {
1884 char_u buf[MB_MAXBYTES + 1];
1885
1886 (*mb_char2bytes)(c, buf);
1887 buf[cc] = NUL;
1888 ins_char_bytes(buf, cc);
1889 if (compl_opt_refresh_always)
1890 AppendToRedobuff(buf);
1891 }
1892 else
1893 {
1894 ins_char(c);
1895 if (compl_opt_refresh_always)
1896 AppendCharToRedobuff(c);
1897 }
1898
1899 // If we didn't complete finding matches we must search again.
1900 if (ins_compl_need_restart())
1901 ins_compl_restart();
1902
1903 // When 'always' is set, don't reset compl_leader. While completing,
1904 // cursor doesn't point original position, changing compl_leader would
1905 // break redo.
1906 if (!compl_opt_refresh_always)
1907 {
1908 vim_free(compl_leader);
1909 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001910 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001911 if (compl_leader != NULL)
1912 ins_compl_new_leader();
1913 }
1914}
1915
1916/*
1917 * Setup for finding completions again without leaving CTRL-X mode. Used when
1918 * BS or a key was typed while still searching for matches.
1919 */
1920 static void
1921ins_compl_restart(void)
1922{
1923 ins_compl_free();
1924 compl_started = FALSE;
1925 compl_matches = 0;
1926 compl_cont_status = 0;
1927 compl_cont_mode = 0;
1928}
1929
1930/*
1931 * Set the first match, the original text.
1932 */
1933 static void
1934ins_compl_set_original_text(char_u *str)
1935{
1936 char_u *p;
1937
1938 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001939 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
1940 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001941 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001942 {
1943 p = vim_strsave(str);
1944 if (p != NULL)
1945 {
1946 vim_free(compl_first_match->cp_str);
1947 compl_first_match->cp_str = p;
1948 }
1949 }
1950 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001951 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001952 {
1953 p = vim_strsave(str);
1954 if (p != NULL)
1955 {
1956 vim_free(compl_first_match->cp_prev->cp_str);
1957 compl_first_match->cp_prev->cp_str = p;
1958 }
1959 }
1960}
1961
1962/*
1963 * Append one character to the match leader. May reduce the number of
1964 * matches.
1965 */
1966 void
1967ins_compl_addfrommatch(void)
1968{
1969 char_u *p;
1970 int len = (int)curwin->w_cursor.col - (int)compl_col;
1971 int c;
1972 compl_T *cp;
1973
1974 p = compl_shown_match->cp_str;
1975 if ((int)STRLEN(p) <= len) // the match is too short
1976 {
1977 // When still at the original match use the first entry that matches
1978 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001979 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001980 return;
1981
1982 p = NULL;
1983 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001984 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001985 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001986 if (compl_leader == NULL
1987 || ins_compl_equal(cp, compl_leader,
1988 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001989 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001990 p = cp->cp_str;
1991 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001992 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001993 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001994 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001995 return;
1996 }
1997 p += len;
1998 c = PTR2CHAR(p);
1999 ins_compl_addleader(c);
2000}
2001
2002/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002003 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002004 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2005 * compl_cont_mode and compl_cont_status.
2006 * Returns TRUE when the character is not to be inserted.
2007 */
2008 static int
2009set_ctrl_x_mode(int c)
2010{
2011 int retval = FALSE;
2012
2013 switch (c)
2014 {
2015 case Ctrl_E:
2016 case Ctrl_Y:
2017 // scroll the window one line up or down
2018 ctrl_x_mode = CTRL_X_SCROLL;
2019 if (!(State & REPLACE_FLAG))
2020 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2021 else
2022 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2023 edit_submode_pre = NULL;
2024 showmode();
2025 break;
2026 case Ctrl_L:
2027 // complete whole line
2028 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2029 break;
2030 case Ctrl_F:
2031 // complete filenames
2032 ctrl_x_mode = CTRL_X_FILES;
2033 break;
2034 case Ctrl_K:
2035 // complete words from a dictinoary
2036 ctrl_x_mode = CTRL_X_DICTIONARY;
2037 break;
2038 case Ctrl_R:
2039 // Register insertion without exiting CTRL-X mode
2040 // Simply allow ^R to happen without affecting ^X mode
2041 break;
2042 case Ctrl_T:
2043 // complete words from a thesaurus
2044 ctrl_x_mode = CTRL_X_THESAURUS;
2045 break;
2046#ifdef FEAT_COMPL_FUNC
2047 case Ctrl_U:
2048 // user defined completion
2049 ctrl_x_mode = CTRL_X_FUNCTION;
2050 break;
2051 case Ctrl_O:
2052 // omni completion
2053 ctrl_x_mode = CTRL_X_OMNI;
2054 break;
2055#endif
2056 case 's':
2057 case Ctrl_S:
2058 // complete spelling suggestions
2059 ctrl_x_mode = CTRL_X_SPELL;
2060#ifdef FEAT_SPELL
2061 ++emsg_off; // Avoid getting the E756 error twice.
2062 spell_back_to_badword();
2063 --emsg_off;
2064#endif
2065 break;
2066 case Ctrl_RSB:
2067 // complete tag names
2068 ctrl_x_mode = CTRL_X_TAGS;
2069 break;
2070#ifdef FEAT_FIND_ID
2071 case Ctrl_I:
2072 case K_S_TAB:
2073 // complete keywords from included files
2074 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2075 break;
2076 case Ctrl_D:
2077 // complete definitions from included files
2078 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2079 break;
2080#endif
2081 case Ctrl_V:
2082 case Ctrl_Q:
2083 // complete vim commands
2084 ctrl_x_mode = CTRL_X_CMDLINE;
2085 break;
2086 case Ctrl_Z:
2087 // stop completion
2088 ctrl_x_mode = CTRL_X_NORMAL;
2089 edit_submode = NULL;
2090 showmode();
2091 retval = TRUE;
2092 break;
2093 case Ctrl_P:
2094 case Ctrl_N:
2095 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2096 // just started ^X mode, or there were enough ^X's to cancel
2097 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2098 // do normal expansion when interrupting a different mode (say
2099 // ^X^F^X^P or ^P^X^X^P, see below)
2100 // nothing changes if interrupting mode 0, (eg, the flag
2101 // doesn't change when going to ADDING mode -- Acevedo
2102 if (!(compl_cont_status & CONT_INTRPT))
2103 compl_cont_status |= CONT_LOCAL;
2104 else if (compl_cont_mode != 0)
2105 compl_cont_status &= ~CONT_LOCAL;
2106 // FALLTHROUGH
2107 default:
2108 // If we have typed at least 2 ^X's... for modes != 0, we set
2109 // compl_cont_status = 0 (eg, as if we had just started ^X
2110 // mode).
2111 // For mode 0, we set "compl_cont_mode" to an impossible
2112 // value, in both cases ^X^X can be used to restart the same
2113 // mode (avoiding ADDING mode).
2114 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2115 // 'complete' and local ^P expansions respectively.
2116 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2117 // mode -- Acevedo
2118 if (c == Ctrl_X)
2119 {
2120 if (compl_cont_mode != 0)
2121 compl_cont_status = 0;
2122 else
2123 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2124 }
2125 ctrl_x_mode = CTRL_X_NORMAL;
2126 edit_submode = NULL;
2127 showmode();
2128 break;
2129 }
2130
2131 return retval;
2132}
2133
2134/*
2135 * Stop insert completion mode
2136 */
2137 static int
2138ins_compl_stop(int c, int prev_mode, int retval)
2139{
2140 char_u *ptr;
2141#ifdef FEAT_CINDENT
2142 int want_cindent;
2143#endif
2144
2145 // Get here when we have finished typing a sequence of ^N and
2146 // ^P or other completion characters in CTRL-X mode. Free up
2147 // memory that was used, and make sure we can redo the insert.
2148 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2149 {
2150 // If any of the original typed text has been changed, eg when
2151 // ignorecase is set, we must add back-spaces to the redo
2152 // buffer. We add as few as necessary to delete just the part
2153 // of the original text that has changed.
2154 // When using the longest match, edited the match or used
2155 // CTRL-E then don't use the current match.
2156 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2157 ptr = compl_curr_match->cp_str;
2158 else
2159 ptr = NULL;
2160 ins_compl_fixRedoBufForLeader(ptr);
2161 }
2162
2163#ifdef FEAT_CINDENT
2164 want_cindent = (get_can_cindent() && cindent_on());
2165#endif
2166 // When completing whole lines: fix indent for 'cindent'.
2167 // Otherwise, break line if it's too long.
2168 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2169 {
2170#ifdef FEAT_CINDENT
2171 // re-indent the current line
2172 if (want_cindent)
2173 {
2174 do_c_expr_indent();
2175 want_cindent = FALSE; // don't do it again
2176 }
2177#endif
2178 }
2179 else
2180 {
2181 int prev_col = curwin->w_cursor.col;
2182
2183 // put the cursor on the last char, for 'tw' formatting
2184 if (prev_col > 0)
2185 dec_cursor();
2186 // only format when something was inserted
2187 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2188 insertchar(NUL, 0, -1);
2189 if (prev_col > 0
2190 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2191 inc_cursor();
2192 }
2193
2194 // If the popup menu is displayed pressing CTRL-Y means accepting
2195 // the selection without inserting anything. When
2196 // compl_enter_selects is set the Enter key does the same.
2197 if ((c == Ctrl_Y || (compl_enter_selects
2198 && (c == CAR || c == K_KENTER || c == NL)))
2199 && pum_visible())
2200 retval = TRUE;
2201
2202 // CTRL-E means completion is Ended, go back to the typed text.
2203 // but only do this, if the Popup is still visible
2204 if (c == Ctrl_E)
2205 {
2206 ins_compl_delete();
2207 if (compl_leader != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002208 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002209 else if (compl_first_match != NULL)
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002210 ins_bytes(compl_orig_text + get_compl_len());
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002211 retval = TRUE;
2212 }
2213
2214 auto_format(FALSE, TRUE);
2215
2216 // Trigger the CompleteDonePre event to give scripts a chance to
2217 // act upon the completion before clearing the info, and restore
2218 // ctrl_x_mode, so that complete_info() can be used.
2219 ctrl_x_mode = prev_mode;
2220 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2221
2222 ins_compl_free();
2223 compl_started = FALSE;
2224 compl_matches = 0;
2225 if (!shortmess(SHM_COMPLETIONMENU))
2226 msg_clr_cmdline(); // necessary for "noshowmode"
2227 ctrl_x_mode = CTRL_X_NORMAL;
2228 compl_enter_selects = FALSE;
2229 if (edit_submode != NULL)
2230 {
2231 edit_submode = NULL;
2232 showmode();
2233 }
2234
2235#ifdef FEAT_CMDWIN
2236 if (c == Ctrl_C && cmdwin_type != 0)
2237 // Avoid the popup menu remains displayed when leaving the
2238 // command line window.
2239 update_screen(0);
2240#endif
2241#ifdef FEAT_CINDENT
2242 // Indent now if a key was typed that is in 'cinkeys'.
2243 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2244 do_c_expr_indent();
2245#endif
2246 // Trigger the CompleteDone event to give scripts a chance to act
2247 // upon the end of completion.
2248 ins_apply_autocmds(EVENT_COMPLETEDONE);
2249
2250 return retval;
2251}
2252
2253/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002254 * Prepare for Insert mode completion, or stop it.
2255 * Called just after typing a character in Insert mode.
2256 * Returns TRUE when the character is not to be inserted;
2257 */
2258 int
2259ins_compl_prep(int c)
2260{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002261 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002262 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002263
2264 // Forget any previous 'special' messages if this is actually
2265 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2266 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2267 edit_submode_extra = NULL;
2268
2269 // Ignore end of Select mode mapping and mouse scroll buttons.
2270 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01002271 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002272 return retval;
2273
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002274#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002275 // Ignore mouse events in a popup window
2276 if (is_mouse_key(c))
2277 {
2278 // Ignore drag and release events, the position does not need to be in
2279 // the popup and it may have just closed.
2280 if (c == K_LEFTRELEASE
2281 || c == K_LEFTRELEASE_NM
2282 || c == K_MIDDLERELEASE
2283 || c == K_RIGHTRELEASE
2284 || c == K_X1RELEASE
2285 || c == K_X2RELEASE
2286 || c == K_LEFTDRAG
2287 || c == K_MIDDLEDRAG
2288 || c == K_RIGHTDRAG
2289 || c == K_X1DRAG
2290 || c == K_X2DRAG)
2291 return retval;
2292 if (popup_visible)
2293 {
2294 int row = mouse_row;
2295 int col = mouse_col;
2296 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2297
2298 if (wp != NULL && WIN_IS_POPUP(wp))
2299 return retval;
2300 }
2301 }
2302#endif
2303
zeertzjqdca29d92021-08-31 19:12:51 +02002304 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2305 {
2306 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2307 || !vim_is_ctrl_x_key(c))
2308 {
2309 // Not starting another completion mode.
2310 ctrl_x_mode = CTRL_X_CMDLINE;
2311
2312 // CTRL-X CTRL-Z should stop completion without inserting anything
2313 if (c == Ctrl_Z)
2314 retval = TRUE;
2315 }
2316 else
2317 {
2318 ctrl_x_mode = CTRL_X_CMDLINE;
2319
2320 // Other CTRL-X keys first stop completion, then start another
2321 // completion mode.
2322 ins_compl_prep(' ');
2323 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2324 }
2325 }
2326
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002327 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002328 if (ctrl_x_mode_not_defined_yet()
2329 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002330 {
2331 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
2332 compl_used_match = TRUE;
2333
2334 }
2335
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002336 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002337 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2338 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002339 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002340 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002341 {
2342 // We're already in CTRL-X mode, do we stay in it?
2343 if (!vim_is_ctrl_x_key(c))
2344 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002345 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002346 ctrl_x_mode = CTRL_X_NORMAL;
2347 else
2348 ctrl_x_mode = CTRL_X_FINISHED;
2349 edit_submode = NULL;
2350 }
2351 showmode();
2352 }
2353
2354 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2355 {
2356 // Show error message from attempted keyword completion (probably
2357 // 'Pattern not found') until another key is hit, then go back to
2358 // showing what mode we are in.
2359 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002360 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002361 && c != Ctrl_R && !ins_compl_pum_key(c))
2362 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002363 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002364 }
2365 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2366 // Trigger the CompleteDone event to give scripts a chance to act
2367 // upon the (possibly failed) completion.
2368 ins_apply_autocmds(EVENT_COMPLETEDONE);
2369
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002370 trigger_modechanged();
2371
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002372 // reset continue_* if we left expansion-mode, if we stay they'll be
2373 // (re)set properly in ins_complete()
2374 if (!vim_is_ctrl_x_key(c))
2375 {
2376 compl_cont_status = 0;
2377 compl_cont_mode = 0;
2378 }
2379
2380 return retval;
2381}
2382
2383/*
2384 * Fix the redo buffer for the completion leader replacing some of the typed
2385 * text. This inserts backspaces and appends the changed text.
2386 * "ptr" is the known leader text or NUL.
2387 */
2388 static void
2389ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2390{
2391 int len;
2392 char_u *p;
2393 char_u *ptr = ptr_arg;
2394
2395 if (ptr == NULL)
2396 {
2397 if (compl_leader != NULL)
2398 ptr = compl_leader;
2399 else
2400 return; // nothing to do
2401 }
2402 if (compl_orig_text != NULL)
2403 {
2404 p = compl_orig_text;
2405 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2406 ;
2407 if (len > 0)
2408 len -= (*mb_head_off)(p, p + len);
2409 for (p += len; *p != NUL; MB_PTR_ADV(p))
2410 AppendCharToRedobuff(K_BS);
2411 }
2412 else
2413 len = 0;
2414 if (ptr != NULL)
2415 AppendToRedobuffLit(ptr + len, -1);
2416}
2417
2418/*
2419 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2420 * (depending on flag) starting from buf and looking for a non-scanned
2421 * buffer (other than curbuf). curbuf is special, if it is called with
2422 * buf=curbuf then it has to be the first call for a given flag/expansion.
2423 *
2424 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2425 */
2426 static buf_T *
2427ins_compl_next_buf(buf_T *buf, int flag)
2428{
2429 static win_T *wp = NULL;
2430
2431 if (flag == 'w') // just windows
2432 {
2433 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2434 wp = curwin;
2435 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2436 && wp->w_buffer->b_scanned)
2437 ;
2438 buf = wp->w_buffer;
2439 }
2440 else
2441 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2442 // (unlisted buffers)
2443 // When completing whole lines skip unloaded buffers.
2444 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2445 && ((flag == 'U'
2446 ? buf->b_p_bl
2447 : (!buf->b_p_bl
2448 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2449 || buf->b_scanned))
2450 ;
2451 return buf;
2452}
2453
2454#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002455
2456# ifdef FEAT_EVAL
2457static callback_T cfu_cb; // 'completefunc' callback function
2458static callback_T ofu_cb; // 'omnifunc' callback function
2459static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2460# endif
2461
2462/*
2463 * Copy a global callback function to a buffer local callback.
2464 */
2465 static void
2466copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2467{
2468 free_callback(bufcb);
2469 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2470 copy_callback(bufcb, globcb);
2471}
2472
2473/*
2474 * Parse the 'completefunc' option value and set the callback function.
2475 * Invoked when the 'completefunc' option is set. The option value can be a
2476 * name of a function (string), or function(<name>) or funcref(<name>) or a
2477 * lambda expression.
2478 */
2479 int
2480set_completefunc_option(void)
2481{
2482 int retval;
2483
2484 retval = option_set_callback_func(curbuf->b_p_cfu, &cfu_cb);
2485 if (retval == OK)
2486 set_buflocal_cfu_callback(curbuf);
2487
2488 return retval;
2489}
2490
2491/*
2492 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002493 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002494 */
2495 void
2496set_buflocal_cfu_callback(buf_T *buf UNUSED)
2497{
2498# ifdef FEAT_EVAL
2499 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2500# endif
2501}
2502
2503/*
2504 * Parse the 'omnifunc' option value and set the callback function.
2505 * Invoked when the 'omnifunc' option is set. The option value can be a
2506 * name of a function (string), or function(<name>) or funcref(<name>) or a
2507 * lambda expression.
2508 */
2509 int
2510set_omnifunc_option(void)
2511{
2512 int retval;
2513
2514 retval = option_set_callback_func(curbuf->b_p_ofu, &ofu_cb);
2515 if (retval == OK)
2516 set_buflocal_ofu_callback(curbuf);
2517
2518 return retval;
2519}
2520
2521/*
2522 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002523 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002524 */
2525 void
2526set_buflocal_ofu_callback(buf_T *buf UNUSED)
2527{
2528# ifdef FEAT_EVAL
2529 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2530# endif
2531}
2532
2533/*
2534 * Parse the 'thesaurusfunc' option value and set the callback function.
2535 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2536 * name of a function (string), or function(<name>) or funcref(<name>) or a
2537 * lambda expression.
2538 */
2539 int
2540set_thesaurusfunc_option(void)
2541{
2542 int retval;
2543
2544 if (*curbuf->b_p_tsrfu != NUL)
2545 {
2546 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002547 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2548 &curbuf->b_tsrfu_cb);
2549 }
2550 else
2551 {
2552 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002553 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2554 }
2555
2556 return retval;
2557}
2558
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002559/*
2560 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002561 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002562 */
2563 int
2564set_ref_in_insexpand_funcs(int copyID)
2565{
2566 int abort = FALSE;
2567
2568 abort = set_ref_in_callback(&cfu_cb, copyID);
2569 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2570 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2571
2572 return abort;
2573}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002574
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002575/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002576 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002577 */
2578 static char_u *
2579get_complete_funcname(int type)
2580{
2581 switch (type)
2582 {
2583 case CTRL_X_FUNCTION:
2584 return curbuf->b_p_cfu;
2585 case CTRL_X_OMNI:
2586 return curbuf->b_p_ofu;
2587 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002588 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002589 default:
2590 return (char_u *)"";
2591 }
2592}
2593
2594/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002595 * Get the callback to use for insert mode completion.
2596 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002597 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002598get_insert_callback(int type)
2599{
2600 if (type == CTRL_X_FUNCTION)
2601 return &curbuf->b_cfu_cb;
2602 if (type == CTRL_X_OMNI)
2603 return &curbuf->b_ofu_cb;
2604 // CTRL_X_THESAURUS
2605 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2606}
2607
2608/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002609 * Execute user defined complete function 'completefunc', 'omnifunc' or
2610 * 'thesaurusfunc', and get matches in "matches".
2611 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002612 */
2613 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002614expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002615{
2616 list_T *matchlist = NULL;
2617 dict_T *matchdict = NULL;
2618 typval_T args[3];
2619 char_u *funcname;
2620 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002621 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002622 typval_T rettv;
2623 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002624 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002625
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002626 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002627 if (*funcname == NUL)
2628 return;
2629
2630 // Call 'completefunc' to obtain the list of matches.
2631 args[0].v_type = VAR_NUMBER;
2632 args[0].vval.v_number = 0;
2633 args[1].v_type = VAR_STRING;
2634 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2635 args[2].v_type = VAR_UNKNOWN;
2636
2637 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002638 // Lock the text to avoid weird things from happening. Also disallow
2639 // switching to another window, it should not be needed and may end up in
2640 // Insert mode in another buffer.
2641 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002642
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002643 cb = get_insert_callback(type);
2644 retval = call_callback(cb, 0, &rettv, 2, args);
2645
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002646 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002647 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002648 {
2649 switch (rettv.v_type)
2650 {
2651 case VAR_LIST:
2652 matchlist = rettv.vval.v_list;
2653 break;
2654 case VAR_DICT:
2655 matchdict = rettv.vval.v_dict;
2656 break;
2657 case VAR_SPECIAL:
2658 if (rettv.vval.v_number == VVAL_NONE)
2659 compl_opt_suppress_empty = TRUE;
2660 // FALLTHROUGH
2661 default:
2662 // TODO: Give error message?
2663 clear_tv(&rettv);
2664 break;
2665 }
2666 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002667 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002668
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002669 curwin->w_cursor = pos; // restore the cursor position
2670 validate_cursor();
2671 if (!EQUAL_POS(curwin->w_cursor, pos))
2672 {
2673 emsg(_(e_compldel));
2674 goto theend;
2675 }
2676
2677 if (matchlist != NULL)
2678 ins_compl_add_list(matchlist);
2679 else if (matchdict != NULL)
2680 ins_compl_add_dict(matchdict);
2681
2682theend:
2683 // Restore State, it might have been changed.
2684 State = save_State;
2685
2686 if (matchdict != NULL)
2687 dict_unref(matchdict);
2688 if (matchlist != NULL)
2689 list_unref(matchlist);
2690}
2691#endif // FEAT_COMPL_FUNC
2692
2693#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2694/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002695 * Add a match to the list of matches from a typeval_T.
2696 * If the given string is already in the list of completions, then return
2697 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2698 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002699 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002700 */
2701 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002702ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002703{
2704 char_u *word;
2705 int dup = FALSE;
2706 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002707 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002708 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002709 typval_T user_data;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002710
Bram Moolenaar08928322020-01-04 14:32:48 +01002711 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002712 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2713 {
2714 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2715 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2716 (char_u *)"abbr", FALSE);
2717 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2718 (char_u *)"menu", FALSE);
2719 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2720 (char_u *)"kind", FALSE);
2721 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2722 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002723 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002724 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2725 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2726 flags |= CP_ICASE;
2727 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2728 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2729 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2730 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2731 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2732 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2733 flags |= CP_EQUAL;
2734 }
2735 else
2736 {
2737 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002738 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002739 }
2740 if (word == NULL || (!empty && *word == NUL))
2741 return FAIL;
Bram Moolenaar08928322020-01-04 14:32:48 +01002742 return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002743}
2744
2745/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002746 * Add completions from a list.
2747 */
2748 static void
2749ins_compl_add_list(list_T *list)
2750{
2751 listitem_T *li;
2752 int dir = compl_direction;
2753
2754 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002755 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002756 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002757 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002758 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002759 // if dir was BACKWARD then honor it just once
2760 dir = FORWARD;
2761 else if (did_emsg)
2762 break;
2763 }
2764}
2765
2766/*
2767 * Add completions from a dict.
2768 */
2769 static void
2770ins_compl_add_dict(dict_T *dict)
2771{
2772 dictitem_T *di_refresh;
2773 dictitem_T *di_words;
2774
2775 // Check for optional "refresh" item.
2776 compl_opt_refresh_always = FALSE;
2777 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2778 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2779 {
2780 char_u *v = di_refresh->di_tv.vval.v_string;
2781
2782 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2783 compl_opt_refresh_always = TRUE;
2784 }
2785
2786 // Add completions from a "words" list.
2787 di_words = dict_find(dict, (char_u *)"words", 5);
2788 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2789 ins_compl_add_list(di_words->di_tv.vval.v_list);
2790}
2791
2792/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002793 * Start completion for the complete() function.
2794 * "startcol" is where the matched text starts (1 is first column).
2795 * "list" is the list of matches.
2796 */
2797 static void
2798set_completion(colnr_T startcol, list_T *list)
2799{
2800 int save_w_wrow = curwin->w_wrow;
2801 int save_w_leftcol = curwin->w_leftcol;
2802 int flags = CP_ORIGINAL_TEXT;
2803
2804 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002805 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002806 ins_compl_prep(' ');
2807 ins_compl_clear();
2808 ins_compl_free();
2809
2810 compl_direction = FORWARD;
2811 if (startcol > curwin->w_cursor.col)
2812 startcol = curwin->w_cursor.col;
2813 compl_col = startcol;
2814 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2815 // compl_pattern doesn't need to be set
2816 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2817 if (p_ic)
2818 flags |= CP_ICASE;
2819 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002820 -1, NULL, NULL, NULL, 0,
2821 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002822 return;
2823
2824 ctrl_x_mode = CTRL_X_EVAL;
2825
2826 ins_compl_add_list(list);
2827 compl_matches = ins_compl_make_cyclic();
2828 compl_started = TRUE;
2829 compl_used_match = TRUE;
2830 compl_cont_status = 0;
2831
2832 compl_curr_match = compl_first_match;
2833 if (compl_no_insert || compl_no_select)
2834 {
2835 ins_complete(K_DOWN, FALSE);
2836 if (compl_no_select)
2837 // Down/Up has no real effect.
2838 ins_complete(K_UP, FALSE);
2839 }
2840 else
2841 ins_complete(Ctrl_N, FALSE);
2842 compl_enter_selects = compl_no_insert;
2843
2844 // Lazily show the popup menu, unless we got interrupted.
2845 if (!compl_interrupted)
2846 show_pum(save_w_wrow, save_w_leftcol);
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002847 trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002848 out_flush();
2849}
2850
2851/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002852 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002853 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002854 void
2855f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002856{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002857 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002858 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002859
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002860 if (in_vim9script()
2861 && (check_for_number_arg(argvars, 0) == FAIL
2862 || check_for_list_arg(argvars, 1) == FAIL))
2863 return;
2864
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002865 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002866 {
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002867 emsg(_("E785: complete() can only be used in Insert mode"));
2868 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002869 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002870
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002871 // "textlock" is set when evaluating 'completefunc' but we can change
2872 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002873 textlock = 0;
2874
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002875 // Check for undo allowed here, because if something was already inserted
2876 // the line was already saved for undo and this check isn't done.
2877 if (!undo_allowed())
2878 return;
2879
2880 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002881 emsg(_(e_invalid_argument));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002882 else
2883 {
2884 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2885 if (startcol > 0)
2886 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002887 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002888 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002889}
2890
2891/*
2892 * "complete_add()" function
2893 */
2894 void
2895f_complete_add(typval_T *argvars, typval_T *rettv)
2896{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002897 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002898 return;
2899
Bram Moolenaar440cf092021-04-03 20:13:30 +02002900 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002901}
2902
2903/*
2904 * "complete_check()" function
2905 */
2906 void
2907f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2908{
2909 int saved = RedrawingDisabled;
2910
2911 RedrawingDisabled = 0;
2912 ins_compl_check_keys(0, TRUE);
2913 rettv->vval.v_number = ins_compl_interrupted();
2914 RedrawingDisabled = saved;
2915}
2916
2917/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002918 * Return Insert completion mode name string
2919 */
2920 static char_u *
2921ins_compl_mode(void)
2922{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002923 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002924 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2925
2926 return (char_u *)"";
2927}
2928
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00002929/*
2930 * Assign the sequence number to all the completion matches which don't have
2931 * one assigned yet.
2932 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002933 static void
2934ins_compl_update_sequence_numbers()
2935{
2936 int number = 0;
2937 compl_T *match;
2938
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002939 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002940 {
2941 // search backwards for the first valid (!= -1) number.
2942 // This should normally succeed already at the first loop
2943 // cycle, so it's fast!
2944 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002945 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002946 if (match->cp_number != -1)
2947 {
2948 number = match->cp_number;
2949 break;
2950 }
2951 if (match != NULL)
2952 // go up and assign all numbers which are not assigned
2953 // yet
2954 for (match = match->cp_next;
2955 match != NULL && match->cp_number == -1;
2956 match = match->cp_next)
2957 match->cp_number = ++number;
2958 }
2959 else // BACKWARD
2960 {
2961 // search forwards (upwards) for the first valid (!= -1)
2962 // number. This should normally succeed already at the
2963 // first loop cycle, so it's fast!
2964 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002965 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002966 if (match->cp_number != -1)
2967 {
2968 number = match->cp_number;
2969 break;
2970 }
2971 if (match != NULL)
2972 // go down and assign all numbers which are not
2973 // assigned yet
2974 for (match = match->cp_prev; match
2975 && match->cp_number == -1;
2976 match = match->cp_prev)
2977 match->cp_number = ++number;
2978 }
2979}
2980
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002981/*
2982 * Get complete information
2983 */
2984 static void
2985get_complete_info(list_T *what_list, dict_T *retdict)
2986{
2987 int ret = OK;
2988 listitem_T *item;
2989#define CI_WHAT_MODE 0x01
2990#define CI_WHAT_PUM_VISIBLE 0x02
2991#define CI_WHAT_ITEMS 0x04
2992#define CI_WHAT_SELECTED 0x08
2993#define CI_WHAT_INSERTED 0x10
2994#define CI_WHAT_ALL 0xff
2995 int what_flag;
2996
2997 if (what_list == NULL)
2998 what_flag = CI_WHAT_ALL;
2999 else
3000 {
3001 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003002 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003003 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003004 {
3005 char_u *what = tv_get_string(&item->li_tv);
3006
3007 if (STRCMP(what, "mode") == 0)
3008 what_flag |= CI_WHAT_MODE;
3009 else if (STRCMP(what, "pum_visible") == 0)
3010 what_flag |= CI_WHAT_PUM_VISIBLE;
3011 else if (STRCMP(what, "items") == 0)
3012 what_flag |= CI_WHAT_ITEMS;
3013 else if (STRCMP(what, "selected") == 0)
3014 what_flag |= CI_WHAT_SELECTED;
3015 else if (STRCMP(what, "inserted") == 0)
3016 what_flag |= CI_WHAT_INSERTED;
3017 }
3018 }
3019
3020 if (ret == OK && (what_flag & CI_WHAT_MODE))
3021 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3022
3023 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3024 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3025
3026 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3027 {
3028 list_T *li;
3029 dict_T *di;
3030 compl_T *match;
3031
3032 li = list_alloc();
3033 if (li == NULL)
3034 return;
3035 ret = dict_add_list(retdict, "items", li);
3036 if (ret == OK && compl_first_match != NULL)
3037 {
3038 match = compl_first_match;
3039 do
3040 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003041 if (!match_at_original_text(match))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003042 {
3043 di = dict_alloc();
3044 if (di == NULL)
3045 return;
3046 ret = list_append_dict(li, di);
3047 if (ret != OK)
3048 return;
3049 dict_add_string(di, "word", match->cp_str);
3050 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3051 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3052 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3053 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01003054 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3055 // Add an empty string for backwards compatibility
3056 dict_add_string(di, "user_data", (char_u *)"");
3057 else
3058 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003059 }
3060 match = match->cp_next;
3061 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003062 while (match != NULL && !is_first_match(match));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003063 }
3064 }
3065
3066 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003067 {
3068 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3069 ins_compl_update_sequence_numbers();
3070 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
3071 ? compl_curr_match->cp_number - 1 : -1);
3072 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003073
3074 // TODO
3075 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3076}
3077
3078/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003079 * "complete_info()" function
3080 */
3081 void
3082f_complete_info(typval_T *argvars, typval_T *rettv)
3083{
3084 list_T *what_list = NULL;
3085
3086 if (rettv_dict_alloc(rettv) != OK)
3087 return;
3088
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003089 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3090 return;
3091
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003092 if (argvars[0].v_type != VAR_UNKNOWN)
3093 {
3094 if (argvars[0].v_type != VAR_LIST)
3095 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003096 emsg(_(e_list_required));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003097 return;
3098 }
3099 what_list = argvars[0].vval.v_list;
3100 }
3101 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003102}
3103#endif
3104
3105/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003106 * Returns TRUE when using a user-defined function for thesaurus completion.
3107 */
3108 static int
3109thesaurus_func_complete(int type UNUSED)
3110{
3111#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003112 return type == CTRL_X_THESAURUS
3113 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003114#else
3115 return FALSE;
3116#endif
3117}
3118
3119/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003120 * Return value of process_next_cpt_value()
3121 */
3122enum
3123{
3124 INS_COMPL_CPT_OK = 1,
3125 INS_COMPL_CPT_CONT,
3126 INS_COMPL_CPT_END
3127};
3128
3129/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003130 * state information used for getting the next set of insert completion
3131 * matches.
3132 */
3133typedef struct
3134{
3135 char_u *e_cpt; // current entry in 'complete'
3136 buf_T *ins_buf; // buffer being scanned
3137 pos_T *cur_match_pos; // current match position
3138 pos_T prev_match_pos; // previous match position
3139 int set_match_pos; // save first_match_pos/last_match_pos
3140 pos_T first_match_pos; // first match position
3141 pos_T last_match_pos; // last match position
3142 int found_all; // found all matches of a certain type.
3143 char_u *dict; // dictionary file to search
3144 int dict_f; // "dict" is an exact file name or not
3145} ins_compl_next_state_T;
3146
3147/*
3148 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003149 *
3150 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003151 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003152 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003153 * st->found_all - all matches of this type are found
3154 * st->ins_buf - search for completions in this buffer
3155 * st->first_match_pos - position of the first completion match
3156 * st->last_match_pos - position of the last completion match
3157 * st->set_match_pos - TRUE if the first match position should be saved to
3158 * avoid loops after the search wraps around.
3159 * st->dict - name of the dictionary or thesaurus file to search
3160 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003161 *
3162 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
3163 * Returns INS_COMPL_CPT_CONT to skip the current value and process the next
3164 * option value.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003165 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003166 */
3167 static int
3168process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003169 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003170 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003171 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003172{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003173 int compl_type = -1;
3174 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003175
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003176 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003177
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003178 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3179 st->e_cpt++;
3180
3181 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003182 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003183 st->ins_buf = curbuf;
3184 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003185 // Move the cursor back one character so that ^N can match the
3186 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003187 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003188 {
3189 // Move the cursor to after the last character in the
3190 // buffer, so that word at start of buffer is found
3191 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003192 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
3193 st->first_match_pos.col =
3194 (colnr_T)STRLEN(ml_get(st->first_match_pos.lnum));
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003195 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003196 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003197 compl_type = 0;
3198
3199 // Remember the first match so that the loop stops when we
3200 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003201 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003202 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003203 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
3204 && (st->ins_buf = ins_compl_next_buf(st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003205 {
3206 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003207 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003208 {
3209 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003210 st->first_match_pos.col = st->last_match_pos.col = 0;
3211 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3212 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003213 compl_type = 0;
3214 }
3215 else // unloaded buffer, scan like dictionary
3216 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003217 st->found_all = TRUE;
3218 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003219 {
3220 status = INS_COMPL_CPT_CONT;
3221 goto done;
3222 }
3223 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003224 st->dict = st->ins_buf->b_fname;
3225 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003226 }
3227 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3228 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003229 st->ins_buf->b_fname == NULL
3230 ? buf_spname(st->ins_buf)
3231 : st->ins_buf->b_sfname == NULL
3232 ? st->ins_buf->b_fname
3233 : st->ins_buf->b_sfname);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003234 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3235 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003236 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003237 status = INS_COMPL_CPT_END;
3238 else
3239 {
3240 if (ctrl_x_mode_line_or_eval())
3241 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003242 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003244 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003245 compl_type = CTRL_X_DICTIONARY;
3246 else
3247 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003248 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003249 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003250 st->dict = st->e_cpt;
3251 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003252 }
3253 }
3254#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003255 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003256 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003257 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003258 compl_type = CTRL_X_PATH_DEFINES;
3259#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003260 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003261 {
3262 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3263 compl_type = CTRL_X_TAGS;
3264 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3265 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3266 }
3267 else
3268 compl_type = -1;
3269
3270 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003271 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003272
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003273 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 if (compl_type == -1)
3275 status = INS_COMPL_CPT_CONT;
3276 }
3277
3278done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003279 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003280 return status;
3281}
3282
3283#ifdef FEAT_FIND_ID
3284/*
3285 * Get the next set of identifiers or defines matching "compl_pattern" in
3286 * included files.
3287 */
3288 static void
3289get_next_include_file_completion(int compl_type)
3290{
3291 find_pattern_in_path(compl_pattern, compl_direction,
3292 (int)STRLEN(compl_pattern), FALSE, FALSE,
3293 (compl_type == CTRL_X_PATH_DEFINES
3294 && !(compl_cont_status & CONT_SOL))
3295 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3296 (linenr_T)1, (linenr_T)MAXLNUM);
3297}
3298#endif
3299
3300/*
3301 * Get the next set of words matching "compl_pattern" in dictionary or
3302 * thesaurus files.
3303 */
3304 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003305get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003306{
3307#ifdef FEAT_COMPL_FUNC
3308 if (thesaurus_func_complete(compl_type))
3309 expand_by_function(compl_type, compl_pattern);
3310 else
3311#endif
3312 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003313 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003314 : (compl_type == CTRL_X_THESAURUS
3315 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3316 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3317 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003318 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003319 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003320}
3321
3322/*
3323 * Get the next set of tag names matching "compl_pattern".
3324 */
3325 static void
3326get_next_tag_completion(void)
3327{
3328 int save_p_ic;
3329 char_u **matches;
3330 int num_matches;
3331
3332 // set p_ic according to p_ic, p_scs and pat for find_tags().
3333 save_p_ic = p_ic;
3334 p_ic = ignorecase(compl_pattern);
3335
3336 // Find up to TAG_MANY matches. Avoids that an enormous number
3337 // of matches is found when compl_pattern is empty
3338 g_tag_at_cursor = TRUE;
3339 if (find_tags(compl_pattern, &num_matches, &matches,
3340 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003341 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003342 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3343 ins_compl_add_matches(num_matches, matches, p_ic);
3344 g_tag_at_cursor = FALSE;
3345 p_ic = save_p_ic;
3346}
3347
3348/*
3349 * Get the next set of filename matching "compl_pattern".
3350 */
3351 static void
3352get_next_filename_completion(void)
3353{
3354 char_u **matches;
3355 int num_matches;
3356
3357 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3358 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3359 return;
3360
3361 // May change home directory back to "~".
3362 tilde_replace(compl_pattern, num_matches, matches);
3363#ifdef BACKSLASH_IN_FILENAME
3364 if (curbuf->b_p_csl[0] != NUL)
3365 {
3366 int i;
3367
3368 for (i = 0; i < num_matches; ++i)
3369 {
3370 char_u *ptr = matches[i];
3371
3372 while (*ptr != NUL)
3373 {
3374 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3375 *ptr = '/';
3376 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3377 *ptr = '\\';
3378 ptr += (*mb_ptr2len)(ptr);
3379 }
3380 }
3381 }
3382#endif
3383 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3384}
3385
3386/*
3387 * Get the next set of command-line completions matching "compl_pattern".
3388 */
3389 static void
3390get_next_cmdline_completion()
3391{
3392 char_u **matches;
3393 int num_matches;
3394
3395 if (expand_cmdline(&compl_xp, compl_pattern,
3396 (int)STRLEN(compl_pattern),
3397 &num_matches, &matches) == EXPAND_OK)
3398 ins_compl_add_matches(num_matches, matches, FALSE);
3399}
3400
3401/*
3402 * Get the next set of spell suggestions matching "compl_pattern".
3403 */
3404 static void
3405get_next_spell_completion(linenr_T lnum UNUSED)
3406{
3407#ifdef FEAT_SPELL
3408 char_u **matches;
3409 int num_matches;
3410
3411 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3412 if (num_matches > 0)
3413 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003414 else
3415 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003416#endif
3417}
3418
3419/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003420 * Return the next word or line from buffer "ins_buf" at position
3421 * "cur_match_pos" for completion. The length of the match is set in "len".
3422 */
3423 static char_u *
3424ins_comp_get_next_word_or_line(
3425 buf_T *ins_buf, // buffer being scanned
3426 pos_T *cur_match_pos, // current match position
3427 int *match_len,
3428 int *cont_s_ipos) // next ^X<> will set initial_pos
3429{
3430 char_u *ptr;
3431 int len;
3432
3433 *match_len = 0;
3434 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3435 cur_match_pos->col;
3436 if (ctrl_x_mode_line_or_eval())
3437 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003438 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003439 {
3440 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3441 return NULL;
3442 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3443 if (!p_paste)
3444 ptr = skipwhite(ptr);
3445 }
3446 len = (int)STRLEN(ptr);
3447 }
3448 else
3449 {
3450 char_u *tmp_ptr = ptr;
3451
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003452 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003453 {
3454 tmp_ptr += compl_length;
3455 // Skip if already inside a word.
3456 if (vim_iswordp(tmp_ptr))
3457 return NULL;
3458 // Find start of next word.
3459 tmp_ptr = find_word_start(tmp_ptr);
3460 }
3461 // Find end of this word.
3462 tmp_ptr = find_word_end(tmp_ptr);
3463 len = (int)(tmp_ptr - ptr);
3464
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003465 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003466 {
3467 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3468 {
3469 // Try next line, if any. the new word will be
3470 // "join" as if the normal command "J" was used.
3471 // IOSIZE is always greater than
3472 // compl_length, so the next STRNCPY always
3473 // works -- Acevedo
3474 STRNCPY(IObuff, ptr, len);
3475 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3476 tmp_ptr = ptr = skipwhite(ptr);
3477 // Find start of next word.
3478 tmp_ptr = find_word_start(tmp_ptr);
3479 // Find end of next word.
3480 tmp_ptr = find_word_end(tmp_ptr);
3481 if (tmp_ptr > ptr)
3482 {
3483 if (*ptr != ')' && IObuff[len - 1] != TAB)
3484 {
3485 if (IObuff[len - 1] != ' ')
3486 IObuff[len++] = ' ';
3487 // IObuf =~ "\k.* ", thus len >= 2
3488 if (p_js
3489 && (IObuff[len - 2] == '.'
3490 || (vim_strchr(p_cpo, CPO_JOINSP)
3491 == NULL
3492 && (IObuff[len - 2] == '?'
3493 || IObuff[len - 2] == '!'))))
3494 IObuff[len++] = ' ';
3495 }
3496 // copy as much as possible of the new word
3497 if (tmp_ptr - ptr >= IOSIZE - len)
3498 tmp_ptr = ptr + IOSIZE - len - 1;
3499 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3500 len += (int)(tmp_ptr - ptr);
3501 *cont_s_ipos = TRUE;
3502 }
3503 IObuff[len] = NUL;
3504 ptr = IObuff;
3505 }
3506 if (len == compl_length)
3507 return NULL;
3508 }
3509 }
3510
3511 *match_len = len;
3512 return ptr;
3513}
3514
3515/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003516 * Get the next set of words matching "compl_pattern" for default completion(s)
3517 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003518 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3519 * position "st->start_pos" in the "compl_direction" direction. If
3520 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3521 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003522 * Returns OK if a new next match is found, otherwise returns FAIL.
3523 */
3524 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003525get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003526{
3527 int found_new_match = FAIL;
3528 int save_p_scs;
3529 int save_p_ws;
3530 int looped_around = FALSE;
3531 char_u *ptr;
3532 int len;
3533
3534 // If 'infercase' is set, don't use 'smartcase' here
3535 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003536 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003537 p_scs = FALSE;
3538
3539 // Buffers other than curbuf are scanned from the beginning or the
3540 // end but never from the middle, thus setting nowrapscan in this
3541 // buffer is a good idea, on the other hand, we always set
3542 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3543 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003544 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003545 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003546 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003547 p_ws = TRUE;
3548 looped_around = FALSE;
3549 for (;;)
3550 {
3551 int cont_s_ipos = FALSE;
3552
3553 ++msg_silent; // Don't want messages for wrapscan.
3554
3555 // ctrl_x_mode_line_or_eval() || word-wise search that
3556 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003557 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003558 found_new_match = search_for_exact_line(st->ins_buf,
3559 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003560 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003561 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3562 NULL, compl_direction, compl_pattern, 1L,
3563 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003564 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003565 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003566 {
3567 // set "compl_started" even on fail
3568 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003569 st->first_match_pos = *st->cur_match_pos;
3570 st->last_match_pos = *st->cur_match_pos;
3571 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003572 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003573 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3574 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003575 {
3576 found_new_match = FAIL;
3577 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003578 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003579 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3580 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3581 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003582 {
3583 if (looped_around)
3584 found_new_match = FAIL;
3585 else
3586 looped_around = TRUE;
3587 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003588 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003589 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3590 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3591 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003592 {
3593 if (looped_around)
3594 found_new_match = FAIL;
3595 else
3596 looped_around = TRUE;
3597 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003598 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003599 if (found_new_match == FAIL)
3600 break;
3601
3602 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003603 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003604 && start_pos->lnum == st->cur_match_pos->lnum
3605 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003607
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003608 ptr = ins_comp_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3609 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003610 if (ptr == NULL)
3611 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003612
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003613 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003614 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003615 0, cont_s_ipos) != NOTDONE)
3616 {
3617 found_new_match = OK;
3618 break;
3619 }
3620 }
3621 p_scs = save_p_scs;
3622 p_ws = save_p_ws;
3623
3624 return found_new_match;
3625}
3626
3627/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003628 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003629 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003630 */
3631 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003632get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003633{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003634 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003635
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003636 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003637 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003638 case -1:
3639 break;
3640#ifdef FEAT_FIND_ID
3641 case CTRL_X_PATH_PATTERNS:
3642 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003643 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003644 break;
3645#endif
3646
3647 case CTRL_X_DICTIONARY:
3648 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003649 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3650 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003651 break;
3652
3653 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003654 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003655 break;
3656
3657 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003658 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003659 break;
3660
3661 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003662 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003663 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003664 break;
3665
3666#ifdef FEAT_COMPL_FUNC
3667 case CTRL_X_FUNCTION:
3668 case CTRL_X_OMNI:
3669 expand_by_function(type, compl_pattern);
3670 break;
3671#endif
3672
3673 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003674 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003675 break;
3676
3677 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003678 found_new_match = get_next_default_completion(st, ini);
3679 if (found_new_match == FAIL && st->ins_buf == curbuf)
3680 st->found_all = TRUE;
3681 }
3682
3683 // check if compl_curr_match has changed, (e.g. other type of
3684 // expansion added something)
3685 if (type != 0 && compl_curr_match != compl_old_match)
3686 found_new_match = OK;
3687
3688 return found_new_match;
3689}
3690
3691/*
3692 * Get the next expansion(s), using "compl_pattern".
3693 * The search starts at position "ini" in curbuf and in the direction
3694 * compl_direction.
3695 * When "compl_started" is FALSE start at that position, otherwise continue
3696 * where we stopped searching before.
3697 * This may return before finding all the matches.
3698 * Return the total number of matches or -1 if still unknown -- Acevedo
3699 */
3700 static int
3701ins_compl_get_exp(pos_T *ini)
3702{
3703 static ins_compl_next_state_T st;
3704 int i;
3705 int found_new_match;
3706 int type = ctrl_x_mode;
3707
3708 if (!compl_started)
3709 {
3710 FOR_ALL_BUFFERS(st.ins_buf)
3711 st.ins_buf->b_scanned = 0;
3712 st.found_all = FALSE;
3713 st.ins_buf = curbuf;
3714 st.e_cpt = (compl_cont_status & CONT_LOCAL)
3715 ? (char_u *)"." : curbuf->b_p_cpt;
3716 st.last_match_pos = st.first_match_pos = *ini;
3717 }
3718 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3719 st.ins_buf = curbuf; // In case the buffer was wiped out.
3720
3721 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003722 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003723 ? &st.last_match_pos : &st.first_match_pos;
3724
3725 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3726 for (;;)
3727 {
3728 found_new_match = FAIL;
3729 st.set_match_pos = FALSE;
3730
3731 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3732 // or if found_all says this entry is done. For ^X^L only use the
3733 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003734 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003735 && (!compl_started || st.found_all))
3736 {
3737 int status = process_next_cpt_value(&st, &type, ini);
3738
3739 if (status == INS_COMPL_CPT_END)
3740 break;
3741 if (status == INS_COMPL_CPT_CONT)
3742 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003743 }
3744
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003745 // If complete() was called then compl_pattern has been reset. The
3746 // following won't work then, bail out.
3747 if (compl_pattern == NULL)
3748 break;
3749
3750 // get the next set of completion matches
3751 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003752
3753 // break the loop for specialized modes (use 'complete' just for the
3754 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3755 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003756 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3757 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003758 {
3759 if (got_int)
3760 break;
3761 // Fill the popup menu as soon as possible.
3762 if (type != -1)
3763 ins_compl_check_keys(0, FALSE);
3764
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003765 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003766 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3767 break;
3768 compl_started = TRUE;
3769 }
3770 else
3771 {
3772 // Mark a buffer scanned when it has been scanned completely
3773 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003774 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003775
3776 compl_started = FALSE;
3777 }
3778 }
3779 compl_started = TRUE;
3780
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003781 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003782 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003783 found_new_match = FAIL;
3784
3785 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003786 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003787 && !ctrl_x_mode_line_or_eval()))
3788 i = ins_compl_make_cyclic();
3789
3790 if (compl_old_match != NULL)
3791 {
3792 // If several matches were added (FORWARD) or the search failed and has
3793 // just been made cyclic then we have to move compl_curr_match to the
3794 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003795 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003796 : compl_old_match->cp_prev;
3797 if (compl_curr_match == NULL)
3798 compl_curr_match = compl_old_match;
3799 }
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003800 trigger_modechanged();
3801
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003802 return i;
3803}
3804
3805/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003806 * Update "compl_shown_match" to the actually shown match, it may differ when
3807 * "compl_leader" is used to omit some of the matches.
3808 */
3809 static void
3810ins_compl_update_shown_match(void)
3811{
3812 while (!ins_compl_equal(compl_shown_match,
3813 compl_leader, (int)STRLEN(compl_leader))
3814 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003815 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003816 compl_shown_match = compl_shown_match->cp_next;
3817
3818 // If we didn't find it searching forward, and compl_shows_dir is
3819 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003820 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003821 && !ins_compl_equal(compl_shown_match,
3822 compl_leader, (int)STRLEN(compl_leader))
3823 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003824 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003825 {
3826 while (!ins_compl_equal(compl_shown_match,
3827 compl_leader, (int)STRLEN(compl_leader))
3828 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003829 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003830 compl_shown_match = compl_shown_match->cp_prev;
3831 }
3832}
3833
3834/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003835 * Delete the old text being completed.
3836 */
3837 void
3838ins_compl_delete(void)
3839{
3840 int col;
3841
3842 // In insert mode: Delete the typed part.
3843 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003844 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003845 if ((int)curwin->w_cursor.col > col)
3846 {
3847 if (stop_arrow() == FAIL)
3848 return;
3849 backspace_until_column(col);
3850 }
3851
3852 // TODO: is this sufficient for redrawing? Redrawing everything causes
3853 // flicker, thus we can't do that.
3854 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003855#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003856 // clear v:completed_item
3857 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003858#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003859}
3860
3861/*
3862 * Insert the new text being completed.
3863 * "in_compl_func" is TRUE when called from complete_check().
3864 */
3865 void
3866ins_compl_insert(int in_compl_func)
3867{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003868 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003869
3870 // Make sure we don't go over the end of the string, this can happen with
3871 // illegal bytes.
3872 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3873 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003874 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003875 compl_used_match = FALSE;
3876 else
3877 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003878#ifdef FEAT_EVAL
3879 {
3880 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3881
3882 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3883 }
3884#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003885 if (!in_compl_func)
3886 compl_curr_match = compl_shown_match;
3887}
3888
3889/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003890 * show the file name for the completion match (if any). Truncate the file
3891 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003892 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003893 static void
3894ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003895{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003896 char *lead = _("match in file");
3897 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3898 char_u *s;
3899 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003900
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003901 if (space <= 0)
3902 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003903
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003904 // We need the tail that fits. With double-byte encoding going
3905 // back from the end is very slow, thus go from the start and keep
3906 // the text that fits in "space" between "s" and "e".
3907 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003908 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003909 space -= ptr2cells(e);
3910 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003911 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003912 space += ptr2cells(s);
3913 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003914 }
3915 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003916 msg_hist_off = TRUE;
3917 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3918 s > compl_shown_match->cp_fname ? "<" : "", s);
3919 msg((char *)IObuff);
3920 msg_hist_off = FALSE;
3921 redraw_cmdline = FALSE; // don't overwrite!
3922}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003923
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003924/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003925 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003926 * times. The number of matches found is returned in 'num_matches'.
3927 *
3928 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3929 * get more completions. If it is FALSE, then do nothing when there are no more
3930 * completions in the given direction.
3931 *
3932 * If "advance" is TRUE, then completion will move to the first match.
3933 * Otherwise, the original text will be shown.
3934 *
3935 * Returns OK on success and -1 if the number of matches are unknown.
3936 */
3937 static int
3938find_next_completion_match(
3939 int allow_get_expansion,
3940 int todo, // repeat completion this many times
3941 int advance,
3942 int *num_matches)
3943{
3944 int found_end = FALSE;
3945 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003946
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003947 while (--todo >= 0)
3948 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003949 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003950 {
3951 compl_shown_match = compl_shown_match->cp_next;
3952 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003953 && (is_first_match(compl_shown_match->cp_next)
3954 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003955 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003956 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003957 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003958 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003959 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003960 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003961 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003962 }
3963 else
3964 {
3965 if (!allow_get_expansion)
3966 {
3967 if (advance)
3968 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003969 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003970 compl_pending -= todo + 1;
3971 else
3972 compl_pending += todo + 1;
3973 }
3974 return -1;
3975 }
3976
3977 if (!compl_no_select && advance)
3978 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003979 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003980 --compl_pending;
3981 else
3982 ++compl_pending;
3983 }
3984
3985 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003986 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003987
3988 // handle any pending completions
3989 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003990 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003991 {
3992 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3993 {
3994 compl_shown_match = compl_shown_match->cp_next;
3995 --compl_pending;
3996 }
3997 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3998 {
3999 compl_shown_match = compl_shown_match->cp_prev;
4000 ++compl_pending;
4001 }
4002 else
4003 break;
4004 }
4005 found_end = FALSE;
4006 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004007 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004008 && compl_leader != NULL
4009 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004010 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004011 ++todo;
4012 else
4013 // Remember a matching item.
4014 found_compl = compl_shown_match;
4015
4016 // Stop at the end of the list when we found a usable match.
4017 if (found_end)
4018 {
4019 if (found_compl != NULL)
4020 {
4021 compl_shown_match = found_compl;
4022 break;
4023 }
4024 todo = 1; // use first usable match after wrapping around
4025 }
4026 }
4027
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004028 return OK;
4029}
4030
4031/*
4032 * Fill in the next completion in the current direction.
4033 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4034 * get more completions. If it is FALSE, then we just do nothing when there
4035 * are no more completions in a given direction. The latter case is used when
4036 * we are still in the middle of finding completions, to allow browsing
4037 * through the ones found so far.
4038 * Return the total number of matches, or -1 if still unknown -- webb.
4039 *
4040 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4041 * compl_shown_match here.
4042 *
4043 * Note that this function may be called recursively once only. First with
4044 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4045 * calls this function with "allow_get_expansion" FALSE.
4046 */
4047 static int
4048ins_compl_next(
4049 int allow_get_expansion,
4050 int count, // repeat completion this many times; should
4051 // be at least 1
4052 int insert_match, // Insert the newly selected match
4053 int in_compl_func) // called from complete_check()
4054{
4055 int num_matches = -1;
4056 int todo = count;
4057 int advance;
4058 int started = compl_started;
4059
4060 // When user complete function return -1 for findstart which is next
4061 // time of 'always', compl_shown_match become NULL.
4062 if (compl_shown_match == NULL)
4063 return -1;
4064
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004065 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004066 // Update "compl_shown_match" to the actually shown match
4067 ins_compl_update_shown_match();
4068
4069 if (allow_get_expansion && insert_match
4070 && (!(compl_get_longest || compl_restarting) || compl_used_match))
4071 // Delete old text to be replaced
4072 ins_compl_delete();
4073
4074 // When finding the longest common text we stick at the original text,
4075 // don't let CTRL-N or CTRL-P move to the first match.
4076 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4077
4078 // When restarting the search don't insert the first match either.
4079 if (compl_restarting)
4080 {
4081 advance = FALSE;
4082 compl_restarting = FALSE;
4083 }
4084
4085 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4086 // around.
4087 if (find_next_completion_match(allow_get_expansion, todo, advance,
4088 &num_matches) == -1)
4089 return -1;
4090
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004091 // Insert the text of the new completion, or the compl_leader.
4092 if (compl_no_insert && !started)
4093 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004094 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004095 compl_used_match = FALSE;
4096 }
4097 else if (insert_match)
4098 {
4099 if (!compl_get_longest || compl_used_match)
4100 ins_compl_insert(in_compl_func);
4101 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004102 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004103 }
4104 else
4105 compl_used_match = FALSE;
4106
4107 if (!allow_get_expansion)
4108 {
4109 // may undisplay the popup menu first
4110 ins_compl_upd_pum();
4111
4112 if (pum_enough_matches())
4113 // Will display the popup menu, don't redraw yet to avoid flicker.
4114 pum_call_update_screen();
4115 else
4116 // Not showing the popup menu yet, redraw to show the user what was
4117 // inserted.
4118 update_screen(0);
4119
4120 // display the updated popup menu
4121 ins_compl_show_pum();
4122#ifdef FEAT_GUI
4123 if (gui.in_use)
4124 {
4125 // Show the cursor after the match, not after the redrawn text.
4126 setcursor();
4127 out_flush_cursor(FALSE, FALSE);
4128 }
4129#endif
4130
4131 // Delete old text to be replaced, since we're still searching and
4132 // don't want to match ourselves!
4133 ins_compl_delete();
4134 }
4135
4136 // Enter will select a match when the match wasn't inserted and the popup
4137 // menu is visible.
4138 if (compl_no_insert && !started)
4139 compl_enter_selects = TRUE;
4140 else
4141 compl_enter_selects = !insert_match && compl_match_array != NULL;
4142
4143 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004144 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004145 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004146
4147 return num_matches;
4148}
4149
4150/*
4151 * Call this while finding completions, to check whether the user has hit a key
4152 * that should change the currently displayed completion, or exit completion
4153 * mode. Also, when compl_pending is not zero, show a completion as soon as
4154 * possible. -- webb
4155 * "frequency" specifies out of how many calls we actually check.
4156 * "in_compl_func" is TRUE when called from complete_check(), don't set
4157 * compl_curr_match.
4158 */
4159 void
4160ins_compl_check_keys(int frequency, int in_compl_func)
4161{
4162 static int count = 0;
4163 int c;
4164
4165 // Don't check when reading keys from a script, :normal or feedkeys().
4166 // That would break the test scripts. But do check for keys when called
4167 // from complete_check().
4168 if (!in_compl_func && (using_script() || ex_normal_busy))
4169 return;
4170
4171 // Only do this at regular intervals
4172 if (++count < frequency)
4173 return;
4174 count = 0;
4175
4176 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4177 // can't do its work correctly.
4178 c = vpeekc_any();
4179 if (c != NUL)
4180 {
4181 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4182 {
4183 c = safe_vgetc(); // Eat the character
4184 compl_shows_dir = ins_compl_key2dir(c);
4185 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4186 c != K_UP && c != K_DOWN, in_compl_func);
4187 }
4188 else
4189 {
4190 // Need to get the character to have KeyTyped set. We'll put it
4191 // back with vungetc() below. But skip K_IGNORE.
4192 c = safe_vgetc();
4193 if (c != K_IGNORE)
4194 {
4195 // Don't interrupt completion when the character wasn't typed,
4196 // e.g., when doing @q to replay keys.
4197 if (c != Ctrl_R && KeyTyped)
4198 compl_interrupted = TRUE;
4199
4200 vungetc(c);
4201 }
4202 }
4203 }
4204 if (compl_pending != 0 && !got_int && !compl_no_insert)
4205 {
4206 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4207
4208 compl_pending = 0;
4209 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4210 }
4211}
4212
4213/*
4214 * Decide the direction of Insert mode complete from the key typed.
4215 * Returns BACKWARD or FORWARD.
4216 */
4217 static int
4218ins_compl_key2dir(int c)
4219{
4220 if (c == Ctrl_P || c == Ctrl_L
4221 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4222 return BACKWARD;
4223 return FORWARD;
4224}
4225
4226/*
4227 * Return TRUE for keys that are used for completion only when the popup menu
4228 * is visible.
4229 */
4230 static int
4231ins_compl_pum_key(int c)
4232{
4233 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4234 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4235 || c == K_UP || c == K_DOWN);
4236}
4237
4238/*
4239 * Decide the number of completions to move forward.
4240 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4241 */
4242 static int
4243ins_compl_key2count(int c)
4244{
4245 int h;
4246
4247 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4248 {
4249 h = pum_get_height();
4250 if (h > 3)
4251 h -= 2; // keep some context
4252 return h;
4253 }
4254 return 1;
4255}
4256
4257/*
4258 * Return TRUE if completion with "c" should insert the match, FALSE if only
4259 * to change the currently selected completion.
4260 */
4261 static int
4262ins_compl_use_match(int c)
4263{
4264 switch (c)
4265 {
4266 case K_UP:
4267 case K_DOWN:
4268 case K_PAGEDOWN:
4269 case K_KPAGEDOWN:
4270 case K_S_DOWN:
4271 case K_PAGEUP:
4272 case K_KPAGEUP:
4273 case K_S_UP:
4274 return FALSE;
4275 }
4276 return TRUE;
4277}
4278
4279/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004280 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4281 * completion)
4282 * Sets the global variables: compl_col, compl_length and compl_pattern.
4283 * Uses the global variables: compl_cont_status and ctrl_x_mode
4284 */
4285 static int
4286get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4287{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004288 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004289 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004290 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004291 {
4292 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4293 ;
4294 compl_col += ++startcol;
4295 compl_length = curs_col - startcol;
4296 }
4297 if (p_ic)
4298 compl_pattern = str_foldcase(line + compl_col,
4299 compl_length, NULL, 0);
4300 else
4301 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4302 if (compl_pattern == NULL)
4303 return FAIL;
4304 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004305 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004306 {
4307 char_u *prefix = (char_u *)"\\<";
4308
4309 // we need up to 2 extra chars for the prefix
4310 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4311 compl_length) + 2);
4312 if (compl_pattern == NULL)
4313 return FAIL;
4314 if (!vim_iswordp(line + compl_col)
4315 || (compl_col > 0
4316 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4317 prefix = (char_u *)"";
4318 STRCPY((char *)compl_pattern, prefix);
4319 (void)quote_meta(compl_pattern + STRLEN(prefix),
4320 line + compl_col, compl_length);
4321 }
4322 else if (--startcol < 0
4323 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4324 {
4325 // Match any word of at least two chars
4326 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4327 if (compl_pattern == NULL)
4328 return FAIL;
4329 compl_col += curs_col;
4330 compl_length = 0;
4331 }
4332 else
4333 {
4334 // Search the point of change class of multibyte character
4335 // or not a word single byte character backward.
4336 if (has_mbyte)
4337 {
4338 int base_class;
4339 int head_off;
4340
4341 startcol -= (*mb_head_off)(line, line + startcol);
4342 base_class = mb_get_class(line + startcol);
4343 while (--startcol >= 0)
4344 {
4345 head_off = (*mb_head_off)(line, line + startcol);
4346 if (base_class != mb_get_class(line + startcol
4347 - head_off))
4348 break;
4349 startcol -= head_off;
4350 }
4351 }
4352 else
4353 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4354 ;
4355 compl_col += ++startcol;
4356 compl_length = (int)curs_col - startcol;
4357 if (compl_length == 1)
4358 {
4359 // Only match word with at least two chars -- webb
4360 // there's no need to call quote_meta,
4361 // alloc(7) is enough -- Acevedo
4362 compl_pattern = alloc(7);
4363 if (compl_pattern == NULL)
4364 return FAIL;
4365 STRCPY((char *)compl_pattern, "\\<");
4366 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4367 STRCAT((char *)compl_pattern, "\\k");
4368 }
4369 else
4370 {
4371 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4372 compl_length) + 2);
4373 if (compl_pattern == NULL)
4374 return FAIL;
4375 STRCPY((char *)compl_pattern, "\\<");
4376 (void)quote_meta(compl_pattern + 2, line + compl_col,
4377 compl_length);
4378 }
4379 }
4380
4381 return OK;
4382}
4383
4384/*
4385 * Get the pattern, column and length for whole line completion or for the
4386 * complete() function.
4387 * Sets the global variables: compl_col, compl_length and compl_pattern.
4388 */
4389 static int
4390get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4391{
4392 compl_col = (colnr_T)getwhitecols(line);
4393 compl_length = (int)curs_col - (int)compl_col;
4394 if (compl_length < 0) // cursor in indent: empty pattern
4395 compl_length = 0;
4396 if (p_ic)
4397 compl_pattern = str_foldcase(line + compl_col, compl_length,
4398 NULL, 0);
4399 else
4400 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4401 if (compl_pattern == NULL)
4402 return FAIL;
4403
4404 return OK;
4405}
4406
4407/*
4408 * Get the pattern, column and length for filename completion.
4409 * Sets the global variables: compl_col, compl_length and compl_pattern.
4410 */
4411 static int
4412get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4413{
4414 // Go back to just before the first filename character.
4415 if (startcol > 0)
4416 {
4417 char_u *p = line + startcol;
4418
4419 MB_PTR_BACK(line, p);
4420 while (p > line && vim_isfilec(PTR2CHAR(p)))
4421 MB_PTR_BACK(line, p);
4422 if (p == line && vim_isfilec(PTR2CHAR(p)))
4423 startcol = 0;
4424 else
4425 startcol = (int)(p - line) + 1;
4426 }
4427
4428 compl_col += startcol;
4429 compl_length = (int)curs_col - startcol;
4430 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4431 if (compl_pattern == NULL)
4432 return FAIL;
4433
4434 return OK;
4435}
4436
4437/*
4438 * Get the pattern, column and length for command-line completion.
4439 * Sets the global variables: compl_col, compl_length and compl_pattern.
4440 */
4441 static int
4442get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4443{
4444 compl_pattern = vim_strnsave(line, curs_col);
4445 if (compl_pattern == NULL)
4446 return FAIL;
4447 set_cmd_context(&compl_xp, compl_pattern,
4448 (int)STRLEN(compl_pattern), curs_col, FALSE);
4449 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4450 || compl_xp.xp_context == EXPAND_NOTHING)
4451 // No completion possible, use an empty pattern to get a
4452 // "pattern not found" message.
4453 compl_col = curs_col;
4454 else
4455 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4456 compl_length = curs_col - compl_col;
4457
4458 return OK;
4459}
4460
4461/*
4462 * Get the pattern, column and length for user defined completion ('omnifunc',
4463 * 'completefunc' and 'thesaurusfunc')
4464 * Sets the global variables: compl_col, compl_length and compl_pattern.
4465 * Uses the global variable: spell_bad_len
4466 */
4467 static int
4468get_userdefined_compl_info(colnr_T curs_col UNUSED)
4469{
4470 int ret = FAIL;
4471
4472#ifdef FEAT_COMPL_FUNC
4473 // Call user defined function 'completefunc' with "a:findstart"
4474 // set to 1 to obtain the length of text to use for completion.
4475 char_u *line;
4476 typval_T args[3];
4477 int col;
4478 char_u *funcname;
4479 pos_T pos;
4480 int save_State = State;
4481 callback_T *cb;
4482
4483 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4484 // length as a string
4485 funcname = get_complete_funcname(ctrl_x_mode);
4486 if (*funcname == NUL)
4487 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004488 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004489 ? "completefunc" : "omnifunc");
4490 return FAIL;
4491 }
4492
4493 args[0].v_type = VAR_NUMBER;
4494 args[0].vval.v_number = 1;
4495 args[1].v_type = VAR_STRING;
4496 args[1].vval.v_string = (char_u *)"";
4497 args[2].v_type = VAR_UNKNOWN;
4498 pos = curwin->w_cursor;
4499 ++textwinlock;
4500 cb = get_insert_callback(ctrl_x_mode);
4501 col = call_callback_retnr(cb, 2, args);
4502 --textwinlock;
4503
4504 State = save_State;
4505 curwin->w_cursor = pos; // restore the cursor position
4506 validate_cursor();
4507 if (!EQUAL_POS(curwin->w_cursor, pos))
4508 {
4509 emsg(_(e_compldel));
4510 return FAIL;
4511 }
4512
4513 // Return value -2 means the user complete function wants to
4514 // cancel the complete without an error.
4515 // Return value -3 does the same as -2 and leaves CTRL-X mode.
4516 if (col == -2)
4517 return FAIL;
4518 if (col == -3)
4519 {
4520 ctrl_x_mode = CTRL_X_NORMAL;
4521 edit_submode = NULL;
4522 if (!shortmess(SHM_COMPLETIONMENU))
4523 msg_clr_cmdline();
4524 return FAIL;
4525 }
4526
4527 // Reset extended parameters of completion, when start new
4528 // completion.
4529 compl_opt_refresh_always = FALSE;
4530 compl_opt_suppress_empty = FALSE;
4531
4532 if (col < 0)
4533 col = curs_col;
4534 compl_col = col;
4535 if (compl_col > curs_col)
4536 compl_col = curs_col;
4537
4538 // Setup variables for completion. Need to obtain "line" again,
4539 // it may have become invalid.
4540 line = ml_get(curwin->w_cursor.lnum);
4541 compl_length = curs_col - compl_col;
4542 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4543 if (compl_pattern == NULL)
4544 return FAIL;
4545
4546 ret = OK;
4547#endif
4548
4549 return ret;
4550}
4551
4552/*
4553 * Get the pattern, column and length for spell completion.
4554 * Sets the global variables: compl_col, compl_length and compl_pattern.
4555 * Uses the global variable: spell_bad_len
4556 */
4557 static int
4558get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4559{
4560 int ret = FAIL;
4561#ifdef FEAT_SPELL
4562 char_u *line;
4563
4564 if (spell_bad_len > 0)
4565 compl_col = curs_col - spell_bad_len;
4566 else
4567 compl_col = spell_word_start(startcol);
4568 if (compl_col >= (colnr_T)startcol)
4569 {
4570 compl_length = 0;
4571 compl_col = curs_col;
4572 }
4573 else
4574 {
4575 spell_expand_check_cap(compl_col);
4576 compl_length = (int)curs_col - compl_col;
4577 }
4578 // Need to obtain "line" again, it may have become invalid.
4579 line = ml_get(curwin->w_cursor.lnum);
4580 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4581 if (compl_pattern == NULL)
4582 return FAIL;
4583
4584 ret = OK;
4585#endif
4586
4587 return ret;
4588}
4589
4590/*
4591 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004592 * "startcol" - start column number of the completion pattern/text
4593 * "cur_col" - current cursor column
4594 * On return, "line_invalid" is set to TRUE, if the current line may have
4595 * become invalid and needs to be fetched again.
4596 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004597 */
4598 static int
4599compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4600{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004601 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004602 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4603 && !thesaurus_func_complete(ctrl_x_mode)))
4604 {
4605 return get_normal_compl_info(line, startcol, curs_col);
4606 }
4607 else if (ctrl_x_mode_line_or_eval())
4608 {
4609 return get_wholeline_compl_info(line, curs_col);
4610 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004611 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004612 {
4613 return get_filename_compl_info(line, startcol, curs_col);
4614 }
4615 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4616 {
4617 return get_cmdline_compl_info(line, curs_col);
4618 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004619 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004620 || thesaurus_func_complete(ctrl_x_mode))
4621 {
4622 if (get_userdefined_compl_info(curs_col) == FAIL)
4623 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004624 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004625 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004626 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004627 {
4628 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4629 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004630 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004631 }
4632 else
4633 {
4634 internal_error("ins_complete()");
4635 return FAIL;
4636 }
4637
4638 return OK;
4639}
4640
4641/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004642 * Continue an interrupted completion mode search in "line".
4643 *
4644 * If this same ctrl_x_mode has been interrupted use the text from
4645 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4646 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4647 * the same line as the cursor then fix it (the line has been split because it
4648 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4649 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004650 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004651 static void
4652ins_compl_continue_search(char_u *line)
4653{
4654 // it is a continued search
4655 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004656 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4657 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004658 {
4659 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4660 {
4661 // line (probably) wrapped, set compl_startpos to the
4662 // first non_blank in the line, if it is not a wordchar
4663 // include it to get a better pattern, but then we don't
4664 // want the "\\<" prefix, check it below
4665 compl_col = (colnr_T)getwhitecols(line);
4666 compl_startpos.col = compl_col;
4667 compl_startpos.lnum = curwin->w_cursor.lnum;
4668 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4669 }
4670 else
4671 {
4672 // S_IPOS was set when we inserted a word that was at the
4673 // beginning of the line, which means that we'll go to SOL
4674 // mode but first we need to redefine compl_startpos
4675 if (compl_cont_status & CONT_S_IPOS)
4676 {
4677 compl_cont_status |= CONT_SOL;
4678 compl_startpos.col = (colnr_T)(skipwhite(
4679 line + compl_length
4680 + compl_startpos.col) - line);
4681 }
4682 compl_col = compl_startpos.col;
4683 }
4684 compl_length = curwin->w_cursor.col - (int)compl_col;
4685 // IObuff is used to add a "word from the next line" would we
4686 // have enough space? just being paranoid
4687#define MIN_SPACE 75
4688 if (compl_length > (IOSIZE - MIN_SPACE))
4689 {
4690 compl_cont_status &= ~CONT_SOL;
4691 compl_length = (IOSIZE - MIN_SPACE);
4692 compl_col = curwin->w_cursor.col - compl_length;
4693 }
4694 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4695 if (compl_length < 1)
4696 compl_cont_status &= CONT_LOCAL;
4697 }
4698 else if (ctrl_x_mode_line_or_eval())
4699 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4700 else
4701 compl_cont_status = 0;
4702}
4703
4704/*
4705 * start insert mode completion
4706 */
4707 static int
4708ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004709{
4710 char_u *line;
4711 int startcol = 0; // column where searched text starts
4712 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004713 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004714 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004715 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004716
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004717 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004718
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004719 did_ai = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004720#ifdef FEAT_SMARTINDENT
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004721 did_si = FALSE;
4722 can_si = FALSE;
4723 can_si_back = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004724#endif
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004725 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004726 return FAIL;
4727
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004728 line = ml_get(curwin->w_cursor.lnum);
4729 curs_col = curwin->w_cursor.col;
4730 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004731
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004732 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4733 && compl_cont_mode == ctrl_x_mode)
4734 // this same ctrl-x_mode was interrupted previously. Continue the
4735 // completion.
4736 ins_compl_continue_search(line);
4737 else
4738 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004739
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004740 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004741 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004742 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004743 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004744 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4745 compl_cont_status = 0;
4746 compl_cont_status |= CONT_N_ADDS;
4747 compl_startpos = curwin->w_cursor;
4748 startcol = (int)curs_col;
4749 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004750 }
4751
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004752 // Work out completion pattern and original text -- webb
4753 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4754 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004755 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4756 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004757 // restore did_ai, so that adding comment leader works
4758 did_ai = save_did_ai;
4759 return FAIL;
4760 }
4761 // If "line" was changed while getting completion info get it again.
4762 if (line_invalid)
4763 line = ml_get(curwin->w_cursor.lnum);
4764
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004765 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004766 {
4767 edit_submode_pre = (char_u *)_(" Adding");
4768 if (ctrl_x_mode_line_or_eval())
4769 {
4770 // Insert a new line, keep indentation but ignore 'comments'.
4771 char_u *old = curbuf->b_p_com;
4772
4773 curbuf->b_p_com = (char_u *)"";
4774 compl_startpos.lnum = curwin->w_cursor.lnum;
4775 compl_startpos.col = compl_col;
4776 ins_eol('\r');
4777 curbuf->b_p_com = old;
4778 compl_length = 0;
4779 compl_col = curwin->w_cursor.col;
4780 }
4781 }
4782 else
4783 {
4784 edit_submode_pre = NULL;
4785 compl_startpos.col = compl_col;
4786 }
4787
4788 if (compl_cont_status & CONT_LOCAL)
4789 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4790 else
4791 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4792
4793 // If any of the original typed text has been changed we need to fix
4794 // the redo buffer.
4795 ins_compl_fixRedoBufForLeader(NULL);
4796
4797 // Always add completion for the original text.
4798 vim_free(compl_orig_text);
4799 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4800 if (p_ic)
4801 flags |= CP_ICASE;
4802 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4803 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4804 {
4805 VIM_CLEAR(compl_pattern);
4806 VIM_CLEAR(compl_orig_text);
4807 return FAIL;
4808 }
4809
4810 // showmode might reset the internal line pointers, so it must
4811 // be called before line = ml_get(), or when this address is no
4812 // longer needed. -- Acevedo.
4813 edit_submode_extra = (char_u *)_("-- Searching...");
4814 edit_submode_highl = HLF_COUNT;
4815 showmode();
4816 edit_submode_extra = NULL;
4817 out_flush();
4818
4819 return OK;
4820}
4821
4822/*
4823 * display the completion status message
4824 */
4825 static void
4826ins_compl_show_statusmsg(void)
4827{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004828 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004829 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004830 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004831 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004832 ? (char_u *)_(e_hitend)
4833 : (char_u *)_(e_pattern_not_found);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004834 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004835 }
4836
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004837 if (edit_submode_extra == NULL)
4838 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004839 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004840 {
4841 edit_submode_extra = (char_u *)_("Back at original");
4842 edit_submode_highl = HLF_W;
4843 }
4844 else if (compl_cont_status & CONT_S_IPOS)
4845 {
4846 edit_submode_extra = (char_u *)_("Word from other line");
4847 edit_submode_highl = HLF_COUNT;
4848 }
4849 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4850 {
4851 edit_submode_extra = (char_u *)_("The only match");
4852 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004853 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004854 }
4855 else
4856 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004857#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004858 // Update completion sequence number when needed.
4859 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004860 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004861#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004862 // The match should always have a sequence number now, this is
4863 // just a safety check.
4864 if (compl_curr_match->cp_number != -1)
4865 {
4866 // Space for 10 text chars. + 2x10-digit no.s = 31.
4867 // Translations may need more than twice that.
4868 static char_u match_ref[81];
4869
4870 if (compl_matches > 0)
4871 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004872 _("match %d of %d"),
4873 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004874 else
4875 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004876 _("match %d"),
4877 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004878 edit_submode_extra = match_ref;
4879 edit_submode_highl = HLF_R;
4880 if (dollar_vcol >= 0)
4881 curs_columns(FALSE);
4882 }
4883 }
4884 }
4885
4886 // Show a message about what (completion) mode we're in.
4887 if (!compl_opt_suppress_empty)
4888 {
4889 showmode();
4890 if (!shortmess(SHM_COMPLETIONMENU))
4891 {
4892 if (edit_submode_extra != NULL)
4893 {
4894 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004895 {
4896 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004897 msg_attr((char *)edit_submode_extra,
4898 edit_submode_highl < HLF_COUNT
4899 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004900 msg_hist_off = FALSE;
4901 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004902 }
4903 else
4904 msg_clr_cmdline(); // necessary for "noshowmode"
4905 }
4906 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004907}
4908
4909/*
4910 * Do Insert mode completion.
4911 * Called when character "c" was typed, which has a meaning for completion.
4912 * Returns OK if completion was done, FAIL if something failed (out of mem).
4913 */
4914 int
4915ins_complete(int c, int enable_pum)
4916{
4917 int n;
4918 int save_w_wrow;
4919 int save_w_leftcol;
4920 int insert_match;
4921
4922 compl_direction = ins_compl_key2dir(c);
4923 insert_match = ins_compl_use_match(c);
4924
4925 if (!compl_started)
4926 {
4927 if (ins_compl_start() == FAIL)
4928 return FAIL;
4929 }
4930 else if (insert_match && stop_arrow() == FAIL)
4931 return FAIL;
4932
4933 compl_shown_match = compl_curr_match;
4934 compl_shows_dir = compl_direction;
4935
4936 // Find next match (and following matches).
4937 save_w_wrow = curwin->w_wrow;
4938 save_w_leftcol = curwin->w_leftcol;
4939 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4940
4941 // may undisplay the popup menu
4942 ins_compl_upd_pum();
4943
4944 if (n > 1) // all matches have been found
4945 compl_matches = n;
4946 compl_curr_match = compl_shown_match;
4947 compl_direction = compl_shows_dir;
4948
4949 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4950 // mode.
4951 if (got_int && !global_busy)
4952 {
4953 (void)vgetc();
4954 got_int = FALSE;
4955 }
4956
4957 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004958 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004959 {
4960 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4961 // because we couldn't expand anything at first place, but if we used
4962 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4963 // (such as M in M'exico) if not tried already. -- Acevedo
4964 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004965 || compl_status_adding()
4966 || (ctrl_x_mode_not_default()
4967 && !ctrl_x_mode_path_patterns()
4968 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004969 compl_cont_status &= ~CONT_N_ADDS;
4970 }
4971
4972 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4973 compl_cont_status |= CONT_S_IPOS;
4974 else
4975 compl_cont_status &= ~CONT_S_IPOS;
4976
4977 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004978
4979 // Show the popup menu, unless we got interrupted.
4980 if (enable_pum && !compl_interrupted)
4981 show_pum(save_w_wrow, save_w_leftcol);
4982
4983 compl_was_interrupted = compl_interrupted;
4984 compl_interrupted = FALSE;
4985
4986 return OK;
4987}
4988
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00004989/*
4990 * Remove (if needed) and show the popup menu
4991 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004992 static void
4993show_pum(int prev_w_wrow, int prev_w_leftcol)
4994{
4995 // RedrawingDisabled may be set when invoked through complete().
4996 int n = RedrawingDisabled;
4997
4998 RedrawingDisabled = 0;
4999
5000 // If the cursor moved or the display scrolled we need to remove the pum
5001 // first.
5002 setcursor();
5003 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5004 ins_compl_del_pum();
5005
5006 ins_compl_show_pum();
5007 setcursor();
5008 RedrawingDisabled = n;
5009}
5010
5011/*
5012 * Looks in the first "len" chars. of "src" for search-metachars.
5013 * If dest is not NULL the chars. are copied there quoting (with
5014 * a backslash) the metachars, and dest would be NUL terminated.
5015 * Returns the length (needed) of dest
5016 */
5017 static unsigned
5018quote_meta(char_u *dest, char_u *src, int len)
5019{
5020 unsigned m = (unsigned)len + 1; // one extra for the NUL
5021
5022 for ( ; --len >= 0; src++)
5023 {
5024 switch (*src)
5025 {
5026 case '.':
5027 case '*':
5028 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005029 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005030 break;
5031 // FALLTHROUGH
5032 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005033 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005034 break;
5035 // FALLTHROUGH
5036 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005037 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005038 break;
5039 // FALLTHROUGH
5040 case '^': // currently it's not needed.
5041 case '$':
5042 m++;
5043 if (dest != NULL)
5044 *dest++ = '\\';
5045 break;
5046 }
5047 if (dest != NULL)
5048 *dest++ = *src;
5049 // Copy remaining bytes of a multibyte character.
5050 if (has_mbyte)
5051 {
5052 int i, mb_len;
5053
5054 mb_len = (*mb_ptr2len)(src) - 1;
5055 if (mb_len > 0 && len >= mb_len)
5056 for (i = 0; i < mb_len; ++i)
5057 {
5058 --len;
5059 ++src;
5060 if (dest != NULL)
5061 *dest++ = *src;
5062 }
5063 }
5064 }
5065 if (dest != NULL)
5066 *dest = NUL;
5067
5068 return m;
5069}
5070
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005071#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005072 void
5073free_insexpand_stuff(void)
5074{
5075 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005076# ifdef FEAT_EVAL
5077 free_callback(&cfu_cb);
5078 free_callback(&ofu_cb);
5079 free_callback(&tsrfu_cb);
5080# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005081}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005082#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005083
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005084#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005085/*
5086 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5087 * spelled word, if there is one.
5088 */
5089 static void
5090spell_back_to_badword(void)
5091{
5092 pos_T tpos = curwin->w_cursor;
5093
5094 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5095 if (curwin->w_cursor.col != tpos.col)
5096 start_arrow(&tpos);
5097}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005098#endif