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