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