Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [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 | * cmdexpand.c: functions for command-line completion |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | static int cmd_showtail; // Only show path tail in lists ? |
| 17 | |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 18 | static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 19 | char_u ***matches, int *numMatches, |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 20 | char_u *((*func)(expand_T *, int)), int escaped); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 21 | static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int); |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 22 | static char_u *showmatches_gettail(char_u *s); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 23 | static int expand_showtail(expand_T *xp); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 24 | static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 25 | #if defined(FEAT_EVAL) |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 26 | static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 27 | static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 30 | // "compl_match_array" points the currently displayed list of entries in the |
| 31 | // popup menu. It is NULL when there is no popup menu. |
| 32 | static pumitem_T *compl_match_array = NULL; |
| 33 | static int compl_match_arraysize; |
| 34 | // First column in cmdline of the matched item for completion. |
| 35 | static int compl_startcol; |
| 36 | static int compl_selected; |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 37 | |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 38 | #define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m]) |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 39 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 40 | /* |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 41 | * Returns TRUE if fuzzy completion is supported for a given cmdline completion |
| 42 | * context. |
| 43 | */ |
| 44 | static int |
| 45 | cmdline_fuzzy_completion_supported(expand_T *xp) |
| 46 | { |
| 47 | return (vim_strchr(p_wop, WOP_FUZZY) != NULL |
| 48 | && xp->xp_context != EXPAND_BOOL_SETTINGS |
| 49 | && xp->xp_context != EXPAND_COLORS |
| 50 | && xp->xp_context != EXPAND_COMPILER |
| 51 | && xp->xp_context != EXPAND_DIRECTORIES |
| 52 | && xp->xp_context != EXPAND_FILES |
| 53 | && xp->xp_context != EXPAND_FILES_IN_PATH |
| 54 | && xp->xp_context != EXPAND_FILETYPE |
| 55 | && xp->xp_context != EXPAND_HELP |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 56 | && xp->xp_context != EXPAND_OLD_SETTING |
| 57 | && xp->xp_context != EXPAND_OWNSYNTAX |
| 58 | && xp->xp_context != EXPAND_PACKADD |
root | a675938 | 2023-01-21 21:56:06 +0000 | [diff] [blame] | 59 | && xp->xp_context != EXPAND_RUNTIME |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 60 | && xp->xp_context != EXPAND_SHELLCMD |
| 61 | && xp->xp_context != EXPAND_TAGS |
| 62 | && xp->xp_context != EXPAND_TAGS_LISTFILES |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 63 | && xp->xp_context != EXPAND_USER_LIST); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Returns TRUE if fuzzy completion for cmdline completion is enabled and |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 68 | * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy |
| 69 | * matching. |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 70 | */ |
| 71 | int |
| 72 | cmdline_fuzzy_complete(char_u *fuzzystr) |
| 73 | { |
| 74 | return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL; |
| 75 | } |
| 76 | |
| 77 | /* |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 78 | * sort function for the completion matches. |
| 79 | * <SNR> functions should be sorted to the end. |
| 80 | */ |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 81 | static int |
| 82 | sort_func_compare(const void *s1, const void *s2) |
| 83 | { |
| 84 | char_u *p1 = *(char_u **)s1; |
| 85 | char_u *p2 = *(char_u **)s2; |
| 86 | |
| 87 | if (*p1 != '<' && *p2 == '<') return -1; |
| 88 | if (*p1 == '<' && *p2 != '<') return 1; |
| 89 | return STRCMP(p1, p2); |
| 90 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 91 | |
Yegappan Lakshmanan | 4d03d87 | 2022-02-13 11:45:09 +0000 | [diff] [blame] | 92 | /* |
| 93 | * Escape special characters in the cmdline completion matches. |
| 94 | */ |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 95 | static void |
Yegappan Lakshmanan | 68353e5 | 2022-11-13 22:38:10 +0000 | [diff] [blame] | 96 | wildescape( |
| 97 | expand_T *xp, |
| 98 | char_u *str, |
| 99 | int numfiles, |
| 100 | char_u **files) |
| 101 | { |
| 102 | char_u *p; |
| 103 | int vse_what = xp->xp_context == EXPAND_BUFFERS |
| 104 | ? VSE_BUFFER : VSE_NONE; |
| 105 | |
| 106 | if (xp->xp_context == EXPAND_FILES |
| 107 | || xp->xp_context == EXPAND_FILES_IN_PATH |
| 108 | || xp->xp_context == EXPAND_SHELLCMD |
| 109 | || xp->xp_context == EXPAND_BUFFERS |
| 110 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 111 | { |
| 112 | // Insert a backslash into a file name before a space, \, %, # |
| 113 | // and wildmatch characters, except '~'. |
| 114 | for (int i = 0; i < numfiles; ++i) |
| 115 | { |
| 116 | // for ":set path=" we need to escape spaces twice |
| 117 | if (xp->xp_backslash == XP_BS_THREE) |
| 118 | { |
| 119 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 120 | if (p != NULL) |
| 121 | { |
| 122 | vim_free(files[i]); |
| 123 | files[i] = p; |
| 124 | #if defined(BACKSLASH_IN_FILENAME) |
| 125 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 126 | if (p != NULL) |
| 127 | { |
| 128 | vim_free(files[i]); |
| 129 | files[i] = p; |
| 130 | } |
| 131 | #endif |
| 132 | } |
| 133 | } |
| 134 | #ifdef BACKSLASH_IN_FILENAME |
| 135 | p = vim_strsave_fnameescape(files[i], vse_what); |
| 136 | #else |
| 137 | p = vim_strsave_fnameescape(files[i], |
| 138 | xp->xp_shell ? VSE_SHELL : vse_what); |
| 139 | #endif |
| 140 | if (p != NULL) |
| 141 | { |
| 142 | vim_free(files[i]); |
| 143 | files[i] = p; |
| 144 | } |
| 145 | |
| 146 | // If 'str' starts with "\~", replace "~" at start of |
| 147 | // files[i] with "\~". |
| 148 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
| 149 | escape_fname(&files[i]); |
| 150 | } |
| 151 | xp->xp_backslash = XP_BS_NONE; |
| 152 | |
| 153 | // If the first file starts with a '+' escape it. Otherwise it |
| 154 | // could be seen as "+cmd". |
| 155 | if (*files[0] == '+') |
| 156 | escape_fname(&files[0]); |
| 157 | } |
| 158 | else if (xp->xp_context == EXPAND_TAGS) |
| 159 | { |
| 160 | // Insert a backslash before characters in a tag name that |
| 161 | // would terminate the ":tag" command. |
| 162 | for (int i = 0; i < numfiles; ++i) |
| 163 | { |
| 164 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 165 | if (p != NULL) |
| 166 | { |
| 167 | vim_free(files[i]); |
| 168 | files[i] = p; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Escape special characters in the cmdline completion matches. |
| 176 | */ |
| 177 | static void |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 178 | ExpandEscape( |
| 179 | expand_T *xp, |
| 180 | char_u *str, |
| 181 | int numfiles, |
| 182 | char_u **files, |
| 183 | int options) |
| 184 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 185 | // May change home directory back to "~" |
| 186 | if (options & WILD_HOME_REPLACE) |
| 187 | tilde_replace(str, numfiles, files); |
| 188 | |
| 189 | if (options & WILD_ESCAPE) |
Yegappan Lakshmanan | 68353e5 | 2022-11-13 22:38:10 +0000 | [diff] [blame] | 190 | wildescape(xp, str, numfiles, files); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Return FAIL if this is not an appropriate context in which to do |
| 195 | * completion of anything, return OK if it is (even if there are no matches). |
| 196 | * For the caller, this means that the character is just passed through like a |
| 197 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 198 | */ |
| 199 | int |
| 200 | nextwild( |
| 201 | expand_T *xp, |
| 202 | int type, |
| 203 | int options, // extra options for ExpandOne() |
| 204 | int escape) // if TRUE, escape the returned matches |
| 205 | { |
| 206 | cmdline_info_T *ccline = get_cmdline_info(); |
| 207 | int i, j; |
| 208 | char_u *p1; |
| 209 | char_u *p2; |
| 210 | int difflen; |
| 211 | int v; |
| 212 | |
| 213 | if (xp->xp_numfiles == -1) |
| 214 | { |
| 215 | set_expand_context(xp); |
| 216 | cmd_showtail = expand_showtail(xp); |
| 217 | } |
| 218 | |
| 219 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 220 | { |
| 221 | beep_flush(); |
| 222 | return OK; // Something illegal on command line |
| 223 | } |
| 224 | if (xp->xp_context == EXPAND_NOTHING) |
| 225 | { |
| 226 | // Caller can use the character as a normal char instead |
| 227 | return FAIL; |
| 228 | } |
| 229 | |
Bram Moolenaar | 698a00f | 2022-11-14 22:07:45 +0000 | [diff] [blame] | 230 | // If cmd_silent is set then don't show the dots, because redrawcmd() below |
| 231 | // won't remove them. |
| 232 | if (!cmd_silent) |
| 233 | { |
| 234 | msg_puts("..."); // show that we are busy |
| 235 | out_flush(); |
| 236 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 237 | |
| 238 | i = (int)(xp->xp_pattern - ccline->cmdbuff); |
| 239 | xp->xp_pattern_len = ccline->cmdpos - i; |
| 240 | |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 241 | if (type == WILD_NEXT || type == WILD_PREV |
| 242 | || type == WILD_PAGEUP || type == WILD_PAGEDOWN) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 243 | { |
| 244 | // Get next/previous match for a previous expanded pattern. |
| 245 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 246 | } |
| 247 | else |
| 248 | { |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 249 | if (cmdline_fuzzy_completion_supported(xp)) |
| 250 | // If fuzzy matching, don't modify the search string |
| 251 | p1 = vim_strsave(xp->xp_pattern); |
| 252 | else |
| 253 | p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
| 254 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 255 | // Translate string into pattern and expand it. |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 256 | if (p1 == NULL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 257 | p2 = NULL; |
| 258 | else |
| 259 | { |
| 260 | int use_options = options | |
| 261 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 262 | if (escape) |
| 263 | use_options |= WILD_ESCAPE; |
| 264 | |
| 265 | if (p_wic) |
| 266 | use_options += WILD_ICASE; |
| 267 | p2 = ExpandOne(xp, p1, |
| 268 | vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len), |
| 269 | use_options, type); |
| 270 | vim_free(p1); |
| 271 | // longest match: make sure it is not shorter, happens with :help |
| 272 | if (p2 != NULL && type == WILD_LONGEST) |
| 273 | { |
| 274 | for (j = 0; j < xp->xp_pattern_len; ++j) |
| 275 | if (ccline->cmdbuff[i + j] == '*' |
| 276 | || ccline->cmdbuff[i + j] == '?') |
| 277 | break; |
| 278 | if ((int)STRLEN(p2) < j) |
| 279 | VIM_CLEAR(p2); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (p2 != NULL && !got_int) |
| 285 | { |
| 286 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
| 287 | if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen) |
| 288 | { |
| 289 | v = realloc_cmdbuff(ccline->cmdlen + difflen + 4); |
| 290 | xp->xp_pattern = ccline->cmdbuff + i; |
| 291 | } |
| 292 | else |
| 293 | v = OK; |
| 294 | if (v == OK) |
| 295 | { |
| 296 | mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen], |
| 297 | &ccline->cmdbuff[ccline->cmdpos], |
| 298 | (size_t)(ccline->cmdlen - ccline->cmdpos + 1)); |
| 299 | mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2)); |
| 300 | ccline->cmdlen += difflen; |
| 301 | ccline->cmdpos += difflen; |
| 302 | } |
| 303 | } |
| 304 | vim_free(p2); |
| 305 | |
| 306 | redrawcmd(); |
| 307 | cursorcmd(); |
| 308 | |
| 309 | // When expanding a ":map" command and no matches are found, assume that |
| 310 | // the key is supposed to be inserted literally |
| 311 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 312 | return FAIL; |
| 313 | |
| 314 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 315 | beep_flush(); |
| 316 | else if (xp->xp_numfiles == 1) |
| 317 | // free expanded pattern |
| 318 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 319 | |
| 320 | return OK; |
| 321 | } |
| 322 | |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 323 | /* |
| 324 | * Create and display a cmdline completion popup menu with items from |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 325 | * 'matches'. |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 326 | */ |
| 327 | static int |
| 328 | cmdline_pum_create( |
| 329 | cmdline_info_T *ccline, |
| 330 | expand_T *xp, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 331 | char_u **matches, |
| 332 | int numMatches, |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 333 | int showtail) |
| 334 | { |
| 335 | int i; |
| 336 | int columns; |
| 337 | |
| 338 | // Add all the completion matches |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 339 | compl_match_arraysize = numMatches; |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 340 | compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 341 | for (i = 0; i < numMatches; i++) |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 342 | { |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 343 | compl_match_array[i].pum_text = SHOW_MATCH(i); |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 344 | compl_match_array[i].pum_info = NULL; |
| 345 | compl_match_array[i].pum_extra = NULL; |
| 346 | compl_match_array[i].pum_kind = NULL; |
| 347 | } |
| 348 | |
| 349 | // Compute the popup menu starting column |
| 350 | compl_startcol = vim_strsize(ccline->cmdbuff) + 1; |
| 351 | columns = vim_strsize(xp->xp_pattern); |
| 352 | if (showtail) |
| 353 | { |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 354 | columns += vim_strsize(showmatches_gettail(matches[0])); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 355 | columns -= vim_strsize(matches[0]); |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 356 | } |
| 357 | if (columns >= compl_startcol) |
| 358 | compl_startcol = 0; |
| 359 | else |
| 360 | compl_startcol -= columns; |
| 361 | |
| 362 | // no default selection |
| 363 | compl_selected = -1; |
| 364 | |
| 365 | cmdline_pum_display(); |
| 366 | |
| 367 | return EXPAND_OK; |
| 368 | } |
| 369 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 370 | /* |
| 371 | * Display the cmdline completion matches in a popup menu |
| 372 | */ |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 373 | void |
| 374 | cmdline_pum_display(void) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 375 | { |
| 376 | pum_display(compl_match_array, compl_match_arraysize, compl_selected); |
| 377 | } |
| 378 | |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 379 | /* |
| 380 | * Returns TRUE if the cmdline completion popup menu is being displayed. |
| 381 | */ |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 382 | int |
| 383 | cmdline_pum_active(void) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 384 | { |
zeertzjq | b82a2ab | 2022-08-21 14:33:57 +0100 | [diff] [blame] | 385 | return pum_visible() && compl_match_array != NULL; |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 389 | * Remove the cmdline completion popup menu (if present), free the list of |
| 390 | * items and refresh the screen. |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 391 | */ |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 392 | void |
| 393 | cmdline_pum_remove(void) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 394 | { |
Bram Moolenaar | 5c52be4 | 2022-02-27 14:28:31 +0000 | [diff] [blame] | 395 | int save_p_lz = p_lz; |
Bram Moolenaar | 11a57df | 2022-04-11 19:38:56 +0100 | [diff] [blame] | 396 | int save_KeyTyped = KeyTyped; |
Bram Moolenaar | 5c52be4 | 2022-02-27 14:28:31 +0000 | [diff] [blame] | 397 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 398 | pum_undisplay(); |
| 399 | VIM_CLEAR(compl_match_array); |
Bram Moolenaar | 5c52be4 | 2022-02-27 14:28:31 +0000 | [diff] [blame] | 400 | p_lz = FALSE; // avoid the popup menu hanging around |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 401 | update_screen(0); |
Bram Moolenaar | 5c52be4 | 2022-02-27 14:28:31 +0000 | [diff] [blame] | 402 | p_lz = save_p_lz; |
Bram Moolenaar | 414acd3 | 2022-02-10 21:09:45 +0000 | [diff] [blame] | 403 | redrawcmd(); |
Bram Moolenaar | 11a57df | 2022-04-11 19:38:56 +0100 | [diff] [blame] | 404 | |
| 405 | // When a function is called (e.g. for 'foldtext') KeyTyped might be reset |
| 406 | // as a side effect. |
| 407 | KeyTyped = save_KeyTyped; |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 410 | void |
| 411 | cmdline_pum_cleanup(cmdline_info_T *cclp) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 412 | { |
| 413 | cmdline_pum_remove(); |
| 414 | wildmenu_cleanup(cclp); |
| 415 | } |
| 416 | |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 417 | /* |
| 418 | * Returns the starting column number to use for the cmdline completion popup |
| 419 | * menu. |
| 420 | */ |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 421 | int |
| 422 | cmdline_compl_startcol(void) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 423 | { |
| 424 | return compl_startcol; |
| 425 | } |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 426 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 427 | /* |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 428 | * Return the number of characters that should be skipped in a status match. |
| 429 | * These are backslashes used for escaping. Do show backslashes in help tags. |
| 430 | */ |
| 431 | static int |
| 432 | skip_status_match_char(expand_T *xp, char_u *s) |
| 433 | { |
| 434 | if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP) |
| 435 | #ifdef FEAT_MENU |
| 436 | || ((xp->xp_context == EXPAND_MENUS |
| 437 | || xp->xp_context == EXPAND_MENUNAMES) |
| 438 | && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL))) |
| 439 | #endif |
| 440 | ) |
| 441 | { |
| 442 | #ifndef BACKSLASH_IN_FILENAME |
| 443 | if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!') |
| 444 | return 2; |
| 445 | #endif |
| 446 | return 1; |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Get the length of an item as it will be shown in the status line. |
| 453 | */ |
| 454 | static int |
| 455 | status_match_len(expand_T *xp, char_u *s) |
| 456 | { |
| 457 | int len = 0; |
| 458 | |
| 459 | #ifdef FEAT_MENU |
| 460 | int emenu = xp->xp_context == EXPAND_MENUS |
| 461 | || xp->xp_context == EXPAND_MENUNAMES; |
| 462 | |
| 463 | // Check for menu separators - replace with '|'. |
| 464 | if (emenu && menu_is_separator(s)) |
| 465 | return 1; |
| 466 | #endif |
| 467 | |
| 468 | while (*s != NUL) |
| 469 | { |
| 470 | s += skip_status_match_char(xp, s); |
| 471 | len += ptr2cells(s); |
| 472 | MB_PTR_ADV(s); |
| 473 | } |
| 474 | |
| 475 | return len; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Show wildchar matches in the status line. |
| 480 | * Show at least the "match" item. |
| 481 | * We start at item 'first_match' in the list and show all matches that fit. |
| 482 | * |
| 483 | * If inversion is possible we use it. Else '=' characters are used. |
| 484 | */ |
| 485 | static void |
| 486 | win_redr_status_matches( |
| 487 | expand_T *xp, |
| 488 | int num_matches, |
| 489 | char_u **matches, // list of matches |
| 490 | int match, |
| 491 | int showtail) |
| 492 | { |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 493 | int row; |
| 494 | char_u *buf; |
| 495 | int len; |
| 496 | int clen; // length in screen cells |
| 497 | int fillchar; |
| 498 | int attr; |
| 499 | int i; |
| 500 | int highlight = TRUE; |
| 501 | char_u *selstart = NULL; |
| 502 | int selstart_col = 0; |
| 503 | char_u *selend = NULL; |
| 504 | static int first_match = 0; |
| 505 | int add_left = FALSE; |
| 506 | char_u *s; |
| 507 | #ifdef FEAT_MENU |
| 508 | int emenu; |
| 509 | #endif |
| 510 | int l; |
| 511 | |
| 512 | if (matches == NULL) // interrupted completion? |
| 513 | return; |
| 514 | |
| 515 | if (has_mbyte) |
| 516 | buf = alloc(Columns * MB_MAXBYTES + 1); |
| 517 | else |
| 518 | buf = alloc(Columns + 1); |
| 519 | if (buf == NULL) |
| 520 | return; |
| 521 | |
| 522 | if (match == -1) // don't show match but original text |
| 523 | { |
| 524 | match = 0; |
| 525 | highlight = FALSE; |
| 526 | } |
| 527 | // count 1 for the ending ">" |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 528 | clen = status_match_len(xp, SHOW_MATCH(match)) + 3; |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 529 | if (match == 0) |
| 530 | first_match = 0; |
| 531 | else if (match < first_match) |
| 532 | { |
| 533 | // jumping left, as far as we can go |
| 534 | first_match = match; |
| 535 | add_left = TRUE; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | // check if match fits on the screen |
| 540 | for (i = first_match; i < match; ++i) |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 541 | clen += status_match_len(xp, SHOW_MATCH(i)) + 2; |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 542 | if (first_match > 0) |
| 543 | clen += 2; |
| 544 | // jumping right, put match at the left |
| 545 | if ((long)clen > Columns) |
| 546 | { |
| 547 | first_match = match; |
| 548 | // if showing the last match, we can add some on the left |
| 549 | clen = 2; |
| 550 | for (i = match; i < num_matches; ++i) |
| 551 | { |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 552 | clen += status_match_len(xp, SHOW_MATCH(i)) + 2; |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 553 | if ((long)clen >= Columns) |
| 554 | break; |
| 555 | } |
| 556 | if (i == num_matches) |
| 557 | add_left = TRUE; |
| 558 | } |
| 559 | } |
| 560 | if (add_left) |
| 561 | while (first_match > 0) |
| 562 | { |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 563 | clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2; |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 564 | if ((long)clen >= Columns) |
| 565 | break; |
| 566 | --first_match; |
| 567 | } |
| 568 | |
| 569 | fillchar = fillchar_status(&attr, curwin); |
| 570 | |
| 571 | if (first_match == 0) |
| 572 | { |
| 573 | *buf = NUL; |
| 574 | len = 0; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | STRCPY(buf, "< "); |
| 579 | len = 2; |
| 580 | } |
| 581 | clen = len; |
| 582 | |
| 583 | i = first_match; |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 584 | while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns) |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 585 | { |
| 586 | if (i == match) |
| 587 | { |
| 588 | selstart = buf + len; |
| 589 | selstart_col = clen; |
| 590 | } |
| 591 | |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 592 | s = SHOW_MATCH(i); |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 593 | // Check for menu separators - replace with '|' |
| 594 | #ifdef FEAT_MENU |
| 595 | emenu = (xp->xp_context == EXPAND_MENUS |
| 596 | || xp->xp_context == EXPAND_MENUNAMES); |
| 597 | if (emenu && menu_is_separator(s)) |
| 598 | { |
| 599 | STRCPY(buf + len, transchar('|')); |
| 600 | l = (int)STRLEN(buf + len); |
| 601 | len += l; |
| 602 | clen += l; |
| 603 | } |
| 604 | else |
| 605 | #endif |
| 606 | for ( ; *s != NUL; ++s) |
| 607 | { |
| 608 | s += skip_status_match_char(xp, s); |
| 609 | clen += ptr2cells(s); |
| 610 | if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1) |
| 611 | { |
| 612 | STRNCPY(buf + len, s, l); |
| 613 | s += l - 1; |
| 614 | len += l; |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | STRCPY(buf + len, transchar_byte(*s)); |
| 619 | len += (int)STRLEN(buf + len); |
| 620 | } |
| 621 | } |
| 622 | if (i == match) |
| 623 | selend = buf + len; |
| 624 | |
| 625 | *(buf + len++) = ' '; |
| 626 | *(buf + len++) = ' '; |
| 627 | clen += 2; |
| 628 | if (++i == num_matches) |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | if (i != num_matches) |
| 633 | { |
| 634 | *(buf + len++) = '>'; |
| 635 | ++clen; |
| 636 | } |
| 637 | |
| 638 | buf[len] = NUL; |
| 639 | |
| 640 | row = cmdline_row - 1; |
| 641 | if (row >= 0) |
| 642 | { |
| 643 | if (wild_menu_showing == 0) |
| 644 | { |
| 645 | if (msg_scrolled > 0) |
| 646 | { |
| 647 | // Put the wildmenu just above the command line. If there is |
| 648 | // no room, scroll the screen one line up. |
| 649 | if (cmdline_row == Rows - 1) |
| 650 | { |
| 651 | screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL); |
| 652 | ++msg_scrolled; |
| 653 | } |
| 654 | else |
| 655 | { |
| 656 | ++cmdline_row; |
| 657 | ++row; |
| 658 | } |
| 659 | wild_menu_showing = WM_SCROLLED; |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | // Create status line if needed by setting 'laststatus' to 2. |
| 664 | // Set 'winminheight' to zero to avoid that the window is |
| 665 | // resized. |
| 666 | if (lastwin->w_status_height == 0) |
| 667 | { |
| 668 | save_p_ls = p_ls; |
| 669 | save_p_wmh = p_wmh; |
| 670 | p_ls = 2; |
| 671 | p_wmh = 0; |
| 672 | last_status(FALSE); |
| 673 | } |
| 674 | wild_menu_showing = WM_SHOWN; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | screen_puts(buf, row, 0, attr); |
| 679 | if (selstart != NULL && highlight) |
| 680 | { |
| 681 | *selend = NUL; |
| 682 | screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM)); |
| 683 | } |
| 684 | |
| 685 | screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr); |
| 686 | } |
| 687 | |
| 688 | win_redraw_last_status(topframe); |
| 689 | vim_free(buf); |
| 690 | } |
| 691 | |
| 692 | /* |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 693 | * Get the next or prev cmdline completion match. The index of the match is set |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 694 | * in "xp->xp_selected" |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 695 | */ |
| 696 | static char_u * |
| 697 | get_next_or_prev_match( |
| 698 | int mode, |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 699 | expand_T *xp) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 700 | { |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 701 | int findex = xp->xp_selected; |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 702 | int ht; |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 703 | |
| 704 | if (xp->xp_numfiles <= 0) |
| 705 | return NULL; |
| 706 | |
| 707 | if (mode == WILD_PREV) |
| 708 | { |
| 709 | if (findex == -1) |
| 710 | findex = xp->xp_numfiles; |
| 711 | --findex; |
| 712 | } |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 713 | else if (mode == WILD_NEXT) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 714 | ++findex; |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 715 | else if (mode == WILD_PAGEUP) |
| 716 | { |
| 717 | if (findex == 0) |
| 718 | // at the first entry, don't select any entries |
| 719 | findex = -1; |
| 720 | else if (findex == -1) |
| 721 | // no entry is selected. select the last entry |
| 722 | findex = xp->xp_numfiles - 1; |
| 723 | else |
| 724 | { |
| 725 | // go up by the pum height |
| 726 | ht = pum_get_height(); |
| 727 | if (ht > 3) |
| 728 | ht -= 2; |
| 729 | findex -= ht; |
| 730 | if (findex < 0) |
| 731 | // few entries left, select the first entry |
| 732 | findex = 0; |
| 733 | } |
| 734 | } |
| 735 | else // mode == WILD_PAGEDOWN |
| 736 | { |
| 737 | if (findex == xp->xp_numfiles - 1) |
| 738 | // at the last entry, don't select any entries |
| 739 | findex = -1; |
| 740 | else if (findex == -1) |
| 741 | // no entry is selected. select the first entry |
| 742 | findex = 0; |
| 743 | else |
| 744 | { |
| 745 | // go down by the pum height |
| 746 | ht = pum_get_height(); |
| 747 | if (ht > 3) |
| 748 | ht -= 2; |
| 749 | findex += ht; |
| 750 | if (findex >= xp->xp_numfiles) |
| 751 | // few entries left, select the last entry |
| 752 | findex = xp->xp_numfiles - 1; |
| 753 | } |
| 754 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 755 | |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 756 | // When wrapping around, return the original string, set findex to -1. |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 757 | if (findex < 0) |
| 758 | { |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 759 | if (xp->xp_orig == NULL) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 760 | findex = xp->xp_numfiles - 1; |
| 761 | else |
| 762 | findex = -1; |
| 763 | } |
| 764 | if (findex >= xp->xp_numfiles) |
| 765 | { |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 766 | if (xp->xp_orig == NULL) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 767 | findex = 0; |
| 768 | else |
| 769 | findex = -1; |
| 770 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 771 | if (compl_match_array) |
| 772 | { |
| 773 | compl_selected = findex; |
| 774 | cmdline_pum_display(); |
| 775 | } |
| 776 | else if (p_wmnu) |
| 777 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 778 | findex, cmd_showtail); |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 779 | xp->xp_selected = findex; |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 780 | |
| 781 | if (findex == -1) |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 782 | return vim_strsave(xp->xp_orig); |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 783 | |
| 784 | return vim_strsave(xp->xp_files[findex]); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Start the command-line expansion and get the matches. |
| 789 | */ |
| 790 | static char_u * |
| 791 | ExpandOne_start(int mode, expand_T *xp, char_u *str, int options) |
| 792 | { |
| 793 | int non_suf_match; // number without matching suffix |
| 794 | int i; |
| 795 | char_u *ss = NULL; |
| 796 | |
| 797 | // Do the expansion. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 798 | if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 799 | options) == FAIL) |
| 800 | { |
| 801 | #ifdef FNAME_ILLEGAL |
| 802 | // Illegal file name has been silently skipped. But when there |
| 803 | // are wildcards, the real problem is that there was no match, |
| 804 | // causing the pattern to be added, which has illegal characters. |
| 805 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 806 | semsg(_(e_no_match_str_2), str); |
| 807 | #endif |
| 808 | } |
| 809 | else if (xp->xp_numfiles == 0) |
| 810 | { |
| 811 | if (!(options & WILD_SILENT)) |
| 812 | semsg(_(e_no_match_str_2), str); |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | // Escape the matches for use on the command line. |
| 817 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 818 | |
| 819 | // Check for matching suffixes in file names. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 820 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 821 | { |
| 822 | if (xp->xp_numfiles) |
| 823 | non_suf_match = xp->xp_numfiles; |
| 824 | else |
| 825 | non_suf_match = 1; |
| 826 | if ((xp->xp_context == EXPAND_FILES |
| 827 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 828 | && xp->xp_numfiles > 1) |
| 829 | { |
| 830 | // More than one match; check suffix. |
| 831 | // The files will have been sorted on matching suffix in |
| 832 | // expand_wildcards, only need to check the first two. |
| 833 | non_suf_match = 0; |
| 834 | for (i = 0; i < 2; ++i) |
| 835 | if (match_suffix(xp->xp_files[i])) |
| 836 | ++non_suf_match; |
| 837 | } |
| 838 | if (non_suf_match != 1) |
| 839 | { |
| 840 | // Can we ever get here unless it's while expanding |
| 841 | // interactively? If not, we can get rid of this all |
| 842 | // together. Don't really want to wait for this message |
| 843 | // (and possibly have to hit return to continue!). |
| 844 | if (!(options & WILD_SILENT)) |
| 845 | emsg(_(e_too_many_file_names)); |
| 846 | else if (!(options & WILD_NO_BEEP)) |
| 847 | beep_flush(); |
| 848 | } |
| 849 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 850 | ss = vim_strsave(xp->xp_files[0]); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return ss; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Return the longest common part in the list of cmdline completion matches. |
| 859 | */ |
| 860 | static char_u * |
| 861 | find_longest_match(expand_T *xp, int options) |
| 862 | { |
| 863 | long_u len; |
| 864 | int mb_len = 1; |
| 865 | int c0, ci; |
| 866 | int i; |
| 867 | char_u *ss; |
| 868 | |
| 869 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
| 870 | { |
| 871 | if (has_mbyte) |
| 872 | { |
| 873 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 874 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 875 | } |
| 876 | else |
| 877 | c0 = xp->xp_files[0][len]; |
| 878 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 879 | { |
| 880 | if (has_mbyte) |
| 881 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 882 | else |
| 883 | ci = xp->xp_files[i][len]; |
| 884 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
| 885 | || xp->xp_context == EXPAND_FILES |
| 886 | || xp->xp_context == EXPAND_SHELLCMD |
| 887 | || xp->xp_context == EXPAND_BUFFERS)) |
| 888 | { |
| 889 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
| 890 | break; |
| 891 | } |
| 892 | else if (c0 != ci) |
| 893 | break; |
| 894 | } |
| 895 | if (i < xp->xp_numfiles) |
| 896 | { |
| 897 | if (!(options & WILD_NO_BEEP)) |
| 898 | vim_beep(BO_WILD); |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | ss = alloc(len + 1); |
| 904 | if (ss) |
| 905 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
| 906 | |
| 907 | return ss; |
| 908 | } |
| 909 | |
| 910 | /* |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 911 | * Do wildcard expansion on the string "str". |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 912 | * Chars that should not be expanded must be preceded with a backslash. |
| 913 | * Return a pointer to allocated memory containing the new string. |
| 914 | * Return NULL for failure. |
| 915 | * |
| 916 | * "orig" is the originally expanded string, copied to allocated memory. It |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 917 | * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT |
| 918 | * or WILD_PREV "orig" should be NULL. |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 919 | * |
| 920 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 921 | * is WILD_EXPAND_FREE or WILD_ALL. |
| 922 | * |
| 923 | * mode = WILD_FREE: just free previously expanded matches |
| 924 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 925 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 926 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 927 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 928 | * mode = WILD_ALL: return all matches concatenated |
| 929 | * mode = WILD_LONGEST: return longest matched part |
| 930 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 931 | * mode = WILD_APPLY: apply the item selected in the cmdline completion |
| 932 | * popup menu and close the menu. |
| 933 | * mode = WILD_CANCEL: cancel and close the cmdline completion popup and |
| 934 | * use the original text. |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 935 | * |
| 936 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 937 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 938 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 939 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 940 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 941 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 942 | * options = WILD_SILENT: don't print warning messages |
| 943 | * options = WILD_ESCAPE: put backslash before special chars |
| 944 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | ec68028 | 2020-06-12 19:35:32 +0200 | [diff] [blame] | 945 | * options = WILD_ALLLINKS; keep broken links |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 946 | * |
| 947 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 948 | */ |
| 949 | char_u * |
| 950 | ExpandOne( |
| 951 | expand_T *xp, |
| 952 | char_u *str, |
| 953 | char_u *orig, // allocated copy of original of expanded string |
| 954 | int options, |
| 955 | int mode) |
| 956 | { |
| 957 | char_u *ss = NULL; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 958 | int orig_saved = FALSE; |
| 959 | int i; |
| 960 | long_u len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 961 | |
| 962 | // first handle the case of using an old match |
Yegappan Lakshmanan | 5cffa8d | 2022-03-16 13:33:53 +0000 | [diff] [blame] | 963 | if (mode == WILD_NEXT || mode == WILD_PREV |
| 964 | || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN) |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 965 | return get_next_or_prev_match(mode, xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 966 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 967 | if (mode == WILD_CANCEL) |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 968 | ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)""); |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 969 | else if (mode == WILD_APPLY) |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 970 | ss = vim_strsave(xp->xp_selected == -1 |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 971 | ? (xp->xp_orig ? xp->xp_orig : (char_u *)"") |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 972 | : xp->xp_files[xp->xp_selected]); |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 973 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 974 | // free old names |
| 975 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 976 | { |
| 977 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 978 | xp->xp_numfiles = -1; |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 979 | VIM_CLEAR(xp->xp_orig); |
Bram Moolenaar | 038e6d2 | 2022-12-08 12:00:50 +0000 | [diff] [blame] | 980 | |
| 981 | // The entries from xp_files may be used in the PUM, remove it. |
| 982 | if (compl_match_array != NULL) |
| 983 | cmdline_pum_remove(); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 984 | } |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 985 | xp->xp_selected = 0; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 986 | |
| 987 | if (mode == WILD_FREE) // only release file name |
| 988 | return NULL; |
| 989 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 990 | if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 991 | { |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 992 | vim_free(xp->xp_orig); |
| 993 | xp->xp_orig = orig; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 994 | orig_saved = TRUE; |
| 995 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 996 | ss = ExpandOne_start(mode, xp, str, options); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | // Find longest common part |
| 1000 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 1001 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1002 | ss = find_longest_match(xp, options); |
zeertzjq | e9ef347 | 2023-08-17 23:57:05 +0200 | [diff] [blame] | 1003 | xp->xp_selected = -1; // next p_wc gets first one |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Bram Moolenaar | 57e9517 | 2022-08-20 19:26:14 +0100 | [diff] [blame] | 1006 | // Concatenate all matching names. Unless interrupted, this can be slow |
| 1007 | // and the result probably won't be used. |
| 1008 | if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1009 | { |
| 1010 | len = 0; |
| 1011 | for (i = 0; i < xp->xp_numfiles; ++i) |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 1012 | { |
| 1013 | if (i > 0) |
| 1014 | { |
| 1015 | if (xp->xp_prefix == XP_PREFIX_NO) |
| 1016 | len += 2; // prefix "no" |
| 1017 | else if (xp->xp_prefix == XP_PREFIX_INV) |
| 1018 | len += 3; // prefix "inv" |
| 1019 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1020 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 1021 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1022 | ss = alloc(len); |
| 1023 | if (ss != NULL) |
| 1024 | { |
| 1025 | *ss = NUL; |
| 1026 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 1027 | { |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 1028 | if (i > 0) |
| 1029 | { |
| 1030 | if (xp->xp_prefix == XP_PREFIX_NO) |
| 1031 | STRCAT(ss, "no"); |
| 1032 | else if (xp->xp_prefix == XP_PREFIX_INV) |
| 1033 | STRCAT(ss, "inv"); |
| 1034 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1035 | STRCAT(ss, xp->xp_files[i]); |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 1036 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1037 | if (i != xp->xp_numfiles - 1) |
| 1038 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 1044 | ExpandCleanup(xp); |
| 1045 | |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 1046 | // Free "orig" if it wasn't stored in "xp->xp_orig". |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1047 | if (!orig_saved) |
| 1048 | vim_free(orig); |
| 1049 | |
| 1050 | return ss; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Prepare an expand structure for use. |
| 1055 | */ |
| 1056 | void |
| 1057 | ExpandInit(expand_T *xp) |
| 1058 | { |
Bram Moolenaar | c841aff | 2020-07-25 14:11:55 +0200 | [diff] [blame] | 1059 | CLEAR_POINTER(xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1060 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 1061 | xp->xp_prefix = XP_PREFIX_NONE; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1062 | xp->xp_numfiles = -1; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Cleanup an expand structure after use. |
| 1067 | */ |
| 1068 | void |
| 1069 | ExpandCleanup(expand_T *xp) |
| 1070 | { |
| 1071 | if (xp->xp_numfiles >= 0) |
| 1072 | { |
| 1073 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 1074 | xp->xp_numfiles = -1; |
| 1075 | } |
zeertzjq | 28a2360 | 2023-09-29 19:58:35 +0200 | [diff] [blame] | 1076 | VIM_CLEAR(xp->xp_orig); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1080 | * Display one line of completion matches. Multiple matches are displayed in |
| 1081 | * each line (used by wildmode=list and CTRL-D) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1082 | * matches - list of completion match names |
| 1083 | * numMatches - number of completion matches in "matches" |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1084 | * lines - number of output lines |
| 1085 | * linenr - line number of matches to display |
| 1086 | * maxlen - maximum number of characters in each line |
| 1087 | * showtail - display only the tail of the full path of a file name |
| 1088 | * dir_attr - highlight attribute to use for directory names |
| 1089 | */ |
| 1090 | static void |
| 1091 | showmatches_oneline( |
| 1092 | expand_T *xp, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1093 | char_u **matches, |
| 1094 | int numMatches, |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1095 | int lines, |
| 1096 | int linenr, |
| 1097 | int maxlen, |
| 1098 | int showtail, |
| 1099 | int dir_attr) |
| 1100 | { |
| 1101 | int i, j; |
| 1102 | int isdir; |
| 1103 | int lastlen; |
| 1104 | char_u *p; |
| 1105 | |
| 1106 | lastlen = 999; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1107 | for (j = linenr; j < numMatches; j += lines) |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1108 | { |
| 1109 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 1110 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1111 | msg_outtrans_attr(matches[j], HL_ATTR(HLF_D)); |
| 1112 | p = matches[j] + STRLEN(matches[j]) + 1; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1113 | msg_advance(maxlen + 1); |
| 1114 | msg_puts((char *)p); |
| 1115 | msg_advance(maxlen + 3); |
| 1116 | msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D)); |
| 1117 | break; |
| 1118 | } |
| 1119 | for (i = maxlen - lastlen; --i >= 0; ) |
| 1120 | msg_putchar(' '); |
| 1121 | if (xp->xp_context == EXPAND_FILES |
| 1122 | || xp->xp_context == EXPAND_SHELLCMD |
| 1123 | || xp->xp_context == EXPAND_BUFFERS) |
| 1124 | { |
| 1125 | // highlight directories |
| 1126 | if (xp->xp_numfiles != -1) |
| 1127 | { |
| 1128 | char_u *halved_slash; |
| 1129 | char_u *exp_path; |
| 1130 | char_u *path; |
| 1131 | |
| 1132 | // Expansion was done before and special characters |
| 1133 | // were escaped, need to halve backslashes. Also |
| 1134 | // $HOME has been replaced with ~/. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1135 | exp_path = expand_env_save_opt(matches[j], TRUE); |
| 1136 | path = exp_path != NULL ? exp_path : matches[j]; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1137 | halved_slash = backslash_halve_save(path); |
| 1138 | isdir = mch_isdir(halved_slash != NULL ? halved_slash |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1139 | : matches[j]); |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1140 | vim_free(exp_path); |
| 1141 | if (halved_slash != path) |
| 1142 | vim_free(halved_slash); |
| 1143 | } |
| 1144 | else |
| 1145 | // Expansion was done here, file names are literal. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1146 | isdir = mch_isdir(matches[j]); |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1147 | if (showtail) |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 1148 | p = SHOW_MATCH(j); |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1149 | else |
| 1150 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1151 | home_replace(NULL, matches[j], NameBuff, MAXPATHL, |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1152 | TRUE); |
| 1153 | p = NameBuff; |
| 1154 | } |
| 1155 | } |
| 1156 | else |
| 1157 | { |
| 1158 | isdir = FALSE; |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 1159 | p = SHOW_MATCH(j); |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1160 | } |
| 1161 | lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0); |
| 1162 | } |
| 1163 | if (msg_col > 0) // when not wrapped around |
| 1164 | { |
| 1165 | msg_clr_eos(); |
| 1166 | msg_putchar('\n'); |
| 1167 | } |
| 1168 | out_flush(); // show one line at a time |
| 1169 | } |
| 1170 | |
| 1171 | /* |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1172 | * Show all matches for completion on the command line. |
| 1173 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 1174 | * be inserted like a normal character. |
| 1175 | */ |
| 1176 | int |
| 1177 | showmatches(expand_T *xp, int wildmenu UNUSED) |
| 1178 | { |
| 1179 | cmdline_info_T *ccline = get_cmdline_info(); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1180 | int numMatches; |
| 1181 | char_u **matches; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1182 | int i, j; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1183 | int maxlen; |
| 1184 | int lines; |
| 1185 | int columns; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1186 | int attr; |
| 1187 | int showtail; |
| 1188 | |
| 1189 | if (xp->xp_numfiles == -1) |
| 1190 | { |
| 1191 | set_expand_context(xp); |
| 1192 | i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1193 | &numMatches, &matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1194 | showtail = expand_showtail(xp); |
| 1195 | if (i != EXPAND_OK) |
| 1196 | return i; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1197 | } |
| 1198 | else |
| 1199 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1200 | numMatches = xp->xp_numfiles; |
| 1201 | matches = xp->xp_files; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1202 | showtail = cmd_showtail; |
| 1203 | } |
| 1204 | |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 1205 | if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL) |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 1206 | // cmdline completion popup menu (with wildoptions=pum) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1207 | return cmdline_pum_create(ccline, xp, matches, numMatches, showtail); |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 1208 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1209 | if (!wildmenu) |
| 1210 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1211 | msg_didany = FALSE; // lines_left will be set |
| 1212 | msg_start(); // prepare for paging |
| 1213 | msg_putchar('\n'); |
| 1214 | out_flush(); |
| 1215 | cmdline_row = msg_row; |
| 1216 | msg_didany = FALSE; // lines_left will be set again |
| 1217 | msg_start(); // prepare for paging |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1218 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1219 | |
| 1220 | if (got_int) |
| 1221 | got_int = FALSE; // only int. the completion, not the cmd line |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1222 | else if (wildmenu) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1223 | win_redr_status_matches(xp, numMatches, matches, -1, showtail); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1224 | else |
| 1225 | { |
| 1226 | // find the length of the longest file name |
| 1227 | maxlen = 0; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1228 | for (i = 0; i < numMatches; ++i) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1229 | { |
| 1230 | if (!showtail && (xp->xp_context == EXPAND_FILES |
| 1231 | || xp->xp_context == EXPAND_SHELLCMD |
| 1232 | || xp->xp_context == EXPAND_BUFFERS)) |
| 1233 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1234 | home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1235 | j = vim_strsize(NameBuff); |
| 1236 | } |
| 1237 | else |
zeertzjq | c51a376 | 2022-12-10 10:22:29 +0000 | [diff] [blame] | 1238 | j = vim_strsize(SHOW_MATCH(i)); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1239 | if (j > maxlen) |
| 1240 | maxlen = j; |
| 1241 | } |
| 1242 | |
| 1243 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1244 | lines = numMatches; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1245 | else |
| 1246 | { |
| 1247 | // compute the number of columns and lines for the listing |
| 1248 | maxlen += 2; // two spaces between file names |
| 1249 | columns = ((int)Columns + 2) / maxlen; |
| 1250 | if (columns < 1) |
| 1251 | columns = 1; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1252 | lines = (numMatches + columns - 1) / columns; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | attr = HL_ATTR(HLF_D); // find out highlighting for directories |
| 1256 | |
| 1257 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 1258 | { |
| 1259 | msg_puts_attr(_("tagname"), HL_ATTR(HLF_T)); |
| 1260 | msg_clr_eos(); |
| 1261 | msg_advance(maxlen - 3); |
| 1262 | msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T)); |
| 1263 | } |
| 1264 | |
| 1265 | // list the files line by line |
| 1266 | for (i = 0; i < lines; ++i) |
| 1267 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1268 | showmatches_oneline(xp, matches, numMatches, lines, i, |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1269 | maxlen, showtail, attr); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1270 | if (got_int) |
| 1271 | { |
| 1272 | got_int = FALSE; |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | // we redraw the command below the lines that we have just listed |
| 1278 | // This is a bit tricky, but it saves a lot of screen updating. |
| 1279 | cmdline_row = msg_row; // will put it back later |
| 1280 | } |
| 1281 | |
| 1282 | if (xp->xp_numfiles == -1) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1283 | FreeWild(numMatches, matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1284 | |
| 1285 | return EXPAND_OK; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 1289 | * gettail() version for showmatches() and win_redr_status_matches(): |
| 1290 | * Return the tail of file name path "s", ignoring a trailing "/". |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1291 | */ |
Bram Moolenaar | d6e9138 | 2022-11-12 17:44:13 +0000 | [diff] [blame] | 1292 | static char_u * |
| 1293 | showmatches_gettail(char_u *s) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1294 | { |
| 1295 | char_u *p; |
| 1296 | char_u *t = s; |
| 1297 | int had_sep = FALSE; |
| 1298 | |
| 1299 | for (p = s; *p != NUL; ) |
| 1300 | { |
| 1301 | if (vim_ispathsep(*p) |
| 1302 | #ifdef BACKSLASH_IN_FILENAME |
| 1303 | && !rem_backslash(p) |
| 1304 | #endif |
| 1305 | ) |
| 1306 | had_sep = TRUE; |
| 1307 | else if (had_sep) |
| 1308 | { |
| 1309 | t = p; |
| 1310 | had_sep = FALSE; |
| 1311 | } |
| 1312 | MB_PTR_ADV(p); |
| 1313 | } |
| 1314 | return t; |
| 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * Return TRUE if we only need to show the tail of completion matches. |
| 1319 | * When not completing file names or there is a wildcard in the path FALSE is |
| 1320 | * returned. |
| 1321 | */ |
| 1322 | static int |
| 1323 | expand_showtail(expand_T *xp) |
| 1324 | { |
| 1325 | char_u *s; |
| 1326 | char_u *end; |
| 1327 | |
| 1328 | // When not completing file names a "/" may mean something different. |
| 1329 | if (xp->xp_context != EXPAND_FILES |
| 1330 | && xp->xp_context != EXPAND_SHELLCMD |
| 1331 | && xp->xp_context != EXPAND_DIRECTORIES) |
| 1332 | return FALSE; |
| 1333 | |
| 1334 | end = gettail(xp->xp_pattern); |
| 1335 | if (end == xp->xp_pattern) // there is no path separator |
| 1336 | return FALSE; |
| 1337 | |
| 1338 | for (s = xp->xp_pattern; s < end; s++) |
| 1339 | { |
| 1340 | // Skip escaped wildcards. Only when the backslash is not a path |
| 1341 | // separator, on DOS the '*' "path\*\file" must not be skipped. |
| 1342 | if (rem_backslash(s)) |
| 1343 | ++s; |
| 1344 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 1345 | return FALSE; |
| 1346 | } |
| 1347 | return TRUE; |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * Prepare a string for expansion. |
| 1352 | * When expanding file names: The string will be used with expand_wildcards(). |
| 1353 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
| 1354 | * When expanding other names: The string will be used with regcomp(). Copy |
| 1355 | * the name into allocated memory and prepend "^". |
| 1356 | */ |
| 1357 | char_u * |
| 1358 | addstar( |
| 1359 | char_u *fname, |
| 1360 | int len, |
| 1361 | int context) // EXPAND_FILES etc. |
| 1362 | { |
| 1363 | char_u *retval; |
| 1364 | int i, j; |
| 1365 | int new_len; |
| 1366 | char_u *tail; |
| 1367 | int ends_in_star; |
| 1368 | |
| 1369 | if (context != EXPAND_FILES |
| 1370 | && context != EXPAND_FILES_IN_PATH |
| 1371 | && context != EXPAND_SHELLCMD |
| 1372 | && context != EXPAND_DIRECTORIES) |
| 1373 | { |
| 1374 | // Matching will be done internally (on something other than files). |
| 1375 | // So we convert the file-matching-type wildcards into our kind for |
| 1376 | // use with vim_regcomp(). First work out how long it will be: |
| 1377 | |
| 1378 | // For help tags the translation is done in find_help_tags(). |
| 1379 | // For a tag pattern starting with "/" no translation is needed. |
| 1380 | if (context == EXPAND_HELP |
| 1381 | || context == EXPAND_COLORS |
| 1382 | || context == EXPAND_COMPILER |
| 1383 | || context == EXPAND_OWNSYNTAX |
| 1384 | || context == EXPAND_FILETYPE |
| 1385 | || context == EXPAND_PACKADD |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 1386 | || context == EXPAND_RUNTIME |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1387 | || ((context == EXPAND_TAGS_LISTFILES |
| 1388 | || context == EXPAND_TAGS) |
| 1389 | && fname[0] == '/')) |
| 1390 | retval = vim_strnsave(fname, len); |
| 1391 | else |
| 1392 | { |
| 1393 | new_len = len + 2; // +2 for '^' at start, NUL at end |
| 1394 | for (i = 0; i < len; i++) |
| 1395 | { |
| 1396 | if (fname[i] == '*' || fname[i] == '~') |
| 1397 | new_len++; // '*' needs to be replaced by ".*" |
| 1398 | // '~' needs to be replaced by "\~" |
| 1399 | |
| 1400 | // Buffer names are like file names. "." should be literal |
| 1401 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 1402 | new_len++; // "." becomes "\." |
| 1403 | |
| 1404 | // Custom expansion takes care of special things, match |
| 1405 | // backslashes literally (perhaps also for other types?) |
| 1406 | if ((context == EXPAND_USER_DEFINED |
| 1407 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
| 1408 | new_len++; // '\' becomes "\\" |
| 1409 | } |
| 1410 | retval = alloc(new_len); |
| 1411 | if (retval != NULL) |
| 1412 | { |
| 1413 | retval[0] = '^'; |
| 1414 | j = 1; |
| 1415 | for (i = 0; i < len; i++, j++) |
| 1416 | { |
| 1417 | // Skip backslash. But why? At least keep it for custom |
| 1418 | // expansion. |
| 1419 | if (context != EXPAND_USER_DEFINED |
| 1420 | && context != EXPAND_USER_LIST |
| 1421 | && fname[i] == '\\' |
| 1422 | && ++i == len) |
| 1423 | break; |
| 1424 | |
| 1425 | switch (fname[i]) |
| 1426 | { |
| 1427 | case '*': retval[j++] = '.'; |
| 1428 | break; |
| 1429 | case '~': retval[j++] = '\\'; |
| 1430 | break; |
| 1431 | case '?': retval[j] = '.'; |
| 1432 | continue; |
| 1433 | case '.': if (context == EXPAND_BUFFERS) |
| 1434 | retval[j++] = '\\'; |
| 1435 | break; |
| 1436 | case '\\': if (context == EXPAND_USER_DEFINED |
| 1437 | || context == EXPAND_USER_LIST) |
| 1438 | retval[j++] = '\\'; |
| 1439 | break; |
| 1440 | } |
| 1441 | retval[j] = fname[i]; |
| 1442 | } |
| 1443 | retval[j] = NUL; |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | else |
| 1448 | { |
| 1449 | retval = alloc(len + 4); |
| 1450 | if (retval != NULL) |
| 1451 | { |
| 1452 | vim_strncpy(retval, fname, len); |
| 1453 | |
| 1454 | // Don't add a star to *, ~, ~user, $var or `cmd`. |
| 1455 | // * would become **, which walks the whole tree. |
| 1456 | // ~ would be at the start of the file name, but not the tail. |
| 1457 | // $ could be anywhere in the tail. |
| 1458 | // ` could be anywhere in the file name. |
| 1459 | // When the name ends in '$' don't add a star, remove the '$'. |
| 1460 | tail = gettail(retval); |
| 1461 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 1462 | #ifndef BACKSLASH_IN_FILENAME |
| 1463 | for (i = len - 2; i >= 0; --i) |
| 1464 | { |
| 1465 | if (retval[i] != '\\') |
| 1466 | break; |
| 1467 | ends_in_star = !ends_in_star; |
| 1468 | } |
| 1469 | #endif |
| 1470 | if ((*retval != '~' || tail != retval) |
| 1471 | && !ends_in_star |
| 1472 | && vim_strchr(tail, '$') == NULL |
| 1473 | && vim_strchr(retval, '`') == NULL) |
| 1474 | retval[len++] = '*'; |
| 1475 | else if (len > 0 && retval[len - 1] == '$') |
| 1476 | --len; |
| 1477 | retval[len] = NUL; |
| 1478 | } |
| 1479 | } |
| 1480 | return retval; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * Must parse the command line so far to work out what context we are in. |
| 1485 | * Completion can then be done based on that context. |
| 1486 | * This routine sets the variables: |
| 1487 | * xp->xp_pattern The start of the pattern to be expanded within |
| 1488 | * the command line (ends at the cursor). |
| 1489 | * xp->xp_context The type of thing to expand. Will be one of: |
| 1490 | * |
| 1491 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 1492 | * the command line, like an unknown command. Caller |
| 1493 | * should beep. |
| 1494 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 1495 | * a normal char, rather than for completion. eg |
| 1496 | * :s/^I/ |
| 1497 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 1498 | * it. |
| 1499 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 1500 | * EXPAND_FILES After command with EX_XFILE set, or after setting |
| 1501 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 1502 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 1503 | * when we know only directories are of interest. |
| 1504 | * E.g. :set dir=^I and :cd ^I |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1505 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
| 1506 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 1507 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 1508 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 1509 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 1510 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 1511 | * EXPAND_EVENTS Complete event names |
| 1512 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 1513 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 1514 | * EXPAND_AUGROUP Complete autocommand group names |
| 1515 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 1516 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 1517 | * eg :unmap a^I , :cunab x^I |
| 1518 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 1519 | * eg :call sub^I |
| 1520 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 1521 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 1522 | * names in expressions, eg :while s^I |
| 1523 | * EXPAND_ENV_VARS Complete environment variable names |
| 1524 | * EXPAND_USER Complete user names |
| 1525 | */ |
Shougo Matsushita | 79d599b | 2022-05-07 12:48:29 +0100 | [diff] [blame] | 1526 | void |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1527 | set_expand_context(expand_T *xp) |
| 1528 | { |
| 1529 | cmdline_info_T *ccline = get_cmdline_info(); |
| 1530 | |
| 1531 | // only expansion for ':', '>' and '=' command-lines |
| 1532 | if (ccline->cmdfirstc != ':' |
| 1533 | #ifdef FEAT_EVAL |
| 1534 | && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '=' |
| 1535 | && !ccline->input_fn |
| 1536 | #endif |
| 1537 | ) |
| 1538 | { |
| 1539 | xp->xp_context = EXPAND_NOTHING; |
| 1540 | return; |
| 1541 | } |
| 1542 | set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE); |
| 1543 | } |
| 1544 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1545 | /* |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1546 | * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx. |
| 1547 | * For user defined commands, the completion context is set in 'xp' and the |
| 1548 | * completion flags in 'complp'. |
| 1549 | * |
| 1550 | * Returns a pointer to the text after the command or NULL for failure. |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1551 | */ |
| 1552 | static char_u * |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1553 | set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1554 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1555 | char_u *p = NULL; |
| 1556 | int len = 0; |
Yegappan Lakshmanan | 4df5b33 | 2022-02-26 11:04:42 +0000 | [diff] [blame] | 1557 | int fuzzy = cmdline_fuzzy_complete(cmd); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1558 | |
| 1559 | // Isolate the command and search for it in the command table. |
| 1560 | // Exceptions: |
Yegappan Lakshmanan | 6caeda2 | 2022-02-27 12:07:30 +0000 | [diff] [blame] | 1561 | // - the 'k' command can directly be followed by any character, but do |
| 1562 | // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can |
| 1563 | // find matches anywhere in the command name, do this only for command |
| 1564 | // expansion based on regular expression and not for fuzzy matching. |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1565 | // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' |
Yegappan Lakshmanan | 6caeda2 | 2022-02-27 12:07:30 +0000 | [diff] [blame] | 1566 | if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e')) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1567 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1568 | eap->cmdidx = CMD_k; |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1569 | p = cmd + 1; |
| 1570 | } |
| 1571 | else |
| 1572 | { |
| 1573 | p = cmd; |
| 1574 | while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card |
| 1575 | ++p; |
Bram Moolenaar | df749a2 | 2021-03-28 15:29:43 +0200 | [diff] [blame] | 1576 | // A user command may contain digits. |
| 1577 | // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script". |
| 1578 | if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1579 | while (ASCII_ISALNUM(*p) || *p == '*') |
| 1580 | ++p; |
| 1581 | // for python 3.x: ":py3*" commands completion |
| 1582 | if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3') |
| 1583 | { |
| 1584 | ++p; |
| 1585 | while (ASCII_ISALPHA(*p) || *p == '*') |
| 1586 | ++p; |
| 1587 | } |
| 1588 | // check for non-alpha command |
| 1589 | if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) |
| 1590 | ++p; |
| 1591 | len = (int)(p - cmd); |
| 1592 | |
| 1593 | if (len == 0) |
| 1594 | { |
| 1595 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1599 | eap->cmdidx = excmd_get_cmdidx(cmd, len); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1600 | |
Yegappan Lakshmanan | 4df5b33 | 2022-02-26 11:04:42 +0000 | [diff] [blame] | 1601 | // User defined commands support alphanumeric characters. |
Yegappan Lakshmanan | 7db3a8e | 2022-07-26 22:01:36 +0100 | [diff] [blame] | 1602 | // Also when doing fuzzy expansion for non-shell commands, support |
| 1603 | // alphanumeric characters. |
| 1604 | if ((cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 1605 | || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL)) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1606 | while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card |
| 1607 | ++p; |
| 1608 | } |
| 1609 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 1610 | // If the cursor is touching the command, and it ends in an alphanumeric |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1611 | // character, complete the command name. |
| 1612 | if (*p == NUL && ASCII_ISALNUM(p[-1])) |
| 1613 | return NULL; |
| 1614 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1615 | if (eap->cmdidx == CMD_SIZE) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1616 | { |
| 1617 | if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) |
| 1618 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1619 | eap->cmdidx = CMD_substitute; |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1620 | p = cmd + 1; |
| 1621 | } |
| 1622 | else if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 1623 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1624 | eap->cmd = cmd; |
| 1625 | p = find_ucmd(eap, p, NULL, xp, complp); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1626 | if (p == NULL) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1627 | eap->cmdidx = CMD_SIZE; // ambiguous user command |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1628 | } |
| 1629 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1630 | if (eap->cmdidx == CMD_SIZE) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1631 | { |
| 1632 | // Not still touching the command and it was an illegal one |
| 1633 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1637 | return p; |
| 1638 | } |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1639 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1640 | /* |
| 1641 | * Set the completion context for a command argument with wild card characters. |
| 1642 | */ |
| 1643 | static void |
| 1644 | set_context_for_wildcard_arg( |
| 1645 | exarg_T *eap, |
| 1646 | char_u *arg, |
| 1647 | int usefilter, |
| 1648 | expand_T *xp, |
| 1649 | int *complp) |
| 1650 | { |
| 1651 | char_u *p; |
| 1652 | int c; |
| 1653 | int in_quote = FALSE; |
| 1654 | char_u *bow = NULL; // Beginning of word |
| 1655 | int len = 0; |
| 1656 | |
| 1657 | // Allow spaces within back-quotes to count as part of the argument |
| 1658 | // being expanded. |
| 1659 | xp->xp_pattern = skipwhite(arg); |
| 1660 | p = xp->xp_pattern; |
| 1661 | while (*p != NUL) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1662 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1663 | if (has_mbyte) |
| 1664 | c = mb_ptr2char(p); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1665 | else |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1666 | c = *p; |
| 1667 | if (c == '\\' && p[1] != NUL) |
| 1668 | ++p; |
| 1669 | else if (c == '`') |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1670 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1671 | if (!in_quote) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1672 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1673 | xp->xp_pattern = p; |
| 1674 | bow = p + 1; |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1675 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1676 | in_quote = !in_quote; |
| 1677 | } |
| 1678 | // An argument can contain just about everything, except |
| 1679 | // characters that end the command and white space. |
| 1680 | else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1681 | #ifdef SPACE_IN_FILENAME |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1682 | && (!(eap->argt & EX_NOSPC) || usefilter) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1683 | #endif |
| 1684 | )) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1685 | { |
| 1686 | len = 0; // avoid getting stuck when space is in 'isfname' |
| 1687 | while (*p != NUL) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1688 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1689 | if (has_mbyte) |
| 1690 | c = mb_ptr2char(p); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1691 | else |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1692 | c = *p; |
| 1693 | if (c == '`' || vim_isfilec_or_wc(c)) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1694 | break; |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1695 | if (has_mbyte) |
| 1696 | len = (*mb_ptr2len)(p); |
| 1697 | else |
| 1698 | len = 1; |
| 1699 | MB_PTR_ADV(p); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1700 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1701 | if (in_quote) |
| 1702 | bow = p; |
| 1703 | else |
| 1704 | xp->xp_pattern = p; |
| 1705 | p -= len; |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1706 | } |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1707 | MB_PTR_ADV(p); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 1710 | // If we are still inside the quotes, and we passed a space, just |
| 1711 | // expand from there. |
| 1712 | if (bow != NULL && in_quote) |
| 1713 | xp->xp_pattern = bow; |
| 1714 | xp->xp_context = EXPAND_FILES; |
| 1715 | |
| 1716 | // For a shell command more chars need to be escaped. |
| 1717 | if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal) |
| 1718 | { |
| 1719 | #ifndef BACKSLASH_IN_FILENAME |
| 1720 | xp->xp_shell = TRUE; |
| 1721 | #endif |
| 1722 | // When still after the command name expand executables. |
| 1723 | if (xp->xp_pattern == skipwhite(arg)) |
| 1724 | xp->xp_context = EXPAND_SHELLCMD; |
| 1725 | } |
| 1726 | |
| 1727 | // Check for environment variable. |
| 1728 | if (*xp->xp_pattern == '$') |
| 1729 | { |
| 1730 | for (p = xp->xp_pattern + 1; *p != NUL; ++p) |
| 1731 | if (!vim_isIDc(*p)) |
| 1732 | break; |
| 1733 | if (*p == NUL) |
| 1734 | { |
| 1735 | xp->xp_context = EXPAND_ENV_VARS; |
| 1736 | ++xp->xp_pattern; |
| 1737 | // Avoid that the assignment uses EXPAND_FILES again. |
| 1738 | if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST) |
| 1739 | *complp = EXPAND_ENV_VARS; |
| 1740 | } |
| 1741 | } |
| 1742 | // Check for user names. |
| 1743 | if (*xp->xp_pattern == '~') |
| 1744 | { |
| 1745 | for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p) |
| 1746 | ; |
| 1747 | // Complete ~user only if it partially matches a user name. |
| 1748 | // A full match ~user<Tab> will be replaced by user's home |
| 1749 | // directory i.e. something like ~user<Tab> -> /home/user/ |
| 1750 | if (*p == NUL && p > xp->xp_pattern + 1 |
| 1751 | && match_user(xp->xp_pattern + 1) >= 1) |
| 1752 | { |
| 1753 | xp->xp_context = EXPAND_USER; |
| 1754 | ++xp->xp_pattern; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /* |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1760 | * Set the completion context for the :filter command. Returns a pointer to the |
| 1761 | * next command after the :filter command. |
| 1762 | */ |
| 1763 | static char_u * |
| 1764 | set_context_in_filter_cmd(expand_T *xp, char_u *arg) |
| 1765 | { |
| 1766 | if (*arg != NUL) |
| 1767 | arg = skip_vimgrep_pat(arg, NULL, NULL); |
| 1768 | if (arg == NULL || *arg == NUL) |
| 1769 | { |
| 1770 | xp->xp_context = EXPAND_NOTHING; |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | return skipwhite(arg); |
| 1774 | } |
| 1775 | |
| 1776 | #ifdef FEAT_SEARCH_EXTRA |
| 1777 | /* |
| 1778 | * Set the completion context for the :match command. Returns a pointer to the |
| 1779 | * next command after the :match command. |
| 1780 | */ |
| 1781 | static char_u * |
| 1782 | set_context_in_match_cmd(expand_T *xp, char_u *arg) |
| 1783 | { |
| 1784 | if (*arg == NUL || !ends_excmd(*arg)) |
| 1785 | { |
| 1786 | // also complete "None" |
| 1787 | set_context_in_echohl_cmd(xp, arg); |
| 1788 | arg = skipwhite(skiptowhite(arg)); |
| 1789 | if (*arg != NUL) |
| 1790 | { |
| 1791 | xp->xp_context = EXPAND_NOTHING; |
| 1792 | arg = skip_regexp(arg + 1, *arg, magic_isset()); |
| 1793 | } |
| 1794 | } |
| 1795 | return find_nextcmd(arg); |
| 1796 | } |
| 1797 | #endif |
| 1798 | |
| 1799 | /* |
| 1800 | * Returns a pointer to the next command after a :global or a :v command. |
| 1801 | * Returns NULL if there is no next command. |
| 1802 | */ |
| 1803 | static char_u * |
| 1804 | find_cmd_after_global_cmd(char_u *arg) |
| 1805 | { |
| 1806 | int delim; |
| 1807 | |
| 1808 | delim = *arg; // get the delimiter |
| 1809 | if (delim) |
| 1810 | ++arg; // skip delimiter if there is one |
| 1811 | |
| 1812 | while (arg[0] != NUL && arg[0] != delim) |
| 1813 | { |
| 1814 | if (arg[0] == '\\' && arg[1] != NUL) |
| 1815 | ++arg; |
| 1816 | ++arg; |
| 1817 | } |
| 1818 | if (arg[0] != NUL) |
| 1819 | return arg + 1; |
| 1820 | |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | |
| 1824 | /* |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1825 | * Returns a pointer to the next command after a :substitute or a :& command. |
| 1826 | * Returns NULL if there is no next command. |
| 1827 | */ |
| 1828 | static char_u * |
| 1829 | find_cmd_after_substitute_cmd(char_u *arg) |
| 1830 | { |
| 1831 | int delim; |
| 1832 | |
| 1833 | delim = *arg; |
| 1834 | if (delim) |
| 1835 | { |
| 1836 | // skip "from" part |
| 1837 | ++arg; |
| 1838 | arg = skip_regexp(arg, delim, magic_isset()); |
| 1839 | |
| 1840 | if (arg[0] != NUL && arg[0] == delim) |
| 1841 | { |
| 1842 | // skip "to" part |
| 1843 | ++arg; |
| 1844 | while (arg[0] != NUL && arg[0] != delim) |
| 1845 | { |
| 1846 | if (arg[0] == '\\' && arg[1] != NUL) |
| 1847 | ++arg; |
| 1848 | ++arg; |
| 1849 | } |
| 1850 | if (arg[0] != NUL) // skip delimiter |
| 1851 | ++arg; |
| 1852 | } |
| 1853 | } |
| 1854 | while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL) |
| 1855 | ++arg; |
| 1856 | if (arg[0] != NUL) |
| 1857 | return arg; |
| 1858 | |
| 1859 | return NULL; |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * Returns a pointer to the next command after a :isearch/:dsearch/:ilist |
| 1864 | * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command. |
| 1865 | * Returns NULL if there is no next command. |
| 1866 | */ |
| 1867 | static char_u * |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1868 | find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg) |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1869 | { |
| 1870 | arg = skipwhite(skipdigits(arg)); // skip count |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1871 | if (*arg != '/') |
| 1872 | return NULL; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1873 | |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1874 | // Match regexp, not just whole words |
| 1875 | for (++arg; *arg && *arg != '/'; arg++) |
| 1876 | if (*arg == '\\' && arg[1] != NUL) |
| 1877 | arg++; |
| 1878 | if (*arg) |
| 1879 | { |
| 1880 | arg = skipwhite(arg + 1); |
| 1881 | |
| 1882 | // Check for trailing illegal characters |
| 1883 | if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL) |
| 1884 | xp->xp_context = EXPAND_NOTHING; |
| 1885 | else |
| 1886 | return arg; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 1892 | #ifdef FEAT_EVAL |
| 1893 | /* |
| 1894 | * Set the completion context for the :unlet command. Always returns NULL. |
| 1895 | */ |
| 1896 | static char_u * |
| 1897 | set_context_in_unlet_cmd(expand_T *xp, char_u *arg) |
| 1898 | { |
| 1899 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 1900 | arg = xp->xp_pattern + 1; |
| 1901 | |
| 1902 | xp->xp_context = EXPAND_USER_VARS; |
| 1903 | xp->xp_pattern = arg; |
| 1904 | |
| 1905 | if (*xp->xp_pattern == '$') |
| 1906 | { |
| 1907 | xp->xp_context = EXPAND_ENV_VARS; |
| 1908 | ++xp->xp_pattern; |
| 1909 | } |
| 1910 | |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | #endif |
| 1914 | |
| 1915 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 1916 | /* |
| 1917 | * Set the completion context for the :language command. Always returns NULL. |
| 1918 | */ |
| 1919 | static char_u * |
| 1920 | set_context_in_lang_cmd(expand_T *xp, char_u *arg) |
| 1921 | { |
| 1922 | char_u *p; |
| 1923 | |
| 1924 | p = skiptowhite(arg); |
| 1925 | if (*p == NUL) |
| 1926 | { |
| 1927 | xp->xp_context = EXPAND_LANGUAGE; |
| 1928 | xp->xp_pattern = arg; |
| 1929 | } |
| 1930 | else |
| 1931 | { |
| 1932 | if ( STRNCMP(arg, "messages", p - arg) == 0 |
| 1933 | || STRNCMP(arg, "ctype", p - arg) == 0 |
| 1934 | || STRNCMP(arg, "time", p - arg) == 0 |
| 1935 | || STRNCMP(arg, "collate", p - arg) == 0) |
| 1936 | { |
| 1937 | xp->xp_context = EXPAND_LOCALES; |
| 1938 | xp->xp_pattern = skipwhite(p); |
| 1939 | } |
| 1940 | else |
| 1941 | xp->xp_context = EXPAND_NOTHING; |
| 1942 | } |
| 1943 | |
| 1944 | return NULL; |
| 1945 | } |
| 1946 | #endif |
| 1947 | |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 1948 | #ifdef FEAT_EVAL |
| 1949 | static enum |
| 1950 | { |
| 1951 | EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 1952 | EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands |
| 1953 | EXP_PROFDEL // expand ":profdel" sub-commands |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 1954 | } breakpt_expand_what; |
| 1955 | |
| 1956 | /* |
| 1957 | * Set the completion context for the :breakadd command. Always returns NULL. |
| 1958 | */ |
| 1959 | static char_u * |
| 1960 | set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx) |
| 1961 | { |
| 1962 | char_u *p; |
| 1963 | char_u *subcmd_start; |
| 1964 | |
| 1965 | xp->xp_context = EXPAND_BREAKPOINT; |
| 1966 | xp->xp_pattern = arg; |
| 1967 | |
| 1968 | if (cmdidx == CMD_breakadd) |
| 1969 | breakpt_expand_what = EXP_BREAKPT_ADD; |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 1970 | else if (cmdidx == CMD_breakdel) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 1971 | breakpt_expand_what = EXP_BREAKPT_DEL; |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 1972 | else |
| 1973 | breakpt_expand_what = EXP_PROFDEL; |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 1974 | |
| 1975 | p = skipwhite(arg); |
| 1976 | if (*p == NUL) |
| 1977 | return NULL; |
| 1978 | subcmd_start = p; |
| 1979 | |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 1980 | if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 1981 | { |
| 1982 | // :breakadd file [lnum] <filename> |
| 1983 | // :breakadd func [lnum] <funcname> |
| 1984 | p += 4; |
| 1985 | p = skipwhite(p); |
| 1986 | |
| 1987 | // skip line number (if specified) |
| 1988 | if (VIM_ISDIGIT(*p)) |
| 1989 | { |
| 1990 | p = skipdigits(p); |
| 1991 | if (*p != ' ') |
| 1992 | { |
| 1993 | xp->xp_context = EXPAND_NOTHING; |
| 1994 | return NULL; |
| 1995 | } |
| 1996 | p = skipwhite(p); |
| 1997 | } |
| 1998 | if (STRNCMP("file", subcmd_start, 4) == 0) |
| 1999 | xp->xp_context = EXPAND_FILES; |
| 2000 | else |
| 2001 | xp->xp_context = EXPAND_USER_FUNC; |
| 2002 | xp->xp_pattern = p; |
| 2003 | } |
| 2004 | else if (STRNCMP("expr ", p, 5) == 0) |
| 2005 | { |
| 2006 | // :breakadd expr <expression> |
| 2007 | xp->xp_context = EXPAND_EXPRESSION; |
| 2008 | xp->xp_pattern = skipwhite(p + 5); |
| 2009 | } |
| 2010 | |
| 2011 | return NULL; |
| 2012 | } |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 2013 | |
| 2014 | static char_u * |
| 2015 | set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg) |
| 2016 | { |
| 2017 | char_u *p; |
| 2018 | |
| 2019 | xp->xp_context = EXPAND_NOTHING; |
| 2020 | xp->xp_pattern = NULL; |
| 2021 | |
| 2022 | p = skipwhite(arg); |
| 2023 | if (VIM_ISDIGIT(*p)) |
| 2024 | return NULL; |
| 2025 | |
| 2026 | xp->xp_context = EXPAND_SCRIPTNAMES; |
| 2027 | xp->xp_pattern = p; |
| 2028 | |
| 2029 | return NULL; |
| 2030 | } |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2031 | #endif |
| 2032 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 2033 | /* |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2034 | * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'. |
| 2035 | * The argument to the command is 'arg' and the argument flags is 'argt'. |
| 2036 | * For user-defined commands and for environment variables, 'compl' has the |
| 2037 | * completion type. |
| 2038 | * Returns a pointer to the next command. Returns NULL if there is no next |
| 2039 | * command. |
| 2040 | */ |
| 2041 | static char_u * |
| 2042 | set_context_by_cmdname( |
| 2043 | char_u *cmd, |
| 2044 | cmdidx_T cmdidx, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2045 | expand_T *xp, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2046 | char_u *arg, |
| 2047 | long argt, |
| 2048 | int compl, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2049 | int forceit) |
| 2050 | { |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2051 | switch (cmdidx) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2052 | { |
| 2053 | case CMD_find: |
| 2054 | case CMD_sfind: |
| 2055 | case CMD_tabfind: |
| 2056 | if (xp->xp_context == EXPAND_FILES) |
| 2057 | xp->xp_context = EXPAND_FILES_IN_PATH; |
| 2058 | break; |
| 2059 | case CMD_cd: |
| 2060 | case CMD_chdir: |
| 2061 | case CMD_tcd: |
| 2062 | case CMD_tchdir: |
| 2063 | case CMD_lcd: |
| 2064 | case CMD_lchdir: |
| 2065 | if (xp->xp_context == EXPAND_FILES) |
| 2066 | xp->xp_context = EXPAND_DIRECTORIES; |
| 2067 | break; |
| 2068 | case CMD_help: |
| 2069 | xp->xp_context = EXPAND_HELP; |
| 2070 | xp->xp_pattern = arg; |
| 2071 | break; |
| 2072 | |
| 2073 | // Command modifiers: return the argument. |
| 2074 | // Also for commands with an argument that is a command. |
| 2075 | case CMD_aboveleft: |
| 2076 | case CMD_argdo: |
| 2077 | case CMD_belowright: |
| 2078 | case CMD_botright: |
| 2079 | case CMD_browse: |
| 2080 | case CMD_bufdo: |
| 2081 | case CMD_cdo: |
| 2082 | case CMD_cfdo: |
| 2083 | case CMD_confirm: |
| 2084 | case CMD_debug: |
| 2085 | case CMD_folddoclosed: |
| 2086 | case CMD_folddoopen: |
| 2087 | case CMD_hide: |
zeertzjq | d3de178 | 2022-09-01 12:58:52 +0100 | [diff] [blame] | 2088 | case CMD_horizontal: |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2089 | case CMD_keepalt: |
| 2090 | case CMD_keepjumps: |
| 2091 | case CMD_keepmarks: |
| 2092 | case CMD_keeppatterns: |
| 2093 | case CMD_ldo: |
| 2094 | case CMD_leftabove: |
| 2095 | case CMD_lfdo: |
| 2096 | case CMD_lockmarks: |
| 2097 | case CMD_noautocmd: |
| 2098 | case CMD_noswapfile: |
| 2099 | case CMD_rightbelow: |
| 2100 | case CMD_sandbox: |
| 2101 | case CMD_silent: |
| 2102 | case CMD_tab: |
| 2103 | case CMD_tabdo: |
| 2104 | case CMD_topleft: |
| 2105 | case CMD_verbose: |
| 2106 | case CMD_vertical: |
| 2107 | case CMD_windo: |
Bram Moolenaar | e70e12b | 2021-06-13 17:20:08 +0200 | [diff] [blame] | 2108 | case CMD_vim9cmd: |
| 2109 | case CMD_legacy: |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2110 | return arg; |
| 2111 | |
| 2112 | case CMD_filter: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2113 | return set_context_in_filter_cmd(xp, arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2114 | |
| 2115 | #ifdef FEAT_SEARCH_EXTRA |
| 2116 | case CMD_match: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2117 | return set_context_in_match_cmd(xp, arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2118 | #endif |
| 2119 | |
| 2120 | // All completion for the +cmdline_compl feature goes here. |
| 2121 | |
| 2122 | case CMD_command: |
| 2123 | return set_context_in_user_cmd(xp, arg); |
| 2124 | |
| 2125 | case CMD_delcommand: |
| 2126 | xp->xp_context = EXPAND_USER_COMMANDS; |
| 2127 | xp->xp_pattern = arg; |
| 2128 | break; |
| 2129 | |
| 2130 | case CMD_global: |
| 2131 | case CMD_vglobal: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2132 | return find_cmd_after_global_cmd(arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2133 | case CMD_and: |
| 2134 | case CMD_substitute: |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 2135 | return find_cmd_after_substitute_cmd(arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2136 | case CMD_isearch: |
| 2137 | case CMD_dsearch: |
| 2138 | case CMD_ilist: |
| 2139 | case CMD_dlist: |
| 2140 | case CMD_ijump: |
| 2141 | case CMD_psearch: |
| 2142 | case CMD_djump: |
| 2143 | case CMD_isplit: |
| 2144 | case CMD_dsplit: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2145 | return find_cmd_after_isearch_cmd(xp, arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2146 | case CMD_autocmd: |
| 2147 | return set_context_in_autocmd(xp, arg, FALSE); |
| 2148 | case CMD_doautocmd: |
| 2149 | case CMD_doautoall: |
| 2150 | return set_context_in_autocmd(xp, arg, TRUE); |
| 2151 | case CMD_set: |
| 2152 | set_context_in_set_cmd(xp, arg, 0); |
| 2153 | break; |
| 2154 | case CMD_setglobal: |
| 2155 | set_context_in_set_cmd(xp, arg, OPT_GLOBAL); |
| 2156 | break; |
| 2157 | case CMD_setlocal: |
| 2158 | set_context_in_set_cmd(xp, arg, OPT_LOCAL); |
| 2159 | break; |
| 2160 | case CMD_tag: |
| 2161 | case CMD_stag: |
| 2162 | case CMD_ptag: |
| 2163 | case CMD_ltag: |
| 2164 | case CMD_tselect: |
| 2165 | case CMD_stselect: |
| 2166 | case CMD_ptselect: |
| 2167 | case CMD_tjump: |
| 2168 | case CMD_stjump: |
| 2169 | case CMD_ptjump: |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 2170 | if (vim_strchr(p_wop, WOP_TAGFILE) != NULL) |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2171 | xp->xp_context = EXPAND_TAGS_LISTFILES; |
| 2172 | else |
| 2173 | xp->xp_context = EXPAND_TAGS; |
| 2174 | xp->xp_pattern = arg; |
| 2175 | break; |
| 2176 | case CMD_augroup: |
| 2177 | xp->xp_context = EXPAND_AUGROUP; |
| 2178 | xp->xp_pattern = arg; |
| 2179 | break; |
| 2180 | #ifdef FEAT_SYN_HL |
| 2181 | case CMD_syntax: |
| 2182 | set_context_in_syntax_cmd(xp, arg); |
| 2183 | break; |
| 2184 | #endif |
| 2185 | #ifdef FEAT_EVAL |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 2186 | case CMD_final: |
Bram Moolenaar | 8f76e6b | 2019-11-26 16:50:30 +0100 | [diff] [blame] | 2187 | case CMD_const: |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2188 | case CMD_let: |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 2189 | case CMD_var: |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2190 | case CMD_if: |
| 2191 | case CMD_elseif: |
| 2192 | case CMD_while: |
| 2193 | case CMD_for: |
| 2194 | case CMD_echo: |
| 2195 | case CMD_echon: |
| 2196 | case CMD_execute: |
| 2197 | case CMD_echomsg: |
| 2198 | case CMD_echoerr: |
| 2199 | case CMD_call: |
| 2200 | case CMD_return: |
| 2201 | case CMD_cexpr: |
| 2202 | case CMD_caddexpr: |
| 2203 | case CMD_cgetexpr: |
| 2204 | case CMD_lexpr: |
| 2205 | case CMD_laddexpr: |
| 2206 | case CMD_lgetexpr: |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2207 | set_context_for_expression(xp, arg, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2208 | break; |
| 2209 | |
| 2210 | case CMD_unlet: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2211 | return set_context_in_unlet_cmd(xp, arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2212 | case CMD_function: |
| 2213 | case CMD_delfunction: |
| 2214 | xp->xp_context = EXPAND_USER_FUNC; |
| 2215 | xp->xp_pattern = arg; |
| 2216 | break; |
Bram Moolenaar | 4ee9d8e | 2021-06-13 18:38:48 +0200 | [diff] [blame] | 2217 | case CMD_disassemble: |
| 2218 | set_context_in_disassemble_cmd(xp, arg); |
| 2219 | break; |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2220 | |
| 2221 | case CMD_echohl: |
| 2222 | set_context_in_echohl_cmd(xp, arg); |
| 2223 | break; |
| 2224 | #endif |
| 2225 | case CMD_highlight: |
| 2226 | set_context_in_highlight_cmd(xp, arg); |
| 2227 | break; |
| 2228 | #ifdef FEAT_CSCOPE |
| 2229 | case CMD_cscope: |
| 2230 | case CMD_lcscope: |
| 2231 | case CMD_scscope: |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2232 | set_context_in_cscope_cmd(xp, arg, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2233 | break; |
| 2234 | #endif |
| 2235 | #ifdef FEAT_SIGNS |
| 2236 | case CMD_sign: |
| 2237 | set_context_in_sign_cmd(xp, arg); |
| 2238 | break; |
| 2239 | #endif |
| 2240 | case CMD_bdelete: |
| 2241 | case CMD_bwipeout: |
| 2242 | case CMD_bunload: |
| 2243 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 2244 | arg = xp->xp_pattern + 1; |
| 2245 | // FALLTHROUGH |
| 2246 | case CMD_buffer: |
| 2247 | case CMD_sbuffer: |
| 2248 | case CMD_checktime: |
| 2249 | xp->xp_context = EXPAND_BUFFERS; |
| 2250 | xp->xp_pattern = arg; |
| 2251 | break; |
Bram Moolenaar | ae7dba8 | 2019-12-29 13:56:33 +0100 | [diff] [blame] | 2252 | #ifdef FEAT_DIFF |
| 2253 | case CMD_diffget: |
| 2254 | case CMD_diffput: |
| 2255 | // If current buffer is in diff mode, complete buffer names |
| 2256 | // which are in diff mode, and different than current buffer. |
| 2257 | xp->xp_context = EXPAND_DIFF_BUFFERS; |
| 2258 | xp->xp_pattern = arg; |
| 2259 | break; |
| 2260 | #endif |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2261 | case CMD_USER: |
| 2262 | case CMD_USER_BUF: |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 2263 | return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp, |
| 2264 | forceit); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2265 | |
| 2266 | case CMD_map: case CMD_noremap: |
| 2267 | case CMD_nmap: case CMD_nnoremap: |
| 2268 | case CMD_vmap: case CMD_vnoremap: |
| 2269 | case CMD_omap: case CMD_onoremap: |
| 2270 | case CMD_imap: case CMD_inoremap: |
| 2271 | case CMD_cmap: case CMD_cnoremap: |
| 2272 | case CMD_lmap: case CMD_lnoremap: |
| 2273 | case CMD_smap: case CMD_snoremap: |
| 2274 | case CMD_tmap: case CMD_tnoremap: |
| 2275 | case CMD_xmap: case CMD_xnoremap: |
| 2276 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2277 | FALSE, FALSE, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2278 | case CMD_unmap: |
| 2279 | case CMD_nunmap: |
| 2280 | case CMD_vunmap: |
| 2281 | case CMD_ounmap: |
| 2282 | case CMD_iunmap: |
| 2283 | case CMD_cunmap: |
| 2284 | case CMD_lunmap: |
| 2285 | case CMD_sunmap: |
| 2286 | case CMD_tunmap: |
| 2287 | case CMD_xunmap: |
| 2288 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2289 | FALSE, TRUE, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2290 | case CMD_mapclear: |
| 2291 | case CMD_nmapclear: |
| 2292 | case CMD_vmapclear: |
| 2293 | case CMD_omapclear: |
| 2294 | case CMD_imapclear: |
| 2295 | case CMD_cmapclear: |
| 2296 | case CMD_lmapclear: |
| 2297 | case CMD_smapclear: |
| 2298 | case CMD_tmapclear: |
| 2299 | case CMD_xmapclear: |
| 2300 | xp->xp_context = EXPAND_MAPCLEAR; |
| 2301 | xp->xp_pattern = arg; |
| 2302 | break; |
| 2303 | |
| 2304 | case CMD_abbreviate: case CMD_noreabbrev: |
| 2305 | case CMD_cabbrev: case CMD_cnoreabbrev: |
| 2306 | case CMD_iabbrev: case CMD_inoreabbrev: |
| 2307 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2308 | TRUE, FALSE, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2309 | case CMD_unabbreviate: |
| 2310 | case CMD_cunabbrev: |
| 2311 | case CMD_iunabbrev: |
| 2312 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2313 | TRUE, TRUE, cmdidx); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2314 | #ifdef FEAT_MENU |
| 2315 | case CMD_menu: case CMD_noremenu: case CMD_unmenu: |
| 2316 | case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: |
| 2317 | case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: |
| 2318 | case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: |
| 2319 | case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: |
| 2320 | case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: |
| 2321 | case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: |
| 2322 | case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu: |
| 2323 | case CMD_tmenu: case CMD_tunmenu: |
| 2324 | case CMD_popup: case CMD_tearoff: case CMD_emenu: |
| 2325 | return set_context_in_menu_cmd(xp, cmd, arg, forceit); |
| 2326 | #endif |
| 2327 | |
| 2328 | case CMD_colorscheme: |
| 2329 | xp->xp_context = EXPAND_COLORS; |
| 2330 | xp->xp_pattern = arg; |
| 2331 | break; |
| 2332 | |
| 2333 | case CMD_compiler: |
| 2334 | xp->xp_context = EXPAND_COMPILER; |
| 2335 | xp->xp_pattern = arg; |
| 2336 | break; |
| 2337 | |
| 2338 | case CMD_ownsyntax: |
| 2339 | xp->xp_context = EXPAND_OWNSYNTAX; |
| 2340 | xp->xp_pattern = arg; |
| 2341 | break; |
| 2342 | |
| 2343 | case CMD_setfiletype: |
| 2344 | xp->xp_context = EXPAND_FILETYPE; |
| 2345 | xp->xp_pattern = arg; |
| 2346 | break; |
| 2347 | |
| 2348 | case CMD_packadd: |
| 2349 | xp->xp_context = EXPAND_PACKADD; |
| 2350 | xp->xp_pattern = arg; |
| 2351 | break; |
| 2352 | |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 2353 | case CMD_runtime: |
| 2354 | set_context_in_runtime_cmd(xp, arg); |
| 2355 | break; |
| 2356 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2357 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 2358 | case CMD_language: |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2359 | return set_context_in_lang_cmd(xp, arg); |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2360 | #endif |
| 2361 | #if defined(FEAT_PROFILE) |
| 2362 | case CMD_profile: |
| 2363 | set_context_in_profile_cmd(xp, arg); |
| 2364 | break; |
| 2365 | #endif |
| 2366 | case CMD_behave: |
| 2367 | xp->xp_context = EXPAND_BEHAVE; |
| 2368 | xp->xp_pattern = arg; |
| 2369 | break; |
| 2370 | |
| 2371 | case CMD_messages: |
| 2372 | xp->xp_context = EXPAND_MESSAGES; |
| 2373 | xp->xp_pattern = arg; |
| 2374 | break; |
| 2375 | |
| 2376 | case CMD_history: |
| 2377 | xp->xp_context = EXPAND_HISTORY; |
| 2378 | xp->xp_pattern = arg; |
| 2379 | break; |
| 2380 | #if defined(FEAT_PROFILE) |
| 2381 | case CMD_syntime: |
| 2382 | xp->xp_context = EXPAND_SYNTIME; |
| 2383 | xp->xp_pattern = arg; |
| 2384 | break; |
| 2385 | #endif |
| 2386 | |
| 2387 | case CMD_argdelete: |
| 2388 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 2389 | arg = xp->xp_pattern + 1; |
| 2390 | xp->xp_context = EXPAND_ARGLIST; |
| 2391 | xp->xp_pattern = arg; |
| 2392 | break; |
| 2393 | |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2394 | #ifdef FEAT_EVAL |
| 2395 | case CMD_breakadd: |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 2396 | case CMD_profdel: |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2397 | case CMD_breakdel: |
| 2398 | return set_context_in_breakadd_cmd(xp, arg, cmdidx); |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 2399 | |
| 2400 | case CMD_scriptnames: |
| 2401 | return set_context_in_scriptnames_cmd(xp, arg); |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2402 | #endif |
| 2403 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2404 | default: |
| 2405 | break; |
| 2406 | } |
| 2407 | return NULL; |
| 2408 | } |
| 2409 | |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2410 | /* |
| 2411 | * This is all pretty much copied from do_one_cmd(), with all the extra stuff |
| 2412 | * we don't need/want deleted. Maybe this could be done better if we didn't |
| 2413 | * repeat all this stuff. The only problem is that they may not stay |
| 2414 | * perfectly compatible with each other, but then the command line syntax |
| 2415 | * probably won't change that much -- webb. |
| 2416 | */ |
| 2417 | static char_u * |
| 2418 | set_one_cmd_context( |
| 2419 | expand_T *xp, |
| 2420 | char_u *buff) // buffer for command string |
| 2421 | { |
| 2422 | char_u *p; |
| 2423 | char_u *cmd, *arg; |
| 2424 | int len = 0; |
| 2425 | exarg_T ea; |
| 2426 | int compl = EXPAND_NOTHING; |
| 2427 | int forceit = FALSE; |
| 2428 | int usefilter = FALSE; // filter instead of file name |
| 2429 | |
| 2430 | ExpandInit(xp); |
| 2431 | xp->xp_pattern = buff; |
| 2432 | xp->xp_line = buff; |
| 2433 | xp->xp_context = EXPAND_COMMANDS; // Default until we get past command |
| 2434 | ea.argt = 0; |
| 2435 | |
| 2436 | // 1. skip comment lines and leading space, colons or bars |
| 2437 | for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++) |
| 2438 | ; |
| 2439 | xp->xp_pattern = cmd; |
| 2440 | |
| 2441 | if (*cmd == NUL) |
| 2442 | return NULL; |
| 2443 | if (*cmd == '"') // ignore comment lines |
| 2444 | { |
| 2445 | xp->xp_context = EXPAND_NOTHING; |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | |
| 2449 | // 3. Skip over the range to find the command. |
| 2450 | cmd = skip_range(cmd, TRUE, &xp->xp_context); |
| 2451 | xp->xp_pattern = cmd; |
| 2452 | if (*cmd == NUL) |
| 2453 | return NULL; |
| 2454 | if (*cmd == '"') |
| 2455 | { |
| 2456 | xp->xp_context = EXPAND_NOTHING; |
| 2457 | return NULL; |
| 2458 | } |
| 2459 | |
| 2460 | if (*cmd == '|' || *cmd == '\n') |
| 2461 | return cmd + 1; // There's another command |
| 2462 | |
| 2463 | // Get the command index. |
| 2464 | p = set_cmd_index(cmd, &ea, xp, &compl); |
| 2465 | if (p == NULL) |
| 2466 | return NULL; |
| 2467 | |
| 2468 | xp->xp_context = EXPAND_NOTHING; // Default now that we're past command |
| 2469 | |
| 2470 | if (*p == '!') // forced commands |
| 2471 | { |
| 2472 | forceit = TRUE; |
| 2473 | ++p; |
| 2474 | } |
| 2475 | |
| 2476 | // 6. parse arguments |
| 2477 | if (!IS_USER_CMDIDX(ea.cmdidx)) |
| 2478 | ea.argt = excmd_get_argt(ea.cmdidx); |
| 2479 | |
| 2480 | arg = skipwhite(p); |
| 2481 | |
| 2482 | // Skip over ++argopt argument |
| 2483 | if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0) |
| 2484 | { |
| 2485 | p = arg; |
| 2486 | while (*p && !vim_isspace(*p)) |
| 2487 | MB_PTR_ADV(p); |
| 2488 | arg = skipwhite(p); |
| 2489 | } |
| 2490 | |
| 2491 | if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) |
| 2492 | { |
| 2493 | if (*arg == '>') // append |
| 2494 | { |
| 2495 | if (*++arg == '>') |
| 2496 | ++arg; |
| 2497 | arg = skipwhite(arg); |
| 2498 | } |
| 2499 | else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter |
| 2500 | { |
| 2501 | ++arg; |
| 2502 | usefilter = TRUE; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | if (ea.cmdidx == CMD_read) |
| 2507 | { |
| 2508 | usefilter = forceit; // :r! filter if forced |
| 2509 | if (*arg == '!') // :r !filter |
| 2510 | { |
| 2511 | ++arg; |
| 2512 | usefilter = TRUE; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) |
| 2517 | { |
| 2518 | while (*arg == *cmd) // allow any number of '>' or '<' |
| 2519 | ++arg; |
| 2520 | arg = skipwhite(arg); |
| 2521 | } |
| 2522 | |
| 2523 | // Does command allow "+command"? |
| 2524 | if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+') |
| 2525 | { |
| 2526 | // Check if we're in the +command |
| 2527 | p = arg + 1; |
| 2528 | arg = skip_cmd_arg(arg, FALSE); |
| 2529 | |
| 2530 | // Still touching the command after '+'? |
| 2531 | if (*arg == NUL) |
| 2532 | return p; |
| 2533 | |
| 2534 | // Skip space(s) after +command to get to the real argument |
| 2535 | arg = skipwhite(arg); |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | // Check for '|' to separate commands and '"' to start comments. |
| 2540 | // Don't do this for ":read !cmd" and ":write !cmd". |
| 2541 | if ((ea.argt & EX_TRLBAR) && !usefilter) |
| 2542 | { |
| 2543 | p = arg; |
| 2544 | // ":redir @" is not the start of a comment |
| 2545 | if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') |
| 2546 | p += 2; |
| 2547 | while (*p) |
| 2548 | { |
| 2549 | if (*p == Ctrl_V) |
| 2550 | { |
| 2551 | if (p[1] != NUL) |
| 2552 | ++p; |
| 2553 | } |
| 2554 | else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM)) |
| 2555 | || *p == '|' || *p == '\n') |
| 2556 | { |
| 2557 | if (*(p - 1) != '\\') |
| 2558 | { |
| 2559 | if (*p == '|' || *p == '\n') |
| 2560 | return p + 1; |
| 2561 | return NULL; // It's a comment |
| 2562 | } |
| 2563 | } |
| 2564 | MB_PTR_ADV(p); |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | if (!(ea.argt & EX_EXTRA) && *arg != NUL |
| 2569 | && vim_strchr((char_u *)"|\"", *arg) == NULL) |
| 2570 | // no arguments allowed but there is something |
| 2571 | return NULL; |
| 2572 | |
| 2573 | // Find start of last argument (argument just before cursor): |
| 2574 | p = buff; |
| 2575 | xp->xp_pattern = p; |
| 2576 | len = (int)STRLEN(buff); |
| 2577 | while (*p && p < buff + len) |
| 2578 | { |
| 2579 | if (*p == ' ' || *p == TAB) |
| 2580 | { |
| 2581 | // argument starts after a space |
| 2582 | xp->xp_pattern = ++p; |
| 2583 | } |
| 2584 | else |
| 2585 | { |
| 2586 | if (*p == '\\' && *(p + 1) != NUL) |
| 2587 | ++p; // skip over escaped character |
| 2588 | MB_PTR_ADV(p); |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | if (ea.argt & EX_XFILE) |
| 2593 | set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl); |
| 2594 | |
| 2595 | // 6. Switch on command name. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2596 | return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2597 | forceit); |
| 2598 | } |
| 2599 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2600 | /* |
| 2601 | * Set the completion context in 'xp' for command 'str' |
| 2602 | */ |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2603 | void |
| 2604 | set_cmd_context( |
| 2605 | expand_T *xp, |
| 2606 | char_u *str, // start of command line |
| 2607 | int len, // length of command line (excl. NUL) |
| 2608 | int col, // position of cursor |
| 2609 | int use_ccline UNUSED) // use ccline for info |
| 2610 | { |
| 2611 | #ifdef FEAT_EVAL |
| 2612 | cmdline_info_T *ccline = get_cmdline_info(); |
| 2613 | #endif |
| 2614 | int old_char = NUL; |
| 2615 | char_u *nextcomm; |
| 2616 | |
| 2617 | // Avoid a UMR warning from Purify, only save the character if it has been |
| 2618 | // written before. |
| 2619 | if (col < len) |
| 2620 | old_char = str[col]; |
| 2621 | str[col] = NUL; |
| 2622 | nextcomm = str; |
| 2623 | |
| 2624 | #ifdef FEAT_EVAL |
| 2625 | if (use_ccline && ccline->cmdfirstc == '=') |
| 2626 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2627 | // pass CMD_SIZE because there is no real command |
| 2628 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2629 | } |
| 2630 | else if (use_ccline && ccline->input_fn) |
| 2631 | { |
| 2632 | xp->xp_context = ccline->xp_context; |
| 2633 | xp->xp_pattern = ccline->cmdbuff; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2634 | xp->xp_arg = ccline->xp_arg; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2635 | } |
| 2636 | else |
| 2637 | #endif |
| 2638 | while (nextcomm != NULL) |
| 2639 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 2640 | |
| 2641 | // Store the string here so that call_user_expand_func() can get to them |
| 2642 | // easily. |
| 2643 | xp->xp_line = str; |
| 2644 | xp->xp_col = col; |
| 2645 | |
| 2646 | str[col] = old_char; |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * Expand the command line "str" from context "xp". |
| 2651 | * "xp" must have been set by set_cmd_context(). |
| 2652 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 2653 | * starts. |
| 2654 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 2655 | * cursor. |
| 2656 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 2657 | * key that triggered expansion literally. |
| 2658 | * Returns EXPAND_OK otherwise. |
| 2659 | */ |
| 2660 | int |
| 2661 | expand_cmdline( |
| 2662 | expand_T *xp, |
| 2663 | char_u *str, // start of command line |
| 2664 | int col, // position of cursor |
| 2665 | int *matchcount, // return: nr of matches |
| 2666 | char_u ***matches) // return: array of pointers to matches |
| 2667 | { |
| 2668 | char_u *file_str = NULL; |
| 2669 | int options = WILD_ADD_SLASH|WILD_SILENT; |
| 2670 | |
| 2671 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 2672 | { |
| 2673 | beep_flush(); |
| 2674 | return EXPAND_UNSUCCESSFUL; // Something illegal on command line |
| 2675 | } |
| 2676 | if (xp->xp_context == EXPAND_NOTHING) |
| 2677 | { |
| 2678 | // Caller can use the character as a normal char instead |
| 2679 | return EXPAND_NOTHING; |
| 2680 | } |
| 2681 | |
| 2682 | // add star to file name, or convert to regexp if not exp. files. |
| 2683 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 2684 | if (cmdline_fuzzy_completion_supported(xp)) |
| 2685 | // If fuzzy matching, don't modify the search string |
| 2686 | file_str = vim_strsave(xp->xp_pattern); |
| 2687 | else |
| 2688 | { |
| 2689 | file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
| 2690 | if (file_str == NULL) |
| 2691 | return EXPAND_UNSUCCESSFUL; |
| 2692 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2693 | |
| 2694 | if (p_wic) |
| 2695 | options += WILD_ICASE; |
| 2696 | |
| 2697 | // find all files that match the description |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2698 | if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2699 | { |
| 2700 | *matchcount = 0; |
| 2701 | *matches = NULL; |
| 2702 | } |
| 2703 | vim_free(file_str); |
| 2704 | |
| 2705 | return EXPAND_OK; |
| 2706 | } |
| 2707 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2708 | /* |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2709 | * Expand file or directory names. |
Bram Moolenaar | 747f110 | 2022-09-18 13:06:41 +0100 | [diff] [blame] | 2710 | * Returns OK or FAIL. |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2711 | */ |
| 2712 | static int |
| 2713 | expand_files_and_dirs( |
| 2714 | expand_T *xp, |
| 2715 | char_u *pat, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2716 | char_u ***matches, |
| 2717 | int *numMatches, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2718 | int flags, |
| 2719 | int options) |
| 2720 | { |
| 2721 | int free_pat = FALSE; |
| 2722 | int i; |
| 2723 | int ret; |
| 2724 | |
| 2725 | // for ":set path=" and ":set tags=" halve backslashes for escaped |
| 2726 | // space |
| 2727 | if (xp->xp_backslash != XP_BS_NONE) |
| 2728 | { |
| 2729 | free_pat = TRUE; |
| 2730 | pat = vim_strsave(pat); |
| 2731 | for (i = 0; pat[i]; ++i) |
| 2732 | if (pat[i] == '\\') |
| 2733 | { |
| 2734 | if (xp->xp_backslash == XP_BS_THREE |
| 2735 | && pat[i + 1] == '\\' |
| 2736 | && pat[i + 2] == '\\' |
| 2737 | && pat[i + 3] == ' ') |
| 2738 | STRMOVE(pat + i, pat + i + 3); |
| 2739 | if (xp->xp_backslash == XP_BS_ONE |
| 2740 | && pat[i + 1] == ' ') |
| 2741 | STRMOVE(pat + i, pat + i + 1); |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | if (xp->xp_context == EXPAND_FILES) |
| 2746 | flags |= EW_FILE; |
| 2747 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 2748 | flags |= (EW_FILE | EW_PATH); |
| 2749 | else |
| 2750 | flags = (flags | EW_DIR) & ~EW_FILE; |
| 2751 | if (options & WILD_ICASE) |
| 2752 | flags |= EW_ICASE; |
| 2753 | |
| 2754 | // Expand wildcards, supporting %:h and the like. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2755 | ret = expand_wildcards_eval(&pat, numMatches, matches, flags); |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2756 | if (free_pat) |
| 2757 | vim_free(pat); |
| 2758 | #ifdef BACKSLASH_IN_FILENAME |
| 2759 | if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0) |
| 2760 | { |
| 2761 | int j; |
| 2762 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2763 | for (j = 0; j < *numMatches; ++j) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2764 | { |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2765 | char_u *ptr = (*matches)[j]; |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2766 | |
| 2767 | while (*ptr != NUL) |
| 2768 | { |
| 2769 | if (p_csl[0] == 's' && *ptr == '\\') |
| 2770 | *ptr = '/'; |
| 2771 | else if (p_csl[0] == 'b' && *ptr == '/') |
| 2772 | *ptr = '\\'; |
| 2773 | ptr += (*mb_ptr2len)(ptr); |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | #endif |
| 2778 | return ret; |
| 2779 | } |
| 2780 | |
| 2781 | /* |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2782 | * Function given to ExpandGeneric() to obtain the possible arguments of the |
| 2783 | * ":behave {mswin,xterm}" command. |
| 2784 | */ |
| 2785 | static char_u * |
| 2786 | get_behave_arg(expand_T *xp UNUSED, int idx) |
| 2787 | { |
| 2788 | if (idx == 0) |
| 2789 | return (char_u *)"mswin"; |
| 2790 | if (idx == 1) |
| 2791 | return (char_u *)"xterm"; |
| 2792 | return NULL; |
| 2793 | } |
| 2794 | |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 2795 | #ifdef FEAT_EVAL |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2796 | /* |
| 2797 | * Function given to ExpandGeneric() to obtain the possible arguments of the |
| 2798 | * ":breakadd {expr, file, func, here}" command. |
| 2799 | * ":breakdel {func, file, here}" command. |
| 2800 | */ |
| 2801 | static char_u * |
| 2802 | get_breakadd_arg(expand_T *xp UNUSED, int idx) |
| 2803 | { |
| 2804 | char *opts[] = {"expr", "file", "func", "here"}; |
| 2805 | |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 2806 | if (idx >= 0 && idx <= 3) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2807 | { |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 2808 | // breakadd {expr, file, func, here} |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2809 | if (breakpt_expand_what == EXP_BREAKPT_ADD) |
| 2810 | return (char_u *)opts[idx]; |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 2811 | else if (breakpt_expand_what == EXP_BREAKPT_DEL) |
| 2812 | { |
| 2813 | // breakdel {func, file, here} |
| 2814 | if (idx <= 2) |
| 2815 | return (char_u *)opts[idx + 1]; |
| 2816 | } |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2817 | else |
| 2818 | { |
Yegappan Lakshmanan | 1fdf84e | 2022-03-15 10:53:09 +0000 | [diff] [blame] | 2819 | // profdel {func, file} |
| 2820 | if (idx <= 1) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2821 | return (char_u *)opts[idx + 1]; |
| 2822 | } |
| 2823 | } |
| 2824 | return NULL; |
| 2825 | } |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* |
| 2828 | * Function given to ExpandGeneric() to obtain the possible arguments for the |
| 2829 | * ":scriptnames" command. |
| 2830 | */ |
| 2831 | static char_u * |
| 2832 | get_scriptnames_arg(expand_T *xp UNUSED, int idx) |
| 2833 | { |
| 2834 | scriptitem_T *si; |
| 2835 | |
| 2836 | if (!SCRIPT_ID_VALID(idx + 1)) |
| 2837 | return NULL; |
| 2838 | |
| 2839 | si = SCRIPT_ITEM(idx + 1); |
| 2840 | home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE); |
| 2841 | return NameBuff; |
| 2842 | } |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2843 | #endif |
| 2844 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 2845 | /* |
| 2846 | * Function given to ExpandGeneric() to obtain the possible arguments of the |
| 2847 | * ":messages {clear}" command. |
| 2848 | */ |
| 2849 | static char_u * |
| 2850 | get_messages_arg(expand_T *xp UNUSED, int idx) |
| 2851 | { |
| 2852 | if (idx == 0) |
| 2853 | return (char_u *)"clear"; |
| 2854 | return NULL; |
| 2855 | } |
| 2856 | |
| 2857 | static char_u * |
| 2858 | get_mapclear_arg(expand_T *xp UNUSED, int idx) |
| 2859 | { |
| 2860 | if (idx == 0) |
| 2861 | return (char_u *)"<buffer>"; |
| 2862 | return NULL; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2866 | * Do the expansion based on xp->xp_context and 'rmp'. |
| 2867 | */ |
| 2868 | static int |
| 2869 | ExpandOther( |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 2870 | char_u *pat, |
Bram Moolenaar | 29ab6ce | 2022-02-26 15:52:08 +0000 | [diff] [blame] | 2871 | expand_T *xp, |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2872 | regmatch_T *rmp, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2873 | char_u ***matches, |
| 2874 | int *numMatches) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2875 | { |
| 2876 | static struct expgen |
| 2877 | { |
| 2878 | int context; |
| 2879 | char_u *((*func)(expand_T *, int)); |
| 2880 | int ic; |
| 2881 | int escaped; |
| 2882 | } tab[] = |
| 2883 | { |
| 2884 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 2885 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
| 2886 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
| 2887 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
| 2888 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 2889 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
| 2890 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
| 2891 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 2892 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 2893 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2894 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2895 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 2896 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 2897 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 2898 | {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE}, |
| 2899 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2900 | #endif |
| 2901 | #ifdef FEAT_MENU |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2902 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 2903 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2904 | #endif |
| 2905 | #ifdef FEAT_SYN_HL |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2906 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2907 | #endif |
| 2908 | #ifdef FEAT_PROFILE |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2909 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2910 | #endif |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2911 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
| 2912 | {EXPAND_EVENTS, get_event_name, TRUE, FALSE}, |
| 2913 | {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2914 | #ifdef FEAT_CSCOPE |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2915 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2916 | #endif |
| 2917 | #ifdef FEAT_SIGNS |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2918 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2919 | #endif |
| 2920 | #ifdef FEAT_PROFILE |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2921 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2922 | #endif |
| 2923 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2924 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 2925 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2926 | #endif |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2927 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
| 2928 | {EXPAND_USER, get_users, TRUE, FALSE}, |
| 2929 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2930 | #ifdef FEAT_EVAL |
| 2931 | {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE}, |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 2932 | {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE}, |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 2933 | #endif |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2934 | }; |
| 2935 | int i; |
| 2936 | int ret = FAIL; |
| 2937 | |
| 2938 | // Find a context in the table and call the ExpandGeneric() with the |
| 2939 | // right function to do the expansion. |
| 2940 | for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i) |
| 2941 | { |
| 2942 | if (xp->xp_context == tab[i].context) |
| 2943 | { |
| 2944 | if (tab[i].ic) |
| 2945 | rmp->rm_ic = TRUE; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 2946 | ret = ExpandGeneric(pat, xp, rmp, matches, numMatches, |
| 2947 | tab[i].func, tab[i].escaped); |
Yegappan Lakshmanan | 620d8ed | 2022-02-12 12:03:07 +0000 | [diff] [blame] | 2948 | break; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | return ret; |
| 2953 | } |
| 2954 | |
| 2955 | /* |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 2956 | * Map wild expand options to flags for expand_wildcards() |
| 2957 | */ |
| 2958 | static int |
| 2959 | map_wildopts_to_ewflags(int options) |
| 2960 | { |
| 2961 | int flags; |
| 2962 | |
| 2963 | flags = EW_DIR; // include directories |
| 2964 | if (options & WILD_LIST_NOTFOUND) |
| 2965 | flags |= EW_NOTFOUND; |
| 2966 | if (options & WILD_ADD_SLASH) |
| 2967 | flags |= EW_ADDSLASH; |
| 2968 | if (options & WILD_KEEP_ALL) |
| 2969 | flags |= EW_KEEPALL; |
| 2970 | if (options & WILD_SILENT) |
| 2971 | flags |= EW_SILENT; |
| 2972 | if (options & WILD_NOERROR) |
| 2973 | flags |= EW_NOERROR; |
| 2974 | if (options & WILD_ALLLINKS) |
| 2975 | flags |= EW_ALLLINKS; |
| 2976 | |
| 2977 | return flags; |
| 2978 | } |
| 2979 | |
| 2980 | /* |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2981 | * Do the expansion based on xp->xp_context and "pat". |
| 2982 | */ |
| 2983 | static int |
| 2984 | ExpandFromContext( |
| 2985 | expand_T *xp, |
| 2986 | char_u *pat, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 2987 | char_u ***matches, |
| 2988 | int *numMatches, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2989 | int options) // WILD_ flags |
| 2990 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2991 | regmatch_T regmatch; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2992 | int ret; |
| 2993 | int flags; |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 2994 | char_u *tofree = NULL; |
Yegappan Lakshmanan | 00333cb | 2022-02-26 16:05:08 +0000 | [diff] [blame] | 2995 | int fuzzy = cmdline_fuzzy_complete(pat) |
| 2996 | && cmdline_fuzzy_completion_supported(xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2997 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 2998 | flags = map_wildopts_to_ewflags(options); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2999 | |
| 3000 | if (xp->xp_context == EXPAND_FILES |
| 3001 | || xp->xp_context == EXPAND_DIRECTORIES |
| 3002 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3003 | return expand_files_and_dirs(xp, pat, matches, numMatches, flags, |
| 3004 | options); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3005 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3006 | *matches = (char_u **)""; |
| 3007 | *numMatches = 0; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3008 | if (xp->xp_context == EXPAND_HELP) |
| 3009 | { |
| 3010 | // With an empty argument we would get all the help tags, which is |
| 3011 | // very slow. Get matches for "help" instead. |
| 3012 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3013 | numMatches, matches, FALSE) == OK) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3014 | { |
| 3015 | #ifdef FEAT_MULTI_LANG |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3016 | cleanup_help_tags(*numMatches, *matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3017 | #endif |
| 3018 | return OK; |
| 3019 | } |
| 3020 | return FAIL; |
| 3021 | } |
| 3022 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3023 | if (xp->xp_context == EXPAND_SHELLCMD) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3024 | return expand_shellcmd(pat, matches, numMatches, flags); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3025 | if (xp->xp_context == EXPAND_OLD_SETTING) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3026 | return ExpandOldSetting(numMatches, matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3027 | if (xp->xp_context == EXPAND_BUFFERS) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3028 | return ExpandBufnames(pat, numMatches, matches, options); |
Bram Moolenaar | ae7dba8 | 2019-12-29 13:56:33 +0100 | [diff] [blame] | 3029 | #ifdef FEAT_DIFF |
| 3030 | if (xp->xp_context == EXPAND_DIFF_BUFFERS) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3031 | return ExpandBufnames(pat, numMatches, matches, |
| 3032 | options | BUF_DIFF_FILTER); |
Bram Moolenaar | ae7dba8 | 2019-12-29 13:56:33 +0100 | [diff] [blame] | 3033 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3034 | if (xp->xp_context == EXPAND_TAGS |
| 3035 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3036 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches, |
| 3037 | matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3038 | if (xp->xp_context == EXPAND_COLORS) |
| 3039 | { |
| 3040 | char *directories[] = {"colors", NULL}; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3041 | return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3042 | directories); |
| 3043 | } |
| 3044 | if (xp->xp_context == EXPAND_COMPILER) |
| 3045 | { |
| 3046 | char *directories[] = {"compiler", NULL}; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3047 | return ExpandRTDir(pat, 0, numMatches, matches, directories); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3048 | } |
| 3049 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
| 3050 | { |
| 3051 | char *directories[] = {"syntax", NULL}; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3052 | return ExpandRTDir(pat, 0, numMatches, matches, directories); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3053 | } |
| 3054 | if (xp->xp_context == EXPAND_FILETYPE) |
| 3055 | { |
| 3056 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3057 | return ExpandRTDir(pat, 0, numMatches, matches, directories); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3058 | } |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3059 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3060 | if (xp->xp_context == EXPAND_USER_LIST) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3061 | return ExpandUserList(xp, matches, numMatches); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3062 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3063 | if (xp->xp_context == EXPAND_PACKADD) |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3064 | return ExpandPackAddDir(pat, numMatches, matches); |
zeertzjq | 5c8771b | 2023-01-24 12:34:03 +0000 | [diff] [blame] | 3065 | if (xp->xp_context == EXPAND_RUNTIME) |
| 3066 | return expand_runtime_cmd(pat, numMatches, matches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3067 | |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 3068 | // When expanding a function name starting with s:, match the <SNR>nr_ |
| 3069 | // prefix. |
Bram Moolenaar | 47016f5 | 2021-08-26 16:39:58 +0200 | [diff] [blame] | 3070 | if ((xp->xp_context == EXPAND_USER_FUNC |
| 3071 | || xp->xp_context == EXPAND_DISASSEMBLE) |
| 3072 | && STRNCMP(pat, "^s:", 3) == 0) |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 3073 | { |
| 3074 | int len = (int)STRLEN(pat) + 20; |
| 3075 | |
| 3076 | tofree = alloc(len); |
Yegappan Lakshmanan | e3846cf | 2022-02-15 11:35:54 +0000 | [diff] [blame] | 3077 | if (tofree == NULL) |
| 3078 | return FAIL; |
Bram Moolenaar | b54b8e0 | 2020-03-01 01:05:53 +0100 | [diff] [blame] | 3079 | vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3); |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 3080 | pat = tofree; |
| 3081 | } |
| 3082 | |
Yegappan Lakshmanan | 00333cb | 2022-02-26 16:05:08 +0000 | [diff] [blame] | 3083 | if (!fuzzy) |
| 3084 | { |
| 3085 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
| 3086 | if (regmatch.regprog == NULL) |
| 3087 | return FAIL; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3088 | |
Yegappan Lakshmanan | 00333cb | 2022-02-26 16:05:08 +0000 | [diff] [blame] | 3089 | // set ignore-case according to p_ic, p_scs and pat |
| 3090 | regmatch.rm_ic = ignorecase(pat); |
| 3091 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3092 | |
| 3093 | if (xp->xp_context == EXPAND_SETTINGS |
| 3094 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
Christian Brabandt | cb74789 | 2022-05-08 21:10:56 +0100 | [diff] [blame] | 3095 | ret = ExpandSettings(xp, ®match, pat, numMatches, matches, fuzzy); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3096 | else if (xp->xp_context == EXPAND_MAPPINGS) |
Yegappan Lakshmanan | 6caeda2 | 2022-02-27 12:07:30 +0000 | [diff] [blame] | 3097 | ret = ExpandMappings(pat, ®match, numMatches, matches); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3098 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3099 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3100 | ret = ExpandUserDefined(pat, xp, ®match, matches, numMatches); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3101 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3102 | else |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 3103 | ret = ExpandOther(pat, xp, ®match, matches, numMatches); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3104 | |
Yegappan Lakshmanan | 00333cb | 2022-02-26 16:05:08 +0000 | [diff] [blame] | 3105 | if (!fuzzy) |
| 3106 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 3107 | vim_free(tofree); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3108 | |
| 3109 | return ret; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3110 | } |
| 3111 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3112 | /* |
| 3113 | * Expand a list of names. |
| 3114 | * |
| 3115 | * Generic function for command line completion. It calls a function to |
| 3116 | * obtain strings, one by one. The strings are matched against a regexp |
| 3117 | * program. Matching strings are copied into an array, which is returned. |
| 3118 | * |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 3119 | * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching |
| 3120 | * is used. |
| 3121 | * |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3122 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 3123 | */ |
Bram Moolenaar | 61d7c0d | 2020-01-05 14:38:40 +0100 | [diff] [blame] | 3124 | static int |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3125 | ExpandGeneric( |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3126 | char_u *pat, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3127 | expand_T *xp, |
| 3128 | regmatch_T *regmatch, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3129 | char_u ***matches, |
| 3130 | int *numMatches, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3131 | char_u *((*func)(expand_T *, int)), |
| 3132 | // returns a string from the list |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3133 | int escaped) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3134 | { |
| 3135 | int i; |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3136 | garray_T ga; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3137 | char_u *str; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 3138 | fuzmatch_str_T *fuzmatch = NULL; |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3139 | int score = 0; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3140 | int fuzzy; |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 3141 | int match; |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 3142 | int sort_matches = FALSE; |
| 3143 | int funcsort = FALSE; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3144 | |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3145 | fuzzy = cmdline_fuzzy_complete(pat); |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3146 | *matches = NULL; |
| 3147 | *numMatches = 0; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3148 | |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3149 | if (!fuzzy) |
| 3150 | ga_init2(&ga, sizeof(char *), 30); |
| 3151 | else |
| 3152 | ga_init2(&ga, sizeof(fuzmatch_str_T), 30); |
| 3153 | |
| 3154 | for (i = 0; ; ++i) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3155 | { |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3156 | str = (*func)(xp, i); |
| 3157 | if (str == NULL) // end of list |
| 3158 | break; |
| 3159 | if (*str == NUL) // skip empty strings |
| 3160 | continue; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3161 | |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3162 | if (xp->xp_pattern[0] != NUL) |
| 3163 | { |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 3164 | if (!fuzzy) |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3165 | match = vim_regexec(regmatch, str, (colnr_T)0); |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 3166 | else |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3167 | { |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3168 | score = fuzzy_match_str(str, pat); |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 3169 | match = (score != 0); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3170 | } |
| 3171 | } |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3172 | else |
| 3173 | match = TRUE; |
| 3174 | |
| 3175 | if (!match) |
| 3176 | continue; |
| 3177 | |
| 3178 | if (escaped) |
| 3179 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 3180 | else |
| 3181 | str = vim_strsave(str); |
| 3182 | if (str == NULL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3183 | { |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3184 | if (!fuzzy) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3185 | { |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3186 | ga_clear_strings(&ga); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3187 | return FAIL; |
| 3188 | } |
Bram Moolenaar | c6e0a5e | 2022-04-10 18:09:06 +0100 | [diff] [blame] | 3189 | fuzmatch_str_free(ga.ga_data, ga.ga_len); |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3190 | return FAIL; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3191 | } |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3192 | |
| 3193 | if (ga_grow(&ga, 1) == FAIL) |
| 3194 | { |
| 3195 | vim_free(str); |
| 3196 | break; |
| 3197 | } |
| 3198 | |
| 3199 | if (fuzzy) |
| 3200 | { |
| 3201 | fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len]; |
| 3202 | fuzmatch->idx = ga.ga_len; |
| 3203 | fuzmatch->str = str; |
| 3204 | fuzmatch->score = score; |
| 3205 | } |
| 3206 | else |
| 3207 | ((char_u **)ga.ga_data)[ga.ga_len] = str; |
| 3208 | |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3209 | #ifdef FEAT_MENU |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3210 | if (func == get_menu_names) |
| 3211 | { |
| 3212 | // test for separator added by get_menu_names() |
| 3213 | str += STRLEN(str) - 1; |
| 3214 | if (*str == '\001') |
| 3215 | *str = '.'; |
| 3216 | } |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3217 | #endif |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3218 | |
| 3219 | ++ga.ga_len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3222 | if (ga.ga_len == 0) |
| 3223 | return OK; |
| 3224 | |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 3225 | // sort the matches when using regular expression matching and sorting |
| 3226 | // applies to the completion context. Menus and scriptnames should be kept |
| 3227 | // in the specified order. |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3228 | if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 3229 | && xp->xp_context != EXPAND_MENUS |
| 3230 | && xp->xp_context != EXPAND_SCRIPTNAMES) |
| 3231 | sort_matches = TRUE; |
| 3232 | |
| 3233 | // <SNR> functions should be sorted to the end. |
| 3234 | if (xp->xp_context == EXPAND_EXPRESSION |
| 3235 | || xp->xp_context == EXPAND_FUNCTIONS |
| 3236 | || xp->xp_context == EXPAND_USER_FUNC |
| 3237 | || xp->xp_context == EXPAND_DISASSEMBLE) |
| 3238 | funcsort = TRUE; |
| 3239 | |
| 3240 | // Sort the matches. |
| 3241 | if (sort_matches) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3242 | { |
Yegappan Lakshmanan | 454ce67 | 2022-03-24 11:22:13 +0000 | [diff] [blame] | 3243 | if (funcsort) |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3244 | // <SNR> functions should be sorted to the end. |
| 3245 | qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *), |
| 3246 | sort_func_compare); |
| 3247 | else |
| 3248 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
| 3249 | } |
| 3250 | |
| 3251 | if (!fuzzy) |
| 3252 | { |
| 3253 | *matches = ga.ga_data; |
| 3254 | *numMatches = ga.ga_len; |
| 3255 | } |
| 3256 | else |
| 3257 | { |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3258 | if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len, |
| 3259 | funcsort) == FAIL) |
| 3260 | return FAIL; |
| 3261 | *numMatches = ga.ga_len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 3264 | #if defined(FEAT_SYN_HL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3265 | // Reset the variables used for special highlight names expansion, so that |
| 3266 | // they don't show up when getting normal highlight names by ID. |
| 3267 | reset_expand_highlight(); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 3268 | #endif |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 3269 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3270 | return OK; |
| 3271 | } |
| 3272 | |
| 3273 | /* |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3274 | * Expand shell command matches in one directory of $PATH. |
| 3275 | */ |
| 3276 | static void |
| 3277 | expand_shellcmd_onedir( |
| 3278 | char_u *buf, |
| 3279 | char_u *s, |
| 3280 | size_t l, |
| 3281 | char_u *pat, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3282 | char_u ***matches, |
| 3283 | int *numMatches, |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3284 | int flags, |
| 3285 | hashtab_T *ht, |
| 3286 | garray_T *gap) |
| 3287 | { |
| 3288 | int ret; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3289 | hash_T hash; |
| 3290 | hashitem_T *hi; |
| 3291 | |
| 3292 | vim_strncpy(buf, s, l); |
| 3293 | add_pathsep(buf); |
| 3294 | l = STRLEN(buf); |
| 3295 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 3296 | |
| 3297 | // Expand matches in one directory of $PATH. |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3298 | ret = expand_wildcards(1, &buf, numMatches, matches, flags); |
Yegappan Lakshmanan | 68353e5 | 2022-11-13 22:38:10 +0000 | [diff] [blame] | 3299 | if (ret != OK) |
| 3300 | return; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3301 | |
Yegappan Lakshmanan | 68353e5 | 2022-11-13 22:38:10 +0000 | [diff] [blame] | 3302 | if (ga_grow(gap, *numMatches) == FAIL) |
| 3303 | { |
| 3304 | FreeWild(*numMatches, *matches); |
| 3305 | return; |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3306 | } |
Yegappan Lakshmanan | 68353e5 | 2022-11-13 22:38:10 +0000 | [diff] [blame] | 3307 | |
| 3308 | for (int i = 0; i < *numMatches; ++i) |
| 3309 | { |
| 3310 | char_u *name = (*matches)[i]; |
| 3311 | |
| 3312 | if (STRLEN(name) > l) |
| 3313 | { |
| 3314 | // Check if this name was already found. |
| 3315 | hash = hash_hash(name + l); |
| 3316 | hi = hash_lookup(ht, name + l, hash); |
| 3317 | if (HASHITEM_EMPTY(hi)) |
| 3318 | { |
| 3319 | // Remove the path that was prepended. |
| 3320 | STRMOVE(name, name + l); |
| 3321 | ((char_u **)gap->ga_data)[gap->ga_len++] = name; |
| 3322 | hash_add_item(ht, hi, name, hash); |
| 3323 | name = NULL; |
| 3324 | } |
| 3325 | } |
| 3326 | vim_free(name); |
| 3327 | } |
| 3328 | vim_free(*matches); |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | /* |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3332 | * Complete a shell command. |
| 3333 | * Returns FAIL or OK; |
| 3334 | */ |
| 3335 | static int |
| 3336 | expand_shellcmd( |
| 3337 | char_u *filepat, // pattern to match with command names |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3338 | char_u ***matches, // return: array with matches |
| 3339 | int *numMatches, // return: number of matches |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3340 | int flagsarg) // EW_ flags |
| 3341 | { |
| 3342 | char_u *pat; |
| 3343 | int i; |
| 3344 | char_u *path = NULL; |
| 3345 | int mustfree = FALSE; |
| 3346 | garray_T ga; |
Bram Moolenaar | 8b7aa2f | 2020-01-07 21:05:49 +0100 | [diff] [blame] | 3347 | char_u *buf; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3348 | size_t l; |
| 3349 | char_u *s, *e; |
| 3350 | int flags = flagsarg; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3351 | int did_curdir = FALSE; |
| 3352 | hashtab_T found_ht; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3353 | |
Bram Moolenaar | 8b7aa2f | 2020-01-07 21:05:49 +0100 | [diff] [blame] | 3354 | buf = alloc(MAXPATHL); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3355 | if (buf == NULL) |
| 3356 | return FAIL; |
| 3357 | |
Bram Moolenaar | 8b7aa2f | 2020-01-07 21:05:49 +0100 | [diff] [blame] | 3358 | // for ":set path=" and ":set tags=" halve backslashes for escaped space |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3359 | pat = vim_strsave(filepat); |
Bram Moolenaar | 8b7aa2f | 2020-01-07 21:05:49 +0100 | [diff] [blame] | 3360 | if (pat == NULL) |
| 3361 | { |
| 3362 | vim_free(buf); |
| 3363 | return FAIL; |
| 3364 | } |
| 3365 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3366 | for (i = 0; pat[i]; ++i) |
| 3367 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
| 3368 | STRMOVE(pat + i, pat + i + 1); |
| 3369 | |
| 3370 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
| 3371 | |
| 3372 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 3373 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
| 3374 | path = (char_u *)"."; |
| 3375 | else |
| 3376 | { |
| 3377 | // For an absolute name we don't use $PATH. |
| 3378 | if (!mch_isFullName(pat)) |
| 3379 | path = vim_getenv((char_u *)"PATH", &mustfree); |
| 3380 | if (path == NULL) |
| 3381 | path = (char_u *)""; |
| 3382 | } |
| 3383 | |
| 3384 | // Go over all directories in $PATH. Expand matches in that directory and |
| 3385 | // collect them in "ga". When "." is not in $PATH also expand for the |
| 3386 | // current directory, to find "subdir/cmd". |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 3387 | ga_init2(&ga, sizeof(char *), 10); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3388 | hash_init(&found_ht); |
| 3389 | for (s = path; ; s = e) |
| 3390 | { |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3391 | #if defined(MSWIN) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3392 | e = vim_strchr(s, ';'); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3393 | #else |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3394 | e = vim_strchr(s, ':'); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3395 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3396 | if (e == NULL) |
| 3397 | e = s + STRLEN(s); |
| 3398 | |
| 3399 | if (*s == NUL) |
| 3400 | { |
| 3401 | if (did_curdir) |
| 3402 | break; |
| 3403 | // Find directories in the current directory, path is empty. |
| 3404 | did_curdir = TRUE; |
| 3405 | flags |= EW_DIR; |
| 3406 | } |
| 3407 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 3408 | { |
| 3409 | did_curdir = TRUE; |
| 3410 | flags |= EW_DIR; |
| 3411 | } |
| 3412 | else |
| 3413 | // Do not match directories inside a $PATH item. |
| 3414 | flags &= ~EW_DIR; |
| 3415 | |
| 3416 | l = e - s; |
| 3417 | if (l > MAXPATHL - 5) |
| 3418 | break; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3419 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3420 | expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags, |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3421 | &found_ht, &ga); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3422 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3423 | if (*e != NUL) |
| 3424 | ++e; |
| 3425 | } |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3426 | *matches = ga.ga_data; |
| 3427 | *numMatches = ga.ga_len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3428 | |
| 3429 | vim_free(buf); |
| 3430 | vim_free(pat); |
| 3431 | if (mustfree) |
| 3432 | vim_free(path); |
| 3433 | hash_clear(&found_ht); |
| 3434 | return OK; |
| 3435 | } |
| 3436 | |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3437 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3438 | /* |
| 3439 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
Bram Moolenaar | c841aff | 2020-07-25 14:11:55 +0200 | [diff] [blame] | 3440 | * return the result (either a string, a List or NULL). |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3441 | */ |
| 3442 | static void * |
| 3443 | call_user_expand_func( |
| 3444 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3445 | expand_T *xp) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3446 | { |
| 3447 | cmdline_info_T *ccline = get_cmdline_info(); |
| 3448 | int keep = 0; |
| 3449 | typval_T args[4]; |
| 3450 | sctx_T save_current_sctx = current_sctx; |
| 3451 | char_u *pat = NULL; |
| 3452 | void *ret; |
| 3453 | |
| 3454 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL) |
| 3455 | return NULL; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3456 | |
| 3457 | if (ccline->cmdbuff != NULL) |
| 3458 | { |
| 3459 | keep = ccline->cmdbuff[ccline->cmdlen]; |
| 3460 | ccline->cmdbuff[ccline->cmdlen] = 0; |
| 3461 | } |
| 3462 | |
| 3463 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 3464 | |
| 3465 | args[0].v_type = VAR_STRING; |
| 3466 | args[0].vval.v_string = pat; |
| 3467 | args[1].v_type = VAR_STRING; |
| 3468 | args[1].vval.v_string = xp->xp_line; |
| 3469 | args[2].v_type = VAR_NUMBER; |
| 3470 | args[2].vval.v_number = xp->xp_col; |
| 3471 | args[3].v_type = VAR_UNKNOWN; |
| 3472 | |
| 3473 | current_sctx = xp->xp_script_ctx; |
| 3474 | |
| 3475 | ret = user_expand_func(xp->xp_arg, 3, args); |
| 3476 | |
| 3477 | current_sctx = save_current_sctx; |
| 3478 | if (ccline->cmdbuff != NULL) |
| 3479 | ccline->cmdbuff[ccline->cmdlen] = keep; |
| 3480 | |
| 3481 | vim_free(pat); |
| 3482 | return ret; |
| 3483 | } |
| 3484 | |
| 3485 | /* |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3486 | * Expand names with a function defined by the user (EXPAND_USER_DEFINED and |
| 3487 | * EXPAND_USER_LIST). |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3488 | */ |
| 3489 | static int |
| 3490 | ExpandUserDefined( |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3491 | char_u *pat, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3492 | expand_T *xp, |
| 3493 | regmatch_T *regmatch, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3494 | char_u ***matches, |
| 3495 | int *numMatches) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3496 | { |
| 3497 | char_u *retstr; |
| 3498 | char_u *s; |
| 3499 | char_u *e; |
| 3500 | int keep; |
| 3501 | garray_T ga; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3502 | int fuzzy; |
| 3503 | int match; |
Bram Moolenaar | 3e7637b | 2022-02-28 21:02:19 +0000 | [diff] [blame] | 3504 | int score = 0; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3505 | |
| 3506 | fuzzy = cmdline_fuzzy_complete(pat); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3507 | *matches = NULL; |
| 3508 | *numMatches = 0; |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3509 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3510 | retstr = call_user_expand_func(call_func_retstr, xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3511 | if (retstr == NULL) |
| 3512 | return FAIL; |
| 3513 | |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3514 | if (!fuzzy) |
| 3515 | ga_init2(&ga, sizeof(char *), 3); |
| 3516 | else |
| 3517 | ga_init2(&ga, sizeof(fuzmatch_str_T), 3); |
| 3518 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3519 | for (s = retstr; *s != NUL; s = e) |
| 3520 | { |
| 3521 | e = vim_strchr(s, '\n'); |
| 3522 | if (e == NULL) |
| 3523 | e = s + STRLEN(s); |
| 3524 | keep = *e; |
| 3525 | *e = NUL; |
| 3526 | |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3527 | if (xp->xp_pattern[0] != NUL) |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3528 | { |
| 3529 | if (!fuzzy) |
| 3530 | match = vim_regexec(regmatch, s, (colnr_T)0); |
| 3531 | else |
| 3532 | { |
| 3533 | score = fuzzy_match_str(s, pat); |
| 3534 | match = (score != 0); |
| 3535 | } |
| 3536 | } |
| 3537 | else |
| 3538 | match = TRUE; // match everything |
| 3539 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3540 | *e = keep; |
| 3541 | |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3542 | if (match) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3543 | { |
| 3544 | if (ga_grow(&ga, 1) == FAIL) |
| 3545 | break; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3546 | if (!fuzzy) |
| 3547 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s); |
| 3548 | else |
| 3549 | { |
| 3550 | fuzmatch_str_T *fuzmatch = |
| 3551 | &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len]; |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3552 | fuzmatch->idx = ga.ga_len; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3553 | fuzmatch->str = vim_strnsave(s, e - s); |
| 3554 | fuzmatch->score = score; |
| 3555 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3556 | ++ga.ga_len; |
| 3557 | } |
| 3558 | |
| 3559 | if (*e != NUL) |
| 3560 | ++e; |
| 3561 | } |
| 3562 | vim_free(retstr); |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3563 | |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3564 | if (ga.ga_len == 0) |
| 3565 | return OK; |
| 3566 | |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3567 | if (!fuzzy) |
| 3568 | { |
| 3569 | *matches = ga.ga_data; |
| 3570 | *numMatches = ga.ga_len; |
| 3571 | } |
| 3572 | else |
| 3573 | { |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3574 | if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len, |
| 3575 | FALSE) == FAIL) |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3576 | return FAIL; |
Yegappan Lakshmanan | 5de4c43 | 2022-02-28 13:28:38 +0000 | [diff] [blame] | 3577 | *numMatches = ga.ga_len; |
Yegappan Lakshmanan | afd4ae3 | 2022-02-27 21:03:21 +0000 | [diff] [blame] | 3578 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3579 | return OK; |
| 3580 | } |
| 3581 | |
| 3582 | /* |
| 3583 | * Expand names with a list returned by a function defined by the user. |
| 3584 | */ |
| 3585 | static int |
| 3586 | ExpandUserList( |
| 3587 | expand_T *xp, |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3588 | char_u ***matches, |
| 3589 | int *numMatches) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3590 | { |
| 3591 | list_T *retlist; |
| 3592 | listitem_T *li; |
| 3593 | garray_T ga; |
| 3594 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3595 | *matches = NULL; |
| 3596 | *numMatches = 0; |
| 3597 | retlist = call_user_expand_func(call_func_retlist, xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3598 | if (retlist == NULL) |
| 3599 | return FAIL; |
| 3600 | |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 3601 | ga_init2(&ga, sizeof(char *), 3); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3602 | // Loop over the items in the list. |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3603 | FOR_ALL_LIST_ITEMS(retlist, li) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3604 | { |
| 3605 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 3606 | continue; // Skip non-string items and empty strings |
| 3607 | |
| 3608 | if (ga_grow(&ga, 1) == FAIL) |
| 3609 | break; |
| 3610 | |
| 3611 | ((char_u **)ga.ga_data)[ga.ga_len] = |
| 3612 | vim_strsave(li->li_tv.vval.v_string); |
| 3613 | ++ga.ga_len; |
| 3614 | } |
| 3615 | list_unref(retlist); |
| 3616 | |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3617 | *matches = ga.ga_data; |
| 3618 | *numMatches = ga.ga_len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3619 | return OK; |
| 3620 | } |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3621 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3622 | |
| 3623 | /* |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3624 | * Expand "file" for all comma-separated directories in "path". |
| 3625 | * Adds the matches to "ga". Caller must init "ga". |
zeertzjq | 3770f4c | 2023-01-22 18:38:51 +0000 | [diff] [blame] | 3626 | * If "dirs" is TRUE only expand directory names. |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3627 | */ |
| 3628 | void |
| 3629 | globpath( |
| 3630 | char_u *path, |
| 3631 | char_u *file, |
| 3632 | garray_T *ga, |
zeertzjq | 3770f4c | 2023-01-22 18:38:51 +0000 | [diff] [blame] | 3633 | int expand_options, |
| 3634 | int dirs) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3635 | { |
| 3636 | expand_T xpc; |
| 3637 | char_u *buf; |
| 3638 | int i; |
| 3639 | int num_p; |
| 3640 | char_u **p; |
| 3641 | |
| 3642 | buf = alloc(MAXPATHL); |
| 3643 | if (buf == NULL) |
| 3644 | return; |
| 3645 | |
| 3646 | ExpandInit(&xpc); |
zeertzjq | 3770f4c | 2023-01-22 18:38:51 +0000 | [diff] [blame] | 3647 | xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3648 | |
| 3649 | // Loop over all entries in {path}. |
| 3650 | while (*path != NUL) |
| 3651 | { |
| 3652 | // Copy one item of the path to buf[] and concatenate the file name. |
| 3653 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 3654 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 3655 | { |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3656 | #if defined(MSWIN) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3657 | // Using the platform's path separator (\) makes vim incorrectly |
| 3658 | // treat it as an escape character, use '/' instead. |
| 3659 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 3660 | STRCAT(buf, "/"); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3661 | #else |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3662 | add_pathsep(buf); |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3663 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3664 | STRCAT(buf, file); |
Yegappan Lakshmanan | 2438430 | 2022-02-17 11:26:42 +0000 | [diff] [blame] | 3665 | if (ExpandFromContext(&xpc, buf, &p, &num_p, |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3666 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
| 3667 | { |
| 3668 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
| 3669 | |
| 3670 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | f0f8055 | 2020-01-05 22:05:49 +0100 | [diff] [blame] | 3671 | // take over the pointers and put them in "ga" |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3672 | for (i = 0; i < num_p; ++i) |
| 3673 | { |
Bram Moolenaar | f0f8055 | 2020-01-05 22:05:49 +0100 | [diff] [blame] | 3674 | ((char_u **)ga->ga_data)[ga->ga_len] = p[i]; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3675 | ++ga->ga_len; |
| 3676 | } |
Bram Moolenaar | f0f8055 | 2020-01-05 22:05:49 +0100 | [diff] [blame] | 3677 | vim_free(p); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | vim_free(buf); |
| 3683 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3684 | |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3685 | /* |
| 3686 | * Translate some keys pressed when 'wildmenu' is used. |
| 3687 | */ |
| 3688 | int |
| 3689 | wildmenu_translate_key( |
| 3690 | cmdline_info_T *cclp, |
| 3691 | int key, |
| 3692 | expand_T *xp, |
| 3693 | int did_wild_list) |
| 3694 | { |
| 3695 | int c = key; |
| 3696 | |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 3697 | if (cmdline_pum_active()) |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 3698 | { |
Yegappan Lakshmanan | 560dff4 | 2022-02-10 19:52:10 +0000 | [diff] [blame] | 3699 | // When the popup menu is used for cmdline completion: |
| 3700 | // Up : go to the previous item in the menu |
| 3701 | // Down : go to the next item in the menu |
| 3702 | // Left : go to the parent directory |
| 3703 | // Right: list the files in the selected directory |
| 3704 | switch (c) |
| 3705 | { |
| 3706 | case K_UP: c = K_LEFT; break; |
| 3707 | case K_DOWN: c = K_RIGHT; break; |
| 3708 | case K_LEFT: c = K_UP; break; |
| 3709 | case K_RIGHT: c = K_DOWN; break; |
| 3710 | default: break; |
| 3711 | } |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 3712 | } |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 3713 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3714 | if (did_wild_list) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3715 | { |
| 3716 | if (c == K_LEFT) |
| 3717 | c = Ctrl_P; |
| 3718 | else if (c == K_RIGHT) |
| 3719 | c = Ctrl_N; |
| 3720 | } |
Yegappan Lakshmanan | 3908ef5 | 2022-02-08 12:08:07 +0000 | [diff] [blame] | 3721 | |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3722 | // Hitting CR after "emenu Name.": complete submenu |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3723 | if (xp->xp_context == EXPAND_MENUNAMES |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3724 | && cclp->cmdpos > 1 |
| 3725 | && cclp->cmdbuff[cclp->cmdpos - 1] == '.' |
| 3726 | && cclp->cmdbuff[cclp->cmdpos - 2] != '\\' |
| 3727 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 3728 | c = K_DOWN; |
| 3729 | |
| 3730 | return c; |
| 3731 | } |
| 3732 | |
| 3733 | /* |
| 3734 | * Delete characters on the command line, from "from" to the current |
| 3735 | * position. |
| 3736 | */ |
| 3737 | static void |
| 3738 | cmdline_del(cmdline_info_T *cclp, int from) |
| 3739 | { |
| 3740 | mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos, |
| 3741 | (size_t)(cclp->cmdlen - cclp->cmdpos + 1)); |
| 3742 | cclp->cmdlen -= cclp->cmdpos - from; |
| 3743 | cclp->cmdpos = from; |
| 3744 | } |
| 3745 | |
| 3746 | /* |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3747 | * Handle a key pressed when the wild menu for the menu names |
| 3748 | * (EXPAND_MENUNAMES) is displayed. |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3749 | */ |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3750 | static int |
| 3751 | wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3752 | { |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3753 | int i; |
| 3754 | int j; |
| 3755 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3756 | // Hitting <Down> after "emenu Name.": complete submenu |
| 3757 | if (key == K_DOWN && cclp->cmdpos > 0 |
| 3758 | && cclp->cmdbuff[cclp->cmdpos - 1] == '.') |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3759 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3760 | key = p_wc; |
| 3761 | KeyTyped = TRUE; // in case the key was mapped |
| 3762 | } |
| 3763 | else if (key == K_UP) |
| 3764 | { |
| 3765 | // Hitting <Up>: Remove one submenu name in front of the |
| 3766 | // cursor |
| 3767 | int found = FALSE; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3768 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3769 | j = (int)(xp->xp_pattern - cclp->cmdbuff); |
| 3770 | i = 0; |
| 3771 | while (--j > 0) |
| 3772 | { |
| 3773 | // check for start of menu name |
| 3774 | if (cclp->cmdbuff[j] == ' ' |
| 3775 | && cclp->cmdbuff[j - 1] != '\\') |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3776 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3777 | i = j + 1; |
| 3778 | break; |
| 3779 | } |
| 3780 | // check for start of submenu name |
| 3781 | if (cclp->cmdbuff[j] == '.' |
| 3782 | && cclp->cmdbuff[j - 1] != '\\') |
| 3783 | { |
| 3784 | if (found) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3785 | { |
| 3786 | i = j + 1; |
| 3787 | break; |
| 3788 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3789 | else |
| 3790 | found = TRUE; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3791 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3792 | } |
| 3793 | if (i > 0) |
| 3794 | cmdline_del(cclp, i); |
| 3795 | key = p_wc; |
| 3796 | KeyTyped = TRUE; // in case the key was mapped |
| 3797 | xp->xp_context = EXPAND_NOTHING; |
| 3798 | } |
| 3799 | |
| 3800 | return key; |
| 3801 | } |
| 3802 | |
| 3803 | /* |
| 3804 | * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or |
| 3805 | * directory names (EXPAND_DIRECTORIES) or shell command names |
| 3806 | * (EXPAND_SHELLCMD) is displayed. |
| 3807 | */ |
| 3808 | static int |
| 3809 | wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp) |
| 3810 | { |
| 3811 | int i; |
| 3812 | int j; |
| 3813 | char_u upseg[5]; |
| 3814 | |
| 3815 | upseg[0] = PATHSEP; |
| 3816 | upseg[1] = '.'; |
| 3817 | upseg[2] = '.'; |
| 3818 | upseg[3] = PATHSEP; |
| 3819 | upseg[4] = NUL; |
| 3820 | |
| 3821 | if (key == K_DOWN |
| 3822 | && cclp->cmdpos > 0 |
| 3823 | && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP |
| 3824 | && (cclp->cmdpos < 3 |
| 3825 | || cclp->cmdbuff[cclp->cmdpos - 2] != '.' |
| 3826 | || cclp->cmdbuff[cclp->cmdpos - 3] != '.')) |
| 3827 | { |
| 3828 | // go down a directory |
| 3829 | key = p_wc; |
| 3830 | KeyTyped = TRUE; // in case the key was mapped |
| 3831 | } |
| 3832 | else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN) |
| 3833 | { |
| 3834 | // If in a direct ancestor, strip off one ../ to go down |
| 3835 | int found = FALSE; |
| 3836 | |
| 3837 | j = cclp->cmdpos; |
| 3838 | i = (int)(xp->xp_pattern - cclp->cmdbuff); |
| 3839 | while (--j > i) |
| 3840 | { |
| 3841 | if (has_mbyte) |
| 3842 | j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j); |
| 3843 | if (vim_ispathsep(cclp->cmdbuff[j])) |
| 3844 | { |
| 3845 | found = TRUE; |
| 3846 | break; |
| 3847 | } |
| 3848 | } |
| 3849 | if (found |
| 3850 | && cclp->cmdbuff[j - 1] == '.' |
| 3851 | && cclp->cmdbuff[j - 2] == '.' |
| 3852 | && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2)) |
| 3853 | { |
| 3854 | cmdline_del(cclp, j - 2); |
| 3855 | key = p_wc; |
Bram Moolenaar | b0ac4ea | 2020-12-26 12:06:54 +0100 | [diff] [blame] | 3856 | KeyTyped = TRUE; // in case the key was mapped |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3857 | } |
| 3858 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3859 | else if (key == K_UP) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3860 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3861 | // go up a directory |
| 3862 | int found = FALSE; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3863 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3864 | j = cclp->cmdpos - 1; |
| 3865 | i = (int)(xp->xp_pattern - cclp->cmdbuff); |
| 3866 | while (--j > i) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3867 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3868 | if (has_mbyte) |
| 3869 | j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j); |
| 3870 | if (vim_ispathsep(cclp->cmdbuff[j]) |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3871 | #ifdef BACKSLASH_IN_FILENAME |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3872 | && vim_strchr((char_u *)" *?[{`$%#", |
| 3873 | cclp->cmdbuff[j + 1]) == NULL |
K.Takata | 161b6ac | 2022-11-14 15:31:07 +0000 | [diff] [blame] | 3874 | #endif |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3875 | ) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3876 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3877 | if (found) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3878 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3879 | i = j + 1; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3880 | break; |
| 3881 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3882 | else |
| 3883 | found = TRUE; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3884 | } |
| 3885 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3886 | |
| 3887 | if (!found) |
| 3888 | j = i; |
| 3889 | else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0) |
| 3890 | j += 4; |
| 3891 | else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0 |
| 3892 | && j == i) |
| 3893 | j += 3; |
| 3894 | else |
| 3895 | j = 0; |
| 3896 | if (j > 0) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3897 | { |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3898 | // TODO this is only for DOS/UNIX systems - need to put in |
| 3899 | // machine-specific stuff here and in upseg init |
| 3900 | cmdline_del(cclp, j); |
| 3901 | put_on_cmdline(upseg + 1, 3, FALSE); |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3902 | } |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3903 | else if (cclp->cmdpos > i) |
| 3904 | cmdline_del(cclp, i); |
| 3905 | |
| 3906 | // Now complete in the new directory. Set KeyTyped in case the |
| 3907 | // Up key came from a mapping. |
| 3908 | key = p_wc; |
| 3909 | KeyTyped = TRUE; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3910 | } |
| 3911 | |
Yegappan Lakshmanan | b31aec3 | 2022-02-16 12:44:29 +0000 | [diff] [blame] | 3912 | return key; |
| 3913 | } |
| 3914 | |
| 3915 | /* |
| 3916 | * Handle a key pressed when the wild menu is displayed |
| 3917 | */ |
| 3918 | int |
| 3919 | wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp) |
| 3920 | { |
| 3921 | if (xp->xp_context == EXPAND_MENUNAMES) |
| 3922 | return wildmenu_process_key_menunames(cclp, key, xp); |
| 3923 | else if ((xp->xp_context == EXPAND_FILES |
| 3924 | || xp->xp_context == EXPAND_DIRECTORIES |
| 3925 | || xp->xp_context == EXPAND_SHELLCMD)) |
| 3926 | return wildmenu_process_key_filenames(cclp, key, xp); |
| 3927 | |
| 3928 | return key; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /* |
| 3932 | * Free expanded names when finished walking through the matches |
| 3933 | */ |
| 3934 | void |
Bram Moolenaar | 58dcbf1 | 2022-08-26 19:58:49 +0100 | [diff] [blame] | 3935 | wildmenu_cleanup(cmdline_info_T *cclp UNUSED) |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3936 | { |
| 3937 | int skt = KeyTyped; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3938 | |
| 3939 | if (!p_wmnu || wild_menu_showing == 0) |
| 3940 | return; |
| 3941 | |
Bram Moolenaar | 58dcbf1 | 2022-08-26 19:58:49 +0100 | [diff] [blame] | 3942 | #ifdef FEAT_EVAL |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3943 | int save_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3944 | if (cclp->input_fn) |
| 3945 | RedrawingDisabled = 0; |
Bram Moolenaar | 58dcbf1 | 2022-08-26 19:58:49 +0100 | [diff] [blame] | 3946 | #endif |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3947 | |
| 3948 | if (wild_menu_showing == WM_SCROLLED) |
| 3949 | { |
| 3950 | // Entered command line, move it up |
| 3951 | cmdline_row--; |
| 3952 | redrawcmd(); |
| 3953 | } |
| 3954 | else if (save_p_ls != -1) |
| 3955 | { |
| 3956 | // restore 'laststatus' and 'winminheight' |
| 3957 | p_ls = save_p_ls; |
| 3958 | p_wmh = save_p_wmh; |
| 3959 | last_status(FALSE); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 3960 | update_screen(UPD_VALID); // redraw the screen NOW |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3961 | redrawcmd(); |
| 3962 | save_p_ls = -1; |
| 3963 | } |
| 3964 | else |
| 3965 | { |
| 3966 | win_redraw_last_status(topframe); |
| 3967 | redraw_statuslines(); |
| 3968 | } |
| 3969 | KeyTyped = skt; |
| 3970 | wild_menu_showing = 0; |
Bram Moolenaar | 58dcbf1 | 2022-08-26 19:58:49 +0100 | [diff] [blame] | 3971 | #ifdef FEAT_EVAL |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3972 | if (cclp->input_fn) |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 3973 | RedrawingDisabled = save_RedrawingDisabled; |
Bram Moolenaar | 58dcbf1 | 2022-08-26 19:58:49 +0100 | [diff] [blame] | 3974 | #endif |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3975 | } |
Bram Moolenaar | eadee48 | 2020-09-04 15:37:31 +0200 | [diff] [blame] | 3976 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 3977 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3978 | /* |
| 3979 | * "getcompletion()" function |
| 3980 | */ |
| 3981 | void |
| 3982 | f_getcompletion(typval_T *argvars, typval_T *rettv) |
| 3983 | { |
| 3984 | char_u *pat; |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 3985 | char_u *type; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 3986 | expand_T xpc; |
| 3987 | int filtered = FALSE; |
| 3988 | int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH |
Shougo Matsushita | ae38a9d | 2021-10-21 11:39:53 +0100 | [diff] [blame] | 3989 | | WILD_NO_BEEP | WILD_HOME_REPLACE; |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 3990 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 3991 | if (in_vim9script() |
| 3992 | && (check_for_string_arg(argvars, 0) == FAIL |
| 3993 | || check_for_string_arg(argvars, 1) == FAIL |
| 3994 | || check_for_opt_bool_arg(argvars, 2) == FAIL)) |
| 3995 | return; |
| 3996 | |
ii14 | 4785fe0 | 2021-11-21 12:13:56 +0000 | [diff] [blame] | 3997 | pat = tv_get_string(&argvars[0]); |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 3998 | if (check_for_string_arg(argvars, 1) == FAIL) |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 3999 | return; |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4000 | type = tv_get_string(&argvars[1]); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4001 | |
| 4002 | if (argvars[2].v_type != VAR_UNKNOWN) |
Bram Moolenaar | d217a87 | 2020-09-05 18:31:33 +0200 | [diff] [blame] | 4003 | filtered = tv_get_bool_chk(&argvars[2], NULL); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4004 | |
| 4005 | if (p_wic) |
| 4006 | options |= WILD_ICASE; |
| 4007 | |
| 4008 | // For filtered results, 'wildignore' is used |
| 4009 | if (!filtered) |
| 4010 | options |= WILD_KEEP_ALL; |
| 4011 | |
| 4012 | ExpandInit(&xpc); |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4013 | if (STRCMP(type, "cmdline") == 0) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4014 | { |
zeertzjq | e4c79d3 | 2023-08-15 22:41:53 +0200 | [diff] [blame] | 4015 | int cmdline_len = (int)STRLEN(pat); |
| 4016 | set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE); |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4017 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
zeertzjq | e4c79d3 | 2023-08-15 22:41:53 +0200 | [diff] [blame] | 4018 | xpc.xp_col = cmdline_len; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4019 | } |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4020 | else |
| 4021 | { |
ii14 | 4785fe0 | 2021-11-21 12:13:56 +0000 | [diff] [blame] | 4022 | xpc.xp_pattern = pat; |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4023 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
Yegappan Lakshmanan | d4e4ecb | 2023-08-27 18:35:45 +0200 | [diff] [blame] | 4024 | xpc.xp_line = pat; |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4025 | |
| 4026 | xpc.xp_context = cmdcomplete_str_to_type(type); |
| 4027 | if (xpc.xp_context == EXPAND_NOTHING) |
| 4028 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4029 | semsg(_(e_invalid_argument_str), type); |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4030 | return; |
| 4031 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4032 | |
Shougo Matsushita | 92997dd | 2023-08-20 20:55:55 +0200 | [diff] [blame] | 4033 | if (xpc.xp_context == EXPAND_USER_DEFINED) |
| 4034 | { |
Yegappan Lakshmanan | d4e4ecb | 2023-08-27 18:35:45 +0200 | [diff] [blame] | 4035 | // Must be "custom,funcname" pattern |
| 4036 | if (STRNCMP(type, "custom,", 7) != 0) |
| 4037 | { |
| 4038 | semsg(_(e_invalid_argument_str), type); |
| 4039 | return; |
| 4040 | } |
Shougo Matsushita | 92997dd | 2023-08-20 20:55:55 +0200 | [diff] [blame] | 4041 | |
Yegappan Lakshmanan | d4e4ecb | 2023-08-27 18:35:45 +0200 | [diff] [blame] | 4042 | xpc.xp_arg = type + 7; |
Shougo Matsushita | 92997dd | 2023-08-20 20:55:55 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | if (xpc.xp_context == EXPAND_USER_LIST) |
| 4046 | { |
Yegappan Lakshmanan | d4e4ecb | 2023-08-27 18:35:45 +0200 | [diff] [blame] | 4047 | // Must be "customlist,funcname" pattern |
| 4048 | if (STRNCMP(type, "customlist,", 11) != 0) |
| 4049 | { |
| 4050 | semsg(_(e_invalid_argument_str), type); |
| 4051 | return; |
| 4052 | } |
Shougo Matsushita | 92997dd | 2023-08-20 20:55:55 +0200 | [diff] [blame] | 4053 | |
Yegappan Lakshmanan | d4e4ecb | 2023-08-27 18:35:45 +0200 | [diff] [blame] | 4054 | xpc.xp_arg = type + 11; |
Shougo Matsushita | 92997dd | 2023-08-20 20:55:55 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4057 | # if defined(FEAT_MENU) |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4058 | if (xpc.xp_context == EXPAND_MENUS) |
| 4059 | { |
| 4060 | set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE); |
| 4061 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4062 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4063 | # endif |
| 4064 | # ifdef FEAT_CSCOPE |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4065 | if (xpc.xp_context == EXPAND_CSCOPE) |
| 4066 | { |
| 4067 | set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope); |
| 4068 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4069 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4070 | # endif |
| 4071 | # ifdef FEAT_SIGNS |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4072 | if (xpc.xp_context == EXPAND_SIGN) |
| 4073 | { |
| 4074 | set_context_in_sign_cmd(&xpc, xpc.xp_pattern); |
| 4075 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4076 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4077 | # endif |
zeertzjq | 3770f4c | 2023-01-22 18:38:51 +0000 | [diff] [blame] | 4078 | if (xpc.xp_context == EXPAND_RUNTIME) |
| 4079 | { |
| 4080 | set_context_in_runtime_cmd(&xpc, xpc.xp_pattern); |
| 4081 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4082 | } |
Bram Moolenaar | 1f1fd44 | 2020-06-07 18:45:14 +0200 | [diff] [blame] | 4083 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4084 | |
Yegappan Lakshmanan | e7dd0fa | 2022-03-22 16:06:31 +0000 | [diff] [blame] | 4085 | if (cmdline_fuzzy_completion_supported(&xpc)) |
| 4086 | // when fuzzy matching, don't modify the search string |
| 4087 | pat = vim_strsave(xpc.xp_pattern); |
| 4088 | else |
| 4089 | pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context); |
| 4090 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 4091 | if (rettv_list_alloc(rettv) == OK && pat != NULL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 4092 | { |
| 4093 | int i; |
| 4094 | |
| 4095 | ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP); |
| 4096 | |
| 4097 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 4098 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 4099 | } |
| 4100 | vim_free(pat); |
| 4101 | ExpandCleanup(&xpc); |
| 4102 | } |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 4103 | #endif // FEAT_EVAL |