Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1 | /* 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 Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 16 | /* |
| 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 Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 23 | # define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 24 | # 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 Moolenaar | 9810cfb | 2019-12-11 21:23:00 +0100 | [diff] [blame] | 35 | # define CTRL_X_FUNCTION 12 |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 36 | # define CTRL_X_OMNI 13 |
| 37 | # define CTRL_X_SPELL 14 |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 38 | # define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs" |
| 39 | # define CTRL_X_EVAL 16 // for builtin function complete() |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 40 | # define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 41 | |
| 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. |
| 45 | static 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)"), |
zeertzjq | 7002c05 | 2024-06-21 07:55:07 +0200 | [diff] [blame] | 61 | N_(" Spelling suggestion (^S^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 62 | N_(" Keyword Local completion (^N^P)"), |
| 63 | NULL, // CTRL_X_EVAL doesn't use msg. |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 64 | N_(" Command-line completion (^V^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 67 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 68 | static char *ctrl_x_mode_names[] = { |
| 69 | "keyword", |
| 70 | "ctrl_x", |
zeertzjq | 27fef59 | 2021-10-03 12:01:27 +0100 | [diff] [blame] | 71 | "scroll", |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 72 | "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" |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 85 | "eval", |
| 86 | "cmdline", |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 87 | }; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 88 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 91 | * Structure used to store one match for insert completion. |
| 92 | */ |
| 93 | typedef struct compl_S compl_T; |
| 94 | struct compl_S |
| 95 | { |
| 96 | compl_T *cp_next; |
| 97 | compl_T *cp_prev; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 98 | compl_T *cp_match_next; // matched next compl_T |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 99 | string_T cp_str; // matched text |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 100 | char_u *(cp_text[CPT_COUNT]); // text for the menu |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 101 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 102 | typval_T cp_user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 103 | #endif |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 104 | char_u *cp_fname; // file containing the match, allocated when |
| 105 | // cp_flags has CP_FREE_FNAME |
| 106 | int cp_flags; // CP_ values |
| 107 | int cp_number; // sequence number |
| 108 | int cp_score; // fuzzy match score |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 109 | int cp_in_match_array; // collected by compl_match_array |
glepnir | 7baa014 | 2024-10-09 20:19:25 +0200 | [diff] [blame] | 110 | int cp_user_abbr_hlattr; // highlight attribute for abbr |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 111 | int cp_user_kind_hlattr; // highlight attribute for kind |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 112 | int cp_cpt_value_idx; // index of this match's source in 'cpt' option |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 115 | // values for cp_flags |
| 116 | # define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun |
| 117 | # define CP_FREE_FNAME 2 // cp_fname is allocated |
| 118 | # define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status |
| 119 | # define CP_EQUAL 8 // ins_compl_equal() always returns TRUE |
| 120 | # define CP_ICASE 16 // ins_compl_equal() ignores case |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 121 | # define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 122 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 123 | /* |
| 124 | * All the current matches are stored in a list. |
| 125 | * "compl_first_match" points to the start of the list. |
| 126 | * "compl_curr_match" points to the currently selected entry. |
| 127 | * "compl_shown_match" is different from compl_curr_match during |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 128 | * ins_compl_get_exp(), when new matches are added to the list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 129 | * "compl_old_match" points to previous "compl_curr_match". |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 130 | */ |
| 131 | static compl_T *compl_first_match = NULL; |
| 132 | static compl_T *compl_curr_match = NULL; |
| 133 | static compl_T *compl_shown_match = NULL; |
| 134 | static compl_T *compl_old_match = NULL; |
| 135 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 136 | // list used to store the compl_T which have the max score |
| 137 | // used for completefuzzycollect |
| 138 | static compl_T **compl_best_matches = NULL; |
| 139 | static int compl_num_bests = 0; |
| 140 | // inserted a longest when completefuzzycollect enabled |
| 141 | static int compl_cfc_longest_ins = FALSE; |
| 142 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 143 | // After using a cursor key <Enter> selects a match in the popup menu, |
| 144 | // otherwise it inserts a line break. |
| 145 | static int compl_enter_selects = FALSE; |
| 146 | |
| 147 | // When "compl_leader" is not NULL only matches that start with this string |
| 148 | // are used. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 149 | static string_T compl_leader = {NULL, 0}; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 150 | |
| 151 | static int compl_get_longest = FALSE; // put longest common string |
| 152 | // in compl_leader |
| 153 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 154 | // Selected one of the matches. When FALSE the match was edited or using the |
| 155 | // longest common string. |
| 156 | static int compl_used_match; |
| 157 | |
| 158 | // didn't finish finding completions. |
| 159 | static int compl_was_interrupted = FALSE; |
| 160 | |
| 161 | // Set when character typed while looking for matches and it means we should |
| 162 | // stop looking for matches. |
| 163 | static int compl_interrupted = FALSE; |
| 164 | |
| 165 | static int compl_restarting = FALSE; // don't insert match |
| 166 | |
| 167 | // When the first completion is done "compl_started" is set. When it's |
| 168 | // FALSE the word to be completed must be located. |
| 169 | static int compl_started = FALSE; |
| 170 | |
| 171 | // Which Ctrl-X mode are we in? |
| 172 | static int ctrl_x_mode = CTRL_X_NORMAL; |
| 173 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 174 | static int compl_matches = 0; // number of completion matches |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 175 | static string_T compl_pattern = {NULL, 0}; // search pattern for matching items |
| 176 | #ifdef FEAT_COMPL_FUNC |
| 177 | static string_T cpt_compl_pattern = {NULL, 0}; // pattern returned by func in 'cpt' |
| 178 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 179 | static int compl_direction = FORWARD; |
| 180 | static int compl_shows_dir = FORWARD; |
| 181 | static int compl_pending = 0; // > 1 for postponed CTRL-N |
| 182 | static pos_T compl_startpos; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 183 | // Length in bytes of the text being completed (this is deleted to be replaced |
| 184 | // by the match.) |
| 185 | static int compl_length = 0; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 186 | static linenr_T compl_lnum = 0; // lnum where the completion start |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 187 | static colnr_T compl_col = 0; // column where the text starts |
| 188 | // that is being completed |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 189 | static colnr_T compl_ins_end_col = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 190 | static string_T compl_orig_text = {NULL, 0}; // text as it was before |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 191 | // completion started |
| 192 | static int compl_cont_mode = 0; |
| 193 | static expand_T compl_xp; |
| 194 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 195 | // List of flags for method of completion. |
| 196 | static int compl_cont_status = 0; |
| 197 | # define CONT_ADDING 1 // "normal" or "adding" expansion |
| 198 | # define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion |
| 199 | // it's set only iff N_ADDS is set |
| 200 | # define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current |
| 201 | # define CONT_S_IPOS 8 // next ^X<> will set initial_pos? |
| 202 | // if so, word-wise-expansion will set SOL |
| 203 | # define CONT_SOL 16 // pattern includes start of line, just for |
| 204 | // word-wise expansion, not set for ^X^L |
| 205 | # define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local |
| 206 | // expansion, (eg use complete=.) |
| 207 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 208 | static int compl_opt_refresh_always = FALSE; |
| 209 | static int compl_opt_suppress_empty = FALSE; |
| 210 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 211 | static int compl_selected_item = -1; |
| 212 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 213 | static int *compl_fuzzy_scores; |
| 214 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 215 | static int *cpt_func_refresh_always; // array indicating which 'cpt' functions have 'refresh:always' set |
| 216 | static int cpt_value_count; // total number of completion sources specified in the 'cpt' option |
| 217 | static int cpt_value_idx; // index of the current completion source being expanded |
| 218 | |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 219 | // "compl_match_array" points the currently displayed list of entries in the |
| 220 | // popup menu. It is NULL when there is no popup menu. |
| 221 | static pumitem_T *compl_match_array = NULL; |
| 222 | static int compl_match_arraysize; |
| 223 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 224 | static 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, int *user_hl, int score); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 225 | static void ins_compl_longest_match(compl_T *match); |
| 226 | static void ins_compl_del_pum(void); |
| 227 | static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 228 | static void ins_compl_free(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 229 | static int ins_compl_need_restart(void); |
| 230 | static void ins_compl_new_leader(void); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 231 | static int get_compl_len(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 232 | static void ins_compl_restart(void); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 233 | static void ins_compl_set_original_text(char_u *str, size_t len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 234 | static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg); |
| 235 | # if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 236 | static void ins_compl_add_list(list_T *list); |
| 237 | static void ins_compl_add_dict(dict_T *dict); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 238 | static int get_userdefined_compl_info(colnr_T curs_col, callback_T *cb, int *startcol); |
| 239 | static callback_T *get_cpt_func_callback(char_u *funcname); |
| 240 | static void get_cpt_func_completion_matches(callback_T *cb); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 241 | # endif |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 242 | static int cpt_compl_src_init(char_u *p_cpt); |
| 243 | static int is_cpt_func_refresh_always(void); |
| 244 | static void cpt_compl_src_clear(void); |
| 245 | static void cpt_compl_refresh(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 246 | static int ins_compl_key2dir(int c); |
| 247 | static int ins_compl_pum_key(int c); |
| 248 | static int ins_compl_key2count(int c); |
| 249 | static void show_pum(int prev_w_wrow, int prev_w_leftcol); |
| 250 | static unsigned quote_meta(char_u *dest, char_u *str, int len); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 251 | static int ins_compl_has_multiple(void); |
| 252 | static void ins_compl_expand_multiple(char_u *str); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 253 | static void ins_compl_longest_insert(char_u *prefix); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 254 | |
| 255 | #ifdef FEAT_SPELL |
| 256 | static void spell_back_to_badword(void); |
| 257 | static int spell_bad_len = 0; // length of located bad word |
| 258 | #endif |
| 259 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 260 | /* |
| 261 | * CTRL-X pressed in Insert mode. |
| 262 | */ |
| 263 | void |
| 264 | ins_ctrl_x(void) |
| 265 | { |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 266 | if (!ctrl_x_mode_cmdline()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 267 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 268 | // if the next ^X<> won't ADD nothing, then reset compl_cont_status |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 269 | if (compl_cont_status & CONT_N_ADDS) |
| 270 | compl_cont_status |= CONT_INTRPT; |
| 271 | else |
| 272 | compl_cont_status = 0; |
| 273 | // We're not sure which CTRL-X mode it will be yet |
| 274 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 275 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 276 | edit_submode_pre = NULL; |
| 277 | showmode(); |
| 278 | } |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 279 | else |
| 280 | // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X |
| 281 | // CTRL-V look like CTRL-N |
| 282 | ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X; |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 283 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 284 | may_trigger_modechanged(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Functions to check the current CTRL-X mode. |
| 289 | */ |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 290 | int ctrl_x_mode_none(void) |
| 291 | { return ctrl_x_mode == 0; } |
| 292 | int ctrl_x_mode_normal(void) |
| 293 | { return ctrl_x_mode == CTRL_X_NORMAL; } |
| 294 | int ctrl_x_mode_scroll(void) |
| 295 | { return ctrl_x_mode == CTRL_X_SCROLL; } |
| 296 | int ctrl_x_mode_whole_line(void) |
| 297 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE; } |
| 298 | int ctrl_x_mode_files(void) |
| 299 | { return ctrl_x_mode == CTRL_X_FILES; } |
| 300 | int ctrl_x_mode_tags(void) |
| 301 | { return ctrl_x_mode == CTRL_X_TAGS; } |
| 302 | int ctrl_x_mode_path_patterns(void) |
| 303 | { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; } |
| 304 | int ctrl_x_mode_path_defines(void) |
| 305 | { return ctrl_x_mode == CTRL_X_PATH_DEFINES; } |
| 306 | int ctrl_x_mode_dictionary(void) |
| 307 | { return ctrl_x_mode == CTRL_X_DICTIONARY; } |
| 308 | int ctrl_x_mode_thesaurus(void) |
| 309 | { return ctrl_x_mode == CTRL_X_THESAURUS; } |
| 310 | int ctrl_x_mode_cmdline(void) |
| 311 | { return ctrl_x_mode == CTRL_X_CMDLINE |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 312 | || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; } |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 313 | int ctrl_x_mode_function(void) |
| 314 | { return ctrl_x_mode == CTRL_X_FUNCTION; } |
| 315 | int ctrl_x_mode_omni(void) |
| 316 | { return ctrl_x_mode == CTRL_X_OMNI; } |
| 317 | int ctrl_x_mode_spell(void) |
| 318 | { return ctrl_x_mode == CTRL_X_SPELL; } |
| 319 | static int ctrl_x_mode_eval(void) |
| 320 | { return ctrl_x_mode == CTRL_X_EVAL; } |
| 321 | int ctrl_x_mode_line_or_eval(void) |
| 322 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Whether other than default completion has been selected. |
| 326 | */ |
| 327 | int |
| 328 | ctrl_x_mode_not_default(void) |
| 329 | { |
| 330 | return ctrl_x_mode != CTRL_X_NORMAL; |
| 331 | } |
| 332 | |
| 333 | /* |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 334 | * Whether CTRL-X was typed without a following character, |
| 335 | * not including when in CTRL-X CTRL-V mode. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 336 | */ |
| 337 | int |
| 338 | ctrl_x_mode_not_defined_yet(void) |
| 339 | { |
| 340 | return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; |
| 341 | } |
| 342 | |
| 343 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 344 | * Return TRUE if currently in "normal" or "adding" insert completion matches |
| 345 | * state |
| 346 | */ |
| 347 | int |
| 348 | compl_status_adding(void) |
| 349 | { |
| 350 | return compl_cont_status & CONT_ADDING; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Return TRUE if the completion pattern includes start of line, just for |
| 355 | * word-wise expansion. |
| 356 | */ |
| 357 | int |
| 358 | compl_status_sol(void) |
| 359 | { |
| 360 | return compl_cont_status & CONT_SOL; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.) |
| 365 | */ |
| 366 | int |
| 367 | compl_status_local(void) |
| 368 | { |
| 369 | return compl_cont_status & CONT_LOCAL; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Clear the completion status flags |
| 374 | */ |
| 375 | void |
| 376 | compl_status_clear(void) |
| 377 | { |
| 378 | compl_cont_status = 0; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Return TRUE if completion is using the forward direction matches |
| 383 | */ |
| 384 | static int |
| 385 | compl_dir_forward(void) |
| 386 | { |
| 387 | return compl_direction == FORWARD; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Return TRUE if currently showing forward completion matches |
| 392 | */ |
| 393 | static int |
| 394 | compl_shows_dir_forward(void) |
| 395 | { |
| 396 | return compl_shows_dir == FORWARD; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Return TRUE if currently showing backward completion matches |
| 401 | */ |
| 402 | static int |
| 403 | compl_shows_dir_backward(void) |
| 404 | { |
| 405 | return compl_shows_dir == BACKWARD; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Return TRUE if the 'dictionary' or 'thesaurus' option can be used. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 410 | */ |
| 411 | int |
| 412 | has_compl_option(int dict_opt) |
| 413 | { |
| 414 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 415 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 416 | && !curwin->w_p_spell |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 417 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 418 | ) |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 419 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL |
| 420 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 421 | && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 422 | #endif |
| 423 | )) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 424 | { |
| 425 | ctrl_x_mode = CTRL_X_NORMAL; |
| 426 | edit_submode = NULL; |
| 427 | msg_attr(dict_opt ? _("'dictionary' option is empty") |
| 428 | : _("'thesaurus' option is empty"), |
| 429 | HL_ATTR(HLF_E)); |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 430 | if (emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 431 | { |
| 432 | vim_beep(BO_COMPL); |
| 433 | setcursor(); |
| 434 | out_flush(); |
| 435 | #ifdef FEAT_EVAL |
| 436 | if (!get_vim_var_nr(VV_TESTING)) |
| 437 | #endif |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 438 | ui_delay(2004L, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 439 | } |
| 440 | return FALSE; |
| 441 | } |
| 442 | return TRUE; |
| 443 | } |
| 444 | |
| 445 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 446 | * Is the character "c" a valid key to go to or keep us in CTRL-X mode? |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 447 | * This depends on the current mode. |
| 448 | */ |
| 449 | int |
| 450 | vim_is_ctrl_x_key(int c) |
| 451 | { |
| 452 | // Always allow ^R - let its results then be checked |
| 453 | if (c == Ctrl_R) |
| 454 | return TRUE; |
| 455 | |
| 456 | // Accept <PageUp> and <PageDown> if the popup menu is visible. |
| 457 | if (ins_compl_pum_key(c)) |
| 458 | return TRUE; |
| 459 | |
| 460 | switch (ctrl_x_mode) |
| 461 | { |
| 462 | case 0: // Not in any CTRL-X mode |
| 463 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 464 | case CTRL_X_NOT_DEFINED_YET: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 465 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 466 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
| 467 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 468 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 469 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
| 470 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 471 | || c == Ctrl_S || c == Ctrl_K || c == 's' |
| 472 | || c == Ctrl_Z); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 473 | case CTRL_X_SCROLL: |
| 474 | return (c == Ctrl_Y || c == Ctrl_E); |
| 475 | case CTRL_X_WHOLE_LINE: |
| 476 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 477 | case CTRL_X_FILES: |
| 478 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 479 | case CTRL_X_DICTIONARY: |
| 480 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 481 | case CTRL_X_THESAURUS: |
| 482 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 483 | case CTRL_X_TAGS: |
| 484 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 485 | #ifdef FEAT_FIND_ID |
| 486 | case CTRL_X_PATH_PATTERNS: |
| 487 | return (c == Ctrl_P || c == Ctrl_N); |
| 488 | case CTRL_X_PATH_DEFINES: |
| 489 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 490 | #endif |
| 491 | case CTRL_X_CMDLINE: |
| 492 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 493 | || c == Ctrl_X); |
| 494 | #ifdef FEAT_COMPL_FUNC |
| 495 | case CTRL_X_FUNCTION: |
| 496 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
| 497 | case CTRL_X_OMNI: |
| 498 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
| 499 | #endif |
| 500 | case CTRL_X_SPELL: |
| 501 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
| 502 | case CTRL_X_EVAL: |
| 503 | return (c == Ctrl_P || c == Ctrl_N); |
| 504 | } |
| 505 | internal_error("vim_is_ctrl_x_key()"); |
| 506 | return FALSE; |
| 507 | } |
| 508 | |
| 509 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 510 | * Return TRUE if "match" is the original text when the completion began. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 511 | */ |
| 512 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 513 | match_at_original_text(compl_T *match) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 514 | { |
| 515 | return match->cp_flags & CP_ORIGINAL_TEXT; |
| 516 | } |
| 517 | |
| 518 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 519 | * Returns TRUE if "match" is the first match in the completion list. |
| 520 | */ |
| 521 | static int |
| 522 | is_first_match(compl_T *match) |
| 523 | { |
| 524 | return match == compl_first_match; |
| 525 | } |
| 526 | |
| 527 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 528 | * Return TRUE when character "c" is part of the item currently being |
| 529 | * completed. Used to decide whether to abandon complete mode when the menu |
| 530 | * is visible. |
| 531 | */ |
| 532 | int |
| 533 | ins_compl_accept_char(int c) |
| 534 | { |
| 535 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 536 | // When expanding an identifier only accept identifier chars. |
| 537 | return vim_isIDc(c); |
| 538 | |
| 539 | switch (ctrl_x_mode) |
| 540 | { |
| 541 | case CTRL_X_FILES: |
| 542 | // When expanding file name only accept file name chars. But not |
| 543 | // path separators, so that "proto/<Tab>" expands files in |
| 544 | // "proto", not "proto/" as a whole |
| 545 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 546 | |
| 547 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 548 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 549 | case CTRL_X_OMNI: |
| 550 | // Command line and Omni completion can work with just about any |
| 551 | // printable character, but do stop at white space. |
| 552 | return vim_isprintc(c) && !VIM_ISWHITE(c); |
| 553 | |
| 554 | case CTRL_X_WHOLE_LINE: |
| 555 | // For while line completion a space can be part of the line. |
| 556 | return vim_isprintc(c); |
| 557 | } |
| 558 | return vim_iswordc(c); |
| 559 | } |
| 560 | |
| 561 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 562 | * Get the completed text by inferring the case of the originally typed text. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 563 | * If the result is in allocated memory "tofree" is set to it. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 564 | */ |
| 565 | static char_u * |
| 566 | ins_compl_infercase_gettext( |
| 567 | char_u *str, |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 568 | int char_len, |
| 569 | int compl_char_len, |
| 570 | int min_len, |
| 571 | char_u **tofree) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 572 | { |
| 573 | int *wca; // Wide character array. |
| 574 | char_u *p; |
| 575 | int i, c; |
| 576 | int has_lower = FALSE; |
| 577 | int was_letter = FALSE; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 578 | garray_T gap; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 579 | |
| 580 | IObuff[0] = NUL; |
| 581 | |
| 582 | // Allocate wide character array for the completion and fill it. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 583 | wca = ALLOC_MULT(int, char_len); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 584 | if (wca == NULL) |
| 585 | return IObuff; |
| 586 | |
| 587 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 588 | for (i = 0; i < char_len; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 589 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 590 | if (has_mbyte) |
| 591 | wca[i] = mb_ptr2char_adv(&p); |
| 592 | else |
| 593 | wca[i] = *(p++); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 594 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 595 | |
| 596 | // Rule 1: Were any chars converted to lower? |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 597 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 598 | for (i = 0; i < min_len; ++i) |
| 599 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 600 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 601 | if (MB_ISLOWER(c)) |
| 602 | { |
| 603 | has_lower = TRUE; |
| 604 | if (MB_ISUPPER(wca[i])) |
| 605 | { |
| 606 | // Rule 1 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 607 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 608 | wca[i] = MB_TOLOWER(wca[i]); |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // Rule 2: No lower case, 2nd consecutive letter converted to |
| 615 | // upper case. |
| 616 | if (!has_lower) |
| 617 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 618 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 619 | for (i = 0; i < min_len; ++i) |
| 620 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 621 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 622 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 623 | { |
| 624 | // Rule 2 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 625 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 626 | wca[i] = MB_TOUPPER(wca[i]); |
| 627 | break; |
| 628 | } |
| 629 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // Copy the original case of the part we typed. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 634 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 635 | for (i = 0; i < min_len; ++i) |
| 636 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 637 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 638 | if (MB_ISLOWER(c)) |
| 639 | wca[i] = MB_TOLOWER(wca[i]); |
| 640 | else if (MB_ISUPPER(c)) |
| 641 | wca[i] = MB_TOUPPER(wca[i]); |
| 642 | } |
| 643 | |
| 644 | // Generate encoding specific output from wide character array. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 645 | p = IObuff; |
| 646 | i = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 647 | ga_init2(&gap, 1, 500); |
| 648 | while (i < char_len) |
| 649 | { |
| 650 | if (gap.ga_data != NULL) |
| 651 | { |
| 652 | if (ga_grow(&gap, 10) == FAIL) |
| 653 | { |
| 654 | ga_clear(&gap); |
| 655 | return (char_u *)"[failed]"; |
| 656 | } |
| 657 | p = (char_u *)gap.ga_data + gap.ga_len; |
| 658 | if (has_mbyte) |
| 659 | gap.ga_len += (*mb_char2bytes)(wca[i++], p); |
| 660 | else |
| 661 | { |
| 662 | *p = wca[i++]; |
| 663 | ++gap.ga_len; |
| 664 | } |
| 665 | } |
| 666 | else if ((p - IObuff) + 6 >= IOSIZE) |
| 667 | { |
| 668 | // Multi-byte characters can occupy up to five bytes more than |
| 669 | // ASCII characters, and we also need one byte for NUL, so when |
| 670 | // getting to six bytes from the edge of IObuff switch to using a |
| 671 | // growarray. Add the character in the next round. |
| 672 | if (ga_grow(&gap, IOSIZE) == FAIL) |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 673 | { |
| 674 | vim_free(wca); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 675 | return (char_u *)"[failed]"; |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 676 | } |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 677 | *p = NUL; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 678 | STRCPY(gap.ga_data, IObuff); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 679 | gap.ga_len = (int)(p - IObuff); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 680 | } |
| 681 | else if (has_mbyte) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 682 | p += (*mb_char2bytes)(wca[i++], p); |
| 683 | else |
| 684 | *(p++) = wca[i++]; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 685 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 686 | vim_free(wca); |
| 687 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 688 | if (gap.ga_data != NULL) |
| 689 | { |
| 690 | *tofree = gap.ga_data; |
| 691 | return gap.ga_data; |
| 692 | } |
| 693 | |
| 694 | *p = NUL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 695 | return IObuff; |
| 696 | } |
| 697 | |
| 698 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 699 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
| 700 | * case of the originally typed text is used, and the case of the completed |
| 701 | * text is inferred, ie this tries to work out what case you probably wanted |
| 702 | * the rest of the word to be in -- webb |
| 703 | */ |
| 704 | int |
| 705 | ins_compl_add_infercase( |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 706 | char_u *str_arg, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 707 | int len, |
| 708 | int icase, |
| 709 | char_u *fname, |
| 710 | int dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 711 | int cont_s_ipos, // next ^X<> will set initial_pos |
| 712 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 713 | { |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 714 | char_u *str = str_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 715 | char_u *p; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 716 | int char_len; // count multi-byte characters |
| 717 | int compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 718 | int min_len; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 719 | int flags = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 720 | int res; |
| 721 | char_u *tofree = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 722 | |
| 723 | if (p_ic && curbuf->b_p_inf && len > 0) |
| 724 | { |
| 725 | // Infer case of completed part. |
| 726 | |
| 727 | // Find actual length of completion. |
| 728 | if (has_mbyte) |
| 729 | { |
| 730 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 731 | char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 732 | while (*p != NUL) |
| 733 | { |
| 734 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 735 | ++char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 739 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 740 | char_len = len; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 741 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 742 | |
| 743 | // Find actual length of original text. |
| 744 | if (has_mbyte) |
| 745 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 746 | p = compl_orig_text.string; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 747 | compl_char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 748 | while (*p != NUL) |
| 749 | { |
| 750 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 751 | ++compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 755 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 756 | compl_char_len = compl_length; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 757 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 758 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 759 | // "char_len" may be smaller than "compl_char_len" when using |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 760 | // thesaurus, only use the minimum when comparing. |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 761 | min_len = MIN(char_len, compl_char_len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 762 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 763 | str = ins_compl_infercase_gettext(str, char_len, |
| 764 | compl_char_len, min_len, &tofree); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 765 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 766 | if (cont_s_ipos) |
| 767 | flags |= CP_CONT_S_IPOS; |
| 768 | if (icase) |
| 769 | flags |= CP_ICASE; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 770 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 771 | res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL, score); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 772 | vim_free(tofree); |
| 773 | return res; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 777 | * Check if ctrl_x_mode has been configured in 'completefuzzycollect' |
| 778 | */ |
| 779 | static int |
| 780 | cfc_has_mode(void) |
| 781 | { |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 782 | if (ctrl_x_mode_normal() || ctrl_x_mode_dictionary()) |
| 783 | return (cfc_flags & CFC_KEYWORD) != 0; |
| 784 | else if (ctrl_x_mode_files()) |
| 785 | return (cfc_flags & CFC_FILES) != 0; |
| 786 | else if (ctrl_x_mode_whole_line()) |
| 787 | return (cfc_flags & CFC_WHOLELINE) != 0; |
| 788 | else |
| 789 | return FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 793 | * Add a match to the list of matches. The arguments are: |
| 794 | * str - text of the match to add |
| 795 | * len - length of "str". If -1, then the length of "str" is |
| 796 | * computed. |
| 797 | * fname - file name to associate with this match. |
| 798 | * cptext - list of strings to use with this match (for abbr, menu, info |
| 799 | * and kind) |
| 800 | * user_data - user supplied data (any vim type) for this match |
| 801 | * cdir - match direction. If 0, use "compl_direction". |
| 802 | * flags_arg - match flags (cp_flags) |
| 803 | * adup - accept this match even if it is already present. |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 804 | * *user_hl - list of extra highlight attributes for abbr kind. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 805 | * If "cdir" is FORWARD, then the match is added after the current match. |
| 806 | * Otherwise, it is added before the current match. |
| 807 | * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 808 | * If the given string is already in the list of completions, then return |
| 809 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 810 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 811 | */ |
| 812 | static int |
| 813 | ins_compl_add( |
| 814 | char_u *str, |
| 815 | int len, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 816 | char_u *fname, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 817 | char_u **cptext, // extra text for popup menu or NULL |
| 818 | typval_T *user_data UNUSED, // "user_data" entry or NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 819 | int cdir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 820 | int flags_arg, |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 821 | int adup, // accept duplicate match |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 822 | int *user_hl, // user abbr/kind hlattr |
| 823 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 824 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 825 | compl_T *match, *current, *prev; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 826 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 827 | int flags = flags_arg; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 828 | int inserted = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 829 | |
Bram Moolenaar | ceb0619 | 2021-04-04 15:05:22 +0200 | [diff] [blame] | 830 | if (flags & CP_FAST) |
| 831 | fast_breakcheck(); |
| 832 | else |
| 833 | ui_breakcheck(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 834 | if (got_int) |
| 835 | return FAIL; |
| 836 | if (len < 0) |
| 837 | len = (int)STRLEN(str); |
| 838 | |
| 839 | // If the same match is already present, don't add it. |
| 840 | if (compl_first_match != NULL && !adup) |
| 841 | { |
| 842 | match = compl_first_match; |
| 843 | do |
| 844 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 845 | if (!match_at_original_text(match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 846 | && STRNCMP(match->cp_str.string, str, len) == 0 |
| 847 | && ((int)match->cp_str.length <= len |
| 848 | || match->cp_str.string[len] == NUL)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 849 | return NOTDONE; |
| 850 | match = match->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 851 | } while (match != NULL && !is_first_match(match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | // Remove any popup menu before changing the list of matches. |
| 855 | ins_compl_del_pum(); |
| 856 | |
| 857 | // Allocate a new match structure. |
| 858 | // Copy the values to the new match structure. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 859 | match = ALLOC_CLEAR_ONE(compl_T); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 860 | if (match == NULL) |
| 861 | return FAIL; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 862 | match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 863 | if ((match->cp_str.string = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 864 | { |
| 865 | vim_free(match); |
| 866 | return FAIL; |
| 867 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 868 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 869 | match->cp_str.length = len; |
| 870 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 871 | // match-fname is: |
| 872 | // - compl_curr_match->cp_fname if it is a string equal to fname. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 873 | // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 874 | // - NULL otherwise. --Acevedo |
| 875 | if (fname != NULL |
| 876 | && compl_curr_match != NULL |
| 877 | && compl_curr_match->cp_fname != NULL |
| 878 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 879 | match->cp_fname = compl_curr_match->cp_fname; |
| 880 | else if (fname != NULL) |
| 881 | { |
| 882 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 883 | flags |= CP_FREE_FNAME; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 884 | } |
| 885 | else |
| 886 | match->cp_fname = NULL; |
| 887 | match->cp_flags = flags; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 888 | match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1; |
| 889 | match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 890 | match->cp_score = score; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 891 | match->cp_cpt_value_idx = cpt_value_idx; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 892 | |
| 893 | if (cptext != NULL) |
| 894 | { |
| 895 | int i; |
| 896 | |
| 897 | for (i = 0; i < CPT_COUNT; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 898 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 899 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 900 | match->cp_text[i] = vim_strsave(cptext[i]); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 901 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 902 | } |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 903 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 904 | if (user_data != NULL) |
| 905 | match->cp_user_data = *user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 906 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 907 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 908 | // Link the new match structure after (FORWARD) or before (BACKWARD) the |
| 909 | // current match in the list of matches . |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 910 | if (compl_first_match == NULL) |
| 911 | match->cp_next = match->cp_prev = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 912 | else if (cfc_has_mode() && score > 0 && compl_get_longest) |
| 913 | { |
| 914 | current = compl_first_match->cp_next; |
| 915 | prev = compl_first_match; |
| 916 | inserted = FALSE; |
| 917 | // The direction is ignored when using longest and |
| 918 | // completefuzzycollect, because matches are inserted |
| 919 | // and sorted by score. |
| 920 | while (current != NULL && current != compl_first_match) |
| 921 | { |
| 922 | if (current->cp_score < score) |
| 923 | { |
| 924 | match->cp_next = current; |
| 925 | match->cp_prev = current->cp_prev; |
| 926 | if (current->cp_prev) |
| 927 | current->cp_prev->cp_next = match; |
| 928 | current->cp_prev = match; |
| 929 | inserted = TRUE; |
| 930 | break; |
| 931 | } |
| 932 | prev = current; |
| 933 | current = current->cp_next; |
| 934 | } |
| 935 | if (!inserted) |
| 936 | { |
| 937 | prev->cp_next = match; |
| 938 | match->cp_prev = prev; |
| 939 | match->cp_next = compl_first_match; |
| 940 | compl_first_match->cp_prev = match; |
| 941 | } |
| 942 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 943 | else if (dir == FORWARD) |
| 944 | { |
| 945 | match->cp_next = compl_curr_match->cp_next; |
| 946 | match->cp_prev = compl_curr_match; |
| 947 | } |
| 948 | else // BACKWARD |
| 949 | { |
| 950 | match->cp_next = compl_curr_match; |
| 951 | match->cp_prev = compl_curr_match->cp_prev; |
| 952 | } |
| 953 | if (match->cp_next) |
| 954 | match->cp_next->cp_prev = match; |
| 955 | if (match->cp_prev) |
| 956 | match->cp_prev->cp_next = match; |
| 957 | else // if there's nothing before, it is the first match |
| 958 | compl_first_match = match; |
| 959 | compl_curr_match = match; |
| 960 | |
| 961 | // Find the longest common string if still doing that. |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 962 | if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 963 | ins_compl_longest_match(match); |
| 964 | |
| 965 | return OK; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 970 | * match->cp_flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 971 | */ |
| 972 | static int |
| 973 | ins_compl_equal(compl_T *match, char_u *str, int len) |
| 974 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 975 | if (match->cp_flags & CP_EQUAL) |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 976 | return TRUE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 977 | if (match->cp_flags & CP_ICASE) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 978 | return STRNICMP(match->cp_str.string, str, (size_t)len) == 0; |
| 979 | return STRNCMP(match->cp_str.string, str, (size_t)len) == 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 983 | * when len is -1 mean use whole length of p otherwise part of p |
| 984 | */ |
| 985 | static void |
| 986 | ins_compl_insert_bytes(char_u *p, int len) |
| 987 | { |
| 988 | if (len == -1) |
| 989 | len = (int)STRLEN(p); |
| 990 | ins_bytes_len(p, len); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 991 | compl_ins_end_col = curwin->w_cursor.col; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* |
zeertzjq | d32bf0a | 2024-12-17 20:55:13 +0100 | [diff] [blame] | 995 | * Checks if the column is within the currently inserted completion text |
| 996 | * column range. If it is, it returns a special highlight attribute. |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 997 | * -1 means normal item. |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 998 | */ |
| 999 | int |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1000 | ins_compl_col_range_attr(linenr_T lnum, int col) |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1001 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1002 | int start_col; |
| 1003 | int attr; |
| 1004 | |
| 1005 | if ((get_cot_flags() & COT_FUZZY) |
| 1006 | || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0) |
glepnir | e890887 | 2025-01-08 18:30:45 +0100 | [diff] [blame] | 1007 | return -1; |
| 1008 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1009 | start_col = compl_col + (int)ins_compl_leader_len(); |
| 1010 | if (!ins_compl_has_multiple()) |
| 1011 | return (col >= start_col && col < compl_ins_end_col) ? attr : -1; |
| 1012 | |
| 1013 | // Multiple lines |
| 1014 | if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) || |
| 1015 | (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) || |
| 1016 | (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col)) |
| 1017 | return attr; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1018 | |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1023 | * Returns TRUE if the current completion string contains newline characters, |
| 1024 | * indicating it's a multi-line completion. |
| 1025 | */ |
| 1026 | static int |
| 1027 | ins_compl_has_multiple(void) |
| 1028 | { |
| 1029 | return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Returns TRUE if the given line number falls within the range of a multi-line |
| 1034 | * completion, i.e. between the starting line (compl_lnum) and current cursor |
| 1035 | * line. Always returns FALSE for single-line completions. |
| 1036 | */ |
| 1037 | int |
| 1038 | ins_compl_lnum_in_range(linenr_T lnum) |
| 1039 | { |
| 1040 | if (!ins_compl_has_multiple()) |
| 1041 | return FALSE; |
| 1042 | return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1046 | * Reduce the longest common string for match "match". |
| 1047 | */ |
| 1048 | static void |
| 1049 | ins_compl_longest_match(compl_T *match) |
| 1050 | { |
| 1051 | char_u *p, *s; |
| 1052 | int c1, c2; |
| 1053 | int had_match; |
| 1054 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1055 | if (compl_leader.string == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1056 | { |
| 1057 | // First match, use it as a whole. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1058 | compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length); |
| 1059 | if (compl_leader.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1060 | return; |
| 1061 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1062 | compl_leader.length = match->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1063 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1064 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1065 | |
| 1066 | // When the match isn't there (to avoid matching itself) remove it |
| 1067 | // again after redrawing. |
| 1068 | if (!had_match) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1069 | ins_compl_delete(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1070 | compl_used_match = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1071 | |
| 1072 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1073 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1074 | |
| 1075 | // Reduce the text if this match differs from compl_leader. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1076 | p = compl_leader.string; |
| 1077 | s = match->cp_str.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1078 | while (*p != NUL) |
| 1079 | { |
| 1080 | if (has_mbyte) |
| 1081 | { |
| 1082 | c1 = mb_ptr2char(p); |
| 1083 | c2 = mb_ptr2char(s); |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | c1 = *p; |
| 1088 | c2 = *s; |
| 1089 | } |
| 1090 | if ((match->cp_flags & CP_ICASE) |
| 1091 | ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2)) |
| 1092 | break; |
| 1093 | if (has_mbyte) |
| 1094 | { |
| 1095 | MB_PTR_ADV(p); |
| 1096 | MB_PTR_ADV(s); |
| 1097 | } |
| 1098 | else |
| 1099 | { |
| 1100 | ++p; |
| 1101 | ++s; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | if (*p != NUL) |
| 1106 | { |
| 1107 | // Leader was shortened, need to change the inserted text. |
| 1108 | *p = NUL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1109 | compl_leader.length = (size_t)(p - compl_leader.string); |
| 1110 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1111 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1112 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1113 | |
| 1114 | // When the match isn't there (to avoid matching itself) remove it |
| 1115 | // again after redrawing. |
| 1116 | if (!had_match) |
| 1117 | ins_compl_delete(); |
| 1118 | } |
| 1119 | |
| 1120 | compl_used_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Add an array of matches to the list of matches. |
| 1125 | * Frees matches[]. |
| 1126 | */ |
| 1127 | static void |
| 1128 | ins_compl_add_matches( |
| 1129 | int num_matches, |
| 1130 | char_u **matches, |
| 1131 | int icase) |
| 1132 | { |
| 1133 | int i; |
| 1134 | int add_r = OK; |
| 1135 | int dir = compl_direction; |
| 1136 | |
| 1137 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1138 | { |
| 1139 | add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1140 | CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1141 | if (add_r == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1142 | // if dir was BACKWARD then honor it just once |
| 1143 | dir = FORWARD; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1144 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1145 | FreeWild(num_matches, matches); |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Make the completion list cyclic. |
| 1150 | * Return the number of matches (excluding the original). |
| 1151 | */ |
| 1152 | static int |
| 1153 | ins_compl_make_cyclic(void) |
| 1154 | { |
| 1155 | compl_T *match; |
| 1156 | int count = 0; |
| 1157 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1158 | if (compl_first_match == NULL) |
| 1159 | return 0; |
| 1160 | |
| 1161 | // Find the end of the list. |
| 1162 | match = compl_first_match; |
| 1163 | // there's always an entry for the compl_orig_text, it doesn't count. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1164 | while (match->cp_next != NULL && !is_first_match(match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1165 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1166 | match = match->cp_next; |
| 1167 | ++count; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1168 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1169 | match->cp_next = compl_first_match; |
| 1170 | compl_first_match->cp_prev = match; |
| 1171 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1172 | return count; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Return whether there currently is a shown match. |
| 1177 | */ |
| 1178 | int |
| 1179 | ins_compl_has_shown_match(void) |
| 1180 | { |
| 1181 | return compl_shown_match == NULL |
| 1182 | || compl_shown_match != compl_shown_match->cp_next; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Return whether the shown match is long enough. |
| 1187 | */ |
| 1188 | int |
| 1189 | ins_compl_long_shown_match(void) |
| 1190 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1191 | return (int)compl_shown_match->cp_str.length |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1192 | > curwin->w_cursor.col - compl_col; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1196 | * Get the local or global value of 'completeopt' flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1197 | */ |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1198 | unsigned int |
| 1199 | get_cot_flags(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1200 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1201 | return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1204 | /* |
| 1205 | * Update the screen and when there is any scrolling remove the popup menu. |
| 1206 | */ |
| 1207 | static void |
| 1208 | ins_compl_upd_pum(void) |
| 1209 | { |
| 1210 | int h; |
| 1211 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1212 | if (compl_match_array == NULL) |
| 1213 | return; |
| 1214 | |
| 1215 | h = curwin->w_cline_height; |
| 1216 | // Update the screen later, before drawing the popup menu over it. |
| 1217 | pum_call_update_screen(); |
| 1218 | if (h != curwin->w_cline_height) |
| 1219 | ins_compl_del_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Remove any popup menu. |
| 1224 | */ |
| 1225 | static void |
| 1226 | ins_compl_del_pum(void) |
| 1227 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1228 | if (compl_match_array == NULL) |
| 1229 | return; |
| 1230 | |
| 1231 | pum_undisplay(); |
| 1232 | VIM_CLEAR(compl_match_array); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Return TRUE if the popup menu should be displayed. |
| 1237 | */ |
| 1238 | int |
| 1239 | pum_wanted(void) |
| 1240 | { |
| 1241 | // 'completeopt' must contain "menu" or "menuone" |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1242 | if ((get_cot_flags() & COT_ANY_MENU) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1243 | return FALSE; |
| 1244 | |
| 1245 | // The display looks bad on a B&W display. |
| 1246 | if (t_colors < 8 |
| 1247 | #ifdef FEAT_GUI |
| 1248 | && !gui.in_use |
| 1249 | #endif |
| 1250 | ) |
| 1251 | return FALSE; |
| 1252 | return TRUE; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 1257 | * One if 'completopt' contains "menuone". |
| 1258 | */ |
| 1259 | static int |
| 1260 | pum_enough_matches(void) |
| 1261 | { |
| 1262 | compl_T *compl; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1263 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1264 | |
| 1265 | // Don't display the popup menu if there are no matches or there is only |
| 1266 | // one (ignoring the original text). |
| 1267 | compl = compl_first_match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1268 | do |
| 1269 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1270 | if (compl == NULL || (!match_at_original_text(compl) && ++i == 2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1271 | break; |
| 1272 | compl = compl->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1273 | } while (!is_first_match(compl)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1274 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1275 | if (get_cot_flags() & COT_MENUONE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1276 | return (i >= 1); |
| 1277 | return (i >= 2); |
| 1278 | } |
| 1279 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1280 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1281 | /* |
| 1282 | * Allocate Dict for the completed item. |
| 1283 | * { word, abbr, menu, kind, info } |
| 1284 | */ |
| 1285 | static dict_T * |
| 1286 | ins_compl_dict_alloc(compl_T *match) |
| 1287 | { |
| 1288 | dict_T *dict = dict_alloc_lock(VAR_FIXED); |
| 1289 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1290 | if (dict == NULL) |
| 1291 | return NULL; |
| 1292 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1293 | dict_add_string(dict, "word", match->cp_str.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1294 | dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]); |
| 1295 | dict_add_string(dict, "menu", match->cp_text[CPT_MENU]); |
| 1296 | dict_add_string(dict, "kind", match->cp_text[CPT_KIND]); |
| 1297 | dict_add_string(dict, "info", match->cp_text[CPT_INFO]); |
| 1298 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 1299 | dict_add_string(dict, "user_data", (char_u *)""); |
| 1300 | else |
| 1301 | dict_add_tv(dict, "user_data", &match->cp_user_data); |
| 1302 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1303 | return dict; |
| 1304 | } |
| 1305 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1306 | /* |
| 1307 | * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode |
| 1308 | * completion menu is changed. |
| 1309 | */ |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1310 | static void |
| 1311 | trigger_complete_changed_event(int cur) |
| 1312 | { |
| 1313 | dict_T *v_event; |
| 1314 | dict_T *item; |
| 1315 | static int recursive = FALSE; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1316 | save_v_event_T save_v_event; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1317 | |
| 1318 | if (recursive) |
| 1319 | return; |
| 1320 | |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 1321 | item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1322 | if (item == NULL) |
| 1323 | return; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1324 | v_event = get_v_event(&save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1325 | dict_add_dict(v_event, "completed_item", item); |
| 1326 | pum_set_event_info(v_event); |
| 1327 | dict_set_items_ro(v_event); |
| 1328 | |
| 1329 | recursive = TRUE; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1330 | textlock++; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1331 | apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1332 | textlock--; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1333 | recursive = FALSE; |
| 1334 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1335 | restore_v_event(v_event, &save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1336 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1337 | #endif |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1338 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1339 | /* |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1340 | * pumitem qsort compare func |
| 1341 | */ |
| 1342 | static int |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1343 | ins_compl_fuzzy_cmp(const void *a, const void *b) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1344 | { |
| 1345 | const int sa = (*(pumitem_T *)a).pum_score; |
| 1346 | const int sb = (*(pumitem_T *)b).pum_score; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1347 | const int ia = (*(pumitem_T *)a).pum_idx; |
| 1348 | const int ib = (*(pumitem_T *)b).pum_idx; |
| 1349 | return sa == sb ? (ia == ib ? 0 : (ia < ib ? -1 : 1)) : (sa < sb ? 1 : -1); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1353 | * Build a popup menu to show the completion matches. |
| 1354 | * Returns the popup menu entry that should be selected. Returns -1 if nothing |
| 1355 | * should be selected. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1356 | */ |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1357 | static int |
| 1358 | ins_compl_build_pum(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1359 | { |
| 1360 | compl_T *compl; |
| 1361 | compl_T *shown_compl = NULL; |
| 1362 | int did_find_shown_match = FALSE; |
| 1363 | int shown_match_ok = FALSE; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1364 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1365 | int cur = -1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1366 | int max_fuzzy_score = 0; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 1367 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1368 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1369 | int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0; |
| 1370 | int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1371 | compl_T *match_head = NULL; |
| 1372 | compl_T *match_tail = NULL; |
| 1373 | compl_T *match_next = NULL; |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1374 | int update_shown_match = fuzzy_filter; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1375 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1376 | // Need to build the popup menu list. |
| 1377 | compl_match_arraysize = 0; |
| 1378 | compl = compl_first_match; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1379 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1380 | // If the current match is the original text don't find the first |
| 1381 | // match after it, don't highlight anything. |
| 1382 | if (match_at_original_text(compl_shown_match)) |
| 1383 | shown_match_ok = TRUE; |
| 1384 | |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1385 | if (fuzzy_filter && ctrl_x_mode_normal() && compl_leader.string == NULL |
| 1386 | && compl_shown_match->cp_score > 0) |
| 1387 | update_shown_match = FALSE; |
| 1388 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1389 | if (compl_leader.string != NULL |
| 1390 | && STRCMP(compl_leader.string, compl_orig_text.string) == 0 |
| 1391 | && shown_match_ok == FALSE) |
| 1392 | compl_shown_match = compl_no_select ? compl_first_match |
| 1393 | : compl_first_match->cp_next; |
| 1394 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1395 | do |
| 1396 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1397 | compl->cp_in_match_array = FALSE; |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 1398 | // When 'completeopt' contains "fuzzy" and leader is not NULL or empty, |
| 1399 | // set the cp_score for later comparisons. |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1400 | if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1401 | compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1402 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1403 | if (!match_at_original_text(compl) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1404 | && (compl_leader.string == NULL |
| 1405 | || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length) |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1406 | || (fuzzy_filter && compl->cp_score > 0))) |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1407 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1408 | ++compl_match_arraysize; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1409 | compl->cp_in_match_array = TRUE; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1410 | if (match_head == NULL) |
| 1411 | match_head = compl; |
| 1412 | else |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1413 | match_tail->cp_match_next = compl; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1414 | match_tail = compl; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1415 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1416 | if (!shown_match_ok && !fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1417 | { |
| 1418 | if (compl == compl_shown_match || did_find_shown_match) |
| 1419 | { |
| 1420 | // This item is the shown match or this is the |
| 1421 | // first displayed item after the shown match. |
| 1422 | compl_shown_match = compl; |
| 1423 | did_find_shown_match = TRUE; |
| 1424 | shown_match_ok = TRUE; |
| 1425 | } |
| 1426 | else |
| 1427 | // Remember this displayed match for when the |
| 1428 | // shown match is just below it. |
| 1429 | shown_compl = compl; |
| 1430 | cur = i; |
| 1431 | } |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1432 | else if (fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1433 | { |
| 1434 | if (i == 0) |
| 1435 | shown_compl = compl; |
| 1436 | // Update the maximum fuzzy score and the shown match |
| 1437 | // if the current item's score is higher |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1438 | if (fuzzy_sort && compl->cp_score > max_fuzzy_score |
| 1439 | && update_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1440 | { |
| 1441 | did_find_shown_match = TRUE; |
| 1442 | max_fuzzy_score = compl->cp_score; |
| 1443 | if (!compl_no_select) |
| 1444 | compl_shown_match = compl; |
| 1445 | } |
| 1446 | |
glepnir | 3af0a8d | 2025-02-20 22:06:16 +0100 | [diff] [blame] | 1447 | if (!shown_match_ok && compl == compl_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1448 | { |
| 1449 | cur = i; |
| 1450 | shown_match_ok = TRUE; |
| 1451 | } |
| 1452 | } |
| 1453 | i++; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1454 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1455 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1456 | if (compl == compl_shown_match && !fuzzy_filter) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1457 | { |
| 1458 | did_find_shown_match = TRUE; |
| 1459 | |
| 1460 | // When the original text is the shown match don't set |
| 1461 | // compl_shown_match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1462 | if (match_at_original_text(compl)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1463 | shown_match_ok = TRUE; |
| 1464 | |
| 1465 | if (!shown_match_ok && shown_compl != NULL) |
| 1466 | { |
| 1467 | // The shown match isn't displayed, set it to the |
| 1468 | // previously displayed match. |
| 1469 | compl_shown_match = shown_compl; |
| 1470 | shown_match_ok = TRUE; |
| 1471 | } |
| 1472 | } |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1473 | compl = compl->cp_next; |
| 1474 | } while (compl != NULL && !is_first_match(compl)); |
| 1475 | |
| 1476 | if (compl_match_arraysize == 0) |
| 1477 | return -1; |
| 1478 | |
glepnir | c0b7ca4 | 2025-02-13 20:27:44 +0100 | [diff] [blame] | 1479 | if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok) |
| 1480 | { |
| 1481 | compl_shown_match = shown_compl; |
| 1482 | shown_match_ok = TRUE; |
| 1483 | cur = 0; |
| 1484 | } |
| 1485 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1486 | compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize); |
| 1487 | if (compl_match_array == NULL) |
| 1488 | return -1; |
| 1489 | |
| 1490 | compl = match_head; |
| 1491 | i = 0; |
| 1492 | while (compl != NULL) |
| 1493 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1494 | compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL |
| 1495 | ? compl->cp_text[CPT_ABBR] : compl->cp_str.string; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1496 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 1497 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 1498 | compl_match_array[i].pum_score = compl->cp_score; |
| 1499 | compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr; |
| 1500 | compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1501 | compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL |
| 1502 | ? compl->cp_text[CPT_MENU] : compl->cp_fname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1503 | match_next = compl->cp_match_next; |
| 1504 | compl->cp_match_next = NULL; |
| 1505 | compl = match_next; |
| 1506 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1507 | |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1508 | if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0) |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1509 | { |
| 1510 | for (i = 0; i < compl_match_arraysize; i++) |
| 1511 | compl_match_array[i].pum_idx = i; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1512 | // sort by the largest score of fuzzy match |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1513 | qsort(compl_match_array, (size_t)compl_match_arraysize, |
| 1514 | sizeof(pumitem_T), ins_compl_fuzzy_cmp); |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 1515 | shown_match_ok = TRUE; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1516 | } |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1517 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1518 | if (!shown_match_ok) // no displayed match at all |
| 1519 | cur = -1; |
| 1520 | |
| 1521 | return cur; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * Show the popup menu for the list of matches. |
| 1526 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
| 1527 | */ |
| 1528 | void |
| 1529 | ins_compl_show_pum(void) |
| 1530 | { |
| 1531 | int i; |
| 1532 | int cur = -1; |
| 1533 | colnr_T col; |
| 1534 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1535 | if (!pum_wanted() || !pum_enough_matches()) |
| 1536 | return; |
| 1537 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1538 | // Update the screen later, before drawing the popup menu over it. |
| 1539 | pum_call_update_screen(); |
| 1540 | |
| 1541 | if (compl_match_array == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1542 | // Need to build the popup menu list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1543 | cur = ins_compl_build_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1544 | else |
| 1545 | { |
| 1546 | // popup menu already exists, only need to find the current item. |
| 1547 | for (i = 0; i < compl_match_arraysize; ++i) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1548 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str.string |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1549 | || compl_match_array[i].pum_text |
| 1550 | == compl_shown_match->cp_text[CPT_ABBR]) |
| 1551 | { |
| 1552 | cur = i; |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1557 | if (compl_match_array == NULL) |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1558 | { |
| 1559 | #ifdef FEAT_EVAL |
| 1560 | if (compl_started && has_completechanged()) |
| 1561 | trigger_complete_changed_event(cur); |
| 1562 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1563 | return; |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1564 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1565 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1566 | // In Replace mode when a $ is displayed at the end of the line only |
| 1567 | // part of the screen would be updated. We do need to redraw here. |
| 1568 | dollar_vcol = -1; |
| 1569 | |
| 1570 | // Compute the screen column of the start of the completed text. |
| 1571 | // Use the cursor to get all wrapping and other settings right. |
| 1572 | col = curwin->w_cursor.col; |
| 1573 | curwin->w_cursor.col = compl_col; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1574 | compl_selected_item = cur; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1575 | pum_display(compl_match_array, compl_match_arraysize, cur); |
| 1576 | curwin->w_cursor.col = col; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1577 | |
glepnir | cbb46b4 | 2024-02-03 18:11:13 +0100 | [diff] [blame] | 1578 | // After adding leader, set the current match to shown match. |
| 1579 | if (compl_started && compl_curr_match != compl_shown_match) |
| 1580 | compl_curr_match = compl_shown_match; |
| 1581 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1582 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1583 | if (has_completechanged()) |
| 1584 | trigger_complete_changed_event(cur); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1585 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | #define DICT_FIRST (1) // use just first element in "dict" |
| 1589 | #define DICT_EXACT (2) // "dict" is the exact name of a file |
| 1590 | |
| 1591 | /* |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1592 | * Get current completion leader |
| 1593 | */ |
| 1594 | char_u * |
| 1595 | ins_compl_leader(void) |
| 1596 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1597 | return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string; |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Get current completion leader length |
| 1602 | */ |
| 1603 | size_t |
| 1604 | ins_compl_leader_len(void) |
| 1605 | { |
| 1606 | return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /* |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1610 | * Add any identifiers that match the given pattern "pat" in the list of |
| 1611 | * dictionary files "dict_start" to the list of completions. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1612 | */ |
| 1613 | static void |
| 1614 | ins_compl_dictionaries( |
| 1615 | char_u *dict_start, |
| 1616 | char_u *pat, |
| 1617 | int flags, // DICT_FIRST and/or DICT_EXACT |
| 1618 | int thesaurus) // Thesaurus completion |
| 1619 | { |
| 1620 | char_u *dict = dict_start; |
| 1621 | char_u *ptr; |
| 1622 | char_u *buf; |
| 1623 | regmatch_T regmatch; |
| 1624 | char_u **files; |
| 1625 | int count; |
| 1626 | int save_p_scs; |
| 1627 | int dir = compl_direction; |
| 1628 | |
| 1629 | if (*dict == NUL) |
| 1630 | { |
| 1631 | #ifdef FEAT_SPELL |
| 1632 | // When 'dictionary' is empty and spell checking is enabled use |
| 1633 | // "spell". |
| 1634 | if (!thesaurus && curwin->w_p_spell) |
| 1635 | dict = (char_u *)"spell"; |
| 1636 | else |
| 1637 | #endif |
| 1638 | return; |
| 1639 | } |
| 1640 | |
| 1641 | buf = alloc(LSIZE); |
| 1642 | if (buf == NULL) |
| 1643 | return; |
| 1644 | regmatch.regprog = NULL; // so that we can goto theend |
| 1645 | |
| 1646 | // If 'infercase' is set, don't use 'smartcase' here |
| 1647 | save_p_scs = p_scs; |
| 1648 | if (curbuf->b_p_inf) |
| 1649 | p_scs = FALSE; |
| 1650 | |
| 1651 | // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 1652 | // to only match at the start of a line. Otherwise just match the |
| 1653 | // pattern. Also need to double backslashes. |
| 1654 | if (ctrl_x_mode_line_or_eval()) |
| 1655 | { |
| 1656 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
| 1657 | size_t len; |
| 1658 | |
| 1659 | if (pat_esc == NULL) |
| 1660 | goto theend; |
| 1661 | len = STRLEN(pat_esc) + 10; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1662 | ptr = alloc(len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1663 | if (ptr == NULL) |
| 1664 | { |
| 1665 | vim_free(pat_esc); |
| 1666 | goto theend; |
| 1667 | } |
| 1668 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
| 1669 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 1670 | vim_free(pat_esc); |
| 1671 | vim_free(ptr); |
| 1672 | } |
| 1673 | else |
| 1674 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1675 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1676 | if (regmatch.regprog == NULL) |
| 1677 | goto theend; |
| 1678 | } |
| 1679 | |
| 1680 | // ignore case depends on 'ignorecase', 'smartcase' and "pat" |
| 1681 | regmatch.rm_ic = ignorecase(pat); |
| 1682 | while (*dict != NUL && !got_int && !compl_interrupted) |
| 1683 | { |
| 1684 | // copy one dictionary file name into buf |
| 1685 | if (flags == DICT_EXACT) |
| 1686 | { |
| 1687 | count = 1; |
| 1688 | files = &dict; |
| 1689 | } |
| 1690 | else |
| 1691 | { |
| 1692 | // Expand wildcards in the dictionary name, but do not allow |
| 1693 | // backticks (for security, the 'dict' option may have been set in |
| 1694 | // a modeline). |
| 1695 | copy_option_part(&dict, buf, LSIZE, ","); |
| 1696 | # ifdef FEAT_SPELL |
| 1697 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 1698 | count = -1; |
| 1699 | else |
| 1700 | # endif |
| 1701 | if (vim_strchr(buf, '`') != NULL |
| 1702 | || expand_wildcards(1, &buf, &count, &files, |
| 1703 | EW_FILE|EW_SILENT) != OK) |
| 1704 | count = 0; |
| 1705 | } |
| 1706 | |
| 1707 | # ifdef FEAT_SPELL |
| 1708 | if (count == -1) |
| 1709 | { |
| 1710 | // Complete from active spelling. Skip "\<" in the pattern, we |
| 1711 | // don't use it as a RE. |
| 1712 | if (pat[0] == '\\' && pat[1] == '<') |
| 1713 | ptr = pat + 2; |
| 1714 | else |
| 1715 | ptr = pat; |
| 1716 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
| 1717 | } |
| 1718 | else |
| 1719 | # endif |
| 1720 | if (count > 0) // avoid warning for using "files" uninit |
| 1721 | { |
| 1722 | ins_compl_files(count, files, thesaurus, flags, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1723 | (cfc_has_mode() ? NULL : ®match), buf, &dir); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1724 | if (flags != DICT_EXACT) |
| 1725 | FreeWild(count, files); |
| 1726 | } |
| 1727 | if (flags != 0) |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | theend: |
| 1732 | p_scs = save_p_scs; |
| 1733 | vim_regfree(regmatch.regprog); |
| 1734 | vim_free(buf); |
| 1735 | } |
| 1736 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1737 | /* |
| 1738 | * Add all the words in the line "*buf_arg" from the thesaurus file "fname" |
| 1739 | * skipping the word at 'skip_word'. Returns OK on success. |
| 1740 | */ |
| 1741 | static int |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 1742 | thesaurus_add_words_in_line( |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1743 | char_u *fname, |
| 1744 | char_u **buf_arg, |
| 1745 | int dir, |
| 1746 | char_u *skip_word) |
| 1747 | { |
| 1748 | int status = OK; |
| 1749 | char_u *ptr; |
| 1750 | char_u *wstart; |
| 1751 | |
| 1752 | // Add the other matches on the line |
| 1753 | ptr = *buf_arg; |
| 1754 | while (!got_int) |
| 1755 | { |
| 1756 | // Find start of the next word. Skip white |
| 1757 | // space and punctuation. |
| 1758 | ptr = find_word_start(ptr); |
| 1759 | if (*ptr == NUL || *ptr == NL) |
| 1760 | break; |
| 1761 | wstart = ptr; |
| 1762 | |
| 1763 | // Find end of the word. |
| 1764 | if (has_mbyte) |
| 1765 | // Japanese words may have characters in |
| 1766 | // different classes, only separate words |
| 1767 | // with single-byte non-word characters. |
| 1768 | while (*ptr != NUL) |
| 1769 | { |
| 1770 | int l = (*mb_ptr2len)(ptr); |
| 1771 | |
| 1772 | if (l < 2 && !vim_iswordc(*ptr)) |
| 1773 | break; |
| 1774 | ptr += l; |
| 1775 | } |
| 1776 | else |
| 1777 | ptr = find_word_end(ptr); |
| 1778 | |
| 1779 | // Add the word. Skip the regexp match. |
| 1780 | if (wstart != skip_word) |
| 1781 | { |
| 1782 | status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1783 | fname, dir, FALSE, 0); |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1784 | if (status == FAIL) |
| 1785 | break; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | *buf_arg = ptr; |
| 1790 | return status; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * Process "count" dictionary/thesaurus "files" and add the text matching |
| 1795 | * "regmatch". |
| 1796 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1797 | static void |
| 1798 | ins_compl_files( |
| 1799 | int count, |
| 1800 | char_u **files, |
| 1801 | int thesaurus, |
| 1802 | int flags, |
| 1803 | regmatch_T *regmatch, |
| 1804 | char_u *buf, |
| 1805 | int *dir) |
| 1806 | { |
| 1807 | char_u *ptr; |
| 1808 | int i; |
| 1809 | FILE *fp; |
| 1810 | int add_r; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1811 | char_u *leader = NULL; |
| 1812 | int leader_len = 0; |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 1813 | int in_fuzzy_collect = cfc_has_mode(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1814 | int score = 0; |
| 1815 | int len = 0; |
| 1816 | char_u *line_end = NULL; |
| 1817 | |
| 1818 | if (in_fuzzy_collect) |
| 1819 | { |
| 1820 | leader = ins_compl_leader(); |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 1821 | leader_len = (int)ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1822 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1823 | |
| 1824 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 1825 | { |
| 1826 | fp = mch_fopen((char *)files[i], "r"); // open dictionary file |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 1827 | if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1828 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1829 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1830 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1831 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 1832 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 1833 | } |
| 1834 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1835 | if (fp == NULL) |
| 1836 | continue; |
| 1837 | |
| 1838 | // Read dictionary file line by line. |
| 1839 | // Check each line for a match. |
| 1840 | while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1841 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1842 | ptr = buf; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1843 | if (regmatch != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1844 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1845 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1846 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1847 | ptr = regmatch->startp[0]; |
| 1848 | ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr) |
| 1849 | : find_word_end(ptr); |
| 1850 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 1851 | (int)(ptr - regmatch->startp[0]), |
| 1852 | p_ic, files[i], *dir, FALSE, 0); |
| 1853 | if (thesaurus) |
| 1854 | { |
| 1855 | // For a thesaurus, add all the words in the line |
| 1856 | ptr = buf; |
| 1857 | add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir, |
| 1858 | regmatch->startp[0]); |
| 1859 | } |
| 1860 | if (add_r == OK) |
| 1861 | // if dir was BACKWARD then honor it just once |
| 1862 | *dir = FORWARD; |
| 1863 | else if (add_r == FAIL) |
| 1864 | break; |
| 1865 | // avoid expensive call to vim_regexec() when at end |
| 1866 | // of line |
| 1867 | if (*ptr == '\n' || got_int) |
| 1868 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1869 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1870 | } |
| 1871 | else if (in_fuzzy_collect && leader_len > 0) |
| 1872 | { |
| 1873 | line_end = find_line_end(ptr); |
| 1874 | while (ptr < line_end) |
| 1875 | { |
| 1876 | if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score)) |
| 1877 | { |
| 1878 | char_u *end_ptr = ctrl_x_mode_line_or_eval() |
| 1879 | ? find_line_end(ptr) : find_word_end(ptr); |
| 1880 | add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr), |
| 1881 | p_ic, files[i], *dir, FALSE, score); |
| 1882 | if (add_r == FAIL) |
| 1883 | break; |
| 1884 | ptr = end_ptr; // start from next word |
| 1885 | if (compl_get_longest && ctrl_x_mode_normal() |
| 1886 | && compl_first_match->cp_next |
| 1887 | && score == compl_first_match->cp_next->cp_score) |
| 1888 | compl_num_bests++; |
| 1889 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1890 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1891 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1892 | line_breakcheck(); |
| 1893 | ins_compl_check_keys(50, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1894 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1895 | fclose(fp); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * Find the start of the next word. |
| 1901 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 1902 | */ |
| 1903 | char_u * |
| 1904 | find_word_start(char_u *ptr) |
| 1905 | { |
| 1906 | if (has_mbyte) |
| 1907 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
| 1908 | ptr += (*mb_ptr2len)(ptr); |
| 1909 | else |
| 1910 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 1911 | ++ptr; |
| 1912 | return ptr; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Find the end of the word. Assumes it starts inside a word. |
| 1917 | * Returns a pointer to just after the word. |
| 1918 | */ |
| 1919 | char_u * |
| 1920 | find_word_end(char_u *ptr) |
| 1921 | { |
| 1922 | int start_class; |
| 1923 | |
| 1924 | if (has_mbyte) |
| 1925 | { |
| 1926 | start_class = mb_get_class(ptr); |
| 1927 | if (start_class > 1) |
| 1928 | while (*ptr != NUL) |
| 1929 | { |
| 1930 | ptr += (*mb_ptr2len)(ptr); |
| 1931 | if (mb_get_class(ptr) != start_class) |
| 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | else |
| 1936 | while (vim_iswordc(*ptr)) |
| 1937 | ++ptr; |
| 1938 | return ptr; |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * Find the end of the line, omitting CR and NL at the end. |
| 1943 | * Returns a pointer to just after the line. |
| 1944 | */ |
glepnir | dd42b05 | 2025-03-08 16:52:55 +0100 | [diff] [blame] | 1945 | char_u * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1946 | find_line_end(char_u *ptr) |
| 1947 | { |
| 1948 | char_u *s; |
| 1949 | |
| 1950 | s = ptr + STRLEN(ptr); |
| 1951 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 1952 | --s; |
| 1953 | return s; |
| 1954 | } |
| 1955 | |
| 1956 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 1957 | * Free a completion item in the list |
| 1958 | */ |
| 1959 | static void |
| 1960 | ins_compl_item_free(compl_T *match) |
| 1961 | { |
| 1962 | int i; |
| 1963 | |
| 1964 | VIM_CLEAR_STRING(match->cp_str); |
| 1965 | // several entries may use the same fname, free it just once. |
| 1966 | if (match->cp_flags & CP_FREE_FNAME) |
| 1967 | vim_free(match->cp_fname); |
| 1968 | for (i = 0; i < CPT_COUNT; ++i) |
| 1969 | vim_free(match->cp_text[i]); |
| 1970 | #ifdef FEAT_EVAL |
| 1971 | clear_tv(&match->cp_user_data); |
| 1972 | #endif |
| 1973 | vim_free(match); |
| 1974 | } |
| 1975 | |
| 1976 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1977 | * Free the list of completions |
| 1978 | */ |
| 1979 | static void |
| 1980 | ins_compl_free(void) |
| 1981 | { |
| 1982 | compl_T *match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1983 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1984 | VIM_CLEAR_STRING(compl_pattern); |
| 1985 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1986 | |
| 1987 | if (compl_first_match == NULL) |
| 1988 | return; |
| 1989 | |
| 1990 | ins_compl_del_pum(); |
| 1991 | pum_clear(); |
| 1992 | |
| 1993 | compl_curr_match = compl_first_match; |
| 1994 | do |
| 1995 | { |
| 1996 | match = compl_curr_match; |
| 1997 | compl_curr_match = compl_curr_match->cp_next; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 1998 | ins_compl_item_free(match); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1999 | } while (compl_curr_match != NULL && !is_first_match(compl_curr_match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2000 | compl_first_match = compl_curr_match = NULL; |
| 2001 | compl_shown_match = NULL; |
| 2002 | compl_old_match = NULL; |
| 2003 | } |
| 2004 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2005 | /* |
| 2006 | * Reset/clear the completion state. |
| 2007 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2008 | void |
| 2009 | ins_compl_clear(void) |
| 2010 | { |
| 2011 | compl_cont_status = 0; |
| 2012 | compl_started = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2013 | compl_cfc_longest_ins = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2014 | compl_matches = 0; |
glepnir | 07f0dbe | 2025-02-18 20:27:30 +0100 | [diff] [blame] | 2015 | compl_selected_item = -1; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2016 | compl_ins_end_col = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2017 | VIM_CLEAR_STRING(compl_pattern); |
| 2018 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2019 | edit_submode_extra = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2020 | VIM_CLEAR_STRING(compl_orig_text); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2021 | compl_enter_selects = FALSE; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 2022 | cpt_compl_src_clear(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2023 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2024 | // clear v:completed_item |
| 2025 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2026 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * Return TRUE when Insert completion is active. |
| 2031 | */ |
| 2032 | int |
| 2033 | ins_compl_active(void) |
| 2034 | { |
| 2035 | return compl_started; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2039 | * Return True when wp is the actual completion window |
| 2040 | */ |
| 2041 | int |
| 2042 | ins_compl_win_active(win_T *wp UNUSED) |
| 2043 | { |
| 2044 | return ins_compl_active() |
| 2045 | #if defined(FEAT_QUICKFIX) |
| 2046 | && (!wp->w_p_pvw |
| 2047 | # ifdef FEAT_PROP_POPUP |
| 2048 | && !(wp->w_popup_flags & POPF_INFO) |
| 2049 | # endif |
| 2050 | ) |
| 2051 | #endif |
| 2052 | ; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 2056 | * Selected one of the matches. When FALSE, the match was either edited or |
| 2057 | * using the longest common string. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2058 | */ |
| 2059 | int |
| 2060 | ins_compl_used_match(void) |
| 2061 | { |
| 2062 | return compl_used_match; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * Initialize get longest common string. |
| 2067 | */ |
| 2068 | void |
| 2069 | ins_compl_init_get_longest(void) |
| 2070 | { |
| 2071 | compl_get_longest = FALSE; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Returns TRUE when insert completion is interrupted. |
| 2076 | */ |
| 2077 | int |
| 2078 | ins_compl_interrupted(void) |
| 2079 | { |
| 2080 | return compl_interrupted; |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Returns TRUE if the <Enter> key selects a match in the completion popup |
| 2085 | * menu. |
| 2086 | */ |
| 2087 | int |
| 2088 | ins_compl_enter_selects(void) |
| 2089 | { |
| 2090 | return compl_enter_selects; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * Return the column where the text starts that is being completed |
| 2095 | */ |
| 2096 | colnr_T |
| 2097 | ins_compl_col(void) |
| 2098 | { |
| 2099 | return compl_col; |
| 2100 | } |
| 2101 | |
| 2102 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2103 | * Return the length in bytes of the text being completed |
| 2104 | */ |
| 2105 | int |
| 2106 | ins_compl_len(void) |
| 2107 | { |
| 2108 | return compl_length; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2112 | * Return TRUE when the 'completeopt' "preinsert" flag is in effect, |
| 2113 | * otherwise return FALSE. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2114 | */ |
| 2115 | static int |
| 2116 | ins_compl_has_preinsert(void) |
| 2117 | { |
glepnir | a2c5559 | 2025-02-28 17:43:42 +0100 | [diff] [blame] | 2118 | int cur_cot_flags = get_cot_flags(); |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2119 | return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE)) |
| 2120 | == (COT_PREINSERT | COT_MENUONE); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * Returns TRUE if the pre-insert effect is valid and the cursor is within |
| 2125 | * the `compl_ins_end_col` range. |
| 2126 | */ |
| 2127 | int |
| 2128 | ins_compl_preinsert_effect(void) |
| 2129 | { |
| 2130 | if (!ins_compl_has_preinsert()) |
| 2131 | return FALSE; |
| 2132 | |
| 2133 | return curwin->w_cursor.col < compl_ins_end_col; |
| 2134 | } |
| 2135 | |
| 2136 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2137 | * Delete one character before the cursor and show the subset of the matches |
| 2138 | * that match the word that is now before the cursor. |
| 2139 | * Returns the character to be used, NUL if the work is done and another char |
| 2140 | * to be got from the user. |
| 2141 | */ |
| 2142 | int |
| 2143 | ins_compl_bs(void) |
| 2144 | { |
| 2145 | char_u *line; |
| 2146 | char_u *p; |
| 2147 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2148 | if (ins_compl_preinsert_effect()) |
| 2149 | ins_compl_delete(); |
| 2150 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2151 | line = ml_get_curline(); |
| 2152 | p = line + curwin->w_cursor.col; |
| 2153 | MB_PTR_BACK(line, p); |
| 2154 | |
| 2155 | // Stop completion when the whole word was deleted. For Omni completion |
| 2156 | // allow the word to be deleted, we won't match everything. |
| 2157 | // Respect the 'backspace' option. |
| 2158 | if ((int)(p - line) - (int)compl_col < 0 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2159 | || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni()) |
| 2160 | || ctrl_x_mode_eval() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2161 | || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col |
| 2162 | - compl_length < 0)) |
| 2163 | return K_BS; |
| 2164 | |
| 2165 | // Deleted more than what was used to find matches or didn't finish |
| 2166 | // finding all matches: need to look for matches all over again. |
| 2167 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 2168 | || ins_compl_need_restart()) |
| 2169 | ins_compl_restart(); |
| 2170 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2171 | VIM_CLEAR_STRING(compl_leader); |
| 2172 | compl_leader.length = (size_t)((p - line) - compl_col); |
| 2173 | compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length); |
| 2174 | if (compl_leader.string == NULL) |
| 2175 | { |
| 2176 | compl_leader.length = 0; |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2177 | return K_BS; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2178 | } |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2179 | |
| 2180 | ins_compl_new_leader(); |
| 2181 | if (compl_shown_match != NULL) |
| 2182 | // Make sure current match is not a hidden item. |
| 2183 | compl_curr_match = compl_shown_match; |
| 2184 | return NUL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 2189 | * be called. |
| 2190 | */ |
| 2191 | static int |
| 2192 | ins_compl_need_restart(void) |
| 2193 | { |
| 2194 | // Return TRUE if we didn't complete finding matches or when the |
| 2195 | // 'completefunc' returned "always" in the "refresh" dictionary item. |
| 2196 | return compl_was_interrupted |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2197 | || ((ctrl_x_mode_function() || ctrl_x_mode_omni()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2198 | && compl_opt_refresh_always); |
| 2199 | } |
| 2200 | |
| 2201 | /* |
| 2202 | * Called after changing "compl_leader". |
| 2203 | * Show the popup menu with a different set of matches. |
| 2204 | * May also search for matches again if the previous search was interrupted. |
| 2205 | */ |
| 2206 | static void |
| 2207 | ins_compl_new_leader(void) |
| 2208 | { |
| 2209 | ins_compl_del_pum(); |
| 2210 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2211 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2212 | compl_used_match = FALSE; |
| 2213 | |
| 2214 | if (compl_started) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 2215 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2216 | ins_compl_set_original_text(compl_leader.string, compl_leader.length); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 2217 | if (is_cpt_func_refresh_always()) |
| 2218 | cpt_compl_refresh(); |
| 2219 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2220 | else |
| 2221 | { |
| 2222 | #ifdef FEAT_SPELL |
| 2223 | spell_bad_len = 0; // need to redetect bad word |
| 2224 | #endif |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2225 | // Matches were cleared, need to search for them now. Before drawing |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2226 | // the popup menu display the changed text before the cursor. Set |
| 2227 | // "compl_restarting" to avoid that the first match is inserted. |
| 2228 | pum_call_update_screen(); |
| 2229 | #ifdef FEAT_GUI |
| 2230 | if (gui.in_use) |
| 2231 | { |
| 2232 | // Show the cursor after the match, not after the redrawn text. |
| 2233 | setcursor(); |
| 2234 | out_flush_cursor(FALSE, FALSE); |
| 2235 | } |
| 2236 | #endif |
| 2237 | compl_restarting = TRUE; |
| 2238 | if (ins_complete(Ctrl_N, TRUE) == FAIL) |
| 2239 | compl_cont_status = 0; |
| 2240 | compl_restarting = FALSE; |
| 2241 | } |
| 2242 | |
glepnir | 4418041 | 2025-02-20 22:09:48 +0100 | [diff] [blame] | 2243 | compl_enter_selects = !compl_used_match && compl_selected_item != -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2244 | |
| 2245 | // Show the popup menu with a different set of matches. |
| 2246 | ins_compl_show_pum(); |
| 2247 | |
| 2248 | // Don't let Enter select the original text when there is no popup menu. |
| 2249 | if (compl_match_array == NULL) |
| 2250 | compl_enter_selects = FALSE; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2251 | else if (ins_compl_has_preinsert() && compl_leader.length > 0) |
| 2252 | ins_compl_insert(FALSE, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * Return the length of the completion, from the completion start column to |
| 2257 | * the cursor column. Making sure it never goes below zero. |
| 2258 | */ |
| 2259 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2260 | get_compl_len(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2261 | { |
| 2262 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2263 | return MAX(0, off); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* |
| 2267 | * Append one character to the match leader. May reduce the number of |
| 2268 | * matches. |
| 2269 | */ |
| 2270 | void |
| 2271 | ins_compl_addleader(int c) |
| 2272 | { |
| 2273 | int cc; |
| 2274 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2275 | if (ins_compl_preinsert_effect()) |
| 2276 | ins_compl_delete(); |
| 2277 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2278 | if (stop_arrow() == FAIL) |
| 2279 | return; |
| 2280 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2281 | { |
| 2282 | char_u buf[MB_MAXBYTES + 1]; |
| 2283 | |
| 2284 | (*mb_char2bytes)(c, buf); |
| 2285 | buf[cc] = NUL; |
| 2286 | ins_char_bytes(buf, cc); |
| 2287 | if (compl_opt_refresh_always) |
| 2288 | AppendToRedobuff(buf); |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | ins_char(c); |
| 2293 | if (compl_opt_refresh_always) |
| 2294 | AppendCharToRedobuff(c); |
| 2295 | } |
| 2296 | |
| 2297 | // If we didn't complete finding matches we must search again. |
| 2298 | if (ins_compl_need_restart()) |
| 2299 | ins_compl_restart(); |
| 2300 | |
| 2301 | // When 'always' is set, don't reset compl_leader. While completing, |
| 2302 | // cursor doesn't point original position, changing compl_leader would |
| 2303 | // break redo. |
| 2304 | if (!compl_opt_refresh_always) |
| 2305 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2306 | VIM_CLEAR_STRING(compl_leader); |
| 2307 | compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col); |
| 2308 | compl_leader.string = vim_strnsave(ml_get_curline() + compl_col, |
| 2309 | compl_leader.length); |
| 2310 | if (compl_leader.string == NULL) |
| 2311 | { |
| 2312 | compl_leader.length = 0; |
| 2313 | return; |
| 2314 | } |
| 2315 | |
| 2316 | ins_compl_new_leader(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 2322 | * BS or a key was typed while still searching for matches. |
| 2323 | */ |
| 2324 | static void |
| 2325 | ins_compl_restart(void) |
| 2326 | { |
| 2327 | ins_compl_free(); |
| 2328 | compl_started = FALSE; |
| 2329 | compl_matches = 0; |
| 2330 | compl_cont_status = 0; |
| 2331 | compl_cont_mode = 0; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 2332 | cpt_compl_src_clear(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Set the first match, the original text. |
| 2337 | */ |
| 2338 | static void |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2339 | ins_compl_set_original_text(char_u *str, size_t len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2340 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2341 | // Replace the original text entry. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2342 | // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly |
| 2343 | // be at the last item for backward completion |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2344 | if (match_at_original_text(compl_first_match)) // safety check |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2345 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2346 | char_u *p = vim_strnsave(str, len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2347 | if (p != NULL) |
| 2348 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2349 | VIM_CLEAR_STRING(compl_first_match->cp_str); |
| 2350 | compl_first_match->cp_str.string = p; |
| 2351 | compl_first_match->cp_str.length = len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | else if (compl_first_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2355 | && match_at_original_text(compl_first_match->cp_prev)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2356 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2357 | char_u *p = vim_strnsave(str, len); |
| 2358 | if (p != NULL) |
| 2359 | { |
| 2360 | VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str); |
| 2361 | compl_first_match->cp_prev->cp_str.string = p; |
| 2362 | compl_first_match->cp_prev->cp_str.length = len; |
| 2363 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Append one character to the match leader. May reduce the number of |
| 2369 | * matches. |
| 2370 | */ |
| 2371 | void |
| 2372 | ins_compl_addfrommatch(void) |
| 2373 | { |
| 2374 | char_u *p; |
| 2375 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
| 2376 | int c; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2377 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2378 | p = compl_shown_match->cp_str.string; |
| 2379 | if ((int)compl_shown_match->cp_str.length <= len) // the match is too short |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2380 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2381 | size_t plen; |
| 2382 | compl_T *cp; |
| 2383 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2384 | // When still at the original match use the first entry that matches |
| 2385 | // the leader. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2386 | if (!match_at_original_text(compl_shown_match)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2387 | return; |
| 2388 | |
| 2389 | p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2390 | plen = 0; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2391 | for (cp = compl_shown_match->cp_next; cp != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2392 | && !is_first_match(cp); cp = cp->cp_next) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2393 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2394 | if (compl_leader.string == NULL |
| 2395 | || ins_compl_equal(cp, compl_leader.string, |
| 2396 | (int)compl_leader.length)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2397 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2398 | p = cp->cp_str.string; |
| 2399 | plen = cp->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2400 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2401 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2402 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2403 | if (p == NULL || (int)plen <= len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2404 | return; |
| 2405 | } |
| 2406 | p += len; |
| 2407 | c = PTR2CHAR(p); |
| 2408 | ins_compl_addleader(c); |
| 2409 | } |
| 2410 | |
| 2411 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2412 | * Set the CTRL-X completion mode based on the key "c" typed after a CTRL-X. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2413 | * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre, |
| 2414 | * compl_cont_mode and compl_cont_status. |
| 2415 | * Returns TRUE when the character is not to be inserted. |
| 2416 | */ |
| 2417 | static int |
| 2418 | set_ctrl_x_mode(int c) |
| 2419 | { |
| 2420 | int retval = FALSE; |
| 2421 | |
| 2422 | switch (c) |
| 2423 | { |
| 2424 | case Ctrl_E: |
| 2425 | case Ctrl_Y: |
| 2426 | // scroll the window one line up or down |
| 2427 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2428 | if (!(State & REPLACE_FLAG)) |
| 2429 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2430 | else |
| 2431 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2432 | edit_submode_pre = NULL; |
| 2433 | showmode(); |
| 2434 | break; |
| 2435 | case Ctrl_L: |
| 2436 | // complete whole line |
| 2437 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2438 | break; |
| 2439 | case Ctrl_F: |
| 2440 | // complete filenames |
| 2441 | ctrl_x_mode = CTRL_X_FILES; |
| 2442 | break; |
| 2443 | case Ctrl_K: |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 2444 | // complete words from a dictionary |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2445 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2446 | break; |
| 2447 | case Ctrl_R: |
| 2448 | // Register insertion without exiting CTRL-X mode |
| 2449 | // Simply allow ^R to happen without affecting ^X mode |
| 2450 | break; |
| 2451 | case Ctrl_T: |
| 2452 | // complete words from a thesaurus |
| 2453 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2454 | break; |
| 2455 | #ifdef FEAT_COMPL_FUNC |
| 2456 | case Ctrl_U: |
| 2457 | // user defined completion |
| 2458 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2459 | break; |
| 2460 | case Ctrl_O: |
| 2461 | // omni completion |
| 2462 | ctrl_x_mode = CTRL_X_OMNI; |
| 2463 | break; |
| 2464 | #endif |
| 2465 | case 's': |
| 2466 | case Ctrl_S: |
| 2467 | // complete spelling suggestions |
| 2468 | ctrl_x_mode = CTRL_X_SPELL; |
| 2469 | #ifdef FEAT_SPELL |
| 2470 | ++emsg_off; // Avoid getting the E756 error twice. |
| 2471 | spell_back_to_badword(); |
| 2472 | --emsg_off; |
| 2473 | #endif |
| 2474 | break; |
| 2475 | case Ctrl_RSB: |
| 2476 | // complete tag names |
| 2477 | ctrl_x_mode = CTRL_X_TAGS; |
| 2478 | break; |
| 2479 | #ifdef FEAT_FIND_ID |
| 2480 | case Ctrl_I: |
| 2481 | case K_S_TAB: |
| 2482 | // complete keywords from included files |
| 2483 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2484 | break; |
| 2485 | case Ctrl_D: |
| 2486 | // complete definitions from included files |
| 2487 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2488 | break; |
| 2489 | #endif |
| 2490 | case Ctrl_V: |
| 2491 | case Ctrl_Q: |
| 2492 | // complete vim commands |
| 2493 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2494 | break; |
| 2495 | case Ctrl_Z: |
| 2496 | // stop completion |
| 2497 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2498 | edit_submode = NULL; |
| 2499 | showmode(); |
| 2500 | retval = TRUE; |
| 2501 | break; |
| 2502 | case Ctrl_P: |
| 2503 | case Ctrl_N: |
| 2504 | // ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2505 | // just started ^X mode, or there were enough ^X's to cancel |
| 2506 | // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2507 | // do normal expansion when interrupting a different mode (say |
| 2508 | // ^X^F^X^P or ^P^X^X^P, see below) |
| 2509 | // nothing changes if interrupting mode 0, (eg, the flag |
| 2510 | // doesn't change when going to ADDING mode -- Acevedo |
| 2511 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2512 | compl_cont_status |= CONT_LOCAL; |
| 2513 | else if (compl_cont_mode != 0) |
| 2514 | compl_cont_status &= ~CONT_LOCAL; |
| 2515 | // FALLTHROUGH |
| 2516 | default: |
| 2517 | // If we have typed at least 2 ^X's... for modes != 0, we set |
| 2518 | // compl_cont_status = 0 (eg, as if we had just started ^X |
| 2519 | // mode). |
| 2520 | // For mode 0, we set "compl_cont_mode" to an impossible |
| 2521 | // value, in both cases ^X^X can be used to restart the same |
| 2522 | // mode (avoiding ADDING mode). |
| 2523 | // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2524 | // 'complete' and local ^P expansions respectively. |
| 2525 | // In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2526 | // mode -- Acevedo |
| 2527 | if (c == Ctrl_X) |
| 2528 | { |
| 2529 | if (compl_cont_mode != 0) |
| 2530 | compl_cont_status = 0; |
| 2531 | else |
| 2532 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
| 2533 | } |
| 2534 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2535 | edit_submode = NULL; |
| 2536 | showmode(); |
| 2537 | break; |
| 2538 | } |
| 2539 | |
| 2540 | return retval; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2544 | * Trigger CompleteDone event and adds relevant information to v:event |
| 2545 | */ |
| 2546 | static void |
| 2547 | trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED) |
| 2548 | { |
| 2549 | #if defined(FEAT_EVAL) |
| 2550 | save_v_event_T save_v_event; |
| 2551 | dict_T *v_event = get_v_event(&save_v_event); |
| 2552 | char_u *mode_str = NULL; |
| 2553 | |
| 2554 | mode = mode & ~CTRL_X_WANT_IDENT; |
| 2555 | if (ctrl_x_mode_names[mode]) |
| 2556 | mode_str = (char_u *)ctrl_x_mode_names[mode]; |
| 2557 | |
| 2558 | (void)dict_add_string(v_event, "complete_word", |
| 2559 | word == NULL ? (char_u *)"" : word); |
| 2560 | (void)dict_add_string(v_event, "complete_type", |
| 2561 | mode_str != NULL ? mode_str : (char_u *)""); |
| 2562 | |
| 2563 | dict_set_items_ro(v_event); |
| 2564 | #endif |
| 2565 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
| 2566 | |
| 2567 | #if defined(FEAT_EVAL) |
| 2568 | restore_v_event(v_event, &save_v_event); |
| 2569 | #endif |
| 2570 | } |
| 2571 | |
| 2572 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2573 | * Stop insert completion mode |
| 2574 | */ |
| 2575 | static int |
| 2576 | ins_compl_stop(int c, int prev_mode, int retval) |
| 2577 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2578 | int want_cindent; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2579 | char_u *word = NULL; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2580 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 2581 | // Remove pre-inserted text when present. |
| 2582 | if (ins_compl_preinsert_effect()) |
| 2583 | ins_compl_delete(); |
| 2584 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2585 | // Get here when we have finished typing a sequence of ^N and |
| 2586 | // ^P or other completion characters in CTRL-X mode. Free up |
| 2587 | // memory that was used, and make sure we can redo the insert. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2588 | if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2589 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2590 | char_u *ptr = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2591 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2592 | // If any of the original typed text has been changed, eg when |
| 2593 | // ignorecase is set, we must add back-spaces to the redo |
| 2594 | // buffer. We add as few as necessary to delete just the part |
| 2595 | // of the original text that has changed. |
| 2596 | // When using the longest match, edited the match or used |
| 2597 | // CTRL-E then don't use the current match. |
| 2598 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2599 | ptr = compl_curr_match->cp_str.string; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2600 | ins_compl_fixRedoBufForLeader(ptr); |
| 2601 | } |
| 2602 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2603 | want_cindent = (get_can_cindent() && cindent_on()); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2604 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2605 | // When completing whole lines: fix indent for 'cindent'. |
| 2606 | // Otherwise, break line if it's too long. |
| 2607 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
| 2608 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2609 | // re-indent the current line |
| 2610 | if (want_cindent) |
| 2611 | { |
| 2612 | do_c_expr_indent(); |
| 2613 | want_cindent = FALSE; // don't do it again |
| 2614 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2615 | } |
| 2616 | else |
| 2617 | { |
| 2618 | int prev_col = curwin->w_cursor.col; |
| 2619 | |
| 2620 | // put the cursor on the last char, for 'tw' formatting |
| 2621 | if (prev_col > 0) |
| 2622 | dec_cursor(); |
| 2623 | // only format when something was inserted |
| 2624 | if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) |
| 2625 | insertchar(NUL, 0, -1); |
| 2626 | if (prev_col > 0 |
| 2627 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 2628 | inc_cursor(); |
| 2629 | } |
| 2630 | |
| 2631 | // If the popup menu is displayed pressing CTRL-Y means accepting |
| 2632 | // the selection without inserting anything. When |
| 2633 | // compl_enter_selects is set the Enter key does the same. |
| 2634 | if ((c == Ctrl_Y || (compl_enter_selects |
| 2635 | && (c == CAR || c == K_KENTER || c == NL))) |
| 2636 | && pum_visible()) |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2637 | { |
| 2638 | word = vim_strsave(compl_shown_match->cp_str.string); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2639 | retval = TRUE; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2640 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2641 | |
| 2642 | // CTRL-E means completion is Ended, go back to the typed text. |
| 2643 | // but only do this, if the Popup is still visible |
| 2644 | if (c == Ctrl_E) |
| 2645 | { |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2646 | char_u *p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2647 | size_t plen = 0; |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2648 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2649 | ins_compl_delete(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2650 | if (compl_leader.string != NULL) |
| 2651 | { |
| 2652 | p = compl_leader.string; |
| 2653 | plen = compl_leader.length; |
| 2654 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2655 | else if (compl_first_match != NULL) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2656 | { |
| 2657 | p = compl_orig_text.string; |
| 2658 | plen = compl_orig_text.length; |
| 2659 | } |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2660 | if (p != NULL) |
| 2661 | { |
| 2662 | int compl_len = get_compl_len(); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2663 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2664 | if ((int)plen > compl_len) |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 2665 | ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2666 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2667 | retval = TRUE; |
| 2668 | } |
| 2669 | |
glepnir | 001c26c | 2025-02-02 09:36:22 +0100 | [diff] [blame] | 2670 | if ((c == Ctrl_W || c == Ctrl_U) && ins_compl_preinsert_effect()) |
| 2671 | ins_compl_delete(); |
| 2672 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2673 | auto_format(FALSE, TRUE); |
| 2674 | |
| 2675 | // Trigger the CompleteDonePre event to give scripts a chance to |
| 2676 | // act upon the completion before clearing the info, and restore |
| 2677 | // ctrl_x_mode, so that complete_info() can be used. |
| 2678 | ctrl_x_mode = prev_mode; |
| 2679 | ins_apply_autocmds(EVENT_COMPLETEDONEPRE); |
| 2680 | |
| 2681 | ins_compl_free(); |
| 2682 | compl_started = FALSE; |
| 2683 | compl_matches = 0; |
| 2684 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 2685 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 2686 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2687 | compl_enter_selects = FALSE; |
| 2688 | if (edit_submode != NULL) |
| 2689 | { |
| 2690 | edit_submode = NULL; |
| 2691 | showmode(); |
| 2692 | } |
| 2693 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2694 | if (c == Ctrl_C && cmdwin_type != 0) |
| 2695 | // Avoid the popup menu remains displayed when leaving the |
| 2696 | // command line window. |
| 2697 | update_screen(0); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2698 | // Indent now if a key was typed that is in 'cinkeys'. |
| 2699 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2700 | do_c_expr_indent(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2701 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2702 | // upon the end of completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2703 | trigger_complete_done_event(prev_mode, word); |
| 2704 | vim_free(word); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2705 | |
| 2706 | return retval; |
| 2707 | } |
| 2708 | |
| 2709 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2710 | * Prepare for Insert mode completion, or stop it. |
| 2711 | * Called just after typing a character in Insert mode. |
| 2712 | * Returns TRUE when the character is not to be inserted; |
| 2713 | */ |
| 2714 | int |
| 2715 | ins_compl_prep(int c) |
| 2716 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2717 | int retval = FALSE; |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 2718 | int prev_mode = ctrl_x_mode; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2719 | |
| 2720 | // Forget any previous 'special' messages if this is actually |
| 2721 | // a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2722 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2723 | edit_submode_extra = NULL; |
| 2724 | |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2725 | // Ignore end of Select mode mapping and mouse scroll/movement. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2726 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2727 | || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 2728 | || c == K_COMMAND || c == K_SCRIPT_COMMAND) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2729 | return retval; |
| 2730 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2731 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | f0bc15c | 2019-08-18 19:23:45 +0200 | [diff] [blame] | 2732 | // Ignore mouse events in a popup window |
| 2733 | if (is_mouse_key(c)) |
| 2734 | { |
| 2735 | // Ignore drag and release events, the position does not need to be in |
| 2736 | // the popup and it may have just closed. |
| 2737 | if (c == K_LEFTRELEASE |
| 2738 | || c == K_LEFTRELEASE_NM |
| 2739 | || c == K_MIDDLERELEASE |
| 2740 | || c == K_RIGHTRELEASE |
| 2741 | || c == K_X1RELEASE |
| 2742 | || c == K_X2RELEASE |
| 2743 | || c == K_LEFTDRAG |
| 2744 | || c == K_MIDDLEDRAG |
| 2745 | || c == K_RIGHTDRAG |
| 2746 | || c == K_X1DRAG |
| 2747 | || c == K_X2DRAG) |
| 2748 | return retval; |
| 2749 | if (popup_visible) |
| 2750 | { |
| 2751 | int row = mouse_row; |
| 2752 | int col = mouse_col; |
| 2753 | win_T *wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2754 | |
| 2755 | if (wp != NULL && WIN_IS_POPUP(wp)) |
| 2756 | return retval; |
| 2757 | } |
| 2758 | } |
| 2759 | #endif |
| 2760 | |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 2761 | if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X) |
| 2762 | { |
| 2763 | if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c) |
| 2764 | || !vim_is_ctrl_x_key(c)) |
| 2765 | { |
| 2766 | // Not starting another completion mode. |
| 2767 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2768 | |
| 2769 | // CTRL-X CTRL-Z should stop completion without inserting anything |
| 2770 | if (c == Ctrl_Z) |
| 2771 | retval = TRUE; |
| 2772 | } |
| 2773 | else |
| 2774 | { |
| 2775 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2776 | |
| 2777 | // Other CTRL-X keys first stop completion, then start another |
| 2778 | // completion mode. |
| 2779 | ins_compl_prep(' '); |
| 2780 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2781 | } |
| 2782 | } |
| 2783 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2784 | // Set "compl_get_longest" when finding the first matches. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2785 | if (ctrl_x_mode_not_defined_yet() |
| 2786 | || (ctrl_x_mode_normal() && !compl_started)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2787 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 2788 | compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2789 | compl_used_match = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2792 | if (ctrl_x_mode_not_defined_yet()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2793 | // We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2794 | // it will be yet. Now we decide. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2795 | retval = set_ctrl_x_mode(c); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2796 | else if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2797 | { |
| 2798 | // We're already in CTRL-X mode, do we stay in it? |
| 2799 | if (!vim_is_ctrl_x_key(c)) |
| 2800 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2801 | ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2802 | edit_submode = NULL; |
| 2803 | } |
| 2804 | showmode(); |
| 2805 | } |
| 2806 | |
| 2807 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
| 2808 | { |
| 2809 | // Show error message from attempted keyword completion (probably |
| 2810 | // 'Pattern not found') until another key is hit, then go back to |
| 2811 | // showing what mode we are in. |
| 2812 | showmode(); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2813 | if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2814 | && c != Ctrl_R && !ins_compl_pum_key(c)) |
| 2815 | || ctrl_x_mode == CTRL_X_FINISHED) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2816 | retval = ins_compl_stop(c, prev_mode, retval); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2817 | } |
| 2818 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 2819 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2820 | // upon the (possibly failed) completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2821 | trigger_complete_done_event(ctrl_x_mode, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2822 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 2823 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 2824 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2825 | // reset continue_* if we left expansion-mode, if we stay they'll be |
| 2826 | // (re)set properly in ins_complete() |
| 2827 | if (!vim_is_ctrl_x_key(c)) |
| 2828 | { |
| 2829 | compl_cont_status = 0; |
| 2830 | compl_cont_mode = 0; |
| 2831 | } |
| 2832 | |
| 2833 | return retval; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 2838 | * text. This inserts backspaces and appends the changed text. |
| 2839 | * "ptr" is the known leader text or NUL. |
| 2840 | */ |
| 2841 | static void |
| 2842 | ins_compl_fixRedoBufForLeader(char_u *ptr_arg) |
| 2843 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2844 | int len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2845 | char_u *p; |
| 2846 | char_u *ptr = ptr_arg; |
| 2847 | |
| 2848 | if (ptr == NULL) |
| 2849 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2850 | if (compl_leader.string != NULL) |
| 2851 | ptr = compl_leader.string; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2852 | else |
| 2853 | return; // nothing to do |
| 2854 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2855 | if (compl_orig_text.string != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2856 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2857 | p = compl_orig_text.string; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2858 | // Find length of common prefix between original text and new completion |
| 2859 | while (p[len] != NUL && p[len] == ptr[len]) |
| 2860 | len++; |
| 2861 | // Adjust length to not break inside a multi-byte character |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2862 | if (len > 0) |
| 2863 | len -= (*mb_head_off)(p, p + len); |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2864 | // Add backspace characters for each remaining character in |
| 2865 | // original text |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2866 | for (p += len; *p != NUL; MB_PTR_ADV(p)) |
| 2867 | AppendCharToRedobuff(K_BS); |
| 2868 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2869 | if (ptr != NULL) |
| 2870 | AppendToRedobuffLit(ptr + len, -1); |
| 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 2875 | * (depending on flag) starting from buf and looking for a non-scanned |
| 2876 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 2877 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 2878 | * |
| 2879 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 2880 | */ |
| 2881 | static buf_T * |
| 2882 | ins_compl_next_buf(buf_T *buf, int flag) |
| 2883 | { |
| 2884 | static win_T *wp = NULL; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2885 | int skip_buffer; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2886 | |
| 2887 | if (flag == 'w') // just windows |
| 2888 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2889 | if (buf == curbuf || !win_valid(wp)) |
| 2890 | // first call for this flag/expansion or window was closed |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2891 | wp = curwin; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2892 | |
| 2893 | while (TRUE) |
| 2894 | { |
| 2895 | // Move to next window (wrap to first window if at the end) |
| 2896 | wp = (wp->w_next != NULL) ? wp->w_next : firstwin; |
| 2897 | // Break if we're back at start or found an unscanned buffer |
| 2898 | if (wp == curwin || !wp->w_buffer->b_scanned) |
| 2899 | break; |
| 2900 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2901 | buf = wp->w_buffer; |
| 2902 | } |
| 2903 | else |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2904 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2905 | // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 2906 | // (unlisted buffers) |
| 2907 | // When completing whole lines skip unloaded buffers. |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2908 | while (TRUE) |
| 2909 | { |
| 2910 | // Move to next buffer (wrap to first buffer if at the end) |
| 2911 | buf = (buf->b_next != NULL) ? buf->b_next : firstbuf; |
| 2912 | // Break if we're back at start buffer |
| 2913 | if (buf == curbuf) |
| 2914 | break; |
| 2915 | |
| 2916 | // Check buffer conditions based on flag |
| 2917 | if (flag == 'U') |
| 2918 | skip_buffer = buf->b_p_bl; |
| 2919 | else |
| 2920 | skip_buffer = !buf->b_p_bl || |
| 2921 | (buf->b_ml.ml_mfp == NULL) != (flag == 'u'); |
| 2922 | |
| 2923 | // Break if we found a buffer that matches our criteria |
| 2924 | if (!skip_buffer && !buf->b_scanned) |
| 2925 | break; |
| 2926 | } |
| 2927 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2928 | return buf; |
| 2929 | } |
| 2930 | |
| 2931 | #ifdef FEAT_COMPL_FUNC |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2932 | |
| 2933 | # ifdef FEAT_EVAL |
| 2934 | static callback_T cfu_cb; // 'completefunc' callback function |
| 2935 | static callback_T ofu_cb; // 'omnifunc' callback function |
| 2936 | static callback_T tsrfu_cb; // 'thesaurusfunc' callback function |
| 2937 | # endif |
| 2938 | |
| 2939 | /* |
| 2940 | * Copy a global callback function to a buffer local callback. |
| 2941 | */ |
| 2942 | static void |
| 2943 | copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb) |
| 2944 | { |
| 2945 | free_callback(bufcb); |
| 2946 | if (globcb->cb_name != NULL && *globcb->cb_name != NUL) |
| 2947 | copy_callback(bufcb, globcb); |
| 2948 | } |
| 2949 | |
| 2950 | /* |
| 2951 | * Parse the 'completefunc' option value and set the callback function. |
| 2952 | * Invoked when the 'completefunc' option is set. The option value can be a |
| 2953 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 2954 | * lambda expression. |
| 2955 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2956 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2957 | did_set_completefunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2958 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2959 | if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL) |
| 2960 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2961 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2962 | set_buflocal_cfu_callback(curbuf); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2963 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2964 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * Copy the global 'completefunc' callback function to the buffer-local |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2969 | * 'completefunc' callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2970 | */ |
| 2971 | void |
| 2972 | set_buflocal_cfu_callback(buf_T *buf UNUSED) |
| 2973 | { |
| 2974 | # ifdef FEAT_EVAL |
| 2975 | copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb); |
| 2976 | # endif |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Parse the 'omnifunc' option value and set the callback function. |
| 2981 | * Invoked when the 'omnifunc' option is set. The option value can be a |
| 2982 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 2983 | * lambda expression. |
| 2984 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2985 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2986 | did_set_omnifunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2987 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2988 | if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL) |
| 2989 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2990 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2991 | set_buflocal_ofu_callback(curbuf); |
| 2992 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* |
| 2996 | * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc' |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2997 | * callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2998 | */ |
| 2999 | void |
| 3000 | set_buflocal_ofu_callback(buf_T *buf UNUSED) |
| 3001 | { |
| 3002 | # ifdef FEAT_EVAL |
| 3003 | copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb); |
| 3004 | # endif |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * Parse the 'thesaurusfunc' option value and set the callback function. |
| 3009 | * Invoked when the 'thesaurusfunc' option is set. The option value can be a |
| 3010 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3011 | * lambda expression. |
| 3012 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3013 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3014 | did_set_thesaurusfunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3015 | { |
| 3016 | int retval; |
| 3017 | |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3018 | if (args->os_flags & OPT_LOCAL) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3019 | // buffer-local option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3020 | retval = option_set_callback_func(curbuf->b_p_tsrfu, |
| 3021 | &curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3022 | else |
| 3023 | { |
| 3024 | // global option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3025 | retval = option_set_callback_func(p_tsrfu, &tsrfu_cb); |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3026 | // when using :set, free the local callback |
| 3027 | if (!(args->os_flags & OPT_GLOBAL)) |
| 3028 | free_callback(&curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3031 | return retval == FAIL ? e_invalid_argument : NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3034 | /* |
| 3035 | * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3036 | * "copyID" so that they are not garbage collected. |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3037 | */ |
| 3038 | int |
| 3039 | set_ref_in_insexpand_funcs(int copyID) |
| 3040 | { |
| 3041 | int abort = FALSE; |
| 3042 | |
| 3043 | abort = set_ref_in_callback(&cfu_cb, copyID); |
| 3044 | abort = abort || set_ref_in_callback(&ofu_cb, copyID); |
| 3045 | abort = abort || set_ref_in_callback(&tsrfu_cb, copyID); |
| 3046 | |
| 3047 | return abort; |
| 3048 | } |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3049 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3050 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3051 | * Get the user-defined completion function name for completion "type" |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3052 | */ |
| 3053 | static char_u * |
| 3054 | get_complete_funcname(int type) |
| 3055 | { |
| 3056 | switch (type) |
| 3057 | { |
| 3058 | case CTRL_X_FUNCTION: |
| 3059 | return curbuf->b_p_cfu; |
| 3060 | case CTRL_X_OMNI: |
| 3061 | return curbuf->b_p_ofu; |
| 3062 | case CTRL_X_THESAURUS: |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3063 | return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu; |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3064 | default: |
| 3065 | return (char_u *)""; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /* |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3070 | * Get the callback to use for insert mode completion. |
| 3071 | */ |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3072 | static callback_T * |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3073 | get_insert_callback(int type) |
| 3074 | { |
| 3075 | if (type == CTRL_X_FUNCTION) |
| 3076 | return &curbuf->b_cfu_cb; |
| 3077 | if (type == CTRL_X_OMNI) |
| 3078 | return &curbuf->b_ofu_cb; |
| 3079 | // CTRL_X_THESAURUS |
| 3080 | return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb; |
| 3081 | } |
| 3082 | |
| 3083 | /* |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 3084 | * Execute user defined complete function 'completefunc', 'omnifunc' or |
| 3085 | * 'thesaurusfunc', and get matches in "matches". |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3086 | * "type" can be one of CTRL_X_OMNI, CTRL_X_FUNCTION, or CTRL_X_THESAURUS. |
| 3087 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 3088 | * option; otherwise, it is NULL. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3089 | */ |
| 3090 | static void |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3091 | expand_by_function(int type, char_u *base, callback_T *cb) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3092 | { |
| 3093 | list_T *matchlist = NULL; |
| 3094 | dict_T *matchdict = NULL; |
| 3095 | typval_T args[3]; |
| 3096 | char_u *funcname; |
| 3097 | pos_T pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3098 | typval_T rettv; |
| 3099 | int save_State = State; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3100 | int retval; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3101 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3102 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3103 | if (!is_cpt_function) |
| 3104 | { |
| 3105 | funcname = get_complete_funcname(type); |
| 3106 | if (*funcname == NUL) |
| 3107 | return; |
| 3108 | cb = get_insert_callback(type); |
| 3109 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3110 | |
| 3111 | // Call 'completefunc' to obtain the list of matches. |
| 3112 | args[0].v_type = VAR_NUMBER; |
| 3113 | args[0].vval.v_number = 0; |
| 3114 | args[1].v_type = VAR_STRING; |
| 3115 | args[1].vval.v_string = base != NULL ? base : (char_u *)""; |
| 3116 | args[2].v_type = VAR_UNKNOWN; |
| 3117 | |
| 3118 | pos = curwin->w_cursor; |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 3119 | // Lock the text to avoid weird things from happening. Also disallow |
| 3120 | // switching to another window, it should not be needed and may end up in |
| 3121 | // Insert mode in another buffer. |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3122 | ++textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3123 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3124 | retval = call_callback(cb, 0, &rettv, 2, args); |
| 3125 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3126 | // Call a function, which returns a list or dict. |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3127 | if (retval == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3128 | { |
| 3129 | switch (rettv.v_type) |
| 3130 | { |
| 3131 | case VAR_LIST: |
| 3132 | matchlist = rettv.vval.v_list; |
| 3133 | break; |
| 3134 | case VAR_DICT: |
| 3135 | matchdict = rettv.vval.v_dict; |
| 3136 | break; |
| 3137 | case VAR_SPECIAL: |
| 3138 | if (rettv.vval.v_number == VVAL_NONE) |
| 3139 | compl_opt_suppress_empty = TRUE; |
| 3140 | // FALLTHROUGH |
| 3141 | default: |
| 3142 | // TODO: Give error message? |
| 3143 | clear_tv(&rettv); |
| 3144 | break; |
| 3145 | } |
| 3146 | } |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3147 | --textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3148 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3149 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 3150 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3151 | validate_cursor(); |
| 3152 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 3153 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 3154 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3155 | goto theend; |
| 3156 | } |
| 3157 | |
| 3158 | if (matchlist != NULL) |
| 3159 | ins_compl_add_list(matchlist); |
| 3160 | else if (matchdict != NULL) |
| 3161 | ins_compl_add_dict(matchdict); |
| 3162 | |
| 3163 | theend: |
| 3164 | // Restore State, it might have been changed. |
| 3165 | State = save_State; |
| 3166 | |
| 3167 | if (matchdict != NULL) |
| 3168 | dict_unref(matchdict); |
| 3169 | if (matchlist != NULL) |
| 3170 | list_unref(matchlist); |
| 3171 | } |
| 3172 | #endif // FEAT_COMPL_FUNC |
| 3173 | |
| 3174 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3175 | |
| 3176 | static inline int |
| 3177 | get_user_highlight_attr(char_u *hlname) |
| 3178 | { |
| 3179 | if (hlname != NULL && *hlname != NUL) |
| 3180 | return syn_name2attr(hlname); |
| 3181 | return -1; |
| 3182 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3183 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3184 | * Add a match to the list of matches from a typeval_T. |
| 3185 | * If the given string is already in the list of completions, then return |
| 3186 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 3187 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3188 | * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck(). |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3189 | */ |
| 3190 | static int |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3191 | ins_compl_add_tv(typval_T *tv, int dir, int fast) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3192 | { |
| 3193 | char_u *word; |
| 3194 | int dup = FALSE; |
| 3195 | int empty = FALSE; |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3196 | int flags = fast ? CP_FAST : 0; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3197 | char_u *(cptext[CPT_COUNT]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3198 | typval_T user_data; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3199 | int status; |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3200 | char_u *user_abbr_hlname; |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3201 | char_u *user_kind_hlname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3202 | int user_hl[2] = { -1, -1 }; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3203 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3204 | user_data.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3205 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 3206 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3207 | word = dict_get_string(tv->vval.v_dict, "word", FALSE); |
| 3208 | cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE); |
| 3209 | cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE); |
| 3210 | cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE); |
| 3211 | cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3212 | |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3213 | user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3214 | user_hl[0] = get_user_highlight_attr(user_abbr_hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3215 | |
| 3216 | user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3217 | user_hl[1] = get_user_highlight_attr(user_kind_hlname); |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3218 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3219 | dict_get_tv(tv->vval.v_dict, "user_data", &user_data); |
| 3220 | if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL |
| 3221 | && dict_get_number(tv->vval.v_dict, "icase")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3222 | flags |= CP_ICASE; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3223 | if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL) |
| 3224 | dup = dict_get_number(tv->vval.v_dict, "dup"); |
| 3225 | if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL) |
| 3226 | empty = dict_get_number(tv->vval.v_dict, "empty"); |
| 3227 | if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL |
| 3228 | && dict_get_number(tv->vval.v_dict, "equal")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3229 | flags |= CP_EQUAL; |
| 3230 | } |
| 3231 | else |
| 3232 | { |
| 3233 | word = tv_get_string_chk(tv); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 3234 | CLEAR_FIELD(cptext); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3235 | } |
| 3236 | if (word == NULL || (!empty && *word == NUL)) |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3237 | { |
| 3238 | clear_tv(&user_data); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3239 | return FAIL; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3240 | } |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3241 | status = ins_compl_add(word, -1, NULL, cptext, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3242 | &user_data, dir, flags, dup, user_hl, 0); |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3243 | if (status != OK) |
| 3244 | clear_tv(&user_data); |
| 3245 | return status; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3249 | * Add completions from a list. |
| 3250 | */ |
| 3251 | static void |
| 3252 | ins_compl_add_list(list_T *list) |
| 3253 | { |
| 3254 | listitem_T *li; |
| 3255 | int dir = compl_direction; |
| 3256 | |
| 3257 | // Go through the List with matches and add each of them. |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3258 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3259 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3260 | { |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3261 | if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3262 | // if dir was BACKWARD then honor it just once |
| 3263 | dir = FORWARD; |
| 3264 | else if (did_emsg) |
| 3265 | break; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * Add completions from a dict. |
| 3271 | */ |
| 3272 | static void |
| 3273 | ins_compl_add_dict(dict_T *dict) |
| 3274 | { |
| 3275 | dictitem_T *di_refresh; |
| 3276 | dictitem_T *di_words; |
| 3277 | |
| 3278 | // Check for optional "refresh" item. |
| 3279 | compl_opt_refresh_always = FALSE; |
| 3280 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 3281 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
| 3282 | { |
| 3283 | char_u *v = di_refresh->di_tv.vval.v_string; |
| 3284 | |
| 3285 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 3286 | compl_opt_refresh_always = TRUE; |
| 3287 | } |
| 3288 | |
| 3289 | // Add completions from a "words" list. |
| 3290 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 3291 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 3292 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
| 3293 | } |
| 3294 | |
| 3295 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3296 | * Start completion for the complete() function. |
| 3297 | * "startcol" is where the matched text starts (1 is first column). |
| 3298 | * "list" is the list of matches. |
| 3299 | */ |
| 3300 | static void |
| 3301 | set_completion(colnr_T startcol, list_T *list) |
| 3302 | { |
| 3303 | int save_w_wrow = curwin->w_wrow; |
| 3304 | int save_w_leftcol = curwin->w_leftcol; |
| 3305 | int flags = CP_ORIGINAL_TEXT; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 3306 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 3307 | int compl_longest = (cur_cot_flags & COT_LONGEST) != 0; |
| 3308 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 3309 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3310 | |
| 3311 | // If already doing completions stop it. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3312 | if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3313 | ins_compl_prep(' '); |
| 3314 | ins_compl_clear(); |
| 3315 | ins_compl_free(); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3316 | compl_get_longest = compl_longest; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3317 | |
| 3318 | compl_direction = FORWARD; |
| 3319 | if (startcol > curwin->w_cursor.col) |
| 3320 | startcol = curwin->w_cursor.col; |
| 3321 | compl_col = startcol; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 3322 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3323 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
| 3324 | // compl_pattern doesn't need to be set |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3325 | compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, |
| 3326 | (size_t)compl_length); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3327 | if (p_ic) |
| 3328 | flags |= CP_ICASE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3329 | if (compl_orig_text.string == NULL) |
| 3330 | { |
| 3331 | compl_orig_text.length = 0; |
| 3332 | return; |
| 3333 | } |
| 3334 | compl_orig_text.length = (size_t)compl_length; |
| 3335 | if (ins_compl_add(compl_orig_text.string, |
| 3336 | (int)compl_orig_text.length, NULL, NULL, NULL, 0, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3337 | flags | CP_FAST, FALSE, NULL, 0) != OK) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3338 | return; |
| 3339 | |
| 3340 | ctrl_x_mode = CTRL_X_EVAL; |
| 3341 | |
| 3342 | ins_compl_add_list(list); |
| 3343 | compl_matches = ins_compl_make_cyclic(); |
| 3344 | compl_started = TRUE; |
| 3345 | compl_used_match = TRUE; |
| 3346 | compl_cont_status = 0; |
| 3347 | |
| 3348 | compl_curr_match = compl_first_match; |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3349 | int no_select = compl_no_select || compl_longest; |
| 3350 | if (compl_no_insert || no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3351 | { |
| 3352 | ins_complete(K_DOWN, FALSE); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3353 | if (no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3354 | // Down/Up has no real effect. |
| 3355 | ins_complete(K_UP, FALSE); |
| 3356 | } |
| 3357 | else |
| 3358 | ins_complete(Ctrl_N, FALSE); |
| 3359 | compl_enter_selects = compl_no_insert; |
| 3360 | |
| 3361 | // Lazily show the popup menu, unless we got interrupted. |
| 3362 | if (!compl_interrupted) |
| 3363 | show_pum(save_w_wrow, save_w_leftcol); |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 3364 | may_trigger_modechanged(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3365 | out_flush(); |
| 3366 | } |
| 3367 | |
| 3368 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3369 | * "complete()" function |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3370 | */ |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3371 | void |
| 3372 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3373 | { |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3374 | int startcol; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3375 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3376 | if (in_vim9script() |
| 3377 | && (check_for_number_arg(argvars, 0) == FAIL |
| 3378 | || check_for_list_arg(argvars, 1) == FAIL)) |
| 3379 | return; |
| 3380 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 3381 | if ((State & MODE_INSERT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3382 | { |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 3383 | emsg(_(e_complete_can_only_be_used_in_insert_mode)); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3384 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3385 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3386 | |
| 3387 | // Check for undo allowed here, because if something was already inserted |
| 3388 | // the line was already saved for undo and this check isn't done. |
| 3389 | if (!undo_allowed()) |
| 3390 | return; |
| 3391 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3392 | if (check_for_nonnull_list_arg(argvars, 1) != FAIL) |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3393 | { |
| 3394 | startcol = (int)tv_get_number_chk(&argvars[0], NULL); |
| 3395 | if (startcol > 0) |
| 3396 | set_completion(startcol - 1, argvars[1].vval.v_list); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3397 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | /* |
| 3401 | * "complete_add()" function |
| 3402 | */ |
| 3403 | void |
| 3404 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 3405 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3406 | if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3407 | return; |
| 3408 | |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3409 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | * "complete_check()" function |
| 3414 | */ |
| 3415 | void |
| 3416 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 3417 | { |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3418 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3419 | RedrawingDisabled = 0; |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3420 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3421 | ins_compl_check_keys(0, TRUE); |
| 3422 | rettv->vval.v_number = ins_compl_interrupted(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3423 | |
| 3424 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3428 | * Return Insert completion mode name string |
| 3429 | */ |
| 3430 | static char_u * |
| 3431 | ins_compl_mode(void) |
| 3432 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3433 | if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3434 | return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; |
| 3435 | |
| 3436 | return (char_u *)""; |
| 3437 | } |
| 3438 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 3439 | /* |
| 3440 | * Assign the sequence number to all the completion matches which don't have |
| 3441 | * one assigned yet. |
| 3442 | */ |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3443 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 3444 | ins_compl_update_sequence_numbers(void) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3445 | { |
| 3446 | int number = 0; |
| 3447 | compl_T *match; |
| 3448 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3449 | if (compl_dir_forward()) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3450 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3451 | // Search backwards for the first valid (!= -1) number. |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3452 | // This should normally succeed already at the first loop |
| 3453 | // cycle, so it's fast! |
| 3454 | for (match = compl_curr_match->cp_prev; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3455 | && !is_first_match(match); match = match->cp_prev) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3456 | if (match->cp_number != -1) |
| 3457 | { |
| 3458 | number = match->cp_number; |
| 3459 | break; |
| 3460 | } |
| 3461 | if (match != NULL) |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3462 | // go up and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3463 | for (match = match->cp_next; |
| 3464 | match != NULL && match->cp_number == -1; |
| 3465 | match = match->cp_next) |
| 3466 | match->cp_number = ++number; |
| 3467 | } |
| 3468 | else // BACKWARD |
| 3469 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3470 | // Search forwards (upwards) for the first valid (!= -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3471 | // number. This should normally succeed already at the |
| 3472 | // first loop cycle, so it's fast! |
| 3473 | for (match = compl_curr_match->cp_next; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3474 | && !is_first_match(match); match = match->cp_next) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3475 | { |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3476 | if (match->cp_number != -1) |
| 3477 | { |
| 3478 | number = match->cp_number; |
| 3479 | break; |
| 3480 | } |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3481 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3482 | if (match != NULL) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3483 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3484 | // go down and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3485 | for (match = match->cp_prev; match |
| 3486 | && match->cp_number == -1; |
| 3487 | match = match->cp_prev) |
| 3488 | match->cp_number = ++number; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3489 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3490 | } |
| 3491 | } |
| 3492 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3493 | /* |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3494 | * Fill the dict of complete_info |
| 3495 | */ |
| 3496 | static void |
| 3497 | fill_complete_info_dict(dict_T *di, compl_T *match, int add_match) |
| 3498 | { |
| 3499 | dict_add_string(di, "word", match->cp_str.string); |
| 3500 | dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]); |
| 3501 | dict_add_string(di, "menu", match->cp_text[CPT_MENU]); |
| 3502 | dict_add_string(di, "kind", match->cp_text[CPT_KIND]); |
| 3503 | dict_add_string(di, "info", match->cp_text[CPT_INFO]); |
| 3504 | if (add_match) |
| 3505 | dict_add_bool(di, "match", match->cp_in_match_array); |
| 3506 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 3507 | // Add an empty string for backwards compatibility |
| 3508 | dict_add_string(di, "user_data", (char_u *)""); |
| 3509 | else |
| 3510 | dict_add_tv(di, "user_data", &match->cp_user_data); |
| 3511 | } |
| 3512 | |
| 3513 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3514 | * Get complete information |
| 3515 | */ |
| 3516 | static void |
| 3517 | get_complete_info(list_T *what_list, dict_T *retdict) |
| 3518 | { |
| 3519 | int ret = OK; |
| 3520 | listitem_T *item; |
| 3521 | #define CI_WHAT_MODE 0x01 |
| 3522 | #define CI_WHAT_PUM_VISIBLE 0x02 |
| 3523 | #define CI_WHAT_ITEMS 0x04 |
| 3524 | #define CI_WHAT_SELECTED 0x08 |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3525 | #define CI_WHAT_COMPLETED 0x10 |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3526 | #define CI_WHAT_MATCHES 0x20 |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3527 | #define CI_WHAT_ALL 0xff |
| 3528 | int what_flag; |
| 3529 | |
| 3530 | if (what_list == NULL) |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3531 | what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3532 | else |
| 3533 | { |
| 3534 | what_flag = 0; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3535 | CHECK_LIST_MATERIALIZE(what_list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3536 | FOR_ALL_LIST_ITEMS(what_list, item) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3537 | { |
| 3538 | char_u *what = tv_get_string(&item->li_tv); |
| 3539 | |
| 3540 | if (STRCMP(what, "mode") == 0) |
| 3541 | what_flag |= CI_WHAT_MODE; |
| 3542 | else if (STRCMP(what, "pum_visible") == 0) |
| 3543 | what_flag |= CI_WHAT_PUM_VISIBLE; |
| 3544 | else if (STRCMP(what, "items") == 0) |
| 3545 | what_flag |= CI_WHAT_ITEMS; |
| 3546 | else if (STRCMP(what, "selected") == 0) |
| 3547 | what_flag |= CI_WHAT_SELECTED; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3548 | else if (STRCMP(what, "completed") == 0) |
| 3549 | what_flag |= CI_WHAT_COMPLETED; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3550 | else if (STRCMP(what, "matches") == 0) |
| 3551 | what_flag |= CI_WHAT_MATCHES; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | if (ret == OK && (what_flag & CI_WHAT_MODE)) |
| 3556 | ret = dict_add_string(retdict, "mode", ins_compl_mode()); |
| 3557 | |
| 3558 | if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) |
| 3559 | ret = dict_add_number(retdict, "pum_visible", pum_visible()); |
| 3560 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3561 | if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED |
| 3562 | | CI_WHAT_MATCHES | CI_WHAT_COMPLETED))) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3563 | { |
Christian Brabandt | 9eb236f | 2024-03-21 20:12:59 +0100 | [diff] [blame] | 3564 | list_T *li = NULL; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3565 | dict_T *di; |
| 3566 | compl_T *match; |
| 3567 | int selected_idx = -1; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3568 | int has_items = what_flag & CI_WHAT_ITEMS; |
| 3569 | int has_matches = what_flag & CI_WHAT_MATCHES; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3570 | int has_completed = what_flag & CI_WHAT_COMPLETED; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3571 | |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3572 | if (has_items || has_matches) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3573 | { |
| 3574 | li = list_alloc(); |
| 3575 | if (li == NULL) |
| 3576 | return; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3577 | ret = dict_add_list(retdict, (has_matches && !has_items) |
| 3578 | ? "matches" : "items", li); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3579 | } |
| 3580 | if (ret == OK && what_flag & CI_WHAT_SELECTED) |
| 3581 | if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) |
| 3582 | ins_compl_update_sequence_numbers(); |
| 3583 | if (ret == OK && compl_first_match != NULL) |
| 3584 | { |
| 3585 | int list_idx = 0; |
| 3586 | match = compl_first_match; |
| 3587 | do |
| 3588 | { |
| 3589 | if (!match_at_original_text(match)) |
| 3590 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3591 | if (has_items |
| 3592 | || (has_matches && match->cp_in_match_array)) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3593 | { |
| 3594 | di = dict_alloc(); |
| 3595 | if (di == NULL) |
| 3596 | return; |
| 3597 | ret = list_append_dict(li, di); |
| 3598 | if (ret != OK) |
| 3599 | return; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3600 | fill_complete_info_dict(di, match, has_matches && has_items); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3601 | } |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3602 | if (compl_curr_match != NULL |
| 3603 | && compl_curr_match->cp_number == match->cp_number) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3604 | selected_idx = list_idx; |
| 3605 | list_idx += 1; |
| 3606 | } |
| 3607 | match = match->cp_next; |
| 3608 | } |
| 3609 | while (match != NULL && !is_first_match(match)); |
| 3610 | } |
| 3611 | if (ret == OK && (what_flag & CI_WHAT_SELECTED)) |
| 3612 | ret = dict_add_number(retdict, "selected", selected_idx); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3613 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3614 | if (ret == OK && selected_idx != -1 && has_completed) |
| 3615 | { |
| 3616 | di = dict_alloc(); |
| 3617 | if (di == NULL) |
| 3618 | return; |
| 3619 | fill_complete_info_dict(di, compl_curr_match, FALSE); |
| 3620 | ret = dict_add_dict(retdict, "completed", di); |
| 3621 | } |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 3622 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3626 | * "complete_info()" function |
| 3627 | */ |
| 3628 | void |
| 3629 | f_complete_info(typval_T *argvars, typval_T *rettv) |
| 3630 | { |
| 3631 | list_T *what_list = NULL; |
| 3632 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 3633 | if (rettv_dict_alloc(rettv) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3634 | return; |
| 3635 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3636 | if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL) |
| 3637 | return; |
| 3638 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3639 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 3640 | { |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3641 | if (check_for_list_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3642 | return; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3643 | what_list = argvars[0].vval.v_list; |
| 3644 | } |
| 3645 | get_complete_info(what_list, rettv->vval.v_dict); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3646 | } |
| 3647 | #endif |
| 3648 | |
| 3649 | /* |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3650 | * Returns TRUE when using a user-defined function for thesaurus completion. |
| 3651 | */ |
| 3652 | static int |
| 3653 | thesaurus_func_complete(int type UNUSED) |
| 3654 | { |
| 3655 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3656 | return type == CTRL_X_THESAURUS |
| 3657 | && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL); |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3658 | #else |
| 3659 | return FALSE; |
| 3660 | #endif |
| 3661 | } |
| 3662 | |
| 3663 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3664 | * Return value of process_next_cpt_value() |
| 3665 | */ |
| 3666 | enum |
| 3667 | { |
| 3668 | INS_COMPL_CPT_OK = 1, |
| 3669 | INS_COMPL_CPT_CONT, |
| 3670 | INS_COMPL_CPT_END |
| 3671 | }; |
| 3672 | |
| 3673 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3674 | * state information used for getting the next set of insert completion |
| 3675 | * matches. |
| 3676 | */ |
| 3677 | typedef struct |
| 3678 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3679 | char_u *e_cpt_copy; // copy of 'complete' |
| 3680 | char_u *e_cpt; // current entry in "e_cpt_copy" |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3681 | buf_T *ins_buf; // buffer being scanned |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3682 | pos_T *cur_match_pos; // current match position |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3683 | pos_T prev_match_pos; // previous match position |
| 3684 | int set_match_pos; // save first_match_pos/last_match_pos |
| 3685 | pos_T first_match_pos; // first match position |
| 3686 | pos_T last_match_pos; // last match position |
| 3687 | int found_all; // found all matches of a certain type. |
| 3688 | char_u *dict; // dictionary file to search |
| 3689 | int dict_f; // "dict" is an exact file name or not |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3690 | callback_T *func_cb; // callback of function in 'cpt' option |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3691 | } ins_compl_next_state_T; |
| 3692 | |
| 3693 | /* |
| 3694 | * Process the next 'complete' option value in st->e_cpt. |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3695 | * |
| 3696 | * If successful, the arguments are set as below: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3697 | * st->cpt - pointer to the next option value in "st->cpt" |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3698 | * compl_type_arg - type of insert mode completion to use |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3699 | * st->found_all - all matches of this type are found |
| 3700 | * st->ins_buf - search for completions in this buffer |
| 3701 | * st->first_match_pos - position of the first completion match |
| 3702 | * st->last_match_pos - position of the last completion match |
| 3703 | * st->set_match_pos - TRUE if the first match position should be saved to |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 3704 | * avoid loops after the search wraps around. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3705 | * st->dict - name of the dictionary or thesaurus file to search |
| 3706 | * st->dict_f - flag specifying whether "dict" is an exact file name or not |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3707 | * |
| 3708 | * Returns INS_COMPL_CPT_OK if the next value is processed successfully. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3709 | * Returns INS_COMPL_CPT_CONT to skip the current completion source matching |
| 3710 | * the "st->e_cpt" option value and process the next matching source. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3711 | * Returns INS_COMPL_CPT_END if all the values in "st->e_cpt" are processed. |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3712 | */ |
| 3713 | static int |
| 3714 | process_next_cpt_value( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3715 | ins_compl_next_state_T *st, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3716 | int *compl_type_arg, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3717 | pos_T *start_match_pos, |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 3718 | int fuzzy_collect) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3719 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3720 | int compl_type = -1; |
| 3721 | int status = INS_COMPL_CPT_OK; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3722 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3723 | st->found_all = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3724 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3725 | while (*st->e_cpt == ',' || *st->e_cpt == ' ') |
| 3726 | st->e_cpt++; |
| 3727 | |
| 3728 | if (*st->e_cpt == '.' && !curbuf->b_scanned) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3729 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3730 | st->ins_buf = curbuf; |
| 3731 | st->first_match_pos = *start_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3732 | // Move the cursor back one character so that ^N can match the |
| 3733 | // word immediately after the cursor. |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 3734 | if (ctrl_x_mode_normal() && (!fuzzy_collect && dec(&st->first_match_pos) < 0)) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3735 | { |
| 3736 | // Move the cursor to after the last character in the |
| 3737 | // buffer, so that word at start of buffer is found |
| 3738 | // correctly. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3739 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 3740 | st->first_match_pos.col = ml_get_len(st->first_match_pos.lnum); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3741 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3742 | st->last_match_pos = st->first_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3743 | compl_type = 0; |
| 3744 | |
| 3745 | // Remember the first match so that the loop stops when we |
| 3746 | // wrap and come back there a second time. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3747 | st->set_match_pos = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3748 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3749 | else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3750 | && (st->ins_buf = ins_compl_next_buf( |
| 3751 | st->ins_buf, *st->e_cpt)) != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3752 | { |
| 3753 | // Scan a buffer, but not the current one. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3754 | if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3755 | { |
| 3756 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3757 | st->first_match_pos.col = st->last_match_pos.col = 0; |
| 3758 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1; |
| 3759 | st->last_match_pos.lnum = 0; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3760 | compl_type = 0; |
| 3761 | } |
| 3762 | else // unloaded buffer, scan like dictionary |
| 3763 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3764 | st->found_all = TRUE; |
| 3765 | if (st->ins_buf->b_fname == NULL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3766 | { |
| 3767 | status = INS_COMPL_CPT_CONT; |
| 3768 | goto done; |
| 3769 | } |
| 3770 | compl_type = CTRL_X_DICTIONARY; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3771 | st->dict = st->ins_buf->b_fname; |
| 3772 | st->dict_f = DICT_EXACT; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3773 | } |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 3774 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 3775 | { |
| 3776 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 3777 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
| 3778 | st->ins_buf->b_fname == NULL |
| 3779 | ? buf_spname(st->ins_buf) |
| 3780 | : st->ins_buf->b_sfname == NULL |
| 3781 | ? st->ins_buf->b_fname |
| 3782 | : st->ins_buf->b_sfname); |
| 3783 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 3784 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3785 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3786 | else if (*st->e_cpt == NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3787 | status = INS_COMPL_CPT_END; |
| 3788 | else |
| 3789 | { |
| 3790 | if (ctrl_x_mode_line_or_eval()) |
| 3791 | compl_type = -1; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3792 | else if (*st->e_cpt == 'k' || *st->e_cpt == 's') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3793 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3794 | if (*st->e_cpt == 'k') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3795 | compl_type = CTRL_X_DICTIONARY; |
| 3796 | else |
| 3797 | compl_type = CTRL_X_THESAURUS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3798 | if (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3799 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3800 | st->dict = st->e_cpt; |
| 3801 | st->dict_f = DICT_FIRST; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3802 | } |
| 3803 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3804 | #ifdef FEAT_COMPL_FUNC |
| 3805 | else if (*st->e_cpt == 'f' || *st->e_cpt == 'o') |
| 3806 | { |
| 3807 | compl_type = CTRL_X_FUNCTION; |
| 3808 | if (*st->e_cpt == 'o') |
| 3809 | st->func_cb = &curbuf->b_ofu_cb; |
| 3810 | else |
| 3811 | st->func_cb = (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
| 3812 | ? get_cpt_func_callback(st->e_cpt) : &curbuf->b_cfu_cb; |
| 3813 | if (!st->func_cb) |
| 3814 | compl_type = -1; |
| 3815 | } |
| 3816 | #endif |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3817 | #ifdef FEAT_FIND_ID |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3818 | else if (*st->e_cpt == 'i') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3819 | compl_type = CTRL_X_PATH_PATTERNS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3820 | else if (*st->e_cpt == 'd') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3821 | compl_type = CTRL_X_PATH_DEFINES; |
| 3822 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3823 | else if (*st->e_cpt == ']' || *st->e_cpt == 't') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3824 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3825 | compl_type = CTRL_X_TAGS; |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 3826 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 3827 | { |
| 3828 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 3829 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
| 3830 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 3831 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3832 | } |
| 3833 | else |
| 3834 | compl_type = -1; |
| 3835 | |
| 3836 | // in any case e_cpt is advanced to the next entry |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3837 | (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ","); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3838 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3839 | st->found_all = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3840 | if (compl_type == -1) |
| 3841 | status = INS_COMPL_CPT_CONT; |
| 3842 | } |
| 3843 | |
| 3844 | done: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3845 | *compl_type_arg = compl_type; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3846 | return status; |
| 3847 | } |
| 3848 | |
| 3849 | #ifdef FEAT_FIND_ID |
| 3850 | /* |
| 3851 | * Get the next set of identifiers or defines matching "compl_pattern" in |
| 3852 | * included files. |
| 3853 | */ |
| 3854 | static void |
| 3855 | get_next_include_file_completion(int compl_type) |
| 3856 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3857 | find_pattern_in_path(compl_pattern.string, compl_direction, |
| 3858 | (int)compl_pattern.length, FALSE, FALSE, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3859 | (compl_type == CTRL_X_PATH_DEFINES |
| 3860 | && !(compl_cont_status & CONT_SOL)) |
| 3861 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 3862 | (linenr_T)1, (linenr_T)MAXLNUM, FALSE); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3863 | } |
| 3864 | #endif |
| 3865 | |
| 3866 | /* |
| 3867 | * Get the next set of words matching "compl_pattern" in dictionary or |
| 3868 | * thesaurus files. |
| 3869 | */ |
| 3870 | static void |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3871 | get_next_dict_tsr_completion(int compl_type, char_u *dict, int dict_f) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3872 | { |
| 3873 | #ifdef FEAT_COMPL_FUNC |
| 3874 | if (thesaurus_func_complete(compl_type)) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 3875 | expand_by_function(compl_type, compl_pattern.string, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3876 | else |
| 3877 | #endif |
| 3878 | ins_compl_dictionaries( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3879 | dict != NULL ? dict |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3880 | : (compl_type == CTRL_X_THESAURUS |
| 3881 | ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr) |
| 3882 | : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)), |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3883 | compl_pattern.string, |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3884 | dict != NULL ? dict_f : 0, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3885 | compl_type == CTRL_X_THESAURUS); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | /* |
| 3889 | * Get the next set of tag names matching "compl_pattern". |
| 3890 | */ |
| 3891 | static void |
| 3892 | get_next_tag_completion(void) |
| 3893 | { |
| 3894 | int save_p_ic; |
| 3895 | char_u **matches; |
| 3896 | int num_matches; |
| 3897 | |
| 3898 | // set p_ic according to p_ic, p_scs and pat for find_tags(). |
| 3899 | save_p_ic = p_ic; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3900 | p_ic = ignorecase(compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3901 | |
| 3902 | // Find up to TAG_MANY matches. Avoids that an enormous number |
| 3903 | // of matches is found when compl_pattern is empty |
| 3904 | g_tag_at_cursor = TRUE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3905 | if (find_tags(compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3906 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3907 | | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0), |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3908 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 3909 | ins_compl_add_matches(num_matches, matches, p_ic); |
| 3910 | g_tag_at_cursor = FALSE; |
| 3911 | p_ic = save_p_ic; |
| 3912 | } |
| 3913 | |
| 3914 | /* |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3915 | * Compare function for qsort |
| 3916 | */ |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3917 | static int |
| 3918 | compare_scores(const void *a, const void *b) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3919 | { |
| 3920 | int idx_a = *(const int *)a; |
| 3921 | int idx_b = *(const int *)b; |
| 3922 | int score_a = compl_fuzzy_scores[idx_a]; |
| 3923 | int score_b = compl_fuzzy_scores[idx_b]; |
zeertzjq | 58d7052 | 2024-08-31 17:05:39 +0200 | [diff] [blame] | 3924 | return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1)) |
| 3925 | : (score_a > score_b ? -1 : 1); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3929 | * insert prefix with redraw |
| 3930 | */ |
| 3931 | static void |
| 3932 | ins_compl_longest_insert(char_u *prefix) |
| 3933 | { |
| 3934 | ins_compl_delete(); |
| 3935 | ins_compl_insert_bytes(prefix + get_compl_len(), -1); |
| 3936 | ins_redraw(FALSE); |
| 3937 | } |
| 3938 | |
| 3939 | /* |
| 3940 | * Calculate the longest common prefix among the best fuzzy matches |
| 3941 | * stored in compl_best_matches, and insert it as the longest. |
| 3942 | */ |
| 3943 | static void |
| 3944 | fuzzy_longest_match(void) |
| 3945 | { |
| 3946 | char_u *prefix = NULL; |
| 3947 | int prefix_len = 0; |
| 3948 | int i = 0; |
| 3949 | int j = 0; |
| 3950 | char_u *match_str = NULL; |
| 3951 | char_u *prefix_ptr = NULL; |
| 3952 | char_u *match_ptr = NULL; |
| 3953 | char_u *leader = NULL; |
| 3954 | size_t leader_len = 0; |
| 3955 | compl_T *compl = NULL; |
| 3956 | int more_candidates = FALSE; |
| 3957 | compl_T *nn_compl = NULL; |
| 3958 | |
| 3959 | if (compl_num_bests == 0) |
| 3960 | return; |
| 3961 | |
| 3962 | nn_compl = compl_first_match->cp_next->cp_next; |
| 3963 | if (nn_compl && nn_compl != compl_first_match) |
| 3964 | more_candidates = TRUE; |
| 3965 | |
| 3966 | compl = ctrl_x_mode_whole_line() ? compl_first_match |
| 3967 | : compl_first_match->cp_next; |
| 3968 | if (compl_num_bests == 1) |
| 3969 | { |
| 3970 | // no more candidates insert the match str |
| 3971 | if (!more_candidates) |
| 3972 | { |
| 3973 | ins_compl_longest_insert(compl->cp_str.string); |
| 3974 | compl_num_bests = 0; |
| 3975 | } |
| 3976 | compl_num_bests = 0; |
| 3977 | return; |
| 3978 | } |
| 3979 | |
| 3980 | compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *)); |
| 3981 | if (compl_best_matches == NULL) |
| 3982 | return; |
| 3983 | while (compl != NULL && i < compl_num_bests) |
| 3984 | { |
| 3985 | compl_best_matches[i] = compl; |
| 3986 | compl = compl->cp_next; |
| 3987 | i++; |
| 3988 | } |
| 3989 | |
| 3990 | prefix = compl_best_matches[0]->cp_str.string; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 3991 | prefix_len = (int)compl_best_matches[0]->cp_str.length; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3992 | |
| 3993 | for (i = 1; i < compl_num_bests; i++) |
| 3994 | { |
| 3995 | match_str = compl_best_matches[i]->cp_str.string; |
| 3996 | prefix_ptr = prefix; |
| 3997 | match_ptr = match_str; |
| 3998 | j = 0; |
| 3999 | |
| 4000 | while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL) |
| 4001 | { |
| 4002 | if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0) |
| 4003 | break; |
| 4004 | |
| 4005 | MB_PTR_ADV(prefix_ptr); |
| 4006 | MB_PTR_ADV(match_ptr); |
| 4007 | j++; |
| 4008 | } |
| 4009 | |
| 4010 | if (j > 0) |
| 4011 | prefix_len = j; |
| 4012 | } |
| 4013 | |
| 4014 | leader = ins_compl_leader(); |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4015 | leader_len = ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4016 | |
| 4017 | // skip non-consecutive prefixes |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4018 | if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4019 | goto end; |
| 4020 | |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4021 | prefix = vim_strnsave(prefix, prefix_len); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4022 | if (prefix != NULL) |
| 4023 | { |
| 4024 | ins_compl_longest_insert(prefix); |
| 4025 | compl_cfc_longest_ins = TRUE; |
| 4026 | vim_free(prefix); |
| 4027 | } |
| 4028 | |
| 4029 | end: |
| 4030 | vim_free(compl_best_matches); |
| 4031 | compl_best_matches = NULL; |
| 4032 | compl_num_bests = 0; |
| 4033 | } |
| 4034 | |
| 4035 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4036 | * Get the next set of filename matching "compl_pattern". |
| 4037 | */ |
| 4038 | static void |
| 4039 | get_next_filename_completion(void) |
| 4040 | { |
| 4041 | char_u **matches; |
| 4042 | int num_matches; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4043 | char_u *ptr; |
| 4044 | garray_T fuzzy_indices; |
| 4045 | int i; |
| 4046 | int score; |
| 4047 | char_u *leader = ins_compl_leader(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4048 | size_t leader_len = ins_compl_leader_len();; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4049 | int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4050 | int *fuzzy_indices_data; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4051 | char_u *last_sep = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4052 | int need_collect_bests = in_fuzzy_collect && compl_get_longest; |
| 4053 | int max_score = 0; |
| 4054 | int current_score = 0; |
| 4055 | int dir = compl_direction; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4056 | |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4057 | #ifdef BACKSLASH_IN_FILENAME |
| 4058 | char pathsep = (curbuf->b_p_csl[0] == 's') ? |
| 4059 | '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP; |
| 4060 | #else |
| 4061 | char pathsep = PATHSEP; |
| 4062 | #endif |
| 4063 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4064 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4065 | { |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4066 | #ifdef BACKSLASH_IN_FILENAME |
| 4067 | if (curbuf->b_p_csl[0] == 's') |
| 4068 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4069 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4070 | { |
| 4071 | if (leader[i] == '\\') |
| 4072 | leader[i] = '/'; |
| 4073 | } |
| 4074 | } |
| 4075 | else if (curbuf->b_p_csl[0] == 'b') |
| 4076 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4077 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4078 | { |
| 4079 | if (leader[i] == '/') |
| 4080 | leader[i] = '\\'; |
| 4081 | } |
| 4082 | } |
| 4083 | #endif |
| 4084 | last_sep = vim_strrchr(leader, pathsep); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4085 | if (last_sep == NULL) |
| 4086 | { |
| 4087 | // No path separator or separator is the last character, |
| 4088 | // fuzzy match the whole leader |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4089 | VIM_CLEAR_STRING(compl_pattern); |
| 4090 | compl_pattern.string = vim_strnsave((char_u *)"*", 1); |
| 4091 | if (compl_pattern.string == NULL) |
| 4092 | return; |
| 4093 | compl_pattern.length = 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4094 | } |
| 4095 | else if (*(last_sep + 1) == '\0') |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4096 | in_fuzzy_collect = FALSE; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4097 | else |
| 4098 | { |
| 4099 | // Split leader into path and file parts |
| 4100 | int path_len = last_sep - leader + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4101 | char_u *path_with_wildcard; |
| 4102 | |
| 4103 | path_with_wildcard = alloc(path_len + 2); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4104 | if (path_with_wildcard != NULL) |
| 4105 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4106 | vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader); |
| 4107 | VIM_CLEAR_STRING(compl_pattern); |
| 4108 | compl_pattern.string = path_with_wildcard; |
| 4109 | compl_pattern.length = path_len + 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4110 | |
| 4111 | // Move leader to the file part |
| 4112 | leader = last_sep + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4113 | leader_len -= path_len; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4114 | } |
| 4115 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4116 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4117 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4118 | if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4119 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK) |
| 4120 | return; |
| 4121 | |
| 4122 | // May change home directory back to "~". |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4123 | tilde_replace(compl_pattern.string, num_matches, matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4124 | #ifdef BACKSLASH_IN_FILENAME |
| 4125 | if (curbuf->b_p_csl[0] != NUL) |
| 4126 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4127 | for (i = 0; i < num_matches; ++i) |
| 4128 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4129 | ptr = matches[i]; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4130 | while (*ptr != NUL) |
| 4131 | { |
| 4132 | if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') |
| 4133 | *ptr = '/'; |
| 4134 | else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') |
| 4135 | *ptr = '\\'; |
| 4136 | ptr += (*mb_ptr2len)(ptr); |
| 4137 | } |
| 4138 | } |
| 4139 | } |
| 4140 | #endif |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4141 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4142 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4143 | { |
| 4144 | ga_init2(&fuzzy_indices, sizeof(int), 10); |
| 4145 | compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches); |
| 4146 | |
| 4147 | for (i = 0; i < num_matches; i++) |
| 4148 | { |
| 4149 | ptr = matches[i]; |
| 4150 | score = fuzzy_match_str(ptr, leader); |
| 4151 | if (score > 0) |
| 4152 | { |
| 4153 | if (ga_grow(&fuzzy_indices, 1) == OK) |
| 4154 | { |
| 4155 | ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i; |
| 4156 | compl_fuzzy_scores[i] = score; |
| 4157 | fuzzy_indices.ga_len++; |
| 4158 | } |
| 4159 | } |
| 4160 | } |
| 4161 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4162 | // prevent qsort from deref NULL pointer |
| 4163 | if (fuzzy_indices.ga_len > 0) |
| 4164 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4165 | char_u *match = NULL; |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4166 | fuzzy_indices_data = (int *)fuzzy_indices.ga_data; |
| 4167 | qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4168 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4169 | for (i = 0; i < fuzzy_indices.ga_len; ++i) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4170 | { |
| 4171 | match = matches[fuzzy_indices_data[i]]; |
| 4172 | current_score = compl_fuzzy_scores[fuzzy_indices_data[i]]; |
| 4173 | if (ins_compl_add(match, -1, NULL, NULL, NULL, dir, |
| 4174 | CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0), |
| 4175 | FALSE, NULL, current_score) == OK) |
| 4176 | dir = FORWARD; |
| 4177 | |
| 4178 | if (need_collect_bests) |
| 4179 | { |
| 4180 | if (i == 0 || current_score == max_score) |
| 4181 | { |
| 4182 | compl_num_bests++; |
| 4183 | max_score = current_score; |
| 4184 | } |
| 4185 | } |
| 4186 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4187 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4188 | FreeWild(num_matches, matches); |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4189 | } |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4190 | else if (leader_len > 0) |
| 4191 | { |
| 4192 | FreeWild(num_matches, matches); |
| 4193 | num_matches = 0; |
| 4194 | } |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4195 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4196 | vim_free(compl_fuzzy_scores); |
| 4197 | ga_clear(&fuzzy_indices); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4198 | |
| 4199 | if (compl_num_bests > 0 && compl_get_longest) |
| 4200 | fuzzy_longest_match(); |
| 4201 | return; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4204 | if (num_matches > 0) |
| 4205 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /* |
| 4209 | * Get the next set of command-line completions matching "compl_pattern". |
| 4210 | */ |
| 4211 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 4212 | get_next_cmdline_completion(void) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4213 | { |
| 4214 | char_u **matches; |
| 4215 | int num_matches; |
| 4216 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4217 | if (expand_cmdline(&compl_xp, compl_pattern.string, |
| 4218 | (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4219 | ins_compl_add_matches(num_matches, matches, FALSE); |
| 4220 | } |
| 4221 | |
| 4222 | /* |
| 4223 | * Get the next set of spell suggestions matching "compl_pattern". |
| 4224 | */ |
| 4225 | static void |
| 4226 | get_next_spell_completion(linenr_T lnum UNUSED) |
| 4227 | { |
| 4228 | #ifdef FEAT_SPELL |
| 4229 | char_u **matches; |
| 4230 | int num_matches; |
| 4231 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4232 | num_matches = expand_spelling(lnum, compl_pattern.string, &matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4233 | if (num_matches > 0) |
| 4234 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 8e7cc6b | 2021-12-30 10:32:25 +0000 | [diff] [blame] | 4235 | else |
| 4236 | vim_free(matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4237 | #endif |
| 4238 | } |
| 4239 | |
| 4240 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4241 | * Return the next word or line from buffer "ins_buf" at position |
| 4242 | * "cur_match_pos" for completion. The length of the match is set in "len". |
| 4243 | */ |
| 4244 | static char_u * |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 4245 | ins_compl_get_next_word_or_line( |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4246 | buf_T *ins_buf, // buffer being scanned |
| 4247 | pos_T *cur_match_pos, // current match position |
| 4248 | int *match_len, |
| 4249 | int *cont_s_ipos) // next ^X<> will set initial_pos |
| 4250 | { |
| 4251 | char_u *ptr; |
| 4252 | int len; |
| 4253 | |
| 4254 | *match_len = 0; |
| 4255 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) + |
| 4256 | cur_match_pos->col; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4257 | len = (int)ml_get_buf_len(ins_buf, cur_match_pos->lnum) - cur_match_pos->col; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4258 | if (ctrl_x_mode_line_or_eval()) |
| 4259 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4260 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4261 | { |
| 4262 | if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 4263 | return NULL; |
| 4264 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4265 | len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4266 | if (!p_paste) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4267 | { |
| 4268 | char_u *tmp_ptr = ptr; |
| 4269 | |
| 4270 | ptr = skipwhite(tmp_ptr); |
| 4271 | len -= (int)(ptr - tmp_ptr); |
| 4272 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4273 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4274 | } |
| 4275 | else |
| 4276 | { |
| 4277 | char_u *tmp_ptr = ptr; |
| 4278 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4279 | if (compl_status_adding() && compl_length <= len) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4280 | { |
| 4281 | tmp_ptr += compl_length; |
| 4282 | // Skip if already inside a word. |
| 4283 | if (vim_iswordp(tmp_ptr)) |
| 4284 | return NULL; |
| 4285 | // Find start of next word. |
| 4286 | tmp_ptr = find_word_start(tmp_ptr); |
| 4287 | } |
| 4288 | // Find end of this word. |
| 4289 | tmp_ptr = find_word_end(tmp_ptr); |
| 4290 | len = (int)(tmp_ptr - ptr); |
| 4291 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4292 | if (compl_status_adding() && len == compl_length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4293 | { |
| 4294 | if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4295 | { |
| 4296 | // Try next line, if any. the new word will be |
| 4297 | // "join" as if the normal command "J" was used. |
| 4298 | // IOSIZE is always greater than |
| 4299 | // compl_length, so the next STRNCPY always |
| 4300 | // works -- Acevedo |
| 4301 | STRNCPY(IObuff, ptr, len); |
| 4302 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
| 4303 | tmp_ptr = ptr = skipwhite(ptr); |
| 4304 | // Find start of next word. |
| 4305 | tmp_ptr = find_word_start(tmp_ptr); |
| 4306 | // Find end of next word. |
| 4307 | tmp_ptr = find_word_end(tmp_ptr); |
| 4308 | if (tmp_ptr > ptr) |
| 4309 | { |
| 4310 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
| 4311 | { |
| 4312 | if (IObuff[len - 1] != ' ') |
| 4313 | IObuff[len++] = ' '; |
| 4314 | // IObuf =~ "\k.* ", thus len >= 2 |
| 4315 | if (p_js |
| 4316 | && (IObuff[len - 2] == '.' |
| 4317 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4318 | == NULL |
| 4319 | && (IObuff[len - 2] == '?' |
| 4320 | || IObuff[len - 2] == '!')))) |
| 4321 | IObuff[len++] = ' '; |
| 4322 | } |
| 4323 | // copy as much as possible of the new word |
| 4324 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4325 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4326 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4327 | len += (int)(tmp_ptr - ptr); |
| 4328 | *cont_s_ipos = TRUE; |
| 4329 | } |
| 4330 | IObuff[len] = NUL; |
| 4331 | ptr = IObuff; |
| 4332 | } |
| 4333 | if (len == compl_length) |
| 4334 | return NULL; |
| 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | *match_len = len; |
| 4339 | return ptr; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4343 | * Get the next set of words matching "compl_pattern" for default completion(s) |
| 4344 | * (normal ^P/^N and ^X^L). |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4345 | * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the |
| 4346 | * position "st->start_pos" in the "compl_direction" direction. If |
| 4347 | * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and |
| 4348 | * "st->last_match_pos". |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4349 | * Returns OK if a new next match is found, otherwise returns FAIL. |
| 4350 | */ |
| 4351 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4352 | get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4353 | { |
| 4354 | int found_new_match = FAIL; |
| 4355 | int save_p_scs; |
| 4356 | int save_p_ws; |
| 4357 | int looped_around = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4358 | char_u *ptr = NULL; |
| 4359 | int len = 0; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4360 | int in_collect = (cfc_has_mode() && compl_length > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4361 | char_u *leader = ins_compl_leader(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4362 | int score = 0; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4363 | |
| 4364 | // If 'infercase' is set, don't use 'smartcase' here |
| 4365 | save_p_scs = p_scs; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4366 | if (st->ins_buf->b_p_inf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4367 | p_scs = FALSE; |
| 4368 | |
| 4369 | // Buffers other than curbuf are scanned from the beginning or the |
| 4370 | // end but never from the middle, thus setting nowrapscan in this |
| 4371 | // buffer is a good idea, on the other hand, we always set |
| 4372 | // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb |
| 4373 | save_p_ws = p_ws; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4374 | if (st->ins_buf != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4375 | p_ws = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4376 | else if (*st->e_cpt == '.') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4377 | p_ws = TRUE; |
| 4378 | looped_around = FALSE; |
| 4379 | for (;;) |
| 4380 | { |
| 4381 | int cont_s_ipos = FALSE; |
| 4382 | |
| 4383 | ++msg_silent; // Don't want messages for wrapscan. |
| 4384 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4385 | if (in_collect) |
| 4386 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4387 | found_new_match = search_for_fuzzy_match(st->ins_buf, |
| 4388 | st->cur_match_pos, leader, compl_direction, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4389 | start_pos, &len, &ptr, &score); |
| 4390 | } |
| 4391 | // ctrl_x_mode_line_or_eval() || word-wise search that |
| 4392 | // has added a word that was at the beginning of the line |
| 4393 | else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL)) |
| 4394 | found_new_match = search_for_exact_line(st->ins_buf, |
| 4395 | st->cur_match_pos, compl_direction, compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4396 | else |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4397 | found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4398 | NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4399 | 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4400 | --msg_silent; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4401 | if (!compl_started || st->set_match_pos) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4402 | { |
| 4403 | // set "compl_started" even on fail |
| 4404 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4405 | st->first_match_pos = *st->cur_match_pos; |
| 4406 | st->last_match_pos = *st->cur_match_pos; |
| 4407 | st->set_match_pos = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4408 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4409 | else if (st->first_match_pos.lnum == st->last_match_pos.lnum |
| 4410 | && st->first_match_pos.col == st->last_match_pos.col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4411 | { |
| 4412 | found_new_match = FAIL; |
| 4413 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4414 | else if (compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4415 | && (st->prev_match_pos.lnum > st->cur_match_pos->lnum |
| 4416 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4417 | && st->prev_match_pos.col >= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4418 | { |
| 4419 | if (looped_around) |
| 4420 | found_new_match = FAIL; |
| 4421 | else |
| 4422 | looped_around = TRUE; |
| 4423 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4424 | else if (!compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4425 | && (st->prev_match_pos.lnum < st->cur_match_pos->lnum |
| 4426 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4427 | && st->prev_match_pos.col <= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4428 | { |
| 4429 | if (looped_around) |
| 4430 | found_new_match = FAIL; |
| 4431 | else |
| 4432 | looped_around = TRUE; |
| 4433 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4434 | st->prev_match_pos = *st->cur_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4435 | if (found_new_match == FAIL) |
| 4436 | break; |
| 4437 | |
| 4438 | // when ADDING, the text before the cursor matches, skip it |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4439 | if (compl_status_adding() && st->ins_buf == curbuf |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4440 | && start_pos->lnum == st->cur_match_pos->lnum |
| 4441 | && start_pos->col == st->cur_match_pos->col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4442 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4443 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4444 | if (!in_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4445 | ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos, |
| 4446 | &len, &cont_s_ipos); |
glepnir | bfb4eea | 2025-01-31 15:28:29 +0100 | [diff] [blame] | 4447 | if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4448 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4449 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4450 | if (ins_compl_add_infercase(ptr, len, p_ic, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4451 | st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname, |
| 4452 | 0, cont_s_ipos, score) != NOTDONE) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4453 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4454 | if (in_collect && score == compl_first_match->cp_next->cp_score) |
| 4455 | compl_num_bests++; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4456 | found_new_match = OK; |
| 4457 | break; |
| 4458 | } |
| 4459 | } |
| 4460 | p_scs = save_p_scs; |
| 4461 | p_ws = save_p_ws; |
| 4462 | |
| 4463 | return found_new_match; |
| 4464 | } |
| 4465 | |
| 4466 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4467 | * Return the callback function associated with "funcname". |
| 4468 | */ |
| 4469 | #ifdef FEAT_COMPL_FUNC |
| 4470 | static callback_T * |
| 4471 | get_cpt_func_callback(char_u *funcname) |
| 4472 | { |
| 4473 | static callback_T cb; |
| 4474 | char_u buf[LSIZE]; |
| 4475 | int slen; |
| 4476 | |
| 4477 | slen = copy_option_part(&funcname, buf, LSIZE, ","); |
| 4478 | if (slen > 0 && option_set_callback_func(buf, &cb)) |
| 4479 | return &cb; |
| 4480 | return NULL; |
| 4481 | } |
| 4482 | |
| 4483 | /* |
| 4484 | * Retrieve new completion matches by invoking callback "cb". |
| 4485 | */ |
| 4486 | static void |
| 4487 | expand_cpt_function(callback_T *cb) |
| 4488 | { |
| 4489 | // Re-insert the text removed by ins_compl_delete(). |
| 4490 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
| 4491 | // Get matches |
| 4492 | get_cpt_func_completion_matches(cb); |
| 4493 | // Undo insertion |
| 4494 | ins_compl_delete(); |
| 4495 | } |
| 4496 | #endif |
| 4497 | |
| 4498 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4499 | * get the next set of completion matches for "type". |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4500 | * Returns TRUE if a new match is found. Otherwise returns FALSE. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4501 | */ |
| 4502 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4503 | get_next_completion_match(int type, ins_compl_next_state_T *st, pos_T *ini) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4504 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4505 | int found_new_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4506 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4507 | switch (type) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4508 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4509 | case -1: |
| 4510 | break; |
| 4511 | #ifdef FEAT_FIND_ID |
| 4512 | case CTRL_X_PATH_PATTERNS: |
| 4513 | case CTRL_X_PATH_DEFINES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4514 | get_next_include_file_completion(type); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4515 | break; |
| 4516 | #endif |
| 4517 | |
| 4518 | case CTRL_X_DICTIONARY: |
| 4519 | case CTRL_X_THESAURUS: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4520 | get_next_dict_tsr_completion(type, st->dict, st->dict_f); |
| 4521 | st->dict = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4522 | break; |
| 4523 | |
| 4524 | case CTRL_X_TAGS: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4525 | get_next_tag_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4526 | break; |
| 4527 | |
| 4528 | case CTRL_X_FILES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4529 | get_next_filename_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4530 | break; |
| 4531 | |
| 4532 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 4533 | case CTRL_X_CMDLINE_CTRL_X: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4534 | get_next_cmdline_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4535 | break; |
| 4536 | |
| 4537 | #ifdef FEAT_COMPL_FUNC |
| 4538 | case CTRL_X_FUNCTION: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4539 | if (ctrl_x_mode_normal()) // Invoked by a func in 'cpt' option |
| 4540 | expand_cpt_function(st->func_cb); |
| 4541 | else |
| 4542 | expand_by_function(type, compl_pattern.string, NULL); |
| 4543 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4544 | case CTRL_X_OMNI: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4545 | expand_by_function(type, compl_pattern.string, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4546 | break; |
| 4547 | #endif |
| 4548 | |
| 4549 | case CTRL_X_SPELL: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4550 | get_next_spell_completion(st->first_match_pos.lnum); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4551 | break; |
| 4552 | |
| 4553 | default: // normal ^P/^N and ^X^L |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4554 | found_new_match = get_next_default_completion(st, ini); |
| 4555 | if (found_new_match == FAIL && st->ins_buf == curbuf) |
| 4556 | st->found_all = TRUE; |
| 4557 | } |
| 4558 | |
| 4559 | // check if compl_curr_match has changed, (e.g. other type of |
| 4560 | // expansion added something) |
| 4561 | if (type != 0 && compl_curr_match != compl_old_match) |
| 4562 | found_new_match = OK; |
| 4563 | |
| 4564 | return found_new_match; |
| 4565 | } |
| 4566 | |
| 4567 | /* |
| 4568 | * Get the next expansion(s), using "compl_pattern". |
| 4569 | * The search starts at position "ini" in curbuf and in the direction |
| 4570 | * compl_direction. |
| 4571 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4572 | * where we stopped searching before. |
| 4573 | * This may return before finding all the matches. |
| 4574 | * Return the total number of matches or -1 if still unknown -- Acevedo |
| 4575 | */ |
| 4576 | static int |
| 4577 | ins_compl_get_exp(pos_T *ini) |
| 4578 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4579 | static ins_compl_next_state_T st; |
| 4580 | static int st_cleared = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4581 | int i; |
| 4582 | int found_new_match; |
| 4583 | int type = ctrl_x_mode; |
| 4584 | |
| 4585 | if (!compl_started) |
| 4586 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4587 | buf_T *buf; |
| 4588 | |
| 4589 | FOR_ALL_BUFFERS(buf) |
| 4590 | buf->b_scanned = 0; |
| 4591 | if (!st_cleared) |
| 4592 | { |
| 4593 | CLEAR_FIELD(st); |
| 4594 | st_cleared = TRUE; |
| 4595 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4596 | st.found_all = FALSE; |
| 4597 | st.ins_buf = curbuf; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4598 | vim_free(st.e_cpt_copy); |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 4599 | // Make a copy of 'complete', in case the buffer is wiped out. |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4600 | st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL) |
| 4601 | ? (char_u *)"." : curbuf->b_p_cpt); |
| 4602 | st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4603 | st.last_match_pos = st.first_match_pos = *ini; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4604 | |
| 4605 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
| 4606 | && !cpt_compl_src_init(st.e_cpt)) |
| 4607 | return FAIL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4608 | } |
| 4609 | else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf)) |
| 4610 | st.ins_buf = curbuf; // In case the buffer was wiped out. |
| 4611 | |
| 4612 | compl_old_match = compl_curr_match; // remember the last current match |
Christian Brabandt | 13032a4 | 2024-07-28 21:16:48 +0200 | [diff] [blame] | 4613 | st.cur_match_pos = (compl_dir_forward()) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4614 | ? &st.last_match_pos : &st.first_match_pos; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4615 | |
| 4616 | // For ^N/^P loop over all the flags/windows/buffers in 'complete'. |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4617 | for (cpt_value_idx = 0;;) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4618 | { |
| 4619 | found_new_match = FAIL; |
| 4620 | st.set_match_pos = FALSE; |
| 4621 | |
| 4622 | // For ^N/^P pick a new entry from e_cpt if compl_started is off, |
| 4623 | // or if found_all says this entry is done. For ^X^L only use the |
| 4624 | // entries from 'complete' that look in loaded buffers. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4625 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4626 | && (!compl_started || st.found_all)) |
| 4627 | { |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 4628 | int status = process_next_cpt_value(&st, &type, ini, cfc_has_mode()); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4629 | |
| 4630 | if (status == INS_COMPL_CPT_END) |
| 4631 | break; |
| 4632 | if (status == INS_COMPL_CPT_CONT) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4633 | { |
| 4634 | cpt_value_idx++; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4635 | continue; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4636 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4639 | // If complete() was called then compl_pattern has been reset. The |
| 4640 | // following won't work then, bail out. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4641 | if (compl_pattern.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4642 | break; |
| 4643 | |
| 4644 | // get the next set of completion matches |
| 4645 | found_new_match = get_next_completion_match(type, &st, ini); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4646 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4647 | if (type > 0) |
| 4648 | cpt_value_idx++; |
| 4649 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4650 | // break the loop for specialized modes (use 'complete' just for the |
| 4651 | // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new |
| 4652 | // match |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4653 | if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval()) |
| 4654 | || found_new_match != FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4655 | { |
| 4656 | if (got_int) |
| 4657 | break; |
| 4658 | // Fill the popup menu as soon as possible. |
| 4659 | if (type != -1) |
| 4660 | ins_compl_check_keys(0, FALSE); |
| 4661 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4662 | if ((ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4663 | && !ctrl_x_mode_line_or_eval()) || compl_interrupted) |
| 4664 | break; |
| 4665 | compl_started = TRUE; |
| 4666 | } |
| 4667 | else |
| 4668 | { |
| 4669 | // Mark a buffer scanned when it has been scanned completely |
Christian Brabandt | ee9166e | 2023-09-03 21:24:33 +0200 | [diff] [blame] | 4670 | if (buf_valid(st.ins_buf) && (type == 0 || type == CTRL_X_PATH_PATTERNS)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4671 | st.ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4672 | |
| 4673 | compl_started = FALSE; |
| 4674 | } |
| 4675 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 4676 | cpt_value_idx = -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4677 | compl_started = TRUE; |
| 4678 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4679 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4680 | && *st.e_cpt == NUL) // Got to end of 'complete' |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4681 | found_new_match = FAIL; |
| 4682 | |
| 4683 | i = -1; // total of matches, unknown |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4684 | if (found_new_match == FAIL || (ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4685 | && !ctrl_x_mode_line_or_eval())) |
| 4686 | i = ins_compl_make_cyclic(); |
| 4687 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4688 | if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0) |
| 4689 | fuzzy_longest_match(); |
| 4690 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4691 | if (compl_old_match != NULL) |
| 4692 | { |
| 4693 | // If several matches were added (FORWARD) or the search failed and has |
| 4694 | // just been made cyclic then we have to move compl_curr_match to the |
| 4695 | // next or previous entry (if any) -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4696 | compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4697 | : compl_old_match->cp_prev; |
| 4698 | if (compl_curr_match == NULL) |
| 4699 | compl_curr_match = compl_old_match; |
| 4700 | } |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 4701 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 4702 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4703 | return i; |
| 4704 | } |
| 4705 | |
| 4706 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4707 | * Update "compl_shown_match" to the actually shown match, it may differ when |
| 4708 | * "compl_leader" is used to omit some of the matches. |
| 4709 | */ |
| 4710 | static void |
| 4711 | ins_compl_update_shown_match(void) |
| 4712 | { |
| 4713 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4714 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4715 | && compl_shown_match->cp_next != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4716 | && !is_first_match(compl_shown_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4717 | compl_shown_match = compl_shown_match->cp_next; |
| 4718 | |
| 4719 | // If we didn't find it searching forward, and compl_shows_dir is |
| 4720 | // backward, find the last match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4721 | if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4722 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4723 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4724 | && (compl_shown_match->cp_next == NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4725 | || is_first_match(compl_shown_match->cp_next))) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4726 | { |
| 4727 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4728 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4729 | && compl_shown_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4730 | && !is_first_match(compl_shown_match->cp_prev)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4731 | compl_shown_match = compl_shown_match->cp_prev; |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4736 | * Delete the old text being completed. |
| 4737 | */ |
| 4738 | void |
| 4739 | ins_compl_delete(void) |
| 4740 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4741 | // In insert mode: Delete the typed part. |
| 4742 | // In replace mode: Put the old characters back, if any. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4743 | int col = compl_col + (compl_status_adding() ? compl_length : 0); |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4744 | string_T remaining = {NULL, 0}; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4745 | int orig_col; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4746 | int has_preinsert = ins_compl_preinsert_effect(); |
| 4747 | if (has_preinsert) |
| 4748 | { |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 4749 | col += (int)ins_compl_leader_len(); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4750 | curwin->w_cursor.col = compl_ins_end_col; |
| 4751 | } |
| 4752 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4753 | if (curwin->w_cursor.lnum > compl_lnum) |
| 4754 | { |
| 4755 | if (curwin->w_cursor.col < ml_get_curline_len()) |
| 4756 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4757 | char_u *line = ml_get_cursor(); |
| 4758 | remaining.length = ml_get_cursor_len(); |
| 4759 | remaining.string = vim_strnsave(line, remaining.length); |
| 4760 | if (remaining.string == NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4761 | return; |
| 4762 | } |
| 4763 | while (curwin->w_cursor.lnum > compl_lnum) |
| 4764 | { |
| 4765 | if (ml_delete(curwin->w_cursor.lnum) == FAIL) |
| 4766 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4767 | if (remaining.string) |
| 4768 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4769 | return; |
| 4770 | } |
zeertzjq | 060e655 | 2025-02-21 20:06:26 +0100 | [diff] [blame] | 4771 | deleted_lines_mark(curwin->w_cursor.lnum, 1L); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4772 | curwin->w_cursor.lnum--; |
| 4773 | } |
| 4774 | // move cursor to end of line |
| 4775 | curwin->w_cursor.col = ml_get_curline_len(); |
| 4776 | } |
| 4777 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4778 | if ((int)curwin->w_cursor.col > col) |
| 4779 | { |
| 4780 | if (stop_arrow() == FAIL) |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 4781 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4782 | if (remaining.string) |
| 4783 | vim_free(remaining.string); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4784 | return; |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 4785 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4786 | backspace_until_column(col); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 4787 | compl_ins_end_col = curwin->w_cursor.col; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4790 | if (remaining.string != NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4791 | { |
| 4792 | orig_col = curwin->w_cursor.col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4793 | ins_str(remaining.string, remaining.length); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4794 | curwin->w_cursor.col = orig_col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 4795 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4796 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4797 | // TODO: is this sufficient for redrawing? Redrawing everything causes |
| 4798 | // flicker, thus we can't do that. |
| 4799 | changed_cline_bef_curs(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4800 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4801 | // clear v:completed_item |
| 4802 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4803 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4807 | * Insert a completion string that contains newlines. |
| 4808 | * The string is split and inserted line by line. |
| 4809 | */ |
| 4810 | static void |
| 4811 | ins_compl_expand_multiple(char_u *str) |
| 4812 | { |
| 4813 | char_u *start = str; |
| 4814 | char_u *curr = str; |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 4815 | int base_indent = get_indent(); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4816 | |
| 4817 | while (*curr != NUL) |
| 4818 | { |
| 4819 | if (*curr == '\n') |
| 4820 | { |
| 4821 | // Insert the text chunk before newline |
| 4822 | if (curr > start) |
| 4823 | ins_char_bytes(start, (int)(curr - start)); |
| 4824 | |
| 4825 | // Handle newline |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 4826 | open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4827 | start = curr + 1; |
| 4828 | } |
| 4829 | curr++; |
| 4830 | } |
| 4831 | |
| 4832 | // Handle remaining text after last newline (if any) |
| 4833 | if (curr > start) |
| 4834 | ins_char_bytes(start, (int)(curr - start)); |
| 4835 | |
| 4836 | compl_ins_end_col = curwin->w_cursor.col; |
| 4837 | } |
| 4838 | |
| 4839 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4840 | * Insert the new text being completed. |
| 4841 | * "in_compl_func" is TRUE when called from complete_check(). |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4842 | * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE |
| 4843 | * cursor needs to move back from the inserted text to the compl_leader. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4844 | */ |
| 4845 | void |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4846 | ins_compl_insert(int in_compl_func, int move_cursor) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4847 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4848 | int compl_len = get_compl_len(); |
| 4849 | int preinsert = ins_compl_has_preinsert(); |
| 4850 | char_u *cp_str = compl_shown_match->cp_str.string; |
| 4851 | size_t cp_str_len = compl_shown_match->cp_str.length; |
| 4852 | size_t leader_len = ins_compl_leader_len(); |
| 4853 | char_u *has_multiple = vim_strchr(cp_str, '\n'); |
Bram Moolenaar | 4b28ba3 | 2021-12-27 19:28:37 +0000 | [diff] [blame] | 4854 | |
| 4855 | // Make sure we don't go over the end of the string, this can happen with |
| 4856 | // illegal bytes. |
zeertzjq | 8297e2c | 2025-01-30 11:04:47 +0100 | [diff] [blame] | 4857 | if (compl_len < (int)cp_str_len) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4858 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 4859 | if (has_multiple) |
| 4860 | ins_compl_expand_multiple(cp_str + compl_len); |
| 4861 | else |
| 4862 | { |
| 4863 | ins_compl_insert_bytes(cp_str + compl_len, -1); |
| 4864 | if (preinsert && move_cursor) |
| 4865 | curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len); |
| 4866 | } |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 4867 | } |
| 4868 | if (match_at_original_text(compl_shown_match) || preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4869 | compl_used_match = FALSE; |
| 4870 | else |
| 4871 | compl_used_match = TRUE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4872 | #ifdef FEAT_EVAL |
| 4873 | { |
| 4874 | dict_T *dict = ins_compl_dict_alloc(compl_shown_match); |
| 4875 | |
| 4876 | set_vim_var_dict(VV_COMPLETED_ITEM, dict); |
| 4877 | } |
| 4878 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4879 | if (!in_compl_func) |
| 4880 | compl_curr_match = compl_shown_match; |
| 4881 | } |
| 4882 | |
| 4883 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4884 | * show the file name for the completion match (if any). Truncate the file |
| 4885 | * name to avoid a wait for return. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4886 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4887 | static void |
| 4888 | ins_compl_show_filename(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4889 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4890 | char *lead = _("match in file"); |
| 4891 | int space = sc_col - vim_strsize((char_u *)lead) - 2; |
| 4892 | char_u *s; |
| 4893 | char_u *e; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4894 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4895 | if (space <= 0) |
| 4896 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4897 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4898 | // We need the tail that fits. With double-byte encoding going |
| 4899 | // back from the end is very slow, thus go from the start and keep |
| 4900 | // the text that fits in "space" between "s" and "e". |
| 4901 | for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4902 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4903 | space -= ptr2cells(e); |
| 4904 | while (space < 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4905 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4906 | space += ptr2cells(s); |
| 4907 | MB_PTR_ADV(s); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4908 | } |
| 4909 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4910 | msg_hist_off = TRUE; |
| 4911 | vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, |
| 4912 | s > compl_shown_match->cp_fname ? "<" : "", s); |
| 4913 | msg((char *)IObuff); |
| 4914 | msg_hist_off = FALSE; |
| 4915 | redraw_cmdline = FALSE; // don't overwrite! |
| 4916 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4917 | |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4918 | /* |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 4919 | * Find a completion item when 'completeopt' contains "fuzzy". |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4920 | */ |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4921 | static compl_T * |
| 4922 | find_comp_when_fuzzy(void) |
| 4923 | { |
| 4924 | int score; |
| 4925 | char_u* str; |
| 4926 | int target_idx = -1; |
| 4927 | int is_forward = compl_shows_dir_forward(); |
| 4928 | int is_backward = compl_shows_dir_backward(); |
| 4929 | compl_T *comp = NULL; |
| 4930 | |
glepnir | 43eef88 | 2024-06-19 20:20:48 +0200 | [diff] [blame] | 4931 | if ((is_forward && compl_selected_item == compl_match_arraysize - 1) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4932 | || (is_backward && compl_selected_item == 0)) |
glepnir | 3e50a28 | 2025-04-03 21:17:06 +0200 | [diff] [blame] | 4933 | return compl_first_match != compl_shown_match ? |
| 4934 | (is_forward ? compl_shown_match->cp_next : compl_first_match) : |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 4935 | (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4936 | |
| 4937 | if (is_forward) |
| 4938 | target_idx = compl_selected_item + 1; |
| 4939 | else if (is_backward) |
| 4940 | target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1 |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4941 | : compl_selected_item - 1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4942 | |
| 4943 | score = compl_match_array[target_idx].pum_score; |
| 4944 | str = compl_match_array[target_idx].pum_text; |
| 4945 | |
| 4946 | comp = compl_first_match; |
| 4947 | do { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4948 | if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR])) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4949 | return comp; |
| 4950 | comp = comp->cp_next; |
| 4951 | } while (comp != NULL && !is_first_match(comp)); |
| 4952 | |
| 4953 | return NULL; |
| 4954 | } |
| 4955 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4956 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4957 | * Find the next set of matches for completion. Repeat the completion "todo" |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4958 | * times. The number of matches found is returned in 'num_matches'. |
| 4959 | * |
| 4960 | * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to |
| 4961 | * get more completions. If it is FALSE, then do nothing when there are no more |
| 4962 | * completions in the given direction. |
| 4963 | * |
| 4964 | * If "advance" is TRUE, then completion will move to the first match. |
| 4965 | * Otherwise, the original text will be shown. |
| 4966 | * |
| 4967 | * Returns OK on success and -1 if the number of matches are unknown. |
| 4968 | */ |
| 4969 | static int |
| 4970 | find_next_completion_match( |
| 4971 | int allow_get_expansion, |
| 4972 | int todo, // repeat completion this many times |
| 4973 | int advance, |
| 4974 | int *num_matches) |
| 4975 | { |
| 4976 | int found_end = FALSE; |
| 4977 | compl_T *found_compl = NULL; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 4978 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 4979 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
| 4980 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4981 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4982 | while (--todo >= 0) |
| 4983 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4984 | if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4985 | { |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 4986 | compl_shown_match = compl_fuzzy_match && compl_match_array != NULL |
| 4987 | ? find_comp_when_fuzzy() : compl_shown_match->cp_next; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4988 | found_end = (compl_first_match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4989 | && (is_first_match(compl_shown_match->cp_next) |
| 4990 | || is_first_match(compl_shown_match))); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4991 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4992 | else if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4993 | && compl_shown_match->cp_prev != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4994 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4995 | found_end = is_first_match(compl_shown_match); |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 4996 | compl_shown_match = compl_fuzzy_match && compl_match_array != NULL |
| 4997 | ? find_comp_when_fuzzy() : compl_shown_match->cp_prev; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4998 | found_end |= is_first_match(compl_shown_match); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4999 | } |
| 5000 | else |
| 5001 | { |
| 5002 | if (!allow_get_expansion) |
| 5003 | { |
| 5004 | if (advance) |
| 5005 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5006 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5007 | compl_pending -= todo + 1; |
| 5008 | else |
| 5009 | compl_pending += todo + 1; |
| 5010 | } |
| 5011 | return -1; |
| 5012 | } |
| 5013 | |
| 5014 | if (!compl_no_select && advance) |
| 5015 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5016 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5017 | --compl_pending; |
| 5018 | else |
| 5019 | ++compl_pending; |
| 5020 | } |
| 5021 | |
| 5022 | // Find matches. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5023 | *num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5024 | |
| 5025 | // handle any pending completions |
| 5026 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5027 | && advance) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5028 | { |
| 5029 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 5030 | { |
| 5031 | compl_shown_match = compl_shown_match->cp_next; |
| 5032 | --compl_pending; |
| 5033 | } |
| 5034 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 5035 | { |
| 5036 | compl_shown_match = compl_shown_match->cp_prev; |
| 5037 | ++compl_pending; |
| 5038 | } |
| 5039 | else |
| 5040 | break; |
| 5041 | } |
| 5042 | found_end = FALSE; |
| 5043 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5044 | if (!match_at_original_text(compl_shown_match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5045 | && compl_leader.string != NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5046 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5047 | compl_leader.string, (int)compl_leader.length) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5048 | && !(compl_fuzzy_match && compl_shown_match->cp_score > 0)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5049 | ++todo; |
| 5050 | else |
| 5051 | // Remember a matching item. |
| 5052 | found_compl = compl_shown_match; |
| 5053 | |
| 5054 | // Stop at the end of the list when we found a usable match. |
| 5055 | if (found_end) |
| 5056 | { |
| 5057 | if (found_compl != NULL) |
| 5058 | { |
| 5059 | compl_shown_match = found_compl; |
| 5060 | break; |
| 5061 | } |
| 5062 | todo = 1; // use first usable match after wrapping around |
| 5063 | } |
| 5064 | } |
| 5065 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5066 | return OK; |
| 5067 | } |
| 5068 | |
| 5069 | /* |
| 5070 | * Fill in the next completion in the current direction. |
| 5071 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 5072 | * get more completions. If it is FALSE, then we just do nothing when there |
| 5073 | * are no more completions in a given direction. The latter case is used when |
| 5074 | * we are still in the middle of finding completions, to allow browsing |
| 5075 | * through the ones found so far. |
| 5076 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 5077 | * |
| 5078 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 5079 | * compl_shown_match here. |
| 5080 | * |
| 5081 | * Note that this function may be called recursively once only. First with |
| 5082 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 5083 | * calls this function with "allow_get_expansion" FALSE. |
| 5084 | */ |
| 5085 | static int |
| 5086 | ins_compl_next( |
| 5087 | int allow_get_expansion, |
| 5088 | int count, // repeat completion this many times; should |
| 5089 | // be at least 1 |
| 5090 | int insert_match, // Insert the newly selected match |
| 5091 | int in_compl_func) // called from complete_check() |
| 5092 | { |
| 5093 | int num_matches = -1; |
| 5094 | int todo = count; |
| 5095 | int advance; |
| 5096 | int started = compl_started; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5097 | buf_T *orig_curbuf = curbuf; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 5098 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5099 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 5100 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5101 | int compl_preinsert = ins_compl_has_preinsert(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5102 | |
| 5103 | // When user complete function return -1 for findstart which is next |
| 5104 | // time of 'always', compl_shown_match become NULL. |
| 5105 | if (compl_shown_match == NULL) |
| 5106 | return -1; |
| 5107 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5108 | if (compl_leader.string != NULL |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5109 | && !match_at_original_text(compl_shown_match) |
| 5110 | && !compl_fuzzy_match) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5111 | // Update "compl_shown_match" to the actually shown match |
| 5112 | ins_compl_update_shown_match(); |
| 5113 | |
| 5114 | if (allow_get_expansion && insert_match |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 5115 | && (!compl_get_longest || compl_used_match)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5116 | // Delete old text to be replaced |
| 5117 | ins_compl_delete(); |
| 5118 | |
| 5119 | // When finding the longest common text we stick at the original text, |
| 5120 | // don't let CTRL-N or CTRL-P move to the first match. |
| 5121 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 5122 | |
| 5123 | // When restarting the search don't insert the first match either. |
| 5124 | if (compl_restarting) |
| 5125 | { |
| 5126 | advance = FALSE; |
| 5127 | compl_restarting = FALSE; |
| 5128 | } |
| 5129 | |
| 5130 | // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 5131 | // around. |
| 5132 | if (find_next_completion_match(allow_get_expansion, todo, advance, |
| 5133 | &num_matches) == -1) |
| 5134 | return -1; |
| 5135 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5136 | if (curbuf != orig_curbuf) |
| 5137 | { |
| 5138 | // In case some completion function switched buffer, don't want to |
| 5139 | // insert the completion elsewhere. |
| 5140 | return -1; |
| 5141 | } |
| 5142 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5143 | // Insert the text of the new completion, or the compl_leader. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5144 | if (compl_no_insert && !started && !compl_preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5145 | { |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5146 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5147 | compl_used_match = FALSE; |
| 5148 | } |
| 5149 | else if (insert_match) |
| 5150 | { |
| 5151 | if (!compl_get_longest || compl_used_match) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5152 | ins_compl_insert(in_compl_func, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5153 | else |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5154 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5155 | } |
| 5156 | else |
| 5157 | compl_used_match = FALSE; |
| 5158 | |
| 5159 | if (!allow_get_expansion) |
| 5160 | { |
| 5161 | // may undisplay the popup menu first |
| 5162 | ins_compl_upd_pum(); |
| 5163 | |
| 5164 | if (pum_enough_matches()) |
| 5165 | // Will display the popup menu, don't redraw yet to avoid flicker. |
| 5166 | pum_call_update_screen(); |
| 5167 | else |
| 5168 | // Not showing the popup menu yet, redraw to show the user what was |
| 5169 | // inserted. |
| 5170 | update_screen(0); |
| 5171 | |
| 5172 | // display the updated popup menu |
| 5173 | ins_compl_show_pum(); |
| 5174 | #ifdef FEAT_GUI |
| 5175 | if (gui.in_use) |
| 5176 | { |
| 5177 | // Show the cursor after the match, not after the redrawn text. |
| 5178 | setcursor(); |
| 5179 | out_flush_cursor(FALSE, FALSE); |
| 5180 | } |
| 5181 | #endif |
| 5182 | |
| 5183 | // Delete old text to be replaced, since we're still searching and |
| 5184 | // don't want to match ourselves! |
| 5185 | ins_compl_delete(); |
| 5186 | } |
| 5187 | |
| 5188 | // Enter will select a match when the match wasn't inserted and the popup |
| 5189 | // menu is visible. |
| 5190 | if (compl_no_insert && !started) |
| 5191 | compl_enter_selects = TRUE; |
| 5192 | else |
| 5193 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 5194 | |
| 5195 | // Show the file name for the match (if any) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5196 | if (compl_shown_match->cp_fname != NULL) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5197 | ins_compl_show_filename(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5198 | |
| 5199 | return num_matches; |
| 5200 | } |
| 5201 | |
| 5202 | /* |
| 5203 | * Call this while finding completions, to check whether the user has hit a key |
| 5204 | * that should change the currently displayed completion, or exit completion |
| 5205 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
| 5206 | * possible. -- webb |
| 5207 | * "frequency" specifies out of how many calls we actually check. |
| 5208 | * "in_compl_func" is TRUE when called from complete_check(), don't set |
| 5209 | * compl_curr_match. |
| 5210 | */ |
| 5211 | void |
| 5212 | ins_compl_check_keys(int frequency, int in_compl_func) |
| 5213 | { |
| 5214 | static int count = 0; |
| 5215 | int c; |
| 5216 | |
| 5217 | // Don't check when reading keys from a script, :normal or feedkeys(). |
| 5218 | // That would break the test scripts. But do check for keys when called |
| 5219 | // from complete_check(). |
| 5220 | if (!in_compl_func && (using_script() || ex_normal_busy)) |
| 5221 | return; |
| 5222 | |
| 5223 | // Only do this at regular intervals |
| 5224 | if (++count < frequency) |
| 5225 | return; |
| 5226 | count = 0; |
| 5227 | |
| 5228 | // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 5229 | // can't do its work correctly. |
| 5230 | c = vpeekc_any(); |
| 5231 | if (c != NUL) |
| 5232 | { |
| 5233 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 5234 | { |
| 5235 | c = safe_vgetc(); // Eat the character |
| 5236 | compl_shows_dir = ins_compl_key2dir(c); |
| 5237 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 5238 | c != K_UP && c != K_DOWN, in_compl_func); |
| 5239 | } |
| 5240 | else |
| 5241 | { |
| 5242 | // Need to get the character to have KeyTyped set. We'll put it |
| 5243 | // back with vungetc() below. But skip K_IGNORE. |
| 5244 | c = safe_vgetc(); |
| 5245 | if (c != K_IGNORE) |
| 5246 | { |
| 5247 | // Don't interrupt completion when the character wasn't typed, |
| 5248 | // e.g., when doing @q to replay keys. |
| 5249 | if (c != Ctrl_R && KeyTyped) |
| 5250 | compl_interrupted = TRUE; |
| 5251 | |
| 5252 | vungetc(c); |
| 5253 | } |
| 5254 | } |
| 5255 | } |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5256 | if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5257 | { |
| 5258 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 5259 | |
| 5260 | compl_pending = 0; |
| 5261 | (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func); |
| 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | /* |
| 5266 | * Decide the direction of Insert mode complete from the key typed. |
| 5267 | * Returns BACKWARD or FORWARD. |
| 5268 | */ |
| 5269 | static int |
| 5270 | ins_compl_key2dir(int c) |
| 5271 | { |
| 5272 | if (c == Ctrl_P || c == Ctrl_L |
| 5273 | || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP) |
| 5274 | return BACKWARD; |
| 5275 | return FORWARD; |
| 5276 | } |
| 5277 | |
| 5278 | /* |
| 5279 | * Return TRUE for keys that are used for completion only when the popup menu |
| 5280 | * is visible. |
| 5281 | */ |
| 5282 | static int |
| 5283 | ins_compl_pum_key(int c) |
| 5284 | { |
| 5285 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
| 5286 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 5287 | || c == K_UP || c == K_DOWN); |
| 5288 | } |
| 5289 | |
| 5290 | /* |
| 5291 | * Decide the number of completions to move forward. |
| 5292 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 5293 | */ |
| 5294 | static int |
| 5295 | ins_compl_key2count(int c) |
| 5296 | { |
| 5297 | int h; |
| 5298 | |
| 5299 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
| 5300 | { |
| 5301 | h = pum_get_height(); |
| 5302 | if (h > 3) |
| 5303 | h -= 2; // keep some context |
| 5304 | return h; |
| 5305 | } |
| 5306 | return 1; |
| 5307 | } |
| 5308 | |
| 5309 | /* |
| 5310 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 5311 | * to change the currently selected completion. |
| 5312 | */ |
| 5313 | static int |
| 5314 | ins_compl_use_match(int c) |
| 5315 | { |
| 5316 | switch (c) |
| 5317 | { |
| 5318 | case K_UP: |
| 5319 | case K_DOWN: |
| 5320 | case K_PAGEDOWN: |
| 5321 | case K_KPAGEDOWN: |
| 5322 | case K_S_DOWN: |
| 5323 | case K_PAGEUP: |
| 5324 | case K_KPAGEUP: |
| 5325 | case K_S_UP: |
| 5326 | return FALSE; |
| 5327 | } |
| 5328 | return TRUE; |
| 5329 | } |
| 5330 | |
| 5331 | /* |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5332 | * Get the pattern, column and length for normal completion (CTRL-N CTRL-P |
| 5333 | * completion) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5334 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5335 | * Uses the global variables: compl_cont_status and ctrl_x_mode |
| 5336 | */ |
| 5337 | static int |
| 5338 | get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5339 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5340 | if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5341 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5342 | if (!compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5343 | { |
| 5344 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
| 5345 | ; |
| 5346 | compl_col += ++startcol; |
| 5347 | compl_length = curs_col - startcol; |
| 5348 | } |
| 5349 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5350 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5351 | compl_pattern.string = str_foldcase(line + compl_col, |
| 5352 | compl_length, NULL, 0); |
| 5353 | if (compl_pattern.string == NULL) |
| 5354 | { |
| 5355 | compl_pattern.length = 0; |
| 5356 | return FAIL; |
| 5357 | } |
| 5358 | compl_pattern.length = STRLEN(compl_pattern.string); |
| 5359 | } |
| 5360 | else |
| 5361 | { |
| 5362 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5363 | if (compl_pattern.string == NULL) |
| 5364 | { |
| 5365 | compl_pattern.length = 0; |
| 5366 | return FAIL; |
| 5367 | } |
| 5368 | compl_pattern.length = (size_t)compl_length; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5369 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5370 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5371 | else if (compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5372 | { |
| 5373 | char_u *prefix = (char_u *)"\\<"; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5374 | size_t prefixlen = STRLEN_LITERAL("\\<"); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5375 | size_t n; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5376 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5377 | if (!vim_iswordp(line + compl_col) |
| 5378 | || (compl_col > 0 |
| 5379 | && (vim_iswordp(mb_prevptr(line, line + compl_col))))) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5380 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5381 | prefix = (char_u *)""; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5382 | prefixlen = 0; |
| 5383 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5384 | |
| 5385 | // we need up to 2 extra chars for the prefix |
| 5386 | n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen; |
| 5387 | compl_pattern.string = alloc(n); |
| 5388 | if (compl_pattern.string == NULL) |
| 5389 | { |
| 5390 | compl_pattern.length = 0; |
| 5391 | return FAIL; |
| 5392 | } |
| 5393 | STRCPY((char *)compl_pattern.string, prefix); |
| 5394 | (void)quote_meta(compl_pattern.string + prefixlen, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5395 | line + compl_col, compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5396 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5397 | } |
| 5398 | else if (--startcol < 0 |
| 5399 | || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) |
| 5400 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5401 | size_t len = STRLEN_LITERAL("\\<\\k\\k"); |
| 5402 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5403 | // Match any word of at least two chars |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5404 | compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len); |
| 5405 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5406 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5407 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5408 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5409 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5410 | compl_pattern.length = len; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5411 | compl_col += curs_col; |
| 5412 | compl_length = 0; |
| 5413 | } |
| 5414 | else |
| 5415 | { |
| 5416 | // Search the point of change class of multibyte character |
| 5417 | // or not a word single byte character backward. |
| 5418 | if (has_mbyte) |
| 5419 | { |
| 5420 | int base_class; |
| 5421 | int head_off; |
| 5422 | |
| 5423 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5424 | base_class = mb_get_class(line + startcol); |
| 5425 | while (--startcol >= 0) |
| 5426 | { |
| 5427 | head_off = (*mb_head_off)(line, line + startcol); |
| 5428 | if (base_class != mb_get_class(line + startcol |
| 5429 | - head_off)) |
| 5430 | break; |
| 5431 | startcol -= head_off; |
| 5432 | } |
| 5433 | } |
| 5434 | else |
| 5435 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
| 5436 | ; |
| 5437 | compl_col += ++startcol; |
| 5438 | compl_length = (int)curs_col - startcol; |
| 5439 | if (compl_length == 1) |
| 5440 | { |
| 5441 | // Only match word with at least two chars -- webb |
| 5442 | // there's no need to call quote_meta, |
| 5443 | // alloc(7) is enough -- Acevedo |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5444 | compl_pattern.string = alloc(7); |
| 5445 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5446 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5447 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5448 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5449 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5450 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5451 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1); |
| 5452 | STRCAT((char *)compl_pattern.string, "\\k"); |
| 5453 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5454 | } |
| 5455 | else |
| 5456 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5457 | size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2; |
| 5458 | |
| 5459 | compl_pattern.string = alloc(n); |
| 5460 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5461 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5462 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5463 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5464 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5465 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5466 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5467 | compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5468 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
| 5472 | return OK; |
| 5473 | } |
| 5474 | |
| 5475 | /* |
| 5476 | * Get the pattern, column and length for whole line completion or for the |
| 5477 | * complete() function. |
| 5478 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5479 | */ |
| 5480 | static int |
| 5481 | get_wholeline_compl_info(char_u *line, colnr_T curs_col) |
| 5482 | { |
| 5483 | compl_col = (colnr_T)getwhitecols(line); |
| 5484 | compl_length = (int)curs_col - (int)compl_col; |
| 5485 | if (compl_length < 0) // cursor in indent: empty pattern |
| 5486 | compl_length = 0; |
| 5487 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5488 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5489 | compl_pattern.string = str_foldcase(line + compl_col, compl_length, |
| 5490 | NULL, 0); |
| 5491 | if (compl_pattern.string == NULL) |
| 5492 | { |
| 5493 | compl_pattern.length = 0; |
| 5494 | return FAIL; |
| 5495 | } |
| 5496 | compl_pattern.length = STRLEN(compl_pattern.string); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5497 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5498 | else |
| 5499 | { |
| 5500 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5501 | if (compl_pattern.string == NULL) |
| 5502 | { |
| 5503 | compl_pattern.length = 0; |
| 5504 | return FAIL; |
| 5505 | } |
| 5506 | compl_pattern.length = (size_t)compl_length; |
| 5507 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5508 | |
| 5509 | return OK; |
| 5510 | } |
| 5511 | |
| 5512 | /* |
| 5513 | * Get the pattern, column and length for filename completion. |
| 5514 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5515 | */ |
| 5516 | static int |
| 5517 | get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5518 | { |
| 5519 | // Go back to just before the first filename character. |
| 5520 | if (startcol > 0) |
| 5521 | { |
| 5522 | char_u *p = line + startcol; |
| 5523 | |
| 5524 | MB_PTR_BACK(line, p); |
| 5525 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 5526 | MB_PTR_BACK(line, p); |
| 5527 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 5528 | startcol = 0; |
| 5529 | else |
| 5530 | startcol = (int)(p - line) + 1; |
| 5531 | } |
| 5532 | |
| 5533 | compl_col += startcol; |
| 5534 | compl_length = (int)curs_col - startcol; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5535 | compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES); |
| 5536 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5537 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5538 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5539 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5540 | } |
| 5541 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5542 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5543 | |
| 5544 | return OK; |
| 5545 | } |
| 5546 | |
| 5547 | /* |
| 5548 | * Get the pattern, column and length for command-line completion. |
| 5549 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5550 | */ |
| 5551 | static int |
| 5552 | get_cmdline_compl_info(char_u *line, colnr_T curs_col) |
| 5553 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5554 | compl_pattern.string = vim_strnsave(line, curs_col); |
| 5555 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5556 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5557 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5558 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5559 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5560 | compl_pattern.length = curs_col; |
| 5561 | set_cmd_context(&compl_xp, compl_pattern.string, |
| 5562 | (int)compl_pattern.length, curs_col, FALSE); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5563 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5564 | || compl_xp.xp_context == EXPAND_NOTHING) |
| 5565 | // No completion possible, use an empty pattern to get a |
| 5566 | // "pattern not found" message. |
| 5567 | compl_col = curs_col; |
| 5568 | else |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5569 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5570 | compl_length = curs_col - compl_col; |
| 5571 | |
| 5572 | return OK; |
| 5573 | } |
| 5574 | |
| 5575 | /* |
| 5576 | * Get the pattern, column and length for user defined completion ('omnifunc', |
| 5577 | * 'completefunc' and 'thesaurusfunc') |
| 5578 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5579 | * Uses the global variable: spell_bad_len |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5580 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 5581 | * option; otherwise, it is NULL. |
| 5582 | * "startcol", when not NULL, contains the column returned by function. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5583 | */ |
| 5584 | static int |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5585 | get_userdefined_compl_info(colnr_T curs_col UNUSED, callback_T *cb UNUSED, |
| 5586 | int *startcol UNUSED) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5587 | { |
| 5588 | int ret = FAIL; |
| 5589 | |
| 5590 | #ifdef FEAT_COMPL_FUNC |
| 5591 | // Call user defined function 'completefunc' with "a:findstart" |
| 5592 | // set to 1 to obtain the length of text to use for completion. |
| 5593 | char_u *line; |
| 5594 | typval_T args[3]; |
| 5595 | int col; |
| 5596 | char_u *funcname; |
| 5597 | pos_T pos; |
| 5598 | int save_State = State; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5599 | int len; |
| 5600 | string_T *compl_pat; |
| 5601 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5602 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5603 | if (!is_cpt_function) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5604 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5605 | // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern |
| 5606 | // length as a string |
| 5607 | funcname = get_complete_funcname(ctrl_x_mode); |
| 5608 | if (*funcname == NUL) |
| 5609 | { |
| 5610 | semsg(_(e_option_str_is_not_set), ctrl_x_mode_function() |
| 5611 | ? "completefunc" : "omnifunc"); |
| 5612 | return FAIL; |
| 5613 | } |
| 5614 | cb = get_insert_callback(ctrl_x_mode); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | args[0].v_type = VAR_NUMBER; |
| 5618 | args[0].vval.v_number = 1; |
| 5619 | args[1].v_type = VAR_STRING; |
| 5620 | args[1].vval.v_string = (char_u *)""; |
| 5621 | args[2].v_type = VAR_UNKNOWN; |
| 5622 | pos = curwin->w_cursor; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5623 | ++textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5624 | col = call_callback_retnr(cb, 2, args); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5625 | --textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5626 | |
| 5627 | State = save_State; |
| 5628 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 5629 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5630 | validate_cursor(); |
| 5631 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 5632 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 5633 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5634 | return FAIL; |
| 5635 | } |
| 5636 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5637 | if (startcol != NULL) |
| 5638 | *startcol = col; |
| 5639 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5640 | // Return value -2 means the user complete function wants to cancel the |
| 5641 | // complete without an error, do the same if the function did not execute |
| 5642 | // successfully. |
| 5643 | if (col == -2 || aborting()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5644 | return FAIL; |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5645 | // Return value -3 does the same as -2 and leaves CTRL-X mode. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5646 | if (col == -3) |
| 5647 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5648 | if (is_cpt_function) |
| 5649 | return FAIL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5650 | ctrl_x_mode = CTRL_X_NORMAL; |
| 5651 | edit_submode = NULL; |
| 5652 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 5653 | msg_clr_cmdline(); |
| 5654 | return FAIL; |
| 5655 | } |
| 5656 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 5657 | // Reset extended parameters of completion, when starting new |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5658 | // completion. |
| 5659 | compl_opt_refresh_always = FALSE; |
| 5660 | compl_opt_suppress_empty = FALSE; |
| 5661 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5662 | if (col < 0 || col > curs_col) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5663 | col = curs_col; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5664 | |
| 5665 | // Setup variables for completion. Need to obtain "line" again, |
| 5666 | // it may have become invalid. |
| 5667 | line = ml_get(curwin->w_cursor.lnum); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5668 | len = curs_col - col; |
| 5669 | compl_pat = is_cpt_function ? &cpt_compl_pattern : &compl_pattern; |
| 5670 | compl_pat->string = vim_strnsave(line + col, (size_t)len); |
| 5671 | if (compl_pat->string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5672 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5673 | compl_pat->length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5674 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5675 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5676 | compl_pat->length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5677 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5678 | if (!is_cpt_function) |
| 5679 | { |
| 5680 | compl_col = col; |
| 5681 | compl_length = len; |
| 5682 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5683 | ret = OK; |
| 5684 | #endif |
| 5685 | |
| 5686 | return ret; |
| 5687 | } |
| 5688 | |
| 5689 | /* |
| 5690 | * Get the pattern, column and length for spell completion. |
| 5691 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5692 | * Uses the global variable: spell_bad_len |
| 5693 | */ |
| 5694 | static int |
| 5695 | get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED) |
| 5696 | { |
| 5697 | int ret = FAIL; |
| 5698 | #ifdef FEAT_SPELL |
| 5699 | char_u *line; |
| 5700 | |
| 5701 | if (spell_bad_len > 0) |
| 5702 | compl_col = curs_col - spell_bad_len; |
| 5703 | else |
| 5704 | compl_col = spell_word_start(startcol); |
| 5705 | if (compl_col >= (colnr_T)startcol) |
| 5706 | { |
| 5707 | compl_length = 0; |
| 5708 | compl_col = curs_col; |
| 5709 | } |
| 5710 | else |
| 5711 | { |
| 5712 | spell_expand_check_cap(compl_col); |
| 5713 | compl_length = (int)curs_col - compl_col; |
| 5714 | } |
| 5715 | // Need to obtain "line" again, it may have become invalid. |
| 5716 | line = ml_get(curwin->w_cursor.lnum); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5717 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5718 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5719 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5720 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5721 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5722 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5723 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5724 | compl_pattern.length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5725 | ret = OK; |
| 5726 | #endif |
| 5727 | |
| 5728 | return ret; |
| 5729 | } |
| 5730 | |
| 5731 | /* |
| 5732 | * Get the completion pattern, column and length. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5733 | * "startcol" - start column number of the completion pattern/text |
| 5734 | * "cur_col" - current cursor column |
| 5735 | * On return, "line_invalid" is set to TRUE, if the current line may have |
| 5736 | * become invalid and needs to be fetched again. |
| 5737 | * Returns OK on success. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5738 | */ |
| 5739 | static int |
| 5740 | compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid) |
| 5741 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5742 | if (ctrl_x_mode_normal() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5743 | || (ctrl_x_mode & CTRL_X_WANT_IDENT |
| 5744 | && !thesaurus_func_complete(ctrl_x_mode))) |
| 5745 | { |
| 5746 | return get_normal_compl_info(line, startcol, curs_col); |
| 5747 | } |
| 5748 | else if (ctrl_x_mode_line_or_eval()) |
| 5749 | { |
| 5750 | return get_wholeline_compl_info(line, curs_col); |
| 5751 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5752 | else if (ctrl_x_mode_files()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5753 | { |
| 5754 | return get_filename_compl_info(line, startcol, curs_col); |
| 5755 | } |
| 5756 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 5757 | { |
| 5758 | return get_cmdline_compl_info(line, curs_col); |
| 5759 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5760 | else if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5761 | || thesaurus_func_complete(ctrl_x_mode)) |
| 5762 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 5763 | if (get_userdefined_compl_info(curs_col, NULL, NULL) != OK) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5764 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5765 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5766 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5767 | else if (ctrl_x_mode_spell()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5768 | { |
| 5769 | if (get_spell_compl_info(startcol, curs_col) == FAIL) |
| 5770 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5771 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5772 | } |
| 5773 | else |
| 5774 | { |
| 5775 | internal_error("ins_complete()"); |
| 5776 | return FAIL; |
| 5777 | } |
| 5778 | |
| 5779 | return OK; |
| 5780 | } |
| 5781 | |
| 5782 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5783 | * Continue an interrupted completion mode search in "line". |
| 5784 | * |
| 5785 | * If this same ctrl_x_mode has been interrupted use the text from |
| 5786 | * "compl_startpos" to the cursor as a pattern to add a new word instead of |
| 5787 | * expand the one before the cursor, in word-wise if "compl_startpos" is not in |
| 5788 | * the same line as the cursor then fix it (the line has been split because it |
| 5789 | * was longer than 'tw'). if SOL is set then skip the previous pattern, a word |
| 5790 | * at the beginning of the line has been inserted, we'll look for that. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5791 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5792 | static void |
| 5793 | ins_compl_continue_search(char_u *line) |
| 5794 | { |
| 5795 | // it is a continued search |
| 5796 | compl_cont_status &= ~CONT_INTRPT; // remove INTRPT |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5797 | if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns() |
| 5798 | || ctrl_x_mode_path_defines()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5799 | { |
| 5800 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
| 5801 | { |
| 5802 | // line (probably) wrapped, set compl_startpos to the |
| 5803 | // first non_blank in the line, if it is not a wordchar |
| 5804 | // include it to get a better pattern, but then we don't |
| 5805 | // want the "\\<" prefix, check it below |
| 5806 | compl_col = (colnr_T)getwhitecols(line); |
| 5807 | compl_startpos.col = compl_col; |
| 5808 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5809 | compl_cont_status &= ~CONT_SOL; // clear SOL if present |
| 5810 | } |
| 5811 | else |
| 5812 | { |
| 5813 | // S_IPOS was set when we inserted a word that was at the |
| 5814 | // beginning of the line, which means that we'll go to SOL |
| 5815 | // mode but first we need to redefine compl_startpos |
| 5816 | if (compl_cont_status & CONT_S_IPOS) |
| 5817 | { |
| 5818 | compl_cont_status |= CONT_SOL; |
| 5819 | compl_startpos.col = (colnr_T)(skipwhite( |
| 5820 | line + compl_length |
| 5821 | + compl_startpos.col) - line); |
| 5822 | } |
| 5823 | compl_col = compl_startpos.col; |
| 5824 | } |
| 5825 | compl_length = curwin->w_cursor.col - (int)compl_col; |
| 5826 | // IObuff is used to add a "word from the next line" would we |
| 5827 | // have enough space? just being paranoid |
| 5828 | #define MIN_SPACE 75 |
| 5829 | if (compl_length > (IOSIZE - MIN_SPACE)) |
| 5830 | { |
| 5831 | compl_cont_status &= ~CONT_SOL; |
| 5832 | compl_length = (IOSIZE - MIN_SPACE); |
| 5833 | compl_col = curwin->w_cursor.col - compl_length; |
| 5834 | } |
| 5835 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 5836 | if (compl_length < 1) |
| 5837 | compl_cont_status &= CONT_LOCAL; |
| 5838 | } |
| 5839 | else if (ctrl_x_mode_line_or_eval()) |
| 5840 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
| 5841 | else |
| 5842 | compl_cont_status = 0; |
| 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * start insert mode completion |
| 5847 | */ |
| 5848 | static int |
| 5849 | ins_compl_start(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5850 | { |
| 5851 | char_u *line; |
| 5852 | int startcol = 0; // column where searched text starts |
| 5853 | colnr_T curs_col; // cursor column |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5854 | int line_invalid = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5855 | int save_did_ai = did_ai; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 5856 | int flags = CP_ORIGINAL_TEXT; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5857 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5858 | // First time we hit ^N or ^P (in a row, I mean) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5859 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5860 | did_ai = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5861 | did_si = FALSE; |
| 5862 | can_si = FALSE; |
| 5863 | can_si_back = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5864 | if (stop_arrow() == FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5865 | return FAIL; |
| 5866 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5867 | line = ml_get(curwin->w_cursor.lnum); |
| 5868 | curs_col = curwin->w_cursor.col; |
| 5869 | compl_pending = 0; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5870 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5871 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5872 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 5873 | && compl_cont_mode == ctrl_x_mode) |
| 5874 | // this same ctrl-x_mode was interrupted previously. Continue the |
| 5875 | // completion. |
| 5876 | ins_compl_continue_search(line); |
| 5877 | else |
| 5878 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5879 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5880 | if (!compl_status_adding()) // normal expansion |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5881 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5882 | compl_cont_mode = ctrl_x_mode; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5883 | if (ctrl_x_mode_not_default()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5884 | // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL |
| 5885 | compl_cont_status = 0; |
| 5886 | compl_cont_status |= CONT_N_ADDS; |
| 5887 | compl_startpos = curwin->w_cursor; |
| 5888 | startcol = (int)curs_col; |
| 5889 | compl_col = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5890 | } |
| 5891 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5892 | // Work out completion pattern and original text -- webb |
| 5893 | if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL) |
| 5894 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5895 | if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
| 5896 | || thesaurus_func_complete(ctrl_x_mode)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5897 | // restore did_ai, so that adding comment leader works |
| 5898 | did_ai = save_did_ai; |
| 5899 | return FAIL; |
| 5900 | } |
| 5901 | // If "line" was changed while getting completion info get it again. |
| 5902 | if (line_invalid) |
| 5903 | line = ml_get(curwin->w_cursor.lnum); |
| 5904 | |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5905 | int in_fuzzy = get_cot_flags() & COT_FUZZY; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5906 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5907 | { |
| 5908 | edit_submode_pre = (char_u *)_(" Adding"); |
| 5909 | if (ctrl_x_mode_line_or_eval()) |
| 5910 | { |
| 5911 | // Insert a new line, keep indentation but ignore 'comments'. |
| 5912 | char_u *old = curbuf->b_p_com; |
| 5913 | |
| 5914 | curbuf->b_p_com = (char_u *)""; |
| 5915 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5916 | compl_startpos.col = compl_col; |
| 5917 | ins_eol('\r'); |
| 5918 | curbuf->b_p_com = old; |
| 5919 | compl_length = 0; |
| 5920 | compl_col = curwin->w_cursor.col; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5921 | compl_lnum = curwin->w_cursor.lnum; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5922 | } |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5923 | else if (ctrl_x_mode_normal() && in_fuzzy) |
| 5924 | { |
| 5925 | compl_startpos = curwin->w_cursor; |
| 5926 | compl_cont_status &= CONT_S_IPOS; |
| 5927 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5928 | } |
| 5929 | else |
| 5930 | { |
| 5931 | edit_submode_pre = NULL; |
| 5932 | compl_startpos.col = compl_col; |
| 5933 | } |
| 5934 | |
| 5935 | if (compl_cont_status & CONT_LOCAL) |
| 5936 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
| 5937 | else |
| 5938 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 5939 | |
| 5940 | // If any of the original typed text has been changed we need to fix |
| 5941 | // the redo buffer. |
| 5942 | ins_compl_fixRedoBufForLeader(NULL); |
| 5943 | |
| 5944 | // Always add completion for the original text. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5945 | VIM_CLEAR_STRING(compl_orig_text); |
| 5946 | compl_orig_text.length = (size_t)compl_length; |
| 5947 | compl_orig_text.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5948 | if (p_ic) |
| 5949 | flags |= CP_ICASE; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 5950 | if (compl_orig_text.string == NULL |
| 5951 | || ins_compl_add(compl_orig_text.string, |
| 5952 | (int)compl_orig_text.length, |
| 5953 | NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5954 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5955 | VIM_CLEAR_STRING(compl_pattern); |
| 5956 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5957 | return FAIL; |
| 5958 | } |
| 5959 | |
| 5960 | // showmode might reset the internal line pointers, so it must |
| 5961 | // be called before line = ml_get(), or when this address is no |
| 5962 | // longer needed. -- Acevedo. |
| 5963 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 5964 | edit_submode_highl = HLF_COUNT; |
| 5965 | showmode(); |
| 5966 | edit_submode_extra = NULL; |
| 5967 | out_flush(); |
| 5968 | |
| 5969 | return OK; |
| 5970 | } |
| 5971 | |
| 5972 | /* |
| 5973 | * display the completion status message |
| 5974 | */ |
| 5975 | static void |
| 5976 | ins_compl_show_statusmsg(void) |
| 5977 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5978 | // we found no match if the list has only the "compl_orig_text"-entry |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5979 | if (is_first_match(compl_first_match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5980 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5981 | edit_submode_extra = compl_status_adding() && compl_length > 1 |
Bram Moolenaar | b53a190 | 2022-11-15 13:57:38 +0000 | [diff] [blame] | 5982 | ? (char_u *)_("Hit end of paragraph") |
| 5983 | : (char_u *)_("Pattern not found"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5984 | edit_submode_highl = HLF_E; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5987 | if (edit_submode_extra == NULL) |
| 5988 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5989 | if (match_at_original_text(compl_curr_match)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5990 | { |
| 5991 | edit_submode_extra = (char_u *)_("Back at original"); |
| 5992 | edit_submode_highl = HLF_W; |
| 5993 | } |
| 5994 | else if (compl_cont_status & CONT_S_IPOS) |
| 5995 | { |
| 5996 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 5997 | edit_submode_highl = HLF_COUNT; |
| 5998 | } |
| 5999 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
| 6000 | { |
| 6001 | edit_submode_extra = (char_u *)_("The only match"); |
| 6002 | edit_submode_highl = HLF_COUNT; |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6003 | compl_curr_match->cp_number = 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6004 | } |
| 6005 | else |
| 6006 | { |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6007 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6008 | // Update completion sequence number when needed. |
| 6009 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6010 | ins_compl_update_sequence_numbers(); |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6011 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6012 | // The match should always have a sequence number now, this is |
| 6013 | // just a safety check. |
| 6014 | if (compl_curr_match->cp_number != -1) |
| 6015 | { |
| 6016 | // Space for 10 text chars. + 2x10-digit no.s = 31. |
| 6017 | // Translations may need more than twice that. |
| 6018 | static char_u match_ref[81]; |
| 6019 | |
| 6020 | if (compl_matches > 0) |
| 6021 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6022 | _("match %d of %d"), |
| 6023 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6024 | else |
| 6025 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6026 | _("match %d"), |
| 6027 | compl_curr_match->cp_number); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6028 | edit_submode_extra = match_ref; |
| 6029 | edit_submode_highl = HLF_R; |
| 6030 | if (dollar_vcol >= 0) |
| 6031 | curs_columns(FALSE); |
| 6032 | } |
| 6033 | } |
| 6034 | } |
| 6035 | |
| 6036 | // Show a message about what (completion) mode we're in. |
| 6037 | if (!compl_opt_suppress_empty) |
| 6038 | { |
| 6039 | showmode(); |
| 6040 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 6041 | { |
| 6042 | if (edit_submode_extra != NULL) |
| 6043 | { |
| 6044 | if (!p_smd) |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6045 | { |
| 6046 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6047 | msg_attr((char *)edit_submode_extra, |
| 6048 | edit_submode_highl < HLF_COUNT |
| 6049 | ? HL_ATTR(edit_submode_highl) : 0); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6050 | msg_hist_off = FALSE; |
| 6051 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6052 | } |
| 6053 | else |
| 6054 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 6055 | } |
| 6056 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | /* |
| 6060 | * Do Insert mode completion. |
| 6061 | * Called when character "c" was typed, which has a meaning for completion. |
| 6062 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 6063 | */ |
| 6064 | int |
| 6065 | ins_complete(int c, int enable_pum) |
| 6066 | { |
| 6067 | int n; |
| 6068 | int save_w_wrow; |
| 6069 | int save_w_leftcol; |
| 6070 | int insert_match; |
| 6071 | |
| 6072 | compl_direction = ins_compl_key2dir(c); |
| 6073 | insert_match = ins_compl_use_match(c); |
| 6074 | |
| 6075 | if (!compl_started) |
| 6076 | { |
| 6077 | if (ins_compl_start() == FAIL) |
| 6078 | return FAIL; |
| 6079 | } |
| 6080 | else if (insert_match && stop_arrow() == FAIL) |
| 6081 | return FAIL; |
| 6082 | |
| 6083 | compl_shown_match = compl_curr_match; |
| 6084 | compl_shows_dir = compl_direction; |
| 6085 | |
| 6086 | // Find next match (and following matches). |
| 6087 | save_w_wrow = curwin->w_wrow; |
| 6088 | save_w_leftcol = curwin->w_leftcol; |
| 6089 | n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); |
| 6090 | |
| 6091 | // may undisplay the popup menu |
| 6092 | ins_compl_upd_pum(); |
| 6093 | |
| 6094 | if (n > 1) // all matches have been found |
| 6095 | compl_matches = n; |
| 6096 | compl_curr_match = compl_shown_match; |
| 6097 | compl_direction = compl_shows_dir; |
| 6098 | |
| 6099 | // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 6100 | // mode. |
| 6101 | if (got_int && !global_busy) |
| 6102 | { |
| 6103 | (void)vgetc(); |
| 6104 | got_int = FALSE; |
| 6105 | } |
| 6106 | |
| 6107 | // we found no match if the list has only the "compl_orig_text"-entry |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6108 | if (is_first_match(compl_first_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6109 | { |
| 6110 | // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 6111 | // because we couldn't expand anything at first place, but if we used |
| 6112 | // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 6113 | // (such as M in M'exico) if not tried already. -- Acevedo |
| 6114 | if (compl_length > 1 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6115 | || compl_status_adding() |
| 6116 | || (ctrl_x_mode_not_default() |
| 6117 | && !ctrl_x_mode_path_patterns() |
| 6118 | && !ctrl_x_mode_path_defines())) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6119 | compl_cont_status &= ~CONT_N_ADDS; |
| 6120 | } |
| 6121 | |
| 6122 | if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) |
| 6123 | compl_cont_status |= CONT_S_IPOS; |
| 6124 | else |
| 6125 | compl_cont_status &= ~CONT_S_IPOS; |
| 6126 | |
| 6127 | ins_compl_show_statusmsg(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6128 | |
| 6129 | // Show the popup menu, unless we got interrupted. |
| 6130 | if (enable_pum && !compl_interrupted) |
| 6131 | show_pum(save_w_wrow, save_w_leftcol); |
| 6132 | |
| 6133 | compl_was_interrupted = compl_interrupted; |
| 6134 | compl_interrupted = FALSE; |
| 6135 | |
| 6136 | return OK; |
| 6137 | } |
| 6138 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 6139 | /* |
| 6140 | * Remove (if needed) and show the popup menu |
| 6141 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6142 | static void |
| 6143 | show_pum(int prev_w_wrow, int prev_w_leftcol) |
| 6144 | { |
| 6145 | // RedrawingDisabled may be set when invoked through complete(). |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6146 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6147 | RedrawingDisabled = 0; |
| 6148 | |
| 6149 | // If the cursor moved or the display scrolled we need to remove the pum |
| 6150 | // first. |
| 6151 | setcursor(); |
| 6152 | if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) |
| 6153 | ins_compl_del_pum(); |
| 6154 | |
| 6155 | ins_compl_show_pum(); |
| 6156 | setcursor(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6157 | |
| 6158 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6159 | } |
| 6160 | |
| 6161 | /* |
| 6162 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 6163 | * If dest is not NULL the chars. are copied there quoting (with |
| 6164 | * a backslash) the metachars, and dest would be NUL terminated. |
| 6165 | * Returns the length (needed) of dest |
| 6166 | */ |
| 6167 | static unsigned |
| 6168 | quote_meta(char_u *dest, char_u *src, int len) |
| 6169 | { |
| 6170 | unsigned m = (unsigned)len + 1; // one extra for the NUL |
| 6171 | |
| 6172 | for ( ; --len >= 0; src++) |
| 6173 | { |
| 6174 | switch (*src) |
| 6175 | { |
| 6176 | case '.': |
| 6177 | case '*': |
| 6178 | case '[': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6179 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6180 | break; |
| 6181 | // FALLTHROUGH |
| 6182 | case '~': |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 6183 | if (!magic_isset()) // quote these only if magic is set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6184 | break; |
| 6185 | // FALLTHROUGH |
| 6186 | case '\\': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6187 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6188 | break; |
| 6189 | // FALLTHROUGH |
| 6190 | case '^': // currently it's not needed. |
| 6191 | case '$': |
| 6192 | m++; |
| 6193 | if (dest != NULL) |
| 6194 | *dest++ = '\\'; |
| 6195 | break; |
| 6196 | } |
| 6197 | if (dest != NULL) |
| 6198 | *dest++ = *src; |
| 6199 | // Copy remaining bytes of a multibyte character. |
| 6200 | if (has_mbyte) |
| 6201 | { |
| 6202 | int i, mb_len; |
| 6203 | |
| 6204 | mb_len = (*mb_ptr2len)(src) - 1; |
| 6205 | if (mb_len > 0 && len >= mb_len) |
| 6206 | for (i = 0; i < mb_len; ++i) |
| 6207 | { |
| 6208 | --len; |
| 6209 | ++src; |
| 6210 | if (dest != NULL) |
| 6211 | *dest++ = *src; |
| 6212 | } |
| 6213 | } |
| 6214 | } |
| 6215 | if (dest != NULL) |
| 6216 | *dest = NUL; |
| 6217 | |
| 6218 | return m; |
| 6219 | } |
| 6220 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6221 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6222 | void |
| 6223 | free_insexpand_stuff(void) |
| 6224 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6225 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 6226 | # ifdef FEAT_EVAL |
| 6227 | free_callback(&cfu_cb); |
| 6228 | free_callback(&ofu_cb); |
| 6229 | free_callback(&tsrfu_cb); |
| 6230 | # endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6231 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6232 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6233 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6234 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6235 | /* |
| 6236 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6237 | * spelled word, if there is one. |
| 6238 | */ |
| 6239 | static void |
| 6240 | spell_back_to_badword(void) |
| 6241 | { |
| 6242 | pos_T tpos = curwin->w_cursor; |
| 6243 | |
Christ van Willegen - van Noort | 8e4c4c7 | 2024-05-17 18:49:27 +0200 | [diff] [blame] | 6244 | spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6245 | if (curwin->w_cursor.col != tpos.col) |
| 6246 | start_arrow(&tpos); |
| 6247 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6248 | #endif |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame^] | 6249 | |
| 6250 | /* |
| 6251 | * Reset the info associated with completion sources. |
| 6252 | */ |
| 6253 | static void |
| 6254 | cpt_compl_src_clear(void) |
| 6255 | { |
| 6256 | VIM_CLEAR(cpt_func_refresh_always); |
| 6257 | cpt_value_idx = -1; |
| 6258 | cpt_value_count = 0; |
| 6259 | } |
| 6260 | |
| 6261 | /* |
| 6262 | * Initialize the info associated with completion sources. |
| 6263 | */ |
| 6264 | static int |
| 6265 | cpt_compl_src_init(char_u *cpt_str) |
| 6266 | { |
| 6267 | int count = 0; |
| 6268 | char_u *p = cpt_str; |
| 6269 | |
| 6270 | while (*p) |
| 6271 | { |
| 6272 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6273 | p++; |
| 6274 | if (*p) // If not end of string, count this segment |
| 6275 | { |
| 6276 | count++; |
| 6277 | copy_option_part(&p, IObuff, IOSIZE, ","); // Advance p |
| 6278 | } |
| 6279 | } |
| 6280 | cpt_compl_src_clear(); |
| 6281 | cpt_value_count = count; |
| 6282 | if (count > 0) |
| 6283 | { |
| 6284 | cpt_func_refresh_always = ALLOC_CLEAR_MULT(int, count); |
| 6285 | if (cpt_func_refresh_always == NULL) |
| 6286 | { |
| 6287 | cpt_value_count = 0; |
| 6288 | return FAIL; |
| 6289 | } |
| 6290 | } |
| 6291 | return OK; |
| 6292 | } |
| 6293 | |
| 6294 | /* |
| 6295 | * Return TRUE if any of the completion sources have 'refresh' set to 'always'. |
| 6296 | */ |
| 6297 | static int |
| 6298 | is_cpt_func_refresh_always(void) |
| 6299 | { |
| 6300 | #ifdef FEAT_COMPL_FUNC |
| 6301 | int i; |
| 6302 | |
| 6303 | for (i = 0; i < cpt_value_count; i++) |
| 6304 | if (cpt_func_refresh_always[i]) |
| 6305 | return TRUE; |
| 6306 | #endif |
| 6307 | return FALSE; |
| 6308 | } |
| 6309 | |
| 6310 | /* |
| 6311 | * Make the completion list non-cyclic. |
| 6312 | */ |
| 6313 | #ifdef FEAT_COMPL_FUNC |
| 6314 | static void |
| 6315 | ins_compl_make_linear(void) |
| 6316 | { |
| 6317 | compl_T *m; |
| 6318 | |
| 6319 | if (compl_first_match == NULL || compl_first_match->cp_prev == NULL) |
| 6320 | return; |
| 6321 | m = compl_first_match->cp_prev; |
| 6322 | m->cp_next = NULL; |
| 6323 | compl_first_match->cp_prev = NULL; |
| 6324 | } |
| 6325 | #endif |
| 6326 | |
| 6327 | /* |
| 6328 | * Remove the matches linked to the current completion source (as indicated by |
| 6329 | * cpt_value_idx) from the completion list. |
| 6330 | */ |
| 6331 | #ifdef FEAT_COMPL_FUNC |
| 6332 | static compl_T * |
| 6333 | remove_old_matches(void) |
| 6334 | { |
| 6335 | compl_T *sublist_start = NULL, *sublist_end = NULL, *insert_at = NULL; |
| 6336 | compl_T *current, *next; |
| 6337 | int compl_shown_removed = FALSE; |
| 6338 | int forward = compl_dir_forward(); |
| 6339 | |
| 6340 | // Identify the sublist of old matches that needs removal |
| 6341 | for (current = compl_first_match; current != NULL; current = current->cp_next) |
| 6342 | { |
| 6343 | if (current->cp_cpt_value_idx < cpt_value_idx && (forward || (!forward && !insert_at))) |
| 6344 | insert_at = current; |
| 6345 | |
| 6346 | if (current->cp_cpt_value_idx == cpt_value_idx) |
| 6347 | { |
| 6348 | if (!sublist_start) |
| 6349 | sublist_start = current; |
| 6350 | sublist_end = current; |
| 6351 | if (!compl_shown_removed && compl_shown_match == current) |
| 6352 | compl_shown_removed = TRUE; |
| 6353 | } |
| 6354 | |
| 6355 | if ((forward && current->cp_cpt_value_idx > cpt_value_idx) || (!forward && insert_at)) |
| 6356 | break; |
| 6357 | } |
| 6358 | |
| 6359 | // Re-assign compl_shown_match if necessary |
| 6360 | if (compl_shown_removed) |
| 6361 | { |
| 6362 | if (forward) |
| 6363 | compl_shown_match = compl_first_match; |
| 6364 | else |
| 6365 | { // Last node will have the prefix that is being completed |
| 6366 | for (current = compl_first_match; current->cp_next != NULL; current = current->cp_next) |
| 6367 | ; |
| 6368 | compl_shown_match = current; |
| 6369 | } |
| 6370 | } |
| 6371 | |
| 6372 | if (!sublist_start) // No nodes to remove |
| 6373 | return insert_at; |
| 6374 | |
| 6375 | // Update links to remove sublist |
| 6376 | if (sublist_start->cp_prev) |
| 6377 | sublist_start->cp_prev->cp_next = sublist_end->cp_next; |
| 6378 | else |
| 6379 | compl_first_match = sublist_end->cp_next; |
| 6380 | |
| 6381 | if (sublist_end->cp_next) |
| 6382 | sublist_end->cp_next->cp_prev = sublist_start->cp_prev; |
| 6383 | |
| 6384 | // Free all nodes in the sublist |
| 6385 | sublist_end->cp_next = NULL; |
| 6386 | for (current = sublist_start; current != NULL; current = next) |
| 6387 | { |
| 6388 | next = current->cp_next; |
| 6389 | ins_compl_item_free(current); |
| 6390 | } |
| 6391 | |
| 6392 | return insert_at; |
| 6393 | } |
| 6394 | #endif |
| 6395 | |
| 6396 | /* |
| 6397 | * Retrieve completion matches using the callback function "cb" and store the |
| 6398 | * 'refresh:always' flag. |
| 6399 | */ |
| 6400 | #ifdef FEAT_COMPL_FUNC |
| 6401 | static void |
| 6402 | get_cpt_func_completion_matches(callback_T *cb UNUSED) |
| 6403 | { |
| 6404 | int ret; |
| 6405 | int startcol; |
| 6406 | |
| 6407 | VIM_CLEAR_STRING(cpt_compl_pattern); |
| 6408 | ret = get_userdefined_compl_info(curwin->w_cursor.col, cb, &startcol); |
| 6409 | if (ret == FAIL && startcol == -3) |
| 6410 | cpt_func_refresh_always[cpt_value_idx] = FALSE; |
| 6411 | else if (ret == OK) |
| 6412 | { |
| 6413 | expand_by_function(0, cpt_compl_pattern.string, cb); |
| 6414 | cpt_func_refresh_always[cpt_value_idx] = compl_opt_refresh_always; |
| 6415 | compl_opt_refresh_always = FALSE; |
| 6416 | } |
| 6417 | } |
| 6418 | #endif |
| 6419 | |
| 6420 | /* |
| 6421 | * Retrieve completion matches from functions in the 'cpt' option where the |
| 6422 | * 'refresh:always' flag is set. |
| 6423 | */ |
| 6424 | static void |
| 6425 | cpt_compl_refresh(void) |
| 6426 | { |
| 6427 | #ifdef FEAT_COMPL_FUNC |
| 6428 | char_u *cpt; |
| 6429 | char_u *p; |
| 6430 | callback_T *cb; |
| 6431 | |
| 6432 | // Make the completion list linear (non-cyclic) |
| 6433 | ins_compl_make_linear(); |
| 6434 | // Make a copy of 'cpt' in case the buffer gets wiped out |
| 6435 | cpt = vim_strsave(curbuf->b_p_cpt); |
| 6436 | |
| 6437 | cpt_value_idx = 0; |
| 6438 | for (p = cpt; *p; cpt_value_idx++) |
| 6439 | { |
| 6440 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6441 | p++; |
| 6442 | |
| 6443 | if (cpt_func_refresh_always[cpt_value_idx]) |
| 6444 | { |
| 6445 | if (*p == 'o') |
| 6446 | cb = &curbuf->b_ofu_cb; |
| 6447 | else if (*p == 'f') |
| 6448 | cb = (*(p + 1) != ',' && *(p + 1) != NUL) |
| 6449 | ? get_cpt_func_callback(p + 1) : &curbuf->b_cfu_cb; |
| 6450 | if (cb) |
| 6451 | { |
| 6452 | compl_curr_match = remove_old_matches(); |
| 6453 | get_cpt_func_completion_matches(cb); |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | copy_option_part(&p, IObuff, IOSIZE, ","); // Advance p |
| 6458 | } |
| 6459 | cpt_value_idx = -1; |
| 6460 | |
| 6461 | vim_free(cpt); |
| 6462 | // Make the list cyclic |
| 6463 | compl_matches = ins_compl_make_cyclic(); |
| 6464 | #endif |
| 6465 | } |