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