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