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