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