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