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 |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 41 | # define CTRL_X_REGISTER 18 // complete words from registers |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 42 | |
| 43 | # define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
| 44 | |
| 45 | // Message for CTRL-X mode, index is ctrl_x_mode. |
| 46 | static char *ctrl_x_msgs[] = |
| 47 | { |
| 48 | N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl. |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 49 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^Rs^U^V^Y)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 50 | NULL, // CTRL_X_SCROLL: depends on state |
| 51 | N_(" Whole line completion (^L^N^P)"), |
| 52 | N_(" File name completion (^F^N^P)"), |
| 53 | N_(" Tag completion (^]^N^P)"), |
| 54 | N_(" Path pattern completion (^N^P)"), |
| 55 | N_(" Definition completion (^D^N^P)"), |
| 56 | NULL, // CTRL_X_FINISHED |
| 57 | N_(" Dictionary completion (^K^N^P)"), |
| 58 | N_(" Thesaurus completion (^T^N^P)"), |
| 59 | N_(" Command-line completion (^V^N^P)"), |
| 60 | N_(" User defined completion (^U^N^P)"), |
| 61 | N_(" Omni completion (^O^N^P)"), |
zeertzjq | 7002c05 | 2024-06-21 07:55:07 +0200 | [diff] [blame] | 62 | N_(" Spelling suggestion (^S^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 63 | N_(" Keyword Local completion (^N^P)"), |
| 64 | NULL, // CTRL_X_EVAL doesn't use msg. |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 65 | N_(" Command-line completion (^V^N^P)"), |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 66 | N_(" Register completion (^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 69 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 70 | static char *ctrl_x_mode_names[] = { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 71 | "keyword", |
| 72 | "ctrl_x", |
| 73 | "scroll", |
| 74 | "whole_line", |
| 75 | "files", |
| 76 | "tags", |
| 77 | "path_patterns", |
| 78 | "path_defines", |
| 79 | "unknown", // CTRL_X_FINISHED |
| 80 | "dictionary", |
| 81 | "thesaurus", |
| 82 | "cmdline", |
| 83 | "function", |
| 84 | "omni", |
| 85 | "spell", |
| 86 | NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs" |
| 87 | "eval", |
| 88 | "cmdline", |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 89 | "register", |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 90 | }; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 91 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 92 | |
| 93 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 94 | * Structure used to store one match for insert completion. |
| 95 | */ |
| 96 | typedef struct compl_S compl_T; |
| 97 | struct compl_S |
| 98 | { |
| 99 | compl_T *cp_next; |
| 100 | compl_T *cp_prev; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 101 | compl_T *cp_match_next; // matched next compl_T |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 102 | string_T cp_str; // matched text |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 103 | char_u *(cp_text[CPT_COUNT]); // text for the menu |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 104 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 105 | typval_T cp_user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 106 | #endif |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 107 | char_u *cp_fname; // file containing the match, allocated when |
| 108 | // cp_flags has CP_FREE_FNAME |
| 109 | int cp_flags; // CP_ values |
| 110 | int cp_number; // sequence number |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 111 | int cp_score; // fuzzy match score or proximity score |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 112 | int cp_in_match_array; // collected by compl_match_array |
glepnir | 7baa014 | 2024-10-09 20:19:25 +0200 | [diff] [blame] | 113 | int cp_user_abbr_hlattr; // highlight attribute for abbr |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 114 | int cp_user_kind_hlattr; // highlight attribute for kind |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 115 | 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] | 116 | }; |
| 117 | |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 118 | // values for cp_flags |
| 119 | # define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun |
| 120 | # define CP_FREE_FNAME 2 // cp_fname is allocated |
| 121 | # define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status |
| 122 | # define CP_EQUAL 8 // ins_compl_equal() always returns TRUE |
| 123 | # define CP_ICASE 16 // ins_compl_equal() ignores case |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 124 | # define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 125 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 126 | /* |
| 127 | * All the current matches are stored in a list. |
| 128 | * "compl_first_match" points to the start of the list. |
| 129 | * "compl_curr_match" points to the currently selected entry. |
| 130 | * "compl_shown_match" is different from compl_curr_match during |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 131 | * ins_compl_get_exp(), when new matches are added to the list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 132 | * "compl_old_match" points to previous "compl_curr_match". |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 133 | */ |
| 134 | static compl_T *compl_first_match = NULL; |
| 135 | static compl_T *compl_curr_match = NULL; |
| 136 | static compl_T *compl_shown_match = NULL; |
| 137 | static compl_T *compl_old_match = NULL; |
| 138 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 139 | // list used to store the compl_T which have the max score |
| 140 | // used for completefuzzycollect |
| 141 | static compl_T **compl_best_matches = NULL; |
| 142 | static int compl_num_bests = 0; |
| 143 | // inserted a longest when completefuzzycollect enabled |
| 144 | static int compl_cfc_longest_ins = FALSE; |
| 145 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 146 | // After using a cursor key <Enter> selects a match in the popup menu, |
| 147 | // otherwise it inserts a line break. |
| 148 | static int compl_enter_selects = FALSE; |
| 149 | |
| 150 | // When "compl_leader" is not NULL only matches that start with this string |
| 151 | // are used. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 152 | static string_T compl_leader = {NULL, 0}; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 153 | |
| 154 | static int compl_get_longest = FALSE; // put longest common string |
| 155 | // in compl_leader |
| 156 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 157 | // Selected one of the matches. When FALSE the match was edited or using the |
| 158 | // longest common string. |
| 159 | static int compl_used_match; |
| 160 | |
| 161 | // didn't finish finding completions. |
| 162 | static int compl_was_interrupted = FALSE; |
| 163 | |
| 164 | // Set when character typed while looking for matches and it means we should |
| 165 | // stop looking for matches. |
| 166 | static int compl_interrupted = FALSE; |
| 167 | |
| 168 | static int compl_restarting = FALSE; // don't insert match |
| 169 | |
| 170 | // When the first completion is done "compl_started" is set. When it's |
| 171 | // FALSE the word to be completed must be located. |
| 172 | static int compl_started = FALSE; |
| 173 | |
| 174 | // Which Ctrl-X mode are we in? |
| 175 | static int ctrl_x_mode = CTRL_X_NORMAL; |
| 176 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 177 | static int compl_matches = 0; // number of completion matches |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 178 | static string_T compl_pattern = {NULL, 0}; // search pattern for matching items |
| 179 | #ifdef FEAT_COMPL_FUNC |
| 180 | static string_T cpt_compl_pattern = {NULL, 0}; // pattern returned by func in 'cpt' |
| 181 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 182 | static int compl_direction = FORWARD; |
| 183 | static int compl_shows_dir = FORWARD; |
| 184 | static int compl_pending = 0; // > 1 for postponed CTRL-N |
| 185 | static pos_T compl_startpos; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 186 | // Length in bytes of the text being completed (this is deleted to be replaced |
| 187 | // by the match.) |
| 188 | static int compl_length = 0; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 189 | static linenr_T compl_lnum = 0; // lnum where the completion start |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 190 | static colnr_T compl_col = 0; // column where the text starts |
| 191 | // that is being completed |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 192 | static colnr_T compl_ins_end_col = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 193 | 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] | 194 | // completion started |
| 195 | static int compl_cont_mode = 0; |
| 196 | static expand_T compl_xp; |
| 197 | |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 198 | static win_T *compl_curr_win = NULL; // win where completion is active |
| 199 | static buf_T *compl_curr_buf = NULL; // buf where completion is active |
| 200 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 201 | // List of flags for method of completion. |
| 202 | static int compl_cont_status = 0; |
| 203 | # define CONT_ADDING 1 // "normal" or "adding" expansion |
| 204 | # define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion |
| 205 | // it's set only iff N_ADDS is set |
| 206 | # define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current |
| 207 | # define CONT_S_IPOS 8 // next ^X<> will set initial_pos? |
| 208 | // if so, word-wise-expansion will set SOL |
| 209 | # define CONT_SOL 16 // pattern includes start of line, just for |
| 210 | // word-wise expansion, not set for ^X^L |
| 211 | # define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local |
| 212 | // expansion, (eg use complete=.) |
| 213 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 214 | static int compl_opt_refresh_always = FALSE; |
| 215 | static int compl_opt_suppress_empty = FALSE; |
| 216 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 217 | static int compl_selected_item = -1; |
| 218 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 219 | static int *compl_fuzzy_scores; |
| 220 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 221 | // Define the structure for completion source (in 'cpt' option) information |
| 222 | typedef struct cpt_source_T |
| 223 | { |
| 224 | int refresh_always; // Flag array to indicate which 'cpt' functions have 'refresh:always' set |
| 225 | int max_matches; // Maximum number of items to display in the menu from the source |
| 226 | } cpt_source_T; |
| 227 | |
| 228 | static cpt_source_T *cpt_sources_array; // Pointer to the array of completion sources |
| 229 | static int cpt_sources_count; // Total number of completion sources specified in the 'cpt' option |
| 230 | 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] | 231 | |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 232 | // "compl_match_array" points the currently displayed list of entries in the |
| 233 | // popup menu. It is NULL when there is no popup menu. |
| 234 | static pumitem_T *compl_match_array = NULL; |
| 235 | static int compl_match_arraysize; |
| 236 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 237 | 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] | 238 | static void ins_compl_longest_match(compl_T *match); |
| 239 | static void ins_compl_del_pum(void); |
| 240 | 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] | 241 | static void ins_compl_free(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 242 | static int ins_compl_need_restart(void); |
| 243 | static void ins_compl_new_leader(void); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 244 | static int get_compl_len(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 245 | static void ins_compl_restart(void); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 246 | 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] | 247 | static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg); |
| 248 | # if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 249 | static void ins_compl_add_list(list_T *list); |
| 250 | static void ins_compl_add_dict(dict_T *dict); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 251 | 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] | 252 | static void get_cpt_func_completion_matches(callback_T *cb); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 253 | static callback_T *get_cpt_func_callback(char_u *funcname); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 254 | # endif |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 255 | static int cpt_sources_init(void); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 256 | static int is_cpt_func_refresh_always(void); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 257 | static void cpt_sources_clear(void); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 258 | static void cpt_compl_refresh(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 259 | static int ins_compl_key2dir(int c); |
| 260 | static int ins_compl_pum_key(int c); |
| 261 | static int ins_compl_key2count(int c); |
| 262 | static void show_pum(int prev_w_wrow, int prev_w_leftcol); |
| 263 | static unsigned quote_meta(char_u *dest, char_u *str, int len); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 264 | static int ins_compl_has_multiple(void); |
| 265 | static void ins_compl_expand_multiple(char_u *str); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 266 | static void ins_compl_longest_insert(char_u *prefix); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 267 | |
| 268 | #ifdef FEAT_SPELL |
| 269 | static void spell_back_to_badword(void); |
| 270 | static int spell_bad_len = 0; // length of located bad word |
| 271 | #endif |
| 272 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 273 | /* |
| 274 | * CTRL-X pressed in Insert mode. |
| 275 | */ |
| 276 | void |
| 277 | ins_ctrl_x(void) |
| 278 | { |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 279 | if (!ctrl_x_mode_cmdline()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 280 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 281 | // 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] | 282 | if (compl_cont_status & CONT_N_ADDS) |
| 283 | compl_cont_status |= CONT_INTRPT; |
| 284 | else |
| 285 | compl_cont_status = 0; |
| 286 | // We're not sure which CTRL-X mode it will be yet |
| 287 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 288 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 289 | edit_submode_pre = NULL; |
| 290 | showmode(); |
| 291 | } |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 292 | else |
| 293 | // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X |
| 294 | // CTRL-V look like CTRL-N |
| 295 | 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] | 296 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 297 | may_trigger_modechanged(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Functions to check the current CTRL-X mode. |
| 302 | */ |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 303 | int ctrl_x_mode_none(void) |
| 304 | { return ctrl_x_mode == 0; } |
| 305 | int ctrl_x_mode_normal(void) |
| 306 | { return ctrl_x_mode == CTRL_X_NORMAL; } |
| 307 | int ctrl_x_mode_scroll(void) |
| 308 | { return ctrl_x_mode == CTRL_X_SCROLL; } |
| 309 | int ctrl_x_mode_whole_line(void) |
| 310 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE; } |
| 311 | int ctrl_x_mode_files(void) |
| 312 | { return ctrl_x_mode == CTRL_X_FILES; } |
| 313 | int ctrl_x_mode_tags(void) |
| 314 | { return ctrl_x_mode == CTRL_X_TAGS; } |
| 315 | int ctrl_x_mode_path_patterns(void) |
| 316 | { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; } |
| 317 | int ctrl_x_mode_path_defines(void) |
| 318 | { return ctrl_x_mode == CTRL_X_PATH_DEFINES; } |
| 319 | int ctrl_x_mode_dictionary(void) |
| 320 | { return ctrl_x_mode == CTRL_X_DICTIONARY; } |
| 321 | int ctrl_x_mode_thesaurus(void) |
| 322 | { return ctrl_x_mode == CTRL_X_THESAURUS; } |
| 323 | int ctrl_x_mode_cmdline(void) |
| 324 | { return ctrl_x_mode == CTRL_X_CMDLINE |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 325 | || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; } |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 326 | int ctrl_x_mode_function(void) |
| 327 | { return ctrl_x_mode == CTRL_X_FUNCTION; } |
| 328 | int ctrl_x_mode_omni(void) |
| 329 | { return ctrl_x_mode == CTRL_X_OMNI; } |
| 330 | int ctrl_x_mode_spell(void) |
| 331 | { return ctrl_x_mode == CTRL_X_SPELL; } |
| 332 | static int ctrl_x_mode_eval(void) |
| 333 | { return ctrl_x_mode == CTRL_X_EVAL; } |
| 334 | int ctrl_x_mode_line_or_eval(void) |
| 335 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; } |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 336 | int ctrl_x_mode_register(void) |
| 337 | { return ctrl_x_mode == CTRL_X_REGISTER; } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Whether other than default completion has been selected. |
| 341 | */ |
| 342 | int |
| 343 | ctrl_x_mode_not_default(void) |
| 344 | { |
| 345 | return ctrl_x_mode != CTRL_X_NORMAL; |
| 346 | } |
| 347 | |
| 348 | /* |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 349 | * Whether CTRL-X was typed without a following character, |
| 350 | * not including when in CTRL-X CTRL-V mode. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 351 | */ |
| 352 | int |
| 353 | ctrl_x_mode_not_defined_yet(void) |
| 354 | { |
| 355 | return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; |
| 356 | } |
| 357 | |
| 358 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 359 | * Return TRUE if currently in "normal" or "adding" insert completion matches |
| 360 | * state |
| 361 | */ |
| 362 | int |
| 363 | compl_status_adding(void) |
| 364 | { |
| 365 | return compl_cont_status & CONT_ADDING; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Return TRUE if the completion pattern includes start of line, just for |
| 370 | * word-wise expansion. |
| 371 | */ |
| 372 | int |
| 373 | compl_status_sol(void) |
| 374 | { |
| 375 | return compl_cont_status & CONT_SOL; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.) |
| 380 | */ |
| 381 | int |
| 382 | compl_status_local(void) |
| 383 | { |
| 384 | return compl_cont_status & CONT_LOCAL; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Clear the completion status flags |
| 389 | */ |
| 390 | void |
| 391 | compl_status_clear(void) |
| 392 | { |
| 393 | compl_cont_status = 0; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Return TRUE if completion is using the forward direction matches |
| 398 | */ |
| 399 | static int |
| 400 | compl_dir_forward(void) |
| 401 | { |
| 402 | return compl_direction == FORWARD; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Return TRUE if currently showing forward completion matches |
| 407 | */ |
| 408 | static int |
| 409 | compl_shows_dir_forward(void) |
| 410 | { |
| 411 | return compl_shows_dir == FORWARD; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Return TRUE if currently showing backward completion matches |
| 416 | */ |
| 417 | static int |
| 418 | compl_shows_dir_backward(void) |
| 419 | { |
| 420 | return compl_shows_dir == BACKWARD; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Return TRUE if the 'dictionary' or 'thesaurus' option can be used. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 425 | */ |
| 426 | int |
| 427 | has_compl_option(int dict_opt) |
| 428 | { |
| 429 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 430 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 431 | && !curwin->w_p_spell |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 432 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 433 | ) |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 434 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL |
| 435 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 436 | && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 437 | #endif |
| 438 | )) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 439 | { |
| 440 | ctrl_x_mode = CTRL_X_NORMAL; |
| 441 | edit_submode = NULL; |
| 442 | msg_attr(dict_opt ? _("'dictionary' option is empty") |
| 443 | : _("'thesaurus' option is empty"), |
| 444 | HL_ATTR(HLF_E)); |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 445 | if (emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 446 | { |
| 447 | vim_beep(BO_COMPL); |
| 448 | setcursor(); |
| 449 | out_flush(); |
| 450 | #ifdef FEAT_EVAL |
| 451 | if (!get_vim_var_nr(VV_TESTING)) |
| 452 | #endif |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 453 | ui_delay(2004L, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 454 | } |
| 455 | return FALSE; |
| 456 | } |
| 457 | return TRUE; |
| 458 | } |
| 459 | |
| 460 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 461 | * 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] | 462 | * This depends on the current mode. |
| 463 | */ |
| 464 | int |
| 465 | vim_is_ctrl_x_key(int c) |
| 466 | { |
| 467 | // Always allow ^R - let its results then be checked |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 468 | if (c == Ctrl_R && ctrl_x_mode != CTRL_X_REGISTER) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 469 | return TRUE; |
| 470 | |
| 471 | // Accept <PageUp> and <PageDown> if the popup menu is visible. |
| 472 | if (ins_compl_pum_key(c)) |
| 473 | return TRUE; |
| 474 | |
| 475 | switch (ctrl_x_mode) |
| 476 | { |
| 477 | case 0: // Not in any CTRL-X mode |
| 478 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 479 | case CTRL_X_NOT_DEFINED_YET: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 480 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 481 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
| 482 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 483 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 484 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
| 485 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 486 | || c == Ctrl_S || c == Ctrl_K || c == 's' |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 487 | || c == Ctrl_Z || c == Ctrl_R); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 488 | case CTRL_X_SCROLL: |
| 489 | return (c == Ctrl_Y || c == Ctrl_E); |
| 490 | case CTRL_X_WHOLE_LINE: |
| 491 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 492 | case CTRL_X_FILES: |
| 493 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 494 | case CTRL_X_DICTIONARY: |
| 495 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 496 | case CTRL_X_THESAURUS: |
| 497 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 498 | case CTRL_X_TAGS: |
| 499 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 500 | #ifdef FEAT_FIND_ID |
| 501 | case CTRL_X_PATH_PATTERNS: |
| 502 | return (c == Ctrl_P || c == Ctrl_N); |
| 503 | case CTRL_X_PATH_DEFINES: |
| 504 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 505 | #endif |
| 506 | case CTRL_X_CMDLINE: |
| 507 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 508 | || c == Ctrl_X); |
| 509 | #ifdef FEAT_COMPL_FUNC |
| 510 | case CTRL_X_FUNCTION: |
| 511 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
| 512 | case CTRL_X_OMNI: |
| 513 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
| 514 | #endif |
| 515 | case CTRL_X_SPELL: |
| 516 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
| 517 | case CTRL_X_EVAL: |
| 518 | return (c == Ctrl_P || c == Ctrl_N); |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 519 | case CTRL_X_REGISTER: |
| 520 | return (c == Ctrl_R || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 521 | } |
| 522 | internal_error("vim_is_ctrl_x_key()"); |
| 523 | return FALSE; |
| 524 | } |
| 525 | |
| 526 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 527 | * Return TRUE if "match" is the original text when the completion began. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 528 | */ |
| 529 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 530 | match_at_original_text(compl_T *match) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 531 | { |
| 532 | return match->cp_flags & CP_ORIGINAL_TEXT; |
| 533 | } |
| 534 | |
| 535 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 536 | * Returns TRUE if "match" is the first match in the completion list. |
| 537 | */ |
| 538 | static int |
| 539 | is_first_match(compl_T *match) |
| 540 | { |
| 541 | return match == compl_first_match; |
| 542 | } |
| 543 | |
| 544 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 545 | * Return TRUE when character "c" is part of the item currently being |
| 546 | * completed. Used to decide whether to abandon complete mode when the menu |
| 547 | * is visible. |
| 548 | */ |
| 549 | int |
| 550 | ins_compl_accept_char(int c) |
| 551 | { |
| 552 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 553 | // When expanding an identifier only accept identifier chars. |
| 554 | return vim_isIDc(c); |
| 555 | |
| 556 | switch (ctrl_x_mode) |
| 557 | { |
| 558 | case CTRL_X_FILES: |
| 559 | // When expanding file name only accept file name chars. But not |
| 560 | // path separators, so that "proto/<Tab>" expands files in |
| 561 | // "proto", not "proto/" as a whole |
| 562 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 563 | |
| 564 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 565 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 566 | case CTRL_X_OMNI: |
| 567 | // Command line and Omni completion can work with just about any |
| 568 | // printable character, but do stop at white space. |
| 569 | return vim_isprintc(c) && !VIM_ISWHITE(c); |
| 570 | |
| 571 | case CTRL_X_WHOLE_LINE: |
| 572 | // For while line completion a space can be part of the line. |
| 573 | return vim_isprintc(c); |
| 574 | } |
| 575 | return vim_iswordc(c); |
| 576 | } |
| 577 | |
| 578 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 579 | * 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] | 580 | * If the result is in allocated memory "tofree" is set to it. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 581 | */ |
| 582 | static char_u * |
| 583 | ins_compl_infercase_gettext( |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 584 | char_u *str, |
| 585 | int char_len, |
| 586 | int compl_char_len, |
| 587 | int min_len, |
| 588 | char_u **tofree) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 589 | { |
| 590 | int *wca; // Wide character array. |
| 591 | char_u *p; |
| 592 | int i, c; |
| 593 | int has_lower = FALSE; |
| 594 | int was_letter = FALSE; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 595 | garray_T gap; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 596 | |
| 597 | IObuff[0] = NUL; |
| 598 | |
| 599 | // Allocate wide character array for the completion and fill it. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 600 | wca = ALLOC_MULT(int, char_len); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 601 | if (wca == NULL) |
| 602 | return IObuff; |
| 603 | |
| 604 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 605 | for (i = 0; i < char_len; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 606 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 607 | if (has_mbyte) |
| 608 | wca[i] = mb_ptr2char_adv(&p); |
| 609 | else |
| 610 | wca[i] = *(p++); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 611 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 612 | |
| 613 | // Rule 1: Were any chars converted to lower? |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 614 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 615 | for (i = 0; i < min_len; ++i) |
| 616 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 617 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 618 | if (MB_ISLOWER(c)) |
| 619 | { |
| 620 | has_lower = TRUE; |
| 621 | if (MB_ISUPPER(wca[i])) |
| 622 | { |
| 623 | // Rule 1 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 624 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 625 | wca[i] = MB_TOLOWER(wca[i]); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // Rule 2: No lower case, 2nd consecutive letter converted to |
| 632 | // upper case. |
| 633 | if (!has_lower) |
| 634 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 635 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 636 | for (i = 0; i < min_len; ++i) |
| 637 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 638 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 639 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 640 | { |
| 641 | // Rule 2 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 642 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 643 | wca[i] = MB_TOUPPER(wca[i]); |
| 644 | break; |
| 645 | } |
| 646 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // Copy the original case of the part we typed. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 651 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 652 | for (i = 0; i < min_len; ++i) |
| 653 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 654 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 655 | if (MB_ISLOWER(c)) |
| 656 | wca[i] = MB_TOLOWER(wca[i]); |
| 657 | else if (MB_ISUPPER(c)) |
| 658 | wca[i] = MB_TOUPPER(wca[i]); |
| 659 | } |
| 660 | |
| 661 | // Generate encoding specific output from wide character array. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 662 | p = IObuff; |
| 663 | i = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 664 | ga_init2(&gap, 1, 500); |
| 665 | while (i < char_len) |
| 666 | { |
| 667 | if (gap.ga_data != NULL) |
| 668 | { |
| 669 | if (ga_grow(&gap, 10) == FAIL) |
| 670 | { |
| 671 | ga_clear(&gap); |
| 672 | return (char_u *)"[failed]"; |
| 673 | } |
| 674 | p = (char_u *)gap.ga_data + gap.ga_len; |
| 675 | if (has_mbyte) |
| 676 | gap.ga_len += (*mb_char2bytes)(wca[i++], p); |
| 677 | else |
| 678 | { |
| 679 | *p = wca[i++]; |
| 680 | ++gap.ga_len; |
| 681 | } |
| 682 | } |
| 683 | else if ((p - IObuff) + 6 >= IOSIZE) |
| 684 | { |
| 685 | // Multi-byte characters can occupy up to five bytes more than |
| 686 | // ASCII characters, and we also need one byte for NUL, so when |
| 687 | // getting to six bytes from the edge of IObuff switch to using a |
| 688 | // growarray. Add the character in the next round. |
| 689 | if (ga_grow(&gap, IOSIZE) == FAIL) |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 690 | { |
| 691 | vim_free(wca); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 692 | return (char_u *)"[failed]"; |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 693 | } |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 694 | *p = NUL; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 695 | STRCPY(gap.ga_data, IObuff); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 696 | gap.ga_len = (int)(p - IObuff); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 697 | } |
| 698 | else if (has_mbyte) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 699 | p += (*mb_char2bytes)(wca[i++], p); |
| 700 | else |
| 701 | *(p++) = wca[i++]; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 702 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 703 | vim_free(wca); |
| 704 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 705 | if (gap.ga_data != NULL) |
| 706 | { |
| 707 | *tofree = gap.ga_data; |
| 708 | return gap.ga_data; |
| 709 | } |
| 710 | |
| 711 | *p = NUL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 712 | return IObuff; |
| 713 | } |
| 714 | |
| 715 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 716 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
| 717 | * case of the originally typed text is used, and the case of the completed |
| 718 | * text is inferred, ie this tries to work out what case you probably wanted |
| 719 | * the rest of the word to be in -- webb |
| 720 | */ |
| 721 | int |
| 722 | ins_compl_add_infercase( |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 723 | char_u *str_arg, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 724 | int len, |
| 725 | int icase, |
| 726 | char_u *fname, |
| 727 | int dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 728 | int cont_s_ipos, // next ^X<> will set initial_pos |
| 729 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 730 | { |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 731 | char_u *str = str_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 732 | char_u *p; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 733 | int char_len; // count multi-byte characters |
| 734 | int compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 735 | int min_len; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 736 | int flags = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 737 | int res; |
| 738 | char_u *tofree = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 739 | |
| 740 | if (p_ic && curbuf->b_p_inf && len > 0) |
| 741 | { |
| 742 | // Infer case of completed part. |
| 743 | |
| 744 | // Find actual length of completion. |
| 745 | if (has_mbyte) |
| 746 | { |
| 747 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 748 | char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 749 | while (*p != NUL) |
| 750 | { |
| 751 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 752 | ++char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 756 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 757 | char_len = len; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 758 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 759 | |
| 760 | // Find actual length of original text. |
| 761 | if (has_mbyte) |
| 762 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 763 | p = compl_orig_text.string; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 764 | compl_char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 765 | while (*p != NUL) |
| 766 | { |
| 767 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 768 | ++compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 772 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 773 | compl_char_len = compl_length; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 774 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 775 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 776 | // "char_len" may be smaller than "compl_char_len" when using |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 777 | // thesaurus, only use the minimum when comparing. |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 778 | min_len = MIN(char_len, compl_char_len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 779 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 780 | str = ins_compl_infercase_gettext(str, char_len, |
| 781 | compl_char_len, min_len, &tofree); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 782 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 783 | if (cont_s_ipos) |
| 784 | flags |= CP_CONT_S_IPOS; |
| 785 | if (icase) |
| 786 | flags |= CP_ICASE; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 787 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 788 | 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] | 789 | vim_free(tofree); |
| 790 | return res; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 794 | * Check if ctrl_x_mode has been configured in 'completefuzzycollect' |
| 795 | */ |
| 796 | static int |
| 797 | cfc_has_mode(void) |
| 798 | { |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 799 | if (ctrl_x_mode_normal() || ctrl_x_mode_dictionary()) |
| 800 | return (cfc_flags & CFC_KEYWORD) != 0; |
| 801 | else if (ctrl_x_mode_files()) |
| 802 | return (cfc_flags & CFC_FILES) != 0; |
| 803 | else if (ctrl_x_mode_whole_line()) |
| 804 | return (cfc_flags & CFC_WHOLELINE) != 0; |
| 805 | else |
| 806 | return FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 810 | * Returns TRUE if matches should be sorted based on proximity to the cursor. |
| 811 | */ |
| 812 | static int |
| 813 | is_nearest_active(void) |
| 814 | { |
glepnir | c3fbaa0 | 2025-05-08 23:05:10 +0200 | [diff] [blame] | 815 | return (get_cot_flags() & (COT_NEAREST | COT_FUZZY)) == COT_NEAREST; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Repositions a match in the completion list based on its proximity score. |
| 820 | * If the match is at the head and has a higher score than the next node, |
| 821 | * or if it's in the middle/tail and has a lower score than the previous node, |
| 822 | * it is moved to the correct position while maintaining ascending order. |
| 823 | */ |
| 824 | static void |
| 825 | reposition_match(compl_T *match) |
| 826 | { |
| 827 | compl_T *insert_before = NULL; |
| 828 | compl_T *insert_after = NULL; |
| 829 | |
| 830 | // Node is at head and score is too big |
| 831 | if (!match->cp_prev) |
| 832 | { |
| 833 | if (match->cp_next && match->cp_next->cp_score > 0 && |
| 834 | match->cp_next->cp_score < match->cp_score) |
| 835 | { |
| 836 | // <c-p>: compl_first_match is at head and newly inserted node |
| 837 | compl_first_match = compl_curr_match = match->cp_next; |
| 838 | // Find the correct position in ascending order |
| 839 | insert_before = match->cp_next; |
| 840 | do |
| 841 | { |
| 842 | insert_after = insert_before; |
| 843 | insert_before = insert_before->cp_next; |
| 844 | } while (insert_before && insert_before->cp_score > 0 && |
| 845 | insert_before->cp_score < match->cp_score); |
| 846 | } |
| 847 | else |
| 848 | return; |
| 849 | } |
| 850 | // Node is at tail or in the middle but score is too small |
| 851 | else |
| 852 | { |
| 853 | if (match->cp_prev->cp_score > 0 && match->cp_prev->cp_score > match->cp_score) |
| 854 | { |
| 855 | // <c-n>: compl_curr_match (and newly inserted match) is at tail |
| 856 | if (!match->cp_next) |
| 857 | compl_curr_match = compl_curr_match->cp_prev; |
| 858 | // Find the correct position in ascending order |
| 859 | insert_after = match->cp_prev; |
| 860 | do |
| 861 | { |
| 862 | insert_before = insert_after; |
| 863 | insert_after = insert_after->cp_prev; |
| 864 | } while (insert_after && insert_after->cp_score > 0 && |
| 865 | insert_after->cp_score > match->cp_score); |
| 866 | } |
| 867 | else |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | if (insert_after) |
| 872 | { |
| 873 | // Remove the match from its current position |
| 874 | if (match->cp_prev) |
| 875 | match->cp_prev->cp_next = match->cp_next; |
| 876 | else |
| 877 | compl_first_match = match->cp_next; |
| 878 | if (match->cp_next) |
| 879 | match->cp_next->cp_prev = match->cp_prev; |
| 880 | |
| 881 | // Insert the match at the correct position |
| 882 | match->cp_next = insert_before; |
| 883 | match->cp_prev = insert_after; |
| 884 | insert_after->cp_next = match; |
| 885 | insert_before->cp_prev = match; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 890 | * Add a match to the list of matches. The arguments are: |
| 891 | * str - text of the match to add |
| 892 | * len - length of "str". If -1, then the length of "str" is |
| 893 | * computed. |
| 894 | * fname - file name to associate with this match. |
| 895 | * cptext - list of strings to use with this match (for abbr, menu, info |
| 896 | * and kind) |
| 897 | * user_data - user supplied data (any vim type) for this match |
| 898 | * cdir - match direction. If 0, use "compl_direction". |
| 899 | * flags_arg - match flags (cp_flags) |
| 900 | * adup - accept this match even if it is already present. |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 901 | * *user_hl - list of extra highlight attributes for abbr kind. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 902 | * If "cdir" is FORWARD, then the match is added after the current match. |
| 903 | * Otherwise, it is added before the current match. |
| 904 | * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 905 | * If the given string is already in the list of completions, then return |
| 906 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 907 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 908 | */ |
| 909 | static int |
| 910 | ins_compl_add( |
| 911 | char_u *str, |
| 912 | int len, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 913 | char_u *fname, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 914 | char_u **cptext, // extra text for popup menu or NULL |
| 915 | typval_T *user_data UNUSED, // "user_data" entry or NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 916 | int cdir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 917 | int flags_arg, |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 918 | int adup, // accept duplicate match |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 919 | int *user_hl, // user abbr/kind hlattr |
| 920 | int score) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 921 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 922 | compl_T *match, *current, *prev; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 923 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 924 | int flags = flags_arg; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 925 | int inserted = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 926 | |
Bram Moolenaar | ceb0619 | 2021-04-04 15:05:22 +0200 | [diff] [blame] | 927 | if (flags & CP_FAST) |
| 928 | fast_breakcheck(); |
| 929 | else |
| 930 | ui_breakcheck(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 931 | if (got_int) |
| 932 | return FAIL; |
| 933 | if (len < 0) |
| 934 | len = (int)STRLEN(str); |
| 935 | |
| 936 | // If the same match is already present, don't add it. |
| 937 | if (compl_first_match != NULL && !adup) |
| 938 | { |
| 939 | match = compl_first_match; |
| 940 | do |
| 941 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 942 | if (!match_at_original_text(match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 943 | && STRNCMP(match->cp_str.string, str, len) == 0 |
| 944 | && ((int)match->cp_str.length <= len |
| 945 | || match->cp_str.string[len] == NUL)) |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 946 | { |
| 947 | if (is_nearest_active() && score > 0 && score < match->cp_score) |
| 948 | { |
| 949 | match->cp_score = score; |
| 950 | reposition_match(match); |
| 951 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 952 | return NOTDONE; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 953 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 954 | match = match->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 955 | } while (match != NULL && !is_first_match(match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | // Remove any popup menu before changing the list of matches. |
| 959 | ins_compl_del_pum(); |
| 960 | |
| 961 | // Allocate a new match structure. |
| 962 | // Copy the values to the new match structure. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 963 | match = ALLOC_CLEAR_ONE(compl_T); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 964 | if (match == NULL) |
| 965 | return FAIL; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 966 | match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 967 | if ((match->cp_str.string = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 968 | { |
| 969 | vim_free(match); |
| 970 | return FAIL; |
| 971 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 972 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 973 | match->cp_str.length = len; |
| 974 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 975 | // match-fname is: |
| 976 | // - 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] | 977 | // - 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] | 978 | // - NULL otherwise. --Acevedo |
| 979 | if (fname != NULL |
| 980 | && compl_curr_match != NULL |
| 981 | && compl_curr_match->cp_fname != NULL |
| 982 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 983 | match->cp_fname = compl_curr_match->cp_fname; |
| 984 | else if (fname != NULL) |
| 985 | { |
| 986 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 987 | flags |= CP_FREE_FNAME; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 988 | } |
| 989 | else |
| 990 | match->cp_fname = NULL; |
| 991 | match->cp_flags = flags; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 992 | match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1; |
| 993 | match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 994 | match->cp_score = score; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 995 | match->cp_cpt_source_idx = cpt_sources_index; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 996 | |
| 997 | if (cptext != NULL) |
| 998 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 999 | for (int i = 0; i < CPT_COUNT; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1000 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1001 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 1002 | match->cp_text[i] = vim_strsave(cptext[i]); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1003 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1004 | } |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1005 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 1006 | if (user_data != NULL) |
| 1007 | match->cp_user_data = *user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1008 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1009 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1010 | // Link the new match structure after (FORWARD) or before (BACKWARD) the |
| 1011 | // current match in the list of matches . |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1012 | if (compl_first_match == NULL) |
| 1013 | match->cp_next = match->cp_prev = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1014 | else if (cfc_has_mode() && score > 0 && compl_get_longest) |
| 1015 | { |
| 1016 | current = compl_first_match->cp_next; |
| 1017 | prev = compl_first_match; |
| 1018 | inserted = FALSE; |
| 1019 | // The direction is ignored when using longest and |
| 1020 | // completefuzzycollect, because matches are inserted |
| 1021 | // and sorted by score. |
| 1022 | while (current != NULL && current != compl_first_match) |
| 1023 | { |
| 1024 | if (current->cp_score < score) |
| 1025 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 1026 | match->cp_next = current; |
| 1027 | match->cp_prev = current->cp_prev; |
| 1028 | if (current->cp_prev) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1029 | current->cp_prev->cp_next = match; |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 1030 | current->cp_prev = match; |
| 1031 | inserted = TRUE; |
| 1032 | break; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1033 | } |
| 1034 | prev = current; |
| 1035 | current = current->cp_next; |
| 1036 | } |
| 1037 | if (!inserted) |
| 1038 | { |
| 1039 | prev->cp_next = match; |
| 1040 | match->cp_prev = prev; |
| 1041 | match->cp_next = compl_first_match; |
| 1042 | compl_first_match->cp_prev = match; |
| 1043 | } |
| 1044 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1045 | else if (dir == FORWARD) |
| 1046 | { |
| 1047 | match->cp_next = compl_curr_match->cp_next; |
| 1048 | match->cp_prev = compl_curr_match; |
| 1049 | } |
| 1050 | else // BACKWARD |
| 1051 | { |
| 1052 | match->cp_next = compl_curr_match; |
| 1053 | match->cp_prev = compl_curr_match->cp_prev; |
| 1054 | } |
| 1055 | if (match->cp_next) |
| 1056 | match->cp_next->cp_prev = match; |
| 1057 | if (match->cp_prev) |
| 1058 | match->cp_prev->cp_next = match; |
| 1059 | else // if there's nothing before, it is the first match |
| 1060 | compl_first_match = match; |
| 1061 | compl_curr_match = match; |
| 1062 | |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 1063 | if (is_nearest_active() && score > 0) |
| 1064 | reposition_match(match); |
| 1065 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1066 | // Find the longest common string if still doing that. |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1067 | if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0 && !cfc_has_mode()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1068 | ins_compl_longest_match(match); |
| 1069 | |
| 1070 | return OK; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1075 | * match->cp_flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1076 | */ |
| 1077 | static int |
| 1078 | ins_compl_equal(compl_T *match, char_u *str, int len) |
| 1079 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1080 | if (match->cp_flags & CP_EQUAL) |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 1081 | return TRUE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1082 | if (match->cp_flags & CP_ICASE) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1083 | return STRNICMP(match->cp_str.string, str, (size_t)len) == 0; |
| 1084 | return STRNCMP(match->cp_str.string, str, (size_t)len) == 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1088 | * when len is -1 mean use whole length of p otherwise part of p |
| 1089 | */ |
| 1090 | static void |
| 1091 | ins_compl_insert_bytes(char_u *p, int len) |
| 1092 | { |
| 1093 | if (len == -1) |
| 1094 | len = (int)STRLEN(p); |
| 1095 | ins_bytes_len(p, len); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 1096 | compl_ins_end_col = curwin->w_cursor.col; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
zeertzjq | d32bf0a | 2024-12-17 20:55:13 +0100 | [diff] [blame] | 1100 | * Checks if the column is within the currently inserted completion text |
| 1101 | * column range. If it is, it returns a special highlight attribute. |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1102 | * -1 means normal item. |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1103 | */ |
| 1104 | int |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1105 | ins_compl_col_range_attr(linenr_T lnum, int col) |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1106 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1107 | int start_col; |
| 1108 | int attr; |
| 1109 | |
| 1110 | if ((get_cot_flags() & COT_FUZZY) |
| 1111 | || (attr = syn_name2attr((char_u *)"ComplMatchIns")) == 0) |
glepnir | e890887 | 2025-01-08 18:30:45 +0100 | [diff] [blame] | 1112 | return -1; |
| 1113 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1114 | start_col = compl_col + (int)ins_compl_leader_len(); |
| 1115 | if (!ins_compl_has_multiple()) |
| 1116 | return (col >= start_col && col < compl_ins_end_col) ? attr : -1; |
| 1117 | |
| 1118 | // Multiple lines |
| 1119 | if ((lnum == compl_lnum && col >= start_col && col < MAXCOL) || |
| 1120 | (lnum > compl_lnum && lnum < curwin->w_cursor.lnum) || |
| 1121 | (lnum == curwin->w_cursor.lnum && col <= compl_ins_end_col)) |
| 1122 | return attr; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1123 | |
| 1124 | return -1; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 1128 | * Returns TRUE if the current completion string contains newline characters, |
| 1129 | * indicating it's a multi-line completion. |
| 1130 | */ |
| 1131 | static int |
| 1132 | ins_compl_has_multiple(void) |
| 1133 | { |
| 1134 | return vim_strchr(compl_shown_match->cp_str.string, '\n') != NULL; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Returns TRUE if the given line number falls within the range of a multi-line |
| 1139 | * completion, i.e. between the starting line (compl_lnum) and current cursor |
| 1140 | * line. Always returns FALSE for single-line completions. |
| 1141 | */ |
| 1142 | int |
| 1143 | ins_compl_lnum_in_range(linenr_T lnum) |
| 1144 | { |
| 1145 | if (!ins_compl_has_multiple()) |
| 1146 | return FALSE; |
| 1147 | return lnum >= compl_lnum && lnum <= curwin->w_cursor.lnum; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1151 | * Reduce the longest common string for match "match". |
| 1152 | */ |
| 1153 | static void |
| 1154 | ins_compl_longest_match(compl_T *match) |
| 1155 | { |
| 1156 | char_u *p, *s; |
| 1157 | int c1, c2; |
| 1158 | int had_match; |
| 1159 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1160 | if (compl_leader.string == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1161 | { |
| 1162 | // First match, use it as a whole. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1163 | compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length); |
| 1164 | if (compl_leader.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1165 | return; |
| 1166 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1167 | compl_leader.length = match->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1168 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1169 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1170 | |
| 1171 | // When the match isn't there (to avoid matching itself) remove it |
| 1172 | // again after redrawing. |
| 1173 | if (!had_match) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1174 | ins_compl_delete(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1175 | compl_used_match = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1176 | |
| 1177 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1178 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Reduce the text if this match differs from compl_leader. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1181 | p = compl_leader.string; |
| 1182 | s = match->cp_str.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1183 | while (*p != NUL) |
| 1184 | { |
| 1185 | if (has_mbyte) |
| 1186 | { |
| 1187 | c1 = mb_ptr2char(p); |
| 1188 | c2 = mb_ptr2char(s); |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | c1 = *p; |
| 1193 | c2 = *s; |
| 1194 | } |
| 1195 | if ((match->cp_flags & CP_ICASE) |
| 1196 | ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2)) |
| 1197 | break; |
| 1198 | if (has_mbyte) |
| 1199 | { |
| 1200 | MB_PTR_ADV(p); |
| 1201 | MB_PTR_ADV(s); |
| 1202 | } |
| 1203 | else |
| 1204 | { |
| 1205 | ++p; |
| 1206 | ++s; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | if (*p != NUL) |
| 1211 | { |
| 1212 | // Leader was shortened, need to change the inserted text. |
| 1213 | *p = NUL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1214 | compl_leader.length = (size_t)(p - compl_leader.string); |
| 1215 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1216 | had_match = (curwin->w_cursor.col > compl_col); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1217 | ins_compl_longest_insert(compl_leader.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1218 | |
| 1219 | // When the match isn't there (to avoid matching itself) remove it |
| 1220 | // again after redrawing. |
| 1221 | if (!had_match) |
| 1222 | ins_compl_delete(); |
| 1223 | } |
| 1224 | |
| 1225 | compl_used_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Add an array of matches to the list of matches. |
| 1230 | * Frees matches[]. |
| 1231 | */ |
| 1232 | static void |
| 1233 | ins_compl_add_matches( |
| 1234 | int num_matches, |
| 1235 | char_u **matches, |
| 1236 | int icase) |
| 1237 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1238 | int add_r = OK; |
| 1239 | int dir = compl_direction; |
| 1240 | |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1241 | for (int i = 0; i < num_matches && add_r != FAIL; i++) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1242 | { |
| 1243 | add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1244 | CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL, 0); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1245 | if (add_r == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1246 | // if dir was BACKWARD then honor it just once |
| 1247 | dir = FORWARD; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1248 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1249 | FreeWild(num_matches, matches); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * Make the completion list cyclic. |
| 1254 | * Return the number of matches (excluding the original). |
| 1255 | */ |
| 1256 | static int |
| 1257 | ins_compl_make_cyclic(void) |
| 1258 | { |
| 1259 | compl_T *match; |
| 1260 | int count = 0; |
| 1261 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1262 | if (compl_first_match == NULL) |
| 1263 | return 0; |
| 1264 | |
| 1265 | // Find the end of the list. |
| 1266 | match = compl_first_match; |
| 1267 | // 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] | 1268 | while (match->cp_next != NULL && !is_first_match(match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1269 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1270 | match = match->cp_next; |
| 1271 | ++count; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1272 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1273 | match->cp_next = compl_first_match; |
| 1274 | compl_first_match->cp_prev = match; |
| 1275 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1276 | return count; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Return whether there currently is a shown match. |
| 1281 | */ |
| 1282 | int |
| 1283 | ins_compl_has_shown_match(void) |
| 1284 | { |
| 1285 | return compl_shown_match == NULL |
| 1286 | || compl_shown_match != compl_shown_match->cp_next; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Return whether the shown match is long enough. |
| 1291 | */ |
| 1292 | int |
| 1293 | ins_compl_long_shown_match(void) |
| 1294 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1295 | return (int)compl_shown_match->cp_str.length |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1296 | > curwin->w_cursor.col - compl_col; |
| 1297 | } |
| 1298 | |
| 1299 | /* |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1300 | * Get the local or global value of 'completeopt' flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1301 | */ |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1302 | unsigned int |
| 1303 | get_cot_flags(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1304 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1305 | return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1308 | /* |
| 1309 | * Update the screen and when there is any scrolling remove the popup menu. |
| 1310 | */ |
| 1311 | static void |
| 1312 | ins_compl_upd_pum(void) |
| 1313 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1314 | if (compl_match_array == NULL) |
| 1315 | return; |
| 1316 | |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1317 | int h = curwin->w_cline_height; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1318 | // Update the screen later, before drawing the popup menu over it. |
| 1319 | pum_call_update_screen(); |
| 1320 | if (h != curwin->w_cline_height) |
| 1321 | ins_compl_del_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * Remove any popup menu. |
| 1326 | */ |
| 1327 | static void |
| 1328 | ins_compl_del_pum(void) |
| 1329 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1330 | if (compl_match_array == NULL) |
| 1331 | return; |
| 1332 | |
| 1333 | pum_undisplay(); |
| 1334 | VIM_CLEAR(compl_match_array); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Return TRUE if the popup menu should be displayed. |
| 1339 | */ |
| 1340 | int |
| 1341 | pum_wanted(void) |
| 1342 | { |
| 1343 | // 'completeopt' must contain "menu" or "menuone" |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1344 | if ((get_cot_flags() & COT_ANY_MENU) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1345 | return FALSE; |
| 1346 | |
| 1347 | // The display looks bad on a B&W display. |
| 1348 | if (t_colors < 8 |
| 1349 | #ifdef FEAT_GUI |
| 1350 | && !gui.in_use |
| 1351 | #endif |
| 1352 | ) |
| 1353 | return FALSE; |
| 1354 | return TRUE; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 1359 | * One if 'completopt' contains "menuone". |
| 1360 | */ |
| 1361 | static int |
| 1362 | pum_enough_matches(void) |
| 1363 | { |
| 1364 | compl_T *compl; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1365 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1366 | |
| 1367 | // Don't display the popup menu if there are no matches or there is only |
| 1368 | // one (ignoring the original text). |
| 1369 | compl = compl_first_match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1370 | do |
| 1371 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1372 | if (compl == NULL || (!match_at_original_text(compl) && ++i == 2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1373 | break; |
| 1374 | compl = compl->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1375 | } while (!is_first_match(compl)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1376 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1377 | if (get_cot_flags() & COT_MENUONE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1378 | return (i >= 1); |
| 1379 | return (i >= 2); |
| 1380 | } |
| 1381 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1382 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1383 | /* |
| 1384 | * Allocate Dict for the completed item. |
| 1385 | * { word, abbr, menu, kind, info } |
| 1386 | */ |
| 1387 | static dict_T * |
| 1388 | ins_compl_dict_alloc(compl_T *match) |
| 1389 | { |
| 1390 | dict_T *dict = dict_alloc_lock(VAR_FIXED); |
| 1391 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1392 | if (dict == NULL) |
| 1393 | return NULL; |
| 1394 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1395 | dict_add_string(dict, "word", match->cp_str.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1396 | dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]); |
| 1397 | dict_add_string(dict, "menu", match->cp_text[CPT_MENU]); |
| 1398 | dict_add_string(dict, "kind", match->cp_text[CPT_KIND]); |
| 1399 | dict_add_string(dict, "info", match->cp_text[CPT_INFO]); |
| 1400 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 1401 | dict_add_string(dict, "user_data", (char_u *)""); |
| 1402 | else |
| 1403 | dict_add_tv(dict, "user_data", &match->cp_user_data); |
| 1404 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1405 | return dict; |
| 1406 | } |
| 1407 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1408 | /* |
| 1409 | * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode |
| 1410 | * completion menu is changed. |
| 1411 | */ |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1412 | static void |
| 1413 | trigger_complete_changed_event(int cur) |
| 1414 | { |
| 1415 | dict_T *v_event; |
| 1416 | dict_T *item; |
| 1417 | static int recursive = FALSE; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1418 | save_v_event_T save_v_event; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1419 | |
| 1420 | if (recursive) |
| 1421 | return; |
| 1422 | |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 1423 | item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1424 | if (item == NULL) |
| 1425 | return; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1426 | v_event = get_v_event(&save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1427 | dict_add_dict(v_event, "completed_item", item); |
| 1428 | pum_set_event_info(v_event); |
| 1429 | dict_set_items_ro(v_event); |
| 1430 | |
| 1431 | recursive = TRUE; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1432 | textlock++; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1433 | apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1434 | textlock--; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1435 | recursive = FALSE; |
| 1436 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1437 | restore_v_event(v_event, &save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1438 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1439 | #endif |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1440 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1441 | /* |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1442 | * pumitem qsort compare func |
| 1443 | */ |
| 1444 | static int |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1445 | ins_compl_fuzzy_cmp(const void *a, const void *b) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1446 | { |
| 1447 | const int sa = (*(pumitem_T *)a).pum_score; |
| 1448 | const int sb = (*(pumitem_T *)b).pum_score; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1449 | const int ia = (*(pumitem_T *)a).pum_idx; |
| 1450 | const int ib = (*(pumitem_T *)b).pum_idx; |
| 1451 | 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] | 1452 | } |
| 1453 | |
| 1454 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1455 | * Build a popup menu to show the completion matches. |
| 1456 | * Returns the popup menu entry that should be selected. Returns -1 if nothing |
| 1457 | * should be selected. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1458 | */ |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1459 | static int |
| 1460 | ins_compl_build_pum(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1461 | { |
| 1462 | compl_T *compl; |
| 1463 | compl_T *shown_compl = NULL; |
| 1464 | int did_find_shown_match = FALSE; |
| 1465 | int shown_match_ok = FALSE; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1466 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1467 | int cur = -1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1468 | int max_fuzzy_score = 0; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 1469 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1470 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1471 | int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0; |
| 1472 | int fuzzy_sort = fuzzy_filter && !(cur_cot_flags & COT_NOSORT); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1473 | compl_T *match_head = NULL; |
| 1474 | compl_T *match_tail = NULL; |
| 1475 | compl_T *match_next = NULL; |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1476 | int update_shown_match = fuzzy_filter; |
Christian Brabandt | b53d4fb | 2025-04-16 21:12:30 +0200 | [diff] [blame] | 1477 | int match_count = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1478 | int cur_source = -1; |
| 1479 | int max_matches_found = FALSE; |
| 1480 | int is_forward = compl_shows_dir_forward() && !fuzzy_filter; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1481 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1482 | // Need to build the popup menu list. |
| 1483 | compl_match_arraysize = 0; |
| 1484 | compl = compl_first_match; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1485 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1486 | // If the current match is the original text don't find the first |
| 1487 | // match after it, don't highlight anything. |
| 1488 | if (match_at_original_text(compl_shown_match)) |
| 1489 | shown_match_ok = TRUE; |
| 1490 | |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1491 | if (fuzzy_filter && ctrl_x_mode_normal() && compl_leader.string == NULL |
| 1492 | && compl_shown_match->cp_score > 0) |
| 1493 | update_shown_match = FALSE; |
| 1494 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1495 | if (compl_leader.string != NULL |
| 1496 | && STRCMP(compl_leader.string, compl_orig_text.string) == 0 |
| 1497 | && shown_match_ok == FALSE) |
| 1498 | compl_shown_match = compl_no_select ? compl_first_match |
| 1499 | : compl_first_match->cp_next; |
| 1500 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1501 | do |
| 1502 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1503 | compl->cp_in_match_array = FALSE; |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 1504 | // When 'completeopt' contains "fuzzy" and leader is not NULL or empty, |
| 1505 | // set the cp_score for later comparisons. |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1506 | if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1507 | compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1508 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1509 | if (is_forward && compl->cp_cpt_source_idx != -1) |
| 1510 | { |
| 1511 | if (cur_source != compl->cp_cpt_source_idx) |
| 1512 | { |
| 1513 | cur_source = compl->cp_cpt_source_idx; |
| 1514 | match_count = 1; |
| 1515 | max_matches_found = FALSE; |
| 1516 | } |
Girish Palya | dc31405 | 2025-05-08 23:28:52 +0200 | [diff] [blame] | 1517 | else if (cpt_sources_array != NULL && !max_matches_found) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1518 | { |
| 1519 | int max_matches = cpt_sources_array[cur_source].max_matches; |
| 1520 | if (max_matches > 0 && match_count > max_matches) |
| 1521 | max_matches_found = TRUE; |
| 1522 | } |
| 1523 | } |
| 1524 | |
Girish Palya | dc31405 | 2025-05-08 23:28:52 +0200 | [diff] [blame] | 1525 | // Apply 'smartcase' behavior during normal mode |
| 1526 | if (ctrl_x_mode_normal() && !p_inf && compl_leader.string |
| 1527 | && !ignorecase(compl_leader.string) && !fuzzy_filter) |
| 1528 | compl->cp_flags &= ~CP_ICASE; |
| 1529 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1530 | if (!match_at_original_text(compl) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1531 | && !max_matches_found |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1532 | && (compl_leader.string == NULL |
Girish Palya | dc31405 | 2025-05-08 23:28:52 +0200 | [diff] [blame] | 1533 | || ins_compl_equal(compl, compl_leader.string, |
| 1534 | (int)compl_leader.length) |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1535 | || (fuzzy_filter && compl->cp_score > 0))) |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1536 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1537 | ++compl_match_arraysize; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 1538 | compl->cp_in_match_array = TRUE; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1539 | if (match_head == NULL) |
| 1540 | match_head = compl; |
| 1541 | else |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1542 | match_tail->cp_match_next = compl; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1543 | match_tail = compl; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1544 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1545 | if (!shown_match_ok && !fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1546 | { |
| 1547 | if (compl == compl_shown_match || did_find_shown_match) |
| 1548 | { |
| 1549 | // This item is the shown match or this is the |
| 1550 | // first displayed item after the shown match. |
| 1551 | compl_shown_match = compl; |
| 1552 | did_find_shown_match = TRUE; |
| 1553 | shown_match_ok = TRUE; |
| 1554 | } |
| 1555 | else |
| 1556 | // Remember this displayed match for when the |
| 1557 | // shown match is just below it. |
| 1558 | shown_compl = compl; |
| 1559 | cur = i; |
| 1560 | } |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1561 | else if (fuzzy_filter) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1562 | { |
| 1563 | if (i == 0) |
| 1564 | shown_compl = compl; |
| 1565 | // Update the maximum fuzzy score and the shown match |
| 1566 | // if the current item's score is higher |
glepnir | e4e4d1c | 2025-04-02 20:18:25 +0200 | [diff] [blame] | 1567 | if (fuzzy_sort && compl->cp_score > max_fuzzy_score |
| 1568 | && update_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1569 | { |
| 1570 | did_find_shown_match = TRUE; |
| 1571 | max_fuzzy_score = compl->cp_score; |
| 1572 | if (!compl_no_select) |
| 1573 | compl_shown_match = compl; |
| 1574 | } |
| 1575 | |
glepnir | 3af0a8d | 2025-02-20 22:06:16 +0100 | [diff] [blame] | 1576 | if (!shown_match_ok && compl == compl_shown_match) |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1577 | { |
| 1578 | cur = i; |
| 1579 | shown_match_ok = TRUE; |
| 1580 | } |
| 1581 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 1582 | if (is_forward && compl->cp_cpt_source_idx != -1) |
| 1583 | match_count++; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1584 | i++; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1585 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1586 | |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 1587 | if (compl == compl_shown_match && !fuzzy_filter) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1588 | { |
| 1589 | did_find_shown_match = TRUE; |
| 1590 | |
| 1591 | // When the original text is the shown match don't set |
| 1592 | // compl_shown_match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1593 | if (match_at_original_text(compl)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1594 | shown_match_ok = TRUE; |
| 1595 | |
| 1596 | if (!shown_match_ok && shown_compl != NULL) |
| 1597 | { |
| 1598 | // The shown match isn't displayed, set it to the |
| 1599 | // previously displayed match. |
| 1600 | compl_shown_match = shown_compl; |
| 1601 | shown_match_ok = TRUE; |
| 1602 | } |
| 1603 | } |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1604 | compl = compl->cp_next; |
| 1605 | } while (compl != NULL && !is_first_match(compl)); |
| 1606 | |
| 1607 | if (compl_match_arraysize == 0) |
| 1608 | return -1; |
| 1609 | |
glepnir | c0b7ca4 | 2025-02-13 20:27:44 +0100 | [diff] [blame] | 1610 | if (fuzzy_filter && !fuzzy_sort && !compl_no_select && !shown_match_ok) |
| 1611 | { |
| 1612 | compl_shown_match = shown_compl; |
| 1613 | shown_match_ok = TRUE; |
| 1614 | cur = 0; |
| 1615 | } |
| 1616 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1617 | compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize); |
| 1618 | if (compl_match_array == NULL) |
| 1619 | return -1; |
| 1620 | |
| 1621 | compl = match_head; |
| 1622 | i = 0; |
| 1623 | while (compl != NULL) |
| 1624 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1625 | compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL |
| 1626 | ? compl->cp_text[CPT_ABBR] : compl->cp_str.string; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1627 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 1628 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 1629 | compl_match_array[i].pum_score = compl->cp_score; |
| 1630 | compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr; |
| 1631 | compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1632 | compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL |
| 1633 | ? compl->cp_text[CPT_MENU] : compl->cp_fname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1634 | match_next = compl->cp_match_next; |
| 1635 | compl->cp_match_next = NULL; |
| 1636 | compl = match_next; |
| 1637 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1638 | |
zeertzjq | d65aa1b | 2025-01-25 15:29:03 +0100 | [diff] [blame] | 1639 | if (fuzzy_sort && compl_leader.string != NULL && compl_leader.length > 0) |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1640 | { |
| 1641 | for (i = 0; i < compl_match_arraysize; i++) |
| 1642 | compl_match_array[i].pum_idx = i; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1643 | // sort by the largest score of fuzzy match |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1644 | qsort(compl_match_array, (size_t)compl_match_arraysize, |
| 1645 | sizeof(pumitem_T), ins_compl_fuzzy_cmp); |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 1646 | shown_match_ok = TRUE; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1647 | } |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1648 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1649 | if (!shown_match_ok) // no displayed match at all |
| 1650 | cur = -1; |
| 1651 | |
| 1652 | return cur; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * Show the popup menu for the list of matches. |
| 1657 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
| 1658 | */ |
| 1659 | void |
| 1660 | ins_compl_show_pum(void) |
| 1661 | { |
| 1662 | int i; |
| 1663 | int cur = -1; |
| 1664 | colnr_T col; |
| 1665 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1666 | if (!pum_wanted() || !pum_enough_matches()) |
| 1667 | return; |
| 1668 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1669 | // Update the screen later, before drawing the popup menu over it. |
| 1670 | pum_call_update_screen(); |
| 1671 | |
| 1672 | if (compl_match_array == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1673 | // Need to build the popup menu list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1674 | cur = ins_compl_build_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1675 | else |
| 1676 | { |
| 1677 | // popup menu already exists, only need to find the current item. |
| 1678 | for (i = 0; i < compl_match_arraysize; ++i) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1679 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1680 | 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] | 1681 | || compl_match_array[i].pum_text |
| 1682 | == compl_shown_match->cp_text[CPT_ABBR]) |
| 1683 | { |
| 1684 | cur = i; |
| 1685 | break; |
| 1686 | } |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1687 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1688 | } |
| 1689 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1690 | if (compl_match_array == NULL) |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1691 | { |
| 1692 | #ifdef FEAT_EVAL |
| 1693 | if (compl_started && has_completechanged()) |
| 1694 | trigger_complete_changed_event(cur); |
| 1695 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1696 | return; |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1697 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1698 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1699 | // In Replace mode when a $ is displayed at the end of the line only |
| 1700 | // part of the screen would be updated. We do need to redraw here. |
| 1701 | dollar_vcol = -1; |
| 1702 | |
| 1703 | // Compute the screen column of the start of the completed text. |
| 1704 | // Use the cursor to get all wrapping and other settings right. |
| 1705 | col = curwin->w_cursor.col; |
| 1706 | curwin->w_cursor.col = compl_col; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1707 | compl_selected_item = cur; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1708 | pum_display(compl_match_array, compl_match_arraysize, cur); |
| 1709 | curwin->w_cursor.col = col; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1710 | |
glepnir | cbb46b4 | 2024-02-03 18:11:13 +0100 | [diff] [blame] | 1711 | // After adding leader, set the current match to shown match. |
| 1712 | if (compl_started && compl_curr_match != compl_shown_match) |
| 1713 | compl_curr_match = compl_shown_match; |
| 1714 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1715 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1716 | if (has_completechanged()) |
| 1717 | trigger_complete_changed_event(cur); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1718 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | #define DICT_FIRST (1) // use just first element in "dict" |
| 1722 | #define DICT_EXACT (2) // "dict" is the exact name of a file |
| 1723 | |
| 1724 | /* |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1725 | * Get current completion leader |
| 1726 | */ |
| 1727 | char_u * |
| 1728 | ins_compl_leader(void) |
| 1729 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1730 | return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Get current completion leader length |
| 1735 | */ |
| 1736 | size_t |
| 1737 | ins_compl_leader_len(void) |
| 1738 | { |
| 1739 | return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1743 | * Add any identifiers that match the given pattern "pat" in the list of |
| 1744 | * dictionary files "dict_start" to the list of completions. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1745 | */ |
| 1746 | static void |
| 1747 | ins_compl_dictionaries( |
| 1748 | char_u *dict_start, |
| 1749 | char_u *pat, |
| 1750 | int flags, // DICT_FIRST and/or DICT_EXACT |
| 1751 | int thesaurus) // Thesaurus completion |
| 1752 | { |
| 1753 | char_u *dict = dict_start; |
| 1754 | char_u *ptr; |
| 1755 | char_u *buf; |
| 1756 | regmatch_T regmatch; |
| 1757 | char_u **files; |
| 1758 | int count; |
| 1759 | int save_p_scs; |
| 1760 | int dir = compl_direction; |
| 1761 | |
| 1762 | if (*dict == NUL) |
| 1763 | { |
| 1764 | #ifdef FEAT_SPELL |
| 1765 | // When 'dictionary' is empty and spell checking is enabled use |
| 1766 | // "spell". |
| 1767 | if (!thesaurus && curwin->w_p_spell) |
| 1768 | dict = (char_u *)"spell"; |
| 1769 | else |
| 1770 | #endif |
| 1771 | return; |
| 1772 | } |
| 1773 | |
| 1774 | buf = alloc(LSIZE); |
| 1775 | if (buf == NULL) |
| 1776 | return; |
| 1777 | regmatch.regprog = NULL; // so that we can goto theend |
| 1778 | |
| 1779 | // If 'infercase' is set, don't use 'smartcase' here |
| 1780 | save_p_scs = p_scs; |
| 1781 | if (curbuf->b_p_inf) |
| 1782 | p_scs = FALSE; |
| 1783 | |
| 1784 | // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 1785 | // to only match at the start of a line. Otherwise just match the |
| 1786 | // pattern. Also need to double backslashes. |
| 1787 | if (ctrl_x_mode_line_or_eval()) |
| 1788 | { |
| 1789 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1790 | |
| 1791 | if (pat_esc == NULL) |
| 1792 | goto theend; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1793 | size_t len = STRLEN(pat_esc) + 10; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1794 | ptr = alloc(len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1795 | if (ptr == NULL) |
| 1796 | { |
| 1797 | vim_free(pat_esc); |
| 1798 | goto theend; |
| 1799 | } |
| 1800 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
| 1801 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 1802 | vim_free(pat_esc); |
| 1803 | vim_free(ptr); |
| 1804 | } |
| 1805 | else |
| 1806 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1807 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1808 | if (regmatch.regprog == NULL) |
| 1809 | goto theend; |
| 1810 | } |
| 1811 | |
| 1812 | // ignore case depends on 'ignorecase', 'smartcase' and "pat" |
| 1813 | regmatch.rm_ic = ignorecase(pat); |
| 1814 | while (*dict != NUL && !got_int && !compl_interrupted) |
| 1815 | { |
| 1816 | // copy one dictionary file name into buf |
| 1817 | if (flags == DICT_EXACT) |
| 1818 | { |
| 1819 | count = 1; |
| 1820 | files = &dict; |
| 1821 | } |
| 1822 | else |
| 1823 | { |
| 1824 | // Expand wildcards in the dictionary name, but do not allow |
| 1825 | // backticks (for security, the 'dict' option may have been set in |
| 1826 | // a modeline). |
| 1827 | copy_option_part(&dict, buf, LSIZE, ","); |
| 1828 | # ifdef FEAT_SPELL |
| 1829 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 1830 | count = -1; |
| 1831 | else |
| 1832 | # endif |
| 1833 | if (vim_strchr(buf, '`') != NULL |
| 1834 | || expand_wildcards(1, &buf, &count, &files, |
| 1835 | EW_FILE|EW_SILENT) != OK) |
| 1836 | count = 0; |
| 1837 | } |
| 1838 | |
| 1839 | # ifdef FEAT_SPELL |
| 1840 | if (count == -1) |
| 1841 | { |
| 1842 | // Complete from active spelling. Skip "\<" in the pattern, we |
| 1843 | // don't use it as a RE. |
| 1844 | if (pat[0] == '\\' && pat[1] == '<') |
| 1845 | ptr = pat + 2; |
| 1846 | else |
| 1847 | ptr = pat; |
| 1848 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
| 1849 | } |
| 1850 | else |
| 1851 | # endif |
| 1852 | if (count > 0) // avoid warning for using "files" uninit |
| 1853 | { |
| 1854 | ins_compl_files(count, files, thesaurus, flags, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1855 | (cfc_has_mode() ? NULL : ®match), buf, &dir); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1856 | if (flags != DICT_EXACT) |
| 1857 | FreeWild(count, files); |
| 1858 | } |
| 1859 | if (flags != 0) |
| 1860 | break; |
| 1861 | } |
| 1862 | |
| 1863 | theend: |
| 1864 | p_scs = save_p_scs; |
| 1865 | vim_regfree(regmatch.regprog); |
| 1866 | vim_free(buf); |
| 1867 | } |
| 1868 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1869 | /* |
| 1870 | * Add all the words in the line "*buf_arg" from the thesaurus file "fname" |
| 1871 | * skipping the word at 'skip_word'. Returns OK on success. |
| 1872 | */ |
| 1873 | static int |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 1874 | thesaurus_add_words_in_line( |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 1875 | char_u *fname, |
| 1876 | char_u **buf_arg, |
| 1877 | int dir, |
| 1878 | char_u *skip_word) |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1879 | { |
| 1880 | int status = OK; |
| 1881 | char_u *ptr; |
| 1882 | char_u *wstart; |
| 1883 | |
| 1884 | // Add the other matches on the line |
| 1885 | ptr = *buf_arg; |
| 1886 | while (!got_int) |
| 1887 | { |
| 1888 | // Find start of the next word. Skip white |
| 1889 | // space and punctuation. |
| 1890 | ptr = find_word_start(ptr); |
| 1891 | if (*ptr == NUL || *ptr == NL) |
| 1892 | break; |
| 1893 | wstart = ptr; |
| 1894 | |
| 1895 | // Find end of the word. |
| 1896 | if (has_mbyte) |
| 1897 | // Japanese words may have characters in |
| 1898 | // different classes, only separate words |
| 1899 | // with single-byte non-word characters. |
| 1900 | while (*ptr != NUL) |
| 1901 | { |
| 1902 | int l = (*mb_ptr2len)(ptr); |
| 1903 | |
| 1904 | if (l < 2 && !vim_iswordc(*ptr)) |
| 1905 | break; |
| 1906 | ptr += l; |
| 1907 | } |
| 1908 | else |
| 1909 | ptr = find_word_end(ptr); |
| 1910 | |
| 1911 | // Add the word. Skip the regexp match. |
| 1912 | if (wstart != skip_word) |
| 1913 | { |
| 1914 | status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1915 | fname, dir, FALSE, 0); |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1916 | if (status == FAIL) |
| 1917 | break; |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | *buf_arg = ptr; |
| 1922 | return status; |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * Process "count" dictionary/thesaurus "files" and add the text matching |
| 1927 | * "regmatch". |
| 1928 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1929 | static void |
| 1930 | ins_compl_files( |
| 1931 | int count, |
| 1932 | char_u **files, |
| 1933 | int thesaurus, |
| 1934 | int flags, |
| 1935 | regmatch_T *regmatch, |
| 1936 | char_u *buf, |
| 1937 | int *dir) |
| 1938 | { |
| 1939 | char_u *ptr; |
| 1940 | int i; |
| 1941 | FILE *fp; |
| 1942 | int add_r; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1943 | char_u *leader = NULL; |
| 1944 | int leader_len = 0; |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 1945 | int in_fuzzy_collect = cfc_has_mode(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1946 | int score = 0; |
| 1947 | int len = 0; |
| 1948 | char_u *line_end = NULL; |
| 1949 | |
| 1950 | if (in_fuzzy_collect) |
| 1951 | { |
| 1952 | leader = ins_compl_leader(); |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 1953 | leader_len = (int)ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1954 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1955 | |
| 1956 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 1957 | { |
| 1958 | 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] | 1959 | if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1960 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1961 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1962 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1963 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 1964 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 1965 | } |
| 1966 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1967 | if (fp == NULL) |
| 1968 | continue; |
| 1969 | |
| 1970 | // Read dictionary file line by line. |
| 1971 | // Check each line for a match. |
| 1972 | while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1973 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1974 | ptr = buf; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1975 | if (regmatch != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1976 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1977 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1978 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 1979 | ptr = regmatch->startp[0]; |
| 1980 | ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr) |
| 1981 | : find_word_end(ptr); |
| 1982 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 1983 | (int)(ptr - regmatch->startp[0]), |
| 1984 | p_ic, files[i], *dir, FALSE, 0); |
| 1985 | if (thesaurus) |
| 1986 | { |
| 1987 | // For a thesaurus, add all the words in the line |
| 1988 | ptr = buf; |
| 1989 | add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir, |
| 1990 | regmatch->startp[0]); |
| 1991 | } |
| 1992 | if (add_r == OK) |
| 1993 | // if dir was BACKWARD then honor it just once |
| 1994 | *dir = FORWARD; |
| 1995 | else if (add_r == FAIL) |
| 1996 | break; |
| 1997 | // avoid expensive call to vim_regexec() when at end |
| 1998 | // of line |
| 1999 | if (*ptr == '\n' || got_int) |
| 2000 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2001 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2002 | } |
| 2003 | else if (in_fuzzy_collect && leader_len > 0) |
| 2004 | { |
| 2005 | line_end = find_line_end(ptr); |
| 2006 | while (ptr < line_end) |
| 2007 | { |
| 2008 | if (fuzzy_match_str_in_line(&ptr, leader, &len, NULL, &score)) |
| 2009 | { |
| 2010 | char_u *end_ptr = ctrl_x_mode_line_or_eval() |
| 2011 | ? find_line_end(ptr) : find_word_end(ptr); |
| 2012 | add_r = ins_compl_add_infercase(ptr, (int)(end_ptr - ptr), |
| 2013 | p_ic, files[i], *dir, FALSE, score); |
| 2014 | if (add_r == FAIL) |
| 2015 | break; |
| 2016 | ptr = end_ptr; // start from next word |
| 2017 | if (compl_get_longest && ctrl_x_mode_normal() |
| 2018 | && compl_first_match->cp_next |
| 2019 | && score == compl_first_match->cp_next->cp_score) |
| 2020 | compl_num_bests++; |
| 2021 | } |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2022 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2023 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2024 | line_breakcheck(); |
| 2025 | ins_compl_check_keys(50, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2026 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2027 | fclose(fp); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * Find the start of the next word. |
| 2033 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2034 | */ |
| 2035 | char_u * |
| 2036 | find_word_start(char_u *ptr) |
| 2037 | { |
| 2038 | if (has_mbyte) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2039 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2040 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
| 2041 | ptr += (*mb_ptr2len)(ptr); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2042 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2043 | else |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2044 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2045 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2046 | ++ptr; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2047 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2048 | return ptr; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * Find the end of the word. Assumes it starts inside a word. |
| 2053 | * Returns a pointer to just after the word. |
| 2054 | */ |
| 2055 | char_u * |
| 2056 | find_word_end(char_u *ptr) |
| 2057 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2058 | if (has_mbyte) |
| 2059 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2060 | int start_class = mb_get_class(ptr); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2061 | if (start_class > 1) |
| 2062 | while (*ptr != NUL) |
| 2063 | { |
| 2064 | ptr += (*mb_ptr2len)(ptr); |
| 2065 | if (mb_get_class(ptr) != start_class) |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
| 2069 | else |
| 2070 | while (vim_iswordc(*ptr)) |
| 2071 | ++ptr; |
| 2072 | return ptr; |
| 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * Find the end of the line, omitting CR and NL at the end. |
| 2077 | * Returns a pointer to just after the line. |
| 2078 | */ |
glepnir | dd42b05 | 2025-03-08 16:52:55 +0100 | [diff] [blame] | 2079 | char_u * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2080 | find_line_end(char_u *ptr) |
| 2081 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2082 | char_u *s = ptr + STRLEN(ptr); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2083 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 2084 | --s; |
| 2085 | return s; |
| 2086 | } |
| 2087 | |
| 2088 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2089 | * Free a completion item in the list |
| 2090 | */ |
| 2091 | static void |
| 2092 | ins_compl_item_free(compl_T *match) |
| 2093 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2094 | VIM_CLEAR_STRING(match->cp_str); |
| 2095 | // several entries may use the same fname, free it just once. |
| 2096 | if (match->cp_flags & CP_FREE_FNAME) |
| 2097 | vim_free(match->cp_fname); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2098 | for (int i = 0; i < CPT_COUNT; ++i) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2099 | vim_free(match->cp_text[i]); |
| 2100 | #ifdef FEAT_EVAL |
| 2101 | clear_tv(&match->cp_user_data); |
| 2102 | #endif |
| 2103 | vim_free(match); |
| 2104 | } |
| 2105 | |
| 2106 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2107 | * Free the list of completions |
| 2108 | */ |
| 2109 | static void |
| 2110 | ins_compl_free(void) |
| 2111 | { |
| 2112 | compl_T *match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2113 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2114 | VIM_CLEAR_STRING(compl_pattern); |
| 2115 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2116 | |
| 2117 | if (compl_first_match == NULL) |
| 2118 | return; |
| 2119 | |
| 2120 | ins_compl_del_pum(); |
| 2121 | pum_clear(); |
| 2122 | |
| 2123 | compl_curr_match = compl_first_match; |
| 2124 | do |
| 2125 | { |
| 2126 | match = compl_curr_match; |
| 2127 | compl_curr_match = compl_curr_match->cp_next; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2128 | ins_compl_item_free(match); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2129 | } while (compl_curr_match != NULL && !is_first_match(compl_curr_match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2130 | compl_first_match = compl_curr_match = NULL; |
| 2131 | compl_shown_match = NULL; |
| 2132 | compl_old_match = NULL; |
| 2133 | } |
| 2134 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2135 | /* |
| 2136 | * Reset/clear the completion state. |
| 2137 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2138 | void |
| 2139 | ins_compl_clear(void) |
| 2140 | { |
| 2141 | compl_cont_status = 0; |
| 2142 | compl_started = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2143 | compl_cfc_longest_ins = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2144 | compl_matches = 0; |
glepnir | 07f0dbe | 2025-02-18 20:27:30 +0100 | [diff] [blame] | 2145 | compl_selected_item = -1; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2146 | compl_ins_end_col = 0; |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2147 | compl_curr_win = NULL; |
| 2148 | compl_curr_buf = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2149 | VIM_CLEAR_STRING(compl_pattern); |
| 2150 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2151 | edit_submode_extra = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2152 | VIM_CLEAR_STRING(compl_orig_text); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2153 | compl_enter_selects = FALSE; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 2154 | cpt_sources_clear(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2155 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2156 | // clear v:completed_item |
| 2157 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2158 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | * Return TRUE when Insert completion is active. |
| 2163 | */ |
| 2164 | int |
| 2165 | ins_compl_active(void) |
| 2166 | { |
| 2167 | return compl_started; |
| 2168 | } |
| 2169 | |
| 2170 | /* |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2171 | * Return True when wp is the actual completion window |
| 2172 | */ |
| 2173 | int |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2174 | ins_compl_win_active(win_T *wp) |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2175 | { |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2176 | return ins_compl_active() && wp == compl_curr_win |
| 2177 | && wp->w_buffer == compl_curr_buf; |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2181 | * Selected one of the matches. When FALSE, the match was either edited or |
| 2182 | * using the longest common string. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2183 | */ |
| 2184 | int |
| 2185 | ins_compl_used_match(void) |
| 2186 | { |
| 2187 | return compl_used_match; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * Initialize get longest common string. |
| 2192 | */ |
| 2193 | void |
| 2194 | ins_compl_init_get_longest(void) |
| 2195 | { |
| 2196 | compl_get_longest = FALSE; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Returns TRUE when insert completion is interrupted. |
| 2201 | */ |
| 2202 | int |
| 2203 | ins_compl_interrupted(void) |
| 2204 | { |
| 2205 | return compl_interrupted; |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | * Returns TRUE if the <Enter> key selects a match in the completion popup |
| 2210 | * menu. |
| 2211 | */ |
| 2212 | int |
| 2213 | ins_compl_enter_selects(void) |
| 2214 | { |
| 2215 | return compl_enter_selects; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Return the column where the text starts that is being completed |
| 2220 | */ |
| 2221 | colnr_T |
| 2222 | ins_compl_col(void) |
| 2223 | { |
| 2224 | return compl_col; |
| 2225 | } |
| 2226 | |
| 2227 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2228 | * Return the length in bytes of the text being completed |
| 2229 | */ |
| 2230 | int |
| 2231 | ins_compl_len(void) |
| 2232 | { |
| 2233 | return compl_length; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2237 | * Return TRUE when the 'completeopt' "preinsert" flag is in effect, |
| 2238 | * otherwise return FALSE. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2239 | */ |
| 2240 | static int |
| 2241 | ins_compl_has_preinsert(void) |
| 2242 | { |
glepnir | a2c5559 | 2025-02-28 17:43:42 +0100 | [diff] [blame] | 2243 | int cur_cot_flags = get_cot_flags(); |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 2244 | return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE)) |
| 2245 | == (COT_PREINSERT | COT_MENUONE); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Returns TRUE if the pre-insert effect is valid and the cursor is within |
| 2250 | * the `compl_ins_end_col` range. |
| 2251 | */ |
| 2252 | int |
| 2253 | ins_compl_preinsert_effect(void) |
| 2254 | { |
| 2255 | if (!ins_compl_has_preinsert()) |
| 2256 | return FALSE; |
| 2257 | |
| 2258 | return curwin->w_cursor.col < compl_ins_end_col; |
| 2259 | } |
| 2260 | |
| 2261 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2262 | * Delete one character before the cursor and show the subset of the matches |
| 2263 | * that match the word that is now before the cursor. |
| 2264 | * Returns the character to be used, NUL if the work is done and another char |
| 2265 | * to be got from the user. |
| 2266 | */ |
| 2267 | int |
| 2268 | ins_compl_bs(void) |
| 2269 | { |
| 2270 | char_u *line; |
| 2271 | char_u *p; |
| 2272 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2273 | if (ins_compl_preinsert_effect()) |
| 2274 | ins_compl_delete(); |
| 2275 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2276 | line = ml_get_curline(); |
| 2277 | p = line + curwin->w_cursor.col; |
| 2278 | MB_PTR_BACK(line, p); |
| 2279 | |
| 2280 | // Stop completion when the whole word was deleted. For Omni completion |
| 2281 | // allow the word to be deleted, we won't match everything. |
| 2282 | // Respect the 'backspace' option. |
| 2283 | if ((int)(p - line) - (int)compl_col < 0 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2284 | || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni()) |
| 2285 | || ctrl_x_mode_eval() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2286 | || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col |
| 2287 | - compl_length < 0)) |
| 2288 | return K_BS; |
| 2289 | |
| 2290 | // Deleted more than what was used to find matches or didn't finish |
| 2291 | // finding all matches: need to look for matches all over again. |
| 2292 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 2293 | || ins_compl_need_restart()) |
| 2294 | ins_compl_restart(); |
| 2295 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2296 | VIM_CLEAR_STRING(compl_leader); |
| 2297 | compl_leader.length = (size_t)((p - line) - compl_col); |
| 2298 | compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length); |
| 2299 | if (compl_leader.string == NULL) |
| 2300 | { |
| 2301 | compl_leader.length = 0; |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2302 | return K_BS; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2303 | } |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 2304 | |
| 2305 | ins_compl_new_leader(); |
| 2306 | if (compl_shown_match != NULL) |
| 2307 | // Make sure current match is not a hidden item. |
| 2308 | compl_curr_match = compl_shown_match; |
| 2309 | return NUL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 2314 | * be called. |
| 2315 | */ |
| 2316 | static int |
| 2317 | ins_compl_need_restart(void) |
| 2318 | { |
| 2319 | // Return TRUE if we didn't complete finding matches or when the |
| 2320 | // 'completefunc' returned "always" in the "refresh" dictionary item. |
| 2321 | return compl_was_interrupted |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2322 | || ((ctrl_x_mode_function() || ctrl_x_mode_omni()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2323 | && compl_opt_refresh_always); |
| 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * Called after changing "compl_leader". |
| 2328 | * Show the popup menu with a different set of matches. |
| 2329 | * May also search for matches again if the previous search was interrupted. |
| 2330 | */ |
| 2331 | static void |
| 2332 | ins_compl_new_leader(void) |
| 2333 | { |
| 2334 | ins_compl_del_pum(); |
| 2335 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2336 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2337 | compl_used_match = FALSE; |
| 2338 | |
| 2339 | if (compl_started) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2340 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2341 | ins_compl_set_original_text(compl_leader.string, compl_leader.length); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 2342 | if (is_cpt_func_refresh_always()) |
| 2343 | cpt_compl_refresh(); |
| 2344 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2345 | else |
| 2346 | { |
| 2347 | #ifdef FEAT_SPELL |
| 2348 | spell_bad_len = 0; // need to redetect bad word |
| 2349 | #endif |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2350 | // Matches were cleared, need to search for them now. Before drawing |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2351 | // the popup menu display the changed text before the cursor. Set |
| 2352 | // "compl_restarting" to avoid that the first match is inserted. |
| 2353 | pum_call_update_screen(); |
| 2354 | #ifdef FEAT_GUI |
| 2355 | if (gui.in_use) |
| 2356 | { |
| 2357 | // Show the cursor after the match, not after the redrawn text. |
| 2358 | setcursor(); |
| 2359 | out_flush_cursor(FALSE, FALSE); |
| 2360 | } |
| 2361 | #endif |
| 2362 | compl_restarting = TRUE; |
| 2363 | if (ins_complete(Ctrl_N, TRUE) == FAIL) |
| 2364 | compl_cont_status = 0; |
| 2365 | compl_restarting = FALSE; |
| 2366 | } |
| 2367 | |
glepnir | 4418041 | 2025-02-20 22:09:48 +0100 | [diff] [blame] | 2368 | compl_enter_selects = !compl_used_match && compl_selected_item != -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2369 | |
| 2370 | // Show the popup menu with a different set of matches. |
| 2371 | ins_compl_show_pum(); |
| 2372 | |
| 2373 | // Don't let Enter select the original text when there is no popup menu. |
| 2374 | if (compl_match_array == NULL) |
| 2375 | compl_enter_selects = FALSE; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2376 | else if (ins_compl_has_preinsert() && compl_leader.length > 0) |
| 2377 | ins_compl_insert(FALSE, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* |
| 2381 | * Return the length of the completion, from the completion start column to |
| 2382 | * the cursor column. Making sure it never goes below zero. |
| 2383 | */ |
| 2384 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2385 | get_compl_len(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2386 | { |
| 2387 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2388 | return MAX(0, off); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Append one character to the match leader. May reduce the number of |
| 2393 | * matches. |
| 2394 | */ |
| 2395 | void |
| 2396 | ins_compl_addleader(int c) |
| 2397 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2398 | int cc; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2399 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 2400 | if (ins_compl_preinsert_effect()) |
| 2401 | ins_compl_delete(); |
| 2402 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2403 | if (stop_arrow() == FAIL) |
| 2404 | return; |
| 2405 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2406 | { |
| 2407 | char_u buf[MB_MAXBYTES + 1]; |
| 2408 | |
| 2409 | (*mb_char2bytes)(c, buf); |
| 2410 | buf[cc] = NUL; |
| 2411 | ins_char_bytes(buf, cc); |
| 2412 | if (compl_opt_refresh_always) |
| 2413 | AppendToRedobuff(buf); |
| 2414 | } |
| 2415 | else |
| 2416 | { |
| 2417 | ins_char(c); |
| 2418 | if (compl_opt_refresh_always) |
| 2419 | AppendCharToRedobuff(c); |
| 2420 | } |
| 2421 | |
| 2422 | // If we didn't complete finding matches we must search again. |
| 2423 | if (ins_compl_need_restart()) |
| 2424 | ins_compl_restart(); |
| 2425 | |
| 2426 | // When 'always' is set, don't reset compl_leader. While completing, |
| 2427 | // cursor doesn't point original position, changing compl_leader would |
| 2428 | // break redo. |
| 2429 | if (!compl_opt_refresh_always) |
| 2430 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2431 | VIM_CLEAR_STRING(compl_leader); |
| 2432 | compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col); |
| 2433 | compl_leader.string = vim_strnsave(ml_get_curline() + compl_col, |
| 2434 | compl_leader.length); |
| 2435 | if (compl_leader.string == NULL) |
| 2436 | { |
| 2437 | compl_leader.length = 0; |
| 2438 | return; |
| 2439 | } |
| 2440 | |
| 2441 | ins_compl_new_leader(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | /* |
| 2446 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 2447 | * BS or a key was typed while still searching for matches. |
| 2448 | */ |
| 2449 | static void |
| 2450 | ins_compl_restart(void) |
| 2451 | { |
| 2452 | ins_compl_free(); |
| 2453 | compl_started = FALSE; |
| 2454 | compl_matches = 0; |
| 2455 | compl_cont_status = 0; |
| 2456 | compl_cont_mode = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 2457 | cpt_sources_clear(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * Set the first match, the original text. |
| 2462 | */ |
| 2463 | static void |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2464 | ins_compl_set_original_text(char_u *str, size_t len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2465 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2466 | // Replace the original text entry. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2467 | // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly |
| 2468 | // be at the last item for backward completion |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2469 | if (match_at_original_text(compl_first_match)) // safety check |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2470 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2471 | char_u *p = vim_strnsave(str, len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2472 | if (p != NULL) |
| 2473 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2474 | VIM_CLEAR_STRING(compl_first_match->cp_str); |
| 2475 | compl_first_match->cp_str.string = p; |
| 2476 | compl_first_match->cp_str.length = len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | else if (compl_first_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2480 | && match_at_original_text(compl_first_match->cp_prev)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2481 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2482 | char_u *p = vim_strnsave(str, len); |
| 2483 | if (p != NULL) |
| 2484 | { |
| 2485 | VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str); |
| 2486 | compl_first_match->cp_prev->cp_str.string = p; |
| 2487 | compl_first_match->cp_prev->cp_str.length = len; |
| 2488 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | /* |
| 2493 | * Append one character to the match leader. May reduce the number of |
| 2494 | * matches. |
| 2495 | */ |
| 2496 | void |
| 2497 | ins_compl_addfrommatch(void) |
| 2498 | { |
| 2499 | char_u *p; |
| 2500 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
| 2501 | int c; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2502 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2503 | p = compl_shown_match->cp_str.string; |
| 2504 | 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] | 2505 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2506 | size_t plen; |
| 2507 | compl_T *cp; |
| 2508 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2509 | // When still at the original match use the first entry that matches |
| 2510 | // the leader. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2511 | if (!match_at_original_text(compl_shown_match)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2512 | return; |
| 2513 | |
| 2514 | p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2515 | plen = 0; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2516 | for (cp = compl_shown_match->cp_next; cp != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2517 | && !is_first_match(cp); cp = cp->cp_next) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2518 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2519 | if (compl_leader.string == NULL |
| 2520 | || ins_compl_equal(cp, compl_leader.string, |
| 2521 | (int)compl_leader.length)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2522 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2523 | p = cp->cp_str.string; |
| 2524 | plen = cp->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2525 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2526 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2527 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2528 | if (p == NULL || (int)plen <= len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2529 | return; |
| 2530 | } |
| 2531 | p += len; |
| 2532 | c = PTR2CHAR(p); |
| 2533 | ins_compl_addleader(c); |
| 2534 | } |
| 2535 | |
| 2536 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2537 | * 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] | 2538 | * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre, |
| 2539 | * compl_cont_mode and compl_cont_status. |
| 2540 | * Returns TRUE when the character is not to be inserted. |
| 2541 | */ |
| 2542 | static int |
| 2543 | set_ctrl_x_mode(int c) |
| 2544 | { |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 2545 | int retval = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2546 | |
| 2547 | switch (c) |
| 2548 | { |
| 2549 | case Ctrl_E: |
| 2550 | case Ctrl_Y: |
| 2551 | // scroll the window one line up or down |
| 2552 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2553 | if (!(State & REPLACE_FLAG)) |
| 2554 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2555 | else |
| 2556 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2557 | edit_submode_pre = NULL; |
| 2558 | showmode(); |
| 2559 | break; |
| 2560 | case Ctrl_L: |
| 2561 | // complete whole line |
| 2562 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2563 | break; |
| 2564 | case Ctrl_F: |
| 2565 | // complete filenames |
| 2566 | ctrl_x_mode = CTRL_X_FILES; |
| 2567 | break; |
| 2568 | case Ctrl_K: |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 2569 | // complete words from a dictionary |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2570 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2571 | break; |
| 2572 | case Ctrl_R: |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 2573 | // When CTRL-R is followed by '=', don't trigger register completion |
| 2574 | // This allows expressions like <C-R>=func()<CR> to work normally |
| 2575 | if (vpeekc() == '=') |
| 2576 | break; |
| 2577 | ctrl_x_mode = CTRL_X_REGISTER; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | case Ctrl_T: |
| 2580 | // complete words from a thesaurus |
| 2581 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2582 | break; |
| 2583 | #ifdef FEAT_COMPL_FUNC |
| 2584 | case Ctrl_U: |
| 2585 | // user defined completion |
| 2586 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2587 | break; |
| 2588 | case Ctrl_O: |
| 2589 | // omni completion |
| 2590 | ctrl_x_mode = CTRL_X_OMNI; |
| 2591 | break; |
| 2592 | #endif |
| 2593 | case 's': |
| 2594 | case Ctrl_S: |
| 2595 | // complete spelling suggestions |
| 2596 | ctrl_x_mode = CTRL_X_SPELL; |
| 2597 | #ifdef FEAT_SPELL |
| 2598 | ++emsg_off; // Avoid getting the E756 error twice. |
| 2599 | spell_back_to_badword(); |
| 2600 | --emsg_off; |
| 2601 | #endif |
| 2602 | break; |
| 2603 | case Ctrl_RSB: |
| 2604 | // complete tag names |
| 2605 | ctrl_x_mode = CTRL_X_TAGS; |
| 2606 | break; |
| 2607 | #ifdef FEAT_FIND_ID |
| 2608 | case Ctrl_I: |
| 2609 | case K_S_TAB: |
| 2610 | // complete keywords from included files |
| 2611 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2612 | break; |
| 2613 | case Ctrl_D: |
| 2614 | // complete definitions from included files |
| 2615 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2616 | break; |
| 2617 | #endif |
| 2618 | case Ctrl_V: |
| 2619 | case Ctrl_Q: |
| 2620 | // complete vim commands |
| 2621 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2622 | break; |
| 2623 | case Ctrl_Z: |
| 2624 | // stop completion |
| 2625 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2626 | edit_submode = NULL; |
| 2627 | showmode(); |
| 2628 | retval = TRUE; |
| 2629 | break; |
| 2630 | case Ctrl_P: |
| 2631 | case Ctrl_N: |
| 2632 | // ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2633 | // just started ^X mode, or there were enough ^X's to cancel |
| 2634 | // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2635 | // do normal expansion when interrupting a different mode (say |
| 2636 | // ^X^F^X^P or ^P^X^X^P, see below) |
| 2637 | // nothing changes if interrupting mode 0, (eg, the flag |
| 2638 | // doesn't change when going to ADDING mode -- Acevedo |
| 2639 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2640 | compl_cont_status |= CONT_LOCAL; |
| 2641 | else if (compl_cont_mode != 0) |
| 2642 | compl_cont_status &= ~CONT_LOCAL; |
| 2643 | // FALLTHROUGH |
| 2644 | default: |
| 2645 | // If we have typed at least 2 ^X's... for modes != 0, we set |
| 2646 | // compl_cont_status = 0 (eg, as if we had just started ^X |
| 2647 | // mode). |
| 2648 | // For mode 0, we set "compl_cont_mode" to an impossible |
| 2649 | // value, in both cases ^X^X can be used to restart the same |
| 2650 | // mode (avoiding ADDING mode). |
| 2651 | // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2652 | // 'complete' and local ^P expansions respectively. |
| 2653 | // In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2654 | // mode -- Acevedo |
| 2655 | if (c == Ctrl_X) |
| 2656 | { |
| 2657 | if (compl_cont_mode != 0) |
| 2658 | compl_cont_status = 0; |
| 2659 | else |
| 2660 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
| 2661 | } |
| 2662 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2663 | edit_submode = NULL; |
| 2664 | showmode(); |
| 2665 | break; |
| 2666 | } |
| 2667 | |
| 2668 | return retval; |
| 2669 | } |
| 2670 | |
| 2671 | /* |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2672 | * Trigger CompleteDone event and adds relevant information to v:event |
| 2673 | */ |
| 2674 | static void |
| 2675 | trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED) |
| 2676 | { |
| 2677 | #if defined(FEAT_EVAL) |
| 2678 | save_v_event_T save_v_event; |
| 2679 | dict_T *v_event = get_v_event(&save_v_event); |
| 2680 | char_u *mode_str = NULL; |
| 2681 | |
| 2682 | mode = mode & ~CTRL_X_WANT_IDENT; |
| 2683 | if (ctrl_x_mode_names[mode]) |
| 2684 | mode_str = (char_u *)ctrl_x_mode_names[mode]; |
| 2685 | |
| 2686 | (void)dict_add_string(v_event, "complete_word", |
| 2687 | word == NULL ? (char_u *)"" : word); |
| 2688 | (void)dict_add_string(v_event, "complete_type", |
| 2689 | mode_str != NULL ? mode_str : (char_u *)""); |
| 2690 | |
| 2691 | dict_set_items_ro(v_event); |
| 2692 | #endif |
| 2693 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
| 2694 | |
| 2695 | #if defined(FEAT_EVAL) |
| 2696 | restore_v_event(v_event, &save_v_event); |
| 2697 | #endif |
| 2698 | } |
| 2699 | |
| 2700 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2701 | * Stop insert completion mode |
| 2702 | */ |
| 2703 | static int |
| 2704 | ins_compl_stop(int c, int prev_mode, int retval) |
| 2705 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2706 | int want_cindent; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2707 | char_u *word = NULL; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2708 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 2709 | // Remove pre-inserted text when present. |
zeertzjq | 1343681 | 2025-04-23 20:46:35 +0200 | [diff] [blame] | 2710 | if (ins_compl_preinsert_effect() && ins_compl_win_active(curwin)) |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 2711 | ins_compl_delete(); |
| 2712 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2713 | // Get here when we have finished typing a sequence of ^N and |
| 2714 | // ^P or other completion characters in CTRL-X mode. Free up |
| 2715 | // memory that was used, and make sure we can redo the insert. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2716 | if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2717 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2718 | char_u *ptr = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2719 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2720 | // If any of the original typed text has been changed, eg when |
| 2721 | // ignorecase is set, we must add back-spaces to the redo |
| 2722 | // buffer. We add as few as necessary to delete just the part |
| 2723 | // of the original text that has changed. |
| 2724 | // When using the longest match, edited the match or used |
| 2725 | // CTRL-E then don't use the current match. |
| 2726 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2727 | ptr = compl_curr_match->cp_str.string; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2728 | ins_compl_fixRedoBufForLeader(ptr); |
| 2729 | } |
| 2730 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2731 | want_cindent = (get_can_cindent() && cindent_on()); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2732 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2733 | // When completing whole lines: fix indent for 'cindent'. |
| 2734 | // Otherwise, break line if it's too long. |
| 2735 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
| 2736 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2737 | // re-indent the current line |
| 2738 | if (want_cindent) |
| 2739 | { |
| 2740 | do_c_expr_indent(); |
| 2741 | want_cindent = FALSE; // don't do it again |
| 2742 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2743 | } |
| 2744 | else |
| 2745 | { |
| 2746 | int prev_col = curwin->w_cursor.col; |
| 2747 | |
| 2748 | // put the cursor on the last char, for 'tw' formatting |
| 2749 | if (prev_col > 0) |
| 2750 | dec_cursor(); |
| 2751 | // only format when something was inserted |
| 2752 | if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) |
| 2753 | insertchar(NUL, 0, -1); |
| 2754 | if (prev_col > 0 |
| 2755 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 2756 | inc_cursor(); |
| 2757 | } |
| 2758 | |
| 2759 | // If the popup menu is displayed pressing CTRL-Y means accepting |
| 2760 | // the selection without inserting anything. When |
| 2761 | // compl_enter_selects is set the Enter key does the same. |
| 2762 | if ((c == Ctrl_Y || (compl_enter_selects |
| 2763 | && (c == CAR || c == K_KENTER || c == NL))) |
| 2764 | && pum_visible()) |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2765 | { |
| 2766 | word = vim_strsave(compl_shown_match->cp_str.string); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2767 | retval = TRUE; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2768 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2769 | |
| 2770 | // CTRL-E means completion is Ended, go back to the typed text. |
| 2771 | // but only do this, if the Popup is still visible |
| 2772 | if (c == Ctrl_E) |
| 2773 | { |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2774 | char_u *p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2775 | size_t plen = 0; |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2776 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2777 | ins_compl_delete(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2778 | if (compl_leader.string != NULL) |
| 2779 | { |
| 2780 | p = compl_leader.string; |
| 2781 | plen = compl_leader.length; |
| 2782 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2783 | else if (compl_first_match != NULL) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2784 | { |
| 2785 | p = compl_orig_text.string; |
| 2786 | plen = compl_orig_text.length; |
| 2787 | } |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2788 | if (p != NULL) |
| 2789 | { |
| 2790 | int compl_len = get_compl_len(); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2791 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2792 | if ((int)plen > compl_len) |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 2793 | ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2794 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2795 | retval = TRUE; |
| 2796 | } |
| 2797 | |
| 2798 | auto_format(FALSE, TRUE); |
| 2799 | |
| 2800 | // Trigger the CompleteDonePre event to give scripts a chance to |
| 2801 | // act upon the completion before clearing the info, and restore |
| 2802 | // ctrl_x_mode, so that complete_info() can be used. |
| 2803 | ctrl_x_mode = prev_mode; |
| 2804 | ins_apply_autocmds(EVENT_COMPLETEDONEPRE); |
| 2805 | |
| 2806 | ins_compl_free(); |
| 2807 | compl_started = FALSE; |
| 2808 | compl_matches = 0; |
| 2809 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 2810 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 2811 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2812 | compl_enter_selects = FALSE; |
| 2813 | if (edit_submode != NULL) |
| 2814 | { |
| 2815 | edit_submode = NULL; |
| 2816 | showmode(); |
| 2817 | } |
| 2818 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2819 | if (c == Ctrl_C && cmdwin_type != 0) |
| 2820 | // Avoid the popup menu remains displayed when leaving the |
| 2821 | // command line window. |
| 2822 | update_screen(0); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2823 | // Indent now if a key was typed that is in 'cinkeys'. |
| 2824 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2825 | do_c_expr_indent(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2826 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2827 | // upon the end of completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2828 | trigger_complete_done_event(prev_mode, word); |
| 2829 | vim_free(word); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2830 | |
| 2831 | return retval; |
| 2832 | } |
| 2833 | |
| 2834 | /* |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 2835 | * Cancel completion. |
| 2836 | */ |
| 2837 | int |
| 2838 | ins_compl_cancel(void) |
| 2839 | { |
| 2840 | return ins_compl_stop(' ', ctrl_x_mode, TRUE); |
| 2841 | } |
| 2842 | |
| 2843 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2844 | * Prepare for Insert mode completion, or stop it. |
| 2845 | * Called just after typing a character in Insert mode. |
| 2846 | * Returns TRUE when the character is not to be inserted; |
| 2847 | */ |
| 2848 | int |
| 2849 | ins_compl_prep(int c) |
| 2850 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 2851 | int retval = FALSE; |
| 2852 | int prev_mode = ctrl_x_mode; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2853 | |
| 2854 | // Forget any previous 'special' messages if this is actually |
| 2855 | // a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2856 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2857 | edit_submode_extra = NULL; |
| 2858 | |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2859 | // Ignore end of Select mode mapping and mouse scroll/movement. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2860 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2861 | || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 2862 | || c == K_COMMAND || c == K_SCRIPT_COMMAND) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2863 | return retval; |
| 2864 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2865 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | f0bc15c | 2019-08-18 19:23:45 +0200 | [diff] [blame] | 2866 | // Ignore mouse events in a popup window |
| 2867 | if (is_mouse_key(c)) |
| 2868 | { |
| 2869 | // Ignore drag and release events, the position does not need to be in |
| 2870 | // the popup and it may have just closed. |
| 2871 | if (c == K_LEFTRELEASE |
| 2872 | || c == K_LEFTRELEASE_NM |
| 2873 | || c == K_MIDDLERELEASE |
| 2874 | || c == K_RIGHTRELEASE |
| 2875 | || c == K_X1RELEASE |
| 2876 | || c == K_X2RELEASE |
| 2877 | || c == K_LEFTDRAG |
| 2878 | || c == K_MIDDLEDRAG |
| 2879 | || c == K_RIGHTDRAG |
| 2880 | || c == K_X1DRAG |
| 2881 | || c == K_X2DRAG) |
| 2882 | return retval; |
| 2883 | if (popup_visible) |
| 2884 | { |
| 2885 | int row = mouse_row; |
| 2886 | int col = mouse_col; |
| 2887 | win_T *wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2888 | |
| 2889 | if (wp != NULL && WIN_IS_POPUP(wp)) |
| 2890 | return retval; |
| 2891 | } |
| 2892 | } |
| 2893 | #endif |
| 2894 | |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 2895 | if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X) |
| 2896 | { |
| 2897 | if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c) |
| 2898 | || !vim_is_ctrl_x_key(c)) |
| 2899 | { |
| 2900 | // Not starting another completion mode. |
| 2901 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2902 | |
| 2903 | // CTRL-X CTRL-Z should stop completion without inserting anything |
| 2904 | if (c == Ctrl_Z) |
| 2905 | retval = TRUE; |
| 2906 | } |
| 2907 | else |
| 2908 | { |
| 2909 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2910 | |
| 2911 | // Other CTRL-X keys first stop completion, then start another |
| 2912 | // completion mode. |
| 2913 | ins_compl_prep(' '); |
| 2914 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2915 | } |
| 2916 | } |
| 2917 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2918 | // Set "compl_get_longest" when finding the first matches. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2919 | if (ctrl_x_mode_not_defined_yet() |
| 2920 | || (ctrl_x_mode_normal() && !compl_started)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2921 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 2922 | compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2923 | compl_used_match = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2924 | } |
| 2925 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2926 | if (ctrl_x_mode_not_defined_yet()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2927 | // We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2928 | // it will be yet. Now we decide. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2929 | retval = set_ctrl_x_mode(c); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2930 | else if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2931 | { |
| 2932 | // We're already in CTRL-X mode, do we stay in it? |
| 2933 | if (!vim_is_ctrl_x_key(c)) |
| 2934 | { |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2935 | 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] | 2936 | edit_submode = NULL; |
| 2937 | } |
| 2938 | showmode(); |
| 2939 | } |
| 2940 | |
| 2941 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
| 2942 | { |
| 2943 | // Show error message from attempted keyword completion (probably |
| 2944 | // 'Pattern not found') until another key is hit, then go back to |
| 2945 | // showing what mode we are in. |
| 2946 | showmode(); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2947 | if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2948 | && c != Ctrl_R && !ins_compl_pum_key(c)) |
| 2949 | || ctrl_x_mode == CTRL_X_FINISHED) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2950 | retval = ins_compl_stop(c, prev_mode, retval); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2951 | } |
| 2952 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 2953 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2954 | // upon the (possibly failed) completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2955 | trigger_complete_done_event(ctrl_x_mode, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2956 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 2957 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 2958 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2959 | // reset continue_* if we left expansion-mode, if we stay they'll be |
| 2960 | // (re)set properly in ins_complete() |
| 2961 | if (!vim_is_ctrl_x_key(c)) |
| 2962 | { |
| 2963 | compl_cont_status = 0; |
| 2964 | compl_cont_mode = 0; |
| 2965 | } |
| 2966 | |
| 2967 | return retval; |
| 2968 | } |
| 2969 | |
| 2970 | /* |
| 2971 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 2972 | * text. This inserts backspaces and appends the changed text. |
| 2973 | * "ptr" is the known leader text or NUL. |
| 2974 | */ |
| 2975 | static void |
| 2976 | ins_compl_fixRedoBufForLeader(char_u *ptr_arg) |
| 2977 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2978 | int len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2979 | char_u *p; |
| 2980 | char_u *ptr = ptr_arg; |
| 2981 | |
| 2982 | if (ptr == NULL) |
| 2983 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2984 | if (compl_leader.string != NULL) |
| 2985 | ptr = compl_leader.string; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2986 | else |
| 2987 | return; // nothing to do |
| 2988 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2989 | if (compl_orig_text.string != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2990 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2991 | p = compl_orig_text.string; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2992 | // Find length of common prefix between original text and new completion |
| 2993 | while (p[len] != NUL && p[len] == ptr[len]) |
| 2994 | len++; |
| 2995 | // Adjust length to not break inside a multi-byte character |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2996 | if (len > 0) |
| 2997 | len -= (*mb_head_off)(p, p + len); |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 2998 | // Add backspace characters for each remaining character in |
| 2999 | // original text |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3000 | for (p += len; *p != NUL; MB_PTR_ADV(p)) |
| 3001 | AppendCharToRedobuff(K_BS); |
| 3002 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3003 | if (ptr != NULL) |
| 3004 | AppendToRedobuffLit(ptr + len, -1); |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3009 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3010 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3011 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3012 | * |
| 3013 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3014 | */ |
| 3015 | static buf_T * |
| 3016 | ins_compl_next_buf(buf_T *buf, int flag) |
| 3017 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3018 | static win_T *wp = NULL; |
| 3019 | int skip_buffer; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3020 | |
| 3021 | if (flag == 'w') // just windows |
| 3022 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3023 | if (buf == curbuf || !win_valid(wp)) |
| 3024 | // first call for this flag/expansion or window was closed |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3025 | wp = curwin; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3026 | |
| 3027 | while (TRUE) |
| 3028 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3029 | // Move to next window (wrap to first window if at the end) |
| 3030 | wp = (wp->w_next != NULL) ? wp->w_next : firstwin; |
| 3031 | // Break if we're back at start or found an unscanned buffer |
| 3032 | if (wp == curwin || !wp->w_buffer->b_scanned) |
| 3033 | break; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3034 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3035 | buf = wp->w_buffer; |
| 3036 | } |
| 3037 | else |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3038 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3039 | // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3040 | // (unlisted buffers) |
| 3041 | // When completing whole lines skip unloaded buffers. |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3042 | while (TRUE) |
| 3043 | { |
| 3044 | // Move to next buffer (wrap to first buffer if at the end) |
| 3045 | buf = (buf->b_next != NULL) ? buf->b_next : firstbuf; |
| 3046 | // Break if we're back at start buffer |
| 3047 | if (buf == curbuf) |
| 3048 | break; |
| 3049 | |
| 3050 | // Check buffer conditions based on flag |
| 3051 | if (flag == 'U') |
| 3052 | skip_buffer = buf->b_p_bl; |
| 3053 | else |
| 3054 | skip_buffer = !buf->b_p_bl || |
| 3055 | (buf->b_ml.ml_mfp == NULL) != (flag == 'u'); |
| 3056 | |
| 3057 | // Break if we found a buffer that matches our criteria |
| 3058 | if (!skip_buffer && !buf->b_scanned) |
| 3059 | break; |
| 3060 | } |
| 3061 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3062 | return buf; |
| 3063 | } |
| 3064 | |
| 3065 | #ifdef FEAT_COMPL_FUNC |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3066 | |
| 3067 | # ifdef FEAT_EVAL |
| 3068 | static callback_T cfu_cb; // 'completefunc' callback function |
| 3069 | static callback_T ofu_cb; // 'omnifunc' callback function |
| 3070 | static callback_T tsrfu_cb; // 'thesaurusfunc' callback function |
| 3071 | # endif |
| 3072 | |
| 3073 | /* |
| 3074 | * Copy a global callback function to a buffer local callback. |
| 3075 | */ |
| 3076 | static void |
| 3077 | copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb) |
| 3078 | { |
| 3079 | free_callback(bufcb); |
| 3080 | if (globcb->cb_name != NULL && *globcb->cb_name != NUL) |
| 3081 | copy_callback(bufcb, globcb); |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * Parse the 'completefunc' option value and set the callback function. |
| 3086 | * Invoked when the 'completefunc' option is set. The option value can be a |
| 3087 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3088 | * lambda expression. |
| 3089 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3090 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3091 | did_set_completefunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3092 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3093 | if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL) |
| 3094 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3095 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3096 | set_buflocal_cfu_callback(curbuf); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3097 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3098 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * Copy the global 'completefunc' callback function to the buffer-local |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3103 | * 'completefunc' callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3104 | */ |
| 3105 | void |
| 3106 | set_buflocal_cfu_callback(buf_T *buf UNUSED) |
| 3107 | { |
| 3108 | # ifdef FEAT_EVAL |
| 3109 | copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb); |
| 3110 | # endif |
| 3111 | } |
| 3112 | |
| 3113 | /* |
| 3114 | * Parse the 'omnifunc' option value and set the callback function. |
| 3115 | * Invoked when the 'omnifunc' option is set. The option value can be a |
| 3116 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3117 | * lambda expression. |
| 3118 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3119 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3120 | did_set_omnifunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3121 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3122 | if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL) |
| 3123 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3124 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3125 | set_buflocal_ofu_callback(curbuf); |
| 3126 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc' |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3131 | * callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3132 | */ |
| 3133 | void |
| 3134 | set_buflocal_ofu_callback(buf_T *buf UNUSED) |
| 3135 | { |
| 3136 | # ifdef FEAT_EVAL |
| 3137 | copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb); |
| 3138 | # endif |
| 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * Parse the 'thesaurusfunc' option value and set the callback function. |
| 3143 | * Invoked when the 'thesaurusfunc' option is set. The option value can be a |
| 3144 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 3145 | * lambda expression. |
| 3146 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3147 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3148 | did_set_thesaurusfunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3149 | { |
| 3150 | int retval; |
| 3151 | |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3152 | if (args->os_flags & OPT_LOCAL) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3153 | // buffer-local option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3154 | retval = option_set_callback_func(curbuf->b_p_tsrfu, |
| 3155 | &curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3156 | else |
| 3157 | { |
| 3158 | // global option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3159 | retval = option_set_callback_func(p_tsrfu, &tsrfu_cb); |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 3160 | // when using :set, free the local callback |
| 3161 | if (!(args->os_flags & OPT_GLOBAL)) |
| 3162 | free_callback(&curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 3165 | return retval == FAIL ? e_invalid_argument : NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3168 | /* |
| 3169 | * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3170 | * "copyID" so that they are not garbage collected. |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3171 | */ |
| 3172 | int |
| 3173 | set_ref_in_insexpand_funcs(int copyID) |
| 3174 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3175 | int abort = set_ref_in_callback(&cfu_cb, copyID); |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 3176 | abort = abort || set_ref_in_callback(&ofu_cb, copyID); |
| 3177 | abort = abort || set_ref_in_callback(&tsrfu_cb, copyID); |
| 3178 | |
| 3179 | return abort; |
| 3180 | } |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3181 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3182 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3183 | * Get the user-defined completion function name for completion "type" |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3184 | */ |
| 3185 | static char_u * |
| 3186 | get_complete_funcname(int type) |
| 3187 | { |
| 3188 | switch (type) |
| 3189 | { |
| 3190 | case CTRL_X_FUNCTION: |
| 3191 | return curbuf->b_p_cfu; |
| 3192 | case CTRL_X_OMNI: |
| 3193 | return curbuf->b_p_ofu; |
| 3194 | case CTRL_X_THESAURUS: |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3195 | return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu; |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3196 | default: |
| 3197 | return (char_u *)""; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | /* |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3202 | * Get the callback to use for insert mode completion. |
| 3203 | */ |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3204 | static callback_T * |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3205 | get_insert_callback(int type) |
| 3206 | { |
| 3207 | if (type == CTRL_X_FUNCTION) |
| 3208 | return &curbuf->b_cfu_cb; |
| 3209 | if (type == CTRL_X_OMNI) |
| 3210 | return &curbuf->b_ofu_cb; |
| 3211 | // CTRL_X_THESAURUS |
| 3212 | return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb; |
| 3213 | } |
| 3214 | |
| 3215 | /* |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 3216 | * Execute user defined complete function 'completefunc', 'omnifunc' or |
| 3217 | * 'thesaurusfunc', and get matches in "matches". |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3218 | * "type" can be one of CTRL_X_OMNI, CTRL_X_FUNCTION, or CTRL_X_THESAURUS. |
| 3219 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 3220 | * option; otherwise, it is NULL. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3221 | */ |
| 3222 | static void |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3223 | expand_by_function(int type, char_u *base, callback_T *cb) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3224 | { |
| 3225 | list_T *matchlist = NULL; |
| 3226 | dict_T *matchdict = NULL; |
| 3227 | typval_T args[3]; |
| 3228 | char_u *funcname; |
| 3229 | pos_T pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3230 | typval_T rettv; |
| 3231 | int save_State = State; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3232 | int retval; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3233 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3234 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3235 | if (!is_cpt_function) |
| 3236 | { |
| 3237 | funcname = get_complete_funcname(type); |
| 3238 | if (*funcname == NUL) |
| 3239 | return; |
| 3240 | cb = get_insert_callback(type); |
| 3241 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3242 | |
| 3243 | // Call 'completefunc' to obtain the list of matches. |
| 3244 | args[0].v_type = VAR_NUMBER; |
| 3245 | args[0].vval.v_number = 0; |
| 3246 | args[1].v_type = VAR_STRING; |
| 3247 | args[1].vval.v_string = base != NULL ? base : (char_u *)""; |
| 3248 | args[2].v_type = VAR_UNKNOWN; |
| 3249 | |
| 3250 | pos = curwin->w_cursor; |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 3251 | // Lock the text to avoid weird things from happening. Also disallow |
| 3252 | // switching to another window, it should not be needed and may end up in |
| 3253 | // Insert mode in another buffer. |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3254 | ++textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3255 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3256 | retval = call_callback(cb, 0, &rettv, 2, args); |
| 3257 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3258 | // Call a function, which returns a list or dict. |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 3259 | if (retval == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3260 | { |
| 3261 | switch (rettv.v_type) |
| 3262 | { |
| 3263 | case VAR_LIST: |
| 3264 | matchlist = rettv.vval.v_list; |
| 3265 | break; |
| 3266 | case VAR_DICT: |
| 3267 | matchdict = rettv.vval.v_dict; |
| 3268 | break; |
| 3269 | case VAR_SPECIAL: |
| 3270 | if (rettv.vval.v_number == VVAL_NONE) |
| 3271 | compl_opt_suppress_empty = TRUE; |
| 3272 | // FALLTHROUGH |
| 3273 | default: |
| 3274 | // TODO: Give error message? |
| 3275 | clear_tv(&rettv); |
| 3276 | break; |
| 3277 | } |
| 3278 | } |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 3279 | --textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3280 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3281 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 3282 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3283 | validate_cursor(); |
| 3284 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 3285 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 3286 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3287 | goto theend; |
| 3288 | } |
| 3289 | |
| 3290 | if (matchlist != NULL) |
| 3291 | ins_compl_add_list(matchlist); |
| 3292 | else if (matchdict != NULL) |
| 3293 | ins_compl_add_dict(matchdict); |
| 3294 | |
| 3295 | theend: |
| 3296 | // Restore State, it might have been changed. |
| 3297 | State = save_State; |
| 3298 | |
| 3299 | if (matchdict != NULL) |
| 3300 | dict_unref(matchdict); |
| 3301 | if (matchlist != NULL) |
| 3302 | list_unref(matchlist); |
| 3303 | } |
| 3304 | #endif // FEAT_COMPL_FUNC |
| 3305 | |
| 3306 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3307 | |
| 3308 | static inline int |
| 3309 | get_user_highlight_attr(char_u *hlname) |
| 3310 | { |
| 3311 | if (hlname != NULL && *hlname != NUL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3312 | return syn_name2attr(hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3313 | return -1; |
| 3314 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3315 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3316 | * Add a match to the list of matches from a typeval_T. |
| 3317 | * If the given string is already in the list of completions, then return |
| 3318 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 3319 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3320 | * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck(). |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3321 | */ |
| 3322 | static int |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3323 | ins_compl_add_tv(typval_T *tv, int dir, int fast) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3324 | { |
| 3325 | char_u *word; |
| 3326 | int dup = FALSE; |
| 3327 | int empty = FALSE; |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3328 | int flags = fast ? CP_FAST : 0; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3329 | char_u *(cptext[CPT_COUNT]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3330 | typval_T user_data; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3331 | int status; |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3332 | char_u *user_abbr_hlname; |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3333 | char_u *user_kind_hlname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3334 | int user_hl[2] = { -1, -1 }; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3335 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 3336 | user_data.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3337 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 3338 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3339 | word = dict_get_string(tv->vval.v_dict, "word", FALSE); |
| 3340 | cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE); |
| 3341 | cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE); |
| 3342 | cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE); |
| 3343 | cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3344 | |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 3345 | user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3346 | user_hl[0] = get_user_highlight_attr(user_abbr_hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 3347 | |
| 3348 | user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3349 | user_hl[1] = get_user_highlight_attr(user_kind_hlname); |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3350 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3351 | dict_get_tv(tv->vval.v_dict, "user_data", &user_data); |
| 3352 | if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL |
| 3353 | && dict_get_number(tv->vval.v_dict, "icase")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3354 | flags |= CP_ICASE; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 3355 | if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL) |
| 3356 | dup = dict_get_number(tv->vval.v_dict, "dup"); |
| 3357 | if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL) |
| 3358 | empty = dict_get_number(tv->vval.v_dict, "empty"); |
| 3359 | if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL |
| 3360 | && dict_get_number(tv->vval.v_dict, "equal")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3361 | flags |= CP_EQUAL; |
| 3362 | } |
| 3363 | else |
| 3364 | { |
| 3365 | word = tv_get_string_chk(tv); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 3366 | CLEAR_FIELD(cptext); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3367 | } |
| 3368 | if (word == NULL || (!empty && *word == NUL)) |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3369 | { |
| 3370 | clear_tv(&user_data); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3371 | return FAIL; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3372 | } |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3373 | status = ins_compl_add(word, -1, NULL, cptext, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3374 | &user_data, dir, flags, dup, user_hl, 0); |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3375 | if (status != OK) |
| 3376 | clear_tv(&user_data); |
| 3377 | return status; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3381 | * Add completions from a list. |
| 3382 | */ |
| 3383 | static void |
| 3384 | ins_compl_add_list(list_T *list) |
| 3385 | { |
| 3386 | listitem_T *li; |
| 3387 | int dir = compl_direction; |
| 3388 | |
| 3389 | // Go through the List with matches and add each of them. |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3390 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3391 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3392 | { |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3393 | if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3394 | // if dir was BACKWARD then honor it just once |
| 3395 | dir = FORWARD; |
| 3396 | else if (did_emsg) |
| 3397 | break; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | /* |
| 3402 | * Add completions from a dict. |
| 3403 | */ |
| 3404 | static void |
| 3405 | ins_compl_add_dict(dict_T *dict) |
| 3406 | { |
| 3407 | dictitem_T *di_refresh; |
| 3408 | dictitem_T *di_words; |
| 3409 | |
| 3410 | // Check for optional "refresh" item. |
| 3411 | compl_opt_refresh_always = FALSE; |
| 3412 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 3413 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
| 3414 | { |
| 3415 | char_u *v = di_refresh->di_tv.vval.v_string; |
| 3416 | |
| 3417 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 3418 | compl_opt_refresh_always = TRUE; |
| 3419 | } |
| 3420 | |
| 3421 | // Add completions from a "words" list. |
| 3422 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 3423 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 3424 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
| 3425 | } |
| 3426 | |
| 3427 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3428 | * Start completion for the complete() function. |
| 3429 | * "startcol" is where the matched text starts (1 is first column). |
| 3430 | * "list" is the list of matches. |
| 3431 | */ |
| 3432 | static void |
| 3433 | set_completion(colnr_T startcol, list_T *list) |
| 3434 | { |
| 3435 | int save_w_wrow = curwin->w_wrow; |
| 3436 | int save_w_leftcol = curwin->w_leftcol; |
| 3437 | int flags = CP_ORIGINAL_TEXT; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 3438 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 3439 | int compl_longest = (cur_cot_flags & COT_LONGEST) != 0; |
| 3440 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 3441 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3442 | |
| 3443 | // If already doing completions stop it. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3444 | if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3445 | ins_compl_prep(' '); |
| 3446 | ins_compl_clear(); |
| 3447 | ins_compl_free(); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3448 | compl_get_longest = compl_longest; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3449 | |
| 3450 | compl_direction = FORWARD; |
| 3451 | if (startcol > curwin->w_cursor.col) |
| 3452 | startcol = curwin->w_cursor.col; |
| 3453 | compl_col = startcol; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 3454 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3455 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
| 3456 | // compl_pattern doesn't need to be set |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3457 | compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, |
| 3458 | (size_t)compl_length); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3459 | if (p_ic) |
| 3460 | flags |= CP_ICASE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3461 | if (compl_orig_text.string == NULL) |
| 3462 | { |
| 3463 | compl_orig_text.length = 0; |
| 3464 | return; |
| 3465 | } |
| 3466 | compl_orig_text.length = (size_t)compl_length; |
| 3467 | if (ins_compl_add(compl_orig_text.string, |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3468 | (int)compl_orig_text.length, NULL, NULL, NULL, 0, |
| 3469 | flags | CP_FAST, FALSE, NULL, 0) != OK) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3470 | return; |
| 3471 | |
| 3472 | ctrl_x_mode = CTRL_X_EVAL; |
| 3473 | |
| 3474 | ins_compl_add_list(list); |
| 3475 | compl_matches = ins_compl_make_cyclic(); |
| 3476 | compl_started = TRUE; |
| 3477 | compl_used_match = TRUE; |
| 3478 | compl_cont_status = 0; |
| 3479 | |
| 3480 | compl_curr_match = compl_first_match; |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3481 | int no_select = compl_no_select || compl_longest; |
| 3482 | if (compl_no_insert || no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3483 | { |
| 3484 | ins_complete(K_DOWN, FALSE); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3485 | if (no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3486 | // Down/Up has no real effect. |
| 3487 | ins_complete(K_UP, FALSE); |
| 3488 | } |
| 3489 | else |
| 3490 | ins_complete(Ctrl_N, FALSE); |
| 3491 | compl_enter_selects = compl_no_insert; |
| 3492 | |
| 3493 | // Lazily show the popup menu, unless we got interrupted. |
| 3494 | if (!compl_interrupted) |
| 3495 | show_pum(save_w_wrow, save_w_leftcol); |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 3496 | may_trigger_modechanged(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3497 | out_flush(); |
| 3498 | } |
| 3499 | |
| 3500 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3501 | * "complete()" function |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3502 | */ |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3503 | void |
| 3504 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3505 | { |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3506 | if (in_vim9script() |
| 3507 | && (check_for_number_arg(argvars, 0) == FAIL |
| 3508 | || check_for_list_arg(argvars, 1) == FAIL)) |
| 3509 | return; |
| 3510 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 3511 | if ((State & MODE_INSERT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3512 | { |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 3513 | emsg(_(e_complete_can_only_be_used_in_insert_mode)); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3514 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3515 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3516 | |
| 3517 | // Check for undo allowed here, because if something was already inserted |
| 3518 | // the line was already saved for undo and this check isn't done. |
| 3519 | if (!undo_allowed()) |
| 3520 | return; |
| 3521 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3522 | if (check_for_nonnull_list_arg(argvars, 1) != FAIL) |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3523 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 3524 | int startcol = (int)tv_get_number_chk(&argvars[0], NULL); |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3525 | if (startcol > 0) |
| 3526 | set_completion(startcol - 1, argvars[1].vval.v_list); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3527 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | /* |
| 3531 | * "complete_add()" function |
| 3532 | */ |
| 3533 | void |
| 3534 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 3535 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3536 | if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3537 | return; |
| 3538 | |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3539 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* |
| 3543 | * "complete_check()" function |
| 3544 | */ |
| 3545 | void |
| 3546 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 3547 | { |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3548 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3549 | RedrawingDisabled = 0; |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3550 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3551 | ins_compl_check_keys(0, TRUE); |
| 3552 | rettv->vval.v_number = ins_compl_interrupted(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3553 | |
| 3554 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | /* |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3558 | * Add match item to the return list. |
| 3559 | * Returns FAIL if out of memory, OK otherwise. |
| 3560 | */ |
| 3561 | static int |
| 3562 | add_match_to_list( |
| 3563 | typval_T *rettv, |
| 3564 | char_u *str, |
| 3565 | int len, |
| 3566 | int pos) |
| 3567 | { |
| 3568 | list_T *match; |
| 3569 | int ret; |
| 3570 | |
| 3571 | match = list_alloc(); |
| 3572 | if (match == NULL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3573 | return FAIL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3574 | |
| 3575 | if ((ret = list_append_number(match, pos + 1)) == FAIL |
| 3576 | || (ret = list_append_string(match, str, len)) == FAIL |
| 3577 | || (ret = list_append_list(rettv->vval.v_list, match)) == FAIL) |
| 3578 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3579 | vim_free(match); |
| 3580 | return FAIL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | return OK; |
| 3584 | } |
| 3585 | |
| 3586 | /* |
| 3587 | * "complete_match()" function |
| 3588 | */ |
| 3589 | void |
| 3590 | f_complete_match(typval_T *argvars, typval_T *rettv) |
| 3591 | { |
| 3592 | linenr_T lnum; |
| 3593 | colnr_T col; |
| 3594 | char_u *line = NULL; |
| 3595 | char_u *ise = NULL; |
| 3596 | regmatch_T regmatch; |
| 3597 | char_u *before_cursor = NULL; |
| 3598 | char_u *cur_end = NULL; |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3599 | int bytepos = 0; |
| 3600 | char_u part[MAXPATHL]; |
| 3601 | int ret; |
| 3602 | |
| 3603 | if (rettv_list_alloc(rettv) == FAIL) |
| 3604 | return; |
| 3605 | |
| 3606 | ise = curbuf->b_p_ise[0] != NUL ? curbuf->b_p_ise : p_ise; |
| 3607 | |
| 3608 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 3609 | { |
| 3610 | lnum = curwin->w_cursor.lnum; |
| 3611 | col = curwin->w_cursor.col; |
| 3612 | } |
| 3613 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 3614 | { |
| 3615 | emsg(_(e_invalid_argument)); |
| 3616 | return; |
| 3617 | } |
| 3618 | else |
| 3619 | { |
| 3620 | lnum = (linenr_T)tv_get_number(&argvars[0]); |
| 3621 | col = (colnr_T)tv_get_number(&argvars[1]); |
| 3622 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 3623 | { |
| 3624 | semsg(_(e_invalid_line_number_nr), lnum); |
| 3625 | return; |
| 3626 | } |
| 3627 | if (col < 1 || col > ml_get_buf_len(curbuf, lnum)) |
| 3628 | { |
| 3629 | semsg(_(e_invalid_column_number_nr), col + 1); |
| 3630 | return; |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 3635 | if (line == NULL) |
| 3636 | return; |
| 3637 | |
| 3638 | before_cursor = vim_strnsave(line, col); |
| 3639 | if (before_cursor == NULL) |
| 3640 | return; |
| 3641 | |
| 3642 | if (ise == NULL || *ise == NUL) |
| 3643 | { |
| 3644 | regmatch.regprog = vim_regcomp((char_u *)"\\k\\+$", RE_MAGIC); |
| 3645 | if (regmatch.regprog != NULL) |
| 3646 | { |
| 3647 | if (vim_regexec_nl(®match, before_cursor, (colnr_T)0)) |
| 3648 | { |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3649 | char_u *trig = vim_strnsave(regmatch.startp[0], |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3650 | regmatch.endp[0] - regmatch.startp[0]); |
| 3651 | if (trig == NULL) |
| 3652 | { |
| 3653 | vim_free(before_cursor); |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3654 | vim_regfree(regmatch.regprog); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3655 | return; |
| 3656 | } |
| 3657 | |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3658 | bytepos = (int)(regmatch.startp[0] - before_cursor); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3659 | ret = add_match_to_list(rettv, trig, -1, bytepos); |
| 3660 | vim_free(trig); |
| 3661 | if (ret == FAIL) |
| 3662 | { |
Christian Brabandt | 3accf04 | 2025-04-25 19:01:06 +0200 | [diff] [blame] | 3663 | vim_free(before_cursor); |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3664 | vim_regfree(regmatch.regprog); |
| 3665 | return; |
| 3666 | } |
| 3667 | } |
| 3668 | vim_regfree(regmatch.regprog); |
| 3669 | } |
| 3670 | } |
| 3671 | else |
| 3672 | { |
glepnir | 08db2f4 | 2025-05-14 20:26:19 +0200 | [diff] [blame] | 3673 | char_u *p = ise; |
| 3674 | char_u *p_space = NULL; |
| 3675 | |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3676 | cur_end = before_cursor + (int)STRLEN(before_cursor); |
| 3677 | |
| 3678 | while (*p != NUL) |
| 3679 | { |
glepnir | 8d0e42b | 2025-05-12 20:28:28 +0200 | [diff] [blame] | 3680 | int len = 0; |
glepnir | 08db2f4 | 2025-05-14 20:26:19 +0200 | [diff] [blame] | 3681 | if (p_space) |
glepnir | 8d0e42b | 2025-05-12 20:28:28 +0200 | [diff] [blame] | 3682 | { |
glepnir | 08db2f4 | 2025-05-14 20:26:19 +0200 | [diff] [blame] | 3683 | len = p - p_space - 1; |
| 3684 | memcpy(part, p_space + 1, len); |
| 3685 | p_space = NULL; |
glepnir | 8d0e42b | 2025-05-12 20:28:28 +0200 | [diff] [blame] | 3686 | } |
| 3687 | else |
glepnir | 08db2f4 | 2025-05-14 20:26:19 +0200 | [diff] [blame] | 3688 | { |
| 3689 | char_u *next_comma = vim_strchr((*p == ',') ? p + 1 : p, ','); |
| 3690 | if (next_comma && *(next_comma + 1) == ' ') |
| 3691 | p_space = next_comma; |
| 3692 | |
glepnir | 8d0e42b | 2025-05-12 20:28:28 +0200 | [diff] [blame] | 3693 | len = copy_option_part(&p, part, MAXPATHL, ","); |
glepnir | 08db2f4 | 2025-05-14 20:26:19 +0200 | [diff] [blame] | 3694 | } |
glepnir | bcd5995 | 2025-04-24 21:48:35 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (len > 0 && len <= col) |
| 3697 | { |
| 3698 | if (STRNCMP(cur_end - len, part, len) == 0) |
| 3699 | { |
| 3700 | bytepos = col - len; |
| 3701 | if (add_match_to_list(rettv, part, len, bytepos) == FAIL) |
| 3702 | { |
| 3703 | vim_free(before_cursor); |
| 3704 | return; |
| 3705 | } |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | vim_free(before_cursor); |
| 3712 | } |
| 3713 | |
| 3714 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3715 | * Return Insert completion mode name string |
| 3716 | */ |
| 3717 | static char_u * |
| 3718 | ins_compl_mode(void) |
| 3719 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3720 | 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] | 3721 | return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; |
| 3722 | |
| 3723 | return (char_u *)""; |
| 3724 | } |
| 3725 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 3726 | /* |
| 3727 | * Assign the sequence number to all the completion matches which don't have |
| 3728 | * one assigned yet. |
| 3729 | */ |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3730 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 3731 | ins_compl_update_sequence_numbers(void) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3732 | { |
| 3733 | int number = 0; |
| 3734 | compl_T *match; |
| 3735 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3736 | if (compl_dir_forward()) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3737 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3738 | // Search backwards for the first valid (!= -1) number. |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3739 | // This should normally succeed already at the first loop |
| 3740 | // cycle, so it's fast! |
| 3741 | for (match = compl_curr_match->cp_prev; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3742 | && !is_first_match(match); match = match->cp_prev) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3743 | if (match->cp_number != -1) |
| 3744 | { |
| 3745 | number = match->cp_number; |
| 3746 | break; |
| 3747 | } |
| 3748 | if (match != NULL) |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3749 | // go up and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3750 | for (match = match->cp_next; |
| 3751 | match != NULL && match->cp_number == -1; |
| 3752 | match = match->cp_next) |
| 3753 | match->cp_number = ++number; |
| 3754 | } |
| 3755 | else // BACKWARD |
| 3756 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3757 | // Search forwards (upwards) for the first valid (!= -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3758 | // number. This should normally succeed already at the |
| 3759 | // first loop cycle, so it's fast! |
| 3760 | for (match = compl_curr_match->cp_next; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3761 | && !is_first_match(match); match = match->cp_next) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3762 | { |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3763 | if (match->cp_number != -1) |
| 3764 | { |
| 3765 | number = match->cp_number; |
| 3766 | break; |
| 3767 | } |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3768 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3769 | if (match != NULL) |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3770 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3771 | // go down and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3772 | for (match = match->cp_prev; match |
| 3773 | && match->cp_number == -1; |
| 3774 | match = match->cp_prev) |
| 3775 | match->cp_number = ++number; |
glepnir | 40891ba | 2025-02-10 22:18:00 +0100 | [diff] [blame] | 3776 | } |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3777 | } |
| 3778 | } |
| 3779 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3780 | /* |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3781 | * Fill the dict of complete_info |
| 3782 | */ |
| 3783 | static void |
| 3784 | fill_complete_info_dict(dict_T *di, compl_T *match, int add_match) |
| 3785 | { |
| 3786 | dict_add_string(di, "word", match->cp_str.string); |
| 3787 | dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]); |
| 3788 | dict_add_string(di, "menu", match->cp_text[CPT_MENU]); |
| 3789 | dict_add_string(di, "kind", match->cp_text[CPT_KIND]); |
| 3790 | dict_add_string(di, "info", match->cp_text[CPT_INFO]); |
| 3791 | if (add_match) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3792 | dict_add_bool(di, "match", match->cp_in_match_array); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3793 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3794 | // Add an empty string for backwards compatibility |
| 3795 | dict_add_string(di, "user_data", (char_u *)""); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3796 | else |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 3797 | dict_add_tv(di, "user_data", &match->cp_user_data); |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3801 | * Get complete information |
| 3802 | */ |
| 3803 | static void |
| 3804 | get_complete_info(list_T *what_list, dict_T *retdict) |
| 3805 | { |
| 3806 | int ret = OK; |
| 3807 | listitem_T *item; |
| 3808 | #define CI_WHAT_MODE 0x01 |
| 3809 | #define CI_WHAT_PUM_VISIBLE 0x02 |
| 3810 | #define CI_WHAT_ITEMS 0x04 |
| 3811 | #define CI_WHAT_SELECTED 0x08 |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3812 | #define CI_WHAT_COMPLETED 0x10 |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3813 | #define CI_WHAT_MATCHES 0x20 |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3814 | #define CI_WHAT_ALL 0xff |
| 3815 | int what_flag; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 3816 | int compl_fuzzy_match = (get_cot_flags() & COT_FUZZY) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3817 | |
| 3818 | if (what_list == NULL) |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3819 | what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3820 | else |
| 3821 | { |
| 3822 | what_flag = 0; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3823 | CHECK_LIST_MATERIALIZE(what_list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3824 | FOR_ALL_LIST_ITEMS(what_list, item) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3825 | { |
| 3826 | char_u *what = tv_get_string(&item->li_tv); |
| 3827 | |
| 3828 | if (STRCMP(what, "mode") == 0) |
| 3829 | what_flag |= CI_WHAT_MODE; |
| 3830 | else if (STRCMP(what, "pum_visible") == 0) |
| 3831 | what_flag |= CI_WHAT_PUM_VISIBLE; |
| 3832 | else if (STRCMP(what, "items") == 0) |
| 3833 | what_flag |= CI_WHAT_ITEMS; |
| 3834 | else if (STRCMP(what, "selected") == 0) |
| 3835 | what_flag |= CI_WHAT_SELECTED; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3836 | else if (STRCMP(what, "completed") == 0) |
| 3837 | what_flag |= CI_WHAT_COMPLETED; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3838 | else if (STRCMP(what, "matches") == 0) |
| 3839 | what_flag |= CI_WHAT_MATCHES; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | if (ret == OK && (what_flag & CI_WHAT_MODE)) |
| 3844 | ret = dict_add_string(retdict, "mode", ins_compl_mode()); |
| 3845 | |
| 3846 | if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) |
| 3847 | ret = dict_add_number(retdict, "pum_visible", pum_visible()); |
| 3848 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3849 | if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED |
| 3850 | | CI_WHAT_MATCHES | CI_WHAT_COMPLETED))) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3851 | { |
Christian Brabandt | 9eb236f | 2024-03-21 20:12:59 +0100 | [diff] [blame] | 3852 | list_T *li = NULL; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3853 | dict_T *di; |
| 3854 | compl_T *match; |
| 3855 | int selected_idx = -1; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3856 | int has_items = what_flag & CI_WHAT_ITEMS; |
| 3857 | int has_matches = what_flag & CI_WHAT_MATCHES; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3858 | int has_completed = what_flag & CI_WHAT_COMPLETED; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3859 | |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3860 | if (has_items || has_matches) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3861 | { |
| 3862 | li = list_alloc(); |
| 3863 | if (li == NULL) |
| 3864 | return; |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3865 | ret = dict_add_list(retdict, (has_matches && !has_items) |
| 3866 | ? "matches" : "items", li); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3867 | } |
| 3868 | if (ret == OK && what_flag & CI_WHAT_SELECTED) |
| 3869 | if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) |
| 3870 | ins_compl_update_sequence_numbers(); |
| 3871 | if (ret == OK && compl_first_match != NULL) |
| 3872 | { |
| 3873 | int list_idx = 0; |
| 3874 | match = compl_first_match; |
| 3875 | do |
| 3876 | { |
| 3877 | if (!match_at_original_text(match)) |
| 3878 | { |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3879 | if (has_items |
| 3880 | || (has_matches && match->cp_in_match_array)) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3881 | { |
| 3882 | di = dict_alloc(); |
| 3883 | if (di == NULL) |
| 3884 | return; |
| 3885 | ret = list_append_dict(li, di); |
| 3886 | if (ret != OK) |
| 3887 | return; |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3888 | fill_complete_info_dict(di, match, has_matches && has_items); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3889 | } |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3890 | if (compl_curr_match != NULL |
| 3891 | && compl_curr_match->cp_number == match->cp_number) |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3892 | selected_idx = list_idx; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 3893 | if (compl_fuzzy_match || match->cp_in_match_array) |
| 3894 | list_idx += 1; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3895 | } |
| 3896 | match = match->cp_next; |
| 3897 | } |
| 3898 | while (match != NULL && !is_first_match(match)); |
| 3899 | } |
| 3900 | if (ret == OK && (what_flag & CI_WHAT_SELECTED)) |
| 3901 | ret = dict_add_number(retdict, "selected", selected_idx); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3902 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3903 | if (ret == OK && selected_idx != -1 && has_completed) |
| 3904 | { |
| 3905 | di = dict_alloc(); |
| 3906 | if (di == NULL) |
| 3907 | return; |
| 3908 | fill_complete_info_dict(di, compl_curr_match, FALSE); |
| 3909 | ret = dict_add_dict(retdict, "completed", di); |
| 3910 | } |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 3911 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3915 | * "complete_info()" function |
| 3916 | */ |
| 3917 | void |
| 3918 | f_complete_info(typval_T *argvars, typval_T *rettv) |
| 3919 | { |
| 3920 | list_T *what_list = NULL; |
| 3921 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 3922 | if (rettv_dict_alloc(rettv) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3923 | return; |
| 3924 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3925 | if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL) |
| 3926 | return; |
| 3927 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3928 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 3929 | { |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3930 | if (check_for_list_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3931 | return; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3932 | what_list = argvars[0].vval.v_list; |
| 3933 | } |
| 3934 | get_complete_info(what_list, rettv->vval.v_dict); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3935 | } |
| 3936 | #endif |
| 3937 | |
| 3938 | /* |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3939 | * Returns TRUE when using a user-defined function for thesaurus completion. |
| 3940 | */ |
| 3941 | static int |
| 3942 | thesaurus_func_complete(int type UNUSED) |
| 3943 | { |
| 3944 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3945 | return type == CTRL_X_THESAURUS |
| 3946 | && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL); |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3947 | #else |
| 3948 | return FALSE; |
| 3949 | #endif |
| 3950 | } |
| 3951 | |
| 3952 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3953 | * Return value of process_next_cpt_value() |
| 3954 | */ |
| 3955 | enum |
| 3956 | { |
| 3957 | INS_COMPL_CPT_OK = 1, |
| 3958 | INS_COMPL_CPT_CONT, |
| 3959 | INS_COMPL_CPT_END |
| 3960 | }; |
| 3961 | |
| 3962 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3963 | * state information used for getting the next set of insert completion |
| 3964 | * matches. |
| 3965 | */ |
| 3966 | typedef struct |
| 3967 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3968 | char_u *e_cpt_copy; // copy of 'complete' |
| 3969 | char_u *e_cpt; // current entry in "e_cpt_copy" |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3970 | buf_T *ins_buf; // buffer being scanned |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3971 | pos_T *cur_match_pos; // current match position |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3972 | pos_T prev_match_pos; // previous match position |
| 3973 | int set_match_pos; // save first_match_pos/last_match_pos |
| 3974 | pos_T first_match_pos; // first match position |
| 3975 | pos_T last_match_pos; // last match position |
| 3976 | int found_all; // found all matches of a certain type. |
| 3977 | char_u *dict; // dictionary file to search |
| 3978 | int dict_f; // "dict" is an exact file name or not |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 3979 | callback_T *func_cb; // callback of function in 'cpt' option |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3980 | } ins_compl_next_state_T; |
| 3981 | |
| 3982 | /* |
| 3983 | * Process the next 'complete' option value in st->e_cpt. |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3984 | * |
| 3985 | * If successful, the arguments are set as below: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3986 | * st->cpt - pointer to the next option value in "st->cpt" |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3987 | * compl_type_arg - type of insert mode completion to use |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3988 | * st->found_all - all matches of this type are found |
| 3989 | * st->ins_buf - search for completions in this buffer |
| 3990 | * st->first_match_pos - position of the first completion match |
| 3991 | * st->last_match_pos - position of the last completion match |
| 3992 | * 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] | 3993 | * avoid loops after the search wraps around. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3994 | * st->dict - name of the dictionary or thesaurus file to search |
| 3995 | * 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] | 3996 | * |
| 3997 | * Returns INS_COMPL_CPT_OK if the next value is processed successfully. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3998 | * Returns INS_COMPL_CPT_CONT to skip the current completion source matching |
| 3999 | * the "st->e_cpt" option value and process the next matching source. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4000 | * 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] | 4001 | */ |
| 4002 | static int |
| 4003 | process_next_cpt_value( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4004 | ins_compl_next_state_T *st, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4005 | int *compl_type_arg, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4006 | pos_T *start_match_pos, |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 4007 | int fuzzy_collect) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4008 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4009 | int compl_type = -1; |
| 4010 | int status = INS_COMPL_CPT_OK; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4011 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4012 | st->found_all = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4013 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4014 | while (*st->e_cpt == ',' || *st->e_cpt == ' ') |
| 4015 | st->e_cpt++; |
| 4016 | |
| 4017 | if (*st->e_cpt == '.' && !curbuf->b_scanned) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4018 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4019 | st->ins_buf = curbuf; |
| 4020 | st->first_match_pos = *start_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4021 | // Move the cursor back one character so that ^N can match the |
| 4022 | // word immediately after the cursor. |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 4023 | 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] | 4024 | { |
| 4025 | // Move the cursor to after the last character in the |
| 4026 | // buffer, so that word at start of buffer is found |
| 4027 | // correctly. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4028 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 4029 | 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] | 4030 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4031 | st->last_match_pos = st->first_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4032 | compl_type = 0; |
| 4033 | |
| 4034 | // Remember the first match so that the loop stops when we |
| 4035 | // wrap and come back there a second time. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4036 | st->set_match_pos = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4037 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4038 | else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4039 | && (st->ins_buf = ins_compl_next_buf( |
| 4040 | st->ins_buf, *st->e_cpt)) != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4041 | { |
| 4042 | // Scan a buffer, but not the current one. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4043 | if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4044 | { |
| 4045 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4046 | st->first_match_pos.col = st->last_match_pos.col = 0; |
| 4047 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1; |
| 4048 | st->last_match_pos.lnum = 0; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4049 | compl_type = 0; |
| 4050 | } |
| 4051 | else // unloaded buffer, scan like dictionary |
| 4052 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4053 | st->found_all = TRUE; |
| 4054 | if (st->ins_buf->b_fname == NULL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4055 | { |
| 4056 | status = INS_COMPL_CPT_CONT; |
| 4057 | goto done; |
| 4058 | } |
| 4059 | compl_type = CTRL_X_DICTIONARY; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4060 | st->dict = st->ins_buf->b_fname; |
| 4061 | st->dict_f = DICT_EXACT; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4062 | } |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 4063 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 4064 | { |
| 4065 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 4066 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
| 4067 | st->ins_buf->b_fname == NULL |
| 4068 | ? buf_spname(st->ins_buf) |
| 4069 | : st->ins_buf->b_sfname == NULL |
| 4070 | ? st->ins_buf->b_fname |
| 4071 | : st->ins_buf->b_sfname); |
| 4072 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 4073 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4074 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4075 | else if (*st->e_cpt == NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4076 | status = INS_COMPL_CPT_END; |
| 4077 | else |
| 4078 | { |
| 4079 | if (ctrl_x_mode_line_or_eval()) |
| 4080 | compl_type = -1; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4081 | else if (*st->e_cpt == 'k' || *st->e_cpt == 's') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4082 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4083 | if (*st->e_cpt == 'k') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4084 | compl_type = CTRL_X_DICTIONARY; |
| 4085 | else |
| 4086 | compl_type = CTRL_X_THESAURUS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4087 | if (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4088 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4089 | st->dict = st->e_cpt; |
| 4090 | st->dict_f = DICT_FIRST; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4091 | } |
| 4092 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4093 | #ifdef FEAT_COMPL_FUNC |
| 4094 | else if (*st->e_cpt == 'f' || *st->e_cpt == 'o') |
| 4095 | { |
| 4096 | compl_type = CTRL_X_FUNCTION; |
| 4097 | if (*st->e_cpt == 'o') |
| 4098 | st->func_cb = &curbuf->b_ofu_cb; |
| 4099 | else |
| 4100 | st->func_cb = (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
| 4101 | ? get_cpt_func_callback(st->e_cpt) : &curbuf->b_cfu_cb; |
| 4102 | if (!st->func_cb) |
| 4103 | compl_type = -1; |
| 4104 | } |
| 4105 | #endif |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4106 | #ifdef FEAT_FIND_ID |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4107 | else if (*st->e_cpt == 'i') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4108 | compl_type = CTRL_X_PATH_PATTERNS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4109 | else if (*st->e_cpt == 'd') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4110 | compl_type = CTRL_X_PATH_DEFINES; |
| 4111 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4112 | else if (*st->e_cpt == ']' || *st->e_cpt == 't') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4113 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4114 | compl_type = CTRL_X_TAGS; |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 4115 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 4116 | { |
| 4117 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 4118 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
| 4119 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 4120 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4121 | } |
| 4122 | else |
| 4123 | compl_type = -1; |
| 4124 | |
| 4125 | // in any case e_cpt is advanced to the next entry |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4126 | (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ","); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4127 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4128 | st->found_all = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4129 | if (compl_type == -1) |
| 4130 | status = INS_COMPL_CPT_CONT; |
| 4131 | } |
| 4132 | |
| 4133 | done: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4134 | *compl_type_arg = compl_type; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4135 | return status; |
| 4136 | } |
| 4137 | |
| 4138 | #ifdef FEAT_FIND_ID |
| 4139 | /* |
| 4140 | * Get the next set of identifiers or defines matching "compl_pattern" in |
| 4141 | * included files. |
| 4142 | */ |
| 4143 | static void |
| 4144 | get_next_include_file_completion(int compl_type) |
| 4145 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4146 | find_pattern_in_path(compl_pattern.string, compl_direction, |
| 4147 | (int)compl_pattern.length, FALSE, FALSE, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4148 | (compl_type == CTRL_X_PATH_DEFINES |
| 4149 | && !(compl_cont_status & CONT_SOL)) |
| 4150 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 4151 | (linenr_T)1, (linenr_T)MAXLNUM, FALSE); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4152 | } |
| 4153 | #endif |
| 4154 | |
| 4155 | /* |
| 4156 | * Get the next set of words matching "compl_pattern" in dictionary or |
| 4157 | * thesaurus files. |
| 4158 | */ |
| 4159 | static void |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4160 | 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] | 4161 | { |
| 4162 | #ifdef FEAT_COMPL_FUNC |
| 4163 | if (thesaurus_func_complete(compl_type)) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4164 | expand_by_function(compl_type, compl_pattern.string, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4165 | else |
| 4166 | #endif |
| 4167 | ins_compl_dictionaries( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4168 | dict != NULL ? dict |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4169 | : (compl_type == CTRL_X_THESAURUS |
| 4170 | ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr) |
| 4171 | : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)), |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4172 | compl_pattern.string, |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4173 | dict != NULL ? dict_f : 0, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4174 | compl_type == CTRL_X_THESAURUS); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /* |
| 4178 | * Get the next set of tag names matching "compl_pattern". |
| 4179 | */ |
| 4180 | static void |
| 4181 | get_next_tag_completion(void) |
| 4182 | { |
| 4183 | int save_p_ic; |
| 4184 | char_u **matches; |
| 4185 | int num_matches; |
| 4186 | |
| 4187 | // set p_ic according to p_ic, p_scs and pat for find_tags(). |
| 4188 | save_p_ic = p_ic; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4189 | p_ic = ignorecase(compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4190 | |
| 4191 | // Find up to TAG_MANY matches. Avoids that an enormous number |
| 4192 | // of matches is found when compl_pattern is empty |
| 4193 | g_tag_at_cursor = TRUE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4194 | if (find_tags(compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4195 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4196 | | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0), |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4197 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 4198 | ins_compl_add_matches(num_matches, matches, p_ic); |
| 4199 | g_tag_at_cursor = FALSE; |
| 4200 | p_ic = save_p_ic; |
| 4201 | } |
| 4202 | |
| 4203 | /* |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4204 | * Compare function for qsort |
| 4205 | */ |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4206 | static int |
| 4207 | compare_scores(const void *a, const void *b) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4208 | { |
| 4209 | int idx_a = *(const int *)a; |
| 4210 | int idx_b = *(const int *)b; |
| 4211 | int score_a = compl_fuzzy_scores[idx_a]; |
| 4212 | int score_b = compl_fuzzy_scores[idx_b]; |
zeertzjq | 58d7052 | 2024-08-31 17:05:39 +0200 | [diff] [blame] | 4213 | return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1)) |
| 4214 | : (score_a > score_b ? -1 : 1); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4218 | * insert prefix with redraw |
| 4219 | */ |
| 4220 | static void |
| 4221 | ins_compl_longest_insert(char_u *prefix) |
| 4222 | { |
| 4223 | ins_compl_delete(); |
| 4224 | ins_compl_insert_bytes(prefix + get_compl_len(), -1); |
| 4225 | ins_redraw(FALSE); |
| 4226 | } |
| 4227 | |
| 4228 | /* |
| 4229 | * Calculate the longest common prefix among the best fuzzy matches |
| 4230 | * stored in compl_best_matches, and insert it as the longest. |
| 4231 | */ |
| 4232 | static void |
| 4233 | fuzzy_longest_match(void) |
| 4234 | { |
| 4235 | char_u *prefix = NULL; |
| 4236 | int prefix_len = 0; |
| 4237 | int i = 0; |
| 4238 | int j = 0; |
| 4239 | char_u *match_str = NULL; |
| 4240 | char_u *prefix_ptr = NULL; |
| 4241 | char_u *match_ptr = NULL; |
| 4242 | char_u *leader = NULL; |
| 4243 | size_t leader_len = 0; |
| 4244 | compl_T *compl = NULL; |
| 4245 | int more_candidates = FALSE; |
| 4246 | compl_T *nn_compl = NULL; |
| 4247 | |
| 4248 | if (compl_num_bests == 0) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4249 | return; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4250 | |
| 4251 | nn_compl = compl_first_match->cp_next->cp_next; |
| 4252 | if (nn_compl && nn_compl != compl_first_match) |
| 4253 | more_candidates = TRUE; |
| 4254 | |
| 4255 | compl = ctrl_x_mode_whole_line() ? compl_first_match |
| 4256 | : compl_first_match->cp_next; |
| 4257 | if (compl_num_bests == 1) |
| 4258 | { |
| 4259 | // no more candidates insert the match str |
| 4260 | if (!more_candidates) |
| 4261 | { |
| 4262 | ins_compl_longest_insert(compl->cp_str.string); |
| 4263 | compl_num_bests = 0; |
| 4264 | } |
| 4265 | compl_num_bests = 0; |
| 4266 | return; |
| 4267 | } |
| 4268 | |
| 4269 | compl_best_matches = (compl_T **)alloc(compl_num_bests * sizeof(compl_T *)); |
| 4270 | if (compl_best_matches == NULL) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4271 | return; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4272 | while (compl != NULL && i < compl_num_bests) |
| 4273 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4274 | compl_best_matches[i] = compl; |
| 4275 | compl = compl->cp_next; |
| 4276 | i++; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | prefix = compl_best_matches[0]->cp_str.string; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4280 | prefix_len = (int)compl_best_matches[0]->cp_str.length; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4281 | |
| 4282 | for (i = 1; i < compl_num_bests; i++) |
| 4283 | { |
| 4284 | match_str = compl_best_matches[i]->cp_str.string; |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4285 | prefix_ptr = prefix; |
| 4286 | match_ptr = match_str; |
| 4287 | j = 0; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4288 | |
| 4289 | while (j < prefix_len && *match_ptr != NUL && *prefix_ptr != NUL) |
| 4290 | { |
| 4291 | if (STRNCMP(prefix_ptr, match_ptr, mb_ptr2len(prefix_ptr)) != 0) |
| 4292 | break; |
| 4293 | |
| 4294 | MB_PTR_ADV(prefix_ptr); |
| 4295 | MB_PTR_ADV(match_ptr); |
| 4296 | j++; |
| 4297 | } |
| 4298 | |
| 4299 | if (j > 0) |
| 4300 | prefix_len = j; |
| 4301 | } |
| 4302 | |
| 4303 | leader = ins_compl_leader(); |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4304 | leader_len = ins_compl_leader_len(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4305 | |
| 4306 | // skip non-consecutive prefixes |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4307 | if (leader_len > 0 && STRNCMP(prefix, leader, leader_len) != 0) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4308 | goto end; |
| 4309 | |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 4310 | prefix = vim_strnsave(prefix, prefix_len); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4311 | if (prefix != NULL) |
| 4312 | { |
| 4313 | ins_compl_longest_insert(prefix); |
| 4314 | compl_cfc_longest_ins = TRUE; |
| 4315 | vim_free(prefix); |
| 4316 | } |
| 4317 | |
| 4318 | end: |
| 4319 | vim_free(compl_best_matches); |
| 4320 | compl_best_matches = NULL; |
| 4321 | compl_num_bests = 0; |
| 4322 | } |
| 4323 | |
| 4324 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4325 | * Get the next set of filename matching "compl_pattern". |
| 4326 | */ |
| 4327 | static void |
| 4328 | get_next_filename_completion(void) |
| 4329 | { |
| 4330 | char_u **matches; |
| 4331 | int num_matches; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4332 | char_u *ptr; |
| 4333 | garray_T fuzzy_indices; |
| 4334 | int i; |
| 4335 | int score; |
| 4336 | char_u *leader = ins_compl_leader(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4337 | size_t leader_len = ins_compl_leader_len();; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4338 | int in_fuzzy_collect = (cfc_has_mode() && leader_len > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4339 | int *fuzzy_indices_data; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4340 | char_u *last_sep = NULL; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4341 | int need_collect_bests = in_fuzzy_collect && compl_get_longest; |
| 4342 | int max_score = 0; |
| 4343 | int current_score = 0; |
| 4344 | int dir = compl_direction; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4345 | |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4346 | #ifdef BACKSLASH_IN_FILENAME |
| 4347 | char pathsep = (curbuf->b_p_csl[0] == 's') ? |
| 4348 | '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP; |
| 4349 | #else |
| 4350 | char pathsep = PATHSEP; |
| 4351 | #endif |
| 4352 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4353 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4354 | { |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4355 | #ifdef BACKSLASH_IN_FILENAME |
| 4356 | if (curbuf->b_p_csl[0] == 's') |
| 4357 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4358 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4359 | { |
| 4360 | if (leader[i] == '\\') |
| 4361 | leader[i] = '/'; |
| 4362 | } |
| 4363 | } |
| 4364 | else if (curbuf->b_p_csl[0] == 'b') |
| 4365 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4366 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 4367 | { |
| 4368 | if (leader[i] == '/') |
| 4369 | leader[i] = '\\'; |
| 4370 | } |
| 4371 | } |
| 4372 | #endif |
| 4373 | last_sep = vim_strrchr(leader, pathsep); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4374 | if (last_sep == NULL) |
| 4375 | { |
| 4376 | // No path separator or separator is the last character, |
| 4377 | // fuzzy match the whole leader |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4378 | VIM_CLEAR_STRING(compl_pattern); |
| 4379 | compl_pattern.string = vim_strnsave((char_u *)"*", 1); |
| 4380 | if (compl_pattern.string == NULL) |
| 4381 | return; |
| 4382 | compl_pattern.length = 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4383 | } |
| 4384 | else if (*(last_sep + 1) == '\0') |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4385 | in_fuzzy_collect = FALSE; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4386 | else |
| 4387 | { |
| 4388 | // Split leader into path and file parts |
| 4389 | int path_len = last_sep - leader + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4390 | char_u *path_with_wildcard; |
| 4391 | |
| 4392 | path_with_wildcard = alloc(path_len + 2); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4393 | if (path_with_wildcard != NULL) |
| 4394 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4395 | vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader); |
| 4396 | VIM_CLEAR_STRING(compl_pattern); |
| 4397 | compl_pattern.string = path_with_wildcard; |
| 4398 | compl_pattern.length = path_len + 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4399 | |
| 4400 | // Move leader to the file part |
| 4401 | leader = last_sep + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4402 | leader_len -= path_len; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 4403 | } |
| 4404 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4405 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4406 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4407 | if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4408 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK) |
| 4409 | return; |
| 4410 | |
| 4411 | // May change home directory back to "~". |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4412 | tilde_replace(compl_pattern.string, num_matches, matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4413 | #ifdef BACKSLASH_IN_FILENAME |
| 4414 | if (curbuf->b_p_csl[0] != NUL) |
| 4415 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4416 | for (i = 0; i < num_matches; ++i) |
| 4417 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4418 | ptr = matches[i]; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4419 | while (*ptr != NUL) |
| 4420 | { |
| 4421 | if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') |
| 4422 | *ptr = '/'; |
| 4423 | else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') |
| 4424 | *ptr = '\\'; |
| 4425 | ptr += (*mb_ptr2len)(ptr); |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | #endif |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4430 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4431 | if (in_fuzzy_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4432 | { |
| 4433 | ga_init2(&fuzzy_indices, sizeof(int), 10); |
| 4434 | compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches); |
| 4435 | |
| 4436 | for (i = 0; i < num_matches; i++) |
| 4437 | { |
| 4438 | ptr = matches[i]; |
| 4439 | score = fuzzy_match_str(ptr, leader); |
| 4440 | if (score > 0) |
| 4441 | { |
| 4442 | if (ga_grow(&fuzzy_indices, 1) == OK) |
| 4443 | { |
| 4444 | ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i; |
| 4445 | compl_fuzzy_scores[i] = score; |
| 4446 | fuzzy_indices.ga_len++; |
| 4447 | } |
| 4448 | } |
| 4449 | } |
| 4450 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4451 | // prevent qsort from deref NULL pointer |
| 4452 | if (fuzzy_indices.ga_len > 0) |
| 4453 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4454 | char_u *match = NULL; |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4455 | fuzzy_indices_data = (int *)fuzzy_indices.ga_data; |
| 4456 | qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4457 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4458 | for (i = 0; i < fuzzy_indices.ga_len; ++i) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4459 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4460 | match = matches[fuzzy_indices_data[i]]; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4461 | current_score = compl_fuzzy_scores[fuzzy_indices_data[i]]; |
| 4462 | if (ins_compl_add(match, -1, NULL, NULL, NULL, dir, |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4463 | CP_FAST | ((p_fic || p_wic) ? CP_ICASE : 0), |
| 4464 | FALSE, NULL, current_score) == OK) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4465 | dir = FORWARD; |
| 4466 | |
| 4467 | if (need_collect_bests) |
| 4468 | { |
| 4469 | if (i == 0 || current_score == max_score) |
| 4470 | { |
| 4471 | compl_num_bests++; |
| 4472 | max_score = current_score; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4476 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4477 | FreeWild(num_matches, matches); |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4478 | } |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4479 | else if (leader_len > 0) |
| 4480 | { |
| 4481 | FreeWild(num_matches, matches); |
| 4482 | num_matches = 0; |
| 4483 | } |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 4484 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4485 | vim_free(compl_fuzzy_scores); |
| 4486 | ga_clear(&fuzzy_indices); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4487 | |
| 4488 | if (compl_num_bests > 0 && compl_get_longest) |
| 4489 | fuzzy_longest_match(); |
| 4490 | return; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4491 | } |
| 4492 | |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 4493 | if (num_matches > 0) |
| 4494 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /* |
| 4498 | * Get the next set of command-line completions matching "compl_pattern". |
| 4499 | */ |
| 4500 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 4501 | get_next_cmdline_completion(void) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4502 | { |
| 4503 | char_u **matches; |
| 4504 | int num_matches; |
| 4505 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4506 | if (expand_cmdline(&compl_xp, compl_pattern.string, |
| 4507 | (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4508 | ins_compl_add_matches(num_matches, matches, FALSE); |
| 4509 | } |
| 4510 | |
| 4511 | /* |
| 4512 | * Get the next set of spell suggestions matching "compl_pattern". |
| 4513 | */ |
| 4514 | static void |
| 4515 | get_next_spell_completion(linenr_T lnum UNUSED) |
| 4516 | { |
| 4517 | #ifdef FEAT_SPELL |
| 4518 | char_u **matches; |
| 4519 | int num_matches; |
| 4520 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4521 | num_matches = expand_spelling(lnum, compl_pattern.string, &matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4522 | if (num_matches > 0) |
| 4523 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 8e7cc6b | 2021-12-30 10:32:25 +0000 | [diff] [blame] | 4524 | else |
| 4525 | vim_free(matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4526 | #endif |
| 4527 | } |
| 4528 | |
| 4529 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4530 | * Return the next word or line from buffer "ins_buf" at position |
| 4531 | * "cur_match_pos" for completion. The length of the match is set in "len". |
| 4532 | */ |
| 4533 | static char_u * |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 4534 | ins_compl_get_next_word_or_line( |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4535 | buf_T *ins_buf, // buffer being scanned |
| 4536 | pos_T *cur_match_pos, // current match position |
| 4537 | int *match_len, |
| 4538 | int *cont_s_ipos) // next ^X<> will set initial_pos |
| 4539 | { |
| 4540 | char_u *ptr; |
| 4541 | int len; |
| 4542 | |
| 4543 | *match_len = 0; |
| 4544 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) + |
| 4545 | cur_match_pos->col; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4546 | 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] | 4547 | if (ctrl_x_mode_line_or_eval()) |
| 4548 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4549 | if (compl_status_adding()) |
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 | return NULL; |
| 4553 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4554 | len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4555 | if (!p_paste) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4556 | { |
| 4557 | char_u *tmp_ptr = ptr; |
| 4558 | |
| 4559 | ptr = skipwhite(tmp_ptr); |
| 4560 | len -= (int)(ptr - tmp_ptr); |
| 4561 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4562 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4563 | } |
| 4564 | else |
| 4565 | { |
| 4566 | char_u *tmp_ptr = ptr; |
| 4567 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4568 | if (compl_status_adding() && compl_length <= len) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4569 | { |
| 4570 | tmp_ptr += compl_length; |
| 4571 | // Skip if already inside a word. |
| 4572 | if (vim_iswordp(tmp_ptr)) |
| 4573 | return NULL; |
| 4574 | // Find start of next word. |
| 4575 | tmp_ptr = find_word_start(tmp_ptr); |
| 4576 | } |
| 4577 | // Find end of this word. |
| 4578 | tmp_ptr = find_word_end(tmp_ptr); |
| 4579 | len = (int)(tmp_ptr - ptr); |
| 4580 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4581 | if (compl_status_adding() && len == compl_length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4582 | { |
| 4583 | if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4584 | { |
| 4585 | // Try next line, if any. the new word will be |
| 4586 | // "join" as if the normal command "J" was used. |
| 4587 | // IOSIZE is always greater than |
| 4588 | // compl_length, so the next STRNCPY always |
| 4589 | // works -- Acevedo |
| 4590 | STRNCPY(IObuff, ptr, len); |
| 4591 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
| 4592 | tmp_ptr = ptr = skipwhite(ptr); |
| 4593 | // Find start of next word. |
| 4594 | tmp_ptr = find_word_start(tmp_ptr); |
| 4595 | // Find end of next word. |
| 4596 | tmp_ptr = find_word_end(tmp_ptr); |
| 4597 | if (tmp_ptr > ptr) |
| 4598 | { |
| 4599 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
| 4600 | { |
| 4601 | if (IObuff[len - 1] != ' ') |
| 4602 | IObuff[len++] = ' '; |
| 4603 | // IObuf =~ "\k.* ", thus len >= 2 |
| 4604 | if (p_js |
| 4605 | && (IObuff[len - 2] == '.' |
| 4606 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4607 | == NULL |
| 4608 | && (IObuff[len - 2] == '?' |
| 4609 | || IObuff[len - 2] == '!')))) |
| 4610 | IObuff[len++] = ' '; |
| 4611 | } |
| 4612 | // copy as much as possible of the new word |
| 4613 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4614 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4615 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4616 | len += (int)(tmp_ptr - ptr); |
| 4617 | *cont_s_ipos = TRUE; |
| 4618 | } |
| 4619 | IObuff[len] = NUL; |
| 4620 | ptr = IObuff; |
| 4621 | } |
| 4622 | if (len == compl_length) |
| 4623 | return NULL; |
| 4624 | } |
| 4625 | } |
| 4626 | |
| 4627 | *match_len = len; |
| 4628 | return ptr; |
| 4629 | } |
| 4630 | |
| 4631 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4632 | * Get the next set of words matching "compl_pattern" for default completion(s) |
| 4633 | * (normal ^P/^N and ^X^L). |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4634 | * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the |
| 4635 | * position "st->start_pos" in the "compl_direction" direction. If |
| 4636 | * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and |
| 4637 | * "st->last_match_pos". |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4638 | * Returns OK if a new next match is found, otherwise returns FAIL. |
| 4639 | */ |
| 4640 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4641 | 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] | 4642 | { |
| 4643 | int found_new_match = FAIL; |
| 4644 | int save_p_scs; |
| 4645 | int save_p_ws; |
| 4646 | int looped_around = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4647 | char_u *ptr = NULL; |
| 4648 | int len = 0; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4649 | int in_collect = (cfc_has_mode() && compl_length > 0); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4650 | char_u *leader = ins_compl_leader(); |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4651 | int score = 0; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4652 | int in_curbuf = st->ins_buf == curbuf; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4653 | |
| 4654 | // If 'infercase' is set, don't use 'smartcase' here |
| 4655 | save_p_scs = p_scs; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4656 | if (st->ins_buf->b_p_inf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4657 | p_scs = FALSE; |
| 4658 | |
| 4659 | // Buffers other than curbuf are scanned from the beginning or the |
| 4660 | // end but never from the middle, thus setting nowrapscan in this |
| 4661 | // buffer is a good idea, on the other hand, we always set |
| 4662 | // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb |
| 4663 | save_p_ws = p_ws; |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4664 | if (!in_curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4665 | p_ws = FALSE; |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4666 | else if (*st->e_cpt == '.') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4667 | p_ws = TRUE; |
| 4668 | looped_around = FALSE; |
| 4669 | for (;;) |
| 4670 | { |
| 4671 | int cont_s_ipos = FALSE; |
| 4672 | |
| 4673 | ++msg_silent; // Don't want messages for wrapscan. |
| 4674 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4675 | if (in_collect) |
| 4676 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 4677 | found_new_match = search_for_fuzzy_match(st->ins_buf, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4678 | st->cur_match_pos, leader, compl_direction, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4679 | start_pos, &len, &ptr, &score); |
| 4680 | } |
| 4681 | // ctrl_x_mode_line_or_eval() || word-wise search that |
| 4682 | // has added a word that was at the beginning of the line |
| 4683 | else if (ctrl_x_mode_whole_line() || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL)) |
| 4684 | found_new_match = search_for_exact_line(st->ins_buf, |
| 4685 | st->cur_match_pos, compl_direction, compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4686 | else |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4687 | found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4688 | NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4689 | 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4690 | --msg_silent; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4691 | if (!compl_started || st->set_match_pos) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4692 | { |
| 4693 | // set "compl_started" even on fail |
| 4694 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4695 | st->first_match_pos = *st->cur_match_pos; |
| 4696 | st->last_match_pos = *st->cur_match_pos; |
| 4697 | st->set_match_pos = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4698 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4699 | else if (st->first_match_pos.lnum == st->last_match_pos.lnum |
| 4700 | && st->first_match_pos.col == st->last_match_pos.col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4701 | { |
| 4702 | found_new_match = FAIL; |
| 4703 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4704 | else if (compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4705 | && (st->prev_match_pos.lnum > st->cur_match_pos->lnum |
| 4706 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4707 | && st->prev_match_pos.col >= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4708 | { |
| 4709 | if (looped_around) |
| 4710 | found_new_match = FAIL; |
| 4711 | else |
| 4712 | looped_around = TRUE; |
| 4713 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4714 | else if (!compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4715 | && (st->prev_match_pos.lnum < st->cur_match_pos->lnum |
| 4716 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4717 | && st->prev_match_pos.col <= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4718 | { |
| 4719 | if (looped_around) |
| 4720 | found_new_match = FAIL; |
| 4721 | else |
| 4722 | looped_around = TRUE; |
| 4723 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4724 | st->prev_match_pos = *st->cur_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4725 | if (found_new_match == FAIL) |
| 4726 | break; |
| 4727 | |
| 4728 | // when ADDING, the text before the cursor matches, skip it |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4729 | if (compl_status_adding() && in_curbuf |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4730 | && start_pos->lnum == st->cur_match_pos->lnum |
| 4731 | && start_pos->col == st->cur_match_pos->col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4732 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4733 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4734 | if (!in_collect) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4735 | ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos, |
| 4736 | &len, &cont_s_ipos); |
glepnir | bfb4eea | 2025-01-31 15:28:29 +0100 | [diff] [blame] | 4737 | 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] | 4738 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4739 | |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4740 | if (is_nearest_active() && in_curbuf) |
| 4741 | { |
| 4742 | score = st->cur_match_pos->lnum - curwin->w_cursor.lnum; |
| 4743 | if (score < 0) |
| 4744 | score = -score; |
| 4745 | score++; |
| 4746 | } |
| 4747 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4748 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Girish Palya | b156588 | 2025-04-15 20:16:00 +0200 | [diff] [blame] | 4749 | in_curbuf ? NULL : st->ins_buf->b_sfname, |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4750 | 0, cont_s_ipos, score) != NOTDONE) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4751 | { |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 4752 | if (in_collect && score == compl_first_match->cp_next->cp_score) |
| 4753 | compl_num_bests++; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4754 | found_new_match = OK; |
| 4755 | break; |
| 4756 | } |
| 4757 | } |
| 4758 | p_scs = save_p_scs; |
| 4759 | p_ws = save_p_ws; |
| 4760 | |
| 4761 | return found_new_match; |
| 4762 | } |
| 4763 | |
| 4764 | /* |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4765 | * Return the callback function associated with "funcname". |
| 4766 | */ |
| 4767 | #ifdef FEAT_COMPL_FUNC |
| 4768 | static callback_T * |
| 4769 | get_cpt_func_callback(char_u *funcname) |
| 4770 | { |
| 4771 | static callback_T cb; |
| 4772 | char_u buf[LSIZE]; |
| 4773 | int slen; |
| 4774 | |
| 4775 | slen = copy_option_part(&funcname, buf, LSIZE, ","); |
| 4776 | if (slen > 0 && option_set_callback_func(buf, &cb)) |
| 4777 | return &cb; |
| 4778 | return NULL; |
| 4779 | } |
| 4780 | |
| 4781 | /* |
| 4782 | * Retrieve new completion matches by invoking callback "cb". |
| 4783 | */ |
| 4784 | static void |
| 4785 | expand_cpt_function(callback_T *cb) |
| 4786 | { |
| 4787 | // Re-insert the text removed by ins_compl_delete(). |
| 4788 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
| 4789 | // Get matches |
| 4790 | get_cpt_func_completion_matches(cb); |
| 4791 | // Undo insertion |
| 4792 | ins_compl_delete(); |
| 4793 | } |
| 4794 | #endif |
| 4795 | |
| 4796 | /* |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 4797 | * Get completion matches from register contents. |
| 4798 | * Extracts words from all available registers and adds them to the completion list. |
| 4799 | */ |
| 4800 | static void |
| 4801 | get_register_completion(void) |
| 4802 | { |
| 4803 | int dir = compl_direction; |
| 4804 | yankreg_T *reg = NULL; |
| 4805 | void *reg_ptr = NULL; |
| 4806 | |
| 4807 | for (int i = 0; i < NUM_REGISTERS; i++) |
| 4808 | { |
| 4809 | int regname = 0; |
| 4810 | |
| 4811 | if (i == 0) |
| 4812 | regname = '"'; // unnamed register |
| 4813 | else if (i < 10) |
| 4814 | regname = '0' + i; |
| 4815 | else if (i == DELETION_REGISTER) |
| 4816 | regname = '-'; |
| 4817 | #ifdef FEAT_CLIPBOARD |
| 4818 | else if (i == STAR_REGISTER) |
| 4819 | regname = '*'; |
| 4820 | else if (i == PLUS_REGISTER) |
| 4821 | regname = '+'; |
| 4822 | #endif |
| 4823 | else |
| 4824 | regname = 'a' + i - 10; |
| 4825 | |
| 4826 | // Skip invalid or black hole register |
| 4827 | if (!valid_yank_reg(regname, FALSE) || regname == '_') |
| 4828 | continue; |
| 4829 | |
| 4830 | reg_ptr = get_register(regname, FALSE); |
| 4831 | if (reg_ptr == NULL) |
| 4832 | continue; |
| 4833 | |
| 4834 | reg = (yankreg_T *)reg_ptr; |
| 4835 | |
| 4836 | for (int j = 0; j < reg->y_size; j++) |
| 4837 | { |
| 4838 | char_u *str = reg->y_array[j].string; |
| 4839 | if (str == NULL) |
| 4840 | continue; |
| 4841 | |
| 4842 | char_u *p = str; |
| 4843 | while (*p != NUL) |
| 4844 | { |
| 4845 | p = find_word_start(p); |
| 4846 | if (*p == NUL) |
| 4847 | break; |
| 4848 | |
| 4849 | char_u *word_end = find_word_end(p); |
| 4850 | |
| 4851 | // Add the word to the completion list |
| 4852 | int len = (int)(word_end - p); |
| 4853 | if (len > 0 && (!compl_orig_text.string |
| 4854 | || (p_ic ? STRNICMP(p, compl_orig_text.string, |
| 4855 | compl_orig_text.length) == 0 |
| 4856 | : STRNCMP(p, compl_orig_text.string, |
| 4857 | compl_orig_text.length) == 0))) |
| 4858 | { |
| 4859 | if (ins_compl_add_infercase(p, len, p_ic, NULL, |
| 4860 | dir, FALSE, 0) == OK) |
| 4861 | dir = FORWARD; |
| 4862 | } |
| 4863 | |
| 4864 | p = word_end; |
| 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | // Free the register copy |
| 4869 | put_register(regname, reg_ptr); |
| 4870 | } |
| 4871 | } |
| 4872 | |
| 4873 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4874 | * get the next set of completion matches for "type". |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4875 | * Returns TRUE if a new match is found. Otherwise returns FALSE. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4876 | */ |
| 4877 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4878 | 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] | 4879 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4880 | int found_new_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4881 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4882 | switch (type) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4883 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4884 | case -1: |
| 4885 | break; |
| 4886 | #ifdef FEAT_FIND_ID |
| 4887 | case CTRL_X_PATH_PATTERNS: |
| 4888 | case CTRL_X_PATH_DEFINES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4889 | get_next_include_file_completion(type); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4890 | break; |
| 4891 | #endif |
| 4892 | |
| 4893 | case CTRL_X_DICTIONARY: |
| 4894 | case CTRL_X_THESAURUS: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4895 | get_next_dict_tsr_completion(type, st->dict, st->dict_f); |
| 4896 | st->dict = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4897 | break; |
| 4898 | |
| 4899 | case CTRL_X_TAGS: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4900 | get_next_tag_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4901 | break; |
| 4902 | |
| 4903 | case CTRL_X_FILES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4904 | get_next_filename_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4905 | break; |
| 4906 | |
| 4907 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 4908 | case CTRL_X_CMDLINE_CTRL_X: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4909 | get_next_cmdline_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4910 | break; |
| 4911 | |
| 4912 | #ifdef FEAT_COMPL_FUNC |
| 4913 | case CTRL_X_FUNCTION: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4914 | if (ctrl_x_mode_normal()) // Invoked by a func in 'cpt' option |
| 4915 | expand_cpt_function(st->func_cb); |
| 4916 | else |
| 4917 | expand_by_function(type, compl_pattern.string, NULL); |
| 4918 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4919 | case CTRL_X_OMNI: |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 4920 | expand_by_function(type, compl_pattern.string, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4921 | break; |
| 4922 | #endif |
| 4923 | |
| 4924 | case CTRL_X_SPELL: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4925 | get_next_spell_completion(st->first_match_pos.lnum); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4926 | break; |
| 4927 | |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 4928 | case CTRL_X_REGISTER: |
| 4929 | get_register_completion(); |
| 4930 | break; |
| 4931 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4932 | default: // normal ^P/^N and ^X^L |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4933 | found_new_match = get_next_default_completion(st, ini); |
| 4934 | if (found_new_match == FAIL && st->ins_buf == curbuf) |
| 4935 | st->found_all = TRUE; |
| 4936 | } |
| 4937 | |
| 4938 | // check if compl_curr_match has changed, (e.g. other type of |
| 4939 | // expansion added something) |
| 4940 | if (type != 0 && compl_curr_match != compl_old_match) |
| 4941 | found_new_match = OK; |
| 4942 | |
| 4943 | return found_new_match; |
| 4944 | } |
| 4945 | |
| 4946 | /* |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 4947 | * Strips carets followed by numbers. This suffix typically represents the |
| 4948 | * max_matches setting. |
| 4949 | */ |
| 4950 | static void |
| 4951 | strip_caret_numbers_in_place(char_u *str) |
| 4952 | { |
| 4953 | char_u *read = str, *write = str, *p; |
| 4954 | |
| 4955 | if (str == NULL) |
| 4956 | return; |
| 4957 | |
| 4958 | while (*read) |
| 4959 | { |
| 4960 | if (*read == '^') |
| 4961 | { |
| 4962 | p = read + 1; |
| 4963 | while (vim_isdigit(*p)) |
| 4964 | p++; |
| 4965 | if ((*p == ',' || *p == '\0') && p != read + 1) |
| 4966 | { |
| 4967 | read = p; |
| 4968 | continue; |
| 4969 | } |
| 4970 | else |
| 4971 | *write++ = *read++; |
| 4972 | } |
| 4973 | else |
| 4974 | *write++ = *read++; |
| 4975 | } |
| 4976 | *write = '\0'; |
| 4977 | } |
| 4978 | |
| 4979 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4980 | * Get the next expansion(s), using "compl_pattern". |
| 4981 | * The search starts at position "ini" in curbuf and in the direction |
| 4982 | * compl_direction. |
| 4983 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4984 | * where we stopped searching before. |
| 4985 | * This may return before finding all the matches. |
| 4986 | * Return the total number of matches or -1 if still unknown -- Acevedo |
| 4987 | */ |
| 4988 | static int |
| 4989 | ins_compl_get_exp(pos_T *ini) |
| 4990 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4991 | static ins_compl_next_state_T st; |
| 4992 | static int st_cleared = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4993 | int i; |
| 4994 | int found_new_match; |
| 4995 | int type = ctrl_x_mode; |
| 4996 | |
| 4997 | if (!compl_started) |
| 4998 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4999 | buf_T *buf; |
| 5000 | |
| 5001 | FOR_ALL_BUFFERS(buf) |
| 5002 | buf->b_scanned = 0; |
| 5003 | if (!st_cleared) |
| 5004 | { |
| 5005 | CLEAR_FIELD(st); |
| 5006 | st_cleared = TRUE; |
| 5007 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5008 | st.found_all = FALSE; |
| 5009 | st.ins_buf = curbuf; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5010 | vim_free(st.e_cpt_copy); |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 5011 | // Make a copy of 'complete', in case the buffer is wiped out. |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5012 | st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL) |
| 5013 | ? (char_u *)"." : curbuf->b_p_cpt); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5014 | strip_caret_numbers_in_place(st.e_cpt_copy); |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5015 | 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] | 5016 | st.last_match_pos = st.first_match_pos = *ini; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5017 | |
| 5018 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5019 | && !cpt_sources_init()) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5020 | return FAIL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5021 | } |
| 5022 | else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf)) |
| 5023 | st.ins_buf = curbuf; // In case the buffer was wiped out. |
| 5024 | |
| 5025 | compl_old_match = compl_curr_match; // remember the last current match |
Christian Brabandt | 13032a4 | 2024-07-28 21:16:48 +0200 | [diff] [blame] | 5026 | st.cur_match_pos = (compl_dir_forward()) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5027 | ? &st.last_match_pos : &st.first_match_pos; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5028 | |
| 5029 | // For ^N/^P loop over all the flags/windows/buffers in 'complete'. |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5030 | for (cpt_sources_index = 0;;) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5031 | { |
| 5032 | found_new_match = FAIL; |
| 5033 | st.set_match_pos = FALSE; |
| 5034 | |
| 5035 | // For ^N/^P pick a new entry from e_cpt if compl_started is off, |
| 5036 | // or if found_all says this entry is done. For ^X^L only use the |
| 5037 | // entries from 'complete' that look in loaded buffers. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5038 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5039 | && (!compl_started || st.found_all)) |
| 5040 | { |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 5041 | int status = process_next_cpt_value(&st, &type, ini, cfc_has_mode()); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (status == INS_COMPL_CPT_END) |
| 5044 | break; |
| 5045 | if (status == INS_COMPL_CPT_CONT) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5046 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5047 | cpt_sources_index++; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5048 | continue; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5049 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5052 | // If complete() was called then compl_pattern has been reset. The |
| 5053 | // following won't work then, bail out. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5054 | if (compl_pattern.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5055 | break; |
| 5056 | |
| 5057 | // get the next set of completion matches |
| 5058 | found_new_match = get_next_completion_match(type, &st, ini); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5059 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5060 | if (type > 0) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5061 | cpt_sources_index++; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 5062 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5063 | // break the loop for specialized modes (use 'complete' just for the |
| 5064 | // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new |
| 5065 | // match |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5066 | if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval()) |
| 5067 | || found_new_match != FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5068 | { |
| 5069 | if (got_int) |
| 5070 | break; |
| 5071 | // Fill the popup menu as soon as possible. |
| 5072 | if (type != -1) |
| 5073 | ins_compl_check_keys(0, FALSE); |
| 5074 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5075 | if ((ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5076 | && !ctrl_x_mode_line_or_eval()) || compl_interrupted) |
| 5077 | break; |
| 5078 | compl_started = TRUE; |
| 5079 | } |
| 5080 | else |
| 5081 | { |
| 5082 | // Mark a buffer scanned when it has been scanned completely |
Christian Brabandt | ee9166e | 2023-09-03 21:24:33 +0200 | [diff] [blame] | 5083 | 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] | 5084 | st.ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5085 | |
| 5086 | compl_started = FALSE; |
| 5087 | } |
| 5088 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5089 | cpt_sources_index = -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5090 | compl_started = TRUE; |
| 5091 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5092 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 5093 | && *st.e_cpt == NUL) // Got to end of 'complete' |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5094 | found_new_match = FAIL; |
| 5095 | |
| 5096 | i = -1; // total of matches, unknown |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5097 | if (found_new_match == FAIL || (ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5098 | && !ctrl_x_mode_line_or_eval())) |
| 5099 | i = ins_compl_make_cyclic(); |
| 5100 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 5101 | if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0) |
| 5102 | fuzzy_longest_match(); |
| 5103 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5104 | if (compl_old_match != NULL) |
| 5105 | { |
| 5106 | // If several matches were added (FORWARD) or the search failed and has |
| 5107 | // just been made cyclic then we have to move compl_curr_match to the |
| 5108 | // next or previous entry (if any) -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5109 | compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5110 | : compl_old_match->cp_prev; |
| 5111 | if (compl_curr_match == NULL) |
| 5112 | compl_curr_match = compl_old_match; |
| 5113 | } |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 5114 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 5115 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5116 | return i; |
| 5117 | } |
| 5118 | |
| 5119 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5120 | * Update "compl_shown_match" to the actually shown match, it may differ when |
| 5121 | * "compl_leader" is used to omit some of the matches. |
| 5122 | */ |
| 5123 | static void |
| 5124 | ins_compl_update_shown_match(void) |
| 5125 | { |
| 5126 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5127 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5128 | && compl_shown_match->cp_next != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5129 | && !is_first_match(compl_shown_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5130 | compl_shown_match = compl_shown_match->cp_next; |
| 5131 | |
| 5132 | // If we didn't find it searching forward, and compl_shows_dir is |
| 5133 | // backward, find the last match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5134 | if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5135 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5136 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5137 | && (compl_shown_match->cp_next == NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5138 | || is_first_match(compl_shown_match->cp_next))) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5139 | { |
| 5140 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5141 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5142 | && compl_shown_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5143 | && !is_first_match(compl_shown_match->cp_prev)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5144 | compl_shown_match = compl_shown_match->cp_prev; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5149 | * Delete the old text being completed. |
| 5150 | */ |
| 5151 | void |
| 5152 | ins_compl_delete(void) |
| 5153 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5154 | // In insert mode: Delete the typed part. |
| 5155 | // In replace mode: Put the old characters back, if any. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5156 | int col = compl_col + (compl_status_adding() ? compl_length : 0); |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5157 | string_T remaining = {NULL, 0}; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5158 | int orig_col; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5159 | int has_preinsert = ins_compl_preinsert_effect(); |
| 5160 | if (has_preinsert) |
| 5161 | { |
Yegappan Lakshmanan | 7b6add0 | 2025-04-01 20:38:37 +0200 | [diff] [blame] | 5162 | col += (int)ins_compl_leader_len(); |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5163 | curwin->w_cursor.col = compl_ins_end_col; |
| 5164 | } |
| 5165 | |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5166 | if (curwin->w_cursor.lnum > compl_lnum) |
| 5167 | { |
| 5168 | if (curwin->w_cursor.col < ml_get_curline_len()) |
| 5169 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5170 | char_u *line = ml_get_cursor(); |
| 5171 | remaining.length = ml_get_cursor_len(); |
| 5172 | remaining.string = vim_strnsave(line, remaining.length); |
| 5173 | if (remaining.string == NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5174 | return; |
| 5175 | } |
| 5176 | while (curwin->w_cursor.lnum > compl_lnum) |
| 5177 | { |
| 5178 | if (ml_delete(curwin->w_cursor.lnum) == FAIL) |
| 5179 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5180 | if (remaining.string) |
| 5181 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5182 | return; |
| 5183 | } |
zeertzjq | 060e655 | 2025-02-21 20:06:26 +0100 | [diff] [blame] | 5184 | deleted_lines_mark(curwin->w_cursor.lnum, 1L); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5185 | curwin->w_cursor.lnum--; |
| 5186 | } |
| 5187 | // move cursor to end of line |
| 5188 | curwin->w_cursor.col = ml_get_curline_len(); |
| 5189 | } |
| 5190 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5191 | if ((int)curwin->w_cursor.col > col) |
| 5192 | { |
| 5193 | if (stop_arrow() == FAIL) |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 5194 | { |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5195 | if (remaining.string) |
| 5196 | vim_free(remaining.string); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5197 | return; |
glepnir | e3647c8 | 2025-02-10 21:16:32 +0100 | [diff] [blame] | 5198 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5199 | backspace_until_column(col); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 5200 | compl_ins_end_col = curwin->w_cursor.col; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5201 | } |
| 5202 | |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5203 | if (remaining.string != NULL) |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5204 | { |
| 5205 | orig_col = curwin->w_cursor.col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5206 | ins_str(remaining.string, remaining.length); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5207 | curwin->w_cursor.col = orig_col; |
John Marriott | f4b3641 | 2025-02-23 09:09:59 +0100 | [diff] [blame] | 5208 | vim_free(remaining.string); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5209 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5210 | // TODO: is this sufficient for redrawing? Redrawing everything causes |
| 5211 | // flicker, thus we can't do that. |
| 5212 | changed_cline_bef_curs(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5213 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5214 | // clear v:completed_item |
| 5215 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5216 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | /* |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5220 | * Insert a completion string that contains newlines. |
| 5221 | * The string is split and inserted line by line. |
| 5222 | */ |
| 5223 | static void |
| 5224 | ins_compl_expand_multiple(char_u *str) |
| 5225 | { |
| 5226 | char_u *start = str; |
| 5227 | char_u *curr = str; |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 5228 | int base_indent = get_indent(); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5229 | |
| 5230 | while (*curr != NUL) |
| 5231 | { |
| 5232 | if (*curr == '\n') |
| 5233 | { |
| 5234 | // Insert the text chunk before newline |
| 5235 | if (curr > start) |
| 5236 | ins_char_bytes(start, (int)(curr - start)); |
| 5237 | |
| 5238 | // Handle newline |
glepnir | 5090a1f | 2025-02-24 19:10:37 +0100 | [diff] [blame] | 5239 | open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL); |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5240 | start = curr + 1; |
| 5241 | } |
| 5242 | curr++; |
| 5243 | } |
| 5244 | |
| 5245 | // Handle remaining text after last newline (if any) |
| 5246 | if (curr > start) |
| 5247 | ins_char_bytes(start, (int)(curr - start)); |
| 5248 | |
| 5249 | compl_ins_end_col = curwin->w_cursor.col; |
| 5250 | } |
| 5251 | |
| 5252 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5253 | * Insert the new text being completed. |
| 5254 | * "in_compl_func" is TRUE when called from complete_check(). |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5255 | * "move_cursor" is used when 'completeopt' includes "preinsert" and when TRUE |
| 5256 | * 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] | 5257 | */ |
| 5258 | void |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5259 | ins_compl_insert(int in_compl_func, int move_cursor) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5260 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5261 | int compl_len = get_compl_len(); |
| 5262 | int preinsert = ins_compl_has_preinsert(); |
| 5263 | char_u *cp_str = compl_shown_match->cp_str.string; |
| 5264 | size_t cp_str_len = compl_shown_match->cp_str.length; |
| 5265 | size_t leader_len = ins_compl_leader_len(); |
| 5266 | char_u *has_multiple = vim_strchr(cp_str, '\n'); |
Bram Moolenaar | 4b28ba3 | 2021-12-27 19:28:37 +0000 | [diff] [blame] | 5267 | |
| 5268 | // Make sure we don't go over the end of the string, this can happen with |
| 5269 | // illegal bytes. |
zeertzjq | 8297e2c | 2025-01-30 11:04:47 +0100 | [diff] [blame] | 5270 | if (compl_len < (int)cp_str_len) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5271 | { |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 5272 | if (has_multiple) |
| 5273 | ins_compl_expand_multiple(cp_str + compl_len); |
| 5274 | else |
| 5275 | { |
| 5276 | ins_compl_insert_bytes(cp_str + compl_len, -1); |
| 5277 | if (preinsert && move_cursor) |
| 5278 | curwin->w_cursor.col -= (colnr_T)(cp_str_len - leader_len); |
| 5279 | } |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5280 | } |
| 5281 | if (match_at_original_text(compl_shown_match) || preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5282 | compl_used_match = FALSE; |
| 5283 | else |
| 5284 | compl_used_match = TRUE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 5285 | #ifdef FEAT_EVAL |
| 5286 | { |
| 5287 | dict_T *dict = ins_compl_dict_alloc(compl_shown_match); |
| 5288 | |
| 5289 | set_vim_var_dict(VV_COMPLETED_ITEM, dict); |
| 5290 | } |
| 5291 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5292 | if (!in_compl_func) |
| 5293 | compl_curr_match = compl_shown_match; |
| 5294 | } |
| 5295 | |
| 5296 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5297 | * show the file name for the completion match (if any). Truncate the file |
| 5298 | * name to avoid a wait for return. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5299 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5300 | static void |
| 5301 | ins_compl_show_filename(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5302 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5303 | char *lead = _("match in file"); |
| 5304 | int space = sc_col - vim_strsize((char_u *)lead) - 2; |
| 5305 | char_u *s; |
| 5306 | char_u *e; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5307 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5308 | if (space <= 0) |
| 5309 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5310 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5311 | // We need the tail that fits. With double-byte encoding going |
| 5312 | // back from the end is very slow, thus go from the start and keep |
| 5313 | // the text that fits in "space" between "s" and "e". |
| 5314 | 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] | 5315 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5316 | space -= ptr2cells(e); |
| 5317 | while (space < 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5318 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5319 | space += ptr2cells(s); |
| 5320 | MB_PTR_ADV(s); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5321 | } |
| 5322 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5323 | msg_hist_off = TRUE; |
| 5324 | vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, |
| 5325 | s > compl_shown_match->cp_fname ? "<" : "", s); |
| 5326 | msg((char *)IObuff); |
| 5327 | msg_hist_off = FALSE; |
| 5328 | redraw_cmdline = FALSE; // don't overwrite! |
| 5329 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5330 | |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5331 | /* |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 5332 | * Find a completion item when 'completeopt' contains "fuzzy". |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5333 | */ |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5334 | static compl_T * |
| 5335 | find_comp_when_fuzzy(void) |
| 5336 | { |
| 5337 | int score; |
| 5338 | char_u* str; |
| 5339 | int target_idx = -1; |
| 5340 | int is_forward = compl_shows_dir_forward(); |
| 5341 | int is_backward = compl_shows_dir_backward(); |
| 5342 | compl_T *comp = NULL; |
| 5343 | |
glepnir | 43eef88 | 2024-06-19 20:20:48 +0200 | [diff] [blame] | 5344 | if ((is_forward && compl_selected_item == compl_match_arraysize - 1) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5345 | || (is_backward && compl_selected_item == 0)) |
glepnir | 5a18ccf | 2025-05-11 13:48:33 +0200 | [diff] [blame] | 5346 | return match_at_original_text(compl_first_match) |
| 5347 | ? compl_first_match : compl_first_match->cp_prev; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5348 | |
| 5349 | if (is_forward) |
| 5350 | target_idx = compl_selected_item + 1; |
| 5351 | else if (is_backward) |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 5352 | target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1 |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 5353 | : compl_selected_item - 1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5354 | |
| 5355 | score = compl_match_array[target_idx].pum_score; |
| 5356 | str = compl_match_array[target_idx].pum_text; |
| 5357 | |
| 5358 | comp = compl_first_match; |
Hirohito Higashi | a4a00a7 | 2025-05-08 22:58:31 +0200 | [diff] [blame] | 5359 | do |
| 5360 | { |
Hirohito Higashi | 355db99 | 2025-04-28 18:07:02 +0200 | [diff] [blame] | 5361 | if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR])) |
| 5362 | return comp; |
| 5363 | comp = comp->cp_next; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5364 | } while (comp != NULL && !is_first_match(comp)); |
| 5365 | |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5369 | /* |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5370 | * Find the appropriate completion item when 'complete' ('cpt') includes |
| 5371 | * a 'max_matches' postfix. In this case, we search for a match where |
| 5372 | * 'cp_in_match_array' is set, indicating that the match is also present |
| 5373 | * in 'compl_match_array'. |
| 5374 | */ |
| 5375 | static compl_T * |
| 5376 | find_comp_when_cpt_sources(void) |
| 5377 | { |
| 5378 | int is_forward = compl_shows_dir_forward(); |
| 5379 | compl_T *match = compl_shown_match; |
| 5380 | |
| 5381 | do |
| 5382 | match = is_forward ? match->cp_next : match->cp_prev; |
| 5383 | while (match->cp_next && !match->cp_in_match_array |
| 5384 | && !match_at_original_text(match)); |
| 5385 | return match; |
| 5386 | } |
| 5387 | |
| 5388 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5389 | * Find the next set of matches for completion. Repeat the completion "todo" |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5390 | * times. The number of matches found is returned in 'num_matches'. |
| 5391 | * |
| 5392 | * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to |
| 5393 | * get more completions. If it is FALSE, then do nothing when there are no more |
| 5394 | * completions in the given direction. |
| 5395 | * |
| 5396 | * If "advance" is TRUE, then completion will move to the first match. |
| 5397 | * Otherwise, the original text will be shown. |
| 5398 | * |
| 5399 | * Returns OK on success and -1 if the number of matches are unknown. |
| 5400 | */ |
| 5401 | static int |
| 5402 | find_next_completion_match( |
| 5403 | int allow_get_expansion, |
| 5404 | int todo, // repeat completion this many times |
| 5405 | int advance, |
| 5406 | int *num_matches) |
| 5407 | { |
| 5408 | int found_end = FALSE; |
| 5409 | compl_T *found_compl = NULL; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 5410 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5411 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
| 5412 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5413 | int cpt_sources_active = compl_match_array && cpt_sources_array; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5414 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5415 | while (--todo >= 0) |
| 5416 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5417 | if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5418 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5419 | if (compl_match_array != NULL && compl_fuzzy_match) |
| 5420 | compl_shown_match = find_comp_when_fuzzy(); |
| 5421 | else if (cpt_sources_active) |
| 5422 | compl_shown_match = find_comp_when_cpt_sources(); |
| 5423 | else |
| 5424 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5425 | found_end = (compl_first_match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5426 | && (is_first_match(compl_shown_match->cp_next) |
| 5427 | || is_first_match(compl_shown_match))); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5428 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5429 | else if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5430 | && compl_shown_match->cp_prev != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5431 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5432 | found_end = is_first_match(compl_shown_match); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 5433 | if (compl_match_array != NULL && compl_fuzzy_match) |
| 5434 | compl_shown_match = find_comp_when_fuzzy(); |
| 5435 | else if (cpt_sources_active) |
| 5436 | compl_shown_match = find_comp_when_cpt_sources(); |
| 5437 | else |
| 5438 | compl_shown_match = compl_shown_match->cp_prev; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5439 | found_end |= is_first_match(compl_shown_match); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5440 | } |
| 5441 | else |
| 5442 | { |
| 5443 | if (!allow_get_expansion) |
| 5444 | { |
| 5445 | if (advance) |
| 5446 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5447 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5448 | compl_pending -= todo + 1; |
| 5449 | else |
| 5450 | compl_pending += todo + 1; |
| 5451 | } |
| 5452 | return -1; |
| 5453 | } |
| 5454 | |
| 5455 | if (!compl_no_select && advance) |
| 5456 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5457 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5458 | --compl_pending; |
| 5459 | else |
| 5460 | ++compl_pending; |
| 5461 | } |
| 5462 | |
| 5463 | // Find matches. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5464 | *num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5465 | |
| 5466 | // handle any pending completions |
| 5467 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5468 | && advance) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5469 | { |
| 5470 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 5471 | { |
| 5472 | compl_shown_match = compl_shown_match->cp_next; |
| 5473 | --compl_pending; |
| 5474 | } |
| 5475 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 5476 | { |
| 5477 | compl_shown_match = compl_shown_match->cp_prev; |
| 5478 | ++compl_pending; |
| 5479 | } |
| 5480 | else |
| 5481 | break; |
| 5482 | } |
| 5483 | found_end = FALSE; |
| 5484 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5485 | if (!match_at_original_text(compl_shown_match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5486 | && compl_leader.string != NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5487 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5488 | compl_leader.string, (int)compl_leader.length) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5489 | && !(compl_fuzzy_match && compl_shown_match->cp_score > 0)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5490 | ++todo; |
| 5491 | else |
| 5492 | // Remember a matching item. |
| 5493 | found_compl = compl_shown_match; |
| 5494 | |
| 5495 | // Stop at the end of the list when we found a usable match. |
| 5496 | if (found_end) |
| 5497 | { |
| 5498 | if (found_compl != NULL) |
| 5499 | { |
| 5500 | compl_shown_match = found_compl; |
| 5501 | break; |
| 5502 | } |
| 5503 | todo = 1; // use first usable match after wrapping around |
| 5504 | } |
| 5505 | } |
| 5506 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5507 | return OK; |
| 5508 | } |
| 5509 | |
| 5510 | /* |
| 5511 | * Fill in the next completion in the current direction. |
| 5512 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 5513 | * get more completions. If it is FALSE, then we just do nothing when there |
| 5514 | * are no more completions in a given direction. The latter case is used when |
| 5515 | * we are still in the middle of finding completions, to allow browsing |
| 5516 | * through the ones found so far. |
| 5517 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 5518 | * |
| 5519 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 5520 | * compl_shown_match here. |
| 5521 | * |
| 5522 | * Note that this function may be called recursively once only. First with |
| 5523 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 5524 | * calls this function with "allow_get_expansion" FALSE. |
| 5525 | */ |
| 5526 | static int |
| 5527 | ins_compl_next( |
| 5528 | int allow_get_expansion, |
| 5529 | int count, // repeat completion this many times; should |
| 5530 | // be at least 1 |
| 5531 | int insert_match, // Insert the newly selected match |
| 5532 | int in_compl_func) // called from complete_check() |
| 5533 | { |
| 5534 | int num_matches = -1; |
| 5535 | int todo = count; |
| 5536 | int advance; |
| 5537 | int started = compl_started; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5538 | buf_T *orig_curbuf = curbuf; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 5539 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5540 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 5541 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5542 | int compl_preinsert = ins_compl_has_preinsert(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5543 | |
| 5544 | // When user complete function return -1 for findstart which is next |
| 5545 | // time of 'always', compl_shown_match become NULL. |
| 5546 | if (compl_shown_match == NULL) |
| 5547 | return -1; |
| 5548 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5549 | if (compl_leader.string != NULL |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 5550 | && !match_at_original_text(compl_shown_match) |
| 5551 | && !compl_fuzzy_match) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5552 | // Update "compl_shown_match" to the actually shown match |
| 5553 | ins_compl_update_shown_match(); |
| 5554 | |
| 5555 | if (allow_get_expansion && insert_match |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 5556 | && (!compl_get_longest || compl_used_match)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5557 | // Delete old text to be replaced |
| 5558 | ins_compl_delete(); |
| 5559 | |
| 5560 | // When finding the longest common text we stick at the original text, |
| 5561 | // don't let CTRL-N or CTRL-P move to the first match. |
| 5562 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 5563 | |
| 5564 | // When restarting the search don't insert the first match either. |
| 5565 | if (compl_restarting) |
| 5566 | { |
| 5567 | advance = FALSE; |
| 5568 | compl_restarting = FALSE; |
| 5569 | } |
| 5570 | |
| 5571 | // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 5572 | // around. |
| 5573 | if (find_next_completion_match(allow_get_expansion, todo, advance, |
| 5574 | &num_matches) == -1) |
| 5575 | return -1; |
| 5576 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 5577 | if (curbuf != orig_curbuf) |
| 5578 | { |
| 5579 | // In case some completion function switched buffer, don't want to |
| 5580 | // insert the completion elsewhere. |
| 5581 | return -1; |
| 5582 | } |
| 5583 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5584 | // Insert the text of the new completion, or the compl_leader. |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5585 | if (compl_no_insert && !started && !compl_preinsert) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5586 | { |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5587 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5588 | compl_used_match = FALSE; |
| 5589 | } |
| 5590 | else if (insert_match) |
| 5591 | { |
| 5592 | if (!compl_get_longest || compl_used_match) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 5593 | ins_compl_insert(in_compl_func, TRUE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5594 | else |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 5595 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5596 | } |
| 5597 | else |
| 5598 | compl_used_match = FALSE; |
| 5599 | |
| 5600 | if (!allow_get_expansion) |
| 5601 | { |
| 5602 | // may undisplay the popup menu first |
| 5603 | ins_compl_upd_pum(); |
| 5604 | |
| 5605 | if (pum_enough_matches()) |
| 5606 | // Will display the popup menu, don't redraw yet to avoid flicker. |
| 5607 | pum_call_update_screen(); |
| 5608 | else |
| 5609 | // Not showing the popup menu yet, redraw to show the user what was |
| 5610 | // inserted. |
| 5611 | update_screen(0); |
| 5612 | |
| 5613 | // display the updated popup menu |
| 5614 | ins_compl_show_pum(); |
| 5615 | #ifdef FEAT_GUI |
| 5616 | if (gui.in_use) |
| 5617 | { |
| 5618 | // Show the cursor after the match, not after the redrawn text. |
| 5619 | setcursor(); |
| 5620 | out_flush_cursor(FALSE, FALSE); |
| 5621 | } |
| 5622 | #endif |
| 5623 | |
| 5624 | // Delete old text to be replaced, since we're still searching and |
| 5625 | // don't want to match ourselves! |
| 5626 | ins_compl_delete(); |
| 5627 | } |
| 5628 | |
| 5629 | // Enter will select a match when the match wasn't inserted and the popup |
| 5630 | // menu is visible. |
| 5631 | if (compl_no_insert && !started) |
| 5632 | compl_enter_selects = TRUE; |
| 5633 | else |
| 5634 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 5635 | |
| 5636 | // Show the file name for the match (if any) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5637 | if (compl_shown_match->cp_fname != NULL) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5638 | ins_compl_show_filename(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5639 | |
| 5640 | return num_matches; |
| 5641 | } |
| 5642 | |
| 5643 | /* |
| 5644 | * Call this while finding completions, to check whether the user has hit a key |
| 5645 | * that should change the currently displayed completion, or exit completion |
| 5646 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
| 5647 | * possible. -- webb |
| 5648 | * "frequency" specifies out of how many calls we actually check. |
| 5649 | * "in_compl_func" is TRUE when called from complete_check(), don't set |
| 5650 | * compl_curr_match. |
| 5651 | */ |
| 5652 | void |
| 5653 | ins_compl_check_keys(int frequency, int in_compl_func) |
| 5654 | { |
| 5655 | static int count = 0; |
| 5656 | int c; |
| 5657 | |
| 5658 | // Don't check when reading keys from a script, :normal or feedkeys(). |
| 5659 | // That would break the test scripts. But do check for keys when called |
| 5660 | // from complete_check(). |
| 5661 | if (!in_compl_func && (using_script() || ex_normal_busy)) |
| 5662 | return; |
| 5663 | |
| 5664 | // Only do this at regular intervals |
| 5665 | if (++count < frequency) |
| 5666 | return; |
| 5667 | count = 0; |
| 5668 | |
| 5669 | // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 5670 | // can't do its work correctly. |
| 5671 | c = vpeekc_any(); |
| 5672 | if (c != NUL) |
| 5673 | { |
| 5674 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 5675 | { |
| 5676 | c = safe_vgetc(); // Eat the character |
| 5677 | compl_shows_dir = ins_compl_key2dir(c); |
| 5678 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 5679 | c != K_UP && c != K_DOWN, in_compl_func); |
| 5680 | } |
| 5681 | else |
| 5682 | { |
| 5683 | // Need to get the character to have KeyTyped set. We'll put it |
| 5684 | // back with vungetc() below. But skip K_IGNORE. |
| 5685 | c = safe_vgetc(); |
| 5686 | if (c != K_IGNORE) |
| 5687 | { |
| 5688 | // Don't interrupt completion when the character wasn't typed, |
| 5689 | // e.g., when doing @q to replay keys. |
| 5690 | if (c != Ctrl_R && KeyTyped) |
| 5691 | compl_interrupted = TRUE; |
| 5692 | |
| 5693 | vungetc(c); |
| 5694 | } |
| 5695 | } |
| 5696 | } |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 5697 | if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5698 | { |
| 5699 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 5700 | |
| 5701 | compl_pending = 0; |
| 5702 | (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func); |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | /* |
| 5707 | * Decide the direction of Insert mode complete from the key typed. |
| 5708 | * Returns BACKWARD or FORWARD. |
| 5709 | */ |
| 5710 | static int |
| 5711 | ins_compl_key2dir(int c) |
| 5712 | { |
| 5713 | if (c == Ctrl_P || c == Ctrl_L |
| 5714 | || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP) |
| 5715 | return BACKWARD; |
| 5716 | return FORWARD; |
| 5717 | } |
| 5718 | |
| 5719 | /* |
| 5720 | * Return TRUE for keys that are used for completion only when the popup menu |
| 5721 | * is visible. |
| 5722 | */ |
| 5723 | static int |
| 5724 | ins_compl_pum_key(int c) |
| 5725 | { |
| 5726 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
| 5727 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 5728 | || c == K_UP || c == K_DOWN); |
| 5729 | } |
| 5730 | |
| 5731 | /* |
| 5732 | * Decide the number of completions to move forward. |
| 5733 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 5734 | */ |
| 5735 | static int |
| 5736 | ins_compl_key2count(int c) |
| 5737 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5738 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
| 5739 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5740 | int h = pum_get_height(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5741 | if (h > 3) |
| 5742 | h -= 2; // keep some context |
| 5743 | return h; |
| 5744 | } |
| 5745 | return 1; |
| 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 5750 | * to change the currently selected completion. |
| 5751 | */ |
| 5752 | static int |
| 5753 | ins_compl_use_match(int c) |
| 5754 | { |
| 5755 | switch (c) |
| 5756 | { |
| 5757 | case K_UP: |
| 5758 | case K_DOWN: |
| 5759 | case K_PAGEDOWN: |
| 5760 | case K_KPAGEDOWN: |
| 5761 | case K_S_DOWN: |
| 5762 | case K_PAGEUP: |
| 5763 | case K_KPAGEUP: |
| 5764 | case K_S_UP: |
| 5765 | return FALSE; |
| 5766 | } |
| 5767 | return TRUE; |
| 5768 | } |
| 5769 | |
| 5770 | /* |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5771 | * Get the pattern, column and length for normal completion (CTRL-N CTRL-P |
| 5772 | * completion) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5773 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5774 | * Uses the global variables: compl_cont_status and ctrl_x_mode |
| 5775 | */ |
| 5776 | static int |
| 5777 | get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5778 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5779 | if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5780 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5781 | if (!compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5782 | { |
| 5783 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
| 5784 | ; |
| 5785 | compl_col += ++startcol; |
| 5786 | compl_length = curs_col - startcol; |
| 5787 | } |
| 5788 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5789 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5790 | compl_pattern.string = str_foldcase(line + compl_col, |
| 5791 | compl_length, NULL, 0); |
| 5792 | if (compl_pattern.string == NULL) |
| 5793 | { |
| 5794 | compl_pattern.length = 0; |
| 5795 | return FAIL; |
| 5796 | } |
| 5797 | compl_pattern.length = STRLEN(compl_pattern.string); |
| 5798 | } |
| 5799 | else |
| 5800 | { |
| 5801 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5802 | if (compl_pattern.string == NULL) |
| 5803 | { |
| 5804 | compl_pattern.length = 0; |
| 5805 | return FAIL; |
| 5806 | } |
| 5807 | compl_pattern.length = (size_t)compl_length; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5808 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5809 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5810 | else if (compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5811 | { |
| 5812 | char_u *prefix = (char_u *)"\\<"; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5813 | size_t prefixlen = STRLEN_LITERAL("\\<"); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5814 | size_t n; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5815 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5816 | if (!vim_iswordp(line + compl_col) |
| 5817 | || (compl_col > 0 |
| 5818 | && (vim_iswordp(mb_prevptr(line, line + compl_col))))) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5819 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5820 | prefix = (char_u *)""; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5821 | prefixlen = 0; |
| 5822 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5823 | |
| 5824 | // we need up to 2 extra chars for the prefix |
| 5825 | n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen; |
| 5826 | compl_pattern.string = alloc(n); |
| 5827 | if (compl_pattern.string == NULL) |
| 5828 | { |
| 5829 | compl_pattern.length = 0; |
| 5830 | return FAIL; |
| 5831 | } |
| 5832 | STRCPY((char *)compl_pattern.string, prefix); |
| 5833 | (void)quote_meta(compl_pattern.string + prefixlen, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5834 | line + compl_col, compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5835 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5836 | } |
| 5837 | else if (--startcol < 0 |
| 5838 | || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) |
| 5839 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5840 | size_t len = STRLEN_LITERAL("\\<\\k\\k"); |
| 5841 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5842 | // Match any word of at least two chars |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5843 | compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len); |
| 5844 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5845 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5846 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5847 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5848 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5849 | compl_pattern.length = len; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5850 | compl_col += curs_col; |
| 5851 | compl_length = 0; |
| 5852 | } |
| 5853 | else |
| 5854 | { |
| 5855 | // Search the point of change class of multibyte character |
| 5856 | // or not a word single byte character backward. |
| 5857 | if (has_mbyte) |
| 5858 | { |
| 5859 | int base_class; |
| 5860 | int head_off; |
| 5861 | |
| 5862 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5863 | base_class = mb_get_class(line + startcol); |
| 5864 | while (--startcol >= 0) |
| 5865 | { |
| 5866 | head_off = (*mb_head_off)(line, line + startcol); |
| 5867 | if (base_class != mb_get_class(line + startcol |
| 5868 | - head_off)) |
| 5869 | break; |
| 5870 | startcol -= head_off; |
| 5871 | } |
| 5872 | } |
| 5873 | else |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5874 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5875 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
| 5876 | ; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 5877 | } |
| 5878 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5879 | compl_col += ++startcol; |
| 5880 | compl_length = (int)curs_col - startcol; |
| 5881 | if (compl_length == 1) |
| 5882 | { |
| 5883 | // Only match word with at least two chars -- webb |
| 5884 | // there's no need to call quote_meta, |
| 5885 | // alloc(7) is enough -- Acevedo |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5886 | compl_pattern.string = alloc(7); |
| 5887 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5888 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5889 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5890 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5891 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5892 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5893 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1); |
| 5894 | STRCAT((char *)compl_pattern.string, "\\k"); |
| 5895 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5896 | } |
| 5897 | else |
| 5898 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5899 | size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2; |
| 5900 | |
| 5901 | compl_pattern.string = alloc(n); |
| 5902 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5903 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5904 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5905 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5906 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5907 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 5908 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5909 | compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5910 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | return OK; |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * Get the pattern, column and length for whole line completion or for the |
| 5919 | * complete() function. |
| 5920 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5921 | */ |
| 5922 | static int |
| 5923 | get_wholeline_compl_info(char_u *line, colnr_T curs_col) |
| 5924 | { |
| 5925 | compl_col = (colnr_T)getwhitecols(line); |
| 5926 | compl_length = (int)curs_col - (int)compl_col; |
| 5927 | if (compl_length < 0) // cursor in indent: empty pattern |
| 5928 | compl_length = 0; |
| 5929 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5930 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5931 | compl_pattern.string = str_foldcase(line + compl_col, compl_length, |
| 5932 | NULL, 0); |
| 5933 | if (compl_pattern.string == NULL) |
| 5934 | { |
| 5935 | compl_pattern.length = 0; |
| 5936 | return FAIL; |
| 5937 | } |
| 5938 | compl_pattern.length = STRLEN(compl_pattern.string); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5939 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5940 | else |
| 5941 | { |
| 5942 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5943 | if (compl_pattern.string == NULL) |
| 5944 | { |
| 5945 | compl_pattern.length = 0; |
| 5946 | return FAIL; |
| 5947 | } |
| 5948 | compl_pattern.length = (size_t)compl_length; |
| 5949 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5950 | |
| 5951 | return OK; |
| 5952 | } |
| 5953 | |
| 5954 | /* |
| 5955 | * Get the pattern, column and length for filename completion. |
| 5956 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5957 | */ |
| 5958 | static int |
| 5959 | get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 5960 | { |
| 5961 | // Go back to just before the first filename character. |
| 5962 | if (startcol > 0) |
| 5963 | { |
| 5964 | char_u *p = line + startcol; |
| 5965 | |
| 5966 | MB_PTR_BACK(line, p); |
| 5967 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 5968 | MB_PTR_BACK(line, p); |
| 5969 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 5970 | startcol = 0; |
| 5971 | else |
| 5972 | startcol = (int)(p - line) + 1; |
| 5973 | } |
| 5974 | |
| 5975 | compl_col += startcol; |
| 5976 | compl_length = (int)curs_col - startcol; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5977 | compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES); |
| 5978 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5979 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5980 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5981 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5982 | } |
| 5983 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5984 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5985 | |
| 5986 | return OK; |
| 5987 | } |
| 5988 | |
| 5989 | /* |
| 5990 | * Get the pattern, column and length for command-line completion. |
| 5991 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5992 | */ |
| 5993 | static int |
| 5994 | get_cmdline_compl_info(char_u *line, colnr_T curs_col) |
| 5995 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5996 | compl_pattern.string = vim_strnsave(line, curs_col); |
| 5997 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5998 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5999 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6000 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6001 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6002 | compl_pattern.length = curs_col; |
| 6003 | set_cmd_context(&compl_xp, compl_pattern.string, |
| 6004 | (int)compl_pattern.length, curs_col, FALSE); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6005 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 6006 | || compl_xp.xp_context == EXPAND_NOTHING) |
| 6007 | // No completion possible, use an empty pattern to get a |
| 6008 | // "pattern not found" message. |
| 6009 | compl_col = curs_col; |
| 6010 | else |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6011 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6012 | compl_length = curs_col - compl_col; |
| 6013 | |
| 6014 | return OK; |
| 6015 | } |
| 6016 | |
| 6017 | /* |
| 6018 | * Get the pattern, column and length for user defined completion ('omnifunc', |
| 6019 | * 'completefunc' and 'thesaurusfunc') |
| 6020 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 6021 | * Uses the global variable: spell_bad_len |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6022 | * Callback function "cb" is set if triggered by a function in the 'cpt' |
| 6023 | * option; otherwise, it is NULL. |
| 6024 | * "startcol", when not NULL, contains the column returned by function. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6025 | */ |
| 6026 | static int |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6027 | get_userdefined_compl_info( |
| 6028 | colnr_T curs_col UNUSED, |
| 6029 | callback_T *cb UNUSED, |
| 6030 | int *startcol UNUSED) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6031 | { |
| 6032 | int ret = FAIL; |
| 6033 | |
| 6034 | #ifdef FEAT_COMPL_FUNC |
| 6035 | // Call user defined function 'completefunc' with "a:findstart" |
| 6036 | // set to 1 to obtain the length of text to use for completion. |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6037 | char_u *line = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6038 | typval_T args[3]; |
| 6039 | int col; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6040 | char_u *funcname = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6041 | pos_T pos; |
| 6042 | int save_State = State; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6043 | int len; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6044 | string_T *compl_pat = NULL; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6045 | int is_cpt_function = (cb != NULL); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6046 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6047 | if (!is_cpt_function) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6048 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6049 | // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern |
| 6050 | // length as a string |
| 6051 | funcname = get_complete_funcname(ctrl_x_mode); |
| 6052 | if (*funcname == NUL) |
| 6053 | { |
| 6054 | semsg(_(e_option_str_is_not_set), ctrl_x_mode_function() |
| 6055 | ? "completefunc" : "omnifunc"); |
| 6056 | return FAIL; |
| 6057 | } |
| 6058 | cb = get_insert_callback(ctrl_x_mode); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | args[0].v_type = VAR_NUMBER; |
| 6062 | args[0].vval.v_number = 1; |
| 6063 | args[1].v_type = VAR_STRING; |
| 6064 | args[1].vval.v_string = (char_u *)""; |
| 6065 | args[2].v_type = VAR_UNKNOWN; |
| 6066 | pos = curwin->w_cursor; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 6067 | ++textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6068 | col = call_callback_retnr(cb, 2, args); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 6069 | --textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6070 | |
| 6071 | State = save_State; |
| 6072 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 6073 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6074 | validate_cursor(); |
| 6075 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 6076 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 6077 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6078 | return FAIL; |
| 6079 | } |
| 6080 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6081 | if (startcol != NULL) |
| 6082 | *startcol = col; |
| 6083 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 6084 | // Return value -2 means the user complete function wants to cancel the |
| 6085 | // complete without an error, do the same if the function did not execute |
| 6086 | // successfully. |
| 6087 | if (col == -2 || aborting()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6088 | return FAIL; |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6089 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 6090 | // 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] | 6091 | if (col == -3) |
| 6092 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6093 | if (is_cpt_function) |
| 6094 | return FAIL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6095 | ctrl_x_mode = CTRL_X_NORMAL; |
| 6096 | edit_submode = NULL; |
| 6097 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 6098 | msg_clr_cmdline(); |
| 6099 | return FAIL; |
| 6100 | } |
| 6101 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 6102 | // Reset extended parameters of completion, when starting new |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6103 | // completion. |
| 6104 | compl_opt_refresh_always = FALSE; |
| 6105 | compl_opt_suppress_empty = FALSE; |
| 6106 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6107 | if (col < 0 || col > curs_col) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6108 | col = curs_col; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6109 | |
| 6110 | // Setup variables for completion. Need to obtain "line" again, |
| 6111 | // it may have become invalid. |
| 6112 | line = ml_get(curwin->w_cursor.lnum); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6113 | len = curs_col - col; |
| 6114 | compl_pat = is_cpt_function ? &cpt_compl_pattern : &compl_pattern; |
| 6115 | compl_pat->string = vim_strnsave(line + col, (size_t)len); |
| 6116 | if (compl_pat->string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6117 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6118 | compl_pat->length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6119 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6120 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6121 | compl_pat->length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6122 | |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6123 | if (!is_cpt_function) |
| 6124 | { |
| 6125 | compl_col = col; |
| 6126 | compl_length = len; |
| 6127 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6128 | ret = OK; |
| 6129 | #endif |
| 6130 | |
| 6131 | return ret; |
| 6132 | } |
| 6133 | |
| 6134 | /* |
| 6135 | * Get the pattern, column and length for spell completion. |
| 6136 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 6137 | * Uses the global variable: spell_bad_len |
| 6138 | */ |
| 6139 | static int |
| 6140 | get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED) |
| 6141 | { |
| 6142 | int ret = FAIL; |
| 6143 | #ifdef FEAT_SPELL |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6144 | char_u *line = NULL; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6145 | |
| 6146 | if (spell_bad_len > 0) |
| 6147 | compl_col = curs_col - spell_bad_len; |
| 6148 | else |
| 6149 | compl_col = spell_word_start(startcol); |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6150 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6151 | if (compl_col >= (colnr_T)startcol) |
| 6152 | { |
| 6153 | compl_length = 0; |
| 6154 | compl_col = curs_col; |
| 6155 | } |
| 6156 | else |
| 6157 | { |
| 6158 | spell_expand_check_cap(compl_col); |
| 6159 | compl_length = (int)curs_col - compl_col; |
| 6160 | } |
| 6161 | // Need to obtain "line" again, it may have become invalid. |
| 6162 | line = ml_get(curwin->w_cursor.lnum); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6163 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 6164 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6165 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6166 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6167 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 6168 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6169 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6170 | compl_pattern.length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6171 | ret = OK; |
| 6172 | #endif |
| 6173 | |
| 6174 | return ret; |
| 6175 | } |
| 6176 | |
| 6177 | /* |
| 6178 | * Get the completion pattern, column and length. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6179 | * "startcol" - start column number of the completion pattern/text |
| 6180 | * "cur_col" - current cursor column |
| 6181 | * On return, "line_invalid" is set to TRUE, if the current line may have |
| 6182 | * become invalid and needs to be fetched again. |
| 6183 | * Returns OK on success. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6184 | */ |
| 6185 | static int |
| 6186 | compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid) |
| 6187 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6188 | if (ctrl_x_mode_normal() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6189 | || (ctrl_x_mode & CTRL_X_WANT_IDENT |
| 6190 | && !thesaurus_func_complete(ctrl_x_mode))) |
| 6191 | { |
| 6192 | return get_normal_compl_info(line, startcol, curs_col); |
| 6193 | } |
| 6194 | else if (ctrl_x_mode_line_or_eval()) |
| 6195 | { |
| 6196 | return get_wholeline_compl_info(line, curs_col); |
| 6197 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6198 | else if (ctrl_x_mode_files()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6199 | { |
| 6200 | return get_filename_compl_info(line, startcol, curs_col); |
| 6201 | } |
| 6202 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 6203 | { |
| 6204 | return get_cmdline_compl_info(line, curs_col); |
| 6205 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6206 | else if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6207 | || thesaurus_func_complete(ctrl_x_mode)) |
| 6208 | { |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6209 | if (get_userdefined_compl_info(curs_col, NULL, NULL) != OK) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6210 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6211 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6212 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6213 | else if (ctrl_x_mode_spell()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6214 | { |
| 6215 | if (get_spell_compl_info(startcol, curs_col) == FAIL) |
| 6216 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6217 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6218 | } |
glepnir | 0546068 | 2025-05-26 18:23:27 +0200 | [diff] [blame^] | 6219 | else if (ctrl_x_mode_register()) |
| 6220 | { |
| 6221 | return get_normal_compl_info(line, startcol, curs_col); |
| 6222 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 6223 | else |
| 6224 | { |
| 6225 | internal_error("ins_complete()"); |
| 6226 | return FAIL; |
| 6227 | } |
| 6228 | |
| 6229 | return OK; |
| 6230 | } |
| 6231 | |
| 6232 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6233 | * Continue an interrupted completion mode search in "line". |
| 6234 | * |
| 6235 | * If this same ctrl_x_mode has been interrupted use the text from |
| 6236 | * "compl_startpos" to the cursor as a pattern to add a new word instead of |
| 6237 | * expand the one before the cursor, in word-wise if "compl_startpos" is not in |
| 6238 | * the same line as the cursor then fix it (the line has been split because it |
| 6239 | * was longer than 'tw'). if SOL is set then skip the previous pattern, a word |
| 6240 | * 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] | 6241 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6242 | static void |
| 6243 | ins_compl_continue_search(char_u *line) |
| 6244 | { |
| 6245 | // it is a continued search |
| 6246 | compl_cont_status &= ~CONT_INTRPT; // remove INTRPT |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6247 | if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns() |
| 6248 | || ctrl_x_mode_path_defines()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6249 | { |
| 6250 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
| 6251 | { |
| 6252 | // line (probably) wrapped, set compl_startpos to the |
| 6253 | // first non_blank in the line, if it is not a wordchar |
| 6254 | // include it to get a better pattern, but then we don't |
| 6255 | // want the "\\<" prefix, check it below |
| 6256 | compl_col = (colnr_T)getwhitecols(line); |
| 6257 | compl_startpos.col = compl_col; |
| 6258 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 6259 | compl_cont_status &= ~CONT_SOL; // clear SOL if present |
| 6260 | } |
| 6261 | else |
| 6262 | { |
| 6263 | // S_IPOS was set when we inserted a word that was at the |
| 6264 | // beginning of the line, which means that we'll go to SOL |
| 6265 | // mode but first we need to redefine compl_startpos |
| 6266 | if (compl_cont_status & CONT_S_IPOS) |
| 6267 | { |
| 6268 | compl_cont_status |= CONT_SOL; |
| 6269 | compl_startpos.col = (colnr_T)(skipwhite( |
| 6270 | line + compl_length |
| 6271 | + compl_startpos.col) - line); |
| 6272 | } |
| 6273 | compl_col = compl_startpos.col; |
| 6274 | } |
| 6275 | compl_length = curwin->w_cursor.col - (int)compl_col; |
| 6276 | // IObuff is used to add a "word from the next line" would we |
| 6277 | // have enough space? just being paranoid |
| 6278 | #define MIN_SPACE 75 |
| 6279 | if (compl_length > (IOSIZE - MIN_SPACE)) |
| 6280 | { |
| 6281 | compl_cont_status &= ~CONT_SOL; |
| 6282 | compl_length = (IOSIZE - MIN_SPACE); |
| 6283 | compl_col = curwin->w_cursor.col - compl_length; |
| 6284 | } |
| 6285 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 6286 | if (compl_length < 1) |
| 6287 | compl_cont_status &= CONT_LOCAL; |
| 6288 | } |
| 6289 | else if (ctrl_x_mode_line_or_eval()) |
| 6290 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
| 6291 | else |
| 6292 | compl_cont_status = 0; |
| 6293 | } |
| 6294 | |
| 6295 | /* |
| 6296 | * start insert mode completion |
| 6297 | */ |
| 6298 | static int |
| 6299 | ins_compl_start(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6300 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6301 | char_u *line = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6302 | int startcol = 0; // column where searched text starts |
| 6303 | colnr_T curs_col; // cursor column |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6304 | int line_invalid = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6305 | int save_did_ai = did_ai; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 6306 | int flags = CP_ORIGINAL_TEXT; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6307 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6308 | // First time we hit ^N or ^P (in a row, I mean) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6309 | did_ai = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6310 | did_si = FALSE; |
| 6311 | can_si = FALSE; |
| 6312 | can_si_back = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6313 | if (stop_arrow() == FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6314 | return FAIL; |
| 6315 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6316 | line = ml_get(curwin->w_cursor.lnum); |
| 6317 | curs_col = curwin->w_cursor.col; |
| 6318 | compl_pending = 0; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 6319 | compl_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6320 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6321 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 6322 | && compl_cont_mode == ctrl_x_mode) |
| 6323 | // this same ctrl-x_mode was interrupted previously. Continue the |
| 6324 | // completion. |
| 6325 | ins_compl_continue_search(line); |
| 6326 | else |
| 6327 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6328 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6329 | if (!compl_status_adding()) // normal expansion |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6330 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6331 | compl_cont_mode = ctrl_x_mode; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6332 | if (ctrl_x_mode_not_default()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6333 | // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL |
| 6334 | compl_cont_status = 0; |
| 6335 | compl_cont_status |= CONT_N_ADDS; |
| 6336 | compl_startpos = curwin->w_cursor; |
| 6337 | startcol = (int)curs_col; |
| 6338 | compl_col = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6339 | } |
| 6340 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6341 | // Work out completion pattern and original text -- webb |
| 6342 | if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL) |
| 6343 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6344 | if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
| 6345 | || thesaurus_func_complete(ctrl_x_mode)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6346 | // restore did_ai, so that adding comment leader works |
| 6347 | did_ai = save_did_ai; |
| 6348 | return FAIL; |
| 6349 | } |
| 6350 | // If "line" was changed while getting completion info get it again. |
| 6351 | if (line_invalid) |
| 6352 | line = ml_get(curwin->w_cursor.lnum); |
| 6353 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6354 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6355 | { |
| 6356 | edit_submode_pre = (char_u *)_(" Adding"); |
| 6357 | if (ctrl_x_mode_line_or_eval()) |
| 6358 | { |
| 6359 | // Insert a new line, keep indentation but ignore 'comments'. |
| 6360 | char_u *old = curbuf->b_p_com; |
| 6361 | |
| 6362 | curbuf->b_p_com = (char_u *)""; |
| 6363 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 6364 | compl_startpos.col = compl_col; |
| 6365 | ins_eol('\r'); |
| 6366 | curbuf->b_p_com = old; |
| 6367 | compl_length = 0; |
| 6368 | compl_col = curwin->w_cursor.col; |
glepnir | 76bdb82 | 2025-02-08 19:04:51 +0100 | [diff] [blame] | 6369 | compl_lnum = curwin->w_cursor.lnum; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6370 | } |
glepnir | cfe502c | 2025-04-17 20:17:53 +0200 | [diff] [blame] | 6371 | else if (ctrl_x_mode_normal() && cfc_has_mode()) |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 6372 | { |
| 6373 | compl_startpos = curwin->w_cursor; |
| 6374 | compl_cont_status &= CONT_S_IPOS; |
| 6375 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6376 | } |
| 6377 | else |
| 6378 | { |
| 6379 | edit_submode_pre = NULL; |
| 6380 | compl_startpos.col = compl_col; |
| 6381 | } |
| 6382 | |
| 6383 | if (compl_cont_status & CONT_LOCAL) |
| 6384 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
| 6385 | else |
| 6386 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 6387 | |
| 6388 | // If any of the original typed text has been changed we need to fix |
| 6389 | // the redo buffer. |
| 6390 | ins_compl_fixRedoBufForLeader(NULL); |
| 6391 | |
| 6392 | // Always add completion for the original text. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6393 | VIM_CLEAR_STRING(compl_orig_text); |
| 6394 | compl_orig_text.length = (size_t)compl_length; |
| 6395 | 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] | 6396 | if (p_ic) |
| 6397 | flags |= CP_ICASE; |
zeertzjq | 4422de6 | 2025-03-07 19:06:02 +0100 | [diff] [blame] | 6398 | if (compl_orig_text.string == NULL |
| 6399 | || ins_compl_add(compl_orig_text.string, |
| 6400 | (int)compl_orig_text.length, |
| 6401 | NULL, NULL, NULL, 0, flags, FALSE, NULL, 0) != OK) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6402 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6403 | VIM_CLEAR_STRING(compl_pattern); |
| 6404 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6405 | return FAIL; |
| 6406 | } |
| 6407 | |
| 6408 | // showmode might reset the internal line pointers, so it must |
| 6409 | // be called before line = ml_get(), or when this address is no |
| 6410 | // longer needed. -- Acevedo. |
| 6411 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 6412 | edit_submode_highl = HLF_COUNT; |
| 6413 | showmode(); |
| 6414 | edit_submode_extra = NULL; |
| 6415 | out_flush(); |
| 6416 | |
| 6417 | return OK; |
| 6418 | } |
| 6419 | |
| 6420 | /* |
| 6421 | * display the completion status message |
| 6422 | */ |
| 6423 | static void |
| 6424 | ins_compl_show_statusmsg(void) |
| 6425 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6426 | // 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] | 6427 | if (is_first_match(compl_first_match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6428 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6429 | edit_submode_extra = compl_status_adding() && compl_length > 1 |
Bram Moolenaar | b53a190 | 2022-11-15 13:57:38 +0000 | [diff] [blame] | 6430 | ? (char_u *)_("Hit end of paragraph") |
| 6431 | : (char_u *)_("Pattern not found"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6432 | edit_submode_highl = HLF_E; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6433 | } |
| 6434 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6435 | if (edit_submode_extra == NULL) |
| 6436 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6437 | if (match_at_original_text(compl_curr_match)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6438 | { |
| 6439 | edit_submode_extra = (char_u *)_("Back at original"); |
| 6440 | edit_submode_highl = HLF_W; |
| 6441 | } |
| 6442 | else if (compl_cont_status & CONT_S_IPOS) |
| 6443 | { |
| 6444 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 6445 | edit_submode_highl = HLF_COUNT; |
| 6446 | } |
| 6447 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
| 6448 | { |
| 6449 | edit_submode_extra = (char_u *)_("The only match"); |
| 6450 | edit_submode_highl = HLF_COUNT; |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6451 | compl_curr_match->cp_number = 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6452 | } |
| 6453 | else |
| 6454 | { |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6455 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6456 | // Update completion sequence number when needed. |
| 6457 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 6458 | ins_compl_update_sequence_numbers(); |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 6459 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6460 | // The match should always have a sequence number now, this is |
| 6461 | // just a safety check. |
| 6462 | if (compl_curr_match->cp_number != -1) |
| 6463 | { |
| 6464 | // Space for 10 text chars. + 2x10-digit no.s = 31. |
| 6465 | // Translations may need more than twice that. |
| 6466 | static char_u match_ref[81]; |
| 6467 | |
| 6468 | if (compl_matches > 0) |
| 6469 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6470 | _("match %d of %d"), |
| 6471 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6472 | else |
| 6473 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6474 | _("match %d"), |
| 6475 | compl_curr_match->cp_number); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6476 | edit_submode_extra = match_ref; |
| 6477 | edit_submode_highl = HLF_R; |
| 6478 | if (dollar_vcol >= 0) |
| 6479 | curs_columns(FALSE); |
| 6480 | } |
| 6481 | } |
| 6482 | } |
| 6483 | |
| 6484 | // Show a message about what (completion) mode we're in. |
| 6485 | if (!compl_opt_suppress_empty) |
| 6486 | { |
| 6487 | showmode(); |
| 6488 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 6489 | { |
| 6490 | if (edit_submode_extra != NULL) |
| 6491 | { |
| 6492 | if (!p_smd) |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6493 | { |
| 6494 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6495 | msg_attr((char *)edit_submode_extra, |
| 6496 | edit_submode_highl < HLF_COUNT |
| 6497 | ? HL_ATTR(edit_submode_highl) : 0); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 6498 | msg_hist_off = FALSE; |
| 6499 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6500 | } |
| 6501 | else |
| 6502 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 6503 | } |
| 6504 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6505 | } |
| 6506 | |
| 6507 | /* |
| 6508 | * Do Insert mode completion. |
| 6509 | * Called when character "c" was typed, which has a meaning for completion. |
| 6510 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 6511 | */ |
| 6512 | int |
| 6513 | ins_complete(int c, int enable_pum) |
| 6514 | { |
| 6515 | int n; |
| 6516 | int save_w_wrow; |
| 6517 | int save_w_leftcol; |
| 6518 | int insert_match; |
| 6519 | |
| 6520 | compl_direction = ins_compl_key2dir(c); |
| 6521 | insert_match = ins_compl_use_match(c); |
| 6522 | |
| 6523 | if (!compl_started) |
| 6524 | { |
| 6525 | if (ins_compl_start() == FAIL) |
| 6526 | return FAIL; |
| 6527 | } |
| 6528 | else if (insert_match && stop_arrow() == FAIL) |
| 6529 | return FAIL; |
| 6530 | |
glepnir | cf7f012 | 2025-04-15 19:02:00 +0200 | [diff] [blame] | 6531 | compl_curr_win = curwin; |
| 6532 | compl_curr_buf = curwin->w_buffer; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6533 | compl_shown_match = compl_curr_match; |
| 6534 | compl_shows_dir = compl_direction; |
| 6535 | |
| 6536 | // Find next match (and following matches). |
| 6537 | save_w_wrow = curwin->w_wrow; |
| 6538 | save_w_leftcol = curwin->w_leftcol; |
| 6539 | n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); |
| 6540 | |
| 6541 | // may undisplay the popup menu |
| 6542 | ins_compl_upd_pum(); |
| 6543 | |
| 6544 | if (n > 1) // all matches have been found |
| 6545 | compl_matches = n; |
| 6546 | compl_curr_match = compl_shown_match; |
| 6547 | compl_direction = compl_shows_dir; |
| 6548 | |
| 6549 | // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 6550 | // mode. |
| 6551 | if (got_int && !global_busy) |
| 6552 | { |
| 6553 | (void)vgetc(); |
| 6554 | got_int = FALSE; |
| 6555 | } |
| 6556 | |
| 6557 | // 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] | 6558 | if (is_first_match(compl_first_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6559 | { |
| 6560 | // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 6561 | // because we couldn't expand anything at first place, but if we used |
| 6562 | // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 6563 | // (such as M in M'exico) if not tried already. -- Acevedo |
| 6564 | if (compl_length > 1 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6565 | || compl_status_adding() |
| 6566 | || (ctrl_x_mode_not_default() |
| 6567 | && !ctrl_x_mode_path_patterns() |
| 6568 | && !ctrl_x_mode_path_defines())) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 6569 | compl_cont_status &= ~CONT_N_ADDS; |
| 6570 | } |
| 6571 | |
| 6572 | if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) |
| 6573 | compl_cont_status |= CONT_S_IPOS; |
| 6574 | else |
| 6575 | compl_cont_status &= ~CONT_S_IPOS; |
| 6576 | |
| 6577 | ins_compl_show_statusmsg(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6578 | |
| 6579 | // Show the popup menu, unless we got interrupted. |
| 6580 | if (enable_pum && !compl_interrupted) |
| 6581 | show_pum(save_w_wrow, save_w_leftcol); |
| 6582 | |
| 6583 | compl_was_interrupted = compl_interrupted; |
| 6584 | compl_interrupted = FALSE; |
| 6585 | |
| 6586 | return OK; |
| 6587 | } |
| 6588 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 6589 | /* |
| 6590 | * Remove (if needed) and show the popup menu |
| 6591 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6592 | static void |
| 6593 | show_pum(int prev_w_wrow, int prev_w_leftcol) |
| 6594 | { |
| 6595 | // RedrawingDisabled may be set when invoked through complete(). |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6596 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6597 | RedrawingDisabled = 0; |
| 6598 | |
| 6599 | // If the cursor moved or the display scrolled we need to remove the pum |
| 6600 | // first. |
| 6601 | setcursor(); |
| 6602 | if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) |
| 6603 | ins_compl_del_pum(); |
| 6604 | |
| 6605 | ins_compl_show_pum(); |
| 6606 | setcursor(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 6607 | |
| 6608 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | /* |
| 6612 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 6613 | * If dest is not NULL the chars. are copied there quoting (with |
| 6614 | * a backslash) the metachars, and dest would be NUL terminated. |
| 6615 | * Returns the length (needed) of dest |
| 6616 | */ |
| 6617 | static unsigned |
| 6618 | quote_meta(char_u *dest, char_u *src, int len) |
| 6619 | { |
| 6620 | unsigned m = (unsigned)len + 1; // one extra for the NUL |
| 6621 | |
| 6622 | for ( ; --len >= 0; src++) |
| 6623 | { |
| 6624 | switch (*src) |
| 6625 | { |
| 6626 | case '.': |
| 6627 | case '*': |
| 6628 | case '[': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6629 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6630 | break; |
| 6631 | // FALLTHROUGH |
| 6632 | case '~': |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 6633 | if (!magic_isset()) // quote these only if magic is set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6634 | break; |
| 6635 | // FALLTHROUGH |
| 6636 | case '\\': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 6637 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6638 | break; |
| 6639 | // FALLTHROUGH |
| 6640 | case '^': // currently it's not needed. |
| 6641 | case '$': |
| 6642 | m++; |
| 6643 | if (dest != NULL) |
| 6644 | *dest++ = '\\'; |
| 6645 | break; |
| 6646 | } |
| 6647 | if (dest != NULL) |
| 6648 | *dest++ = *src; |
| 6649 | // Copy remaining bytes of a multibyte character. |
| 6650 | if (has_mbyte) |
| 6651 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6652 | int mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6653 | if (mb_len > 0 && len >= mb_len) |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6654 | for (int i = 0; i < mb_len; ++i) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6655 | { |
| 6656 | --len; |
| 6657 | ++src; |
| 6658 | if (dest != NULL) |
| 6659 | *dest++ = *src; |
| 6660 | } |
| 6661 | } |
| 6662 | } |
| 6663 | if (dest != NULL) |
| 6664 | *dest = NUL; |
| 6665 | |
| 6666 | return m; |
| 6667 | } |
| 6668 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6669 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6670 | void |
| 6671 | free_insexpand_stuff(void) |
| 6672 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 6673 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 6674 | # ifdef FEAT_EVAL |
| 6675 | free_callback(&cfu_cb); |
| 6676 | free_callback(&ofu_cb); |
| 6677 | free_callback(&tsrfu_cb); |
| 6678 | # endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6679 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6680 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6681 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6682 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6683 | /* |
| 6684 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6685 | * spelled word, if there is one. |
| 6686 | */ |
| 6687 | static void |
| 6688 | spell_back_to_badword(void) |
| 6689 | { |
| 6690 | pos_T tpos = curwin->w_cursor; |
| 6691 | |
Christ van Willegen - van Noort | 8e4c4c7 | 2024-05-17 18:49:27 +0200 | [diff] [blame] | 6692 | spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 6693 | if (curwin->w_cursor.col != tpos.col) |
| 6694 | start_arrow(&tpos); |
| 6695 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6696 | #endif |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6697 | |
| 6698 | /* |
| 6699 | * Reset the info associated with completion sources. |
| 6700 | */ |
| 6701 | static void |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6702 | cpt_sources_clear(void) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6703 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6704 | VIM_CLEAR(cpt_sources_array); |
| 6705 | cpt_sources_index = -1; |
| 6706 | cpt_sources_count = 0; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | /* |
| 6710 | * Initialize the info associated with completion sources. |
| 6711 | */ |
| 6712 | static int |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6713 | cpt_sources_init(void) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6714 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6715 | char_u buf[LSIZE]; |
| 6716 | int slen; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6717 | int count = 0; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6718 | char_u *p; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6719 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6720 | for (p = curbuf->b_p_cpt; *p;) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6721 | { |
| 6722 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6723 | p++; |
| 6724 | if (*p) // If not end of string, count this segment |
| 6725 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6726 | (void)copy_option_part(&p, buf, LSIZE, ","); // Advance p |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6727 | count++; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6728 | } |
| 6729 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6730 | cpt_sources_clear(); |
| 6731 | cpt_sources_count = count; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6732 | if (count > 0) |
| 6733 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6734 | cpt_sources_array = ALLOC_CLEAR_MULT(cpt_source_T, count); |
| 6735 | if (cpt_sources_array == NULL) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6736 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6737 | cpt_sources_count = 0; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6738 | return FAIL; |
| 6739 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6740 | count = 0; |
| 6741 | for (p = curbuf->b_p_cpt; *p;) |
| 6742 | { |
| 6743 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6744 | p++; |
| 6745 | if (*p) // If not end of string, count this segment |
| 6746 | { |
| 6747 | char_u *t; |
| 6748 | |
| 6749 | vim_memset(buf, 0, LSIZE); |
| 6750 | slen = copy_option_part(&p, buf, LSIZE, ","); // Advance p |
| 6751 | if (slen > 0 && (t = vim_strchr(buf, '^')) != NULL) |
| 6752 | cpt_sources_array[count].max_matches = atoi((char *)t + 1); |
| 6753 | count++; |
| 6754 | } |
| 6755 | } |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6756 | } |
| 6757 | return OK; |
| 6758 | } |
| 6759 | |
| 6760 | /* |
| 6761 | * Return TRUE if any of the completion sources have 'refresh' set to 'always'. |
| 6762 | */ |
| 6763 | static int |
| 6764 | is_cpt_func_refresh_always(void) |
| 6765 | { |
| 6766 | #ifdef FEAT_COMPL_FUNC |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6767 | for (int i = 0; i < cpt_sources_count; i++) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6768 | if (cpt_sources_array[i].refresh_always) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6769 | return TRUE; |
| 6770 | #endif |
| 6771 | return FALSE; |
| 6772 | } |
| 6773 | |
| 6774 | /* |
| 6775 | * Make the completion list non-cyclic. |
| 6776 | */ |
| 6777 | #ifdef FEAT_COMPL_FUNC |
| 6778 | static void |
| 6779 | ins_compl_make_linear(void) |
| 6780 | { |
| 6781 | compl_T *m; |
| 6782 | |
| 6783 | if (compl_first_match == NULL || compl_first_match->cp_prev == NULL) |
| 6784 | return; |
| 6785 | m = compl_first_match->cp_prev; |
| 6786 | m->cp_next = NULL; |
| 6787 | compl_first_match->cp_prev = NULL; |
| 6788 | } |
| 6789 | #endif |
| 6790 | |
| 6791 | /* |
| 6792 | * Remove the matches linked to the current completion source (as indicated by |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6793 | * cpt_sources_index) from the completion list. |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6794 | */ |
| 6795 | #ifdef FEAT_COMPL_FUNC |
| 6796 | static compl_T * |
| 6797 | remove_old_matches(void) |
| 6798 | { |
| 6799 | compl_T *sublist_start = NULL, *sublist_end = NULL, *insert_at = NULL; |
| 6800 | compl_T *current, *next; |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6801 | int compl_shown_removed = FALSE; |
| 6802 | int forward = (compl_first_match->cp_cpt_source_idx < 0); |
| 6803 | |
| 6804 | compl_direction = forward ? FORWARD : BACKWARD; |
| 6805 | compl_shows_dir = compl_direction; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6806 | |
| 6807 | // Identify the sublist of old matches that needs removal |
| 6808 | for (current = compl_first_match; current != NULL; current = current->cp_next) |
| 6809 | { |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6810 | if (current->cp_cpt_source_idx < cpt_sources_index && |
| 6811 | (forward || (!forward && !insert_at))) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6812 | insert_at = current; |
| 6813 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6814 | if (current->cp_cpt_source_idx == cpt_sources_index) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6815 | { |
| 6816 | if (!sublist_start) |
| 6817 | sublist_start = current; |
| 6818 | sublist_end = current; |
| 6819 | if (!compl_shown_removed && compl_shown_match == current) |
| 6820 | compl_shown_removed = TRUE; |
| 6821 | } |
| 6822 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6823 | if ((forward && current->cp_cpt_source_idx > cpt_sources_index) |
| 6824 | || (!forward && insert_at)) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6825 | break; |
| 6826 | } |
| 6827 | |
| 6828 | // Re-assign compl_shown_match if necessary |
| 6829 | if (compl_shown_removed) |
| 6830 | { |
| 6831 | if (forward) |
| 6832 | compl_shown_match = compl_first_match; |
| 6833 | else |
| 6834 | { // Last node will have the prefix that is being completed |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6835 | for (current = compl_first_match; current->cp_next != NULL; |
| 6836 | current = current->cp_next) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6837 | ; |
| 6838 | compl_shown_match = current; |
| 6839 | } |
| 6840 | } |
| 6841 | |
| 6842 | if (!sublist_start) // No nodes to remove |
| 6843 | return insert_at; |
| 6844 | |
| 6845 | // Update links to remove sublist |
| 6846 | if (sublist_start->cp_prev) |
| 6847 | sublist_start->cp_prev->cp_next = sublist_end->cp_next; |
| 6848 | else |
| 6849 | compl_first_match = sublist_end->cp_next; |
| 6850 | |
| 6851 | if (sublist_end->cp_next) |
| 6852 | sublist_end->cp_next->cp_prev = sublist_start->cp_prev; |
| 6853 | |
| 6854 | // Free all nodes in the sublist |
| 6855 | sublist_end->cp_next = NULL; |
| 6856 | for (current = sublist_start; current != NULL; current = next) |
| 6857 | { |
| 6858 | next = current->cp_next; |
| 6859 | ins_compl_item_free(current); |
| 6860 | } |
| 6861 | |
| 6862 | return insert_at; |
| 6863 | } |
| 6864 | #endif |
| 6865 | |
| 6866 | /* |
| 6867 | * Retrieve completion matches using the callback function "cb" and store the |
| 6868 | * 'refresh:always' flag. |
| 6869 | */ |
| 6870 | #ifdef FEAT_COMPL_FUNC |
| 6871 | static void |
| 6872 | get_cpt_func_completion_matches(callback_T *cb UNUSED) |
| 6873 | { |
glepnir | 19e1dd6 | 2025-05-08 22:50:38 +0200 | [diff] [blame] | 6874 | int ret; |
| 6875 | int startcol; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6876 | |
| 6877 | VIM_CLEAR_STRING(cpt_compl_pattern); |
| 6878 | ret = get_userdefined_compl_info(curwin->w_cursor.col, cb, &startcol); |
| 6879 | if (ret == FAIL && startcol == -3) |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6880 | cpt_sources_array[cpt_sources_index].refresh_always = FALSE; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6881 | else if (ret == OK) |
| 6882 | { |
| 6883 | expand_by_function(0, cpt_compl_pattern.string, cb); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6884 | cpt_sources_array[cpt_sources_index].refresh_always = |
| 6885 | compl_opt_refresh_always; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6886 | compl_opt_refresh_always = FALSE; |
| 6887 | } |
| 6888 | } |
| 6889 | #endif |
| 6890 | |
| 6891 | /* |
| 6892 | * Retrieve completion matches from functions in the 'cpt' option where the |
| 6893 | * 'refresh:always' flag is set. |
| 6894 | */ |
| 6895 | static void |
| 6896 | cpt_compl_refresh(void) |
| 6897 | { |
| 6898 | #ifdef FEAT_COMPL_FUNC |
| 6899 | char_u *cpt; |
| 6900 | char_u *p; |
Christian Brabandt | d2079cf | 2025-04-15 18:10:26 +0200 | [diff] [blame] | 6901 | callback_T *cb = NULL; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6902 | |
| 6903 | // Make the completion list linear (non-cyclic) |
| 6904 | ins_compl_make_linear(); |
| 6905 | // Make a copy of 'cpt' in case the buffer gets wiped out |
| 6906 | cpt = vim_strsave(curbuf->b_p_cpt); |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6907 | strip_caret_numbers_in_place(cpt); |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6908 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6909 | cpt_sources_index = 0; |
| 6910 | for (p = cpt; *p; cpt_sources_index++) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6911 | { |
| 6912 | while (*p == ',' || *p == ' ') // Skip delimiters |
| 6913 | p++; |
| 6914 | |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6915 | if (cpt_sources_array[cpt_sources_index].refresh_always) |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6916 | { |
| 6917 | if (*p == 'o') |
| 6918 | cb = &curbuf->b_ofu_cb; |
| 6919 | else if (*p == 'f') |
| 6920 | cb = (*(p + 1) != ',' && *(p + 1) != NUL) |
| 6921 | ? get_cpt_func_callback(p + 1) : &curbuf->b_cfu_cb; |
| 6922 | if (cb) |
| 6923 | { |
| 6924 | compl_curr_match = remove_old_matches(); |
| 6925 | get_cpt_func_completion_matches(cb); |
| 6926 | } |
| 6927 | } |
| 6928 | |
| 6929 | copy_option_part(&p, IObuff, IOSIZE, ","); // Advance p |
| 6930 | } |
Girish Palya | 0ac1eb3 | 2025-04-16 20:18:33 +0200 | [diff] [blame] | 6931 | cpt_sources_index = -1; |
Girish Palya | cbe5319 | 2025-04-14 22:13:15 +0200 | [diff] [blame] | 6932 | |
| 6933 | vim_free(cpt); |
| 6934 | // Make the list cyclic |
| 6935 | compl_matches = ins_compl_make_cyclic(); |
| 6936 | #endif |
| 6937 | } |