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[] = { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 69 | "keyword", |
| 70 | "ctrl_x", |
| 71 | "scroll", |
| 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" |
| 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 |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 108 | int cp_score; // fuzzy match score or proximity 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 | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 112 | int cp_cpt_source_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 | |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 195 | static win_T *compl_curr_win = NULL; // win where completion is active |
| 196 | static buf_T *compl_curr_buf = NULL; // buf where completion is active |
| 197 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 198 | // List of flags for method of completion. |
| 199 | static int compl_cont_status = 0; |
| 200 | # define CONT_ADDING 1 // "normal" or "adding" expansion |
| 201 | # define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion |
| 202 | // it's set only iff N_ADDS is set |
| 203 | # define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current |
| 204 | # define CONT_S_IPOS 8 // next ^X<> will set initial_pos? |
| 205 | // if so, word-wise-expansion will set SOL |
| 206 | # define CONT_SOL 16 // pattern includes start of line, just for |
| 207 | // word-wise expansion, not set for ^X^L |
| 208 | # define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local |
| 209 | // expansion, (eg use complete=.) |
| 210 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 211 | static int compl_opt_refresh_always = FALSE; |
| 212 | static int compl_opt_suppress_empty = FALSE; |
| 213 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 214 | static int compl_selected_item = -1; |
| 215 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 216 | static int *compl_fuzzy_scores; |
| 217 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 218 | // Define the structure for completion source (in 'cpt' option) information |
| 219 | typedef struct cpt_source_T |
| 220 | { |
| 221 | int refresh_always; // Flag array to indicate which 'cpt' functions have 'refresh:always' set |
| 222 | int max_matches; // Maximum number of items to display in the menu from the source |
| 223 | } cpt_source_T; |
| 224 | |
| 225 | static cpt_source_T *cpt_sources_array; // Pointer to the array of completion sources |
| 226 | static int cpt_sources_count; // Total number of completion sources specified in the 'cpt' option |
| 227 | static int cpt_sources_index; // Index of the current completion source being expanded |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 228 | |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 229 | // "compl_match_array" points the currently displayed list of entries in the |
| 230 | // popup menu. It is NULL when there is no popup menu. |
| 231 | static pumitem_T *compl_match_array = NULL; |
| 232 | static int compl_match_arraysize; |
| 233 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 234 | 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] | 235 | static void ins_compl_longest_match(compl_T *match); |
| 236 | static void ins_compl_del_pum(void); |
| 237 | 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] | 238 | static void ins_compl_free(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 239 | static int ins_compl_need_restart(void); |
| 240 | static void ins_compl_new_leader(void); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 241 | static int get_compl_len(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 242 | static void ins_compl_restart(void); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 243 | 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] | 244 | static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg); |
| 245 | # if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 246 | static void ins_compl_add_list(list_T *list); |
| 247 | static void ins_compl_add_dict(dict_T *dict); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 248 | static int get_userdefined_compl_info(colnr_T curs_col, callback_T *cb, int *startcol); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 249 | static void get_cpt_func_completion_matches(callback_T *cb); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 250 | static callback_T *get_cpt_func_callback(char_u *funcname); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 251 | # endif |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 252 | static int cpt_sources_init(void); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 253 | static int is_cpt_func_refresh_always(void); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 254 | static void cpt_sources_clear(void); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 255 | static void cpt_compl_refresh(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 256 | static int ins_compl_key2dir(int c); |
| 257 | static int ins_compl_pum_key(int c); |
| 258 | static int ins_compl_key2count(int c); |
| 259 | static void show_pum(int prev_w_wrow, int prev_w_leftcol); |
| 260 | static unsigned quote_meta(char_u *dest, char_u *str, int len); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 261 | static int ins_compl_has_multiple(void); |
| 262 | static void ins_compl_expand_multiple(char_u *str); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 263 | static void ins_compl_longest_insert(char_u *prefix); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 264 | |
| 265 | #ifdef FEAT_SPELL |
| 266 | static void spell_back_to_badword(void); |
| 267 | static int spell_bad_len = 0; // length of located bad word |
| 268 | #endif |
| 269 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 270 | /* |
| 271 | * CTRL-X pressed in Insert mode. |
| 272 | */ |
| 273 | void |
| 274 | ins_ctrl_x(void) |
| 275 | { |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 276 | if (!ctrl_x_mode_cmdline()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 277 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 278 | // 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] | 279 | if (compl_cont_status & CONT_N_ADDS) |
| 280 | compl_cont_status |= CONT_INTRPT; |
| 281 | else |
| 282 | compl_cont_status = 0; |
| 283 | // We're not sure which CTRL-X mode it will be yet |
| 284 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 285 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 286 | edit_submode_pre = NULL; |
| 287 | showmode(); |
| 288 | } |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 289 | else |
| 290 | // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X |
| 291 | // CTRL-V look like CTRL-N |
| 292 | 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] | 293 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 294 | may_trigger_modechanged(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Functions to check the current CTRL-X mode. |
| 299 | */ |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 300 | int ctrl_x_mode_none(void) |
| 301 | { return ctrl_x_mode == 0; } |
| 302 | int ctrl_x_mode_normal(void) |
| 303 | { return ctrl_x_mode == CTRL_X_NORMAL; } |
| 304 | int ctrl_x_mode_scroll(void) |
| 305 | { return ctrl_x_mode == CTRL_X_SCROLL; } |
| 306 | int ctrl_x_mode_whole_line(void) |
| 307 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE; } |
| 308 | int ctrl_x_mode_files(void) |
| 309 | { return ctrl_x_mode == CTRL_X_FILES; } |
| 310 | int ctrl_x_mode_tags(void) |
| 311 | { return ctrl_x_mode == CTRL_X_TAGS; } |
| 312 | int ctrl_x_mode_path_patterns(void) |
| 313 | { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; } |
| 314 | int ctrl_x_mode_path_defines(void) |
| 315 | { return ctrl_x_mode == CTRL_X_PATH_DEFINES; } |
| 316 | int ctrl_x_mode_dictionary(void) |
| 317 | { return ctrl_x_mode == CTRL_X_DICTIONARY; } |
| 318 | int ctrl_x_mode_thesaurus(void) |
| 319 | { return ctrl_x_mode == CTRL_X_THESAURUS; } |
| 320 | int ctrl_x_mode_cmdline(void) |
| 321 | { return ctrl_x_mode == CTRL_X_CMDLINE |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 322 | || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; } |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 323 | int ctrl_x_mode_function(void) |
| 324 | { return ctrl_x_mode == CTRL_X_FUNCTION; } |
| 325 | int ctrl_x_mode_omni(void) |
| 326 | { return ctrl_x_mode == CTRL_X_OMNI; } |
| 327 | int ctrl_x_mode_spell(void) |
| 328 | { return ctrl_x_mode == CTRL_X_SPELL; } |
| 329 | static int ctrl_x_mode_eval(void) |
| 330 | { return ctrl_x_mode == CTRL_X_EVAL; } |
| 331 | int ctrl_x_mode_line_or_eval(void) |
| 332 | { 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] | 333 | |
| 334 | /* |
| 335 | * Whether other than default completion has been selected. |
| 336 | */ |
| 337 | int |
| 338 | ctrl_x_mode_not_default(void) |
| 339 | { |
| 340 | return ctrl_x_mode != CTRL_X_NORMAL; |
| 341 | } |
| 342 | |
| 343 | /* |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 344 | * Whether CTRL-X was typed without a following character, |
| 345 | * not including when in CTRL-X CTRL-V mode. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 346 | */ |
| 347 | int |
| 348 | ctrl_x_mode_not_defined_yet(void) |
| 349 | { |
| 350 | return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; |
| 351 | } |
| 352 | |
| 353 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 354 | * Return TRUE if currently in "normal" or "adding" insert completion matches |
| 355 | * state |
| 356 | */ |
| 357 | int |
| 358 | compl_status_adding(void) |
| 359 | { |
| 360 | return compl_cont_status & CONT_ADDING; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Return TRUE if the completion pattern includes start of line, just for |
| 365 | * word-wise expansion. |
| 366 | */ |
| 367 | int |
| 368 | compl_status_sol(void) |
| 369 | { |
| 370 | return compl_cont_status & CONT_SOL; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.) |
| 375 | */ |
| 376 | int |
| 377 | compl_status_local(void) |
| 378 | { |
| 379 | return compl_cont_status & CONT_LOCAL; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Clear the completion status flags |
| 384 | */ |
| 385 | void |
| 386 | compl_status_clear(void) |
| 387 | { |
| 388 | compl_cont_status = 0; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Return TRUE if completion is using the forward direction matches |
| 393 | */ |
| 394 | static int |
| 395 | compl_dir_forward(void) |
| 396 | { |
| 397 | return compl_direction == FORWARD; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Return TRUE if currently showing forward completion matches |
| 402 | */ |
| 403 | static int |
| 404 | compl_shows_dir_forward(void) |
| 405 | { |
| 406 | return compl_shows_dir == FORWARD; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Return TRUE if currently showing backward completion matches |
| 411 | */ |
| 412 | static int |
| 413 | compl_shows_dir_backward(void) |
| 414 | { |
| 415 | return compl_shows_dir == BACKWARD; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Return TRUE if the 'dictionary' or 'thesaurus' option can be used. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 420 | */ |
| 421 | int |
| 422 | has_compl_option(int dict_opt) |
| 423 | { |
| 424 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 425 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 426 | && !curwin->w_p_spell |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 427 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 428 | ) |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 429 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL |
| 430 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 431 | && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 432 | #endif |
| 433 | )) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 434 | { |
| 435 | ctrl_x_mode = CTRL_X_NORMAL; |
| 436 | edit_submode = NULL; |
| 437 | msg_attr(dict_opt ? _("'dictionary' option is empty") |
| 438 | : _("'thesaurus' option is empty"), |
| 439 | HL_ATTR(HLF_E)); |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 440 | if (emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 441 | { |
| 442 | vim_beep(BO_COMPL); |
| 443 | setcursor(); |
| 444 | out_flush(); |
| 445 | #ifdef FEAT_EVAL |
| 446 | if (!get_vim_var_nr(VV_TESTING)) |
| 447 | #endif |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 448 | ui_delay(2004L, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 449 | } |
| 450 | return FALSE; |
| 451 | } |
| 452 | return TRUE; |
| 453 | } |
| 454 | |
| 455 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 456 | * 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] | 457 | * This depends on the current mode. |
| 458 | */ |
| 459 | int |
| 460 | vim_is_ctrl_x_key(int c) |
| 461 | { |
| 462 | // Always allow ^R - let its results then be checked |
| 463 | if (c == Ctrl_R) |
| 464 | return TRUE; |
| 465 | |
| 466 | // Accept <PageUp> and <PageDown> if the popup menu is visible. |
| 467 | if (ins_compl_pum_key(c)) |
| 468 | return TRUE; |
| 469 | |
| 470 | switch (ctrl_x_mode) |
| 471 | { |
| 472 | case 0: // Not in any CTRL-X mode |
| 473 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 474 | case CTRL_X_NOT_DEFINED_YET: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 475 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 476 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
| 477 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 478 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 479 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
| 480 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 481 | || c == Ctrl_S || c == Ctrl_K || c == 's' |
| 482 | || c == Ctrl_Z); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 483 | case CTRL_X_SCROLL: |
| 484 | return (c == Ctrl_Y || c == Ctrl_E); |
| 485 | case CTRL_X_WHOLE_LINE: |
| 486 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 487 | case CTRL_X_FILES: |
| 488 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 489 | case CTRL_X_DICTIONARY: |
| 490 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 491 | case CTRL_X_THESAURUS: |
| 492 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 493 | case CTRL_X_TAGS: |
| 494 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 495 | #ifdef FEAT_FIND_ID |
| 496 | case CTRL_X_PATH_PATTERNS: |
| 497 | return (c == Ctrl_P || c == Ctrl_N); |
| 498 | case CTRL_X_PATH_DEFINES: |
| 499 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 500 | #endif |
| 501 | case CTRL_X_CMDLINE: |
| 502 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 503 | || c == Ctrl_X); |
| 504 | #ifdef FEAT_COMPL_FUNC |
| 505 | case CTRL_X_FUNCTION: |
| 506 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
| 507 | case CTRL_X_OMNI: |
| 508 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
| 509 | #endif |
| 510 | case CTRL_X_SPELL: |
| 511 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
| 512 | case CTRL_X_EVAL: |
| 513 | return (c == Ctrl_P || c == Ctrl_N); |
| 514 | } |
| 515 | internal_error("vim_is_ctrl_x_key()"); |
| 516 | return FALSE; |
| 517 | } |
| 518 | |
| 519 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 520 | * Return TRUE if "match" is the original text when the completion began. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 521 | */ |
| 522 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 523 | match_at_original_text(compl_T *match) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 524 | { |
| 525 | return match->cp_flags & CP_ORIGINAL_TEXT; |
| 526 | } |
| 527 | |
| 528 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 529 | * Returns TRUE if "match" is the first match in the completion list. |
| 530 | */ |
| 531 | static int |
| 532 | is_first_match(compl_T *match) |
| 533 | { |
| 534 | return match == compl_first_match; |
| 535 | } |
| 536 | |
| 537 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 538 | * Return TRUE when character "c" is part of the item currently being |
| 539 | * completed. Used to decide whether to abandon complete mode when the menu |
| 540 | * is visible. |
| 541 | */ |
| 542 | int |
| 543 | ins_compl_accept_char(int c) |
| 544 | { |
| 545 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 546 | // When expanding an identifier only accept identifier chars. |
| 547 | return vim_isIDc(c); |
| 548 | |
| 549 | switch (ctrl_x_mode) |
| 550 | { |
| 551 | case CTRL_X_FILES: |
| 552 | // When expanding file name only accept file name chars. But not |
| 553 | // path separators, so that "proto/<Tab>" expands files in |
| 554 | // "proto", not "proto/" as a whole |
| 555 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 556 | |
| 557 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 558 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 559 | case CTRL_X_OMNI: |
| 560 | // Command line and Omni completion can work with just about any |
| 561 | // printable character, but do stop at white space. |
| 562 | return vim_isprintc(c) && !VIM_ISWHITE(c); |
| 563 | |
| 564 | case CTRL_X_WHOLE_LINE: |
| 565 | // For while line completion a space can be part of the line. |
| 566 | return vim_isprintc(c); |
| 567 | } |
| 568 | return vim_iswordc(c); |
| 569 | } |
| 570 | |
| 571 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 572 | * 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] | 573 | * If the result is in allocated memory "tofree" is set to it. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 574 | */ |
| 575 | static char_u * |
| 576 | ins_compl_infercase_gettext( |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 577 | char_u *str, |
| 578 | int char_len, |
| 579 | int compl_char_len, |
| 580 | int min_len, |
| 581 | char_u **tofree) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 582 | { |
| 583 | int *wca; // Wide character array. |
| 584 | char_u *p; |
| 585 | int i, c; |
| 586 | int has_lower = FALSE; |
| 587 | int was_letter = FALSE; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 588 | garray_T gap; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 589 | |
| 590 | IObuff[0] = NUL; |
| 591 | |
| 592 | // Allocate wide character array for the completion and fill it. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 593 | wca = ALLOC_MULT(int, char_len); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 594 | if (wca == NULL) |
| 595 | return IObuff; |
| 596 | |
| 597 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 598 | for (i = 0; i < char_len; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 599 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 600 | if (has_mbyte) |
| 601 | wca[i] = mb_ptr2char_adv(&p); |
| 602 | else |
| 603 | wca[i] = *(p++); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 604 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 605 | |
| 606 | // Rule 1: Were any chars converted to lower? |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 607 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 608 | for (i = 0; i < min_len; ++i) |
| 609 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 610 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 611 | if (MB_ISLOWER(c)) |
| 612 | { |
| 613 | has_lower = TRUE; |
| 614 | if (MB_ISUPPER(wca[i])) |
| 615 | { |
| 616 | // Rule 1 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 617 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 618 | wca[i] = MB_TOLOWER(wca[i]); |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // Rule 2: No lower case, 2nd consecutive letter converted to |
| 625 | // upper case. |
| 626 | if (!has_lower) |
| 627 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 628 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 629 | for (i = 0; i < min_len; ++i) |
| 630 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 631 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 632 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 633 | { |
| 634 | // Rule 2 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 635 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 636 | wca[i] = MB_TOUPPER(wca[i]); |
| 637 | break; |
| 638 | } |
| 639 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | // Copy the original case of the part we typed. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 644 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 645 | for (i = 0; i < min_len; ++i) |
| 646 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 647 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 648 | if (MB_ISLOWER(c)) |
| 649 | wca[i] = MB_TOLOWER(wca[i]); |
| 650 | else if (MB_ISUPPER(c)) |
| 651 | wca[i] = MB_TOUPPER(wca[i]); |
| 652 | } |
| 653 | |
| 654 | // Generate encoding specific output from wide character array. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 655 | p = IObuff; |
| 656 | i = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 657 | ga_init2(&gap, 1, 500); |
| 658 | while (i < char_len) |
| 659 | { |
| 660 | if (gap.ga_data != NULL) |
| 661 | { |
| 662 | if (ga_grow(&gap, 10) == FAIL) |
| 663 | { |
| 664 | ga_clear(&gap); |
| 665 | return (char_u *)"[failed]"; |
| 666 | } |
| 667 | p = (char_u *)gap.ga_data + gap.ga_len; |
| 668 | if (has_mbyte) |
| 669 | gap.ga_len += (*mb_char2bytes)(wca[i++], p); |
| 670 | else |
| 671 | { |
| 672 | *p = wca[i++]; |
| 673 | ++gap.ga_len; |
| 674 | } |
| 675 | } |
| 676 | else if ((p - IObuff) + 6 >= IOSIZE) |
| 677 | { |
| 678 | // Multi-byte characters can occupy up to five bytes more than |
| 679 | // ASCII characters, and we also need one byte for NUL, so when |
| 680 | // getting to six bytes from the edge of IObuff switch to using a |
| 681 | // growarray. Add the character in the next round. |
| 682 | if (ga_grow(&gap, IOSIZE) == FAIL) |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 683 | { |
| 684 | vim_free(wca); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 685 | return (char_u *)"[failed]"; |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 686 | } |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 687 | *p = NUL; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 688 | STRCPY(gap.ga_data, IObuff); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 689 | gap.ga_len = (int)(p - IObuff); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 690 | } |
| 691 | else if (has_mbyte) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 692 | p += (*mb_char2bytes)(wca[i++], p); |
| 693 | else |
| 694 | *(p++) = wca[i++]; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 695 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 696 | vim_free(wca); |
| 697 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 698 | if (gap.ga_data != NULL) |
| 699 | { |
| 700 | *tofree = gap.ga_data; |
| 701 | return gap.ga_data; |
| 702 | } |
| 703 | |
| 704 | *p = NUL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 705 | return IObuff; |
| 706 | } |
| 707 | |
| 708 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 709 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
| 710 | * case of the originally typed text is used, and the case of the completed |
| 711 | * text is inferred, ie this tries to work out what case you probably wanted |
| 712 | * the rest of the word to be in -- webb |
| 713 | */ |
| 714 | int |
| 715 | ins_compl_add_infercase( |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 716 | char_u *str_arg, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 717 | int len, |
| 718 | int icase, |
| 719 | char_u *fname, |
| 720 | int dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 721 | int cont_s_ipos, // next ^X<> will set initial_pos |
| 722 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 723 | { |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 724 | char_u *str = str_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 725 | char_u *p; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 726 | int char_len; // count multi-byte characters |
| 727 | int compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 728 | int min_len; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 729 | int flags = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 730 | int res; |
| 731 | char_u *tofree = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 732 | |
| 733 | if (p_ic && curbuf->b_p_inf && len > 0) |
| 734 | { |
| 735 | // Infer case of completed part. |
| 736 | |
| 737 | // Find actual length of completion. |
| 738 | if (has_mbyte) |
| 739 | { |
| 740 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 741 | char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 742 | while (*p != NUL) |
| 743 | { |
| 744 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 745 | ++char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 749 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 750 | char_len = len; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 751 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 752 | |
| 753 | // Find actual length of original text. |
| 754 | if (has_mbyte) |
| 755 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 756 | p = compl_orig_text.string; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 757 | compl_char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 758 | while (*p != NUL) |
| 759 | { |
| 760 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 761 | ++compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 765 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 766 | compl_char_len = compl_length; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 767 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 768 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 769 | // "char_len" may be smaller than "compl_char_len" when using |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 770 | // thesaurus, only use the minimum when comparing. |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 771 | min_len = MIN(char_len, compl_char_len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 772 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 773 | str = ins_compl_infercase_gettext(str, char_len, |
| 774 | compl_char_len, min_len, &tofree); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 775 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 776 | if (cont_s_ipos) |
| 777 | flags |= CP_CONT_S_IPOS; |
| 778 | if (icase) |
| 779 | flags |= CP_ICASE; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 780 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 781 | 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] | 782 | vim_free(tofree); |
| 783 | return res; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 787 | * Check if ctrl_x_mode has been configured in 'completefuzzycollect' |
| 788 | */ |
| 789 | static int |
| 790 | cfc_has_mode(void) |
| 791 | { |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 792 | if (ctrl_x_mode_normal() || ctrl_x_mode_dictionary()) |
| 793 | return (cfc_flags & CFC_KEYWORD) != 0; |
| 794 | else if (ctrl_x_mode_files()) |
| 795 | return (cfc_flags & CFC_FILES) != 0; |
| 796 | else if (ctrl_x_mode_whole_line()) |
| 797 | return (cfc_flags & CFC_WHOLELINE) != 0; |
| 798 | else |
| 799 | return FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 803 | * Returns TRUE if matches should be sorted based on proximity to the cursor. |
| 804 | */ |
| 805 | static int |
| 806 | is_nearest_active(void) |
| 807 | { |
glepnir | c3fbaa0 | 2025-05-08 23:05:10 +0200 | [diff] [blame^] | 808 | return (get_cot_flags() & (COT_NEAREST | COT_FUZZY)) == COT_NEAREST; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Repositions a match in the completion list based on its proximity score. |
| 813 | * If the match is at the head and has a higher score than the next node, |
| 814 | * or if it's in the middle/tail and has a lower score than the previous node, |
| 815 | * it is moved to the correct position while maintaining ascending order. |
| 816 | */ |
| 817 | static void |
| 818 | reposition_match(compl_T *match) |
| 819 | { |
| 820 | compl_T *insert_before = NULL; |
| 821 | compl_T *insert_after = NULL; |
| 822 | |
| 823 | // Node is at head and score is too big |
| 824 | if (!match->cp_prev) |
| 825 | { |
| 826 | if (match->cp_next && match->cp_next->cp_score > 0 && |
| 827 | match->cp_next->cp_score < match->cp_score) |
| 828 | { |
| 829 | // <c-p>: compl_first_match is at head and newly inserted node |
| 830 | compl_first_match = compl_curr_match = match->cp_next; |
| 831 | // Find the correct position in ascending order |
| 832 | insert_before = match->cp_next; |
| 833 | do |
| 834 | { |
| 835 | insert_after = insert_before; |
| 836 | insert_before = insert_before->cp_next; |
| 837 | } while (insert_before && insert_before->cp_score > 0 && |
| 838 | insert_before->cp_score < match->cp_score); |
| 839 | } |
| 840 | else |
| 841 | return; |
| 842 | } |
| 843 | // Node is at tail or in the middle but score is too small |
| 844 | else |
| 845 | { |
| 846 | if (match->cp_prev->cp_score > 0 && match->cp_prev->cp_score > match->cp_score) |
| 847 | { |
| 848 | // <c-n>: compl_curr_match (and newly inserted match) is at tail |
| 849 | if (!match->cp_next) |
| 850 | compl_curr_match = compl_curr_match->cp_prev; |
| 851 | // Find the correct position in ascending order |
| 852 | insert_after = match->cp_prev; |
| 853 | do |
| 854 | { |
| 855 | insert_before = insert_after; |
| 856 | insert_after = insert_after->cp_prev; |
| 857 | } while (insert_after && insert_after->cp_score > 0 && |
| 858 | insert_after->cp_score > match->cp_score); |
| 859 | } |
| 860 | else |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | if (insert_after) |
| 865 | { |
| 866 | // Remove the match from its current position |
| 867 | if (match->cp_prev) |
| 868 | match->cp_prev->cp_next = match->cp_next; |
| 869 | else |
| 870 | compl_first_match = match->cp_next; |
| 871 | if (match->cp_next) |
| 872 | match->cp_next->cp_prev = match->cp_prev; |
| 873 | |
| 874 | // Insert the match at the correct position |
| 875 | match->cp_next = insert_before; |
| 876 | match->cp_prev = insert_after; |
| 877 | insert_after->cp_next = match; |
| 878 | insert_before->cp_prev = match; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 883 | * Add a match to the list of matches. The arguments are: |
| 884 | * str - text of the match to add |
| 885 | * len - length of "str". If -1, then the length of "str" is |
| 886 | * computed. |
| 887 | * fname - file name to associate with this match. |
| 888 | * cptext - list of strings to use with this match (for abbr, menu, info |
| 889 | * and kind) |
| 890 | * user_data - user supplied data (any vim type) for this match |
| 891 | * cdir - match direction. If 0, use "compl_direction". |
| 892 | * flags_arg - match flags (cp_flags) |
| 893 | * adup - accept this match even if it is already present. |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 894 | * *user_hl - list of extra highlight attributes for abbr kind. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 895 | * If "cdir" is FORWARD, then the match is added after the current match. |
| 896 | * Otherwise, it is added before the current match. |
| 897 | * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 898 | * If the given string is already in the list of completions, then return |
| 899 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 900 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 901 | */ |
| 902 | static int |
| 903 | ins_compl_add( |
| 904 | char_u *str, |
| 905 | int len, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 906 | char_u *fname, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 907 | char_u **cptext, // extra text for popup menu or NULL |
| 908 | typval_T *user_data UNUSED, // "user_data" entry or NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 909 | int cdir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 910 | int flags_arg, |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 911 | int adup, // accept duplicate match |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 912 | int *user_hl, // user abbr/kind hlattr |
| 913 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 914 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 915 | compl_T *match, *current, *prev; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 916 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 917 | int flags = flags_arg; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 918 | int inserted = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 919 | |
Bram Moolenaar | ceb0619 | 2021-04-04 15:05:22 +0200 | [diff] [blame] | 920 | if (flags & CP_FAST) |
| 921 | fast_breakcheck(); |
| 922 | else |
| 923 | ui_breakcheck(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 924 | if (got_int) |
| 925 | return FAIL; |
| 926 | if (len < 0) |
| 927 | len = (int)STRLEN(str); |
| 928 | |
| 929 | // If the same match is already present, don't add it. |
| 930 | if (compl_first_match != NULL && !adup) |
| 931 | { |
| 932 | match = compl_first_match; |
| 933 | do |
| 934 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 935 | if (!match_at_original_text(match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 936 | && STRNCMP(match->cp_str.string, str, len) == 0 |
| 937 | && ((int)match->cp_str.length <= len |
| 938 | || match->cp_str.string[len] == NUL)) |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 939 | { |
| 940 | if (is_nearest_active() && score > 0 && score < match->cp_score) |
| 941 | { |
| 942 | match->cp_score = score; |
| 943 | reposition_match(match); |
| 944 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 945 | return NOTDONE; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 946 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 947 | match = match->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 948 | } while (match != NULL && !is_first_match(match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // Remove any popup menu before changing the list of matches. |
| 952 | ins_compl_del_pum(); |
| 953 | |
| 954 | // Allocate a new match structure. |
| 955 | // Copy the values to the new match structure. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 956 | match = ALLOC_CLEAR_ONE(compl_T); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 957 | if (match == NULL) |
| 958 | return FAIL; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 959 | match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 960 | if ((match->cp_str.string = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 961 | { |
| 962 | vim_free(match); |
| 963 | return FAIL; |
| 964 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 965 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 966 | match->cp_str.length = len; |
| 967 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 968 | // match-fname is: |
| 969 | // - 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] | 970 | // - 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] | 971 | // - NULL otherwise. --Acevedo |
| 972 | if (fname != NULL |
| 973 | && compl_curr_match != NULL |
| 974 | && compl_curr_match->cp_fname != NULL |
| 975 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 976 | match->cp_fname = compl_curr_match->cp_fname; |
| 977 | else if (fname != NULL) |
| 978 | { |
| 979 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 980 | flags |= CP_FREE_FNAME; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 981 | } |
| 982 | else |
| 983 | match->cp_fname = NULL; |
| 984 | match->cp_flags = flags; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 985 | match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1; |
| 986 | match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 987 | match->cp_score = score; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 988 | match->cp_cpt_source_idx = cpt_sources_index; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 989 | |
| 990 | if (cptext != NULL) |
| 991 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 992 | for (int i = 0; i < CPT_COUNT; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 993 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 994 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 995 | match->cp_text[i] = vim_strsave(cptext[i]); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 996 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 997 | } |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 998 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 999 | if (user_data != NULL) |
| 1000 | match->cp_user_data = *user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1001 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1002 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1003 | // Link the new match structure after (FORWARD) or before (BACKWARD) the |
| 1004 | // current match in the list of matches . |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1005 | if (compl_first_match == NULL) |
| 1006 | match->cp_next = match->cp_prev = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1007 | else if (cfc_has_mode() && score > 0 && compl_get_longest) |
| 1008 | { |
| 1009 | current = compl_first_match->cp_next; |
| 1010 | prev = compl_first_match; |
| 1011 | inserted = FALSE; |
| 1012 | // The direction is ignored when using longest and |
| 1013 | // completefuzzycollect, because matches are inserted |
| 1014 | // and sorted by score. |
| 1015 | while (current != NULL && current != compl_first_match) |
| 1016 | { |
| 1017 | if (current->cp_score < score) |
| 1018 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 1019 | match->cp_next = current; |
| 1020 | match->cp_prev = current->cp_prev; |
| 1021 | if (current->cp_prev) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1022 | current->cp_prev->cp_next = match; |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 1023 | current->cp_prev = match; |
| 1024 | inserted = TRUE; |
| 1025 | break; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1026 | } |
| 1027 | prev = current; |
| 1028 | current = current->cp_next; |
| 1029 | } |
| 1030 | if (!inserted) |
| 1031 | { |
| 1032 | prev->cp_next = match; |
| 1033 | match->cp_prev = prev; |
| 1034 | match->cp_next = compl_first_match; |
| 1035 | compl_first_match->cp_prev = match; |
| 1036 | } |
| 1037 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1038 | else if (dir == FORWARD) |
| 1039 | { |
| 1040 | match->cp_next = compl_curr_match->cp_next; |
| 1041 | match->cp_prev = compl_curr_match; |
| 1042 | } |
| 1043 | else // BACKWARD |
| 1044 | { |
| 1045 | match->cp_next = compl_curr_match; |
| 1046 | match->cp_prev = compl_curr_match->cp_prev; |
| 1047 | } |
| 1048 | if (match->cp_next) |
| 1049 | match->cp_next->cp_prev = match; |
| 1050 | if (match->cp_prev) |
| 1051 | match->cp_prev->cp_next = match; |
| 1052 | else // if there's nothing before, it is the first match |
| 1053 | compl_first_match = match; |
| 1054 | compl_curr_match = match; |
| 1055 | |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 1056 | if (is_nearest_active() && score > 0) |
| 1057 | reposition_match(match); |
| 1058 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1059 | // Find the longest common string if still doing that. |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1060 | if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1061 | ins_compl_longest_match(match); |
| 1062 | |
| 1063 | return OK; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1068 | * match->cp_flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1069 | */ |
| 1070 | static int |
| 1071 | ins_compl_equal(compl_T *match, char_u *str, int len) |
| 1072 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1073 | if (match->cp_flags & CP_EQUAL) |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 1074 | return TRUE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1075 | if (match->cp_flags & CP_ICASE) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1076 | return STRNICMP(match->cp_str.string, str, (size_t)len) == 0; |
| 1077 | return STRNCMP(match->cp_str.string, str, (size_t)len) == 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1081 | * when len is -1 mean use whole length of p otherwise part of p |
| 1082 | */ |
| 1083 | static void |
| 1084 | ins_compl_insert_bytes(char_u *p, int len) |
| 1085 | { |
| 1086 | if (len == -1) |
| 1087 | len = (int)STRLEN(p); |
| 1088 | ins_bytes_len(p, len); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 1089 | compl_ins_end_col = curwin->w_cursor.col; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
zeertzjq | d32bf0a | 2024-12-17 20:55:13 +0100 | [diff] [blame] | 1093 | * Checks if the column is within the currently inserted completion text |
| 1094 | * column range. If it is, it returns a special highlight attribute. |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1095 | * -1 means normal item. |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1096 | */ |
| 1097 | int |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1098 | ins_compl_col_range_attr(linenr_T lnum, int col) |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1099 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1100 | int start_col; |
| 1101 | int attr; |
| 1102 | |
| 1103 | if ((get_cot_flags() & COT_FUZZY) |
| 1104 | || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0) |
glepnir | e890887 | 2025-01-08 18:30:45 +0100 | [diff] [blame] | 1105 | return -1; |
| 1106 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1107 | start_col = compl_col + (int)ins_compl_leader_len(); |
| 1108 | if (!ins_compl_has_multiple()) |
| 1109 | return (col >= start_col && col < compl_ins_end_col) ? attr : -1; |
| 1110 | |
| 1111 | // Multiple lines |
| 1112 | if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) || |
| 1113 | (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) || |
| 1114 | (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col)) |
| 1115 | return attr; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1116 | |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1121 | * Returns TRUE if the current completion string contains newline characters, |
| 1122 | * indicating it's a multi-line completion. |
| 1123 | */ |
| 1124 | static int |
| 1125 | ins_compl_has_multiple(void) |
| 1126 | { |
| 1127 | return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Returns TRUE if the given line number falls within the range of a multi-line |
| 1132 | * completion, i.e. between the starting line (compl_lnum) and current cursor |
| 1133 | * line. Always returns FALSE for single-line completions. |
| 1134 | */ |
| 1135 | int |
| 1136 | ins_compl_lnum_in_range(linenr_T lnum) |
| 1137 | { |
| 1138 | if (!ins_compl_has_multiple()) |
| 1139 | return FALSE; |
| 1140 | return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1144 | * Reduce the longest common string for match "match". |
| 1145 | */ |
| 1146 | static void |
| 1147 | ins_compl_longest_match(compl_T *match) |
| 1148 | { |
| 1149 | char_u *p, *s; |
| 1150 | int c1, c2; |
| 1151 | int had_match; |
| 1152 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1153 | if (compl_leader.string == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1154 | { |
| 1155 | // First match, use it as a whole. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1156 | compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length); |
| 1157 | if (compl_leader.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1158 | return; |
| 1159 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1160 | compl_leader.length = match->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1161 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1162 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1163 | |
| 1164 | // When the match isn't there (to avoid matching itself) remove it |
| 1165 | // again after redrawing. |
| 1166 | if (!had_match) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1167 | ins_compl_delete(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1168 | compl_used_match = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1169 | |
| 1170 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1171 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Reduce the text if this match differs from compl_leader. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1174 | p = compl_leader.string; |
| 1175 | s = match->cp_str.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1176 | while (*p != NUL) |
| 1177 | { |
| 1178 | if (has_mbyte) |
| 1179 | { |
| 1180 | c1 = mb_ptr2char(p); |
| 1181 | c2 = mb_ptr2char(s); |
| 1182 | } |
| 1183 | else |
| 1184 | { |
| 1185 | c1 = *p; |
| 1186 | c2 = *s; |
| 1187 | } |
| 1188 | if ((match->cp_flags & CP_ICASE) |
| 1189 | ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2)) |
| 1190 | break; |
| 1191 | if (has_mbyte) |
| 1192 | { |
| 1193 | MB_PTR_ADV(p); |
| 1194 | MB_PTR_ADV(s); |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | ++p; |
| 1199 | ++s; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (*p != NUL) |
| 1204 | { |
| 1205 | // Leader was shortened, need to change the inserted text. |
| 1206 | *p = NUL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1207 | compl_leader.length = (size_t)(p - compl_leader.string); |
| 1208 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1209 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1210 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1211 | |
| 1212 | // When the match isn't there (to avoid matching itself) remove it |
| 1213 | // again after redrawing. |
| 1214 | if (!had_match) |
| 1215 | ins_compl_delete(); |
| 1216 | } |
| 1217 | |
| 1218 | compl_used_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Add an array of matches to the list of matches. |
| 1223 | * Frees matches[]. |
| 1224 | */ |
| 1225 | static void |
| 1226 | ins_compl_add_matches( |
| 1227 | int num_matches, |
| 1228 | char_u **matches, |
| 1229 | int icase) |
| 1230 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1231 | int add_r = OK; |
| 1232 | int dir = compl_direction; |
| 1233 | |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1234 | for (int i = 0; i < num_matches && add_r != FAIL; i++) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1235 | { |
| 1236 | add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1237 | CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1238 | if (add_r == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1239 | // if dir was BACKWARD then honor it just once |
| 1240 | dir = FORWARD; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1241 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1242 | FreeWild(num_matches, matches); |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * Make the completion list cyclic. |
| 1247 | * Return the number of matches (excluding the original). |
| 1248 | */ |
| 1249 | static int |
| 1250 | ins_compl_make_cyclic(void) |
| 1251 | { |
| 1252 | compl_T *match; |
| 1253 | int count = 0; |
| 1254 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1255 | if (compl_first_match == NULL) |
| 1256 | return 0; |
| 1257 | |
| 1258 | // Find the end of the list. |
| 1259 | match = compl_first_match; |
| 1260 | // 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] | 1261 | while (match->cp_next != NULL && !is_first_match(match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1262 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1263 | match = match->cp_next; |
| 1264 | ++count; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1265 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1266 | match->cp_next = compl_first_match; |
| 1267 | compl_first_match->cp_prev = match; |
| 1268 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1269 | return count; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * Return whether there currently is a shown match. |
| 1274 | */ |
| 1275 | int |
| 1276 | ins_compl_has_shown_match(void) |
| 1277 | { |
| 1278 | return compl_shown_match == NULL |
| 1279 | || compl_shown_match != compl_shown_match->cp_next; |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Return whether the shown match is long enough. |
| 1284 | */ |
| 1285 | int |
| 1286 | ins_compl_long_shown_match(void) |
| 1287 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1288 | return (int)compl_shown_match->cp_str.length |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1289 | > curwin->w_cursor.col - compl_col; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1293 | * Get the local or global value of 'completeopt' flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1294 | */ |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1295 | unsigned int |
| 1296 | get_cot_flags(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1297 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1298 | return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1301 | /* |
| 1302 | * Update the screen and when there is any scrolling remove the popup menu. |
| 1303 | */ |
| 1304 | static void |
| 1305 | ins_compl_upd_pum(void) |
| 1306 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1307 | if (compl_match_array == NULL) |
| 1308 | return; |
| 1309 | |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1310 | int h = curwin->w_cline_height; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1311 | // Update the screen later, before drawing the popup menu over it. |
| 1312 | pum_call_update_screen(); |
| 1313 | if (h != curwin->w_cline_height) |
| 1314 | ins_compl_del_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * Remove any popup menu. |
| 1319 | */ |
| 1320 | static void |
| 1321 | ins_compl_del_pum(void) |
| 1322 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1323 | if (compl_match_array == NULL) |
| 1324 | return; |
| 1325 | |
| 1326 | pum_undisplay(); |
| 1327 | VIM_CLEAR(compl_match_array); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Return TRUE if the popup menu should be displayed. |
| 1332 | */ |
| 1333 | int |
| 1334 | pum_wanted(void) |
| 1335 | { |
| 1336 | // 'completeopt' must contain "menu" or "menuone" |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1337 | if ((get_cot_flags() & COT_ANY_MENU) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1338 | return FALSE; |
| 1339 | |
| 1340 | // The display looks bad on a B&W display. |
| 1341 | if (t_colors < 8 |
| 1342 | #ifdef FEAT_GUI |
| 1343 | && !gui.in_use |
| 1344 | #endif |
| 1345 | ) |
| 1346 | return FALSE; |
| 1347 | return TRUE; |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 1352 | * One if 'completopt' contains "menuone". |
| 1353 | */ |
| 1354 | static int |
| 1355 | pum_enough_matches(void) |
| 1356 | { |
| 1357 | compl_T *compl; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1358 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1359 | |
| 1360 | // Don't display the popup menu if there are no matches or there is only |
| 1361 | // one (ignoring the original text). |
| 1362 | compl = compl_first_match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1363 | do |
| 1364 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1365 | if (compl == NULL || (!match_at_original_text(compl) && ++i == 2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1366 | break; |
| 1367 | compl = compl->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1368 | } while (!is_first_match(compl)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1369 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1370 | if (get_cot_flags() & COT_MENUONE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1371 | return (i >= 1); |
| 1372 | return (i >= 2); |
| 1373 | } |
| 1374 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1375 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1376 | /* |
| 1377 | * Allocate Dict for the completed item. |
| 1378 | * { word, abbr, menu, kind, info } |
| 1379 | */ |
| 1380 | static dict_T * |
| 1381 | ins_compl_dict_alloc(compl_T *match) |
| 1382 | { |
| 1383 | dict_T *dict = dict_alloc_lock(VAR_FIXED); |
| 1384 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1385 | if (dict == NULL) |
| 1386 | return NULL; |
| 1387 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1388 | dict_add_string(dict, "word", match->cp_str.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1389 | dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]); |
| 1390 | dict_add_string(dict, "menu", match->cp_text[CPT_MENU]); |
| 1391 | dict_add_string(dict, "kind", match->cp_text[CPT_KIND]); |
| 1392 | dict_add_string(dict, "info", match->cp_text[CPT_INFO]); |
| 1393 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 1394 | dict_add_string(dict, "user_data", (char_u *)""); |
| 1395 | else |
| 1396 | dict_add_tv(dict, "user_data", &match->cp_user_data); |
| 1397 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1398 | return dict; |
| 1399 | } |
| 1400 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1401 | /* |
| 1402 | * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode |
| 1403 | * completion menu is changed. |
| 1404 | */ |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1405 | static void |
| 1406 | trigger_complete_changed_event(int cur) |
| 1407 | { |
| 1408 | dict_T *v_event; |
| 1409 | dict_T *item; |
| 1410 | static int recursive = FALSE; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1411 | save_v_event_T save_v_event; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1412 | |
| 1413 | if (recursive) |
| 1414 | return; |
| 1415 | |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 1416 | item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1417 | if (item == NULL) |
| 1418 | return; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1419 | v_event = get_v_event(&save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1420 | dict_add_dict(v_event, "completed_item", item); |
| 1421 | pum_set_event_info(v_event); |
| 1422 | dict_set_items_ro(v_event); |
| 1423 | |
| 1424 | recursive = TRUE; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1425 | textlock++; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1426 | apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1427 | textlock--; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1428 | recursive = FALSE; |
| 1429 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1430 | restore_v_event(v_event, &save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1431 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1432 | #endif |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1433 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1434 | /* |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1435 | * pumitem qsort compare func |
| 1436 | */ |
| 1437 | static int |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1438 | ins_compl_fuzzy_cmp(const void *a, const void *b) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1439 | { |
| 1440 | const int sa = (*(pumitem_T *)a).pum_score; |
| 1441 | const int sb = (*(pumitem_T *)b).pum_score; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1442 | const int ia = (*(pumitem_T *)a).pum_idx; |
| 1443 | const int ib = (*(pumitem_T *)b).pum_idx; |
| 1444 | 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] | 1445 | } |
| 1446 | |
| 1447 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1448 | * Build a popup menu to show the completion matches. |
| 1449 | * Returns the popup menu entry that should be selected. Returns -1 if nothing |
| 1450 | * should be selected. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1451 | */ |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1452 | static int |
| 1453 | ins_compl_build_pum(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1454 | { |
| 1455 | compl_T *compl; |
| 1456 | compl_T *shown_compl = NULL; |
| 1457 | int did_find_shown_match = FALSE; |
| 1458 | int shown_match_ok = FALSE; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1459 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1460 | int cur = -1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1461 | int max_fuzzy_score = 0; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 1462 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1463 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1464 | int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0; |
| 1465 | int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1466 | compl_T *match_head = NULL; |
| 1467 | compl_T *match_tail = NULL; |
| 1468 | compl_T *match_next = NULL; |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1469 | int update_shown_match = fuzzy_filter; |
Christian Brabandt | b53d4fb | 2025-04-16 21:12:30 +0200 | [diff] [blame] | 1470 | int match_count = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1471 | int cur_source = -1; |
| 1472 | int max_matches_found = FALSE; |
| 1473 | int is_forward = compl_shows_dir_forward() && !fuzzy_filter; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1474 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1475 | // Need to build the popup menu list. |
| 1476 | compl_match_arraysize = 0; |
| 1477 | compl = compl_first_match; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1478 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1479 | // If the current match is the original text don't find the first |
| 1480 | // match after it, don't highlight anything. |
| 1481 | if (match_at_original_text(compl_shown_match)) |
| 1482 | shown_match_ok = TRUE; |
| 1483 | |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1484 | if (fuzzy_filter && ctrl_x_mode_normal() && compl_leader.string == NULL |
| 1485 | && compl_shown_match->cp_score > 0) |
| 1486 | update_shown_match = FALSE; |
| 1487 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1488 | if (compl_leader.string != NULL |
| 1489 | && STRCMP(compl_leader.string, compl_orig_text.string) == 0 |
| 1490 | && shown_match_ok == FALSE) |
| 1491 | compl_shown_match = compl_no_select ? compl_first_match |
| 1492 | : compl_first_match->cp_next; |
| 1493 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1494 | do |
| 1495 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1496 | compl->cp_in_match_array = FALSE; |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 1497 | // When 'completeopt' contains "fuzzy" and leader is not NULL or empty, |
| 1498 | // set the cp_score for later comparisons. |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1499 | if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1500 | compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1501 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1502 | if (is_forward && compl->cp_cpt_source_idx != -1) |
| 1503 | { |
| 1504 | if (cur_source != compl->cp_cpt_source_idx) |
| 1505 | { |
| 1506 | cur_source = compl->cp_cpt_source_idx; |
| 1507 | match_count = 1; |
| 1508 | max_matches_found = FALSE; |
| 1509 | } |
| 1510 | else if (cpt_sources_array && !max_matches_found) |
| 1511 | { |
| 1512 | int max_matches = cpt_sources_array[cur_source].max_matches; |
| 1513 | if (max_matches > 0 && match_count > max_matches) |
| 1514 | max_matches_found = TRUE; |
| 1515 | } |
| 1516 | } |
| 1517 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1518 | if (!match_at_original_text(compl) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1519 | && !max_matches_found |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1520 | && (compl_leader.string == NULL |
| 1521 | || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length) |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1522 | || (fuzzy_filter && compl->cp_score > 0))) |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1523 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1524 | ++compl_match_arraysize; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1525 | compl->cp_in_match_array = TRUE; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1526 | if (match_head == NULL) |
| 1527 | match_head = compl; |
| 1528 | else |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1529 | match_tail->cp_match_next = compl; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1530 | match_tail = compl; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1531 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1532 | if (!shown_match_ok && !fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1533 | { |
| 1534 | if (compl == compl_shown_match || did_find_shown_match) |
| 1535 | { |
| 1536 | // This item is the shown match or this is the |
| 1537 | // first displayed item after the shown match. |
| 1538 | compl_shown_match = compl; |
| 1539 | did_find_shown_match = TRUE; |
| 1540 | shown_match_ok = TRUE; |
| 1541 | } |
| 1542 | else |
| 1543 | // Remember this displayed match for when the |
| 1544 | // shown match is just below it. |
| 1545 | shown_compl = compl; |
| 1546 | cur = i; |
| 1547 | } |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1548 | else if (fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1549 | { |
| 1550 | if (i == 0) |
| 1551 | shown_compl = compl; |
| 1552 | // Update the maximum fuzzy score and the shown match |
| 1553 | // if the current item's score is higher |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1554 | if (fuzzy_sort && compl->cp_score > max_fuzzy_score |
| 1555 | && update_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1556 | { |
| 1557 | did_find_shown_match = TRUE; |
| 1558 | max_fuzzy_score = compl->cp_score; |
| 1559 | if (!compl_no_select) |
| 1560 | compl_shown_match = compl; |
| 1561 | } |
| 1562 | |
glepnir | 3af0a8d | 2025-02-20 22:06:16 +0100 | [diff] [blame] | 1563 | if (!shown_match_ok && compl == compl_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1564 | { |
| 1565 | cur = i; |
| 1566 | shown_match_ok = TRUE; |
| 1567 | } |
| 1568 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1569 | if (is_forward && compl->cp_cpt_source_idx != -1) |
| 1570 | match_count++; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1571 | i++; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1572 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1573 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1574 | if (compl == compl_shown_match && !fuzzy_filter) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1575 | { |
| 1576 | did_find_shown_match = TRUE; |
| 1577 | |
| 1578 | // When the original text is the shown match don't set |
| 1579 | // compl_shown_match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1580 | if (match_at_original_text(compl)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1581 | shown_match_ok = TRUE; |
| 1582 | |
| 1583 | if (!shown_match_ok && shown_compl != NULL) |
| 1584 | { |
| 1585 | // The shown match isn't displayed, set it to the |
| 1586 | // previously displayed match. |
| 1587 | compl_shown_match = shown_compl; |
| 1588 | shown_match_ok = TRUE; |
| 1589 | } |
| 1590 | } |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1591 | compl = compl->cp_next; |
| 1592 | } while (compl != NULL && !is_first_match(compl)); |
| 1593 | |
| 1594 | if (compl_match_arraysize == 0) |
| 1595 | return -1; |
| 1596 | |
glepnir | c0b7ca4 | 2025-02-13 20:27:44 +0100 | [diff] [blame] | 1597 | if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok) |
| 1598 | { |
| 1599 | compl_shown_match = shown_compl; |
| 1600 | shown_match_ok = TRUE; |
| 1601 | cur = 0; |
| 1602 | } |
| 1603 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1604 | compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize); |
| 1605 | if (compl_match_array == NULL) |
| 1606 | return -1; |
| 1607 | |
| 1608 | compl = match_head; |
| 1609 | i = 0; |
| 1610 | while (compl != NULL) |
| 1611 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1612 | compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL |
| 1613 | ? compl->cp_text[CPT_ABBR] : compl->cp_str.string; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1614 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 1615 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 1616 | compl_match_array[i].pum_score = compl->cp_score; |
| 1617 | compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr; |
| 1618 | compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1619 | compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL |
| 1620 | ? compl->cp_text[CPT_MENU] : compl->cp_fname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1621 | match_next = compl->cp_match_next; |
| 1622 | compl->cp_match_next = NULL; |
| 1623 | compl = match_next; |
| 1624 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1625 | |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1626 | if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0) |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1627 | { |
| 1628 | for (i = 0; i < compl_match_arraysize; i++) |
| 1629 | compl_match_array[i].pum_idx = i; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1630 | // sort by the largest score of fuzzy match |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1631 | qsort(compl_match_array, (size_t)compl_match_arraysize, |
| 1632 | sizeof(pumitem_T), ins_compl_fuzzy_cmp); |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 1633 | shown_match_ok = TRUE; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1634 | } |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1635 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1636 | if (!shown_match_ok) // no displayed match at all |
| 1637 | cur = -1; |
| 1638 | |
| 1639 | return cur; |
| 1640 | } |
| 1641 | |
| 1642 | /* |
| 1643 | * Show the popup menu for the list of matches. |
| 1644 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
| 1645 | */ |
| 1646 | void |
| 1647 | ins_compl_show_pum(void) |
| 1648 | { |
| 1649 | int i; |
| 1650 | int cur = -1; |
| 1651 | colnr_T col; |
| 1652 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1653 | if (!pum_wanted() || !pum_enough_matches()) |
| 1654 | return; |
| 1655 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1656 | // Update the screen later, before drawing the popup menu over it. |
| 1657 | pum_call_update_screen(); |
| 1658 | |
| 1659 | if (compl_match_array == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1660 | // Need to build the popup menu list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1661 | cur = ins_compl_build_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1662 | else |
| 1663 | { |
| 1664 | // popup menu already exists, only need to find the current item. |
| 1665 | for (i = 0; i < compl_match_arraysize; ++i) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1666 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1667 | 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] | 1668 | || compl_match_array[i].pum_text |
| 1669 | == compl_shown_match->cp_text[CPT_ABBR]) |
| 1670 | { |
| 1671 | cur = i; |
| 1672 | break; |
| 1673 | } |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1674 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1677 | if (compl_match_array == NULL) |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1678 | { |
| 1679 | #ifdef FEAT_EVAL |
| 1680 | if (compl_started && has_completechanged()) |
| 1681 | trigger_complete_changed_event(cur); |
| 1682 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1683 | return; |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1684 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1685 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1686 | // In Replace mode when a $ is displayed at the end of the line only |
| 1687 | // part of the screen would be updated. We do need to redraw here. |
| 1688 | dollar_vcol = -1; |
| 1689 | |
| 1690 | // Compute the screen column of the start of the completed text. |
| 1691 | // Use the cursor to get all wrapping and other settings right. |
| 1692 | col = curwin->w_cursor.col; |
| 1693 | curwin->w_cursor.col = compl_col; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1694 | compl_selected_item = cur; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1695 | pum_display(compl_match_array, compl_match_arraysize, cur); |
| 1696 | curwin->w_cursor.col = col; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1697 | |
glepnir | cbb46b4 | 2024-02-03 18:11:13 +0100 | [diff] [blame] | 1698 | // After adding leader, set the current match to shown match. |
| 1699 | if (compl_started && compl_curr_match != compl_shown_match) |
| 1700 | compl_curr_match = compl_shown_match; |
| 1701 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1702 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1703 | if (has_completechanged()) |
| 1704 | trigger_complete_changed_event(cur); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1705 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | #define DICT_FIRST (1) // use just first element in "dict" |
| 1709 | #define DICT_EXACT (2) // "dict" is the exact name of a file |
| 1710 | |
| 1711 | /* |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1712 | * Get current completion leader |
| 1713 | */ |
| 1714 | char_u * |
| 1715 | ins_compl_leader(void) |
| 1716 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1717 | return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Get current completion leader length |
| 1722 | */ |
| 1723 | size_t |
| 1724 | ins_compl_leader_len(void) |
| 1725 | { |
| 1726 | return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1730 | * Add any identifiers that match the given pattern "pat" in the list of |
| 1731 | * dictionary files "dict_start" to the list of completions. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1732 | */ |
| 1733 | static void |
| 1734 | ins_compl_dictionaries( |
| 1735 | char_u *dict_start, |
| 1736 | char_u *pat, |
| 1737 | int flags, // DICT_FIRST and/or DICT_EXACT |
| 1738 | int thesaurus) // Thesaurus completion |
| 1739 | { |
| 1740 | char_u *dict = dict_start; |
| 1741 | char_u *ptr; |
| 1742 | char_u *buf; |
| 1743 | regmatch_T regmatch; |
| 1744 | char_u **files; |
| 1745 | int count; |
| 1746 | int save_p_scs; |
| 1747 | int dir = compl_direction; |
| 1748 | |
| 1749 | if (*dict == NUL) |
| 1750 | { |
| 1751 | #ifdef FEAT_SPELL |
| 1752 | // When 'dictionary' is empty and spell checking is enabled use |
| 1753 | // "spell". |
| 1754 | if (!thesaurus && curwin->w_p_spell) |
| 1755 | dict = (char_u *)"spell"; |
| 1756 | else |
| 1757 | #endif |
| 1758 | return; |
| 1759 | } |
| 1760 | |
| 1761 | buf = alloc(LSIZE); |
| 1762 | if (buf == NULL) |
| 1763 | return; |
| 1764 | regmatch.regprog = NULL; // so that we can goto theend |
| 1765 | |
| 1766 | // If 'infercase' is set, don't use 'smartcase' here |
| 1767 | save_p_scs = p_scs; |
| 1768 | if (curbuf->b_p_inf) |
| 1769 | p_scs = FALSE; |
| 1770 | |
| 1771 | // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 1772 | // to only match at the start of a line. Otherwise just match the |
| 1773 | // pattern. Also need to double backslashes. |
| 1774 | if (ctrl_x_mode_line_or_eval()) |
| 1775 | { |
| 1776 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1777 | |
| 1778 | if (pat_esc == NULL) |
| 1779 | goto theend; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1780 | size_t len = STRLEN(pat_esc) + 10; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1781 | ptr = alloc(len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1782 | if (ptr == NULL) |
| 1783 | { |
| 1784 | vim_free(pat_esc); |
| 1785 | goto theend; |
| 1786 | } |
| 1787 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
| 1788 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 1789 | vim_free(pat_esc); |
| 1790 | vim_free(ptr); |
| 1791 | } |
| 1792 | else |
| 1793 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1794 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1795 | if (regmatch.regprog == NULL) |
| 1796 | goto theend; |
| 1797 | } |
| 1798 | |
| 1799 | // ignore case depends on 'ignorecase', 'smartcase' and "pat" |
| 1800 | regmatch.rm_ic = ignorecase(pat); |
| 1801 | while (*dict != NUL && !got_int && !compl_interrupted) |
| 1802 | { |
| 1803 | // copy one dictionary file name into buf |
| 1804 | if (flags == DICT_EXACT) |
| 1805 | { |
| 1806 | count = 1; |
| 1807 | files = &dict; |
| 1808 | } |
| 1809 | else |
| 1810 | { |
| 1811 | // Expand wildcards in the dictionary name, but do not allow |
| 1812 | // backticks (for security, the 'dict' option may have been set in |
| 1813 | // a modeline). |
| 1814 | copy_option_part(&dict, buf, LSIZE, ","); |
| 1815 | # ifdef FEAT_SPELL |
| 1816 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 1817 | count = -1; |
| 1818 | else |
| 1819 | # endif |
| 1820 | if (vim_strchr(buf, '`') != NULL |
| 1821 | || expand_wildcards(1, &buf, &count, &files, |
| 1822 | EW_FILE|EW_SILENT) != OK) |
| 1823 | count = 0; |
| 1824 | } |
| 1825 | |
| 1826 | # ifdef FEAT_SPELL |
| 1827 | if (count == -1) |
| 1828 | { |
| 1829 | // Complete from active spelling. Skip "\<" in the pattern, we |
| 1830 | // don't use it as a RE. |
| 1831 | if (pat[0] == '\\' && pat[1] == '<') |
| 1832 | ptr = pat + 2; |
| 1833 | else |
| 1834 | ptr = pat; |
| 1835 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
| 1836 | } |
| 1837 | else |
| 1838 | # endif |
| 1839 | if (count > 0) // avoid warning for using "files" uninit |
| 1840 | { |
| 1841 | ins_compl_files(count, files, thesaurus, flags, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1842 | (cfc_has_mode() ? NULL : ®match), buf, &dir); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1843 | if (flags != DICT_EXACT) |
| 1844 | FreeWild(count, files); |
| 1845 | } |
| 1846 | if (flags != 0) |
| 1847 | break; |
| 1848 | } |
| 1849 | |
| 1850 | theend: |
| 1851 | p_scs = save_p_scs; |
| 1852 | vim_regfree(regmatch.regprog); |
| 1853 | vim_free(buf); |
| 1854 | } |
| 1855 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1856 | /* |
| 1857 | * Add all the words in the line "*buf_arg" from the thesaurus file "fname" |
| 1858 | * skipping the word at 'skip_word'. Returns OK on success. |
| 1859 | */ |
| 1860 | static int |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 1861 | thesaurus_add_words_in_line( |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1862 | char_u *fname, |
| 1863 | char_u **buf_arg, |
| 1864 | int dir, |
| 1865 | char_u *skip_word) |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1866 | { |
| 1867 | int status = OK; |
| 1868 | char_u *ptr; |
| 1869 | char_u *wstart; |
| 1870 | |
| 1871 | // Add the other matches on the line |
| 1872 | ptr = *buf_arg; |
| 1873 | while (!got_int) |
| 1874 | { |
| 1875 | // Find start of the next word. Skip white |
| 1876 | // space and punctuation. |
| 1877 | ptr = find_word_start(ptr); |
| 1878 | if (*ptr == NUL || *ptr == NL) |
| 1879 | break; |
| 1880 | wstart = ptr; |
| 1881 | |
| 1882 | // Find end of the word. |
| 1883 | if (has_mbyte) |
| 1884 | // Japanese words may have characters in |
| 1885 | // different classes, only separate words |
| 1886 | // with single-byte non-word characters. |
| 1887 | while (*ptr != NUL) |
| 1888 | { |
| 1889 | int l = (*mb_ptr2len)(ptr); |
| 1890 | |
| 1891 | if (l < 2 && !vim_iswordc(*ptr)) |
| 1892 | break; |
| 1893 | ptr += l; |
| 1894 | } |
| 1895 | else |
| 1896 | ptr = find_word_end(ptr); |
| 1897 | |
| 1898 | // Add the word. Skip the regexp match. |
| 1899 | if (wstart != skip_word) |
| 1900 | { |
| 1901 | status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1902 | fname, dir, FALSE, 0); |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1903 | if (status == FAIL) |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | *buf_arg = ptr; |
| 1909 | return status; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * Process "count" dictionary/thesaurus "files" and add the text matching |
| 1914 | * "regmatch". |
| 1915 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1916 | static void |
| 1917 | ins_compl_files( |
| 1918 | int count, |
| 1919 | char_u **files, |
| 1920 | int thesaurus, |
| 1921 | int flags, |
| 1922 | regmatch_T *regmatch, |
| 1923 | char_u *buf, |
| 1924 | int *dir) |
| 1925 | { |
| 1926 | char_u *ptr; |
| 1927 | int i; |
| 1928 | FILE *fp; |
| 1929 | int add_r; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1930 | char_u *leader = NULL; |
| 1931 | int leader_len = 0; |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 1932 | int in_fuzzy_collect = cfc_has_mode(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1933 | int score = 0; |
| 1934 | int len = 0; |
| 1935 | char_u *line_end = NULL; |
| 1936 | |
| 1937 | if (in_fuzzy_collect) |
| 1938 | { |
| 1939 | leader = ins_compl_leader(); |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 1940 | leader_len = (int)ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1941 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1942 | |
| 1943 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 1944 | { |
| 1945 | 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] | 1946 | if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1947 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1948 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1949 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1950 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 1951 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 1952 | } |
| 1953 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1954 | if (fp == NULL) |
| 1955 | continue; |
| 1956 | |
| 1957 | // Read dictionary file line by line. |
| 1958 | // Check each line for a match. |
| 1959 | while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1960 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1961 | ptr = buf; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1962 | if (regmatch != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1963 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1964 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1965 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1966 | ptr = regmatch->startp[0]; |
| 1967 | ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr) |
| 1968 | : find_word_end(ptr); |
| 1969 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 1970 | (int)(ptr - regmatch->startp[0]), |
| 1971 | p_ic, files[i], *dir, FALSE, 0); |
| 1972 | if (thesaurus) |
| 1973 | { |
| 1974 | // For a thesaurus, add all the words in the line |
| 1975 | ptr = buf; |
| 1976 | add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir, |
| 1977 | regmatch->startp[0]); |
| 1978 | } |
| 1979 | if (add_r == OK) |
| 1980 | // if dir was BACKWARD then honor it just once |
| 1981 | *dir = FORWARD; |
| 1982 | else if (add_r == FAIL) |
| 1983 | break; |
| 1984 | // avoid expensive call to vim_regexec() when at end |
| 1985 | // of line |
| 1986 | if (*ptr == '\n' || got_int) |
| 1987 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1988 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1989 | } |
| 1990 | else if (in_fuzzy_collect && leader_len > 0) |
| 1991 | { |
| 1992 | line_end = find_line_end(ptr); |
| 1993 | while (ptr < line_end) |
| 1994 | { |
| 1995 | if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score)) |
| 1996 | { |
| 1997 | char_u *end_ptr = ctrl_x_mode_line_or_eval() |
| 1998 | ? find_line_end(ptr) : find_word_end(ptr); |
| 1999 | add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr), |
| 2000 | p_ic, files[i], *dir, FALSE, score); |
| 2001 | if (add_r == FAIL) |
| 2002 | break; |
| 2003 | ptr = end_ptr; // start from next word |
| 2004 | if (compl_get_longest && ctrl_x_mode_normal() |
| 2005 | && compl_first_match->cp_next |
| 2006 | && score == compl_first_match->cp_next->cp_score) |
| 2007 | compl_num_bests++; |
| 2008 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2009 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2010 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2011 | line_breakcheck(); |
| 2012 | ins_compl_check_keys(50, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2013 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2014 | fclose(fp); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Find the start of the next word. |
| 2020 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2021 | */ |
| 2022 | char_u * |
| 2023 | find_word_start(char_u *ptr) |
| 2024 | { |
| 2025 | if (has_mbyte) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2026 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2027 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
| 2028 | ptr += (*mb_ptr2len)(ptr); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2029 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2030 | else |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2031 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2032 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2033 | ++ptr; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2034 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2035 | return ptr; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * Find the end of the word. Assumes it starts inside a word. |
| 2040 | * Returns a pointer to just after the word. |
| 2041 | */ |
| 2042 | char_u * |
| 2043 | find_word_end(char_u *ptr) |
| 2044 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2045 | if (has_mbyte) |
| 2046 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2047 | int start_class = mb_get_class(ptr); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2048 | if (start_class > 1) |
| 2049 | while (*ptr != NUL) |
| 2050 | { |
| 2051 | ptr += (*mb_ptr2len)(ptr); |
| 2052 | if (mb_get_class(ptr) != start_class) |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
| 2056 | else |
| 2057 | while (vim_iswordc(*ptr)) |
| 2058 | ++ptr; |
| 2059 | return ptr; |
| 2060 | } |
| 2061 | |
| 2062 | /* |
| 2063 | * Find the end of the line, omitting CR and NL at the end. |
| 2064 | * Returns a pointer to just after the line. |
| 2065 | */ |
glepnir | dd42b05 | 2025-03-08 16:52:55 +0100 | [diff] [blame] | 2066 | char_u * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2067 | find_line_end(char_u *ptr) |
| 2068 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2069 | char_u *s = ptr + STRLEN(ptr); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2070 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 2071 | --s; |
| 2072 | return s; |
| 2073 | } |
| 2074 | |
| 2075 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2076 | * Free a completion item in the list |
| 2077 | */ |
| 2078 | static void |
| 2079 | ins_compl_item_free(compl_T *match) |
| 2080 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2081 | VIM_CLEAR_STRING(match->cp_str); |
| 2082 | // several entries may use the same fname, free it just once. |
| 2083 | if (match->cp_flags & CP_FREE_FNAME) |
| 2084 | vim_free(match->cp_fname); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2085 | for (int i = 0; i < CPT_COUNT; ++i) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2086 | vim_free(match->cp_text[i]); |
| 2087 | #ifdef FEAT_EVAL |
| 2088 | clear_tv(&match->cp_user_data); |
| 2089 | #endif |
| 2090 | vim_free(match); |
| 2091 | } |
| 2092 | |
| 2093 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2094 | * Free the list of completions |
| 2095 | */ |
| 2096 | static void |
| 2097 | ins_compl_free(void) |
| 2098 | { |
| 2099 | compl_T *match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2100 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2101 | VIM_CLEAR_STRING(compl_pattern); |
| 2102 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2103 | |
| 2104 | if (compl_first_match == NULL) |
| 2105 | return; |
| 2106 | |
| 2107 | ins_compl_del_pum(); |
| 2108 | pum_clear(); |
| 2109 | |
| 2110 | compl_curr_match = compl_first_match; |
| 2111 | do |
| 2112 | { |
| 2113 | match = compl_curr_match; |
| 2114 | compl_curr_match = compl_curr_match->cp_next; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2115 | ins_compl_item_free(match); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2116 | } while (compl_curr_match != NULL && !is_first_match(compl_curr_match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2117 | compl_first_match = compl_curr_match = NULL; |
| 2118 | compl_shown_match = NULL; |
| 2119 | compl_old_match = NULL; |
| 2120 | } |
| 2121 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2122 | /* |
| 2123 | * Reset/clear the completion state. |
| 2124 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2125 | void |
| 2126 | ins_compl_clear(void) |
| 2127 | { |
| 2128 | compl_cont_status = 0; |
| 2129 | compl_started = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2130 | compl_cfc_longest_ins = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2131 | compl_matches = 0; |
glepnir | 07f0dbe | 2025-02-18 20:27:30 +0100 | [diff] [blame] | 2132 | compl_selected_item = -1; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2133 | compl_ins_end_col = 0; |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2134 | compl_curr_win = NULL; |
| 2135 | compl_curr_buf = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2136 | VIM_CLEAR_STRING(compl_pattern); |
| 2137 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2138 | edit_submode_extra = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2139 | VIM_CLEAR_STRING(compl_orig_text); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2140 | compl_enter_selects = FALSE; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 2141 | cpt_sources_clear(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2142 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2143 | // clear v:completed_item |
| 2144 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2145 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* |
| 2149 | * Return TRUE when Insert completion is active. |
| 2150 | */ |
| 2151 | int |
| 2152 | ins_compl_active(void) |
| 2153 | { |
| 2154 | return compl_started; |
| 2155 | } |
| 2156 | |
| 2157 | /* |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2158 | * Return True when wp is the actual completion window |
| 2159 | */ |
| 2160 | int |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2161 | ins_compl_win_active(win_T *wp) |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2162 | { |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2163 | return ins_compl_active() && wp == compl_curr_win |
| 2164 | && wp->w_buffer == compl_curr_buf; |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2168 | * Selected one of the matches. When FALSE, the match was either edited or |
| 2169 | * using the longest common string. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2170 | */ |
| 2171 | int |
| 2172 | ins_compl_used_match(void) |
| 2173 | { |
| 2174 | return compl_used_match; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Initialize get longest common string. |
| 2179 | */ |
| 2180 | void |
| 2181 | ins_compl_init_get_longest(void) |
| 2182 | { |
| 2183 | compl_get_longest = FALSE; |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * Returns TRUE when insert completion is interrupted. |
| 2188 | */ |
| 2189 | int |
| 2190 | ins_compl_interrupted(void) |
| 2191 | { |
| 2192 | return compl_interrupted; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Returns TRUE if the <Enter> key selects a match in the completion popup |
| 2197 | * menu. |
| 2198 | */ |
| 2199 | int |
| 2200 | ins_compl_enter_selects(void) |
| 2201 | { |
| 2202 | return compl_enter_selects; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Return the column where the text starts that is being completed |
| 2207 | */ |
| 2208 | colnr_T |
| 2209 | ins_compl_col(void) |
| 2210 | { |
| 2211 | return compl_col; |
| 2212 | } |
| 2213 | |
| 2214 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2215 | * Return the length in bytes of the text being completed |
| 2216 | */ |
| 2217 | int |
| 2218 | ins_compl_len(void) |
| 2219 | { |
| 2220 | return compl_length; |
| 2221 | } |
| 2222 | |
| 2223 | /* |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2224 | * Return TRUE when the 'completeopt' "preinsert" flag is in effect, |
| 2225 | * otherwise return FALSE. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2226 | */ |
| 2227 | static int |
| 2228 | ins_compl_has_preinsert(void) |
| 2229 | { |
glepnir | a2c5559 | 2025-02-28 17:43:42 +0100 | [diff] [blame] | 2230 | int cur_cot_flags = get_cot_flags(); |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2231 | return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE)) |
| 2232 | == (COT_PREINSERT | COT_MENUONE); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | * Returns TRUE if the pre-insert effect is valid and the cursor is within |
| 2237 | * the `compl_ins_end_col` range. |
| 2238 | */ |
| 2239 | int |
| 2240 | ins_compl_preinsert_effect(void) |
| 2241 | { |
| 2242 | if (!ins_compl_has_preinsert()) |
| 2243 | return FALSE; |
| 2244 | |
| 2245 | return curwin->w_cursor.col < compl_ins_end_col; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2249 | * Delete one character before the cursor and show the subset of the matches |
| 2250 | * that match the word that is now before the cursor. |
| 2251 | * Returns the character to be used, NUL if the work is done and another char |
| 2252 | * to be got from the user. |
| 2253 | */ |
| 2254 | int |
| 2255 | ins_compl_bs(void) |
| 2256 | { |
| 2257 | char_u *line; |
| 2258 | char_u *p; |
| 2259 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2260 | if (ins_compl_preinsert_effect()) |
| 2261 | ins_compl_delete(); |
| 2262 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2263 | line = ml_get_curline(); |
| 2264 | p = line + curwin->w_cursor.col; |
| 2265 | MB_PTR_BACK(line, p); |
| 2266 | |
| 2267 | // Stop completion when the whole word was deleted. For Omni completion |
| 2268 | // allow the word to be deleted, we won't match everything. |
| 2269 | // Respect the 'backspace' option. |
| 2270 | if ((int)(p - line) - (int)compl_col < 0 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2271 | || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni()) |
| 2272 | || ctrl_x_mode_eval() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2273 | || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col |
| 2274 | - compl_length < 0)) |
| 2275 | return K_BS; |
| 2276 | |
| 2277 | // Deleted more than what was used to find matches or didn't finish |
| 2278 | // finding all matches: need to look for matches all over again. |
| 2279 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 2280 | || ins_compl_need_restart()) |
| 2281 | ins_compl_restart(); |
| 2282 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2283 | VIM_CLEAR_STRING(compl_leader); |
| 2284 | compl_leader.length = (size_t)((p - line) - compl_col); |
| 2285 | compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length); |
| 2286 | if (compl_leader.string == NULL) |
| 2287 | { |
| 2288 | compl_leader.length = 0; |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2289 | return K_BS; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2290 | } |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2291 | |
| 2292 | ins_compl_new_leader(); |
| 2293 | if (compl_shown_match != NULL) |
| 2294 | // Make sure current match is not a hidden item. |
| 2295 | compl_curr_match = compl_shown_match; |
| 2296 | return NUL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 2301 | * be called. |
| 2302 | */ |
| 2303 | static int |
| 2304 | ins_compl_need_restart(void) |
| 2305 | { |
| 2306 | // Return TRUE if we didn't complete finding matches or when the |
| 2307 | // 'completefunc' returned "always" in the "refresh" dictionary item. |
| 2308 | return compl_was_interrupted |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2309 | || ((ctrl_x_mode_function() || ctrl_x_mode_omni()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2310 | && compl_opt_refresh_always); |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * Called after changing "compl_leader". |
| 2315 | * Show the popup menu with a different set of matches. |
| 2316 | * May also search for matches again if the previous search was interrupted. |
| 2317 | */ |
| 2318 | static void |
| 2319 | ins_compl_new_leader(void) |
| 2320 | { |
| 2321 | ins_compl_del_pum(); |
| 2322 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2323 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2324 | compl_used_match = FALSE; |
| 2325 | |
| 2326 | if (compl_started) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2327 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2328 | ins_compl_set_original_text(compl_leader.string, compl_leader.length); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2329 | if (is_cpt_func_refresh_always()) |
| 2330 | cpt_compl_refresh(); |
| 2331 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2332 | else |
| 2333 | { |
| 2334 | #ifdef FEAT_SPELL |
| 2335 | spell_bad_len = 0; // need to redetect bad word |
| 2336 | #endif |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2337 | // Matches were cleared, need to search for them now. Before drawing |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2338 | // the popup menu display the changed text before the cursor. Set |
| 2339 | // "compl_restarting" to avoid that the first match is inserted. |
| 2340 | pum_call_update_screen(); |
| 2341 | #ifdef FEAT_GUI |
| 2342 | if (gui.in_use) |
| 2343 | { |
| 2344 | // Show the cursor after the match, not after the redrawn text. |
| 2345 | setcursor(); |
| 2346 | out_flush_cursor(FALSE, FALSE); |
| 2347 | } |
| 2348 | #endif |
| 2349 | compl_restarting = TRUE; |
| 2350 | if (ins_complete(Ctrl_N, TRUE) == FAIL) |
| 2351 | compl_cont_status = 0; |
| 2352 | compl_restarting = FALSE; |
| 2353 | } |
| 2354 | |
glepnir | 4418041 | 2025-02-20 22:09:48 +0100 | [diff] [blame] | 2355 | compl_enter_selects = !compl_used_match && compl_selected_item != -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2356 | |
| 2357 | // Show the popup menu with a different set of matches. |
| 2358 | ins_compl_show_pum(); |
| 2359 | |
| 2360 | // Don't let Enter select the original text when there is no popup menu. |
| 2361 | if (compl_match_array == NULL) |
| 2362 | compl_enter_selects = FALSE; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2363 | else if (ins_compl_has_preinsert() && compl_leader.length > 0) |
| 2364 | ins_compl_insert(FALSE, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Return the length of the completion, from the completion start column to |
| 2369 | * the cursor column. Making sure it never goes below zero. |
| 2370 | */ |
| 2371 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2372 | get_compl_len(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2373 | { |
| 2374 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2375 | return MAX(0, off); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * Append one character to the match leader. May reduce the number of |
| 2380 | * matches. |
| 2381 | */ |
| 2382 | void |
| 2383 | ins_compl_addleader(int c) |
| 2384 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2385 | int cc; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2386 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2387 | if (ins_compl_preinsert_effect()) |
| 2388 | ins_compl_delete(); |
| 2389 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2390 | if (stop_arrow() == FAIL) |
| 2391 | return; |
| 2392 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2393 | { |
| 2394 | char_u buf[MB_MAXBYTES + 1]; |
| 2395 | |
| 2396 | (*mb_char2bytes)(c, buf); |
| 2397 | buf[cc] = NUL; |
| 2398 | ins_char_bytes(buf, cc); |
| 2399 | if (compl_opt_refresh_always) |
| 2400 | AppendToRedobuff(buf); |
| 2401 | } |
| 2402 | else |
| 2403 | { |
| 2404 | ins_char(c); |
| 2405 | if (compl_opt_refresh_always) |
| 2406 | AppendCharToRedobuff(c); |
| 2407 | } |
| 2408 | |
| 2409 | // If we didn't complete finding matches we must search again. |
| 2410 | if (ins_compl_need_restart()) |
| 2411 | ins_compl_restart(); |
| 2412 | |
| 2413 | // When 'always' is set, don't reset compl_leader. While completing, |
| 2414 | // cursor doesn't point original position, changing compl_leader would |
| 2415 | // break redo. |
| 2416 | if (!compl_opt_refresh_always) |
| 2417 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2418 | VIM_CLEAR_STRING(compl_leader); |
| 2419 | compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col); |
| 2420 | compl_leader.string = vim_strnsave(ml_get_curline() + compl_col, |
| 2421 | compl_leader.length); |
| 2422 | if (compl_leader.string == NULL) |
| 2423 | { |
| 2424 | compl_leader.length = 0; |
| 2425 | return; |
| 2426 | } |
| 2427 | |
| 2428 | ins_compl_new_leader(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 2434 | * BS or a key was typed while still searching for matches. |
| 2435 | */ |
| 2436 | static void |
| 2437 | ins_compl_restart(void) |
| 2438 | { |
| 2439 | ins_compl_free(); |
| 2440 | compl_started = FALSE; |
| 2441 | compl_matches = 0; |
| 2442 | compl_cont_status = 0; |
| 2443 | compl_cont_mode = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 2444 | cpt_sources_clear(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | /* |
| 2448 | * Set the first match, the original text. |
| 2449 | */ |
| 2450 | static void |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2451 | ins_compl_set_original_text(char_u *str, size_t len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2452 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2453 | // Replace the original text entry. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2454 | // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly |
| 2455 | // be at the last item for backward completion |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2456 | if (match_at_original_text(compl_first_match)) // safety check |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2457 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2458 | char_u *p = vim_strnsave(str, len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2459 | if (p != NULL) |
| 2460 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2461 | VIM_CLEAR_STRING(compl_first_match->cp_str); |
| 2462 | compl_first_match->cp_str.string = p; |
| 2463 | compl_first_match->cp_str.length = len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | else if (compl_first_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2467 | && match_at_original_text(compl_first_match->cp_prev)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2468 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2469 | char_u *p = vim_strnsave(str, len); |
| 2470 | if (p != NULL) |
| 2471 | { |
| 2472 | VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str); |
| 2473 | compl_first_match->cp_prev->cp_str.string = p; |
| 2474 | compl_first_match->cp_prev->cp_str.length = len; |
| 2475 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | * Append one character to the match leader. May reduce the number of |
| 2481 | * matches. |
| 2482 | */ |
| 2483 | void |
| 2484 | ins_compl_addfrommatch(void) |
| 2485 | { |
| 2486 | char_u *p; |
| 2487 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
| 2488 | int c; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2489 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2490 | p = compl_shown_match->cp_str.string; |
| 2491 | 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] | 2492 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2493 | size_t plen; |
| 2494 | compl_T *cp; |
| 2495 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2496 | // When still at the original match use the first entry that matches |
| 2497 | // the leader. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2498 | if (!match_at_original_text(compl_shown_match)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2499 | return; |
| 2500 | |
| 2501 | p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2502 | plen = 0; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2503 | for (cp = compl_shown_match->cp_next; cp != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2504 | && !is_first_match(cp); cp = cp->cp_next) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2505 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2506 | if (compl_leader.string == NULL |
| 2507 | || ins_compl_equal(cp, compl_leader.string, |
| 2508 | (int)compl_leader.length)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2509 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2510 | p = cp->cp_str.string; |
| 2511 | plen = cp->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2512 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2513 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2514 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2515 | if (p == NULL || (int)plen <= len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2516 | return; |
| 2517 | } |
| 2518 | p += len; |
| 2519 | c = PTR2CHAR(p); |
| 2520 | ins_compl_addleader(c); |
| 2521 | } |
| 2522 | |
| 2523 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2524 | * 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] | 2525 | * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre, |
| 2526 | * compl_cont_mode and compl_cont_status. |
| 2527 | * Returns TRUE when the character is not to be inserted. |
| 2528 | */ |
| 2529 | static int |
| 2530 | set_ctrl_x_mode(int c) |
| 2531 | { |
| 2532 | int retval = FALSE; |
| 2533 | |
| 2534 | switch (c) |
| 2535 | { |
| 2536 | case Ctrl_E: |
| 2537 | case Ctrl_Y: |
| 2538 | // scroll the window one line up or down |
| 2539 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2540 | if (!(State & REPLACE_FLAG)) |
| 2541 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2542 | else |
| 2543 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2544 | edit_submode_pre = NULL; |
| 2545 | showmode(); |
| 2546 | break; |
| 2547 | case Ctrl_L: |
| 2548 | // complete whole line |
| 2549 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2550 | break; |
| 2551 | case Ctrl_F: |
| 2552 | // complete filenames |
| 2553 | ctrl_x_mode = CTRL_X_FILES; |
| 2554 | break; |
| 2555 | case Ctrl_K: |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 2556 | // complete words from a dictionary |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2557 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2558 | break; |
| 2559 | case Ctrl_R: |
| 2560 | // Register insertion without exiting CTRL-X mode |
| 2561 | // Simply allow ^R to happen without affecting ^X mode |
| 2562 | break; |
| 2563 | case Ctrl_T: |
| 2564 | // complete words from a thesaurus |
| 2565 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2566 | break; |
| 2567 | #ifdef FEAT_COMPL_FUNC |
| 2568 | case Ctrl_U: |
| 2569 | // user defined completion |
| 2570 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2571 | break; |
| 2572 | case Ctrl_O: |
| 2573 | // omni completion |
| 2574 | ctrl_x_mode = CTRL_X_OMNI; |
| 2575 | break; |
| 2576 | #endif |
| 2577 | case 's': |
| 2578 | case Ctrl_S: |
| 2579 | // complete spelling suggestions |
| 2580 | ctrl_x_mode = CTRL_X_SPELL; |
| 2581 | #ifdef FEAT_SPELL |
| 2582 | ++emsg_off; // Avoid getting the E756 error twice. |
| 2583 | spell_back_to_badword(); |
| 2584 | --emsg_off; |
| 2585 | #endif |
| 2586 | break; |
| 2587 | case Ctrl_RSB: |
| 2588 | // complete tag names |
| 2589 | ctrl_x_mode = CTRL_X_TAGS; |
| 2590 | break; |
| 2591 | #ifdef FEAT_FIND_ID |
| 2592 | case Ctrl_I: |
| 2593 | case K_S_TAB: |
| 2594 | // complete keywords from included files |
| 2595 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2596 | break; |
| 2597 | case Ctrl_D: |
| 2598 | // complete definitions from included files |
| 2599 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2600 | break; |
| 2601 | #endif |
| 2602 | case Ctrl_V: |
| 2603 | case Ctrl_Q: |
| 2604 | // complete vim commands |
| 2605 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2606 | break; |
| 2607 | case Ctrl_Z: |
| 2608 | // stop completion |
| 2609 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2610 | edit_submode = NULL; |
| 2611 | showmode(); |
| 2612 | retval = TRUE; |
| 2613 | break; |
| 2614 | case Ctrl_P: |
| 2615 | case Ctrl_N: |
| 2616 | // ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2617 | // just started ^X mode, or there were enough ^X's to cancel |
| 2618 | // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2619 | // do normal expansion when interrupting a different mode (say |
| 2620 | // ^X^F^X^P or ^P^X^X^P, see below) |
| 2621 | // nothing changes if interrupting mode 0, (eg, the flag |
| 2622 | // doesn't change when going to ADDING mode -- Acevedo |
| 2623 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2624 | compl_cont_status |= CONT_LOCAL; |
| 2625 | else if (compl_cont_mode != 0) |
| 2626 | compl_cont_status &= ~CONT_LOCAL; |
| 2627 | // FALLTHROUGH |
| 2628 | default: |
| 2629 | // If we have typed at least 2 ^X's... for modes != 0, we set |
| 2630 | // compl_cont_status = 0 (eg, as if we had just started ^X |
| 2631 | // mode). |
| 2632 | // For mode 0, we set "compl_cont_mode" to an impossible |
| 2633 | // value, in both cases ^X^X can be used to restart the same |
| 2634 | // mode (avoiding ADDING mode). |
| 2635 | // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2636 | // 'complete' and local ^P expansions respectively. |
| 2637 | // In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2638 | // mode -- Acevedo |
| 2639 | if (c == Ctrl_X) |
| 2640 | { |
| 2641 | if (compl_cont_mode != 0) |
| 2642 | compl_cont_status = 0; |
| 2643 | else |
| 2644 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
| 2645 | } |
| 2646 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2647 | edit_submode = NULL; |
| 2648 | showmode(); |
| 2649 | break; |
| 2650 | } |
| 2651 | |
| 2652 | return retval; |
| 2653 | } |
| 2654 | |
| 2655 | /* |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2656 | * Trigger CompleteDone event and adds relevant information to v:event |
| 2657 | */ |
| 2658 | static void |
| 2659 | trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED) |
| 2660 | { |
| 2661 | #if defined(FEAT_EVAL) |
| 2662 | save_v_event_T save_v_event; |
| 2663 | dict_T *v_event = get_v_event(&save_v_event); |
| 2664 | char_u *mode_str = NULL; |
| 2665 | |
| 2666 | mode = mode & ~CTRL_X_WANT_IDENT; |
| 2667 | if (ctrl_x_mode_names[mode]) |
| 2668 | mode_str = (char_u *)ctrl_x_mode_names[mode]; |
| 2669 | |
| 2670 | (void)dict_add_string(v_event, "complete_word", |
| 2671 | word == NULL ? (char_u *)"" : word); |
| 2672 | (void)dict_add_string(v_event, "complete_type", |
| 2673 | mode_str != NULL ? mode_str : (char_u *)""); |
| 2674 | |
| 2675 | dict_set_items_ro(v_event); |
| 2676 | #endif |
| 2677 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
| 2678 | |
| 2679 | #if defined(FEAT_EVAL) |
| 2680 | restore_v_event(v_event, &save_v_event); |
| 2681 | #endif |
| 2682 | } |
| 2683 | |
| 2684 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2685 | * Stop insert completion mode |
| 2686 | */ |
| 2687 | static int |
| 2688 | ins_compl_stop(int c, int prev_mode, int retval) |
| 2689 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2690 | int want_cindent; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2691 | char_u *word = NULL; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2692 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 2693 | // Remove pre-inserted text when present. |
zeertzjq | 1343681 | 2025-04-23 20:46:35 +0200 | [diff] [blame] | 2694 | if (ins_compl_preinsert_effect() && ins_compl_win_active(curwin)) |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 2695 | ins_compl_delete(); |
| 2696 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2697 | // Get here when we have finished typing a sequence of ^N and |
| 2698 | // ^P or other completion characters in CTRL-X mode. Free up |
| 2699 | // memory that was used, and make sure we can redo the insert. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2700 | if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2701 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2702 | char_u *ptr = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2703 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2704 | // If any of the original typed text has been changed, eg when |
| 2705 | // ignorecase is set, we must add back-spaces to the redo |
| 2706 | // buffer. We add as few as necessary to delete just the part |
| 2707 | // of the original text that has changed. |
| 2708 | // When using the longest match, edited the match or used |
| 2709 | // CTRL-E then don't use the current match. |
| 2710 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2711 | ptr = compl_curr_match->cp_str.string; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2712 | ins_compl_fixRedoBufForLeader(ptr); |
| 2713 | } |
| 2714 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2715 | want_cindent = (get_can_cindent() && cindent_on()); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2716 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2717 | // When completing whole lines: fix indent for 'cindent'. |
| 2718 | // Otherwise, break line if it's too long. |
| 2719 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
| 2720 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2721 | // re-indent the current line |
| 2722 | if (want_cindent) |
| 2723 | { |
| 2724 | do_c_expr_indent(); |
| 2725 | want_cindent = FALSE; // don't do it again |
| 2726 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2727 | } |
| 2728 | else |
| 2729 | { |
| 2730 | int prev_col = curwin->w_cursor.col; |
| 2731 | |
| 2732 | // put the cursor on the last char, for 'tw' formatting |
| 2733 | if (prev_col > 0) |
| 2734 | dec_cursor(); |
| 2735 | // only format when something was inserted |
| 2736 | if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) |
| 2737 | insertchar(NUL, 0, -1); |
| 2738 | if (prev_col > 0 |
| 2739 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 2740 | inc_cursor(); |
| 2741 | } |
| 2742 | |
| 2743 | // If the popup menu is displayed pressing CTRL-Y means accepting |
| 2744 | // the selection without inserting anything. When |
| 2745 | // compl_enter_selects is set the Enter key does the same. |
| 2746 | if ((c == Ctrl_Y || (compl_enter_selects |
| 2747 | && (c == CAR || c == K_KENTER || c == NL))) |
| 2748 | && pum_visible()) |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2749 | { |
| 2750 | word = vim_strsave(compl_shown_match->cp_str.string); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2751 | retval = TRUE; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2752 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2753 | |
| 2754 | // CTRL-E means completion is Ended, go back to the typed text. |
| 2755 | // but only do this, if the Popup is still visible |
| 2756 | if (c == Ctrl_E) |
| 2757 | { |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2758 | char_u *p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2759 | size_t plen = 0; |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2760 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2761 | ins_compl_delete(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2762 | if (compl_leader.string != NULL) |
| 2763 | { |
| 2764 | p = compl_leader.string; |
| 2765 | plen = compl_leader.length; |
| 2766 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2767 | else if (compl_first_match != NULL) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2768 | { |
| 2769 | p = compl_orig_text.string; |
| 2770 | plen = compl_orig_text.length; |
| 2771 | } |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2772 | if (p != NULL) |
| 2773 | { |
| 2774 | int compl_len = get_compl_len(); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2775 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2776 | if ((int)plen > compl_len) |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 2777 | ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2778 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2779 | retval = TRUE; |
| 2780 | } |
| 2781 | |
| 2782 | auto_format(FALSE, TRUE); |
| 2783 | |
| 2784 | // Trigger the CompleteDonePre event to give scripts a chance to |
| 2785 | // act upon the completion before clearing the info, and restore |
| 2786 | // ctrl_x_mode, so that complete_info() can be used. |
| 2787 | ctrl_x_mode = prev_mode; |
| 2788 | ins_apply_autocmds(EVENT_COMPLETEDONEPRE); |
| 2789 | |
| 2790 | ins_compl_free(); |
| 2791 | compl_started = FALSE; |
| 2792 | compl_matches = 0; |
| 2793 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 2794 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 2795 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2796 | compl_enter_selects = FALSE; |
| 2797 | if (edit_submode != NULL) |
| 2798 | { |
| 2799 | edit_submode = NULL; |
| 2800 | showmode(); |
| 2801 | } |
| 2802 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2803 | if (c == Ctrl_C && cmdwin_type != 0) |
| 2804 | // Avoid the popup menu remains displayed when leaving the |
| 2805 | // command line window. |
| 2806 | update_screen(0); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2807 | // Indent now if a key was typed that is in 'cinkeys'. |
| 2808 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2809 | do_c_expr_indent(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2810 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2811 | // upon the end of completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2812 | trigger_complete_done_event(prev_mode, word); |
| 2813 | vim_free(word); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2814 | |
| 2815 | return retval; |
| 2816 | } |
| 2817 | |
| 2818 | /* |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2819 | * Cancel completion. |
| 2820 | */ |
| 2821 | int |
| 2822 | ins_compl_cancel(void) |
| 2823 | { |
| 2824 | return ins_compl_stop(' ', ctrl_x_mode, TRUE); |
| 2825 | } |
| 2826 | |
| 2827 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2828 | * Prepare for Insert mode completion, or stop it. |
| 2829 | * Called just after typing a character in Insert mode. |
| 2830 | * Returns TRUE when the character is not to be inserted; |
| 2831 | */ |
| 2832 | int |
| 2833 | ins_compl_prep(int c) |
| 2834 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2835 | int retval = FALSE; |
| 2836 | int prev_mode = ctrl_x_mode; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2837 | |
| 2838 | // Forget any previous 'special' messages if this is actually |
| 2839 | // a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2840 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2841 | edit_submode_extra = NULL; |
| 2842 | |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2843 | // Ignore end of Select mode mapping and mouse scroll/movement. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2844 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2845 | || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 2846 | || c == K_COMMAND || c == K_SCRIPT_COMMAND) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2847 | return retval; |
| 2848 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2849 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | f0bc15c | 2019-08-18 19:23:45 +0200 | [diff] [blame] | 2850 | // Ignore mouse events in a popup window |
| 2851 | if (is_mouse_key(c)) |
| 2852 | { |
| 2853 | // Ignore drag and release events, the position does not need to be in |
| 2854 | // the popup and it may have just closed. |
| 2855 | if (c == K_LEFTRELEASE |
| 2856 | || c == K_LEFTRELEASE_NM |
| 2857 | || c == K_MIDDLERELEASE |
| 2858 | || c == K_RIGHTRELEASE |
| 2859 | || c == K_X1RELEASE |
| 2860 | || c == K_X2RELEASE |
| 2861 | || c == K_LEFTDRAG |
| 2862 | || c == K_MIDDLEDRAG |
| 2863 | || c == K_RIGHTDRAG |
| 2864 | || c == K_X1DRAG |
| 2865 | || c == K_X2DRAG) |
| 2866 | return retval; |
| 2867 | if (popup_visible) |
| 2868 | { |
| 2869 | int row = mouse_row; |
| 2870 | int col = mouse_col; |
| 2871 | win_T *wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2872 | |
| 2873 | if (wp != NULL && WIN_IS_POPUP(wp)) |
| 2874 | return retval; |
| 2875 | } |
| 2876 | } |
| 2877 | #endif |
| 2878 | |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 2879 | if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X) |
| 2880 | { |
| 2881 | if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c) |
| 2882 | || !vim_is_ctrl_x_key(c)) |
| 2883 | { |
| 2884 | // Not starting another completion mode. |
| 2885 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2886 | |
| 2887 | // CTRL-X CTRL-Z should stop completion without inserting anything |
| 2888 | if (c == Ctrl_Z) |
| 2889 | retval = TRUE; |
| 2890 | } |
| 2891 | else |
| 2892 | { |
| 2893 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2894 | |
| 2895 | // Other CTRL-X keys first stop completion, then start another |
| 2896 | // completion mode. |
| 2897 | ins_compl_prep(' '); |
| 2898 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2899 | } |
| 2900 | } |
| 2901 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2902 | // Set "compl_get_longest" when finding the first matches. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2903 | if (ctrl_x_mode_not_defined_yet() |
| 2904 | || (ctrl_x_mode_normal() && !compl_started)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2905 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 2906 | compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2907 | compl_used_match = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2910 | if (ctrl_x_mode_not_defined_yet()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2911 | // We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2912 | // it will be yet. Now we decide. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2913 | retval = set_ctrl_x_mode(c); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2914 | else if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2915 | { |
| 2916 | // We're already in CTRL-X mode, do we stay in it? |
| 2917 | if (!vim_is_ctrl_x_key(c)) |
| 2918 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2919 | 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] | 2920 | edit_submode = NULL; |
| 2921 | } |
| 2922 | showmode(); |
| 2923 | } |
| 2924 | |
| 2925 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
| 2926 | { |
| 2927 | // Show error message from attempted keyword completion (probably |
| 2928 | // 'Pattern not found') until another key is hit, then go back to |
| 2929 | // showing what mode we are in. |
| 2930 | showmode(); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2931 | if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2932 | && c != Ctrl_R && !ins_compl_pum_key(c)) |
| 2933 | || ctrl_x_mode == CTRL_X_FINISHED) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2934 | retval = ins_compl_stop(c, prev_mode, retval); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2935 | } |
| 2936 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 2937 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2938 | // upon the (possibly failed) completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2939 | trigger_complete_done_event(ctrl_x_mode, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2940 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 2941 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 2942 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2943 | // reset continue_* if we left expansion-mode, if we stay they'll be |
| 2944 | // (re)set properly in ins_complete() |
| 2945 | if (!vim_is_ctrl_x_key(c)) |
| 2946 | { |
| 2947 | compl_cont_status = 0; |
| 2948 | compl_cont_mode = 0; |
| 2949 | } |
| 2950 | |
| 2951 | return retval; |
| 2952 | } |
| 2953 | |
| 2954 | /* |
| 2955 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 2956 | * text. This inserts backspaces and appends the changed text. |
| 2957 | * "ptr" is the known leader text or NUL. |
| 2958 | */ |
| 2959 | static void |
| 2960 | ins_compl_fixRedoBufForLeader(char_u *ptr_arg) |
| 2961 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2962 | int len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2963 | char_u *p; |
| 2964 | char_u *ptr = ptr_arg; |
| 2965 | |
| 2966 | if (ptr == NULL) |
| 2967 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2968 | if (compl_leader.string != NULL) |
| 2969 | ptr = compl_leader.string; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2970 | else |
| 2971 | return; // nothing to do |
| 2972 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2973 | if (compl_orig_text.string != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2974 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2975 | p = compl_orig_text.string; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2976 | // Find length of common prefix between original text and new completion |
| 2977 | while (p[len] != NUL && p[len] == ptr[len]) |
| 2978 | len++; |
| 2979 | // Adjust length to not break inside a multi-byte character |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2980 | if (len > 0) |
| 2981 | len -= (*mb_head_off)(p, p + len); |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2982 | // Add backspace characters for each remaining character in |
| 2983 | // original text |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2984 | for (p += len; *p != NUL; MB_PTR_ADV(p)) |
| 2985 | AppendCharToRedobuff(K_BS); |
| 2986 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2987 | if (ptr != NULL) |
| 2988 | AppendToRedobuffLit(ptr + len, -1); |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 2993 | * (depending on flag) starting from buf and looking for a non-scanned |
| 2994 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 2995 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 2996 | * |
| 2997 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 2998 | */ |
| 2999 | static buf_T * |
| 3000 | ins_compl_next_buf(buf_T *buf, int flag) |
| 3001 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3002 | static win_T *wp = NULL; |
| 3003 | int skip_buffer; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3004 | |
| 3005 | if (flag == 'w') // just windows |
| 3006 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3007 | if (buf == curbuf || !win_valid(wp)) |
| 3008 | // first call for this flag/expansion or window was closed |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3009 | wp = curwin; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3010 | |
| 3011 | while (TRUE) |
| 3012 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3013 | // Move to next window (wrap to first window if at the end) |
| 3014 | wp = (wp->w_next != NULL) ? wp->w_next : firstwin; |
| 3015 | // Break if we're back at start or found an unscanned buffer |
| 3016 | if (wp == curwin || !wp->w_buffer->b_scanned) |
| 3017 | break; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3018 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3019 | buf = wp->w_buffer; |
| 3020 | } |
| 3021 | else |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3022 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3023 | // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3024 | // (unlisted buffers) |
| 3025 | // When completing whole lines skip unloaded buffers. |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3026 | while (TRUE) |
| 3027 | { |
| 3028 | // Move to next buffer (wrap to first buffer if at the end) |
| 3029 | buf = (buf->b_next != NULL) ? buf->b_next : firstbuf; |
| 3030 | // Break if we're back at start buffer |
| 3031 | if (buf == curbuf) |
| 3032 | break; |
| 3033 | |
| 3034 | // Check buffer conditions based on flag |
| 3035 | if (flag == 'U') |
| 3036 | skip_buffer = buf->b_p_bl; |
| 3037 | else |
| 3038 | skip_buffer = !buf->b_p_bl || |
| 3039 | (buf->b_ml.ml_mfp == NULL) != (flag == 'u'); |
| 3040 | |
| 3041 | // Break if we found a buffer that matches our criteria |
| 3042 | if (!skip_buffer && !buf->b_scanned) |
| 3043 | break; |
| 3044 | } |
| 3045 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3046 | return buf; |
| 3047 | } |
| 3048 | |
| 3049 | #ifdef FEAT_COMPL_FUNC |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3050 | |
| 3051 | # ifdef FEAT_EVAL |
| 3052 | static callback_T cfu_cb; // 'completefunc' callback function |
| 3053 | static callback_T ofu_cb; // 'omnifunc' callback function |
| 3054 | static callback_T tsrfu_cb; // 'thesaurusfunc' callback function |
| 3055 | # endif |
| 3056 | |
| 3057 | /* |
| 3058 | * Copy a global callback function to a buffer local callback. |
| 3059 | */ |
| 3060 | static void |
| 3061 | copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb) |
| 3062 | { |
| 3063 | free_callback(bufcb); |
| 3064 | if (globcb->cb_name != NULL && *globcb->cb_name != NUL) |
| 3065 | copy_callback(bufcb, globcb); |
| 3066 | } |
| 3067 | |
| 3068 | /* |
| 3069 | * Parse the 'completefunc' option value and set the callback function. |
| 3070 | * Invoked when the 'completefunc' option is set. The option value can be a |
| 3071 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3072 | * lambda expression. |
| 3073 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3074 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3075 | did_set_completefunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3076 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3077 | if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL) |
| 3078 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3079 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3080 | set_buflocal_cfu_callback(curbuf); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3081 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3082 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * Copy the global 'completefunc' callback function to the buffer-local |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3087 | * 'completefunc' callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3088 | */ |
| 3089 | void |
| 3090 | set_buflocal_cfu_callback(buf_T *buf UNUSED) |
| 3091 | { |
| 3092 | # ifdef FEAT_EVAL |
| 3093 | copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb); |
| 3094 | # endif |
| 3095 | } |
| 3096 | |
| 3097 | /* |
| 3098 | * Parse the 'omnifunc' option value and set the callback function. |
| 3099 | * Invoked when the 'omnifunc' option is set. The option value can be a |
| 3100 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3101 | * lambda expression. |
| 3102 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3103 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3104 | did_set_omnifunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3105 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3106 | if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL) |
| 3107 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3108 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3109 | set_buflocal_ofu_callback(curbuf); |
| 3110 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /* |
| 3114 | * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc' |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3115 | * callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3116 | */ |
| 3117 | void |
| 3118 | set_buflocal_ofu_callback(buf_T *buf UNUSED) |
| 3119 | { |
| 3120 | # ifdef FEAT_EVAL |
| 3121 | copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb); |
| 3122 | # endif |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * Parse the 'thesaurusfunc' option value and set the callback function. |
| 3127 | * Invoked when the 'thesaurusfunc' option is set. The option value can be a |
| 3128 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3129 | * lambda expression. |
| 3130 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3131 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3132 | did_set_thesaurusfunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3133 | { |
| 3134 | int retval; |
| 3135 | |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3136 | if (args->os_flags & OPT_LOCAL) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3137 | // buffer-local option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3138 | retval = option_set_callback_func(curbuf->b_p_tsrfu, |
| 3139 | &curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3140 | else |
| 3141 | { |
| 3142 | // global option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3143 | retval = option_set_callback_func(p_tsrfu, &tsrfu_cb); |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3144 | // when using :set, free the local callback |
| 3145 | if (!(args->os_flags & OPT_GLOBAL)) |
| 3146 | free_callback(&curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3149 | return retval == FAIL ? e_invalid_argument : NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3152 | /* |
| 3153 | * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3154 | * "copyID" so that they are not garbage collected. |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3155 | */ |
| 3156 | int |
| 3157 | set_ref_in_insexpand_funcs(int copyID) |
| 3158 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3159 | int abort = set_ref_in_callback(&cfu_cb, copyID); |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3160 | abort = abort || set_ref_in_callback(&ofu_cb, copyID); |
| 3161 | abort = abort || set_ref_in_callback(&tsrfu_cb, copyID); |
| 3162 | |
| 3163 | return abort; |
| 3164 | } |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3165 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3166 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3167 | * Get the user-defined completion function name for completion "type" |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3168 | */ |
| 3169 | static char_u * |
| 3170 | get_complete_funcname(int type) |
| 3171 | { |
| 3172 | switch (type) |
| 3173 | { |
| 3174 | case CTRL_X_FUNCTION: |
| 3175 | return curbuf->b_p_cfu; |
| 3176 | case CTRL_X_OMNI: |
| 3177 | return curbuf->b_p_ofu; |
| 3178 | case CTRL_X_THESAURUS: |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3179 | return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu; |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3180 | default: |
| 3181 | return (char_u *)""; |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | /* |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3186 | * Get the callback to use for insert mode completion. |
| 3187 | */ |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3188 | static callback_T * |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3189 | get_insert_callback(int type) |
| 3190 | { |
| 3191 | if (type == CTRL_X_FUNCTION) |
| 3192 | return &curbuf->b_cfu_cb; |
| 3193 | if (type == CTRL_X_OMNI) |
| 3194 | return &curbuf->b_ofu_cb; |
| 3195 | // CTRL_X_THESAURUS |
| 3196 | return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb; |
| 3197 | } |
| 3198 | |
| 3199 | /* |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 3200 | * Execute user defined complete function 'completefunc', 'omnifunc' or |
| 3201 | * 'thesaurusfunc', and get matches in "matches". |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3202 | * "type" can be one of CTRL_X_OMNI, CTRL_X_FUNCTION, or CTRL_X_THESAURUS. |
| 3203 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 3204 | * option; otherwise, it is NULL. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3205 | */ |
| 3206 | static void |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3207 | expand_by_function(int type, char_u *base, callback_T *cb) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3208 | { |
| 3209 | list_T *matchlist = NULL; |
| 3210 | dict_T *matchdict = NULL; |
| 3211 | typval_T args[3]; |
| 3212 | char_u *funcname; |
| 3213 | pos_T pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3214 | typval_T rettv; |
| 3215 | int save_State = State; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3216 | int retval; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3217 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3218 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3219 | if (!is_cpt_function) |
| 3220 | { |
| 3221 | funcname = get_complete_funcname(type); |
| 3222 | if (*funcname == NUL) |
| 3223 | return; |
| 3224 | cb = get_insert_callback(type); |
| 3225 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3226 | |
| 3227 | // Call 'completefunc' to obtain the list of matches. |
| 3228 | args[0].v_type = VAR_NUMBER; |
| 3229 | args[0].vval.v_number = 0; |
| 3230 | args[1].v_type = VAR_STRING; |
| 3231 | args[1].vval.v_string = base != NULL ? base : (char_u *)""; |
| 3232 | args[2].v_type = VAR_UNKNOWN; |
| 3233 | |
| 3234 | pos = curwin->w_cursor; |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 3235 | // Lock the text to avoid weird things from happening. Also disallow |
| 3236 | // switching to another window, it should not be needed and may end up in |
| 3237 | // Insert mode in another buffer. |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3238 | ++textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3239 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3240 | retval = call_callback(cb, 0, &rettv, 2, args); |
| 3241 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3242 | // Call a function, which returns a list or dict. |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3243 | if (retval == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3244 | { |
| 3245 | switch (rettv.v_type) |
| 3246 | { |
| 3247 | case VAR_LIST: |
| 3248 | matchlist = rettv.vval.v_list; |
| 3249 | break; |
| 3250 | case VAR_DICT: |
| 3251 | matchdict = rettv.vval.v_dict; |
| 3252 | break; |
| 3253 | case VAR_SPECIAL: |
| 3254 | if (rettv.vval.v_number == VVAL_NONE) |
| 3255 | compl_opt_suppress_empty = TRUE; |
| 3256 | // FALLTHROUGH |
| 3257 | default: |
| 3258 | // TODO: Give error message? |
| 3259 | clear_tv(&rettv); |
| 3260 | break; |
| 3261 | } |
| 3262 | } |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3263 | --textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3264 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3265 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 3266 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3267 | validate_cursor(); |
| 3268 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 3269 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 3270 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3271 | goto theend; |
| 3272 | } |
| 3273 | |
| 3274 | if (matchlist != NULL) |
| 3275 | ins_compl_add_list(matchlist); |
| 3276 | else if (matchdict != NULL) |
| 3277 | ins_compl_add_dict(matchdict); |
| 3278 | |
| 3279 | theend: |
| 3280 | // Restore State, it might have been changed. |
| 3281 | State = save_State; |
| 3282 | |
| 3283 | if (matchdict != NULL) |
| 3284 | dict_unref(matchdict); |
| 3285 | if (matchlist != NULL) |
| 3286 | list_unref(matchlist); |
| 3287 | } |
| 3288 | #endif // FEAT_COMPL_FUNC |
| 3289 | |
| 3290 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3291 | |
| 3292 | static inline int |
| 3293 | get_user_highlight_attr(char_u *hlname) |
| 3294 | { |
| 3295 | if (hlname != NULL && *hlname != NUL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3296 | return syn_name2attr(hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3297 | return -1; |
| 3298 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3299 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3300 | * Add a match to the list of matches from a typeval_T. |
| 3301 | * If the given string is already in the list of completions, then return |
| 3302 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 3303 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3304 | * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck(). |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3305 | */ |
| 3306 | static int |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3307 | ins_compl_add_tv(typval_T *tv, int dir, int fast) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3308 | { |
| 3309 | char_u *word; |
| 3310 | int dup = FALSE; |
| 3311 | int empty = FALSE; |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3312 | int flags = fast ? CP_FAST : 0; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3313 | char_u *(cptext[CPT_COUNT]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3314 | typval_T user_data; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3315 | int status; |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3316 | char_u *user_abbr_hlname; |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3317 | char_u *user_kind_hlname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3318 | int user_hl[2] = { -1, -1 }; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3319 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3320 | user_data.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3321 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 3322 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3323 | word = dict_get_string(tv->vval.v_dict, "word", FALSE); |
| 3324 | cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE); |
| 3325 | cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE); |
| 3326 | cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE); |
| 3327 | cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3328 | |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3329 | user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3330 | user_hl[0] = get_user_highlight_attr(user_abbr_hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3331 | |
| 3332 | user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3333 | user_hl[1] = get_user_highlight_attr(user_kind_hlname); |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3334 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3335 | dict_get_tv(tv->vval.v_dict, "user_data", &user_data); |
| 3336 | if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL |
| 3337 | && dict_get_number(tv->vval.v_dict, "icase")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3338 | flags |= CP_ICASE; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3339 | if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL) |
| 3340 | dup = dict_get_number(tv->vval.v_dict, "dup"); |
| 3341 | if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL) |
| 3342 | empty = dict_get_number(tv->vval.v_dict, "empty"); |
| 3343 | if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL |
| 3344 | && dict_get_number(tv->vval.v_dict, "equal")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3345 | flags |= CP_EQUAL; |
| 3346 | } |
| 3347 | else |
| 3348 | { |
| 3349 | word = tv_get_string_chk(tv); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 3350 | CLEAR_FIELD(cptext); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3351 | } |
| 3352 | if (word == NULL || (!empty && *word == NUL)) |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3353 | { |
| 3354 | clear_tv(&user_data); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3355 | return FAIL; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3356 | } |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3357 | status = ins_compl_add(word, -1, NULL, cptext, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3358 | &user_data, dir, flags, dup, user_hl, 0); |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3359 | if (status != OK) |
| 3360 | clear_tv(&user_data); |
| 3361 | return status; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3365 | * Add completions from a list. |
| 3366 | */ |
| 3367 | static void |
| 3368 | ins_compl_add_list(list_T *list) |
| 3369 | { |
| 3370 | listitem_T *li; |
| 3371 | int dir = compl_direction; |
| 3372 | |
| 3373 | // Go through the List with matches and add each of them. |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3374 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3375 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3376 | { |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3377 | if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3378 | // if dir was BACKWARD then honor it just once |
| 3379 | dir = FORWARD; |
| 3380 | else if (did_emsg) |
| 3381 | break; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | /* |
| 3386 | * Add completions from a dict. |
| 3387 | */ |
| 3388 | static void |
| 3389 | ins_compl_add_dict(dict_T *dict) |
| 3390 | { |
| 3391 | dictitem_T *di_refresh; |
| 3392 | dictitem_T *di_words; |
| 3393 | |
| 3394 | // Check for optional "refresh" item. |
| 3395 | compl_opt_refresh_always = FALSE; |
| 3396 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 3397 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
| 3398 | { |
| 3399 | char_u *v = di_refresh->di_tv.vval.v_string; |
| 3400 | |
| 3401 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 3402 | compl_opt_refresh_always = TRUE; |
| 3403 | } |
| 3404 | |
| 3405 | // Add completions from a "words" list. |
| 3406 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 3407 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 3408 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
| 3409 | } |
| 3410 | |
| 3411 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3412 | * Start completion for the complete() function. |
| 3413 | * "startcol" is where the matched text starts (1 is first column). |
| 3414 | * "list" is the list of matches. |
| 3415 | */ |
| 3416 | static void |
| 3417 | set_completion(colnr_T startcol, list_T *list) |
| 3418 | { |
| 3419 | int save_w_wrow = curwin->w_wrow; |
| 3420 | int save_w_leftcol = curwin->w_leftcol; |
| 3421 | int flags = CP_ORIGINAL_TEXT; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 3422 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 3423 | int compl_longest = (cur_cot_flags & COT_LONGEST) != 0; |
| 3424 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 3425 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3426 | |
| 3427 | // If already doing completions stop it. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3428 | if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3429 | ins_compl_prep(' '); |
| 3430 | ins_compl_clear(); |
| 3431 | ins_compl_free(); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3432 | compl_get_longest = compl_longest; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3433 | |
| 3434 | compl_direction = FORWARD; |
| 3435 | if (startcol > curwin->w_cursor.col) |
| 3436 | startcol = curwin->w_cursor.col; |
| 3437 | compl_col = startcol; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 3438 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3439 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
| 3440 | // compl_pattern doesn't need to be set |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3441 | compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, |
| 3442 | (size_t)compl_length); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3443 | if (p_ic) |
| 3444 | flags |= CP_ICASE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3445 | if (compl_orig_text.string == NULL) |
| 3446 | { |
| 3447 | compl_orig_text.length = 0; |
| 3448 | return; |
| 3449 | } |
| 3450 | compl_orig_text.length = (size_t)compl_length; |
| 3451 | if (ins_compl_add(compl_orig_text.string, |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3452 | (int)compl_orig_text.length, NULL, NULL, NULL, 0, |
| 3453 | flags | CP_FAST, FALSE, NULL, 0) != OK) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3454 | return; |
| 3455 | |
| 3456 | ctrl_x_mode = CTRL_X_EVAL; |
| 3457 | |
| 3458 | ins_compl_add_list(list); |
| 3459 | compl_matches = ins_compl_make_cyclic(); |
| 3460 | compl_started = TRUE; |
| 3461 | compl_used_match = TRUE; |
| 3462 | compl_cont_status = 0; |
| 3463 | |
| 3464 | compl_curr_match = compl_first_match; |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3465 | int no_select = compl_no_select || compl_longest; |
| 3466 | if (compl_no_insert || no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3467 | { |
| 3468 | ins_complete(K_DOWN, FALSE); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3469 | if (no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3470 | // Down/Up has no real effect. |
| 3471 | ins_complete(K_UP, FALSE); |
| 3472 | } |
| 3473 | else |
| 3474 | ins_complete(Ctrl_N, FALSE); |
| 3475 | compl_enter_selects = compl_no_insert; |
| 3476 | |
| 3477 | // Lazily show the popup menu, unless we got interrupted. |
| 3478 | if (!compl_interrupted) |
| 3479 | show_pum(save_w_wrow, save_w_leftcol); |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 3480 | may_trigger_modechanged(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3481 | out_flush(); |
| 3482 | } |
| 3483 | |
| 3484 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3485 | * "complete()" function |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3486 | */ |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3487 | void |
| 3488 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3489 | { |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3490 | if (in_vim9script() |
| 3491 | && (check_for_number_arg(argvars, 0) == FAIL |
| 3492 | || check_for_list_arg(argvars, 1) == FAIL)) |
| 3493 | return; |
| 3494 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 3495 | if ((State & MODE_INSERT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3496 | { |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 3497 | emsg(_(e_complete_can_only_be_used_in_insert_mode)); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3498 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3499 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3500 | |
| 3501 | // Check for undo allowed here, because if something was already inserted |
| 3502 | // the line was already saved for undo and this check isn't done. |
| 3503 | if (!undo_allowed()) |
| 3504 | return; |
| 3505 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3506 | if (check_for_nonnull_list_arg(argvars, 1) != FAIL) |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3507 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3508 | int startcol = (int)tv_get_number_chk(&argvars[0], NULL); |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3509 | if (startcol > 0) |
| 3510 | set_completion(startcol - 1, argvars[1].vval.v_list); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3511 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | * "complete_add()" function |
| 3516 | */ |
| 3517 | void |
| 3518 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 3519 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3520 | if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3521 | return; |
| 3522 | |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3523 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * "complete_check()" function |
| 3528 | */ |
| 3529 | void |
| 3530 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 3531 | { |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3532 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3533 | RedrawingDisabled = 0; |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3534 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3535 | ins_compl_check_keys(0, TRUE); |
| 3536 | rettv->vval.v_number = ins_compl_interrupted(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3537 | |
| 3538 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /* |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3542 | * Add match item to the return list. |
| 3543 | * Returns FAIL if out of memory, OK otherwise. |
| 3544 | */ |
| 3545 | static int |
| 3546 | add_match_to_list( |
| 3547 | typval_T *rettv, |
| 3548 | char_u *str, |
| 3549 | int len, |
| 3550 | int pos) |
| 3551 | { |
| 3552 | list_T *match; |
| 3553 | int ret; |
| 3554 | |
| 3555 | match = list_alloc(); |
| 3556 | if (match == NULL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3557 | return FAIL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3558 | |
| 3559 | if ((ret = list_append_number(match, pos + 1)) == FAIL |
| 3560 | || (ret = list_append_string(match, str, len)) == FAIL |
| 3561 | || (ret = list_append_list(rettv->vval.v_list, match)) == FAIL) |
| 3562 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3563 | vim_free(match); |
| 3564 | return FAIL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | return OK; |
| 3568 | } |
| 3569 | |
| 3570 | /* |
| 3571 | * "complete_match()" function |
| 3572 | */ |
| 3573 | void |
| 3574 | f_complete_match(typval_T *argvars, typval_T *rettv) |
| 3575 | { |
| 3576 | linenr_T lnum; |
| 3577 | colnr_T col; |
| 3578 | char_u *line = NULL; |
| 3579 | char_u *ise = NULL; |
| 3580 | regmatch_T regmatch; |
| 3581 | char_u *before_cursor = NULL; |
| 3582 | char_u *cur_end = NULL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3583 | int bytepos = 0; |
| 3584 | char_u part[MAXPATHL]; |
| 3585 | int ret; |
| 3586 | |
| 3587 | if (rettv_list_alloc(rettv) == FAIL) |
| 3588 | return; |
| 3589 | |
| 3590 | ise = curbuf->b_p_ise[0] != NUL ? curbuf->b_p_ise : p_ise; |
| 3591 | |
| 3592 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 3593 | { |
| 3594 | lnum = curwin->w_cursor.lnum; |
| 3595 | col = curwin->w_cursor.col; |
| 3596 | } |
| 3597 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 3598 | { |
| 3599 | emsg(_(e_invalid_argument)); |
| 3600 | return; |
| 3601 | } |
| 3602 | else |
| 3603 | { |
| 3604 | lnum = (linenr_T)tv_get_number(&argvars[0]); |
| 3605 | col = (colnr_T)tv_get_number(&argvars[1]); |
| 3606 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 3607 | { |
| 3608 | semsg(_(e_invalid_line_number_nr), lnum); |
| 3609 | return; |
| 3610 | } |
| 3611 | if (col < 1 || col > ml_get_buf_len(curbuf, lnum)) |
| 3612 | { |
| 3613 | semsg(_(e_invalid_column_number_nr), col + 1); |
| 3614 | return; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 3619 | if (line == NULL) |
| 3620 | return; |
| 3621 | |
| 3622 | before_cursor = vim_strnsave(line, col); |
| 3623 | if (before_cursor == NULL) |
| 3624 | return; |
| 3625 | |
| 3626 | if (ise == NULL || *ise == NUL) |
| 3627 | { |
| 3628 | regmatch.regprog = vim_regcomp((char_u *)"\\k\\+$", RE_MAGIC); |
| 3629 | if (regmatch.regprog != NULL) |
| 3630 | { |
| 3631 | if (vim_regexec_nl(®match, before_cursor, (colnr_T)0)) |
| 3632 | { |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3633 | char_u *trig = vim_strnsave(regmatch.startp[0], |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3634 | regmatch.endp[0] - regmatch.startp[0]); |
| 3635 | if (trig == NULL) |
| 3636 | { |
| 3637 | vim_free(before_cursor); |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3638 | vim_regfree(regmatch.regprog); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3639 | return; |
| 3640 | } |
| 3641 | |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3642 | bytepos = (int)(regmatch.startp[0] - before_cursor); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3643 | ret = add_match_to_list(rettv, trig, -1, bytepos); |
| 3644 | vim_free(trig); |
| 3645 | if (ret == FAIL) |
| 3646 | { |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3647 | vim_free(before_cursor); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3648 | vim_regfree(regmatch.regprog); |
| 3649 | return; |
| 3650 | } |
| 3651 | } |
| 3652 | vim_regfree(regmatch.regprog); |
| 3653 | } |
| 3654 | } |
| 3655 | else |
| 3656 | { |
| 3657 | char_u *p = ise; |
| 3658 | cur_end = before_cursor + (int)STRLEN(before_cursor); |
| 3659 | |
| 3660 | while (*p != NUL) |
| 3661 | { |
| 3662 | int len = copy_option_part(&p, part, MAXPATHL, ","); |
| 3663 | |
| 3664 | if (len > 0 && len <= col) |
| 3665 | { |
| 3666 | if (STRNCMP(cur_end - len, part, len) == 0) |
| 3667 | { |
| 3668 | bytepos = col - len; |
| 3669 | if (add_match_to_list(rettv, part, len, bytepos) == FAIL) |
| 3670 | { |
| 3671 | vim_free(before_cursor); |
| 3672 | return; |
| 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | } |
| 3677 | } |
| 3678 | |
| 3679 | vim_free(before_cursor); |
| 3680 | } |
| 3681 | |
| 3682 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3683 | * Return Insert completion mode name string |
| 3684 | */ |
| 3685 | static char_u * |
| 3686 | ins_compl_mode(void) |
| 3687 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3688 | 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] | 3689 | return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; |
| 3690 | |
| 3691 | return (char_u *)""; |
| 3692 | } |
| 3693 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 3694 | /* |
| 3695 | * Assign the sequence number to all the completion matches which don't have |
| 3696 | * one assigned yet. |
| 3697 | */ |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3698 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 3699 | ins_compl_update_sequence_numbers(void) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3700 | { |
| 3701 | int number = 0; |
| 3702 | compl_T *match; |
| 3703 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3704 | if (compl_dir_forward()) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3705 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3706 | // Search backwards for the first valid (!= -1) number. |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3707 | // This should normally succeed already at the first loop |
| 3708 | // cycle, so it's fast! |
| 3709 | for (match = compl_curr_match->cp_prev; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3710 | && !is_first_match(match); match = match->cp_prev) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3711 | if (match->cp_number != -1) |
| 3712 | { |
| 3713 | number = match->cp_number; |
| 3714 | break; |
| 3715 | } |
| 3716 | if (match != NULL) |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3717 | // go up and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3718 | for (match = match->cp_next; |
| 3719 | match != NULL && match->cp_number == -1; |
| 3720 | match = match->cp_next) |
| 3721 | match->cp_number = ++number; |
| 3722 | } |
| 3723 | else // BACKWARD |
| 3724 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3725 | // Search forwards (upwards) for the first valid (!= -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3726 | // number. This should normally succeed already at the |
| 3727 | // first loop cycle, so it's fast! |
| 3728 | for (match = compl_curr_match->cp_next; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3729 | && !is_first_match(match); match = match->cp_next) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3730 | { |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3731 | if (match->cp_number != -1) |
| 3732 | { |
| 3733 | number = match->cp_number; |
| 3734 | break; |
| 3735 | } |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3736 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3737 | if (match != NULL) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3738 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3739 | // go down and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3740 | for (match = match->cp_prev; match |
| 3741 | && match->cp_number == -1; |
| 3742 | match = match->cp_prev) |
| 3743 | match->cp_number = ++number; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3744 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3748 | /* |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3749 | * Fill the dict of complete_info |
| 3750 | */ |
| 3751 | static void |
| 3752 | fill_complete_info_dict(dict_T *di, compl_T *match, int add_match) |
| 3753 | { |
| 3754 | dict_add_string(di, "word", match->cp_str.string); |
| 3755 | dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]); |
| 3756 | dict_add_string(di, "menu", match->cp_text[CPT_MENU]); |
| 3757 | dict_add_string(di, "kind", match->cp_text[CPT_KIND]); |
| 3758 | dict_add_string(di, "info", match->cp_text[CPT_INFO]); |
| 3759 | if (add_match) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3760 | dict_add_bool(di, "match", match->cp_in_match_array); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3761 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3762 | // Add an empty string for backwards compatibility |
| 3763 | dict_add_string(di, "user_data", (char_u *)""); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3764 | else |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3765 | dict_add_tv(di, "user_data", &match->cp_user_data); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3769 | * Get complete information |
| 3770 | */ |
| 3771 | static void |
| 3772 | get_complete_info(list_T *what_list, dict_T *retdict) |
| 3773 | { |
| 3774 | int ret = OK; |
| 3775 | listitem_T *item; |
| 3776 | #define CI_WHAT_MODE 0x01 |
| 3777 | #define CI_WHAT_PUM_VISIBLE 0x02 |
| 3778 | #define CI_WHAT_ITEMS 0x04 |
| 3779 | #define CI_WHAT_SELECTED 0x08 |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3780 | #define CI_WHAT_COMPLETED 0x10 |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3781 | #define CI_WHAT_MATCHES 0x20 |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3782 | #define CI_WHAT_ALL 0xff |
| 3783 | int what_flag; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 3784 | int compl_fuzzy_match = (get_cot_flags() & COT_FUZZY) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3785 | |
| 3786 | if (what_list == NULL) |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3787 | what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3788 | else |
| 3789 | { |
| 3790 | what_flag = 0; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3791 | CHECK_LIST_MATERIALIZE(what_list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3792 | FOR_ALL_LIST_ITEMS(what_list, item) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3793 | { |
| 3794 | char_u *what = tv_get_string(&item->li_tv); |
| 3795 | |
| 3796 | if (STRCMP(what, "mode") == 0) |
| 3797 | what_flag |= CI_WHAT_MODE; |
| 3798 | else if (STRCMP(what, "pum_visible") == 0) |
| 3799 | what_flag |= CI_WHAT_PUM_VISIBLE; |
| 3800 | else if (STRCMP(what, "items") == 0) |
| 3801 | what_flag |= CI_WHAT_ITEMS; |
| 3802 | else if (STRCMP(what, "selected") == 0) |
| 3803 | what_flag |= CI_WHAT_SELECTED; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3804 | else if (STRCMP(what, "completed") == 0) |
| 3805 | what_flag |= CI_WHAT_COMPLETED; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3806 | else if (STRCMP(what, "matches") == 0) |
| 3807 | what_flag |= CI_WHAT_MATCHES; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | if (ret == OK && (what_flag & CI_WHAT_MODE)) |
| 3812 | ret = dict_add_string(retdict, "mode", ins_compl_mode()); |
| 3813 | |
| 3814 | if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) |
| 3815 | ret = dict_add_number(retdict, "pum_visible", pum_visible()); |
| 3816 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3817 | if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED |
| 3818 | | CI_WHAT_MATCHES | CI_WHAT_COMPLETED))) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3819 | { |
Christian Brabandt | 9eb236f | 2024-03-21 20:12:59 +0100 | [diff] [blame] | 3820 | list_T *li = NULL; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3821 | dict_T *di; |
| 3822 | compl_T *match; |
| 3823 | int selected_idx = -1; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3824 | int has_items = what_flag & CI_WHAT_ITEMS; |
| 3825 | int has_matches = what_flag & CI_WHAT_MATCHES; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3826 | int has_completed = what_flag & CI_WHAT_COMPLETED; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3827 | |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3828 | if (has_items || has_matches) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3829 | { |
| 3830 | li = list_alloc(); |
| 3831 | if (li == NULL) |
| 3832 | return; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3833 | ret = dict_add_list(retdict, (has_matches && !has_items) |
| 3834 | ? "matches" : "items", li); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3835 | } |
| 3836 | if (ret == OK && what_flag & CI_WHAT_SELECTED) |
| 3837 | if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) |
| 3838 | ins_compl_update_sequence_numbers(); |
| 3839 | if (ret == OK && compl_first_match != NULL) |
| 3840 | { |
| 3841 | int list_idx = 0; |
| 3842 | match = compl_first_match; |
| 3843 | do |
| 3844 | { |
| 3845 | if (!match_at_original_text(match)) |
| 3846 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3847 | if (has_items |
| 3848 | || (has_matches && match->cp_in_match_array)) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3849 | { |
| 3850 | di = dict_alloc(); |
| 3851 | if (di == NULL) |
| 3852 | return; |
| 3853 | ret = list_append_dict(li, di); |
| 3854 | if (ret != OK) |
| 3855 | return; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3856 | fill_complete_info_dict(di, match, has_matches && has_items); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3857 | } |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3858 | if (compl_curr_match != NULL |
| 3859 | && compl_curr_match->cp_number == match->cp_number) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3860 | selected_idx = list_idx; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 3861 | if (compl_fuzzy_match || match->cp_in_match_array) |
| 3862 | list_idx += 1; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3863 | } |
| 3864 | match = match->cp_next; |
| 3865 | } |
| 3866 | while (match != NULL && !is_first_match(match)); |
| 3867 | } |
| 3868 | if (ret == OK && (what_flag & CI_WHAT_SELECTED)) |
| 3869 | ret = dict_add_number(retdict, "selected", selected_idx); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3870 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3871 | if (ret == OK && selected_idx != -1 && has_completed) |
| 3872 | { |
| 3873 | di = dict_alloc(); |
| 3874 | if (di == NULL) |
| 3875 | return; |
| 3876 | fill_complete_info_dict(di, compl_curr_match, FALSE); |
| 3877 | ret = dict_add_dict(retdict, "completed", di); |
| 3878 | } |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 3879 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3883 | * "complete_info()" function |
| 3884 | */ |
| 3885 | void |
| 3886 | f_complete_info(typval_T *argvars, typval_T *rettv) |
| 3887 | { |
| 3888 | list_T *what_list = NULL; |
| 3889 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 3890 | if (rettv_dict_alloc(rettv) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3891 | return; |
| 3892 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3893 | if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL) |
| 3894 | return; |
| 3895 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3896 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 3897 | { |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3898 | if (check_for_list_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3899 | return; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3900 | what_list = argvars[0].vval.v_list; |
| 3901 | } |
| 3902 | get_complete_info(what_list, rettv->vval.v_dict); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3903 | } |
| 3904 | #endif |
| 3905 | |
| 3906 | /* |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3907 | * Returns TRUE when using a user-defined function for thesaurus completion. |
| 3908 | */ |
| 3909 | static int |
| 3910 | thesaurus_func_complete(int type UNUSED) |
| 3911 | { |
| 3912 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3913 | return type == CTRL_X_THESAURUS |
| 3914 | && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL); |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3915 | #else |
| 3916 | return FALSE; |
| 3917 | #endif |
| 3918 | } |
| 3919 | |
| 3920 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3921 | * Return value of process_next_cpt_value() |
| 3922 | */ |
| 3923 | enum |
| 3924 | { |
| 3925 | INS_COMPL_CPT_OK = 1, |
| 3926 | INS_COMPL_CPT_CONT, |
| 3927 | INS_COMPL_CPT_END |
| 3928 | }; |
| 3929 | |
| 3930 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3931 | * state information used for getting the next set of insert completion |
| 3932 | * matches. |
| 3933 | */ |
| 3934 | typedef struct |
| 3935 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3936 | char_u *e_cpt_copy; // copy of 'complete' |
| 3937 | char_u *e_cpt; // current entry in "e_cpt_copy" |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3938 | buf_T *ins_buf; // buffer being scanned |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3939 | pos_T *cur_match_pos; // current match position |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3940 | pos_T prev_match_pos; // previous match position |
| 3941 | int set_match_pos; // save first_match_pos/last_match_pos |
| 3942 | pos_T first_match_pos; // first match position |
| 3943 | pos_T last_match_pos; // last match position |
| 3944 | int found_all; // found all matches of a certain type. |
| 3945 | char_u *dict; // dictionary file to search |
| 3946 | int dict_f; // "dict" is an exact file name or not |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3947 | callback_T *func_cb; // callback of function in 'cpt' option |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3948 | } ins_compl_next_state_T; |
| 3949 | |
| 3950 | /* |
| 3951 | * Process the next 'complete' option value in st->e_cpt. |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3952 | * |
| 3953 | * If successful, the arguments are set as below: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3954 | * st->cpt - pointer to the next option value in "st->cpt" |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3955 | * compl_type_arg - type of insert mode completion to use |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3956 | * st->found_all - all matches of this type are found |
| 3957 | * st->ins_buf - search for completions in this buffer |
| 3958 | * st->first_match_pos - position of the first completion match |
| 3959 | * st->last_match_pos - position of the last completion match |
| 3960 | * 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] | 3961 | * avoid loops after the search wraps around. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3962 | * st->dict - name of the dictionary or thesaurus file to search |
| 3963 | * 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] | 3964 | * |
| 3965 | * Returns INS_COMPL_CPT_OK if the next value is processed successfully. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3966 | * Returns INS_COMPL_CPT_CONT to skip the current completion source matching |
| 3967 | * the "st->e_cpt" option value and process the next matching source. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3968 | * 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] | 3969 | */ |
| 3970 | static int |
| 3971 | process_next_cpt_value( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3972 | ins_compl_next_state_T *st, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3973 | int *compl_type_arg, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3974 | pos_T *start_match_pos, |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 3975 | int fuzzy_collect) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3976 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3977 | int compl_type = -1; |
| 3978 | int status = INS_COMPL_CPT_OK; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3979 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3980 | st->found_all = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3981 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3982 | while (*st->e_cpt == ',' || *st->e_cpt == ' ') |
| 3983 | st->e_cpt++; |
| 3984 | |
| 3985 | if (*st->e_cpt == '.' && !curbuf->b_scanned) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3986 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3987 | st->ins_buf = curbuf; |
| 3988 | st->first_match_pos = *start_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3989 | // Move the cursor back one character so that ^N can match the |
| 3990 | // word immediately after the cursor. |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 3991 | 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] | 3992 | { |
| 3993 | // Move the cursor to after the last character in the |
| 3994 | // buffer, so that word at start of buffer is found |
| 3995 | // correctly. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3996 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 3997 | 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] | 3998 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3999 | st->last_match_pos = st->first_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4000 | compl_type = 0; |
| 4001 | |
| 4002 | // Remember the first match so that the loop stops when we |
| 4003 | // wrap and come back there a second time. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4004 | st->set_match_pos = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4005 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4006 | else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4007 | && (st->ins_buf = ins_compl_next_buf( |
| 4008 | st->ins_buf, *st->e_cpt)) != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4009 | { |
| 4010 | // Scan a buffer, but not the current one. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4011 | if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4012 | { |
| 4013 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4014 | st->first_match_pos.col = st->last_match_pos.col = 0; |
| 4015 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1; |
| 4016 | st->last_match_pos.lnum = 0; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4017 | compl_type = 0; |
| 4018 | } |
| 4019 | else // unloaded buffer, scan like dictionary |
| 4020 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4021 | st->found_all = TRUE; |
| 4022 | if (st->ins_buf->b_fname == NULL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4023 | { |
| 4024 | status = INS_COMPL_CPT_CONT; |
| 4025 | goto done; |
| 4026 | } |
| 4027 | compl_type = CTRL_X_DICTIONARY; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4028 | st->dict = st->ins_buf->b_fname; |
| 4029 | st->dict_f = DICT_EXACT; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4030 | } |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 4031 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 4032 | { |
| 4033 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 4034 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
| 4035 | st->ins_buf->b_fname == NULL |
| 4036 | ? buf_spname(st->ins_buf) |
| 4037 | : st->ins_buf->b_sfname == NULL |
| 4038 | ? st->ins_buf->b_fname |
| 4039 | : st->ins_buf->b_sfname); |
| 4040 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 4041 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4042 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4043 | else if (*st->e_cpt == NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4044 | status = INS_COMPL_CPT_END; |
| 4045 | else |
| 4046 | { |
| 4047 | if (ctrl_x_mode_line_or_eval()) |
| 4048 | compl_type = -1; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4049 | else if (*st->e_cpt == 'k' || *st->e_cpt == 's') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4050 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4051 | if (*st->e_cpt == 'k') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4052 | compl_type = CTRL_X_DICTIONARY; |
| 4053 | else |
| 4054 | compl_type = CTRL_X_THESAURUS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4055 | if (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4056 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4057 | st->dict = st->e_cpt; |
| 4058 | st->dict_f = DICT_FIRST; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4061 | #ifdef FEAT_COMPL_FUNC |
| 4062 | else if (*st->e_cpt == 'f' || *st->e_cpt == 'o') |
| 4063 | { |
| 4064 | compl_type = CTRL_X_FUNCTION; |
| 4065 | if (*st->e_cpt == 'o') |
| 4066 | st->func_cb = &curbuf->b_ofu_cb; |
| 4067 | else |
| 4068 | st->func_cb = (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
| 4069 | ? get_cpt_func_callback(st->e_cpt) : &curbuf->b_cfu_cb; |
| 4070 | if (!st->func_cb) |
| 4071 | compl_type = -1; |
| 4072 | } |
| 4073 | #endif |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4074 | #ifdef FEAT_FIND_ID |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4075 | else if (*st->e_cpt == 'i') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4076 | compl_type = CTRL_X_PATH_PATTERNS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4077 | else if (*st->e_cpt == 'd') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4078 | compl_type = CTRL_X_PATH_DEFINES; |
| 4079 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4080 | else if (*st->e_cpt == ']' || *st->e_cpt == 't') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4081 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4082 | compl_type = CTRL_X_TAGS; |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 4083 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 4084 | { |
| 4085 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 4086 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
| 4087 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 4088 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4089 | } |
| 4090 | else |
| 4091 | compl_type = -1; |
| 4092 | |
| 4093 | // in any case e_cpt is advanced to the next entry |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4094 | (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ","); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4095 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4096 | st->found_all = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4097 | if (compl_type == -1) |
| 4098 | status = INS_COMPL_CPT_CONT; |
| 4099 | } |
| 4100 | |
| 4101 | done: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4102 | *compl_type_arg = compl_type; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4103 | return status; |
| 4104 | } |
| 4105 | |
| 4106 | #ifdef FEAT_FIND_ID |
| 4107 | /* |
| 4108 | * Get the next set of identifiers or defines matching "compl_pattern" in |
| 4109 | * included files. |
| 4110 | */ |
| 4111 | static void |
| 4112 | get_next_include_file_completion(int compl_type) |
| 4113 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4114 | find_pattern_in_path(compl_pattern.string, compl_direction, |
| 4115 | (int)compl_pattern.length, FALSE, FALSE, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4116 | (compl_type == CTRL_X_PATH_DEFINES |
| 4117 | && !(compl_cont_status & CONT_SOL)) |
| 4118 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 4119 | (linenr_T)1, (linenr_T)MAXLNUM, FALSE); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4120 | } |
| 4121 | #endif |
| 4122 | |
| 4123 | /* |
| 4124 | * Get the next set of words matching "compl_pattern" in dictionary or |
| 4125 | * thesaurus files. |
| 4126 | */ |
| 4127 | static void |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4128 | 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] | 4129 | { |
| 4130 | #ifdef FEAT_COMPL_FUNC |
| 4131 | if (thesaurus_func_complete(compl_type)) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4132 | expand_by_function(compl_type, compl_pattern.string, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4133 | else |
| 4134 | #endif |
| 4135 | ins_compl_dictionaries( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4136 | dict != NULL ? dict |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4137 | : (compl_type == CTRL_X_THESAURUS |
| 4138 | ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr) |
| 4139 | : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)), |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4140 | compl_pattern.string, |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4141 | dict != NULL ? dict_f : 0, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4142 | compl_type == CTRL_X_THESAURUS); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | /* |
| 4146 | * Get the next set of tag names matching "compl_pattern". |
| 4147 | */ |
| 4148 | static void |
| 4149 | get_next_tag_completion(void) |
| 4150 | { |
| 4151 | int save_p_ic; |
| 4152 | char_u **matches; |
| 4153 | int num_matches; |
| 4154 | |
| 4155 | // set p_ic according to p_ic, p_scs and pat for find_tags(). |
| 4156 | save_p_ic = p_ic; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4157 | p_ic = ignorecase(compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4158 | |
| 4159 | // Find up to TAG_MANY matches. Avoids that an enormous number |
| 4160 | // of matches is found when compl_pattern is empty |
| 4161 | g_tag_at_cursor = TRUE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4162 | if (find_tags(compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4163 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4164 | | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0), |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4165 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 4166 | ins_compl_add_matches(num_matches, matches, p_ic); |
| 4167 | g_tag_at_cursor = FALSE; |
| 4168 | p_ic = save_p_ic; |
| 4169 | } |
| 4170 | |
| 4171 | /* |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4172 | * Compare function for qsort |
| 4173 | */ |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4174 | static int |
| 4175 | compare_scores(const void *a, const void *b) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4176 | { |
| 4177 | int idx_a = *(const int *)a; |
| 4178 | int idx_b = *(const int *)b; |
| 4179 | int score_a = compl_fuzzy_scores[idx_a]; |
| 4180 | int score_b = compl_fuzzy_scores[idx_b]; |
zeertzjq | 58d7052 | 2024-08-31 17:05:39 +0200 | [diff] [blame] | 4181 | return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1)) |
| 4182 | : (score_a > score_b ? -1 : 1); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4186 | * insert prefix with redraw |
| 4187 | */ |
| 4188 | static void |
| 4189 | ins_compl_longest_insert(char_u *prefix) |
| 4190 | { |
| 4191 | ins_compl_delete(); |
| 4192 | ins_compl_insert_bytes(prefix + get_compl_len(), -1); |
| 4193 | ins_redraw(FALSE); |
| 4194 | } |
| 4195 | |
| 4196 | /* |
| 4197 | * Calculate the longest common prefix among the best fuzzy matches |
| 4198 | * stored in compl_best_matches, and insert it as the longest. |
| 4199 | */ |
| 4200 | static void |
| 4201 | fuzzy_longest_match(void) |
| 4202 | { |
| 4203 | char_u *prefix = NULL; |
| 4204 | int prefix_len = 0; |
| 4205 | int i = 0; |
| 4206 | int j = 0; |
| 4207 | char_u *match_str = NULL; |
| 4208 | char_u *prefix_ptr = NULL; |
| 4209 | char_u *match_ptr = NULL; |
| 4210 | char_u *leader = NULL; |
| 4211 | size_t leader_len = 0; |
| 4212 | compl_T *compl = NULL; |
| 4213 | int more_candidates = FALSE; |
| 4214 | compl_T *nn_compl = NULL; |
| 4215 | |
| 4216 | if (compl_num_bests == 0) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4217 | return; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4218 | |
| 4219 | nn_compl = compl_first_match->cp_next->cp_next; |
| 4220 | if (nn_compl && nn_compl != compl_first_match) |
| 4221 | more_candidates = TRUE; |
| 4222 | |
| 4223 | compl = ctrl_x_mode_whole_line() ? compl_first_match |
| 4224 | : compl_first_match->cp_next; |
| 4225 | if (compl_num_bests == 1) |
| 4226 | { |
| 4227 | // no more candidates insert the match str |
| 4228 | if (!more_candidates) |
| 4229 | { |
| 4230 | ins_compl_longest_insert(compl->cp_str.string); |
| 4231 | compl_num_bests = 0; |
| 4232 | } |
| 4233 | compl_num_bests = 0; |
| 4234 | return; |
| 4235 | } |
| 4236 | |
| 4237 | compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *)); |
| 4238 | if (compl_best_matches == NULL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4239 | return; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4240 | while (compl != NULL && i < compl_num_bests) |
| 4241 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4242 | compl_best_matches[i] = compl; |
| 4243 | compl = compl->cp_next; |
| 4244 | i++; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | prefix = compl_best_matches[0]->cp_str.string; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4248 | prefix_len = (int)compl_best_matches[0]->cp_str.length; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4249 | |
| 4250 | for (i = 1; i < compl_num_bests; i++) |
| 4251 | { |
| 4252 | match_str = compl_best_matches[i]->cp_str.string; |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4253 | prefix_ptr = prefix; |
| 4254 | match_ptr = match_str; |
| 4255 | j = 0; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4256 | |
| 4257 | while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL) |
| 4258 | { |
| 4259 | if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0) |
| 4260 | break; |
| 4261 | |
| 4262 | MB_PTR_ADV(prefix_ptr); |
| 4263 | MB_PTR_ADV(match_ptr); |
| 4264 | j++; |
| 4265 | } |
| 4266 | |
| 4267 | if (j > 0) |
| 4268 | prefix_len = j; |
| 4269 | } |
| 4270 | |
| 4271 | leader = ins_compl_leader(); |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4272 | leader_len = ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4273 | |
| 4274 | // skip non-consecutive prefixes |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4275 | if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4276 | goto end; |
| 4277 | |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4278 | prefix = vim_strnsave(prefix, prefix_len); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4279 | if (prefix != NULL) |
| 4280 | { |
| 4281 | ins_compl_longest_insert(prefix); |
| 4282 | compl_cfc_longest_ins = TRUE; |
| 4283 | vim_free(prefix); |
| 4284 | } |
| 4285 | |
| 4286 | end: |
| 4287 | vim_free(compl_best_matches); |
| 4288 | compl_best_matches = NULL; |
| 4289 | compl_num_bests = 0; |
| 4290 | } |
| 4291 | |
| 4292 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4293 | * Get the next set of filename matching "compl_pattern". |
| 4294 | */ |
| 4295 | static void |
| 4296 | get_next_filename_completion(void) |
| 4297 | { |
| 4298 | char_u **matches; |
| 4299 | int num_matches; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4300 | char_u *ptr; |
| 4301 | garray_T fuzzy_indices; |
| 4302 | int i; |
| 4303 | int score; |
| 4304 | char_u *leader = ins_compl_leader(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4305 | size_t leader_len = ins_compl_leader_len();; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4306 | int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4307 | int *fuzzy_indices_data; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4308 | char_u *last_sep = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4309 | int need_collect_bests = in_fuzzy_collect && compl_get_longest; |
| 4310 | int max_score = 0; |
| 4311 | int current_score = 0; |
| 4312 | int dir = compl_direction; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4313 | |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4314 | #ifdef BACKSLASH_IN_FILENAME |
| 4315 | char pathsep = (curbuf->b_p_csl[0] == 's') ? |
| 4316 | '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP; |
| 4317 | #else |
| 4318 | char pathsep = PATHSEP; |
| 4319 | #endif |
| 4320 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4321 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4322 | { |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4323 | #ifdef BACKSLASH_IN_FILENAME |
| 4324 | if (curbuf->b_p_csl[0] == 's') |
| 4325 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4326 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4327 | { |
| 4328 | if (leader[i] == '\\') |
| 4329 | leader[i] = '/'; |
| 4330 | } |
| 4331 | } |
| 4332 | else if (curbuf->b_p_csl[0] == 'b') |
| 4333 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4334 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4335 | { |
| 4336 | if (leader[i] == '/') |
| 4337 | leader[i] = '\\'; |
| 4338 | } |
| 4339 | } |
| 4340 | #endif |
| 4341 | last_sep = vim_strrchr(leader, pathsep); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4342 | if (last_sep == NULL) |
| 4343 | { |
| 4344 | // No path separator or separator is the last character, |
| 4345 | // fuzzy match the whole leader |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4346 | VIM_CLEAR_STRING(compl_pattern); |
| 4347 | compl_pattern.string = vim_strnsave((char_u *)"*", 1); |
| 4348 | if (compl_pattern.string == NULL) |
| 4349 | return; |
| 4350 | compl_pattern.length = 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4351 | } |
| 4352 | else if (*(last_sep + 1) == '\0') |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4353 | in_fuzzy_collect = FALSE; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4354 | else |
| 4355 | { |
| 4356 | // Split leader into path and file parts |
| 4357 | int path_len = last_sep - leader + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4358 | char_u *path_with_wildcard; |
| 4359 | |
| 4360 | path_with_wildcard = alloc(path_len + 2); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4361 | if (path_with_wildcard != NULL) |
| 4362 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4363 | vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader); |
| 4364 | VIM_CLEAR_STRING(compl_pattern); |
| 4365 | compl_pattern.string = path_with_wildcard; |
| 4366 | compl_pattern.length = path_len + 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4367 | |
| 4368 | // Move leader to the file part |
| 4369 | leader = last_sep + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4370 | leader_len -= path_len; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4371 | } |
| 4372 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4373 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4374 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4375 | if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4376 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK) |
| 4377 | return; |
| 4378 | |
| 4379 | // May change home directory back to "~". |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4380 | tilde_replace(compl_pattern.string, num_matches, matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4381 | #ifdef BACKSLASH_IN_FILENAME |
| 4382 | if (curbuf->b_p_csl[0] != NUL) |
| 4383 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4384 | for (i = 0; i < num_matches; ++i) |
| 4385 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4386 | ptr = matches[i]; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4387 | while (*ptr != NUL) |
| 4388 | { |
| 4389 | if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') |
| 4390 | *ptr = '/'; |
| 4391 | else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') |
| 4392 | *ptr = '\\'; |
| 4393 | ptr += (*mb_ptr2len)(ptr); |
| 4394 | } |
| 4395 | } |
| 4396 | } |
| 4397 | #endif |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4398 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4399 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4400 | { |
| 4401 | ga_init2(&fuzzy_indices, sizeof(int), 10); |
| 4402 | compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches); |
| 4403 | |
| 4404 | for (i = 0; i < num_matches; i++) |
| 4405 | { |
| 4406 | ptr = matches[i]; |
| 4407 | score = fuzzy_match_str(ptr, leader); |
| 4408 | if (score > 0) |
| 4409 | { |
| 4410 | if (ga_grow(&fuzzy_indices, 1) == OK) |
| 4411 | { |
| 4412 | ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i; |
| 4413 | compl_fuzzy_scores[i] = score; |
| 4414 | fuzzy_indices.ga_len++; |
| 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4419 | // prevent qsort from deref NULL pointer |
| 4420 | if (fuzzy_indices.ga_len > 0) |
| 4421 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4422 | char_u *match = NULL; |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4423 | fuzzy_indices_data = (int *)fuzzy_indices.ga_data; |
| 4424 | qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4425 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4426 | for (i = 0; i < fuzzy_indices.ga_len; ++i) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4427 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4428 | match = matches[fuzzy_indices_data[i]]; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4429 | current_score = compl_fuzzy_scores[fuzzy_indices_data[i]]; |
| 4430 | if (ins_compl_add(match, -1, NULL, NULL, NULL, dir, |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4431 | CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0), |
| 4432 | FALSE, NULL, current_score) == OK) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4433 | dir = FORWARD; |
| 4434 | |
| 4435 | if (need_collect_bests) |
| 4436 | { |
| 4437 | if (i == 0 || current_score == max_score) |
| 4438 | { |
| 4439 | compl_num_bests++; |
| 4440 | max_score = current_score; |
| 4441 | } |
| 4442 | } |
| 4443 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4444 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4445 | FreeWild(num_matches, matches); |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4446 | } |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4447 | else if (leader_len > 0) |
| 4448 | { |
| 4449 | FreeWild(num_matches, matches); |
| 4450 | num_matches = 0; |
| 4451 | } |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4452 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4453 | vim_free(compl_fuzzy_scores); |
| 4454 | ga_clear(&fuzzy_indices); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4455 | |
| 4456 | if (compl_num_bests > 0 && compl_get_longest) |
| 4457 | fuzzy_longest_match(); |
| 4458 | return; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4459 | } |
| 4460 | |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4461 | if (num_matches > 0) |
| 4462 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | * Get the next set of command-line completions matching "compl_pattern". |
| 4467 | */ |
| 4468 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 4469 | get_next_cmdline_completion(void) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4470 | { |
| 4471 | char_u **matches; |
| 4472 | int num_matches; |
| 4473 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4474 | if (expand_cmdline(&compl_xp, compl_pattern.string, |
| 4475 | (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4476 | ins_compl_add_matches(num_matches, matches, FALSE); |
| 4477 | } |
| 4478 | |
| 4479 | /* |
| 4480 | * Get the next set of spell suggestions matching "compl_pattern". |
| 4481 | */ |
| 4482 | static void |
| 4483 | get_next_spell_completion(linenr_T lnum UNUSED) |
| 4484 | { |
| 4485 | #ifdef FEAT_SPELL |
| 4486 | char_u **matches; |
| 4487 | int num_matches; |
| 4488 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4489 | num_matches = expand_spelling(lnum, compl_pattern.string, &matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4490 | if (num_matches > 0) |
| 4491 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 8e7cc6b | 2021-12-30 10:32:25 +0000 | [diff] [blame] | 4492 | else |
| 4493 | vim_free(matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4494 | #endif |
| 4495 | } |
| 4496 | |
| 4497 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4498 | * Return the next word or line from buffer "ins_buf" at position |
| 4499 | * "cur_match_pos" for completion. The length of the match is set in "len". |
| 4500 | */ |
| 4501 | static char_u * |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 4502 | ins_compl_get_next_word_or_line( |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4503 | buf_T *ins_buf, // buffer being scanned |
| 4504 | pos_T *cur_match_pos, // current match position |
| 4505 | int *match_len, |
| 4506 | int *cont_s_ipos) // next ^X<> will set initial_pos |
| 4507 | { |
| 4508 | char_u *ptr; |
| 4509 | int len; |
| 4510 | |
| 4511 | *match_len = 0; |
| 4512 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) + |
| 4513 | cur_match_pos->col; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4514 | 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] | 4515 | if (ctrl_x_mode_line_or_eval()) |
| 4516 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4517 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4518 | { |
| 4519 | if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 4520 | return NULL; |
| 4521 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4522 | len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4523 | if (!p_paste) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4524 | { |
| 4525 | char_u *tmp_ptr = ptr; |
| 4526 | |
| 4527 | ptr = skipwhite(tmp_ptr); |
| 4528 | len -= (int)(ptr - tmp_ptr); |
| 4529 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4530 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4531 | } |
| 4532 | else |
| 4533 | { |
| 4534 | char_u *tmp_ptr = ptr; |
| 4535 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4536 | if (compl_status_adding() && compl_length <= len) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4537 | { |
| 4538 | tmp_ptr += compl_length; |
| 4539 | // Skip if already inside a word. |
| 4540 | if (vim_iswordp(tmp_ptr)) |
| 4541 | return NULL; |
| 4542 | // Find start of next word. |
| 4543 | tmp_ptr = find_word_start(tmp_ptr); |
| 4544 | } |
| 4545 | // Find end of this word. |
| 4546 | tmp_ptr = find_word_end(tmp_ptr); |
| 4547 | len = (int)(tmp_ptr - ptr); |
| 4548 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4549 | if (compl_status_adding() && len == compl_length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4550 | { |
| 4551 | if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4552 | { |
| 4553 | // Try next line, if any. the new word will be |
| 4554 | // "join" as if the normal command "J" was used. |
| 4555 | // IOSIZE is always greater than |
| 4556 | // compl_length, so the next STRNCPY always |
| 4557 | // works -- Acevedo |
| 4558 | STRNCPY(IObuff, ptr, len); |
| 4559 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
| 4560 | tmp_ptr = ptr = skipwhite(ptr); |
| 4561 | // Find start of next word. |
| 4562 | tmp_ptr = find_word_start(tmp_ptr); |
| 4563 | // Find end of next word. |
| 4564 | tmp_ptr = find_word_end(tmp_ptr); |
| 4565 | if (tmp_ptr > ptr) |
| 4566 | { |
| 4567 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
| 4568 | { |
| 4569 | if (IObuff[len - 1] != ' ') |
| 4570 | IObuff[len++] = ' '; |
| 4571 | // IObuf =~ "\k.* ", thus len >= 2 |
| 4572 | if (p_js |
| 4573 | && (IObuff[len - 2] == '.' |
| 4574 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4575 | == NULL |
| 4576 | && (IObuff[len - 2] == '?' |
| 4577 | || IObuff[len - 2] == '!')))) |
| 4578 | IObuff[len++] = ' '; |
| 4579 | } |
| 4580 | // copy as much as possible of the new word |
| 4581 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4582 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4583 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4584 | len += (int)(tmp_ptr - ptr); |
| 4585 | *cont_s_ipos = TRUE; |
| 4586 | } |
| 4587 | IObuff[len] = NUL; |
| 4588 | ptr = IObuff; |
| 4589 | } |
| 4590 | if (len == compl_length) |
| 4591 | return NULL; |
| 4592 | } |
| 4593 | } |
| 4594 | |
| 4595 | *match_len = len; |
| 4596 | return ptr; |
| 4597 | } |
| 4598 | |
| 4599 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4600 | * Get the next set of words matching "compl_pattern" for default completion(s) |
| 4601 | * (normal ^P/^N and ^X^L). |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4602 | * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the |
| 4603 | * position "st->start_pos" in the "compl_direction" direction. If |
| 4604 | * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and |
| 4605 | * "st->last_match_pos". |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4606 | * Returns OK if a new next match is found, otherwise returns FAIL. |
| 4607 | */ |
| 4608 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4609 | 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] | 4610 | { |
| 4611 | int found_new_match = FAIL; |
| 4612 | int save_p_scs; |
| 4613 | int save_p_ws; |
| 4614 | int looped_around = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4615 | char_u *ptr = NULL; |
| 4616 | int len = 0; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4617 | int in_collect = (cfc_has_mode() && compl_length > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4618 | char_u *leader = ins_compl_leader(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4619 | int score = 0; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4620 | int in_curbuf = st->ins_buf == curbuf; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4621 | |
| 4622 | // If 'infercase' is set, don't use 'smartcase' here |
| 4623 | save_p_scs = p_scs; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4624 | if (st->ins_buf->b_p_inf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4625 | p_scs = FALSE; |
| 4626 | |
| 4627 | // Buffers other than curbuf are scanned from the beginning or the |
| 4628 | // end but never from the middle, thus setting nowrapscan in this |
| 4629 | // buffer is a good idea, on the other hand, we always set |
| 4630 | // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb |
| 4631 | save_p_ws = p_ws; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4632 | if (!in_curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4633 | p_ws = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4634 | else if (*st->e_cpt == '.') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4635 | p_ws = TRUE; |
| 4636 | looped_around = FALSE; |
| 4637 | for (;;) |
| 4638 | { |
| 4639 | int cont_s_ipos = FALSE; |
| 4640 | |
| 4641 | ++msg_silent; // Don't want messages for wrapscan. |
| 4642 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4643 | if (in_collect) |
| 4644 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4645 | found_new_match = search_for_fuzzy_match(st->ins_buf, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4646 | st->cur_match_pos, leader, compl_direction, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4647 | start_pos, &len, &ptr, &score); |
| 4648 | } |
| 4649 | // ctrl_x_mode_line_or_eval() || word-wise search that |
| 4650 | // has added a word that was at the beginning of the line |
| 4651 | else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL)) |
| 4652 | found_new_match = search_for_exact_line(st->ins_buf, |
| 4653 | st->cur_match_pos, compl_direction, compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4654 | else |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4655 | found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4656 | NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4657 | 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4658 | --msg_silent; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4659 | if (!compl_started || st->set_match_pos) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4660 | { |
| 4661 | // set "compl_started" even on fail |
| 4662 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4663 | st->first_match_pos = *st->cur_match_pos; |
| 4664 | st->last_match_pos = *st->cur_match_pos; |
| 4665 | st->set_match_pos = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4666 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4667 | else if (st->first_match_pos.lnum == st->last_match_pos.lnum |
| 4668 | && st->first_match_pos.col == st->last_match_pos.col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4669 | { |
| 4670 | found_new_match = FAIL; |
| 4671 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4672 | else if (compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4673 | && (st->prev_match_pos.lnum > st->cur_match_pos->lnum |
| 4674 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4675 | && st->prev_match_pos.col >= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4676 | { |
| 4677 | if (looped_around) |
| 4678 | found_new_match = FAIL; |
| 4679 | else |
| 4680 | looped_around = TRUE; |
| 4681 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4682 | else if (!compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4683 | && (st->prev_match_pos.lnum < st->cur_match_pos->lnum |
| 4684 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4685 | && st->prev_match_pos.col <= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4686 | { |
| 4687 | if (looped_around) |
| 4688 | found_new_match = FAIL; |
| 4689 | else |
| 4690 | looped_around = TRUE; |
| 4691 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4692 | st->prev_match_pos = *st->cur_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4693 | if (found_new_match == FAIL) |
| 4694 | break; |
| 4695 | |
| 4696 | // when ADDING, the text before the cursor matches, skip it |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4697 | if (compl_status_adding() && in_curbuf |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4698 | && start_pos->lnum == st->cur_match_pos->lnum |
| 4699 | && start_pos->col == st->cur_match_pos->col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4700 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4701 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4702 | if (!in_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4703 | ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos, |
| 4704 | &len, &cont_s_ipos); |
glepnir | bfb4eea | 2025-01-31 15:28:29 +0100 | [diff] [blame] | 4705 | 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] | 4706 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4707 | |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4708 | if (is_nearest_active() && in_curbuf) |
| 4709 | { |
| 4710 | score = st->cur_match_pos->lnum - curwin->w_cursor.lnum; |
| 4711 | if (score < 0) |
| 4712 | score = -score; |
| 4713 | score++; |
| 4714 | } |
| 4715 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4716 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4717 | in_curbuf ? NULL : st->ins_buf->b_sfname, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4718 | 0, cont_s_ipos, score) != NOTDONE) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4719 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4720 | if (in_collect && score == compl_first_match->cp_next->cp_score) |
| 4721 | compl_num_bests++; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4722 | found_new_match = OK; |
| 4723 | break; |
| 4724 | } |
| 4725 | } |
| 4726 | p_scs = save_p_scs; |
| 4727 | p_ws = save_p_ws; |
| 4728 | |
| 4729 | return found_new_match; |
| 4730 | } |
| 4731 | |
| 4732 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4733 | * Return the callback function associated with "funcname". |
| 4734 | */ |
| 4735 | #ifdef FEAT_COMPL_FUNC |
| 4736 | static callback_T * |
| 4737 | get_cpt_func_callback(char_u *funcname) |
| 4738 | { |
| 4739 | static callback_T cb; |
| 4740 | char_u buf[LSIZE]; |
| 4741 | int slen; |
| 4742 | |
| 4743 | slen = copy_option_part(&funcname, buf, LSIZE, ","); |
| 4744 | if (slen > 0 && option_set_callback_func(buf, &cb)) |
| 4745 | return &cb; |
| 4746 | return NULL; |
| 4747 | } |
| 4748 | |
| 4749 | /* |
| 4750 | * Retrieve new completion matches by invoking callback "cb". |
| 4751 | */ |
| 4752 | static void |
| 4753 | expand_cpt_function(callback_T *cb) |
| 4754 | { |
| 4755 | // Re-insert the text removed by ins_compl_delete(). |
| 4756 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
| 4757 | // Get matches |
| 4758 | get_cpt_func_completion_matches(cb); |
| 4759 | // Undo insertion |
| 4760 | ins_compl_delete(); |
| 4761 | } |
| 4762 | #endif |
| 4763 | |
| 4764 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4765 | * get the next set of completion matches for "type". |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4766 | * Returns TRUE if a new match is found. Otherwise returns FALSE. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4767 | */ |
| 4768 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4769 | 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] | 4770 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4771 | int found_new_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4772 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4773 | switch (type) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4774 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4775 | case -1: |
| 4776 | break; |
| 4777 | #ifdef FEAT_FIND_ID |
| 4778 | case CTRL_X_PATH_PATTERNS: |
| 4779 | case CTRL_X_PATH_DEFINES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4780 | get_next_include_file_completion(type); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4781 | break; |
| 4782 | #endif |
| 4783 | |
| 4784 | case CTRL_X_DICTIONARY: |
| 4785 | case CTRL_X_THESAURUS: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4786 | get_next_dict_tsr_completion(type, st->dict, st->dict_f); |
| 4787 | st->dict = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4788 | break; |
| 4789 | |
| 4790 | case CTRL_X_TAGS: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4791 | get_next_tag_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4792 | break; |
| 4793 | |
| 4794 | case CTRL_X_FILES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4795 | get_next_filename_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4796 | break; |
| 4797 | |
| 4798 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 4799 | case CTRL_X_CMDLINE_CTRL_X: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4800 | get_next_cmdline_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4801 | break; |
| 4802 | |
| 4803 | #ifdef FEAT_COMPL_FUNC |
| 4804 | case CTRL_X_FUNCTION: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4805 | if (ctrl_x_mode_normal()) // Invoked by a func in 'cpt' option |
| 4806 | expand_cpt_function(st->func_cb); |
| 4807 | else |
| 4808 | expand_by_function(type, compl_pattern.string, NULL); |
| 4809 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4810 | case CTRL_X_OMNI: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4811 | expand_by_function(type, compl_pattern.string, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4812 | break; |
| 4813 | #endif |
| 4814 | |
| 4815 | case CTRL_X_SPELL: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4816 | get_next_spell_completion(st->first_match_pos.lnum); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4817 | break; |
| 4818 | |
| 4819 | default: // normal ^P/^N and ^X^L |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4820 | found_new_match = get_next_default_completion(st, ini); |
| 4821 | if (found_new_match == FAIL && st->ins_buf == curbuf) |
| 4822 | st->found_all = TRUE; |
| 4823 | } |
| 4824 | |
| 4825 | // check if compl_curr_match has changed, (e.g. other type of |
| 4826 | // expansion added something) |
| 4827 | if (type != 0 && compl_curr_match != compl_old_match) |
| 4828 | found_new_match = OK; |
| 4829 | |
| 4830 | return found_new_match; |
| 4831 | } |
| 4832 | |
| 4833 | /* |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4834 | * Strips carets followed by numbers. This suffix typically represents the |
| 4835 | * max_matches setting. |
| 4836 | */ |
| 4837 | static void |
| 4838 | strip_caret_numbers_in_place(char_u *str) |
| 4839 | { |
| 4840 | char_u *read = str, *write = str, *p; |
| 4841 | |
| 4842 | if (str == NULL) |
| 4843 | return; |
| 4844 | |
| 4845 | while (*read) |
| 4846 | { |
| 4847 | if (*read == '^') |
| 4848 | { |
| 4849 | p = read + 1; |
| 4850 | while (vim_isdigit(*p)) |
| 4851 | p++; |
| 4852 | if ((*p == ',' || *p == '\0') && p != read + 1) |
| 4853 | { |
| 4854 | read = p; |
| 4855 | continue; |
| 4856 | } |
| 4857 | else |
| 4858 | *write++ = *read++; |
| 4859 | } |
| 4860 | else |
| 4861 | *write++ = *read++; |
| 4862 | } |
| 4863 | *write = '\0'; |
| 4864 | } |
| 4865 | |
| 4866 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4867 | * Get the next expansion(s), using "compl_pattern". |
| 4868 | * The search starts at position "ini" in curbuf and in the direction |
| 4869 | * compl_direction. |
| 4870 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4871 | * where we stopped searching before. |
| 4872 | * This may return before finding all the matches. |
| 4873 | * Return the total number of matches or -1 if still unknown -- Acevedo |
| 4874 | */ |
| 4875 | static int |
| 4876 | ins_compl_get_exp(pos_T *ini) |
| 4877 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4878 | static ins_compl_next_state_T st; |
| 4879 | static int st_cleared = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4880 | int i; |
| 4881 | int found_new_match; |
| 4882 | int type = ctrl_x_mode; |
| 4883 | |
| 4884 | if (!compl_started) |
| 4885 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4886 | buf_T *buf; |
| 4887 | |
| 4888 | FOR_ALL_BUFFERS(buf) |
| 4889 | buf->b_scanned = 0; |
| 4890 | if (!st_cleared) |
| 4891 | { |
| 4892 | CLEAR_FIELD(st); |
| 4893 | st_cleared = TRUE; |
| 4894 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4895 | st.found_all = FALSE; |
| 4896 | st.ins_buf = curbuf; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4897 | vim_free(st.e_cpt_copy); |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 4898 | // Make a copy of 'complete', in case the buffer is wiped out. |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4899 | st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL) |
| 4900 | ? (char_u *)"." : curbuf->b_p_cpt); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4901 | strip_caret_numbers_in_place(st.e_cpt_copy); |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4902 | 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] | 4903 | st.last_match_pos = st.first_match_pos = *ini; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4904 | |
| 4905 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4906 | && !cpt_sources_init()) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4907 | return FAIL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4908 | } |
| 4909 | else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf)) |
| 4910 | st.ins_buf = curbuf; // In case the buffer was wiped out. |
| 4911 | |
| 4912 | compl_old_match = compl_curr_match; // remember the last current match |
Christian Brabandt | 13032a4 | 2024-07-28 21:16:48 +0200 | [diff] [blame] | 4913 | st.cur_match_pos = (compl_dir_forward()) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4914 | ? &st.last_match_pos : &st.first_match_pos; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4915 | |
| 4916 | // For ^N/^P loop over all the flags/windows/buffers in 'complete'. |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4917 | for (cpt_sources_index = 0;;) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4918 | { |
| 4919 | found_new_match = FAIL; |
| 4920 | st.set_match_pos = FALSE; |
| 4921 | |
| 4922 | // For ^N/^P pick a new entry from e_cpt if compl_started is off, |
| 4923 | // or if found_all says this entry is done. For ^X^L only use the |
| 4924 | // entries from 'complete' that look in loaded buffers. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4925 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4926 | && (!compl_started || st.found_all)) |
| 4927 | { |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 4928 | int status = process_next_cpt_value(&st, &type, ini, cfc_has_mode()); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4929 | |
| 4930 | if (status == INS_COMPL_CPT_END) |
| 4931 | break; |
| 4932 | if (status == INS_COMPL_CPT_CONT) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4933 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4934 | cpt_sources_index++; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4935 | continue; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4936 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4937 | } |
| 4938 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4939 | // If complete() was called then compl_pattern has been reset. The |
| 4940 | // following won't work then, bail out. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4941 | if (compl_pattern.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4942 | break; |
| 4943 | |
| 4944 | // get the next set of completion matches |
| 4945 | found_new_match = get_next_completion_match(type, &st, ini); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4946 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4947 | if (type > 0) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4948 | cpt_sources_index++; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4949 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4950 | // break the loop for specialized modes (use 'complete' just for the |
| 4951 | // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new |
| 4952 | // match |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4953 | if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval()) |
| 4954 | || found_new_match != FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4955 | { |
| 4956 | if (got_int) |
| 4957 | break; |
| 4958 | // Fill the popup menu as soon as possible. |
| 4959 | if (type != -1) |
| 4960 | ins_compl_check_keys(0, FALSE); |
| 4961 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4962 | if ((ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4963 | && !ctrl_x_mode_line_or_eval()) || compl_interrupted) |
| 4964 | break; |
| 4965 | compl_started = TRUE; |
| 4966 | } |
| 4967 | else |
| 4968 | { |
| 4969 | // Mark a buffer scanned when it has been scanned completely |
Christian Brabandt | ee9166e | 2023-09-03 21:24:33 +0200 | [diff] [blame] | 4970 | 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] | 4971 | st.ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4972 | |
| 4973 | compl_started = FALSE; |
| 4974 | } |
| 4975 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4976 | cpt_sources_index = -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4977 | compl_started = TRUE; |
| 4978 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4979 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4980 | && *st.e_cpt == NUL) // Got to end of 'complete' |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4981 | found_new_match = FAIL; |
| 4982 | |
| 4983 | i = -1; // total of matches, unknown |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4984 | if (found_new_match == FAIL || (ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4985 | && !ctrl_x_mode_line_or_eval())) |
| 4986 | i = ins_compl_make_cyclic(); |
| 4987 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4988 | if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0) |
| 4989 | fuzzy_longest_match(); |
| 4990 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4991 | if (compl_old_match != NULL) |
| 4992 | { |
| 4993 | // If several matches were added (FORWARD) or the search failed and has |
| 4994 | // just been made cyclic then we have to move compl_curr_match to the |
| 4995 | // next or previous entry (if any) -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4996 | compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4997 | : compl_old_match->cp_prev; |
| 4998 | if (compl_curr_match == NULL) |
| 4999 | compl_curr_match = compl_old_match; |
| 5000 | } |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 5001 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 5002 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5003 | return i; |
| 5004 | } |
| 5005 | |
| 5006 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5007 | * Update "compl_shown_match" to the actually shown match, it may differ when |
| 5008 | * "compl_leader" is used to omit some of the matches. |
| 5009 | */ |
| 5010 | static void |
| 5011 | ins_compl_update_shown_match(void) |
| 5012 | { |
| 5013 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5014 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5015 | && compl_shown_match->cp_next != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5016 | && !is_first_match(compl_shown_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5017 | compl_shown_match = compl_shown_match->cp_next; |
| 5018 | |
| 5019 | // If we didn't find it searching forward, and compl_shows_dir is |
| 5020 | // backward, find the last match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5021 | if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5022 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5023 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5024 | && (compl_shown_match->cp_next == NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5025 | || is_first_match(compl_shown_match->cp_next))) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5026 | { |
| 5027 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5028 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5029 | && compl_shown_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5030 | && !is_first_match(compl_shown_match->cp_prev)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5031 | compl_shown_match = compl_shown_match->cp_prev; |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5036 | * Delete the old text being completed. |
| 5037 | */ |
| 5038 | void |
| 5039 | ins_compl_delete(void) |
| 5040 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5041 | // In insert mode: Delete the typed part. |
| 5042 | // In replace mode: Put the old characters back, if any. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5043 | int col = compl_col + (compl_status_adding() ? compl_length : 0); |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5044 | string_T remaining = {NULL, 0}; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5045 | int orig_col; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5046 | int has_preinsert = ins_compl_preinsert_effect(); |
| 5047 | if (has_preinsert) |
| 5048 | { |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 5049 | col += (int)ins_compl_leader_len(); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5050 | curwin->w_cursor.col = compl_ins_end_col; |
| 5051 | } |
| 5052 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5053 | if (curwin->w_cursor.lnum > compl_lnum) |
| 5054 | { |
| 5055 | if (curwin->w_cursor.col < ml_get_curline_len()) |
| 5056 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5057 | char_u *line = ml_get_cursor(); |
| 5058 | remaining.length = ml_get_cursor_len(); |
| 5059 | remaining.string = vim_strnsave(line, remaining.length); |
| 5060 | if (remaining.string == NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5061 | return; |
| 5062 | } |
| 5063 | while (curwin->w_cursor.lnum > compl_lnum) |
| 5064 | { |
| 5065 | if (ml_delete(curwin->w_cursor.lnum) == FAIL) |
| 5066 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5067 | if (remaining.string) |
| 5068 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5069 | return; |
| 5070 | } |
zeertzjq | 060e655 | 2025-02-21 20:06:26 +0100 | [diff] [blame] | 5071 | deleted_lines_mark(curwin->w_cursor.lnum, 1L); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5072 | curwin->w_cursor.lnum--; |
| 5073 | } |
| 5074 | // move cursor to end of line |
| 5075 | curwin->w_cursor.col = ml_get_curline_len(); |
| 5076 | } |
| 5077 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5078 | if ((int)curwin->w_cursor.col > col) |
| 5079 | { |
| 5080 | if (stop_arrow() == FAIL) |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 5081 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5082 | if (remaining.string) |
| 5083 | vim_free(remaining.string); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5084 | return; |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 5085 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5086 | backspace_until_column(col); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 5087 | compl_ins_end_col = curwin->w_cursor.col; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5088 | } |
| 5089 | |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5090 | if (remaining.string != NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5091 | { |
| 5092 | orig_col = curwin->w_cursor.col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5093 | ins_str(remaining.string, remaining.length); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5094 | curwin->w_cursor.col = orig_col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5095 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5096 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5097 | // TODO: is this sufficient for redrawing? Redrawing everything causes |
| 5098 | // flicker, thus we can't do that. |
| 5099 | changed_cline_bef_curs(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5100 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5101 | // clear v:completed_item |
| 5102 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5103 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5107 | * Insert a completion string that contains newlines. |
| 5108 | * The string is split and inserted line by line. |
| 5109 | */ |
| 5110 | static void |
| 5111 | ins_compl_expand_multiple(char_u *str) |
| 5112 | { |
| 5113 | char_u *start = str; |
| 5114 | char_u *curr = str; |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 5115 | int base_indent = get_indent(); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5116 | |
| 5117 | while (*curr != NUL) |
| 5118 | { |
| 5119 | if (*curr == '\n') |
| 5120 | { |
| 5121 | // Insert the text chunk before newline |
| 5122 | if (curr > start) |
| 5123 | ins_char_bytes(start, (int)(curr - start)); |
| 5124 | |
| 5125 | // Handle newline |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 5126 | open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5127 | start = curr + 1; |
| 5128 | } |
| 5129 | curr++; |
| 5130 | } |
| 5131 | |
| 5132 | // Handle remaining text after last newline (if any) |
| 5133 | if (curr > start) |
| 5134 | ins_char_bytes(start, (int)(curr - start)); |
| 5135 | |
| 5136 | compl_ins_end_col = curwin->w_cursor.col; |
| 5137 | } |
| 5138 | |
| 5139 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5140 | * Insert the new text being completed. |
| 5141 | * "in_compl_func" is TRUE when called from complete_check(). |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5142 | * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE |
| 5143 | * 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] | 5144 | */ |
| 5145 | void |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5146 | ins_compl_insert(int in_compl_func, int move_cursor) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5147 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5148 | int compl_len = get_compl_len(); |
| 5149 | int preinsert = ins_compl_has_preinsert(); |
| 5150 | char_u *cp_str = compl_shown_match->cp_str.string; |
| 5151 | size_t cp_str_len = compl_shown_match->cp_str.length; |
| 5152 | size_t leader_len = ins_compl_leader_len(); |
| 5153 | char_u *has_multiple = vim_strchr(cp_str, '\n'); |
Bram Moolenaar | 4b28ba3 | 2021-12-27 19:28:37 +0000 | [diff] [blame] | 5154 | |
| 5155 | // Make sure we don't go over the end of the string, this can happen with |
| 5156 | // illegal bytes. |
zeertzjq | 8297e2c | 2025-01-30 11:04:47 +0100 | [diff] [blame] | 5157 | if (compl_len < (int)cp_str_len) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5158 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5159 | if (has_multiple) |
| 5160 | ins_compl_expand_multiple(cp_str + compl_len); |
| 5161 | else |
| 5162 | { |
| 5163 | ins_compl_insert_bytes(cp_str + compl_len, -1); |
| 5164 | if (preinsert && move_cursor) |
| 5165 | curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len); |
| 5166 | } |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5167 | } |
| 5168 | if (match_at_original_text(compl_shown_match) || preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5169 | compl_used_match = FALSE; |
| 5170 | else |
| 5171 | compl_used_match = TRUE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5172 | #ifdef FEAT_EVAL |
| 5173 | { |
| 5174 | dict_T *dict = ins_compl_dict_alloc(compl_shown_match); |
| 5175 | |
| 5176 | set_vim_var_dict(VV_COMPLETED_ITEM, dict); |
| 5177 | } |
| 5178 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5179 | if (!in_compl_func) |
| 5180 | compl_curr_match = compl_shown_match; |
| 5181 | } |
| 5182 | |
| 5183 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5184 | * show the file name for the completion match (if any). Truncate the file |
| 5185 | * name to avoid a wait for return. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5186 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5187 | static void |
| 5188 | ins_compl_show_filename(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5189 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5190 | char *lead = _("match in file"); |
| 5191 | int space = sc_col - vim_strsize((char_u *)lead) - 2; |
| 5192 | char_u *s; |
| 5193 | char_u *e; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5194 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5195 | if (space <= 0) |
| 5196 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5197 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5198 | // We need the tail that fits. With double-byte encoding going |
| 5199 | // back from the end is very slow, thus go from the start and keep |
| 5200 | // the text that fits in "space" between "s" and "e". |
| 5201 | 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] | 5202 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5203 | space -= ptr2cells(e); |
| 5204 | while (space < 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5205 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5206 | space += ptr2cells(s); |
| 5207 | MB_PTR_ADV(s); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5208 | } |
| 5209 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5210 | msg_hist_off = TRUE; |
| 5211 | vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, |
| 5212 | s > compl_shown_match->cp_fname ? "<" : "", s); |
| 5213 | msg((char *)IObuff); |
| 5214 | msg_hist_off = FALSE; |
| 5215 | redraw_cmdline = FALSE; // don't overwrite! |
| 5216 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5217 | |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5218 | /* |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 5219 | * Find a completion item when 'completeopt' contains "fuzzy". |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5220 | */ |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5221 | static compl_T * |
| 5222 | find_comp_when_fuzzy(void) |
| 5223 | { |
| 5224 | int score; |
| 5225 | char_u* str; |
| 5226 | int target_idx = -1; |
| 5227 | int is_forward = compl_shows_dir_forward(); |
| 5228 | int is_backward = compl_shows_dir_backward(); |
| 5229 | compl_T *comp = NULL; |
| 5230 | |
glepnir | 43eef88 | 2024-06-19 20:20:48 +0200 | [diff] [blame] | 5231 | if ((is_forward && compl_selected_item == compl_match_arraysize - 1) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5232 | || (is_backward && compl_selected_item == 0)) |
glepnir | 3e50a28 | 2025-04-03 21:17:06 +0200 | [diff] [blame] | 5233 | return compl_first_match != compl_shown_match ? |
| 5234 | (is_forward ? compl_shown_match->cp_next : compl_first_match) : |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 5235 | (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5236 | |
| 5237 | if (is_forward) |
| 5238 | target_idx = compl_selected_item + 1; |
| 5239 | else if (is_backward) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 5240 | target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1 |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5241 | : compl_selected_item - 1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5242 | |
| 5243 | score = compl_match_array[target_idx].pum_score; |
| 5244 | str = compl_match_array[target_idx].pum_text; |
| 5245 | |
| 5246 | comp = compl_first_match; |
Hirohito Higashi | a4a00a7 | 2025-05-08 22:58:31 +0200 | [diff] [blame] | 5247 | do |
| 5248 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 5249 | if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR])) |
| 5250 | return comp; |
| 5251 | comp = comp->cp_next; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5252 | } while (comp != NULL && !is_first_match(comp)); |
| 5253 | |
| 5254 | return NULL; |
| 5255 | } |
| 5256 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5257 | /* |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5258 | * Find the appropriate completion item when 'complete' ('cpt') includes |
| 5259 | * a 'max_matches' postfix. In this case, we search for a match where |
| 5260 | * 'cp_in_match_array' is set, indicating that the match is also present |
| 5261 | * in 'compl_match_array'. |
| 5262 | */ |
| 5263 | static compl_T * |
| 5264 | find_comp_when_cpt_sources(void) |
| 5265 | { |
| 5266 | int is_forward = compl_shows_dir_forward(); |
| 5267 | compl_T *match = compl_shown_match; |
| 5268 | |
| 5269 | do |
| 5270 | match = is_forward ? match->cp_next : match->cp_prev; |
| 5271 | while (match->cp_next && !match->cp_in_match_array |
| 5272 | && !match_at_original_text(match)); |
| 5273 | return match; |
| 5274 | } |
| 5275 | |
| 5276 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5277 | * Find the next set of matches for completion. Repeat the completion "todo" |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5278 | * times. The number of matches found is returned in 'num_matches'. |
| 5279 | * |
| 5280 | * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to |
| 5281 | * get more completions. If it is FALSE, then do nothing when there are no more |
| 5282 | * completions in the given direction. |
| 5283 | * |
| 5284 | * If "advance" is TRUE, then completion will move to the first match. |
| 5285 | * Otherwise, the original text will be shown. |
| 5286 | * |
| 5287 | * Returns OK on success and -1 if the number of matches are unknown. |
| 5288 | */ |
| 5289 | static int |
| 5290 | find_next_completion_match( |
| 5291 | int allow_get_expansion, |
| 5292 | int todo, // repeat completion this many times |
| 5293 | int advance, |
| 5294 | int *num_matches) |
| 5295 | { |
| 5296 | int found_end = FALSE; |
| 5297 | compl_T *found_compl = NULL; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 5298 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5299 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
| 5300 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5301 | int cpt_sources_active = compl_match_array && cpt_sources_array; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5302 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5303 | while (--todo >= 0) |
| 5304 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5305 | if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5306 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5307 | if (compl_match_array != NULL && compl_fuzzy_match) |
| 5308 | compl_shown_match = find_comp_when_fuzzy(); |
| 5309 | else if (cpt_sources_active) |
| 5310 | compl_shown_match = find_comp_when_cpt_sources(); |
| 5311 | else |
| 5312 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5313 | found_end = (compl_first_match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5314 | && (is_first_match(compl_shown_match->cp_next) |
| 5315 | || is_first_match(compl_shown_match))); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5316 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5317 | else if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5318 | && compl_shown_match->cp_prev != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5319 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5320 | found_end = is_first_match(compl_shown_match); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5321 | if (compl_match_array != NULL && compl_fuzzy_match) |
| 5322 | compl_shown_match = find_comp_when_fuzzy(); |
| 5323 | else if (cpt_sources_active) |
| 5324 | compl_shown_match = find_comp_when_cpt_sources(); |
| 5325 | else |
| 5326 | compl_shown_match = compl_shown_match->cp_prev; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5327 | found_end |= is_first_match(compl_shown_match); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5328 | } |
| 5329 | else |
| 5330 | { |
| 5331 | if (!allow_get_expansion) |
| 5332 | { |
| 5333 | if (advance) |
| 5334 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5335 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5336 | compl_pending -= todo + 1; |
| 5337 | else |
| 5338 | compl_pending += todo + 1; |
| 5339 | } |
| 5340 | return -1; |
| 5341 | } |
| 5342 | |
| 5343 | if (!compl_no_select && advance) |
| 5344 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5345 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5346 | --compl_pending; |
| 5347 | else |
| 5348 | ++compl_pending; |
| 5349 | } |
| 5350 | |
| 5351 | // Find matches. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5352 | *num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5353 | |
| 5354 | // handle any pending completions |
| 5355 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5356 | && advance) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5357 | { |
| 5358 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 5359 | { |
| 5360 | compl_shown_match = compl_shown_match->cp_next; |
| 5361 | --compl_pending; |
| 5362 | } |
| 5363 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 5364 | { |
| 5365 | compl_shown_match = compl_shown_match->cp_prev; |
| 5366 | ++compl_pending; |
| 5367 | } |
| 5368 | else |
| 5369 | break; |
| 5370 | } |
| 5371 | found_end = FALSE; |
| 5372 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5373 | if (!match_at_original_text(compl_shown_match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5374 | && compl_leader.string != NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5375 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5376 | compl_leader.string, (int)compl_leader.length) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5377 | && !(compl_fuzzy_match && compl_shown_match->cp_score > 0)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5378 | ++todo; |
| 5379 | else |
| 5380 | // Remember a matching item. |
| 5381 | found_compl = compl_shown_match; |
| 5382 | |
| 5383 | // Stop at the end of the list when we found a usable match. |
| 5384 | if (found_end) |
| 5385 | { |
| 5386 | if (found_compl != NULL) |
| 5387 | { |
| 5388 | compl_shown_match = found_compl; |
| 5389 | break; |
| 5390 | } |
| 5391 | todo = 1; // use first usable match after wrapping around |
| 5392 | } |
| 5393 | } |
| 5394 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5395 | return OK; |
| 5396 | } |
| 5397 | |
| 5398 | /* |
| 5399 | * Fill in the next completion in the current direction. |
| 5400 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 5401 | * get more completions. If it is FALSE, then we just do nothing when there |
| 5402 | * are no more completions in a given direction. The latter case is used when |
| 5403 | * we are still in the middle of finding completions, to allow browsing |
| 5404 | * through the ones found so far. |
| 5405 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 5406 | * |
| 5407 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 5408 | * compl_shown_match here. |
| 5409 | * |
| 5410 | * Note that this function may be called recursively once only. First with |
| 5411 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 5412 | * calls this function with "allow_get_expansion" FALSE. |
| 5413 | */ |
| 5414 | static int |
| 5415 | ins_compl_next( |
| 5416 | int allow_get_expansion, |
| 5417 | int count, // repeat completion this many times; should |
| 5418 | // be at least 1 |
| 5419 | int insert_match, // Insert the newly selected match |
| 5420 | int in_compl_func) // called from complete_check() |
| 5421 | { |
| 5422 | int num_matches = -1; |
| 5423 | int todo = count; |
| 5424 | int advance; |
| 5425 | int started = compl_started; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5426 | buf_T *orig_curbuf = curbuf; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 5427 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5428 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 5429 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5430 | int compl_preinsert = ins_compl_has_preinsert(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5431 | |
| 5432 | // When user complete function return -1 for findstart which is next |
| 5433 | // time of 'always', compl_shown_match become NULL. |
| 5434 | if (compl_shown_match == NULL) |
| 5435 | return -1; |
| 5436 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5437 | if (compl_leader.string != NULL |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5438 | && !match_at_original_text(compl_shown_match) |
| 5439 | && !compl_fuzzy_match) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5440 | // Update "compl_shown_match" to the actually shown match |
| 5441 | ins_compl_update_shown_match(); |
| 5442 | |
| 5443 | if (allow_get_expansion && insert_match |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 5444 | && (!compl_get_longest || compl_used_match)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5445 | // Delete old text to be replaced |
| 5446 | ins_compl_delete(); |
| 5447 | |
| 5448 | // When finding the longest common text we stick at the original text, |
| 5449 | // don't let CTRL-N or CTRL-P move to the first match. |
| 5450 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 5451 | |
| 5452 | // When restarting the search don't insert the first match either. |
| 5453 | if (compl_restarting) |
| 5454 | { |
| 5455 | advance = FALSE; |
| 5456 | compl_restarting = FALSE; |
| 5457 | } |
| 5458 | |
| 5459 | // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 5460 | // around. |
| 5461 | if (find_next_completion_match(allow_get_expansion, todo, advance, |
| 5462 | &num_matches) == -1) |
| 5463 | return -1; |
| 5464 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5465 | if (curbuf != orig_curbuf) |
| 5466 | { |
| 5467 | // In case some completion function switched buffer, don't want to |
| 5468 | // insert the completion elsewhere. |
| 5469 | return -1; |
| 5470 | } |
| 5471 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5472 | // Insert the text of the new completion, or the compl_leader. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5473 | if (compl_no_insert && !started && !compl_preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5474 | { |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5475 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5476 | compl_used_match = FALSE; |
| 5477 | } |
| 5478 | else if (insert_match) |
| 5479 | { |
| 5480 | if (!compl_get_longest || compl_used_match) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5481 | ins_compl_insert(in_compl_func, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5482 | else |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5483 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5484 | } |
| 5485 | else |
| 5486 | compl_used_match = FALSE; |
| 5487 | |
| 5488 | if (!allow_get_expansion) |
| 5489 | { |
| 5490 | // may undisplay the popup menu first |
| 5491 | ins_compl_upd_pum(); |
| 5492 | |
| 5493 | if (pum_enough_matches()) |
| 5494 | // Will display the popup menu, don't redraw yet to avoid flicker. |
| 5495 | pum_call_update_screen(); |
| 5496 | else |
| 5497 | // Not showing the popup menu yet, redraw to show the user what was |
| 5498 | // inserted. |
| 5499 | update_screen(0); |
| 5500 | |
| 5501 | // display the updated popup menu |
| 5502 | ins_compl_show_pum(); |
| 5503 | #ifdef FEAT_GUI |
| 5504 | if (gui.in_use) |
| 5505 | { |
| 5506 | // Show the cursor after the match, not after the redrawn text. |
| 5507 | setcursor(); |
| 5508 | out_flush_cursor(FALSE, FALSE); |
| 5509 | } |
| 5510 | #endif |
| 5511 | |
| 5512 | // Delete old text to be replaced, since we're still searching and |
| 5513 | // don't want to match ourselves! |
| 5514 | ins_compl_delete(); |
| 5515 | } |
| 5516 | |
| 5517 | // Enter will select a match when the match wasn't inserted and the popup |
| 5518 | // menu is visible. |
| 5519 | if (compl_no_insert && !started) |
| 5520 | compl_enter_selects = TRUE; |
| 5521 | else |
| 5522 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 5523 | |
| 5524 | // Show the file name for the match (if any) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5525 | if (compl_shown_match->cp_fname != NULL) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5526 | ins_compl_show_filename(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5527 | |
| 5528 | return num_matches; |
| 5529 | } |
| 5530 | |
| 5531 | /* |
| 5532 | * Call this while finding completions, to check whether the user has hit a key |
| 5533 | * that should change the currently displayed completion, or exit completion |
| 5534 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
| 5535 | * possible. -- webb |
| 5536 | * "frequency" specifies out of how many calls we actually check. |
| 5537 | * "in_compl_func" is TRUE when called from complete_check(), don't set |
| 5538 | * compl_curr_match. |
| 5539 | */ |
| 5540 | void |
| 5541 | ins_compl_check_keys(int frequency, int in_compl_func) |
| 5542 | { |
| 5543 | static int count = 0; |
| 5544 | int c; |
| 5545 | |
| 5546 | // Don't check when reading keys from a script, :normal or feedkeys(). |
| 5547 | // That would break the test scripts. But do check for keys when called |
| 5548 | // from complete_check(). |
| 5549 | if (!in_compl_func && (using_script() || ex_normal_busy)) |
| 5550 | return; |
| 5551 | |
| 5552 | // Only do this at regular intervals |
| 5553 | if (++count < frequency) |
| 5554 | return; |
| 5555 | count = 0; |
| 5556 | |
| 5557 | // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 5558 | // can't do its work correctly. |
| 5559 | c = vpeekc_any(); |
| 5560 | if (c != NUL) |
| 5561 | { |
| 5562 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 5563 | { |
| 5564 | c = safe_vgetc(); // Eat the character |
| 5565 | compl_shows_dir = ins_compl_key2dir(c); |
| 5566 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 5567 | c != K_UP && c != K_DOWN, in_compl_func); |
| 5568 | } |
| 5569 | else |
| 5570 | { |
| 5571 | // Need to get the character to have KeyTyped set. We'll put it |
| 5572 | // back with vungetc() below. But skip K_IGNORE. |
| 5573 | c = safe_vgetc(); |
| 5574 | if (c != K_IGNORE) |
| 5575 | { |
| 5576 | // Don't interrupt completion when the character wasn't typed, |
| 5577 | // e.g., when doing @q to replay keys. |
| 5578 | if (c != Ctrl_R && KeyTyped) |
| 5579 | compl_interrupted = TRUE; |
| 5580 | |
| 5581 | vungetc(c); |
| 5582 | } |
| 5583 | } |
| 5584 | } |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5585 | if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5586 | { |
| 5587 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 5588 | |
| 5589 | compl_pending = 0; |
| 5590 | (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func); |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | /* |
| 5595 | * Decide the direction of Insert mode complete from the key typed. |
| 5596 | * Returns BACKWARD or FORWARD. |
| 5597 | */ |
| 5598 | static int |
| 5599 | ins_compl_key2dir(int c) |
| 5600 | { |
| 5601 | if (c == Ctrl_P || c == Ctrl_L |
| 5602 | || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP) |
| 5603 | return BACKWARD; |
| 5604 | return FORWARD; |
| 5605 | } |
| 5606 | |
| 5607 | /* |
| 5608 | * Return TRUE for keys that are used for completion only when the popup menu |
| 5609 | * is visible. |
| 5610 | */ |
| 5611 | static int |
| 5612 | ins_compl_pum_key(int c) |
| 5613 | { |
| 5614 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
| 5615 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 5616 | || c == K_UP || c == K_DOWN); |
| 5617 | } |
| 5618 | |
| 5619 | /* |
| 5620 | * Decide the number of completions to move forward. |
| 5621 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 5622 | */ |
| 5623 | static int |
| 5624 | ins_compl_key2count(int c) |
| 5625 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5626 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
| 5627 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5628 | int h = pum_get_height(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5629 | if (h > 3) |
| 5630 | h -= 2; // keep some context |
| 5631 | return h; |
| 5632 | } |
| 5633 | return 1; |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 5638 | * to change the currently selected completion. |
| 5639 | */ |
| 5640 | static int |
| 5641 | ins_compl_use_match(int c) |
| 5642 | { |
| 5643 | switch (c) |
| 5644 | { |
| 5645 | case K_UP: |
| 5646 | case K_DOWN: |
| 5647 | case K_PAGEDOWN: |
| 5648 | case K_KPAGEDOWN: |
| 5649 | case K_S_DOWN: |
| 5650 | case K_PAGEUP: |
| 5651 | case K_KPAGEUP: |
| 5652 | case K_S_UP: |
| 5653 | return FALSE; |
| 5654 | } |
| 5655 | return TRUE; |
| 5656 | } |
| 5657 | |
| 5658 | /* |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5659 | * Get the pattern, column and length for normal completion (CTRL-N CTRL-P |
| 5660 | * completion) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5661 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5662 | * Uses the global variables: compl_cont_status and ctrl_x_mode |
| 5663 | */ |
| 5664 | static int |
| 5665 | get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5666 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5667 | if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5668 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5669 | if (!compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5670 | { |
| 5671 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
| 5672 | ; |
| 5673 | compl_col += ++startcol; |
| 5674 | compl_length = curs_col - startcol; |
| 5675 | } |
| 5676 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5677 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5678 | compl_pattern.string = str_foldcase(line + compl_col, |
| 5679 | compl_length, NULL, 0); |
| 5680 | if (compl_pattern.string == NULL) |
| 5681 | { |
| 5682 | compl_pattern.length = 0; |
| 5683 | return FAIL; |
| 5684 | } |
| 5685 | compl_pattern.length = STRLEN(compl_pattern.string); |
| 5686 | } |
| 5687 | else |
| 5688 | { |
| 5689 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5690 | if (compl_pattern.string == NULL) |
| 5691 | { |
| 5692 | compl_pattern.length = 0; |
| 5693 | return FAIL; |
| 5694 | } |
| 5695 | compl_pattern.length = (size_t)compl_length; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5696 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5697 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5698 | else if (compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5699 | { |
| 5700 | char_u *prefix = (char_u *)"\\<"; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5701 | size_t prefixlen = STRLEN_LITERAL("\\<"); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5702 | size_t n; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5703 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5704 | if (!vim_iswordp(line + compl_col) |
| 5705 | || (compl_col > 0 |
| 5706 | && (vim_iswordp(mb_prevptr(line, line + compl_col))))) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5707 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5708 | prefix = (char_u *)""; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5709 | prefixlen = 0; |
| 5710 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5711 | |
| 5712 | // we need up to 2 extra chars for the prefix |
| 5713 | n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen; |
| 5714 | compl_pattern.string = alloc(n); |
| 5715 | if (compl_pattern.string == NULL) |
| 5716 | { |
| 5717 | compl_pattern.length = 0; |
| 5718 | return FAIL; |
| 5719 | } |
| 5720 | STRCPY((char *)compl_pattern.string, prefix); |
| 5721 | (void)quote_meta(compl_pattern.string + prefixlen, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5722 | line + compl_col, compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5723 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5724 | } |
| 5725 | else if (--startcol < 0 |
| 5726 | || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) |
| 5727 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5728 | size_t len = STRLEN_LITERAL("\\<\\k\\k"); |
| 5729 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5730 | // Match any word of at least two chars |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5731 | compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len); |
| 5732 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5733 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5734 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5735 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5736 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5737 | compl_pattern.length = len; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5738 | compl_col += curs_col; |
| 5739 | compl_length = 0; |
| 5740 | } |
| 5741 | else |
| 5742 | { |
| 5743 | // Search the point of change class of multibyte character |
| 5744 | // or not a word single byte character backward. |
| 5745 | if (has_mbyte) |
| 5746 | { |
| 5747 | int base_class; |
| 5748 | int head_off; |
| 5749 | |
| 5750 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5751 | base_class = mb_get_class(line + startcol); |
| 5752 | while (--startcol >= 0) |
| 5753 | { |
| 5754 | head_off = (*mb_head_off)(line, line + startcol); |
| 5755 | if (base_class != mb_get_class(line + startcol |
| 5756 | - head_off)) |
| 5757 | break; |
| 5758 | startcol -= head_off; |
| 5759 | } |
| 5760 | } |
| 5761 | else |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5762 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5763 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
| 5764 | ; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5767 | compl_col += ++startcol; |
| 5768 | compl_length = (int)curs_col - startcol; |
| 5769 | if (compl_length == 1) |
| 5770 | { |
| 5771 | // Only match word with at least two chars -- webb |
| 5772 | // there's no need to call quote_meta, |
| 5773 | // alloc(7) is enough -- Acevedo |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5774 | compl_pattern.string = alloc(7); |
| 5775 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5776 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5777 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5778 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5779 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5780 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5781 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1); |
| 5782 | STRCAT((char *)compl_pattern.string, "\\k"); |
| 5783 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5784 | } |
| 5785 | else |
| 5786 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5787 | size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2; |
| 5788 | |
| 5789 | compl_pattern.string = alloc(n); |
| 5790 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5791 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5792 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5793 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5794 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5795 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5796 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5797 | compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5798 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | return OK; |
| 5803 | } |
| 5804 | |
| 5805 | /* |
| 5806 | * Get the pattern, column and length for whole line completion or for the |
| 5807 | * complete() function. |
| 5808 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5809 | */ |
| 5810 | static int |
| 5811 | get_wholeline_compl_info(char_u *line, colnr_T curs_col) |
| 5812 | { |
| 5813 | compl_col = (colnr_T)getwhitecols(line); |
| 5814 | compl_length = (int)curs_col - (int)compl_col; |
| 5815 | if (compl_length < 0) // cursor in indent: empty pattern |
| 5816 | compl_length = 0; |
| 5817 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5818 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5819 | compl_pattern.string = str_foldcase(line + compl_col, compl_length, |
| 5820 | NULL, 0); |
| 5821 | if (compl_pattern.string == NULL) |
| 5822 | { |
| 5823 | compl_pattern.length = 0; |
| 5824 | return FAIL; |
| 5825 | } |
| 5826 | compl_pattern.length = STRLEN(compl_pattern.string); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5827 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5828 | else |
| 5829 | { |
| 5830 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5831 | if (compl_pattern.string == NULL) |
| 5832 | { |
| 5833 | compl_pattern.length = 0; |
| 5834 | return FAIL; |
| 5835 | } |
| 5836 | compl_pattern.length = (size_t)compl_length; |
| 5837 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5838 | |
| 5839 | return OK; |
| 5840 | } |
| 5841 | |
| 5842 | /* |
| 5843 | * Get the pattern, column and length for filename completion. |
| 5844 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5845 | */ |
| 5846 | static int |
| 5847 | get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5848 | { |
| 5849 | // Go back to just before the first filename character. |
| 5850 | if (startcol > 0) |
| 5851 | { |
| 5852 | char_u *p = line + startcol; |
| 5853 | |
| 5854 | MB_PTR_BACK(line, p); |
| 5855 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 5856 | MB_PTR_BACK(line, p); |
| 5857 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 5858 | startcol = 0; |
| 5859 | else |
| 5860 | startcol = (int)(p - line) + 1; |
| 5861 | } |
| 5862 | |
| 5863 | compl_col += startcol; |
| 5864 | compl_length = (int)curs_col - startcol; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5865 | compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES); |
| 5866 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5867 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5868 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5869 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5870 | } |
| 5871 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5872 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5873 | |
| 5874 | return OK; |
| 5875 | } |
| 5876 | |
| 5877 | /* |
| 5878 | * Get the pattern, column and length for command-line completion. |
| 5879 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5880 | */ |
| 5881 | static int |
| 5882 | get_cmdline_compl_info(char_u *line, colnr_T curs_col) |
| 5883 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5884 | compl_pattern.string = vim_strnsave(line, curs_col); |
| 5885 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5886 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5887 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5888 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5889 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5890 | compl_pattern.length = curs_col; |
| 5891 | set_cmd_context(&compl_xp, compl_pattern.string, |
| 5892 | (int)compl_pattern.length, curs_col, FALSE); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5893 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5894 | || compl_xp.xp_context == EXPAND_NOTHING) |
| 5895 | // No completion possible, use an empty pattern to get a |
| 5896 | // "pattern not found" message. |
| 5897 | compl_col = curs_col; |
| 5898 | else |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5899 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5900 | compl_length = curs_col - compl_col; |
| 5901 | |
| 5902 | return OK; |
| 5903 | } |
| 5904 | |
| 5905 | /* |
| 5906 | * Get the pattern, column and length for user defined completion ('omnifunc', |
| 5907 | * 'completefunc' and 'thesaurusfunc') |
| 5908 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5909 | * Uses the global variable: spell_bad_len |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5910 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 5911 | * option; otherwise, it is NULL. |
| 5912 | * "startcol", when not NULL, contains the column returned by function. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5913 | */ |
| 5914 | static int |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5915 | get_userdefined_compl_info( |
| 5916 | colnr_T curs_col UNUSED, |
| 5917 | callback_T *cb UNUSED, |
| 5918 | int *startcol UNUSED) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5919 | { |
| 5920 | int ret = FAIL; |
| 5921 | |
| 5922 | #ifdef FEAT_COMPL_FUNC |
| 5923 | // Call user defined function 'completefunc' with "a:findstart" |
| 5924 | // set to 1 to obtain the length of text to use for completion. |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5925 | char_u *line = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5926 | typval_T args[3]; |
| 5927 | int col; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5928 | char_u *funcname = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5929 | pos_T pos; |
| 5930 | int save_State = State; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5931 | int len; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5932 | string_T *compl_pat = NULL; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5933 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5934 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5935 | if (!is_cpt_function) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5936 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5937 | // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern |
| 5938 | // length as a string |
| 5939 | funcname = get_complete_funcname(ctrl_x_mode); |
| 5940 | if (*funcname == NUL) |
| 5941 | { |
| 5942 | semsg(_(e_option_str_is_not_set), ctrl_x_mode_function() |
| 5943 | ? "completefunc" : "omnifunc"); |
| 5944 | return FAIL; |
| 5945 | } |
| 5946 | cb = get_insert_callback(ctrl_x_mode); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | args[0].v_type = VAR_NUMBER; |
| 5950 | args[0].vval.v_number = 1; |
| 5951 | args[1].v_type = VAR_STRING; |
| 5952 | args[1].vval.v_string = (char_u *)""; |
| 5953 | args[2].v_type = VAR_UNKNOWN; |
| 5954 | pos = curwin->w_cursor; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5955 | ++textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5956 | col = call_callback_retnr(cb, 2, args); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5957 | --textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5958 | |
| 5959 | State = save_State; |
| 5960 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 5961 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5962 | validate_cursor(); |
| 5963 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 5964 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 5965 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5966 | return FAIL; |
| 5967 | } |
| 5968 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5969 | if (startcol != NULL) |
| 5970 | *startcol = col; |
| 5971 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5972 | // Return value -2 means the user complete function wants to cancel the |
| 5973 | // complete without an error, do the same if the function did not execute |
| 5974 | // successfully. |
| 5975 | if (col == -2 || aborting()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5976 | return FAIL; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5977 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5978 | // 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] | 5979 | if (col == -3) |
| 5980 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5981 | if (is_cpt_function) |
| 5982 | return FAIL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5983 | ctrl_x_mode = CTRL_X_NORMAL; |
| 5984 | edit_submode = NULL; |
| 5985 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 5986 | msg_clr_cmdline(); |
| 5987 | return FAIL; |
| 5988 | } |
| 5989 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 5990 | // Reset extended parameters of completion, when starting new |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5991 | // completion. |
| 5992 | compl_opt_refresh_always = FALSE; |
| 5993 | compl_opt_suppress_empty = FALSE; |
| 5994 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5995 | if (col < 0 || col > curs_col) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5996 | col = curs_col; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5997 | |
| 5998 | // Setup variables for completion. Need to obtain "line" again, |
| 5999 | // it may have become invalid. |
| 6000 | line = ml_get(curwin->w_cursor.lnum); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6001 | len = curs_col - col; |
| 6002 | compl_pat = is_cpt_function ? &cpt_compl_pattern : &compl_pattern; |
| 6003 | compl_pat->string = vim_strnsave(line + col, (size_t)len); |
| 6004 | if (compl_pat->string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6005 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6006 | compl_pat->length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6007 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6008 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6009 | compl_pat->length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6010 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6011 | if (!is_cpt_function) |
| 6012 | { |
| 6013 | compl_col = col; |
| 6014 | compl_length = len; |
| 6015 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6016 | ret = OK; |
| 6017 | #endif |
| 6018 | |
| 6019 | return ret; |
| 6020 | } |
| 6021 | |
| 6022 | /* |
| 6023 | * Get the pattern, column and length for spell completion. |
| 6024 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 6025 | * Uses the global variable: spell_bad_len |
| 6026 | */ |
| 6027 | static int |
| 6028 | get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED) |
| 6029 | { |
| 6030 | int ret = FAIL; |
| 6031 | #ifdef FEAT_SPELL |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6032 | char_u *line = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6033 | |
| 6034 | if (spell_bad_len > 0) |
| 6035 | compl_col = curs_col - spell_bad_len; |
| 6036 | else |
| 6037 | compl_col = spell_word_start(startcol); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6038 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6039 | if (compl_col >= (colnr_T)startcol) |
| 6040 | { |
| 6041 | compl_length = 0; |
| 6042 | compl_col = curs_col; |
| 6043 | } |
| 6044 | else |
| 6045 | { |
| 6046 | spell_expand_check_cap(compl_col); |
| 6047 | compl_length = (int)curs_col - compl_col; |
| 6048 | } |
| 6049 | // Need to obtain "line" again, it may have become invalid. |
| 6050 | line = ml_get(curwin->w_cursor.lnum); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6051 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 6052 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6053 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6054 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6055 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6056 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6057 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6058 | compl_pattern.length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6059 | ret = OK; |
| 6060 | #endif |
| 6061 | |
| 6062 | return ret; |
| 6063 | } |
| 6064 | |
| 6065 | /* |
| 6066 | * Get the completion pattern, column and length. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6067 | * "startcol" - start column number of the completion pattern/text |
| 6068 | * "cur_col" - current cursor column |
| 6069 | * On return, "line_invalid" is set to TRUE, if the current line may have |
| 6070 | * become invalid and needs to be fetched again. |
| 6071 | * Returns OK on success. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6072 | */ |
| 6073 | static int |
| 6074 | compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid) |
| 6075 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6076 | if (ctrl_x_mode_normal() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6077 | || (ctrl_x_mode & CTRL_X_WANT_IDENT |
| 6078 | && !thesaurus_func_complete(ctrl_x_mode))) |
| 6079 | { |
| 6080 | return get_normal_compl_info(line, startcol, curs_col); |
| 6081 | } |
| 6082 | else if (ctrl_x_mode_line_or_eval()) |
| 6083 | { |
| 6084 | return get_wholeline_compl_info(line, curs_col); |
| 6085 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6086 | else if (ctrl_x_mode_files()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6087 | { |
| 6088 | return get_filename_compl_info(line, startcol, curs_col); |
| 6089 | } |
| 6090 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 6091 | { |
| 6092 | return get_cmdline_compl_info(line, curs_col); |
| 6093 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6094 | else if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6095 | || thesaurus_func_complete(ctrl_x_mode)) |
| 6096 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6097 | if (get_userdefined_compl_info(curs_col, NULL, NULL) != OK) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6098 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6099 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6100 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6101 | else if (ctrl_x_mode_spell()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6102 | { |
| 6103 | if (get_spell_compl_info(startcol, curs_col) == FAIL) |
| 6104 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6105 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6106 | } |
| 6107 | else |
| 6108 | { |
| 6109 | internal_error("ins_complete()"); |
| 6110 | return FAIL; |
| 6111 | } |
| 6112 | |
| 6113 | return OK; |
| 6114 | } |
| 6115 | |
| 6116 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6117 | * Continue an interrupted completion mode search in "line". |
| 6118 | * |
| 6119 | * If this same ctrl_x_mode has been interrupted use the text from |
| 6120 | * "compl_startpos" to the cursor as a pattern to add a new word instead of |
| 6121 | * expand the one before the cursor, in word-wise if "compl_startpos" is not in |
| 6122 | * the same line as the cursor then fix it (the line has been split because it |
| 6123 | * was longer than 'tw'). if SOL is set then skip the previous pattern, a word |
| 6124 | * 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] | 6125 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6126 | static void |
| 6127 | ins_compl_continue_search(char_u *line) |
| 6128 | { |
| 6129 | // it is a continued search |
| 6130 | compl_cont_status &= ~CONT_INTRPT; // remove INTRPT |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6131 | if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns() |
| 6132 | || ctrl_x_mode_path_defines()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6133 | { |
| 6134 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
| 6135 | { |
| 6136 | // line (probably) wrapped, set compl_startpos to the |
| 6137 | // first non_blank in the line, if it is not a wordchar |
| 6138 | // include it to get a better pattern, but then we don't |
| 6139 | // want the "\\<" prefix, check it below |
| 6140 | compl_col = (colnr_T)getwhitecols(line); |
| 6141 | compl_startpos.col = compl_col; |
| 6142 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 6143 | compl_cont_status &= ~CONT_SOL; // clear SOL if present |
| 6144 | } |
| 6145 | else |
| 6146 | { |
| 6147 | // S_IPOS was set when we inserted a word that was at the |
| 6148 | // beginning of the line, which means that we'll go to SOL |
| 6149 | // mode but first we need to redefine compl_startpos |
| 6150 | if (compl_cont_status & CONT_S_IPOS) |
| 6151 | { |
| 6152 | compl_cont_status |= CONT_SOL; |
| 6153 | compl_startpos.col = (colnr_T)(skipwhite( |
| 6154 | line + compl_length |
| 6155 | + compl_startpos.col) - line); |
| 6156 | } |
| 6157 | compl_col = compl_startpos.col; |
| 6158 | } |
| 6159 | compl_length = curwin->w_cursor.col - (int)compl_col; |
| 6160 | // IObuff is used to add a "word from the next line" would we |
| 6161 | // have enough space? just being paranoid |
| 6162 | #define MIN_SPACE 75 |
| 6163 | if (compl_length > (IOSIZE - MIN_SPACE)) |
| 6164 | { |
| 6165 | compl_cont_status &= ~CONT_SOL; |
| 6166 | compl_length = (IOSIZE - MIN_SPACE); |
| 6167 | compl_col = curwin->w_cursor.col - compl_length; |
| 6168 | } |
| 6169 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 6170 | if (compl_length < 1) |
| 6171 | compl_cont_status &= CONT_LOCAL; |
| 6172 | } |
| 6173 | else if (ctrl_x_mode_line_or_eval()) |
| 6174 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
| 6175 | else |
| 6176 | compl_cont_status = 0; |
| 6177 | } |
| 6178 | |
| 6179 | /* |
| 6180 | * start insert mode completion |
| 6181 | */ |
| 6182 | static int |
| 6183 | ins_compl_start(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6184 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6185 | char_u *line = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6186 | int startcol = 0; // column where searched text starts |
| 6187 | colnr_T curs_col; // cursor column |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6188 | int line_invalid = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6189 | int save_did_ai = did_ai; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 6190 | int flags = CP_ORIGINAL_TEXT; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6191 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6192 | // First time we hit ^N or ^P (in a row, I mean) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6193 | did_ai = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6194 | did_si = FALSE; |
| 6195 | can_si = FALSE; |
| 6196 | can_si_back = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6197 | if (stop_arrow() == FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6198 | return FAIL; |
| 6199 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6200 | line = ml_get(curwin->w_cursor.lnum); |
| 6201 | curs_col = curwin->w_cursor.col; |
| 6202 | compl_pending = 0; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 6203 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6204 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6205 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 6206 | && compl_cont_mode == ctrl_x_mode) |
| 6207 | // this same ctrl-x_mode was interrupted previously. Continue the |
| 6208 | // completion. |
| 6209 | ins_compl_continue_search(line); |
| 6210 | else |
| 6211 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6212 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6213 | if (!compl_status_adding()) // normal expansion |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6214 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6215 | compl_cont_mode = ctrl_x_mode; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6216 | if (ctrl_x_mode_not_default()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6217 | // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL |
| 6218 | compl_cont_status = 0; |
| 6219 | compl_cont_status |= CONT_N_ADDS; |
| 6220 | compl_startpos = curwin->w_cursor; |
| 6221 | startcol = (int)curs_col; |
| 6222 | compl_col = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6223 | } |
| 6224 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6225 | // Work out completion pattern and original text -- webb |
| 6226 | if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL) |
| 6227 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6228 | if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
| 6229 | || thesaurus_func_complete(ctrl_x_mode)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6230 | // restore did_ai, so that adding comment leader works |
| 6231 | did_ai = save_did_ai; |
| 6232 | return FAIL; |
| 6233 | } |
| 6234 | // If "line" was changed while getting completion info get it again. |
| 6235 | if (line_invalid) |
| 6236 | line = ml_get(curwin->w_cursor.lnum); |
| 6237 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6238 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6239 | { |
| 6240 | edit_submode_pre = (char_u *)_(" Adding"); |
| 6241 | if (ctrl_x_mode_line_or_eval()) |
| 6242 | { |
| 6243 | // Insert a new line, keep indentation but ignore 'comments'. |
| 6244 | char_u *old = curbuf->b_p_com; |
| 6245 | |
| 6246 | curbuf->b_p_com = (char_u *)""; |
| 6247 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 6248 | compl_startpos.col = compl_col; |
| 6249 | ins_eol('\r'); |
| 6250 | curbuf->b_p_com = old; |
| 6251 | compl_length = 0; |
| 6252 | compl_col = curwin->w_cursor.col; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 6253 | compl_lnum = curwin->w_cursor.lnum; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6254 | } |
glepnir | cfe502c | 2025-04-17 20:17:53 +0200 | [diff] [blame] | 6255 | else if (ctrl_x_mode_normal() && cfc_has_mode()) |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 6256 | { |
| 6257 | compl_startpos = curwin->w_cursor; |
| 6258 | compl_cont_status &= CONT_S_IPOS; |
| 6259 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6260 | } |
| 6261 | else |
| 6262 | { |
| 6263 | edit_submode_pre = NULL; |
| 6264 | compl_startpos.col = compl_col; |
| 6265 | } |
| 6266 | |
| 6267 | if (compl_cont_status & CONT_LOCAL) |
| 6268 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
| 6269 | else |
| 6270 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 6271 | |
| 6272 | // If any of the original typed text has been changed we need to fix |
| 6273 | // the redo buffer. |
| 6274 | ins_compl_fixRedoBufForLeader(NULL); |
| 6275 | |
| 6276 | // Always add completion for the original text. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6277 | VIM_CLEAR_STRING(compl_orig_text); |
| 6278 | compl_orig_text.length = (size_t)compl_length; |
| 6279 | 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] | 6280 | if (p_ic) |
| 6281 | flags |= CP_ICASE; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 6282 | if (compl_orig_text.string == NULL |
| 6283 | || ins_compl_add(compl_orig_text.string, |
| 6284 | (int)compl_orig_text.length, |
| 6285 | NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6286 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6287 | VIM_CLEAR_STRING(compl_pattern); |
| 6288 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6289 | return FAIL; |
| 6290 | } |
| 6291 | |
| 6292 | // showmode might reset the internal line pointers, so it must |
| 6293 | // be called before line = ml_get(), or when this address is no |
| 6294 | // longer needed. -- Acevedo. |
| 6295 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 6296 | edit_submode_highl = HLF_COUNT; |
| 6297 | showmode(); |
| 6298 | edit_submode_extra = NULL; |
| 6299 | out_flush(); |
| 6300 | |
| 6301 | return OK; |
| 6302 | } |
| 6303 | |
| 6304 | /* |
| 6305 | * display the completion status message |
| 6306 | */ |
| 6307 | static void |
| 6308 | ins_compl_show_statusmsg(void) |
| 6309 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6310 | // 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] | 6311 | if (is_first_match(compl_first_match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6312 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6313 | edit_submode_extra = compl_status_adding() && compl_length > 1 |
Bram Moolenaar | b53a190 | 2022-11-15 13:57:38 +0000 | [diff] [blame] | 6314 | ? (char_u *)_("Hit end of paragraph") |
| 6315 | : (char_u *)_("Pattern not found"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6316 | edit_submode_highl = HLF_E; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6317 | } |
| 6318 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6319 | if (edit_submode_extra == NULL) |
| 6320 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6321 | if (match_at_original_text(compl_curr_match)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6322 | { |
| 6323 | edit_submode_extra = (char_u *)_("Back at original"); |
| 6324 | edit_submode_highl = HLF_W; |
| 6325 | } |
| 6326 | else if (compl_cont_status & CONT_S_IPOS) |
| 6327 | { |
| 6328 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 6329 | edit_submode_highl = HLF_COUNT; |
| 6330 | } |
| 6331 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
| 6332 | { |
| 6333 | edit_submode_extra = (char_u *)_("The only match"); |
| 6334 | edit_submode_highl = HLF_COUNT; |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6335 | compl_curr_match->cp_number = 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6336 | } |
| 6337 | else |
| 6338 | { |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6339 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6340 | // Update completion sequence number when needed. |
| 6341 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6342 | ins_compl_update_sequence_numbers(); |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6343 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6344 | // The match should always have a sequence number now, this is |
| 6345 | // just a safety check. |
| 6346 | if (compl_curr_match->cp_number != -1) |
| 6347 | { |
| 6348 | // Space for 10 text chars. + 2x10-digit no.s = 31. |
| 6349 | // Translations may need more than twice that. |
| 6350 | static char_u match_ref[81]; |
| 6351 | |
| 6352 | if (compl_matches > 0) |
| 6353 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6354 | _("match %d of %d"), |
| 6355 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6356 | else |
| 6357 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6358 | _("match %d"), |
| 6359 | compl_curr_match->cp_number); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6360 | edit_submode_extra = match_ref; |
| 6361 | edit_submode_highl = HLF_R; |
| 6362 | if (dollar_vcol >= 0) |
| 6363 | curs_columns(FALSE); |
| 6364 | } |
| 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | // Show a message about what (completion) mode we're in. |
| 6369 | if (!compl_opt_suppress_empty) |
| 6370 | { |
| 6371 | showmode(); |
| 6372 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 6373 | { |
| 6374 | if (edit_submode_extra != NULL) |
| 6375 | { |
| 6376 | if (!p_smd) |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6377 | { |
| 6378 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6379 | msg_attr((char *)edit_submode_extra, |
| 6380 | edit_submode_highl < HLF_COUNT |
| 6381 | ? HL_ATTR(edit_submode_highl) : 0); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6382 | msg_hist_off = FALSE; |
| 6383 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6384 | } |
| 6385 | else |
| 6386 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 6387 | } |
| 6388 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | /* |
| 6392 | * Do Insert mode completion. |
| 6393 | * Called when character "c" was typed, which has a meaning for completion. |
| 6394 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 6395 | */ |
| 6396 | int |
| 6397 | ins_complete(int c, int enable_pum) |
| 6398 | { |
| 6399 | int n; |
| 6400 | int save_w_wrow; |
| 6401 | int save_w_leftcol; |
| 6402 | int insert_match; |
| 6403 | |
| 6404 | compl_direction = ins_compl_key2dir(c); |
| 6405 | insert_match = ins_compl_use_match(c); |
| 6406 | |
| 6407 | if (!compl_started) |
| 6408 | { |
| 6409 | if (ins_compl_start() == FAIL) |
| 6410 | return FAIL; |
| 6411 | } |
| 6412 | else if (insert_match && stop_arrow() == FAIL) |
| 6413 | return FAIL; |
| 6414 | |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 6415 | compl_curr_win = curwin; |
| 6416 | compl_curr_buf = curwin->w_buffer; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6417 | compl_shown_match = compl_curr_match; |
| 6418 | compl_shows_dir = compl_direction; |
| 6419 | |
| 6420 | // Find next match (and following matches). |
| 6421 | save_w_wrow = curwin->w_wrow; |
| 6422 | save_w_leftcol = curwin->w_leftcol; |
| 6423 | n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); |
| 6424 | |
| 6425 | // may undisplay the popup menu |
| 6426 | ins_compl_upd_pum(); |
| 6427 | |
| 6428 | if (n > 1) // all matches have been found |
| 6429 | compl_matches = n; |
| 6430 | compl_curr_match = compl_shown_match; |
| 6431 | compl_direction = compl_shows_dir; |
| 6432 | |
| 6433 | // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 6434 | // mode. |
| 6435 | if (got_int && !global_busy) |
| 6436 | { |
| 6437 | (void)vgetc(); |
| 6438 | got_int = FALSE; |
| 6439 | } |
| 6440 | |
| 6441 | // 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] | 6442 | if (is_first_match(compl_first_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6443 | { |
| 6444 | // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 6445 | // because we couldn't expand anything at first place, but if we used |
| 6446 | // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 6447 | // (such as M in M'exico) if not tried already. -- Acevedo |
| 6448 | if (compl_length > 1 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6449 | || compl_status_adding() |
| 6450 | || (ctrl_x_mode_not_default() |
| 6451 | && !ctrl_x_mode_path_patterns() |
| 6452 | && !ctrl_x_mode_path_defines())) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6453 | compl_cont_status &= ~CONT_N_ADDS; |
| 6454 | } |
| 6455 | |
| 6456 | if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) |
| 6457 | compl_cont_status |= CONT_S_IPOS; |
| 6458 | else |
| 6459 | compl_cont_status &= ~CONT_S_IPOS; |
| 6460 | |
| 6461 | ins_compl_show_statusmsg(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6462 | |
| 6463 | // Show the popup menu, unless we got interrupted. |
| 6464 | if (enable_pum && !compl_interrupted) |
| 6465 | show_pum(save_w_wrow, save_w_leftcol); |
| 6466 | |
| 6467 | compl_was_interrupted = compl_interrupted; |
| 6468 | compl_interrupted = FALSE; |
| 6469 | |
| 6470 | return OK; |
| 6471 | } |
| 6472 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 6473 | /* |
| 6474 | * Remove (if needed) and show the popup menu |
| 6475 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6476 | static void |
| 6477 | show_pum(int prev_w_wrow, int prev_w_leftcol) |
| 6478 | { |
| 6479 | // RedrawingDisabled may be set when invoked through complete(). |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6480 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6481 | RedrawingDisabled = 0; |
| 6482 | |
| 6483 | // If the cursor moved or the display scrolled we need to remove the pum |
| 6484 | // first. |
| 6485 | setcursor(); |
| 6486 | if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) |
| 6487 | ins_compl_del_pum(); |
| 6488 | |
| 6489 | ins_compl_show_pum(); |
| 6490 | setcursor(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6491 | |
| 6492 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6493 | } |
| 6494 | |
| 6495 | /* |
| 6496 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 6497 | * If dest is not NULL the chars. are copied there quoting (with |
| 6498 | * a backslash) the metachars, and dest would be NUL terminated. |
| 6499 | * Returns the length (needed) of dest |
| 6500 | */ |
| 6501 | static unsigned |
| 6502 | quote_meta(char_u *dest, char_u *src, int len) |
| 6503 | { |
| 6504 | unsigned m = (unsigned)len + 1; // one extra for the NUL |
| 6505 | |
| 6506 | for ( ; --len >= 0; src++) |
| 6507 | { |
| 6508 | switch (*src) |
| 6509 | { |
| 6510 | case '.': |
| 6511 | case '*': |
| 6512 | case '[': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6513 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6514 | break; |
| 6515 | // FALLTHROUGH |
| 6516 | case '~': |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 6517 | if (!magic_isset()) // quote these only if magic is set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6518 | break; |
| 6519 | // FALLTHROUGH |
| 6520 | case '\\': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6521 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6522 | break; |
| 6523 | // FALLTHROUGH |
| 6524 | case '^': // currently it's not needed. |
| 6525 | case '$': |
| 6526 | m++; |
| 6527 | if (dest != NULL) |
| 6528 | *dest++ = '\\'; |
| 6529 | break; |
| 6530 | } |
| 6531 | if (dest != NULL) |
| 6532 | *dest++ = *src; |
| 6533 | // Copy remaining bytes of a multibyte character. |
| 6534 | if (has_mbyte) |
| 6535 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6536 | int mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6537 | if (mb_len > 0 && len >= mb_len) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6538 | for (int i = 0; i < mb_len; ++i) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6539 | { |
| 6540 | --len; |
| 6541 | ++src; |
| 6542 | if (dest != NULL) |
| 6543 | *dest++ = *src; |
| 6544 | } |
| 6545 | } |
| 6546 | } |
| 6547 | if (dest != NULL) |
| 6548 | *dest = NUL; |
| 6549 | |
| 6550 | return m; |
| 6551 | } |
| 6552 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6553 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6554 | void |
| 6555 | free_insexpand_stuff(void) |
| 6556 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6557 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 6558 | # ifdef FEAT_EVAL |
| 6559 | free_callback(&cfu_cb); |
| 6560 | free_callback(&ofu_cb); |
| 6561 | free_callback(&tsrfu_cb); |
| 6562 | # endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6563 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6564 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6565 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6566 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6567 | /* |
| 6568 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6569 | * spelled word, if there is one. |
| 6570 | */ |
| 6571 | static void |
| 6572 | spell_back_to_badword(void) |
| 6573 | { |
| 6574 | pos_T tpos = curwin->w_cursor; |
| 6575 | |
Christ van Willegen - van Noort | 8e4c4c7 | 2024-05-17 18:49:27 +0200 | [diff] [blame] | 6576 | spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6577 | if (curwin->w_cursor.col != tpos.col) |
| 6578 | start_arrow(&tpos); |
| 6579 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6580 | #endif |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6581 | |
| 6582 | /* |
| 6583 | * Reset the info associated with completion sources. |
| 6584 | */ |
| 6585 | static void |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6586 | cpt_sources_clear(void) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6587 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6588 | VIM_CLEAR(cpt_sources_array); |
| 6589 | cpt_sources_index = -1; |
| 6590 | cpt_sources_count = 0; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | /* |
| 6594 | * Initialize the info associated with completion sources. |
| 6595 | */ |
| 6596 | static int |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6597 | cpt_sources_init(void) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6598 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6599 | char_u buf[LSIZE]; |
| 6600 | int slen; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6601 | int count = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6602 | char_u *p; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6603 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6604 | for (p = curbuf->b_p_cpt; *p;) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6605 | { |
| 6606 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6607 | p++; |
| 6608 | if (*p) // If not end of string, count this segment |
| 6609 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6610 | (void)copy_option_part(&p, buf, LSIZE, ","); // Advance p |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6611 | count++; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6612 | } |
| 6613 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6614 | cpt_sources_clear(); |
| 6615 | cpt_sources_count = count; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6616 | if (count > 0) |
| 6617 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6618 | cpt_sources_array = ALLOC_CLEAR_MULT(cpt_source_T, count); |
| 6619 | if (cpt_sources_array == NULL) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6620 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6621 | cpt_sources_count = 0; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6622 | return FAIL; |
| 6623 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6624 | count = 0; |
| 6625 | for (p = curbuf->b_p_cpt; *p;) |
| 6626 | { |
| 6627 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6628 | p++; |
| 6629 | if (*p) // If not end of string, count this segment |
| 6630 | { |
| 6631 | char_u *t; |
| 6632 | |
| 6633 | vim_memset(buf, 0, LSIZE); |
| 6634 | slen = copy_option_part(&p, buf, LSIZE, ","); // Advance p |
| 6635 | if (slen > 0 && (t = vim_strchr(buf, '^')) != NULL) |
| 6636 | cpt_sources_array[count].max_matches = atoi((char *)t + 1); |
| 6637 | count++; |
| 6638 | } |
| 6639 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6640 | } |
| 6641 | return OK; |
| 6642 | } |
| 6643 | |
| 6644 | /* |
| 6645 | * Return TRUE if any of the completion sources have 'refresh' set to 'always'. |
| 6646 | */ |
| 6647 | static int |
| 6648 | is_cpt_func_refresh_always(void) |
| 6649 | { |
| 6650 | #ifdef FEAT_COMPL_FUNC |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6651 | for (int i = 0; i < cpt_sources_count; i++) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6652 | if (cpt_sources_array[i].refresh_always) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6653 | return TRUE; |
| 6654 | #endif |
| 6655 | return FALSE; |
| 6656 | } |
| 6657 | |
| 6658 | /* |
| 6659 | * Make the completion list non-cyclic. |
| 6660 | */ |
| 6661 | #ifdef FEAT_COMPL_FUNC |
| 6662 | static void |
| 6663 | ins_compl_make_linear(void) |
| 6664 | { |
| 6665 | compl_T *m; |
| 6666 | |
| 6667 | if (compl_first_match == NULL || compl_first_match->cp_prev == NULL) |
| 6668 | return; |
| 6669 | m = compl_first_match->cp_prev; |
| 6670 | m->cp_next = NULL; |
| 6671 | compl_first_match->cp_prev = NULL; |
| 6672 | } |
| 6673 | #endif |
| 6674 | |
| 6675 | /* |
| 6676 | * Remove the matches linked to the current completion source (as indicated by |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6677 | * cpt_sources_index) from the completion list. |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6678 | */ |
| 6679 | #ifdef FEAT_COMPL_FUNC |
| 6680 | static compl_T * |
| 6681 | remove_old_matches(void) |
| 6682 | { |
| 6683 | compl_T *sublist_start = NULL, *sublist_end = NULL, *insert_at = NULL; |
| 6684 | compl_T *current, *next; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6685 | int compl_shown_removed = FALSE; |
| 6686 | int forward = (compl_first_match->cp_cpt_source_idx < 0); |
| 6687 | |
| 6688 | compl_direction = forward ? FORWARD : BACKWARD; |
| 6689 | compl_shows_dir = compl_direction; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6690 | |
| 6691 | // Identify the sublist of old matches that needs removal |
| 6692 | for (current = compl_first_match; current != NULL; current = current->cp_next) |
| 6693 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6694 | if (current->cp_cpt_source_idx < cpt_sources_index && |
| 6695 | (forward || (!forward && !insert_at))) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6696 | insert_at = current; |
| 6697 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6698 | if (current->cp_cpt_source_idx == cpt_sources_index) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6699 | { |
| 6700 | if (!sublist_start) |
| 6701 | sublist_start = current; |
| 6702 | sublist_end = current; |
| 6703 | if (!compl_shown_removed && compl_shown_match == current) |
| 6704 | compl_shown_removed = TRUE; |
| 6705 | } |
| 6706 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6707 | if ((forward && current->cp_cpt_source_idx > cpt_sources_index) |
| 6708 | || (!forward && insert_at)) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6709 | break; |
| 6710 | } |
| 6711 | |
| 6712 | // Re-assign compl_shown_match if necessary |
| 6713 | if (compl_shown_removed) |
| 6714 | { |
| 6715 | if (forward) |
| 6716 | compl_shown_match = compl_first_match; |
| 6717 | else |
| 6718 | { // Last node will have the prefix that is being completed |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6719 | for (current = compl_first_match; current->cp_next != NULL; |
| 6720 | current = current->cp_next) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6721 | ; |
| 6722 | compl_shown_match = current; |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | if (!sublist_start) // No nodes to remove |
| 6727 | return insert_at; |
| 6728 | |
| 6729 | // Update links to remove sublist |
| 6730 | if (sublist_start->cp_prev) |
| 6731 | sublist_start->cp_prev->cp_next = sublist_end->cp_next; |
| 6732 | else |
| 6733 | compl_first_match = sublist_end->cp_next; |
| 6734 | |
| 6735 | if (sublist_end->cp_next) |
| 6736 | sublist_end->cp_next->cp_prev = sublist_start->cp_prev; |
| 6737 | |
| 6738 | // Free all nodes in the sublist |
| 6739 | sublist_end->cp_next = NULL; |
| 6740 | for (current = sublist_start; current != NULL; current = next) |
| 6741 | { |
| 6742 | next = current->cp_next; |
| 6743 | ins_compl_item_free(current); |
| 6744 | } |
| 6745 | |
| 6746 | return insert_at; |
| 6747 | } |
| 6748 | #endif |
| 6749 | |
| 6750 | /* |
| 6751 | * Retrieve completion matches using the callback function "cb" and store the |
| 6752 | * 'refresh:always' flag. |
| 6753 | */ |
| 6754 | #ifdef FEAT_COMPL_FUNC |
| 6755 | static void |
| 6756 | get_cpt_func_completion_matches(callback_T *cb UNUSED) |
| 6757 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6758 | int ret; |
| 6759 | int startcol; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6760 | |
| 6761 | VIM_CLEAR_STRING(cpt_compl_pattern); |
| 6762 | ret = get_userdefined_compl_info(curwin->w_cursor.col, cb, &startcol); |
| 6763 | if (ret == FAIL && startcol == -3) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6764 | cpt_sources_array[cpt_sources_index].refresh_always = FALSE; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6765 | else if (ret == OK) |
| 6766 | { |
| 6767 | expand_by_function(0, cpt_compl_pattern.string, cb); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6768 | cpt_sources_array[cpt_sources_index].refresh_always = |
| 6769 | compl_opt_refresh_always; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6770 | compl_opt_refresh_always = FALSE; |
| 6771 | } |
| 6772 | } |
| 6773 | #endif |
| 6774 | |
| 6775 | /* |
| 6776 | * Retrieve completion matches from functions in the 'cpt' option where the |
| 6777 | * 'refresh:always' flag is set. |
| 6778 | */ |
| 6779 | static void |
| 6780 | cpt_compl_refresh(void) |
| 6781 | { |
| 6782 | #ifdef FEAT_COMPL_FUNC |
| 6783 | char_u *cpt; |
| 6784 | char_u *p; |
Christian Brabandt | d2079cf | 2025-04-15 18:10:26 +0200 | [diff] [blame] | 6785 | callback_T *cb = NULL; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6786 | |
| 6787 | // Make the completion list linear (non-cyclic) |
| 6788 | ins_compl_make_linear(); |
| 6789 | // Make a copy of 'cpt' in case the buffer gets wiped out |
| 6790 | cpt = vim_strsave(curbuf->b_p_cpt); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6791 | strip_caret_numbers_in_place(cpt); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6792 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6793 | cpt_sources_index = 0; |
| 6794 | for (p = cpt; *p; cpt_sources_index++) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6795 | { |
| 6796 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6797 | p++; |
| 6798 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6799 | if (cpt_sources_array[cpt_sources_index].refresh_always) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6800 | { |
| 6801 | if (*p == 'o') |
| 6802 | cb = &curbuf->b_ofu_cb; |
| 6803 | else if (*p == 'f') |
| 6804 | cb = (*(p + 1) != ',' && *(p + 1) != NUL) |
| 6805 | ? get_cpt_func_callback(p + 1) : &curbuf->b_cfu_cb; |
| 6806 | if (cb) |
| 6807 | { |
| 6808 | compl_curr_match = remove_old_matches(); |
| 6809 | get_cpt_func_completion_matches(cb); |
| 6810 | } |
| 6811 | } |
| 6812 | |
| 6813 | copy_option_part(&p, IObuff, IOSIZE, ","); // Advance p |
| 6814 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6815 | cpt_sources_index = -1; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6816 | |
| 6817 | vim_free(cpt); |
| 6818 | // Make the list cyclic |
| 6819 | compl_matches = ins_compl_make_cyclic(); |
| 6820 | #endif |
| 6821 | } |