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