blob: c080d60cd9a7ccd3ca6650e5ef91312003a3542c [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100126/*
127 * All the current matches are stored in a list.
128 * "compl_first_match" points to the start of the list.
129 * "compl_curr_match" points to the currently selected entry.
130 * "compl_shown_match" is different from compl_curr_match during
131 * ins_compl_get_exp().
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000132 * "compl_old_match" points to previous "compl_curr_match".
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100133 */
134static compl_T *compl_first_match = NULL;
135static compl_T *compl_curr_match = NULL;
136static compl_T *compl_shown_match = NULL;
137static compl_T *compl_old_match = NULL;
138
139// After using a cursor key <Enter> selects a match in the popup menu,
140// otherwise it inserts a line break.
141static int compl_enter_selects = FALSE;
142
143// When "compl_leader" is not NULL only matches that start with this string
144// are used.
145static char_u *compl_leader = NULL;
146
147static int compl_get_longest = FALSE; // put longest common string
148 // in compl_leader
149
150static int compl_no_insert = FALSE; // FALSE: select & insert
151 // TRUE: noinsert
152static int compl_no_select = FALSE; // FALSE: select & insert
153 // TRUE: noselect
bfredl87af60c2022-09-24 11:17:51 +0100154static int compl_longest = FALSE; // FALSE: insert full match
155 // TRUE: insert longest prefix
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100156
157// Selected one of the matches. When FALSE the match was edited or using the
158// longest common string.
159static int compl_used_match;
160
161// didn't finish finding completions.
162static int compl_was_interrupted = FALSE;
163
164// Set when character typed while looking for matches and it means we should
165// stop looking for matches.
166static int compl_interrupted = FALSE;
167
168static int compl_restarting = FALSE; // don't insert match
169
170// When the first completion is done "compl_started" is set. When it's
171// FALSE the word to be completed must be located.
172static int compl_started = FALSE;
173
174// Which Ctrl-X mode are we in?
175static int ctrl_x_mode = CTRL_X_NORMAL;
176
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000177static int compl_matches = 0; // number of completion matches
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100178static char_u *compl_pattern = NULL;
179static int compl_direction = FORWARD;
180static int compl_shows_dir = FORWARD;
181static int compl_pending = 0; // > 1 for postponed CTRL-N
182static pos_T compl_startpos;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000183// Length in bytes of the text being completed (this is deleted to be replaced
184// by the match.)
185static int compl_length = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100186static colnr_T compl_col = 0; // column where the text starts
187 // that is being completed
188static char_u *compl_orig_text = NULL; // text as it was before
189 // completion started
190static int compl_cont_mode = 0;
191static expand_T compl_xp;
192
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000193// List of flags for method of completion.
194static int compl_cont_status = 0;
195# define CONT_ADDING 1 // "normal" or "adding" expansion
196# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
197 // it's set only iff N_ADDS is set
198# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
199# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
200 // if so, word-wise-expansion will set SOL
201# define CONT_SOL 16 // pattern includes start of line, just for
202 // word-wise expansion, not set for ^X^L
203# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
204 // expansion, (eg use complete=.)
205
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100206static int compl_opt_refresh_always = FALSE;
207static int compl_opt_suppress_empty = FALSE;
208
Bram Moolenaar08928322020-01-04 14:32:48 +0100209static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100210static void ins_compl_longest_match(compl_T *match);
211static void ins_compl_del_pum(void);
212static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
213static char_u *find_line_end(char_u *ptr);
214static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100215static int ins_compl_need_restart(void);
216static void ins_compl_new_leader(void);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000217static int get_compl_len(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100218static void ins_compl_restart(void);
219static void ins_compl_set_original_text(char_u *str);
220static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
221# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
222static void ins_compl_add_list(list_T *list);
223static void ins_compl_add_dict(dict_T *dict);
224# endif
225static int ins_compl_key2dir(int c);
226static int ins_compl_pum_key(int c);
227static int ins_compl_key2count(int c);
228static void show_pum(int prev_w_wrow, int prev_w_leftcol);
229static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100230
231#ifdef FEAT_SPELL
232static void spell_back_to_badword(void);
233static int spell_bad_len = 0; // length of located bad word
234#endif
235
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100236/*
237 * CTRL-X pressed in Insert mode.
238 */
239 void
240ins_ctrl_x(void)
241{
zeertzjqdca29d92021-08-31 19:12:51 +0200242 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100243 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000244 // if the next ^X<> won't ADD nothing, then reset compl_cont_status
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100245 if (compl_cont_status & CONT_N_ADDS)
246 compl_cont_status |= CONT_INTRPT;
247 else
248 compl_cont_status = 0;
249 // We're not sure which CTRL-X mode it will be yet
250 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
251 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
252 edit_submode_pre = NULL;
253 showmode();
254 }
zeertzjqdca29d92021-08-31 19:12:51 +0200255 else
256 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
257 // CTRL-V look like CTRL-N
258 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100259
LemonBoy2bf52dd2022-04-09 18:17:34 +0100260 may_trigger_modechanged();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100261}
262
263/*
264 * Functions to check the current CTRL-X mode.
265 */
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000266int ctrl_x_mode_none(void)
267 { return ctrl_x_mode == 0; }
268int ctrl_x_mode_normal(void)
269 { return ctrl_x_mode == CTRL_X_NORMAL; }
270int ctrl_x_mode_scroll(void)
271 { return ctrl_x_mode == CTRL_X_SCROLL; }
272int ctrl_x_mode_whole_line(void)
273 { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
274int ctrl_x_mode_files(void)
275 { return ctrl_x_mode == CTRL_X_FILES; }
276int ctrl_x_mode_tags(void)
277 { return ctrl_x_mode == CTRL_X_TAGS; }
278int ctrl_x_mode_path_patterns(void)
279 { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
280int ctrl_x_mode_path_defines(void)
281 { return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
282int ctrl_x_mode_dictionary(void)
283 { return ctrl_x_mode == CTRL_X_DICTIONARY; }
284int ctrl_x_mode_thesaurus(void)
285 { return ctrl_x_mode == CTRL_X_THESAURUS; }
286int ctrl_x_mode_cmdline(void)
287 { return ctrl_x_mode == CTRL_X_CMDLINE
zeertzjqdca29d92021-08-31 19:12:51 +0200288 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000289int ctrl_x_mode_function(void)
290 { return ctrl_x_mode == CTRL_X_FUNCTION; }
291int ctrl_x_mode_omni(void)
292 { return ctrl_x_mode == CTRL_X_OMNI; }
293int ctrl_x_mode_spell(void)
294 { return ctrl_x_mode == CTRL_X_SPELL; }
295static int ctrl_x_mode_eval(void)
296 { return ctrl_x_mode == CTRL_X_EVAL; }
297int ctrl_x_mode_line_or_eval(void)
298 { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100299
300/*
301 * Whether other than default completion has been selected.
302 */
303 int
304ctrl_x_mode_not_default(void)
305{
306 return ctrl_x_mode != CTRL_X_NORMAL;
307}
308
309/*
zeertzjqdca29d92021-08-31 19:12:51 +0200310 * Whether CTRL-X was typed without a following character,
311 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100312 */
313 int
314ctrl_x_mode_not_defined_yet(void)
315{
316 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
317}
318
319/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000320 * Return TRUE if currently in "normal" or "adding" insert completion matches
321 * state
322 */
323 int
324compl_status_adding(void)
325{
326 return compl_cont_status & CONT_ADDING;
327}
328
329/*
330 * Return TRUE if the completion pattern includes start of line, just for
331 * word-wise expansion.
332 */
333 int
334compl_status_sol(void)
335{
336 return compl_cont_status & CONT_SOL;
337}
338
339/*
340 * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
341 */
342 int
343compl_status_local(void)
344{
345 return compl_cont_status & CONT_LOCAL;
346}
347
348/*
349 * Clear the completion status flags
350 */
351 void
352compl_status_clear(void)
353{
354 compl_cont_status = 0;
355}
356
357/*
358 * Return TRUE if completion is using the forward direction matches
359 */
360 static int
361compl_dir_forward(void)
362{
363 return compl_direction == FORWARD;
364}
365
366/*
367 * Return TRUE if currently showing forward completion matches
368 */
369 static int
370compl_shows_dir_forward(void)
371{
372 return compl_shows_dir == FORWARD;
373}
374
375/*
376 * Return TRUE if currently showing backward completion matches
377 */
378 static int
379compl_shows_dir_backward(void)
380{
381 return compl_shows_dir == BACKWARD;
382}
383
384/*
385 * Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100386 */
387 int
388has_compl_option(int dict_opt)
389{
390 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200391#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100392 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200393#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100394 )
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100395 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
396#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100397 && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100398#endif
399 ))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100400 {
401 ctrl_x_mode = CTRL_X_NORMAL;
402 edit_submode = NULL;
403 msg_attr(dict_opt ? _("'dictionary' option is empty")
404 : _("'thesaurus' option is empty"),
405 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100406 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100407 {
408 vim_beep(BO_COMPL);
409 setcursor();
410 out_flush();
411#ifdef FEAT_EVAL
412 if (!get_vim_var_nr(VV_TESTING))
413#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100414 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100415 }
416 return FALSE;
417 }
418 return TRUE;
419}
420
421/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000422 * Is the character "c" a valid key to go to or keep us in CTRL-X mode?
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100423 * This depends on the current mode.
424 */
425 int
426vim_is_ctrl_x_key(int c)
427{
428 // Always allow ^R - let its results then be checked
429 if (c == Ctrl_R)
430 return TRUE;
431
432 // Accept <PageUp> and <PageDown> if the popup menu is visible.
433 if (ins_compl_pum_key(c))
434 return TRUE;
435
436 switch (ctrl_x_mode)
437 {
438 case 0: // Not in any CTRL-X mode
439 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
440 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200441 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100442 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
443 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
444 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
445 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
446 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200447 || c == Ctrl_S || c == Ctrl_K || c == 's'
448 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100449 case CTRL_X_SCROLL:
450 return (c == Ctrl_Y || c == Ctrl_E);
451 case CTRL_X_WHOLE_LINE:
452 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
453 case CTRL_X_FILES:
454 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
455 case CTRL_X_DICTIONARY:
456 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
457 case CTRL_X_THESAURUS:
458 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
459 case CTRL_X_TAGS:
460 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
461#ifdef FEAT_FIND_ID
462 case CTRL_X_PATH_PATTERNS:
463 return (c == Ctrl_P || c == Ctrl_N);
464 case CTRL_X_PATH_DEFINES:
465 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
466#endif
467 case CTRL_X_CMDLINE:
468 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
469 || c == Ctrl_X);
470#ifdef FEAT_COMPL_FUNC
471 case CTRL_X_FUNCTION:
472 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
473 case CTRL_X_OMNI:
474 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
475#endif
476 case CTRL_X_SPELL:
477 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
478 case CTRL_X_EVAL:
479 return (c == Ctrl_P || c == Ctrl_N);
480 }
481 internal_error("vim_is_ctrl_x_key()");
482 return FALSE;
483}
484
485/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000486 * Return TRUE if "match" is the original text when the completion began.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000487 */
488 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000489match_at_original_text(compl_T *match)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000490{
491 return match->cp_flags & CP_ORIGINAL_TEXT;
492}
493
494/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000495 * Returns TRUE if "match" is the first match in the completion list.
496 */
497 static int
498is_first_match(compl_T *match)
499{
500 return match == compl_first_match;
501}
502
503/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100504 * Return TRUE when character "c" is part of the item currently being
505 * completed. Used to decide whether to abandon complete mode when the menu
506 * is visible.
507 */
508 int
509ins_compl_accept_char(int c)
510{
511 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
512 // When expanding an identifier only accept identifier chars.
513 return vim_isIDc(c);
514
515 switch (ctrl_x_mode)
516 {
517 case CTRL_X_FILES:
518 // When expanding file name only accept file name chars. But not
519 // path separators, so that "proto/<Tab>" expands files in
520 // "proto", not "proto/" as a whole
521 return vim_isfilec(c) && !vim_ispathsep(c);
522
523 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200524 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100525 case CTRL_X_OMNI:
526 // Command line and Omni completion can work with just about any
527 // printable character, but do stop at white space.
528 return vim_isprintc(c) && !VIM_ISWHITE(c);
529
530 case CTRL_X_WHOLE_LINE:
531 // For while line completion a space can be part of the line.
532 return vim_isprintc(c);
533 }
534 return vim_iswordc(c);
535}
536
537/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000538 * Get the completed text by inferring the case of the originally typed text.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100539 * If the result is in allocated memory "tofree" is set to it.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000540 */
541 static char_u *
542ins_compl_infercase_gettext(
543 char_u *str,
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100544 int char_len,
545 int compl_char_len,
546 int min_len,
547 char_u **tofree)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000548{
549 int *wca; // Wide character array.
550 char_u *p;
551 int i, c;
552 int has_lower = FALSE;
553 int was_letter = FALSE;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100554 garray_T gap;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000555
556 IObuff[0] = NUL;
557
558 // Allocate wide character array for the completion and fill it.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100559 wca = ALLOC_MULT(int, char_len);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000560 if (wca == NULL)
561 return IObuff;
562
563 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100564 for (i = 0; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000565 if (has_mbyte)
566 wca[i] = mb_ptr2char_adv(&p);
567 else
568 wca[i] = *(p++);
569
570 // Rule 1: Were any chars converted to lower?
571 p = compl_orig_text;
572 for (i = 0; i < min_len; ++i)
573 {
574 if (has_mbyte)
575 c = mb_ptr2char_adv(&p);
576 else
577 c = *(p++);
578 if (MB_ISLOWER(c))
579 {
580 has_lower = TRUE;
581 if (MB_ISUPPER(wca[i]))
582 {
583 // Rule 1 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100584 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000585 wca[i] = MB_TOLOWER(wca[i]);
586 break;
587 }
588 }
589 }
590
591 // Rule 2: No lower case, 2nd consecutive letter converted to
592 // upper case.
593 if (!has_lower)
594 {
595 p = compl_orig_text;
596 for (i = 0; i < min_len; ++i)
597 {
598 if (has_mbyte)
599 c = mb_ptr2char_adv(&p);
600 else
601 c = *(p++);
602 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
603 {
604 // Rule 2 is satisfied.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100605 for (i = compl_char_len; i < char_len; ++i)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000606 wca[i] = MB_TOUPPER(wca[i]);
607 break;
608 }
609 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
610 }
611 }
612
613 // Copy the original case of the part we typed.
614 p = compl_orig_text;
615 for (i = 0; i < min_len; ++i)
616 {
617 if (has_mbyte)
618 c = mb_ptr2char_adv(&p);
619 else
620 c = *(p++);
621 if (MB_ISLOWER(c))
622 wca[i] = MB_TOLOWER(wca[i]);
623 else if (MB_ISUPPER(c))
624 wca[i] = MB_TOUPPER(wca[i]);
625 }
626
627 // Generate encoding specific output from wide character array.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000628 p = IObuff;
629 i = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100630 ga_init2(&gap, 1, 500);
631 while (i < char_len)
632 {
633 if (gap.ga_data != NULL)
634 {
635 if (ga_grow(&gap, 10) == FAIL)
636 {
637 ga_clear(&gap);
638 return (char_u *)"[failed]";
639 }
640 p = (char_u *)gap.ga_data + gap.ga_len;
641 if (has_mbyte)
642 gap.ga_len += (*mb_char2bytes)(wca[i++], p);
643 else
644 {
645 *p = wca[i++];
646 ++gap.ga_len;
647 }
648 }
649 else if ((p - IObuff) + 6 >= IOSIZE)
650 {
651 // Multi-byte characters can occupy up to five bytes more than
652 // ASCII characters, and we also need one byte for NUL, so when
653 // getting to six bytes from the edge of IObuff switch to using a
654 // growarray. Add the character in the next round.
655 if (ga_grow(&gap, IOSIZE) == FAIL)
zeertzjq70e566b2024-03-21 07:11:58 +0100656 {
657 vim_free(wca);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100658 return (char_u *)"[failed]";
zeertzjq70e566b2024-03-21 07:11:58 +0100659 }
Bram Moolenaarb9e71732022-07-23 06:53:08 +0100660 *p = NUL;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100661 STRCPY(gap.ga_data, IObuff);
Bram Moolenaarc7bd2f02022-07-15 20:45:20 +0100662 gap.ga_len = (int)STRLEN(IObuff);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100663 }
664 else if (has_mbyte)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000665 p += (*mb_char2bytes)(wca[i++], p);
666 else
667 *(p++) = wca[i++];
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100668 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000669 vim_free(wca);
670
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100671 if (gap.ga_data != NULL)
672 {
673 *tofree = gap.ga_data;
674 return gap.ga_data;
675 }
676
677 *p = NUL;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000678 return IObuff;
679}
680
681/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100682 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
683 * case of the originally typed text is used, and the case of the completed
684 * text is inferred, ie this tries to work out what case you probably wanted
685 * the rest of the word to be in -- webb
686 */
687 int
688ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200689 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100690 int len,
691 int icase,
692 char_u *fname,
693 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200694 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100695{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200696 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100697 char_u *p;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100698 int char_len; // count multi-byte characters
699 int compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100700 int min_len;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200701 int flags = 0;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100702 int res;
703 char_u *tofree = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100704
705 if (p_ic && curbuf->b_p_inf && len > 0)
706 {
707 // Infer case of completed part.
708
709 // Find actual length of completion.
710 if (has_mbyte)
711 {
712 p = str;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100713 char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 while (*p != NUL)
715 {
716 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100717 ++char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100718 }
719 }
720 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100721 char_len = len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100722
723 // Find actual length of original text.
724 if (has_mbyte)
725 {
726 p = compl_orig_text;
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100727 compl_char_len = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100728 while (*p != NUL)
729 {
730 MB_PTR_ADV(p);
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100731 ++compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100732 }
733 }
734 else
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100735 compl_char_len = compl_length;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100736
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100737 // "char_len" may be smaller than "compl_char_len" when using
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100738 // thesaurus, only use the minimum when comparing.
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100739 min_len = char_len < compl_char_len ? char_len : compl_char_len;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100740
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100741 str = ins_compl_infercase_gettext(str, char_len,
742 compl_char_len, min_len, &tofree);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100743 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200744 if (cont_s_ipos)
745 flags |= CP_CONT_S_IPOS;
746 if (icase)
747 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200748
Bram Moolenaarcaea6642022-07-07 19:42:04 +0100749 res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
750 vim_free(tofree);
751 return res;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100752}
753
754/*
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000755 * Add a match to the list of matches. The arguments are:
756 * str - text of the match to add
757 * len - length of "str". If -1, then the length of "str" is
758 * computed.
759 * fname - file name to associate with this match.
760 * cptext - list of strings to use with this match (for abbr, menu, info
761 * and kind)
762 * user_data - user supplied data (any vim type) for this match
763 * cdir - match direction. If 0, use "compl_direction".
764 * flags_arg - match flags (cp_flags)
765 * adup - accept this match even if it is already present.
766 * If "cdir" is FORWARD, then the match is added after the current match.
767 * Otherwise, it is added before the current match.
768 *
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100769 * If the given string is already in the list of completions, then return
770 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
771 * maybe because alloc() returns NULL, then FAIL is returned.
772 */
773 static int
774ins_compl_add(
775 char_u *str,
776 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100777 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 char_u **cptext, // extra text for popup menu or NULL
779 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100780 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200781 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100782 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100783{
784 compl_T *match;
785 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200786 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100787
Bram Moolenaarceb06192021-04-04 15:05:22 +0200788 if (flags & CP_FAST)
789 fast_breakcheck();
790 else
791 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100792 if (got_int)
793 return FAIL;
794 if (len < 0)
795 len = (int)STRLEN(str);
796
797 // If the same match is already present, don't add it.
798 if (compl_first_match != NULL && !adup)
799 {
800 match = compl_first_match;
801 do
802 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000803 if (!match_at_original_text(match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100804 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaarbaefde12022-07-07 19:59:49 +0100805 && ((int)STRLEN(match->cp_str) <= len
806 || match->cp_str[len] == NUL))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100807 return NOTDONE;
808 match = match->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000809 } while (match != NULL && !is_first_match(match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100810 }
811
812 // Remove any popup menu before changing the list of matches.
813 ins_compl_del_pum();
814
815 // Allocate a new match structure.
816 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200817 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100818 if (match == NULL)
819 return FAIL;
820 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200821 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100822 match->cp_number = 0;
823 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
824 {
825 vim_free(match);
826 return FAIL;
827 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100828
829 // match-fname is:
830 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200831 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100832 // - NULL otherwise. --Acevedo
833 if (fname != NULL
834 && compl_curr_match != NULL
835 && compl_curr_match->cp_fname != NULL
836 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
837 match->cp_fname = compl_curr_match->cp_fname;
838 else if (fname != NULL)
839 {
840 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200841 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100842 }
843 else
844 match->cp_fname = NULL;
845 match->cp_flags = flags;
846
847 if (cptext != NULL)
848 {
849 int i;
850
851 for (i = 0; i < CPT_COUNT; ++i)
852 if (cptext[i] != NULL && *cptext[i] != NUL)
853 match->cp_text[i] = vim_strsave(cptext[i]);
854 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100855#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100856 if (user_data != NULL)
857 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100858#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100859
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000860 // Link the new match structure after (FORWARD) or before (BACKWARD) the
861 // current match in the list of matches .
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862 if (compl_first_match == NULL)
863 match->cp_next = match->cp_prev = NULL;
864 else if (dir == FORWARD)
865 {
866 match->cp_next = compl_curr_match->cp_next;
867 match->cp_prev = compl_curr_match;
868 }
869 else // BACKWARD
870 {
871 match->cp_next = compl_curr_match;
872 match->cp_prev = compl_curr_match->cp_prev;
873 }
874 if (match->cp_next)
875 match->cp_next->cp_prev = match;
876 if (match->cp_prev)
877 match->cp_prev->cp_next = match;
878 else // if there's nothing before, it is the first match
879 compl_first_match = match;
880 compl_curr_match = match;
881
882 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200883 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100884 ins_compl_longest_match(match);
885
886 return OK;
887}
888
889/*
890 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200891 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100892 */
893 static int
894ins_compl_equal(compl_T *match, char_u *str, int len)
895{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200896 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200897 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200898 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100899 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
900 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
901}
902
903/*
904 * Reduce the longest common string for match "match".
905 */
906 static void
907ins_compl_longest_match(compl_T *match)
908{
909 char_u *p, *s;
910 int c1, c2;
911 int had_match;
912
913 if (compl_leader == NULL)
914 {
915 // First match, use it as a whole.
916 compl_leader = vim_strsave(match->cp_str);
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000917 if (compl_leader == NULL)
918 return;
919
920 had_match = (curwin->w_cursor.col > compl_col);
921 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000922 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000923 ins_redraw(FALSE);
924
925 // When the match isn't there (to avoid matching itself) remove it
926 // again after redrawing.
927 if (!had_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100928 ins_compl_delete();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 compl_used_match = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000930
931 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100932 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000933
934 // Reduce the text if this match differs from compl_leader.
935 p = compl_leader;
936 s = match->cp_str;
937 while (*p != NUL)
938 {
939 if (has_mbyte)
940 {
941 c1 = mb_ptr2char(p);
942 c2 = mb_ptr2char(s);
943 }
944 else
945 {
946 c1 = *p;
947 c2 = *s;
948 }
949 if ((match->cp_flags & CP_ICASE)
950 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
951 break;
952 if (has_mbyte)
953 {
954 MB_PTR_ADV(p);
955 MB_PTR_ADV(s);
956 }
957 else
958 {
959 ++p;
960 ++s;
961 }
962 }
963
964 if (*p != NUL)
965 {
966 // Leader was shortened, need to change the inserted text.
967 *p = NUL;
968 had_match = (curwin->w_cursor.col > compl_col);
969 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +0000970 ins_bytes(compl_leader + get_compl_len());
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +0000971 ins_redraw(FALSE);
972
973 // When the match isn't there (to avoid matching itself) remove it
974 // again after redrawing.
975 if (!had_match)
976 ins_compl_delete();
977 }
978
979 compl_used_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100980}
981
982/*
983 * Add an array of matches to the list of matches.
984 * Frees matches[].
985 */
986 static void
987ins_compl_add_matches(
988 int num_matches,
989 char_u **matches,
990 int icase)
991{
992 int i;
993 int add_r = OK;
994 int dir = compl_direction;
995
996 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100997 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200998 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100999 // if dir was BACKWARD then honor it just once
1000 dir = FORWARD;
1001 FreeWild(num_matches, matches);
1002}
1003
1004/*
1005 * Make the completion list cyclic.
1006 * Return the number of matches (excluding the original).
1007 */
1008 static int
1009ins_compl_make_cyclic(void)
1010{
1011 compl_T *match;
1012 int count = 0;
1013
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001014 if (compl_first_match == NULL)
1015 return 0;
1016
1017 // Find the end of the list.
1018 match = compl_first_match;
1019 // there's always an entry for the compl_orig_text, it doesn't count.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001020 while (match->cp_next != NULL && !is_first_match(match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001021 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001022 match = match->cp_next;
1023 ++count;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001024 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001025 match->cp_next = compl_first_match;
1026 compl_first_match->cp_prev = match;
1027
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001028 return count;
1029}
1030
1031/*
1032 * Return whether there currently is a shown match.
1033 */
1034 int
1035ins_compl_has_shown_match(void)
1036{
1037 return compl_shown_match == NULL
1038 || compl_shown_match != compl_shown_match->cp_next;
1039}
1040
1041/*
1042 * Return whether the shown match is long enough.
1043 */
1044 int
1045ins_compl_long_shown_match(void)
1046{
1047 return (int)STRLEN(compl_shown_match->cp_str)
1048 > curwin->w_cursor.col - compl_col;
1049}
1050
1051/*
1052 * Set variables that store noselect and noinsert behavior from the
1053 * 'completeopt' value.
1054 */
1055 void
1056completeopt_was_set(void)
1057{
1058 compl_no_insert = FALSE;
1059 compl_no_select = FALSE;
bfredl87af60c2022-09-24 11:17:51 +01001060 compl_longest = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001061 if (strstr((char *)p_cot, "noselect") != NULL)
1062 compl_no_select = TRUE;
1063 if (strstr((char *)p_cot, "noinsert") != NULL)
1064 compl_no_insert = TRUE;
bfredl87af60c2022-09-24 11:17:51 +01001065 if (strstr((char *)p_cot, "longest") != NULL)
1066 compl_longest = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001067}
1068
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001069
1070// "compl_match_array" points the currently displayed list of entries in the
1071// popup menu. It is NULL when there is no popup menu.
1072static pumitem_T *compl_match_array = NULL;
1073static int compl_match_arraysize;
1074
1075/*
1076 * Update the screen and when there is any scrolling remove the popup menu.
1077 */
1078 static void
1079ins_compl_upd_pum(void)
1080{
1081 int h;
1082
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001083 if (compl_match_array == NULL)
1084 return;
1085
1086 h = curwin->w_cline_height;
1087 // Update the screen later, before drawing the popup menu over it.
1088 pum_call_update_screen();
1089 if (h != curwin->w_cline_height)
1090 ins_compl_del_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001091}
1092
1093/*
1094 * Remove any popup menu.
1095 */
1096 static void
1097ins_compl_del_pum(void)
1098{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001099 if (compl_match_array == NULL)
1100 return;
1101
1102 pum_undisplay();
1103 VIM_CLEAR(compl_match_array);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001104}
1105
1106/*
1107 * Return TRUE if the popup menu should be displayed.
1108 */
1109 int
1110pum_wanted(void)
1111{
1112 // 'completeopt' must contain "menu" or "menuone"
1113 if (vim_strchr(p_cot, 'm') == NULL)
1114 return FALSE;
1115
1116 // The display looks bad on a B&W display.
1117 if (t_colors < 8
1118#ifdef FEAT_GUI
1119 && !gui.in_use
1120#endif
1121 )
1122 return FALSE;
1123 return TRUE;
1124}
1125
1126/*
1127 * Return TRUE if there are two or more matches to be shown in the popup menu.
1128 * One if 'completopt' contains "menuone".
1129 */
1130 static int
1131pum_enough_matches(void)
1132{
1133 compl_T *compl;
1134 int i;
1135
1136 // Don't display the popup menu if there are no matches or there is only
1137 // one (ignoring the original text).
1138 compl = compl_first_match;
1139 i = 0;
1140 do
1141 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001142 if (compl == NULL || (!match_at_original_text(compl) && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001143 break;
1144 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001145 } while (!is_first_match(compl));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001146
1147 if (strstr((char *)p_cot, "menuone") != NULL)
1148 return (i >= 1);
1149 return (i >= 2);
1150}
1151
Bram Moolenaar3075a452021-11-17 15:51:52 +00001152#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001153/*
1154 * Allocate Dict for the completed item.
1155 * { word, abbr, menu, kind, info }
1156 */
1157 static dict_T *
1158ins_compl_dict_alloc(compl_T *match)
1159{
1160 dict_T *dict = dict_alloc_lock(VAR_FIXED);
1161
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001162 if (dict == NULL)
1163 return NULL;
1164
1165 dict_add_string(dict, "word", match->cp_str);
1166 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
1167 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
1168 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
1169 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
1170 if (match->cp_user_data.v_type == VAR_UNKNOWN)
1171 dict_add_string(dict, "user_data", (char_u *)"");
1172 else
1173 dict_add_tv(dict, "user_data", &match->cp_user_data);
1174
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001175 return dict;
1176}
1177
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001178/*
1179 * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode
1180 * completion menu is changed.
1181 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001182 static void
1183trigger_complete_changed_event(int cur)
1184{
1185 dict_T *v_event;
1186 dict_T *item;
1187 static int recursive = FALSE;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001188 save_v_event_T save_v_event;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001189
1190 if (recursive)
1191 return;
1192
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001193 if (cur < 0)
1194 item = dict_alloc();
1195 else
1196 item = ins_compl_dict_alloc(compl_curr_match);
1197 if (item == NULL)
1198 return;
Bram Moolenaar3075a452021-11-17 15:51:52 +00001199 v_event = get_v_event(&save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001200 dict_add_dict(v_event, "completed_item", item);
1201 pum_set_event_info(v_event);
1202 dict_set_items_ro(v_event);
1203
1204 recursive = TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01001205 textlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001206 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
zeertzjqcfe45652022-05-27 17:26:55 +01001207 textlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001208 recursive = FALSE;
1209
Bram Moolenaar3075a452021-11-17 15:51:52 +00001210 restore_v_event(v_event, &save_v_event);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001211}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001212#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001213
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001214/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001215 * Build a popup menu to show the completion matches.
1216 * Returns the popup menu entry that should be selected. Returns -1 if nothing
1217 * should be selected.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001218 */
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001219 static int
1220ins_compl_build_pum(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001221{
1222 compl_T *compl;
1223 compl_T *shown_compl = NULL;
1224 int did_find_shown_match = FALSE;
1225 int shown_match_ok = FALSE;
1226 int i;
1227 int cur = -1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001228 int lead_len = 0;
1229
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001230 // Need to build the popup menu list.
1231 compl_match_arraysize = 0;
1232 compl = compl_first_match;
1233 if (compl_leader != NULL)
1234 lead_len = (int)STRLEN(compl_leader);
1235
1236 do
1237 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001238 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001239 && (compl_leader == NULL
1240 || ins_compl_equal(compl, compl_leader, lead_len)))
1241 ++compl_match_arraysize;
1242 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001243 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001244
1245 if (compl_match_arraysize == 0)
1246 return -1;
1247
1248 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
1249 if (compl_match_array == NULL)
1250 return -1;
1251
1252 // If the current match is the original text don't find the first
1253 // match after it, don't highlight anything.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001254 if (match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001255 shown_match_ok = TRUE;
1256
1257 i = 0;
1258 compl = compl_first_match;
1259 do
1260 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001261 if (!match_at_original_text(compl)
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001262 && (compl_leader == NULL
1263 || ins_compl_equal(compl, compl_leader, lead_len)))
1264 {
1265 if (!shown_match_ok)
1266 {
1267 if (compl == compl_shown_match || did_find_shown_match)
1268 {
1269 // This item is the shown match or this is the
1270 // first displayed item after the shown match.
1271 compl_shown_match = compl;
1272 did_find_shown_match = TRUE;
1273 shown_match_ok = TRUE;
1274 }
1275 else
1276 // Remember this displayed match for when the
1277 // shown match is just below it.
1278 shown_compl = compl;
1279 cur = i;
1280 }
1281
1282 if (compl->cp_text[CPT_ABBR] != NULL)
1283 compl_match_array[i].pum_text =
1284 compl->cp_text[CPT_ABBR];
1285 else
1286 compl_match_array[i].pum_text = compl->cp_str;
1287 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1288 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1289 if (compl->cp_text[CPT_MENU] != NULL)
1290 compl_match_array[i++].pum_extra =
1291 compl->cp_text[CPT_MENU];
1292 else
1293 compl_match_array[i++].pum_extra = compl->cp_fname;
1294 }
1295
1296 if (compl == compl_shown_match)
1297 {
1298 did_find_shown_match = TRUE;
1299
1300 // When the original text is the shown match don't set
1301 // compl_shown_match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001302 if (match_at_original_text(compl))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001303 shown_match_ok = TRUE;
1304
1305 if (!shown_match_ok && shown_compl != NULL)
1306 {
1307 // The shown match isn't displayed, set it to the
1308 // previously displayed match.
1309 compl_shown_match = shown_compl;
1310 shown_match_ok = TRUE;
1311 }
1312 }
1313 compl = compl->cp_next;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001314 } while (compl != NULL && !is_first_match(compl));
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001315
1316 if (!shown_match_ok) // no displayed match at all
1317 cur = -1;
1318
1319 return cur;
1320}
1321
1322/*
1323 * Show the popup menu for the list of matches.
1324 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1325 */
1326 void
1327ins_compl_show_pum(void)
1328{
1329 int i;
1330 int cur = -1;
1331 colnr_T col;
1332
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001333 if (!pum_wanted() || !pum_enough_matches())
1334 return;
1335
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001336 // Update the screen later, before drawing the popup menu over it.
1337 pum_call_update_screen();
1338
1339 if (compl_match_array == NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001340 // Need to build the popup menu list.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001341 cur = ins_compl_build_pum();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001342 else
1343 {
1344 // popup menu already exists, only need to find the current item.
1345 for (i = 0; i < compl_match_arraysize; ++i)
1346 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1347 || compl_match_array[i].pum_text
1348 == compl_shown_match->cp_text[CPT_ABBR])
1349 {
1350 cur = i;
1351 break;
1352 }
1353 }
1354
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001355 if (compl_match_array == NULL)
glepnir0d3c0a62024-02-11 17:52:40 +01001356 {
1357#ifdef FEAT_EVAL
1358 if (compl_started && has_completechanged())
1359 trigger_complete_changed_event(cur);
1360#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001361 return;
glepnir0d3c0a62024-02-11 17:52:40 +01001362 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001363
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001364 // In Replace mode when a $ is displayed at the end of the line only
1365 // part of the screen would be updated. We do need to redraw here.
1366 dollar_vcol = -1;
1367
1368 // Compute the screen column of the start of the completed text.
1369 // Use the cursor to get all wrapping and other settings right.
1370 col = curwin->w_cursor.col;
1371 curwin->w_cursor.col = compl_col;
1372 pum_display(compl_match_array, compl_match_arraysize, cur);
1373 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001374
glepnircbb46b42024-02-03 18:11:13 +01001375 // After adding leader, set the current match to shown match.
1376 if (compl_started && compl_curr_match != compl_shown_match)
1377 compl_curr_match = compl_shown_match;
1378
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001379#ifdef FEAT_EVAL
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001380 if (has_completechanged())
1381 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001382#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001383}
1384
1385#define DICT_FIRST (1) // use just first element in "dict"
1386#define DICT_EXACT (2) // "dict" is the exact name of a file
1387
1388/*
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001389 * Add any identifiers that match the given pattern "pat" in the list of
1390 * dictionary files "dict_start" to the list of completions.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001391 */
1392 static void
1393ins_compl_dictionaries(
1394 char_u *dict_start,
1395 char_u *pat,
1396 int flags, // DICT_FIRST and/or DICT_EXACT
1397 int thesaurus) // Thesaurus completion
1398{
1399 char_u *dict = dict_start;
1400 char_u *ptr;
1401 char_u *buf;
1402 regmatch_T regmatch;
1403 char_u **files;
1404 int count;
1405 int save_p_scs;
1406 int dir = compl_direction;
1407
1408 if (*dict == NUL)
1409 {
1410#ifdef FEAT_SPELL
1411 // When 'dictionary' is empty and spell checking is enabled use
1412 // "spell".
1413 if (!thesaurus && curwin->w_p_spell)
1414 dict = (char_u *)"spell";
1415 else
1416#endif
1417 return;
1418 }
1419
1420 buf = alloc(LSIZE);
1421 if (buf == NULL)
1422 return;
1423 regmatch.regprog = NULL; // so that we can goto theend
1424
1425 // If 'infercase' is set, don't use 'smartcase' here
1426 save_p_scs = p_scs;
1427 if (curbuf->b_p_inf)
1428 p_scs = FALSE;
1429
1430 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1431 // to only match at the start of a line. Otherwise just match the
1432 // pattern. Also need to double backslashes.
1433 if (ctrl_x_mode_line_or_eval())
1434 {
1435 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1436 size_t len;
1437
1438 if (pat_esc == NULL)
1439 goto theend;
1440 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001441 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001442 if (ptr == NULL)
1443 {
1444 vim_free(pat_esc);
1445 goto theend;
1446 }
1447 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1448 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1449 vim_free(pat_esc);
1450 vim_free(ptr);
1451 }
1452 else
1453 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001454 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001455 if (regmatch.regprog == NULL)
1456 goto theend;
1457 }
1458
1459 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1460 regmatch.rm_ic = ignorecase(pat);
1461 while (*dict != NUL && !got_int && !compl_interrupted)
1462 {
1463 // copy one dictionary file name into buf
1464 if (flags == DICT_EXACT)
1465 {
1466 count = 1;
1467 files = &dict;
1468 }
1469 else
1470 {
1471 // Expand wildcards in the dictionary name, but do not allow
1472 // backticks (for security, the 'dict' option may have been set in
1473 // a modeline).
1474 copy_option_part(&dict, buf, LSIZE, ",");
1475# ifdef FEAT_SPELL
1476 if (!thesaurus && STRCMP(buf, "spell") == 0)
1477 count = -1;
1478 else
1479# endif
1480 if (vim_strchr(buf, '`') != NULL
1481 || expand_wildcards(1, &buf, &count, &files,
1482 EW_FILE|EW_SILENT) != OK)
1483 count = 0;
1484 }
1485
1486# ifdef FEAT_SPELL
1487 if (count == -1)
1488 {
1489 // Complete from active spelling. Skip "\<" in the pattern, we
1490 // don't use it as a RE.
1491 if (pat[0] == '\\' && pat[1] == '<')
1492 ptr = pat + 2;
1493 else
1494 ptr = pat;
1495 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1496 }
1497 else
1498# endif
1499 if (count > 0) // avoid warning for using "files" uninit
1500 {
1501 ins_compl_files(count, files, thesaurus, flags,
1502 &regmatch, buf, &dir);
1503 if (flags != DICT_EXACT)
1504 FreeWild(count, files);
1505 }
1506 if (flags != 0)
1507 break;
1508 }
1509
1510theend:
1511 p_scs = save_p_scs;
1512 vim_regfree(regmatch.regprog);
1513 vim_free(buf);
1514}
1515
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001516/*
1517 * Add all the words in the line "*buf_arg" from the thesaurus file "fname"
1518 * skipping the word at 'skip_word'. Returns OK on success.
1519 */
1520 static int
zeertzjq5fb3aab2022-08-24 16:48:23 +01001521thesaurus_add_words_in_line(
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001522 char_u *fname,
1523 char_u **buf_arg,
1524 int dir,
1525 char_u *skip_word)
1526{
1527 int status = OK;
1528 char_u *ptr;
1529 char_u *wstart;
1530
1531 // Add the other matches on the line
1532 ptr = *buf_arg;
1533 while (!got_int)
1534 {
1535 // Find start of the next word. Skip white
1536 // space and punctuation.
1537 ptr = find_word_start(ptr);
1538 if (*ptr == NUL || *ptr == NL)
1539 break;
1540 wstart = ptr;
1541
1542 // Find end of the word.
1543 if (has_mbyte)
1544 // Japanese words may have characters in
1545 // different classes, only separate words
1546 // with single-byte non-word characters.
1547 while (*ptr != NUL)
1548 {
1549 int l = (*mb_ptr2len)(ptr);
1550
1551 if (l < 2 && !vim_iswordc(*ptr))
1552 break;
1553 ptr += l;
1554 }
1555 else
1556 ptr = find_word_end(ptr);
1557
1558 // Add the word. Skip the regexp match.
1559 if (wstart != skip_word)
1560 {
1561 status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic,
1562 fname, dir, FALSE);
1563 if (status == FAIL)
1564 break;
1565 }
1566 }
1567
1568 *buf_arg = ptr;
1569 return status;
1570}
1571
1572/*
1573 * Process "count" dictionary/thesaurus "files" and add the text matching
1574 * "regmatch".
1575 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001576 static void
1577ins_compl_files(
1578 int count,
1579 char_u **files,
1580 int thesaurus,
1581 int flags,
1582 regmatch_T *regmatch,
1583 char_u *buf,
1584 int *dir)
1585{
1586 char_u *ptr;
1587 int i;
1588 FILE *fp;
1589 int add_r;
1590
1591 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1592 {
1593 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01001594 if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001595 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001596 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001597 vim_snprintf((char *)IObuff, IOSIZE,
1598 _("Scanning dictionary: %s"), (char *)files[i]);
1599 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1600 }
1601
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001602 if (fp == NULL)
1603 continue;
1604
1605 // Read dictionary file line by line.
1606 // Check each line for a match.
1607 while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001608 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001609 ptr = buf;
1610 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001611 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001612 ptr = regmatch->startp[0];
1613 if (ctrl_x_mode_line_or_eval())
1614 ptr = find_line_end(ptr);
1615 else
1616 ptr = find_word_end(ptr);
1617 add_r = ins_compl_add_infercase(regmatch->startp[0],
1618 (int)(ptr - regmatch->startp[0]),
1619 p_ic, files[i], *dir, FALSE);
1620 if (thesaurus)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001621 {
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001622 // For a thesaurus, add all the words in the line
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001623 ptr = buf;
zeertzjq5fb3aab2022-08-24 16:48:23 +01001624 add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir,
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001625 regmatch->startp[0]);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001626 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001627 if (add_r == OK)
1628 // if dir was BACKWARD then honor it just once
1629 *dir = FORWARD;
1630 else if (add_r == FAIL)
1631 break;
1632 // avoid expensive call to vim_regexec() when at end
1633 // of line
1634 if (*ptr == '\n' || got_int)
1635 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001636 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001637 line_breakcheck();
1638 ins_compl_check_keys(50, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001639 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00001640 fclose(fp);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001641 }
1642}
1643
1644/*
1645 * Find the start of the next word.
1646 * Returns a pointer to the first char of the word. Also stops at a NUL.
1647 */
1648 char_u *
1649find_word_start(char_u *ptr)
1650{
1651 if (has_mbyte)
1652 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1653 ptr += (*mb_ptr2len)(ptr);
1654 else
1655 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1656 ++ptr;
1657 return ptr;
1658}
1659
1660/*
1661 * Find the end of the word. Assumes it starts inside a word.
1662 * Returns a pointer to just after the word.
1663 */
1664 char_u *
1665find_word_end(char_u *ptr)
1666{
1667 int start_class;
1668
1669 if (has_mbyte)
1670 {
1671 start_class = mb_get_class(ptr);
1672 if (start_class > 1)
1673 while (*ptr != NUL)
1674 {
1675 ptr += (*mb_ptr2len)(ptr);
1676 if (mb_get_class(ptr) != start_class)
1677 break;
1678 }
1679 }
1680 else
1681 while (vim_iswordc(*ptr))
1682 ++ptr;
1683 return ptr;
1684}
1685
1686/*
1687 * Find the end of the line, omitting CR and NL at the end.
1688 * Returns a pointer to just after the line.
1689 */
1690 static char_u *
1691find_line_end(char_u *ptr)
1692{
1693 char_u *s;
1694
1695 s = ptr + STRLEN(ptr);
1696 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1697 --s;
1698 return s;
1699}
1700
1701/*
1702 * Free the list of completions
1703 */
1704 static void
1705ins_compl_free(void)
1706{
1707 compl_T *match;
1708 int i;
1709
1710 VIM_CLEAR(compl_pattern);
1711 VIM_CLEAR(compl_leader);
1712
1713 if (compl_first_match == NULL)
1714 return;
1715
1716 ins_compl_del_pum();
1717 pum_clear();
1718
1719 compl_curr_match = compl_first_match;
1720 do
1721 {
1722 match = compl_curr_match;
1723 compl_curr_match = compl_curr_match->cp_next;
1724 vim_free(match->cp_str);
1725 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001726 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001727 vim_free(match->cp_fname);
1728 for (i = 0; i < CPT_COUNT; ++i)
1729 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001730#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001731 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001732#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001733 vim_free(match);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001734 } while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001735 compl_first_match = compl_curr_match = NULL;
1736 compl_shown_match = NULL;
1737 compl_old_match = NULL;
1738}
1739
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001740/*
1741 * Reset/clear the completion state.
1742 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001743 void
1744ins_compl_clear(void)
1745{
1746 compl_cont_status = 0;
1747 compl_started = FALSE;
1748 compl_matches = 0;
1749 VIM_CLEAR(compl_pattern);
1750 VIM_CLEAR(compl_leader);
1751 edit_submode_extra = NULL;
1752 VIM_CLEAR(compl_orig_text);
1753 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001754#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001755 // clear v:completed_item
1756 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001757#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001758}
1759
1760/*
1761 * Return TRUE when Insert completion is active.
1762 */
1763 int
1764ins_compl_active(void)
1765{
1766 return compl_started;
1767}
1768
1769/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001770 * Selected one of the matches. When FALSE the match was edited or using the
1771 * longest common string.
1772 */
1773 int
1774ins_compl_used_match(void)
1775{
1776 return compl_used_match;
1777}
1778
1779/*
1780 * Initialize get longest common string.
1781 */
1782 void
1783ins_compl_init_get_longest(void)
1784{
1785 compl_get_longest = FALSE;
1786}
1787
1788/*
1789 * Returns TRUE when insert completion is interrupted.
1790 */
1791 int
1792ins_compl_interrupted(void)
1793{
1794 return compl_interrupted;
1795}
1796
1797/*
1798 * Returns TRUE if the <Enter> key selects a match in the completion popup
1799 * menu.
1800 */
1801 int
1802ins_compl_enter_selects(void)
1803{
1804 return compl_enter_selects;
1805}
1806
1807/*
1808 * Return the column where the text starts that is being completed
1809 */
1810 colnr_T
1811ins_compl_col(void)
1812{
1813 return compl_col;
1814}
1815
1816/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001817 * Return the length in bytes of the text being completed
1818 */
1819 int
1820ins_compl_len(void)
1821{
1822 return compl_length;
1823}
1824
1825/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001826 * Delete one character before the cursor and show the subset of the matches
1827 * that match the word that is now before the cursor.
1828 * Returns the character to be used, NUL if the work is done and another char
1829 * to be got from the user.
1830 */
1831 int
1832ins_compl_bs(void)
1833{
1834 char_u *line;
1835 char_u *p;
1836
1837 line = ml_get_curline();
1838 p = line + curwin->w_cursor.col;
1839 MB_PTR_BACK(line, p);
1840
1841 // Stop completion when the whole word was deleted. For Omni completion
1842 // allow the word to be deleted, we won't match everything.
1843 // Respect the 'backspace' option.
1844 if ((int)(p - line) - (int)compl_col < 0
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001845 || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni())
1846 || ctrl_x_mode_eval()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001847 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1848 - compl_length < 0))
1849 return K_BS;
1850
1851 // Deleted more than what was used to find matches or didn't finish
1852 // finding all matches: need to look for matches all over again.
1853 if (curwin->w_cursor.col <= compl_col + compl_length
1854 || ins_compl_need_restart())
1855 ins_compl_restart();
1856
1857 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001858 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001859 if (compl_leader == NULL)
1860 return K_BS;
1861
1862 ins_compl_new_leader();
1863 if (compl_shown_match != NULL)
1864 // Make sure current match is not a hidden item.
1865 compl_curr_match = compl_shown_match;
1866 return NUL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001867}
1868
1869/*
1870 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1871 * be called.
1872 */
1873 static int
1874ins_compl_need_restart(void)
1875{
1876 // Return TRUE if we didn't complete finding matches or when the
1877 // 'completefunc' returned "always" in the "refresh" dictionary item.
1878 return compl_was_interrupted
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001879 || ((ctrl_x_mode_function() || ctrl_x_mode_omni())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001880 && compl_opt_refresh_always);
1881}
1882
1883/*
1884 * Called after changing "compl_leader".
1885 * Show the popup menu with a different set of matches.
1886 * May also search for matches again if the previous search was interrupted.
1887 */
1888 static void
1889ins_compl_new_leader(void)
1890{
1891 ins_compl_del_pum();
1892 ins_compl_delete();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001893 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001894 compl_used_match = FALSE;
1895
1896 if (compl_started)
1897 ins_compl_set_original_text(compl_leader);
1898 else
1899 {
1900#ifdef FEAT_SPELL
1901 spell_bad_len = 0; // need to redetect bad word
1902#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001903 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001904 // the popup menu display the changed text before the cursor. Set
1905 // "compl_restarting" to avoid that the first match is inserted.
1906 pum_call_update_screen();
1907#ifdef FEAT_GUI
1908 if (gui.in_use)
1909 {
1910 // Show the cursor after the match, not after the redrawn text.
1911 setcursor();
1912 out_flush_cursor(FALSE, FALSE);
1913 }
1914#endif
1915 compl_restarting = TRUE;
1916 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1917 compl_cont_status = 0;
1918 compl_restarting = FALSE;
1919 }
1920
1921 compl_enter_selects = !compl_used_match;
1922
1923 // Show the popup menu with a different set of matches.
1924 ins_compl_show_pum();
1925
1926 // Don't let Enter select the original text when there is no popup menu.
1927 if (compl_match_array == NULL)
1928 compl_enter_selects = FALSE;
1929}
1930
1931/*
1932 * Return the length of the completion, from the completion start column to
1933 * the cursor column. Making sure it never goes below zero.
1934 */
1935 static int
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001936get_compl_len(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001937{
1938 int off = (int)curwin->w_cursor.col - (int)compl_col;
1939
1940 if (off < 0)
1941 return 0;
1942 return off;
1943}
1944
1945/*
1946 * Append one character to the match leader. May reduce the number of
1947 * matches.
1948 */
1949 void
1950ins_compl_addleader(int c)
1951{
1952 int cc;
1953
1954 if (stop_arrow() == FAIL)
1955 return;
1956 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1957 {
1958 char_u buf[MB_MAXBYTES + 1];
1959
1960 (*mb_char2bytes)(c, buf);
1961 buf[cc] = NUL;
1962 ins_char_bytes(buf, cc);
1963 if (compl_opt_refresh_always)
1964 AppendToRedobuff(buf);
1965 }
1966 else
1967 {
1968 ins_char(c);
1969 if (compl_opt_refresh_always)
1970 AppendCharToRedobuff(c);
1971 }
1972
1973 // If we didn't complete finding matches we must search again.
1974 if (ins_compl_need_restart())
1975 ins_compl_restart();
1976
1977 // When 'always' is set, don't reset compl_leader. While completing,
1978 // cursor doesn't point original position, changing compl_leader would
1979 // break redo.
1980 if (!compl_opt_refresh_always)
1981 {
1982 vim_free(compl_leader);
1983 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001984 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001985 if (compl_leader != NULL)
1986 ins_compl_new_leader();
1987 }
1988}
1989
1990/*
1991 * Setup for finding completions again without leaving CTRL-X mode. Used when
1992 * BS or a key was typed while still searching for matches.
1993 */
1994 static void
1995ins_compl_restart(void)
1996{
1997 ins_compl_free();
1998 compl_started = FALSE;
1999 compl_matches = 0;
2000 compl_cont_status = 0;
2001 compl_cont_mode = 0;
2002}
2003
2004/*
2005 * Set the first match, the original text.
2006 */
2007 static void
2008ins_compl_set_original_text(char_u *str)
2009{
2010 char_u *p;
2011
2012 // Replace the original text entry.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002013 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly
2014 // be at the last item for backward completion
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002015 if (match_at_original_text(compl_first_match)) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002016 {
2017 p = vim_strsave(str);
2018 if (p != NULL)
2019 {
2020 vim_free(compl_first_match->cp_str);
2021 compl_first_match->cp_str = p;
2022 }
2023 }
2024 else if (compl_first_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002025 && match_at_original_text(compl_first_match->cp_prev))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002026 {
2027 p = vim_strsave(str);
2028 if (p != NULL)
2029 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002030 vim_free(compl_first_match->cp_prev->cp_str);
2031 compl_first_match->cp_prev->cp_str = p;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002032 }
2033 }
2034}
2035
2036/*
2037 * Append one character to the match leader. May reduce the number of
2038 * matches.
2039 */
2040 void
2041ins_compl_addfrommatch(void)
2042{
2043 char_u *p;
2044 int len = (int)curwin->w_cursor.col - (int)compl_col;
2045 int c;
2046 compl_T *cp;
2047
2048 p = compl_shown_match->cp_str;
2049 if ((int)STRLEN(p) <= len) // the match is too short
2050 {
2051 // When still at the original match use the first entry that matches
2052 // the leader.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002053 if (!match_at_original_text(compl_shown_match))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002054 return;
2055
2056 p = NULL;
2057 for (cp = compl_shown_match->cp_next; cp != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002058 && !is_first_match(cp); cp = cp->cp_next)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002059 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002060 if (compl_leader == NULL
2061 || ins_compl_equal(cp, compl_leader,
2062 (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002063 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002064 p = cp->cp_str;
2065 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002066 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002067 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00002068 if (p == NULL || (int)STRLEN(p) <= len)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002069 return;
2070 }
2071 p += len;
2072 c = PTR2CHAR(p);
2073 ins_compl_addleader(c);
2074}
2075
2076/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002077 * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002078 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
2079 * compl_cont_mode and compl_cont_status.
2080 * Returns TRUE when the character is not to be inserted.
2081 */
2082 static int
2083set_ctrl_x_mode(int c)
2084{
2085 int retval = FALSE;
2086
2087 switch (c)
2088 {
2089 case Ctrl_E:
2090 case Ctrl_Y:
2091 // scroll the window one line up or down
2092 ctrl_x_mode = CTRL_X_SCROLL;
2093 if (!(State & REPLACE_FLAG))
2094 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2095 else
2096 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2097 edit_submode_pre = NULL;
2098 showmode();
2099 break;
2100 case Ctrl_L:
2101 // complete whole line
2102 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2103 break;
2104 case Ctrl_F:
2105 // complete filenames
2106 ctrl_x_mode = CTRL_X_FILES;
2107 break;
2108 case Ctrl_K:
zeertzjq5fb3aab2022-08-24 16:48:23 +01002109 // complete words from a dictionary
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002110 ctrl_x_mode = CTRL_X_DICTIONARY;
2111 break;
2112 case Ctrl_R:
2113 // Register insertion without exiting CTRL-X mode
2114 // Simply allow ^R to happen without affecting ^X mode
2115 break;
2116 case Ctrl_T:
2117 // complete words from a thesaurus
2118 ctrl_x_mode = CTRL_X_THESAURUS;
2119 break;
2120#ifdef FEAT_COMPL_FUNC
2121 case Ctrl_U:
2122 // user defined completion
2123 ctrl_x_mode = CTRL_X_FUNCTION;
2124 break;
2125 case Ctrl_O:
2126 // omni completion
2127 ctrl_x_mode = CTRL_X_OMNI;
2128 break;
2129#endif
2130 case 's':
2131 case Ctrl_S:
2132 // complete spelling suggestions
2133 ctrl_x_mode = CTRL_X_SPELL;
2134#ifdef FEAT_SPELL
2135 ++emsg_off; // Avoid getting the E756 error twice.
2136 spell_back_to_badword();
2137 --emsg_off;
2138#endif
2139 break;
2140 case Ctrl_RSB:
2141 // complete tag names
2142 ctrl_x_mode = CTRL_X_TAGS;
2143 break;
2144#ifdef FEAT_FIND_ID
2145 case Ctrl_I:
2146 case K_S_TAB:
2147 // complete keywords from included files
2148 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2149 break;
2150 case Ctrl_D:
2151 // complete definitions from included files
2152 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2153 break;
2154#endif
2155 case Ctrl_V:
2156 case Ctrl_Q:
2157 // complete vim commands
2158 ctrl_x_mode = CTRL_X_CMDLINE;
2159 break;
2160 case Ctrl_Z:
2161 // stop completion
2162 ctrl_x_mode = CTRL_X_NORMAL;
2163 edit_submode = NULL;
2164 showmode();
2165 retval = TRUE;
2166 break;
2167 case Ctrl_P:
2168 case Ctrl_N:
2169 // ^X^P means LOCAL expansion if nothing interrupted (eg we
2170 // just started ^X mode, or there were enough ^X's to cancel
2171 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2172 // do normal expansion when interrupting a different mode (say
2173 // ^X^F^X^P or ^P^X^X^P, see below)
2174 // nothing changes if interrupting mode 0, (eg, the flag
2175 // doesn't change when going to ADDING mode -- Acevedo
2176 if (!(compl_cont_status & CONT_INTRPT))
2177 compl_cont_status |= CONT_LOCAL;
2178 else if (compl_cont_mode != 0)
2179 compl_cont_status &= ~CONT_LOCAL;
2180 // FALLTHROUGH
2181 default:
2182 // If we have typed at least 2 ^X's... for modes != 0, we set
2183 // compl_cont_status = 0 (eg, as if we had just started ^X
2184 // mode).
2185 // For mode 0, we set "compl_cont_mode" to an impossible
2186 // value, in both cases ^X^X can be used to restart the same
2187 // mode (avoiding ADDING mode).
2188 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2189 // 'complete' and local ^P expansions respectively.
2190 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2191 // mode -- Acevedo
2192 if (c == Ctrl_X)
2193 {
2194 if (compl_cont_mode != 0)
2195 compl_cont_status = 0;
2196 else
2197 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2198 }
2199 ctrl_x_mode = CTRL_X_NORMAL;
2200 edit_submode = NULL;
2201 showmode();
2202 break;
2203 }
2204
2205 return retval;
2206}
2207
2208/*
2209 * Stop insert completion mode
2210 */
2211 static int
2212ins_compl_stop(int c, int prev_mode, int retval)
2213{
2214 char_u *ptr;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002215 int want_cindent;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002216
2217 // Get here when we have finished typing a sequence of ^N and
2218 // ^P or other completion characters in CTRL-X mode. Free up
2219 // memory that was used, and make sure we can redo the insert.
2220 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2221 {
2222 // If any of the original typed text has been changed, eg when
2223 // ignorecase is set, we must add back-spaces to the redo
2224 // buffer. We add as few as necessary to delete just the part
2225 // of the original text that has changed.
2226 // When using the longest match, edited the match or used
2227 // CTRL-E then don't use the current match.
2228 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2229 ptr = compl_curr_match->cp_str;
2230 else
2231 ptr = NULL;
2232 ins_compl_fixRedoBufForLeader(ptr);
2233 }
2234
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002235 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002236
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002237 // When completing whole lines: fix indent for 'cindent'.
2238 // Otherwise, break line if it's too long.
2239 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2240 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002241 // re-indent the current line
2242 if (want_cindent)
2243 {
2244 do_c_expr_indent();
2245 want_cindent = FALSE; // don't do it again
2246 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002247 }
2248 else
2249 {
2250 int prev_col = curwin->w_cursor.col;
2251
2252 // put the cursor on the last char, for 'tw' formatting
2253 if (prev_col > 0)
2254 dec_cursor();
2255 // only format when something was inserted
2256 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2257 insertchar(NUL, 0, -1);
2258 if (prev_col > 0
2259 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2260 inc_cursor();
2261 }
2262
2263 // If the popup menu is displayed pressing CTRL-Y means accepting
2264 // the selection without inserting anything. When
2265 // compl_enter_selects is set the Enter key does the same.
2266 if ((c == Ctrl_Y || (compl_enter_selects
2267 && (c == CAR || c == K_KENTER || c == NL)))
2268 && pum_visible())
2269 retval = TRUE;
2270
2271 // CTRL-E means completion is Ended, go back to the typed text.
2272 // but only do this, if the Popup is still visible
2273 if (c == Ctrl_E)
2274 {
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002275 char_u *p = NULL;
2276
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002277 ins_compl_delete();
2278 if (compl_leader != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002279 p = compl_leader;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002280 else if (compl_first_match != NULL)
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002281 p = compl_orig_text;
2282 if (p != NULL)
2283 {
2284 int compl_len = get_compl_len();
2285 int len = (int)STRLEN(p);
2286
2287 if (len > compl_len)
2288 ins_bytes_len(p + compl_len, len - compl_len);
2289 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002290 retval = TRUE;
2291 }
2292
2293 auto_format(FALSE, TRUE);
2294
2295 // Trigger the CompleteDonePre event to give scripts a chance to
2296 // act upon the completion before clearing the info, and restore
2297 // ctrl_x_mode, so that complete_info() can be used.
2298 ctrl_x_mode = prev_mode;
2299 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2300
2301 ins_compl_free();
2302 compl_started = FALSE;
2303 compl_matches = 0;
2304 if (!shortmess(SHM_COMPLETIONMENU))
2305 msg_clr_cmdline(); // necessary for "noshowmode"
2306 ctrl_x_mode = CTRL_X_NORMAL;
2307 compl_enter_selects = FALSE;
2308 if (edit_submode != NULL)
2309 {
2310 edit_submode = NULL;
2311 showmode();
2312 }
2313
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002314 if (c == Ctrl_C && cmdwin_type != 0)
2315 // Avoid the popup menu remains displayed when leaving the
2316 // command line window.
2317 update_screen(0);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002318 // Indent now if a key was typed that is in 'cinkeys'.
2319 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2320 do_c_expr_indent();
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002321 // Trigger the CompleteDone event to give scripts a chance to act
2322 // upon the end of completion.
2323 ins_apply_autocmds(EVENT_COMPLETEDONE);
2324
2325 return retval;
2326}
2327
2328/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002329 * Prepare for Insert mode completion, or stop it.
2330 * Called just after typing a character in Insert mode.
2331 * Returns TRUE when the character is not to be inserted;
2332 */
2333 int
2334ins_compl_prep(int c)
2335{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002336 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01002337 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002338
2339 // Forget any previous 'special' messages if this is actually
2340 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2341 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2342 edit_submode_extra = NULL;
2343
zeertzjq440d4cb2023-03-02 17:51:32 +00002344 // Ignore end of Select mode mapping and mouse scroll/movement.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002345 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
zeertzjq440d4cb2023-03-02 17:51:32 +00002346 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002347 || c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002348 return retval;
2349
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002350#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02002351 // Ignore mouse events in a popup window
2352 if (is_mouse_key(c))
2353 {
2354 // Ignore drag and release events, the position does not need to be in
2355 // the popup and it may have just closed.
2356 if (c == K_LEFTRELEASE
2357 || c == K_LEFTRELEASE_NM
2358 || c == K_MIDDLERELEASE
2359 || c == K_RIGHTRELEASE
2360 || c == K_X1RELEASE
2361 || c == K_X2RELEASE
2362 || c == K_LEFTDRAG
2363 || c == K_MIDDLEDRAG
2364 || c == K_RIGHTDRAG
2365 || c == K_X1DRAG
2366 || c == K_X2DRAG)
2367 return retval;
2368 if (popup_visible)
2369 {
2370 int row = mouse_row;
2371 int col = mouse_col;
2372 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
2373
2374 if (wp != NULL && WIN_IS_POPUP(wp))
2375 return retval;
2376 }
2377 }
2378#endif
2379
zeertzjqdca29d92021-08-31 19:12:51 +02002380 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
2381 {
2382 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
2383 || !vim_is_ctrl_x_key(c))
2384 {
2385 // Not starting another completion mode.
2386 ctrl_x_mode = CTRL_X_CMDLINE;
2387
2388 // CTRL-X CTRL-Z should stop completion without inserting anything
2389 if (c == Ctrl_Z)
2390 retval = TRUE;
2391 }
2392 else
2393 {
2394 ctrl_x_mode = CTRL_X_CMDLINE;
2395
2396 // Other CTRL-X keys first stop completion, then start another
2397 // completion mode.
2398 ins_compl_prep(' ');
2399 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2400 }
2401 }
2402
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002403 // Set "compl_get_longest" when finding the first matches.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002404 if (ctrl_x_mode_not_defined_yet()
2405 || (ctrl_x_mode_normal() && !compl_started))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002406 {
bfredl87af60c2022-09-24 11:17:51 +01002407 compl_get_longest = compl_longest;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002408 compl_used_match = TRUE;
2409
2410 }
2411
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002412 if (ctrl_x_mode_not_defined_yet())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002413 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2414 // it will be yet. Now we decide.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002415 retval = set_ctrl_x_mode(c);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002416 else if (ctrl_x_mode_not_default())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002417 {
2418 // We're already in CTRL-X mode, do we stay in it?
2419 if (!vim_is_ctrl_x_key(c))
2420 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002421 if (ctrl_x_mode_scroll())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002422 ctrl_x_mode = CTRL_X_NORMAL;
2423 else
2424 ctrl_x_mode = CTRL_X_FINISHED;
2425 edit_submode = NULL;
2426 }
2427 showmode();
2428 }
2429
2430 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2431 {
2432 // Show error message from attempted keyword completion (probably
2433 // 'Pattern not found') until another key is hit, then go back to
2434 // showing what mode we are in.
2435 showmode();
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002436 if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002437 && c != Ctrl_R && !ins_compl_pum_key(c))
2438 || ctrl_x_mode == CTRL_X_FINISHED)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00002439 retval = ins_compl_stop(c, prev_mode, retval);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002440 }
2441 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2442 // Trigger the CompleteDone event to give scripts a chance to act
2443 // upon the (possibly failed) completion.
2444 ins_apply_autocmds(EVENT_COMPLETEDONE);
2445
LemonBoy2bf52dd2022-04-09 18:17:34 +01002446 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002447
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002448 // reset continue_* if we left expansion-mode, if we stay they'll be
2449 // (re)set properly in ins_complete()
2450 if (!vim_is_ctrl_x_key(c))
2451 {
2452 compl_cont_status = 0;
2453 compl_cont_mode = 0;
2454 }
2455
2456 return retval;
2457}
2458
2459/*
2460 * Fix the redo buffer for the completion leader replacing some of the typed
2461 * text. This inserts backspaces and appends the changed text.
2462 * "ptr" is the known leader text or NUL.
2463 */
2464 static void
2465ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2466{
2467 int len;
2468 char_u *p;
2469 char_u *ptr = ptr_arg;
2470
2471 if (ptr == NULL)
2472 {
2473 if (compl_leader != NULL)
2474 ptr = compl_leader;
2475 else
2476 return; // nothing to do
2477 }
2478 if (compl_orig_text != NULL)
2479 {
2480 p = compl_orig_text;
2481 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2482 ;
2483 if (len > 0)
2484 len -= (*mb_head_off)(p, p + len);
2485 for (p += len; *p != NUL; MB_PTR_ADV(p))
2486 AppendCharToRedobuff(K_BS);
2487 }
2488 else
2489 len = 0;
2490 if (ptr != NULL)
2491 AppendToRedobuffLit(ptr + len, -1);
2492}
2493
2494/*
2495 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2496 * (depending on flag) starting from buf and looking for a non-scanned
2497 * buffer (other than curbuf). curbuf is special, if it is called with
2498 * buf=curbuf then it has to be the first call for a given flag/expansion.
2499 *
2500 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2501 */
2502 static buf_T *
2503ins_compl_next_buf(buf_T *buf, int flag)
2504{
2505 static win_T *wp = NULL;
2506
2507 if (flag == 'w') // just windows
2508 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002509 if (buf == curbuf || !win_valid(wp))
2510 // first call for this flag/expansion or window was closed
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002511 wp = curwin;
2512 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2513 && wp->w_buffer->b_scanned)
2514 ;
2515 buf = wp->w_buffer;
2516 }
2517 else
2518 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2519 // (unlisted buffers)
2520 // When completing whole lines skip unloaded buffers.
2521 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2522 && ((flag == 'U'
2523 ? buf->b_p_bl
2524 : (!buf->b_p_bl
2525 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2526 || buf->b_scanned))
2527 ;
2528 return buf;
2529}
2530
2531#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002532
2533# ifdef FEAT_EVAL
2534static callback_T cfu_cb; // 'completefunc' callback function
2535static callback_T ofu_cb; // 'omnifunc' callback function
2536static callback_T tsrfu_cb; // 'thesaurusfunc' callback function
2537# endif
2538
2539/*
2540 * Copy a global callback function to a buffer local callback.
2541 */
2542 static void
2543copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb)
2544{
2545 free_callback(bufcb);
2546 if (globcb->cb_name != NULL && *globcb->cb_name != NUL)
2547 copy_callback(bufcb, globcb);
2548}
2549
2550/*
2551 * Parse the 'completefunc' option value and set the callback function.
2552 * Invoked when the 'completefunc' option is set. The option value can be a
2553 * name of a function (string), or function(<name>) or funcref(<name>) or a
2554 * lambda expression.
2555 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002556 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002557did_set_completefunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002558{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559 if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL)
2560 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002561
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002562 set_buflocal_cfu_callback(curbuf);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002563
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002564 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002565}
2566
2567/*
2568 * Copy the global 'completefunc' callback function to the buffer-local
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002569 * 'completefunc' callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002570 */
2571 void
2572set_buflocal_cfu_callback(buf_T *buf UNUSED)
2573{
2574# ifdef FEAT_EVAL
2575 copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb);
2576# endif
2577}
2578
2579/*
2580 * Parse the 'omnifunc' option value and set the callback function.
2581 * Invoked when the 'omnifunc' option is set. The option value can be a
2582 * name of a function (string), or function(<name>) or funcref(<name>) or a
2583 * lambda expression.
2584 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002585 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002586did_set_omnifunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002587{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002588 if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL)
2589 return e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002590
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002591 set_buflocal_ofu_callback(curbuf);
2592 return NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002593}
2594
2595/*
2596 * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc'
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002597 * callback for "buf".
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002598 */
2599 void
2600set_buflocal_ofu_callback(buf_T *buf UNUSED)
2601{
2602# ifdef FEAT_EVAL
2603 copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb);
2604# endif
2605}
2606
2607/*
2608 * Parse the 'thesaurusfunc' option value and set the callback function.
2609 * Invoked when the 'thesaurusfunc' option is set. The option value can be a
2610 * name of a function (string), or function(<name>) or funcref(<name>) or a
2611 * lambda expression.
2612 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002613 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002614did_set_thesaurusfunc(optset_T *args UNUSED)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002615{
2616 int retval;
2617
2618 if (*curbuf->b_p_tsrfu != NUL)
2619 {
2620 // buffer-local option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002621 retval = option_set_callback_func(curbuf->b_p_tsrfu,
2622 &curbuf->b_tsrfu_cb);
2623 }
2624 else
2625 {
2626 // global option set
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002627 retval = option_set_callback_func(p_tsrfu, &tsrfu_cb);
2628 }
2629
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002630 return retval == FAIL ? e_invalid_argument : NULL;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002631}
2632
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002633/*
2634 * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002635 * "copyID" so that they are not garbage collected.
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002636 */
2637 int
2638set_ref_in_insexpand_funcs(int copyID)
2639{
2640 int abort = FALSE;
2641
2642 abort = set_ref_in_callback(&cfu_cb, copyID);
2643 abort = abort || set_ref_in_callback(&ofu_cb, copyID);
2644 abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
2645
2646 return abort;
2647}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002648
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002649/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002650 * Get the user-defined completion function name for completion "type"
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002651 */
2652 static char_u *
2653get_complete_funcname(int type)
2654{
2655 switch (type)
2656 {
2657 case CTRL_X_FUNCTION:
2658 return curbuf->b_p_cfu;
2659 case CTRL_X_OMNI:
2660 return curbuf->b_p_ofu;
2661 case CTRL_X_THESAURUS:
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01002662 return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002663 default:
2664 return (char_u *)"";
2665 }
2666}
2667
2668/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002669 * Get the callback to use for insert mode completion.
2670 */
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002671 static callback_T *
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002672get_insert_callback(int type)
2673{
2674 if (type == CTRL_X_FUNCTION)
2675 return &curbuf->b_cfu_cb;
2676 if (type == CTRL_X_OMNI)
2677 return &curbuf->b_ofu_cb;
2678 // CTRL_X_THESAURUS
2679 return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb;
2680}
2681
2682/*
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002683 * Execute user defined complete function 'completefunc', 'omnifunc' or
2684 * 'thesaurusfunc', and get matches in "matches".
2685 * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002686 */
2687 static void
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00002688expand_by_function(int type, char_u *base)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002689{
2690 list_T *matchlist = NULL;
2691 dict_T *matchdict = NULL;
2692 typval_T args[3];
2693 char_u *funcname;
2694 pos_T pos;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002695 callback_T *cb;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002696 typval_T rettv;
2697 int save_State = State;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002698 int retval;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002699
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01002700 funcname = get_complete_funcname(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002701 if (*funcname == NUL)
2702 return;
2703
2704 // Call 'completefunc' to obtain the list of matches.
2705 args[0].v_type = VAR_NUMBER;
2706 args[0].vval.v_number = 0;
2707 args[1].v_type = VAR_STRING;
2708 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2709 args[2].v_type = VAR_UNKNOWN;
2710
2711 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002712 // Lock the text to avoid weird things from happening. Also disallow
2713 // switching to another window, it should not be needed and may end up in
2714 // Insert mode in another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +01002715 ++textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002716
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002717 cb = get_insert_callback(type);
2718 retval = call_callback(cb, 0, &rettv, 2, args);
2719
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720 // Call a function, which returns a list or dict.
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002721 if (retval == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002722 {
2723 switch (rettv.v_type)
2724 {
2725 case VAR_LIST:
2726 matchlist = rettv.vval.v_list;
2727 break;
2728 case VAR_DICT:
2729 matchdict = rettv.vval.v_dict;
2730 break;
2731 case VAR_SPECIAL:
2732 if (rettv.vval.v_number == VVAL_NONE)
2733 compl_opt_suppress_empty = TRUE;
2734 // FALLTHROUGH
2735 default:
2736 // TODO: Give error message?
2737 clear_tv(&rettv);
2738 break;
2739 }
2740 }
zeertzjqcfe45652022-05-27 17:26:55 +01002741 --textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002742
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002743 curwin->w_cursor = pos; // restore the cursor position
2744 validate_cursor();
2745 if (!EQUAL_POS(curwin->w_cursor, pos))
2746 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002747 emsg(_(e_complete_function_deleted_text));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002748 goto theend;
2749 }
2750
2751 if (matchlist != NULL)
2752 ins_compl_add_list(matchlist);
2753 else if (matchdict != NULL)
2754 ins_compl_add_dict(matchdict);
2755
2756theend:
2757 // Restore State, it might have been changed.
2758 State = save_State;
2759
2760 if (matchdict != NULL)
2761 dict_unref(matchdict);
2762 if (matchlist != NULL)
2763 list_unref(matchlist);
2764}
2765#endif // FEAT_COMPL_FUNC
2766
2767#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2768/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002769 * Add a match to the list of matches from a typeval_T.
2770 * If the given string is already in the list of completions, then return
2771 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2772 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002773 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002774 */
2775 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002776ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002777{
2778 char_u *word;
2779 int dup = FALSE;
2780 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002781 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002782 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002783 typval_T user_data;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002784 int status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002785
Bram Moolenaar08928322020-01-04 14:32:48 +01002786 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002787 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2788 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01002789 word = dict_get_string(tv->vval.v_dict, "word", FALSE);
2790 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE);
2791 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
2792 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
2793 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2794 dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
2795 if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
2796 && dict_get_number(tv->vval.v_dict, "icase"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002797 flags |= CP_ICASE;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002798 if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL)
2799 dup = dict_get_number(tv->vval.v_dict, "dup");
2800 if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL)
2801 empty = dict_get_number(tv->vval.v_dict, "empty");
2802 if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL
2803 && dict_get_number(tv->vval.v_dict, "equal"))
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002804 flags |= CP_EQUAL;
2805 }
2806 else
2807 {
2808 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002809 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002810 }
2811 if (word == NULL || (!empty && *word == NUL))
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002812 {
2813 clear_tv(&user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002814 return FAIL;
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00002815 }
2816 status = ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
2817 if (status != OK)
2818 clear_tv(&user_data);
2819 return status;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002820}
2821
2822/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823 * Add completions from a list.
2824 */
2825 static void
2826ins_compl_add_list(list_T *list)
2827{
2828 listitem_T *li;
2829 int dir = compl_direction;
2830
2831 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002832 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002833 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002834 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002835 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002836 // if dir was BACKWARD then honor it just once
2837 dir = FORWARD;
2838 else if (did_emsg)
2839 break;
2840 }
2841}
2842
2843/*
2844 * Add completions from a dict.
2845 */
2846 static void
2847ins_compl_add_dict(dict_T *dict)
2848{
2849 dictitem_T *di_refresh;
2850 dictitem_T *di_words;
2851
2852 // Check for optional "refresh" item.
2853 compl_opt_refresh_always = FALSE;
2854 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2855 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2856 {
2857 char_u *v = di_refresh->di_tv.vval.v_string;
2858
2859 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2860 compl_opt_refresh_always = TRUE;
2861 }
2862
2863 // Add completions from a "words" list.
2864 di_words = dict_find(dict, (char_u *)"words", 5);
2865 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2866 ins_compl_add_list(di_words->di_tv.vval.v_list);
2867}
2868
2869/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002870 * Start completion for the complete() function.
2871 * "startcol" is where the matched text starts (1 is first column).
2872 * "list" is the list of matches.
2873 */
2874 static void
2875set_completion(colnr_T startcol, list_T *list)
2876{
2877 int save_w_wrow = curwin->w_wrow;
2878 int save_w_leftcol = curwin->w_leftcol;
2879 int flags = CP_ORIGINAL_TEXT;
2880
2881 // If already doing completions stop it.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002882 if (ctrl_x_mode_not_default())
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002883 ins_compl_prep(' ');
2884 ins_compl_clear();
2885 ins_compl_free();
bfredl87af60c2022-09-24 11:17:51 +01002886 compl_get_longest = compl_longest;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002887
2888 compl_direction = FORWARD;
2889 if (startcol > curwin->w_cursor.col)
2890 startcol = curwin->w_cursor.col;
2891 compl_col = startcol;
2892 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2893 // compl_pattern doesn't need to be set
2894 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2895 if (p_ic)
2896 flags |= CP_ICASE;
2897 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002898 -1, NULL, NULL, NULL, 0,
2899 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002900 return;
2901
2902 ctrl_x_mode = CTRL_X_EVAL;
2903
2904 ins_compl_add_list(list);
2905 compl_matches = ins_compl_make_cyclic();
2906 compl_started = TRUE;
2907 compl_used_match = TRUE;
2908 compl_cont_status = 0;
2909
2910 compl_curr_match = compl_first_match;
bfredl87af60c2022-09-24 11:17:51 +01002911 int no_select = compl_no_select || compl_longest;
2912 if (compl_no_insert || no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002913 {
2914 ins_complete(K_DOWN, FALSE);
bfredl87af60c2022-09-24 11:17:51 +01002915 if (no_select)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002916 // Down/Up has no real effect.
2917 ins_complete(K_UP, FALSE);
2918 }
2919 else
2920 ins_complete(Ctrl_N, FALSE);
2921 compl_enter_selects = compl_no_insert;
2922
2923 // Lazily show the popup menu, unless we got interrupted.
2924 if (!compl_interrupted)
2925 show_pum(save_w_wrow, save_w_leftcol);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002926 may_trigger_modechanged();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002927 out_flush();
2928}
2929
2930/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002931 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002932 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002933 void
2934f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002935{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002936 int startcol;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002937
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002938 if (in_vim9script()
2939 && (check_for_number_arg(argvars, 0) == FAIL
2940 || check_for_list_arg(argvars, 1) == FAIL))
2941 return;
2942
Bram Moolenaar24959102022-05-07 20:01:16 +01002943 if ((State & MODE_INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002944 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002945 emsg(_(e_complete_can_only_be_used_in_insert_mode));
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002946 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002947 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002948
2949 // Check for undo allowed here, because if something was already inserted
2950 // the line was already saved for undo and this check isn't done.
2951 if (!undo_allowed())
2952 return;
2953
Bram Moolenaard83392a2022-09-01 12:22:46 +01002954 if (check_for_nonnull_list_arg(argvars, 1) != FAIL)
Bram Moolenaarff06f282020-04-21 22:01:14 +02002955 {
2956 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2957 if (startcol > 0)
2958 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002959 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002960}
2961
2962/*
2963 * "complete_add()" function
2964 */
2965 void
2966f_complete_add(typval_T *argvars, typval_T *rettv)
2967{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002968 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002969 return;
2970
Bram Moolenaar440cf092021-04-03 20:13:30 +02002971 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002972}
2973
2974/*
2975 * "complete_check()" function
2976 */
2977 void
2978f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2979{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002980 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002981 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002982
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002983 ins_compl_check_keys(0, TRUE);
2984 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002985
2986 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002987}
2988
2989/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002990 * Return Insert completion mode name string
2991 */
2992 static char_u *
2993ins_compl_mode(void)
2994{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002995 if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002996 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2997
2998 return (char_u *)"";
2999}
3000
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00003001/*
3002 * Assign the sequence number to all the completion matches which don't have
3003 * one assigned yet.
3004 */
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003005 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003006ins_compl_update_sequence_numbers(void)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003007{
3008 int number = 0;
3009 compl_T *match;
3010
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003011 if (compl_dir_forward())
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003012 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003013 // Search backwards for the first valid (!= -1) number.
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003014 // This should normally succeed already at the first loop
3015 // cycle, so it's fast!
3016 for (match = compl_curr_match->cp_prev; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003017 && !is_first_match(match); match = match->cp_prev)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003018 if (match->cp_number != -1)
3019 {
3020 number = match->cp_number;
3021 break;
3022 }
3023 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003024 // go up and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003025 for (match = match->cp_next;
3026 match != NULL && match->cp_number == -1;
3027 match = match->cp_next)
3028 match->cp_number = ++number;
3029 }
3030 else // BACKWARD
3031 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003032 // Search forwards (upwards) for the first valid (!= -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003033 // number. This should normally succeed already at the
3034 // first loop cycle, so it's fast!
3035 for (match = compl_curr_match->cp_next; match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003036 && !is_first_match(match); match = match->cp_next)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003037 if (match->cp_number != -1)
3038 {
3039 number = match->cp_number;
3040 break;
3041 }
3042 if (match != NULL)
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003043 // go down and assign all numbers which are not assigned yet
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003044 for (match = match->cp_prev; match
3045 && match->cp_number == -1;
3046 match = match->cp_prev)
3047 match->cp_number = ++number;
3048 }
3049}
3050
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003051/*
3052 * Get complete information
3053 */
3054 static void
3055get_complete_info(list_T *what_list, dict_T *retdict)
3056{
3057 int ret = OK;
3058 listitem_T *item;
3059#define CI_WHAT_MODE 0x01
3060#define CI_WHAT_PUM_VISIBLE 0x02
3061#define CI_WHAT_ITEMS 0x04
3062#define CI_WHAT_SELECTED 0x08
3063#define CI_WHAT_INSERTED 0x10
3064#define CI_WHAT_ALL 0xff
3065 int what_flag;
3066
3067 if (what_list == NULL)
3068 what_flag = CI_WHAT_ALL;
3069 else
3070 {
3071 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003072 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003073 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003074 {
3075 char_u *what = tv_get_string(&item->li_tv);
3076
3077 if (STRCMP(what, "mode") == 0)
3078 what_flag |= CI_WHAT_MODE;
3079 else if (STRCMP(what, "pum_visible") == 0)
3080 what_flag |= CI_WHAT_PUM_VISIBLE;
3081 else if (STRCMP(what, "items") == 0)
3082 what_flag |= CI_WHAT_ITEMS;
3083 else if (STRCMP(what, "selected") == 0)
3084 what_flag |= CI_WHAT_SELECTED;
3085 else if (STRCMP(what, "inserted") == 0)
3086 what_flag |= CI_WHAT_INSERTED;
3087 }
3088 }
3089
3090 if (ret == OK && (what_flag & CI_WHAT_MODE))
3091 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3092
3093 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3094 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3095
Girish Palya8950bf72024-03-20 20:07:29 +01003096 if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003097 {
3098 list_T *li;
Girish Palya8950bf72024-03-20 20:07:29 +01003099 dict_T *di;
3100 compl_T *match;
3101 int selected_idx = -1;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003102
Girish Palya8950bf72024-03-20 20:07:29 +01003103 if (what_flag & CI_WHAT_ITEMS)
3104 {
3105 li = list_alloc();
3106 if (li == NULL)
3107 return;
3108 ret = dict_add_list(retdict, "items", li);
3109 }
3110 if (ret == OK && what_flag & CI_WHAT_SELECTED)
3111 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
3112 ins_compl_update_sequence_numbers();
3113 if (ret == OK && compl_first_match != NULL)
3114 {
3115 int list_idx = 0;
3116 match = compl_first_match;
3117 do
3118 {
3119 if (!match_at_original_text(match))
3120 {
3121 if (what_flag & CI_WHAT_ITEMS)
3122 {
3123 di = dict_alloc();
3124 if (di == NULL)
3125 return;
3126 ret = list_append_dict(li, di);
3127 if (ret != OK)
3128 return;
3129 dict_add_string(di, "word", match->cp_str);
3130 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3131 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3132 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3133 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3134 if (match->cp_user_data.v_type == VAR_UNKNOWN)
3135 // Add an empty string for backwards compatibility
3136 dict_add_string(di, "user_data", (char_u *)"");
3137 else
3138 dict_add_tv(di, "user_data", &match->cp_user_data);
3139 }
3140 if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3141 selected_idx = list_idx;
3142 list_idx += 1;
3143 }
3144 match = match->cp_next;
3145 }
3146 while (match != NULL && !is_first_match(match));
3147 }
3148 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3149 ret = dict_add_number(retdict, "selected", selected_idx);
Bram Moolenaarf9d51352020-10-26 19:22:42 +01003150 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003151
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01003152 if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3153 {
3154 // TODO
3155 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003156}
3157
3158/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003159 * "complete_info()" function
3160 */
3161 void
3162f_complete_info(typval_T *argvars, typval_T *rettv)
3163{
3164 list_T *what_list = NULL;
3165
Bram Moolenaar93a10962022-06-16 11:42:09 +01003166 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003167 return;
3168
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003169 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
3170 return;
3171
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003172 if (argvars[0].v_type != VAR_UNKNOWN)
3173 {
Bram Moolenaard83392a2022-09-01 12:22:46 +01003174 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003175 return;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02003176 what_list = argvars[0].vval.v_list;
3177 }
3178 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003179}
3180#endif
3181
3182/*
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003183 * Returns TRUE when using a user-defined function for thesaurus completion.
3184 */
3185 static int
3186thesaurus_func_complete(int type UNUSED)
3187{
3188#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01003189 return type == CTRL_X_THESAURUS
3190 && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +01003191#else
3192 return FALSE;
3193#endif
3194}
3195
3196/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003197 * Return value of process_next_cpt_value()
3198 */
3199enum
3200{
3201 INS_COMPL_CPT_OK = 1,
3202 INS_COMPL_CPT_CONT,
3203 INS_COMPL_CPT_END
3204};
3205
3206/*
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003207 * state information used for getting the next set of insert completion
3208 * matches.
3209 */
3210typedef struct
3211{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003212 char_u *e_cpt_copy; // copy of 'complete'
3213 char_u *e_cpt; // current entry in "e_cpt_copy"
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003214 buf_T *ins_buf; // buffer being scanned
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003215 pos_T *cur_match_pos; // current match position
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003216 pos_T prev_match_pos; // previous match position
3217 int set_match_pos; // save first_match_pos/last_match_pos
3218 pos_T first_match_pos; // first match position
3219 pos_T last_match_pos; // last match position
3220 int found_all; // found all matches of a certain type.
3221 char_u *dict; // dictionary file to search
3222 int dict_f; // "dict" is an exact file name or not
3223} ins_compl_next_state_T;
3224
3225/*
3226 * Process the next 'complete' option value in st->e_cpt.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003227 *
3228 * If successful, the arguments are set as below:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003229 * st->cpt - pointer to the next option value in "st->cpt"
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003230 * compl_type_arg - type of insert mode completion to use
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003231 * st->found_all - all matches of this type are found
3232 * st->ins_buf - search for completions in this buffer
3233 * st->first_match_pos - position of the first completion match
3234 * st->last_match_pos - position of the last completion match
3235 * st->set_match_pos - TRUE if the first match position should be saved to
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003236 * avoid loops after the search wraps around.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003237 * st->dict - name of the dictionary or thesaurus file to search
3238 * st->dict_f - flag specifying whether "dict" is an exact file name or not
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003239 *
3240 * Returns INS_COMPL_CPT_OK if the next value is processed successfully.
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00003241 * Returns INS_COMPL_CPT_CONT to skip the current completion source matching
3242 * the "st->e_cpt" option value and process the next matching source.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003243 * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed.
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003244 */
3245 static int
3246process_next_cpt_value(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003247 ins_compl_next_state_T *st,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003248 int *compl_type_arg,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003249 pos_T *start_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003250{
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003251 int compl_type = -1;
3252 int status = INS_COMPL_CPT_OK;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003253
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003254 st->found_all = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003255
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003256 while (*st->e_cpt == ',' || *st->e_cpt == ' ')
3257 st->e_cpt++;
3258
3259 if (*st->e_cpt == '.' && !curbuf->b_scanned)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003260 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003261 st->ins_buf = curbuf;
3262 st->first_match_pos = *start_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003263 // Move the cursor back one character so that ^N can match the
3264 // word immediately after the cursor.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003265 if (ctrl_x_mode_normal() && dec(&st->first_match_pos) < 0)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003266 {
3267 // Move the cursor to after the last character in the
3268 // buffer, so that word at start of buffer is found
3269 // correctly.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003270 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003271 st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003272 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003273 st->last_match_pos = st->first_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003274 compl_type = 0;
3275
3276 // Remember the first match so that the loop stops when we
3277 // wrap and come back there a second time.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003278 st->set_match_pos = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003279 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003280 else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003281 && (st->ins_buf = ins_compl_next_buf(
3282 st->ins_buf, *st->e_cpt)) != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003283 {
3284 // Scan a buffer, but not the current one.
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003285 if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003286 {
3287 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003288 st->first_match_pos.col = st->last_match_pos.col = 0;
3289 st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1;
3290 st->last_match_pos.lnum = 0;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003291 compl_type = 0;
3292 }
3293 else // unloaded buffer, scan like dictionary
3294 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003295 st->found_all = TRUE;
3296 if (st->ins_buf->b_fname == NULL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003297 {
3298 status = INS_COMPL_CPT_CONT;
3299 goto done;
3300 }
3301 compl_type = CTRL_X_DICTIONARY;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003302 st->dict = st->ins_buf->b_fname;
3303 st->dict_f = DICT_EXACT;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003304 }
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003305 if (!shortmess(SHM_COMPLETIONSCAN))
3306 {
3307 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3308 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
3309 st->ins_buf->b_fname == NULL
3310 ? buf_spname(st->ins_buf)
3311 : st->ins_buf->b_sfname == NULL
3312 ? st->ins_buf->b_fname
3313 : st->ins_buf->b_sfname);
3314 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3315 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003316 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003317 else if (*st->e_cpt == NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003318 status = INS_COMPL_CPT_END;
3319 else
3320 {
3321 if (ctrl_x_mode_line_or_eval())
3322 compl_type = -1;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003323 else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003324 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003325 if (*st->e_cpt == 'k')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003326 compl_type = CTRL_X_DICTIONARY;
3327 else
3328 compl_type = CTRL_X_THESAURUS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003329 if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003330 {
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003331 st->dict = st->e_cpt;
3332 st->dict_f = DICT_FIRST;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003333 }
3334 }
3335#ifdef FEAT_FIND_ID
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003336 else if (*st->e_cpt == 'i')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003337 compl_type = CTRL_X_PATH_PATTERNS;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003338 else if (*st->e_cpt == 'd')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003339 compl_type = CTRL_X_PATH_DEFINES;
3340#endif
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003341 else if (*st->e_cpt == ']' || *st->e_cpt == 't')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003342 {
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003343 compl_type = CTRL_X_TAGS;
=?UTF-8?q?Bj=C3=B6rn=20Linse?=91ccbad2022-10-13 12:51:13 +01003344 if (!shortmess(SHM_COMPLETIONSCAN))
3345 {
3346 msg_hist_off = TRUE; // reset in msg_trunc_attr()
3347 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
3348 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
3349 }
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003350 }
3351 else
3352 compl_type = -1;
3353
3354 // in any case e_cpt is advanced to the next entry
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003355 (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003356
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003357 st->found_all = TRUE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003358 if (compl_type == -1)
3359 status = INS_COMPL_CPT_CONT;
3360 }
3361
3362done:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003363 *compl_type_arg = compl_type;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003364 return status;
3365}
3366
3367#ifdef FEAT_FIND_ID
3368/*
3369 * Get the next set of identifiers or defines matching "compl_pattern" in
3370 * included files.
3371 */
3372 static void
3373get_next_include_file_completion(int compl_type)
3374{
3375 find_pattern_in_path(compl_pattern, compl_direction,
3376 (int)STRLEN(compl_pattern), FALSE, FALSE,
3377 (compl_type == CTRL_X_PATH_DEFINES
3378 && !(compl_cont_status & CONT_SOL))
3379 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
Colin Kennedy21570352024-03-03 16:16:47 +01003380 (linenr_T)1, (linenr_T)MAXLNUM, FALSE);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003381}
3382#endif
3383
3384/*
3385 * Get the next set of words matching "compl_pattern" in dictionary or
3386 * thesaurus files.
3387 */
3388 static void
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003389get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003390{
3391#ifdef FEAT_COMPL_FUNC
3392 if (thesaurus_func_complete(compl_type))
3393 expand_by_function(compl_type, compl_pattern);
3394 else
3395#endif
3396 ins_compl_dictionaries(
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003397 dict != NULL ? dict
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003398 : (compl_type == CTRL_X_THESAURUS
3399 ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
3400 : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
3401 compl_pattern,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003402 dict != NULL ? dict_f : 0,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003403 compl_type == CTRL_X_THESAURUS);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003404}
3405
3406/*
3407 * Get the next set of tag names matching "compl_pattern".
3408 */
3409 static void
3410get_next_tag_completion(void)
3411{
3412 int save_p_ic;
3413 char_u **matches;
3414 int num_matches;
3415
3416 // set p_ic according to p_ic, p_scs and pat for find_tags().
3417 save_p_ic = p_ic;
3418 p_ic = ignorecase(compl_pattern);
3419
3420 // Find up to TAG_MANY matches. Avoids that an enormous number
3421 // of matches is found when compl_pattern is empty
3422 g_tag_at_cursor = TRUE;
3423 if (find_tags(compl_pattern, &num_matches, &matches,
3424 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003425 | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003426 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3427 ins_compl_add_matches(num_matches, matches, p_ic);
3428 g_tag_at_cursor = FALSE;
3429 p_ic = save_p_ic;
3430}
3431
3432/*
3433 * Get the next set of filename matching "compl_pattern".
3434 */
3435 static void
3436get_next_filename_completion(void)
3437{
3438 char_u **matches;
3439 int num_matches;
3440
3441 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
3442 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK)
3443 return;
3444
3445 // May change home directory back to "~".
3446 tilde_replace(compl_pattern, num_matches, matches);
3447#ifdef BACKSLASH_IN_FILENAME
3448 if (curbuf->b_p_csl[0] != NUL)
3449 {
3450 int i;
3451
3452 for (i = 0; i < num_matches; ++i)
3453 {
3454 char_u *ptr = matches[i];
3455
3456 while (*ptr != NUL)
3457 {
3458 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
3459 *ptr = '/';
3460 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
3461 *ptr = '\\';
3462 ptr += (*mb_ptr2len)(ptr);
3463 }
3464 }
3465 }
3466#endif
3467 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
3468}
3469
3470/*
3471 * Get the next set of command-line completions matching "compl_pattern".
3472 */
3473 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003474get_next_cmdline_completion(void)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003475{
3476 char_u **matches;
3477 int num_matches;
3478
3479 if (expand_cmdline(&compl_xp, compl_pattern,
3480 (int)STRLEN(compl_pattern),
3481 &num_matches, &matches) == EXPAND_OK)
3482 ins_compl_add_matches(num_matches, matches, FALSE);
3483}
3484
3485/*
3486 * Get the next set of spell suggestions matching "compl_pattern".
3487 */
3488 static void
3489get_next_spell_completion(linenr_T lnum UNUSED)
3490{
3491#ifdef FEAT_SPELL
3492 char_u **matches;
3493 int num_matches;
3494
3495 num_matches = expand_spelling(lnum, compl_pattern, &matches);
3496 if (num_matches > 0)
3497 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar8e7cc6b2021-12-30 10:32:25 +00003498 else
3499 vim_free(matches);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003500#endif
3501}
3502
3503/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003504 * Return the next word or line from buffer "ins_buf" at position
3505 * "cur_match_pos" for completion. The length of the match is set in "len".
3506 */
3507 static char_u *
zeertzjq440d4cb2023-03-02 17:51:32 +00003508ins_compl_get_next_word_or_line(
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003509 buf_T *ins_buf, // buffer being scanned
3510 pos_T *cur_match_pos, // current match position
3511 int *match_len,
3512 int *cont_s_ipos) // next ^X<> will set initial_pos
3513{
3514 char_u *ptr;
3515 int len;
3516
3517 *match_len = 0;
3518 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3519 cur_match_pos->col;
3520 if (ctrl_x_mode_line_or_eval())
3521 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003522 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003523 {
3524 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3525 return NULL;
3526 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3527 if (!p_paste)
3528 ptr = skipwhite(ptr);
3529 }
3530 len = (int)STRLEN(ptr);
3531 }
3532 else
3533 {
3534 char_u *tmp_ptr = ptr;
3535
Bram Moolenaara6f9e302022-07-28 21:51:37 +01003536 if (compl_status_adding() && compl_length <= (int)STRLEN(tmp_ptr))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003537 {
3538 tmp_ptr += compl_length;
3539 // Skip if already inside a word.
3540 if (vim_iswordp(tmp_ptr))
3541 return NULL;
3542 // Find start of next word.
3543 tmp_ptr = find_word_start(tmp_ptr);
3544 }
3545 // Find end of this word.
3546 tmp_ptr = find_word_end(tmp_ptr);
3547 len = (int)(tmp_ptr - ptr);
3548
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003549 if (compl_status_adding() && len == compl_length)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003550 {
3551 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3552 {
3553 // Try next line, if any. the new word will be
3554 // "join" as if the normal command "J" was used.
3555 // IOSIZE is always greater than
3556 // compl_length, so the next STRNCPY always
3557 // works -- Acevedo
3558 STRNCPY(IObuff, ptr, len);
3559 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3560 tmp_ptr = ptr = skipwhite(ptr);
3561 // Find start of next word.
3562 tmp_ptr = find_word_start(tmp_ptr);
3563 // Find end of next word.
3564 tmp_ptr = find_word_end(tmp_ptr);
3565 if (tmp_ptr > ptr)
3566 {
3567 if (*ptr != ')' && IObuff[len - 1] != TAB)
3568 {
3569 if (IObuff[len - 1] != ' ')
3570 IObuff[len++] = ' ';
3571 // IObuf =~ "\k.* ", thus len >= 2
3572 if (p_js
3573 && (IObuff[len - 2] == '.'
3574 || (vim_strchr(p_cpo, CPO_JOINSP)
3575 == NULL
3576 && (IObuff[len - 2] == '?'
3577 || IObuff[len - 2] == '!'))))
3578 IObuff[len++] = ' ';
3579 }
3580 // copy as much as possible of the new word
3581 if (tmp_ptr - ptr >= IOSIZE - len)
3582 tmp_ptr = ptr + IOSIZE - len - 1;
3583 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3584 len += (int)(tmp_ptr - ptr);
3585 *cont_s_ipos = TRUE;
3586 }
3587 IObuff[len] = NUL;
3588 ptr = IObuff;
3589 }
3590 if (len == compl_length)
3591 return NULL;
3592 }
3593 }
3594
3595 *match_len = len;
3596 return ptr;
3597}
3598
3599/*
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003600 * Get the next set of words matching "compl_pattern" for default completion(s)
3601 * (normal ^P/^N and ^X^L).
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003602 * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the
3603 * position "st->start_pos" in the "compl_direction" direction. If
3604 * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and
3605 * "st->last_match_pos".
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003606 * Returns OK if a new next match is found, otherwise returns FAIL.
3607 */
3608 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003609get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003610{
3611 int found_new_match = FAIL;
3612 int save_p_scs;
3613 int save_p_ws;
3614 int looped_around = FALSE;
3615 char_u *ptr;
3616 int len;
3617
3618 // If 'infercase' is set, don't use 'smartcase' here
3619 save_p_scs = p_scs;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003620 if (st->ins_buf->b_p_inf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003621 p_scs = FALSE;
3622
3623 // Buffers other than curbuf are scanned from the beginning or the
3624 // end but never from the middle, thus setting nowrapscan in this
3625 // buffer is a good idea, on the other hand, we always set
3626 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3627 save_p_ws = p_ws;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003628 if (st->ins_buf != curbuf)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003629 p_ws = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003630 else if (*st->e_cpt == '.')
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003631 p_ws = TRUE;
3632 looped_around = FALSE;
3633 for (;;)
3634 {
3635 int cont_s_ipos = FALSE;
3636
3637 ++msg_silent; // Don't want messages for wrapscan.
3638
3639 // ctrl_x_mode_line_or_eval() || word-wise search that
3640 // has added a word that was at the beginning of the line
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003641 if (ctrl_x_mode_line_or_eval() || (compl_cont_status & CONT_SOL))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003642 found_new_match = search_for_exact_line(st->ins_buf,
3643 st->cur_match_pos, compl_direction, compl_pattern);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003644 else
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003645 found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos,
3646 NULL, compl_direction, compl_pattern, 1L,
3647 SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL);
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003648 --msg_silent;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003649 if (!compl_started || st->set_match_pos)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003650 {
3651 // set "compl_started" even on fail
3652 compl_started = TRUE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003653 st->first_match_pos = *st->cur_match_pos;
3654 st->last_match_pos = *st->cur_match_pos;
3655 st->set_match_pos = FALSE;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003656 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003657 else if (st->first_match_pos.lnum == st->last_match_pos.lnum
3658 && st->first_match_pos.col == st->last_match_pos.col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003659 {
3660 found_new_match = FAIL;
3661 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003662 else if (compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003663 && (st->prev_match_pos.lnum > st->cur_match_pos->lnum
3664 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3665 && st->prev_match_pos.col >= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003666 {
3667 if (looped_around)
3668 found_new_match = FAIL;
3669 else
3670 looped_around = TRUE;
3671 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003672 else if (!compl_dir_forward()
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003673 && (st->prev_match_pos.lnum < st->cur_match_pos->lnum
3674 || (st->prev_match_pos.lnum == st->cur_match_pos->lnum
3675 && st->prev_match_pos.col <= st->cur_match_pos->col)))
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003676 {
3677 if (looped_around)
3678 found_new_match = FAIL;
3679 else
3680 looped_around = TRUE;
3681 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003682 st->prev_match_pos = *st->cur_match_pos;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003683 if (found_new_match == FAIL)
3684 break;
3685
3686 // when ADDING, the text before the cursor matches, skip it
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003687 if (compl_status_adding() && st->ins_buf == curbuf
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003688 && start_pos->lnum == st->cur_match_pos->lnum
3689 && start_pos->col == st->cur_match_pos->col)
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003690 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003691
zeertzjq440d4cb2023-03-02 17:51:32 +00003692 ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
3693 &len, &cont_s_ipos);
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003694 if (ptr == NULL)
3695 continue;
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003696
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003697 if (ins_compl_add_infercase(ptr, len, p_ic,
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003698 st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname,
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003699 0, cont_s_ipos) != NOTDONE)
3700 {
3701 found_new_match = OK;
3702 break;
3703 }
3704 }
3705 p_scs = save_p_scs;
3706 p_ws = save_p_ws;
3707
3708 return found_new_match;
3709}
3710
3711/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003712 * get the next set of completion matches for "type".
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003713 * Returns TRUE if a new match is found. Otherwise returns FALSE.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003714 */
3715 static int
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003716get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003717{
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003718 int found_new_match = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003719
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003720 switch (type)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003721 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003722 case -1:
3723 break;
3724#ifdef FEAT_FIND_ID
3725 case CTRL_X_PATH_PATTERNS:
3726 case CTRL_X_PATH_DEFINES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003727 get_next_include_file_completion(type);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003728 break;
3729#endif
3730
3731 case CTRL_X_DICTIONARY:
3732 case CTRL_X_THESAURUS:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003733 get_next_dict_tsr_completion(type, st->dict, st->dict_f);
3734 st->dict = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003735 break;
3736
3737 case CTRL_X_TAGS:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003738 get_next_tag_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003739 break;
3740
3741 case CTRL_X_FILES:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003742 get_next_filename_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003743 break;
3744
3745 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02003746 case CTRL_X_CMDLINE_CTRL_X:
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00003747 get_next_cmdline_completion();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003748 break;
3749
3750#ifdef FEAT_COMPL_FUNC
3751 case CTRL_X_FUNCTION:
3752 case CTRL_X_OMNI:
3753 expand_by_function(type, compl_pattern);
3754 break;
3755#endif
3756
3757 case CTRL_X_SPELL:
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003758 get_next_spell_completion(st->first_match_pos.lnum);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003759 break;
3760
3761 default: // normal ^P/^N and ^X^L
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003762 found_new_match = get_next_default_completion(st, ini);
3763 if (found_new_match == FAIL && st->ins_buf == curbuf)
3764 st->found_all = TRUE;
3765 }
3766
3767 // check if compl_curr_match has changed, (e.g. other type of
3768 // expansion added something)
3769 if (type != 0 && compl_curr_match != compl_old_match)
3770 found_new_match = OK;
3771
3772 return found_new_match;
3773}
3774
3775/*
3776 * Get the next expansion(s), using "compl_pattern".
3777 * The search starts at position "ini" in curbuf and in the direction
3778 * compl_direction.
3779 * When "compl_started" is FALSE start at that position, otherwise continue
3780 * where we stopped searching before.
3781 * This may return before finding all the matches.
3782 * Return the total number of matches or -1 if still unknown -- Acevedo
3783 */
3784 static int
3785ins_compl_get_exp(pos_T *ini)
3786{
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003787 static ins_compl_next_state_T st;
3788 static int st_cleared = FALSE;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003789 int i;
3790 int found_new_match;
3791 int type = ctrl_x_mode;
3792
3793 if (!compl_started)
3794 {
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003795 buf_T *buf;
3796
3797 FOR_ALL_BUFFERS(buf)
3798 buf->b_scanned = 0;
3799 if (!st_cleared)
3800 {
3801 CLEAR_FIELD(st);
3802 st_cleared = TRUE;
3803 }
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003804 st.found_all = FALSE;
3805 st.ins_buf = curbuf;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003806 vim_free(st.e_cpt_copy);
Christian Brabandtee17b6f2023-09-09 11:23:50 +02003807 // Make a copy of 'complete', in case the buffer is wiped out.
Bram Moolenaar0ff01832022-09-24 19:20:30 +01003808 st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL)
3809 ? (char_u *)"." : curbuf->b_p_cpt);
3810 st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003811 st.last_match_pos = st.first_match_pos = *ini;
3812 }
3813 else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
3814 st.ins_buf = curbuf; // In case the buffer was wiped out.
3815
3816 compl_old_match = compl_curr_match; // remember the last current match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003817 st.cur_match_pos = (compl_dir_forward())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003818 ? &st.last_match_pos : &st.first_match_pos;
3819
3820 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
3821 for (;;)
3822 {
3823 found_new_match = FAIL;
3824 st.set_match_pos = FALSE;
3825
3826 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
3827 // or if found_all says this entry is done. For ^X^L only use the
3828 // entries from 'complete' that look in loaded buffers.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003829 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003830 && (!compl_started || st.found_all))
3831 {
3832 int status = process_next_cpt_value(&st, &type, ini);
3833
3834 if (status == INS_COMPL_CPT_END)
3835 break;
3836 if (status == INS_COMPL_CPT_CONT)
3837 continue;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003838 }
3839
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003840 // If complete() was called then compl_pattern has been reset. The
3841 // following won't work then, bail out.
3842 if (compl_pattern == NULL)
3843 break;
3844
3845 // get the next set of completion matches
3846 found_new_match = get_next_completion_match(type, &st, ini);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003847
3848 // break the loop for specialized modes (use 'complete' just for the
3849 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3850 // match
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003851 if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())
3852 || found_new_match != FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003853 {
3854 if (got_int)
3855 break;
3856 // Fill the popup menu as soon as possible.
3857 if (type != -1)
3858 ins_compl_check_keys(0, FALSE);
3859
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003860 if ((ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003861 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3862 break;
3863 compl_started = TRUE;
3864 }
3865 else
3866 {
3867 // Mark a buffer scanned when it has been scanned completely
Christian Brabandtee9166e2023-09-03 21:24:33 +02003868 if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS))
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003869 st.ins_buf->b_scanned = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003870
3871 compl_started = FALSE;
3872 }
3873 }
3874 compl_started = TRUE;
3875
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003876 if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
Yegappan Lakshmanan6ad84ab2021-12-31 12:59:53 +00003877 && *st.e_cpt == NUL) // Got to end of 'complete'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003878 found_new_match = FAIL;
3879
3880 i = -1; // total of matches, unknown
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003881 if (found_new_match == FAIL || (ctrl_x_mode_not_default()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003882 && !ctrl_x_mode_line_or_eval()))
3883 i = ins_compl_make_cyclic();
3884
3885 if (compl_old_match != NULL)
3886 {
3887 // If several matches were added (FORWARD) or the search failed and has
3888 // just been made cyclic then we have to move compl_curr_match to the
3889 // next or previous entry (if any) -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003890 compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003891 : compl_old_match->cp_prev;
3892 if (compl_curr_match == NULL)
3893 compl_curr_match = compl_old_match;
3894 }
LemonBoy2bf52dd2022-04-09 18:17:34 +01003895 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003896
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003897 return i;
3898}
3899
3900/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003901 * Update "compl_shown_match" to the actually shown match, it may differ when
3902 * "compl_leader" is used to omit some of the matches.
3903 */
3904 static void
3905ins_compl_update_shown_match(void)
3906{
3907 while (!ins_compl_equal(compl_shown_match,
3908 compl_leader, (int)STRLEN(compl_leader))
3909 && compl_shown_match->cp_next != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003910 && !is_first_match(compl_shown_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003911 compl_shown_match = compl_shown_match->cp_next;
3912
3913 // If we didn't find it searching forward, and compl_shows_dir is
3914 // backward, find the last match.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003915 if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003916 && !ins_compl_equal(compl_shown_match,
3917 compl_leader, (int)STRLEN(compl_leader))
3918 && (compl_shown_match->cp_next == NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003919 || is_first_match(compl_shown_match->cp_next)))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003920 {
3921 while (!ins_compl_equal(compl_shown_match,
3922 compl_leader, (int)STRLEN(compl_leader))
3923 && compl_shown_match->cp_prev != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003924 && !is_first_match(compl_shown_match->cp_prev))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003925 compl_shown_match = compl_shown_match->cp_prev;
3926 }
3927}
3928
3929/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003930 * Delete the old text being completed.
3931 */
3932 void
3933ins_compl_delete(void)
3934{
3935 int col;
3936
3937 // In insert mode: Delete the typed part.
3938 // In replace mode: Put the old characters back, if any.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003939 col = compl_col + (compl_status_adding() ? compl_length : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003940 if ((int)curwin->w_cursor.col > col)
3941 {
3942 if (stop_arrow() == FAIL)
3943 return;
3944 backspace_until_column(col);
3945 }
3946
3947 // TODO: is this sufficient for redrawing? Redrawing everything causes
3948 // flicker, thus we can't do that.
3949 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003950#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003951 // clear v:completed_item
3952 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003953#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003954}
3955
3956/*
3957 * Insert the new text being completed.
3958 * "in_compl_func" is TRUE when called from complete_check().
3959 */
3960 void
3961ins_compl_insert(int in_compl_func)
3962{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003963 int compl_len = get_compl_len();
Bram Moolenaar4b28ba32021-12-27 19:28:37 +00003964
3965 // Make sure we don't go over the end of the string, this can happen with
3966 // illegal bytes.
3967 if (compl_len < (int)STRLEN(compl_shown_match->cp_str))
3968 ins_bytes(compl_shown_match->cp_str + compl_len);
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003969 if (match_at_original_text(compl_shown_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003970 compl_used_match = FALSE;
3971 else
3972 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003973#ifdef FEAT_EVAL
3974 {
3975 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3976
3977 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3978 }
3979#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003980 if (!in_compl_func)
3981 compl_curr_match = compl_shown_match;
3982}
3983
3984/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003985 * show the file name for the completion match (if any). Truncate the file
3986 * name to avoid a wait for return.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003987 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003988 static void
3989ins_compl_show_filename(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003990{
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003991 char *lead = _("match in file");
3992 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3993 char_u *s;
3994 char_u *e;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003995
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003996 if (space <= 0)
3997 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003998
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00003999 // We need the tail that fits. With double-byte encoding going
4000 // back from the end is very slow, thus go from the start and keep
4001 // the text that fits in "space" between "s" and "e".
4002 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004003 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004004 space -= ptr2cells(e);
4005 while (space < 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004006 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004007 space += ptr2cells(s);
4008 MB_PTR_ADV(s);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004009 }
4010 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004011 msg_hist_off = TRUE;
4012 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4013 s > compl_shown_match->cp_fname ? "<" : "", s);
4014 msg((char *)IObuff);
4015 msg_hist_off = FALSE;
4016 redraw_cmdline = FALSE; // don't overwrite!
4017}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004018
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004019/*
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004020 * Find the next set of matches for completion. Repeat the completion "todo"
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004021 * times. The number of matches found is returned in 'num_matches'.
4022 *
4023 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
4024 * get more completions. If it is FALSE, then do nothing when there are no more
4025 * completions in the given direction.
4026 *
4027 * If "advance" is TRUE, then completion will move to the first match.
4028 * Otherwise, the original text will be shown.
4029 *
4030 * Returns OK on success and -1 if the number of matches are unknown.
4031 */
4032 static int
4033find_next_completion_match(
4034 int allow_get_expansion,
4035 int todo, // repeat completion this many times
4036 int advance,
4037 int *num_matches)
4038{
4039 int found_end = FALSE;
4040 compl_T *found_compl = NULL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004041
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004042 while (--todo >= 0)
4043 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004044 if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004045 {
4046 compl_shown_match = compl_shown_match->cp_next;
4047 found_end = (compl_first_match != NULL
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004048 && (is_first_match(compl_shown_match->cp_next)
4049 || is_first_match(compl_shown_match)));
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004050 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004051 else if (compl_shows_dir_backward()
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004052 && compl_shown_match->cp_prev != NULL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004054 found_end = is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004055 compl_shown_match = compl_shown_match->cp_prev;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004056 found_end |= is_first_match(compl_shown_match);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004057 }
4058 else
4059 {
4060 if (!allow_get_expansion)
4061 {
4062 if (advance)
4063 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004064 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004065 compl_pending -= todo + 1;
4066 else
4067 compl_pending += todo + 1;
4068 }
4069 return -1;
4070 }
4071
4072 if (!compl_no_select && advance)
4073 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004074 if (compl_shows_dir_backward())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004075 --compl_pending;
4076 else
4077 ++compl_pending;
4078 }
4079
4080 // Find matches.
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004081 *num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004082
4083 // handle any pending completions
4084 while (compl_pending != 0 && compl_direction == compl_shows_dir
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004085 && advance)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004086 {
4087 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4088 {
4089 compl_shown_match = compl_shown_match->cp_next;
4090 --compl_pending;
4091 }
4092 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4093 {
4094 compl_shown_match = compl_shown_match->cp_prev;
4095 ++compl_pending;
4096 }
4097 else
4098 break;
4099 }
4100 found_end = FALSE;
4101 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004102 if (!match_at_original_text(compl_shown_match)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004103 && compl_leader != NULL
4104 && !ins_compl_equal(compl_shown_match,
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004105 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004106 ++todo;
4107 else
4108 // Remember a matching item.
4109 found_compl = compl_shown_match;
4110
4111 // Stop at the end of the list when we found a usable match.
4112 if (found_end)
4113 {
4114 if (found_compl != NULL)
4115 {
4116 compl_shown_match = found_compl;
4117 break;
4118 }
4119 todo = 1; // use first usable match after wrapping around
4120 }
4121 }
4122
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004123 return OK;
4124}
4125
4126/*
4127 * Fill in the next completion in the current direction.
4128 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4129 * get more completions. If it is FALSE, then we just do nothing when there
4130 * are no more completions in a given direction. The latter case is used when
4131 * we are still in the middle of finding completions, to allow browsing
4132 * through the ones found so far.
4133 * Return the total number of matches, or -1 if still unknown -- webb.
4134 *
4135 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4136 * compl_shown_match here.
4137 *
4138 * Note that this function may be called recursively once only. First with
4139 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4140 * calls this function with "allow_get_expansion" FALSE.
4141 */
4142 static int
4143ins_compl_next(
4144 int allow_get_expansion,
4145 int count, // repeat completion this many times; should
4146 // be at least 1
4147 int insert_match, // Insert the newly selected match
4148 int in_compl_func) // called from complete_check()
4149{
4150 int num_matches = -1;
4151 int todo = count;
4152 int advance;
4153 int started = compl_started;
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004154 buf_T *orig_curbuf = curbuf;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004155
4156 // When user complete function return -1 for findstart which is next
4157 // time of 'always', compl_shown_match become NULL.
4158 if (compl_shown_match == NULL)
4159 return -1;
4160
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004161 if (compl_leader != NULL && !match_at_original_text(compl_shown_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004162 // Update "compl_shown_match" to the actually shown match
4163 ins_compl_update_shown_match();
4164
4165 if (allow_get_expansion && insert_match
nwounkn2e3cd522023-10-17 11:05:38 +02004166 && (!compl_get_longest || compl_used_match))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004167 // Delete old text to be replaced
4168 ins_compl_delete();
4169
4170 // When finding the longest common text we stick at the original text,
4171 // don't let CTRL-N or CTRL-P move to the first match.
4172 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4173
4174 // When restarting the search don't insert the first match either.
4175 if (compl_restarting)
4176 {
4177 advance = FALSE;
4178 compl_restarting = FALSE;
4179 }
4180
4181 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4182 // around.
4183 if (find_next_completion_match(allow_get_expansion, todo, advance,
4184 &num_matches) == -1)
4185 return -1;
4186
Bram Moolenaar0ff01832022-09-24 19:20:30 +01004187 if (curbuf != orig_curbuf)
4188 {
4189 // In case some completion function switched buffer, don't want to
4190 // insert the completion elsewhere.
4191 return -1;
4192 }
4193
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004194 // Insert the text of the new completion, or the compl_leader.
4195 if (compl_no_insert && !started)
4196 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004197 ins_bytes(compl_orig_text + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004198 compl_used_match = FALSE;
4199 }
4200 else if (insert_match)
4201 {
4202 if (!compl_get_longest || compl_used_match)
4203 ins_compl_insert(in_compl_func);
4204 else
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004205 ins_bytes(compl_leader + get_compl_len());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004206 }
4207 else
4208 compl_used_match = FALSE;
4209
4210 if (!allow_get_expansion)
4211 {
4212 // may undisplay the popup menu first
4213 ins_compl_upd_pum();
4214
4215 if (pum_enough_matches())
4216 // Will display the popup menu, don't redraw yet to avoid flicker.
4217 pum_call_update_screen();
4218 else
4219 // Not showing the popup menu yet, redraw to show the user what was
4220 // inserted.
4221 update_screen(0);
4222
4223 // display the updated popup menu
4224 ins_compl_show_pum();
4225#ifdef FEAT_GUI
4226 if (gui.in_use)
4227 {
4228 // Show the cursor after the match, not after the redrawn text.
4229 setcursor();
4230 out_flush_cursor(FALSE, FALSE);
4231 }
4232#endif
4233
4234 // Delete old text to be replaced, since we're still searching and
4235 // don't want to match ourselves!
4236 ins_compl_delete();
4237 }
4238
4239 // Enter will select a match when the match wasn't inserted and the popup
4240 // menu is visible.
4241 if (compl_no_insert && !started)
4242 compl_enter_selects = TRUE;
4243 else
4244 compl_enter_selects = !insert_match && compl_match_array != NULL;
4245
4246 // Show the file name for the match (if any)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004247 if (compl_shown_match->cp_fname != NULL)
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004248 ins_compl_show_filename();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004249
4250 return num_matches;
4251}
4252
4253/*
4254 * Call this while finding completions, to check whether the user has hit a key
4255 * that should change the currently displayed completion, or exit completion
4256 * mode. Also, when compl_pending is not zero, show a completion as soon as
4257 * possible. -- webb
4258 * "frequency" specifies out of how many calls we actually check.
4259 * "in_compl_func" is TRUE when called from complete_check(), don't set
4260 * compl_curr_match.
4261 */
4262 void
4263ins_compl_check_keys(int frequency, int in_compl_func)
4264{
4265 static int count = 0;
4266 int c;
4267
4268 // Don't check when reading keys from a script, :normal or feedkeys().
4269 // That would break the test scripts. But do check for keys when called
4270 // from complete_check().
4271 if (!in_compl_func && (using_script() || ex_normal_busy))
4272 return;
4273
4274 // Only do this at regular intervals
4275 if (++count < frequency)
4276 return;
4277 count = 0;
4278
4279 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4280 // can't do its work correctly.
4281 c = vpeekc_any();
4282 if (c != NUL)
4283 {
4284 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4285 {
4286 c = safe_vgetc(); // Eat the character
4287 compl_shows_dir = ins_compl_key2dir(c);
4288 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4289 c != K_UP && c != K_DOWN, in_compl_func);
4290 }
4291 else
4292 {
4293 // Need to get the character to have KeyTyped set. We'll put it
4294 // back with vungetc() below. But skip K_IGNORE.
4295 c = safe_vgetc();
4296 if (c != K_IGNORE)
4297 {
4298 // Don't interrupt completion when the character wasn't typed,
4299 // e.g., when doing @q to replay keys.
4300 if (c != Ctrl_R && KeyTyped)
4301 compl_interrupted = TRUE;
4302
4303 vungetc(c);
4304 }
4305 }
4306 }
4307 if (compl_pending != 0 && !got_int && !compl_no_insert)
4308 {
4309 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4310
4311 compl_pending = 0;
4312 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
4313 }
4314}
4315
4316/*
4317 * Decide the direction of Insert mode complete from the key typed.
4318 * Returns BACKWARD or FORWARD.
4319 */
4320 static int
4321ins_compl_key2dir(int c)
4322{
4323 if (c == Ctrl_P || c == Ctrl_L
4324 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
4325 return BACKWARD;
4326 return FORWARD;
4327}
4328
4329/*
4330 * Return TRUE for keys that are used for completion only when the popup menu
4331 * is visible.
4332 */
4333 static int
4334ins_compl_pum_key(int c)
4335{
4336 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
4337 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4338 || c == K_UP || c == K_DOWN);
4339}
4340
4341/*
4342 * Decide the number of completions to move forward.
4343 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4344 */
4345 static int
4346ins_compl_key2count(int c)
4347{
4348 int h;
4349
4350 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
4351 {
4352 h = pum_get_height();
4353 if (h > 3)
4354 h -= 2; // keep some context
4355 return h;
4356 }
4357 return 1;
4358}
4359
4360/*
4361 * Return TRUE if completion with "c" should insert the match, FALSE if only
4362 * to change the currently selected completion.
4363 */
4364 static int
4365ins_compl_use_match(int c)
4366{
4367 switch (c)
4368 {
4369 case K_UP:
4370 case K_DOWN:
4371 case K_PAGEDOWN:
4372 case K_KPAGEDOWN:
4373 case K_S_DOWN:
4374 case K_PAGEUP:
4375 case K_KPAGEUP:
4376 case K_S_UP:
4377 return FALSE;
4378 }
4379 return TRUE;
4380}
4381
4382/*
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004383 * Get the pattern, column and length for normal completion (CTRL-N CTRL-P
4384 * completion)
4385 * Sets the global variables: compl_col, compl_length and compl_pattern.
4386 * Uses the global variables: compl_cont_status and ctrl_x_mode
4387 */
4388 static int
4389get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
4390{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004391 if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004392 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004393 if (!compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004394 {
4395 while (--startcol >= 0 && vim_isIDc(line[startcol]))
4396 ;
4397 compl_col += ++startcol;
4398 compl_length = curs_col - startcol;
4399 }
4400 if (p_ic)
4401 compl_pattern = str_foldcase(line + compl_col,
4402 compl_length, NULL, 0);
4403 else
4404 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4405 if (compl_pattern == NULL)
4406 return FAIL;
4407 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004408 else if (compl_status_adding())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004409 {
4410 char_u *prefix = (char_u *)"\\<";
4411
4412 // we need up to 2 extra chars for the prefix
4413 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4414 compl_length) + 2);
4415 if (compl_pattern == NULL)
4416 return FAIL;
4417 if (!vim_iswordp(line + compl_col)
4418 || (compl_col > 0
4419 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
4420 prefix = (char_u *)"";
4421 STRCPY((char *)compl_pattern, prefix);
4422 (void)quote_meta(compl_pattern + STRLEN(prefix),
4423 line + compl_col, compl_length);
4424 }
4425 else if (--startcol < 0
4426 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
4427 {
4428 // Match any word of at least two chars
4429 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4430 if (compl_pattern == NULL)
4431 return FAIL;
4432 compl_col += curs_col;
4433 compl_length = 0;
4434 }
4435 else
4436 {
4437 // Search the point of change class of multibyte character
4438 // or not a word single byte character backward.
4439 if (has_mbyte)
4440 {
4441 int base_class;
4442 int head_off;
4443
4444 startcol -= (*mb_head_off)(line, line + startcol);
4445 base_class = mb_get_class(line + startcol);
4446 while (--startcol >= 0)
4447 {
4448 head_off = (*mb_head_off)(line, line + startcol);
4449 if (base_class != mb_get_class(line + startcol
4450 - head_off))
4451 break;
4452 startcol -= head_off;
4453 }
4454 }
4455 else
4456 while (--startcol >= 0 && vim_iswordc(line[startcol]))
4457 ;
4458 compl_col += ++startcol;
4459 compl_length = (int)curs_col - startcol;
4460 if (compl_length == 1)
4461 {
4462 // Only match word with at least two chars -- webb
4463 // there's no need to call quote_meta,
4464 // alloc(7) is enough -- Acevedo
4465 compl_pattern = alloc(7);
4466 if (compl_pattern == NULL)
4467 return FAIL;
4468 STRCPY((char *)compl_pattern, "\\<");
4469 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4470 STRCAT((char *)compl_pattern, "\\k");
4471 }
4472 else
4473 {
4474 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4475 compl_length) + 2);
4476 if (compl_pattern == NULL)
4477 return FAIL;
4478 STRCPY((char *)compl_pattern, "\\<");
4479 (void)quote_meta(compl_pattern + 2, line + compl_col,
4480 compl_length);
4481 }
4482 }
4483
4484 return OK;
4485}
4486
4487/*
4488 * Get the pattern, column and length for whole line completion or for the
4489 * complete() function.
4490 * Sets the global variables: compl_col, compl_length and compl_pattern.
4491 */
4492 static int
4493get_wholeline_compl_info(char_u *line, colnr_T curs_col)
4494{
4495 compl_col = (colnr_T)getwhitecols(line);
4496 compl_length = (int)curs_col - (int)compl_col;
4497 if (compl_length < 0) // cursor in indent: empty pattern
4498 compl_length = 0;
4499 if (p_ic)
4500 compl_pattern = str_foldcase(line + compl_col, compl_length,
4501 NULL, 0);
4502 else
4503 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4504 if (compl_pattern == NULL)
4505 return FAIL;
4506
4507 return OK;
4508}
4509
4510/*
4511 * Get the pattern, column and length for filename completion.
4512 * Sets the global variables: compl_col, compl_length and compl_pattern.
4513 */
4514 static int
4515get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col)
4516{
4517 // Go back to just before the first filename character.
4518 if (startcol > 0)
4519 {
4520 char_u *p = line + startcol;
4521
4522 MB_PTR_BACK(line, p);
4523 while (p > line && vim_isfilec(PTR2CHAR(p)))
4524 MB_PTR_BACK(line, p);
4525 if (p == line && vim_isfilec(PTR2CHAR(p)))
4526 startcol = 0;
4527 else
4528 startcol = (int)(p - line) + 1;
4529 }
4530
4531 compl_col += startcol;
4532 compl_length = (int)curs_col - startcol;
4533 compl_pattern = addstar(line + compl_col, compl_length, EXPAND_FILES);
4534 if (compl_pattern == NULL)
4535 return FAIL;
4536
4537 return OK;
4538}
4539
4540/*
4541 * Get the pattern, column and length for command-line completion.
4542 * Sets the global variables: compl_col, compl_length and compl_pattern.
4543 */
4544 static int
4545get_cmdline_compl_info(char_u *line, colnr_T curs_col)
4546{
4547 compl_pattern = vim_strnsave(line, curs_col);
4548 if (compl_pattern == NULL)
4549 return FAIL;
4550 set_cmd_context(&compl_xp, compl_pattern,
4551 (int)STRLEN(compl_pattern), curs_col, FALSE);
4552 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4553 || compl_xp.xp_context == EXPAND_NOTHING)
4554 // No completion possible, use an empty pattern to get a
4555 // "pattern not found" message.
4556 compl_col = curs_col;
4557 else
4558 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4559 compl_length = curs_col - compl_col;
4560
4561 return OK;
4562}
4563
4564/*
4565 * Get the pattern, column and length for user defined completion ('omnifunc',
4566 * 'completefunc' and 'thesaurusfunc')
4567 * Sets the global variables: compl_col, compl_length and compl_pattern.
4568 * Uses the global variable: spell_bad_len
4569 */
4570 static int
4571get_userdefined_compl_info(colnr_T curs_col UNUSED)
4572{
4573 int ret = FAIL;
4574
4575#ifdef FEAT_COMPL_FUNC
4576 // Call user defined function 'completefunc' with "a:findstart"
4577 // set to 1 to obtain the length of text to use for completion.
4578 char_u *line;
4579 typval_T args[3];
4580 int col;
4581 char_u *funcname;
4582 pos_T pos;
4583 int save_State = State;
4584 callback_T *cb;
4585
4586 // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern
4587 // length as a string
4588 funcname = get_complete_funcname(ctrl_x_mode);
4589 if (*funcname == NUL)
4590 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004591 semsg(_(e_option_str_is_not_set), ctrl_x_mode_function()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004592 ? "completefunc" : "omnifunc");
4593 return FAIL;
4594 }
4595
4596 args[0].v_type = VAR_NUMBER;
4597 args[0].vval.v_number = 1;
4598 args[1].v_type = VAR_STRING;
4599 args[1].vval.v_string = (char_u *)"";
4600 args[2].v_type = VAR_UNKNOWN;
4601 pos = curwin->w_cursor;
zeertzjqcfe45652022-05-27 17:26:55 +01004602 ++textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004603 cb = get_insert_callback(ctrl_x_mode);
4604 col = call_callback_retnr(cb, 2, args);
zeertzjqcfe45652022-05-27 17:26:55 +01004605 --textlock;
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004606
4607 State = save_State;
4608 curwin->w_cursor = pos; // restore the cursor position
4609 validate_cursor();
4610 if (!EQUAL_POS(curwin->w_cursor, pos))
4611 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004612 emsg(_(e_complete_function_deleted_text));
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004613 return FAIL;
4614 }
4615
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004616 // Return value -2 means the user complete function wants to cancel the
4617 // complete without an error, do the same if the function did not execute
4618 // successfully.
4619 if (col == -2 || aborting())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004620 return FAIL;
LemonBoy9bcb9ca2022-05-26 15:23:26 +01004621 // Return value -3 does the same as -2 and leaves CTRL-X mode.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004622 if (col == -3)
4623 {
4624 ctrl_x_mode = CTRL_X_NORMAL;
4625 edit_submode = NULL;
4626 if (!shortmess(SHM_COMPLETIONMENU))
4627 msg_clr_cmdline();
4628 return FAIL;
4629 }
4630
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00004631 // Reset extended parameters of completion, when starting new
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004632 // completion.
4633 compl_opt_refresh_always = FALSE;
4634 compl_opt_suppress_empty = FALSE;
4635
4636 if (col < 0)
4637 col = curs_col;
4638 compl_col = col;
4639 if (compl_col > curs_col)
4640 compl_col = curs_col;
4641
4642 // Setup variables for completion. Need to obtain "line" again,
4643 // it may have become invalid.
4644 line = ml_get(curwin->w_cursor.lnum);
4645 compl_length = curs_col - compl_col;
4646 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4647 if (compl_pattern == NULL)
4648 return FAIL;
4649
4650 ret = OK;
4651#endif
4652
4653 return ret;
4654}
4655
4656/*
4657 * Get the pattern, column and length for spell completion.
4658 * Sets the global variables: compl_col, compl_length and compl_pattern.
4659 * Uses the global variable: spell_bad_len
4660 */
4661 static int
4662get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED)
4663{
4664 int ret = FAIL;
4665#ifdef FEAT_SPELL
4666 char_u *line;
4667
4668 if (spell_bad_len > 0)
4669 compl_col = curs_col - spell_bad_len;
4670 else
4671 compl_col = spell_word_start(startcol);
4672 if (compl_col >= (colnr_T)startcol)
4673 {
4674 compl_length = 0;
4675 compl_col = curs_col;
4676 }
4677 else
4678 {
4679 spell_expand_check_cap(compl_col);
4680 compl_length = (int)curs_col - compl_col;
4681 }
4682 // Need to obtain "line" again, it may have become invalid.
4683 line = ml_get(curwin->w_cursor.lnum);
4684 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4685 if (compl_pattern == NULL)
4686 return FAIL;
4687
4688 ret = OK;
4689#endif
4690
4691 return ret;
4692}
4693
4694/*
4695 * Get the completion pattern, column and length.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004696 * "startcol" - start column number of the completion pattern/text
4697 * "cur_col" - current cursor column
4698 * On return, "line_invalid" is set to TRUE, if the current line may have
4699 * become invalid and needs to be fetched again.
4700 * Returns OK on success.
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004701 */
4702 static int
4703compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid)
4704{
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004705 if (ctrl_x_mode_normal()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004706 || (ctrl_x_mode & CTRL_X_WANT_IDENT
4707 && !thesaurus_func_complete(ctrl_x_mode)))
4708 {
4709 return get_normal_compl_info(line, startcol, curs_col);
4710 }
4711 else if (ctrl_x_mode_line_or_eval())
4712 {
4713 return get_wholeline_compl_info(line, curs_col);
4714 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004715 else if (ctrl_x_mode_files())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004716 {
4717 return get_filename_compl_info(line, startcol, curs_col);
4718 }
4719 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4720 {
4721 return get_cmdline_compl_info(line, curs_col);
4722 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004723 else if (ctrl_x_mode_function() || ctrl_x_mode_omni()
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004724 || thesaurus_func_complete(ctrl_x_mode))
4725 {
4726 if (get_userdefined_compl_info(curs_col) == FAIL)
4727 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004728 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004729 }
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004730 else if (ctrl_x_mode_spell())
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004731 {
4732 if (get_spell_compl_info(startcol, curs_col) == FAIL)
4733 return FAIL;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004734 *line_invalid = TRUE; // "line" may have become invalid
Bram Moolenaarbf7ff612021-12-27 12:52:07 +00004735 }
4736 else
4737 {
4738 internal_error("ins_complete()");
4739 return FAIL;
4740 }
4741
4742 return OK;
4743}
4744
4745/*
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004746 * Continue an interrupted completion mode search in "line".
4747 *
4748 * If this same ctrl_x_mode has been interrupted use the text from
4749 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4750 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4751 * the same line as the cursor then fix it (the line has been split because it
4752 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4753 * at the beginning of the line has been inserted, we'll look for that.
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004754 */
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004755 static void
4756ins_compl_continue_search(char_u *line)
4757{
4758 // it is a continued search
4759 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004760 if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns()
4761 || ctrl_x_mode_path_defines())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004762 {
4763 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4764 {
4765 // line (probably) wrapped, set compl_startpos to the
4766 // first non_blank in the line, if it is not a wordchar
4767 // include it to get a better pattern, but then we don't
4768 // want the "\\<" prefix, check it below
4769 compl_col = (colnr_T)getwhitecols(line);
4770 compl_startpos.col = compl_col;
4771 compl_startpos.lnum = curwin->w_cursor.lnum;
4772 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4773 }
4774 else
4775 {
4776 // S_IPOS was set when we inserted a word that was at the
4777 // beginning of the line, which means that we'll go to SOL
4778 // mode but first we need to redefine compl_startpos
4779 if (compl_cont_status & CONT_S_IPOS)
4780 {
4781 compl_cont_status |= CONT_SOL;
4782 compl_startpos.col = (colnr_T)(skipwhite(
4783 line + compl_length
4784 + compl_startpos.col) - line);
4785 }
4786 compl_col = compl_startpos.col;
4787 }
4788 compl_length = curwin->w_cursor.col - (int)compl_col;
4789 // IObuff is used to add a "word from the next line" would we
4790 // have enough space? just being paranoid
4791#define MIN_SPACE 75
4792 if (compl_length > (IOSIZE - MIN_SPACE))
4793 {
4794 compl_cont_status &= ~CONT_SOL;
4795 compl_length = (IOSIZE - MIN_SPACE);
4796 compl_col = curwin->w_cursor.col - compl_length;
4797 }
4798 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4799 if (compl_length < 1)
4800 compl_cont_status &= CONT_LOCAL;
4801 }
4802 else if (ctrl_x_mode_line_or_eval())
4803 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4804 else
4805 compl_cont_status = 0;
4806}
4807
4808/*
4809 * start insert mode completion
4810 */
4811 static int
4812ins_compl_start(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004813{
4814 char_u *line;
4815 int startcol = 0; // column where searched text starts
4816 colnr_T curs_col; // cursor column
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004817 int line_invalid = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004818 int save_did_ai = did_ai;
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004819 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004820
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004821 // First time we hit ^N or ^P (in a row, I mean)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004822
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004823 did_ai = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004824 did_si = FALSE;
4825 can_si = FALSE;
4826 can_si_back = FALSE;
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004827 if (stop_arrow() == FAIL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004828 return FAIL;
4829
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004830 line = ml_get(curwin->w_cursor.lnum);
4831 curs_col = curwin->w_cursor.col;
4832 compl_pending = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004833
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004834 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4835 && compl_cont_mode == ctrl_x_mode)
4836 // this same ctrl-x_mode was interrupted previously. Continue the
4837 // completion.
4838 ins_compl_continue_search(line);
4839 else
4840 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004841
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004842 if (!compl_status_adding()) // normal expansion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004843 {
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004844 compl_cont_mode = ctrl_x_mode;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004845 if (ctrl_x_mode_not_default())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004846 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4847 compl_cont_status = 0;
4848 compl_cont_status |= CONT_N_ADDS;
4849 compl_startpos = curwin->w_cursor;
4850 startcol = (int)curs_col;
4851 compl_col = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004852 }
4853
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004854 // Work out completion pattern and original text -- webb
4855 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4856 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004857 if (ctrl_x_mode_function() || ctrl_x_mode_omni()
4858 || thesaurus_func_complete(ctrl_x_mode))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004859 // restore did_ai, so that adding comment leader works
4860 did_ai = save_did_ai;
4861 return FAIL;
4862 }
4863 // If "line" was changed while getting completion info get it again.
4864 if (line_invalid)
4865 line = ml_get(curwin->w_cursor.lnum);
4866
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004867 if (compl_status_adding())
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004868 {
4869 edit_submode_pre = (char_u *)_(" Adding");
4870 if (ctrl_x_mode_line_or_eval())
4871 {
4872 // Insert a new line, keep indentation but ignore 'comments'.
4873 char_u *old = curbuf->b_p_com;
4874
4875 curbuf->b_p_com = (char_u *)"";
4876 compl_startpos.lnum = curwin->w_cursor.lnum;
4877 compl_startpos.col = compl_col;
4878 ins_eol('\r');
4879 curbuf->b_p_com = old;
4880 compl_length = 0;
4881 compl_col = curwin->w_cursor.col;
4882 }
4883 }
4884 else
4885 {
4886 edit_submode_pre = NULL;
4887 compl_startpos.col = compl_col;
4888 }
4889
4890 if (compl_cont_status & CONT_LOCAL)
4891 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4892 else
4893 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4894
4895 // If any of the original typed text has been changed we need to fix
4896 // the redo buffer.
4897 ins_compl_fixRedoBufForLeader(NULL);
4898
4899 // Always add completion for the original text.
4900 vim_free(compl_orig_text);
4901 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4902 if (p_ic)
4903 flags |= CP_ICASE;
4904 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4905 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4906 {
4907 VIM_CLEAR(compl_pattern);
4908 VIM_CLEAR(compl_orig_text);
4909 return FAIL;
4910 }
4911
4912 // showmode might reset the internal line pointers, so it must
4913 // be called before line = ml_get(), or when this address is no
4914 // longer needed. -- Acevedo.
4915 edit_submode_extra = (char_u *)_("-- Searching...");
4916 edit_submode_highl = HLF_COUNT;
4917 showmode();
4918 edit_submode_extra = NULL;
4919 out_flush();
4920
4921 return OK;
4922}
4923
4924/*
4925 * display the completion status message
4926 */
4927 static void
4928ins_compl_show_statusmsg(void)
4929{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004930 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004931 if (is_first_match(compl_first_match->cp_next))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004932 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004933 edit_submode_extra = compl_status_adding() && compl_length > 1
Bram Moolenaarb53a1902022-11-15 13:57:38 +00004934 ? (char_u *)_("Hit end of paragraph")
4935 : (char_u *)_("Pattern not found");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004936 edit_submode_highl = HLF_E;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004937 }
4938
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004939 if (edit_submode_extra == NULL)
4940 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00004941 if (match_at_original_text(compl_curr_match))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004942 {
4943 edit_submode_extra = (char_u *)_("Back at original");
4944 edit_submode_highl = HLF_W;
4945 }
4946 else if (compl_cont_status & CONT_S_IPOS)
4947 {
4948 edit_submode_extra = (char_u *)_("Word from other line");
4949 edit_submode_highl = HLF_COUNT;
4950 }
4951 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4952 {
4953 edit_submode_extra = (char_u *)_("The only match");
4954 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004955 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004956 }
4957 else
4958 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004959#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004960 // Update completion sequence number when needed.
4961 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004962 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004963#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004964 // The match should always have a sequence number now, this is
4965 // just a safety check.
4966 if (compl_curr_match->cp_number != -1)
4967 {
4968 // Space for 10 text chars. + 2x10-digit no.s = 31.
4969 // Translations may need more than twice that.
4970 static char_u match_ref[81];
4971
4972 if (compl_matches > 0)
4973 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004974 _("match %d of %d"),
4975 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004976 else
4977 vim_snprintf((char *)match_ref, sizeof(match_ref),
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00004978 _("match %d"),
4979 compl_curr_match->cp_number);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004980 edit_submode_extra = match_ref;
4981 edit_submode_highl = HLF_R;
4982 if (dollar_vcol >= 0)
4983 curs_columns(FALSE);
4984 }
4985 }
4986 }
4987
4988 // Show a message about what (completion) mode we're in.
4989 if (!compl_opt_suppress_empty)
4990 {
4991 showmode();
4992 if (!shortmess(SHM_COMPLETIONMENU))
4993 {
4994 if (edit_submode_extra != NULL)
4995 {
4996 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004997 {
4998 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004999 msg_attr((char *)edit_submode_extra,
5000 edit_submode_highl < HLF_COUNT
5001 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01005002 msg_hist_off = FALSE;
5003 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005004 }
5005 else
5006 msg_clr_cmdline(); // necessary for "noshowmode"
5007 }
5008 }
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005009}
5010
5011/*
5012 * Do Insert mode completion.
5013 * Called when character "c" was typed, which has a meaning for completion.
5014 * Returns OK if completion was done, FAIL if something failed (out of mem).
5015 */
5016 int
5017ins_complete(int c, int enable_pum)
5018{
5019 int n;
5020 int save_w_wrow;
5021 int save_w_leftcol;
5022 int insert_match;
5023
5024 compl_direction = ins_compl_key2dir(c);
5025 insert_match = ins_compl_use_match(c);
5026
5027 if (!compl_started)
5028 {
5029 if (ins_compl_start() == FAIL)
5030 return FAIL;
5031 }
5032 else if (insert_match && stop_arrow() == FAIL)
5033 return FAIL;
5034
5035 compl_shown_match = compl_curr_match;
5036 compl_shows_dir = compl_direction;
5037
5038 // Find next match (and following matches).
5039 save_w_wrow = curwin->w_wrow;
5040 save_w_leftcol = curwin->w_leftcol;
5041 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
5042
5043 // may undisplay the popup menu
5044 ins_compl_upd_pum();
5045
5046 if (n > 1) // all matches have been found
5047 compl_matches = n;
5048 compl_curr_match = compl_shown_match;
5049 compl_direction = compl_shows_dir;
5050
5051 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5052 // mode.
5053 if (got_int && !global_busy)
5054 {
5055 (void)vgetc();
5056 got_int = FALSE;
5057 }
5058
5059 // we found no match if the list has only the "compl_orig_text"-entry
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005060 if (is_first_match(compl_first_match->cp_next))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005061 {
5062 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5063 // because we couldn't expand anything at first place, but if we used
5064 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5065 // (such as M in M'exico) if not tried already. -- Acevedo
5066 if (compl_length > 1
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005067 || compl_status_adding()
5068 || (ctrl_x_mode_not_default()
5069 && !ctrl_x_mode_path_patterns()
5070 && !ctrl_x_mode_path_defines()))
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00005071 compl_cont_status &= ~CONT_N_ADDS;
5072 }
5073
5074 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
5075 compl_cont_status |= CONT_S_IPOS;
5076 else
5077 compl_cont_status &= ~CONT_S_IPOS;
5078
5079 ins_compl_show_statusmsg();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005080
5081 // Show the popup menu, unless we got interrupted.
5082 if (enable_pum && !compl_interrupted)
5083 show_pum(save_w_wrow, save_w_leftcol);
5084
5085 compl_was_interrupted = compl_interrupted;
5086 compl_interrupted = FALSE;
5087
5088 return OK;
5089}
5090
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00005091/*
5092 * Remove (if needed) and show the popup menu
5093 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005094 static void
5095show_pum(int prev_w_wrow, int prev_w_leftcol)
5096{
5097 // RedrawingDisabled may be set when invoked through complete().
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005098 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005099 RedrawingDisabled = 0;
5100
5101 // If the cursor moved or the display scrolled we need to remove the pum
5102 // first.
5103 setcursor();
5104 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5105 ins_compl_del_pum();
5106
5107 ins_compl_show_pum();
5108 setcursor();
Bram Moolenaar79cdf022023-05-20 14:07:00 +01005109
5110 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005111}
5112
5113/*
5114 * Looks in the first "len" chars. of "src" for search-metachars.
5115 * If dest is not NULL the chars. are copied there quoting (with
5116 * a backslash) the metachars, and dest would be NUL terminated.
5117 * Returns the length (needed) of dest
5118 */
5119 static unsigned
5120quote_meta(char_u *dest, char_u *src, int len)
5121{
5122 unsigned m = (unsigned)len + 1; // one extra for the NUL
5123
5124 for ( ; --len >= 0; src++)
5125 {
5126 switch (*src)
5127 {
5128 case '.':
5129 case '*':
5130 case '[':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005131 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005132 break;
5133 // FALLTHROUGH
5134 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01005135 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005136 break;
5137 // FALLTHROUGH
5138 case '\\':
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00005139 if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus())
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005140 break;
5141 // FALLTHROUGH
5142 case '^': // currently it's not needed.
5143 case '$':
5144 m++;
5145 if (dest != NULL)
5146 *dest++ = '\\';
5147 break;
5148 }
5149 if (dest != NULL)
5150 *dest++ = *src;
5151 // Copy remaining bytes of a multibyte character.
5152 if (has_mbyte)
5153 {
5154 int i, mb_len;
5155
5156 mb_len = (*mb_ptr2len)(src) - 1;
5157 if (mb_len > 0 && len >= mb_len)
5158 for (i = 0; i < mb_len; ++i)
5159 {
5160 --len;
5161 ++src;
5162 if (dest != NULL)
5163 *dest++ = *src;
5164 }
5165 }
5166 }
5167 if (dest != NULL)
5168 *dest = NUL;
5169
5170 return m;
5171}
5172
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005173#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005174 void
5175free_insexpand_stuff(void)
5176{
5177 VIM_CLEAR(compl_orig_text);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005178# ifdef FEAT_EVAL
5179 free_callback(&cfu_cb);
5180 free_callback(&ofu_cb);
5181 free_callback(&tsrfu_cb);
5182# endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005183}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005184#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005185
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005186#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005187/*
5188 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5189 * spelled word, if there is one.
5190 */
5191 static void
5192spell_back_to_badword(void)
5193{
5194 pos_T tpos = curwin->w_cursor;
5195
5196 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
5197 if (curwin->w_cursor.col != tpos.col)
5198 start_arrow(&tpos);
5199}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005200#endif