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() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 40 | |
| 41 | # define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
| 42 | |
| 43 | // Message for CTRL-X mode, index is ctrl_x_mode. |
| 44 | static char *ctrl_x_msgs[] = |
| 45 | { |
| 46 | N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl. |
| 47 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"), |
| 48 | NULL, // CTRL_X_SCROLL: depends on state |
| 49 | N_(" Whole line completion (^L^N^P)"), |
| 50 | N_(" File name completion (^F^N^P)"), |
| 51 | N_(" Tag completion (^]^N^P)"), |
| 52 | N_(" Path pattern completion (^N^P)"), |
| 53 | N_(" Definition completion (^D^N^P)"), |
| 54 | NULL, // CTRL_X_FINISHED |
| 55 | N_(" Dictionary completion (^K^N^P)"), |
| 56 | N_(" Thesaurus completion (^T^N^P)"), |
| 57 | N_(" Command-line completion (^V^N^P)"), |
| 58 | N_(" User defined completion (^U^N^P)"), |
| 59 | N_(" Omni completion (^O^N^P)"), |
| 60 | N_(" Spelling suggestion (s^N^P)"), |
| 61 | N_(" Keyword Local completion (^N^P)"), |
| 62 | NULL, // CTRL_X_EVAL doesn't use msg. |
| 63 | }; |
| 64 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 65 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 66 | static char *ctrl_x_mode_names[] = { |
| 67 | "keyword", |
| 68 | "ctrl_x", |
| 69 | "unknown", // CTRL_X_SCROLL |
| 70 | "whole_line", |
| 71 | "files", |
| 72 | "tags", |
| 73 | "path_patterns", |
| 74 | "path_defines", |
| 75 | "unknown", // CTRL_X_FINISHED |
| 76 | "dictionary", |
| 77 | "thesaurus", |
| 78 | "cmdline", |
| 79 | "function", |
| 80 | "omni", |
| 81 | "spell", |
| 82 | NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs" |
| 83 | "eval" |
| 84 | }; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 85 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Array indexes used for cp_text[]. |
| 89 | */ |
| 90 | #define CPT_ABBR 0 // "abbr" |
| 91 | #define CPT_MENU 1 // "menu" |
| 92 | #define CPT_KIND 2 // "kind" |
| 93 | #define CPT_INFO 3 // "info" |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 94 | #define CPT_COUNT 4 // Number of entries |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Structure used to store one match for insert completion. |
| 98 | */ |
| 99 | typedef struct compl_S compl_T; |
| 100 | struct compl_S |
| 101 | { |
| 102 | compl_T *cp_next; |
| 103 | compl_T *cp_prev; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 104 | char_u *cp_str; // matched text |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 105 | char_u *(cp_text[CPT_COUNT]); // text for the menu |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 106 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 107 | typval_T cp_user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 108 | #endif |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 109 | char_u *cp_fname; // file containing the match, allocated when |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 110 | // cp_flags has CP_FREE_FNAME |
| 111 | int cp_flags; // CP_ values |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 112 | int cp_number; // sequence number |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 115 | // values for cp_flags |
| 116 | # define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun |
| 117 | # define CP_FREE_FNAME 2 // cp_fname is allocated |
| 118 | # define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status |
| 119 | # define CP_EQUAL 8 // ins_compl_equal() always returns TRUE |
| 120 | # define CP_ICASE 16 // ins_compl_equal() ignores case |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 121 | # define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 122 | |
| 123 | static char e_hitend[] = N_("Hit end of paragraph"); |
| 124 | # ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 125 | static char e_compldel[] = N_("E840: Completion function deleted text"); |
| 126 | # endif |
| 127 | |
| 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(). |
| 134 | */ |
| 135 | static compl_T *compl_first_match = NULL; |
| 136 | static compl_T *compl_curr_match = NULL; |
| 137 | static compl_T *compl_shown_match = NULL; |
| 138 | static compl_T *compl_old_match = NULL; |
| 139 | |
| 140 | // After using a cursor key <Enter> selects a match in the popup menu, |
| 141 | // otherwise it inserts a line break. |
| 142 | static int compl_enter_selects = FALSE; |
| 143 | |
| 144 | // When "compl_leader" is not NULL only matches that start with this string |
| 145 | // are used. |
| 146 | static char_u *compl_leader = NULL; |
| 147 | |
| 148 | static int compl_get_longest = FALSE; // put longest common string |
| 149 | // in compl_leader |
| 150 | |
| 151 | static int compl_no_insert = FALSE; // FALSE: select & insert |
| 152 | // TRUE: noinsert |
| 153 | static int compl_no_select = FALSE; // FALSE: select & insert |
| 154 | // TRUE: noselect |
| 155 | |
| 156 | // Selected one of the matches. When FALSE the match was edited or using the |
| 157 | // longest common string. |
| 158 | static int compl_used_match; |
| 159 | |
| 160 | // didn't finish finding completions. |
| 161 | static int compl_was_interrupted = FALSE; |
| 162 | |
| 163 | // Set when character typed while looking for matches and it means we should |
| 164 | // stop looking for matches. |
| 165 | static int compl_interrupted = FALSE; |
| 166 | |
| 167 | static int compl_restarting = FALSE; // don't insert match |
| 168 | |
| 169 | // When the first completion is done "compl_started" is set. When it's |
| 170 | // FALSE the word to be completed must be located. |
| 171 | static int compl_started = FALSE; |
| 172 | |
| 173 | // Which Ctrl-X mode are we in? |
| 174 | static int ctrl_x_mode = CTRL_X_NORMAL; |
| 175 | |
| 176 | static int compl_matches = 0; |
| 177 | static char_u *compl_pattern = NULL; |
| 178 | static int compl_direction = FORWARD; |
| 179 | static int compl_shows_dir = FORWARD; |
| 180 | static int compl_pending = 0; // > 1 for postponed CTRL-N |
| 181 | static pos_T compl_startpos; |
| 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 | |
| 189 | static int compl_opt_refresh_always = FALSE; |
| 190 | static int compl_opt_suppress_empty = FALSE; |
| 191 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 192 | 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); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 193 | static void ins_compl_longest_match(compl_T *match); |
| 194 | static void ins_compl_del_pum(void); |
| 195 | static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir); |
| 196 | static char_u *find_line_end(char_u *ptr); |
| 197 | static void ins_compl_free(void); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 198 | static int ins_compl_need_restart(void); |
| 199 | static void ins_compl_new_leader(void); |
| 200 | static int ins_compl_len(void); |
| 201 | static void ins_compl_restart(void); |
| 202 | static void ins_compl_set_original_text(char_u *str); |
| 203 | static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg); |
| 204 | # if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 205 | static void ins_compl_add_list(list_T *list); |
| 206 | static void ins_compl_add_dict(dict_T *dict); |
| 207 | # endif |
| 208 | static int ins_compl_key2dir(int c); |
| 209 | static int ins_compl_pum_key(int c); |
| 210 | static int ins_compl_key2count(int c); |
| 211 | static void show_pum(int prev_w_wrow, int prev_w_leftcol); |
| 212 | static unsigned quote_meta(char_u *dest, char_u *str, int len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 213 | |
| 214 | #ifdef FEAT_SPELL |
| 215 | static void spell_back_to_badword(void); |
| 216 | static int spell_bad_len = 0; // length of located bad word |
| 217 | #endif |
| 218 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 219 | /* |
| 220 | * CTRL-X pressed in Insert mode. |
| 221 | */ |
| 222 | void |
| 223 | ins_ctrl_x(void) |
| 224 | { |
| 225 | // CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 226 | // CTRL-V works like CTRL-N |
| 227 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 228 | { |
| 229 | // if the next ^X<> won't ADD nothing, then reset |
| 230 | // compl_cont_status |
| 231 | if (compl_cont_status & CONT_N_ADDS) |
| 232 | compl_cont_status |= CONT_INTRPT; |
| 233 | else |
| 234 | compl_cont_status = 0; |
| 235 | // We're not sure which CTRL-X mode it will be yet |
| 236 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 237 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 238 | edit_submode_pre = NULL; |
| 239 | showmode(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Functions to check the current CTRL-X mode. |
| 245 | */ |
| 246 | int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; } |
| 247 | int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; } |
| 248 | int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; } |
| 249 | int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; } |
| 250 | int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; } |
| 251 | int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; } |
| 252 | int ctrl_x_mode_path_patterns(void) { |
| 253 | return ctrl_x_mode == CTRL_X_PATH_PATTERNS; } |
| 254 | int ctrl_x_mode_path_defines(void) { |
| 255 | return ctrl_x_mode == CTRL_X_PATH_DEFINES; } |
| 256 | int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; } |
| 257 | int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; } |
| 258 | int ctrl_x_mode_cmdline(void) { return ctrl_x_mode == CTRL_X_CMDLINE; } |
| 259 | int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; } |
| 260 | int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; } |
| 261 | int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; } |
| 262 | int ctrl_x_mode_line_or_eval(void) { |
| 263 | return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; } |
| 264 | |
| 265 | /* |
| 266 | * Whether other than default completion has been selected. |
| 267 | */ |
| 268 | int |
| 269 | ctrl_x_mode_not_default(void) |
| 270 | { |
| 271 | return ctrl_x_mode != CTRL_X_NORMAL; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Whether CTRL-X was typed without a following character. |
| 276 | */ |
| 277 | int |
| 278 | ctrl_x_mode_not_defined_yet(void) |
| 279 | { |
| 280 | return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 285 | */ |
| 286 | int |
| 287 | has_compl_option(int dict_opt) |
| 288 | { |
| 289 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 290 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 291 | && !curwin->w_p_spell |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 292 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 293 | ) |
| 294 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 295 | { |
| 296 | ctrl_x_mode = CTRL_X_NORMAL; |
| 297 | edit_submode = NULL; |
| 298 | msg_attr(dict_opt ? _("'dictionary' option is empty") |
| 299 | : _("'thesaurus' option is empty"), |
| 300 | HL_ATTR(HLF_E)); |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 301 | if (emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 302 | { |
| 303 | vim_beep(BO_COMPL); |
| 304 | setcursor(); |
| 305 | out_flush(); |
| 306 | #ifdef FEAT_EVAL |
| 307 | if (!get_vim_var_nr(VV_TESTING)) |
| 308 | #endif |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 309 | ui_delay(2004L, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 310 | } |
| 311 | return FALSE; |
| 312 | } |
| 313 | return TRUE; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 318 | * This depends on the current mode. |
| 319 | */ |
| 320 | int |
| 321 | vim_is_ctrl_x_key(int c) |
| 322 | { |
| 323 | // Always allow ^R - let its results then be checked |
| 324 | if (c == Ctrl_R) |
| 325 | return TRUE; |
| 326 | |
| 327 | // Accept <PageUp> and <PageDown> if the popup menu is visible. |
| 328 | if (ins_compl_pum_key(c)) |
| 329 | return TRUE; |
| 330 | |
| 331 | switch (ctrl_x_mode) |
| 332 | { |
| 333 | case 0: // Not in any CTRL-X mode |
| 334 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 335 | case CTRL_X_NOT_DEFINED_YET: |
| 336 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
| 337 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 338 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 339 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
| 340 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
| 341 | || c == Ctrl_S || c == Ctrl_K || c == 's'); |
| 342 | case CTRL_X_SCROLL: |
| 343 | return (c == Ctrl_Y || c == Ctrl_E); |
| 344 | case CTRL_X_WHOLE_LINE: |
| 345 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 346 | case CTRL_X_FILES: |
| 347 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 348 | case CTRL_X_DICTIONARY: |
| 349 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 350 | case CTRL_X_THESAURUS: |
| 351 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 352 | case CTRL_X_TAGS: |
| 353 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 354 | #ifdef FEAT_FIND_ID |
| 355 | case CTRL_X_PATH_PATTERNS: |
| 356 | return (c == Ctrl_P || c == Ctrl_N); |
| 357 | case CTRL_X_PATH_DEFINES: |
| 358 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 359 | #endif |
| 360 | case CTRL_X_CMDLINE: |
| 361 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 362 | || c == Ctrl_X); |
| 363 | #ifdef FEAT_COMPL_FUNC |
| 364 | case CTRL_X_FUNCTION: |
| 365 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
| 366 | case CTRL_X_OMNI: |
| 367 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
| 368 | #endif |
| 369 | case CTRL_X_SPELL: |
| 370 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
| 371 | case CTRL_X_EVAL: |
| 372 | return (c == Ctrl_P || c == Ctrl_N); |
| 373 | } |
| 374 | internal_error("vim_is_ctrl_x_key()"); |
| 375 | return FALSE; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Return TRUE when character "c" is part of the item currently being |
| 380 | * completed. Used to decide whether to abandon complete mode when the menu |
| 381 | * is visible. |
| 382 | */ |
| 383 | int |
| 384 | ins_compl_accept_char(int c) |
| 385 | { |
| 386 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 387 | // When expanding an identifier only accept identifier chars. |
| 388 | return vim_isIDc(c); |
| 389 | |
| 390 | switch (ctrl_x_mode) |
| 391 | { |
| 392 | case CTRL_X_FILES: |
| 393 | // When expanding file name only accept file name chars. But not |
| 394 | // path separators, so that "proto/<Tab>" expands files in |
| 395 | // "proto", not "proto/" as a whole |
| 396 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 397 | |
| 398 | case CTRL_X_CMDLINE: |
| 399 | case CTRL_X_OMNI: |
| 400 | // Command line and Omni completion can work with just about any |
| 401 | // printable character, but do stop at white space. |
| 402 | return vim_isprintc(c) && !VIM_ISWHITE(c); |
| 403 | |
| 404 | case CTRL_X_WHOLE_LINE: |
| 405 | // For while line completion a space can be part of the line. |
| 406 | return vim_isprintc(c); |
| 407 | } |
| 408 | return vim_iswordc(c); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
| 413 | * case of the originally typed text is used, and the case of the completed |
| 414 | * text is inferred, ie this tries to work out what case you probably wanted |
| 415 | * the rest of the word to be in -- webb |
| 416 | */ |
| 417 | int |
| 418 | ins_compl_add_infercase( |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 419 | char_u *str_arg, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 420 | int len, |
| 421 | int icase, |
| 422 | char_u *fname, |
| 423 | int dir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 424 | int cont_s_ipos) // next ^X<> will set initial_pos |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 425 | { |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 426 | char_u *str = str_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 427 | char_u *p; |
| 428 | int i, c; |
| 429 | int actual_len; // Take multi-byte characters |
| 430 | int actual_compl_length; // into account. |
| 431 | int min_len; |
| 432 | int *wca; // Wide character array. |
| 433 | int has_lower = FALSE; |
| 434 | int was_letter = FALSE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 435 | int flags = 0; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 436 | |
| 437 | if (p_ic && curbuf->b_p_inf && len > 0) |
| 438 | { |
| 439 | // Infer case of completed part. |
| 440 | |
| 441 | // Find actual length of completion. |
| 442 | if (has_mbyte) |
| 443 | { |
| 444 | p = str; |
| 445 | actual_len = 0; |
| 446 | while (*p != NUL) |
| 447 | { |
| 448 | MB_PTR_ADV(p); |
| 449 | ++actual_len; |
| 450 | } |
| 451 | } |
| 452 | else |
| 453 | actual_len = len; |
| 454 | |
| 455 | // Find actual length of original text. |
| 456 | if (has_mbyte) |
| 457 | { |
| 458 | p = compl_orig_text; |
| 459 | actual_compl_length = 0; |
| 460 | while (*p != NUL) |
| 461 | { |
| 462 | MB_PTR_ADV(p); |
| 463 | ++actual_compl_length; |
| 464 | } |
| 465 | } |
| 466 | else |
| 467 | actual_compl_length = compl_length; |
| 468 | |
| 469 | // "actual_len" may be smaller than "actual_compl_length" when using |
| 470 | // thesaurus, only use the minimum when comparing. |
| 471 | min_len = actual_len < actual_compl_length |
| 472 | ? actual_len : actual_compl_length; |
| 473 | |
| 474 | // Allocate wide character array for the completion and fill it. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 475 | wca = ALLOC_MULT(int, actual_len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 476 | if (wca != NULL) |
| 477 | { |
| 478 | p = str; |
| 479 | for (i = 0; i < actual_len; ++i) |
| 480 | if (has_mbyte) |
| 481 | wca[i] = mb_ptr2char_adv(&p); |
| 482 | else |
| 483 | wca[i] = *(p++); |
| 484 | |
| 485 | // Rule 1: Were any chars converted to lower? |
| 486 | p = compl_orig_text; |
| 487 | for (i = 0; i < min_len; ++i) |
| 488 | { |
| 489 | if (has_mbyte) |
| 490 | c = mb_ptr2char_adv(&p); |
| 491 | else |
| 492 | c = *(p++); |
| 493 | if (MB_ISLOWER(c)) |
| 494 | { |
| 495 | has_lower = TRUE; |
| 496 | if (MB_ISUPPER(wca[i])) |
| 497 | { |
| 498 | // Rule 1 is satisfied. |
| 499 | for (i = actual_compl_length; i < actual_len; ++i) |
| 500 | wca[i] = MB_TOLOWER(wca[i]); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Rule 2: No lower case, 2nd consecutive letter converted to |
| 507 | // upper case. |
| 508 | if (!has_lower) |
| 509 | { |
| 510 | p = compl_orig_text; |
| 511 | for (i = 0; i < min_len; ++i) |
| 512 | { |
| 513 | if (has_mbyte) |
| 514 | c = mb_ptr2char_adv(&p); |
| 515 | else |
| 516 | c = *(p++); |
| 517 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 518 | { |
| 519 | // Rule 2 is satisfied. |
| 520 | for (i = actual_compl_length; i < actual_len; ++i) |
| 521 | wca[i] = MB_TOUPPER(wca[i]); |
| 522 | break; |
| 523 | } |
| 524 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Copy the original case of the part we typed. |
| 529 | p = compl_orig_text; |
| 530 | for (i = 0; i < min_len; ++i) |
| 531 | { |
| 532 | if (has_mbyte) |
| 533 | c = mb_ptr2char_adv(&p); |
| 534 | else |
| 535 | c = *(p++); |
| 536 | if (MB_ISLOWER(c)) |
| 537 | wca[i] = MB_TOLOWER(wca[i]); |
| 538 | else if (MB_ISUPPER(c)) |
| 539 | wca[i] = MB_TOUPPER(wca[i]); |
| 540 | } |
| 541 | |
| 542 | // Generate encoding specific output from wide character array. |
| 543 | // Multi-byte characters can occupy up to five bytes more than |
| 544 | // ASCII characters, and we also need one byte for NUL, so stay |
| 545 | // six bytes away from the edge of IObuff. |
| 546 | p = IObuff; |
| 547 | i = 0; |
| 548 | while (i < actual_len && (p - IObuff + 6) < IOSIZE) |
| 549 | if (has_mbyte) |
| 550 | p += (*mb_char2bytes)(wca[i++], p); |
| 551 | else |
| 552 | *(p++) = wca[i++]; |
| 553 | *p = NUL; |
| 554 | |
| 555 | vim_free(wca); |
| 556 | } |
| 557 | |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 558 | str = IObuff; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 559 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 560 | if (cont_s_ipos) |
| 561 | flags |= CP_CONT_S_IPOS; |
| 562 | if (icase) |
| 563 | flags |= CP_ICASE; |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 564 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 565 | return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Add a match to the list of matches. |
| 570 | * If the given string is already in the list of completions, then return |
| 571 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 572 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 573 | */ |
| 574 | static int |
| 575 | ins_compl_add( |
| 576 | char_u *str, |
| 577 | int len, |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 578 | char_u *fname, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 579 | char_u **cptext, // extra text for popup menu or NULL |
| 580 | typval_T *user_data UNUSED, // "user_data" entry or NULL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 581 | int cdir, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 582 | int flags_arg, |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 583 | int adup) // accept duplicate match |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 584 | { |
| 585 | compl_T *match; |
| 586 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 587 | int flags = flags_arg; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 588 | |
Bram Moolenaar | ceb0619 | 2021-04-04 15:05:22 +0200 | [diff] [blame] | 589 | if (flags & CP_FAST) |
| 590 | fast_breakcheck(); |
| 591 | else |
| 592 | ui_breakcheck(); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 593 | if (got_int) |
| 594 | return FAIL; |
| 595 | if (len < 0) |
| 596 | len = (int)STRLEN(str); |
| 597 | |
| 598 | // If the same match is already present, don't add it. |
| 599 | if (compl_first_match != NULL && !adup) |
| 600 | { |
| 601 | match = compl_first_match; |
| 602 | do |
| 603 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 604 | if ( !(match->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 605 | && STRNCMP(match->cp_str, str, len) == 0 |
| 606 | && match->cp_str[len] == NUL) |
| 607 | return NOTDONE; |
| 608 | match = match->cp_next; |
| 609 | } while (match != NULL && match != compl_first_match); |
| 610 | } |
| 611 | |
| 612 | // Remove any popup menu before changing the list of matches. |
| 613 | ins_compl_del_pum(); |
| 614 | |
| 615 | // Allocate a new match structure. |
| 616 | // Copy the values to the new match structure. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 617 | match = ALLOC_CLEAR_ONE(compl_T); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 618 | if (match == NULL) |
| 619 | return FAIL; |
| 620 | match->cp_number = -1; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 621 | if (flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 622 | match->cp_number = 0; |
| 623 | if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
| 624 | { |
| 625 | vim_free(match); |
| 626 | return FAIL; |
| 627 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 628 | |
| 629 | // match-fname is: |
| 630 | // - 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] | 631 | // - 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] | 632 | // - NULL otherwise. --Acevedo |
| 633 | if (fname != NULL |
| 634 | && compl_curr_match != NULL |
| 635 | && compl_curr_match->cp_fname != NULL |
| 636 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 637 | match->cp_fname = compl_curr_match->cp_fname; |
| 638 | else if (fname != NULL) |
| 639 | { |
| 640 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 641 | flags |= CP_FREE_FNAME; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 642 | } |
| 643 | else |
| 644 | match->cp_fname = NULL; |
| 645 | match->cp_flags = flags; |
| 646 | |
| 647 | if (cptext != NULL) |
| 648 | { |
| 649 | int i; |
| 650 | |
| 651 | for (i = 0; i < CPT_COUNT; ++i) |
| 652 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 653 | match->cp_text[i] = vim_strsave(cptext[i]); |
| 654 | } |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 655 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 656 | if (user_data != NULL) |
| 657 | match->cp_user_data = *user_data; |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 658 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 659 | |
| 660 | // Link the new match structure in the list of matches. |
| 661 | if (compl_first_match == NULL) |
| 662 | match->cp_next = match->cp_prev = NULL; |
| 663 | else if (dir == FORWARD) |
| 664 | { |
| 665 | match->cp_next = compl_curr_match->cp_next; |
| 666 | match->cp_prev = compl_curr_match; |
| 667 | } |
| 668 | else // BACKWARD |
| 669 | { |
| 670 | match->cp_next = compl_curr_match; |
| 671 | match->cp_prev = compl_curr_match->cp_prev; |
| 672 | } |
| 673 | if (match->cp_next) |
| 674 | match->cp_next->cp_prev = match; |
| 675 | if (match->cp_prev) |
| 676 | match->cp_prev->cp_next = match; |
| 677 | else // if there's nothing before, it is the first match |
| 678 | compl_first_match = match; |
| 679 | compl_curr_match = match; |
| 680 | |
| 681 | // Find the longest common string if still doing that. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 682 | if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 683 | ins_compl_longest_match(match); |
| 684 | |
| 685 | return OK; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 690 | * match->cp_flags. |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 691 | */ |
| 692 | static int |
| 693 | ins_compl_equal(compl_T *match, char_u *str, int len) |
| 694 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 695 | if (match->cp_flags & CP_EQUAL) |
Bram Moolenaar | 73655cf | 2019-04-06 13:45:55 +0200 | [diff] [blame] | 696 | return TRUE; |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 697 | if (match->cp_flags & CP_ICASE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 698 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 699 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Reduce the longest common string for match "match". |
| 704 | */ |
| 705 | static void |
| 706 | ins_compl_longest_match(compl_T *match) |
| 707 | { |
| 708 | char_u *p, *s; |
| 709 | int c1, c2; |
| 710 | int had_match; |
| 711 | |
| 712 | if (compl_leader == NULL) |
| 713 | { |
| 714 | // First match, use it as a whole. |
| 715 | compl_leader = vim_strsave(match->cp_str); |
| 716 | if (compl_leader != NULL) |
| 717 | { |
| 718 | had_match = (curwin->w_cursor.col > compl_col); |
| 719 | ins_compl_delete(); |
| 720 | ins_bytes(compl_leader + ins_compl_len()); |
| 721 | ins_redraw(FALSE); |
| 722 | |
| 723 | // When the match isn't there (to avoid matching itself) remove it |
| 724 | // again after redrawing. |
| 725 | if (!had_match) |
| 726 | ins_compl_delete(); |
| 727 | compl_used_match = FALSE; |
| 728 | } |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | // Reduce the text if this match differs from compl_leader. |
| 733 | p = compl_leader; |
| 734 | s = match->cp_str; |
| 735 | while (*p != NUL) |
| 736 | { |
| 737 | if (has_mbyte) |
| 738 | { |
| 739 | c1 = mb_ptr2char(p); |
| 740 | c2 = mb_ptr2char(s); |
| 741 | } |
| 742 | else |
| 743 | { |
| 744 | c1 = *p; |
| 745 | c2 = *s; |
| 746 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 747 | if ((match->cp_flags & CP_ICASE) |
| 748 | ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 749 | break; |
| 750 | if (has_mbyte) |
| 751 | { |
| 752 | MB_PTR_ADV(p); |
| 753 | MB_PTR_ADV(s); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | ++p; |
| 758 | ++s; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (*p != NUL) |
| 763 | { |
| 764 | // Leader was shortened, need to change the inserted text. |
| 765 | *p = NUL; |
| 766 | had_match = (curwin->w_cursor.col > compl_col); |
| 767 | ins_compl_delete(); |
| 768 | ins_bytes(compl_leader + ins_compl_len()); |
| 769 | ins_redraw(FALSE); |
| 770 | |
| 771 | // When the match isn't there (to avoid matching itself) remove it |
| 772 | // again after redrawing. |
| 773 | if (!had_match) |
| 774 | ins_compl_delete(); |
| 775 | } |
| 776 | |
| 777 | compl_used_match = FALSE; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Add an array of matches to the list of matches. |
| 783 | * Frees matches[]. |
| 784 | */ |
| 785 | static void |
| 786 | ins_compl_add_matches( |
| 787 | int num_matches, |
| 788 | char_u **matches, |
| 789 | int icase) |
| 790 | { |
| 791 | int i; |
| 792 | int add_r = OK; |
| 793 | int dir = compl_direction; |
| 794 | |
| 795 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 796 | if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir, |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 797 | CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 798 | // if dir was BACKWARD then honor it just once |
| 799 | dir = FORWARD; |
| 800 | FreeWild(num_matches, matches); |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * Make the completion list cyclic. |
| 805 | * Return the number of matches (excluding the original). |
| 806 | */ |
| 807 | static int |
| 808 | ins_compl_make_cyclic(void) |
| 809 | { |
| 810 | compl_T *match; |
| 811 | int count = 0; |
| 812 | |
| 813 | if (compl_first_match != NULL) |
| 814 | { |
| 815 | // Find the end of the list. |
| 816 | match = compl_first_match; |
| 817 | // there's always an entry for the compl_orig_text, it doesn't count. |
| 818 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
| 819 | { |
| 820 | match = match->cp_next; |
| 821 | ++count; |
| 822 | } |
| 823 | match->cp_next = compl_first_match; |
| 824 | compl_first_match->cp_prev = match; |
| 825 | } |
| 826 | return count; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Return whether there currently is a shown match. |
| 831 | */ |
| 832 | int |
| 833 | ins_compl_has_shown_match(void) |
| 834 | { |
| 835 | return compl_shown_match == NULL |
| 836 | || compl_shown_match != compl_shown_match->cp_next; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Return whether the shown match is long enough. |
| 841 | */ |
| 842 | int |
| 843 | ins_compl_long_shown_match(void) |
| 844 | { |
| 845 | return (int)STRLEN(compl_shown_match->cp_str) |
| 846 | > curwin->w_cursor.col - compl_col; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Set variables that store noselect and noinsert behavior from the |
| 851 | * 'completeopt' value. |
| 852 | */ |
| 853 | void |
| 854 | completeopt_was_set(void) |
| 855 | { |
| 856 | compl_no_insert = FALSE; |
| 857 | compl_no_select = FALSE; |
| 858 | if (strstr((char *)p_cot, "noselect") != NULL) |
| 859 | compl_no_select = TRUE; |
| 860 | if (strstr((char *)p_cot, "noinsert") != NULL) |
| 861 | compl_no_insert = TRUE; |
| 862 | } |
| 863 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 864 | |
| 865 | // "compl_match_array" points the currently displayed list of entries in the |
| 866 | // popup menu. It is NULL when there is no popup menu. |
| 867 | static pumitem_T *compl_match_array = NULL; |
| 868 | static int compl_match_arraysize; |
| 869 | |
| 870 | /* |
| 871 | * Update the screen and when there is any scrolling remove the popup menu. |
| 872 | */ |
| 873 | static void |
| 874 | ins_compl_upd_pum(void) |
| 875 | { |
| 876 | int h; |
| 877 | |
| 878 | if (compl_match_array != NULL) |
| 879 | { |
| 880 | h = curwin->w_cline_height; |
| 881 | // Update the screen later, before drawing the popup menu over it. |
| 882 | pum_call_update_screen(); |
| 883 | if (h != curwin->w_cline_height) |
| 884 | ins_compl_del_pum(); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Remove any popup menu. |
| 890 | */ |
| 891 | static void |
| 892 | ins_compl_del_pum(void) |
| 893 | { |
| 894 | if (compl_match_array != NULL) |
| 895 | { |
| 896 | pum_undisplay(); |
| 897 | VIM_CLEAR(compl_match_array); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Return TRUE if the popup menu should be displayed. |
| 903 | */ |
| 904 | int |
| 905 | pum_wanted(void) |
| 906 | { |
| 907 | // 'completeopt' must contain "menu" or "menuone" |
| 908 | if (vim_strchr(p_cot, 'm') == NULL) |
| 909 | return FALSE; |
| 910 | |
| 911 | // The display looks bad on a B&W display. |
| 912 | if (t_colors < 8 |
| 913 | #ifdef FEAT_GUI |
| 914 | && !gui.in_use |
| 915 | #endif |
| 916 | ) |
| 917 | return FALSE; |
| 918 | return TRUE; |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 923 | * One if 'completopt' contains "menuone". |
| 924 | */ |
| 925 | static int |
| 926 | pum_enough_matches(void) |
| 927 | { |
| 928 | compl_T *compl; |
| 929 | int i; |
| 930 | |
| 931 | // Don't display the popup menu if there are no matches or there is only |
| 932 | // one (ignoring the original text). |
| 933 | compl = compl_first_match; |
| 934 | i = 0; |
| 935 | do |
| 936 | { |
| 937 | if (compl == NULL |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 938 | || ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 939 | break; |
| 940 | compl = compl->cp_next; |
| 941 | } while (compl != compl_first_match); |
| 942 | |
| 943 | if (strstr((char *)p_cot, "menuone") != NULL) |
| 944 | return (i >= 1); |
| 945 | return (i >= 2); |
| 946 | } |
| 947 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 948 | #ifdef FEAT_EVAL |
| 949 | /* |
| 950 | * Allocate Dict for the completed item. |
| 951 | * { word, abbr, menu, kind, info } |
| 952 | */ |
| 953 | static dict_T * |
| 954 | ins_compl_dict_alloc(compl_T *match) |
| 955 | { |
| 956 | dict_T *dict = dict_alloc_lock(VAR_FIXED); |
| 957 | |
| 958 | if (dict != NULL) |
| 959 | { |
| 960 | dict_add_string(dict, "word", match->cp_str); |
| 961 | dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]); |
| 962 | dict_add_string(dict, "menu", match->cp_text[CPT_MENU]); |
| 963 | dict_add_string(dict, "kind", match->cp_text[CPT_KIND]); |
| 964 | dict_add_string(dict, "info", match->cp_text[CPT_INFO]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 965 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 966 | dict_add_string(dict, "user_data", (char_u *)""); |
| 967 | else |
| 968 | dict_add_tv(dict, "user_data", &match->cp_user_data); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 969 | } |
| 970 | return dict; |
| 971 | } |
| 972 | |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 973 | static void |
| 974 | trigger_complete_changed_event(int cur) |
| 975 | { |
| 976 | dict_T *v_event; |
| 977 | dict_T *item; |
| 978 | static int recursive = FALSE; |
| 979 | |
| 980 | if (recursive) |
| 981 | return; |
| 982 | |
| 983 | v_event = get_vim_var_dict(VV_EVENT); |
| 984 | if (cur < 0) |
| 985 | item = dict_alloc(); |
| 986 | else |
| 987 | item = ins_compl_dict_alloc(compl_curr_match); |
| 988 | if (item == NULL) |
| 989 | return; |
| 990 | dict_add_dict(v_event, "completed_item", item); |
| 991 | pum_set_event_info(v_event); |
| 992 | dict_set_items_ro(v_event); |
| 993 | |
| 994 | recursive = TRUE; |
Bram Moolenaar | 6adb9ea | 2020-04-30 22:31:18 +0200 | [diff] [blame] | 995 | textwinlock++; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 996 | apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 6adb9ea | 2020-04-30 22:31:18 +0200 | [diff] [blame] | 997 | textwinlock--; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 998 | recursive = FALSE; |
| 999 | |
| 1000 | dict_free_contents(v_event); |
| 1001 | hash_init(&v_event->dv_hashtab); |
| 1002 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1003 | #endif |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1004 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1005 | /* |
| 1006 | * Show the popup menu for the list of matches. |
| 1007 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
| 1008 | */ |
| 1009 | void |
| 1010 | ins_compl_show_pum(void) |
| 1011 | { |
| 1012 | compl_T *compl; |
| 1013 | compl_T *shown_compl = NULL; |
| 1014 | int did_find_shown_match = FALSE; |
| 1015 | int shown_match_ok = FALSE; |
| 1016 | int i; |
| 1017 | int cur = -1; |
| 1018 | colnr_T col; |
| 1019 | int lead_len = 0; |
| 1020 | |
| 1021 | if (!pum_wanted() || !pum_enough_matches()) |
| 1022 | return; |
| 1023 | |
| 1024 | #if defined(FEAT_EVAL) |
| 1025 | // Dirty hard-coded hack: remove any matchparen highlighting. |
Bram Moolenaar | 179eb56 | 2020-12-27 18:03:22 +0100 | [diff] [blame] | 1026 | do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif"); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1027 | #endif |
| 1028 | |
| 1029 | // Update the screen later, before drawing the popup menu over it. |
| 1030 | pum_call_update_screen(); |
| 1031 | |
| 1032 | if (compl_match_array == NULL) |
| 1033 | { |
| 1034 | // Need to build the popup menu list. |
| 1035 | compl_match_arraysize = 0; |
| 1036 | compl = compl_first_match; |
| 1037 | if (compl_leader != NULL) |
| 1038 | lead_len = (int)STRLEN(compl_leader); |
| 1039 | do |
| 1040 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1041 | if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1042 | && (compl_leader == NULL |
| 1043 | || ins_compl_equal(compl, compl_leader, lead_len))) |
| 1044 | ++compl_match_arraysize; |
| 1045 | compl = compl->cp_next; |
| 1046 | } while (compl != NULL && compl != compl_first_match); |
| 1047 | if (compl_match_arraysize == 0) |
| 1048 | return; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1049 | compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1050 | if (compl_match_array != NULL) |
| 1051 | { |
| 1052 | // If the current match is the original text don't find the first |
| 1053 | // match after it, don't highlight anything. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1054 | if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1055 | shown_match_ok = TRUE; |
| 1056 | |
| 1057 | i = 0; |
| 1058 | compl = compl_first_match; |
| 1059 | do |
| 1060 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1061 | if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1062 | && (compl_leader == NULL |
| 1063 | || ins_compl_equal(compl, compl_leader, lead_len))) |
| 1064 | { |
| 1065 | if (!shown_match_ok) |
| 1066 | { |
| 1067 | if (compl == compl_shown_match || did_find_shown_match) |
| 1068 | { |
| 1069 | // This item is the shown match or this is the |
| 1070 | // first displayed item after the shown match. |
| 1071 | compl_shown_match = compl; |
| 1072 | did_find_shown_match = TRUE; |
| 1073 | shown_match_ok = TRUE; |
| 1074 | } |
| 1075 | else |
| 1076 | // Remember this displayed match for when the |
| 1077 | // shown match is just below it. |
| 1078 | shown_compl = compl; |
| 1079 | cur = i; |
| 1080 | } |
| 1081 | |
| 1082 | if (compl->cp_text[CPT_ABBR] != NULL) |
| 1083 | compl_match_array[i].pum_text = |
| 1084 | compl->cp_text[CPT_ABBR]; |
| 1085 | else |
| 1086 | compl_match_array[i].pum_text = compl->cp_str; |
| 1087 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 1088 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 1089 | if (compl->cp_text[CPT_MENU] != NULL) |
| 1090 | compl_match_array[i++].pum_extra = |
| 1091 | compl->cp_text[CPT_MENU]; |
| 1092 | else |
| 1093 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 1094 | } |
| 1095 | |
| 1096 | if (compl == compl_shown_match) |
| 1097 | { |
| 1098 | did_find_shown_match = TRUE; |
| 1099 | |
| 1100 | // When the original text is the shown match don't set |
| 1101 | // compl_shown_match. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1102 | if (compl->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1103 | shown_match_ok = TRUE; |
| 1104 | |
| 1105 | if (!shown_match_ok && shown_compl != NULL) |
| 1106 | { |
| 1107 | // The shown match isn't displayed, set it to the |
| 1108 | // previously displayed match. |
| 1109 | compl_shown_match = shown_compl; |
| 1110 | shown_match_ok = TRUE; |
| 1111 | } |
| 1112 | } |
| 1113 | compl = compl->cp_next; |
| 1114 | } while (compl != NULL && compl != compl_first_match); |
| 1115 | |
| 1116 | if (!shown_match_ok) // no displayed match at all |
| 1117 | cur = -1; |
| 1118 | } |
| 1119 | } |
| 1120 | else |
| 1121 | { |
| 1122 | // popup menu already exists, only need to find the current item. |
| 1123 | for (i = 0; i < compl_match_arraysize; ++i) |
| 1124 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
| 1125 | || compl_match_array[i].pum_text |
| 1126 | == compl_shown_match->cp_text[CPT_ABBR]) |
| 1127 | { |
| 1128 | cur = i; |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (compl_match_array != NULL) |
| 1134 | { |
| 1135 | // In Replace mode when a $ is displayed at the end of the line only |
| 1136 | // part of the screen would be updated. We do need to redraw here. |
| 1137 | dollar_vcol = -1; |
| 1138 | |
| 1139 | // Compute the screen column of the start of the completed text. |
| 1140 | // Use the cursor to get all wrapping and other settings right. |
| 1141 | col = curwin->w_cursor.col; |
| 1142 | curwin->w_cursor.col = compl_col; |
| 1143 | pum_display(compl_match_array, compl_match_arraysize, cur); |
| 1144 | curwin->w_cursor.col = col; |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1145 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1146 | #ifdef FEAT_EVAL |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 1147 | if (has_completechanged()) |
| 1148 | trigger_complete_changed_event(cur); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1149 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | #define DICT_FIRST (1) // use just first element in "dict" |
| 1154 | #define DICT_EXACT (2) // "dict" is the exact name of a file |
| 1155 | |
| 1156 | /* |
| 1157 | * Add any identifiers that match the given pattern in the list of dictionary |
| 1158 | * files "dict_start" to the list of completions. |
| 1159 | */ |
| 1160 | static void |
| 1161 | ins_compl_dictionaries( |
| 1162 | char_u *dict_start, |
| 1163 | char_u *pat, |
| 1164 | int flags, // DICT_FIRST and/or DICT_EXACT |
| 1165 | int thesaurus) // Thesaurus completion |
| 1166 | { |
| 1167 | char_u *dict = dict_start; |
| 1168 | char_u *ptr; |
| 1169 | char_u *buf; |
| 1170 | regmatch_T regmatch; |
| 1171 | char_u **files; |
| 1172 | int count; |
| 1173 | int save_p_scs; |
| 1174 | int dir = compl_direction; |
| 1175 | |
| 1176 | if (*dict == NUL) |
| 1177 | { |
| 1178 | #ifdef FEAT_SPELL |
| 1179 | // When 'dictionary' is empty and spell checking is enabled use |
| 1180 | // "spell". |
| 1181 | if (!thesaurus && curwin->w_p_spell) |
| 1182 | dict = (char_u *)"spell"; |
| 1183 | else |
| 1184 | #endif |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | buf = alloc(LSIZE); |
| 1189 | if (buf == NULL) |
| 1190 | return; |
| 1191 | regmatch.regprog = NULL; // so that we can goto theend |
| 1192 | |
| 1193 | // If 'infercase' is set, don't use 'smartcase' here |
| 1194 | save_p_scs = p_scs; |
| 1195 | if (curbuf->b_p_inf) |
| 1196 | p_scs = FALSE; |
| 1197 | |
| 1198 | // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 1199 | // to only match at the start of a line. Otherwise just match the |
| 1200 | // pattern. Also need to double backslashes. |
| 1201 | if (ctrl_x_mode_line_or_eval()) |
| 1202 | { |
| 1203 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
| 1204 | size_t len; |
| 1205 | |
| 1206 | if (pat_esc == NULL) |
| 1207 | goto theend; |
| 1208 | len = STRLEN(pat_esc) + 10; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1209 | ptr = alloc(len); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1210 | if (ptr == NULL) |
| 1211 | { |
| 1212 | vim_free(pat_esc); |
| 1213 | goto theend; |
| 1214 | } |
| 1215 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
| 1216 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 1217 | vim_free(pat_esc); |
| 1218 | vim_free(ptr); |
| 1219 | } |
| 1220 | else |
| 1221 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1222 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1223 | if (regmatch.regprog == NULL) |
| 1224 | goto theend; |
| 1225 | } |
| 1226 | |
| 1227 | // ignore case depends on 'ignorecase', 'smartcase' and "pat" |
| 1228 | regmatch.rm_ic = ignorecase(pat); |
| 1229 | while (*dict != NUL && !got_int && !compl_interrupted) |
| 1230 | { |
| 1231 | // copy one dictionary file name into buf |
| 1232 | if (flags == DICT_EXACT) |
| 1233 | { |
| 1234 | count = 1; |
| 1235 | files = &dict; |
| 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | // Expand wildcards in the dictionary name, but do not allow |
| 1240 | // backticks (for security, the 'dict' option may have been set in |
| 1241 | // a modeline). |
| 1242 | copy_option_part(&dict, buf, LSIZE, ","); |
| 1243 | # ifdef FEAT_SPELL |
| 1244 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 1245 | count = -1; |
| 1246 | else |
| 1247 | # endif |
| 1248 | if (vim_strchr(buf, '`') != NULL |
| 1249 | || expand_wildcards(1, &buf, &count, &files, |
| 1250 | EW_FILE|EW_SILENT) != OK) |
| 1251 | count = 0; |
| 1252 | } |
| 1253 | |
| 1254 | # ifdef FEAT_SPELL |
| 1255 | if (count == -1) |
| 1256 | { |
| 1257 | // Complete from active spelling. Skip "\<" in the pattern, we |
| 1258 | // don't use it as a RE. |
| 1259 | if (pat[0] == '\\' && pat[1] == '<') |
| 1260 | ptr = pat + 2; |
| 1261 | else |
| 1262 | ptr = pat; |
| 1263 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
| 1264 | } |
| 1265 | else |
| 1266 | # endif |
| 1267 | if (count > 0) // avoid warning for using "files" uninit |
| 1268 | { |
| 1269 | ins_compl_files(count, files, thesaurus, flags, |
| 1270 | ®match, buf, &dir); |
| 1271 | if (flags != DICT_EXACT) |
| 1272 | FreeWild(count, files); |
| 1273 | } |
| 1274 | if (flags != 0) |
| 1275 | break; |
| 1276 | } |
| 1277 | |
| 1278 | theend: |
| 1279 | p_scs = save_p_scs; |
| 1280 | vim_regfree(regmatch.regprog); |
| 1281 | vim_free(buf); |
| 1282 | } |
| 1283 | |
| 1284 | static void |
| 1285 | ins_compl_files( |
| 1286 | int count, |
| 1287 | char_u **files, |
| 1288 | int thesaurus, |
| 1289 | int flags, |
| 1290 | regmatch_T *regmatch, |
| 1291 | char_u *buf, |
| 1292 | int *dir) |
| 1293 | { |
| 1294 | char_u *ptr; |
| 1295 | int i; |
| 1296 | FILE *fp; |
| 1297 | int add_r; |
| 1298 | |
| 1299 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 1300 | { |
| 1301 | fp = mch_fopen((char *)files[i], "r"); // open dictionary file |
| 1302 | if (flags != DICT_EXACT) |
| 1303 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1304 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1305 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1306 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 1307 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 1308 | } |
| 1309 | |
| 1310 | if (fp != NULL) |
| 1311 | { |
| 1312 | // Read dictionary file line by line. |
| 1313 | // Check each line for a match. |
| 1314 | while (!got_int && !compl_interrupted |
| 1315 | && !vim_fgets(buf, LSIZE, fp)) |
| 1316 | { |
| 1317 | ptr = buf; |
| 1318 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
| 1319 | { |
| 1320 | ptr = regmatch->startp[0]; |
| 1321 | if (ctrl_x_mode_line_or_eval()) |
| 1322 | ptr = find_line_end(ptr); |
| 1323 | else |
| 1324 | ptr = find_word_end(ptr); |
| 1325 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 1326 | (int)(ptr - regmatch->startp[0]), |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1327 | p_ic, files[i], *dir, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1328 | if (thesaurus) |
| 1329 | { |
| 1330 | char_u *wstart; |
| 1331 | |
| 1332 | // Add the other matches on the line |
| 1333 | ptr = buf; |
| 1334 | while (!got_int) |
| 1335 | { |
| 1336 | // Find start of the next word. Skip white |
| 1337 | // space and punctuation. |
| 1338 | ptr = find_word_start(ptr); |
| 1339 | if (*ptr == NUL || *ptr == NL) |
| 1340 | break; |
| 1341 | wstart = ptr; |
| 1342 | |
| 1343 | // Find end of the word. |
| 1344 | if (has_mbyte) |
| 1345 | // Japanese words may have characters in |
| 1346 | // different classes, only separate words |
| 1347 | // with single-byte non-word characters. |
| 1348 | while (*ptr != NUL) |
| 1349 | { |
| 1350 | int l = (*mb_ptr2len)(ptr); |
| 1351 | |
| 1352 | if (l < 2 && !vim_iswordc(*ptr)) |
| 1353 | break; |
| 1354 | ptr += l; |
| 1355 | } |
| 1356 | else |
| 1357 | ptr = find_word_end(ptr); |
| 1358 | |
| 1359 | // Add the word. Skip the regexp match. |
| 1360 | if (wstart != regmatch->startp[0]) |
| 1361 | add_r = ins_compl_add_infercase(wstart, |
| 1362 | (int)(ptr - wstart), |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1363 | p_ic, files[i], *dir, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | if (add_r == OK) |
| 1367 | // if dir was BACKWARD then honor it just once |
| 1368 | *dir = FORWARD; |
| 1369 | else if (add_r == FAIL) |
| 1370 | break; |
| 1371 | // avoid expensive call to vim_regexec() when at end |
| 1372 | // of line |
| 1373 | if (*ptr == '\n' || got_int) |
| 1374 | break; |
| 1375 | } |
| 1376 | line_breakcheck(); |
| 1377 | ins_compl_check_keys(50, FALSE); |
| 1378 | } |
| 1379 | fclose(fp); |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Find the start of the next word. |
| 1386 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 1387 | */ |
| 1388 | char_u * |
| 1389 | find_word_start(char_u *ptr) |
| 1390 | { |
| 1391 | if (has_mbyte) |
| 1392 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
| 1393 | ptr += (*mb_ptr2len)(ptr); |
| 1394 | else |
| 1395 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 1396 | ++ptr; |
| 1397 | return ptr; |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Find the end of the word. Assumes it starts inside a word. |
| 1402 | * Returns a pointer to just after the word. |
| 1403 | */ |
| 1404 | char_u * |
| 1405 | find_word_end(char_u *ptr) |
| 1406 | { |
| 1407 | int start_class; |
| 1408 | |
| 1409 | if (has_mbyte) |
| 1410 | { |
| 1411 | start_class = mb_get_class(ptr); |
| 1412 | if (start_class > 1) |
| 1413 | while (*ptr != NUL) |
| 1414 | { |
| 1415 | ptr += (*mb_ptr2len)(ptr); |
| 1416 | if (mb_get_class(ptr) != start_class) |
| 1417 | break; |
| 1418 | } |
| 1419 | } |
| 1420 | else |
| 1421 | while (vim_iswordc(*ptr)) |
| 1422 | ++ptr; |
| 1423 | return ptr; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Find the end of the line, omitting CR and NL at the end. |
| 1428 | * Returns a pointer to just after the line. |
| 1429 | */ |
| 1430 | static char_u * |
| 1431 | find_line_end(char_u *ptr) |
| 1432 | { |
| 1433 | char_u *s; |
| 1434 | |
| 1435 | s = ptr + STRLEN(ptr); |
| 1436 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 1437 | --s; |
| 1438 | return s; |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * Free the list of completions |
| 1443 | */ |
| 1444 | static void |
| 1445 | ins_compl_free(void) |
| 1446 | { |
| 1447 | compl_T *match; |
| 1448 | int i; |
| 1449 | |
| 1450 | VIM_CLEAR(compl_pattern); |
| 1451 | VIM_CLEAR(compl_leader); |
| 1452 | |
| 1453 | if (compl_first_match == NULL) |
| 1454 | return; |
| 1455 | |
| 1456 | ins_compl_del_pum(); |
| 1457 | pum_clear(); |
| 1458 | |
| 1459 | compl_curr_match = compl_first_match; |
| 1460 | do |
| 1461 | { |
| 1462 | match = compl_curr_match; |
| 1463 | compl_curr_match = compl_curr_match->cp_next; |
| 1464 | vim_free(match->cp_str); |
| 1465 | // several entries may use the same fname, free it just once. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1466 | if (match->cp_flags & CP_FREE_FNAME) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1467 | vim_free(match->cp_fname); |
| 1468 | for (i = 0; i < CPT_COUNT; ++i) |
| 1469 | vim_free(match->cp_text[i]); |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1470 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 1471 | clear_tv(&match->cp_user_data); |
Bram Moolenaar | ab782c5 | 2020-01-04 19:00:11 +0100 | [diff] [blame] | 1472 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1473 | vim_free(match); |
| 1474 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 1475 | compl_first_match = compl_curr_match = NULL; |
| 1476 | compl_shown_match = NULL; |
| 1477 | compl_old_match = NULL; |
| 1478 | } |
| 1479 | |
| 1480 | void |
| 1481 | ins_compl_clear(void) |
| 1482 | { |
| 1483 | compl_cont_status = 0; |
| 1484 | compl_started = FALSE; |
| 1485 | compl_matches = 0; |
| 1486 | VIM_CLEAR(compl_pattern); |
| 1487 | VIM_CLEAR(compl_leader); |
| 1488 | edit_submode_extra = NULL; |
| 1489 | VIM_CLEAR(compl_orig_text); |
| 1490 | compl_enter_selects = FALSE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1491 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1492 | // clear v:completed_item |
| 1493 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1494 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Return TRUE when Insert completion is active. |
| 1499 | */ |
| 1500 | int |
| 1501 | ins_compl_active(void) |
| 1502 | { |
| 1503 | return compl_started; |
| 1504 | } |
| 1505 | |
| 1506 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1507 | * Selected one of the matches. When FALSE the match was edited or using the |
| 1508 | * longest common string. |
| 1509 | */ |
| 1510 | int |
| 1511 | ins_compl_used_match(void) |
| 1512 | { |
| 1513 | return compl_used_match; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * Initialize get longest common string. |
| 1518 | */ |
| 1519 | void |
| 1520 | ins_compl_init_get_longest(void) |
| 1521 | { |
| 1522 | compl_get_longest = FALSE; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * Returns TRUE when insert completion is interrupted. |
| 1527 | */ |
| 1528 | int |
| 1529 | ins_compl_interrupted(void) |
| 1530 | { |
| 1531 | return compl_interrupted; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * Returns TRUE if the <Enter> key selects a match in the completion popup |
| 1536 | * menu. |
| 1537 | */ |
| 1538 | int |
| 1539 | ins_compl_enter_selects(void) |
| 1540 | { |
| 1541 | return compl_enter_selects; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * Return the column where the text starts that is being completed |
| 1546 | */ |
| 1547 | colnr_T |
| 1548 | ins_compl_col(void) |
| 1549 | { |
| 1550 | return compl_col; |
| 1551 | } |
| 1552 | |
| 1553 | /* |
| 1554 | * Delete one character before the cursor and show the subset of the matches |
| 1555 | * that match the word that is now before the cursor. |
| 1556 | * Returns the character to be used, NUL if the work is done and another char |
| 1557 | * to be got from the user. |
| 1558 | */ |
| 1559 | int |
| 1560 | ins_compl_bs(void) |
| 1561 | { |
| 1562 | char_u *line; |
| 1563 | char_u *p; |
| 1564 | |
| 1565 | line = ml_get_curline(); |
| 1566 | p = line + curwin->w_cursor.col; |
| 1567 | MB_PTR_BACK(line, p); |
| 1568 | |
| 1569 | // Stop completion when the whole word was deleted. For Omni completion |
| 1570 | // allow the word to be deleted, we won't match everything. |
| 1571 | // Respect the 'backspace' option. |
| 1572 | if ((int)(p - line) - (int)compl_col < 0 |
| 1573 | || ((int)(p - line) - (int)compl_col == 0 |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 1574 | && ctrl_x_mode != CTRL_X_OMNI) |
| 1575 | || ctrl_x_mode == CTRL_X_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1576 | || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col |
| 1577 | - compl_length < 0)) |
| 1578 | return K_BS; |
| 1579 | |
| 1580 | // Deleted more than what was used to find matches or didn't finish |
| 1581 | // finding all matches: need to look for matches all over again. |
| 1582 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 1583 | || ins_compl_need_restart()) |
| 1584 | ins_compl_restart(); |
| 1585 | |
| 1586 | vim_free(compl_leader); |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1587 | compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1588 | if (compl_leader != NULL) |
| 1589 | { |
| 1590 | ins_compl_new_leader(); |
| 1591 | if (compl_shown_match != NULL) |
| 1592 | // Make sure current match is not a hidden item. |
| 1593 | compl_curr_match = compl_shown_match; |
| 1594 | return NUL; |
| 1595 | } |
| 1596 | return K_BS; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 1601 | * be called. |
| 1602 | */ |
| 1603 | static int |
| 1604 | ins_compl_need_restart(void) |
| 1605 | { |
| 1606 | // Return TRUE if we didn't complete finding matches or when the |
| 1607 | // 'completefunc' returned "always" in the "refresh" dictionary item. |
| 1608 | return compl_was_interrupted |
| 1609 | || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
| 1610 | && compl_opt_refresh_always); |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * Called after changing "compl_leader". |
| 1615 | * Show the popup menu with a different set of matches. |
| 1616 | * May also search for matches again if the previous search was interrupted. |
| 1617 | */ |
| 1618 | static void |
| 1619 | ins_compl_new_leader(void) |
| 1620 | { |
| 1621 | ins_compl_del_pum(); |
| 1622 | ins_compl_delete(); |
| 1623 | ins_bytes(compl_leader + ins_compl_len()); |
| 1624 | compl_used_match = FALSE; |
| 1625 | |
| 1626 | if (compl_started) |
| 1627 | ins_compl_set_original_text(compl_leader); |
| 1628 | else |
| 1629 | { |
| 1630 | #ifdef FEAT_SPELL |
| 1631 | spell_bad_len = 0; // need to redetect bad word |
| 1632 | #endif |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1633 | // Matches were cleared, need to search for them now. Before drawing |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1634 | // the popup menu display the changed text before the cursor. Set |
| 1635 | // "compl_restarting" to avoid that the first match is inserted. |
| 1636 | pum_call_update_screen(); |
| 1637 | #ifdef FEAT_GUI |
| 1638 | if (gui.in_use) |
| 1639 | { |
| 1640 | // Show the cursor after the match, not after the redrawn text. |
| 1641 | setcursor(); |
| 1642 | out_flush_cursor(FALSE, FALSE); |
| 1643 | } |
| 1644 | #endif |
| 1645 | compl_restarting = TRUE; |
| 1646 | if (ins_complete(Ctrl_N, TRUE) == FAIL) |
| 1647 | compl_cont_status = 0; |
| 1648 | compl_restarting = FALSE; |
| 1649 | } |
| 1650 | |
| 1651 | compl_enter_selects = !compl_used_match; |
| 1652 | |
| 1653 | // Show the popup menu with a different set of matches. |
| 1654 | ins_compl_show_pum(); |
| 1655 | |
| 1656 | // Don't let Enter select the original text when there is no popup menu. |
| 1657 | if (compl_match_array == NULL) |
| 1658 | compl_enter_selects = FALSE; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * Return the length of the completion, from the completion start column to |
| 1663 | * the cursor column. Making sure it never goes below zero. |
| 1664 | */ |
| 1665 | static int |
| 1666 | ins_compl_len(void) |
| 1667 | { |
| 1668 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
| 1669 | |
| 1670 | if (off < 0) |
| 1671 | return 0; |
| 1672 | return off; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * Append one character to the match leader. May reduce the number of |
| 1677 | * matches. |
| 1678 | */ |
| 1679 | void |
| 1680 | ins_compl_addleader(int c) |
| 1681 | { |
| 1682 | int cc; |
| 1683 | |
| 1684 | if (stop_arrow() == FAIL) |
| 1685 | return; |
| 1686 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 1687 | { |
| 1688 | char_u buf[MB_MAXBYTES + 1]; |
| 1689 | |
| 1690 | (*mb_char2bytes)(c, buf); |
| 1691 | buf[cc] = NUL; |
| 1692 | ins_char_bytes(buf, cc); |
| 1693 | if (compl_opt_refresh_always) |
| 1694 | AppendToRedobuff(buf); |
| 1695 | } |
| 1696 | else |
| 1697 | { |
| 1698 | ins_char(c); |
| 1699 | if (compl_opt_refresh_always) |
| 1700 | AppendCharToRedobuff(c); |
| 1701 | } |
| 1702 | |
| 1703 | // If we didn't complete finding matches we must search again. |
| 1704 | if (ins_compl_need_restart()) |
| 1705 | ins_compl_restart(); |
| 1706 | |
| 1707 | // When 'always' is set, don't reset compl_leader. While completing, |
| 1708 | // cursor doesn't point original position, changing compl_leader would |
| 1709 | // break redo. |
| 1710 | if (!compl_opt_refresh_always) |
| 1711 | { |
| 1712 | vim_free(compl_leader); |
| 1713 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1714 | curwin->w_cursor.col - compl_col); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1715 | if (compl_leader != NULL) |
| 1716 | ins_compl_new_leader(); |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 1722 | * BS or a key was typed while still searching for matches. |
| 1723 | */ |
| 1724 | static void |
| 1725 | ins_compl_restart(void) |
| 1726 | { |
| 1727 | ins_compl_free(); |
| 1728 | compl_started = FALSE; |
| 1729 | compl_matches = 0; |
| 1730 | compl_cont_status = 0; |
| 1731 | compl_cont_mode = 0; |
| 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * Set the first match, the original text. |
| 1736 | */ |
| 1737 | static void |
| 1738 | ins_compl_set_original_text(char_u *str) |
| 1739 | { |
| 1740 | char_u *p; |
| 1741 | |
| 1742 | // Replace the original text entry. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1743 | // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1744 | // at the last item for backward completion |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1745 | if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) // safety check |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1746 | { |
| 1747 | p = vim_strsave(str); |
| 1748 | if (p != NULL) |
| 1749 | { |
| 1750 | vim_free(compl_first_match->cp_str); |
| 1751 | compl_first_match->cp_str = p; |
| 1752 | } |
| 1753 | } |
| 1754 | else if (compl_first_match->cp_prev != NULL |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1755 | && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT)) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1756 | { |
| 1757 | p = vim_strsave(str); |
| 1758 | if (p != NULL) |
| 1759 | { |
| 1760 | vim_free(compl_first_match->cp_prev->cp_str); |
| 1761 | compl_first_match->cp_prev->cp_str = p; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * Append one character to the match leader. May reduce the number of |
| 1768 | * matches. |
| 1769 | */ |
| 1770 | void |
| 1771 | ins_compl_addfrommatch(void) |
| 1772 | { |
| 1773 | char_u *p; |
| 1774 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
| 1775 | int c; |
| 1776 | compl_T *cp; |
| 1777 | |
| 1778 | p = compl_shown_match->cp_str; |
| 1779 | if ((int)STRLEN(p) <= len) // the match is too short |
| 1780 | { |
| 1781 | // When still at the original match use the first entry that matches |
| 1782 | // the leader. |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 1783 | if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1784 | { |
| 1785 | p = NULL; |
| 1786 | for (cp = compl_shown_match->cp_next; cp != NULL |
| 1787 | && cp != compl_first_match; cp = cp->cp_next) |
| 1788 | { |
| 1789 | if (compl_leader == NULL |
| 1790 | || ins_compl_equal(cp, compl_leader, |
| 1791 | (int)STRLEN(compl_leader))) |
| 1792 | { |
| 1793 | p = cp->cp_str; |
| 1794 | break; |
| 1795 | } |
| 1796 | } |
| 1797 | if (p == NULL || (int)STRLEN(p) <= len) |
| 1798 | return; |
| 1799 | } |
| 1800 | else |
| 1801 | return; |
| 1802 | } |
| 1803 | p += len; |
| 1804 | c = PTR2CHAR(p); |
| 1805 | ins_compl_addleader(c); |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * Prepare for Insert mode completion, or stop it. |
| 1810 | * Called just after typing a character in Insert mode. |
| 1811 | * Returns TRUE when the character is not to be inserted; |
| 1812 | */ |
| 1813 | int |
| 1814 | ins_compl_prep(int c) |
| 1815 | { |
| 1816 | char_u *ptr; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1817 | #ifdef FEAT_CINDENT |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1818 | int want_cindent; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 1819 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1820 | int retval = FALSE; |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 1821 | int prev_mode = ctrl_x_mode; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1822 | |
| 1823 | // Forget any previous 'special' messages if this is actually |
| 1824 | // a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 1825 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 1826 | edit_submode_extra = NULL; |
| 1827 | |
| 1828 | // Ignore end of Select mode mapping and mouse scroll buttons. |
| 1829 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
Bram Moolenaar | 957cf67 | 2020-11-12 14:21:06 +0100 | [diff] [blame] | 1830 | || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1831 | return retval; |
| 1832 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1833 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | f0bc15c | 2019-08-18 19:23:45 +0200 | [diff] [blame] | 1834 | // Ignore mouse events in a popup window |
| 1835 | if (is_mouse_key(c)) |
| 1836 | { |
| 1837 | // Ignore drag and release events, the position does not need to be in |
| 1838 | // the popup and it may have just closed. |
| 1839 | if (c == K_LEFTRELEASE |
| 1840 | || c == K_LEFTRELEASE_NM |
| 1841 | || c == K_MIDDLERELEASE |
| 1842 | || c == K_RIGHTRELEASE |
| 1843 | || c == K_X1RELEASE |
| 1844 | || c == K_X2RELEASE |
| 1845 | || c == K_LEFTDRAG |
| 1846 | || c == K_MIDDLEDRAG |
| 1847 | || c == K_RIGHTDRAG |
| 1848 | || c == K_X1DRAG |
| 1849 | || c == K_X2DRAG) |
| 1850 | return retval; |
| 1851 | if (popup_visible) |
| 1852 | { |
| 1853 | int row = mouse_row; |
| 1854 | int col = mouse_col; |
| 1855 | win_T *wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 1856 | |
| 1857 | if (wp != NULL && WIN_IS_POPUP(wp)) |
| 1858 | return retval; |
| 1859 | } |
| 1860 | } |
| 1861 | #endif |
| 1862 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 1863 | // Set "compl_get_longest" when finding the first matches. |
| 1864 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 1865 | || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started)) |
| 1866 | { |
| 1867 | compl_get_longest = (strstr((char *)p_cot, "longest") != NULL); |
| 1868 | compl_used_match = TRUE; |
| 1869 | |
| 1870 | } |
| 1871 | |
| 1872 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 1873 | { |
| 1874 | // We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 1875 | // it will be yet. Now we decide. |
| 1876 | switch (c) |
| 1877 | { |
| 1878 | case Ctrl_E: |
| 1879 | case Ctrl_Y: |
| 1880 | ctrl_x_mode = CTRL_X_SCROLL; |
| 1881 | if (!(State & REPLACE_FLAG)) |
| 1882 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 1883 | else |
| 1884 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 1885 | edit_submode_pre = NULL; |
| 1886 | showmode(); |
| 1887 | break; |
| 1888 | case Ctrl_L: |
| 1889 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 1890 | break; |
| 1891 | case Ctrl_F: |
| 1892 | ctrl_x_mode = CTRL_X_FILES; |
| 1893 | break; |
| 1894 | case Ctrl_K: |
| 1895 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 1896 | break; |
| 1897 | case Ctrl_R: |
| 1898 | // Simply allow ^R to happen without affecting ^X mode |
| 1899 | break; |
| 1900 | case Ctrl_T: |
| 1901 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 1902 | break; |
| 1903 | #ifdef FEAT_COMPL_FUNC |
| 1904 | case Ctrl_U: |
| 1905 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 1906 | break; |
| 1907 | case Ctrl_O: |
| 1908 | ctrl_x_mode = CTRL_X_OMNI; |
| 1909 | break; |
| 1910 | #endif |
| 1911 | case 's': |
| 1912 | case Ctrl_S: |
| 1913 | ctrl_x_mode = CTRL_X_SPELL; |
| 1914 | #ifdef FEAT_SPELL |
| 1915 | ++emsg_off; // Avoid getting the E756 error twice. |
| 1916 | spell_back_to_badword(); |
| 1917 | --emsg_off; |
| 1918 | #endif |
| 1919 | break; |
| 1920 | case Ctrl_RSB: |
| 1921 | ctrl_x_mode = CTRL_X_TAGS; |
| 1922 | break; |
| 1923 | #ifdef FEAT_FIND_ID |
| 1924 | case Ctrl_I: |
| 1925 | case K_S_TAB: |
| 1926 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 1927 | break; |
| 1928 | case Ctrl_D: |
| 1929 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 1930 | break; |
| 1931 | #endif |
| 1932 | case Ctrl_V: |
| 1933 | case Ctrl_Q: |
| 1934 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 1935 | break; |
| 1936 | case Ctrl_P: |
| 1937 | case Ctrl_N: |
| 1938 | // ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 1939 | // just started ^X mode, or there were enough ^X's to cancel |
| 1940 | // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 1941 | // do normal expansion when interrupting a different mode (say |
| 1942 | // ^X^F^X^P or ^P^X^X^P, see below) |
| 1943 | // nothing changes if interrupting mode 0, (eg, the flag |
| 1944 | // doesn't change when going to ADDING mode -- Acevedo |
| 1945 | if (!(compl_cont_status & CONT_INTRPT)) |
| 1946 | compl_cont_status |= CONT_LOCAL; |
| 1947 | else if (compl_cont_mode != 0) |
| 1948 | compl_cont_status &= ~CONT_LOCAL; |
| 1949 | // FALLTHROUGH |
| 1950 | default: |
| 1951 | // If we have typed at least 2 ^X's... for modes != 0, we set |
| 1952 | // compl_cont_status = 0 (eg, as if we had just started ^X |
| 1953 | // mode). |
| 1954 | // For mode 0, we set "compl_cont_mode" to an impossible |
| 1955 | // value, in both cases ^X^X can be used to restart the same |
| 1956 | // mode (avoiding ADDING mode). |
| 1957 | // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 1958 | // 'complete' and local ^P expansions respectively. |
| 1959 | // In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 1960 | // mode -- Acevedo |
| 1961 | if (c == Ctrl_X) |
| 1962 | { |
| 1963 | if (compl_cont_mode != 0) |
| 1964 | compl_cont_status = 0; |
| 1965 | else |
| 1966 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
| 1967 | } |
| 1968 | ctrl_x_mode = CTRL_X_NORMAL; |
| 1969 | edit_submode = NULL; |
| 1970 | showmode(); |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | else if (ctrl_x_mode != CTRL_X_NORMAL) |
| 1975 | { |
| 1976 | // We're already in CTRL-X mode, do we stay in it? |
| 1977 | if (!vim_is_ctrl_x_key(c)) |
| 1978 | { |
| 1979 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 1980 | ctrl_x_mode = CTRL_X_NORMAL; |
| 1981 | else |
| 1982 | ctrl_x_mode = CTRL_X_FINISHED; |
| 1983 | edit_submode = NULL; |
| 1984 | } |
| 1985 | showmode(); |
| 1986 | } |
| 1987 | |
| 1988 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
| 1989 | { |
| 1990 | // Show error message from attempted keyword completion (probably |
| 1991 | // 'Pattern not found') until another key is hit, then go back to |
| 1992 | // showing what mode we are in. |
| 1993 | showmode(); |
| 1994 | if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P |
| 1995 | && c != Ctrl_R && !ins_compl_pum_key(c)) |
| 1996 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 1997 | { |
| 1998 | // Get here when we have finished typing a sequence of ^N and |
| 1999 | // ^P or other completion characters in CTRL-X mode. Free up |
| 2000 | // memory that was used, and make sure we can redo the insert. |
| 2001 | if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
| 2002 | { |
| 2003 | // If any of the original typed text has been changed, eg when |
| 2004 | // ignorecase is set, we must add back-spaces to the redo |
| 2005 | // buffer. We add as few as necessary to delete just the part |
| 2006 | // of the original text that has changed. |
| 2007 | // When using the longest match, edited the match or used |
| 2008 | // CTRL-E then don't use the current match. |
| 2009 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
| 2010 | ptr = compl_curr_match->cp_str; |
| 2011 | else |
| 2012 | ptr = NULL; |
| 2013 | ins_compl_fixRedoBufForLeader(ptr); |
| 2014 | } |
| 2015 | |
| 2016 | #ifdef FEAT_CINDENT |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2017 | want_cindent = (get_can_cindent() && cindent_on()); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2018 | #endif |
| 2019 | // When completing whole lines: fix indent for 'cindent'. |
| 2020 | // Otherwise, break line if it's too long. |
| 2021 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
| 2022 | { |
| 2023 | #ifdef FEAT_CINDENT |
| 2024 | // re-indent the current line |
| 2025 | if (want_cindent) |
| 2026 | { |
| 2027 | do_c_expr_indent(); |
| 2028 | want_cindent = FALSE; // don't do it again |
| 2029 | } |
| 2030 | #endif |
| 2031 | } |
| 2032 | else |
| 2033 | { |
| 2034 | int prev_col = curwin->w_cursor.col; |
| 2035 | |
| 2036 | // put the cursor on the last char, for 'tw' formatting |
| 2037 | if (prev_col > 0) |
| 2038 | dec_cursor(); |
| 2039 | // only format when something was inserted |
| 2040 | if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) |
| 2041 | insertchar(NUL, 0, -1); |
| 2042 | if (prev_col > 0 |
| 2043 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 2044 | inc_cursor(); |
| 2045 | } |
| 2046 | |
| 2047 | // If the popup menu is displayed pressing CTRL-Y means accepting |
| 2048 | // the selection without inserting anything. When |
| 2049 | // compl_enter_selects is set the Enter key does the same. |
| 2050 | if ((c == Ctrl_Y || (compl_enter_selects |
| 2051 | && (c == CAR || c == K_KENTER || c == NL))) |
| 2052 | && pum_visible()) |
| 2053 | retval = TRUE; |
| 2054 | |
| 2055 | // CTRL-E means completion is Ended, go back to the typed text. |
| 2056 | // but only do this, if the Popup is still visible |
| 2057 | if (c == Ctrl_E) |
| 2058 | { |
| 2059 | ins_compl_delete(); |
| 2060 | if (compl_leader != NULL) |
| 2061 | ins_bytes(compl_leader + ins_compl_len()); |
| 2062 | else if (compl_first_match != NULL) |
| 2063 | ins_bytes(compl_orig_text + ins_compl_len()); |
| 2064 | retval = TRUE; |
| 2065 | } |
| 2066 | |
| 2067 | auto_format(FALSE, TRUE); |
| 2068 | |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 2069 | // Trigger the CompleteDonePre event to give scripts a chance to |
| 2070 | // act upon the completion before clearing the info, and restore |
| 2071 | // ctrl_x_mode, so that complete_info() can be used. |
Bram Moolenaar | da812e2 | 2020-01-26 18:35:31 +0100 | [diff] [blame] | 2072 | ctrl_x_mode = prev_mode; |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 2073 | ins_apply_autocmds(EVENT_COMPLETEDONEPRE); |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 2074 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2075 | ins_compl_free(); |
| 2076 | compl_started = FALSE; |
| 2077 | compl_matches = 0; |
| 2078 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 2079 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 2080 | ctrl_x_mode = CTRL_X_NORMAL; |
| 2081 | compl_enter_selects = FALSE; |
| 2082 | if (edit_submode != NULL) |
| 2083 | { |
| 2084 | edit_submode = NULL; |
| 2085 | showmode(); |
| 2086 | } |
| 2087 | |
| 2088 | #ifdef FEAT_CMDWIN |
| 2089 | if (c == Ctrl_C && cmdwin_type != 0) |
| 2090 | // Avoid the popup menu remains displayed when leaving the |
| 2091 | // command line window. |
| 2092 | update_screen(0); |
| 2093 | #endif |
| 2094 | #ifdef FEAT_CINDENT |
| 2095 | // Indent now if a key was typed that is in 'cinkeys'. |
| 2096 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2097 | do_c_expr_indent(); |
| 2098 | #endif |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 2099 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2100 | // upon the end of completion. |
| 2101 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 2105 | // Trigger the CompleteDone event to give scripts a chance to act |
| 2106 | // upon the (possibly failed) completion. |
| 2107 | ins_apply_autocmds(EVENT_COMPLETEDONE); |
| 2108 | |
| 2109 | // reset continue_* if we left expansion-mode, if we stay they'll be |
| 2110 | // (re)set properly in ins_complete() |
| 2111 | if (!vim_is_ctrl_x_key(c)) |
| 2112 | { |
| 2113 | compl_cont_status = 0; |
| 2114 | compl_cont_mode = 0; |
| 2115 | } |
| 2116 | |
| 2117 | return retval; |
| 2118 | } |
| 2119 | |
| 2120 | /* |
| 2121 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 2122 | * text. This inserts backspaces and appends the changed text. |
| 2123 | * "ptr" is the known leader text or NUL. |
| 2124 | */ |
| 2125 | static void |
| 2126 | ins_compl_fixRedoBufForLeader(char_u *ptr_arg) |
| 2127 | { |
| 2128 | int len; |
| 2129 | char_u *p; |
| 2130 | char_u *ptr = ptr_arg; |
| 2131 | |
| 2132 | if (ptr == NULL) |
| 2133 | { |
| 2134 | if (compl_leader != NULL) |
| 2135 | ptr = compl_leader; |
| 2136 | else |
| 2137 | return; // nothing to do |
| 2138 | } |
| 2139 | if (compl_orig_text != NULL) |
| 2140 | { |
| 2141 | p = compl_orig_text; |
| 2142 | for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) |
| 2143 | ; |
| 2144 | if (len > 0) |
| 2145 | len -= (*mb_head_off)(p, p + len); |
| 2146 | for (p += len; *p != NUL; MB_PTR_ADV(p)) |
| 2147 | AppendCharToRedobuff(K_BS); |
| 2148 | } |
| 2149 | else |
| 2150 | len = 0; |
| 2151 | if (ptr != NULL) |
| 2152 | AppendToRedobuffLit(ptr + len, -1); |
| 2153 | } |
| 2154 | |
| 2155 | /* |
| 2156 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 2157 | * (depending on flag) starting from buf and looking for a non-scanned |
| 2158 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 2159 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 2160 | * |
| 2161 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 2162 | */ |
| 2163 | static buf_T * |
| 2164 | ins_compl_next_buf(buf_T *buf, int flag) |
| 2165 | { |
| 2166 | static win_T *wp = NULL; |
| 2167 | |
| 2168 | if (flag == 'w') // just windows |
| 2169 | { |
| 2170 | if (buf == curbuf || wp == NULL) // first call for this flag/expansion |
| 2171 | wp = curwin; |
| 2172 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
| 2173 | && wp->w_buffer->b_scanned) |
| 2174 | ; |
| 2175 | buf = wp->w_buffer; |
| 2176 | } |
| 2177 | else |
| 2178 | // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 2179 | // (unlisted buffers) |
| 2180 | // When completing whole lines skip unloaded buffers. |
| 2181 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
| 2182 | && ((flag == 'U' |
| 2183 | ? buf->b_p_bl |
| 2184 | : (!buf->b_p_bl |
| 2185 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
| 2186 | || buf->b_scanned)) |
| 2187 | ; |
| 2188 | return buf; |
| 2189 | } |
| 2190 | |
| 2191 | #ifdef FEAT_COMPL_FUNC |
| 2192 | /* |
| 2193 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
| 2194 | * get matches in "matches". |
| 2195 | */ |
| 2196 | static void |
| 2197 | expand_by_function( |
| 2198 | int type, // CTRL_X_OMNI or CTRL_X_FUNCTION |
| 2199 | char_u *base) |
| 2200 | { |
| 2201 | list_T *matchlist = NULL; |
| 2202 | dict_T *matchdict = NULL; |
| 2203 | typval_T args[3]; |
| 2204 | char_u *funcname; |
| 2205 | pos_T pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2206 | typval_T rettv; |
| 2207 | int save_State = State; |
| 2208 | |
| 2209 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 2210 | if (*funcname == NUL) |
| 2211 | return; |
| 2212 | |
| 2213 | // Call 'completefunc' to obtain the list of matches. |
| 2214 | args[0].v_type = VAR_NUMBER; |
| 2215 | args[0].vval.v_number = 0; |
| 2216 | args[1].v_type = VAR_STRING; |
| 2217 | args[1].vval.v_string = base != NULL ? base : (char_u *)""; |
| 2218 | args[2].v_type = VAR_UNKNOWN; |
| 2219 | |
| 2220 | pos = curwin->w_cursor; |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 2221 | // Lock the text to avoid weird things from happening. Also disallow |
| 2222 | // switching to another window, it should not be needed and may end up in |
| 2223 | // Insert mode in another buffer. |
| 2224 | ++textwinlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2225 | |
| 2226 | // Call a function, which returns a list or dict. |
| 2227 | if (call_vim_function(funcname, 2, args, &rettv) == OK) |
| 2228 | { |
| 2229 | switch (rettv.v_type) |
| 2230 | { |
| 2231 | case VAR_LIST: |
| 2232 | matchlist = rettv.vval.v_list; |
| 2233 | break; |
| 2234 | case VAR_DICT: |
| 2235 | matchdict = rettv.vval.v_dict; |
| 2236 | break; |
| 2237 | case VAR_SPECIAL: |
| 2238 | if (rettv.vval.v_number == VVAL_NONE) |
| 2239 | compl_opt_suppress_empty = TRUE; |
| 2240 | // FALLTHROUGH |
| 2241 | default: |
| 2242 | // TODO: Give error message? |
| 2243 | clear_tv(&rettv); |
| 2244 | break; |
| 2245 | } |
| 2246 | } |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 2247 | --textwinlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2248 | |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2249 | curwin->w_cursor = pos; // restore the cursor position |
| 2250 | validate_cursor(); |
| 2251 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 2252 | { |
| 2253 | emsg(_(e_compldel)); |
| 2254 | goto theend; |
| 2255 | } |
| 2256 | |
| 2257 | if (matchlist != NULL) |
| 2258 | ins_compl_add_list(matchlist); |
| 2259 | else if (matchdict != NULL) |
| 2260 | ins_compl_add_dict(matchdict); |
| 2261 | |
| 2262 | theend: |
| 2263 | // Restore State, it might have been changed. |
| 2264 | State = save_State; |
| 2265 | |
| 2266 | if (matchdict != NULL) |
| 2267 | dict_unref(matchdict); |
| 2268 | if (matchlist != NULL) |
| 2269 | list_unref(matchlist); |
| 2270 | } |
| 2271 | #endif // FEAT_COMPL_FUNC |
| 2272 | |
| 2273 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
| 2274 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2275 | * Add a match to the list of matches from a typeval_T. |
| 2276 | * If the given string is already in the list of completions, then return |
| 2277 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 2278 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2279 | * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck(). |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2280 | */ |
| 2281 | static int |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2282 | ins_compl_add_tv(typval_T *tv, int dir, int fast) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2283 | { |
| 2284 | char_u *word; |
| 2285 | int dup = FALSE; |
| 2286 | int empty = FALSE; |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2287 | int flags = fast ? CP_FAST : 0; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2288 | char_u *(cptext[CPT_COUNT]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2289 | typval_T user_data; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2290 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2291 | user_data.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2292 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 2293 | { |
| 2294 | word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE); |
| 2295 | cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict, |
| 2296 | (char_u *)"abbr", FALSE); |
| 2297 | cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, |
| 2298 | (char_u *)"menu", FALSE); |
| 2299 | cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, |
| 2300 | (char_u *)"kind", FALSE); |
| 2301 | cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, |
| 2302 | (char_u *)"info", FALSE); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2303 | dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2304 | if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL |
| 2305 | && dict_get_number(tv->vval.v_dict, (char_u *)"icase")) |
| 2306 | flags |= CP_ICASE; |
| 2307 | if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL) |
| 2308 | dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup"); |
| 2309 | if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL) |
| 2310 | empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty"); |
| 2311 | if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL |
| 2312 | && dict_get_number(tv->vval.v_dict, (char_u *)"equal")) |
| 2313 | flags |= CP_EQUAL; |
| 2314 | } |
| 2315 | else |
| 2316 | { |
| 2317 | word = tv_get_string_chk(tv); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 2318 | CLEAR_FIELD(cptext); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2319 | } |
| 2320 | if (word == NULL || (!empty && *word == NUL)) |
| 2321 | return FAIL; |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2322 | return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | /* |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2326 | * Add completions from a list. |
| 2327 | */ |
| 2328 | static void |
| 2329 | ins_compl_add_list(list_T *list) |
| 2330 | { |
| 2331 | listitem_T *li; |
| 2332 | int dir = compl_direction; |
| 2333 | |
| 2334 | // Go through the List with matches and add each of them. |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 2335 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 2336 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2337 | { |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2338 | if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2339 | // if dir was BACKWARD then honor it just once |
| 2340 | dir = FORWARD; |
| 2341 | else if (did_emsg) |
| 2342 | break; |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Add completions from a dict. |
| 2348 | */ |
| 2349 | static void |
| 2350 | ins_compl_add_dict(dict_T *dict) |
| 2351 | { |
| 2352 | dictitem_T *di_refresh; |
| 2353 | dictitem_T *di_words; |
| 2354 | |
| 2355 | // Check for optional "refresh" item. |
| 2356 | compl_opt_refresh_always = FALSE; |
| 2357 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 2358 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
| 2359 | { |
| 2360 | char_u *v = di_refresh->di_tv.vval.v_string; |
| 2361 | |
| 2362 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 2363 | compl_opt_refresh_always = TRUE; |
| 2364 | } |
| 2365 | |
| 2366 | // Add completions from a "words" list. |
| 2367 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 2368 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 2369 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
| 2370 | } |
| 2371 | |
| 2372 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2373 | * Start completion for the complete() function. |
| 2374 | * "startcol" is where the matched text starts (1 is first column). |
| 2375 | * "list" is the list of matches. |
| 2376 | */ |
| 2377 | static void |
| 2378 | set_completion(colnr_T startcol, list_T *list) |
| 2379 | { |
| 2380 | int save_w_wrow = curwin->w_wrow; |
| 2381 | int save_w_leftcol = curwin->w_leftcol; |
| 2382 | int flags = CP_ORIGINAL_TEXT; |
| 2383 | |
| 2384 | // If already doing completions stop it. |
| 2385 | if (ctrl_x_mode != CTRL_X_NORMAL) |
| 2386 | ins_compl_prep(' '); |
| 2387 | ins_compl_clear(); |
| 2388 | ins_compl_free(); |
| 2389 | |
| 2390 | compl_direction = FORWARD; |
| 2391 | if (startcol > curwin->w_cursor.col) |
| 2392 | startcol = curwin->w_cursor.col; |
| 2393 | compl_col = startcol; |
| 2394 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
| 2395 | // compl_pattern doesn't need to be set |
| 2396 | compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); |
| 2397 | if (p_ic) |
| 2398 | flags |= CP_ICASE; |
| 2399 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2400 | -1, NULL, NULL, NULL, 0, |
| 2401 | flags | CP_FAST, FALSE) != OK) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2402 | return; |
| 2403 | |
| 2404 | ctrl_x_mode = CTRL_X_EVAL; |
| 2405 | |
| 2406 | ins_compl_add_list(list); |
| 2407 | compl_matches = ins_compl_make_cyclic(); |
| 2408 | compl_started = TRUE; |
| 2409 | compl_used_match = TRUE; |
| 2410 | compl_cont_status = 0; |
| 2411 | |
| 2412 | compl_curr_match = compl_first_match; |
| 2413 | if (compl_no_insert || compl_no_select) |
| 2414 | { |
| 2415 | ins_complete(K_DOWN, FALSE); |
| 2416 | if (compl_no_select) |
| 2417 | // Down/Up has no real effect. |
| 2418 | ins_complete(K_UP, FALSE); |
| 2419 | } |
| 2420 | else |
| 2421 | ins_complete(Ctrl_N, FALSE); |
| 2422 | compl_enter_selects = compl_no_insert; |
| 2423 | |
| 2424 | // Lazily show the popup menu, unless we got interrupted. |
| 2425 | if (!compl_interrupted) |
| 2426 | show_pum(save_w_wrow, save_w_leftcol); |
| 2427 | out_flush(); |
| 2428 | } |
| 2429 | |
| 2430 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2431 | * "complete()" function |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2432 | */ |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2433 | void |
| 2434 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2435 | { |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2436 | int startcol; |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 2437 | int save_textlock = textlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2438 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 2439 | if (in_vim9script() |
| 2440 | && (check_for_number_arg(argvars, 0) == FAIL |
| 2441 | || check_for_list_arg(argvars, 1) == FAIL)) |
| 2442 | return; |
| 2443 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2444 | if ((State & INSERT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2445 | { |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2446 | emsg(_("E785: complete() can only be used in Insert mode")); |
| 2447 | return; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2448 | } |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2449 | |
Bram Moolenaar | 6adb9ea | 2020-04-30 22:31:18 +0200 | [diff] [blame] | 2450 | // "textlock" is set when evaluating 'completefunc' but we can change |
| 2451 | // text here. |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 2452 | textlock = 0; |
| 2453 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2454 | // Check for undo allowed here, because if something was already inserted |
| 2455 | // the line was already saved for undo and this check isn't done. |
| 2456 | if (!undo_allowed()) |
| 2457 | return; |
| 2458 | |
| 2459 | if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL) |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2460 | emsg(_(e_invarg)); |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 2461 | else |
| 2462 | { |
| 2463 | startcol = (int)tv_get_number_chk(&argvars[0], NULL); |
| 2464 | if (startcol > 0) |
| 2465 | set_completion(startcol - 1, argvars[1].vval.v_list); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2466 | } |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 2467 | textlock = save_textlock; |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * "complete_add()" function |
| 2472 | */ |
| 2473 | void |
| 2474 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 2475 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2476 | if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 2477 | return; |
| 2478 | |
Bram Moolenaar | 440cf09 | 2021-04-03 20:13:30 +0200 | [diff] [blame] | 2479 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE); |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * "complete_check()" function |
| 2484 | */ |
| 2485 | void |
| 2486 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 2487 | { |
| 2488 | int saved = RedrawingDisabled; |
| 2489 | |
| 2490 | RedrawingDisabled = 0; |
| 2491 | ins_compl_check_keys(0, TRUE); |
| 2492 | rettv->vval.v_number = ins_compl_interrupted(); |
| 2493 | RedrawingDisabled = saved; |
| 2494 | } |
| 2495 | |
| 2496 | /* |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2497 | * Return Insert completion mode name string |
| 2498 | */ |
| 2499 | static char_u * |
| 2500 | ins_compl_mode(void) |
| 2501 | { |
| 2502 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started) |
| 2503 | return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; |
| 2504 | |
| 2505 | return (char_u *)""; |
| 2506 | } |
| 2507 | |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 2508 | static void |
| 2509 | ins_compl_update_sequence_numbers() |
| 2510 | { |
| 2511 | int number = 0; |
| 2512 | compl_T *match; |
| 2513 | |
| 2514 | if (compl_direction == FORWARD) |
| 2515 | { |
| 2516 | // search backwards for the first valid (!= -1) number. |
| 2517 | // This should normally succeed already at the first loop |
| 2518 | // cycle, so it's fast! |
| 2519 | for (match = compl_curr_match->cp_prev; match != NULL |
| 2520 | && match != compl_first_match; |
| 2521 | match = match->cp_prev) |
| 2522 | if (match->cp_number != -1) |
| 2523 | { |
| 2524 | number = match->cp_number; |
| 2525 | break; |
| 2526 | } |
| 2527 | if (match != NULL) |
| 2528 | // go up and assign all numbers which are not assigned |
| 2529 | // yet |
| 2530 | for (match = match->cp_next; |
| 2531 | match != NULL && match->cp_number == -1; |
| 2532 | match = match->cp_next) |
| 2533 | match->cp_number = ++number; |
| 2534 | } |
| 2535 | else // BACKWARD |
| 2536 | { |
| 2537 | // search forwards (upwards) for the first valid (!= -1) |
| 2538 | // number. This should normally succeed already at the |
| 2539 | // first loop cycle, so it's fast! |
| 2540 | for (match = compl_curr_match->cp_next; match != NULL |
| 2541 | && match != compl_first_match; |
| 2542 | match = match->cp_next) |
| 2543 | if (match->cp_number != -1) |
| 2544 | { |
| 2545 | number = match->cp_number; |
| 2546 | break; |
| 2547 | } |
| 2548 | if (match != NULL) |
| 2549 | // go down and assign all numbers which are not |
| 2550 | // assigned yet |
| 2551 | for (match = match->cp_prev; match |
| 2552 | && match->cp_number == -1; |
| 2553 | match = match->cp_prev) |
| 2554 | match->cp_number = ++number; |
| 2555 | } |
| 2556 | } |
| 2557 | |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2558 | /* |
| 2559 | * Get complete information |
| 2560 | */ |
| 2561 | static void |
| 2562 | get_complete_info(list_T *what_list, dict_T *retdict) |
| 2563 | { |
| 2564 | int ret = OK; |
| 2565 | listitem_T *item; |
| 2566 | #define CI_WHAT_MODE 0x01 |
| 2567 | #define CI_WHAT_PUM_VISIBLE 0x02 |
| 2568 | #define CI_WHAT_ITEMS 0x04 |
| 2569 | #define CI_WHAT_SELECTED 0x08 |
| 2570 | #define CI_WHAT_INSERTED 0x10 |
| 2571 | #define CI_WHAT_ALL 0xff |
| 2572 | int what_flag; |
| 2573 | |
| 2574 | if (what_list == NULL) |
| 2575 | what_flag = CI_WHAT_ALL; |
| 2576 | else |
| 2577 | { |
| 2578 | what_flag = 0; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 2579 | CHECK_LIST_MATERIALIZE(what_list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 2580 | FOR_ALL_LIST_ITEMS(what_list, item) |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2581 | { |
| 2582 | char_u *what = tv_get_string(&item->li_tv); |
| 2583 | |
| 2584 | if (STRCMP(what, "mode") == 0) |
| 2585 | what_flag |= CI_WHAT_MODE; |
| 2586 | else if (STRCMP(what, "pum_visible") == 0) |
| 2587 | what_flag |= CI_WHAT_PUM_VISIBLE; |
| 2588 | else if (STRCMP(what, "items") == 0) |
| 2589 | what_flag |= CI_WHAT_ITEMS; |
| 2590 | else if (STRCMP(what, "selected") == 0) |
| 2591 | what_flag |= CI_WHAT_SELECTED; |
| 2592 | else if (STRCMP(what, "inserted") == 0) |
| 2593 | what_flag |= CI_WHAT_INSERTED; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | if (ret == OK && (what_flag & CI_WHAT_MODE)) |
| 2598 | ret = dict_add_string(retdict, "mode", ins_compl_mode()); |
| 2599 | |
| 2600 | if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) |
| 2601 | ret = dict_add_number(retdict, "pum_visible", pum_visible()); |
| 2602 | |
| 2603 | if (ret == OK && (what_flag & CI_WHAT_ITEMS)) |
| 2604 | { |
| 2605 | list_T *li; |
| 2606 | dict_T *di; |
| 2607 | compl_T *match; |
| 2608 | |
| 2609 | li = list_alloc(); |
| 2610 | if (li == NULL) |
| 2611 | return; |
| 2612 | ret = dict_add_list(retdict, "items", li); |
| 2613 | if (ret == OK && compl_first_match != NULL) |
| 2614 | { |
| 2615 | match = compl_first_match; |
| 2616 | do |
| 2617 | { |
| 2618 | if (!(match->cp_flags & CP_ORIGINAL_TEXT)) |
| 2619 | { |
| 2620 | di = dict_alloc(); |
| 2621 | if (di == NULL) |
| 2622 | return; |
| 2623 | ret = list_append_dict(li, di); |
| 2624 | if (ret != OK) |
| 2625 | return; |
| 2626 | dict_add_string(di, "word", match->cp_str); |
| 2627 | dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]); |
| 2628 | dict_add_string(di, "menu", match->cp_text[CPT_MENU]); |
| 2629 | dict_add_string(di, "kind", match->cp_text[CPT_KIND]); |
| 2630 | dict_add_string(di, "info", match->cp_text[CPT_INFO]); |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 2631 | if (match->cp_user_data.v_type == VAR_UNKNOWN) |
| 2632 | // Add an empty string for backwards compatibility |
| 2633 | dict_add_string(di, "user_data", (char_u *)""); |
| 2634 | else |
| 2635 | dict_add_tv(di, "user_data", &match->cp_user_data); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2636 | } |
| 2637 | match = match->cp_next; |
| 2638 | } |
| 2639 | while (match != NULL && match != compl_first_match); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | if (ret == OK && (what_flag & CI_WHAT_SELECTED)) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 2644 | { |
| 2645 | if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) |
| 2646 | ins_compl_update_sequence_numbers(); |
| 2647 | ret = dict_add_number(retdict, "selected", compl_curr_match != NULL |
| 2648 | ? compl_curr_match->cp_number - 1 : -1); |
| 2649 | } |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 2650 | |
| 2651 | // TODO |
| 2652 | // if (ret == OK && (what_flag & CI_WHAT_INSERTED)) |
| 2653 | } |
| 2654 | |
| 2655 | /* |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2656 | * "complete_info()" function |
| 2657 | */ |
| 2658 | void |
| 2659 | f_complete_info(typval_T *argvars, typval_T *rettv) |
| 2660 | { |
| 2661 | list_T *what_list = NULL; |
| 2662 | |
| 2663 | if (rettv_dict_alloc(rettv) != OK) |
| 2664 | return; |
| 2665 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2666 | if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL) |
| 2667 | return; |
| 2668 | |
Bram Moolenaar | 9bca58f | 2019-08-15 21:31:52 +0200 | [diff] [blame] | 2669 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 2670 | { |
| 2671 | if (argvars[0].v_type != VAR_LIST) |
| 2672 | { |
| 2673 | emsg(_(e_listreq)); |
| 2674 | return; |
| 2675 | } |
| 2676 | what_list = argvars[0].vval.v_list; |
| 2677 | } |
| 2678 | get_complete_info(what_list, rettv->vval.v_dict); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2679 | } |
| 2680 | #endif |
| 2681 | |
| 2682 | /* |
| 2683 | * Get the next expansion(s), using "compl_pattern". |
| 2684 | * The search starts at position "ini" in curbuf and in the direction |
| 2685 | * compl_direction. |
| 2686 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 2687 | * where we stopped searching before. |
| 2688 | * This may return before finding all the matches. |
| 2689 | * Return the total number of matches or -1 if still unknown -- Acevedo |
| 2690 | */ |
| 2691 | static int |
| 2692 | ins_compl_get_exp(pos_T *ini) |
| 2693 | { |
| 2694 | static pos_T first_match_pos; |
| 2695 | static pos_T last_match_pos; |
| 2696 | static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete' |
| 2697 | static int found_all = FALSE; // Found all matches of a |
| 2698 | // certain type. |
| 2699 | static buf_T *ins_buf = NULL; // buffer being scanned |
| 2700 | |
| 2701 | pos_T *pos; |
| 2702 | char_u **matches; |
| 2703 | int save_p_scs; |
| 2704 | int save_p_ws; |
| 2705 | int save_p_ic; |
| 2706 | int i; |
| 2707 | int num_matches; |
| 2708 | int len; |
| 2709 | int found_new_match; |
| 2710 | int type = ctrl_x_mode; |
| 2711 | char_u *ptr; |
| 2712 | char_u *dict = NULL; |
| 2713 | int dict_f = 0; |
| 2714 | int set_match_pos; |
Andy Gozas | 6a230c6 | 2021-08-05 16:23:27 +0200 | [diff] [blame] | 2715 | pos_T prev_pos = {0, 0, 0}; |
| 2716 | int looped_around = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2717 | |
| 2718 | if (!compl_started) |
| 2719 | { |
| 2720 | FOR_ALL_BUFFERS(ins_buf) |
| 2721 | ins_buf->b_scanned = 0; |
| 2722 | found_all = FALSE; |
| 2723 | ins_buf = curbuf; |
| 2724 | e_cpt = (compl_cont_status & CONT_LOCAL) |
| 2725 | ? (char_u *)"." : curbuf->b_p_cpt; |
| 2726 | last_match_pos = first_match_pos = *ini; |
| 2727 | } |
| 2728 | else if (ins_buf != curbuf && !buf_valid(ins_buf)) |
| 2729 | ins_buf = curbuf; // In case the buffer was wiped out. |
| 2730 | |
| 2731 | compl_old_match = compl_curr_match; // remember the last current match |
| 2732 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
| 2733 | |
| 2734 | // For ^N/^P loop over all the flags/windows/buffers in 'complete'. |
| 2735 | for (;;) |
| 2736 | { |
| 2737 | found_new_match = FAIL; |
| 2738 | set_match_pos = FALSE; |
| 2739 | |
| 2740 | // For ^N/^P pick a new entry from e_cpt if compl_started is off, |
| 2741 | // or if found_all says this entry is done. For ^X^L only use the |
| 2742 | // entries from 'complete' that look in loaded buffers. |
| 2743 | if ((ctrl_x_mode == CTRL_X_NORMAL |
| 2744 | || ctrl_x_mode_line_or_eval()) |
| 2745 | && (!compl_started || found_all)) |
| 2746 | { |
| 2747 | found_all = FALSE; |
| 2748 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 2749 | e_cpt++; |
| 2750 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 2751 | { |
| 2752 | ins_buf = curbuf; |
| 2753 | first_match_pos = *ini; |
| 2754 | // Move the cursor back one character so that ^N can match the |
| 2755 | // word immediately after the cursor. |
| 2756 | if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0) |
| 2757 | { |
| 2758 | // Move the cursor to after the last character in the |
| 2759 | // buffer, so that word at start of buffer is found |
| 2760 | // correctly. |
| 2761 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count; |
| 2762 | first_match_pos.col = |
| 2763 | (colnr_T)STRLEN(ml_get(first_match_pos.lnum)); |
| 2764 | } |
| 2765 | last_match_pos = first_match_pos; |
| 2766 | type = 0; |
| 2767 | |
| 2768 | // Remember the first match so that the loop stops when we |
| 2769 | // wrap and come back there a second time. |
| 2770 | set_match_pos = TRUE; |
| 2771 | } |
| 2772 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 2773 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 2774 | { |
| 2775 | // Scan a buffer, but not the current one. |
| 2776 | if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer |
| 2777 | { |
| 2778 | compl_started = TRUE; |
| 2779 | first_match_pos.col = last_match_pos.col = 0; |
| 2780 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 2781 | last_match_pos.lnum = 0; |
| 2782 | type = 0; |
| 2783 | } |
| 2784 | else // unloaded buffer, scan like dictionary |
| 2785 | { |
| 2786 | found_all = TRUE; |
| 2787 | if (ins_buf->b_fname == NULL) |
| 2788 | continue; |
| 2789 | type = CTRL_X_DICTIONARY; |
| 2790 | dict = ins_buf->b_fname; |
| 2791 | dict_f = DICT_EXACT; |
| 2792 | } |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 2793 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2794 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
| 2795 | ins_buf->b_fname == NULL |
| 2796 | ? buf_spname(ins_buf) |
| 2797 | : ins_buf->b_sfname == NULL |
| 2798 | ? ins_buf->b_fname |
| 2799 | : ins_buf->b_sfname); |
| 2800 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 2801 | } |
| 2802 | else if (*e_cpt == NUL) |
| 2803 | break; |
| 2804 | else |
| 2805 | { |
| 2806 | if (ctrl_x_mode_line_or_eval()) |
| 2807 | type = -1; |
| 2808 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 2809 | { |
| 2810 | if (*e_cpt == 'k') |
| 2811 | type = CTRL_X_DICTIONARY; |
| 2812 | else |
| 2813 | type = CTRL_X_THESAURUS; |
| 2814 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 2815 | { |
| 2816 | dict = e_cpt; |
| 2817 | dict_f = DICT_FIRST; |
| 2818 | } |
| 2819 | } |
| 2820 | #ifdef FEAT_FIND_ID |
| 2821 | else if (*e_cpt == 'i') |
| 2822 | type = CTRL_X_PATH_PATTERNS; |
| 2823 | else if (*e_cpt == 'd') |
| 2824 | type = CTRL_X_PATH_DEFINES; |
| 2825 | #endif |
| 2826 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 2827 | { |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 2828 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2829 | type = CTRL_X_TAGS; |
| 2830 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
| 2831 | (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
| 2832 | } |
| 2833 | else |
| 2834 | type = -1; |
| 2835 | |
| 2836 | // in any case e_cpt is advanced to the next entry |
| 2837 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 2838 | |
| 2839 | found_all = TRUE; |
| 2840 | if (type == -1) |
| 2841 | continue; |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | // If complete() was called then compl_pattern has been reset. The |
| 2846 | // following won't work then, bail out. |
| 2847 | if (compl_pattern == NULL) |
| 2848 | break; |
| 2849 | |
| 2850 | switch (type) |
| 2851 | { |
| 2852 | case -1: |
| 2853 | break; |
| 2854 | #ifdef FEAT_FIND_ID |
| 2855 | case CTRL_X_PATH_PATTERNS: |
| 2856 | case CTRL_X_PATH_DEFINES: |
| 2857 | find_pattern_in_path(compl_pattern, compl_direction, |
| 2858 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
| 2859 | (type == CTRL_X_PATH_DEFINES |
| 2860 | && !(compl_cont_status & CONT_SOL)) |
| 2861 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 2862 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 2863 | break; |
| 2864 | #endif |
| 2865 | |
| 2866 | case CTRL_X_DICTIONARY: |
| 2867 | case CTRL_X_THESAURUS: |
| 2868 | ins_compl_dictionaries( |
| 2869 | dict != NULL ? dict |
| 2870 | : (type == CTRL_X_THESAURUS |
| 2871 | ? (*curbuf->b_p_tsr == NUL |
| 2872 | ? p_tsr |
| 2873 | : curbuf->b_p_tsr) |
| 2874 | : (*curbuf->b_p_dict == NUL |
| 2875 | ? p_dict |
| 2876 | : curbuf->b_p_dict)), |
| 2877 | compl_pattern, |
| 2878 | dict != NULL ? dict_f |
| 2879 | : 0, type == CTRL_X_THESAURUS); |
| 2880 | dict = NULL; |
| 2881 | break; |
| 2882 | |
| 2883 | case CTRL_X_TAGS: |
| 2884 | // set p_ic according to p_ic, p_scs and pat for find_tags(). |
| 2885 | save_p_ic = p_ic; |
| 2886 | p_ic = ignorecase(compl_pattern); |
| 2887 | |
| 2888 | // Find up to TAG_MANY matches. Avoids that an enormous number |
| 2889 | // of matches is found when compl_pattern is empty |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 2890 | g_tag_at_cursor = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2891 | if (find_tags(compl_pattern, &num_matches, &matches, |
| 2892 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP |
| 2893 | | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0), |
| 2894 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2895 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 2896 | g_tag_at_cursor = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2897 | p_ic = save_p_ic; |
| 2898 | break; |
| 2899 | |
| 2900 | case CTRL_X_FILES: |
| 2901 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
| 2902 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 2903 | { |
| 2904 | |
| 2905 | // May change home directory back to "~". |
| 2906 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 2907 | #ifdef BACKSLASH_IN_FILENAME |
| 2908 | if (curbuf->b_p_csl[0] != NUL) |
| 2909 | { |
| 2910 | int i; |
| 2911 | |
| 2912 | for (i = 0; i < num_matches; ++i) |
| 2913 | { |
| 2914 | char_u *ptr = matches[i]; |
| 2915 | |
| 2916 | while (*ptr != NUL) |
| 2917 | { |
| 2918 | if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') |
| 2919 | *ptr = '/'; |
| 2920 | else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') |
| 2921 | *ptr = '\\'; |
| 2922 | ptr += (*mb_ptr2len)(ptr); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2927 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
| 2928 | } |
| 2929 | break; |
| 2930 | |
| 2931 | case CTRL_X_CMDLINE: |
| 2932 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 2933 | (int)STRLEN(compl_pattern), |
| 2934 | &num_matches, &matches) == EXPAND_OK) |
| 2935 | ins_compl_add_matches(num_matches, matches, FALSE); |
| 2936 | break; |
| 2937 | |
| 2938 | #ifdef FEAT_COMPL_FUNC |
| 2939 | case CTRL_X_FUNCTION: |
| 2940 | case CTRL_X_OMNI: |
| 2941 | expand_by_function(type, compl_pattern); |
| 2942 | break; |
| 2943 | #endif |
| 2944 | |
| 2945 | case CTRL_X_SPELL: |
| 2946 | #ifdef FEAT_SPELL |
| 2947 | num_matches = expand_spelling(first_match_pos.lnum, |
| 2948 | compl_pattern, &matches); |
| 2949 | if (num_matches > 0) |
| 2950 | ins_compl_add_matches(num_matches, matches, p_ic); |
| 2951 | #endif |
| 2952 | break; |
| 2953 | |
| 2954 | default: // normal ^P/^N and ^X^L |
| 2955 | // If 'infercase' is set, don't use 'smartcase' here |
| 2956 | save_p_scs = p_scs; |
| 2957 | if (ins_buf->b_p_inf) |
| 2958 | p_scs = FALSE; |
| 2959 | |
| 2960 | // Buffers other than curbuf are scanned from the beginning or the |
| 2961 | // end but never from the middle, thus setting nowrapscan in this |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2962 | // buffer is a good idea, on the other hand, we always set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2963 | // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb |
| 2964 | save_p_ws = p_ws; |
| 2965 | if (ins_buf != curbuf) |
| 2966 | p_ws = FALSE; |
| 2967 | else if (*e_cpt == '.') |
| 2968 | p_ws = TRUE; |
Andy Gozas | 6a230c6 | 2021-08-05 16:23:27 +0200 | [diff] [blame] | 2969 | looped_around = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2970 | for (;;) |
| 2971 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 2972 | int cont_s_ipos = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2973 | |
| 2974 | ++msg_silent; // Don't want messages for wrapscan. |
| 2975 | |
| 2976 | // ctrl_x_mode_line_or_eval() || word-wise search that |
| 2977 | // has added a word that was at the beginning of the line |
| 2978 | if (ctrl_x_mode_line_or_eval() |
| 2979 | || (compl_cont_status & CONT_SOL)) |
| 2980 | found_new_match = search_for_exact_line(ins_buf, pos, |
| 2981 | compl_direction, compl_pattern); |
| 2982 | else |
| 2983 | found_new_match = searchit(NULL, ins_buf, pos, NULL, |
| 2984 | compl_direction, |
| 2985 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 2986 | RE_LAST, NULL); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2987 | --msg_silent; |
| 2988 | if (!compl_started || set_match_pos) |
| 2989 | { |
| 2990 | // set "compl_started" even on fail |
| 2991 | compl_started = TRUE; |
| 2992 | first_match_pos = *pos; |
| 2993 | last_match_pos = *pos; |
| 2994 | set_match_pos = FALSE; |
| 2995 | } |
| 2996 | else if (first_match_pos.lnum == last_match_pos.lnum |
Andy Gozas | 6a230c6 | 2021-08-05 16:23:27 +0200 | [diff] [blame] | 2997 | && first_match_pos.col == last_match_pos.col) |
| 2998 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 2999 | found_new_match = FAIL; |
Andy Gozas | 6a230c6 | 2021-08-05 16:23:27 +0200 | [diff] [blame] | 3000 | } |
| 3001 | else if ((compl_direction == FORWARD) |
| 3002 | && (prev_pos.lnum > pos->lnum |
| 3003 | || (prev_pos.lnum == pos->lnum |
| 3004 | && prev_pos.col >= pos->col))) |
| 3005 | { |
| 3006 | if (looped_around) |
| 3007 | found_new_match = FAIL; |
| 3008 | else |
| 3009 | looped_around = TRUE; |
| 3010 | } |
| 3011 | else if ((compl_direction != FORWARD) |
| 3012 | && (prev_pos.lnum < pos->lnum |
| 3013 | || (prev_pos.lnum == pos->lnum |
| 3014 | && prev_pos.col <= pos->col))) |
| 3015 | { |
| 3016 | if (looped_around) |
| 3017 | found_new_match = FAIL; |
| 3018 | else |
| 3019 | looped_around = TRUE; |
| 3020 | } |
| 3021 | prev_pos = *pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3022 | if (found_new_match == FAIL) |
| 3023 | { |
| 3024 | if (ins_buf == curbuf) |
| 3025 | found_all = TRUE; |
| 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | // when ADDING, the text before the cursor matches, skip it |
| 3030 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
| 3031 | && ini->lnum == pos->lnum |
| 3032 | && ini->col == pos->col) |
| 3033 | continue; |
| 3034 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 3035 | if (ctrl_x_mode_line_or_eval()) |
| 3036 | { |
| 3037 | if (compl_cont_status & CONT_ADDING) |
| 3038 | { |
| 3039 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 3040 | continue; |
| 3041 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3042 | if (!p_paste) |
| 3043 | ptr = skipwhite(ptr); |
| 3044 | } |
| 3045 | len = (int)STRLEN(ptr); |
| 3046 | } |
| 3047 | else |
| 3048 | { |
| 3049 | char_u *tmp_ptr = ptr; |
| 3050 | |
| 3051 | if (compl_cont_status & CONT_ADDING) |
| 3052 | { |
| 3053 | tmp_ptr += compl_length; |
| 3054 | // Skip if already inside a word. |
| 3055 | if (vim_iswordp(tmp_ptr)) |
| 3056 | continue; |
| 3057 | // Find start of next word. |
| 3058 | tmp_ptr = find_word_start(tmp_ptr); |
| 3059 | } |
| 3060 | // Find end of this word. |
| 3061 | tmp_ptr = find_word_end(tmp_ptr); |
| 3062 | len = (int)(tmp_ptr - ptr); |
| 3063 | |
| 3064 | if ((compl_cont_status & CONT_ADDING) |
| 3065 | && len == compl_length) |
| 3066 | { |
| 3067 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 3068 | { |
| 3069 | // Try next line, if any. the new word will be |
| 3070 | // "join" as if the normal command "J" was used. |
| 3071 | // IOSIZE is always greater than |
| 3072 | // compl_length, so the next STRNCPY always |
| 3073 | // works -- Acevedo |
| 3074 | STRNCPY(IObuff, ptr, len); |
| 3075 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3076 | tmp_ptr = ptr = skipwhite(ptr); |
| 3077 | // Find start of next word. |
| 3078 | tmp_ptr = find_word_start(tmp_ptr); |
| 3079 | // Find end of next word. |
| 3080 | tmp_ptr = find_word_end(tmp_ptr); |
| 3081 | if (tmp_ptr > ptr) |
| 3082 | { |
| 3083 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
| 3084 | { |
| 3085 | if (IObuff[len - 1] != ' ') |
| 3086 | IObuff[len++] = ' '; |
| 3087 | // IObuf =~ "\k.* ", thus len >= 2 |
| 3088 | if (p_js |
| 3089 | && (IObuff[len - 2] == '.' |
| 3090 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 3091 | == NULL |
| 3092 | && (IObuff[len - 2] == '?' |
| 3093 | || IObuff[len - 2] == '!')))) |
| 3094 | IObuff[len++] = ' '; |
| 3095 | } |
| 3096 | // copy as much as possible of the new word |
| 3097 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 3098 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 3099 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 3100 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3101 | cont_s_ipos = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3102 | } |
| 3103 | IObuff[len] = NUL; |
| 3104 | ptr = IObuff; |
| 3105 | } |
| 3106 | if (len == compl_length) |
| 3107 | continue; |
| 3108 | } |
| 3109 | } |
| 3110 | if (ins_compl_add_infercase(ptr, len, p_ic, |
| 3111 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3112 | 0, cont_s_ipos) != NOTDONE) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3113 | { |
| 3114 | found_new_match = OK; |
| 3115 | break; |
| 3116 | } |
| 3117 | } |
| 3118 | p_scs = save_p_scs; |
| 3119 | p_ws = save_p_ws; |
| 3120 | } |
| 3121 | |
| 3122 | // check if compl_curr_match has changed, (e.g. other type of |
| 3123 | // expansion added something) |
| 3124 | if (type != 0 && compl_curr_match != compl_old_match) |
| 3125 | found_new_match = OK; |
| 3126 | |
| 3127 | // break the loop for specialized modes (use 'complete' just for the |
| 3128 | // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new |
| 3129 | // match |
| 3130 | if ((ctrl_x_mode != CTRL_X_NORMAL |
| 3131 | && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL) |
| 3132 | { |
| 3133 | if (got_int) |
| 3134 | break; |
| 3135 | // Fill the popup menu as soon as possible. |
| 3136 | if (type != -1) |
| 3137 | ins_compl_check_keys(0, FALSE); |
| 3138 | |
| 3139 | if ((ctrl_x_mode != CTRL_X_NORMAL |
| 3140 | && !ctrl_x_mode_line_or_eval()) || compl_interrupted) |
| 3141 | break; |
| 3142 | compl_started = TRUE; |
| 3143 | } |
| 3144 | else |
| 3145 | { |
| 3146 | // Mark a buffer scanned when it has been scanned completely |
| 3147 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 3148 | ins_buf->b_scanned = TRUE; |
| 3149 | |
| 3150 | compl_started = FALSE; |
| 3151 | } |
| 3152 | } |
| 3153 | compl_started = TRUE; |
| 3154 | |
| 3155 | if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval()) |
| 3156 | && *e_cpt == NUL) // Got to end of 'complete' |
| 3157 | found_new_match = FAIL; |
| 3158 | |
| 3159 | i = -1; // total of matches, unknown |
| 3160 | if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL |
| 3161 | && !ctrl_x_mode_line_or_eval())) |
| 3162 | i = ins_compl_make_cyclic(); |
| 3163 | |
| 3164 | if (compl_old_match != NULL) |
| 3165 | { |
| 3166 | // If several matches were added (FORWARD) or the search failed and has |
| 3167 | // just been made cyclic then we have to move compl_curr_match to the |
| 3168 | // next or previous entry (if any) -- Acevedo |
| 3169 | compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next |
| 3170 | : compl_old_match->cp_prev; |
| 3171 | if (compl_curr_match == NULL) |
| 3172 | compl_curr_match = compl_old_match; |
| 3173 | } |
| 3174 | return i; |
| 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * Delete the old text being completed. |
| 3179 | */ |
| 3180 | void |
| 3181 | ins_compl_delete(void) |
| 3182 | { |
| 3183 | int col; |
| 3184 | |
| 3185 | // In insert mode: Delete the typed part. |
| 3186 | // In replace mode: Put the old characters back, if any. |
| 3187 | col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
| 3188 | if ((int)curwin->w_cursor.col > col) |
| 3189 | { |
| 3190 | if (stop_arrow() == FAIL) |
| 3191 | return; |
| 3192 | backspace_until_column(col); |
| 3193 | } |
| 3194 | |
| 3195 | // TODO: is this sufficient for redrawing? Redrawing everything causes |
| 3196 | // flicker, thus we can't do that. |
| 3197 | changed_cline_bef_curs(); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3198 | #ifdef FEAT_EVAL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3199 | // clear v:completed_item |
| 3200 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3201 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | /* |
| 3205 | * Insert the new text being completed. |
| 3206 | * "in_compl_func" is TRUE when called from complete_check(). |
| 3207 | */ |
| 3208 | void |
| 3209 | ins_compl_insert(int in_compl_func) |
| 3210 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3211 | ins_bytes(compl_shown_match->cp_str + ins_compl_len()); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3212 | if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3213 | compl_used_match = FALSE; |
| 3214 | else |
| 3215 | compl_used_match = TRUE; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3216 | #ifdef FEAT_EVAL |
| 3217 | { |
| 3218 | dict_T *dict = ins_compl_dict_alloc(compl_shown_match); |
| 3219 | |
| 3220 | set_vim_var_dict(VV_COMPLETED_ITEM, dict); |
| 3221 | } |
| 3222 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3223 | if (!in_compl_func) |
| 3224 | compl_curr_match = compl_shown_match; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * Fill in the next completion in the current direction. |
| 3229 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 3230 | * get more completions. If it is FALSE, then we just do nothing when there |
| 3231 | * are no more completions in a given direction. The latter case is used when |
| 3232 | * we are still in the middle of finding completions, to allow browsing |
| 3233 | * through the ones found so far. |
| 3234 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 3235 | * |
| 3236 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 3237 | * compl_shown_match here. |
| 3238 | * |
| 3239 | * Note that this function may be called recursively once only. First with |
| 3240 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 3241 | * calls this function with "allow_get_expansion" FALSE. |
| 3242 | */ |
| 3243 | static int |
| 3244 | ins_compl_next( |
| 3245 | int allow_get_expansion, |
| 3246 | int count, // repeat completion this many times; should |
| 3247 | // be at least 1 |
| 3248 | int insert_match, // Insert the newly selected match |
| 3249 | int in_compl_func) // called from complete_check() |
| 3250 | { |
| 3251 | int num_matches = -1; |
| 3252 | int todo = count; |
| 3253 | compl_T *found_compl = NULL; |
| 3254 | int found_end = FALSE; |
| 3255 | int advance; |
| 3256 | int started = compl_started; |
| 3257 | |
| 3258 | // When user complete function return -1 for findstart which is next |
| 3259 | // time of 'always', compl_shown_match become NULL. |
| 3260 | if (compl_shown_match == NULL) |
| 3261 | return -1; |
| 3262 | |
| 3263 | if (compl_leader != NULL |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 3264 | && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3265 | { |
| 3266 | // Set "compl_shown_match" to the actually shown match, it may differ |
| 3267 | // when "compl_leader" is used to omit some of the matches. |
| 3268 | while (!ins_compl_equal(compl_shown_match, |
| 3269 | compl_leader, (int)STRLEN(compl_leader)) |
| 3270 | && compl_shown_match->cp_next != NULL |
| 3271 | && compl_shown_match->cp_next != compl_first_match) |
| 3272 | compl_shown_match = compl_shown_match->cp_next; |
| 3273 | |
| 3274 | // If we didn't find it searching forward, and compl_shows_dir is |
| 3275 | // backward, find the last match. |
| 3276 | if (compl_shows_dir == BACKWARD |
| 3277 | && !ins_compl_equal(compl_shown_match, |
| 3278 | compl_leader, (int)STRLEN(compl_leader)) |
| 3279 | && (compl_shown_match->cp_next == NULL |
| 3280 | || compl_shown_match->cp_next == compl_first_match)) |
| 3281 | { |
| 3282 | while (!ins_compl_equal(compl_shown_match, |
| 3283 | compl_leader, (int)STRLEN(compl_leader)) |
| 3284 | && compl_shown_match->cp_prev != NULL |
| 3285 | && compl_shown_match->cp_prev != compl_first_match) |
| 3286 | compl_shown_match = compl_shown_match->cp_prev; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | if (allow_get_expansion && insert_match |
| 3291 | && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
| 3292 | // Delete old text to be replaced |
| 3293 | ins_compl_delete(); |
| 3294 | |
| 3295 | // When finding the longest common text we stick at the original text, |
| 3296 | // don't let CTRL-N or CTRL-P move to the first match. |
| 3297 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 3298 | |
| 3299 | // When restarting the search don't insert the first match either. |
| 3300 | if (compl_restarting) |
| 3301 | { |
| 3302 | advance = FALSE; |
| 3303 | compl_restarting = FALSE; |
| 3304 | } |
| 3305 | |
| 3306 | // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 3307 | // around. |
| 3308 | while (--todo >= 0) |
| 3309 | { |
| 3310 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
| 3311 | { |
| 3312 | compl_shown_match = compl_shown_match->cp_next; |
| 3313 | found_end = (compl_first_match != NULL |
| 3314 | && (compl_shown_match->cp_next == compl_first_match |
| 3315 | || compl_shown_match == compl_first_match)); |
| 3316 | } |
| 3317 | else if (compl_shows_dir == BACKWARD |
| 3318 | && compl_shown_match->cp_prev != NULL) |
| 3319 | { |
| 3320 | found_end = (compl_shown_match == compl_first_match); |
| 3321 | compl_shown_match = compl_shown_match->cp_prev; |
| 3322 | found_end |= (compl_shown_match == compl_first_match); |
| 3323 | } |
| 3324 | else |
| 3325 | { |
| 3326 | if (!allow_get_expansion) |
| 3327 | { |
| 3328 | if (advance) |
| 3329 | { |
| 3330 | if (compl_shows_dir == BACKWARD) |
| 3331 | compl_pending -= todo + 1; |
| 3332 | else |
| 3333 | compl_pending += todo + 1; |
| 3334 | } |
| 3335 | return -1; |
| 3336 | } |
| 3337 | |
| 3338 | if (!compl_no_select && advance) |
| 3339 | { |
| 3340 | if (compl_shows_dir == BACKWARD) |
| 3341 | --compl_pending; |
| 3342 | else |
| 3343 | ++compl_pending; |
| 3344 | } |
| 3345 | |
| 3346 | // Find matches. |
| 3347 | num_matches = ins_compl_get_exp(&compl_startpos); |
| 3348 | |
| 3349 | // handle any pending completions |
| 3350 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
| 3351 | && advance) |
| 3352 | { |
| 3353 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 3354 | { |
| 3355 | compl_shown_match = compl_shown_match->cp_next; |
| 3356 | --compl_pending; |
| 3357 | } |
| 3358 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 3359 | { |
| 3360 | compl_shown_match = compl_shown_match->cp_prev; |
| 3361 | ++compl_pending; |
| 3362 | } |
| 3363 | else |
| 3364 | break; |
| 3365 | } |
| 3366 | found_end = FALSE; |
| 3367 | } |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3368 | if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0 |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3369 | && compl_leader != NULL |
| 3370 | && !ins_compl_equal(compl_shown_match, |
| 3371 | compl_leader, (int)STRLEN(compl_leader))) |
| 3372 | ++todo; |
| 3373 | else |
| 3374 | // Remember a matching item. |
| 3375 | found_compl = compl_shown_match; |
| 3376 | |
| 3377 | // Stop at the end of the list when we found a usable match. |
| 3378 | if (found_end) |
| 3379 | { |
| 3380 | if (found_compl != NULL) |
| 3381 | { |
| 3382 | compl_shown_match = found_compl; |
| 3383 | break; |
| 3384 | } |
| 3385 | todo = 1; // use first usable match after wrapping around |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | // Insert the text of the new completion, or the compl_leader. |
| 3390 | if (compl_no_insert && !started) |
| 3391 | { |
| 3392 | ins_bytes(compl_orig_text + ins_compl_len()); |
| 3393 | compl_used_match = FALSE; |
| 3394 | } |
| 3395 | else if (insert_match) |
| 3396 | { |
| 3397 | if (!compl_get_longest || compl_used_match) |
| 3398 | ins_compl_insert(in_compl_func); |
| 3399 | else |
| 3400 | ins_bytes(compl_leader + ins_compl_len()); |
| 3401 | } |
| 3402 | else |
| 3403 | compl_used_match = FALSE; |
| 3404 | |
| 3405 | if (!allow_get_expansion) |
| 3406 | { |
| 3407 | // may undisplay the popup menu first |
| 3408 | ins_compl_upd_pum(); |
| 3409 | |
| 3410 | if (pum_enough_matches()) |
| 3411 | // Will display the popup menu, don't redraw yet to avoid flicker. |
| 3412 | pum_call_update_screen(); |
| 3413 | else |
| 3414 | // Not showing the popup menu yet, redraw to show the user what was |
| 3415 | // inserted. |
| 3416 | update_screen(0); |
| 3417 | |
| 3418 | // display the updated popup menu |
| 3419 | ins_compl_show_pum(); |
| 3420 | #ifdef FEAT_GUI |
| 3421 | if (gui.in_use) |
| 3422 | { |
| 3423 | // Show the cursor after the match, not after the redrawn text. |
| 3424 | setcursor(); |
| 3425 | out_flush_cursor(FALSE, FALSE); |
| 3426 | } |
| 3427 | #endif |
| 3428 | |
| 3429 | // Delete old text to be replaced, since we're still searching and |
| 3430 | // don't want to match ourselves! |
| 3431 | ins_compl_delete(); |
| 3432 | } |
| 3433 | |
| 3434 | // Enter will select a match when the match wasn't inserted and the popup |
| 3435 | // menu is visible. |
| 3436 | if (compl_no_insert && !started) |
| 3437 | compl_enter_selects = TRUE; |
| 3438 | else |
| 3439 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 3440 | |
| 3441 | // Show the file name for the match (if any) |
| 3442 | // Truncate the file name to avoid a wait for return. |
| 3443 | if (compl_shown_match->cp_fname != NULL) |
| 3444 | { |
| 3445 | char *lead = _("match in file"); |
| 3446 | int space = sc_col - vim_strsize((char_u *)lead) - 2; |
| 3447 | char_u *s; |
| 3448 | char_u *e; |
| 3449 | |
| 3450 | if (space > 0) |
| 3451 | { |
| 3452 | // We need the tail that fits. With double-byte encoding going |
| 3453 | // back from the end is very slow, thus go from the start and keep |
| 3454 | // the text that fits in "space" between "s" and "e". |
| 3455 | for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e)) |
| 3456 | { |
| 3457 | space -= ptr2cells(e); |
| 3458 | while (space < 0) |
| 3459 | { |
| 3460 | space += ptr2cells(s); |
| 3461 | MB_PTR_ADV(s); |
| 3462 | } |
| 3463 | } |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 3464 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3465 | vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, |
| 3466 | s > compl_shown_match->cp_fname ? "<" : "", s); |
| 3467 | msg((char *)IObuff); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 3468 | msg_hist_off = FALSE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3469 | redraw_cmdline = FALSE; // don't overwrite! |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | return num_matches; |
| 3474 | } |
| 3475 | |
| 3476 | /* |
| 3477 | * Call this while finding completions, to check whether the user has hit a key |
| 3478 | * that should change the currently displayed completion, or exit completion |
| 3479 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
| 3480 | * possible. -- webb |
| 3481 | * "frequency" specifies out of how many calls we actually check. |
| 3482 | * "in_compl_func" is TRUE when called from complete_check(), don't set |
| 3483 | * compl_curr_match. |
| 3484 | */ |
| 3485 | void |
| 3486 | ins_compl_check_keys(int frequency, int in_compl_func) |
| 3487 | { |
| 3488 | static int count = 0; |
| 3489 | int c; |
| 3490 | |
| 3491 | // Don't check when reading keys from a script, :normal or feedkeys(). |
| 3492 | // That would break the test scripts. But do check for keys when called |
| 3493 | // from complete_check(). |
| 3494 | if (!in_compl_func && (using_script() || ex_normal_busy)) |
| 3495 | return; |
| 3496 | |
| 3497 | // Only do this at regular intervals |
| 3498 | if (++count < frequency) |
| 3499 | return; |
| 3500 | count = 0; |
| 3501 | |
| 3502 | // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 3503 | // can't do its work correctly. |
| 3504 | c = vpeekc_any(); |
| 3505 | if (c != NUL) |
| 3506 | { |
| 3507 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 3508 | { |
| 3509 | c = safe_vgetc(); // Eat the character |
| 3510 | compl_shows_dir = ins_compl_key2dir(c); |
| 3511 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 3512 | c != K_UP && c != K_DOWN, in_compl_func); |
| 3513 | } |
| 3514 | else |
| 3515 | { |
| 3516 | // Need to get the character to have KeyTyped set. We'll put it |
| 3517 | // back with vungetc() below. But skip K_IGNORE. |
| 3518 | c = safe_vgetc(); |
| 3519 | if (c != K_IGNORE) |
| 3520 | { |
| 3521 | // Don't interrupt completion when the character wasn't typed, |
| 3522 | // e.g., when doing @q to replay keys. |
| 3523 | if (c != Ctrl_R && KeyTyped) |
| 3524 | compl_interrupted = TRUE; |
| 3525 | |
| 3526 | vungetc(c); |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | if (compl_pending != 0 && !got_int && !compl_no_insert) |
| 3531 | { |
| 3532 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 3533 | |
| 3534 | compl_pending = 0; |
| 3535 | (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func); |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | /* |
| 3540 | * Decide the direction of Insert mode complete from the key typed. |
| 3541 | * Returns BACKWARD or FORWARD. |
| 3542 | */ |
| 3543 | static int |
| 3544 | ins_compl_key2dir(int c) |
| 3545 | { |
| 3546 | if (c == Ctrl_P || c == Ctrl_L |
| 3547 | || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP) |
| 3548 | return BACKWARD; |
| 3549 | return FORWARD; |
| 3550 | } |
| 3551 | |
| 3552 | /* |
| 3553 | * Return TRUE for keys that are used for completion only when the popup menu |
| 3554 | * is visible. |
| 3555 | */ |
| 3556 | static int |
| 3557 | ins_compl_pum_key(int c) |
| 3558 | { |
| 3559 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
| 3560 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 3561 | || c == K_UP || c == K_DOWN); |
| 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | * Decide the number of completions to move forward. |
| 3566 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 3567 | */ |
| 3568 | static int |
| 3569 | ins_compl_key2count(int c) |
| 3570 | { |
| 3571 | int h; |
| 3572 | |
| 3573 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
| 3574 | { |
| 3575 | h = pum_get_height(); |
| 3576 | if (h > 3) |
| 3577 | h -= 2; // keep some context |
| 3578 | return h; |
| 3579 | } |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
| 3583 | /* |
| 3584 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 3585 | * to change the currently selected completion. |
| 3586 | */ |
| 3587 | static int |
| 3588 | ins_compl_use_match(int c) |
| 3589 | { |
| 3590 | switch (c) |
| 3591 | { |
| 3592 | case K_UP: |
| 3593 | case K_DOWN: |
| 3594 | case K_PAGEDOWN: |
| 3595 | case K_KPAGEDOWN: |
| 3596 | case K_S_DOWN: |
| 3597 | case K_PAGEUP: |
| 3598 | case K_KPAGEUP: |
| 3599 | case K_S_UP: |
| 3600 | return FALSE; |
| 3601 | } |
| 3602 | return TRUE; |
| 3603 | } |
| 3604 | |
| 3605 | /* |
| 3606 | * Do Insert mode completion. |
| 3607 | * Called when character "c" was typed, which has a meaning for completion. |
| 3608 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 3609 | */ |
| 3610 | int |
| 3611 | ins_complete(int c, int enable_pum) |
| 3612 | { |
| 3613 | char_u *line; |
| 3614 | int startcol = 0; // column where searched text starts |
| 3615 | colnr_T curs_col; // cursor column |
| 3616 | int n; |
| 3617 | int save_w_wrow; |
| 3618 | int save_w_leftcol; |
| 3619 | int insert_match; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3620 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3621 | int save_did_ai = did_ai; |
Bram Moolenaar | 9cb698d | 2019-08-21 15:30:45 +0200 | [diff] [blame] | 3622 | #endif |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3623 | int flags = CP_ORIGINAL_TEXT; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3624 | |
| 3625 | compl_direction = ins_compl_key2dir(c); |
| 3626 | insert_match = ins_compl_use_match(c); |
| 3627 | |
| 3628 | if (!compl_started) |
| 3629 | { |
| 3630 | // First time we hit ^N or ^P (in a row, I mean) |
| 3631 | |
| 3632 | did_ai = FALSE; |
| 3633 | #ifdef FEAT_SMARTINDENT |
| 3634 | did_si = FALSE; |
| 3635 | can_si = FALSE; |
| 3636 | can_si_back = FALSE; |
| 3637 | #endif |
| 3638 | if (stop_arrow() == FAIL) |
| 3639 | return FAIL; |
| 3640 | |
| 3641 | line = ml_get(curwin->w_cursor.lnum); |
| 3642 | curs_col = curwin->w_cursor.col; |
| 3643 | compl_pending = 0; |
| 3644 | |
| 3645 | // If this same ctrl_x_mode has been interrupted use the text from |
| 3646 | // "compl_startpos" to the cursor as a pattern to add a new word |
| 3647 | // instead of expand the one before the cursor, in word-wise if |
| 3648 | // "compl_startpos" is not in the same line as the cursor then fix it |
| 3649 | // (the line has been split because it was longer than 'tw'). if SOL |
| 3650 | // is set then skip the previous pattern, a word at the beginning of |
| 3651 | // the line has been inserted, we'll look for that -- Acevedo. |
| 3652 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 3653 | && compl_cont_mode == ctrl_x_mode) |
| 3654 | { |
| 3655 | // it is a continued search |
| 3656 | compl_cont_status &= ~CONT_INTRPT; // remove INTRPT |
| 3657 | if (ctrl_x_mode == CTRL_X_NORMAL |
| 3658 | || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 3659 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 3660 | { |
| 3661 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
| 3662 | { |
| 3663 | // line (probably) wrapped, set compl_startpos to the |
| 3664 | // first non_blank in the line, if it is not a wordchar |
| 3665 | // include it to get a better pattern, but then we don't |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3666 | // want the "\\<" prefix, check it below |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3667 | compl_col = (colnr_T)getwhitecols(line); |
| 3668 | compl_startpos.col = compl_col; |
| 3669 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 3670 | compl_cont_status &= ~CONT_SOL; // clear SOL if present |
| 3671 | } |
| 3672 | else |
| 3673 | { |
| 3674 | // S_IPOS was set when we inserted a word that was at the |
| 3675 | // beginning of the line, which means that we'll go to SOL |
| 3676 | // mode but first we need to redefine compl_startpos |
| 3677 | if (compl_cont_status & CONT_S_IPOS) |
| 3678 | { |
| 3679 | compl_cont_status |= CONT_SOL; |
| 3680 | compl_startpos.col = (colnr_T)(skipwhite( |
| 3681 | line + compl_length |
| 3682 | + compl_startpos.col) - line); |
| 3683 | } |
| 3684 | compl_col = compl_startpos.col; |
| 3685 | } |
| 3686 | compl_length = curwin->w_cursor.col - (int)compl_col; |
| 3687 | // IObuff is used to add a "word from the next line" would we |
| 3688 | // have enough space? just being paranoid |
| 3689 | #define MIN_SPACE 75 |
| 3690 | if (compl_length > (IOSIZE - MIN_SPACE)) |
| 3691 | { |
| 3692 | compl_cont_status &= ~CONT_SOL; |
| 3693 | compl_length = (IOSIZE - MIN_SPACE); |
| 3694 | compl_col = curwin->w_cursor.col - compl_length; |
| 3695 | } |
| 3696 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 3697 | if (compl_length < 1) |
| 3698 | compl_cont_status &= CONT_LOCAL; |
| 3699 | } |
| 3700 | else if (ctrl_x_mode_line_or_eval()) |
| 3701 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
| 3702 | else |
| 3703 | compl_cont_status = 0; |
| 3704 | } |
| 3705 | else |
| 3706 | compl_cont_status &= CONT_LOCAL; |
| 3707 | |
| 3708 | if (!(compl_cont_status & CONT_ADDING)) // normal expansion |
| 3709 | { |
| 3710 | compl_cont_mode = ctrl_x_mode; |
| 3711 | if (ctrl_x_mode != CTRL_X_NORMAL) |
| 3712 | // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL |
| 3713 | compl_cont_status = 0; |
| 3714 | compl_cont_status |= CONT_N_ADDS; |
| 3715 | compl_startpos = curwin->w_cursor; |
| 3716 | startcol = (int)curs_col; |
| 3717 | compl_col = 0; |
| 3718 | } |
| 3719 | |
| 3720 | // Work out completion pattern and original text -- webb |
| 3721 | if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 3722 | { |
| 3723 | if ((compl_cont_status & CONT_SOL) |
| 3724 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 3725 | { |
| 3726 | if (!(compl_cont_status & CONT_ADDING)) |
| 3727 | { |
| 3728 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
| 3729 | ; |
| 3730 | compl_col += ++startcol; |
| 3731 | compl_length = curs_col - startcol; |
| 3732 | } |
| 3733 | if (p_ic) |
| 3734 | compl_pattern = str_foldcase(line + compl_col, |
| 3735 | compl_length, NULL, 0); |
| 3736 | else |
| 3737 | compl_pattern = vim_strnsave(line + compl_col, |
| 3738 | compl_length); |
| 3739 | if (compl_pattern == NULL) |
| 3740 | return FAIL; |
| 3741 | } |
| 3742 | else if (compl_cont_status & CONT_ADDING) |
| 3743 | { |
| 3744 | char_u *prefix = (char_u *)"\\<"; |
| 3745 | |
| 3746 | // we need up to 2 extra chars for the prefix |
| 3747 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 3748 | compl_length) + 2); |
| 3749 | if (compl_pattern == NULL) |
| 3750 | return FAIL; |
| 3751 | if (!vim_iswordp(line + compl_col) |
| 3752 | || (compl_col > 0 |
| 3753 | && (vim_iswordp(mb_prevptr(line, line + compl_col))))) |
| 3754 | prefix = (char_u *)""; |
| 3755 | STRCPY((char *)compl_pattern, prefix); |
| 3756 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 3757 | line + compl_col, compl_length); |
| 3758 | } |
| 3759 | else if (--startcol < 0 |
| 3760 | || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) |
| 3761 | { |
| 3762 | // Match any word of at least two chars |
| 3763 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 3764 | if (compl_pattern == NULL) |
| 3765 | return FAIL; |
| 3766 | compl_col += curs_col; |
| 3767 | compl_length = 0; |
| 3768 | } |
| 3769 | else |
| 3770 | { |
| 3771 | // Search the point of change class of multibyte character |
| 3772 | // or not a word single byte character backward. |
| 3773 | if (has_mbyte) |
| 3774 | { |
| 3775 | int base_class; |
| 3776 | int head_off; |
| 3777 | |
| 3778 | startcol -= (*mb_head_off)(line, line + startcol); |
| 3779 | base_class = mb_get_class(line + startcol); |
| 3780 | while (--startcol >= 0) |
| 3781 | { |
| 3782 | head_off = (*mb_head_off)(line, line + startcol); |
| 3783 | if (base_class != mb_get_class(line + startcol |
| 3784 | - head_off)) |
| 3785 | break; |
| 3786 | startcol -= head_off; |
| 3787 | } |
| 3788 | } |
| 3789 | else |
| 3790 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
| 3791 | ; |
| 3792 | compl_col += ++startcol; |
| 3793 | compl_length = (int)curs_col - startcol; |
| 3794 | if (compl_length == 1) |
| 3795 | { |
| 3796 | // Only match word with at least two chars -- webb |
| 3797 | // there's no need to call quote_meta, |
| 3798 | // alloc(7) is enough -- Acevedo |
| 3799 | compl_pattern = alloc(7); |
| 3800 | if (compl_pattern == NULL) |
| 3801 | return FAIL; |
| 3802 | STRCPY((char *)compl_pattern, "\\<"); |
| 3803 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 3804 | STRCAT((char *)compl_pattern, "\\k"); |
| 3805 | } |
| 3806 | else |
| 3807 | { |
| 3808 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 3809 | compl_length) + 2); |
| 3810 | if (compl_pattern == NULL) |
| 3811 | return FAIL; |
| 3812 | STRCPY((char *)compl_pattern, "\\<"); |
| 3813 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 3814 | compl_length); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | else if (ctrl_x_mode_line_or_eval()) |
| 3819 | { |
| 3820 | compl_col = (colnr_T)getwhitecols(line); |
| 3821 | compl_length = (int)curs_col - (int)compl_col; |
| 3822 | if (compl_length < 0) // cursor in indent: empty pattern |
| 3823 | compl_length = 0; |
| 3824 | if (p_ic) |
| 3825 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 3826 | NULL, 0); |
| 3827 | else |
| 3828 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3829 | if (compl_pattern == NULL) |
| 3830 | return FAIL; |
| 3831 | } |
| 3832 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 3833 | { |
| 3834 | // Go back to just before the first filename character. |
| 3835 | if (startcol > 0) |
| 3836 | { |
| 3837 | char_u *p = line + startcol; |
| 3838 | |
| 3839 | MB_PTR_BACK(line, p); |
| 3840 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 3841 | MB_PTR_BACK(line, p); |
| 3842 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 3843 | startcol = 0; |
| 3844 | else |
| 3845 | startcol = (int)(p - line) + 1; |
| 3846 | } |
| 3847 | |
| 3848 | compl_col += startcol; |
| 3849 | compl_length = (int)curs_col - startcol; |
| 3850 | compl_pattern = addstar(line + compl_col, compl_length, |
| 3851 | EXPAND_FILES); |
| 3852 | if (compl_pattern == NULL) |
| 3853 | return FAIL; |
| 3854 | } |
| 3855 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 3856 | { |
| 3857 | compl_pattern = vim_strnsave(line, curs_col); |
| 3858 | if (compl_pattern == NULL) |
| 3859 | return FAIL; |
| 3860 | set_cmd_context(&compl_xp, compl_pattern, |
| 3861 | (int)STRLEN(compl_pattern), curs_col, FALSE); |
| 3862 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 3863 | || compl_xp.xp_context == EXPAND_NOTHING) |
| 3864 | // No completion possible, use an empty pattern to get a |
| 3865 | // "pattern not found" message. |
| 3866 | compl_col = curs_col; |
| 3867 | else |
| 3868 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
| 3869 | compl_length = curs_col - compl_col; |
| 3870 | } |
| 3871 | else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
| 3872 | { |
| 3873 | #ifdef FEAT_COMPL_FUNC |
| 3874 | // Call user defined function 'completefunc' with "a:findstart" |
| 3875 | // set to 1 to obtain the length of text to use for completion. |
| 3876 | typval_T args[3]; |
| 3877 | int col; |
| 3878 | char_u *funcname; |
| 3879 | pos_T pos; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3880 | int save_State = State; |
| 3881 | |
| 3882 | // Call 'completefunc' or 'omnifunc' and get pattern length as a |
| 3883 | // string |
| 3884 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 3885 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3886 | if (*funcname == NUL) |
| 3887 | { |
| 3888 | semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 3889 | ? "completefunc" : "omnifunc"); |
| 3890 | // restore did_ai, so that adding comment leader works |
| 3891 | did_ai = save_did_ai; |
| 3892 | return FAIL; |
| 3893 | } |
| 3894 | |
| 3895 | args[0].v_type = VAR_NUMBER; |
| 3896 | args[0].vval.v_number = 1; |
| 3897 | args[1].v_type = VAR_STRING; |
| 3898 | args[1].vval.v_string = (char_u *)""; |
| 3899 | args[2].v_type = VAR_UNKNOWN; |
| 3900 | pos = curwin->w_cursor; |
Bram Moolenaar | 3eb6bd9 | 2021-01-29 21:47:24 +0100 | [diff] [blame] | 3901 | ++textwinlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3902 | col = call_func_retnr(funcname, 2, args); |
Bram Moolenaar | 3eb6bd9 | 2021-01-29 21:47:24 +0100 | [diff] [blame] | 3903 | --textwinlock; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3904 | |
| 3905 | State = save_State; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3906 | curwin->w_cursor = pos; // restore the cursor position |
| 3907 | validate_cursor(); |
| 3908 | if (!EQUAL_POS(curwin->w_cursor, pos)) |
| 3909 | { |
| 3910 | emsg(_(e_compldel)); |
| 3911 | return FAIL; |
| 3912 | } |
| 3913 | |
| 3914 | // Return value -2 means the user complete function wants to |
| 3915 | // cancel the complete without an error. |
| 3916 | // Return value -3 does the same as -2 and leaves CTRL-X mode. |
| 3917 | if (col == -2) |
| 3918 | return FAIL; |
| 3919 | if (col == -3) |
| 3920 | { |
| 3921 | ctrl_x_mode = CTRL_X_NORMAL; |
| 3922 | edit_submode = NULL; |
| 3923 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 3924 | msg_clr_cmdline(); |
| 3925 | return FAIL; |
| 3926 | } |
| 3927 | |
| 3928 | // Reset extended parameters of completion, when start new |
| 3929 | // completion. |
| 3930 | compl_opt_refresh_always = FALSE; |
| 3931 | compl_opt_suppress_empty = FALSE; |
| 3932 | |
| 3933 | if (col < 0) |
| 3934 | col = curs_col; |
| 3935 | compl_col = col; |
| 3936 | if (compl_col > curs_col) |
| 3937 | compl_col = curs_col; |
| 3938 | |
| 3939 | // Setup variables for completion. Need to obtain "line" again, |
| 3940 | // it may have become invalid. |
| 3941 | line = ml_get(curwin->w_cursor.lnum); |
| 3942 | compl_length = curs_col - compl_col; |
| 3943 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3944 | if (compl_pattern == NULL) |
| 3945 | #endif |
| 3946 | return FAIL; |
| 3947 | } |
| 3948 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 3949 | { |
| 3950 | #ifdef FEAT_SPELL |
| 3951 | if (spell_bad_len > 0) |
| 3952 | compl_col = curs_col - spell_bad_len; |
| 3953 | else |
| 3954 | compl_col = spell_word_start(startcol); |
| 3955 | if (compl_col >= (colnr_T)startcol) |
| 3956 | { |
| 3957 | compl_length = 0; |
| 3958 | compl_col = curs_col; |
| 3959 | } |
| 3960 | else |
| 3961 | { |
| 3962 | spell_expand_check_cap(compl_col); |
| 3963 | compl_length = (int)curs_col - compl_col; |
| 3964 | } |
| 3965 | // Need to obtain "line" again, it may have become invalid. |
| 3966 | line = ml_get(curwin->w_cursor.lnum); |
| 3967 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3968 | if (compl_pattern == NULL) |
| 3969 | #endif |
| 3970 | return FAIL; |
| 3971 | } |
| 3972 | else |
| 3973 | { |
| 3974 | internal_error("ins_complete()"); |
| 3975 | return FAIL; |
| 3976 | } |
| 3977 | |
| 3978 | if (compl_cont_status & CONT_ADDING) |
| 3979 | { |
| 3980 | edit_submode_pre = (char_u *)_(" Adding"); |
| 3981 | if (ctrl_x_mode_line_or_eval()) |
| 3982 | { |
| 3983 | // Insert a new line, keep indentation but ignore 'comments' |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3984 | char_u *old = curbuf->b_p_com; |
| 3985 | |
| 3986 | curbuf->b_p_com = (char_u *)""; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3987 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 3988 | compl_startpos.col = compl_col; |
| 3989 | ins_eol('\r'); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3990 | curbuf->b_p_com = old; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3991 | compl_length = 0; |
| 3992 | compl_col = curwin->w_cursor.col; |
| 3993 | } |
| 3994 | } |
| 3995 | else |
| 3996 | { |
| 3997 | edit_submode_pre = NULL; |
| 3998 | compl_startpos.col = compl_col; |
| 3999 | } |
| 4000 | |
| 4001 | if (compl_cont_status & CONT_LOCAL) |
| 4002 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
| 4003 | else |
| 4004 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 4005 | |
| 4006 | // If any of the original typed text has been changed we need to fix |
| 4007 | // the redo buffer. |
| 4008 | ins_compl_fixRedoBufForLeader(NULL); |
| 4009 | |
| 4010 | // Always add completion for the original text. |
| 4011 | vim_free(compl_orig_text); |
| 4012 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 4013 | if (p_ic) |
| 4014 | flags |= CP_ICASE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4015 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 4016 | -1, NULL, NULL, NULL, 0, flags, FALSE) != OK) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4017 | { |
| 4018 | VIM_CLEAR(compl_pattern); |
| 4019 | VIM_CLEAR(compl_orig_text); |
| 4020 | return FAIL; |
| 4021 | } |
| 4022 | |
| 4023 | // showmode might reset the internal line pointers, so it must |
| 4024 | // be called before line = ml_get(), or when this address is no |
| 4025 | // longer needed. -- Acevedo. |
| 4026 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 4027 | edit_submode_highl = HLF_COUNT; |
| 4028 | showmode(); |
| 4029 | edit_submode_extra = NULL; |
| 4030 | out_flush(); |
| 4031 | } |
| 4032 | else if (insert_match && stop_arrow() == FAIL) |
| 4033 | return FAIL; |
| 4034 | |
| 4035 | compl_shown_match = compl_curr_match; |
| 4036 | compl_shows_dir = compl_direction; |
| 4037 | |
| 4038 | // Find next match (and following matches). |
| 4039 | save_w_wrow = curwin->w_wrow; |
| 4040 | save_w_leftcol = curwin->w_leftcol; |
| 4041 | n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); |
| 4042 | |
| 4043 | // may undisplay the popup menu |
| 4044 | ins_compl_upd_pum(); |
| 4045 | |
| 4046 | if (n > 1) // all matches have been found |
| 4047 | compl_matches = n; |
| 4048 | compl_curr_match = compl_shown_match; |
| 4049 | compl_direction = compl_shows_dir; |
| 4050 | |
| 4051 | // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 4052 | // mode. |
| 4053 | if (got_int && !global_busy) |
| 4054 | { |
| 4055 | (void)vgetc(); |
| 4056 | got_int = FALSE; |
| 4057 | } |
| 4058 | |
| 4059 | // we found no match if the list has only the "compl_orig_text"-entry |
| 4060 | if (compl_first_match == compl_first_match->cp_next) |
| 4061 | { |
| 4062 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 4063 | && compl_length > 1 |
| 4064 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 4065 | edit_submode_highl = HLF_E; |
| 4066 | // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 4067 | // because we couldn't expand anything at first place, but if we used |
| 4068 | // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 4069 | // (such as M in M'exico) if not tried already. -- Acevedo |
| 4070 | if ( compl_length > 1 |
| 4071 | || (compl_cont_status & CONT_ADDING) |
| 4072 | || (ctrl_x_mode != CTRL_X_NORMAL |
| 4073 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 4074 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
| 4075 | compl_cont_status &= ~CONT_N_ADDS; |
| 4076 | } |
| 4077 | |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 4078 | if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4079 | compl_cont_status |= CONT_S_IPOS; |
| 4080 | else |
| 4081 | compl_cont_status &= ~CONT_S_IPOS; |
| 4082 | |
| 4083 | if (edit_submode_extra == NULL) |
| 4084 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 4085 | if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4086 | { |
| 4087 | edit_submode_extra = (char_u *)_("Back at original"); |
| 4088 | edit_submode_highl = HLF_W; |
| 4089 | } |
| 4090 | else if (compl_cont_status & CONT_S_IPOS) |
| 4091 | { |
| 4092 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 4093 | edit_submode_highl = HLF_COUNT; |
| 4094 | } |
| 4095 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
| 4096 | { |
| 4097 | edit_submode_extra = (char_u *)_("The only match"); |
| 4098 | edit_submode_highl = HLF_COUNT; |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 4099 | compl_curr_match->cp_number = 1; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4100 | } |
| 4101 | else |
| 4102 | { |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 4103 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4104 | // Update completion sequence number when needed. |
| 4105 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 4106 | ins_compl_update_sequence_numbers(); |
Bram Moolenaar | 977fd0b | 2020-10-27 09:12:45 +0100 | [diff] [blame] | 4107 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4108 | // The match should always have a sequence number now, this is |
| 4109 | // just a safety check. |
| 4110 | if (compl_curr_match->cp_number != -1) |
| 4111 | { |
| 4112 | // Space for 10 text chars. + 2x10-digit no.s = 31. |
| 4113 | // Translations may need more than twice that. |
| 4114 | static char_u match_ref[81]; |
| 4115 | |
| 4116 | if (compl_matches > 0) |
| 4117 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 4118 | _("match %d of %d"), |
| 4119 | compl_curr_match->cp_number, compl_matches); |
| 4120 | else |
| 4121 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 4122 | _("match %d"), |
| 4123 | compl_curr_match->cp_number); |
| 4124 | edit_submode_extra = match_ref; |
| 4125 | edit_submode_highl = HLF_R; |
| 4126 | if (dollar_vcol >= 0) |
| 4127 | curs_columns(FALSE); |
| 4128 | } |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | // Show a message about what (completion) mode we're in. |
| 4133 | if (!compl_opt_suppress_empty) |
| 4134 | { |
| 4135 | showmode(); |
| 4136 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 4137 | { |
| 4138 | if (edit_submode_extra != NULL) |
| 4139 | { |
| 4140 | if (!p_smd) |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 4141 | { |
| 4142 | msg_hist_off = TRUE; |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4143 | msg_attr((char *)edit_submode_extra, |
| 4144 | edit_submode_highl < HLF_COUNT |
| 4145 | ? HL_ATTR(edit_submode_highl) : 0); |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 4146 | msg_hist_off = FALSE; |
| 4147 | } |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4148 | } |
| 4149 | else |
| 4150 | msg_clr_cmdline(); // necessary for "noshowmode" |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | // Show the popup menu, unless we got interrupted. |
| 4155 | if (enable_pum && !compl_interrupted) |
| 4156 | show_pum(save_w_wrow, save_w_leftcol); |
| 4157 | |
| 4158 | compl_was_interrupted = compl_interrupted; |
| 4159 | compl_interrupted = FALSE; |
| 4160 | |
| 4161 | return OK; |
| 4162 | } |
| 4163 | |
| 4164 | static void |
| 4165 | show_pum(int prev_w_wrow, int prev_w_leftcol) |
| 4166 | { |
| 4167 | // RedrawingDisabled may be set when invoked through complete(). |
| 4168 | int n = RedrawingDisabled; |
| 4169 | |
| 4170 | RedrawingDisabled = 0; |
| 4171 | |
| 4172 | // If the cursor moved or the display scrolled we need to remove the pum |
| 4173 | // first. |
| 4174 | setcursor(); |
| 4175 | if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) |
| 4176 | ins_compl_del_pum(); |
| 4177 | |
| 4178 | ins_compl_show_pum(); |
| 4179 | setcursor(); |
| 4180 | RedrawingDisabled = n; |
| 4181 | } |
| 4182 | |
| 4183 | /* |
| 4184 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 4185 | * If dest is not NULL the chars. are copied there quoting (with |
| 4186 | * a backslash) the metachars, and dest would be NUL terminated. |
| 4187 | * Returns the length (needed) of dest |
| 4188 | */ |
| 4189 | static unsigned |
| 4190 | quote_meta(char_u *dest, char_u *src, int len) |
| 4191 | { |
| 4192 | unsigned m = (unsigned)len + 1; // one extra for the NUL |
| 4193 | |
| 4194 | for ( ; --len >= 0; src++) |
| 4195 | { |
| 4196 | switch (*src) |
| 4197 | { |
| 4198 | case '.': |
| 4199 | case '*': |
| 4200 | case '[': |
| 4201 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4202 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4203 | break; |
| 4204 | // FALLTHROUGH |
| 4205 | case '~': |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 4206 | if (!magic_isset()) // quote these only if magic is set |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4207 | break; |
| 4208 | // FALLTHROUGH |
| 4209 | case '\\': |
| 4210 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4211 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4212 | break; |
| 4213 | // FALLTHROUGH |
| 4214 | case '^': // currently it's not needed. |
| 4215 | case '$': |
| 4216 | m++; |
| 4217 | if (dest != NULL) |
| 4218 | *dest++ = '\\'; |
| 4219 | break; |
| 4220 | } |
| 4221 | if (dest != NULL) |
| 4222 | *dest++ = *src; |
| 4223 | // Copy remaining bytes of a multibyte character. |
| 4224 | if (has_mbyte) |
| 4225 | { |
| 4226 | int i, mb_len; |
| 4227 | |
| 4228 | mb_len = (*mb_ptr2len)(src) - 1; |
| 4229 | if (mb_len > 0 && len >= mb_len) |
| 4230 | for (i = 0; i < mb_len; ++i) |
| 4231 | { |
| 4232 | --len; |
| 4233 | ++src; |
| 4234 | if (dest != NULL) |
| 4235 | *dest++ = *src; |
| 4236 | } |
| 4237 | } |
| 4238 | } |
| 4239 | if (dest != NULL) |
| 4240 | *dest = NUL; |
| 4241 | |
| 4242 | return m; |
| 4243 | } |
| 4244 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 4245 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4246 | void |
| 4247 | free_insexpand_stuff(void) |
| 4248 | { |
| 4249 | VIM_CLEAR(compl_orig_text); |
| 4250 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 4251 | #endif |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4252 | |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 4253 | #ifdef FEAT_SPELL |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4254 | /* |
| 4255 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 4256 | * spelled word, if there is one. |
| 4257 | */ |
| 4258 | static void |
| 4259 | spell_back_to_badword(void) |
| 4260 | { |
| 4261 | pos_T tpos = curwin->w_cursor; |
| 4262 | |
| 4263 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
| 4264 | if (curwin->w_cursor.col != tpos.col) |
| 4265 | start_arrow(&tpos); |
| 4266 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 4267 | #endif |