Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * insexpand.c: functions for Insert mode completion |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 16 | /* |
| 17 | * Definitions used for CTRL-X submode. |
| 18 | * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and |
| 19 | * ctrl_x_mode_names[] below. |
| 20 | */ |
| 21 | # define CTRL_X_WANT_IDENT 0x100 |
| 22 | |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 23 | # define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 24 | # define CTRL_X_NOT_DEFINED_YET 1 |
| 25 | # define CTRL_X_SCROLL 2 |
| 26 | # define CTRL_X_WHOLE_LINE 3 |
| 27 | # define CTRL_X_FILES 4 |
| 28 | # define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT) |
| 29 | # define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT) |
| 30 | # define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT) |
| 31 | # define CTRL_X_FINISHED 8 |
| 32 | # define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT) |
| 33 | # define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT) |
| 34 | # define CTRL_X_CMDLINE 11 |
Bram Moolenaar | 9810cfb | 2019-12-11 21:23:00 +0100 | [diff] [blame] | 35 | # define CTRL_X_FUNCTION 12 |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 36 | # define CTRL_X_OMNI 13 |
| 37 | # define CTRL_X_SPELL 14 |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 38 | # define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs" |
| 39 | # define CTRL_X_EVAL 16 // for builtin function complete() |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 40 | # define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 41 | |
| 42 | # define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
| 43 | |
| 44 | // Message for CTRL-X mode, index is ctrl_x_mode. |
| 45 | static char *ctrl_x_msgs[] = |
| 46 | { |
| 47 | N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl. |
| 48 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"), |
| 49 | NULL, // CTRL_X_SCROLL: depends on state |
| 50 | N_(" Whole line completion (^L^N^P)"), |
| 51 | N_(" File name completion (^F^N^P)"), |
| 52 | N_(" Tag completion (^]^N^P)"), |
| 53 | N_(" Path pattern completion (^N^P)"), |
| 54 | N_(" Definition completion (^D^N^P)"), |
| 55 | NULL, // CTRL_X_FINISHED |
| 56 | N_(" Dictionary completion (^K^N^P)"), |
| 57 | N_(" Thesaurus completion (^T^N^P)"), |
| 58 | N_(" Command-line completion (^V^N^P)"), |
| 59 | N_(" User defined completion (^U^N^P)"), |
| 60 | N_(" Omni completion (^O^N^P)"), |
zeertzjq | 7002c05 | 2024-06-21 07:55:07 +0200 | [diff] [blame] | 61 | N_(" Spelling suggestion (^S^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 62 | N_(" Keyword Local completion (^N^P)"), |
| 63 | NULL, // CTRL_X_EVAL doesn't use msg. |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 64 | N_(" Command-line completion (^V^N^P)"), |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 67 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 68 | static char *ctrl_x_mode_names[] = { |
| 69 | "keyword", |
| 70 | "ctrl_x", |
zeertzjq | 27fef59 | 2021-10-03 12:01:27 +0100 | [diff] [blame] | 71 | "scroll", |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 72 | "whole_line", |
| 73 | "files", |
| 74 | "tags", |
| 75 | "path_patterns", |
| 76 | "path_defines", |
| 77 | "unknown", // CTRL_X_FINISHED |
| 78 | "dictionary", |
| 79 | "thesaurus", |
| 80 | "cmdline", |
| 81 | "function", |
| 82 | "omni", |
| 83 | "spell", |
| 84 | NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs" |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 85 | "eval", |
| 86 | "cmdline", |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 87 | }; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 88 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 91 | * Structure used to store one match for insert completion. |
| 92 | */ |
| 93 | typedef struct compl_S compl_T; |
| 94 | struct compl_S |
| 95 | { |
| 96 | compl_T *cp_next; |
| 97 | compl_T *cp_prev; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 98 | compl_T *cp_match_next; // matched next compl_T |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 99 | string_T cp_str; // matched text |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 100 | char_u *(cp_text[CPT_COUNT]); // text for the menu |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 101 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 102 | typval_T cp_user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 103 | #endif |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 104 | char_u *cp_fname; // file containing the match, allocated when |
| 105 | // cp_flags has CP_FREE_FNAME |
| 106 | int cp_flags; // CP_ values |
| 107 | int cp_number; // sequence number |
| 108 | int cp_score; // fuzzy match score |
glepnir | 7baa014 | 2024-10-09 20:19:25 +0200 | [diff] [blame] | 109 | int cp_user_abbr_hlattr; // highlight attribute for abbr |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 110 | int cp_user_kind_hlattr; // highlight attribute for kind |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 113 | // values for cp_flags |
| 114 | # define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun |
| 115 | # define CP_FREE_FNAME 2 // cp_fname is allocated |
| 116 | # define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status |
| 117 | # define CP_EQUAL 8 // ins_compl_equal() always returns TRUE |
| 118 | # define CP_ICASE 16 // ins_compl_equal() ignores case |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 119 | # define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 120 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 121 | /* |
| 122 | * All the current matches are stored in a list. |
| 123 | * "compl_first_match" points to the start of the list. |
| 124 | * "compl_curr_match" points to the currently selected entry. |
| 125 | * "compl_shown_match" is different from compl_curr_match during |
| 126 | * ins_compl_get_exp(). |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 127 | * "compl_old_match" points to previous "compl_curr_match". |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 128 | */ |
| 129 | static compl_T *compl_first_match = NULL; |
| 130 | static compl_T *compl_curr_match = NULL; |
| 131 | static compl_T *compl_shown_match = NULL; |
| 132 | static compl_T *compl_old_match = NULL; |
| 133 | |
| 134 | // After using a cursor key <Enter> selects a match in the popup menu, |
| 135 | // otherwise it inserts a line break. |
| 136 | static int compl_enter_selects = FALSE; |
| 137 | |
| 138 | // When "compl_leader" is not NULL only matches that start with this string |
| 139 | // are used. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 140 | static string_T compl_leader = {NULL, 0}; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 141 | |
| 142 | static int compl_get_longest = FALSE; // put longest common string |
| 143 | // in compl_leader |
| 144 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 145 | // Selected one of the matches. When FALSE the match was edited or using the |
| 146 | // longest common string. |
| 147 | static int compl_used_match; |
| 148 | |
| 149 | // didn't finish finding completions. |
| 150 | static int compl_was_interrupted = FALSE; |
| 151 | |
| 152 | // Set when character typed while looking for matches and it means we should |
| 153 | // stop looking for matches. |
| 154 | static int compl_interrupted = FALSE; |
| 155 | |
| 156 | static int compl_restarting = FALSE; // don't insert match |
| 157 | |
| 158 | // When the first completion is done "compl_started" is set. When it's |
| 159 | // FALSE the word to be completed must be located. |
| 160 | static int compl_started = FALSE; |
| 161 | |
| 162 | // Which Ctrl-X mode are we in? |
| 163 | static int ctrl_x_mode = CTRL_X_NORMAL; |
| 164 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 165 | static int compl_matches = 0; // number of completion matches |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 166 | static string_T compl_pattern = {NULL, 0}; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 167 | static int compl_direction = FORWARD; |
| 168 | static int compl_shows_dir = FORWARD; |
| 169 | static int compl_pending = 0; // > 1 for postponed CTRL-N |
| 170 | static pos_T compl_startpos; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 171 | // Length in bytes of the text being completed (this is deleted to be replaced |
| 172 | // by the match.) |
| 173 | static int compl_length = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 174 | static colnr_T compl_col = 0; // column where the text starts |
| 175 | // that is being completed |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 176 | static colnr_T compl_ins_end_col = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 177 | 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] | 178 | // completion started |
| 179 | static int compl_cont_mode = 0; |
| 180 | static expand_T compl_xp; |
| 181 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 182 | // List of flags for method of completion. |
| 183 | static int compl_cont_status = 0; |
| 184 | # define CONT_ADDING 1 // "normal" or "adding" expansion |
| 185 | # define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion |
| 186 | // it's set only iff N_ADDS is set |
| 187 | # define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current |
| 188 | # define CONT_S_IPOS 8 // next ^X<> will set initial_pos? |
| 189 | // if so, word-wise-expansion will set SOL |
| 190 | # define CONT_SOL 16 // pattern includes start of line, just for |
| 191 | // word-wise expansion, not set for ^X^L |
| 192 | # define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local |
| 193 | // expansion, (eg use complete=.) |
| 194 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 195 | static int compl_opt_refresh_always = FALSE; |
| 196 | static int compl_opt_suppress_empty = FALSE; |
| 197 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 198 | static int compl_selected_item = -1; |
| 199 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 200 | static int *compl_fuzzy_scores; |
| 201 | |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 202 | // "compl_match_array" points the currently displayed list of entries in the |
| 203 | // popup menu. It is NULL when there is no popup menu. |
| 204 | static pumitem_T *compl_match_array = NULL; |
| 205 | static int compl_match_arraysize; |
| 206 | |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 207 | 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); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 208 | static void ins_compl_longest_match(compl_T *match); |
| 209 | static void ins_compl_del_pum(void); |
| 210 | static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir); |
| 211 | static char_u *find_line_end(char_u *ptr); |
| 212 | static void ins_compl_free(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 213 | static int ins_compl_need_restart(void); |
| 214 | static void ins_compl_new_leader(void); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 215 | static int get_compl_len(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 216 | static void ins_compl_restart(void); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 217 | 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] | 218 | static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg); |
| 219 | # if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 220 | static void ins_compl_add_list(list_T *list); |
| 221 | static void ins_compl_add_dict(dict_T *dict); |
| 222 | # endif |
| 223 | static int ins_compl_key2dir(int c); |
| 224 | static int ins_compl_pum_key(int c); |
| 225 | static int ins_compl_key2count(int c); |
| 226 | static void show_pum(int prev_w_wrow, int prev_w_leftcol); |
| 227 | static unsigned quote_meta(char_u *dest, char_u *str, int len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 228 | |
| 229 | #ifdef FEAT_SPELL |
| 230 | static void spell_back_to_badword(void); |
| 231 | static int spell_bad_len = 0; // length of located bad word |
| 232 | #endif |
| 233 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 234 | /* |
| 235 | * CTRL-X pressed in Insert mode. |
| 236 | */ |
| 237 | void |
| 238 | ins_ctrl_x(void) |
| 239 | { |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 240 | if (!ctrl_x_mode_cmdline()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 241 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 242 | // 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] | 243 | if (compl_cont_status & CONT_N_ADDS) |
| 244 | compl_cont_status |= CONT_INTRPT; |
| 245 | else |
| 246 | compl_cont_status = 0; |
| 247 | // We're not sure which CTRL-X mode it will be yet |
| 248 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 249 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 250 | edit_submode_pre = NULL; |
| 251 | showmode(); |
| 252 | } |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 253 | else |
| 254 | // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X |
| 255 | // CTRL-V look like CTRL-N |
| 256 | 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] | 257 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 258 | may_trigger_modechanged(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Functions to check the current CTRL-X mode. |
| 263 | */ |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 264 | int ctrl_x_mode_none(void) |
| 265 | { return ctrl_x_mode == 0; } |
| 266 | int ctrl_x_mode_normal(void) |
| 267 | { return ctrl_x_mode == CTRL_X_NORMAL; } |
| 268 | int ctrl_x_mode_scroll(void) |
| 269 | { return ctrl_x_mode == CTRL_X_SCROLL; } |
| 270 | int ctrl_x_mode_whole_line(void) |
| 271 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE; } |
| 272 | int ctrl_x_mode_files(void) |
| 273 | { return ctrl_x_mode == CTRL_X_FILES; } |
| 274 | int ctrl_x_mode_tags(void) |
| 275 | { return ctrl_x_mode == CTRL_X_TAGS; } |
| 276 | int ctrl_x_mode_path_patterns(void) |
| 277 | { return ctrl_x_mode == CTRL_X_PATH_PATTERNS; } |
| 278 | int ctrl_x_mode_path_defines(void) |
| 279 | { return ctrl_x_mode == CTRL_X_PATH_DEFINES; } |
| 280 | int ctrl_x_mode_dictionary(void) |
| 281 | { return ctrl_x_mode == CTRL_X_DICTIONARY; } |
| 282 | int ctrl_x_mode_thesaurus(void) |
| 283 | { return ctrl_x_mode == CTRL_X_THESAURUS; } |
| 284 | int ctrl_x_mode_cmdline(void) |
| 285 | { return ctrl_x_mode == CTRL_X_CMDLINE |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 286 | || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; } |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 287 | int ctrl_x_mode_function(void) |
| 288 | { return ctrl_x_mode == CTRL_X_FUNCTION; } |
| 289 | int ctrl_x_mode_omni(void) |
| 290 | { return ctrl_x_mode == CTRL_X_OMNI; } |
| 291 | int ctrl_x_mode_spell(void) |
| 292 | { return ctrl_x_mode == CTRL_X_SPELL; } |
| 293 | static int ctrl_x_mode_eval(void) |
| 294 | { return ctrl_x_mode == CTRL_X_EVAL; } |
| 295 | int ctrl_x_mode_line_or_eval(void) |
| 296 | { return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Whether other than default completion has been selected. |
| 300 | */ |
| 301 | int |
| 302 | ctrl_x_mode_not_default(void) |
| 303 | { |
| 304 | return ctrl_x_mode != CTRL_X_NORMAL; |
| 305 | } |
| 306 | |
| 307 | /* |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 308 | * Whether CTRL-X was typed without a following character, |
| 309 | * not including when in CTRL-X CTRL-V mode. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 310 | */ |
| 311 | int |
| 312 | ctrl_x_mode_not_defined_yet(void) |
| 313 | { |
| 314 | return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; |
| 315 | } |
| 316 | |
| 317 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 318 | * Return TRUE if currently in "normal" or "adding" insert completion matches |
| 319 | * state |
| 320 | */ |
| 321 | int |
| 322 | compl_status_adding(void) |
| 323 | { |
| 324 | return compl_cont_status & CONT_ADDING; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Return TRUE if the completion pattern includes start of line, just for |
| 329 | * word-wise expansion. |
| 330 | */ |
| 331 | int |
| 332 | compl_status_sol(void) |
| 333 | { |
| 334 | return compl_cont_status & CONT_SOL; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.) |
| 339 | */ |
| 340 | int |
| 341 | compl_status_local(void) |
| 342 | { |
| 343 | return compl_cont_status & CONT_LOCAL; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Clear the completion status flags |
| 348 | */ |
| 349 | void |
| 350 | compl_status_clear(void) |
| 351 | { |
| 352 | compl_cont_status = 0; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Return TRUE if completion is using the forward direction matches |
| 357 | */ |
| 358 | static int |
| 359 | compl_dir_forward(void) |
| 360 | { |
| 361 | return compl_direction == FORWARD; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Return TRUE if currently showing forward completion matches |
| 366 | */ |
| 367 | static int |
| 368 | compl_shows_dir_forward(void) |
| 369 | { |
| 370 | return compl_shows_dir == FORWARD; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Return TRUE if currently showing backward completion matches |
| 375 | */ |
| 376 | static int |
| 377 | compl_shows_dir_backward(void) |
| 378 | { |
| 379 | return compl_shows_dir == BACKWARD; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Return TRUE if the 'dictionary' or 'thesaurus' option can be used. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 384 | */ |
| 385 | int |
| 386 | has_compl_option(int dict_opt) |
| 387 | { |
| 388 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 389 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 390 | && !curwin->w_p_spell |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 391 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 392 | ) |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 393 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL |
| 394 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 395 | && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 396 | #endif |
| 397 | )) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 398 | { |
| 399 | ctrl_x_mode = CTRL_X_NORMAL; |
| 400 | edit_submode = NULL; |
| 401 | msg_attr(dict_opt ? _("'dictionary' option is empty") |
| 402 | : _("'thesaurus' option is empty"), |
| 403 | HL_ATTR(HLF_E)); |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 404 | if (emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 405 | { |
| 406 | vim_beep(BO_COMPL); |
| 407 | setcursor(); |
| 408 | out_flush(); |
| 409 | #ifdef FEAT_EVAL |
| 410 | if (!get_vim_var_nr(VV_TESTING)) |
| 411 | #endif |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 412 | ui_delay(2004L, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 413 | } |
| 414 | return FALSE; |
| 415 | } |
| 416 | return TRUE; |
| 417 | } |
| 418 | |
| 419 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 420 | * 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] | 421 | * This depends on the current mode. |
| 422 | */ |
| 423 | int |
| 424 | vim_is_ctrl_x_key(int c) |
| 425 | { |
| 426 | // Always allow ^R - let its results then be checked |
| 427 | if (c == Ctrl_R) |
| 428 | return TRUE; |
| 429 | |
| 430 | // Accept <PageUp> and <PageDown> if the popup menu is visible. |
| 431 | if (ins_compl_pum_key(c)) |
| 432 | return TRUE; |
| 433 | |
| 434 | switch (ctrl_x_mode) |
| 435 | { |
| 436 | case 0: // Not in any CTRL-X mode |
| 437 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 438 | case CTRL_X_NOT_DEFINED_YET: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 439 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 440 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
| 441 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 442 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 443 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
| 444 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 445 | || c == Ctrl_S || c == Ctrl_K || c == 's' |
| 446 | || c == Ctrl_Z); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 447 | case CTRL_X_SCROLL: |
| 448 | return (c == Ctrl_Y || c == Ctrl_E); |
| 449 | case CTRL_X_WHOLE_LINE: |
| 450 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 451 | case CTRL_X_FILES: |
| 452 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 453 | case CTRL_X_DICTIONARY: |
| 454 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 455 | case CTRL_X_THESAURUS: |
| 456 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 457 | case CTRL_X_TAGS: |
| 458 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 459 | #ifdef FEAT_FIND_ID |
| 460 | case CTRL_X_PATH_PATTERNS: |
| 461 | return (c == Ctrl_P || c == Ctrl_N); |
| 462 | case CTRL_X_PATH_DEFINES: |
| 463 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 464 | #endif |
| 465 | case CTRL_X_CMDLINE: |
| 466 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 467 | || c == Ctrl_X); |
| 468 | #ifdef FEAT_COMPL_FUNC |
| 469 | case CTRL_X_FUNCTION: |
| 470 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
| 471 | case CTRL_X_OMNI: |
| 472 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
| 473 | #endif |
| 474 | case CTRL_X_SPELL: |
| 475 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
| 476 | case CTRL_X_EVAL: |
| 477 | return (c == Ctrl_P || c == Ctrl_N); |
| 478 | } |
| 479 | internal_error("vim_is_ctrl_x_key()"); |
| 480 | return FALSE; |
| 481 | } |
| 482 | |
| 483 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 484 | * Return TRUE if "match" is the original text when the completion began. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 485 | */ |
| 486 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 487 | match_at_original_text(compl_T *match) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 488 | { |
| 489 | return match->cp_flags & CP_ORIGINAL_TEXT; |
| 490 | } |
| 491 | |
| 492 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 493 | * Returns TRUE if "match" is the first match in the completion list. |
| 494 | */ |
| 495 | static int |
| 496 | is_first_match(compl_T *match) |
| 497 | { |
| 498 | return match == compl_first_match; |
| 499 | } |
| 500 | |
| 501 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 502 | * Return TRUE when character "c" is part of the item currently being |
| 503 | * completed. Used to decide whether to abandon complete mode when the menu |
| 504 | * is visible. |
| 505 | */ |
| 506 | int |
| 507 | ins_compl_accept_char(int c) |
| 508 | { |
| 509 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 510 | // When expanding an identifier only accept identifier chars. |
| 511 | return vim_isIDc(c); |
| 512 | |
| 513 | switch (ctrl_x_mode) |
| 514 | { |
| 515 | case CTRL_X_FILES: |
| 516 | // When expanding file name only accept file name chars. But not |
| 517 | // path separators, so that "proto/<Tab>" expands files in |
| 518 | // "proto", not "proto/" as a whole |
| 519 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 520 | |
| 521 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 522 | case CTRL_X_CMDLINE_CTRL_X: |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 523 | case CTRL_X_OMNI: |
| 524 | // Command line and Omni completion can work with just about any |
| 525 | // printable character, but do stop at white space. |
| 526 | return vim_isprintc(c) && !VIM_ISWHITE(c); |
| 527 | |
| 528 | case CTRL_X_WHOLE_LINE: |
| 529 | // For while line completion a space can be part of the line. |
| 530 | return vim_isprintc(c); |
| 531 | } |
| 532 | return vim_iswordc(c); |
| 533 | } |
| 534 | |
| 535 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 536 | * 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] | 537 | * If the result is in allocated memory "tofree" is set to it. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 538 | */ |
| 539 | static char_u * |
| 540 | ins_compl_infercase_gettext( |
| 541 | char_u *str, |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 542 | int char_len, |
| 543 | int compl_char_len, |
| 544 | int min_len, |
| 545 | char_u **tofree) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 546 | { |
| 547 | int *wca; // Wide character array. |
| 548 | char_u *p; |
| 549 | int i, c; |
| 550 | int has_lower = FALSE; |
| 551 | int was_letter = FALSE; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 552 | garray_T gap; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 553 | |
| 554 | IObuff[0] = NUL; |
| 555 | |
| 556 | // Allocate wide character array for the completion and fill it. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 557 | wca = ALLOC_MULT(int, char_len); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 558 | if (wca == NULL) |
| 559 | return IObuff; |
| 560 | |
| 561 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 562 | for (i = 0; i < char_len; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 563 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 564 | if (has_mbyte) |
| 565 | wca[i] = mb_ptr2char_adv(&p); |
| 566 | else |
| 567 | wca[i] = *(p++); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 568 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 569 | |
| 570 | // Rule 1: Were any chars converted to lower? |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 571 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 572 | for (i = 0; i < min_len; ++i) |
| 573 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 574 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 575 | if (MB_ISLOWER(c)) |
| 576 | { |
| 577 | has_lower = TRUE; |
| 578 | if (MB_ISUPPER(wca[i])) |
| 579 | { |
| 580 | // Rule 1 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 581 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 582 | wca[i] = MB_TOLOWER(wca[i]); |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Rule 2: No lower case, 2nd consecutive letter converted to |
| 589 | // upper case. |
| 590 | if (!has_lower) |
| 591 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 592 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 593 | for (i = 0; i < min_len; ++i) |
| 594 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 595 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 596 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 597 | { |
| 598 | // Rule 2 is satisfied. |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 599 | for (i = compl_char_len; i < char_len; ++i) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 600 | wca[i] = MB_TOUPPER(wca[i]); |
| 601 | break; |
| 602 | } |
| 603 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // Copy the original case of the part we typed. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 608 | p = compl_orig_text.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 609 | for (i = 0; i < min_len; ++i) |
| 610 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 611 | c = has_mbyte ? mb_ptr2char_adv(&p) : *(p++); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 612 | if (MB_ISLOWER(c)) |
| 613 | wca[i] = MB_TOLOWER(wca[i]); |
| 614 | else if (MB_ISUPPER(c)) |
| 615 | wca[i] = MB_TOUPPER(wca[i]); |
| 616 | } |
| 617 | |
| 618 | // Generate encoding specific output from wide character array. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 619 | p = IObuff; |
| 620 | i = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 621 | ga_init2(&gap, 1, 500); |
| 622 | while (i < char_len) |
| 623 | { |
| 624 | if (gap.ga_data != NULL) |
| 625 | { |
| 626 | if (ga_grow(&gap, 10) == FAIL) |
| 627 | { |
| 628 | ga_clear(&gap); |
| 629 | return (char_u *)"[failed]"; |
| 630 | } |
| 631 | p = (char_u *)gap.ga_data + gap.ga_len; |
| 632 | if (has_mbyte) |
| 633 | gap.ga_len += (*mb_char2bytes)(wca[i++], p); |
| 634 | else |
| 635 | { |
| 636 | *p = wca[i++]; |
| 637 | ++gap.ga_len; |
| 638 | } |
| 639 | } |
| 640 | else if ((p - IObuff) + 6 >= IOSIZE) |
| 641 | { |
| 642 | // Multi-byte characters can occupy up to five bytes more than |
| 643 | // ASCII characters, and we also need one byte for NUL, so when |
| 644 | // getting to six bytes from the edge of IObuff switch to using a |
| 645 | // growarray. Add the character in the next round. |
| 646 | if (ga_grow(&gap, IOSIZE) == FAIL) |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 647 | { |
| 648 | vim_free(wca); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 649 | return (char_u *)"[failed]"; |
zeertzjq | 70e566b | 2024-03-21 07:11:58 +0100 | [diff] [blame] | 650 | } |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 651 | *p = NUL; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 652 | STRCPY(gap.ga_data, IObuff); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 653 | gap.ga_len = (int)(p - IObuff); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 654 | } |
| 655 | else if (has_mbyte) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 656 | p += (*mb_char2bytes)(wca[i++], p); |
| 657 | else |
| 658 | *(p++) = wca[i++]; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 659 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 660 | vim_free(wca); |
| 661 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 662 | if (gap.ga_data != NULL) |
| 663 | { |
| 664 | *tofree = gap.ga_data; |
| 665 | return gap.ga_data; |
| 666 | } |
| 667 | |
| 668 | *p = NUL; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 669 | return IObuff; |
| 670 | } |
| 671 | |
| 672 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 673 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
| 674 | * case of the originally typed text is used, and the case of the completed |
| 675 | * text is inferred, ie this tries to work out what case you probably wanted |
| 676 | * the rest of the word to be in -- webb |
| 677 | */ |
| 678 | int |
| 679 | ins_compl_add_infercase( |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 680 | char_u *str_arg, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 681 | int len, |
| 682 | int icase, |
| 683 | char_u *fname, |
| 684 | int dir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 685 | int cont_s_ipos) // next ^X<> will set initial_pos |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 686 | { |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 687 | char_u *str = str_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 688 | char_u *p; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 689 | int char_len; // count multi-byte characters |
| 690 | int compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 691 | int min_len; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 692 | int flags = 0; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 693 | int res; |
| 694 | char_u *tofree = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 695 | |
| 696 | if (p_ic && curbuf->b_p_inf && len > 0) |
| 697 | { |
| 698 | // Infer case of completed part. |
| 699 | |
| 700 | // Find actual length of completion. |
| 701 | if (has_mbyte) |
| 702 | { |
| 703 | p = str; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 704 | char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 705 | while (*p != NUL) |
| 706 | { |
| 707 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 708 | ++char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 712 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 713 | char_len = len; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 714 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 715 | |
| 716 | // Find actual length of original text. |
| 717 | if (has_mbyte) |
| 718 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 719 | p = compl_orig_text.string; |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 720 | compl_char_len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 721 | while (*p != NUL) |
| 722 | { |
| 723 | MB_PTR_ADV(p); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 724 | ++compl_char_len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | else |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 728 | { |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 729 | compl_char_len = compl_length; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 730 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 731 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 732 | // "char_len" may be smaller than "compl_char_len" when using |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 733 | // thesaurus, only use the minimum when comparing. |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 734 | min_len = MIN(char_len, compl_char_len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 735 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 736 | str = ins_compl_infercase_gettext(str, char_len, |
| 737 | compl_char_len, min_len, &tofree); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 738 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 739 | if (cont_s_ipos) |
| 740 | flags |= CP_CONT_S_IPOS; |
| 741 | if (icase) |
| 742 | flags |= CP_ICASE; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 743 | |
glepnir | 5c66e23 | 2024-11-15 19:58:27 +0100 | [diff] [blame] | 744 | res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, NULL); |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 745 | vim_free(tofree); |
| 746 | return res; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 750 | * Add a match to the list of matches. The arguments are: |
| 751 | * str - text of the match to add |
| 752 | * len - length of "str". If -1, then the length of "str" is |
| 753 | * computed. |
| 754 | * fname - file name to associate with this match. |
| 755 | * cptext - list of strings to use with this match (for abbr, menu, info |
| 756 | * and kind) |
| 757 | * user_data - user supplied data (any vim type) for this match |
| 758 | * cdir - match direction. If 0, use "compl_direction". |
| 759 | * flags_arg - match flags (cp_flags) |
| 760 | * adup - accept this match even if it is already present. |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 761 | * *user_hl - list of extra highlight attributes for abbr kind. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 762 | * If "cdir" is FORWARD, then the match is added after the current match. |
| 763 | * Otherwise, it is added before the current match. |
| 764 | * |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 765 | * If the given string is already in the list of completions, then return |
| 766 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 767 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 768 | */ |
| 769 | static int |
| 770 | ins_compl_add( |
| 771 | char_u *str, |
| 772 | int len, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 773 | char_u *fname, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 774 | char_u **cptext, // extra text for popup menu or NULL |
| 775 | typval_T *user_data UNUSED, // "user_data" entry or NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 776 | int cdir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 777 | int flags_arg, |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 778 | int adup, // accept duplicate match |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 779 | int *user_hl) // user abbr/kind hlattr |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 780 | { |
| 781 | compl_T *match; |
| 782 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 783 | int flags = flags_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 784 | |
Bram Moolenaar | ceb0619 | 2021-04-04 15:05:22 +0200 | [diff] [blame] | 785 | if (flags & CP_FAST) |
| 786 | fast_breakcheck(); |
| 787 | else |
| 788 | ui_breakcheck(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 789 | if (got_int) |
| 790 | return FAIL; |
| 791 | if (len < 0) |
| 792 | len = (int)STRLEN(str); |
| 793 | |
| 794 | // If the same match is already present, don't add it. |
| 795 | if (compl_first_match != NULL && !adup) |
| 796 | { |
| 797 | match = compl_first_match; |
| 798 | do |
| 799 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 800 | if (!match_at_original_text(match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 801 | && STRNCMP(match->cp_str.string, str, len) == 0 |
| 802 | && ((int)match->cp_str.length <= len |
| 803 | || match->cp_str.string[len] == NUL)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 804 | return NOTDONE; |
| 805 | match = match->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 806 | } while (match != NULL && !is_first_match(match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // Remove any popup menu before changing the list of matches. |
| 810 | ins_compl_del_pum(); |
| 811 | |
| 812 | // Allocate a new match structure. |
| 813 | // Copy the values to the new match structure. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 814 | match = ALLOC_CLEAR_ONE(compl_T); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 815 | if (match == NULL) |
| 816 | return FAIL; |
| 817 | match->cp_number = -1; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 818 | if (flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 819 | match->cp_number = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 820 | if ((match->cp_str.string = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 821 | { |
| 822 | vim_free(match); |
| 823 | return FAIL; |
| 824 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 825 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 826 | match->cp_str.length = len; |
| 827 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 828 | // match-fname is: |
| 829 | // - 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] | 830 | // - 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] | 831 | // - NULL otherwise. --Acevedo |
| 832 | if (fname != NULL |
| 833 | && compl_curr_match != NULL |
| 834 | && compl_curr_match->cp_fname != NULL |
| 835 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 836 | match->cp_fname = compl_curr_match->cp_fname; |
| 837 | else if (fname != NULL) |
| 838 | { |
| 839 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 840 | flags |= CP_FREE_FNAME; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 841 | } |
| 842 | else |
| 843 | match->cp_fname = NULL; |
| 844 | match->cp_flags = flags; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 845 | match->cp_user_abbr_hlattr = user_hl ? user_hl[0] : -1; |
| 846 | match->cp_user_kind_hlattr = user_hl ? user_hl[1] : -1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 847 | |
| 848 | if (cptext != NULL) |
| 849 | { |
| 850 | int i; |
| 851 | |
| 852 | for (i = 0; i < CPT_COUNT; ++i) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 853 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 854 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 855 | match->cp_text[i] = vim_strsave(cptext[i]); |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 856 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 857 | } |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 858 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 859 | if (user_data != NULL) |
| 860 | match->cp_user_data = *user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 861 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 862 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 863 | // Link the new match structure after (FORWARD) or before (BACKWARD) the |
| 864 | // current match in the list of matches . |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 865 | if (compl_first_match == NULL) |
| 866 | match->cp_next = match->cp_prev = NULL; |
| 867 | else if (dir == FORWARD) |
| 868 | { |
| 869 | match->cp_next = compl_curr_match->cp_next; |
| 870 | match->cp_prev = compl_curr_match; |
| 871 | } |
| 872 | else // BACKWARD |
| 873 | { |
| 874 | match->cp_next = compl_curr_match; |
| 875 | match->cp_prev = compl_curr_match->cp_prev; |
| 876 | } |
| 877 | if (match->cp_next) |
| 878 | match->cp_next->cp_prev = match; |
| 879 | if (match->cp_prev) |
| 880 | match->cp_prev->cp_next = match; |
| 881 | else // if there's nothing before, it is the first match |
| 882 | compl_first_match = match; |
| 883 | compl_curr_match = match; |
| 884 | |
| 885 | // Find the longest common string if still doing that. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 886 | if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 887 | ins_compl_longest_match(match); |
| 888 | |
| 889 | return OK; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 894 | * match->cp_flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 895 | */ |
| 896 | static int |
| 897 | ins_compl_equal(compl_T *match, char_u *str, int len) |
| 898 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 899 | if (match->cp_flags & CP_EQUAL) |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 900 | return TRUE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 901 | if (match->cp_flags & CP_ICASE) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 902 | return STRNICMP(match->cp_str.string, str, (size_t)len) == 0; |
| 903 | return STRNCMP(match->cp_str.string, str, (size_t)len) == 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 907 | * when len is -1 mean use whole length of p otherwise part of p |
| 908 | */ |
| 909 | static void |
| 910 | ins_compl_insert_bytes(char_u *p, int len) |
| 911 | { |
| 912 | if (len == -1) |
| 913 | len = (int)STRLEN(p); |
| 914 | ins_bytes_len(p, len); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 915 | compl_ins_end_col = curwin->w_cursor.col; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* |
zeertzjq | d32bf0a | 2024-12-17 20:55:13 +0100 | [diff] [blame] | 919 | * Checks if the column is within the currently inserted completion text |
| 920 | * column range. If it is, it returns a special highlight attribute. |
| 921 | * -1 mean normal item. |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 922 | */ |
| 923 | int |
| 924 | ins_compl_col_range_attr(int col) |
| 925 | { |
| 926 | if (col >= compl_col && col < compl_ins_end_col) |
| 927 | return syn_name2attr((char_u *)"ComplMatchIns"); |
| 928 | |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 933 | * Reduce the longest common string for match "match". |
| 934 | */ |
| 935 | static void |
| 936 | ins_compl_longest_match(compl_T *match) |
| 937 | { |
| 938 | char_u *p, *s; |
| 939 | int c1, c2; |
| 940 | int had_match; |
| 941 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 942 | if (compl_leader.string == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 943 | { |
| 944 | // First match, use it as a whole. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 945 | compl_leader.string = vim_strnsave(match->cp_str.string, match->cp_str.length); |
| 946 | if (compl_leader.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 947 | return; |
| 948 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 949 | compl_leader.length = match->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 950 | had_match = (curwin->w_cursor.col > compl_col); |
| 951 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 952 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 953 | ins_redraw(FALSE); |
| 954 | |
| 955 | // When the match isn't there (to avoid matching itself) remove it |
| 956 | // again after redrawing. |
| 957 | if (!had_match) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 958 | ins_compl_delete(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 959 | compl_used_match = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 960 | |
| 961 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 962 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 963 | |
| 964 | // Reduce the text if this match differs from compl_leader. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 965 | p = compl_leader.string; |
| 966 | s = match->cp_str.string; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 967 | while (*p != NUL) |
| 968 | { |
| 969 | if (has_mbyte) |
| 970 | { |
| 971 | c1 = mb_ptr2char(p); |
| 972 | c2 = mb_ptr2char(s); |
| 973 | } |
| 974 | else |
| 975 | { |
| 976 | c1 = *p; |
| 977 | c2 = *s; |
| 978 | } |
| 979 | if ((match->cp_flags & CP_ICASE) |
| 980 | ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2)) |
| 981 | break; |
| 982 | if (has_mbyte) |
| 983 | { |
| 984 | MB_PTR_ADV(p); |
| 985 | MB_PTR_ADV(s); |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | ++p; |
| 990 | ++s; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | if (*p != NUL) |
| 995 | { |
| 996 | // Leader was shortened, need to change the inserted text. |
| 997 | *p = NUL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 998 | compl_leader.length = (size_t)(p - compl_leader.string); |
| 999 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1000 | had_match = (curwin->w_cursor.col > compl_col); |
| 1001 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1002 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1003 | ins_redraw(FALSE); |
| 1004 | |
| 1005 | // When the match isn't there (to avoid matching itself) remove it |
| 1006 | // again after redrawing. |
| 1007 | if (!had_match) |
| 1008 | ins_compl_delete(); |
| 1009 | } |
| 1010 | |
| 1011 | compl_used_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * Add an array of matches to the list of matches. |
| 1016 | * Frees matches[]. |
| 1017 | */ |
| 1018 | static void |
| 1019 | ins_compl_add_matches( |
| 1020 | int num_matches, |
| 1021 | char_u **matches, |
| 1022 | int icase) |
| 1023 | { |
| 1024 | int i; |
| 1025 | int add_r = OK; |
| 1026 | int dir = compl_direction; |
| 1027 | |
| 1028 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1029 | { |
| 1030 | add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir, |
| 1031 | CP_FAST | (icase ? CP_ICASE : 0), FALSE, NULL); |
| 1032 | if (add_r == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1033 | // if dir was BACKWARD then honor it just once |
| 1034 | dir = FORWARD; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1035 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1036 | FreeWild(num_matches, matches); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Make the completion list cyclic. |
| 1041 | * Return the number of matches (excluding the original). |
| 1042 | */ |
| 1043 | static int |
| 1044 | ins_compl_make_cyclic(void) |
| 1045 | { |
| 1046 | compl_T *match; |
| 1047 | int count = 0; |
| 1048 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1049 | if (compl_first_match == NULL) |
| 1050 | return 0; |
| 1051 | |
| 1052 | // Find the end of the list. |
| 1053 | match = compl_first_match; |
| 1054 | // 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] | 1055 | while (match->cp_next != NULL && !is_first_match(match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1056 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1057 | match = match->cp_next; |
| 1058 | ++count; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1059 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1060 | match->cp_next = compl_first_match; |
| 1061 | compl_first_match->cp_prev = match; |
| 1062 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1063 | return count; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Return whether there currently is a shown match. |
| 1068 | */ |
| 1069 | int |
| 1070 | ins_compl_has_shown_match(void) |
| 1071 | { |
| 1072 | return compl_shown_match == NULL |
| 1073 | || compl_shown_match != compl_shown_match->cp_next; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * Return whether the shown match is long enough. |
| 1078 | */ |
| 1079 | int |
| 1080 | ins_compl_long_shown_match(void) |
| 1081 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1082 | return (int)compl_shown_match->cp_str.length |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1083 | > curwin->w_cursor.col - compl_col; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1087 | * Get the local or global value of 'completeopt' flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1088 | */ |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1089 | unsigned int |
| 1090 | get_cot_flags(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1091 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1092 | return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * Update the screen and when there is any scrolling remove the popup menu. |
| 1097 | */ |
| 1098 | static void |
| 1099 | ins_compl_upd_pum(void) |
| 1100 | { |
| 1101 | int h; |
| 1102 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1103 | if (compl_match_array == NULL) |
| 1104 | return; |
| 1105 | |
| 1106 | h = curwin->w_cline_height; |
| 1107 | // Update the screen later, before drawing the popup menu over it. |
| 1108 | pum_call_update_screen(); |
| 1109 | if (h != curwin->w_cline_height) |
| 1110 | ins_compl_del_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Remove any popup menu. |
| 1115 | */ |
| 1116 | static void |
| 1117 | ins_compl_del_pum(void) |
| 1118 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1119 | if (compl_match_array == NULL) |
| 1120 | return; |
| 1121 | |
| 1122 | pum_undisplay(); |
| 1123 | VIM_CLEAR(compl_match_array); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * Return TRUE if the popup menu should be displayed. |
| 1128 | */ |
| 1129 | int |
| 1130 | pum_wanted(void) |
| 1131 | { |
| 1132 | // 'completeopt' must contain "menu" or "menuone" |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1133 | if ((get_cot_flags() & COT_ANY_MENU) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1134 | return FALSE; |
| 1135 | |
| 1136 | // The display looks bad on a B&W display. |
| 1137 | if (t_colors < 8 |
| 1138 | #ifdef FEAT_GUI |
| 1139 | && !gui.in_use |
| 1140 | #endif |
| 1141 | ) |
| 1142 | return FALSE; |
| 1143 | return TRUE; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 1148 | * One if 'completopt' contains "menuone". |
| 1149 | */ |
| 1150 | static int |
| 1151 | pum_enough_matches(void) |
| 1152 | { |
| 1153 | compl_T *compl; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1154 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1155 | |
| 1156 | // Don't display the popup menu if there are no matches or there is only |
| 1157 | // one (ignoring the original text). |
| 1158 | compl = compl_first_match; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1159 | do |
| 1160 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1161 | if (compl == NULL || (!match_at_original_text(compl) && ++i == 2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1162 | break; |
| 1163 | compl = compl->cp_next; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1164 | } while (!is_first_match(compl)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1165 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1166 | if (get_cot_flags() & COT_MENUONE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1167 | return (i >= 1); |
| 1168 | return (i >= 2); |
| 1169 | } |
| 1170 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1171 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1172 | /* |
| 1173 | * Allocate Dict for the completed item. |
| 1174 | * { word, abbr, menu, kind, info } |
| 1175 | */ |
| 1176 | static dict_T * |
| 1177 | ins_compl_dict_alloc(compl_T *match) |
| 1178 | { |
| 1179 | dict_T *dict = dict_alloc_lock(VAR_FIXED); |
| 1180 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1181 | if (dict == NULL) |
| 1182 | return NULL; |
| 1183 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1184 | dict_add_string(dict, "word", match->cp_str.string); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1185 | dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]); |
| 1186 | dict_add_string(dict, "menu", match->cp_text[CPT_MENU]); |
| 1187 | dict_add_string(dict, "kind", match->cp_text[CPT_KIND]); |
| 1188 | dict_add_string(dict, "info", match->cp_text[CPT_INFO]); |
| 1189 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 1190 | dict_add_string(dict, "user_data", (char_u *)""); |
| 1191 | else |
| 1192 | dict_add_tv(dict, "user_data", &match->cp_user_data); |
| 1193 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1194 | return dict; |
| 1195 | } |
| 1196 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | * Trigger the CompleteChanged autocmd event. Invoked each time the Insert mode |
| 1199 | * completion menu is changed. |
| 1200 | */ |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1201 | static void |
| 1202 | trigger_complete_changed_event(int cur) |
| 1203 | { |
| 1204 | dict_T *v_event; |
| 1205 | dict_T *item; |
| 1206 | static int recursive = FALSE; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1207 | save_v_event_T save_v_event; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1208 | |
| 1209 | if (recursive) |
| 1210 | return; |
| 1211 | |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1212 | if (cur < 0) |
| 1213 | item = dict_alloc(); |
| 1214 | else |
| 1215 | item = ins_compl_dict_alloc(compl_curr_match); |
| 1216 | if (item == NULL) |
| 1217 | return; |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1218 | v_event = get_v_event(&save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1219 | dict_add_dict(v_event, "completed_item", item); |
| 1220 | pum_set_event_info(v_event); |
| 1221 | dict_set_items_ro(v_event); |
| 1222 | |
| 1223 | recursive = TRUE; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1224 | textlock++; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1225 | apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1226 | textlock--; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1227 | recursive = FALSE; |
| 1228 | |
Bram Moolenaar | 3075a45 | 2021-11-17 15:51:52 +0000 | [diff] [blame] | 1229 | restore_v_event(v_event, &save_v_event); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1230 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1231 | #endif |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1232 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1233 | /* |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1234 | * pumitem qsort compare func |
| 1235 | */ |
| 1236 | static int |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1237 | ins_compl_fuzzy_cmp(const void *a, const void *b) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1238 | { |
| 1239 | const int sa = (*(pumitem_T *)a).pum_score; |
| 1240 | const int sb = (*(pumitem_T *)b).pum_score; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1241 | const int ia = (*(pumitem_T *)a).pum_idx; |
| 1242 | const int ib = (*(pumitem_T *)b).pum_idx; |
| 1243 | 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] | 1244 | } |
| 1245 | |
| 1246 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1247 | * Build a popup menu to show the completion matches. |
| 1248 | * Returns the popup menu entry that should be selected. Returns -1 if nothing |
| 1249 | * should be selected. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1250 | */ |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1251 | static int |
| 1252 | ins_compl_build_pum(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1253 | { |
| 1254 | compl_T *compl; |
| 1255 | compl_T *shown_compl = NULL; |
| 1256 | int did_find_shown_match = FALSE; |
| 1257 | int shown_match_ok = FALSE; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1258 | int i = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1259 | int cur = -1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1260 | int max_fuzzy_score = 0; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 1261 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1262 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
| 1263 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1264 | compl_T *match_head = NULL; |
| 1265 | compl_T *match_tail = NULL; |
| 1266 | compl_T *match_next = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1267 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1268 | // Need to build the popup menu list. |
| 1269 | compl_match_arraysize = 0; |
| 1270 | compl = compl_first_match; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1271 | |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1272 | // If the current match is the original text don't find the first |
| 1273 | // match after it, don't highlight anything. |
| 1274 | if (match_at_original_text(compl_shown_match)) |
| 1275 | shown_match_ok = TRUE; |
| 1276 | |
| 1277 | if (compl_leader.string != NULL |
| 1278 | && STRCMP(compl_leader.string, compl_orig_text.string) == 0 |
| 1279 | && shown_match_ok == FALSE) |
| 1280 | compl_shown_match = compl_no_select ? compl_first_match |
| 1281 | : compl_first_match->cp_next; |
| 1282 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1283 | do |
| 1284 | { |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 1285 | // When 'completeopt' contains "fuzzy" and leader is not NULL or empty, |
| 1286 | // set the cp_score for later comparisons. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1287 | if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0) |
| 1288 | compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1289 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1290 | if (!match_at_original_text(compl) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1291 | && (compl_leader.string == NULL |
| 1292 | || ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1293 | || (compl_fuzzy_match && compl->cp_score > 0))) |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1294 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1295 | ++compl_match_arraysize; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1296 | if (match_head == NULL) |
| 1297 | match_head = compl; |
| 1298 | else |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1299 | match_tail->cp_match_next = compl; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1300 | match_tail = compl; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1301 | |
| 1302 | if (!shown_match_ok && !compl_fuzzy_match) |
| 1303 | { |
| 1304 | if (compl == compl_shown_match || did_find_shown_match) |
| 1305 | { |
| 1306 | // This item is the shown match or this is the |
| 1307 | // first displayed item after the shown match. |
| 1308 | compl_shown_match = compl; |
| 1309 | did_find_shown_match = TRUE; |
| 1310 | shown_match_ok = TRUE; |
| 1311 | } |
| 1312 | else |
| 1313 | // Remember this displayed match for when the |
| 1314 | // shown match is just below it. |
| 1315 | shown_compl = compl; |
| 1316 | cur = i; |
| 1317 | } |
| 1318 | else if (compl_fuzzy_match) |
| 1319 | { |
| 1320 | if (i == 0) |
| 1321 | shown_compl = compl; |
| 1322 | // Update the maximum fuzzy score and the shown match |
| 1323 | // if the current item's score is higher |
| 1324 | if (compl->cp_score > max_fuzzy_score) |
| 1325 | { |
| 1326 | did_find_shown_match = TRUE; |
| 1327 | max_fuzzy_score = compl->cp_score; |
| 1328 | if (!compl_no_select) |
| 1329 | compl_shown_match = compl; |
| 1330 | } |
| 1331 | |
| 1332 | if (!shown_match_ok && compl == compl_shown_match && !compl_no_select) |
| 1333 | { |
| 1334 | cur = i; |
| 1335 | shown_match_ok = TRUE; |
| 1336 | } |
| 1337 | } |
| 1338 | i++; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1339 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1340 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1341 | if (compl == compl_shown_match && !compl_fuzzy_match) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1342 | { |
| 1343 | did_find_shown_match = TRUE; |
| 1344 | |
| 1345 | // When the original text is the shown match don't set |
| 1346 | // compl_shown_match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1347 | if (match_at_original_text(compl)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1348 | shown_match_ok = TRUE; |
| 1349 | |
| 1350 | if (!shown_match_ok && shown_compl != NULL) |
| 1351 | { |
| 1352 | // The shown match isn't displayed, set it to the |
| 1353 | // previously displayed match. |
| 1354 | compl_shown_match = shown_compl; |
| 1355 | shown_match_ok = TRUE; |
| 1356 | } |
| 1357 | } |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1358 | compl = compl->cp_next; |
| 1359 | } while (compl != NULL && !is_first_match(compl)); |
| 1360 | |
| 1361 | if (compl_match_arraysize == 0) |
| 1362 | return -1; |
| 1363 | |
| 1364 | compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize); |
| 1365 | if (compl_match_array == NULL) |
| 1366 | return -1; |
| 1367 | |
| 1368 | compl = match_head; |
| 1369 | i = 0; |
| 1370 | while (compl != NULL) |
| 1371 | { |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1372 | compl_match_array[i].pum_text = compl->cp_text[CPT_ABBR] != NULL |
| 1373 | ? compl->cp_text[CPT_ABBR] : compl->cp_str.string; |
glepnir | a49c077 | 2024-11-30 10:56:30 +0100 | [diff] [blame] | 1374 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 1375 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 1376 | compl_match_array[i].pum_score = compl->cp_score; |
| 1377 | compl_match_array[i].pum_user_abbr_hlattr = compl->cp_user_abbr_hlattr; |
| 1378 | compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1379 | compl_match_array[i++].pum_extra = compl->cp_text[CPT_MENU] != NULL |
| 1380 | ? compl->cp_text[CPT_MENU] : compl->cp_fname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 1381 | match_next = compl->cp_match_next; |
| 1382 | compl->cp_match_next = NULL; |
| 1383 | compl = match_next; |
| 1384 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1385 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1386 | if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0) |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1387 | { |
| 1388 | for (i = 0; i < compl_match_arraysize; i++) |
| 1389 | compl_match_array[i].pum_idx = i; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1390 | // sort by the largest score of fuzzy match |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1391 | qsort(compl_match_array, (size_t)compl_match_arraysize, |
| 1392 | sizeof(pumitem_T), ins_compl_fuzzy_cmp); |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 1393 | shown_match_ok = TRUE; |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 1394 | } |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1395 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1396 | if (!shown_match_ok) // no displayed match at all |
| 1397 | cur = -1; |
| 1398 | |
| 1399 | return cur; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * Show the popup menu for the list of matches. |
| 1404 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
| 1405 | */ |
| 1406 | void |
| 1407 | ins_compl_show_pum(void) |
| 1408 | { |
| 1409 | int i; |
| 1410 | int cur = -1; |
| 1411 | colnr_T col; |
| 1412 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1413 | if (!pum_wanted() || !pum_enough_matches()) |
| 1414 | return; |
| 1415 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1416 | // Update the screen later, before drawing the popup menu over it. |
| 1417 | pum_call_update_screen(); |
| 1418 | |
| 1419 | if (compl_match_array == NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1420 | // Need to build the popup menu list. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1421 | cur = ins_compl_build_pum(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1422 | else |
| 1423 | { |
| 1424 | // popup menu already exists, only need to find the current item. |
| 1425 | for (i = 0; i < compl_match_arraysize; ++i) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1426 | 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] | 1427 | || compl_match_array[i].pum_text |
| 1428 | == compl_shown_match->cp_text[CPT_ABBR]) |
| 1429 | { |
| 1430 | cur = i; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1435 | if (compl_match_array == NULL) |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1436 | { |
| 1437 | #ifdef FEAT_EVAL |
| 1438 | if (compl_started && has_completechanged()) |
| 1439 | trigger_complete_changed_event(cur); |
| 1440 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1441 | return; |
glepnir | 0d3c0a6 | 2024-02-11 17:52:40 +0100 | [diff] [blame] | 1442 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1443 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1444 | // In Replace mode when a $ is displayed at the end of the line only |
| 1445 | // part of the screen would be updated. We do need to redraw here. |
| 1446 | dollar_vcol = -1; |
| 1447 | |
| 1448 | // Compute the screen column of the start of the completed text. |
| 1449 | // Use the cursor to get all wrapping and other settings right. |
| 1450 | col = curwin->w_cursor.col; |
| 1451 | curwin->w_cursor.col = compl_col; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 1452 | compl_selected_item = cur; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1453 | pum_display(compl_match_array, compl_match_arraysize, cur); |
| 1454 | curwin->w_cursor.col = col; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1455 | |
glepnir | cbb46b4 | 2024-02-03 18:11:13 +0100 | [diff] [blame] | 1456 | // After adding leader, set the current match to shown match. |
| 1457 | if (compl_started && compl_curr_match != compl_shown_match) |
| 1458 | compl_curr_match = compl_shown_match; |
| 1459 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1460 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1461 | if (has_completechanged()) |
| 1462 | trigger_complete_changed_event(cur); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1463 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | #define DICT_FIRST (1) // use just first element in "dict" |
| 1467 | #define DICT_EXACT (2) // "dict" is the exact name of a file |
| 1468 | |
| 1469 | /* |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1470 | * Get current completion leader |
| 1471 | */ |
| 1472 | char_u * |
| 1473 | ins_compl_leader(void) |
| 1474 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1475 | return compl_leader.string != NULL ? compl_leader.string : compl_orig_text.string; |
| 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * Get current completion leader length |
| 1480 | */ |
| 1481 | size_t |
| 1482 | ins_compl_leader_len(void) |
| 1483 | { |
| 1484 | return compl_leader.string != NULL ? compl_leader.length : compl_orig_text.length; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1488 | * Add any identifiers that match the given pattern "pat" in the list of |
| 1489 | * dictionary files "dict_start" to the list of completions. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1490 | */ |
| 1491 | static void |
| 1492 | ins_compl_dictionaries( |
| 1493 | char_u *dict_start, |
| 1494 | char_u *pat, |
| 1495 | int flags, // DICT_FIRST and/or DICT_EXACT |
| 1496 | int thesaurus) // Thesaurus completion |
| 1497 | { |
| 1498 | char_u *dict = dict_start; |
| 1499 | char_u *ptr; |
| 1500 | char_u *buf; |
| 1501 | regmatch_T regmatch; |
| 1502 | char_u **files; |
| 1503 | int count; |
| 1504 | int save_p_scs; |
| 1505 | int dir = compl_direction; |
| 1506 | |
| 1507 | if (*dict == NUL) |
| 1508 | { |
| 1509 | #ifdef FEAT_SPELL |
| 1510 | // When 'dictionary' is empty and spell checking is enabled use |
| 1511 | // "spell". |
| 1512 | if (!thesaurus && curwin->w_p_spell) |
| 1513 | dict = (char_u *)"spell"; |
| 1514 | else |
| 1515 | #endif |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | buf = alloc(LSIZE); |
| 1520 | if (buf == NULL) |
| 1521 | return; |
| 1522 | regmatch.regprog = NULL; // so that we can goto theend |
| 1523 | |
| 1524 | // If 'infercase' is set, don't use 'smartcase' here |
| 1525 | save_p_scs = p_scs; |
| 1526 | if (curbuf->b_p_inf) |
| 1527 | p_scs = FALSE; |
| 1528 | |
| 1529 | // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 1530 | // to only match at the start of a line. Otherwise just match the |
| 1531 | // pattern. Also need to double backslashes. |
| 1532 | if (ctrl_x_mode_line_or_eval()) |
| 1533 | { |
| 1534 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
| 1535 | size_t len; |
| 1536 | |
| 1537 | if (pat_esc == NULL) |
| 1538 | goto theend; |
| 1539 | len = STRLEN(pat_esc) + 10; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1540 | ptr = alloc(len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1541 | if (ptr == NULL) |
| 1542 | { |
| 1543 | vim_free(pat_esc); |
| 1544 | goto theend; |
| 1545 | } |
| 1546 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
| 1547 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 1548 | vim_free(pat_esc); |
| 1549 | vim_free(ptr); |
| 1550 | } |
| 1551 | else |
| 1552 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1553 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1554 | if (regmatch.regprog == NULL) |
| 1555 | goto theend; |
| 1556 | } |
| 1557 | |
| 1558 | // ignore case depends on 'ignorecase', 'smartcase' and "pat" |
| 1559 | regmatch.rm_ic = ignorecase(pat); |
| 1560 | while (*dict != NUL && !got_int && !compl_interrupted) |
| 1561 | { |
| 1562 | // copy one dictionary file name into buf |
| 1563 | if (flags == DICT_EXACT) |
| 1564 | { |
| 1565 | count = 1; |
| 1566 | files = &dict; |
| 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | // Expand wildcards in the dictionary name, but do not allow |
| 1571 | // backticks (for security, the 'dict' option may have been set in |
| 1572 | // a modeline). |
| 1573 | copy_option_part(&dict, buf, LSIZE, ","); |
| 1574 | # ifdef FEAT_SPELL |
| 1575 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 1576 | count = -1; |
| 1577 | else |
| 1578 | # endif |
| 1579 | if (vim_strchr(buf, '`') != NULL |
| 1580 | || expand_wildcards(1, &buf, &count, &files, |
| 1581 | EW_FILE|EW_SILENT) != OK) |
| 1582 | count = 0; |
| 1583 | } |
| 1584 | |
| 1585 | # ifdef FEAT_SPELL |
| 1586 | if (count == -1) |
| 1587 | { |
| 1588 | // Complete from active spelling. Skip "\<" in the pattern, we |
| 1589 | // don't use it as a RE. |
| 1590 | if (pat[0] == '\\' && pat[1] == '<') |
| 1591 | ptr = pat + 2; |
| 1592 | else |
| 1593 | ptr = pat; |
| 1594 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
| 1595 | } |
| 1596 | else |
| 1597 | # endif |
| 1598 | if (count > 0) // avoid warning for using "files" uninit |
| 1599 | { |
| 1600 | ins_compl_files(count, files, thesaurus, flags, |
| 1601 | ®match, buf, &dir); |
| 1602 | if (flags != DICT_EXACT) |
| 1603 | FreeWild(count, files); |
| 1604 | } |
| 1605 | if (flags != 0) |
| 1606 | break; |
| 1607 | } |
| 1608 | |
| 1609 | theend: |
| 1610 | p_scs = save_p_scs; |
| 1611 | vim_regfree(regmatch.regprog); |
| 1612 | vim_free(buf); |
| 1613 | } |
| 1614 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1615 | /* |
| 1616 | * Add all the words in the line "*buf_arg" from the thesaurus file "fname" |
| 1617 | * skipping the word at 'skip_word'. Returns OK on success. |
| 1618 | */ |
| 1619 | static int |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 1620 | thesaurus_add_words_in_line( |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1621 | char_u *fname, |
| 1622 | char_u **buf_arg, |
| 1623 | int dir, |
| 1624 | char_u *skip_word) |
| 1625 | { |
| 1626 | int status = OK; |
| 1627 | char_u *ptr; |
| 1628 | char_u *wstart; |
| 1629 | |
| 1630 | // Add the other matches on the line |
| 1631 | ptr = *buf_arg; |
| 1632 | while (!got_int) |
| 1633 | { |
| 1634 | // Find start of the next word. Skip white |
| 1635 | // space and punctuation. |
| 1636 | ptr = find_word_start(ptr); |
| 1637 | if (*ptr == NUL || *ptr == NL) |
| 1638 | break; |
| 1639 | wstart = ptr; |
| 1640 | |
| 1641 | // Find end of the word. |
| 1642 | if (has_mbyte) |
| 1643 | // Japanese words may have characters in |
| 1644 | // different classes, only separate words |
| 1645 | // with single-byte non-word characters. |
| 1646 | while (*ptr != NUL) |
| 1647 | { |
| 1648 | int l = (*mb_ptr2len)(ptr); |
| 1649 | |
| 1650 | if (l < 2 && !vim_iswordc(*ptr)) |
| 1651 | break; |
| 1652 | ptr += l; |
| 1653 | } |
| 1654 | else |
| 1655 | ptr = find_word_end(ptr); |
| 1656 | |
| 1657 | // Add the word. Skip the regexp match. |
| 1658 | if (wstart != skip_word) |
| 1659 | { |
| 1660 | status = ins_compl_add_infercase(wstart, (int)(ptr - wstart), p_ic, |
| 1661 | fname, dir, FALSE); |
| 1662 | if (status == FAIL) |
| 1663 | break; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | *buf_arg = ptr; |
| 1668 | return status; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * Process "count" dictionary/thesaurus "files" and add the text matching |
| 1673 | * "regmatch". |
| 1674 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1675 | static void |
| 1676 | ins_compl_files( |
| 1677 | int count, |
| 1678 | char_u **files, |
| 1679 | int thesaurus, |
| 1680 | int flags, |
| 1681 | regmatch_T *regmatch, |
| 1682 | char_u *buf, |
| 1683 | int *dir) |
| 1684 | { |
| 1685 | char_u *ptr; |
| 1686 | int i; |
| 1687 | FILE *fp; |
| 1688 | int add_r; |
| 1689 | |
| 1690 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 1691 | { |
| 1692 | 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] | 1693 | if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1694 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1695 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1696 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1697 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 1698 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 1699 | } |
| 1700 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1701 | if (fp == NULL) |
| 1702 | continue; |
| 1703 | |
| 1704 | // Read dictionary file line by line. |
| 1705 | // Check each line for a match. |
| 1706 | while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1707 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1708 | ptr = buf; |
| 1709 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1710 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1711 | ptr = regmatch->startp[0]; |
glepnir | 6e19993 | 2024-12-14 21:13:27 +0100 | [diff] [blame] | 1712 | ptr = ctrl_x_mode_line_or_eval() ? find_line_end(ptr) |
| 1713 | : find_word_end(ptr); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1714 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 1715 | (int)(ptr - regmatch->startp[0]), |
| 1716 | p_ic, files[i], *dir, FALSE); |
| 1717 | if (thesaurus) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1718 | { |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1719 | // For a thesaurus, add all the words in the line |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1720 | ptr = buf; |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 1721 | add_r = thesaurus_add_words_in_line(files[i], &ptr, *dir, |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1722 | regmatch->startp[0]); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1723 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1724 | if (add_r == OK) |
| 1725 | // if dir was BACKWARD then honor it just once |
| 1726 | *dir = FORWARD; |
| 1727 | else if (add_r == FAIL) |
| 1728 | break; |
| 1729 | // avoid expensive call to vim_regexec() when at end |
| 1730 | // of line |
| 1731 | if (*ptr == '\n' || got_int) |
| 1732 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1733 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1734 | line_breakcheck(); |
| 1735 | ins_compl_check_keys(50, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1736 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 1737 | fclose(fp); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Find the start of the next word. |
| 1743 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 1744 | */ |
| 1745 | char_u * |
| 1746 | find_word_start(char_u *ptr) |
| 1747 | { |
| 1748 | if (has_mbyte) |
| 1749 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
| 1750 | ptr += (*mb_ptr2len)(ptr); |
| 1751 | else |
| 1752 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 1753 | ++ptr; |
| 1754 | return ptr; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Find the end of the word. Assumes it starts inside a word. |
| 1759 | * Returns a pointer to just after the word. |
| 1760 | */ |
| 1761 | char_u * |
| 1762 | find_word_end(char_u *ptr) |
| 1763 | { |
| 1764 | int start_class; |
| 1765 | |
| 1766 | if (has_mbyte) |
| 1767 | { |
| 1768 | start_class = mb_get_class(ptr); |
| 1769 | if (start_class > 1) |
| 1770 | while (*ptr != NUL) |
| 1771 | { |
| 1772 | ptr += (*mb_ptr2len)(ptr); |
| 1773 | if (mb_get_class(ptr) != start_class) |
| 1774 | break; |
| 1775 | } |
| 1776 | } |
| 1777 | else |
| 1778 | while (vim_iswordc(*ptr)) |
| 1779 | ++ptr; |
| 1780 | return ptr; |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * Find the end of the line, omitting CR and NL at the end. |
| 1785 | * Returns a pointer to just after the line. |
| 1786 | */ |
| 1787 | static char_u * |
| 1788 | find_line_end(char_u *ptr) |
| 1789 | { |
| 1790 | char_u *s; |
| 1791 | |
| 1792 | s = ptr + STRLEN(ptr); |
| 1793 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 1794 | --s; |
| 1795 | return s; |
| 1796 | } |
| 1797 | |
| 1798 | /* |
| 1799 | * Free the list of completions |
| 1800 | */ |
| 1801 | static void |
| 1802 | ins_compl_free(void) |
| 1803 | { |
| 1804 | compl_T *match; |
| 1805 | int i; |
| 1806 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1807 | VIM_CLEAR_STRING(compl_pattern); |
| 1808 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1809 | |
| 1810 | if (compl_first_match == NULL) |
| 1811 | return; |
| 1812 | |
| 1813 | ins_compl_del_pum(); |
| 1814 | pum_clear(); |
| 1815 | |
| 1816 | compl_curr_match = compl_first_match; |
| 1817 | do |
| 1818 | { |
| 1819 | match = compl_curr_match; |
| 1820 | compl_curr_match = compl_curr_match->cp_next; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1821 | VIM_CLEAR_STRING(match->cp_str); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1822 | // several entries may use the same fname, free it just once. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1823 | if (match->cp_flags & CP_FREE_FNAME) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1824 | vim_free(match->cp_fname); |
| 1825 | for (i = 0; i < CPT_COUNT; ++i) |
| 1826 | vim_free(match->cp_text[i]); |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1827 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 1828 | clear_tv(&match->cp_user_data); |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1829 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1830 | vim_free(match); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1831 | } while (compl_curr_match != NULL && !is_first_match(compl_curr_match)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1832 | compl_first_match = compl_curr_match = NULL; |
| 1833 | compl_shown_match = NULL; |
| 1834 | compl_old_match = NULL; |
| 1835 | } |
| 1836 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1837 | /* |
| 1838 | * Reset/clear the completion state. |
| 1839 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1840 | void |
| 1841 | ins_compl_clear(void) |
| 1842 | { |
| 1843 | compl_cont_status = 0; |
| 1844 | compl_started = FALSE; |
| 1845 | compl_matches = 0; |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 1846 | compl_ins_end_col = 0; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1847 | VIM_CLEAR_STRING(compl_pattern); |
| 1848 | VIM_CLEAR_STRING(compl_leader); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1849 | edit_submode_extra = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1850 | VIM_CLEAR_STRING(compl_orig_text); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1851 | compl_enter_selects = FALSE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1852 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1853 | // clear v:completed_item |
| 1854 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1855 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * Return TRUE when Insert completion is active. |
| 1860 | */ |
| 1861 | int |
| 1862 | ins_compl_active(void) |
| 1863 | { |
| 1864 | return compl_started; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
glepnir | 8d0bb6d | 2024-12-24 09:44:35 +0100 | [diff] [blame] | 1868 | * Return True when wp is the actual completion window |
| 1869 | */ |
| 1870 | int |
| 1871 | ins_compl_win_active(win_T *wp UNUSED) |
| 1872 | { |
| 1873 | return ins_compl_active() |
| 1874 | #if defined(FEAT_QUICKFIX) |
| 1875 | && (!wp->w_p_pvw |
| 1876 | # ifdef FEAT_PROP_POPUP |
| 1877 | && !(wp->w_popup_flags & POPF_INFO) |
| 1878 | # endif |
| 1879 | ) |
| 1880 | #endif |
| 1881 | ; |
| 1882 | } |
| 1883 | |
| 1884 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1885 | * Selected one of the matches. When FALSE the match was edited or using the |
| 1886 | * longest common string. |
| 1887 | */ |
| 1888 | int |
| 1889 | ins_compl_used_match(void) |
| 1890 | { |
| 1891 | return compl_used_match; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * Initialize get longest common string. |
| 1896 | */ |
| 1897 | void |
| 1898 | ins_compl_init_get_longest(void) |
| 1899 | { |
| 1900 | compl_get_longest = FALSE; |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Returns TRUE when insert completion is interrupted. |
| 1905 | */ |
| 1906 | int |
| 1907 | ins_compl_interrupted(void) |
| 1908 | { |
| 1909 | return compl_interrupted; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * Returns TRUE if the <Enter> key selects a match in the completion popup |
| 1914 | * menu. |
| 1915 | */ |
| 1916 | int |
| 1917 | ins_compl_enter_selects(void) |
| 1918 | { |
| 1919 | return compl_enter_selects; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Return the column where the text starts that is being completed |
| 1924 | */ |
| 1925 | colnr_T |
| 1926 | ins_compl_col(void) |
| 1927 | { |
| 1928 | return compl_col; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1932 | * Return the length in bytes of the text being completed |
| 1933 | */ |
| 1934 | int |
| 1935 | ins_compl_len(void) |
| 1936 | { |
| 1937 | return compl_length; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1941 | * Delete one character before the cursor and show the subset of the matches |
| 1942 | * that match the word that is now before the cursor. |
| 1943 | * Returns the character to be used, NUL if the work is done and another char |
| 1944 | * to be got from the user. |
| 1945 | */ |
| 1946 | int |
| 1947 | ins_compl_bs(void) |
| 1948 | { |
| 1949 | char_u *line; |
| 1950 | char_u *p; |
| 1951 | |
| 1952 | line = ml_get_curline(); |
| 1953 | p = line + curwin->w_cursor.col; |
| 1954 | MB_PTR_BACK(line, p); |
| 1955 | |
| 1956 | // Stop completion when the whole word was deleted. For Omni completion |
| 1957 | // allow the word to be deleted, we won't match everything. |
| 1958 | // Respect the 'backspace' option. |
| 1959 | if ((int)(p - line) - (int)compl_col < 0 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1960 | || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni()) |
| 1961 | || ctrl_x_mode_eval() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1962 | || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col |
| 1963 | - compl_length < 0)) |
| 1964 | return K_BS; |
| 1965 | |
| 1966 | // Deleted more than what was used to find matches or didn't finish |
| 1967 | // finding all matches: need to look for matches all over again. |
| 1968 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 1969 | || ins_compl_need_restart()) |
| 1970 | ins_compl_restart(); |
| 1971 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1972 | VIM_CLEAR_STRING(compl_leader); |
| 1973 | compl_leader.length = (size_t)((p - line) - compl_col); |
| 1974 | compl_leader.string = vim_strnsave(line + compl_col, compl_leader.length); |
| 1975 | if (compl_leader.string == NULL) |
| 1976 | { |
| 1977 | compl_leader.length = 0; |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1978 | return K_BS; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 1979 | } |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1980 | |
| 1981 | ins_compl_new_leader(); |
| 1982 | if (compl_shown_match != NULL) |
| 1983 | // Make sure current match is not a hidden item. |
| 1984 | compl_curr_match = compl_shown_match; |
| 1985 | return NUL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 1990 | * be called. |
| 1991 | */ |
| 1992 | static int |
| 1993 | ins_compl_need_restart(void) |
| 1994 | { |
| 1995 | // Return TRUE if we didn't complete finding matches or when the |
| 1996 | // 'completefunc' returned "always" in the "refresh" dictionary item. |
| 1997 | return compl_was_interrupted |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1998 | || ((ctrl_x_mode_function() || ctrl_x_mode_omni()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1999 | && compl_opt_refresh_always); |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * Called after changing "compl_leader". |
| 2004 | * Show the popup menu with a different set of matches. |
| 2005 | * May also search for matches again if the previous search was interrupted. |
| 2006 | */ |
| 2007 | static void |
| 2008 | ins_compl_new_leader(void) |
| 2009 | { |
| 2010 | ins_compl_del_pum(); |
| 2011 | ins_compl_delete(); |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 2012 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2013 | compl_used_match = FALSE; |
| 2014 | |
| 2015 | if (compl_started) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2016 | ins_compl_set_original_text(compl_leader.string, compl_leader.length); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2017 | else |
| 2018 | { |
| 2019 | #ifdef FEAT_SPELL |
| 2020 | spell_bad_len = 0; // need to redetect bad word |
| 2021 | #endif |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2022 | // Matches were cleared, need to search for them now. Before drawing |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2023 | // the popup menu display the changed text before the cursor. Set |
| 2024 | // "compl_restarting" to avoid that the first match is inserted. |
| 2025 | pum_call_update_screen(); |
| 2026 | #ifdef FEAT_GUI |
| 2027 | if (gui.in_use) |
| 2028 | { |
| 2029 | // Show the cursor after the match, not after the redrawn text. |
| 2030 | setcursor(); |
| 2031 | out_flush_cursor(FALSE, FALSE); |
| 2032 | } |
| 2033 | #endif |
| 2034 | compl_restarting = TRUE; |
| 2035 | if (ins_complete(Ctrl_N, TRUE) == FAIL) |
| 2036 | compl_cont_status = 0; |
| 2037 | compl_restarting = FALSE; |
| 2038 | } |
| 2039 | |
| 2040 | compl_enter_selects = !compl_used_match; |
| 2041 | |
| 2042 | // Show the popup menu with a different set of matches. |
| 2043 | ins_compl_show_pum(); |
| 2044 | |
| 2045 | // Don't let Enter select the original text when there is no popup menu. |
| 2046 | if (compl_match_array == NULL) |
| 2047 | compl_enter_selects = FALSE; |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * Return the length of the completion, from the completion start column to |
| 2052 | * the cursor column. Making sure it never goes below zero. |
| 2053 | */ |
| 2054 | static int |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2055 | get_compl_len(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2056 | { |
| 2057 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
| 2058 | |
| 2059 | if (off < 0) |
| 2060 | return 0; |
| 2061 | return off; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * Append one character to the match leader. May reduce the number of |
| 2066 | * matches. |
| 2067 | */ |
| 2068 | void |
| 2069 | ins_compl_addleader(int c) |
| 2070 | { |
| 2071 | int cc; |
| 2072 | |
| 2073 | if (stop_arrow() == FAIL) |
| 2074 | return; |
| 2075 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2076 | { |
| 2077 | char_u buf[MB_MAXBYTES + 1]; |
| 2078 | |
| 2079 | (*mb_char2bytes)(c, buf); |
| 2080 | buf[cc] = NUL; |
| 2081 | ins_char_bytes(buf, cc); |
| 2082 | if (compl_opt_refresh_always) |
| 2083 | AppendToRedobuff(buf); |
| 2084 | } |
| 2085 | else |
| 2086 | { |
| 2087 | ins_char(c); |
| 2088 | if (compl_opt_refresh_always) |
| 2089 | AppendCharToRedobuff(c); |
| 2090 | } |
| 2091 | |
| 2092 | // If we didn't complete finding matches we must search again. |
| 2093 | if (ins_compl_need_restart()) |
| 2094 | ins_compl_restart(); |
| 2095 | |
| 2096 | // When 'always' is set, don't reset compl_leader. While completing, |
| 2097 | // cursor doesn't point original position, changing compl_leader would |
| 2098 | // break redo. |
| 2099 | if (!compl_opt_refresh_always) |
| 2100 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2101 | VIM_CLEAR_STRING(compl_leader); |
| 2102 | compl_leader.length = (size_t)(curwin->w_cursor.col - compl_col); |
| 2103 | compl_leader.string = vim_strnsave(ml_get_curline() + compl_col, |
| 2104 | compl_leader.length); |
| 2105 | if (compl_leader.string == NULL) |
| 2106 | { |
| 2107 | compl_leader.length = 0; |
| 2108 | return; |
| 2109 | } |
| 2110 | |
| 2111 | ins_compl_new_leader(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 2117 | * BS or a key was typed while still searching for matches. |
| 2118 | */ |
| 2119 | static void |
| 2120 | ins_compl_restart(void) |
| 2121 | { |
| 2122 | ins_compl_free(); |
| 2123 | compl_started = FALSE; |
| 2124 | compl_matches = 0; |
| 2125 | compl_cont_status = 0; |
| 2126 | compl_cont_mode = 0; |
| 2127 | } |
| 2128 | |
| 2129 | /* |
| 2130 | * Set the first match, the original text. |
| 2131 | */ |
| 2132 | static void |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2133 | ins_compl_set_original_text(char_u *str, size_t len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2134 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2135 | // Replace the original text entry. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2136 | // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly |
| 2137 | // be at the last item for backward completion |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2138 | if (match_at_original_text(compl_first_match)) // safety check |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2139 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2140 | char_u *p = vim_strnsave(str, len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2141 | if (p != NULL) |
| 2142 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2143 | VIM_CLEAR_STRING(compl_first_match->cp_str); |
| 2144 | compl_first_match->cp_str.string = p; |
| 2145 | compl_first_match->cp_str.length = len; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | else if (compl_first_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2149 | && match_at_original_text(compl_first_match->cp_prev)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2150 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2151 | char_u *p = vim_strnsave(str, len); |
| 2152 | if (p != NULL) |
| 2153 | { |
| 2154 | VIM_CLEAR_STRING(compl_first_match->cp_prev->cp_str); |
| 2155 | compl_first_match->cp_prev->cp_str.string = p; |
| 2156 | compl_first_match->cp_prev->cp_str.length = len; |
| 2157 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | * Append one character to the match leader. May reduce the number of |
| 2163 | * matches. |
| 2164 | */ |
| 2165 | void |
| 2166 | ins_compl_addfrommatch(void) |
| 2167 | { |
| 2168 | char_u *p; |
| 2169 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
| 2170 | int c; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2171 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2172 | p = compl_shown_match->cp_str.string; |
| 2173 | 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] | 2174 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2175 | size_t plen; |
| 2176 | compl_T *cp; |
| 2177 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2178 | // When still at the original match use the first entry that matches |
| 2179 | // the leader. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2180 | if (!match_at_original_text(compl_shown_match)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2181 | return; |
| 2182 | |
| 2183 | p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2184 | plen = 0; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2185 | for (cp = compl_shown_match->cp_next; cp != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2186 | && !is_first_match(cp); cp = cp->cp_next) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2187 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2188 | if (compl_leader.string == NULL |
| 2189 | || ins_compl_equal(cp, compl_leader.string, |
| 2190 | (int)compl_leader.length)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2191 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2192 | p = cp->cp_str.string; |
| 2193 | plen = cp->cp_str.length; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 2194 | break; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2195 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2196 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2197 | if (p == NULL || (int)plen <= len) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2198 | return; |
| 2199 | } |
| 2200 | p += len; |
| 2201 | c = PTR2CHAR(p); |
| 2202 | ins_compl_addleader(c); |
| 2203 | } |
| 2204 | |
| 2205 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2206 | * 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] | 2207 | * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre, |
| 2208 | * compl_cont_mode and compl_cont_status. |
| 2209 | * Returns TRUE when the character is not to be inserted. |
| 2210 | */ |
| 2211 | static int |
| 2212 | set_ctrl_x_mode(int c) |
| 2213 | { |
| 2214 | int retval = FALSE; |
| 2215 | |
| 2216 | switch (c) |
| 2217 | { |
| 2218 | case Ctrl_E: |
| 2219 | case Ctrl_Y: |
| 2220 | // scroll the window one line up or down |
| 2221 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2222 | if (!(State & REPLACE_FLAG)) |
| 2223 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2224 | else |
| 2225 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2226 | edit_submode_pre = NULL; |
| 2227 | showmode(); |
| 2228 | break; |
| 2229 | case Ctrl_L: |
| 2230 | // complete whole line |
| 2231 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2232 | break; |
| 2233 | case Ctrl_F: |
| 2234 | // complete filenames |
| 2235 | ctrl_x_mode = CTRL_X_FILES; |
| 2236 | break; |
| 2237 | case Ctrl_K: |
zeertzjq | 5fb3aab | 2022-08-24 16:48:23 +0100 | [diff] [blame] | 2238 | // complete words from a dictionary |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2239 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2240 | break; |
| 2241 | case Ctrl_R: |
| 2242 | // Register insertion without exiting CTRL-X mode |
| 2243 | // Simply allow ^R to happen without affecting ^X mode |
| 2244 | break; |
| 2245 | case Ctrl_T: |
| 2246 | // complete words from a thesaurus |
| 2247 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2248 | break; |
| 2249 | #ifdef FEAT_COMPL_FUNC |
| 2250 | case Ctrl_U: |
| 2251 | // user defined completion |
| 2252 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2253 | break; |
| 2254 | case Ctrl_O: |
| 2255 | // omni completion |
| 2256 | ctrl_x_mode = CTRL_X_OMNI; |
| 2257 | break; |
| 2258 | #endif |
| 2259 | case 's': |
| 2260 | case Ctrl_S: |
| 2261 | // complete spelling suggestions |
| 2262 | ctrl_x_mode = CTRL_X_SPELL; |
| 2263 | #ifdef FEAT_SPELL |
| 2264 | ++emsg_off; // Avoid getting the E756 error twice. |
| 2265 | spell_back_to_badword(); |
| 2266 | --emsg_off; |
| 2267 | #endif |
| 2268 | break; |
| 2269 | case Ctrl_RSB: |
| 2270 | // complete tag names |
| 2271 | ctrl_x_mode = CTRL_X_TAGS; |
| 2272 | break; |
| 2273 | #ifdef FEAT_FIND_ID |
| 2274 | case Ctrl_I: |
| 2275 | case K_S_TAB: |
| 2276 | // complete keywords from included files |
| 2277 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2278 | break; |
| 2279 | case Ctrl_D: |
| 2280 | // complete definitions from included files |
| 2281 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2282 | break; |
| 2283 | #endif |
| 2284 | case Ctrl_V: |
| 2285 | case Ctrl_Q: |
| 2286 | // complete vim commands |
| 2287 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2288 | break; |
| 2289 | case Ctrl_Z: |
| 2290 | // stop completion |
| 2291 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2292 | edit_submode = NULL; |
| 2293 | showmode(); |
| 2294 | retval = TRUE; |
| 2295 | break; |
| 2296 | case Ctrl_P: |
| 2297 | case Ctrl_N: |
| 2298 | // ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2299 | // just started ^X mode, or there were enough ^X's to cancel |
| 2300 | // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2301 | // do normal expansion when interrupting a different mode (say |
| 2302 | // ^X^F^X^P or ^P^X^X^P, see below) |
| 2303 | // nothing changes if interrupting mode 0, (eg, the flag |
| 2304 | // doesn't change when going to ADDING mode -- Acevedo |
| 2305 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2306 | compl_cont_status |= CONT_LOCAL; |
| 2307 | else if (compl_cont_mode != 0) |
| 2308 | compl_cont_status &= ~CONT_LOCAL; |
| 2309 | // FALLTHROUGH |
| 2310 | default: |
| 2311 | // If we have typed at least 2 ^X's... for modes != 0, we set |
| 2312 | // compl_cont_status = 0 (eg, as if we had just started ^X |
| 2313 | // mode). |
| 2314 | // For mode 0, we set "compl_cont_mode" to an impossible |
| 2315 | // value, in both cases ^X^X can be used to restart the same |
| 2316 | // mode (avoiding ADDING mode). |
| 2317 | // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2318 | // 'complete' and local ^P expansions respectively. |
| 2319 | // In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2320 | // mode -- Acevedo |
| 2321 | if (c == Ctrl_X) |
| 2322 | { |
| 2323 | if (compl_cont_mode != 0) |
| 2324 | compl_cont_status = 0; |
| 2325 | else |
| 2326 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
| 2327 | } |
| 2328 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2329 | edit_submode = NULL; |
| 2330 | showmode(); |
| 2331 | break; |
| 2332 | } |
| 2333 | |
| 2334 | return retval; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2338 | * Trigger CompleteDone event and adds relevant information to v:event |
| 2339 | */ |
| 2340 | static void |
| 2341 | trigger_complete_done_event(int mode UNUSED, char_u *word UNUSED) |
| 2342 | { |
| 2343 | #if defined(FEAT_EVAL) |
| 2344 | save_v_event_T save_v_event; |
| 2345 | dict_T *v_event = get_v_event(&save_v_event); |
| 2346 | char_u *mode_str = NULL; |
| 2347 | |
| 2348 | mode = mode & ~CTRL_X_WANT_IDENT; |
| 2349 | if (ctrl_x_mode_names[mode]) |
| 2350 | mode_str = (char_u *)ctrl_x_mode_names[mode]; |
| 2351 | |
| 2352 | (void)dict_add_string(v_event, "complete_word", |
| 2353 | word == NULL ? (char_u *)"" : word); |
| 2354 | (void)dict_add_string(v_event, "complete_type", |
| 2355 | mode_str != NULL ? mode_str : (char_u *)""); |
| 2356 | |
| 2357 | dict_set_items_ro(v_event); |
| 2358 | #endif |
| 2359 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
| 2360 | |
| 2361 | #if defined(FEAT_EVAL) |
| 2362 | restore_v_event(v_event, &save_v_event); |
| 2363 | #endif |
| 2364 | } |
| 2365 | |
| 2366 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2367 | * Stop insert completion mode |
| 2368 | */ |
| 2369 | static int |
| 2370 | ins_compl_stop(int c, int prev_mode, int retval) |
| 2371 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2372 | int want_cindent; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2373 | char_u *word = NULL; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Get here when we have finished typing a sequence of ^N and |
| 2376 | // ^P or other completion characters in CTRL-X mode. Free up |
| 2377 | // memory that was used, and make sure we can redo the insert. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2378 | if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2379 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2380 | char_u *ptr; |
| 2381 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2382 | // If any of the original typed text has been changed, eg when |
| 2383 | // ignorecase is set, we must add back-spaces to the redo |
| 2384 | // buffer. We add as few as necessary to delete just the part |
| 2385 | // of the original text that has changed. |
| 2386 | // When using the longest match, edited the match or used |
| 2387 | // CTRL-E then don't use the current match. |
| 2388 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2389 | ptr = compl_curr_match->cp_str.string; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2390 | else |
| 2391 | ptr = NULL; |
| 2392 | ins_compl_fixRedoBufForLeader(ptr); |
| 2393 | } |
| 2394 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2395 | want_cindent = (get_can_cindent() && cindent_on()); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2396 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2397 | // When completing whole lines: fix indent for 'cindent'. |
| 2398 | // Otherwise, break line if it's too long. |
| 2399 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
| 2400 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2401 | // re-indent the current line |
| 2402 | if (want_cindent) |
| 2403 | { |
| 2404 | do_c_expr_indent(); |
| 2405 | want_cindent = FALSE; // don't do it again |
| 2406 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2407 | } |
| 2408 | else |
| 2409 | { |
| 2410 | int prev_col = curwin->w_cursor.col; |
| 2411 | |
| 2412 | // put the cursor on the last char, for 'tw' formatting |
| 2413 | if (prev_col > 0) |
| 2414 | dec_cursor(); |
| 2415 | // only format when something was inserted |
| 2416 | if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) |
| 2417 | insertchar(NUL, 0, -1); |
| 2418 | if (prev_col > 0 |
| 2419 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 2420 | inc_cursor(); |
| 2421 | } |
| 2422 | |
| 2423 | // If the popup menu is displayed pressing CTRL-Y means accepting |
| 2424 | // the selection without inserting anything. When |
| 2425 | // compl_enter_selects is set the Enter key does the same. |
| 2426 | if ((c == Ctrl_Y || (compl_enter_selects |
| 2427 | && (c == CAR || c == K_KENTER || c == NL))) |
| 2428 | && pum_visible()) |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2429 | { |
| 2430 | word = vim_strsave(compl_shown_match->cp_str.string); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2431 | retval = TRUE; |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2432 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2433 | |
| 2434 | // CTRL-E means completion is Ended, go back to the typed text. |
| 2435 | // but only do this, if the Popup is still visible |
| 2436 | if (c == Ctrl_E) |
| 2437 | { |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2438 | char_u *p = NULL; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2439 | size_t plen = 0; |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2440 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2441 | ins_compl_delete(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2442 | if (compl_leader.string != NULL) |
| 2443 | { |
| 2444 | p = compl_leader.string; |
| 2445 | plen = compl_leader.length; |
| 2446 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2447 | else if (compl_first_match != NULL) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2448 | { |
| 2449 | p = compl_orig_text.string; |
| 2450 | plen = compl_orig_text.length; |
| 2451 | } |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2452 | if (p != NULL) |
| 2453 | { |
| 2454 | int compl_len = get_compl_len(); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2455 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2456 | if ((int)plen > compl_len) |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 2457 | ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len); |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2458 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2459 | retval = TRUE; |
| 2460 | } |
| 2461 | |
| 2462 | auto_format(FALSE, TRUE); |
| 2463 | |
| 2464 | // Trigger the CompleteDonePre event to give scripts a chance to |
| 2465 | // act upon the completion before clearing the info, and restore |
| 2466 | // ctrl_x_mode, so that complete_info() can be used. |
| 2467 | ctrl_x_mode = prev_mode; |
| 2468 | ins_apply_autocmds(EVENT_COMPLETEDONEPRE); |
| 2469 | |
| 2470 | ins_compl_free(); |
| 2471 | compl_started = FALSE; |
| 2472 | compl_matches = 0; |
| 2473 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 2474 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 2475 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2476 | compl_enter_selects = FALSE; |
| 2477 | if (edit_submode != NULL) |
| 2478 | { |
| 2479 | edit_submode = NULL; |
| 2480 | showmode(); |
| 2481 | } |
| 2482 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2483 | if (c == Ctrl_C && cmdwin_type != 0) |
| 2484 | // Avoid the popup menu remains displayed when leaving the |
| 2485 | // command line window. |
| 2486 | update_screen(0); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2487 | // Indent now if a key was typed that is in 'cinkeys'. |
| 2488 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2489 | do_c_expr_indent(); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2490 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2491 | // upon the end of completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2492 | trigger_complete_done_event(prev_mode, word); |
| 2493 | vim_free(word); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2494 | |
| 2495 | return retval; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2499 | * Prepare for Insert mode completion, or stop it. |
| 2500 | * Called just after typing a character in Insert mode. |
| 2501 | * Returns TRUE when the character is not to be inserted; |
| 2502 | */ |
| 2503 | int |
| 2504 | ins_compl_prep(int c) |
| 2505 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2506 | int retval = FALSE; |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 2507 | int prev_mode = ctrl_x_mode; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2508 | |
| 2509 | // Forget any previous 'special' messages if this is actually |
| 2510 | // a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2511 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2512 | edit_submode_extra = NULL; |
| 2513 | |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2514 | // Ignore end of Select mode mapping and mouse scroll/movement. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2515 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 2516 | || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_MOUSEMOVE |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 2517 | || c == K_COMMAND || c == K_SCRIPT_COMMAND) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2518 | return retval; |
| 2519 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2520 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | f0bc15c | 2019-08-18 19:23:45 +0200 | [diff] [blame] | 2521 | // Ignore mouse events in a popup window |
| 2522 | if (is_mouse_key(c)) |
| 2523 | { |
| 2524 | // Ignore drag and release events, the position does not need to be in |
| 2525 | // the popup and it may have just closed. |
| 2526 | if (c == K_LEFTRELEASE |
| 2527 | || c == K_LEFTRELEASE_NM |
| 2528 | || c == K_MIDDLERELEASE |
| 2529 | || c == K_RIGHTRELEASE |
| 2530 | || c == K_X1RELEASE |
| 2531 | || c == K_X2RELEASE |
| 2532 | || c == K_LEFTDRAG |
| 2533 | || c == K_MIDDLEDRAG |
| 2534 | || c == K_RIGHTDRAG |
| 2535 | || c == K_X1DRAG |
| 2536 | || c == K_X2DRAG) |
| 2537 | return retval; |
| 2538 | if (popup_visible) |
| 2539 | { |
| 2540 | int row = mouse_row; |
| 2541 | int col = mouse_col; |
| 2542 | win_T *wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2543 | |
| 2544 | if (wp != NULL && WIN_IS_POPUP(wp)) |
| 2545 | return retval; |
| 2546 | } |
| 2547 | } |
| 2548 | #endif |
| 2549 | |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 2550 | if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X) |
| 2551 | { |
| 2552 | if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c) |
| 2553 | || !vim_is_ctrl_x_key(c)) |
| 2554 | { |
| 2555 | // Not starting another completion mode. |
| 2556 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2557 | |
| 2558 | // CTRL-X CTRL-Z should stop completion without inserting anything |
| 2559 | if (c == Ctrl_Z) |
| 2560 | retval = TRUE; |
| 2561 | } |
| 2562 | else |
| 2563 | { |
| 2564 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2565 | |
| 2566 | // Other CTRL-X keys first stop completion, then start another |
| 2567 | // completion mode. |
| 2568 | ins_compl_prep(' '); |
| 2569 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2570 | } |
| 2571 | } |
| 2572 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2573 | // Set "compl_get_longest" when finding the first matches. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2574 | if (ctrl_x_mode_not_defined_yet() |
| 2575 | || (ctrl_x_mode_normal() && !compl_started)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2576 | { |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 2577 | compl_get_longest = (get_cot_flags() & COT_LONGEST) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2578 | compl_used_match = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2579 | } |
| 2580 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2581 | if (ctrl_x_mode_not_defined_yet()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2582 | // We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2583 | // it will be yet. Now we decide. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2584 | retval = set_ctrl_x_mode(c); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2585 | else if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2586 | { |
| 2587 | // We're already in CTRL-X mode, do we stay in it? |
| 2588 | if (!vim_is_ctrl_x_key(c)) |
| 2589 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2590 | if (ctrl_x_mode_scroll()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2591 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2592 | else |
| 2593 | ctrl_x_mode = CTRL_X_FINISHED; |
| 2594 | edit_submode = NULL; |
| 2595 | } |
| 2596 | showmode(); |
| 2597 | } |
| 2598 | |
| 2599 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
| 2600 | { |
| 2601 | // Show error message from attempted keyword completion (probably |
| 2602 | // 'Pattern not found') until another key is hit, then go back to |
| 2603 | // showing what mode we are in. |
| 2604 | showmode(); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2605 | if ((ctrl_x_mode_normal() && c != Ctrl_N && c != Ctrl_P |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2606 | && c != Ctrl_R && !ins_compl_pum_key(c)) |
| 2607 | || ctrl_x_mode == CTRL_X_FINISHED) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 2608 | retval = ins_compl_stop(c, prev_mode, retval); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2609 | } |
| 2610 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 2611 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2612 | // upon the (possibly failed) completion. |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 2613 | trigger_complete_done_event(ctrl_x_mode, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2614 | |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 2615 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 2616 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2617 | // reset continue_* if we left expansion-mode, if we stay they'll be |
| 2618 | // (re)set properly in ins_complete() |
| 2619 | if (!vim_is_ctrl_x_key(c)) |
| 2620 | { |
| 2621 | compl_cont_status = 0; |
| 2622 | compl_cont_mode = 0; |
| 2623 | } |
| 2624 | |
| 2625 | return retval; |
| 2626 | } |
| 2627 | |
| 2628 | /* |
| 2629 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 2630 | * text. This inserts backspaces and appends the changed text. |
| 2631 | * "ptr" is the known leader text or NUL. |
| 2632 | */ |
| 2633 | static void |
| 2634 | ins_compl_fixRedoBufForLeader(char_u *ptr_arg) |
| 2635 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2636 | int len = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2637 | char_u *p; |
| 2638 | char_u *ptr = ptr_arg; |
| 2639 | |
| 2640 | if (ptr == NULL) |
| 2641 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2642 | if (compl_leader.string != NULL) |
| 2643 | ptr = compl_leader.string; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2644 | else |
| 2645 | return; // nothing to do |
| 2646 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2647 | if (compl_orig_text.string != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2648 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 2649 | p = compl_orig_text.string; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2650 | for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) |
| 2651 | ; |
| 2652 | if (len > 0) |
| 2653 | len -= (*mb_head_off)(p, p + len); |
| 2654 | for (p += len; *p != NUL; MB_PTR_ADV(p)) |
| 2655 | AppendCharToRedobuff(K_BS); |
| 2656 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2657 | if (ptr != NULL) |
| 2658 | AppendToRedobuffLit(ptr + len, -1); |
| 2659 | } |
| 2660 | |
| 2661 | /* |
| 2662 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 2663 | * (depending on flag) starting from buf and looking for a non-scanned |
| 2664 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 2665 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 2666 | * |
| 2667 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 2668 | */ |
| 2669 | static buf_T * |
| 2670 | ins_compl_next_buf(buf_T *buf, int flag) |
| 2671 | { |
| 2672 | static win_T *wp = NULL; |
| 2673 | |
| 2674 | if (flag == 'w') // just windows |
| 2675 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2676 | if (buf == curbuf || !win_valid(wp)) |
| 2677 | // first call for this flag/expansion or window was closed |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2678 | wp = curwin; |
| 2679 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
| 2680 | && wp->w_buffer->b_scanned) |
| 2681 | ; |
| 2682 | buf = wp->w_buffer; |
| 2683 | } |
| 2684 | else |
| 2685 | // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 2686 | // (unlisted buffers) |
| 2687 | // When completing whole lines skip unloaded buffers. |
| 2688 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
| 2689 | && ((flag == 'U' |
| 2690 | ? buf->b_p_bl |
| 2691 | : (!buf->b_p_bl |
| 2692 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
| 2693 | || buf->b_scanned)) |
| 2694 | ; |
| 2695 | return buf; |
| 2696 | } |
| 2697 | |
| 2698 | #ifdef FEAT_COMPL_FUNC |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2699 | |
| 2700 | # ifdef FEAT_EVAL |
| 2701 | static callback_T cfu_cb; // 'completefunc' callback function |
| 2702 | static callback_T ofu_cb; // 'omnifunc' callback function |
| 2703 | static callback_T tsrfu_cb; // 'thesaurusfunc' callback function |
| 2704 | # endif |
| 2705 | |
| 2706 | /* |
| 2707 | * Copy a global callback function to a buffer local callback. |
| 2708 | */ |
| 2709 | static void |
| 2710 | copy_global_to_buflocal_cb(callback_T *globcb, callback_T *bufcb) |
| 2711 | { |
| 2712 | free_callback(bufcb); |
| 2713 | if (globcb->cb_name != NULL && *globcb->cb_name != NUL) |
| 2714 | copy_callback(bufcb, globcb); |
| 2715 | } |
| 2716 | |
| 2717 | /* |
| 2718 | * Parse the 'completefunc' option value and set the callback function. |
| 2719 | * Invoked when the 'completefunc' option is set. The option value can be a |
| 2720 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 2721 | * lambda expression. |
| 2722 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2723 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2724 | did_set_completefunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2725 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2726 | if (option_set_callback_func(curbuf->b_p_cfu, &cfu_cb) == FAIL) |
| 2727 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2728 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2729 | set_buflocal_cfu_callback(curbuf); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2730 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2731 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | /* |
| 2735 | * Copy the global 'completefunc' callback function to the buffer-local |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2736 | * 'completefunc' callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2737 | */ |
| 2738 | void |
| 2739 | set_buflocal_cfu_callback(buf_T *buf UNUSED) |
| 2740 | { |
| 2741 | # ifdef FEAT_EVAL |
| 2742 | copy_global_to_buflocal_cb(&cfu_cb, &buf->b_cfu_cb); |
| 2743 | # endif |
| 2744 | } |
| 2745 | |
| 2746 | /* |
| 2747 | * Parse the 'omnifunc' option value and set the callback function. |
| 2748 | * Invoked when the 'omnifunc' option is set. The option value can be a |
| 2749 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 2750 | * lambda expression. |
| 2751 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2752 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2753 | did_set_omnifunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2754 | { |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2755 | if (option_set_callback_func(curbuf->b_p_ofu, &ofu_cb) == FAIL) |
| 2756 | return e_invalid_argument; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2757 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2758 | set_buflocal_ofu_callback(curbuf); |
| 2759 | return NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * Copy the global 'omnifunc' callback function to the buffer-local 'omnifunc' |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2764 | * callback for "buf". |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2765 | */ |
| 2766 | void |
| 2767 | set_buflocal_ofu_callback(buf_T *buf UNUSED) |
| 2768 | { |
| 2769 | # ifdef FEAT_EVAL |
| 2770 | copy_global_to_buflocal_cb(&ofu_cb, &buf->b_ofu_cb); |
| 2771 | # endif |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | * Parse the 'thesaurusfunc' option value and set the callback function. |
| 2776 | * Invoked when the 'thesaurusfunc' option is set. The option value can be a |
| 2777 | * name of a function (string), or function(<name>) or funcref(<name>) or a |
| 2778 | * lambda expression. |
| 2779 | */ |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2780 | char * |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2781 | did_set_thesaurusfunc(optset_T *args UNUSED) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2782 | { |
| 2783 | int retval; |
| 2784 | |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 2785 | if (args->os_flags & OPT_LOCAL) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2786 | // buffer-local option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2787 | retval = option_set_callback_func(curbuf->b_p_tsrfu, |
| 2788 | &curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2789 | else |
| 2790 | { |
| 2791 | // global option set |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2792 | retval = option_set_callback_func(p_tsrfu, &tsrfu_cb); |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 2793 | // when using :set, free the local callback |
| 2794 | if (!(args->os_flags & OPT_GLOBAL)) |
| 2795 | free_callback(&curbuf->b_tsrfu_cb); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Yegappan Lakshmanan | f2e30d0 | 2023-01-30 13:04:42 +0000 | [diff] [blame] | 2798 | return retval == FAIL ? e_invalid_argument : NULL; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2801 | /* |
| 2802 | * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2803 | * "copyID" so that they are not garbage collected. |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2804 | */ |
| 2805 | int |
| 2806 | set_ref_in_insexpand_funcs(int copyID) |
| 2807 | { |
| 2808 | int abort = FALSE; |
| 2809 | |
| 2810 | abort = set_ref_in_callback(&cfu_cb, copyID); |
| 2811 | abort = abort || set_ref_in_callback(&ofu_cb, copyID); |
| 2812 | abort = abort || set_ref_in_callback(&tsrfu_cb, copyID); |
| 2813 | |
| 2814 | return abort; |
| 2815 | } |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2816 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2817 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2818 | * Get the user-defined completion function name for completion "type" |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 2819 | */ |
| 2820 | static char_u * |
| 2821 | get_complete_funcname(int type) |
| 2822 | { |
| 2823 | switch (type) |
| 2824 | { |
| 2825 | case CTRL_X_FUNCTION: |
| 2826 | return curbuf->b_p_cfu; |
| 2827 | case CTRL_X_OMNI: |
| 2828 | return curbuf->b_p_ofu; |
| 2829 | case CTRL_X_THESAURUS: |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 2830 | return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu; |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 2831 | default: |
| 2832 | return (char_u *)""; |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | /* |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2837 | * Get the callback to use for insert mode completion. |
| 2838 | */ |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 2839 | static callback_T * |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2840 | get_insert_callback(int type) |
| 2841 | { |
| 2842 | if (type == CTRL_X_FUNCTION) |
| 2843 | return &curbuf->b_cfu_cb; |
| 2844 | if (type == CTRL_X_OMNI) |
| 2845 | return &curbuf->b_ofu_cb; |
| 2846 | // CTRL_X_THESAURUS |
| 2847 | return (*curbuf->b_p_tsrfu != NUL) ? &curbuf->b_tsrfu_cb : &tsrfu_cb; |
| 2848 | } |
| 2849 | |
| 2850 | /* |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 2851 | * Execute user defined complete function 'completefunc', 'omnifunc' or |
| 2852 | * 'thesaurusfunc', and get matches in "matches". |
| 2853 | * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2854 | */ |
| 2855 | static void |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 2856 | expand_by_function(int type, char_u *base) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2857 | { |
| 2858 | list_T *matchlist = NULL; |
| 2859 | dict_T *matchdict = NULL; |
| 2860 | typval_T args[3]; |
| 2861 | char_u *funcname; |
| 2862 | pos_T pos; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2863 | callback_T *cb; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2864 | typval_T rettv; |
| 2865 | int save_State = State; |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2866 | int retval; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2867 | |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 2868 | funcname = get_complete_funcname(type); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2869 | if (*funcname == NUL) |
| 2870 | return; |
| 2871 | |
| 2872 | // Call 'completefunc' to obtain the list of matches. |
| 2873 | args[0].v_type = VAR_NUMBER; |
| 2874 | args[0].vval.v_number = 0; |
| 2875 | args[1].v_type = VAR_STRING; |
| 2876 | args[1].vval.v_string = base != NULL ? base : (char_u *)""; |
| 2877 | args[2].v_type = VAR_UNKNOWN; |
| 2878 | |
| 2879 | pos = curwin->w_cursor; |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 2880 | // Lock the text to avoid weird things from happening. Also disallow |
| 2881 | // switching to another window, it should not be needed and may end up in |
| 2882 | // Insert mode in another buffer. |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 2883 | ++textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2884 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2885 | cb = get_insert_callback(type); |
| 2886 | retval = call_callback(cb, 0, &rettv, 2, args); |
| 2887 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2888 | // Call a function, which returns a list or dict. |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2889 | if (retval == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2890 | { |
| 2891 | switch (rettv.v_type) |
| 2892 | { |
| 2893 | case VAR_LIST: |
| 2894 | matchlist = rettv.vval.v_list; |
| 2895 | break; |
| 2896 | case VAR_DICT: |
| 2897 | matchdict = rettv.vval.v_dict; |
| 2898 | break; |
| 2899 | case VAR_SPECIAL: |
| 2900 | if (rettv.vval.v_number == VVAL_NONE) |
| 2901 | compl_opt_suppress_empty = TRUE; |
| 2902 | // FALLTHROUGH |
| 2903 | default: |
| 2904 | // TODO: Give error message? |
| 2905 | clear_tv(&rettv); |
| 2906 | break; |
| 2907 | } |
| 2908 | } |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 2909 | --textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2910 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2911 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 2912 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2913 | validate_cursor(); |
| 2914 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 2915 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 2916 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2917 | goto theend; |
| 2918 | } |
| 2919 | |
| 2920 | if (matchlist != NULL) |
| 2921 | ins_compl_add_list(matchlist); |
| 2922 | else if (matchdict != NULL) |
| 2923 | ins_compl_add_dict(matchdict); |
| 2924 | |
| 2925 | theend: |
| 2926 | // Restore State, it might have been changed. |
| 2927 | State = save_State; |
| 2928 | |
| 2929 | if (matchdict != NULL) |
| 2930 | dict_unref(matchdict); |
| 2931 | if (matchlist != NULL) |
| 2932 | list_unref(matchlist); |
| 2933 | } |
| 2934 | #endif // FEAT_COMPL_FUNC |
| 2935 | |
| 2936 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 2937 | |
| 2938 | static inline int |
| 2939 | get_user_highlight_attr(char_u *hlname) |
| 2940 | { |
| 2941 | if (hlname != NULL && *hlname != NUL) |
| 2942 | return syn_name2attr(hlname); |
| 2943 | return -1; |
| 2944 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2945 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2946 | * Add a match to the list of matches from a typeval_T. |
| 2947 | * If the given string is already in the list of completions, then return |
| 2948 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 2949 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2950 | * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck(). |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2951 | */ |
| 2952 | static int |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2953 | ins_compl_add_tv(typval_T *tv, int dir, int fast) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2954 | { |
| 2955 | char_u *word; |
| 2956 | int dup = FALSE; |
| 2957 | int empty = FALSE; |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2958 | int flags = fast ? CP_FAST : 0; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2959 | char_u *(cptext[CPT_COUNT]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2960 | typval_T user_data; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 2961 | int status; |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 2962 | char_u *user_abbr_hlname; |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 2963 | char_u *user_kind_hlname; |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 2964 | int user_hl[2] = { -1, -1 }; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2965 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2966 | user_data.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2967 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 2968 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 2969 | word = dict_get_string(tv->vval.v_dict, "word", FALSE); |
| 2970 | cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, "abbr", FALSE); |
| 2971 | cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE); |
| 2972 | cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE); |
| 2973 | cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 2974 | |
glepnir | 0fe17f8 | 2024-10-08 22:26:44 +0200 | [diff] [blame] | 2975 | user_abbr_hlname = dict_get_string(tv->vval.v_dict, "abbr_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 2976 | user_hl[0] = get_user_highlight_attr(user_abbr_hlname); |
glepnir | 38f99a1 | 2024-08-23 18:31:06 +0200 | [diff] [blame] | 2977 | |
| 2978 | user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE); |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 2979 | user_hl[1] = get_user_highlight_attr(user_kind_hlname); |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 2980 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 2981 | dict_get_tv(tv->vval.v_dict, "user_data", &user_data); |
| 2982 | if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL |
| 2983 | && dict_get_number(tv->vval.v_dict, "icase")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2984 | flags |= CP_ICASE; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 2985 | if (dict_get_string(tv->vval.v_dict, "dup", FALSE) != NULL) |
| 2986 | dup = dict_get_number(tv->vval.v_dict, "dup"); |
| 2987 | if (dict_get_string(tv->vval.v_dict, "empty", FALSE) != NULL) |
| 2988 | empty = dict_get_number(tv->vval.v_dict, "empty"); |
| 2989 | if (dict_get_string(tv->vval.v_dict, "equal", FALSE) != NULL |
| 2990 | && dict_get_number(tv->vval.v_dict, "equal")) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2991 | flags |= CP_EQUAL; |
| 2992 | } |
| 2993 | else |
| 2994 | { |
| 2995 | word = tv_get_string_chk(tv); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 2996 | CLEAR_FIELD(cptext); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2997 | } |
| 2998 | if (word == NULL || (!empty && *word == NUL)) |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 2999 | { |
| 3000 | clear_tv(&user_data); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3001 | return FAIL; |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3002 | } |
glepnir | 508e785 | 2024-07-25 21:39:08 +0200 | [diff] [blame] | 3003 | status = ins_compl_add(word, -1, NULL, cptext, |
glepnir | 80b6620 | 2024-11-27 21:53:53 +0100 | [diff] [blame] | 3004 | &user_data, dir, flags, dup, user_hl); |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3005 | if (status != OK) |
| 3006 | clear_tv(&user_data); |
| 3007 | return status; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3011 | * Add completions from a list. |
| 3012 | */ |
| 3013 | static void |
| 3014 | ins_compl_add_list(list_T *list) |
| 3015 | { |
| 3016 | listitem_T *li; |
| 3017 | int dir = compl_direction; |
| 3018 | |
| 3019 | // Go through the List with matches and add each of them. |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3020 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3021 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3022 | { |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3023 | if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3024 | // if dir was BACKWARD then honor it just once |
| 3025 | dir = FORWARD; |
| 3026 | else if (did_emsg) |
| 3027 | break; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | * Add completions from a dict. |
| 3033 | */ |
| 3034 | static void |
| 3035 | ins_compl_add_dict(dict_T *dict) |
| 3036 | { |
| 3037 | dictitem_T *di_refresh; |
| 3038 | dictitem_T *di_words; |
| 3039 | |
| 3040 | // Check for optional "refresh" item. |
| 3041 | compl_opt_refresh_always = FALSE; |
| 3042 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 3043 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
| 3044 | { |
| 3045 | char_u *v = di_refresh->di_tv.vval.v_string; |
| 3046 | |
| 3047 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 3048 | compl_opt_refresh_always = TRUE; |
| 3049 | } |
| 3050 | |
| 3051 | // Add completions from a "words" list. |
| 3052 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 3053 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 3054 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
| 3055 | } |
| 3056 | |
| 3057 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3058 | * Start completion for the complete() function. |
| 3059 | * "startcol" is where the matched text starts (1 is first column). |
| 3060 | * "list" is the list of matches. |
| 3061 | */ |
| 3062 | static void |
| 3063 | set_completion(colnr_T startcol, list_T *list) |
| 3064 | { |
| 3065 | int save_w_wrow = curwin->w_wrow; |
| 3066 | int save_w_leftcol = curwin->w_leftcol; |
| 3067 | int flags = CP_ORIGINAL_TEXT; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 3068 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 3069 | int compl_longest = (cur_cot_flags & COT_LONGEST) != 0; |
| 3070 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 3071 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3072 | |
| 3073 | // If already doing completions stop it. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3074 | if (ctrl_x_mode_not_default()) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3075 | ins_compl_prep(' '); |
| 3076 | ins_compl_clear(); |
| 3077 | ins_compl_free(); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3078 | compl_get_longest = compl_longest; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3079 | |
| 3080 | compl_direction = FORWARD; |
| 3081 | if (startcol > curwin->w_cursor.col) |
| 3082 | startcol = curwin->w_cursor.col; |
| 3083 | compl_col = startcol; |
| 3084 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
| 3085 | // compl_pattern doesn't need to be set |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3086 | compl_orig_text.string = vim_strnsave(ml_get_curline() + compl_col, (size_t)compl_length); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3087 | if (p_ic) |
| 3088 | flags |= CP_ICASE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3089 | if (compl_orig_text.string == NULL) |
| 3090 | { |
| 3091 | compl_orig_text.length = 0; |
| 3092 | return; |
| 3093 | } |
| 3094 | compl_orig_text.length = (size_t)compl_length; |
| 3095 | if (ins_compl_add(compl_orig_text.string, |
| 3096 | (int)compl_orig_text.length, NULL, NULL, NULL, 0, |
| 3097 | flags | CP_FAST, FALSE, NULL) != OK) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3098 | return; |
| 3099 | |
| 3100 | ctrl_x_mode = CTRL_X_EVAL; |
| 3101 | |
| 3102 | ins_compl_add_list(list); |
| 3103 | compl_matches = ins_compl_make_cyclic(); |
| 3104 | compl_started = TRUE; |
| 3105 | compl_used_match = TRUE; |
| 3106 | compl_cont_status = 0; |
| 3107 | |
| 3108 | compl_curr_match = compl_first_match; |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3109 | int no_select = compl_no_select || compl_longest; |
| 3110 | if (compl_no_insert || no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3111 | { |
| 3112 | ins_complete(K_DOWN, FALSE); |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 3113 | if (no_select) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3114 | // Down/Up has no real effect. |
| 3115 | ins_complete(K_UP, FALSE); |
| 3116 | } |
| 3117 | else |
| 3118 | ins_complete(Ctrl_N, FALSE); |
| 3119 | compl_enter_selects = compl_no_insert; |
| 3120 | |
| 3121 | // Lazily show the popup menu, unless we got interrupted. |
| 3122 | if (!compl_interrupted) |
| 3123 | show_pum(save_w_wrow, save_w_leftcol); |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 3124 | may_trigger_modechanged(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3125 | out_flush(); |
| 3126 | } |
| 3127 | |
| 3128 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3129 | * "complete()" function |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3130 | */ |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3131 | void |
| 3132 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3133 | { |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3134 | int startcol; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3135 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3136 | if (in_vim9script() |
| 3137 | && (check_for_number_arg(argvars, 0) == FAIL |
| 3138 | || check_for_list_arg(argvars, 1) == FAIL)) |
| 3139 | return; |
| 3140 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 3141 | if ((State & MODE_INSERT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3142 | { |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 3143 | emsg(_(e_complete_can_only_be_used_in_insert_mode)); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3144 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3145 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3146 | |
| 3147 | // Check for undo allowed here, because if something was already inserted |
| 3148 | // the line was already saved for undo and this check isn't done. |
| 3149 | if (!undo_allowed()) |
| 3150 | return; |
| 3151 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3152 | if (check_for_nonnull_list_arg(argvars, 1) != FAIL) |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 3153 | { |
| 3154 | startcol = (int)tv_get_number_chk(&argvars[0], NULL); |
| 3155 | if (startcol > 0) |
| 3156 | set_completion(startcol - 1, argvars[1].vval.v_list); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3157 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* |
| 3161 | * "complete_add()" function |
| 3162 | */ |
| 3163 | void |
| 3164 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 3165 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3166 | if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3167 | return; |
| 3168 | |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 3169 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * "complete_check()" function |
| 3174 | */ |
| 3175 | void |
| 3176 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 3177 | { |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3178 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3179 | RedrawingDisabled = 0; |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3180 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3181 | ins_compl_check_keys(0, TRUE); |
| 3182 | rettv->vval.v_number = ins_compl_interrupted(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3183 | |
| 3184 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3188 | * Return Insert completion mode name string |
| 3189 | */ |
| 3190 | static char_u * |
| 3191 | ins_compl_mode(void) |
| 3192 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3193 | 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] | 3194 | return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; |
| 3195 | |
| 3196 | return (char_u *)""; |
| 3197 | } |
| 3198 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 3199 | /* |
| 3200 | * Assign the sequence number to all the completion matches which don't have |
| 3201 | * one assigned yet. |
| 3202 | */ |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3203 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 3204 | ins_compl_update_sequence_numbers(void) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3205 | { |
| 3206 | int number = 0; |
| 3207 | compl_T *match; |
| 3208 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3209 | if (compl_dir_forward()) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3210 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3211 | // Search backwards for the first valid (!= -1) number. |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3212 | // This should normally succeed already at the first loop |
| 3213 | // cycle, so it's fast! |
| 3214 | for (match = compl_curr_match->cp_prev; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3215 | && !is_first_match(match); match = match->cp_prev) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3216 | if (match->cp_number != -1) |
| 3217 | { |
| 3218 | number = match->cp_number; |
| 3219 | break; |
| 3220 | } |
| 3221 | if (match != NULL) |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3222 | // go up and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3223 | for (match = match->cp_next; |
| 3224 | match != NULL && match->cp_number == -1; |
| 3225 | match = match->cp_next) |
| 3226 | match->cp_number = ++number; |
| 3227 | } |
| 3228 | else // BACKWARD |
| 3229 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3230 | // Search forwards (upwards) for the first valid (!= -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3231 | // number. This should normally succeed already at the |
| 3232 | // first loop cycle, so it's fast! |
| 3233 | for (match = compl_curr_match->cp_next; match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3234 | && !is_first_match(match); match = match->cp_next) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3235 | if (match->cp_number != -1) |
| 3236 | { |
| 3237 | number = match->cp_number; |
| 3238 | break; |
| 3239 | } |
| 3240 | if (match != NULL) |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 3241 | // go down and assign all numbers which are not assigned yet |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3242 | for (match = match->cp_prev; match |
| 3243 | && match->cp_number == -1; |
| 3244 | match = match->cp_prev) |
| 3245 | match->cp_number = ++number; |
| 3246 | } |
| 3247 | } |
| 3248 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3249 | /* |
| 3250 | * Get complete information |
| 3251 | */ |
| 3252 | static void |
| 3253 | get_complete_info(list_T *what_list, dict_T *retdict) |
| 3254 | { |
| 3255 | int ret = OK; |
| 3256 | listitem_T *item; |
| 3257 | #define CI_WHAT_MODE 0x01 |
| 3258 | #define CI_WHAT_PUM_VISIBLE 0x02 |
| 3259 | #define CI_WHAT_ITEMS 0x04 |
| 3260 | #define CI_WHAT_SELECTED 0x08 |
| 3261 | #define CI_WHAT_INSERTED 0x10 |
| 3262 | #define CI_WHAT_ALL 0xff |
| 3263 | int what_flag; |
| 3264 | |
| 3265 | if (what_list == NULL) |
| 3266 | what_flag = CI_WHAT_ALL; |
| 3267 | else |
| 3268 | { |
| 3269 | what_flag = 0; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 3270 | CHECK_LIST_MATERIALIZE(what_list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3271 | FOR_ALL_LIST_ITEMS(what_list, item) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3272 | { |
| 3273 | char_u *what = tv_get_string(&item->li_tv); |
| 3274 | |
| 3275 | if (STRCMP(what, "mode") == 0) |
| 3276 | what_flag |= CI_WHAT_MODE; |
| 3277 | else if (STRCMP(what, "pum_visible") == 0) |
| 3278 | what_flag |= CI_WHAT_PUM_VISIBLE; |
| 3279 | else if (STRCMP(what, "items") == 0) |
| 3280 | what_flag |= CI_WHAT_ITEMS; |
| 3281 | else if (STRCMP(what, "selected") == 0) |
| 3282 | what_flag |= CI_WHAT_SELECTED; |
| 3283 | else if (STRCMP(what, "inserted") == 0) |
| 3284 | what_flag |= CI_WHAT_INSERTED; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | if (ret == OK && (what_flag & CI_WHAT_MODE)) |
| 3289 | ret = dict_add_string(retdict, "mode", ins_compl_mode()); |
| 3290 | |
| 3291 | if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) |
| 3292 | ret = dict_add_number(retdict, "pum_visible", pum_visible()); |
| 3293 | |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3294 | if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED)) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3295 | { |
Christian Brabandt | 9eb236f | 2024-03-21 20:12:59 +0100 | [diff] [blame] | 3296 | list_T *li = NULL; |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3297 | dict_T *di; |
| 3298 | compl_T *match; |
| 3299 | int selected_idx = -1; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3300 | |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3301 | if (what_flag & CI_WHAT_ITEMS) |
| 3302 | { |
| 3303 | li = list_alloc(); |
| 3304 | if (li == NULL) |
| 3305 | return; |
| 3306 | ret = dict_add_list(retdict, "items", li); |
| 3307 | } |
| 3308 | if (ret == OK && what_flag & CI_WHAT_SELECTED) |
| 3309 | if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) |
| 3310 | ins_compl_update_sequence_numbers(); |
| 3311 | if (ret == OK && compl_first_match != NULL) |
| 3312 | { |
| 3313 | int list_idx = 0; |
| 3314 | match = compl_first_match; |
| 3315 | do |
| 3316 | { |
| 3317 | if (!match_at_original_text(match)) |
| 3318 | { |
| 3319 | if (what_flag & CI_WHAT_ITEMS) |
| 3320 | { |
| 3321 | di = dict_alloc(); |
| 3322 | if (di == NULL) |
| 3323 | return; |
| 3324 | ret = list_append_dict(li, di); |
| 3325 | if (ret != OK) |
| 3326 | return; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3327 | dict_add_string(di, "word", match->cp_str.string); |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 3328 | dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]); |
| 3329 | dict_add_string(di, "menu", match->cp_text[CPT_MENU]); |
| 3330 | dict_add_string(di, "kind", match->cp_text[CPT_KIND]); |
| 3331 | dict_add_string(di, "info", match->cp_text[CPT_INFO]); |
| 3332 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 3333 | // Add an empty string for backwards compatibility |
| 3334 | dict_add_string(di, "user_data", (char_u *)""); |
| 3335 | else |
| 3336 | dict_add_tv(di, "user_data", &match->cp_user_data); |
| 3337 | } |
| 3338 | if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number) |
| 3339 | selected_idx = list_idx; |
| 3340 | list_idx += 1; |
| 3341 | } |
| 3342 | match = match->cp_next; |
| 3343 | } |
| 3344 | while (match != NULL && !is_first_match(match)); |
| 3345 | } |
| 3346 | if (ret == OK && (what_flag & CI_WHAT_SELECTED)) |
| 3347 | ret = dict_add_number(retdict, "selected", selected_idx); |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 3348 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3349 | |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 3350 | if (ret == OK && (what_flag & CI_WHAT_INSERTED)) |
| 3351 | { |
| 3352 | // TODO |
| 3353 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3357 | * "complete_info()" function |
| 3358 | */ |
| 3359 | void |
| 3360 | f_complete_info(typval_T *argvars, typval_T *rettv) |
| 3361 | { |
| 3362 | list_T *what_list = NULL; |
| 3363 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 3364 | if (rettv_dict_alloc(rettv) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3365 | return; |
| 3366 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 3367 | if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL) |
| 3368 | return; |
| 3369 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3370 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 3371 | { |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3372 | if (check_for_list_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3373 | return; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 3374 | what_list = argvars[0].vval.v_list; |
| 3375 | } |
| 3376 | get_complete_info(what_list, rettv->vval.v_dict); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3377 | } |
| 3378 | #endif |
| 3379 | |
| 3380 | /* |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3381 | * Returns TRUE when using a user-defined function for thesaurus completion. |
| 3382 | */ |
| 3383 | static int |
| 3384 | thesaurus_func_complete(int type UNUSED) |
| 3385 | { |
| 3386 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 3387 | return type == CTRL_X_THESAURUS |
| 3388 | && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL); |
Yegappan Lakshmanan | 160e994 | 2021-10-16 15:41:29 +0100 | [diff] [blame] | 3389 | #else |
| 3390 | return FALSE; |
| 3391 | #endif |
| 3392 | } |
| 3393 | |
| 3394 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3395 | * Return value of process_next_cpt_value() |
| 3396 | */ |
| 3397 | enum |
| 3398 | { |
| 3399 | INS_COMPL_CPT_OK = 1, |
| 3400 | INS_COMPL_CPT_CONT, |
| 3401 | INS_COMPL_CPT_END |
| 3402 | }; |
| 3403 | |
| 3404 | /* |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3405 | * state information used for getting the next set of insert completion |
| 3406 | * matches. |
| 3407 | */ |
| 3408 | typedef struct |
| 3409 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3410 | char_u *e_cpt_copy; // copy of 'complete' |
| 3411 | char_u *e_cpt; // current entry in "e_cpt_copy" |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3412 | buf_T *ins_buf; // buffer being scanned |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3413 | pos_T *cur_match_pos; // current match position |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3414 | pos_T prev_match_pos; // previous match position |
| 3415 | int set_match_pos; // save first_match_pos/last_match_pos |
| 3416 | pos_T first_match_pos; // first match position |
| 3417 | pos_T last_match_pos; // last match position |
| 3418 | int found_all; // found all matches of a certain type. |
| 3419 | char_u *dict; // dictionary file to search |
| 3420 | int dict_f; // "dict" is an exact file name or not |
| 3421 | } ins_compl_next_state_T; |
| 3422 | |
| 3423 | /* |
| 3424 | * Process the next 'complete' option value in st->e_cpt. |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3425 | * |
| 3426 | * If successful, the arguments are set as below: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3427 | * st->cpt - pointer to the next option value in "st->cpt" |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3428 | * compl_type_arg - type of insert mode completion to use |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3429 | * st->found_all - all matches of this type are found |
| 3430 | * st->ins_buf - search for completions in this buffer |
| 3431 | * st->first_match_pos - position of the first completion match |
| 3432 | * st->last_match_pos - position of the last completion match |
| 3433 | * 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] | 3434 | * avoid loops after the search wraps around. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3435 | * st->dict - name of the dictionary or thesaurus file to search |
| 3436 | * 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] | 3437 | * |
| 3438 | * Returns INS_COMPL_CPT_OK if the next value is processed successfully. |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 3439 | * Returns INS_COMPL_CPT_CONT to skip the current completion source matching |
| 3440 | * the "st->e_cpt" option value and process the next matching source. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3441 | * 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] | 3442 | */ |
| 3443 | static int |
| 3444 | process_next_cpt_value( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3445 | ins_compl_next_state_T *st, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3446 | int *compl_type_arg, |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3447 | pos_T *start_match_pos, |
| 3448 | int in_fuzzy) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3449 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3450 | int compl_type = -1; |
| 3451 | int status = INS_COMPL_CPT_OK; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3452 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3453 | st->found_all = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3454 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3455 | while (*st->e_cpt == ',' || *st->e_cpt == ' ') |
| 3456 | st->e_cpt++; |
| 3457 | |
| 3458 | if (*st->e_cpt == '.' && !curbuf->b_scanned) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3459 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3460 | st->ins_buf = curbuf; |
| 3461 | st->first_match_pos = *start_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3462 | // Move the cursor back one character so that ^N can match the |
| 3463 | // word immediately after the cursor. |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3464 | if (ctrl_x_mode_normal() && (!in_fuzzy && dec(&st->first_match_pos) < 0)) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3465 | { |
| 3466 | // Move the cursor to after the last character in the |
| 3467 | // buffer, so that word at start of buffer is found |
| 3468 | // correctly. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3469 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 3470 | 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] | 3471 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3472 | st->last_match_pos = st->first_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3473 | compl_type = 0; |
| 3474 | |
| 3475 | // Remember the first match so that the loop stops when we |
| 3476 | // wrap and come back there a second time. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3477 | st->set_match_pos = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3478 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3479 | else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 3480 | && (st->ins_buf = ins_compl_next_buf( |
| 3481 | st->ins_buf, *st->e_cpt)) != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3482 | { |
| 3483 | // Scan a buffer, but not the current one. |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3484 | if (st->ins_buf->b_ml.ml_mfp != NULL) // loaded buffer |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3485 | { |
| 3486 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3487 | st->first_match_pos.col = st->last_match_pos.col = 0; |
| 3488 | st->first_match_pos.lnum = st->ins_buf->b_ml.ml_line_count + 1; |
| 3489 | st->last_match_pos.lnum = 0; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3490 | compl_type = 0; |
| 3491 | } |
| 3492 | else // unloaded buffer, scan like dictionary |
| 3493 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3494 | st->found_all = TRUE; |
| 3495 | if (st->ins_buf->b_fname == NULL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3496 | { |
| 3497 | status = INS_COMPL_CPT_CONT; |
| 3498 | goto done; |
| 3499 | } |
| 3500 | compl_type = CTRL_X_DICTIONARY; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3501 | st->dict = st->ins_buf->b_fname; |
| 3502 | st->dict_f = DICT_EXACT; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3503 | } |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 3504 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 3505 | { |
| 3506 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 3507 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
| 3508 | st->ins_buf->b_fname == NULL |
| 3509 | ? buf_spname(st->ins_buf) |
| 3510 | : st->ins_buf->b_sfname == NULL |
| 3511 | ? st->ins_buf->b_fname |
| 3512 | : st->ins_buf->b_sfname); |
| 3513 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 3514 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3515 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3516 | else if (*st->e_cpt == NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3517 | status = INS_COMPL_CPT_END; |
| 3518 | else |
| 3519 | { |
| 3520 | if (ctrl_x_mode_line_or_eval()) |
| 3521 | compl_type = -1; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3522 | else if (*st->e_cpt == 'k' || *st->e_cpt == 's') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3523 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3524 | if (*st->e_cpt == 'k') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3525 | compl_type = CTRL_X_DICTIONARY; |
| 3526 | else |
| 3527 | compl_type = CTRL_X_THESAURUS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3528 | if (*++st->e_cpt != ',' && *st->e_cpt != NUL) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3529 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3530 | st->dict = st->e_cpt; |
| 3531 | st->dict_f = DICT_FIRST; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3532 | } |
| 3533 | } |
| 3534 | #ifdef FEAT_FIND_ID |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3535 | else if (*st->e_cpt == 'i') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3536 | compl_type = CTRL_X_PATH_PATTERNS; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3537 | else if (*st->e_cpt == 'd') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3538 | compl_type = CTRL_X_PATH_DEFINES; |
| 3539 | #endif |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3540 | else if (*st->e_cpt == ']' || *st->e_cpt == 't') |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3541 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3542 | compl_type = CTRL_X_TAGS; |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 91ccbad | 2022-10-13 12:51:13 +0100 | [diff] [blame] | 3543 | if (!shortmess(SHM_COMPLETIONSCAN)) |
| 3544 | { |
| 3545 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
| 3546 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
| 3547 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 3548 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3549 | } |
| 3550 | else |
| 3551 | compl_type = -1; |
| 3552 | |
| 3553 | // in any case e_cpt is advanced to the next entry |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3554 | (void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ","); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3555 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3556 | st->found_all = TRUE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3557 | if (compl_type == -1) |
| 3558 | status = INS_COMPL_CPT_CONT; |
| 3559 | } |
| 3560 | |
| 3561 | done: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3562 | *compl_type_arg = compl_type; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3563 | return status; |
| 3564 | } |
| 3565 | |
| 3566 | #ifdef FEAT_FIND_ID |
| 3567 | /* |
| 3568 | * Get the next set of identifiers or defines matching "compl_pattern" in |
| 3569 | * included files. |
| 3570 | */ |
| 3571 | static void |
| 3572 | get_next_include_file_completion(int compl_type) |
| 3573 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3574 | find_pattern_in_path(compl_pattern.string, compl_direction, |
| 3575 | (int)compl_pattern.length, FALSE, FALSE, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3576 | (compl_type == CTRL_X_PATH_DEFINES |
| 3577 | && !(compl_cont_status & CONT_SOL)) |
| 3578 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 3579 | (linenr_T)1, (linenr_T)MAXLNUM, FALSE); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3580 | } |
| 3581 | #endif |
| 3582 | |
| 3583 | /* |
| 3584 | * Get the next set of words matching "compl_pattern" in dictionary or |
| 3585 | * thesaurus files. |
| 3586 | */ |
| 3587 | static void |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3588 | 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] | 3589 | { |
| 3590 | #ifdef FEAT_COMPL_FUNC |
| 3591 | if (thesaurus_func_complete(compl_type)) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3592 | expand_by_function(compl_type, compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3593 | else |
| 3594 | #endif |
| 3595 | ins_compl_dictionaries( |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3596 | dict != NULL ? dict |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3597 | : (compl_type == CTRL_X_THESAURUS |
| 3598 | ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr) |
| 3599 | : (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)), |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3600 | compl_pattern.string, |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3601 | dict != NULL ? dict_f : 0, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3602 | compl_type == CTRL_X_THESAURUS); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | /* |
| 3606 | * Get the next set of tag names matching "compl_pattern". |
| 3607 | */ |
| 3608 | static void |
| 3609 | get_next_tag_completion(void) |
| 3610 | { |
| 3611 | int save_p_ic; |
| 3612 | char_u **matches; |
| 3613 | int num_matches; |
| 3614 | |
| 3615 | // set p_ic according to p_ic, p_scs and pat for find_tags(). |
| 3616 | save_p_ic = p_ic; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3617 | p_ic = ignorecase(compl_pattern.string); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Find up to TAG_MANY matches. Avoids that an enormous number |
| 3620 | // of matches is found when compl_pattern is empty |
| 3621 | g_tag_at_cursor = TRUE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3622 | if (find_tags(compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3623 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3624 | | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0), |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3625 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 3626 | ins_compl_add_matches(num_matches, matches, p_ic); |
| 3627 | g_tag_at_cursor = FALSE; |
| 3628 | p_ic = save_p_ic; |
| 3629 | } |
| 3630 | |
| 3631 | /* |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3632 | * Compare function for qsort |
| 3633 | */ |
| 3634 | static int compare_scores(const void *a, const void *b) |
| 3635 | { |
| 3636 | int idx_a = *(const int *)a; |
| 3637 | int idx_b = *(const int *)b; |
| 3638 | int score_a = compl_fuzzy_scores[idx_a]; |
| 3639 | int score_b = compl_fuzzy_scores[idx_b]; |
zeertzjq | 58d7052 | 2024-08-31 17:05:39 +0200 | [diff] [blame] | 3640 | return score_a == score_b ? (idx_a == idx_b ? 0 : (idx_a < idx_b ? -1 : 1)) |
| 3641 | : (score_a > score_b ? -1 : 1); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3645 | * Get the next set of filename matching "compl_pattern". |
| 3646 | */ |
| 3647 | static void |
| 3648 | get_next_filename_completion(void) |
| 3649 | { |
| 3650 | char_u **matches; |
| 3651 | int num_matches; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3652 | char_u *ptr; |
| 3653 | garray_T fuzzy_indices; |
| 3654 | int i; |
| 3655 | int score; |
| 3656 | char_u *leader = ins_compl_leader(); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3657 | size_t leader_len = ins_compl_leader_len();; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3658 | int in_fuzzy = ((get_cot_flags() & COT_FUZZY) != 0 && leader_len > 0); |
| 3659 | char_u **sorted_matches; |
| 3660 | int *fuzzy_indices_data; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3661 | char_u *last_sep = NULL; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3662 | |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3663 | #ifdef BACKSLASH_IN_FILENAME |
| 3664 | char pathsep = (curbuf->b_p_csl[0] == 's') ? |
| 3665 | '/' : (curbuf->b_p_csl[0] == 'b') ? '\\' : PATHSEP; |
| 3666 | #else |
| 3667 | char pathsep = PATHSEP; |
| 3668 | #endif |
| 3669 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3670 | if (in_fuzzy) |
| 3671 | { |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3672 | #ifdef BACKSLASH_IN_FILENAME |
| 3673 | if (curbuf->b_p_csl[0] == 's') |
| 3674 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3675 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3676 | { |
| 3677 | if (leader[i] == '\\') |
| 3678 | leader[i] = '/'; |
| 3679 | } |
| 3680 | } |
| 3681 | else if (curbuf->b_p_csl[0] == 'b') |
| 3682 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3683 | for (i = 0; i < (int)leader_len; i++) |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3684 | { |
| 3685 | if (leader[i] == '/') |
| 3686 | leader[i] = '\\'; |
| 3687 | } |
| 3688 | } |
| 3689 | #endif |
| 3690 | last_sep = vim_strrchr(leader, pathsep); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3691 | if (last_sep == NULL) |
| 3692 | { |
| 3693 | // No path separator or separator is the last character, |
| 3694 | // fuzzy match the whole leader |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3695 | VIM_CLEAR_STRING(compl_pattern); |
| 3696 | compl_pattern.string = vim_strnsave((char_u *)"*", 1); |
| 3697 | if (compl_pattern.string == NULL) |
| 3698 | return; |
| 3699 | compl_pattern.length = 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3700 | } |
| 3701 | else if (*(last_sep + 1) == '\0') |
| 3702 | in_fuzzy = FALSE; |
| 3703 | else |
| 3704 | { |
| 3705 | // Split leader into path and file parts |
| 3706 | int path_len = last_sep - leader + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3707 | char_u *path_with_wildcard; |
| 3708 | |
| 3709 | path_with_wildcard = alloc(path_len + 2); |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3710 | if (path_with_wildcard != NULL) |
| 3711 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3712 | vim_snprintf((char *)path_with_wildcard, path_len + 2, "%*.*s*", path_len, path_len, leader); |
| 3713 | VIM_CLEAR_STRING(compl_pattern); |
| 3714 | compl_pattern.string = path_with_wildcard; |
| 3715 | compl_pattern.length = path_len + 1; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3716 | |
| 3717 | // Move leader to the file part |
| 3718 | leader = last_sep + 1; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3719 | leader_len -= path_len; |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 3720 | } |
| 3721 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3722 | } |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3723 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3724 | if (expand_wildcards(1, &compl_pattern.string, &num_matches, &matches, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3725 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK) |
| 3726 | return; |
| 3727 | |
| 3728 | // May change home directory back to "~". |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3729 | tilde_replace(compl_pattern.string, num_matches, matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3730 | #ifdef BACKSLASH_IN_FILENAME |
| 3731 | if (curbuf->b_p_csl[0] != NUL) |
| 3732 | { |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3733 | for (i = 0; i < num_matches; ++i) |
| 3734 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3735 | ptr = matches[i]; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3736 | while (*ptr != NUL) |
| 3737 | { |
| 3738 | if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') |
| 3739 | *ptr = '/'; |
| 3740 | else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') |
| 3741 | *ptr = '\\'; |
| 3742 | ptr += (*mb_ptr2len)(ptr); |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | #endif |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3747 | |
| 3748 | if (in_fuzzy) |
| 3749 | { |
| 3750 | ga_init2(&fuzzy_indices, sizeof(int), 10); |
| 3751 | compl_fuzzy_scores = (int *)alloc(sizeof(int) * num_matches); |
| 3752 | |
| 3753 | for (i = 0; i < num_matches; i++) |
| 3754 | { |
| 3755 | ptr = matches[i]; |
| 3756 | score = fuzzy_match_str(ptr, leader); |
| 3757 | if (score > 0) |
| 3758 | { |
| 3759 | if (ga_grow(&fuzzy_indices, 1) == OK) |
| 3760 | { |
| 3761 | ((int *)fuzzy_indices.ga_data)[fuzzy_indices.ga_len] = i; |
| 3762 | compl_fuzzy_scores[i] = score; |
| 3763 | fuzzy_indices.ga_len++; |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 3768 | // prevent qsort from deref NULL pointer |
| 3769 | if (fuzzy_indices.ga_len > 0) |
| 3770 | { |
| 3771 | fuzzy_indices_data = (int *)fuzzy_indices.ga_data; |
| 3772 | qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3773 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 3774 | sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len); |
| 3775 | for (i = 0; i < fuzzy_indices.ga_len; ++i) |
| 3776 | sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3777 | |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 3778 | FreeWild(num_matches, matches); |
| 3779 | matches = sorted_matches; |
| 3780 | num_matches = fuzzy_indices.ga_len; |
| 3781 | } |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 3782 | else if (leader_len > 0) |
| 3783 | { |
| 3784 | FreeWild(num_matches, matches); |
| 3785 | num_matches = 0; |
| 3786 | } |
Christian Brabandt | 220474d | 2024-07-20 13:26:44 +0200 | [diff] [blame] | 3787 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3788 | vim_free(compl_fuzzy_scores); |
| 3789 | ga_clear(&fuzzy_indices); |
| 3790 | } |
| 3791 | |
glepnir | 6b6280c | 2024-07-27 16:25:45 +0200 | [diff] [blame] | 3792 | if (num_matches > 0) |
| 3793 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | /* |
| 3797 | * Get the next set of command-line completions matching "compl_pattern". |
| 3798 | */ |
| 3799 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 3800 | get_next_cmdline_completion(void) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3801 | { |
| 3802 | char_u **matches; |
| 3803 | int num_matches; |
| 3804 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3805 | if (expand_cmdline(&compl_xp, compl_pattern.string, |
| 3806 | (int)compl_pattern.length, &num_matches, &matches) == EXPAND_OK) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3807 | ins_compl_add_matches(num_matches, matches, FALSE); |
| 3808 | } |
| 3809 | |
| 3810 | /* |
| 3811 | * Get the next set of spell suggestions matching "compl_pattern". |
| 3812 | */ |
| 3813 | static void |
| 3814 | get_next_spell_completion(linenr_T lnum UNUSED) |
| 3815 | { |
| 3816 | #ifdef FEAT_SPELL |
| 3817 | char_u **matches; |
| 3818 | int num_matches; |
| 3819 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3820 | num_matches = expand_spelling(lnum, compl_pattern.string, &matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3821 | if (num_matches > 0) |
| 3822 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 8e7cc6b | 2021-12-30 10:32:25 +0000 | [diff] [blame] | 3823 | else |
| 3824 | vim_free(matches); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3825 | #endif |
| 3826 | } |
| 3827 | |
| 3828 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3829 | * Return the next word or line from buffer "ins_buf" at position |
| 3830 | * "cur_match_pos" for completion. The length of the match is set in "len". |
| 3831 | */ |
| 3832 | static char_u * |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 3833 | ins_compl_get_next_word_or_line( |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3834 | buf_T *ins_buf, // buffer being scanned |
| 3835 | pos_T *cur_match_pos, // current match position |
| 3836 | int *match_len, |
| 3837 | int *cont_s_ipos) // next ^X<> will set initial_pos |
| 3838 | { |
| 3839 | char_u *ptr; |
| 3840 | int len; |
| 3841 | |
| 3842 | *match_len = 0; |
| 3843 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) + |
| 3844 | cur_match_pos->col; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3845 | 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] | 3846 | if (ctrl_x_mode_line_or_eval()) |
| 3847 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3848 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3849 | { |
| 3850 | if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 3851 | return NULL; |
| 3852 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3853 | len = ml_get_buf_len(ins_buf, cur_match_pos->lnum + 1); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3854 | if (!p_paste) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3855 | { |
| 3856 | char_u *tmp_ptr = ptr; |
| 3857 | |
| 3858 | ptr = skipwhite(tmp_ptr); |
| 3859 | len -= (int)(ptr - tmp_ptr); |
| 3860 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3861 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3862 | } |
| 3863 | else |
| 3864 | { |
| 3865 | char_u *tmp_ptr = ptr; |
| 3866 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3867 | if (compl_status_adding() && compl_length <= len) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3868 | { |
| 3869 | tmp_ptr += compl_length; |
| 3870 | // Skip if already inside a word. |
| 3871 | if (vim_iswordp(tmp_ptr)) |
| 3872 | return NULL; |
| 3873 | // Find start of next word. |
| 3874 | tmp_ptr = find_word_start(tmp_ptr); |
| 3875 | } |
| 3876 | // Find end of this word. |
| 3877 | tmp_ptr = find_word_end(tmp_ptr); |
| 3878 | len = (int)(tmp_ptr - ptr); |
| 3879 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3880 | if (compl_status_adding() && len == compl_length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 3881 | { |
| 3882 | if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count) |
| 3883 | { |
| 3884 | // Try next line, if any. the new word will be |
| 3885 | // "join" as if the normal command "J" was used. |
| 3886 | // IOSIZE is always greater than |
| 3887 | // compl_length, so the next STRNCPY always |
| 3888 | // works -- Acevedo |
| 3889 | STRNCPY(IObuff, ptr, len); |
| 3890 | ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE); |
| 3891 | tmp_ptr = ptr = skipwhite(ptr); |
| 3892 | // Find start of next word. |
| 3893 | tmp_ptr = find_word_start(tmp_ptr); |
| 3894 | // Find end of next word. |
| 3895 | tmp_ptr = find_word_end(tmp_ptr); |
| 3896 | if (tmp_ptr > ptr) |
| 3897 | { |
| 3898 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
| 3899 | { |
| 3900 | if (IObuff[len - 1] != ' ') |
| 3901 | IObuff[len++] = ' '; |
| 3902 | // IObuf =~ "\k.* ", thus len >= 2 |
| 3903 | if (p_js |
| 3904 | && (IObuff[len - 2] == '.' |
| 3905 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 3906 | == NULL |
| 3907 | && (IObuff[len - 2] == '?' |
| 3908 | || IObuff[len - 2] == '!')))) |
| 3909 | IObuff[len++] = ' '; |
| 3910 | } |
| 3911 | // copy as much as possible of the new word |
| 3912 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 3913 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 3914 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 3915 | len += (int)(tmp_ptr - ptr); |
| 3916 | *cont_s_ipos = TRUE; |
| 3917 | } |
| 3918 | IObuff[len] = NUL; |
| 3919 | ptr = IObuff; |
| 3920 | } |
| 3921 | if (len == compl_length) |
| 3922 | return NULL; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | *match_len = len; |
| 3927 | return ptr; |
| 3928 | } |
| 3929 | |
| 3930 | /* |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3931 | * Get the next set of words matching "compl_pattern" for default completion(s) |
| 3932 | * (normal ^P/^N and ^X^L). |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3933 | * Search for "compl_pattern" in the buffer "st->ins_buf" starting from the |
| 3934 | * position "st->start_pos" in the "compl_direction" direction. If |
| 3935 | * "st->set_match_pos" is TRUE, then set the "st->first_match_pos" and |
| 3936 | * "st->last_match_pos". |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3937 | * Returns OK if a new next match is found, otherwise returns FAIL. |
| 3938 | */ |
| 3939 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3940 | 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] | 3941 | { |
| 3942 | int found_new_match = FAIL; |
| 3943 | int save_p_scs; |
| 3944 | int save_p_ws; |
| 3945 | int looped_around = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3946 | char_u *ptr = NULL; |
| 3947 | int len = 0; |
| 3948 | int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0 && compl_length > 0; |
| 3949 | char_u *leader = ins_compl_leader(); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3950 | |
| 3951 | // If 'infercase' is set, don't use 'smartcase' here |
| 3952 | save_p_scs = p_scs; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3953 | if (st->ins_buf->b_p_inf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3954 | p_scs = FALSE; |
| 3955 | |
| 3956 | // Buffers other than curbuf are scanned from the beginning or the |
| 3957 | // end but never from the middle, thus setting nowrapscan in this |
| 3958 | // buffer is a good idea, on the other hand, we always set |
| 3959 | // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb |
| 3960 | save_p_ws = p_ws; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3961 | if (st->ins_buf != curbuf) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3962 | p_ws = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3963 | else if (*st->e_cpt == '.' && !in_fuzzy) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3964 | p_ws = TRUE; |
| 3965 | looped_around = FALSE; |
| 3966 | for (;;) |
| 3967 | { |
| 3968 | int cont_s_ipos = FALSE; |
| 3969 | |
| 3970 | ++msg_silent; // Don't want messages for wrapscan. |
| 3971 | |
| 3972 | // ctrl_x_mode_line_or_eval() || word-wise search that |
| 3973 | // has added a word that was at the beginning of the line |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3974 | if ((ctrl_x_mode_whole_line() && !in_fuzzy) || ctrl_x_mode_eval() || (compl_cont_status & CONT_SOL)) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3975 | found_new_match = search_for_exact_line(st->ins_buf, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3976 | st->cur_match_pos, compl_direction, compl_pattern.string); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 3977 | else if (in_fuzzy) |
| 3978 | found_new_match = search_for_fuzzy_match(st->ins_buf, |
| 3979 | st->cur_match_pos, leader, compl_direction, |
| 3980 | start_pos, &len, &ptr, ctrl_x_mode_whole_line()); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3981 | else |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3982 | found_new_match = searchit(NULL, st->ins_buf, st->cur_match_pos, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 3983 | NULL, compl_direction, compl_pattern.string, (int)compl_pattern.length, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3984 | 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, NULL); |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3985 | --msg_silent; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3986 | if (!compl_started || st->set_match_pos) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3987 | { |
| 3988 | // set "compl_started" even on fail |
| 3989 | compl_started = TRUE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3990 | st->first_match_pos = *st->cur_match_pos; |
| 3991 | st->last_match_pos = *st->cur_match_pos; |
| 3992 | st->set_match_pos = FALSE; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3993 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 3994 | else if (st->first_match_pos.lnum == st->last_match_pos.lnum |
| 3995 | && st->first_match_pos.col == st->last_match_pos.col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 3996 | { |
| 3997 | found_new_match = FAIL; |
| 3998 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3999 | else if (compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4000 | && (st->prev_match_pos.lnum > st->cur_match_pos->lnum |
| 4001 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4002 | && st->prev_match_pos.col >= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4003 | { |
| 4004 | if (looped_around) |
| 4005 | found_new_match = FAIL; |
| 4006 | else |
| 4007 | looped_around = TRUE; |
| 4008 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4009 | else if (!compl_dir_forward() |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4010 | && (st->prev_match_pos.lnum < st->cur_match_pos->lnum |
| 4011 | || (st->prev_match_pos.lnum == st->cur_match_pos->lnum |
| 4012 | && st->prev_match_pos.col <= st->cur_match_pos->col))) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4013 | { |
| 4014 | if (looped_around) |
| 4015 | found_new_match = FAIL; |
| 4016 | else |
| 4017 | looped_around = TRUE; |
| 4018 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4019 | st->prev_match_pos = *st->cur_match_pos; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4020 | if (found_new_match == FAIL) |
| 4021 | break; |
| 4022 | |
| 4023 | // when ADDING, the text before the cursor matches, skip it |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4024 | if (compl_status_adding() && st->ins_buf == curbuf |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4025 | && start_pos->lnum == st->cur_match_pos->lnum |
| 4026 | && start_pos->col == st->cur_match_pos->col) |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4027 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4028 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4029 | if (!in_fuzzy) |
| 4030 | ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos, |
| 4031 | &len, &cont_s_ipos); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4032 | if (ptr == NULL) |
| 4033 | continue; |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4034 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4035 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4036 | st->ins_buf == curbuf ? NULL : st->ins_buf->b_sfname, |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4037 | 0, cont_s_ipos) != NOTDONE) |
| 4038 | { |
| 4039 | found_new_match = OK; |
| 4040 | break; |
| 4041 | } |
| 4042 | } |
| 4043 | p_scs = save_p_scs; |
| 4044 | p_ws = save_p_ws; |
| 4045 | |
| 4046 | return found_new_match; |
| 4047 | } |
| 4048 | |
| 4049 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4050 | * get the next set of completion matches for "type". |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4051 | * Returns TRUE if a new match is found. Otherwise returns FALSE. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4052 | */ |
| 4053 | static int |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4054 | 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] | 4055 | { |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4056 | int found_new_match = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4057 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4058 | switch (type) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4059 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4060 | case -1: |
| 4061 | break; |
| 4062 | #ifdef FEAT_FIND_ID |
| 4063 | case CTRL_X_PATH_PATTERNS: |
| 4064 | case CTRL_X_PATH_DEFINES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4065 | get_next_include_file_completion(type); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4066 | break; |
| 4067 | #endif |
| 4068 | |
| 4069 | case CTRL_X_DICTIONARY: |
| 4070 | case CTRL_X_THESAURUS: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4071 | get_next_dict_tsr_completion(type, st->dict, st->dict_f); |
| 4072 | st->dict = NULL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4073 | break; |
| 4074 | |
| 4075 | case CTRL_X_TAGS: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4076 | get_next_tag_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4077 | break; |
| 4078 | |
| 4079 | case CTRL_X_FILES: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4080 | get_next_filename_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4081 | break; |
| 4082 | |
| 4083 | case CTRL_X_CMDLINE: |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 4084 | case CTRL_X_CMDLINE_CTRL_X: |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 4085 | get_next_cmdline_completion(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4086 | break; |
| 4087 | |
| 4088 | #ifdef FEAT_COMPL_FUNC |
| 4089 | case CTRL_X_FUNCTION: |
| 4090 | case CTRL_X_OMNI: |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4091 | expand_by_function(type, compl_pattern.string); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4092 | break; |
| 4093 | #endif |
| 4094 | |
| 4095 | case CTRL_X_SPELL: |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4096 | get_next_spell_completion(st->first_match_pos.lnum); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4097 | break; |
| 4098 | |
| 4099 | default: // normal ^P/^N and ^X^L |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4100 | found_new_match = get_next_default_completion(st, ini); |
| 4101 | if (found_new_match == FAIL && st->ins_buf == curbuf) |
| 4102 | st->found_all = TRUE; |
| 4103 | } |
| 4104 | |
| 4105 | // check if compl_curr_match has changed, (e.g. other type of |
| 4106 | // expansion added something) |
| 4107 | if (type != 0 && compl_curr_match != compl_old_match) |
| 4108 | found_new_match = OK; |
| 4109 | |
| 4110 | return found_new_match; |
| 4111 | } |
| 4112 | |
| 4113 | /* |
| 4114 | * Get the next expansion(s), using "compl_pattern". |
| 4115 | * The search starts at position "ini" in curbuf and in the direction |
| 4116 | * compl_direction. |
| 4117 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4118 | * where we stopped searching before. |
| 4119 | * This may return before finding all the matches. |
| 4120 | * Return the total number of matches or -1 if still unknown -- Acevedo |
| 4121 | */ |
| 4122 | static int |
| 4123 | ins_compl_get_exp(pos_T *ini) |
| 4124 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4125 | static ins_compl_next_state_T st; |
| 4126 | static int st_cleared = FALSE; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4127 | int i; |
| 4128 | int found_new_match; |
| 4129 | int type = ctrl_x_mode; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4130 | int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4131 | |
| 4132 | if (!compl_started) |
| 4133 | { |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4134 | buf_T *buf; |
| 4135 | |
| 4136 | FOR_ALL_BUFFERS(buf) |
| 4137 | buf->b_scanned = 0; |
| 4138 | if (!st_cleared) |
| 4139 | { |
| 4140 | CLEAR_FIELD(st); |
| 4141 | st_cleared = TRUE; |
| 4142 | } |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4143 | st.found_all = FALSE; |
| 4144 | st.ins_buf = curbuf; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4145 | vim_free(st.e_cpt_copy); |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 4146 | // Make a copy of 'complete', in case the buffer is wiped out. |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4147 | st.e_cpt_copy = vim_strsave((compl_cont_status & CONT_LOCAL) |
| 4148 | ? (char_u *)"." : curbuf->b_p_cpt); |
| 4149 | 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] | 4150 | st.last_match_pos = st.first_match_pos = *ini; |
| 4151 | } |
| 4152 | else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf)) |
| 4153 | st.ins_buf = curbuf; // In case the buffer was wiped out. |
| 4154 | |
| 4155 | compl_old_match = compl_curr_match; // remember the last current match |
Christian Brabandt | 13032a4 | 2024-07-28 21:16:48 +0200 | [diff] [blame] | 4156 | st.cur_match_pos = (compl_dir_forward()) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4157 | ? &st.last_match_pos : &st.first_match_pos; |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4158 | |
| 4159 | // For ^N/^P loop over all the flags/windows/buffers in 'complete'. |
| 4160 | for (;;) |
| 4161 | { |
| 4162 | found_new_match = FAIL; |
| 4163 | st.set_match_pos = FALSE; |
| 4164 | |
| 4165 | // For ^N/^P pick a new entry from e_cpt if compl_started is off, |
| 4166 | // or if found_all says this entry is done. For ^X^L only use the |
| 4167 | // entries from 'complete' that look in loaded buffers. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4168 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4169 | && (!compl_started || st.found_all)) |
| 4170 | { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 4171 | int status = process_next_cpt_value(&st, &type, ini, in_fuzzy); |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4172 | |
| 4173 | if (status == INS_COMPL_CPT_END) |
| 4174 | break; |
| 4175 | if (status == INS_COMPL_CPT_CONT) |
| 4176 | continue; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4177 | } |
| 4178 | |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4179 | // If complete() was called then compl_pattern has been reset. The |
| 4180 | // following won't work then, bail out. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4181 | if (compl_pattern.string == NULL) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4182 | break; |
| 4183 | |
| 4184 | // get the next set of completion matches |
| 4185 | found_new_match = get_next_completion_match(type, &st, ini); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4186 | |
| 4187 | // break the loop for specialized modes (use 'complete' just for the |
| 4188 | // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new |
| 4189 | // match |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4190 | if ((ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval()) |
| 4191 | || found_new_match != FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4192 | { |
| 4193 | if (got_int) |
| 4194 | break; |
| 4195 | // Fill the popup menu as soon as possible. |
| 4196 | if (type != -1) |
| 4197 | ins_compl_check_keys(0, FALSE); |
| 4198 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4199 | if ((ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4200 | && !ctrl_x_mode_line_or_eval()) || compl_interrupted) |
| 4201 | break; |
| 4202 | compl_started = TRUE; |
| 4203 | } |
| 4204 | else |
| 4205 | { |
| 4206 | // Mark a buffer scanned when it has been scanned completely |
Christian Brabandt | ee9166e | 2023-09-03 21:24:33 +0200 | [diff] [blame] | 4207 | 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] | 4208 | st.ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4209 | |
| 4210 | compl_started = FALSE; |
| 4211 | } |
| 4212 | } |
| 4213 | compl_started = TRUE; |
| 4214 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4215 | if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) |
Yegappan Lakshmanan | 6ad84ab | 2021-12-31 12:59:53 +0000 | [diff] [blame] | 4216 | && *st.e_cpt == NUL) // Got to end of 'complete' |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4217 | found_new_match = FAIL; |
| 4218 | |
| 4219 | i = -1; // total of matches, unknown |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4220 | if (found_new_match == FAIL || (ctrl_x_mode_not_default() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4221 | && !ctrl_x_mode_line_or_eval())) |
| 4222 | i = ins_compl_make_cyclic(); |
| 4223 | |
| 4224 | if (compl_old_match != NULL) |
| 4225 | { |
| 4226 | // If several matches were added (FORWARD) or the search failed and has |
| 4227 | // just been made cyclic then we have to move compl_curr_match to the |
| 4228 | // next or previous entry (if any) -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4229 | compl_curr_match = compl_dir_forward() ? compl_old_match->cp_next |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4230 | : compl_old_match->cp_prev; |
| 4231 | if (compl_curr_match == NULL) |
| 4232 | compl_curr_match = compl_old_match; |
| 4233 | } |
LemonBoy | 2bf52dd | 2022-04-09 18:17:34 +0100 | [diff] [blame] | 4234 | may_trigger_modechanged(); |
=?UTF-8?q?Magnus=20Gro=C3=9F?= | 25def2c | 2021-10-22 18:56:39 +0100 | [diff] [blame] | 4235 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4236 | return i; |
| 4237 | } |
| 4238 | |
| 4239 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4240 | * Update "compl_shown_match" to the actually shown match, it may differ when |
| 4241 | * "compl_leader" is used to omit some of the matches. |
| 4242 | */ |
| 4243 | static void |
| 4244 | ins_compl_update_shown_match(void) |
| 4245 | { |
| 4246 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4247 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4248 | && compl_shown_match->cp_next != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4249 | && !is_first_match(compl_shown_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4250 | compl_shown_match = compl_shown_match->cp_next; |
| 4251 | |
| 4252 | // If we didn't find it searching forward, and compl_shows_dir is |
| 4253 | // backward, find the last match. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4254 | if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4255 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4256 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4257 | && (compl_shown_match->cp_next == NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4258 | || is_first_match(compl_shown_match->cp_next))) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4259 | { |
| 4260 | while (!ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4261 | compl_leader.string, (int)compl_leader.length) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4262 | && compl_shown_match->cp_prev != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4263 | && !is_first_match(compl_shown_match->cp_prev)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4264 | compl_shown_match = compl_shown_match->cp_prev; |
| 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4269 | * Delete the old text being completed. |
| 4270 | */ |
| 4271 | void |
| 4272 | ins_compl_delete(void) |
| 4273 | { |
| 4274 | int col; |
| 4275 | |
| 4276 | // In insert mode: Delete the typed part. |
| 4277 | // In replace mode: Put the old characters back, if any. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4278 | col = compl_col + (compl_status_adding() ? compl_length : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4279 | if ((int)curwin->w_cursor.col > col) |
| 4280 | { |
| 4281 | if (stop_arrow() == FAIL) |
| 4282 | return; |
| 4283 | backspace_until_column(col); |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 4284 | compl_ins_end_col = curwin->w_cursor.col; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | // TODO: is this sufficient for redrawing? Redrawing everything causes |
| 4288 | // flicker, thus we can't do that. |
| 4289 | changed_cline_bef_curs(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4290 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4291 | // clear v:completed_item |
| 4292 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4293 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | /* |
| 4297 | * Insert the new text being completed. |
| 4298 | * "in_compl_func" is TRUE when called from complete_check(). |
| 4299 | */ |
| 4300 | void |
| 4301 | ins_compl_insert(int in_compl_func) |
| 4302 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4303 | int compl_len = get_compl_len(); |
Bram Moolenaar | 4b28ba3 | 2021-12-27 19:28:37 +0000 | [diff] [blame] | 4304 | |
| 4305 | // Make sure we don't go over the end of the string, this can happen with |
| 4306 | // illegal bytes. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4307 | if (compl_len < (int)compl_shown_match->cp_str.length) |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 4308 | ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1); |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4309 | if (match_at_original_text(compl_shown_match)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4310 | compl_used_match = FALSE; |
| 4311 | else |
| 4312 | compl_used_match = TRUE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 4313 | #ifdef FEAT_EVAL |
| 4314 | { |
| 4315 | dict_T *dict = ins_compl_dict_alloc(compl_shown_match); |
| 4316 | |
| 4317 | set_vim_var_dict(VV_COMPLETED_ITEM, dict); |
| 4318 | } |
| 4319 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4320 | if (!in_compl_func) |
| 4321 | compl_curr_match = compl_shown_match; |
| 4322 | } |
| 4323 | |
| 4324 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4325 | * show the file name for the completion match (if any). Truncate the file |
| 4326 | * name to avoid a wait for return. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4327 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4328 | static void |
| 4329 | ins_compl_show_filename(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4330 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4331 | char *lead = _("match in file"); |
| 4332 | int space = sc_col - vim_strsize((char_u *)lead) - 2; |
| 4333 | char_u *s; |
| 4334 | char_u *e; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4335 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4336 | if (space <= 0) |
| 4337 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4338 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4339 | // We need the tail that fits. With double-byte encoding going |
| 4340 | // back from the end is very slow, thus go from the start and keep |
| 4341 | // the text that fits in "space" between "s" and "e". |
| 4342 | 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] | 4343 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4344 | space -= ptr2cells(e); |
| 4345 | while (space < 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4346 | { |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4347 | space += ptr2cells(s); |
| 4348 | MB_PTR_ADV(s); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4349 | } |
| 4350 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4351 | msg_hist_off = TRUE; |
| 4352 | vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, |
| 4353 | s > compl_shown_match->cp_fname ? "<" : "", s); |
| 4354 | msg((char *)IObuff); |
| 4355 | msg_hist_off = FALSE; |
| 4356 | redraw_cmdline = FALSE; // don't overwrite! |
| 4357 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4358 | |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4359 | /* |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 4360 | * Find a completion item when 'completeopt' contains "fuzzy". |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4361 | */ |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4362 | static compl_T * |
| 4363 | find_comp_when_fuzzy(void) |
| 4364 | { |
| 4365 | int score; |
| 4366 | char_u* str; |
| 4367 | int target_idx = -1; |
| 4368 | int is_forward = compl_shows_dir_forward(); |
| 4369 | int is_backward = compl_shows_dir_backward(); |
| 4370 | compl_T *comp = NULL; |
| 4371 | |
glepnir | 43eef88 | 2024-06-19 20:20:48 +0200 | [diff] [blame] | 4372 | if ((is_forward && compl_selected_item == compl_match_arraysize - 1) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4373 | || (is_backward && compl_selected_item == 0)) |
glepnir | 65407ce | 2024-07-06 16:09:19 +0200 | [diff] [blame] | 4374 | return compl_first_match != compl_shown_match ? compl_first_match : |
| 4375 | (compl_first_match->cp_prev ? compl_first_match->cp_prev : NULL); |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4376 | |
| 4377 | if (is_forward) |
| 4378 | target_idx = compl_selected_item + 1; |
| 4379 | else if (is_backward) |
| 4380 | target_idx = compl_selected_item == -1 ? compl_match_arraysize - 1 |
glepnir | dca57fb | 2024-06-04 22:01:21 +0200 | [diff] [blame] | 4381 | : compl_selected_item - 1; |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4382 | |
| 4383 | score = compl_match_array[target_idx].pum_score; |
| 4384 | str = compl_match_array[target_idx].pum_text; |
| 4385 | |
| 4386 | comp = compl_first_match; |
| 4387 | do { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4388 | if (comp->cp_score == score && (str == comp->cp_str.string || str == comp->cp_text[CPT_ABBR])) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4389 | return comp; |
| 4390 | comp = comp->cp_next; |
| 4391 | } while (comp != NULL && !is_first_match(comp)); |
| 4392 | |
| 4393 | return NULL; |
| 4394 | } |
| 4395 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4396 | /* |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4397 | * Find the next set of matches for completion. Repeat the completion "todo" |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4398 | * times. The number of matches found is returned in 'num_matches'. |
| 4399 | * |
| 4400 | * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to |
| 4401 | * get more completions. If it is FALSE, then do nothing when there are no more |
| 4402 | * completions in the given direction. |
| 4403 | * |
| 4404 | * If "advance" is TRUE, then completion will move to the first match. |
| 4405 | * Otherwise, the original text will be shown. |
| 4406 | * |
| 4407 | * Returns OK on success and -1 if the number of matches are unknown. |
| 4408 | */ |
| 4409 | static int |
| 4410 | find_next_completion_match( |
| 4411 | int allow_get_expansion, |
| 4412 | int todo, // repeat completion this many times |
| 4413 | int advance, |
| 4414 | int *num_matches) |
| 4415 | { |
| 4416 | int found_end = FALSE; |
| 4417 | compl_T *found_compl = NULL; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 4418 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 4419 | int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0; |
| 4420 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4421 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4422 | while (--todo >= 0) |
| 4423 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4424 | if (compl_shows_dir_forward() && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4425 | { |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 4426 | compl_shown_match = compl_fuzzy_match && compl_match_array != NULL |
| 4427 | ? find_comp_when_fuzzy() : compl_shown_match->cp_next; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4428 | found_end = (compl_first_match != NULL |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4429 | && (is_first_match(compl_shown_match->cp_next) |
| 4430 | || is_first_match(compl_shown_match))); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4431 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4432 | else if (compl_shows_dir_backward() |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4433 | && compl_shown_match->cp_prev != NULL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4434 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4435 | found_end = is_first_match(compl_shown_match); |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 4436 | compl_shown_match = compl_fuzzy_match && compl_match_array != NULL |
| 4437 | ? find_comp_when_fuzzy() : compl_shown_match->cp_prev; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4438 | found_end |= is_first_match(compl_shown_match); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4439 | } |
| 4440 | else |
| 4441 | { |
| 4442 | if (!allow_get_expansion) |
| 4443 | { |
| 4444 | if (advance) |
| 4445 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4446 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4447 | compl_pending -= todo + 1; |
| 4448 | else |
| 4449 | compl_pending += todo + 1; |
| 4450 | } |
| 4451 | return -1; |
| 4452 | } |
| 4453 | |
| 4454 | if (!compl_no_select && advance) |
| 4455 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4456 | if (compl_shows_dir_backward()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4457 | --compl_pending; |
| 4458 | else |
| 4459 | ++compl_pending; |
| 4460 | } |
| 4461 | |
| 4462 | // Find matches. |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4463 | *num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4464 | |
| 4465 | // handle any pending completions |
| 4466 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4467 | && advance) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4468 | { |
| 4469 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 4470 | { |
| 4471 | compl_shown_match = compl_shown_match->cp_next; |
| 4472 | --compl_pending; |
| 4473 | } |
| 4474 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 4475 | { |
| 4476 | compl_shown_match = compl_shown_match->cp_prev; |
| 4477 | ++compl_pending; |
| 4478 | } |
| 4479 | else |
| 4480 | break; |
| 4481 | } |
| 4482 | found_end = FALSE; |
| 4483 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4484 | if (!match_at_original_text(compl_shown_match) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4485 | && compl_leader.string != NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4486 | && !ins_compl_equal(compl_shown_match, |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4487 | compl_leader.string, (int)compl_leader.length) |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4488 | && !(compl_fuzzy_match && compl_shown_match->cp_score > 0)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4489 | ++todo; |
| 4490 | else |
| 4491 | // Remember a matching item. |
| 4492 | found_compl = compl_shown_match; |
| 4493 | |
| 4494 | // Stop at the end of the list when we found a usable match. |
| 4495 | if (found_end) |
| 4496 | { |
| 4497 | if (found_compl != NULL) |
| 4498 | { |
| 4499 | compl_shown_match = found_compl; |
| 4500 | break; |
| 4501 | } |
| 4502 | todo = 1; // use first usable match after wrapping around |
| 4503 | } |
| 4504 | } |
| 4505 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4506 | return OK; |
| 4507 | } |
| 4508 | |
| 4509 | /* |
| 4510 | * Fill in the next completion in the current direction. |
| 4511 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 4512 | * get more completions. If it is FALSE, then we just do nothing when there |
| 4513 | * are no more completions in a given direction. The latter case is used when |
| 4514 | * we are still in the middle of finding completions, to allow browsing |
| 4515 | * through the ones found so far. |
| 4516 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 4517 | * |
| 4518 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 4519 | * compl_shown_match here. |
| 4520 | * |
| 4521 | * Note that this function may be called recursively once only. First with |
| 4522 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 4523 | * calls this function with "allow_get_expansion" FALSE. |
| 4524 | */ |
| 4525 | static int |
| 4526 | ins_compl_next( |
| 4527 | int allow_get_expansion, |
| 4528 | int count, // repeat completion this many times; should |
| 4529 | // be at least 1 |
| 4530 | int insert_match, // Insert the newly selected match |
| 4531 | int in_compl_func) // called from complete_check() |
| 4532 | { |
| 4533 | int num_matches = -1; |
| 4534 | int todo = count; |
| 4535 | int advance; |
| 4536 | int started = compl_started; |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4537 | buf_T *orig_curbuf = curbuf; |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 4538 | unsigned int cur_cot_flags = get_cot_flags(); |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 4539 | int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0; |
| 4540 | int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4541 | |
| 4542 | // When user complete function return -1 for findstart which is next |
| 4543 | // time of 'always', compl_shown_match become NULL. |
| 4544 | if (compl_shown_match == NULL) |
| 4545 | return -1; |
| 4546 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4547 | if (compl_leader.string != NULL |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 4548 | && !match_at_original_text(compl_shown_match) |
| 4549 | && !compl_fuzzy_match) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4550 | // Update "compl_shown_match" to the actually shown match |
| 4551 | ins_compl_update_shown_match(); |
| 4552 | |
| 4553 | if (allow_get_expansion && insert_match |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 4554 | && (!compl_get_longest || compl_used_match)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4555 | // Delete old text to be replaced |
| 4556 | ins_compl_delete(); |
| 4557 | |
| 4558 | // When finding the longest common text we stick at the original text, |
| 4559 | // don't let CTRL-N or CTRL-P move to the first match. |
| 4560 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 4561 | |
| 4562 | // When restarting the search don't insert the first match either. |
| 4563 | if (compl_restarting) |
| 4564 | { |
| 4565 | advance = FALSE; |
| 4566 | compl_restarting = FALSE; |
| 4567 | } |
| 4568 | |
| 4569 | // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 4570 | // around. |
| 4571 | if (find_next_completion_match(allow_get_expansion, todo, advance, |
| 4572 | &num_matches) == -1) |
| 4573 | return -1; |
| 4574 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 4575 | if (curbuf != orig_curbuf) |
| 4576 | { |
| 4577 | // In case some completion function switched buffer, don't want to |
| 4578 | // insert the completion elsewhere. |
| 4579 | return -1; |
| 4580 | } |
| 4581 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4582 | // Insert the text of the new completion, or the compl_leader. |
| 4583 | if (compl_no_insert && !started) |
| 4584 | { |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 4585 | ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4586 | compl_used_match = FALSE; |
| 4587 | } |
| 4588 | else if (insert_match) |
| 4589 | { |
| 4590 | if (!compl_get_longest || compl_used_match) |
| 4591 | ins_compl_insert(in_compl_func); |
| 4592 | else |
glepnir | 6a38aff | 2024-12-16 21:56:16 +0100 | [diff] [blame] | 4593 | ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4594 | } |
| 4595 | else |
| 4596 | compl_used_match = FALSE; |
| 4597 | |
| 4598 | if (!allow_get_expansion) |
| 4599 | { |
| 4600 | // may undisplay the popup menu first |
| 4601 | ins_compl_upd_pum(); |
| 4602 | |
| 4603 | if (pum_enough_matches()) |
| 4604 | // Will display the popup menu, don't redraw yet to avoid flicker. |
| 4605 | pum_call_update_screen(); |
| 4606 | else |
| 4607 | // Not showing the popup menu yet, redraw to show the user what was |
| 4608 | // inserted. |
| 4609 | update_screen(0); |
| 4610 | |
| 4611 | // display the updated popup menu |
| 4612 | ins_compl_show_pum(); |
| 4613 | #ifdef FEAT_GUI |
| 4614 | if (gui.in_use) |
| 4615 | { |
| 4616 | // Show the cursor after the match, not after the redrawn text. |
| 4617 | setcursor(); |
| 4618 | out_flush_cursor(FALSE, FALSE); |
| 4619 | } |
| 4620 | #endif |
| 4621 | |
| 4622 | // Delete old text to be replaced, since we're still searching and |
| 4623 | // don't want to match ourselves! |
| 4624 | ins_compl_delete(); |
| 4625 | } |
| 4626 | |
| 4627 | // Enter will select a match when the match wasn't inserted and the popup |
| 4628 | // menu is visible. |
| 4629 | if (compl_no_insert && !started) |
| 4630 | compl_enter_selects = TRUE; |
| 4631 | else |
| 4632 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 4633 | |
| 4634 | // Show the file name for the match (if any) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4635 | if (compl_shown_match->cp_fname != NULL) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 4636 | ins_compl_show_filename(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4637 | |
| 4638 | return num_matches; |
| 4639 | } |
| 4640 | |
| 4641 | /* |
| 4642 | * Call this while finding completions, to check whether the user has hit a key |
| 4643 | * that should change the currently displayed completion, or exit completion |
| 4644 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
| 4645 | * possible. -- webb |
| 4646 | * "frequency" specifies out of how many calls we actually check. |
| 4647 | * "in_compl_func" is TRUE when called from complete_check(), don't set |
| 4648 | * compl_curr_match. |
| 4649 | */ |
| 4650 | void |
| 4651 | ins_compl_check_keys(int frequency, int in_compl_func) |
| 4652 | { |
| 4653 | static int count = 0; |
| 4654 | int c; |
| 4655 | |
| 4656 | // Don't check when reading keys from a script, :normal or feedkeys(). |
| 4657 | // That would break the test scripts. But do check for keys when called |
| 4658 | // from complete_check(). |
| 4659 | if (!in_compl_func && (using_script() || ex_normal_busy)) |
| 4660 | return; |
| 4661 | |
| 4662 | // Only do this at regular intervals |
| 4663 | if (++count < frequency) |
| 4664 | return; |
| 4665 | count = 0; |
| 4666 | |
| 4667 | // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 4668 | // can't do its work correctly. |
| 4669 | c = vpeekc_any(); |
| 4670 | if (c != NUL) |
| 4671 | { |
| 4672 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 4673 | { |
| 4674 | c = safe_vgetc(); // Eat the character |
| 4675 | compl_shows_dir = ins_compl_key2dir(c); |
| 4676 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 4677 | c != K_UP && c != K_DOWN, in_compl_func); |
| 4678 | } |
| 4679 | else |
| 4680 | { |
| 4681 | // Need to get the character to have KeyTyped set. We'll put it |
| 4682 | // back with vungetc() below. But skip K_IGNORE. |
| 4683 | c = safe_vgetc(); |
| 4684 | if (c != K_IGNORE) |
| 4685 | { |
| 4686 | // Don't interrupt completion when the character wasn't typed, |
| 4687 | // e.g., when doing @q to replay keys. |
| 4688 | if (c != Ctrl_R && KeyTyped) |
| 4689 | compl_interrupted = TRUE; |
| 4690 | |
| 4691 | vungetc(c); |
| 4692 | } |
| 4693 | } |
| 4694 | } |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 4695 | if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4696 | { |
| 4697 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 4698 | |
| 4699 | compl_pending = 0; |
| 4700 | (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func); |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | /* |
| 4705 | * Decide the direction of Insert mode complete from the key typed. |
| 4706 | * Returns BACKWARD or FORWARD. |
| 4707 | */ |
| 4708 | static int |
| 4709 | ins_compl_key2dir(int c) |
| 4710 | { |
| 4711 | if (c == Ctrl_P || c == Ctrl_L |
| 4712 | || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP) |
| 4713 | return BACKWARD; |
| 4714 | return FORWARD; |
| 4715 | } |
| 4716 | |
| 4717 | /* |
| 4718 | * Return TRUE for keys that are used for completion only when the popup menu |
| 4719 | * is visible. |
| 4720 | */ |
| 4721 | static int |
| 4722 | ins_compl_pum_key(int c) |
| 4723 | { |
| 4724 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
| 4725 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 4726 | || c == K_UP || c == K_DOWN); |
| 4727 | } |
| 4728 | |
| 4729 | /* |
| 4730 | * Decide the number of completions to move forward. |
| 4731 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 4732 | */ |
| 4733 | static int |
| 4734 | ins_compl_key2count(int c) |
| 4735 | { |
| 4736 | int h; |
| 4737 | |
| 4738 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
| 4739 | { |
| 4740 | h = pum_get_height(); |
| 4741 | if (h > 3) |
| 4742 | h -= 2; // keep some context |
| 4743 | return h; |
| 4744 | } |
| 4745 | return 1; |
| 4746 | } |
| 4747 | |
| 4748 | /* |
| 4749 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 4750 | * to change the currently selected completion. |
| 4751 | */ |
| 4752 | static int |
| 4753 | ins_compl_use_match(int c) |
| 4754 | { |
| 4755 | switch (c) |
| 4756 | { |
| 4757 | case K_UP: |
| 4758 | case K_DOWN: |
| 4759 | case K_PAGEDOWN: |
| 4760 | case K_KPAGEDOWN: |
| 4761 | case K_S_DOWN: |
| 4762 | case K_PAGEUP: |
| 4763 | case K_KPAGEUP: |
| 4764 | case K_S_UP: |
| 4765 | return FALSE; |
| 4766 | } |
| 4767 | return TRUE; |
| 4768 | } |
| 4769 | |
| 4770 | /* |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4771 | * Get the pattern, column and length for normal completion (CTRL-N CTRL-P |
| 4772 | * completion) |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4773 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4774 | * Uses the global variables: compl_cont_status and ctrl_x_mode |
| 4775 | */ |
| 4776 | static int |
| 4777 | get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 4778 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4779 | if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4780 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4781 | if (!compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4782 | { |
| 4783 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
| 4784 | ; |
| 4785 | compl_col += ++startcol; |
| 4786 | compl_length = curs_col - startcol; |
| 4787 | } |
| 4788 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4789 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4790 | compl_pattern.string = str_foldcase(line + compl_col, |
| 4791 | compl_length, NULL, 0); |
| 4792 | if (compl_pattern.string == NULL) |
| 4793 | { |
| 4794 | compl_pattern.length = 0; |
| 4795 | return FAIL; |
| 4796 | } |
| 4797 | compl_pattern.length = STRLEN(compl_pattern.string); |
| 4798 | } |
| 4799 | else |
| 4800 | { |
| 4801 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 4802 | if (compl_pattern.string == NULL) |
| 4803 | { |
| 4804 | compl_pattern.length = 0; |
| 4805 | return FAIL; |
| 4806 | } |
| 4807 | compl_pattern.length = (size_t)compl_length; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4808 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4809 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 4810 | else if (compl_status_adding()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4811 | { |
| 4812 | char_u *prefix = (char_u *)"\\<"; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4813 | size_t prefixlen = STRLEN_LITERAL("\\<"); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4814 | size_t n; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4815 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4816 | if (!vim_iswordp(line + compl_col) |
| 4817 | || (compl_col > 0 |
| 4818 | && (vim_iswordp(mb_prevptr(line, line + compl_col))))) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4819 | { |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4820 | prefix = (char_u *)""; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4821 | prefixlen = 0; |
| 4822 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4823 | |
| 4824 | // we need up to 2 extra chars for the prefix |
| 4825 | n = quote_meta(NULL, line + compl_col, compl_length) + prefixlen; |
| 4826 | compl_pattern.string = alloc(n); |
| 4827 | if (compl_pattern.string == NULL) |
| 4828 | { |
| 4829 | compl_pattern.length = 0; |
| 4830 | return FAIL; |
| 4831 | } |
| 4832 | STRCPY((char *)compl_pattern.string, prefix); |
| 4833 | (void)quote_meta(compl_pattern.string + prefixlen, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4834 | line + compl_col, compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4835 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4836 | } |
| 4837 | else if (--startcol < 0 |
| 4838 | || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) |
| 4839 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4840 | size_t len = STRLEN_LITERAL("\\<\\k\\k"); |
| 4841 | |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4842 | // Match any word of at least two chars |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4843 | compl_pattern.string = vim_strnsave((char_u *)"\\<\\k\\k", len); |
| 4844 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4845 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4846 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4847 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4848 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4849 | compl_pattern.length = len; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4850 | compl_col += curs_col; |
| 4851 | compl_length = 0; |
| 4852 | } |
| 4853 | else |
| 4854 | { |
| 4855 | // Search the point of change class of multibyte character |
| 4856 | // or not a word single byte character backward. |
| 4857 | if (has_mbyte) |
| 4858 | { |
| 4859 | int base_class; |
| 4860 | int head_off; |
| 4861 | |
| 4862 | startcol -= (*mb_head_off)(line, line + startcol); |
| 4863 | base_class = mb_get_class(line + startcol); |
| 4864 | while (--startcol >= 0) |
| 4865 | { |
| 4866 | head_off = (*mb_head_off)(line, line + startcol); |
| 4867 | if (base_class != mb_get_class(line + startcol |
| 4868 | - head_off)) |
| 4869 | break; |
| 4870 | startcol -= head_off; |
| 4871 | } |
| 4872 | } |
| 4873 | else |
| 4874 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
| 4875 | ; |
| 4876 | compl_col += ++startcol; |
| 4877 | compl_length = (int)curs_col - startcol; |
| 4878 | if (compl_length == 1) |
| 4879 | { |
| 4880 | // Only match word with at least two chars -- webb |
| 4881 | // there's no need to call quote_meta, |
| 4882 | // alloc(7) is enough -- Acevedo |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4883 | compl_pattern.string = alloc(7); |
| 4884 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4885 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4886 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4887 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4888 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4889 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 4890 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, 1); |
| 4891 | STRCAT((char *)compl_pattern.string, "\\k"); |
| 4892 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4893 | } |
| 4894 | else |
| 4895 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4896 | size_t n = quote_meta(NULL, line + compl_col, compl_length) + 2; |
| 4897 | |
| 4898 | compl_pattern.string = alloc(n); |
| 4899 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4900 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4901 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4902 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4903 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4904 | STRCPY((char *)compl_pattern.string, "\\<"); |
| 4905 | (void)quote_meta(compl_pattern.string + 2, line + compl_col, |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4906 | compl_length); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4907 | compl_pattern.length = n - 1; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | return OK; |
| 4912 | } |
| 4913 | |
| 4914 | /* |
| 4915 | * Get the pattern, column and length for whole line completion or for the |
| 4916 | * complete() function. |
| 4917 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 4918 | */ |
| 4919 | static int |
| 4920 | get_wholeline_compl_info(char_u *line, colnr_T curs_col) |
| 4921 | { |
| 4922 | compl_col = (colnr_T)getwhitecols(line); |
| 4923 | compl_length = (int)curs_col - (int)compl_col; |
| 4924 | if (compl_length < 0) // cursor in indent: empty pattern |
| 4925 | compl_length = 0; |
| 4926 | if (p_ic) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4927 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4928 | compl_pattern.string = str_foldcase(line + compl_col, compl_length, |
| 4929 | NULL, 0); |
| 4930 | if (compl_pattern.string == NULL) |
| 4931 | { |
| 4932 | compl_pattern.length = 0; |
| 4933 | return FAIL; |
| 4934 | } |
| 4935 | compl_pattern.length = STRLEN(compl_pattern.string); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4936 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4937 | else |
| 4938 | { |
| 4939 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 4940 | if (compl_pattern.string == NULL) |
| 4941 | { |
| 4942 | compl_pattern.length = 0; |
| 4943 | return FAIL; |
| 4944 | } |
| 4945 | compl_pattern.length = (size_t)compl_length; |
| 4946 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4947 | |
| 4948 | return OK; |
| 4949 | } |
| 4950 | |
| 4951 | /* |
| 4952 | * Get the pattern, column and length for filename completion. |
| 4953 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 4954 | */ |
| 4955 | static int |
| 4956 | get_filename_compl_info(char_u *line, int startcol, colnr_T curs_col) |
| 4957 | { |
| 4958 | // Go back to just before the first filename character. |
| 4959 | if (startcol > 0) |
| 4960 | { |
| 4961 | char_u *p = line + startcol; |
| 4962 | |
| 4963 | MB_PTR_BACK(line, p); |
| 4964 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 4965 | MB_PTR_BACK(line, p); |
| 4966 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 4967 | startcol = 0; |
| 4968 | else |
| 4969 | startcol = (int)(p - line) + 1; |
| 4970 | } |
| 4971 | |
| 4972 | compl_col += startcol; |
| 4973 | compl_length = (int)curs_col - startcol; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4974 | compl_pattern.string = addstar(line + compl_col, compl_length, EXPAND_FILES); |
| 4975 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4976 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4977 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4978 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4979 | } |
| 4980 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4981 | compl_pattern.length = STRLEN(compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4982 | |
| 4983 | return OK; |
| 4984 | } |
| 4985 | |
| 4986 | /* |
| 4987 | * Get the pattern, column and length for command-line completion. |
| 4988 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 4989 | */ |
| 4990 | static int |
| 4991 | get_cmdline_compl_info(char_u *line, colnr_T curs_col) |
| 4992 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4993 | compl_pattern.string = vim_strnsave(line, curs_col); |
| 4994 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4995 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4996 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 4997 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4998 | } |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 4999 | compl_pattern.length = curs_col; |
| 5000 | set_cmd_context(&compl_xp, compl_pattern.string, |
| 5001 | (int)compl_pattern.length, curs_col, FALSE); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5002 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5003 | || compl_xp.xp_context == EXPAND_NOTHING) |
| 5004 | // No completion possible, use an empty pattern to get a |
| 5005 | // "pattern not found" message. |
| 5006 | compl_col = curs_col; |
| 5007 | else |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5008 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern.string); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5009 | compl_length = curs_col - compl_col; |
| 5010 | |
| 5011 | return OK; |
| 5012 | } |
| 5013 | |
| 5014 | /* |
| 5015 | * Get the pattern, column and length for user defined completion ('omnifunc', |
| 5016 | * 'completefunc' and 'thesaurusfunc') |
| 5017 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5018 | * Uses the global variable: spell_bad_len |
| 5019 | */ |
| 5020 | static int |
| 5021 | get_userdefined_compl_info(colnr_T curs_col UNUSED) |
| 5022 | { |
| 5023 | int ret = FAIL; |
| 5024 | |
| 5025 | #ifdef FEAT_COMPL_FUNC |
| 5026 | // Call user defined function 'completefunc' with "a:findstart" |
| 5027 | // set to 1 to obtain the length of text to use for completion. |
| 5028 | char_u *line; |
| 5029 | typval_T args[3]; |
| 5030 | int col; |
| 5031 | char_u *funcname; |
| 5032 | pos_T pos; |
| 5033 | int save_State = State; |
| 5034 | callback_T *cb; |
| 5035 | |
| 5036 | // Call 'completefunc' or 'omnifunc' or 'thesaurusfunc' and get pattern |
| 5037 | // length as a string |
| 5038 | funcname = get_complete_funcname(ctrl_x_mode); |
| 5039 | if (*funcname == NUL) |
| 5040 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5041 | semsg(_(e_option_str_is_not_set), ctrl_x_mode_function() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5042 | ? "completefunc" : "omnifunc"); |
| 5043 | return FAIL; |
| 5044 | } |
| 5045 | |
| 5046 | args[0].v_type = VAR_NUMBER; |
| 5047 | args[0].vval.v_number = 1; |
| 5048 | args[1].v_type = VAR_STRING; |
| 5049 | args[1].vval.v_string = (char_u *)""; |
| 5050 | args[2].v_type = VAR_UNKNOWN; |
| 5051 | pos = curwin->w_cursor; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5052 | ++textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5053 | cb = get_insert_callback(ctrl_x_mode); |
| 5054 | col = call_callback_retnr(cb, 2, args); |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 5055 | --textlock; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5056 | |
| 5057 | State = save_State; |
| 5058 | curwin->w_cursor = pos; // restore the cursor position |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 5059 | check_cursor(); // make sure cursor position is valid, just in case |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5060 | validate_cursor(); |
| 5061 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 5062 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 5063 | emsg(_(e_complete_function_deleted_text)); |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5064 | return FAIL; |
| 5065 | } |
| 5066 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5067 | // Return value -2 means the user complete function wants to cancel the |
| 5068 | // complete without an error, do the same if the function did not execute |
| 5069 | // successfully. |
| 5070 | if (col == -2 || aborting()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5071 | return FAIL; |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 5072 | // 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] | 5073 | if (col == -3) |
| 5074 | { |
| 5075 | ctrl_x_mode = CTRL_X_NORMAL; |
| 5076 | edit_submode = NULL; |
| 5077 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 5078 | msg_clr_cmdline(); |
| 5079 | return FAIL; |
| 5080 | } |
| 5081 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 5082 | // Reset extended parameters of completion, when starting new |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5083 | // completion. |
| 5084 | compl_opt_refresh_always = FALSE; |
| 5085 | compl_opt_suppress_empty = FALSE; |
| 5086 | |
| 5087 | if (col < 0) |
| 5088 | col = curs_col; |
| 5089 | compl_col = col; |
| 5090 | if (compl_col > curs_col) |
| 5091 | compl_col = curs_col; |
| 5092 | |
| 5093 | // Setup variables for completion. Need to obtain "line" again, |
| 5094 | // it may have become invalid. |
| 5095 | line = ml_get(curwin->w_cursor.lnum); |
| 5096 | compl_length = curs_col - compl_col; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5097 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5098 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5099 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5100 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5101 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5102 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5103 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5104 | compl_pattern.length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5105 | ret = OK; |
| 5106 | #endif |
| 5107 | |
| 5108 | return ret; |
| 5109 | } |
| 5110 | |
| 5111 | /* |
| 5112 | * Get the pattern, column and length for spell completion. |
| 5113 | * Sets the global variables: compl_col, compl_length and compl_pattern. |
| 5114 | * Uses the global variable: spell_bad_len |
| 5115 | */ |
| 5116 | static int |
| 5117 | get_spell_compl_info(int startcol UNUSED, colnr_T curs_col UNUSED) |
| 5118 | { |
| 5119 | int ret = FAIL; |
| 5120 | #ifdef FEAT_SPELL |
| 5121 | char_u *line; |
| 5122 | |
| 5123 | if (spell_bad_len > 0) |
| 5124 | compl_col = curs_col - spell_bad_len; |
| 5125 | else |
| 5126 | compl_col = spell_word_start(startcol); |
| 5127 | if (compl_col >= (colnr_T)startcol) |
| 5128 | { |
| 5129 | compl_length = 0; |
| 5130 | compl_col = curs_col; |
| 5131 | } |
| 5132 | else |
| 5133 | { |
| 5134 | spell_expand_check_cap(compl_col); |
| 5135 | compl_length = (int)curs_col - compl_col; |
| 5136 | } |
| 5137 | // Need to obtain "line" again, it may have become invalid. |
| 5138 | line = ml_get(curwin->w_cursor.lnum); |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5139 | compl_pattern.string = vim_strnsave(line + compl_col, (size_t)compl_length); |
| 5140 | if (compl_pattern.string == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5141 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5142 | compl_pattern.length = 0; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5143 | return FAIL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 5144 | } |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5145 | |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5146 | compl_pattern.length = (size_t)compl_length; |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5147 | ret = OK; |
| 5148 | #endif |
| 5149 | |
| 5150 | return ret; |
| 5151 | } |
| 5152 | |
| 5153 | /* |
| 5154 | * Get the completion pattern, column and length. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5155 | * "startcol" - start column number of the completion pattern/text |
| 5156 | * "cur_col" - current cursor column |
| 5157 | * On return, "line_invalid" is set to TRUE, if the current line may have |
| 5158 | * become invalid and needs to be fetched again. |
| 5159 | * Returns OK on success. |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5160 | */ |
| 5161 | static int |
| 5162 | compl_get_info(char_u *line, int startcol, colnr_T curs_col, int *line_invalid) |
| 5163 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5164 | if (ctrl_x_mode_normal() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5165 | || (ctrl_x_mode & CTRL_X_WANT_IDENT |
| 5166 | && !thesaurus_func_complete(ctrl_x_mode))) |
| 5167 | { |
| 5168 | return get_normal_compl_info(line, startcol, curs_col); |
| 5169 | } |
| 5170 | else if (ctrl_x_mode_line_or_eval()) |
| 5171 | { |
| 5172 | return get_wholeline_compl_info(line, curs_col); |
| 5173 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5174 | else if (ctrl_x_mode_files()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5175 | { |
| 5176 | return get_filename_compl_info(line, startcol, curs_col); |
| 5177 | } |
| 5178 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 5179 | { |
| 5180 | return get_cmdline_compl_info(line, curs_col); |
| 5181 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5182 | else if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5183 | || thesaurus_func_complete(ctrl_x_mode)) |
| 5184 | { |
| 5185 | if (get_userdefined_compl_info(curs_col) == FAIL) |
| 5186 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5187 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5188 | } |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5189 | else if (ctrl_x_mode_spell()) |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5190 | { |
| 5191 | if (get_spell_compl_info(startcol, curs_col) == FAIL) |
| 5192 | return FAIL; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5193 | *line_invalid = TRUE; // "line" may have become invalid |
Bram Moolenaar | bf7ff61 | 2021-12-27 12:52:07 +0000 | [diff] [blame] | 5194 | } |
| 5195 | else |
| 5196 | { |
| 5197 | internal_error("ins_complete()"); |
| 5198 | return FAIL; |
| 5199 | } |
| 5200 | |
| 5201 | return OK; |
| 5202 | } |
| 5203 | |
| 5204 | /* |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5205 | * Continue an interrupted completion mode search in "line". |
| 5206 | * |
| 5207 | * If this same ctrl_x_mode has been interrupted use the text from |
| 5208 | * "compl_startpos" to the cursor as a pattern to add a new word instead of |
| 5209 | * expand the one before the cursor, in word-wise if "compl_startpos" is not in |
| 5210 | * the same line as the cursor then fix it (the line has been split because it |
| 5211 | * was longer than 'tw'). if SOL is set then skip the previous pattern, a word |
| 5212 | * 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] | 5213 | */ |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5214 | static void |
| 5215 | ins_compl_continue_search(char_u *line) |
| 5216 | { |
| 5217 | // it is a continued search |
| 5218 | compl_cont_status &= ~CONT_INTRPT; // remove INTRPT |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5219 | if (ctrl_x_mode_normal() || ctrl_x_mode_path_patterns() |
| 5220 | || ctrl_x_mode_path_defines()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5221 | { |
| 5222 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
| 5223 | { |
| 5224 | // line (probably) wrapped, set compl_startpos to the |
| 5225 | // first non_blank in the line, if it is not a wordchar |
| 5226 | // include it to get a better pattern, but then we don't |
| 5227 | // want the "\\<" prefix, check it below |
| 5228 | compl_col = (colnr_T)getwhitecols(line); |
| 5229 | compl_startpos.col = compl_col; |
| 5230 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5231 | compl_cont_status &= ~CONT_SOL; // clear SOL if present |
| 5232 | } |
| 5233 | else |
| 5234 | { |
| 5235 | // S_IPOS was set when we inserted a word that was at the |
| 5236 | // beginning of the line, which means that we'll go to SOL |
| 5237 | // mode but first we need to redefine compl_startpos |
| 5238 | if (compl_cont_status & CONT_S_IPOS) |
| 5239 | { |
| 5240 | compl_cont_status |= CONT_SOL; |
| 5241 | compl_startpos.col = (colnr_T)(skipwhite( |
| 5242 | line + compl_length |
| 5243 | + compl_startpos.col) - line); |
| 5244 | } |
| 5245 | compl_col = compl_startpos.col; |
| 5246 | } |
| 5247 | compl_length = curwin->w_cursor.col - (int)compl_col; |
| 5248 | // IObuff is used to add a "word from the next line" would we |
| 5249 | // have enough space? just being paranoid |
| 5250 | #define MIN_SPACE 75 |
| 5251 | if (compl_length > (IOSIZE - MIN_SPACE)) |
| 5252 | { |
| 5253 | compl_cont_status &= ~CONT_SOL; |
| 5254 | compl_length = (IOSIZE - MIN_SPACE); |
| 5255 | compl_col = curwin->w_cursor.col - compl_length; |
| 5256 | } |
| 5257 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 5258 | if (compl_length < 1) |
| 5259 | compl_cont_status &= CONT_LOCAL; |
| 5260 | } |
| 5261 | else if (ctrl_x_mode_line_or_eval()) |
| 5262 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
| 5263 | else |
| 5264 | compl_cont_status = 0; |
| 5265 | } |
| 5266 | |
| 5267 | /* |
| 5268 | * start insert mode completion |
| 5269 | */ |
| 5270 | static int |
| 5271 | ins_compl_start(void) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5272 | { |
| 5273 | char_u *line; |
| 5274 | int startcol = 0; // column where searched text starts |
| 5275 | colnr_T curs_col; // cursor column |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5276 | int line_invalid = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5277 | int save_did_ai = did_ai; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 5278 | int flags = CP_ORIGINAL_TEXT; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5279 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5280 | // First time we hit ^N or ^P (in a row, I mean) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5281 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5282 | did_ai = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5283 | did_si = FALSE; |
| 5284 | can_si = FALSE; |
| 5285 | can_si_back = FALSE; |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5286 | if (stop_arrow() == FAIL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5287 | return FAIL; |
| 5288 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5289 | line = ml_get(curwin->w_cursor.lnum); |
| 5290 | curs_col = curwin->w_cursor.col; |
| 5291 | compl_pending = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5292 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5293 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 5294 | && compl_cont_mode == ctrl_x_mode) |
| 5295 | // this same ctrl-x_mode was interrupted previously. Continue the |
| 5296 | // completion. |
| 5297 | ins_compl_continue_search(line); |
| 5298 | else |
| 5299 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5300 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5301 | if (!compl_status_adding()) // normal expansion |
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 | compl_cont_mode = ctrl_x_mode; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5304 | if (ctrl_x_mode_not_default()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5305 | // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL |
| 5306 | compl_cont_status = 0; |
| 5307 | compl_cont_status |= CONT_N_ADDS; |
| 5308 | compl_startpos = curwin->w_cursor; |
| 5309 | startcol = (int)curs_col; |
| 5310 | compl_col = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5311 | } |
| 5312 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5313 | // Work out completion pattern and original text -- webb |
| 5314 | if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL) |
| 5315 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5316 | if (ctrl_x_mode_function() || ctrl_x_mode_omni() |
| 5317 | || thesaurus_func_complete(ctrl_x_mode)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5318 | // restore did_ai, so that adding comment leader works |
| 5319 | did_ai = save_did_ai; |
| 5320 | return FAIL; |
| 5321 | } |
| 5322 | // If "line" was changed while getting completion info get it again. |
| 5323 | if (line_invalid) |
| 5324 | line = ml_get(curwin->w_cursor.lnum); |
| 5325 | |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5326 | int in_fuzzy = get_cot_flags() & COT_FUZZY; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5327 | if (compl_status_adding()) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5328 | { |
| 5329 | edit_submode_pre = (char_u *)_(" Adding"); |
| 5330 | if (ctrl_x_mode_line_or_eval()) |
| 5331 | { |
| 5332 | // Insert a new line, keep indentation but ignore 'comments'. |
| 5333 | char_u *old = curbuf->b_p_com; |
| 5334 | |
| 5335 | curbuf->b_p_com = (char_u *)""; |
| 5336 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5337 | compl_startpos.col = compl_col; |
| 5338 | ins_eol('\r'); |
| 5339 | curbuf->b_p_com = old; |
| 5340 | compl_length = 0; |
| 5341 | compl_col = curwin->w_cursor.col; |
| 5342 | } |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5343 | else if (ctrl_x_mode_normal() && in_fuzzy) |
| 5344 | { |
| 5345 | compl_startpos = curwin->w_cursor; |
| 5346 | compl_cont_status &= CONT_S_IPOS; |
| 5347 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5348 | } |
| 5349 | else |
| 5350 | { |
| 5351 | edit_submode_pre = NULL; |
| 5352 | compl_startpos.col = compl_col; |
| 5353 | } |
| 5354 | |
| 5355 | if (compl_cont_status & CONT_LOCAL) |
| 5356 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
| 5357 | else |
| 5358 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 5359 | |
| 5360 | // If any of the original typed text has been changed we need to fix |
| 5361 | // the redo buffer. |
| 5362 | ins_compl_fixRedoBufForLeader(NULL); |
| 5363 | |
| 5364 | // Always add completion for the original text. |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5365 | VIM_CLEAR_STRING(compl_orig_text); |
| 5366 | compl_orig_text.length = (size_t)compl_length; |
| 5367 | 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] | 5368 | if (p_ic) |
| 5369 | flags |= CP_ICASE; |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5370 | if (compl_orig_text.string == NULL || ins_compl_add(compl_orig_text.string, |
| 5371 | (int)compl_orig_text.length, NULL, NULL, NULL, 0, flags, FALSE, NULL) != OK) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5372 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5373 | VIM_CLEAR_STRING(compl_pattern); |
| 5374 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5375 | return FAIL; |
| 5376 | } |
| 5377 | |
| 5378 | // showmode might reset the internal line pointers, so it must |
| 5379 | // be called before line = ml_get(), or when this address is no |
| 5380 | // longer needed. -- Acevedo. |
| 5381 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 5382 | edit_submode_highl = HLF_COUNT; |
| 5383 | showmode(); |
| 5384 | edit_submode_extra = NULL; |
| 5385 | out_flush(); |
| 5386 | |
| 5387 | return OK; |
| 5388 | } |
| 5389 | |
| 5390 | /* |
| 5391 | * display the completion status message |
| 5392 | */ |
| 5393 | static void |
| 5394 | ins_compl_show_statusmsg(void) |
| 5395 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5396 | // 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] | 5397 | if (is_first_match(compl_first_match->cp_next)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5398 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5399 | edit_submode_extra = compl_status_adding() && compl_length > 1 |
Bram Moolenaar | b53a190 | 2022-11-15 13:57:38 +0000 | [diff] [blame] | 5400 | ? (char_u *)_("Hit end of paragraph") |
| 5401 | : (char_u *)_("Pattern not found"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5402 | edit_submode_highl = HLF_E; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5403 | } |
| 5404 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5405 | if (edit_submode_extra == NULL) |
| 5406 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5407 | if (match_at_original_text(compl_curr_match)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5408 | { |
| 5409 | edit_submode_extra = (char_u *)_("Back at original"); |
| 5410 | edit_submode_highl = HLF_W; |
| 5411 | } |
| 5412 | else if (compl_cont_status & CONT_S_IPOS) |
| 5413 | { |
| 5414 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 5415 | edit_submode_highl = HLF_COUNT; |
| 5416 | } |
| 5417 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
| 5418 | { |
| 5419 | edit_submode_extra = (char_u *)_("The only match"); |
| 5420 | edit_submode_highl = HLF_COUNT; |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 5421 | compl_curr_match->cp_number = 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5422 | } |
| 5423 | else |
| 5424 | { |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 5425 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5426 | // Update completion sequence number when needed. |
| 5427 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 5428 | ins_compl_update_sequence_numbers(); |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 5429 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5430 | // The match should always have a sequence number now, this is |
| 5431 | // just a safety check. |
| 5432 | if (compl_curr_match->cp_number != -1) |
| 5433 | { |
| 5434 | // Space for 10 text chars. + 2x10-digit no.s = 31. |
| 5435 | // Translations may need more than twice that. |
| 5436 | static char_u match_ref[81]; |
| 5437 | |
| 5438 | if (compl_matches > 0) |
| 5439 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5440 | _("match %d of %d"), |
| 5441 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5442 | else |
| 5443 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5444 | _("match %d"), |
| 5445 | compl_curr_match->cp_number); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5446 | edit_submode_extra = match_ref; |
| 5447 | edit_submode_highl = HLF_R; |
| 5448 | if (dollar_vcol >= 0) |
| 5449 | curs_columns(FALSE); |
| 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | // Show a message about what (completion) mode we're in. |
| 5455 | if (!compl_opt_suppress_empty) |
| 5456 | { |
| 5457 | showmode(); |
| 5458 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 5459 | { |
| 5460 | if (edit_submode_extra != NULL) |
| 5461 | { |
| 5462 | if (!p_smd) |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 5463 | { |
| 5464 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5465 | msg_attr((char *)edit_submode_extra, |
| 5466 | edit_submode_highl < HLF_COUNT |
| 5467 | ? HL_ATTR(edit_submode_highl) : 0); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 5468 | msg_hist_off = FALSE; |
| 5469 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5470 | } |
| 5471 | else |
| 5472 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 5473 | } |
| 5474 | } |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | /* |
| 5478 | * Do Insert mode completion. |
| 5479 | * Called when character "c" was typed, which has a meaning for completion. |
| 5480 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 5481 | */ |
| 5482 | int |
| 5483 | ins_complete(int c, int enable_pum) |
| 5484 | { |
| 5485 | int n; |
| 5486 | int save_w_wrow; |
| 5487 | int save_w_leftcol; |
| 5488 | int insert_match; |
| 5489 | |
| 5490 | compl_direction = ins_compl_key2dir(c); |
| 5491 | insert_match = ins_compl_use_match(c); |
| 5492 | |
| 5493 | if (!compl_started) |
| 5494 | { |
| 5495 | if (ins_compl_start() == FAIL) |
| 5496 | return FAIL; |
| 5497 | } |
| 5498 | else if (insert_match && stop_arrow() == FAIL) |
| 5499 | return FAIL; |
| 5500 | |
| 5501 | compl_shown_match = compl_curr_match; |
| 5502 | compl_shows_dir = compl_direction; |
| 5503 | |
| 5504 | // Find next match (and following matches). |
| 5505 | save_w_wrow = curwin->w_wrow; |
| 5506 | save_w_leftcol = curwin->w_leftcol; |
| 5507 | n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); |
| 5508 | |
| 5509 | // may undisplay the popup menu |
| 5510 | ins_compl_upd_pum(); |
| 5511 | |
| 5512 | if (n > 1) // all matches have been found |
| 5513 | compl_matches = n; |
| 5514 | compl_curr_match = compl_shown_match; |
| 5515 | compl_direction = compl_shows_dir; |
| 5516 | |
| 5517 | // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 5518 | // mode. |
| 5519 | if (got_int && !global_busy) |
| 5520 | { |
| 5521 | (void)vgetc(); |
| 5522 | got_int = FALSE; |
| 5523 | } |
| 5524 | |
| 5525 | // 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] | 5526 | if (is_first_match(compl_first_match->cp_next)) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5527 | { |
| 5528 | // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 5529 | // because we couldn't expand anything at first place, but if we used |
| 5530 | // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 5531 | // (such as M in M'exico) if not tried already. -- Acevedo |
| 5532 | if (compl_length > 1 |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5533 | || compl_status_adding() |
| 5534 | || (ctrl_x_mode_not_default() |
| 5535 | && !ctrl_x_mode_path_patterns() |
| 5536 | && !ctrl_x_mode_path_defines())) |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 5537 | compl_cont_status &= ~CONT_N_ADDS; |
| 5538 | } |
| 5539 | |
| 5540 | if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) |
| 5541 | compl_cont_status |= CONT_S_IPOS; |
| 5542 | else |
| 5543 | compl_cont_status &= ~CONT_S_IPOS; |
| 5544 | |
| 5545 | ins_compl_show_statusmsg(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5546 | |
| 5547 | // Show the popup menu, unless we got interrupted. |
| 5548 | if (enable_pum && !compl_interrupted) |
| 5549 | show_pum(save_w_wrow, save_w_leftcol); |
| 5550 | |
| 5551 | compl_was_interrupted = compl_interrupted; |
| 5552 | compl_interrupted = FALSE; |
| 5553 | |
| 5554 | return OK; |
| 5555 | } |
| 5556 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 5557 | /* |
| 5558 | * Remove (if needed) and show the popup menu |
| 5559 | */ |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5560 | static void |
| 5561 | show_pum(int prev_w_wrow, int prev_w_leftcol) |
| 5562 | { |
| 5563 | // RedrawingDisabled may be set when invoked through complete(). |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 5564 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5565 | RedrawingDisabled = 0; |
| 5566 | |
| 5567 | // If the cursor moved or the display scrolled we need to remove the pum |
| 5568 | // first. |
| 5569 | setcursor(); |
| 5570 | if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) |
| 5571 | ins_compl_del_pum(); |
| 5572 | |
| 5573 | ins_compl_show_pum(); |
| 5574 | setcursor(); |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 5575 | |
| 5576 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /* |
| 5580 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 5581 | * If dest is not NULL the chars. are copied there quoting (with |
| 5582 | * a backslash) the metachars, and dest would be NUL terminated. |
| 5583 | * Returns the length (needed) of dest |
| 5584 | */ |
| 5585 | static unsigned |
| 5586 | quote_meta(char_u *dest, char_u *src, int len) |
| 5587 | { |
| 5588 | unsigned m = (unsigned)len + 1; // one extra for the NUL |
| 5589 | |
| 5590 | for ( ; --len >= 0; src++) |
| 5591 | { |
| 5592 | switch (*src) |
| 5593 | { |
| 5594 | case '.': |
| 5595 | case '*': |
| 5596 | case '[': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5597 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5598 | break; |
| 5599 | // FALLTHROUGH |
| 5600 | case '~': |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 5601 | if (!magic_isset()) // quote these only if magic is set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5602 | break; |
| 5603 | // FALLTHROUGH |
| 5604 | case '\\': |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 5605 | if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5606 | break; |
| 5607 | // FALLTHROUGH |
| 5608 | case '^': // currently it's not needed. |
| 5609 | case '$': |
| 5610 | m++; |
| 5611 | if (dest != NULL) |
| 5612 | *dest++ = '\\'; |
| 5613 | break; |
| 5614 | } |
| 5615 | if (dest != NULL) |
| 5616 | *dest++ = *src; |
| 5617 | // Copy remaining bytes of a multibyte character. |
| 5618 | if (has_mbyte) |
| 5619 | { |
| 5620 | int i, mb_len; |
| 5621 | |
| 5622 | mb_len = (*mb_ptr2len)(src) - 1; |
| 5623 | if (mb_len > 0 && len >= mb_len) |
| 5624 | for (i = 0; i < mb_len; ++i) |
| 5625 | { |
| 5626 | --len; |
| 5627 | ++src; |
| 5628 | if (dest != NULL) |
| 5629 | *dest++ = *src; |
| 5630 | } |
| 5631 | } |
| 5632 | } |
| 5633 | if (dest != NULL) |
| 5634 | *dest = NUL; |
| 5635 | |
| 5636 | return m; |
| 5637 | } |
| 5638 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 5639 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5640 | void |
| 5641 | free_insexpand_stuff(void) |
| 5642 | { |
John Marriott | 5e6ea92 | 2024-11-23 14:01:57 +0100 | [diff] [blame] | 5643 | VIM_CLEAR_STRING(compl_orig_text); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 5644 | # ifdef FEAT_EVAL |
| 5645 | free_callback(&cfu_cb); |
| 5646 | free_callback(&ofu_cb); |
| 5647 | free_callback(&tsrfu_cb); |
| 5648 | # endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5649 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 5650 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5651 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 5652 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5653 | /* |
| 5654 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 5655 | * spelled word, if there is one. |
| 5656 | */ |
| 5657 | static void |
| 5658 | spell_back_to_badword(void) |
| 5659 | { |
| 5660 | pos_T tpos = curwin->w_cursor; |
| 5661 | |
Christ van Willegen - van Noort | 8e4c4c7 | 2024-05-17 18:49:27 +0200 | [diff] [blame] | 5662 | spell_bad_len = spell_move_to(curwin, BACKWARD, SMT_ALL, TRUE, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5663 | if (curwin->w_cursor.col != tpos.col) |
| 5664 | start_arrow(&tpos); |
| 5665 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 5666 | #endif |