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